File: /var/www/html/insiders/wp-load/wp-content/plugins/gutenmate/block-library/post-terms.php
<?php
/**
* Renders the `gtm/post-terms` block on the server.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
* @return string Returns the post terms for the current post.
*/
function gtm_render_block_post_terms( $attr, $content, $block ) {
if ( ! isset( $block->context['postId'] ) || ! isset( $attr['term'] ) ) {
return '';
}
if ( ! is_taxonomy_viewable( $attr['term'] ) ) {
return '';
}
$post_terms = get_the_terms( $block->context['postId'], $attr['term'] );
if ( is_wp_error( $post_terms ) || empty( $post_terms ) ) {
return '';
}
$separator = empty( $attr['separator'] ) ? '' : $attr['separator'];
$wrapper_attributes = get_block_wrapper_attributes();
$item_prefix = '<span class="wp-block-gtm-post-terms__item">';
$item_suffix = '</span>';
$icon_html = '';
if ( ! empty( $attr['itemIcon'] ) ) {
$item_prefix .= '<i class="gtm-icon ' . esc_attr( $attr['itemIcon'] ) . '"></i>';
$icon_html = '<i class="gtm-icon ' . esc_attr( $attr['itemIcon'] ) . '"></i>';
}
return gtm_get_the_term_list(
$block->context['postId'],
$attr['term'],
[
'before' => "<div $wrapper_attributes>",
'after' => "</div>",
'sep' => $separator ? '<span class="wp-block-gtm-post-terms__separator">' . $separator . '</span>' : '',
'item_class' => 'wp-block-gtm-post-terms__item',
'name_before' => $icon_html,
'name_after' => '',
'limit' => $attr['itemsLimit'] ?? '',
]
);
}
/**
* Registers the `gtm/post-terms` block on the server.
*/
function gtm_register_block_post_terms() {
gtm_register_block_type( 'post-terms', [
'render_callback' => 'gtm_render_block_post_terms',
] );
}
add_action( 'init', 'gtm_register_block_post_terms', 20 );
add_filter( 'gtm.BlockStyleCompiler.gtm/post-terms', 'gtm_compile_block_css_post_terms', 10, 3 );
function gtm_compile_block_css_post_terms( $output, $attributes, $blockName ) {
$block_style = ['classes' => [], 'style' => []];
$block_style['classes'][] = 'gtm-block';
if ( $attributes['enableItemUnderline'] ) {
$block_style['classes'][] = 'gtm-has-post-terms-link-underline';
}
if ( $attributes['enableItemHoverUnderline'] ) {
$block_style['classes'][] = 'gtm-has-post-terms-link-hover-underline';
}
gtm_css_compileCssVarClass( $block_style, "post-terms-taxonony", $attributes['term'] );
gtm_css_compileCssVar( $block_style, "post-terms-spacing", gtm_css_parseSpacingValue( $attributes['itemSpacing'] ) );
gtm_css_compileCssVar( $block_style, "post-terms-item-padding", gtm_css_parseResponsiveBoxProps( $attributes['itemPadding'] ) );
gtm_css_compileCssVar( $block_style, "post-terms-item-radius", gtm_css_parseBorderRadiusValue( $attributes['itemRadius'] ) );
if ( $attributes['itemSkew'] ?? '' ) {
gtm_css_compileCssVar( $block_style, "post-terms-item-skew", $attributes['itemSkew'] . 'deg' );
}
gtm_css_compileCssVar( $block_style, "post-terms-item-color", gtm_css_parseColorValue( $attributes['itemColor'] ) );
gtm_css_compileCssVar( $block_style, "post-terms-item-bg", gtm_css_parseColorValue( $attributes['itemBg'] ) );
gtm_css_compileCssVar( $block_style, "post-terms-item-hover-color", gtm_css_parseColorValue( $attributes['itemHoverColor'] ) );
gtm_css_compileCssVar( $block_style, "post-terms-item-hover-bg", gtm_css_parseColorValue( $attributes['itemHoverBg'] ) );
gtm_css_compileCssVar( $block_style, "post-terms-item-icon-size", $attributes['itemIconSize'] );
gtm_css_compileCssVar( $block_style, "post-terms-item-icon-spacing", gtm_css_parseSpacingValue( $attributes['itemIconSpacing'] ) );
gtm_css_compileCssVar( $block_style, "post-terms-item-icon-color", gtm_css_parseColorValue( $attributes['itemIconColor'] ) );
gtm_css_compileCssVar( $block_style, "post-terms-item-icon-hover-color", gtm_css_parseColorValue( $attributes['itemIconHoverColor'] ) );
gtm_css_compileCssVar( $block_style, "post-terms-separator-color", gtm_css_parseColorValue( $attributes['separatorColor'] ) );
// Apply to output
$block_style['style'] = ['{{BLOCK}}' => $block_style['style']];
return [
'classes' => array_merge( $output['classes'], $block_style['classes'] ),
'style' => array_replace_recursive( $output['style'], $block_style['style'] ),
];
}
function gtm_get_the_term_list( $post_id, $taxonomy, $args = [] ) {
$args = wp_parse_args( $args, [
'before' => '',
'after' => '',
'sep' => '',
'item_class' => '',
'item_before' => '',
'item_after' => '',
'name_before' => '',
'name_after' => '',
'limit' => '',
] );
$terms = get_the_terms( $post_id, $taxonomy );
if ( is_wp_error( $terms ) ) {
return $terms;
}
if ( empty( $terms ) ) {
return false;
}
if ( intval( $args['limit'] ) ) {
$terms = array_slice( $terms, 0, intval( $args['limit'] ) );
}
$links = [];
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) ) {
return $link;
}
$class = '';
if ( $args['item_class'] ) {
$class = sprintf( 'class="%s"', esc_attr( $args['item_class'] ) );
}
$tooltip = gtm_tooltip( sprintf( esc_html__( "View posts in %s", 'gutenmate' ), $term->name ) );
$links[] = $args['item_before'] . sprintf( '<a %1$s href="%2$s" rel="tag" %3$s>', $class, esc_url( $link ), $tooltip )
. $args['name_before']
. esc_html( $term->name )
. $args['name_after']
. '</a>'
. $args['item_after'];
}
return $args['before'] . implode( $args['sep'], $links ) . $args['after'];
}