2019-03-07 04:12:51 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Server-side rendering of the `core/search` block.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dynamically renders the `core/search` block.
|
|
|
|
*
|
|
|
|
* @param array $attributes The block attributes.
|
|
|
|
*
|
|
|
|
* @return string The search block markup.
|
|
|
|
*/
|
|
|
|
function render_block_core_search( $attributes ) {
|
|
|
|
static $instance_id = 0;
|
|
|
|
|
2020-07-14 07:39:09 -04:00
|
|
|
// Older versions of the Search block defaulted the label and buttonText
|
|
|
|
// attributes to `__( 'Search' )` meaning that many posts contain `<!--
|
|
|
|
// wp:search /-->`. Support these by defaulting an undefined label and
|
|
|
|
// buttonText to `__( 'Search' )`.
|
|
|
|
$attributes = wp_parse_args(
|
|
|
|
$attributes,
|
|
|
|
array(
|
|
|
|
'label' => __( 'Search' ),
|
|
|
|
'buttonText' => __( 'Search' ),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2021-11-08 09:29:21 -05:00
|
|
|
$input_id = 'wp-block-search__input-' . ++$instance_id;
|
|
|
|
$classnames = classnames_for_block_core_search( $attributes );
|
|
|
|
$show_label = ( ! empty( $attributes['showLabel'] ) ) ? true : false;
|
|
|
|
$use_icon_button = ( ! empty( $attributes['buttonUseIcon'] ) ) ? true : false;
|
|
|
|
$show_input = ( ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'] ) ? false : true;
|
|
|
|
$show_button = ( ! empty( $attributes['buttonPosition'] ) && 'no-button' === $attributes['buttonPosition'] ) ? false : true;
|
|
|
|
$input_markup = '';
|
|
|
|
$button_markup = '';
|
|
|
|
$inline_styles = styles_for_block_core_search( $attributes );
|
|
|
|
$color_classes = get_color_classes_for_block_core_search( $attributes );
|
|
|
|
$is_button_inside = ! empty( $attributes['buttonPosition'] ) &&
|
|
|
|
'button-inside' === $attributes['buttonPosition'];
|
|
|
|
// Border color classes need to be applied to the elements that have a border color.
|
|
|
|
$border_color_classes = get_border_color_classes_for_block_core_search( $attributes );
|
|
|
|
|
2022-02-17 14:18:25 -05:00
|
|
|
$label_inner_html = empty( $attributes['label'] ) ? __( 'Search' ) : wp_kses_post( $attributes['label'] );
|
|
|
|
|
2021-11-08 09:29:21 -05:00
|
|
|
$label_markup = sprintf(
|
|
|
|
'<label for="%1$s" class="wp-block-search__label screen-reader-text">%2$s</label>',
|
2022-02-17 14:18:25 -05:00
|
|
|
esc_attr( $input_id ),
|
|
|
|
$label_inner_html
|
2021-11-08 09:29:21 -05:00
|
|
|
);
|
|
|
|
if ( $show_label && ! empty( $attributes['label'] ) ) {
|
|
|
|
$label_markup = sprintf(
|
|
|
|
'<label for="%1$s" class="wp-block-search__label">%2$s</label>',
|
|
|
|
$input_id,
|
2022-02-17 14:18:25 -05:00
|
|
|
$label_inner_html
|
2021-11-08 09:29:21 -05:00
|
|
|
);
|
2020-10-13 09:10:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $show_input ) {
|
2021-11-08 09:29:21 -05:00
|
|
|
$input_classes = ! $is_button_inside ? $border_color_classes : '';
|
|
|
|
$input_markup = sprintf(
|
|
|
|
'<input type="search" id="%s" class="wp-block-search__input %s" name="s" value="%s" placeholder="%s" %s required />',
|
2020-01-08 07:10:04 -05:00
|
|
|
$input_id,
|
2022-01-10 22:13:33 -05:00
|
|
|
esc_attr( $input_classes ),
|
2020-10-13 09:10:30 -04:00
|
|
|
esc_attr( get_search_query() ),
|
2021-04-15 11:19:43 -04:00
|
|
|
esc_attr( $attributes['placeholder'] ),
|
2021-11-08 09:29:21 -05:00
|
|
|
$inline_styles['input']
|
2020-01-08 07:10:04 -05:00
|
|
|
);
|
2019-03-07 04:12:51 -05:00
|
|
|
}
|
|
|
|
|
2020-10-13 09:10:30 -04:00
|
|
|
if ( $show_button ) {
|
|
|
|
$button_internal_markup = '';
|
2021-11-08 09:29:21 -05:00
|
|
|
$button_classes = $color_classes;
|
2022-04-12 11:12:47 -04:00
|
|
|
$aria_label = '';
|
2020-10-13 09:10:30 -04:00
|
|
|
|
2021-11-08 09:29:21 -05:00
|
|
|
if ( ! $is_button_inside ) {
|
|
|
|
$button_classes .= ' ' . $border_color_classes;
|
|
|
|
}
|
2020-10-13 09:10:30 -04:00
|
|
|
if ( ! $use_icon_button ) {
|
|
|
|
if ( ! empty( $attributes['buttonText'] ) ) {
|
2022-02-17 14:18:25 -05:00
|
|
|
$button_internal_markup = wp_kses_post( $attributes['buttonText'] );
|
2020-10-13 09:10:30 -04:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-12 11:12:47 -04:00
|
|
|
$aria_label = sprintf( 'aria-label="%s"', esc_attr( wp_strip_all_tags( $attributes['buttonText'] ) ) );
|
|
|
|
$button_classes .= ' has-icon';
|
|
|
|
|
2020-10-13 09:10:30 -04:00
|
|
|
$button_internal_markup =
|
2020-10-20 09:36:16 -04:00
|
|
|
'<svg id="search-icon" class="search-icon" viewBox="0 0 24 24" width="24" height="24">
|
2022-04-12 11:12:47 -04:00
|
|
|
<path d="M13.5 6C10.5 6 8 8.5 8 11.5c0 1.1.3 2.1.9 3l-3.4 3 1 1.1 3.4-2.9c1 .9 2.2 1.4 3.6 1.4 3 0 5.5-2.5 5.5-5.5C19 8.5 16.5 6 13.5 6zm0 9.5c-2.2 0-4-1.8-4-4s1.8-4 4-4 4 1.8 4 4-1.8 4-4 4z"></path>
|
|
|
|
</svg>';
|
2020-10-13 09:10:30 -04:00
|
|
|
}
|
2019-03-07 04:12:51 -05:00
|
|
|
|
|
|
|
$button_markup = sprintf(
|
2022-04-12 11:12:47 -04:00
|
|
|
'<button type="submit" class="wp-block-search__button %s" %s %s>%s</button>',
|
2022-01-10 22:13:33 -05:00
|
|
|
esc_attr( $button_classes ),
|
2021-11-08 09:29:21 -05:00
|
|
|
$inline_styles['button'],
|
2022-04-12 11:12:47 -04:00
|
|
|
$aria_label,
|
2020-10-13 09:10:30 -04:00
|
|
|
$button_internal_markup
|
2019-03-07 04:12:51 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-08 09:29:21 -05:00
|
|
|
$field_markup_classes = $is_button_inside ? $border_color_classes : '';
|
|
|
|
$field_markup = sprintf(
|
|
|
|
'<div class="wp-block-search__inside-wrapper %s" %s>%s</div>',
|
2022-01-10 22:13:33 -05:00
|
|
|
esc_attr( $field_markup_classes ),
|
2021-04-15 11:19:43 -04:00
|
|
|
$inline_styles['wrapper'],
|
2020-10-13 09:10:30 -04:00
|
|
|
$input_markup . $button_markup
|
|
|
|
);
|
2021-11-08 09:29:21 -05:00
|
|
|
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
|
2020-10-13 09:10:30 -04:00
|
|
|
|
2019-03-07 04:12:51 -05:00
|
|
|
return sprintf(
|
2020-10-20 09:36:16 -04:00
|
|
|
'<form role="search" method="get" action="%s" %s>%s</form>',
|
2019-03-07 04:12:51 -05:00
|
|
|
esc_url( home_url( '/' ) ),
|
2020-10-20 09:36:16 -04:00
|
|
|
$wrapper_attributes,
|
2020-10-13 09:10:30 -04:00
|
|
|
$label_markup . $field_markup
|
2019-03-07 04:12:51 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the `core/search` block on the server.
|
|
|
|
*/
|
|
|
|
function register_block_core_search() {
|
2020-06-26 09:33:47 -04:00
|
|
|
register_block_type_from_metadata(
|
|
|
|
__DIR__ . '/search',
|
2019-03-07 04:12:51 -05:00
|
|
|
array(
|
|
|
|
'render_callback' => 'render_block_core_search',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
add_action( 'init', 'register_block_core_search' );
|
2020-10-13 09:10:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the correct top level classnames for the 'core/search' block.
|
|
|
|
*
|
|
|
|
* @param array $attributes The block attributes.
|
|
|
|
*
|
|
|
|
* @return string The classnames used in the block.
|
|
|
|
*/
|
|
|
|
function classnames_for_block_core_search( $attributes ) {
|
|
|
|
$classnames = array();
|
|
|
|
|
|
|
|
if ( ! empty( $attributes['buttonPosition'] ) ) {
|
|
|
|
if ( 'button-inside' === $attributes['buttonPosition'] ) {
|
|
|
|
$classnames[] = 'wp-block-search__button-inside';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'button-outside' === $attributes['buttonPosition'] ) {
|
|
|
|
$classnames[] = 'wp-block-search__button-outside';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'no-button' === $attributes['buttonPosition'] ) {
|
|
|
|
$classnames[] = 'wp-block-search__no-button';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'button-only' === $attributes['buttonPosition'] ) {
|
|
|
|
$classnames[] = 'wp-block-search__button-only';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $attributes['buttonUseIcon'] ) ) {
|
|
|
|
if ( ! empty( $attributes['buttonPosition'] ) && 'no-button' !== $attributes['buttonPosition'] ) {
|
|
|
|
if ( $attributes['buttonUseIcon'] ) {
|
|
|
|
$classnames[] = 'wp-block-search__icon-button';
|
|
|
|
} else {
|
|
|
|
$classnames[] = 'wp-block-search__text-button';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode( ' ', $classnames );
|
|
|
|
}
|
2021-04-15 11:19:43 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds an array of inline styles for the search block.
|
|
|
|
*
|
|
|
|
* The result will contain one entry for shared styles such as those for the
|
|
|
|
* inner input or button and a second for the inner wrapper should the block
|
|
|
|
* be positioning the button "inside".
|
|
|
|
*
|
|
|
|
* @param array $attributes The block attributes.
|
|
|
|
*
|
|
|
|
* @return array Style HTML attribute.
|
|
|
|
*/
|
|
|
|
function styles_for_block_core_search( $attributes ) {
|
2022-04-12 11:12:47 -04:00
|
|
|
$wrapper_styles = array();
|
|
|
|
$button_styles = array();
|
|
|
|
$input_styles = array();
|
|
|
|
$is_button_inside = ! empty( $attributes['buttonPosition'] ) &&
|
|
|
|
'button-inside' === $attributes['buttonPosition'];
|
2021-04-15 11:19:43 -04:00
|
|
|
|
|
|
|
// Add width styles.
|
|
|
|
$has_width = ! empty( $attributes['width'] ) && ! empty( $attributes['widthUnit'] );
|
|
|
|
$button_only = ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'];
|
|
|
|
|
|
|
|
if ( $has_width && ! $button_only ) {
|
|
|
|
$wrapper_styles[] = sprintf(
|
|
|
|
'width: %d%s;',
|
|
|
|
esc_attr( $attributes['width'] ),
|
|
|
|
esc_attr( $attributes['widthUnit'] )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-12 11:12:47 -04:00
|
|
|
// Add border width styles.
|
|
|
|
$has_border_width = ! empty( $attributes['style']['border']['width'] );
|
|
|
|
|
|
|
|
if ( $has_border_width ) {
|
|
|
|
$border_width = $attributes['style']['border']['width'];
|
|
|
|
|
|
|
|
if ( $is_button_inside ) {
|
|
|
|
$wrapper_styles[] = sprintf( 'border-width: %s;', esc_attr( $border_width ) );
|
|
|
|
} else {
|
|
|
|
$button_styles[] = sprintf( 'border-width: %s;', esc_attr( $border_width ) );
|
|
|
|
$input_styles[] = sprintf( 'border-width: %s;', esc_attr( $border_width ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 11:19:43 -04:00
|
|
|
// Add border radius styles.
|
|
|
|
$has_border_radius = ! empty( $attributes['style']['border']['radius'] );
|
|
|
|
|
|
|
|
if ( $has_border_radius ) {
|
2021-11-08 09:29:21 -05:00
|
|
|
$default_padding = '4px';
|
2021-04-15 11:19:43 -04:00
|
|
|
$border_radius = $attributes['style']['border']['radius'];
|
2021-11-08 09:29:21 -05:00
|
|
|
|
|
|
|
if ( is_array( $border_radius ) ) {
|
|
|
|
// Apply styles for individual corner border radii.
|
|
|
|
foreach ( $border_radius as $key => $value ) {
|
|
|
|
if ( null !== $value ) {
|
|
|
|
// Convert camelCase key to kebab-case.
|
|
|
|
$name = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $key ) );
|
|
|
|
|
|
|
|
// Add shared styles for individual border radii for input & button.
|
|
|
|
$border_style = sprintf(
|
|
|
|
'border-%s-radius: %s;',
|
|
|
|
esc_attr( $name ),
|
|
|
|
esc_attr( $value )
|
|
|
|
);
|
|
|
|
$input_styles[] = $border_style;
|
|
|
|
$button_styles[] = $border_style;
|
|
|
|
|
|
|
|
// Add adjusted border radius styles for the wrapper element
|
|
|
|
// if button is positioned inside.
|
|
|
|
if ( $is_button_inside && intval( $value ) !== 0 ) {
|
|
|
|
$wrapper_styles[] = sprintf(
|
|
|
|
'border-%s-radius: calc(%s + %s);',
|
|
|
|
esc_attr( $name ),
|
|
|
|
esc_attr( $value ),
|
|
|
|
$default_padding
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Numeric check is for backwards compatibility purposes.
|
|
|
|
$border_radius = is_numeric( $border_radius ) ? $border_radius . 'px' : $border_radius;
|
|
|
|
$border_style = sprintf( 'border-radius: %s;', esc_attr( $border_radius ) );
|
|
|
|
$input_styles[] = $border_style;
|
|
|
|
$button_styles[] = $border_style;
|
|
|
|
|
|
|
|
if ( $is_button_inside && intval( $border_radius ) !== 0 ) {
|
|
|
|
// Adjust wrapper border radii to maintain visual consistency
|
|
|
|
// with inner elements when button is positioned inside.
|
|
|
|
$wrapper_styles[] = sprintf(
|
|
|
|
'border-radius: calc(%s + %s);',
|
|
|
|
esc_attr( $border_radius ),
|
|
|
|
$default_padding
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add border color styles.
|
|
|
|
$has_border_color = ! empty( $attributes['style']['border']['color'] );
|
|
|
|
|
|
|
|
if ( $has_border_color ) {
|
2022-04-12 11:12:47 -04:00
|
|
|
$border_color = $attributes['style']['border']['color'];
|
2021-04-15 11:19:43 -04:00
|
|
|
|
2021-11-08 09:29:21 -05:00
|
|
|
// Apply wrapper border color if button placed inside.
|
|
|
|
if ( $is_button_inside ) {
|
|
|
|
$wrapper_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) );
|
|
|
|
} else {
|
|
|
|
$button_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) );
|
|
|
|
$input_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) );
|
2021-04-15 11:19:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 09:29:21 -05:00
|
|
|
// Add color styles.
|
|
|
|
$has_text_color = ! empty( $attributes['style']['color']['text'] );
|
|
|
|
if ( $has_text_color ) {
|
|
|
|
$button_styles[] = sprintf( 'color: %s;', esc_attr( $attributes['style']['color']['text'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_background_color = ! empty( $attributes['style']['color']['background'] );
|
|
|
|
if ( $has_background_color ) {
|
|
|
|
$button_styles[] = sprintf( 'background-color: %s;', esc_attr( $attributes['style']['color']['background'] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$has_custom_gradient = ! empty( $attributes['style']['color']['gradient'] );
|
|
|
|
if ( $has_custom_gradient ) {
|
|
|
|
$button_styles[] = sprintf( 'background: %s;', $attributes['style']['color']['gradient'] );
|
|
|
|
}
|
|
|
|
|
2021-04-15 11:19:43 -04:00
|
|
|
return array(
|
2022-01-10 22:13:33 -05:00
|
|
|
'input' => ! empty( $input_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $input_styles ) ) ) : '',
|
|
|
|
'button' => ! empty( $button_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $button_styles ) ) ) : '',
|
|
|
|
'wrapper' => ! empty( $wrapper_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $wrapper_styles ) ) ) : '',
|
2021-04-15 11:19:43 -04:00
|
|
|
);
|
|
|
|
}
|
2021-11-08 09:29:21 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns border color classnames depending on whether there are named or custom border colors.
|
|
|
|
*
|
|
|
|
* @param array $attributes The block attributes.
|
|
|
|
*
|
|
|
|
* @return string The border color classnames to be applied to the block elements.
|
|
|
|
*/
|
|
|
|
function get_border_color_classes_for_block_core_search( $attributes ) {
|
|
|
|
$has_custom_border_color = ! empty( $attributes['style']['border']['color'] );
|
|
|
|
$border_color_classes = ! empty( $attributes['borderColor'] ) ? sprintf( 'has-border-color has-%s-border-color', $attributes['borderColor'] ) : '';
|
|
|
|
// If there's a border color style and no `borderColor` text string, we still want to add the generic `has-border-color` class name to the element.
|
|
|
|
if ( $has_custom_border_color && empty( $attributes['borderColor'] ) ) {
|
|
|
|
$border_color_classes = 'has-border-color';
|
|
|
|
}
|
|
|
|
return $border_color_classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns color classnames depending on whether there are named or custom text and background colors.
|
|
|
|
*
|
|
|
|
* @param array $attributes The block attributes.
|
|
|
|
*
|
|
|
|
* @return string The color classnames to be applied to the block elements.
|
|
|
|
*/
|
|
|
|
function get_color_classes_for_block_core_search( $attributes ) {
|
|
|
|
$classnames = array();
|
|
|
|
|
|
|
|
// Text color.
|
|
|
|
$has_named_text_color = ! empty( $attributes['textColor'] );
|
|
|
|
$has_custom_text_color = ! empty( $attributes['style']['color']['text'] );
|
|
|
|
if ( $has_named_text_color ) {
|
|
|
|
$classnames[] = sprintf( 'has-text-color has-%s-color', $attributes['textColor'] );
|
|
|
|
} elseif ( $has_custom_text_color ) {
|
|
|
|
// If a custom 'textColor' was selected instead of a preset, still add the generic `has-text-color` class.
|
|
|
|
$classnames[] = 'has-text-color';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Background color.
|
|
|
|
$has_named_background_color = ! empty( $attributes['backgroundColor'] );
|
|
|
|
$has_custom_background_color = ! empty( $attributes['style']['color']['background'] );
|
|
|
|
$has_named_gradient = ! empty( $attributes['gradient'] );
|
|
|
|
$has_custom_gradient = ! empty( $attributes['style']['color']['gradient'] );
|
|
|
|
if (
|
|
|
|
$has_named_background_color ||
|
|
|
|
$has_custom_background_color ||
|
|
|
|
$has_named_gradient ||
|
|
|
|
$has_custom_gradient
|
|
|
|
) {
|
|
|
|
$classnames[] = 'has-background';
|
|
|
|
}
|
|
|
|
if ( $has_named_background_color ) {
|
|
|
|
$classnames[] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
|
|
|
|
}
|
|
|
|
if ( $has_named_gradient ) {
|
|
|
|
$classnames[] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode( ' ', $classnames );
|
|
|
|
}
|