-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathExtension_Genesis_Plugin.php
516 lines (452 loc) · 14.4 KB
/
Extension_Genesis_Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
/**
* File: Extension_Genesis_Plugin.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class Extension_Genesis_Plugin
*
* phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
*/
class Extension_Genesis_Plugin {
/**
* Request URI
*
* @var string
*/
private $_request_uri = '';
/**
* Config
*
* @var Config
*/
private $_config;
/**
* Constructs the Extension_Genesis_Plugin instance and initializes configurations.
*
* @return void
*/
public function __construct() {
$this->_config = Dispatcher::config();
}
/**
* Executes the main logic to initialize hooks and actions for the Extension_Genesis_Plugin.
*
* @return void
*/
public function run() {
add_filter( 'w3tc_config_default_values', array( $this, 'w3tc_config_default_values' ) );
add_action( 'w3tc_register_fragment_groups', array( $this, 'register_groups' ) );
$this->_config = Dispatcher::config();
if ( Util_Environment::is_w3tc_pro( $this->_config ) ) {
if ( ! is_admin() ) {
/**
* Register the caching of content to specific hooks
*/
foreach ( array( 'genesis_header', 'genesis_footer', 'genesis_sidebar', 'genesis_loop', 'wp_head', 'wp_footer', 'genesis_comments', 'genesis_pings' ) as $hook ) {
add_action( $hook, array( $this, 'cache_genesis_start' ), -999999999 );
add_action( $hook, array( $this, 'cache_genesis_end' ), 999999999 );
}
foreach ( array( 'genesis_do_subnav', 'genesis_do_nav' ) as $filter ) {
add_filter( $filter, array( $this, 'cache_genesis_filter_start' ), -999999999 );
add_filter( $filter, array( $this, 'cache_genesis_filter_end' ), 999999999 );
}
}
/**
* Since posts pages etc are cached individually need to be able to flush just those and not all fragment
*/
add_action( 'clean_post_cache', array( $this, 'flush_post_fragment' ) );
add_action( 'clean_post_cache', array( $this, 'flush_terms_fragment' ), 0, 0 );
$this->_request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
}
}
/**
* Adds default configuration values for the Genesis theme.
*
* @param array $default_values Existing default configuration values.
*
* @return array The modified configuration values.
*/
public function w3tc_config_default_values( $default_values ) {
$default_values['genesis.theme'] = array(
'wp_head' => '0',
'genesis_header' => '1',
'genesis_do_nav' => '0',
'genesis_do_subnav' => '0',
'loop_front_page' => '1',
'loop_terms' => '1',
'flush_terms' => '1',
'loop_single' => '1',
'loop_single_excluded' => '',
'loop_single_genesis_comments' => '0',
'loop_single_genesis_pings' => '0',
'sidebar' => '0',
'sidebar_excluded' => '',
'genesis_footer' => '1',
'wp_footer' => '0',
'reject_logged_roles' => '1',
'reject_logged_roles_on_actions' => array(
'genesis_loop',
'wp_head',
'wp_footer',
),
'reject_roles' => array( 'administrator' ),
);
return $default_values;
}
/**
* Starts fragment caching for Genesis-specific hooks.
*
* @return void
*/
public function cache_genesis_start() {
$hook = current_filter();
$keys = $this->_get_id_group( $hook );
if ( is_null( $keys ) ) {
return;
}
list( $id, $group ) = $keys;
w3tc_fragmentcache_start( $id, $group, $hook );
}
/**
* Ends fragment caching for Genesis-specific hooks.
*
* @return void
*/
public function cache_genesis_end() {
$keys = $this->_get_id_group( current_filter() );
if ( is_null( $keys ) ) {
return;
}
list( $id, $group ) = $keys;
w3tc_fragmentcache_end( $id, $group, $this->_config->get_boolean( array( 'fragmentcache', 'debug' ) ) );
}
/**
* Starts fragment caching for Genesis navigation-related filters.
*
* @param mixed $data The data to be filtered.
*
* @return mixed The modified data after applying the filter.
*/
public function cache_genesis_filter_start( $data ) {
$hook = current_filter();
$keys = $this->_get_id_group( $hook, strpos( $data, 'current' ) !== false );
if ( is_null( $keys ) ) {
return $data;
}
list( $id, $group ) = $keys;
return w3tc_fragmentcache_filter_start( $id, $group, $hook, $data );
}
/**
* Ends fragment caching for Genesis navigation-related filters.
*
* @param mixed $data The data to be filtered.
*
* @return mixed The modified data after applying the filter.
*/
public function cache_genesis_filter_end( $data ) {
$keys = $this->_get_id_group( current_filter(), strpos( $data, 'current' ) !== false );
if ( is_null( $keys ) ) {
return $data;
}
list( $id, $group ) = $keys;
return w3tc_fragmentcache_filter_end( $id, $group, $data );
}
/**
* Generates the Genesis fragment group identifier.
*
* @param string $subgroup The subgroup to append to the fragment group.
* @param bool $state Whether the group is for logged-in users.
*
* @return string The Genesis fragment group identifier.
*/
private function _genesis_group( $subgroup, $state = false ) {
$postfix = '';
if ( $state && is_user_logged_in() ) {
$postfix = 'logged_in_';
}
return ( $subgroup ? "genesis_fragment_{$subgroup}_" : 'genesis_fragment_' ) . $postfix;
}
/**
* Retrieves the ID and group for fragment caching based on the current hook.
*
* phpcs:disable WordPress.CodeAnalysis.AssignmentInCondition.Found
* phpcs:disable Squiz.PHP.DisallowMultipleAssignments.Found
*
* @param string $hook The current hook being processed.
* @param bool $current_menu Whether the current menu is relevant to caching.
*
* @return array|null An array containing the ID and group or null if not applicable.
*/
private function _get_id_group( $hook, $current_menu = false ) {
if ( $this->cannot_cache_current_hook() ) {
return null;
}
switch ( true ) {
case $keys = $this->generate_sidebar_keys():
list( $group, $genesis_id ) = $keys;
break;
case $keys = $this->generate_genesis_loop_keys():
list( $group, $genesis_id ) = $keys;
break;
case $keys = $this->generate_genesis_comments_pings_keys():
list( $group, $genesis_id ) = $keys;
break;
case $keys = $this->generate_genesis_navigation_keys( $current_menu ):
list( $group, $genesis_id ) = $keys;
break;
default:
$group = $hook;
$genesis_id = $this->get_page_slug();
if ( is_paged() ) {
$genesis_id .= $this->get_paged_page_key();
}
break;
}
if ( $this->_cache_group( $group ) && ! $this->_exclude_page( $group ) ) {
return array( $genesis_id, $this->_genesis_group( $group, true ) );
}
return null;
}
/**
* Checks if a group should be cached based on the plugin's configuration.
*
* @param string $group The group name to check.
*
* @return bool True if the group should be cached, false otherwise.
*/
private function _cache_group( $group ) {
return $this->_config->get_string( array( 'genesis.theme', $group ) );
}
/**
* Checks if a page should be excluded from caching based on the plugin's configuration.
*
* @param string $group The group name to check.
*
* @return bool True if the page should be excluded, false otherwise.
*/
private function _exclude_page( $group ) {
$reject_uri = $this->_config->get_array( array( 'genesis.theme', "{$group}_excluded" ) );
if ( is_null( $reject_uri ) || ! is_array( $reject_uri ) || empty( $reject_uri ) ) {
return false;
}
$auto_reject_uri = array(
'wp-login',
'wp-register',
);
foreach ( $auto_reject_uri as $uri ) {
if ( strstr( $this->_request_uri, $uri ) !== false ) {
return true;
}
}
$reject_uri = array_map( array( '\W3TC\Util_Environment', 'parse_path' ), $reject_uri );
foreach ( $reject_uri as $expr ) {
$expr = trim( $expr );
$expr = str_replace( '~', '\~', $expr );
if ( '' !== $expr && preg_match( '~' . $expr . '~i', $this->_request_uri ) ) {
return true;
}
}
return false;
}
/**
* Registers groups and their corresponding flush actions for fragment caching.
*
* @return void
*/
public function register_groups() {
// blog specific group and an array of actions that will trigger a flush of the group.
$groups = array(
$this->_genesis_group( '' ) => array(
'clean_post_cache',
'update_option_sidebars_widgets',
'wp_update_nav_menu_item',
),
$this->_genesis_group( 'sidebar' ) => array(
'update_option_sidebars_widgets',
),
$this->_genesis_group( 'loop_single' ) => array(
'no_action',
),
$this->_genesis_group( 'loop_front_page' ) => array(
'clean_post_cache',
),
$this->_genesis_group( 'loop_terms' ) => array(
'no_action',
),
);
foreach ( $groups as $group => $actions ) {
w3tc_register_fragment_group( $group, $actions, 3600 );
}
}
/**
* Flushes fragment cache for a specific post.
*
* @param int $post_ID The ID of the post whose fragment cache needs to be flushed.
*
* @return void
*/
public function flush_post_fragment( $post_ID ) {
$page_slug = $this->get_page_slug( $post_ID );
$urls = Util_PageUrls::get_post_urls( $post_ID );
$hooks = array( 'genesis_loop', 'genesis_comments', 'genesis_pings' );
foreach ( $hooks as $hook ) {
$genesis_id = $page_slug;
$genesis_id = "{$hook}_{$genesis_id}";
w3tc_fragmentcache_flush_fragment( $genesis_id, $this->_genesis_group( 'loop_single_logged_in' ) );
w3tc_fragmentcache_flush_fragment( $genesis_id, $this->_genesis_group( 'loop_single' ) );
$count = count( $urls );
for ( $page = 0; $page <= $count; $page++ ) {
$genesis_id = $page_slug;
$genesis_id .= $this->get_paged_page_key( $page );
$genesis_id = "{$hook}_{$genesis_id}";
w3tc_fragmentcache_flush_fragment( $genesis_id, $this->_genesis_group( 'loop_single_logged_in' ) );
w3tc_fragmentcache_flush_fragment( $genesis_id, $this->_genesis_group( 'loop_single' ) );
}
}
}
/**
* Flushes fragment cache for terms if enabled in the configuration.
*
* @return void
*/
public function flush_terms_fragment() {
if ( $this->_config->get_boolean( array( 'genesis.theme', 'flush_terms' ) ) ) {
w3tc_fragmentcache_flush_group( 'loop_terms' );
}
}
/**
* Determines whether the current hook can be cached.
*
* @return bool True if the current hook cannot be cached, false otherwise.
*/
private function cannot_cache_current_hook() {
if ( is_user_logged_in() && $this->_config->get_boolean( array( 'genesis.theme', 'reject_logged_roles' ) ) ) {
$roles = $this->_config->get_array( array( 'genesis.theme', 'reject_roles' ) );
if ( $roles ) {
$hooks = $this->_config->get_array( array( 'genesis.theme', 'reject_logged_roles_on_actions' ) );
$hook = current_filter();
foreach ( $roles as $role ) {
if ( $hooks && current_user_can( $role ) && in_array( $hook, $hooks, true ) ) {
return true;
}
}
}
}
return false;
}
/**
* Generates keys for fragment caching related to Genesis loops.
*
* @return array|null An array containing group and Genesis ID or null if not applicable.
*/
private function generate_genesis_loop_keys() {
$hook = current_filter();
if ( 'genesis_loop' !== $hook ) {
return null;
}
if ( is_front_page() ) {
$group = 'loop_front_page';
} elseif ( is_single() ) {
$group = 'loop_single';
} else {
$group = 'loop_terms';
}
$genesis_id = $this->get_page_slug();
if ( is_paged() ) {
$genesis_id .= $this->get_paged_page_key();
}
$genesis_id = "{$hook}_{$genesis_id}";
return array( $group, $genesis_id );
}
/**
* Generates keys for fragment caching related to Genesis sidebars.
*
* @return array|null An array containing group and Genesis ID or null if not applicable.
*/
private function generate_sidebar_keys() {
$hook = current_filter();
if ( true !== strpos( $hook, 'sidebar' ) ) {
return null;
}
$genesis_id = $hook;
$group = 'sidebar';
return array( $group, $genesis_id );
}
/**
* Generates keys for fragment caching related to Genesis comments and pings.
*
* @return array|null An array containing group and Genesis ID or null if not applicable.
*/
private function generate_genesis_comments_pings_keys() {
$hook = current_filter();
if ( 'genesis_comments' !== $hook ) {
return null;
}
$group = 'loop_single';
$genesis_id = $this->get_page_slug();
if ( is_paged() ) {
$genesis_id .= $this->get_paged_page_key();
}
$genesis_id = "{$hook}_{$genesis_id}";
return array( $group, $genesis_id );
}
/**
* Generates keys for fragment caching related to Genesis navigation menus.
*
* @param bool $current_menu Whether to use the current menu for generating keys.
*
* @return array|null An array containing group and Genesis ID or null if not applicable.
*/
private function generate_genesis_navigation_keys( $current_menu ) {
$hook = current_filter();
if ( ! ( strpos( $hook, '_nav' ) && $current_menu ) ) {
return null;
}
$genesis_id = $this->get_page_slug();
if ( is_paged() ) {
$genesis_id .= $this->get_paged_page_key();
}
return array( $hook, $genesis_id );
}
/**
* Retrieves the slug for the current page or a specific post.
*
* @param int|null $post_ID Optional. The ID of the post to retrieve the slug for.
*
* @return string The generated slug for the page or post.
*/
private function get_page_slug( $post_ID = null ) {
if ( $post_ID ) {
$purl = get_permalink( $post_ID );
return str_replace( '/', '-', trim( str_replace( home_url(), '', $purl ), '/' ) );
}
if ( is_front_page() ) {
return 'front_page';
}
return isset( $_SERVER['REQUEST_URI'] ) ? str_replace( '/', '-', trim( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '/' ) ) : '';
}
/**
* Retrieves the key for paged pages based on the given page number.
*
* @param int|null $page Optional. The page number. If null, the global query's paged variable is used.
*
* @return string The key for the paged page.
*/
private function get_paged_page_key( $page = null ) {
if ( is_null( $page ) ) {
global $wp_query;
return '_' . $wp_query->query_vars['paged'] . '_';
}
return '_' . $page . '_';
}
}
$p = new Extension_Genesis_Plugin();
$p->run();
if ( is_admin() ) {
$p = new Extension_Genesis_Plugin_Admin();
$p->run();
}