[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> navigation-link.php (source)

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


Generated: Tue Jan 25 01:00:04 2022 Cross-referenced by PHPXref 0.7.1