HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/insiders/wp-load/wp-content/plugins/breadly/block-library/post-template.php
<?php

// https://github.com/WordPress/gutenberg/blob/9a761d33ba7aeaec0d8c850b3c8081849974dc7a/packages/block-library/src/post-template/index.php
function gtm_render_block_post_template( $attributes, $content, $block ) {
	$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
	$page     = empty( $_GET[$page_key] ) ? 1 : (int) $_GET[$page_key];

	/* ME start */
	// DON'T update the query code. The new query code in Gutenberg may use global query without re-query.
	// But we need to always re-query to apply an extra filters.
	/* ME end */
	$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, $attributes );
	$query              = new WP_Query( $query_args );

	if ( ! $query->have_posts() ) {
		return '';
	}

	if ( block_core_post_template_uses_featured_image( $block->inner_blocks ) ) {
		update_post_thumbnail_cache( $query );
	}

	$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']}";
		}
	}
	if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
		$classnames .= ' has-link-color';
	}

	// Ensure backwards compatibility by flagging the number of columns via classname when using grid layout.
	if ( isset( $attributes['layout']['type'] ) && 'grid' === $attributes['layout']['type'] && ! empty( $attributes['layout']['columnCount'] ) ) {
		$classnames .= ' ' . sanitize_title( 'columns-' . $attributes['layout']['columnCount'] );
	}

	$wrapper_attributes = get_block_wrapper_attributes( ['class' => trim( $classnames )] );

	$content   = '';
	/* ME */$i = 0;
	while ( $query->have_posts() ) {
		/* ME start */
		if ( ++$i <= ( intval( $attributes['offset'] ?? 0 ) ) ) {
			$query->the_post();
			$i++;
			continue;
		} else if ( ! empty( $attributes['limit'] ) && $i > intval( $attributes['offset'] ?? 0 ) + intval( $attributes['limit'] ) ) {
			break;
		}
		/* ME end */
		$query->the_post();

		// Get an instance of the current Post Template block.
		$block_instance = $block->parsed_block;

		// Set the block name to one that does not correspond to an existing registered block.
		// This ensures that for the inner instances of the Post Template block, we do not render any block supports.
		$block_instance['blockName'] = 'core/null';

		// Render the inner blocks of the Post Template block with `dynamic` set to `false` to prevent calling
		// `render_callback` and ensure that no wrapper markup is included.
		$block_content = (
			new WP_Block(
				$block_instance,
				[
					'postType' => get_post_type(),
					'postId'   => get_the_ID(),
				]
			)
		)->render( ['dynamic' => false] );

		// Wrap the render inner blocks in a `li` element with the appropriate post classes.
		$post_classes = implode( ' ', get_post_class( 'wp-block-post' ) );
		/* ME start */
		if ( ! empty( $attributes['postClass'] ) ) {
			$post_classes = trim( $post_classes . ' ' . $attributes['postClass'] );
		}
		/* ME end */
		$content .= '<li class="' . esc_attr( $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'] ),
	];
}