Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 78 additions & 16 deletions php/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace XWP\SiteCounts;

use WP_Block;
use WP_Query;

/**
* The Site Counts dynamic block.
Expand Down Expand Up @@ -63,28 +64,89 @@ public function register_block() {
*/
public function render_callback( $attributes, $content, $block ) {
$post_types = get_post_types( [ 'public' => true ] );
$class_name = $attributes['className'];
$class_name = $block->block_type->attributes['className'];
ob_start();

?>
<div class="<?php echo $class_name; ?>">
<h2>Post Counts</h2>
<div class="<?php echo esc_attr( $class_name ); ?>">
<h2>
<?php
esc_html_e( 'Post Counts', 'site-counts' );
?>
</h2>
<ul>
<?php
foreach ( $post_types as $post_type_slug ) :
$post_type_object = get_post_type_object( $post_type_slug );
$post_count = count(
get_posts(
[
'post_type' => $post_type_slug,
'posts_per_page' => -1,
]
)
);

?>
<li>
<?php
echo esc_html( 'There are ', 'site-counts' ) . esc_attr( $post_count ) . esc_html( '&nbsp;', 'site-counts' ) . esc_attr( $post_type_object->labels->name ) . esc_html( '.', 'site-counts' );
?>
</li>
<?php
endforeach;
?>
</ul>
<p>
<?php
$current_post_id = get_the_ID();
echo esc_html( 'The current post ID is ', 'site-counts' ) . esc_attr( $current_post_id ) . esc_html( '.', 'site-counts' );
?>
</p>

<?php
foreach ( $post_types as $post_type_slug ) :
$post_type_object = get_post_type_object( $post_type_slug );
$post_count = count(
get_posts(
[
'post_type' => $post_type_slug,
'posts_per_page' => -1,
]
)
);
$query = new WP_Query(
( [
'post_type' => [ 'post', 'page' ],
'post_status' => 'any',
'date_query' => ( [
( [
'hour' => 9,
'compare' => '>=',
] ),
( [
'hour' => 17,
'compare' => '<=',
] ),
] ),
'tag' => 'foo',
'category_name' => 'baz',
'post__not_in' => [ get_the_ID() ],
] )
);

if ( $query->found_posts ) :
?>
<p><?php echo 'There are ' . $post_count . ' ' . $post_type_object->labels->name . '.'; ?></p>
<?php endforeach; ?>
<p><?php echo 'The current post ID is ' . $_GET['post_id'] . '.'; ?></p>
<h2>
<?php
esc_html__( 'Any 5 posts with the tag of foo and the category of baz', 'site-counts' );
?>
</h2>
<ul>
<?php

foreach ( array_slice( $query->posts, 0, 5 ) as $post ) :
?>
<li>
<?php
echo esc_attr( $post->post_title );
?>
</li>
<?php
endforeach;
endif;
?>
</ul>
</div>
<?php

Expand Down