PluginProbe
WebberZone Top 10 — Popular Posts / 4.5.2
WebberZone Top 10 — Popular Posts v4.5.2
4.5.2 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 All 118 releases
← All changes | includes/util/class-cache.php +66 -5 4.4.1 → 4.5.2 View file →
@@ -6,8 +6,9 @@
6 6 */
7 7
8 8 namespace WebberZone\Top_Ten\Util;
9 9
10 +use WebberZone\Top_Ten\Frontend\Language_Handler;
10 11 use WebberZone\Top_Ten\Util\Hook_Registry;
11 12
12 13 if ( ! defined( 'WPINC' ) ) {
13 14 die;
@@ -35,19 +36,27 @@
35 36 * @since 2.2.0
36 37 */
37 38 public function ajax_clearcache() {
38 39
39 - if ( ! current_user_can( 'manage_options' ) ) {
40 + $network_wide = isset( $_POST['network_wide'] ) && '1' === sanitize_key( wp_unslash( $_POST['network_wide'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
41 + $capability = $network_wide ? 'manage_network_options' : 'manage_options';
42 +
43 + if ( ! current_user_can( $capability ) ) {
40 44 wp_die();
41 45 }
42 46 check_ajax_referer( 'tptn-admin', 'security' );
43 47
44 - $count = $this->delete();
48 + $count = $network_wide ? self::delete_network() : $this->delete();
45 49
46 50 wp_send_json_success(
47 51 array(
48 52 /* translators: 1: Number of entries cleared. */
49 - 'message' => sprintf( _n( '%s entry cleared', '%s entries cleared', $count, 'top-10' ), number_format_i18n( $count ) ),
53 + 'message' => sprintf(
54 + $network_wide
55 + ? _n( '%s cache entry cleared across the network', '%s cache entries cleared across the network', $count, 'top-10' )
56 + : _n( '%s entry cleared', '%s entries cleared', $count, 'top-10' ),
57 + number_format_i18n( $count )
58 + ),
50 59 )
51 60 );
52 61 }
53 62
@@ -79,8 +88,53 @@
79 88 return $loop;
80 89 }
81 90
82 91 /**
92 + * Delete the Top 10 cache across all active network sites.
93 + *
94 + * @since 4.5.0
95 + *
96 + * @return int Number of transients deleted.
97 + */
98 + public static function delete_network() {
99 + if ( ! is_multisite() ) {
100 + return self::delete();
101 + }
102 +
103 + $site_ids = wp_list_pluck(
104 + get_sites(
105 + array(
106 + 'archived' => 0,
107 + 'spam' => 0,
108 + 'deleted' => 0,
109 + 'number' => 0,
110 + )
111 + ),
112 + 'blog_id'
113 + );
114 + $count = 0;
115 + $current_blog_id = get_current_blog_id();
116 +
117 + foreach ( $site_ids as $site_id ) {
118 + $site_id = absint( $site_id );
119 + $switched = $site_id !== $current_blog_id;
120 + if ( $switched ) {
121 + switch_to_blog( $site_id );
122 + }
123 +
124 + try {
125 + $count += self::delete();
126 + } finally {
127 + if ( $switched ) {
128 + restore_current_blog();
129 + }
130 + }
131 + }
132 +
133 + return $count;
134 + }
135 +
136 + /**
83 137 * Get the default meta keys used for the cache
84 138 *
85 139 * @return array Transient meta keys
86 140 */
@@ -302,10 +356,10 @@
302 356 unset( $args[ $key ] );
303 357 }
304 358 }
305 359
306 - // Generate cache key.
307 - return md5( wp_json_encode( $args ) );
360 + // Keep generated keys discoverable by get_widget_keys() and delete().
361 + return 'tptn_cache_' . md5( Language_Handler::get_cache_language() . '|' . wp_json_encode( $args ) );
308 362 }
309 363
310 364 /**
311 365 * Get the transient names for the Top 10 widgets.
@@ -332,7 +386,14 @@
332 386 $keys[] = str_replace( '_transient_', '', $result->option_name );
333 387 }
334 388 }
335 389
390 + /**
391 + * Filters the list of Top 10 cache keys found in the options table.
392 + *
393 + * @since 2.5.0
394 + *
395 + * @param string[] $keys Cache keys without the transient prefix.
396 + */
336 397 return apply_filters( 'tptn_cache_get_widget_keys', $keys );
337 398 }
338 399 }