Ei kuvausta

search.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/search` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Dynamically renders the `core/search` block.
  9. *
  10. * @param array $attributes The block attributes.
  11. *
  12. * @return string The search block markup.
  13. */
  14. function render_block_core_search( $attributes ) {
  15. static $instance_id = 0;
  16. // Older versions of the Search block defaulted the label and buttonText
  17. // attributes to `__( 'Search' )` meaning that many posts contain `<!--
  18. // wp:search /-->`. Support these by defaulting an undefined label and
  19. // buttonText to `__( 'Search' )`.
  20. $attributes = wp_parse_args(
  21. $attributes,
  22. array(
  23. 'label' => __( 'Search' ),
  24. 'buttonText' => __( 'Search' ),
  25. )
  26. );
  27. $input_id = 'wp-block-search__input-' . ++$instance_id;
  28. $classnames = classnames_for_block_core_search( $attributes );
  29. $show_label = ( ! empty( $attributes['showLabel'] ) ) ? true : false;
  30. $use_icon_button = ( ! empty( $attributes['buttonUseIcon'] ) ) ? true : false;
  31. $show_input = ( ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'] ) ? false : true;
  32. $show_button = ( ! empty( $attributes['buttonPosition'] ) && 'no-button' === $attributes['buttonPosition'] ) ? false : true;
  33. $input_markup = '';
  34. $button_markup = '';
  35. $inline_styles = styles_for_block_core_search( $attributes );
  36. $color_classes = get_color_classes_for_block_core_search( $attributes );
  37. $is_button_inside = ! empty( $attributes['buttonPosition'] ) &&
  38. 'button-inside' === $attributes['buttonPosition'];
  39. // Border color classes need to be applied to the elements that have a border color.
  40. $border_color_classes = get_border_color_classes_for_block_core_search( $attributes );
  41. $label_inner_html = empty( $attributes['label'] ) ? __( 'Search' ) : wp_kses_post( $attributes['label'] );
  42. $label_markup = sprintf(
  43. '<label for="%1$s" class="wp-block-search__label screen-reader-text">%2$s</label>',
  44. esc_attr( $input_id ),
  45. $label_inner_html
  46. );
  47. if ( $show_label && ! empty( $attributes['label'] ) ) {
  48. $label_markup = sprintf(
  49. '<label for="%1$s" class="wp-block-search__label">%2$s</label>',
  50. $input_id,
  51. $label_inner_html
  52. );
  53. }
  54. if ( $show_input ) {
  55. $input_classes = ! $is_button_inside ? $border_color_classes : '';
  56. $input_markup = sprintf(
  57. '<input type="search" id="%s" class="wp-block-search__input %s" name="s" value="%s" placeholder="%s" %s required />',
  58. $input_id,
  59. esc_attr( $input_classes ),
  60. esc_attr( get_search_query() ),
  61. esc_attr( $attributes['placeholder'] ),
  62. $inline_styles['input']
  63. );
  64. }
  65. if ( $show_button ) {
  66. $button_internal_markup = '';
  67. $button_classes = $color_classes;
  68. $aria_label = '';
  69. if ( ! $is_button_inside ) {
  70. $button_classes .= ' ' . $border_color_classes;
  71. }
  72. if ( ! $use_icon_button ) {
  73. if ( ! empty( $attributes['buttonText'] ) ) {
  74. $button_internal_markup = wp_kses_post( $attributes['buttonText'] );
  75. }
  76. } else {
  77. $aria_label = sprintf( 'aria-label="%s"', esc_attr( wp_strip_all_tags( $attributes['buttonText'] ) ) );
  78. $button_classes .= ' has-icon';
  79. $button_internal_markup =
  80. '<svg id="search-icon" class="search-icon" viewBox="0 0 24 24" width="24" height="24">
  81. <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>
  82. </svg>';
  83. }
  84. $button_markup = sprintf(
  85. '<button type="submit" class="wp-block-search__button %s" %s %s>%s</button>',
  86. esc_attr( $button_classes ),
  87. $inline_styles['button'],
  88. $aria_label,
  89. $button_internal_markup
  90. );
  91. }
  92. $field_markup_classes = $is_button_inside ? $border_color_classes : '';
  93. $field_markup = sprintf(
  94. '<div class="wp-block-search__inside-wrapper %s" %s>%s</div>',
  95. esc_attr( $field_markup_classes ),
  96. $inline_styles['wrapper'],
  97. $input_markup . $button_markup
  98. );
  99. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
  100. return sprintf(
  101. '<form role="search" method="get" action="%s" %s>%s</form>',
  102. esc_url( home_url( '/' ) ),
  103. $wrapper_attributes,
  104. $label_markup . $field_markup
  105. );
  106. }
  107. /**
  108. * Registers the `core/search` block on the server.
  109. */
  110. function register_block_core_search() {
  111. register_block_type_from_metadata(
  112. __DIR__ . '/search',
  113. array(
  114. 'render_callback' => 'render_block_core_search',
  115. )
  116. );
  117. }
  118. add_action( 'init', 'register_block_core_search' );
  119. /**
  120. * Builds the correct top level classnames for the 'core/search' block.
  121. *
  122. * @param array $attributes The block attributes.
  123. *
  124. * @return string The classnames used in the block.
  125. */
  126. function classnames_for_block_core_search( $attributes ) {
  127. $classnames = array();
  128. if ( ! empty( $attributes['buttonPosition'] ) ) {
  129. if ( 'button-inside' === $attributes['buttonPosition'] ) {
  130. $classnames[] = 'wp-block-search__button-inside';
  131. }
  132. if ( 'button-outside' === $attributes['buttonPosition'] ) {
  133. $classnames[] = 'wp-block-search__button-outside';
  134. }
  135. if ( 'no-button' === $attributes['buttonPosition'] ) {
  136. $classnames[] = 'wp-block-search__no-button';
  137. }
  138. if ( 'button-only' === $attributes['buttonPosition'] ) {
  139. $classnames[] = 'wp-block-search__button-only';
  140. }
  141. }
  142. if ( isset( $attributes['buttonUseIcon'] ) ) {
  143. if ( ! empty( $attributes['buttonPosition'] ) && 'no-button' !== $attributes['buttonPosition'] ) {
  144. if ( $attributes['buttonUseIcon'] ) {
  145. $classnames[] = 'wp-block-search__icon-button';
  146. } else {
  147. $classnames[] = 'wp-block-search__text-button';
  148. }
  149. }
  150. }
  151. return implode( ' ', $classnames );
  152. }
  153. /**
  154. * Builds an array of inline styles for the search block.
  155. *
  156. * The result will contain one entry for shared styles such as those for the
  157. * inner input or button and a second for the inner wrapper should the block
  158. * be positioning the button "inside".
  159. *
  160. * @param array $attributes The block attributes.
  161. *
  162. * @return array Style HTML attribute.
  163. */
  164. function styles_for_block_core_search( $attributes ) {
  165. $wrapper_styles = array();
  166. $button_styles = array();
  167. $input_styles = array();
  168. $is_button_inside = ! empty( $attributes['buttonPosition'] ) &&
  169. 'button-inside' === $attributes['buttonPosition'];
  170. // Add width styles.
  171. $has_width = ! empty( $attributes['width'] ) && ! empty( $attributes['widthUnit'] );
  172. $button_only = ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'];
  173. if ( $has_width && ! $button_only ) {
  174. $wrapper_styles[] = sprintf(
  175. 'width: %d%s;',
  176. esc_attr( $attributes['width'] ),
  177. esc_attr( $attributes['widthUnit'] )
  178. );
  179. }
  180. // Add border width styles.
  181. $has_border_width = ! empty( $attributes['style']['border']['width'] );
  182. if ( $has_border_width ) {
  183. $border_width = $attributes['style']['border']['width'];
  184. if ( $is_button_inside ) {
  185. $wrapper_styles[] = sprintf( 'border-width: %s;', esc_attr( $border_width ) );
  186. } else {
  187. $button_styles[] = sprintf( 'border-width: %s;', esc_attr( $border_width ) );
  188. $input_styles[] = sprintf( 'border-width: %s;', esc_attr( $border_width ) );
  189. }
  190. }
  191. // Add border radius styles.
  192. $has_border_radius = ! empty( $attributes['style']['border']['radius'] );
  193. if ( $has_border_radius ) {
  194. $default_padding = '4px';
  195. $border_radius = $attributes['style']['border']['radius'];
  196. if ( is_array( $border_radius ) ) {
  197. // Apply styles for individual corner border radii.
  198. foreach ( $border_radius as $key => $value ) {
  199. if ( null !== $value ) {
  200. // Convert camelCase key to kebab-case.
  201. $name = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $key ) );
  202. // Add shared styles for individual border radii for input & button.
  203. $border_style = sprintf(
  204. 'border-%s-radius: %s;',
  205. esc_attr( $name ),
  206. esc_attr( $value )
  207. );
  208. $input_styles[] = $border_style;
  209. $button_styles[] = $border_style;
  210. // Add adjusted border radius styles for the wrapper element
  211. // if button is positioned inside.
  212. if ( $is_button_inside && intval( $value ) !== 0 ) {
  213. $wrapper_styles[] = sprintf(
  214. 'border-%s-radius: calc(%s + %s);',
  215. esc_attr( $name ),
  216. esc_attr( $value ),
  217. $default_padding
  218. );
  219. }
  220. }
  221. }
  222. } else {
  223. // Numeric check is for backwards compatibility purposes.
  224. $border_radius = is_numeric( $border_radius ) ? $border_radius . 'px' : $border_radius;
  225. $border_style = sprintf( 'border-radius: %s;', esc_attr( $border_radius ) );
  226. $input_styles[] = $border_style;
  227. $button_styles[] = $border_style;
  228. if ( $is_button_inside && intval( $border_radius ) !== 0 ) {
  229. // Adjust wrapper border radii to maintain visual consistency
  230. // with inner elements when button is positioned inside.
  231. $wrapper_styles[] = sprintf(
  232. 'border-radius: calc(%s + %s);',
  233. esc_attr( $border_radius ),
  234. $default_padding
  235. );
  236. }
  237. }
  238. }
  239. // Add border color styles.
  240. $has_border_color = ! empty( $attributes['style']['border']['color'] );
  241. if ( $has_border_color ) {
  242. $border_color = $attributes['style']['border']['color'];
  243. // Apply wrapper border color if button placed inside.
  244. if ( $is_button_inside ) {
  245. $wrapper_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) );
  246. } else {
  247. $button_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) );
  248. $input_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) );
  249. }
  250. }
  251. // Add color styles.
  252. $has_text_color = ! empty( $attributes['style']['color']['text'] );
  253. if ( $has_text_color ) {
  254. $button_styles[] = sprintf( 'color: %s;', esc_attr( $attributes['style']['color']['text'] ) );
  255. }
  256. $has_background_color = ! empty( $attributes['style']['color']['background'] );
  257. if ( $has_background_color ) {
  258. $button_styles[] = sprintf( 'background-color: %s;', esc_attr( $attributes['style']['color']['background'] ) );
  259. }
  260. $has_custom_gradient = ! empty( $attributes['style']['color']['gradient'] );
  261. if ( $has_custom_gradient ) {
  262. $button_styles[] = sprintf( 'background: %s;', $attributes['style']['color']['gradient'] );
  263. }
  264. return array(
  265. 'input' => ! empty( $input_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $input_styles ) ) ) : '',
  266. 'button' => ! empty( $button_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $button_styles ) ) ) : '',
  267. 'wrapper' => ! empty( $wrapper_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $wrapper_styles ) ) ) : '',
  268. );
  269. }
  270. /**
  271. * Returns border color classnames depending on whether there are named or custom border colors.
  272. *
  273. * @param array $attributes The block attributes.
  274. *
  275. * @return string The border color classnames to be applied to the block elements.
  276. */
  277. function get_border_color_classes_for_block_core_search( $attributes ) {
  278. $has_custom_border_color = ! empty( $attributes['style']['border']['color'] );
  279. $border_color_classes = ! empty( $attributes['borderColor'] ) ? sprintf( 'has-border-color has-%s-border-color', $attributes['borderColor'] ) : '';
  280. // 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.
  281. if ( $has_custom_border_color && empty( $attributes['borderColor'] ) ) {
  282. $border_color_classes = 'has-border-color';
  283. }
  284. return $border_color_classes;
  285. }
  286. /**
  287. * Returns color classnames depending on whether there are named or custom text and background colors.
  288. *
  289. * @param array $attributes The block attributes.
  290. *
  291. * @return string The color classnames to be applied to the block elements.
  292. */
  293. function get_color_classes_for_block_core_search( $attributes ) {
  294. $classnames = array();
  295. // Text color.
  296. $has_named_text_color = ! empty( $attributes['textColor'] );
  297. $has_custom_text_color = ! empty( $attributes['style']['color']['text'] );
  298. if ( $has_named_text_color ) {
  299. $classnames[] = sprintf( 'has-text-color has-%s-color', $attributes['textColor'] );
  300. } elseif ( $has_custom_text_color ) {
  301. // If a custom 'textColor' was selected instead of a preset, still add the generic `has-text-color` class.
  302. $classnames[] = 'has-text-color';
  303. }
  304. // Background color.
  305. $has_named_background_color = ! empty( $attributes['backgroundColor'] );
  306. $has_custom_background_color = ! empty( $attributes['style']['color']['background'] );
  307. $has_named_gradient = ! empty( $attributes['gradient'] );
  308. $has_custom_gradient = ! empty( $attributes['style']['color']['gradient'] );
  309. if (
  310. $has_named_background_color ||
  311. $has_custom_background_color ||
  312. $has_named_gradient ||
  313. $has_custom_gradient
  314. ) {
  315. $classnames[] = 'has-background';
  316. }
  317. if ( $has_named_background_color ) {
  318. $classnames[] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
  319. }
  320. if ( $has_named_gradient ) {
  321. $classnames[] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] );
  322. }
  323. return implode( ' ', $classnames );
  324. }