-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathclass-admin.php
More file actions
1133 lines (979 loc) · 29.1 KB
/
class-admin.php
File metadata and controls
1133 lines (979 loc) · 29.1 KB
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
/**
* Centralized manager for WordPress backend functionality.
*
* @package WP_Stream
*/
namespace WP_Stream;
use DateTime;
use DateTimeZone;
use DateInterval;
use \WP_CLI;
use \WP_Roles;
/**
* Class - Admin
*/
class Admin {
/**
* Holds Instance of plugin object
*
* @var Plugin
*/
public $plugin;
/**
* Holds Network class
*
* @var Network
*/
public $network;
/**
* Holds Live Update class
*
* @var Live_Update
*/
public $live_update;
/**
* Holds Export class
*
* @var Export
*/
public $export;
/**
* Menu page screen id
*
* @var string
*/
public $screen_id = array();
/**
* List table object
*
* @var List_Table
*/
public $list_table = null;
/**
* Option to disable access to Stream
*
* @var bool
*/
public $disable_access = false;
/**
* Class applied to the body of the admin screen
*
* @var string
*/
public $admin_body_class = 'wp_stream_screen';
/**
* Slug of the records page
*
* @var string
*/
public $records_page_slug = 'wp_stream';
/**
* Slug of the settings page
*
* @var string
*/
public $settings_page_slug = 'wp_stream_settings';
/**
* Parent page of the records and settings pages
*
* @var string
*/
public $admin_parent_page = 'admin.php';
/**
* Capability name for viewing records
*
* @var string
*/
public $view_cap = 'view_stream';
/**
* Capability name for viewing settings
*
* @var string
*/
public $settings_cap = 'manage_options';
/**
* Total amount of authors to pre-load
*
* @var int
*/
public $preload_users_max = 50;
/**
* Admin notices, collected and displayed on proper action
*
* @var array
*/
public $notices = array();
/**
* Class constructor.
*
* @param Plugin $plugin Instance of plugin object.
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
add_action( 'init', array( $this, 'init' ) );
// Ensure function used in various methods is pre-loaded.
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
// User and role caps.
add_filter( 'user_has_cap', array( $this, 'filter_user_caps' ), 10, 4 );
add_filter( 'role_has_cap', array( $this, 'filter_role_caps' ), 10, 3 );
if ( is_multisite() && $plugin->is_network_activated() && ! is_network_admin() ) {
$options = (array) get_site_option(
'wp_stream_network',
did_action( 'init' ) ? $this->plugin->settings->get_defaults() : array()
);
$option = isset( $options['general_site_access'] ) ? absint( $options['general_site_access'] ) : 1;
$this->disable_access = ( $option ) ? false : true;
}
// Register settings page.
if ( ! $this->disable_access ) {
add_action( 'admin_menu', array( $this, 'register_menu' ) );
}
// Admin notices.
add_action( 'admin_notices', array( $this, 'prepare_admin_notices' ) );
add_action( 'shutdown', array( $this, 'admin_notices' ) );
// Add admin body class.
add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
// Plugin action links.
add_filter(
'plugin_action_links',
array(
$this,
'plugin_action_links',
),
10,
2
);
// Load admin scripts and styles.
add_action(
'admin_enqueue_scripts',
array(
$this,
'admin_enqueue_scripts',
)
);
add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) );
// Reset Streams database.
add_action(
'wp_ajax_wp_stream_reset',
array(
$this,
'wp_ajax_reset',
)
);
/**
* Uninstall Streams and Deactivate plugin.
*
* @todo Confirm if variable assignment is necessary.
*/
$uninstall = $this->plugin->db->driver->purge_storage( $this->plugin );
// Auto purge setup.
add_action( 'wp_loaded', array( $this, 'purge_schedule_setup' ) );
add_action(
'wp_stream_auto_purge',
array(
$this,
'purge_scheduled_action',
)
);
// Ajax users list.
add_action(
'wp_ajax_wp_stream_filters',
array(
$this,
'ajax_filters',
)
);
}
/**
* Load admin classes
*
* @action init
*/
public function init() {
$this->network = new Network( $this->plugin );
$this->live_update = new Live_Update( $this->plugin );
$this->export = new Export( $this->plugin );
}
/**
* Output specific updates passed as URL parameters.
*
* @action admin_notices
*
* @return void
*/
public function prepare_admin_notices() {
$message = wp_stream_filter_input( INPUT_GET, 'message' );
switch ( $message ) {
case 'settings_reset':
$this->notice( esc_html__( 'All site settings have been successfully reset.', 'stream' ) );
break;
}
}
/**
* Handle notice messages according to the appropriate context (WP-CLI or the WP Admin)
*
* @param string $message Message to output.
* @param bool $is_error If the message is error_level (true) or warning (false).
*/
public function notice( $message, $is_error = true ) {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
$message = wp_strip_all_tags( $message );
if ( $is_error ) {
WP_CLI::warning( $message );
} else {
WP_CLI::success( $message );
}
} else {
// Trigger admin notices late, so that any notices which occur during page load are displayed.
add_action( 'shutdown', array( $this, 'admin_notices' ) );
$notice = compact( 'message', 'is_error' );
if ( ! in_array( $notice, $this->notices, true ) ) {
$this->notices[] = $notice;
}
}
}
/**
* Show an error or other message in the WP Admin
*
* @action shutdown
*/
public function admin_notices() {
global $allowedposttags;
$custom = array(
'progress' => array(
'class' => true,
'id' => true,
'max' => true,
'style' => true,
'value' => true,
),
);
$allowed_html = array_merge( $allowedposttags, $custom );
ksort( $allowed_html );
foreach ( $this->notices as $notice ) {
$class_name = empty( $notice['is_error'] ) ? 'updated' : 'error';
$html_message = sprintf( '<div class="%s">%s</div>', esc_attr( $class_name ), wpautop( $notice['message'] ) );
echo wp_kses( $html_message, $allowed_html );
}
}
/**
* Register menu page
*
* @action admin_menu
*
* @return void
*/
public function register_menu() {
/**
* Filter the main admin menu title
*
* @return string
*/
$main_menu_title = apply_filters( 'wp_stream_admin_menu_title', esc_html__( 'Stream', 'stream' ) );
/**
* Filter the main admin menu position
*
* Note: Using longtail decimal string to reduce the chance of position conflicts, see Codex
*
* @return string
*/
$main_menu_position = apply_filters( 'wp_stream_menu_position', '2.999999' );
/**
* Filter the main admin page title
*
* @return string
*/
$main_page_title = apply_filters( 'wp_stream_admin_page_title', esc_html__( 'Stream Records', 'stream' ) );
$this->screen_id['main'] = add_menu_page(
$main_page_title,
$main_menu_title,
$this->view_cap,
$this->records_page_slug,
array( $this, 'render_list_table' ),
'div',
$main_menu_position
);
/**
* Fires before submenu items are added to the Stream menu
* allowing plugins to add menu items before Settings
*
* @return void
*/
do_action( 'wp_stream_admin_menu' );
/**
* Filter the Settings admin page title
*
* @return string
*/
$settings_page_title = apply_filters( 'wp_stream_settings_form_title', esc_html__( 'Stream Settings', 'stream' ) );
$this->screen_id['settings'] = add_submenu_page(
$this->records_page_slug,
$settings_page_title,
esc_html__( 'Settings', 'stream' ),
$this->settings_cap,
$this->settings_page_slug,
array( $this, 'render_settings_page' )
);
if ( isset( $this->screen_id['main'] ) ) {
/**
* Fires just before the Stream list table is registered.
*
* @return void
*/
do_action( 'wp_stream_admin_menu_screens' );
// Register the list table early, so it associates the column headers with 'Screen settings'.
add_action(
'load-' . $this->screen_id['main'],
array(
$this,
'register_list_table',
)
);
}
}
/**
* Enqueue scripts/styles for admin screen
*
* @action admin_enqueue_scripts
*
* @param string $hook Current hook.
*
* @return void
*/
public function admin_enqueue_scripts( $hook ) {
wp_register_script( 'wp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/js/select2.full.min.js', array( 'jquery' ), '3.5.2', true );
wp_register_style( 'wp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/css/select2.min.css', array(), '3.5.2' );
wp_register_script( 'wp-stream-timeago', $this->plugin->locations['url'] . 'ui/lib/timeago/jquery.timeago.js', array(), '1.4.1', true );
$locale = strtolower( substr( get_locale(), 0, 2 ) );
$file_tmpl = 'ui/lib/timeago/locales/jquery.timeago.%s.js';
if ( file_exists( $this->plugin->locations['dir'] . sprintf( $file_tmpl, $locale ) ) ) {
wp_register_script(
'wp-stream-timeago-locale',
$this->plugin->locations['url'] . sprintf( $file_tmpl, $locale ),
array( 'wp-stream-timeago' ),
'1',
false
);
} else {
wp_register_script(
'wp-stream-timeago-locale',
$this->plugin->locations['url'] . sprintf( $file_tmpl, 'en' ),
array( 'wp-stream-timeago' ),
'1',
false
);
}
$min = wp_stream_min_suffix();
wp_enqueue_style( 'wp-stream-admin', $this->plugin->locations['url'] . 'ui/css/admin.' . $min . 'css', array(), $this->plugin->get_version() );
$script_screens = array( 'plugins.php' );
if ( in_array( $hook, $this->screen_id, true ) || in_array( $hook, $script_screens, true ) ) {
wp_enqueue_script( 'wp-stream-select2' );
wp_enqueue_style( 'wp-stream-select2' );
wp_enqueue_script( 'wp-stream-timeago' );
wp_enqueue_script( 'wp-stream-timeago-locale' );
wp_enqueue_script(
'wp-stream-admin',
$this->plugin->locations['url'] . 'ui/js/admin.' . $min . 'js',
array(
'jquery',
'wp-stream-select2',
),
$this->plugin->get_version(),
false
);
wp_enqueue_script(
'wp-stream-admin-exclude',
$this->plugin->locations['url'] . 'ui/js/exclude.' . $min . 'js',
array(
'jquery',
'wp-stream-select2',
),
$this->plugin->get_version(),
false
);
wp_enqueue_script(
'wp-stream-live-updates',
$this->plugin->locations['url'] . 'ui/js/live-updates.' . $min . 'js',
array(
'jquery',
'heartbeat',
),
$this->plugin->get_version(),
false
);
wp_localize_script(
'wp-stream-admin',
'wp_stream',
array(
'i18n' => array(
'confirm_purge' => esc_html__( 'Are you sure you want to delete all Stream activity records from the database? This cannot be undone.', 'stream' ),
'confirm_defaults' => esc_html__( 'Are you sure you want to reset all site settings to default? This cannot be undone.', 'stream' ),
'confirm_uninstall' => esc_html__( 'Are you sure you want to uninstall and deactivate Stream? This will delete all Stream tables from the database and cannot be undone.', 'stream' ),
),
'locale' => esc_js( $locale ),
'gmt_offset' => get_option( 'gmt_offset' ),
)
);
$order_types = array( 'asc', 'desc' );
wp_localize_script(
'wp-stream-live-updates',
'wp_stream_live_updates',
array(
'current_screen' => $hook,
'current_page' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : '1', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
'current_order' => isset( $_GET['order'] ) && in_array( strtolower( $_GET['order'] ), $order_types, true ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
? esc_js( $_GET['order'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
: 'desc',
'current_query' => wp_stream_json_encode( $_GET ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
'current_query_count' => count( $_GET ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
)
);
}
/**
* The maximum number of items that can be updated in bulk without receiving a warning.
*
* Stream watches for bulk actions performed in the WordPress Admin (such as updating
* many posts at once) and warns the user before proceeding if the number of items they
* are attempting to update exceeds this threshold value. Since Stream will try to save
* a log for each item, it will take longer than usual to complete the operation.
*
* The default threshold is 100 items.
*
* @return int
*/
$bulk_actions_threshold = apply_filters( 'wp_stream_bulk_actions_threshold', 100 );
wp_enqueue_script(
'wp-stream-global',
$this->plugin->locations['url'] . 'ui/js/global.' . $min . 'js',
array( 'jquery' ),
$this->plugin->get_version(),
false
);
wp_localize_script(
'wp-stream-global',
'wp_stream_global',
array(
'bulk_actions' => array(
'i18n' => array(
/* translators: %s: a number of items (e.g. "1,742") */
'confirm_action' => sprintf( esc_html__( 'Are you sure you want to perform bulk actions on over %s items? This process could take a while to complete.', 'stream' ), number_format( absint( $bulk_actions_threshold ) ) ),
),
'threshold' => absint( $bulk_actions_threshold ),
),
'plugins_screen_url' => self_admin_url( 'plugins.php#stream' ),
)
);
}
/**
* Check whether or not the current admin screen belongs to Stream
*
* @return bool
*/
public function is_stream_screen() {
if ( is_admin() && false !== strpos( wp_stream_filter_input( INPUT_GET, 'page' ), $this->records_page_slug ) ) {
return true;
}
$screen = get_current_screen();
if ( is_admin() && Alerts::POST_TYPE === $screen->post_type ) {
return true;
}
return false;
}
/**
* Add a specific body class to all Stream admin screens
*
* @param string $classes CSS classes to output to body.
*
* @filter admin_body_class
*
* @return string
*/
public function admin_body_class( $classes ) {
$stream_classes = array();
if ( $this->is_stream_screen() ) {
$stream_classes[] = $this->admin_body_class;
if ( isset( $_GET['page'] ) ) { // // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$stream_classes[] = sanitize_key( $_GET['page'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
}
}
/**
* Filter the Stream admin body classes
*
* @return array
*/
$stream_classes = apply_filters( 'wp_stream_admin_body_classes', $stream_classes );
$stream_classes = implode( ' ', array_map( 'trim', $stream_classes ) );
return sprintf( '%s %s ', $classes, $stream_classes );
}
/**
* Add menu styles for various WP Admin skins
*
* @uses \wp_add_inline_style()
*
* @action admin_enqueue_scripts
*/
public function admin_menu_css() {
$min = wp_stream_min_suffix();
wp_register_style( 'wp-stream-datepicker', $this->plugin->locations['url'] . 'ui/css/datepicker.' . $min . 'css', array(), $this->plugin->get_version() );
wp_register_style( 'wp-stream-icons', $this->plugin->locations['url'] . 'ui/stream-icons/style.css', array(), $this->plugin->get_version() );
// Make sure we're working off a clean version.
if ( ! file_exists( ABSPATH . WPINC . '/version.php' ) ) {
return;
}
include ABSPATH . WPINC . '/version.php';
if ( ! isset( $wp_version ) ) {
return;
}
$body_class = $this->admin_body_class;
$records_page = $this->records_page_slug;
$stream_url = $this->plugin->locations['url'];
if ( version_compare( $wp_version, '3.8-alpha', '>=' ) ) {
wp_enqueue_style( 'wp-stream-icons' );
$css = "
#toplevel_page_{$records_page} .wp-menu-image:before {
font-family: 'WP Stream' !important;
content: '\\73' !important;
}
#toplevel_page_{$records_page} .wp-menu-image {
background-repeat: no-repeat;
}
#menu-posts-feedback .wp-menu-image:before {
font-family: dashicons !important;
content: '\\f175';
}
#adminmenu #menu-posts-feedback div.wp-menu-image {
background: none !important;
background-repeat: no-repeat;
}
body.{$body_class} #wpbody-content .wrap h1:nth-child(1):before {
font-family: 'WP Stream' !important;
content: '\\73';
padding: 0 8px 0 0;
}
";
} else {
$css = "
#toplevel_page_{$records_page} .wp-menu-image {
background: url( {$stream_url}ui/stream-icons/menuicon-sprite.png ) 0 90% no-repeat;
}
/* Retina Stream Menu Icon */
@media only screen and (-moz-min-device-pixel-ratio: 1.5),
only screen and (-o-min-device-pixel-ratio: 3/2),
only screen and (-webkit-min-device-pixel-ratio: 1.5),
only screen and (min-device-pixel-ratio: 1.5) {
#toplevel_page_{$records_page} .wp-menu-image {
background: url( {$stream_url}ui/stream-icons/menuicon-sprite-2x.png ) 0 90% no-repeat;
background-size:30px 64px;
}
}
#toplevel_page_{$records_page}.current .wp-menu-image,
#toplevel_page_{$records_page}.wp-has-current-submenu .wp-menu-image,
#toplevel_page_{$records_page}:hover .wp-menu-image {
background-position: top left;
}
";
}
\wp_add_inline_style( 'wp-admin', $css );
}
/**
* Handle the reset AJAX request to reset logs.
*
* @return bool
*/
public function wp_ajax_reset() {
check_ajax_referer( 'stream_nonce_reset', 'wp_stream_nonce_reset' );
if ( ! current_user_can( $this->settings_cap ) ) {
wp_die(
esc_html__( "You don't have sufficient privileges to do this action.", 'stream' )
);
}
$this->erase_stream_records();
if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
return true;
}
wp_safe_redirect(
add_query_arg(
array(
'page' => is_network_admin() ? $this->network->network_settings_page_slug : $this->settings_page_slug,
'message' => 'data_erased',
),
self_admin_url( $this->admin_parent_page )
)
);
exit;
}
/**
* Clears stream records from the database.
*
* @return void
*/
private function erase_stream_records() {
global $wpdb;
$where = '';
if ( is_multisite() && ! $this->plugin->is_network_activated() ) {
$where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() );
}
$wpdb->query(
"DELETE `stream`, `meta`
FROM {$wpdb->stream} AS `stream`
LEFT JOIN {$wpdb->streammeta} AS `meta`
ON `meta`.`record_id` = `stream`.`ID`
WHERE 1=1 {$where};" // @codingStandardsIgnoreLine $where already prepared
);
}
/**
* Schedules a purge of records.
*
* @return void
*/
public function purge_schedule_setup() {
if ( ! wp_next_scheduled( 'wp_stream_auto_purge' ) ) {
wp_schedule_event( time(), 'twicedaily', 'wp_stream_auto_purge' );
}
}
/**
* Executes a scheduled purge
*
* @return void
*/
public function purge_scheduled_action() {
global $wpdb;
// Don't purge when in Network Admin unless Stream is network activated.
if (
is_multisite()
&&
is_network_admin()
&&
! $this->plugin->is_network_activated()
) {
return;
}
$defaults = $this->plugin->settings->get_defaults();
if ( is_multisite() && $this->plugin->is_network_activated() ) {
$options = (array) get_site_option( 'wp_stream_network', $defaults );
} else {
$options = (array) get_option( 'wp_stream', $defaults );
}
if ( ! empty( $options['general_keep_records_indefinitely'] ) || ! isset( $options['general_records_ttl'] ) ) {
return;
}
$days = $options['general_records_ttl'];
$timezone = new DateTimeZone( 'UTC' );
$date = new DateTime( 'now', $timezone );
$date->sub( DateInterval::createFromDateString( "$days days" ) );
$where = $wpdb->prepare( ' AND `stream`.`created` < %s', $date->format( 'Y-m-d H:i:s' ) );
// Multisite but NOT network activated, only purge the current blog.
if ( is_multisite() && ! $this->plugin->is_network_activated() ) {
$where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() );
}
$wpdb->query(
"DELETE `stream`, `meta`
FROM {$wpdb->stream} AS `stream`
LEFT JOIN {$wpdb->streammeta} AS `meta`
ON `meta`.`record_id` = `stream`.`ID`
WHERE 1=1 {$where};" // @codingStandardsIgnoreLine $where already prepared
);
}
/**
* Returns the admin action links.
*
* @filter plugin_action_links
*
* @param array $links Action links.
* @param string $file Plugin file.
*
* @return array
*/
public function plugin_action_links( $links, $file ) {
if ( plugin_basename( $this->plugin->locations['dir'] . 'stream.php' ) !== $file ) {
return $links;
}
// Also don't show links in Network Admin if Stream isn't network enabled.
if ( is_network_admin() && is_multisite() && ! $this->plugin->is_network_activated() ) {
return $links;
}
if ( is_network_admin() ) {
$admin_page_url = add_query_arg(
array(
'page' => $this->network->network_settings_page_slug,
),
network_admin_url( $this->admin_parent_page )
);
} else {
$admin_page_url = add_query_arg(
array(
'page' => $this->settings_page_slug,
),
admin_url( $this->admin_parent_page )
);
}
$links[] = sprintf( '<a href="%s">%s</a>', esc_url( $admin_page_url ), esc_html__( 'Settings', 'default' ) );
if ( ! defined( 'DISALLOW_FILE_MODS' ) || false === DISALLOW_FILE_MODS ) {
$url = add_query_arg(
array(
'action' => 'wp_stream_uninstall',
'wp_stream_nonce' => wp_create_nonce( 'stream_nonce' ),
),
admin_url( 'admin-ajax.php' )
);
$links[] = sprintf( '<span id="wp_stream_uninstall" class="delete"><a href="%s">%s</a></span>', esc_url( $url ), esc_html__( 'Uninstall', 'stream' ) );
}
return $links;
}
/**
* Render main page
*/
public function render_list_table() {
$this->list_table->prepare_items();
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<?php $this->list_table->display(); ?>
</div>
<?php
}
/**
* Render settings page
*/
public function render_settings_page() {
$option_key = $this->plugin->settings->option_key;
$form_action = apply_filters( 'wp_stream_settings_form_action', admin_url( 'options.php' ) );
$page_description = apply_filters( 'wp_stream_settings_form_description', '' );
$sections = $this->plugin->settings->get_fields();
$active_tab = wp_stream_filter_input( INPUT_GET, 'tab' );
$min = wp_stream_min_suffix();
wp_enqueue_script( 'wp-stream-settings', $this->plugin->locations['url'] . 'ui/js/settings.' . $min . 'js', array( 'jquery' ), $this->plugin->get_version(), true );
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<?php if ( ! empty( $page_description ) ) : ?>
<p><?php echo esc_html( $page_description ); ?></p>
<?php endif; ?>
<?php settings_errors(); ?>
<?php if ( count( $sections ) > 1 ) : ?>
<h2 class="nav-tab-wrapper">
<?php $i = 0; ?>
<?php foreach ( $sections as $section => $data ) : ?>
<?php $i++; ?>
<?php $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section ); ?>
<a href="<?php echo esc_url( add_query_arg( 'tab', $section ) ); ?>" class="nav-tab <?php echo $is_active ? esc_attr( ' nav-tab-active' ) : ''; ?>">
<?php echo esc_html( $data['title'] ); ?>
</a>
<?php endforeach; ?>
</h2>
<?php endif; ?>
<div class="nav-tab-content" id="tab-content-settings">
<form method="post" action="<?php echo esc_attr( $form_action ); ?>" enctype="multipart/form-data">
<div class="settings-sections">
<?php
$i = 0;
foreach ( $sections as $section => $data ) {
$i++;
$is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section );
if ( $is_active ) {
settings_fields( $option_key );
do_settings_sections( $option_key );
}
}
?>
</div>
<?php submit_button(); ?>
</form>
</div>
</div>
<?php
}
/**
* Instantiate the list table
*/
public function register_list_table() {
$this->list_table = new List_Table(
$this->plugin,
array(
'screen' => $this->screen_id['main'],
)
);
}
/**
* Check if a particular role has access
*
* @param string $role User role.
*
* @return bool
*/
private function role_can_view( $role ) {
if ( in_array( $role, $this->plugin->settings->options['general_role_access'], true ) ) {
return true;
}
return false;
}
/**
* Filter user caps to dynamically grant our view cap based on allowed roles
*
* @param array $allcaps All capabilities.
* @param array $caps Required caps.
* @param array $args Unused.
* @param WP_User $user User.
*
* @filter user_has_cap
*
* @return array
*/
public function filter_user_caps( $allcaps, $caps, $args, $user = null ) {
global $wp_roles;
$_wp_roles = isset( $wp_roles ) ? $wp_roles : new WP_Roles();
$user = is_a( $user, 'WP_User' ) ? $user : wp_get_current_user();
// @see
// https://github.com/WordPress/WordPress/blob/c67c9565f1495255807069fdb39dac914046b1a0/wp-includes/capabilities.php#L758
$roles = array_unique(
array_merge(
$user->roles,
array_filter(
array_keys( $user->caps ),
array( $_wp_roles, 'is_role' )
)
)
);
$stream_view_caps = array( $this->view_cap );
foreach ( $caps as $cap ) {
if ( in_array( $cap, $stream_view_caps, true ) ) {
foreach ( $roles as $role ) {
if ( $this->role_can_view( $role ) ) {
$allcaps[ $cap ] = true;
break 2;
}
}
}
}
return $allcaps;
}
/**
* Filter role caps to dynamically grant our view cap based on allowed roles
*
* @filter role_has_cap
*
* @param array $allcaps All capabilities.
* @param string $cap Require cap.
* @param string $role User role.
*
* @return array
*/
public function filter_role_caps( $allcaps, $cap, $role ) {
$stream_view_caps = array( $this->view_cap );
if ( in_array( $cap, $stream_view_caps, true ) && $this->role_can_view( $role ) ) {