PluginProbe
WebberZone Top 10 — Popular Posts / 4.5.1
WebberZone Top 10 — Popular Posts v4.5.1
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
← All changes | includes/frontend/blocks/class-blocks.php +72 -25 4.4.1 → 4.5.1 View file →
@@ -23,8 +23,15 @@
23 23 */
24 24 class Blocks {
25 25
26 26 /**
27 + * Request-local cache for post counts rendered by the Post Count block.
28 + *
29 + * @var array<string, int|string>
30 + */
31 + private static $post_count_cache = array();
32 +
33 + /**
27 34 * Register blocks with WordPress.
28 35 */
29 36 public function __construct() {
30 37 Hook_Registry::add_action( 'init', array( $this, 'register_blocks' ) );
@@ -32,8 +39,22 @@
32 39 Hook_Registry::add_filter( 'block_editor_rest_api_preload_paths', array( $this, 'add_custom_preload_paths' ) );
33 40 }
34 41
35 42 /**
43 + * Add custom preload paths for the REST API.
44 + *
45 + * @since 4.5.0
46 + *
47 + * @param array $preload_paths Existing preload paths.
48 + * @return array Modified preload paths.
49 + */
50 + public static function add_custom_preload_paths( $preload_paths ) {
51 + $preload_paths[] = '/wp/v2/top-10/v1/counter';
52 +
53 + return $preload_paths;
54 + }
55 +
56 + /**
36 57 * Registers the block using the metadata loaded from the `block.json` file.
37 58 * Behind the scenes, it registers also all assets so they can be enqueued
38 59 * through the block editor in the corresponding context.
39 60 *
@@ -40,9 +61,9 @@
40 61 * @since 3.1.0
41 62 */
42 63 public function register_blocks() {
43 64 // Define an array of blocks with their paths and optional render callbacks.
44 - $blocks = array(
65 + $blocks = array(
45 66 'popular-posts' => array(
46 67 'path' => __DIR__ . '/build/popular-posts/',
47 68 'render_callback' => array( $this, 'render_block_popular_posts' ),
48 69 ),
@@ -50,8 +71,9 @@
50 71 'path' => __DIR__ . '/build/post-count/',
51 72 'render_callback' => array( $this, 'render_block_post_count' ),
52 73 ),
53 74 );
75 + $default_blocks = $blocks;
54 76
55 77 /**
56 78 * Filters the blocks registered by the plugin.
57 79 *
@@ -58,10 +80,31 @@
58 80 * @since 4.0.0
59 81 *
60 82 * @param array $blocks Array of blocks registered by the plugin.
61 83 */
62 - $blocks = apply_filters( 'tptn_register_blocks', $blocks );
84 + $blocks = apply_filters( 'tptn_register_blocks', $blocks );
85 + $manifest = __DIR__ . '/build/blocks-manifest.php';
63 86
87 + if ( function_exists( 'wp_register_block_types_from_metadata_collection' ) && file_exists( $manifest ) ) {
88 + wp_register_block_types_from_metadata_collection( __DIR__ . '/build', $manifest );
89 +
90 + // Register any blocks added or changed by the filter that fall outside the manifest.
91 + foreach ( $blocks as $block_name => $block_data ) {
92 + if ( isset( $default_blocks[ $block_name ] ) && $default_blocks[ $block_name ] === $block_data ) {
93 + continue;
94 + }
95 +
96 + $args = array();
97 + if ( isset( $block_data['render_callback'] ) ) {
98 + $args['render_callback'] = $block_data['render_callback'];
99 + }
100 +
101 + register_block_type( $block_data['path'], $args );
102 + }
103 +
104 + return;
105 + }
106 +
64 107 // Loop through each block and register it.
65 108 foreach ( $blocks as $block_name => $block_data ) {
66 109 $args = array();
67 110
@@ -154,10 +197,10 @@
154 197 $output = '';
155 198 $args = array();
156 199
157 200 $post_id = absint( $block->context['postId'] );
158 - $counter = isset( $attributes['counter'] ) ? $attributes['counter'] : 'total';
159 - $blog_id = isset( $attributes['blogId'] ) ? $attributes['blogId'] : get_current_blog_id();
201 + $counter_type = isset( $attributes['counter'] ) ? $attributes['counter'] : 'total';
202 + $blog_id = isset( $attributes['blogId'] ) ? absint( $attributes['blogId'] ) : get_current_blog_id();
160 203 $from_date = isset( $attributes['fromDate'] ) ? $attributes['fromDate'] : '';
161 204 $to_date = isset( $attributes['toDate'] ) ? $attributes['toDate'] : '';
162 205 $text_before = isset( $attributes['textBefore'] ) ? $attributes['textBefore'] : '';
163 206 $text_after = isset( $attributes['textAfter'] ) ? $attributes['textAfter'] : '';
@@ -178,10 +221,8 @@
178 221 if ( ! empty( $to_date ) ) {
179 222 $args['to_date'] = $to_date;
180 223 }
181 224
182 - $counter = Counter::get_post_count_only( $post_id, $counter, $blog_id, $args );
183 -
184 225 $classes = array();
185 226 $classes[] = 'wp-block-tptn-post-count';
186 227 $classes[] = 'tptn-post-count';
187 228
@@ -191,22 +232,24 @@
191 232 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
192 233 $classes[] = 'has-link-color';
193 234 }
194 235
195 - if ( ! $advanced_mode ) {
196 - $output = sprintf(
236 + $counter = null;
237 + if ( ! $advanced_mode || empty( $text_advanced ) ) {
238 + $counter = self::get_cached_post_count( $post_id, $counter_type, $blog_id, $args );
239 + $output = sprintf(
197 240 '%1$s%2$s%3$s',
198 241 wp_kses_post( (string) $text_before ),
199 242 esc_html( (string) $counter ),
200 243 wp_kses_post( (string) $text_after )
201 244 );
202 - } elseif ( ! empty( $text_advanced ) ) {
245 + } else {
203 246 $classes[] = 'tptn-advanced-mode';
204 247 $output = $text_advanced;
205 248
206 249 foreach ( array( 'total', 'daily', 'overall' ) as $type ) {
207 250 if ( false !== strpos( $text_advanced, "%{$type}count%" ) ) {
208 - $count = Counter::get_post_count_only( $post_id, $type, $blog_id, $args );
251 + $count = self::get_cached_post_count( $post_id, $type, $blog_id, $args );
209 252
210 253 $output = str_replace(
211 254 "%{$type}count%",
212 255 (string) $count,
@@ -257,9 +300,8 @@
257 300 $icon = sprintf(
258 301 '<span class="tptn-post-count-icon">%1$s</span>',
259 302 wp_kses( $svg_code_with_style, self::get_allowed_svg_tags() )
260 303 );
261 - $icon = '<span class="tptn-post-count-icon">' . $svg_code_with_style . '</span>';
262 304 }
263 305 if ( ! empty( $icon ) ) {
264 306 if ( 'before' === $svg_icon_location ) {
265 307 $output = $icon . $output;
@@ -277,8 +319,27 @@
277 319 );
278 320 }
279 321
280 322 /**
323 + * Get a request-cached post count.
324 + *
325 + * @param int $post_id Post ID.
326 + * @param string $counter Counter type.
327 + * @param int $blog_id Blog ID.
328 + * @param array $args Additional counter arguments.
329 + * @return int|string Post count.
330 + */
331 + private static function get_cached_post_count( $post_id, $counter, $blog_id, $args ) {
332 + $cache_key = md5( (string) wp_json_encode( array( $post_id, $counter, $blog_id, $args ) ) );
333 +
334 + if ( ! array_key_exists( $cache_key, self::$post_count_cache ) ) {
335 + self::$post_count_cache[ $cache_key ] = Counter::get_post_count_only( $post_id, $counter, $blog_id, $args );
336 + }
337 +
338 + return self::$post_count_cache[ $cache_key ];
339 + }
340 +
341 + /**
281 342 * Get allowed SVG tags for wp_kses.
282 343 *
283 344 * @since 4.0.0
284 345 *
@@ -464,20 +525,6 @@
464 525 );
465 526 wp_add_inline_style( "popular-posts-block-editor-{$style}", $extra_css );
466 527 }
467 528 }
468 - }
469 -
470 - /**
471 - * Add custom preload paths for the REST API.
472 - *
473 - * @since 4.0.0
474 - *
475 - * @param array $preload_paths Existing preload paths.
476 - * @return array Modified preload paths.
477 - */
478 - public static function add_custom_preload_paths( $preload_paths ) {
479 - $preload_paths[] = '/wp/v2/top-10/v1/counter';
480 -
481 - return $preload_paths;
482 529 }
483 530 }