plugin = $plugin;
}
/**
* Adds the action to register the block.
*
* @return void
*/
public function init() {
add_action( 'init', [ $this, 'register_block' ] );
}
/**
* Registers the block.
*/
public function register_block() {
register_block_type_from_metadata(
$this->plugin->dir(),
[
'render_callback' => [ $this, 'render_callback' ],
]
);
}
/**
* Renders the block.
*
* @param array $attributes The attributes for the block.
* @param string $content The block content, if any.
* @param WP_Block $block The instance of this block.
* @return string The markup of the block.
*/
public function render_callback( $attributes, $content, $block ) {
$class_name = $attributes['className'];
$post_id = get_the_ID();
ob_start();
?>
Post Counts
get_post_type_counter() as $post_label => $post_count ) : ?>
-
get_posts_by_category( 'baz', 30 ); ?>
filter_tag_posts( $baz_posts, 'foo', $post_id ); ?>
Any 5 posts with the tag of foo and the category of baz
[ 'post', 'page' ],
'post_status' => 'any',
'fields' => 'ids',
'date_query' => [
[
'hour' => 9,
'compare' => '>=',
],
[
'hour' => 17,
'compare' => '<=',
],
],
'tax_query' => [
[
'taxonomy' => 'category',
'field' => 'name',
'terms' => $category_name,
'include_children' => false,
],
],
'no_found_rows' => true,
'update_post_meta_cache' => false,
'posts_per_page' => $posts_per_page,
]
);
return $query;
}
/**
* Creates an array that will count number of posts per post type.
*
* @return array Associative array with 'label name' => number of posts.
*/
protected function get_post_type_counter() {
$post_types = get_post_types( [ 'public' => true ] );
$counter = [];
foreach ( $post_types as $post_type_slug ) {
$status = ( 'attachment' === $post_type_slug ) ? 'inherit' : 'publish';
$post_count = wp_count_posts( $post_type_slug )->$status;
$post_type_object = get_post_type_object( $post_type_slug );
$counter[ $post_type_object->labels->name ] = $post_count;
}
return $counter;
}
/**
* Given a Query result and a tag name, filter posts that has $tag_name associated.
* We also mock the WP_Query functionality $post__not_in.
*
* @param WP_Query $query WP_Query result where we will filter from.
* @param string $tag_name Tag Name to filter.
* @param int $post__not_in Specify post NOT to retrieve.
*
* @return array
*/
protected function filter_tag_posts( WP_Query $query, $tag_name, $post__not_in ) {
$post_ids = [];
if ( $query->have_posts() ) {
foreach ( $query->posts as $post_id ) {
if ( ! $this->post_has_tag( $post_id, $tag_name ) ) {
continue;
}
if ( $post_id === $post__not_in ) {
continue;
}
$post_ids[] = $post_id;
}
}
return $post_ids;
}
/**
* Returns if a given post_id has $tag_name tag associated.
*
* @param int $post_id Post_id to look up.
* @param string $tag_name Tag name to be looked up.
*
* @return bool
*/
protected function post_has_tag( $post_id, $tag_name ) {
$tags = get_the_tags( $post_id );
foreach ( $tags as $tag ) {
if ( $tag->name === $tag_name ) {
return true;
}
}
return false;
}
}