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 +259 -207 3.0.03.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,10 +556,10 @@
501 556 *
502 557 * @action admin_enqueue_scripts
503 558 */
504 559 public function admin_menu_css() {
505 - wp_register_style( 'jquery-ui', '//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/jquery-ui.css', array(), '1.10.1' );
506 - wp_register_style( 'wp-stream-datepicker', $this->plugin->locations['url'] . 'ui/css/datepicker.css', array( 'jquery-ui' ), $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() );
507 562 wp_register_style( 'wp-stream-icons', $this->plugin->locations['url'] . 'ui/stream-icons/style.css', array(), $this->plugin->get_version() );
508 563
509 564 // Make sure we're working off a clean version
510 565 if ( ! file_exists( ABSPATH . WPINC . '/version.php' ) ) {
@@ -509,9 +564,9 @@
509 564 // Make sure we're working off a clean version
510 565 if ( ! file_exists( ABSPATH . WPINC . '/version.php' ) ) {
511 566 return;
512 567 }
513 - include( ABSPATH . WPINC . '/version.php' );
568 + include ABSPATH . WPINC . '/version.php';
514 569
515 570 if ( ! isset( $wp_version ) ) {
516 571 return;
517 572 }
@@ -538,9 +593,9 @@
538 593 #adminmenu #menu-posts-feedback div.wp-menu-image {
539 594 background: none !important;
540 595 background-repeat: no-repeat;
541 596 }
542 - body.{$body_class} #wpbody-content .wrap h2:nth-child(1):before {
597 + body.{$body_class} #wpbody-content .wrap h1:nth-child(1):before {
543 598 font-family: 'WP Stream' !important;
544 599 content: '\\73';
545 600 padding: 0 8px 0 0;
546 601 }
@@ -570,10 +625,15 @@
570 625
571 626 \wp_add_inline_style( 'wp-admin', $css );
572 627 }
573 628
629 + /**
630 + * Handle the reset AJAX request to reset logs.
631 + *
632 + * @return bool
633 + */
574 634 public function wp_ajax_reset() {
575 - check_ajax_referer( 'stream_nonce', 'wp_stream_nonce' );
635 + check_ajax_referer( 'stream_nonce_reset', 'wp_stream_nonce_reset' );
576 636
577 637 if ( ! current_user_can( $this->settings_cap ) ) {
578 638 wp_die(
579 639 esc_html__( "You don't have sufficient privileges to do this action.", 'stream' )
@@ -603,9 +663,9 @@
603 663 global $wpdb;
604 664
605 665 $where = '';
606 666
607 - if ( is_multisite() && ! is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) {
667 + if ( is_multisite() && ! $this->plugin->is_network_activated() ) {
608 668 $where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() );
609 669 }
610 670
611 671 $wpdb->query(
@@ -612,9 +672,9 @@
612 672 "DELETE `stream`, `meta`
613 673 FROM {$wpdb->stream} AS `stream`
614 674 LEFT JOIN {$wpdb->streammeta} AS `meta`
615 675 ON `meta`.`record_id` = `stream`.`ID`
616 - WHERE 1=1 {$where};"
676 + WHERE 1=1 {$where};" // @codingStandardsIgnoreLine $where already prepared
617 677 );
618 678 }
619 679
620 680 public function purge_schedule_setup() {
@@ -631,28 +691,33 @@
631 691 is_multisite()
632 692 &&
633 693 is_network_admin()
634 694 &&
635 - ! is_plugin_active_for_network( $this->plugin->locations['plugin'] )
695 + ! $this->plugin->is_network_activated()
636 696 ) {
637 697 return;
638 698 }
639 699
640 - if ( is_multisite() && is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) {
700 + if ( is_multisite() && $this->plugin->is_network_activated() ) {
641 701 $options = (array) get_site_option( 'wp_stream_network', array() );
642 702 } else {
643 703 $options = (array) get_option( 'wp_stream', array() );
644 704 }
645 705
646 - $days = $options['general_records_ttl'];
647 - $date = new DateTime( 'now', $timezone = new DateTimeZone( 'UTC' ) );
706 + if ( ! empty( $options['general_keep_records_indefinitely'] ) || ! isset( $options['general_records_ttl'] ) ) {
707 + return;
708 + }
648 709
710 + $days = $options['general_records_ttl'];
711 + $timezone = new DateTimeZone( 'UTC' );
712 + $date = new DateTime( 'now', $timezone );
713 +
649 714 $date->sub( DateInterval::createFromDateString( "$days days" ) );
650 715
651 716 $where = $wpdb->prepare( ' AND `stream`.`created` < %s', $date->format( 'Y-m-d H:i:s' ) );
652 717
653 718 // Multisite but NOT network activated, only purge the current blog
654 - if ( is_multisite() && ! is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) {
719 + if ( is_multisite() && ! $this->plugin->is_network_activated() ) {
655 720 $where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() );
656 721 }
657 722
658 723 $wpdb->query(
@@ -659,14 +724,14 @@
659 724 "DELETE `stream`, `meta`
660 725 FROM {$wpdb->stream} AS `stream`
661 726 LEFT JOIN {$wpdb->streammeta} AS `meta`
662 727 ON `meta`.`record_id` = `stream`.`ID`
663 - WHERE 1=1 {$where};"
728 + WHERE 1=1 {$where};" // @codingStandardsIgnoreLine $where already prepared
664 729 );
665 730 }
666 731
667 732 /**
668 - * @param array $links
733 + * @param array $links
669 734 * @param string $file
670 735 *
671 736 * @filter plugin_action_links
672 737 *
@@ -677,29 +742,41 @@
677 742 return $links;
678 743 }
679 744
680 745 // Also don't show links in Network Admin if Stream isn't network enabled
681 - 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() ) {
682 747 return $links;
683 748 }
684 749
685 750 if ( is_network_admin() ) {
686 - $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 + );
687 757 } else {
688 - $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 + );
689 764 }
690 765
691 766 $links[] = sprintf( '<a href="%s">%s</a>', esc_url( $admin_page_url ), esc_html__( 'Settings', 'default' ) );
692 767
693 - $url = add_query_arg(
694 - array(
695 - 'action' => 'wp_stream_uninstall',
696 - 'wp_stream_nonce' => wp_create_nonce( 'stream_nonce' ),
697 - ),
698 - admin_url( 'admin-ajax.php' )
699 - );
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 + );
700 776
701 - $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 + }
702 779
703 780 return $links;
704 781 }
705 782
@@ -709,12 +786,12 @@
709 786 public function render_list_table() {
710 787 $this->list_table->prepare_items();
711 788 ?>
712 789 <div class="wrap">
713 - <h2><?php echo esc_html( get_admin_page_title() ) ?></h2>
714 - <?php $this->list_table->display() ?>
790 + <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
791 + <?php $this->list_table->display(); ?>
715 792 </div>
716 - <?php
793 + <?php
717 794 }
718 795
719 796 /**
720 797 * Render settings page
@@ -726,28 +803,28 @@
726 803 $page_description = apply_filters( 'wp_stream_settings_form_description', '' );
727 804
728 805 $sections = $this->plugin->settings->get_fields();
729 806 $active_tab = wp_stream_filter_input( INPUT_GET, 'tab' );
730 -
731 - 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 );
732 809 ?>
733 810 <div class="wrap">
734 - <h2><?php echo esc_html( get_admin_page_title() ) ?></h2>
811 + <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
735 812
736 813 <?php if ( ! empty( $page_description ) ) : ?>
737 - <p><?php echo esc_html( $page_description ) ?></p>
814 + <p><?php echo esc_html( $page_description ); ?></p>
738 815 <?php endif; ?>
739 816
740 - <?php settings_errors() ?>
817 + <?php settings_errors(); ?>
741 818
742 819 <?php if ( count( $sections ) > 1 ) : ?>
743 820 <h2 class="nav-tab-wrapper">
744 - <?php $i = 0 ?>
821 + <?php $i = 0; ?>
745 822 <?php foreach ( $sections as $section => $data ) : ?>
746 - <?php $i ++ ?>
747 - <?php $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section ) ?>
748 - <a href="<?php echo esc_url( add_query_arg( 'tab', $section ) ) ?>" class="nav-tab<?php if ( $is_active ) { echo esc_attr( ' nav-tab-active' ); } ?>">
749 - <?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'] ); ?>
750 827 </a>
751 828 <?php endforeach; ?>
752 829 </h2>
753 830 <?php endif; ?>
@@ -752,29 +829,29 @@
752 829 </h2>
753 830 <?php endif; ?>
754 831
755 832 <div class="nav-tab-content" id="tab-content-settings">
756 - <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">
757 834 <div class="settings-sections">
758 - <?php
759 - $i = 0;
760 - foreach ( $sections as $section => $data ) {
761 - $i++;
835 + <?php
836 + $i = 0;
837 + foreach ( $sections as $section => $data ) {
838 + $i++;
762 839
763 - $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section );
840 + $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section );
764 841
765 - if ( $is_active ) {
766 - settings_fields( $option_key );
767 - do_settings_sections( $option_key );
768 - }
769 - }
770 - ?>
842 + if ( $is_active ) {
843 + settings_fields( $option_key );
844 + do_settings_sections( $option_key );
845 + }
846 + }
847 + ?>
771 848 </div>
772 - <?php submit_button() ?>
849 + <?php submit_button(); ?>
773 850 </form>
774 851 </div>
775 852 </div>
776 - <?php
853 + <?php
777 854 }
778 855
779 856 /**
780 857 * Instantiate the list table
@@ -779,9 +856,14 @@
779 856 /**
780 857 * Instantiate the list table
781 858 */
782 859 public function register_list_table() {
783 - $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 + );
784 866 }
785 867
786 868 /**
787 869 * Check if a particular role has access
@@ -790,9 +872,9 @@
790 872 *
791 873 * @return bool
792 874 */
793 875 private function role_can_view( $role ) {
794 - if ( in_array( $role, $this->plugin->settings->options['general_role_access'] ) ) {
876 + if ( in_array( $role, $this->plugin->settings->options['general_role_access'], true ) ) {
795 877 return true;
796 878 }
797 879
798 880 return false;
@@ -831,9 +913,9 @@
831 913
832 914 $stream_view_caps = array( $this->view_cap );
833 915
834 916 foreach ( $caps as $cap ) {
835 - if ( in_array( $cap, $stream_view_caps ) ) {
917 + if ( in_array( $cap, $stream_view_caps, true ) ) {
836 918 foreach ( $roles as $role ) {
837 919 if ( $this->role_can_view( $role ) ) {
838 920 $allcaps[ $cap ] = true;
839 921
@@ -859,9 +941,9 @@
859 941 */
860 942 public function filter_role_caps( $allcaps, $cap, $role ) {
861 943 $stream_view_caps = array( $this->view_cap );
862 944
863 - 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 ) ) {
864 946 $allcaps[ $cap ] = true;
865 947 }
866 948
867 949 return $allcaps;
@@ -870,12 +952,22 @@
870 952 /**
871 953 * @action wp_ajax_wp_stream_filters
872 954 */
873 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 +
874 962 switch ( wp_stream_filter_input( INPUT_GET, 'filter' ) ) {
875 963 case 'user_id':
876 964 $users = array_merge(
877 - array( 0 => (object) array( 'display_name' => 'WP-CLI' ) ),
965 + array(
966 + 0 => (object) array(
967 + 'display_name' => 'WP-CLI',
968 + ),
969 + ),
878 970 get_users()
879 971 );
880 972
881 973 $search = wp_stream_filter_input( INPUT_GET, 'q' );
@@ -899,68 +991,26 @@
899 991 break;
900 992 }
901 993
902 994 if ( isset( $results ) ) {
903 - echo wp_stream_json_encode( array_values( $results ) ); // xss ok
995 + echo wp_stream_json_encode( $results ); // xss ok
904 996 }
905 997
906 - if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
907 - return;
908 - }
909 -
910 998 die();
911 999 }
912 1000
913 - /**
914 - * @action wp_ajax_wp_stream_get_filter_value_by_id
915 - */
916 - public function get_filter_value_by_id() {
917 - $filter = wp_stream_filter_input( INPUT_POST, 'filter' );
918 -
919 - switch ( $filter ) {
920 - case 'user_id':
921 - $id = wp_stream_filter_input( INPUT_POST, 'id' );
922 -
923 - if ( '0' === $id ) {
924 - $value = 'WP-CLI';
925 -
926 - break;
927 - }
928 -
929 - $user = get_userdata( $id );
930 -
931 - if ( ! $user || is_wp_error( $user ) ) {
932 - $value = '';
933 - } else {
934 - $value = $user->display_name;
935 - }
936 -
937 - break;
938 - default:
939 - $value = '';
940 - }
941 -
942 - echo wp_stream_json_encode( $value ); // xss ok
943 -
944 - if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
945 - return;
946 - }
947 -
948 - die();
949 - }
950 -
951 1001 public function get_users_record_meta( $authors ) {
952 1002 $authors_records = array();
953 1003
954 1004 foreach ( $authors as $user_id => $args ) {
955 - $author = new Author( $user_id );
1005 + $author = new Author( $args->ID );
956 1006
957 1007 $authors_records[ $user_id ] = array(
958 - 'text' => $author->get_display_name(),
959 - 'id' => $user_id,
960 - 'label' => $author->get_display_name(),
961 - 'icon' => $author->get_avatar_src( 32 ),
962 - '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' => '',
963 1013 );
964 1014 }
965 1015
966 1016 return $authors_records;
@@ -968,18 +1018,19 @@
968 1018
969 1019 /**
970 1020 * Get user meta in a way that is also safe for VIP
971 1021 *
972 - * @param int $user_id
1022 + * @param int $user_id
973 1023 * @param string $meta_key
974 - * @param bool $single (optional)
1024 + * @param bool $single (optional)
975 1025 *
976 1026 * @return mixed
977 1027 */
978 - function get_user_meta( $user_id, $meta_key, $single = true ) {
1028 + public function get_user_meta( $user_id, $meta_key, $single = true ) {
979 1029 if ( wp_stream_is_vip() && function_exists( 'get_user_attribute' ) ) {
980 1030 return get_user_attribute( $user_id, $meta_key );
981 1031 }
1032 +
982 1033 return get_user_meta( $user_id, $meta_key, $single );
983 1034 }
984 1035
985 1036 /**
@@ -984,19 +1035,20 @@
984 1035
985 1036 /**
986 1037 * Update user meta in a way that is also safe for VIP
987 1038 *
988 - * @param int $user_id
1039 + * @param int $user_id
989 1040 * @param string $meta_key
990 1041 * @param mixed $meta_value
991 - * @param mixed $prev_value (optional)
1042 + * @param mixed $prev_value (optional)
992 1043 *
993 1044 * @return int|bool
994 1045 */
995 - 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 = '' ) {
996 1047 if ( wp_stream_is_vip() && function_exists( 'update_user_attribute' ) ) {
997 1048 return update_user_attribute( $user_id, $meta_key, $meta_value );
998 1049 }
1050 +
999 1051 return update_user_meta( $user_id, $meta_key, $meta_value, $prev_value );
1000 1052 }
1001 1053
1002 1054 /**
@@ -1001,18 +1053,18 @@
1001 1053
1002 1054 /**
1003 1055 * Delete user meta in a way that is also safe for VIP
1004 1056 *
1005 - * @param int $user_id
1057 + * @param int $user_id
1006 1058 * @param string $meta_key
1007 - * @param mixed $meta_value (optional)
1059 + * @param mixed $meta_value (optional)
1008 1060 *
1009 1061 * @return bool
1010 1062 */
1011 - function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) {
1063 + public function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) {
1012 1064 if ( wp_stream_is_vip() && function_exists( 'delete_user_attribute' ) ) {
1013 1065 return delete_user_attribute( $user_id, $meta_key, $meta_value );
1014 1066 }
1067 +
1015 1068 return delete_user_meta( $user_id, $meta_key, $meta_value );
1016 1069 }
1017 -
1018 1070 }