wp/wp-includes/blocks/post-terms.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     5  * @package WordPress
     5  * @package WordPress
     6  */
     6  */
     7 
     7 
     8 /**
     8 /**
     9  * Renders the `core/post-terms` block on the server.
     9  * Renders the `core/post-terms` block on the server.
       
    10  *
       
    11  * @since 5.8.0
    10  *
    12  *
    11  * @param array    $attributes Block attributes.
    13  * @param array    $attributes Block attributes.
    12  * @param string   $content    Block default content.
    14  * @param string   $content    Block default content.
    13  * @param WP_Block $block      Block instance.
    15  * @param WP_Block $block      Block instance.
    14  * @return string Returns the filtered post terms for the current post wrapped inside "a" tags.
    16  * @return string Returns the filtered post terms for the current post wrapped inside "a" tags.
    25 	$post_terms = get_the_terms( $block->context['postId'], $attributes['term'] );
    27 	$post_terms = get_the_terms( $block->context['postId'], $attributes['term'] );
    26 	if ( is_wp_error( $post_terms ) || empty( $post_terms ) ) {
    28 	if ( is_wp_error( $post_terms ) || empty( $post_terms ) ) {
    27 		return '';
    29 		return '';
    28 	}
    30 	}
    29 
    31 
    30 	$classes = 'taxonomy-' . $attributes['term'];
    32 	$classes = array( 'taxonomy-' . $attributes['term'] );
    31 	if ( isset( $attributes['textAlign'] ) ) {
    33 	if ( isset( $attributes['textAlign'] ) ) {
    32 		$classes .= ' has-text-align-' . $attributes['textAlign'];
    34 		$classes[] = 'has-text-align-' . $attributes['textAlign'];
       
    35 	}
       
    36 	if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
       
    37 		$classes[] = 'has-link-color';
    33 	}
    38 	}
    34 
    39 
    35 	$separator = empty( $attributes['separator'] ) ? ' ' : $attributes['separator'];
    40 	$separator = empty( $attributes['separator'] ) ? ' ' : $attributes['separator'];
    36 
    41 
    37 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
    42 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
       
    43 
       
    44 	$prefix = "<div $wrapper_attributes>";
       
    45 	if ( isset( $attributes['prefix'] ) && $attributes['prefix'] ) {
       
    46 		$prefix .= '<span class="wp-block-post-terms__prefix">' . $attributes['prefix'] . '</span>';
       
    47 	}
       
    48 
       
    49 	$suffix = '</div>';
       
    50 	if ( isset( $attributes['suffix'] ) && $attributes['suffix'] ) {
       
    51 		$suffix = '<span class="wp-block-post-terms__suffix">' . $attributes['suffix'] . '</span>' . $suffix;
       
    52 	}
    38 
    53 
    39 	return get_the_term_list(
    54 	return get_the_term_list(
    40 		$block->context['postId'],
    55 		$block->context['postId'],
    41 		$attributes['term'],
    56 		$attributes['term'],
    42 		"<div $wrapper_attributes>",
    57 		wp_kses_post( $prefix ),
    43 		'<span class="wp-block-post-terms__separator">' . esc_html( $separator ) . '</span>',
    58 		'<span class="wp-block-post-terms__separator">' . esc_html( $separator ) . '</span>',
    44 		'</div>'
    59 		wp_kses_post( $suffix )
    45 	);
    60 	);
    46 }
    61 }
    47 
    62 
    48 /**
    63 /**
       
    64  * Returns the available variations for the `core/post-terms` block.
       
    65  *
       
    66  * @since 6.5.0
       
    67  *
       
    68  * @return array The available variations for the block.
       
    69  */
       
    70 function block_core_post_terms_build_variations() {
       
    71 	$taxonomies = get_taxonomies(
       
    72 		array(
       
    73 			'publicly_queryable' => true,
       
    74 			'show_in_rest'       => true,
       
    75 		),
       
    76 		'objects'
       
    77 	);
       
    78 
       
    79 	// Split the available taxonomies to `built_in` and custom ones,
       
    80 	// in order to prioritize the `built_in` taxonomies at the
       
    81 	// search results.
       
    82 	$built_ins         = array();
       
    83 	$custom_variations = array();
       
    84 
       
    85 	// Create and register the eligible taxonomies variations.
       
    86 	foreach ( $taxonomies as $taxonomy ) {
       
    87 		$variation = array(
       
    88 			'name'        => $taxonomy->name,
       
    89 			'title'       => $taxonomy->label,
       
    90 			'description' => sprintf(
       
    91 				/* translators: %s: taxonomy's label */
       
    92 				__( 'Display a list of assigned terms from the taxonomy: %s' ),
       
    93 				$taxonomy->label
       
    94 			),
       
    95 			'attributes'  => array(
       
    96 				'term' => $taxonomy->name,
       
    97 			),
       
    98 			'isActive'    => array( 'term' ),
       
    99 			'scope'       => array( 'inserter', 'transform' ),
       
   100 		);
       
   101 		// Set the category variation as the default one.
       
   102 		if ( 'category' === $taxonomy->name ) {
       
   103 			$variation['isDefault'] = true;
       
   104 		}
       
   105 		if ( $taxonomy->_builtin ) {
       
   106 			$built_ins[] = $variation;
       
   107 		} else {
       
   108 			$custom_variations[] = $variation;
       
   109 		}
       
   110 	}
       
   111 
       
   112 	return array_merge( $built_ins, $custom_variations );
       
   113 }
       
   114 
       
   115 /**
    49  * Registers the `core/post-terms` block on the server.
   116  * Registers the `core/post-terms` block on the server.
       
   117  *
       
   118  * @since 5.8.0
    50  */
   119  */
    51 function register_block_core_post_terms() {
   120 function register_block_core_post_terms() {
    52 	register_block_type_from_metadata(
   121 	register_block_type_from_metadata(
    53 		__DIR__ . '/post-terms',
   122 		__DIR__ . '/post-terms',
    54 		array(
   123 		array(
    55 			'render_callback' => 'render_block_core_post_terms',
   124 			'render_callback'    => 'render_block_core_post_terms',
       
   125 			'variation_callback' => 'block_core_post_terms_build_variations',
    56 		)
   126 		)
    57 	);
   127 	);
    58 }
   128 }
    59 add_action( 'init', 'register_block_core_post_terms' );
   129 add_action( 'init', 'register_block_core_post_terms' );