File: /var/www/html/insiders/wp-load/wp-content/plugins/gutenmate/block-library/post-template.php
<?php
function gtm_render_block_post_template( $attr, $content, $block ) {
// ORIGINAL from https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/post-template/index.php
// Commits on Jan 13, 2022
$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
$page = empty( $_GET[$page_key] ) ? 1 : (int) $_GET[$page_key];
$query_args = gtm_build_query_vars_from_query_block( $block, $page );
// Override the custom query with the global query if needed.
$use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
if ( $use_global_query ) {
global $wp_query;
if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) {
// Unset `offset` because if is set, $wp_query overrides/ignores the paged parameter and breaks pagination.
unset( $query_args['offset'] );
$query_args = wp_parse_args( $wp_query->query_vars, $query_args );
if ( empty( $query_args['post_type'] ) && is_singular() ) {
$query_args['post_type'] = get_post_type( get_the_ID() );
}
}
}
/* ME */$query_args = gtm_apply_post_template_query_args( $query_args, $block, $page, $attr );
$query = new WP_Query( $query_args );
if ( ! $query->have_posts() ) {
return '';
}
$classnames = '';
if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) {
if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) {
$classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}";
}
}
$wrapper_attributes = get_block_wrapper_attributes( ['class' => $classnames] );
$content = '';
/* ME */$i = 0;
while ( $query->have_posts() ) {
/* ME start */
if ( ++$i <= ( $attr['offset'] ?? 0 ) ) {
$query->the_post();
$i++;
continue;
} else if ( ! empty( $attr['limit'] ) && $i > ( $attr['offset'] ?? 0 ) + $attr['limit'] ) {
break;
}
/* ME end */
$query->the_post();
$block_content = (
new WP_Block(
$block->parsed_block,
[
'postType' => get_post_type(),
'postId' => get_the_ID(),
]
)
)->render( ['dynamic' => false] );
$post_classes = implode( ' ', get_post_class( 'wp-block-post' ) );
/* ME start */
if ( ! empty( $attr['postClass'] ) ) {
$post_classes .= ' ' . $attr['postClass'];
}
/* ME end */
$content .= '<li class="' . esc_attr( trim( $post_classes ) ) . '">' . $block_content . '</li>';
}
wp_reset_postdata();
return sprintf(
'<ul %1$s>%2$s</ul>',
$wrapper_attributes,
$content
);
}
function gtm_apply_post_template_query_args( $query, $block, $page, $attr ) {
return gtm_apply_extended_query_filters( $query, [
'gtmQueryFilter' => $block->context['gtmQueryFilter'] ?? '',
'gtmQueryLimitDays' => $block->context['gtmQueryLimitDays'] ?? 0,
'gtmQueryUseRandom' => $block->context['gtmQueryUseRandom'] ?? false,
'gtmQuerySlug' => $block->context['gtmQuerySlug'] ?? '',
] );
}
function gtm_apply_extended_query_filters( $query, $attributes ) {
if ( ! empty( $attributes['gtmQueryFilter'] ) ) {
$filter = sanitize_text_field( $attributes['gtmQueryFilter'] );
if ( 'review' == $filter ) {
$query['meta_query'][] = [
'key' => 'gtm_review_score',
'compare' => 'EXISTS',
];
} else if ( 'high-score-review' == $filter ) {
$query['meta_query'][] = [
'key' => 'gtm_review_score',
'compare' => 'EXISTS',
];
$query['meta_key'] = 'gtm_review_score';
$query['orderby'] = 'meta_value_num';
$query['order'] = 'DESC';
} else if ( 'most-commented' == $filter ) {
$query['orderby'] = 'comment_count';
$query['order'] = 'DESC';
} else if ( 'same-cat' == $filter ) {
if ( is_single() ) {
$post_id = get_queried_object_id();
$cats = wp_get_post_categories( $post_id, ['fields' => 'ids'] );
if ( $cats ) {
$query['category__in'] = $cats;
$query['post__not_in'] = [$post_id];
}
}
} else if ( 'same-tag' == $filter ) {
if ( is_single() ) {
$post_id = get_queried_object_id();
$cats = wp_get_post_tags( $post_id, ['fields' => 'ids'] );
if ( $cats ) {
$query['tag__in'] = $cats;
$query['post__not_in'] = [$post_id];
}
}
} else if ( 'popular-1d' == $filter ) {
$popular_posts_ids = gtm_get_popular_posts( ['range' => 'last24hours', 'limit' => $query["posts_per_page"] ?? 10] );
if ( ! empty( $popular_posts_ids ) ) {
$query['post__in'] = $popular_posts_ids;
$query['orderby'] = 'post__in';
}
} else if ( 'popular-7d' == $filter ) {
$popular_posts_ids = gtm_get_popular_posts( ['range' => 'last7days', 'limit' => $query["posts_per_page"] ?? 10] );
if ( ! empty( $popular_posts_ids ) ) {
$query['post__in'] = $popular_posts_ids;
$query['orderby'] = 'post__in';
}
} else if ( 'popular-30d' == $filter ) {
$popular_posts_ids = gtm_get_popular_posts( ['range' => 'last30days', 'limit' => $query["posts_per_page"] ?? 10] );
if ( ! empty( $popular_posts_ids ) ) {
$query['post__in'] = $popular_posts_ids;
$query['orderby'] = 'post__in';
}
}
}
// Disable this option for populat posts
if ( ! empty( $attributes['gtmQueryLimitDays'] ) && ! isset( $popular_posts_ids ) ) {
$days = intval( $attributes['gtmQueryLimitDays'] ) ?? 0;
if ( $days ) {
$query['date_query']['after'] = "-{$days} days";
}
}
if ( ! empty( $attributes['gtmQueryUseRandom'] ) ) {
$query['orderby'] = 'rand';
}
if ( ! empty( $attributes['gtmQuerySlug'] ) ) {
$query['gtmQuerySlug'] = $attributes['gtmQuerySlug'];
}
return apply_filters( 'gtm_apply_extended_query_filters', $query );
}
function gtm_get_popular_posts( $options = [] ) {
$options = wp_parse_args( $options, [
'range' => 'last7days',
'limit' => 10,
'order_by' => 'views',
] );
if ( class_exists( 'WordPressPopularPosts\Query' ) ) {
$popular_posts = new WordPressPopularPosts\Query( $options );
$popular_posts_all = $popular_posts->get_posts();
$popular_posts_ids = [];
if ( ! empty( $popular_posts_all ) ) {
foreach ( $popular_posts_all as $post ) {
$popular_posts_ids[] = $post->id;
}
return $popular_posts_ids;
}
}
}
/**
* Registers the `ggtm/post-template` block on the server.
*/
function gtm_register_block_post_template() {
gtm_register_block_type( 'post-template', [
'render_callback' => 'gtm_render_block_post_template',
'skip_inner_blocks' => true,
] );
}
add_action( 'init', 'gtm_register_block_post_template' );
add_filter( 'gtm.BlockStyleCompiler.gtm/post-template', 'gtm_compile_block_css_post_template', 10, 3 );
function gtm_compile_block_css_post_template( $output, $attributes, $blockName ) {
$block_style = ['classes' => [], 'style' => []];
$block_style['classes'][] = 'gtm-block';
gtm_css_compileCssVar( $block_style, "post-template-grid-gap", gtm_css_parseSpacingValue( $attributes['gridGap'] ) );
// 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'] ),
];
}