-
Notifications
You must be signed in to change notification settings - Fork 25
/
transients-manager.php
1651 lines (1381 loc) · 42.9 KB
/
transients-manager.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: Transients Manager
* Plugin URI: https://wordpress.org/plugins/transients-manager/
* Description: Provides a familiar interface to view, search, edit, and delete Transients.
* Author: WPBeginner
* Author URI: https://www.wpbeginner.com
* Contributors: wpbeginner, smub, mordauk, johnjamesjacoby
* License: GNU General Public License v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: transients-manager
* Requires PHP: 5.6.20
* Requires at least: 5.3
* Version: 2.0.5
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
class AM_Transients_Manager {
/**
* ID of the plugin page
*
* @since 2.0
* @var string
*/
public $page_id = 'transients-manager';
/**
* ID of the Admin Screen
*
* @since 2.0.5
* @var string
*/
public $screen_id = '';
/**
* Capability the current-user needs to manage transients
*
* @since 2.0
* @var string
*/
public $capability = 'manage_options';
/**
* Timestamp for right now
*
* @since 2.0
* @var int
*/
public $time_now = 0;
/**
* The next timestamp to delete expired transients (via WP Cron)
*
* @since 2.0
* @var int
*/
public $next_cron_delete = 0;
/**
* Transient ID
*
* @since 2.0.4
* @var int
*/
public $transient_id = 0;
/**
* Transient object
*
* @since 2.0.4
* @var object
*/
public $transient = false;
/**
* Action name
*
* @since 2.0.4
* @var string
*/
public $action = '';
/**
* Get things started
*
* @since 1.0
*/
public function __construct() {
$this->add_hooks();
}
/**
* Add all of the hooks
*
* @since 2.0
*/
private function add_hooks() {
add_action( 'plugins_loaded', array( $this, 'text_domain' ) );
add_action( 'admin_init', array( $this, 'set_vars' ) );
add_action( 'admin_init', array( $this, 'process_actions' ) );
add_action( 'admin_menu', array( $this, 'tools_link' ) );
add_action( 'admin_notices', array( $this, 'notices' ) );
add_action( 'admin_bar_menu', array( $this, 'suspend_transients_button' ), 99 );
add_filter( 'pre_update_option', array( $this, 'maybe_block_update_transient' ), -1, 2 );
add_filter( 'pre_get_option', array( $this, 'maybe_block_update_transient' ), -1, 2 );
add_action( 'added_option', array( $this, 'maybe_block_set_transient' ), -1, 1 );
// Styles
add_action( "admin_print_styles-tools_page_{$this->page_id}", array( $this, 'print_styles' ) );
/**
* Allow third-party plugins a chance to modify the hooks above
*
* @since 2.0
* @param object $this
*/
do_action( 'transients_manager_hooks', $this );
}
/**
* Set many of the class variables
*
* @since 2.0
*/
public function set_vars() {
// Times
$this->time_now = time();
$this->next_cron_delete = wp_next_scheduled( 'delete_expired_transients' );
// Sanitize the transient ID
$this->transient_id = ! empty( $_GET['trans_id'] )
? absint( $_GET['trans_id'] )
: 0;
// Get the transient
$this->transient = ! empty( $this->transient_id )
? $this->get_transient_by_id( $this->transient_id )
: false;
// Sanitize the action
$this->action = ! empty( $_REQUEST['action'] )
? sanitize_key( $_REQUEST['action'] )
: '';
}
/**
* Load text domain
*
* @since 1.0
*/
public function text_domain() {
load_plugin_textdomain( 'transients-manager' );
}
/**
* Register menu link under Tools
*
* @since 1.0
*/
public function tools_link() {
// Set the screen ID
$this->screen_id = add_management_page(
esc_html__( 'Transients Manager', 'transients-manager' ),
esc_html__( 'Transients', 'transients-manager' ),
$this->capability,
$this->page_id,
array( $this, 'admin' )
);
}
/**
* Editing or showing?
*
* @since 2.0
* @return string
*/
protected function page_type() {
// Edit or Show?
return ! empty( $this->action ) && ( 'edit_transient' === $this->action ) && ! empty( $this->transient )
? 'edit'
: 'show';
}
/**
* Render the admin UI
*
* @since 1.0
*/
public function admin() {
// Editing a single Transient
( 'edit' === $this->page_type() )
? $this->page_edit_transient( $this->transient )
: $this->page_show_transients();
}
/**
* Admin notices
*
* @since 2.0
*/
public function notices() {
// Get the current screen
$screen = get_current_screen();
// Bail if not the correct screen
if ( $screen->id !== $this->screen_id ) {
return;
}
// Persistent Transients
if ( wp_using_ext_object_cache() ) :
?>
<div class="notice notice-info">
<p><?php esc_html_e( 'You are using a persistent object cache. This screen may show incomplete information.', 'transients-manager' ); ?></p>
</div>
<?php
endif;
// Updated Transient
if ( ! empty( $_GET['updated'] ) ) :
?>
<div class="notice notice-success is-dismissible">
<p><strong><?php esc_html_e( 'Transient updated.', 'transients-manager' ); ?></strong></p>
</div>
<?php
endif;
// Deleted Transients
if ( ! empty( $_GET['deleted'] ) ) :
?>
<div class="notice notice-success is-dismissible">
<p><strong><?php esc_html_e( 'Transient deleted.', 'transients-manager' ); ?></strong></p>
</div>
<?php
endif;
}
/**
* Output the page HTML for the Transients mock-list-table
*
* @since 2.0
*/
protected function page_show_transients() {
// Vars
$search = ! empty( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : '';
$per_page = ! empty( $_GET['per_page'] ) ? absint( $_GET['per_page'] ) : 30;
$page = isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
$offset = $per_page * ( $page - 1 );
$count = $this->get_total_transients( $search );
$pages = ceil( $count / $per_page );
$one_page = ( 1 === $pages ) ? 'one-page' : '';
// Pagination
$pagination = paginate_links( array(
'base' => 'tools.php?%_%',
'format' => '&paged=%#%',
'prev_text' => '«',
'next_text' => '»',
'total' => $pages,
'current' => $page
) );
// Transients
$transients = $this->get_transients( array(
'search' => $search,
'offset' => $offset,
'number' => $per_page
) );
?>
<div class="wrap">
<h1 class="wp-heading-inline"><?php esc_html_e( 'Transients', 'transients-manager' ); ?></h1>
<hr class="wp-header-end">
<form method="get">
<p class="search-box">
<label class="screen-reader-text" for="transient-search-input"><?php esc_html_e( 'Search', 'transients-manager' ); ?></label>
<input type="search" id="transient-search-input" name="s" value="<?php echo esc_attr( $search ); ?>" />
<input type="submit" id="search-submit" class="button" value="<?php esc_attr_e( 'Search Transients', 'transients-manager' ); ?>" />
<input type="hidden" name="page" value="<?php echo esc_html( $this->page_id ); ?>" />
</p>
</form>
<form method="post" id="transients-delete">
<input type="hidden" name="transient" value="all" />
<?php wp_nonce_field( 'transients_manager' ); ?>
<div class="tablenav top">
<div class="alignleft actions bulkactions">
<label for="bulk-action-selector-top" class="screen-reader-text"><?php esc_html_e( 'Select Bulk action', 'transients-manager' ); ?></label>
<select name="action" id="bulk-action-selector-top">
<option value="-1"><?php esc_html_e( 'Bulk actions', 'transients-manager' ); ?></option>
<optgroup label="<?php esc_attr_e( 'Selection', 'transients-manager' ); ?>">
<option value="delete_selected_transients"><?php esc_html_e( 'Delete Selected', 'transients-manager' ); ?></option>
</optgroup>
<optgroup label="<?php esc_attr_e( 'Expiration', 'transients-manager' ); ?>">
<option value="delete_expired_transients"><?php esc_html_e( 'Delete Expired', 'transients-manager' ); ?></option>
<option value="delete_transients_with_expiration"><?php esc_html_e( 'Delete With Expiration', 'transients-manager' ); ?></option>
<option value="delete_transients_without_expiration"><?php esc_html_e( 'Delete Without Expiration', 'transients-manager' ); ?></option>
</optgroup>
<optgroup label="<?php esc_attr_e( 'Reset', 'transients-manager' ); ?>">
<option value="delete_all_transients"><?php esc_html_e( 'Delete All', 'transients-manager' ); ?></option>
</optgroup>
</select>
<input type="submit" class="button secondary" value="<?php esc_attr_e( 'Apply', 'transients-manager' ); ?>" />
</div>
<div class="alignleft actions">
<input type="button" class="button secondary" value="<?php esc_attr_e( 'Delete All', 'transients-manager' ); ?>" onclick="const select = document.getElementById('bulk-action-selector-top'); select.value='delete_all_transients'; select.dispatchEvent(new Event('change')); document.getElementById('transients-delete').submit();" />
</div>
<div class="tablenav-pages <?php echo esc_attr( $one_page ); ?>">
<span class="displaying-num"><?php printf( _n( '%s Transient', '%s Transients', $count, 'transients-manager' ), number_format_i18n( $count ) ); ?></span>
<span class="pagination-links"><?php echo $pagination; // HTML OK ?></span>
</div>
</div>
<table class="wp-list-table widefat fixed transients striped">
<thead>
<tr>
<td id="cb" class="manage-column column-cb check-column">
<label for="cb-select-all-<?php echo (int) $page; ?>" class="screen-reader-text"><?php esc_html_e( 'Select All', 'transients-manager' ); ?></label>
<input type="checkbox" id="cb-select-all-<?php echo (int) $page; ?>">
</td>
<th class="column-primary"><?php esc_html_e( 'Name', 'transients-manager' ); ?></th>
<th class="column-value"><?php esc_html_e( 'Value', 'transients-manager' ); ?></th>
<th class="column-expiration"><?php esc_html_e( 'Expiration', 'transients-manager' ); ?></th>
</tr>
</thead>
<tbody>
<?php if ( ! empty( $transients ) ) :
foreach ( $transients as $transient ) :
// Get Transient name
$name = $this->get_transient_name( $transient );
$value = $this->get_transient_value( $transient );
$expiration = $this->get_transient_expiration( $transient );
// Delete
$delete_url = wp_nonce_url(
remove_query_arg(
array( 'deleted', 'updated' ),
add_query_arg(
array(
'action' => 'delete_transient',
'transient' => $name,
'name' => $transient->option_name
)
)
),
'transients_manager'
);
// Edit
$edit_url = remove_query_arg(
array( 'updated', 'deleted' ),
add_query_arg(
array(
'action' => 'edit_transient',
'trans_id' => $transient->option_id
)
)
); ?>
<tr>
<th id="cb" class="manage-column column-cb check-column">
<label for="cb-select-<?php echo (int) $page; ?>" class="screen-reader-text"><?php printf( esc_attr__( 'Select %s', 'transients-manager' ), esc_html( $name ) ); ?></label>
<input type="checkbox" id="cb-select-<?php echo (int) $transient->option_id; ?>" name="transients[]" value="<?php echo (int) $transient->option_id; ?>">
</th>
<td class="column-primary" data-colname="<?php esc_attr_e( 'Name', 'transients-manager' ); ?>">
<pre class="truncate">
<code class="transient-name" title="<?php printf( esc_attr__( 'Option ID: %d', 'transients-manager' ), (int) $transient->option_id ); ?>"><?php echo esc_html( $name ); ?></code>
</pre>
<div class="row-actions">
<span class="edit"><a href="<?php echo esc_url( $edit_url ); ?>" class="edit"><?php esc_html_e( 'Edit', 'transients-manager' ); ?></a></span>
|
<span class="delete"><a href="<?php echo esc_url( $delete_url ); ?>" class="delete"><?php esc_html_e( 'Delete', 'transients-manager' ); ?></a></span>
</div>
<button type="button" class="toggle-row">
<span class="screen-reader-text"><?php esc_html_e( 'Show more details', 'transients-manager' ); ?></span>
</button>
</td>
<td data-colname="<?php esc_attr_e( 'Value', 'transients-manager' ); ?>">
<span class="transient-value truncate"><?php
echo $value; // HTML OK
?></span>
</td>
<td data-colname="<?php esc_attr_e( 'Expiration', 'transients-manager' ); ?>">
<span class="transient-expiration"><?php
echo $expiration; // HTML OK
?></span>
</td>
</tr>
<?php endforeach;
else : ?>
<tr><td colspan="4"><?php esc_html_e( 'No transients found.', 'transients-manager' ); ?></td>
<?php endif; ?>
</tbody>
<tfoot>
<tr>
<td class="manage-column column-cb check-column">
<label for="cb-select-all-<?php echo (int) $page; ?>" class="screen-reader-text"><?php esc_html_e( 'Select All', 'transients-manager' ); ?></label>
<input type="checkbox" id="cb-select-all-<?php echo (int) $page; ?>">
</td>
<th class="column-primary"><?php esc_html_e( 'Name', 'transients-manager' ); ?></th>
<th><?php esc_html_e( 'Value', 'transients-manager' ); ?></th>
<th><?php esc_html_e( 'Expiration', 'transients-manager' ); ?></th>
</tr>
</tfoot>
</table>
<div class="tablenav bottom">
<div class="alignleft actions bulkactions">
<label for="bulk-action-selector-top" class="screen-reader-text"><?php esc_html_e( 'Select Bulk action', 'transients-manager' ); ?></label>
<select name="action" id="bulk-action-selector-bottom">
<option value="-1"><?php esc_html_e( 'Bulk actions', 'transients-manager' ); ?></option>
<optgroup label="<?php esc_attr_e( 'Selection', 'transients-manager' ); ?>">
<option value="delete_selected_transients"><?php esc_html_e( 'Delete Selected', 'transients-manager' ); ?></option>
</optgroup>
<optgroup label="<?php esc_attr_e( 'Expiration', 'transients-manager' ); ?>">
<option value="delete_expired_transients"><?php esc_html_e( 'Delete Expired', 'transients-manager' ); ?></option>
<option value="delete_transients_with_expiration"><?php esc_html_e( 'Delete With Expiration', 'transients-manager' ); ?></option>
<option value="delete_transients_without_expiration"><?php esc_html_e( 'Delete Without Expiration', 'transients-manager' ); ?></option>
</optgroup>
<optgroup label="<?php esc_attr_e( 'Reset', 'transients-manager' ); ?>">
<option value="delete_all_transients"><?php esc_html_e( 'Delete All', 'transients-manager' ); ?></option>
</optgroup>
</select>
<input type="submit" class="button secondary" value="<?php esc_attr_e( 'Apply', 'transients-manager' ); ?>" />
</div>
<div class="tablenav-pages <?php echo esc_attr( $one_page ); ?>">
<span class="displaying-num"><?php printf( _n( '%s Transient', '%s Transients', $count, 'transients-manager' ), number_format_i18n( $count ) ); ?></span>
<span class="pagination-links"><?php echo $pagination; // HTML OK ?></span>
</div>
</div>
</form>
<?php $this->site_time(); ?>
</div>
<?php
}
/**
* Output the page HTML for editing a Transient
*
* @since 2.0
* @param object $transient
*/
protected function page_edit_transient( $transient = false) {
// Get values
$name = $this->get_transient_name( $transient );
$expiration = $this->get_transient_expiration_time( $transient );
?>
<div class="wrap">
<h1 class="wp-heading-inline"><?php esc_html_e( 'Edit Transient', 'transients-manager' ); ?></h1>
<hr class="wp-header-end">
<form method="post">
<input type="hidden" name="transient" value="<?php echo esc_attr( $name ); ?>" />
<input type="hidden" name="action" value="update_transient" />
<?php wp_nonce_field( 'transients_manager' ); ?>
<table class="form-table">
<tbody>
<tr>
<th><?php esc_html_e( 'Option ID', 'transients-manager' ); ?></th>
<td><input type="text" disabled class="large-text code" name="name" value="<?php echo esc_attr( $transient->option_id ); ?>" /></td>
</tr>
<tr>
<th><?php esc_html_e( 'Name', 'transients-manager' ); ?></th>
<td><input type="text" class="large-text code" name="name" value="<?php echo esc_attr( $transient->option_name ); ?>" /></td>
</tr>
<tr>
<th><?php esc_html_e( 'Expiration', 'transients-manager' ); ?></th>
<td><input type="text" class="large-text" name="expires" value="<?php echo esc_attr( $expiration ); ?>" />
</tr>
<tr>
<th><?php esc_html_e( 'Value', 'transients-manager' ); ?></th>
<td>
<textarea class="large-text code" name="value" id="transient-editor" style="height: 302px; padding-left: 35px;"><?php
echo esc_textarea( $transient->option_value );
?></textarea>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<?php submit_button( '', 'primary', '', false ); ?>
</p>
</form>
</div>
<?php
}
/**
* Output the time for the site
*
* (Props to WP Crontrol for this approach)
*
* @since 2.0
*/
public function site_time() {
// Get options
$timezone_string = get_option( 'timezone_string', '' );
$gmt_offset = get_option( 'gmt_offset', 0 );
$timezone_name = 'UTC';
// UTC
if ( ( 'UTC' !== $timezone_string ) || ! empty( $gmt_offset ) ) {
$formatted_offset = ( 0 <= $gmt_offset )
? '+' . (string) $gmt_offset
: (string) $gmt_offset;
$formatted_offset = str_replace(
array( '.25', '.5', '.75' ),
array( ':15', ':30', ':45' ),
$formatted_offset
);
}
// Format the timezone name
$timezone_name = ! empty( $formatted_offset )
? 'UTC' . $formatted_offset
: str_replace( '_', ' ', $timezone_string );
// Site time
$site_time = date_i18n( 'Y-m-d H:i:s', $this->time_now );
$site_time_utc = gmdate( 'Y-m-d\TH:i:s+00:00', $this->time_now );
$st_html = esc_html( sprintf(
/* translators: 1: Date and time, 2: Timezone */
__( 'Site time: %1$s (%2$s)', 'transients-manager' ),
$site_time,
$timezone_name
) );
// Cron time
$cron_time = wp_date( 'Y-m-d H:i:s', $this->next_cron_delete );
$cron_time_utc = gmdate( 'Y-m-d\TH:i:s+00:00', $this->next_cron_delete );
$ct_time_since = $this->time_since( $this->next_cron_delete - $this->time_now );
$nc_time = esc_html( sprintf(
/* translators: 1: Date and time, 2: Timezone, 3: Time since */
__( 'Expired transients scheduled for deletion on: %1$s (%2$s) – %3$s from now', 'transients-manager' ),
$cron_time,
$timezone_name,
$ct_time_since
) );
?>
<p class="transients-manager-helpful-times">
<?php
echo sprintf( '<time datetime="%1$s" title="%1$s">%2$s</time>',
$site_time_utc,
$st_html
);
?><br><br><?php
echo sprintf( '<time datetime="%1$s" title="%1$s">%2$s</time>',
$cron_time_utc,
$nc_time
);
?></p>
<?php
}
/**
* Add toolbar node for suspending transients
*
* @since 1.6
* @param object $wp_admin_bar
*/
public function suspend_transients_button( $wp_admin_bar ) {
// Bail if user is not capable
if ( ! current_user_can( $this->capability ) ) {
return;
}
// Suspended
if ( get_option( 'pw_tm_suspend' ) ) {
$action = 'unsuspend_transients';
$label = '<span style="color: #b32d2e;">' . esc_html__( 'Unsuspend Transients', 'transients-manager' ) . '</span>';
// Not suspended
} else {
$action = 'suspend_transients';
$label = esc_html__( 'Suspend Transients', 'transients-manager' );
}
// Suspend
$wp_admin_bar->add_node( array(
'id' => 'tm-suspend',
'title' => $label,
'parent' => 'top-secondary',
'href' => wp_nonce_url(
add_query_arg(
array(
'action' => $action
)
),
'transients_manager'
)
) );
// View
$wp_admin_bar->add_node( array(
'id' => 'tm-view',
'title' => esc_html__( 'View Transients', 'transients-manager' ),
'parent' => 'tm-suspend',
'href' => add_query_arg(
array(
'page' => $this->page_id
),
admin_url( 'tools.php' )
)
) );
}
/**
* Get transients from the database
*
* These queries are uncached, to prevent race conditions with persistent
* object cache setups and the way Transients use them.
*
* @since 1.0
* @param array $args
* @return array
*/
private function get_transients( $args = array() ) {
global $wpdb;
// Parse arguments
$r = $this->parse_args( $args );
// Escape some LIKE parts
$esc_name = '%' . $wpdb->esc_like( '_transient_' ) . '%';
$esc_time = '%' . $wpdb->esc_like( '_transient_timeout_' ) . '%';
// SELECT
$sql = array( 'SELECT' );
// COUNT
if ( ! empty( $r['count'] ) ) {
$sql[] = 'count(option_id)';
} else {
$sql[] = '*';
}
// FROM
$sql[] = "FROM {$wpdb->options} WHERE option_name LIKE %s AND option_name NOT LIKE %s";
// Search
if ( ! empty( $r['search'] ) ) {
$search = '%' . $wpdb->esc_like( $r['search'] ) . '%';
$sql[] = $wpdb->prepare( "AND option_name LIKE %s", $search );
}
// Limits
if ( empty( $r['count'] ) ) {
$offset = absint( $r['offset'] );
$number = absint( $r['number'] );
$sql[] = $wpdb->prepare( "ORDER BY option_id DESC LIMIT %d, %d", $offset, $number );
}
// Combine the SQL parts
$query = implode( ' ', $sql );
// Prepare
$prepared = $wpdb->prepare( $query, $esc_name, $esc_time );
// Query
$transients = empty( $r['count'] )
? $wpdb->get_results( $prepared ) // Rows
: $wpdb->get_var( $prepared ); // Count
// Return transients
return $transients;
}
/**
* Parse the query arguments
*
* @since 2.0
* @param array $args
* @return array
*/
private function parse_args( $args = array() ) {
// Parse
$r = wp_parse_args( $args, array(
'offset' => 0,
'number' => 30,
'search' => '',
'count' => false
) );
// Return
return $r;
}
/**
* Retrieve the total number transients in the database
*
* If a search is performed, it returns the number of found results
*
* @since 1.0
* @param string $search
* @return int
*/
private function get_total_transients( $search = '' ) {
// Query
$count = $this->get_transients( array(
'count' => true,
'search' => $search
) );
// Return int
return absint( $count );
}
/**
* Retrieve a transient by its ID
*
* @since 1.0
* @param int $id
* @return object
*/
private function get_transient_by_id( $id = 0 ) {
global $wpdb;
$id = absint( $id );
// Bail if empty ID
if ( empty( $id ) ) {
return false;
}
// Prepare
$prepared = $wpdb->prepare( "SELECT * FROM {$wpdb->options} WHERE option_id = %d", $id );
// Query
return $wpdb->get_row( $prepared );
}
/**
* Is a transient name site-wide?
*
* @since 2.0
* @param string $transient_name
* @return boolean
*/
private function is_site_wide( $transient_name = '' ) {
return ( false !== strpos( $transient_name, '_site_transient' ) );
}
/**
* Retrieve the transient name from the transient object
*
* @since 1.0
* @return string
*/
private function get_transient_name( $transient = false ) {
// Bail if no Transient
if ( empty( $transient ) ) {
return '';
}
// Position
$pos = $this->is_site_wide( $transient->option_name )
? 16
: 11;
return substr( $transient->option_name, $pos, strlen( $transient->option_name ) );
}
/**
* Retrieve the human-friendly transient value from the transient object
*
* @since 1.0
* @param object $transient
* @return string/int
*/
private function get_transient_value( $transient ) {
// Get the value type
$type = $this->get_transient_value_type( $transient );
// Trim value to 100 chars
$value = substr( $transient->option_value, 0, 100 );
// Escape & wrap in <code> tag
$value = '<code>' . esc_html( $value ) . '</code>';
// Return
return $value . '<br><span class="transient-type badge">' . esc_html( $type ) . '</span>';
}
/**
* Try to guess the type of value the Transient is
*
* @since 2.0
* @param object $transient
* @return string
*/
private function get_transient_value_type( $transient ) {
// Default type
$type = esc_html__( 'unknown', 'transients-manager' );
// Try to unserialize
$value = maybe_unserialize( $transient->option_value );
// Array
if ( is_array( $value ) ) {
$type = esc_html__( 'array', 'transients-manager' );
// Object
} elseif ( is_object( $value ) ) {
$type = esc_html__( 'object', 'transients-manager' );
// Serialized array
} elseif ( is_serialized( $value ) ) {
$type = esc_html__( 'serialized', 'transients-manager' );
// HTML
} elseif ( strip_tags( $value ) !== $value ) {
$type = esc_html__( 'html', 'transients-manager' );
// Scalar
} elseif ( is_scalar( $value ) ) {
if ( is_numeric( $value ) ) {
// Likely a timestamp
if ( 10 === strlen( $value ) ) {
$type = esc_html__( 'timestamp?', 'transients-manager' );
// Likely a boolean
} elseif ( in_array( $value, array( '0', '1' ), true ) ) {
$type = esc_html__( 'boolean?', 'transients-manager' );
// Any number
} else {
$type = esc_html__( 'numeric', 'transients-manager' );
}
// JSON
} elseif ( is_string( $value ) && is_object( json_decode( $value ) ) ) {
$type = esc_html__( 'json', 'transients-manager' );
// Scalar
} else {
$type = esc_html__( 'scalar', 'transients-manager' );
}
// Empty
} elseif ( empty( $value ) ) {
$type = esc_html__( 'empty', 'transients-manager' );
}
// Return type
return $type;
}
/**
* Retrieve the expiration timestamp
*
* @since 1.0
* @param object $transient
* @return int
*/
private function get_transient_expiration_time( $transient ) {
// Get the same to use in the option key
$name = $this->get_transient_name( $transient );
// Get the value of the timeout
$time = $this->is_site_wide( $transient->option_name )
? get_option( "_site_transient_timeout_{$name}" )
: get_option( "_transient_timeout_{$name}" );
// Return the value
return $time;
}
/**
* Retrieve the human-friendly expiration time
*
* @since 1.0
* @param object $transient
* @return string
*/
private function get_transient_expiration( $transient ) {
$expiration = $this->get_transient_expiration_time( $transient );
// Bail if no expiration
if ( empty( $expiration ) ) {
return '—<br><span class="badge">' . esc_html__( 'Persistent', 'transients-manager' ) . '</span>';
}
// UTC & local dates
$date_utc = gmdate( 'Y-m-d\TH:i:s+00:00', $expiration );
$date_local = get_date_from_gmt( date( 'Y-m-d H:i:s', $expiration ), 'Y-m-d H:i:s' );
// Create <time> tag
$time = sprintf(
'<time datetime="%1$s" title="%1$s">%2$s</time>',
esc_attr( $date_utc ),
esc_html( $date_local )
);
// Expired
if ( $this->time_now > $expiration ) {
return $time . '<br><span class="transient-expired badge">' . esc_html__( 'Expired', 'transients-manager' ) . '</span>';
}
// Return time since
return $time . '<br><span class="badge green">' . $this->time_since( $expiration - $this->time_now ) . '</span>';
}
/**
* Process delete and update actions
*
* @since 1.0
*/
public function process_actions() {
if ( empty( $this->action ) ) {
return;
}
// Bail if malformed Transient request
if ( empty( $_REQUEST['transient'] ) && ! in_array( $this->action, array( 'suspend_transients', 'unsuspend_transients' ), true ) ) {
return;
}
// Bail if user is not capable
if ( ! current_user_can( $this->capability ) ) {
return;
}
// Bail if nonce fails
if ( ! empty( $_REQUEST['_wpnonce'] ) && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'transients_manager' ) ) {
return;
}
if ( ! in_array( $this->action, array( 'suspend_transients', 'unsuspend_transients' ), true ) ) {
// Encode search string
$search = ! empty( $_REQUEST['s'] )
? urlencode( $_REQUEST['s'] )
: '';
// Sanitize transient