[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

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


Generated : Wed Apr 17 08:20:01 2024 Cross-referenced by PHPXref