This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pods_AJAX_Views_Admin.php
613 lines (507 loc) · 17 KB
/
Pods_AJAX_Views_Admin.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
<?php
/**
* Class Pods_AJAX_Views_Admin
*/
class Pods_AJAX_Views_Admin {
/**
* Setup admin hooks
*/
public static function init() {
if ( defined( 'PODS_AJAX_VIEWS_STATS' ) && PODS_AJAX_VIEWS_STATS ) {
// Admin UI
add_filter( 'pods_admin_components_menu', [ __CLASS__, 'admin_menu' ] );
}
// Admin AJAX callbacks
add_action( 'wp_ajax_pods_ajax_view', [ __CLASS__, 'admin_ajax_view' ] );
add_action( 'wp_ajax_nopriv_pods_ajax_view', [ __CLASS__, 'admin_ajax_view' ] );
add_action( 'wp_ajax_pods_ajax_view_regenerate', [ __CLASS__, 'admin_ajax_view_regenerate' ] );
add_action( 'wp_ajax_nopriv_pods_ajax_view_regenerate', [ __CLASS__, 'admin_ajax_view_regenerate' ] );
add_action( 'wp_ajax_pods_ajax_view_sitemap', [ __CLASS__, 'admin_ajax_view_sitemap' ] );
add_action( 'wp_ajax_nopriv_pods_ajax_view_sitemap', [ __CLASS__, 'admin_ajax_view_sitemap' ] );
}
/**
* Add options page to menu
*
* @since 0.0.1
*
* @param array $admin_menus The submenu items in Pods Admin menu.
*
* @return mixed
*
*/
public static function admin_menu( $admin_menus ) {
$admin_menus['AJAX Views'] = [
'menu_page' => 'pods-ajax-views',
'page_title' => __( 'Pods AJAX Views', 'pods-ajax-views' ),
'capability' => 'manage_options',
'callback' => [ __CLASS__, 'admin_page' ],
];
return $admin_menus;
}
/**
* Output admin page
*/
public static function admin_page() {
/**
* @var $wpdb wpdb
*/ global $wpdb;
$ui = [
'item' => __( 'Pods AJAX View', 'pods-ajax-views' ),
'items' => __( 'Pods AJAX Views', 'pods-ajax-views' ),
'header' => [
'view' => __( 'AJAX View Stats', 'pods-ajax-views' ),
],
'sql' => [
'table' => $wpdb->prefix . 'podsviews',
'field_id' => 'view_id',
'field_index' => 'view',
],
'orderby' => 't.avg_time DESC',
'fields' => [
'manage' => [
'view' => [
'name' => 'view',
'label' => 'View',
'type' => 'text',
],
'cache_mode' => [
'name' => 'cache_mode',
'label' => 'Cache Mode',
'type' => 'text',
'width' => '10%',
],
'uri' => [
'name' => 'uri',
'label' => 'URL',
'type' => 'text',
],
'expires' => [
'name' => 'expires',
'label' => 'Expires (seconds)',
'type' => 'number',
'options' => [
'number_format_type' => '9999.99',
'number_decimals' => 0,
],
'width' => '12%',
],
'avg_time' => [
'name' => 'avg_time',
'label' => 'Average Load Time (seconds)',
'type' => 'number',
'options' => [
'number_decimals' => 3,
],
'width' => '12%',
],
'total_calls' => [
'name' => 'total_calls',
'label' => 'Total Calls',
'type' => 'number',
'options' => [
'number_decimals' => 0,
],
'width' => '10%',
],
'last_generated' => [
'name' => 'last_generated',
'label' => 'Last Generated',
'type' => 'datetime',
],
],
'search' => [
'view' => [
'name' => 'view',
'label' => 'View',
'type' => 'text',
],
'cache_key' => [
'name' => 'cache_key',
'label' => 'Cache Key',
'type' => 'text',
],
'cache_mode' => [
'name' => 'cache_mode',
'label' => 'Cache Mode',
'type' => 'text',
],
'last_generated' => [
'name' => 'last_generated',
'label' => 'Last Generated',
'type' => 'datetime',
],
'tracking_data' => [
'name' => 'tracking_data',
'label' => 'Tracking Data',
'type' => 'paragraph',
],
],
],
'filters' => [
'view',
'cache_mode',
'last_generated',
],
'filters_enhanced' => true,
'actions_disabled' => [
'add',
'edit',
'duplicate',
'export',
],
'actions_custom' => [
'regenerate_view' => [
'callback' => [ __CLASS__, 'admin_page_regenerate_view' ],
],
'view' => [
'callback' => [ __CLASS__, 'admin_page_view_stats' ],
],
'delete' => [
'callback' => [ __CLASS__, 'admin_page_delete_view' ],
],
],
'actions_bulk' => [
'delete' => [
'label' => __( 'Delete', 'pods' )
// callback not needed, Pods has this built-in for delete
],
'regenerate_views' => [
'callback' => [ __CLASS__, 'admin_page_regenerate_views' ],
],
],
];
$ui['fields']['view'] = [];
$ui['fields']['view']['cache_key'] = [
'name' => 'cache_key',
'label' => 'Cache Key',
'type' => 'text',
];
$ui['fields']['view'] = array_merge( $ui['fields']['view'], $ui['fields']['manage'] );
$ui['fields']['view']['tracking_data'] = [
'name' => 'tracking_data',
'label' => 'Tracking Data',
'type' => 'paragraph',
];
unset( $ui['fields']['view']['view'] );
if ( 1 == pods_v( 'deleted_bulk' ) ) {
unset( $ui['actions_custom']['delete'] );
}
pods_ui( $ui );
}
/**
* Handle View action
*
* @param PodsUI $obj
* @param string $id
*/
public static function admin_page_view_stats( $obj, $id ) {
$item = $obj->get_row();
$item = array_map( 'maybe_unserialize', $item );
include_once 'ui/view-stats.php';
}
/**
* Handle Regenerate View action
*
* @param PodsUI $obj
* @param string $id
*/
public static function admin_page_regenerate_view( $obj, $id ) {
self::admin_page_regenerate_views_ajax( [ $id ] );
$obj->action = 'manage';
$obj->id = 0;
unset( $_GET['action'] );
unset( $_GET['id'] );
$obj->manage();
}
/**
* Handle AJAX regeneration of views
*
* @param array<string> $ids
*/
public static function admin_page_regenerate_views_ajax( $ids ) {
/**
* @var $wpdb wpdb
*/ global $wpdb;
$ids = array_map( 'absint', $ids );
$pods_ajax_views = [];
$sql = "
SELECT `cache_key`, `cache_mode`
FROM `{$wpdb->prefix}podsviews`
WHERE `view_id` = %d
";
foreach ( $ids as $id ) {
// Get item info
$view = $wpdb->get_row( $wpdb->prepare( $sql, $id ) );
// Set cache key / mode for noncing
$cache_key = $view->cache_key;
$cache_mode = $view->cache_mode;
$uri = $view->uri;
// Build nonce action from request
$nonce_action = 'pods-ajax-view-' . md5( $cache_key . '/' . $cache_mode . '|' . $uri ) . '/regenerate';
// Build nonce from action
$nonce = wp_create_nonce( $nonce_action );
// Setup object to push for processing
$pods_ajax_views[] = [
'cache_key' => $cache_key,
'cache_mode' => $cache_mode,
'nonce' => $nonce,
'uri' => $uri,
];
}
// Enqueue Pods AJAX Views JS
wp_enqueue_script( 'pods-ajax-views' );
// Queue view to be included via AJAX
echo '<script>' . "\n" . 'var pods_ajax_views = ' . json_encode( $pods_ajax_views ) . ';' . "\n" . '</script>' . "\n";
// Enqueue jQuery UI Progressbar
wp_enqueue_script( 'jquery-ui-progressbar' );
$message = '<span id="pods-ajax-views-progress-status">%s</span>' . '<div id="pods-ajax-views-progress-indicator" style="position:relative;max-width:300px;display:none;">' . '<div id="pods-ajax-views-progress-label" style="position:absolute;left:45%%;top:6px;font-weight:bold;text-shadow:1px 1px 0 #FFF;font-size:12px;">%s</div>' . '</div>';
$message = sprintf( $message, _n( 'Regenerating Pods AJAX View', 'Regenerating Pods AJAX Views', count( $pods_ajax_views ), 'pods-ajax-views' ), __( 'Loading...', 'pods-ajax-views' ) );
pods_message( $message );
}
public static function admin_page_delete_view( $id, $obj ) {
/**
* @var $wpdb wpdb
*/ global $wpdb;
$sql = "
SELECT `cache_key`, `cache_mode`
FROM `{$wpdb->prefix}podsviews`
WHERE `view_id` = %d
";
// Get item info
$view = $wpdb->get_row( $wpdb->prepare( $sql, $id ) );
if ( $view ) {
include_once 'Pods_AJAX_Views_Frontend.php';
$deleted = Pods_AJAX_Views_Frontend::delete_ajax_view( $view->cache_key, $view->cache_mode );
if ( $deleted && 0 < $obj->id ) {
pods_message( sprintf( __( "<strong>Deleted:</strong> %s has been deleted.", 'pods' ), $obj->item ) );
}
return $deleted;
}
return false;
}
/**
* Handle Regenerate Views bulk action
*
* @param array<string> $ids
* @param PodsUI $obj
*/
public static function admin_page_regenerate_views( $ids, $obj ) {
self::admin_page_regenerate_views_ajax( $ids );
$obj->action_bulk = false;
unset( $_GET['action_bulk'] );
$obj->bulk = [];
unset( $_GET['action_bulk_ids'] );
$obj->manage();
}
/**
* Handle the Admin AJAX request for a Pods AJAX View
*/
public static function admin_ajax_view() {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
@ini_set( 'display_errors', 'on' );
@error_reporting( E_ALL | E_STRICT );
}
include_once 'Pods_AJAX_Views_Frontend.php';
// Check if request is there
if ( ! empty( $_REQUEST['pods_ajax_view_key'] ) && ! empty( $_REQUEST['pods_ajax_view_mode'] ) && ! empty( $_REQUEST['pods_ajax_view_nonce'] ) ) {
$uri = Pods_AJAX_Views_Frontend::get_uri();
// Build nonce action from request
$nonce_action = 'pods-ajax-view-' . md5( $_REQUEST['pods_ajax_view_key'] . '/' . $_REQUEST['pods_ajax_view_mode'] . '|' . $uri );
// Verify nonce is correct
if ( false !== wp_verify_nonce( $_REQUEST['pods_ajax_view_nonce'], $nonce_action ) ) {
// Generate view and cache it
Pods_AJAX_Views_Frontend::generate_view( $_REQUEST['pods_ajax_view_key'], $_REQUEST['pods_ajax_view_mode'] );
// View found, bail (needed here if using template_redirect request)
die();
} else {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo '<!--PODS_AJAX_VIEWS_DEBUG: INVALID NONCE-->';
}
}
}
// AJAX must die, won't break if doing template_redirect hook
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
die();
}
}
/**
* Handle the Admin AJAX request for a Pods AJAX View regeneration
*/
public static function admin_ajax_view_regenerate() {
@header( 'Cache-Control: private, max-age=0, no-cache' );
include_once 'Pods_AJAX_Views_Frontend.php';
// Check if request uses API key, and if incorrect, don't serve request
if ( isset( $_REQUEST['pods_ajax_view_api_key'] ) ) {
if ( ! defined( 'PODS_AJAX_VIEWS_API_KEY' ) || PODS_AJAX_VIEWS_API_KEY != $_REQUEST['pods_ajax_view_api_key'] ) {
die();
}
} // If user is not logged in or not a Pods admin, don't serve request
elseif ( ! is_user_logged_in() || ! pods_is_admin( 'pods' ) ) {
// AJAX must die, won't break if doing template_redirect hook
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
die();
}
}
// Check if request is there
if ( ! empty( $_REQUEST['pods_ajax_view_key'] ) && ! empty( $_REQUEST['pods_ajax_view_mode'] ) && ! empty( $_REQUEST['pods_ajax_view_nonce'] ) ) {
$uri = Pods_AJAX_Views_Frontend::get_uri();
// Build nonce action from request
$nonce_action = 'pods-ajax-view-' . md5( $_REQUEST['pods_ajax_view_key'] . '/' . $_REQUEST['pods_ajax_view_mode'] . '|' . $uri ) . '/regenerate';
// Verify nonce is correct
if ( false !== wp_verify_nonce( $_REQUEST['pods_ajax_view_nonce'], $nonce_action ) ) {
// Generate view and cache it
Pods_AJAX_Views_Frontend::generate_view( $_REQUEST['pods_ajax_view_key'], $_REQUEST['pods_ajax_view_mode'], true, true );
}
// Bail (needed here if using template_redirect request)
die();
}
// AJAX must die, won't break if doing template_redirect hook
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
die();
}
}
/**
* Handle the Admin AJAX request for a Pods AJAX View regeneration URLs in XML Sitemap format
*/
public static function admin_ajax_view_sitemap() {
@header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
/**
* @var $wpdb wpdb
*/ global $wpdb;
// Check if request uses API key, and if incorrect, don't serve request
if ( isset( $_REQUEST['pods_ajax_view_api_key'] ) ) {
if ( ! defined( 'PODS_AJAX_VIEWS_API_KEY' ) || PODS_AJAX_VIEWS_API_KEY != $_REQUEST['pods_ajax_view_api_key'] ) {
die();
}
} // If user is not logged in or not a Pods admin, don't serve request
elseif ( ! is_user_logged_in() || ! pods_is_admin( 'pods' ) ) {
die();
}
// XML opening tag
echo '<' . '?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . '"?' . '>' . "\n";
// URL set open
echo '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . ' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"' . ' xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
$limit = 250;
if ( defined( 'PODS_AJAX_VIEWS_SITEMAP_LIMIT' ) ) {
$limit = (int) PODS_AJAX_VIEWS_SITEMAP_LIMIT;
}
$where = '';
if ( isset( $_REQUEST['lastmod'] ) ) {
$where = $wpdb->prepare( 'WHERE FROM_UNIXTIME( %d ) <= `last_generated`', strtotime( $_REQUEST['lastmod'] ) );
}
$sql = "
SELECT *
FROM `{$wpdb->prefix}podsviews`
{$where}
LIMIT %d
";
$views = $wpdb->get_results( $wpdb->prepare( $sql, $limit ) );
foreach ( $views as $view ) {
// Build nonce action
$nonce_action = 'pods-ajax-view-' . md5( $view->cache_key . '/' . $view->cache_mode . '|' . $view->uri ) . '/regenerate';
$nonce = wp_create_nonce( $nonce_action );
$loc = $view->uri;
$query_args = [
'pods_ajax_view_action' => 'view_regenerate',
'pods_ajax_view_key' => $view->cache_key,
'pods_ajax_view_mode' => $view->cache_mode,
'pods_ajax_view_nonce' => $nonce,
'pods_ajax_view_api_key' => PODS_AJAX_VIEWS_API_KEY,
];
$loc = add_query_arg( $query_args, $loc );
$loc = esc_url( $loc );
// Sanitize URL for XML
$loc = str_replace( '&', '&', $loc );
$loc = str_replace( "'", ''', $loc );
$loc = str_replace( '"', '"', $loc );
$loc = str_replace( '>', '>', $loc );
$loc = str_replace( '<', '<', $loc );
// Sanitize URL for XML
$uri = $view->uri;
$uri = str_replace( '&', '&', $uri );
$uri = str_replace( "'", ''', $uri );
$uri = str_replace( '"', '"', $uri );
$uri = str_replace( '>', '>', $uri );
$uri = str_replace( '<', '<', $uri );
$lastmod = $view->last_generated;
$changefreq = 'always';
if ( 0 < $view->expires && $view->expires <= HOUR_IN_SECONDS ) {
$changefreq = 'hourly';
} elseif ( HOUR_IN_SECONDS < $view->expires && $view->expires <= DAY_IN_SECONDS ) {
$changefreq = 'daily';
} elseif ( DAY_IN_SECONDS < $view->expires && $view->expires <= WEEK_IN_SECONDS ) {
$changefreq = 'weekly';
} elseif ( WEEK_IN_SECONDS < $view->expires && $view->expires <= ( DAY_IN_SECONDS * 30 ) ) {
$changefreq = 'monthly';
} elseif ( ( DAY_IN_SECONDS * 30 ) < $view->expires ) {
$changefreq = 'yearly';
}
$priority = '1.0';
echo "\t<url>\n";
echo "\t\t<loc>{$loc}</loc>\n";
echo "\t\t<lastmod>{$lastmod}</lastmod>\n";
echo "\t\t<changefreq>{$changefreq}</changefreq>\n";
echo "\t\t<priority>{$priority}</priority>\n";
echo "\t</url>\n";
}
echo '</urlset>';
// AJAX must die
die();
}
public static function admin_ajax_clean_anon_cache() {
@header( 'Cache-Control: private, max-age=0, no-cache' );
// Check if request is there
if ( ! empty( $_REQUEST['pods_ajax_view_url'] ) && ! empty( $_REQUEST['pods_ajax_view_nonce'] ) ) {
include_once 'Pods_AJAX_Views_Frontend.php';
$uri = Pods_AJAX_Views_Frontend::get_uri( $_REQUEST['pods_ajax_view_url'] );
// Build nonce action from request
$nonce_action = 'pods-ajax-view-' . md5( $uri ) . '/clean';
// Verify nonce is correct
if ( false !== wp_verify_nonce( $_REQUEST['pods_ajax_view_nonce'], $nonce_action ) ) {
// WPEngine
// Credit: https://github.com/cftp/WPEngine-Clear-URL-Cache
if ( defined( 'WPE_PLUGIN_VERSION' ) ) {
global $wpe_varnish_servers, $wpe_ec_servers;
$post_parts = parse_url( $uri );
$post_uri = $post_parts['path'];
if ( ! empty( $post_parts['query'] ) ) {
$post_uri .= '?' . $post_parts['query'];
}
$path = $post_uri;
if ( ! $path ) {
$path = '/';
}
$hostname = $post_parts['host'];
if ( 'pod' == WPE_CLUSTER_TYPE ) {
$wpe_varnish_servers = [ 'localhost' ];
} elseif ( ! isset( $wpe_varnish_servers ) ) {
if ( 'pod' == WPE_CLUSTER_TYPE ) {
$lbmaster = 'localhost';
} elseif ( ! defined( 'WPE_CLUSTER_ID' ) || ! WPE_CLUSTER_ID ) {
$lbmaster = 'lbmaster';
} elseif ( 4 <= WPE_CLUSTER_ID ) {
$lbmaster = 'localhost';
} else {
$lbmaster = 'lbmaster-' . WPE_CLUSTER_ID;
}
$wpe_varnish_servers = [ $lbmaster ];
}
if ( ! isset( $wpe_ec_servers ) || empty( $wpe_ec_servers ) ) {
foreach ( $wpe_varnish_servers as $varnish ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( "Pods AJAX Views: PURGE, {$varnish}, 9002, {$hostname}, {$path}, array(), 0" );
}
WpeCommon::http_request_async( 'PURGE', $varnish, 9002, $hostname, $path, [], 0 );
}
}
} // W3TC
elseif ( 1 == 0 ) {
} // Others?
elseif ( 1 == 0 ) {
}
}
}
// AJAX must die
die();
}
}