Nenhuma Descrição

navigation-link.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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( 'font-size: %s;', $context['style']['typography']['fontSize'] );
  90. }
  91. return $font_sizes;
  92. }
  93. /**
  94. * Returns the top-level submenu SVG chevron icon.
  95. *
  96. * @return string
  97. */
  98. function block_core_navigation_link_render_submenu_icon() {
  99. 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>';
  100. }
  101. /**
  102. * Renders the `core/navigation-link` block.
  103. *
  104. * @param array $attributes The block attributes.
  105. * @param string $content The saved content.
  106. * @param WP_Block $block The parsed block.
  107. *
  108. * @return string Returns the post content with the legacy widget added.
  109. */
  110. function render_block_core_navigation_link( $attributes, $content, $block ) {
  111. $navigation_link_has_id = isset( $attributes['id'] ) && is_numeric( $attributes['id'] );
  112. $is_post_type = isset( $attributes['kind'] ) && 'post-type' === $attributes['kind'];
  113. $is_post_type = $is_post_type || isset( $attributes['type'] ) && ( 'post' === $attributes['type'] || 'page' === $attributes['type'] );
  114. // Don't render the block's subtree if it is a draft or if the ID does not exist.
  115. if ( $is_post_type && $navigation_link_has_id ) {
  116. $post = get_post( $attributes['id'] );
  117. if ( ! $post || 'publish' !== $post->post_status ) {
  118. return '';
  119. }
  120. }
  121. // Don't render the block's subtree if it has no label.
  122. if ( empty( $attributes['label'] ) ) {
  123. return '';
  124. }
  125. $colors = block_core_navigation_link_build_css_colors( $block->context, $attributes );
  126. $font_sizes = block_core_navigation_link_build_css_font_sizes( $block->context );
  127. $classes = array_merge(
  128. $colors['css_classes'],
  129. $font_sizes['css_classes']
  130. );
  131. $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
  132. $css_classes = trim( implode( ' ', $classes ) );
  133. $has_submenu = count( $block->inner_blocks ) > 0;
  134. $is_active = ! empty( $attributes['id'] ) && ( get_the_ID() === $attributes['id'] );
  135. $wrapper_attributes = get_block_wrapper_attributes(
  136. array(
  137. 'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) .
  138. ( $is_active ? ' current-menu-item' : '' ),
  139. 'style' => $style_attribute,
  140. )
  141. );
  142. $html = '<li ' . $wrapper_attributes . '>' .
  143. '<a class="wp-block-navigation-item__content" ';
  144. // Start appending HTML attributes to anchor tag.
  145. if ( isset( $attributes['url'] ) ) {
  146. $html .= ' href="' . esc_url( $attributes['url'] ) . '"';
  147. }
  148. if ( $is_active ) {
  149. $html .= ' aria-current="page"';
  150. }
  151. if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
  152. $html .= ' target="_blank" ';
  153. }
  154. if ( isset( $attributes['rel'] ) ) {
  155. $html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
  156. } elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) {
  157. $html .= ' rel="nofollow"';
  158. }
  159. if ( isset( $attributes['title'] ) ) {
  160. $html .= ' title="' . esc_attr( $attributes['title'] ) . '"';
  161. }
  162. // End appending HTML attributes to anchor tag.
  163. // Start anchor tag content.
  164. $html .= '>' .
  165. // Wrap title with span to isolate it from submenu icon.
  166. '<span class="wp-block-navigation-item__label">';
  167. if ( isset( $attributes['label'] ) ) {
  168. $html .= wp_kses_post( $attributes['label'] );
  169. }
  170. $html .= '</span>';
  171. // Add description if available.
  172. if ( ! empty( $attributes['description'] ) ) {
  173. $html .= '<span class="wp-block-navigation-item__description">';
  174. $html .= wp_kses_post( $attributes['description'] );
  175. $html .= '</span>';
  176. }
  177. $html .= '</a>';
  178. // End anchor tag content.
  179. if ( isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'] && $has_submenu ) {
  180. // The submenu icon can be hidden by a CSS rule on the Navigation Block.
  181. $html .= '<span class="wp-block-navigation__submenu-icon">' . block_core_navigation_link_render_submenu_icon() . '</span>';
  182. }
  183. if ( $has_submenu ) {
  184. $inner_blocks_html = '';
  185. foreach ( $block->inner_blocks as $inner_block ) {
  186. $inner_blocks_html .= $inner_block->render();
  187. }
  188. $html .= sprintf(
  189. '<ul class="wp-block-navigation__submenu-container">%s</ul>',
  190. $inner_blocks_html
  191. );
  192. }
  193. $html .= '</li>';
  194. return $html;
  195. }
  196. /**
  197. * Returns a navigation link variation
  198. *
  199. * @param WP_Taxonomy|WP_Post_Type $entity post type or taxonomy entity.
  200. * @param string $kind string of value 'taxonomy' or 'post-type'.
  201. *
  202. * @return array
  203. */
  204. function build_variation_for_navigation_link( $entity, $kind ) {
  205. $title = '';
  206. $description = '';
  207. if ( property_exists( $entity->labels, 'item_link' ) ) {
  208. $title = $entity->labels->item_link;
  209. }
  210. if ( property_exists( $entity->labels, 'item_link_description' ) ) {
  211. $description = $entity->labels->item_link_description;
  212. }
  213. $variation = array(
  214. 'name' => $entity->name,
  215. 'title' => $title,
  216. 'description' => $description,
  217. 'attributes' => array(
  218. 'type' => $entity->name,
  219. 'kind' => $kind,
  220. ),
  221. );
  222. // Tweak some value for the variations.
  223. $variation_overrides = array(
  224. 'post_tag' => array(
  225. 'name' => 'tag',
  226. 'attributes' => array(
  227. 'type' => 'tag',
  228. 'kind' => $kind,
  229. ),
  230. ),
  231. 'post_format' => array(
  232. // The item_link and item_link_description for post formats is the
  233. // same as for tags, so need to be overridden.
  234. 'title' => __( 'Post Format Link' ),
  235. 'description' => __( 'A link to a post format' ),
  236. 'attributes' => array(
  237. 'type' => 'post_format',
  238. 'kind' => $kind,
  239. ),
  240. ),
  241. );
  242. if ( array_key_exists( $entity->name, $variation_overrides ) ) {
  243. $variation = array_merge(
  244. $variation,
  245. $variation_overrides[ $entity->name ]
  246. );
  247. }
  248. return $variation;
  249. }
  250. /**
  251. * Register the navigation link block.
  252. *
  253. * @uses render_block_core_navigation()
  254. * @throws WP_Error An WP_Error exception parsing the block definition.
  255. */
  256. function register_block_core_navigation_link() {
  257. $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
  258. $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' );
  259. // Use two separate arrays as a way to order the variations in the UI.
  260. // Known variations (like Post Link and Page Link) are added to the
  261. // `built_ins` array. Variations for custom post types and taxonomies are
  262. // added to the `variations` array and will always appear after `built-ins.
  263. $built_ins = array();
  264. $variations = array();
  265. if ( $post_types ) {
  266. foreach ( $post_types as $post_type ) {
  267. $variation = build_variation_for_navigation_link( $post_type, 'post-type' );
  268. if ( $post_type->_builtin ) {
  269. $built_ins[] = $variation;
  270. } else {
  271. $variations[] = $variation;
  272. }
  273. }
  274. }
  275. if ( $taxonomies ) {
  276. foreach ( $taxonomies as $taxonomy ) {
  277. $variation = build_variation_for_navigation_link( $taxonomy, 'taxonomy' );
  278. if ( $taxonomy->_builtin ) {
  279. $built_ins[] = $variation;
  280. } else {
  281. $variations[] = $variation;
  282. }
  283. }
  284. }
  285. register_block_type_from_metadata(
  286. __DIR__ . '/navigation-link',
  287. array(
  288. 'render_callback' => 'render_block_core_navigation_link',
  289. 'variations' => array_merge( $built_ins, $variations ),
  290. )
  291. );
  292. }
  293. add_action( 'init', 'register_block_core_navigation_link' );