PluginProbe
Stream – Activity Log & Audit Trail / 3.4.2
Stream – Activity Log & Audit Trail v3.4.2
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
← All changes | classes/class-admin.php +257 -207 3.0.13.4.2 View file →
@@ -1,5 +1,6 @@
1 1 <?php
2 +
2 3 namespace WP_Stream;
3 4
4 5 use DateTime;
5 6 use DateTimeZone;
@@ -7,28 +8,36 @@
7 8 use \WP_CLI;
8 9 use \WP_Roles;
9 10
10 11 class Admin {
12 +
11 13 /**
12 14 * Hold Plugin class
15 + *
13 16 * @var Plugin
14 17 */
15 18 public $plugin;
16 19
17 20 /**
21 + * Holds Network class
22 + *
18 23 * @var Network
19 24 */
20 25 public $network;
21 26
22 27 /**
28 + * Holds Live Update class
29 + *
23 30 * @var Live_Update
24 31 */
25 32 public $live_update;
26 33
27 34 /**
28 - * @var Migrate
35 + * Holds Export class
36 + *
37 + * @var Export
29 38 */
30 - public $migrate;
39 + public $export;
31 40
32 41 /**
33 42 * Menu page screen id
34 43 *
@@ -115,18 +124,18 @@
115 124 $this->plugin = $plugin;
116 125
117 126 add_action( 'init', array( $this, 'init' ) );
118 127
119 - // Ensure function used in various methods is pre-loaded
128 + // Ensure function used in various methods is pre-loaded.
120 129 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
121 - require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
130 + require_once ABSPATH . '/wp-admin/includes/plugin.php';
122 131 }
123 132
124 - // User and role caps
133 + // User and role caps.
125 134 add_filter( 'user_has_cap', array( $this, 'filter_user_caps' ), 10, 4 );
126 135 add_filter( 'role_has_cap', array( $this, 'filter_role_caps' ), 10, 3 );
127 136
128 - if ( is_multisite() && is_plugin_active_for_network( $this->plugin->locations['plugin'] ) && ! is_network_admin() ) {
137 + if ( is_multisite() && $plugin->is_network_activated() && ! is_network_admin() ) {
129 138 $options = (array) get_site_option( 'wp_stream_network', array() );
130 139 $option = isset( $options['general_site_access'] ) ? absint( $options['general_site_access'] ) : 1;
131 140
132 141 $this->disable_access = ( $option ) ? false : true;
@@ -131,49 +140,71 @@
131 140
132 141 $this->disable_access = ( $option ) ? false : true;
133 142 }
134 143
135 - // Register settings page
144 + // Register settings page.
136 145 if ( ! $this->disable_access ) {
137 146 add_action( 'admin_menu', array( $this, 'register_menu' ) );
138 147 }
139 148
140 - // Admin notices
149 + // Admin notices.
141 150 add_action( 'admin_notices', array( $this, 'prepare_admin_notices' ) );
142 151 add_action( 'shutdown', array( $this, 'admin_notices' ) );
143 152
144 - // Add admin body class
153 + // Add admin body class.
145 154 add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
146 155
147 - // Plugin action links
148 - add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2 );
156 + // Plugin action links.
157 + add_filter(
158 + 'plugin_action_links',
159 + array(
160 + $this,
161 + 'plugin_action_links',
162 + ),
163 + 10,
164 + 2
165 + );
149 166
150 - // Load admin scripts and styles
151 - add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
167 + // Load admin scripts and styles.
168 + add_action(
169 + 'admin_enqueue_scripts',
170 + array(
171 + $this,
172 + 'admin_enqueue_scripts',
173 + )
174 + );
152 175 add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) );
153 176
154 - // Reset Streams database
155 - add_action( 'wp_ajax_wp_stream_reset', array( $this, 'wp_ajax_reset' ) );
177 + // Reset Streams database.
178 + add_action(
179 + 'wp_ajax_wp_stream_reset',
180 + array(
181 + $this,
182 + 'wp_ajax_reset',
183 + )
184 + );
156 185
157 - // Uninstall Streams and Deactivate plugin
158 - $uninstall = new Uninstall( $this->plugin );
159 - add_action( 'wp_ajax_wp_stream_uninstall', array( $uninstall, 'uninstall' ) );
186 + // Uninstall Streams and Deactivate plugin.
187 + $uninstall = $this->plugin->db->driver->purge_storage( $this->plugin );
160 188
161 - // Auto purge setup
189 + // Auto purge setup.
162 190 add_action( 'wp_loaded', array( $this, 'purge_schedule_setup' ) );
163 - add_action( 'wp_stream_auto_purge', array( $this, 'purge_scheduled_action' ) );
191 + add_action(
192 + 'wp_stream_auto_purge',
193 + array(
194 + $this,
195 + 'purge_scheduled_action',
196 + )
197 + );
164 198
165 - // Ajax users list
166 - add_action( 'wp_ajax_wp_stream_filters', array( $this, 'ajax_filters' ) );
167 -
168 - // Ajax user's name by ID
169 - add_action( 'wp_ajax_wp_stream_get_filter_value_by_id', array( $this, 'get_filter_value_by_id' ) );
170 -
171 - // Ajax users list
172 - add_action( 'wp_ajax_wp_stream_filters', array( $this, 'ajax_filters' ) );
173 -
174 - // Ajax user's name by ID
175 - add_action( 'wp_ajax_wp_stream_get_filter_value_by_id', array( $this, 'get_filter_value_by_id' ) );
199 + // Ajax users list.
200 + add_action(
201 + 'wp_ajax_wp_stream_filters',
202 + array(
203 + $this,
204 + 'ajax_filters',
205 + )
206 + );
176 207 }
177 208
178 209 /**
179 210 * Load admin classes
@@ -182,17 +213,17 @@
182 213 */
183 214 public function init() {
184 215 $this->network = new Network( $this->plugin );
185 216 $this->live_update = new Live_Update( $this->plugin );
186 - $this->migrate = new Migrate( $this->plugin );
217 + $this->export = new Export( $this->plugin );
187 218 }
188 219
189 220 /**
190 - * Output specific updates passed as URL parameters
221 + * Output specific updates passed as URL parameters.
191 222 *
192 223 * @action admin_notices
193 224 *
194 - * @return string
225 + * @return void
195 226 */
196 227 public function prepare_admin_notices() {
197 228 $message = wp_stream_filter_input( INPUT_GET, 'message' );
198 229
@@ -205,10 +236,10 @@
205 236
206 237 /**
207 238 * Handle notice messages according to the appropriate context (WP-CLI or the WP Admin)
208 239 *
209 - * @param string $message
210 - * @param bool $is_error
240 + * @param string $message Message to output.
241 + * @param bool $is_error If the message is error_level (true) or warning (false).
211 242 */
212 243 public function notice( $message, $is_error = true ) {
213 244 if ( defined( 'WP_CLI' ) && WP_CLI ) {
214 245 $message = strip_tags( $message );
@@ -218,14 +249,14 @@
218 249 } else {
219 250 WP_CLI::success( $message );
220 251 }
221 252 } else {
222 - // Trigger admin notices late, so that any notices which occur during page load are displayed
253 + // Trigger admin notices late, so that any notices which occur during page load are displayed.
223 254 add_action( 'shutdown', array( $this, 'admin_notices' ) );
224 255
225 256 $notice = compact( 'message', 'is_error' );
226 257
227 - if ( ! in_array( $notice, $this->notices ) ) {
258 + if ( ! in_array( $notice, $this->notices, true ) ) {
228 259 $this->notices[] = $notice;
229 260 }
230 261 }
231 262 }
@@ -264,9 +295,9 @@
264 295 * Register menu page
265 296 *
266 297 * @action admin_menu
267 298 *
268 - * @return bool|void
299 + * @return void
269 300 */
270 301 public function register_menu() {
271 302 /**
272 303 * Filter the main admin menu title
@@ -301,8 +332,16 @@
301 332 $main_menu_position
302 333 );
303 334
304 335 /**
336 + * Fires before submenu items are added to the Stream menu
337 + * allowing plugins to add menu items before Settings
338 + *
339 + * @return void
340 + */
341 + do_action( 'wp_stream_admin_menu' );
342 +
343 + /**
305 344 * Filter the Settings admin page title
306 345 *
307 346 * @return string
308 347 */
@@ -324,10 +363,16 @@
324 363 * @return void
325 364 */
326 365 do_action( 'wp_stream_admin_menu_screens' );
327 366
328 - // Register the list table early, so it associates the column headers with 'Screen settings'
329 - add_action( 'load-' . $this->screen_id['main'], array( $this, 'register_list_table' ) );
367 + // Register the list table early, so it associates the column headers with 'Screen settings'.
368 + add_action(
369 + 'load-' . $this->screen_id['main'],
370 + array(
371 + $this,
372 + 'register_list_table',
373 + )
374 + );
330 375 }
331 376 }
332 377
333 378 /**
@@ -339,34 +384,60 @@
339 384 *
340 385 * @return void
341 386 */
342 387 public function admin_enqueue_scripts( $hook ) {
343 - wp_register_script( 'select2', $this->plugin->locations['url'] . 'ui/lib/select2/select2.js', array( 'jquery' ), '3.5.2', true );
344 - wp_register_style( 'select2', $this->plugin->locations['url'] . 'ui/lib/select2/select2.css', array(), '3.5.2' );
345 - wp_register_script( 'timeago', $this->plugin->locations['url'] . 'ui/lib/timeago/jquery.timeago.js', array(), '1.4.1', true );
388 + wp_register_script( 'wp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/js/select2.full.min.js', array( 'jquery' ), '3.5.2', true );
389 + wp_register_style( 'wp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/css/select2.min.css', array(), '3.5.2' );
390 + wp_register_script( 'wp-stream-timeago', $this->plugin->locations['url'] . 'ui/lib/timeago/jquery.timeago.js', array(), '1.4.1', true );
346 391
347 392 $locale = strtolower( substr( get_locale(), 0, 2 ) );
348 393 $file_tmpl = 'ui/lib/timeago/locales/jquery.timeago.%s.js';
349 394
350 395 if ( file_exists( $this->plugin->locations['dir'] . sprintf( $file_tmpl, $locale ) ) ) {
351 - wp_register_script( 'timeago-locale', $this->plugin->locations['url'] . sprintf( $file_tmpl, $locale ), array( 'timeago' ), '1' );
396 + wp_register_script( 'wp-stream-timeago-locale', $this->plugin->locations['url'] . sprintf( $file_tmpl, $locale ), array( 'wp-stream-timeago' ), '1' );
352 397 } else {
353 - wp_register_script( 'timeago-locale', $this->plugin->locations['url'] . sprintf( $file_tmpl, 'en' ), array( 'timeago' ), '1' );
398 + wp_register_script( 'wp-stream-timeago-locale', $this->plugin->locations['url'] . sprintf( $file_tmpl, 'en' ), array( 'wp-stream-timeago' ), '1' );
354 399 }
355 400
356 - wp_enqueue_style( 'wp-stream-admin', $this->plugin->locations['url'] . 'ui/css/admin.css', array(), $this->plugin->get_version() );
401 + $min = wp_stream_min_suffix();
402 + wp_enqueue_style( 'wp-stream-admin', $this->plugin->locations['url'] . 'ui/css/admin.' . $min . 'css', array(), $this->plugin->get_version() );
357 403
358 - $script_screens = array( 'plugins.php', 'user-edit.php', 'user-new.php', 'profile.php' );
404 + $script_screens = array( 'plugins.php' );
359 405
360 - if ( in_array( $hook, $this->screen_id ) || in_array( $hook, $script_screens ) ) {
361 - wp_enqueue_script( 'select2' );
362 - wp_enqueue_style( 'select2' );
406 + if ( in_array( $hook, $this->screen_id, true ) || in_array( $hook, $script_screens, true ) ) {
407 + wp_enqueue_script( 'wp-stream-select2' );
408 + wp_enqueue_style( 'wp-stream-select2' );
363 409
364 - wp_enqueue_script( 'timeago' );
365 - wp_enqueue_script( 'timeago-locale' );
410 + wp_enqueue_script( 'wp-stream-timeago' );
411 + wp_enqueue_script( 'wp-stream-timeago-locale' );
366 412
367 - wp_enqueue_script( 'wp-stream-admin', $this->plugin->locations['url'] . 'ui/js/admin.js', array( 'jquery', 'select2' ), $this->plugin->get_version() );
368 - wp_enqueue_script( 'wp-stream-live-updates', $this->plugin->locations['url'] . 'ui/js/live-updates.js', array( 'jquery', 'heartbeat' ), $this->plugin->get_version() );
413 + wp_enqueue_script(
414 + 'wp-stream-admin',
415 + $this->plugin->locations['url'] . 'ui/js/admin.' . $min . 'js',
416 + array(
417 + 'jquery',
418 + 'wp-stream-select2',
419 + ),
420 + $this->plugin->get_version()
421 + );
422 + wp_enqueue_script(
423 + 'wp-stream-admin-exclude',
424 + $this->plugin->locations['url'] . 'ui/js/exclude.' . $min . 'js',
425 + array(
426 + 'jquery',
427 + 'wp-stream-select2',
428 + ),
429 + $this->plugin->get_version()
430 + );
431 + wp_enqueue_script(
432 + 'wp-stream-live-updates',
433 + $this->plugin->locations['url'] . 'ui/js/live-updates.' . $min . 'js',
434 + array(
435 + 'jquery',
436 + 'heartbeat',
437 + ),
438 + $this->plugin->get_version()
439 + );
369 440
370 441 wp_localize_script(
371 442 'wp-stream-admin',
372 443 'wp_stream',
@@ -385,42 +456,20 @@
385 456 'wp-stream-live-updates',
386 457 'wp_stream_live_updates',
387 458 array(
388 459 'current_screen' => $hook,
389 - 'current_page' => isset( $_GET['paged'] ) ? esc_js( $_GET['paged'] ) : '1', // input var okay
390 - 'current_order' => isset( $_GET['order'] ) ? esc_js( $_GET['order'] ) : 'desc', // input var okay
391 - 'current_query' => wp_stream_json_encode( $_GET ), // input var okay
392 - 'current_query_count' => count( $_GET ), // input var okay
460 + 'current_page' => isset( $_GET['paged'] ) ? esc_js( $_GET['paged'] ) : '1', // WPCS: CSRF ok.
461 + // input var okay, CSRF okay
462 + 'current_order' => isset( $_GET['order'] ) ? esc_js( $_GET['order'] ) : 'desc', // WPCS: CSRF ok.
463 + // input var okay, CSRF okay
464 + 'current_query' => wp_stream_json_encode( $_GET ), // WPCS: CSRF ok.
465 + // input var okay, CSRF okay
466 + 'current_query_count' => count( $_GET ), // WPCS: CSRF ok.
467 + // input var okay, CSRF okay
393 468 )
394 469 );
395 470 }
396 471
397 - if ( $this->migrate->show_migrate_notice() ) {
398 - $limit = absint( $this->migrate->limit );
399 - $record_count = absint( $this->migrate->record_count );
400 - $chunks = ceil( $record_count / $limit );
401 - $estimated_time = ( $chunks > 1 ) ? round( ( $chunks * 5 ) / 60 ) : 0;
402 - $migrate_time_message = ( $estimated_time > 1 ) ? sprintf( esc_html__( 'This will take about %d minutes.', 'stream' ), absint( $estimated_time ) ) : esc_html__( 'This could take a few minutes.', 'stream' );
403 -
404 - wp_enqueue_script( 'wp-stream-migrate', $this->plugin->locations['url'] . 'ui/js/migrate.js', array( 'jquery' ), $this->plugin->get_version() );
405 - wp_localize_script(
406 - 'wp-stream-migrate',
407 - 'wp_stream_migrate',
408 - array(
409 - 'i18n' => array(
410 - 'migrate_process_title' => esc_html__( 'Migrating Stream Records', 'stream' ),
411 - 'ignore_migrate_title' => esc_html__( 'No Records Were Migrated', 'stream' ),
412 - 'migrate_process_message' => esc_html__( 'Please do not exit this page until the process has completed.', 'stream' ) . ' ' . esc_html( $migrate_time_message ),
413 - 'confirm_start_migrate' => ( $estimated_time > 1 ) ? sprintf( esc_html__( 'Please note: This process will take about %d minutes to complete.', 'stream' ), absint( $estimated_time ) ) : esc_html__( 'Please note: This process could take a few minutes to complete.', 'stream' ),
414 - 'confirm_migrate_reminder' => esc_html__( 'Please note: Your existing records will not appear in Stream until you have migrated them to your local database.', 'stream' ),
415 - 'confirm_ignore_migrate' => sprintf( esc_html__( 'Are you sure you want to lose all %s existing Stream records without migrating?', 'stream' ), number_format( $record_count ), ( $estimated_time > 1 && is_multisite() ) ? sprintf( esc_html__( 'about %d', 'stream' ), absint( $estimated_time ) ) : esc_html__( 'a few', 'stream' ) ),
416 - ),
417 - 'chunks' => absint( $chunks ),
418 - 'nonce' => wp_create_nonce( 'wp_stream_migrate-' . absint( get_current_blog_id() ) . absint( get_current_user_id() ) ),
419 - )
420 - );
421 - }
422 -
423 472 /**
424 473 * The maximum number of items that can be updated in bulk without receiving a warning.
425 474 *
426 475 * Stream watches for bulk actions performed in the WordPress Admin (such as updating
@@ -433,15 +482,16 @@
433 482 * @return int
434 483 */
435 484 $bulk_actions_threshold = apply_filters( 'wp_stream_bulk_actions_threshold', 100 );
436 485
437 - wp_enqueue_script( 'wp-stream-global', $this->plugin->locations['url'] . 'ui/js/global.js', array( 'jquery' ), $this->plugin->get_version() );
486 + wp_enqueue_script( 'wp-stream-global', $this->plugin->locations['url'] . 'ui/js/global.' . $min . 'js', array( 'jquery' ), $this->plugin->get_version() );
438 487 wp_localize_script(
439 488 'wp-stream-global',
440 489 'wp_stream_global',
441 490 array(
442 - 'bulk_actions' => array(
443 - 'i18n' => array(
491 + 'bulk_actions' => array(
492 + 'i18n' => array(
493 + // translators: Placeholder refers to a number of items (e.g. "1,742")
444 494 '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 ) ) ),
445 495 ),
446 496 'threshold' => absint( $bulk_actions_threshold ),
447 497 ),
@@ -459,8 +509,13 @@
459 509 if ( is_admin() && false !== strpos( wp_stream_filter_input( INPUT_GET, 'page' ), $this->records_page_slug ) ) {
460 510 return true;
461 511 }
462 512
513 + $screen = get_current_screen();
514 + if ( is_admin() && Alerts::POST_TYPE === $screen->post_type ) {
515 + return true;
516 + }
517 +
463 518 return false;
464 519 }
465 520
466 521 /**
@@ -465,9 +520,9 @@
465 520
466 521 /**
467 522 * Add a specific body class to all Stream admin screens
468 523 *
469 - * @param string $classes
524 + * @param string $classes CSS classes to output to body
470 525 *
471 526 * @filter admin_body_class
472 527 *
473 528 * @return string
@@ -477,10 +532,10 @@
477 532
478 533 if ( $this->is_stream_screen() ) {
479 534 $stream_classes[] = $this->admin_body_class;
480 535
481 - if ( isset( $_GET['page'] ) ) {
482 - $stream_classes[] = sanitize_key( $_GET['page'] ); // input var okay
536 + if ( isset( $_GET['page'] ) ) { // CSRF okay
537 + $stream_classes[] = sanitize_key( $_GET['page'] ); // input var okay, CSRF okay
483 538 }
484 539 }
485 540
486 541 /**
@@ -501,9 +556,10 @@
501 556 *
502 557 * @action admin_enqueue_scripts
503 558 */
504 559 public function admin_menu_css() {
505 - wp_register_style( 'wp-stream-datepicker', $this->plugin->locations['url'] . 'ui/css/datepicker.css', array(), $this->plugin->get_version() );
560 + $min = wp_stream_min_suffix();
561 + wp_register_style( 'wp-stream-datepicker', $this->plugin->locations['url'] . 'ui/css/datepicker.' . $min . 'css', array(), $this->plugin->get_version() );
506 562 wp_register_style( 'wp-stream-icons', $this->plugin->locations['url'] . 'ui/stream-icons/style.css', array(), $this->plugin->get_version() );
507 563
508 564 // Make sure we're working off a clean version
509 565 if ( ! file_exists( ABSPATH . WPINC . '/version.php' ) ) {
@@ -508,9 +564,9 @@
508 564 // Make sure we're working off a clean version
509 565 if ( ! file_exists( ABSPATH . WPINC . '/version.php' ) ) {
510 566 return;
511 567 }
512 - include( ABSPATH . WPINC . '/version.php' );
568 + include ABSPATH . WPINC . '/version.php';
513 569
514 570 if ( ! isset( $wp_version ) ) {
515 571 return;
516 572 }
@@ -537,9 +593,9 @@
537 593 #adminmenu #menu-posts-feedback div.wp-menu-image {
538 594 background: none !important;
539 595 background-repeat: no-repeat;
540 596 }
541 - body.{$body_class} #wpbody-content .wrap h2:nth-child(1):before {
597 + body.{$body_class} #wpbody-content .wrap h1:nth-child(1):before {
542 598 font-family: 'WP Stream' !important;
543 599 content: '\\73';
544 600 padding: 0 8px 0 0;
545 601 }
@@ -569,10 +625,15 @@
569 625
570 626 \wp_add_inline_style( 'wp-admin', $css );
571 627 }
572 628
629 + /**
630 + * Handle the reset AJAX request to reset logs.
631 + *
632 + * @return bool
633 + */
573 634 public function wp_ajax_reset() {
574 - check_ajax_referer( 'stream_nonce', 'wp_stream_nonce' );
635 + check_ajax_referer( 'stream_nonce_reset', 'wp_stream_nonce_reset' );
575 636
576 637 if ( ! current_user_can( $this->settings_cap ) ) {
577 638 wp_die(
578 639 esc_html__( "You don't have sufficient privileges to do this action.", 'stream' )
@@ -602,9 +663,9 @@
602 663 global $wpdb;
603 664
604 665 $where = '';
605 666
606 - if ( is_multisite() && ! is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) {
667 + if ( is_multisite() && ! $this->plugin->is_network_activated() ) {
607 668 $where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() );
608 669 }
609 670
610 671 $wpdb->query(
@@ -611,9 +672,9 @@
611 672 "DELETE `stream`, `meta`
612 673 FROM {$wpdb->stream} AS `stream`
613 674 LEFT JOIN {$wpdb->streammeta} AS `meta`
614 675 ON `meta`.`record_id` = `stream`.`ID`
615 - WHERE 1=1 {$where};"
676 + WHERE 1=1 {$where};" // @codingStandardsIgnoreLine $where already prepared
616 677 );
617 678 }
618 679
619 680 public function purge_schedule_setup() {
@@ -630,25 +691,26 @@
630 691 is_multisite()
631 692 &&
632 693 is_network_admin()
633 694 &&
634 - ! is_plugin_active_for_network( $this->plugin->locations['plugin'] )
695 + ! $this->plugin->is_network_activated()
635 696 ) {
636 697 return;
637 698 }
638 699
639 - if ( is_multisite() && is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) {
700 + if ( is_multisite() && $this->plugin->is_network_activated() ) {
640 701 $options = (array) get_site_option( 'wp_stream_network', array() );
641 702 } else {
642 703 $options = (array) get_option( 'wp_stream', array() );
643 704 }
644 705
645 - if ( isset( $options['general_keep_records_indefinitely'] ) || ! isset( $options['general_records_ttl'] ) ) {
706 + if ( ! empty( $options['general_keep_records_indefinitely'] ) || ! isset( $options['general_records_ttl'] ) ) {
646 707 return;
647 708 }
648 709
649 - $days = $options['general_records_ttl'];
650 - $date = new DateTime( 'now', $timezone = new DateTimeZone( 'UTC' ) );
710 + $days = $options['general_records_ttl'];
711 + $timezone = new DateTimeZone( 'UTC' );
712 + $date = new DateTime( 'now', $timezone );
651 713
652 714 $date->sub( DateInterval::createFromDateString( "$days days" ) );
653 715
654 716 $where = $wpdb->prepare( ' AND `stream`.`created` < %s', $date->format( 'Y-m-d H:i:s' ) );
@@ -653,9 +715,9 @@
653 715
654 716 $where = $wpdb->prepare( ' AND `stream`.`created` < %s', $date->format( 'Y-m-d H:i:s' ) );
655 717
656 718 // Multisite but NOT network activated, only purge the current blog
657 - if ( is_multisite() && ! is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) {
719 + if ( is_multisite() && ! $this->plugin->is_network_activated() ) {
658 720 $where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() );
659 721 }
660 722
661 723 $wpdb->query(
@@ -662,14 +724,14 @@
662 724 "DELETE `stream`, `meta`
663 725 FROM {$wpdb->stream} AS `stream`
664 726 LEFT JOIN {$wpdb->streammeta} AS `meta`
665 727 ON `meta`.`record_id` = `stream`.`ID`
666 - WHERE 1=1 {$where};"
728 + WHERE 1=1 {$where};" // @codingStandardsIgnoreLine $where already prepared
667 729 );
668 730 }
669 731
670 732 /**
671 - * @param array $links
733 + * @param array $links
672 734 * @param string $file
673 735 *
674 736 * @filter plugin_action_links
675 737 *
@@ -680,29 +742,41 @@
680 742 return $links;
681 743 }
682 744
683 745 // Also don't show links in Network Admin if Stream isn't network enabled
684 - if ( is_network_admin() && is_multisite() && ! is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) {
746 + if ( is_network_admin() && is_multisite() && ! $this->plugin->is_network_activated() ) {
685 747 return $links;
686 748 }
687 749
688 750 if ( is_network_admin() ) {
689 - $admin_page_url = add_query_arg( array( 'page' => $this->network->network_settings_page_slug ), network_admin_url( $this->admin_parent_page ) );
751 + $admin_page_url = add_query_arg(
752 + array(
753 + 'page' => $this->network->network_settings_page_slug,
754 + ),
755 + network_admin_url( $this->admin_parent_page )
756 + );
690 757 } else {
691 - $admin_page_url = add_query_arg( array( 'page' => $this->settings_page_slug ), admin_url( $this->admin_parent_page ) );
758 + $admin_page_url = add_query_arg(
759 + array(
760 + 'page' => $this->settings_page_slug,
761 + ),
762 + admin_url( $this->admin_parent_page )
763 + );
692 764 }
693 765
694 766 $links[] = sprintf( '<a href="%s">%s</a>', esc_url( $admin_page_url ), esc_html__( 'Settings', 'default' ) );
695 767
696 - $url = add_query_arg(
697 - array(
698 - 'action' => 'wp_stream_uninstall',
699 - 'wp_stream_nonce' => wp_create_nonce( 'stream_nonce' ),
700 - ),
701 - admin_url( 'admin-ajax.php' )
702 - );
768 + if ( ! defined( 'DISALLOW_FILE_MODS' ) || false === DISALLOW_FILE_MODS ) {
769 + $url = add_query_arg(
770 + array(
771 + 'action' => 'wp_stream_uninstall',
772 + 'wp_stream_nonce' => wp_create_nonce( 'stream_nonce' ),
773 + ),
774 + admin_url( 'admin-ajax.php' )
775 + );
703 776
704 - $links[] = sprintf( '<span id="wp_stream_uninstall" class="delete"><a href="%s">%s</a></span>', esc_url( $url ), esc_html__( 'Uninstall', 'stream' ) );
777 + $links[] = sprintf( '<span id="wp_stream_uninstall" class="delete"><a href="%s">%s</a></span>', esc_url( $url ), esc_html__( 'Uninstall', 'stream' ) );
778 + }
705 779
706 780 return $links;
707 781 }
708 782
@@ -712,12 +786,12 @@
712 786 public function render_list_table() {
713 787 $this->list_table->prepare_items();
714 788 ?>
715 789 <div class="wrap">
716 - <h2><?php echo esc_html( get_admin_page_title() ) ?></h2>
717 - <?php $this->list_table->display() ?>
790 + <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
791 + <?php $this->list_table->display(); ?>
718 792 </div>
719 - <?php
793 + <?php
720 794 }
721 795
722 796 /**
723 797 * Render settings page
@@ -729,28 +803,28 @@
729 803 $page_description = apply_filters( 'wp_stream_settings_form_description', '' );
730 804
731 805 $sections = $this->plugin->settings->get_fields();
732 806 $active_tab = wp_stream_filter_input( INPUT_GET, 'tab' );
733 -
734 - wp_enqueue_script( 'wp-stream-settings', $this->plugin->locations['url'] . 'ui/js/settings.js', array( 'jquery' ), $this->plugin->get_version(), true );
807 + $min = wp_stream_min_suffix();
808 + wp_enqueue_script( 'wp-stream-settings', $this->plugin->locations['url'] . 'ui/js/settings.' . $min . 'js', array( 'jquery' ), $this->plugin->get_version(), true );
735 809 ?>
736 810 <div class="wrap">
737 - <h2><?php echo esc_html( get_admin_page_title() ) ?></h2>
811 + <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
738 812
739 813 <?php if ( ! empty( $page_description ) ) : ?>
740 - <p><?php echo esc_html( $page_description ) ?></p>
814 + <p><?php echo esc_html( $page_description ); ?></p>
741 815 <?php endif; ?>
742 816
743 - <?php settings_errors() ?>
817 + <?php settings_errors(); ?>
744 818
745 819 <?php if ( count( $sections ) > 1 ) : ?>
746 820 <h2 class="nav-tab-wrapper">
747 - <?php $i = 0 ?>
821 + <?php $i = 0; ?>
748 822 <?php foreach ( $sections as $section => $data ) : ?>
749 - <?php $i ++ ?>
750 - <?php $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section ) ?>
751 - <a href="<?php echo esc_url( add_query_arg( 'tab', $section ) ) ?>" class="nav-tab<?php if ( $is_active ) { echo esc_attr( ' nav-tab-active' ); } ?>">
752 - <?php echo esc_html( $data['title'] ) ?>
823 + <?php $i++; ?>
824 + <?php $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section ); ?>
825 + <a href="<?php echo esc_url( add_query_arg( 'tab', $section ) ); ?>" class="nav-tab <?php echo $is_active ? esc_attr( ' nav-tab-active' ) : ''; ?>">
826 + <?php echo esc_html( $data['title'] ); ?>
753 827 </a>
754 828 <?php endforeach; ?>
755 829 </h2>
756 830 <?php endif; ?>
@@ -755,29 +829,29 @@
755 829 </h2>
756 830 <?php endif; ?>
757 831
758 832 <div class="nav-tab-content" id="tab-content-settings">
759 - <form method="post" action="<?php echo esc_attr( $form_action ) ?>" enctype="multipart/form-data">
833 + <form method="post" action="<?php echo esc_attr( $form_action ); ?>" enctype="multipart/form-data">
760 834 <div class="settings-sections">
761 - <?php
762 - $i = 0;
763 - foreach ( $sections as $section => $data ) {
764 - $i++;
835 + <?php
836 + $i = 0;
837 + foreach ( $sections as $section => $data ) {
838 + $i++;
765 839
766 - $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section );
840 + $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section );
767 841
768 - if ( $is_active ) {
769 - settings_fields( $option_key );
770 - do_settings_sections( $option_key );
771 - }
772 - }
773 - ?>
842 + if ( $is_active ) {
843 + settings_fields( $option_key );
844 + do_settings_sections( $option_key );
845 + }
846 + }
847 + ?>
774 848 </div>
775 - <?php submit_button() ?>
849 + <?php submit_button(); ?>
776 850 </form>
777 851 </div>
778 852 </div>
779 - <?php
853 + <?php
780 854 }
781 855
782 856 /**
783 857 * Instantiate the list table
@@ -782,9 +856,14 @@
782 856 /**
783 857 * Instantiate the list table
784 858 */
785 859 public function register_list_table() {
786 - $this->list_table = new List_Table( $this->plugin, array( 'screen' => $this->screen_id['main'] ) );
860 + $this->list_table = new List_Table(
861 + $this->plugin,
862 + array(
863 + 'screen' => $this->screen_id['main'],
864 + )
865 + );
787 866 }
788 867
789 868 /**
790 869 * Check if a particular role has access
@@ -793,9 +872,9 @@
793 872 *
794 873 * @return bool
795 874 */
796 875 private function role_can_view( $role ) {
797 - if ( in_array( $role, $this->plugin->settings->options['general_role_access'] ) ) {
876 + if ( in_array( $role, $this->plugin->settings->options['general_role_access'], true ) ) {
798 877 return true;
799 878 }
800 879
801 880 return false;
@@ -834,9 +913,9 @@
834 913
835 914 $stream_view_caps = array( $this->view_cap );
836 915
837 916 foreach ( $caps as $cap ) {
838 - if ( in_array( $cap, $stream_view_caps ) ) {
917 + if ( in_array( $cap, $stream_view_caps, true ) ) {
839 918 foreach ( $roles as $role ) {
840 919 if ( $this->role_can_view( $role ) ) {
841 920 $allcaps[ $cap ] = true;
842 921
@@ -862,9 +941,9 @@
862 941 */
863 942 public function filter_role_caps( $allcaps, $cap, $role ) {
864 943 $stream_view_caps = array( $this->view_cap );
865 944
866 - if ( in_array( $cap, $stream_view_caps ) && $this->role_can_view( $role ) ) {
945 + if ( in_array( $cap, $stream_view_caps, true ) && $this->role_can_view( $role ) ) {
867 946 $allcaps[ $cap ] = true;
868 947 }
869 948
870 949 return $allcaps;
@@ -873,12 +952,22 @@
873 952 /**
874 953 * @action wp_ajax_wp_stream_filters
875 954 */
876 955 public function ajax_filters() {
956 + if ( ! defined( 'DOING_AJAX' ) || ! current_user_can( $this->plugin->admin->settings_cap ) ) {
957 + wp_die( '-1' );
958 + }
959 +
960 + check_ajax_referer( 'stream_filters_user_search_nonce', 'nonce' );
961 +
877 962 switch ( wp_stream_filter_input( INPUT_GET, 'filter' ) ) {
878 963 case 'user_id':
879 964 $users = array_merge(
880 - array( 0 => (object) array( 'display_name' => 'WP-CLI' ) ),
965 + array(
966 + 0 => (object) array(
967 + 'display_name' => 'WP-CLI',
968 + ),
969 + ),
881 970 get_users()
882 971 );
883 972
884 973 $search = wp_stream_filter_input( INPUT_GET, 'q' );
@@ -885,9 +974,9 @@
885 974 if ( $search ) {
886 975 // `search` arg for get_users() is not enough
887 976 $users = array_filter(
888 977 $users,
889 - function( $user ) use ( $search ) {
978 + function ( $user ) use ( $search ) {
890 979 return false !== mb_strpos( mb_strtolower( $user->display_name ), mb_strtolower( $search ) );
891 980 }
892 981 );
893 982 }
@@ -902,68 +991,26 @@
902 991 break;
903 992 }
904 993
905 994 if ( isset( $results ) ) {
906 - echo wp_stream_json_encode( array_values( $results ) ); // xss ok
995 + echo wp_stream_json_encode( $results ); // xss ok
907 996 }
908 997
909 - if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
910 - return;
911 - }
912 -
913 998 die();
914 999 }
915 1000
916 - /**
917 - * @action wp_ajax_wp_stream_get_filter_value_by_id
918 - */
919 - public function get_filter_value_by_id() {
920 - $filter = wp_stream_filter_input( INPUT_POST, 'filter' );
921 -
922 - switch ( $filter ) {
923 - case 'user_id':
924 - $id = wp_stream_filter_input( INPUT_POST, 'id' );
925 -
926 - if ( '0' === $id ) {
927 - $value = 'WP-CLI';
928 -
929 - break;
930 - }
931 -
932 - $user = get_userdata( $id );
933 -
934 - if ( ! $user || is_wp_error( $user ) ) {
935 - $value = '';
936 - } else {
937 - $value = $user->display_name;
938 - }
939 -
940 - break;
941 - default:
942 - $value = '';
943 - }
944 -
945 - echo wp_stream_json_encode( $value ); // xss ok
946 -
947 - if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
948 - return;
949 - }
950 -
951 - die();
952 - }
953 -
954 1001 public function get_users_record_meta( $authors ) {
955 1002 $authors_records = array();
956 1003
957 1004 foreach ( $authors as $user_id => $args ) {
958 - $author = new Author( $user_id );
1005 + $author = new Author( $args->ID );
959 1006
960 1007 $authors_records[ $user_id ] = array(
961 - 'text' => $author->get_display_name(),
962 - 'id' => $user_id,
963 - 'label' => $author->get_display_name(),
964 - 'icon' => $author->get_avatar_src( 32 ),
965 - 'title' => '',
1008 + 'text' => $author->get_display_name(),
1009 + 'id' => $author->id,
1010 + 'label' => $author->get_display_name(),
1011 + 'icon' => $author->get_avatar_src( 32 ),
1012 + 'title' => '',
966 1013 );
967 1014 }
968 1015
969 1016 return $authors_records;
@@ -971,18 +1018,19 @@
971 1018
972 1019 /**
973 1020 * Get user meta in a way that is also safe for VIP
974 1021 *
975 - * @param int $user_id
1022 + * @param int $user_id
976 1023 * @param string $meta_key
977 - * @param bool $single (optional)
1024 + * @param bool $single (optional)
978 1025 *
979 1026 * @return mixed
980 1027 */
981 - function get_user_meta( $user_id, $meta_key, $single = true ) {
1028 + public function get_user_meta( $user_id, $meta_key, $single = true ) {
982 1029 if ( wp_stream_is_vip() && function_exists( 'get_user_attribute' ) ) {
983 1030 return get_user_attribute( $user_id, $meta_key );
984 1031 }
1032 +
985 1033 return get_user_meta( $user_id, $meta_key, $single );
986 1034 }
987 1035
988 1036 /**
@@ -987,19 +1035,20 @@
987 1035
988 1036 /**
989 1037 * Update user meta in a way that is also safe for VIP
990 1038 *
991 - * @param int $user_id
1039 + * @param int $user_id
992 1040 * @param string $meta_key
993 1041 * @param mixed $meta_value
994 - * @param mixed $prev_value (optional)
1042 + * @param mixed $prev_value (optional)
995 1043 *
996 1044 * @return int|bool
997 1045 */
998 - function update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) {
1046 + public function update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) {
999 1047 if ( wp_stream_is_vip() && function_exists( 'update_user_attribute' ) ) {
1000 1048 return update_user_attribute( $user_id, $meta_key, $meta_value );
1001 1049 }
1050 +
1002 1051 return update_user_meta( $user_id, $meta_key, $meta_value, $prev_value );
1003 1052 }
1004 1053
1005 1054 /**
@@ -1004,17 +1053,18 @@
1004 1053
1005 1054 /**
1006 1055 * Delete user meta in a way that is also safe for VIP
1007 1056 *
1008 - * @param int $user_id
1057 + * @param int $user_id
1009 1058 * @param string $meta_key
1010 - * @param mixed $meta_value (optional)
1059 + * @param mixed $meta_value (optional)
1011 1060 *
1012 1061 * @return bool
1013 1062 */
1014 - function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) {
1063 + public function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) {
1015 1064 if ( wp_stream_is_vip() && function_exists( 'delete_user_attribute' ) ) {
1016 1065 return delete_user_attribute( $user_id, $meta_key, $meta_value );
1017 1066 }
1067 +
1018 1068 return delete_user_meta( $user_id, $meta_key, $meta_value );
1019 1069 }
1020 1070 }