PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/blocks/navigation-link.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 350 lines | 231 code | 45 blank | 74 comment | 41 complexity | cb3eb58257b0640fe11dec656b28cb9c MD5 | raw file
  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" role="img" 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 array $content The saved content.
  106. * @param array $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(
  169. $attributes['label'],
  170. array(
  171. 'code' => array(),
  172. 'em' => array(),
  173. 'img' => array(
  174. 'scale' => array(),
  175. 'class' => array(),
  176. 'style' => array(),
  177. 'src' => array(),
  178. 'alt' => array(),
  179. ),
  180. 's' => array(),
  181. 'span' => array(
  182. 'style' => array(),
  183. ),
  184. 'strong' => array(),
  185. )
  186. );
  187. }
  188. $html .= '</span>';
  189. if ( isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'] && $has_submenu ) {
  190. // The submenu icon can be hidden by a CSS rule on the Navigation Block.
  191. $html .= '<span class="wp-block-navigation__submenu-icon">' . block_core_navigation_link_render_submenu_icon() . '</span>';
  192. }
  193. $html .= '</a>';
  194. // End anchor tag content.
  195. if ( $has_submenu ) {
  196. $inner_blocks_html = '';
  197. foreach ( $block->inner_blocks as $inner_block ) {
  198. $inner_blocks_html .= $inner_block->render();
  199. }
  200. $html .= sprintf(
  201. '<ul class="wp-block-navigation__submenu-container">%s</ul>',
  202. $inner_blocks_html
  203. );
  204. }
  205. $html .= '</li>';
  206. return $html;
  207. }
  208. /**
  209. * Returns a navigation link variation
  210. *
  211. * @param WP_Taxonomy|WP_Post_Type $entity post type or taxonomy entity.
  212. * @param string $kind string of value 'taxonomy' or 'post-type'.
  213. *
  214. * @return array
  215. */
  216. function build_variation_for_navigation_link( $entity, $kind ) {
  217. $title = '';
  218. $description = '';
  219. if ( property_exists( $entity->labels, 'item_link' ) ) {
  220. $title = $entity->labels->item_link;
  221. }
  222. if ( property_exists( $entity->labels, 'item_link_description' ) ) {
  223. $description = $entity->labels->item_link_description;
  224. }
  225. $variation = array(
  226. 'name' => $entity->name,
  227. 'title' => $title,
  228. 'description' => $description,
  229. 'attributes' => array(
  230. 'type' => $entity->name,
  231. 'kind' => $kind,
  232. ),
  233. );
  234. // Tweak some value for the variations.
  235. $variation_overrides = array(
  236. 'post_tag' => array(
  237. 'name' => 'tag',
  238. 'attributes' => array(
  239. 'type' => 'tag',
  240. 'kind' => $kind,
  241. ),
  242. ),
  243. 'post_format' => array(
  244. // The item_link and item_link_description for post formats is the
  245. // same as for tags, so need to be overridden.
  246. 'title' => __( 'Post Format Link' ),
  247. 'description' => __( 'A link to a post format' ),
  248. 'attributes' => array(
  249. 'type' => 'post_format',
  250. 'kind' => $kind,
  251. ),
  252. ),
  253. );
  254. if ( array_key_exists( $entity->name, $variation_overrides ) ) {
  255. $variation = array_merge(
  256. $variation,
  257. $variation_overrides[ $entity->name ]
  258. );
  259. }
  260. return $variation;
  261. }
  262. /**
  263. * Register the navigation link block.
  264. *
  265. * @uses render_block_core_navigation()
  266. * @throws WP_Error An WP_Error exception parsing the block definition.
  267. */
  268. function register_block_core_navigation_link() {
  269. $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
  270. $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' );
  271. // Use two separate arrays as a way to order the variations in the UI.
  272. // Known variations (like Post Link and Page Link) are added to the
  273. // `built_ins` array. Variations for custom post types and taxonomies are
  274. // added to the `variations` array and will always appear after `built-ins.
  275. $built_ins = array();
  276. $variations = array();
  277. if ( $post_types ) {
  278. foreach ( $post_types as $post_type ) {
  279. $variation = build_variation_for_navigation_link( $post_type, 'post-type' );
  280. if ( $post_type->_builtin ) {
  281. $built_ins[] = $variation;
  282. } else {
  283. $variations[] = $variation;
  284. }
  285. }
  286. }
  287. if ( $taxonomies ) {
  288. foreach ( $taxonomies as $taxonomy ) {
  289. $variation = build_variation_for_navigation_link( $taxonomy, 'taxonomy' );
  290. if ( $taxonomy->_builtin ) {
  291. $built_ins[] = $variation;
  292. } else {
  293. $variations[] = $variation;
  294. }
  295. }
  296. }
  297. register_block_type_from_metadata(
  298. __DIR__ . '/navigation-link',
  299. array(
  300. 'render_callback' => 'render_block_core_navigation_link',
  301. 'variations' => array_merge( $built_ins, $variations ),
  302. )
  303. );
  304. }
  305. add_action( 'init', 'register_block_core_navigation_link' );