navigation-link.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/navigation-link` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Build an array with CSS classes and inline styles defining the colors
  9. * which will be applied to the navigation markup in the front-end.
  10. *
  11. * @param array $context Navigation block context.
  12. * @param array $attributes Block attributes.
  13. * @return array Colors CSS classes and inline styles.
  14. */
  15. function block_core_navigation_link_build_css_colors( $context, $attributes ) {
  16. $colors = array(
  17. 'css_classes' => array(),
  18. 'inline_styles' => '',
  19. );
  20. $is_sub_menu = isset( $attributes['isTopLevelLink'] ) ? ( ! $attributes['isTopLevelLink'] ) : false;
  21. // Text color.
  22. $named_text_color = null;
  23. $custom_text_color = null;
  24. if ( $is_sub_menu && array_key_exists( 'customOverlayTextColor', $context ) ) {
  25. $custom_text_color = $context['customOverlayTextColor'];
  26. } elseif ( $is_sub_menu && array_key_exists( 'overlayTextColor', $context ) ) {
  27. $named_text_color = $context['overlayTextColor'];
  28. } elseif ( array_key_exists( 'customTextColor', $context ) ) {
  29. $custom_text_color = $context['customTextColor'];
  30. } elseif ( array_key_exists( 'textColor', $context ) ) {
  31. $named_text_color = $context['textColor'];
  32. } elseif ( isset( $context['style']['color']['text'] ) ) {
  33. $custom_text_color = $context['style']['color']['text'];
  34. }
  35. // If has text color.
  36. if ( ! is_null( $named_text_color ) ) {
  37. // Add the color class.
  38. array_push( $colors['css_classes'], 'has-text-color', sprintf( 'has-%s-color', $named_text_color ) );
  39. } elseif ( ! is_null( $custom_text_color ) ) {
  40. // Add the custom color inline style.
  41. $colors['css_classes'][] = 'has-text-color';
  42. $colors['inline_styles'] .= sprintf( 'color: %s;', $custom_text_color );
  43. }
  44. // Background color.
  45. $named_background_color = null;
  46. $custom_background_color = null;
  47. if ( $is_sub_menu && array_key_exists( 'customOverlayBackgroundColor', $context ) ) {
  48. $custom_background_color = $context['customOverlayBackgroundColor'];
  49. } elseif ( $is_sub_menu && array_key_exists( 'overlayBackgroundColor', $context ) ) {
  50. $named_background_color = $context['overlayBackgroundColor'];
  51. } elseif ( array_key_exists( 'customBackgroundColor', $context ) ) {
  52. $custom_background_color = $context['customBackgroundColor'];
  53. } elseif ( array_key_exists( 'backgroundColor', $context ) ) {
  54. $named_background_color = $context['backgroundColor'];
  55. } elseif ( isset( $context['style']['color']['background'] ) ) {
  56. $custom_background_color = $context['style']['color']['background'];
  57. }
  58. // If has background color.
  59. if ( ! is_null( $named_background_color ) ) {
  60. // Add the background-color class.
  61. array_push( $colors['css_classes'], 'has-background', sprintf( 'has-%s-background-color', $named_background_color ) );
  62. } elseif ( ! is_null( $custom_background_color ) ) {
  63. // Add the custom background-color inline style.
  64. $colors['css_classes'][] = 'has-background';
  65. $colors['inline_styles'] .= sprintf( 'background-color: %s;', $custom_background_color );
  66. }
  67. return $colors;
  68. }
  69. /**
  70. * Build an array with CSS classes and inline styles defining the font sizes
  71. * which will be applied to the navigation markup in the front-end.
  72. *
  73. * @param array $context Navigation block context.
  74. * @return array Font size CSS classes and inline styles.
  75. */
  76. function block_core_navigation_link_build_css_font_sizes( $context ) {
  77. // CSS classes.
  78. $font_sizes = array(
  79. 'css_classes' => array(),
  80. 'inline_styles' => '',
  81. );
  82. $has_named_font_size = array_key_exists( 'fontSize', $context );
  83. $has_custom_font_size = isset( $context['style']['typography']['fontSize'] );
  84. if ( $has_named_font_size ) {
  85. // Add the font size class.
  86. $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] );
  87. } elseif ( $has_custom_font_size ) {
  88. // Add the custom font size inline style.
  89. $font_sizes['inline_styles'] = sprintf(
  90. 'font-size: %s;',
  91. wp_get_typography_font_size_value(
  92. array(
  93. 'size' => $context['style']['typography']['fontSize'],
  94. )
  95. )
  96. );
  97. }
  98. return $font_sizes;
  99. }
  100. /**
  101. * Returns the top-level submenu SVG chevron icon.
  102. *
  103. * @return string
  104. */
  105. function block_core_navigation_link_render_submenu_icon() {
  106. return '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg>';
  107. }
  108. /**
  109. * Renders the `core/navigation-link` block.
  110. *
  111. * @param array $attributes The block attributes.
  112. * @param string $content The saved content.
  113. * @param WP_Block $block The parsed block.
  114. *
  115. * @return string Returns the post content with the legacy widget added.
  116. */
  117. function render_block_core_navigation_link( $attributes, $content, $block ) {
  118. $navigation_link_has_id = isset( $attributes['id'] ) && is_numeric( $attributes['id'] );
  119. $is_post_type = isset( $attributes['kind'] ) && 'post-type' === $attributes['kind'];
  120. $is_post_type = $is_post_type || isset( $attributes['type'] ) && ( 'post' === $attributes['type'] || 'page' === $attributes['type'] );
  121. // Don't render the block's subtree if it is a draft or if the ID does not exist.
  122. if ( $is_post_type && $navigation_link_has_id ) {
  123. $post = get_post( $attributes['id'] );
  124. if ( ! $post || 'publish' !== $post->post_status ) {
  125. return '';
  126. }
  127. }
  128. // Don't render the block's subtree if it has no label.
  129. if ( empty( $attributes['label'] ) ) {
  130. return '';
  131. }
  132. $colors = block_core_navigation_link_build_css_colors( $block->context, $attributes );
  133. $font_sizes = block_core_navigation_link_build_css_font_sizes( $block->context );
  134. $classes = array_merge(
  135. $colors['css_classes'],
  136. $font_sizes['css_classes']
  137. );
  138. $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
  139. $css_classes = trim( implode( ' ', $classes ) );
  140. $has_submenu = count( $block->inner_blocks ) > 0;
  141. $is_active = ! empty( $attributes['id'] ) && ( get_queried_object_id() === (int) $attributes['id'] );
  142. $wrapper_attributes = get_block_wrapper_attributes(
  143. array(
  144. 'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) .
  145. ( $is_active ? ' current-menu-item' : '' ),
  146. 'style' => $style_attribute,
  147. )
  148. );
  149. $html = '<li ' . $wrapper_attributes . '>' .
  150. '<a class="wp-block-navigation-item__content" ';
  151. // Start appending HTML attributes to anchor tag.
  152. if ( isset( $attributes['url'] ) ) {
  153. $html .= ' href="' . esc_url( $attributes['url'] ) . '"';
  154. }
  155. if ( $is_active ) {
  156. $html .= ' aria-current="page"';
  157. }
  158. if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
  159. $html .= ' target="_blank" ';
  160. }
  161. if ( isset( $attributes['rel'] ) ) {
  162. $html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
  163. } elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) {
  164. $html .= ' rel="nofollow"';
  165. }
  166. if ( isset( $attributes['title'] ) ) {
  167. $html .= ' title="' . esc_attr( $attributes['title'] ) . '"';
  168. }
  169. // End appending HTML attributes to anchor tag.
  170. // Start anchor tag content.
  171. $html .= '>' .
  172. // Wrap title with span to isolate it from submenu icon.
  173. '<span class="wp-block-navigation-item__label">';
  174. if ( isset( $attributes['label'] ) ) {
  175. $html .= wp_kses_post( $attributes['label'] );
  176. }
  177. $html .= '</span>';
  178. // Add description if available.
  179. if ( ! empty( $attributes['description'] ) ) {
  180. $html .= '<span class="wp-block-navigation-item__description">';
  181. $html .= wp_kses_post( $attributes['description'] );
  182. $html .= '</span>';
  183. }
  184. $html .= '</a>';
  185. // End anchor tag content.
  186. if ( isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'] && $has_submenu ) {
  187. // The submenu icon can be hidden by a CSS rule on the Navigation Block.
  188. $html .= '<span class="wp-block-navigation__submenu-icon">' . block_core_navigation_link_render_submenu_icon() . '</span>';
  189. }
  190. if ( $has_submenu ) {
  191. $inner_blocks_html = '';
  192. foreach ( $block->inner_blocks as $inner_block ) {
  193. $inner_blocks_html .= $inner_block->render();
  194. }
  195. $html .= sprintf(
  196. '<ul class="wp-block-navigation__submenu-container">%s</ul>',
  197. $inner_blocks_html
  198. );
  199. }
  200. $html .= '</li>';
  201. return $html;
  202. }
  203. /**
  204. * Returns a navigation link variation
  205. *
  206. * @param WP_Taxonomy|WP_Post_Type $entity post type or taxonomy entity.
  207. * @param string $kind string of value 'taxonomy' or 'post-type'.
  208. *
  209. * @return array
  210. */
  211. function build_variation_for_navigation_link( $entity, $kind ) {
  212. $title = '';
  213. $description = '';
  214. if ( property_exists( $entity->labels, 'item_link' ) ) {
  215. $title = $entity->labels->item_link;
  216. }
  217. if ( property_exists( $entity->labels, 'item_link_description' ) ) {
  218. $description = $entity->labels->item_link_description;
  219. }
  220. $variation = array(
  221. 'name' => $entity->name,
  222. 'title' => $title,
  223. 'description' => $description,
  224. 'attributes' => array(
  225. 'type' => $entity->name,
  226. 'kind' => $kind,
  227. ),
  228. );
  229. // Tweak some value for the variations.
  230. $variation_overrides = array(
  231. 'post_tag' => array(
  232. 'name' => 'tag',
  233. 'attributes' => array(
  234. 'type' => 'tag',
  235. 'kind' => $kind,
  236. ),
  237. ),
  238. 'post_format' => array(
  239. // The item_link and item_link_description for post formats is the
  240. // same as for tags, so need to be overridden.
  241. 'title' => __( 'Post Format Link' ),
  242. 'description' => __( 'A link to a post format' ),
  243. 'attributes' => array(
  244. 'type' => 'post_format',
  245. 'kind' => $kind,
  246. ),
  247. ),
  248. );
  249. if ( array_key_exists( $entity->name, $variation_overrides ) ) {
  250. $variation = array_merge(
  251. $variation,
  252. $variation_overrides[ $entity->name ]
  253. );
  254. }
  255. return $variation;
  256. }
  257. /**
  258. * Register the navigation link block.
  259. *
  260. * @uses render_block_core_navigation()
  261. * @throws WP_Error An WP_Error exception parsing the block definition.
  262. */
  263. function register_block_core_navigation_link() {
  264. $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
  265. $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' );
  266. // Use two separate arrays as a way to order the variations in the UI.
  267. // Known variations (like Post Link and Page Link) are added to the
  268. // `built_ins` array. Variations for custom post types and taxonomies are
  269. // added to the `variations` array and will always appear after `built-ins.
  270. $built_ins = array();
  271. $variations = array();
  272. if ( $post_types ) {
  273. foreach ( $post_types as $post_type ) {
  274. $variation = build_variation_for_navigation_link( $post_type, 'post-type' );
  275. if ( $post_type->_builtin ) {
  276. $built_ins[] = $variation;
  277. } else {
  278. $variations[] = $variation;
  279. }
  280. }
  281. }
  282. if ( $taxonomies ) {
  283. foreach ( $taxonomies as $taxonomy ) {
  284. $variation = build_variation_for_navigation_link( $taxonomy, 'taxonomy' );
  285. if ( $taxonomy->_builtin ) {
  286. $built_ins[] = $variation;
  287. } else {
  288. $variations[] = $variation;
  289. }
  290. }
  291. }
  292. register_block_type_from_metadata(
  293. __DIR__ . '/navigation-link',
  294. array(
  295. 'render_callback' => 'render_block_core_navigation_link',
  296. 'variations' => array_merge( $built_ins, $variations ),
  297. )
  298. );
  299. }
  300. add_action( 'init', 'register_block_core_navigation_link' );