PluginProbe
Stream – Activity Log & Audit Trail / 4.0.2
Stream – Activity Log & Audit Trail v4.0.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 +297 -188 3.0.54.0.2 View file →
@@ -1,16 +1,26 @@
1 1 <?php
2 +/**
3 + * Centralized manager for WordPress backend functionality.
4 + *
5 + * @package WP_Stream
6 + */
7 +
2 8 namespace WP_Stream;
3 9
4 10 use DateTime;
5 11 use DateTimeZone;
6 12 use DateInterval;
7 -use \WP_CLI;
8 -use \WP_Roles;
13 +use WP_CLI;
14 +use WP_Roles;
9 15
16 +/**
17 + * Class - Admin
18 + */
10 19 class Admin {
20 +
11 21 /**
12 - * Hold Plugin class
22 + * Holds Instance of plugin object
13 23 *
14 24 * @var Plugin
15 25 */
16 26 public $plugin;
@@ -115,9 +125,9 @@
115 125
116 126 /**
117 127 * Class constructor.
118 128 *
119 - * @param Plugin $plugin The main Plugin class.
129 + * @param Plugin $plugin Instance of plugin object.
120 130 */
121 131 public function __construct( $plugin ) {
122 132 $this->plugin = $plugin;
123 133
@@ -124,9 +134,9 @@
124 134 add_action( 'init', array( $this, 'init' ) );
125 135
126 136 // Ensure function used in various methods is pre-loaded.
127 137 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
128 - require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
138 + require_once ABSPATH . '/wp-admin/includes/plugin.php';
129 139 }
130 140
131 141 // User and role caps.
132 142 add_filter( 'user_has_cap', array( $this, 'filter_user_caps' ), 10, 4 );
@@ -131,9 +141,9 @@
131 141 // User and role caps.
132 142 add_filter( 'user_has_cap', array( $this, 'filter_user_caps' ), 10, 4 );
133 143 add_filter( 'role_has_cap', array( $this, 'filter_role_caps' ), 10, 3 );
134 144
135 - if ( is_multisite() && is_plugin_active_for_network( $this->plugin->locations['plugin'] ) && ! is_network_admin() ) {
145 + if ( is_multisite() && $plugin->is_network_activated() && ! is_network_admin() ) {
136 146 $options = (array) get_site_option( 'wp_stream_network', array() );
137 147 $option = isset( $options['general_site_access'] ) ? absint( $options['general_site_access'] ) : 1;
138 148
139 149 $this->disable_access = ( $option ) ? false : true;
@@ -151,36 +161,55 @@
151 161 // Add admin body class.
152 162 add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
153 163
154 164 // Plugin action links.
155 - add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2 );
165 + add_filter(
166 + 'plugin_action_links',
167 + array(
168 + $this,
169 + 'plugin_action_links',
170 + ),
171 + 10,
172 + 2
173 + );
156 174
157 175 // Load admin scripts and styles.
158 - add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
176 + add_action(
177 + 'admin_enqueue_scripts',
178 + array(
179 + $this,
180 + 'admin_enqueue_scripts',
181 + )
182 + );
159 183 add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) );
160 184
161 185 // Reset Streams database.
162 - add_action( 'wp_ajax_wp_stream_reset', array( $this, 'wp_ajax_reset' ) );
186 + add_action(
187 + 'wp_ajax_wp_stream_reset',
188 + array(
189 + $this,
190 + 'wp_ajax_reset',
191 + )
192 + );
163 193
164 - // Uninstall Streams and Deactivate plugin.
165 - $uninstall = new Uninstall( $this->plugin );
166 - add_action( 'wp_ajax_wp_stream_uninstall', array( $uninstall, 'uninstall' ) );
167 -
168 194 // Auto purge setup.
169 195 add_action( 'wp_loaded', array( $this, 'purge_schedule_setup' ) );
170 - add_action( 'wp_stream_auto_purge', array( $this, 'purge_scheduled_action' ) );
196 + add_action(
197 + 'wp_stream_auto_purge',
198 + array(
199 + $this,
200 + 'purge_scheduled_action',
201 + )
202 + );
171 203
172 204 // Ajax users list.
173 - add_action( 'wp_ajax_wp_stream_filters', array( $this, 'ajax_filters' ) );
174 -
175 - // Ajax user's name by ID.
176 - add_action( 'wp_ajax_wp_stream_get_filter_value_by_id', array( $this, 'get_filter_value_by_id' ) );
177 -
178 - // Ajax users list.
179 - add_action( 'wp_ajax_wp_stream_filters', array( $this, 'ajax_filters' ) );
180 -
181 - // Ajax user's name by ID.
182 - add_action( 'wp_ajax_wp_stream_get_filter_value_by_id', array( $this, 'get_filter_value_by_id' ) );
205 + add_action(
206 + 'wp_ajax_wp_stream_filters',
207 + array(
208 + $this,
209 + 'ajax_filters',
210 + )
211 + );
183 212 }
184 213
185 214 /**
186 215 * Load admin classes
@@ -190,8 +219,14 @@
190 219 public function init() {
191 220 $this->network = new Network( $this->plugin );
192 221 $this->live_update = new Live_Update( $this->plugin );
193 222 $this->export = new Export( $this->plugin );
223 +
224 + // Check if the host has configured the `REMOTE_ADDR` correctly.
225 + $client_ip = $this->plugin->get_client_ip_address();
226 + if ( empty( $client_ip ) && $this->is_stream_screen() ) {
227 + $this->notice( __( 'Stream plugin can\'t determine a reliable client IP address! Please update the hosting environment to set the $_SERVER[\'REMOTE_ADDR\'] variable or use the wp_stream_client_ip_address filter to specify the verified client IP address!', 'stream' ) );
228 + }
194 229 }
195 230
196 231 /**
197 232 * Output specific updates passed as URL parameters.
@@ -217,9 +252,9 @@
217 252 * @param bool $is_error If the message is error_level (true) or warning (false).
218 253 */
219 254 public function notice( $message, $is_error = true ) {
220 255 if ( defined( 'WP_CLI' ) && WP_CLI ) {
221 - $message = strip_tags( $message );
256 + $message = wp_strip_all_tags( $message );
222 257
223 258 if ( $is_error ) {
224 259 WP_CLI::warning( $message );
225 260 } else {
@@ -308,8 +343,16 @@
308 343 $main_menu_position
309 344 );
310 345
311 346 /**
347 + * Fires before submenu items are added to the Stream menu
348 + * allowing plugins to add menu items before Settings
349 + *
350 + * @return void
351 + */
352 + do_action( 'wp_stream_admin_menu' );
353 +
354 + /**
312 355 * Filter the Settings admin page title
313 356 *
314 357 * @return string
315 358 */
@@ -332,9 +375,15 @@
332 375 */
333 376 do_action( 'wp_stream_admin_menu_screens' );
334 377
335 378 // Register the list table early, so it associates the column headers with 'Screen settings'.
336 - add_action( 'load-' . $this->screen_id['main'], array( $this, 'register_list_table' ) );
379 + add_action(
380 + 'load-' . $this->screen_id['main'],
381 + array(
382 + $this,
383 + 'register_list_table',
384 + )
385 + );
337 386 }
338 387 }
339 388
340 389 /**
@@ -341,15 +390,15 @@
341 390 * Enqueue scripts/styles for admin screen
342 391 *
343 392 * @action admin_enqueue_scripts
344 393 *
345 - * @param string $hook
394 + * @param string $hook Current hook.
346 395 *
347 396 * @return void
348 397 */
349 398 public function admin_enqueue_scripts( $hook ) {
350 - wp_register_script( 'wp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/select2.js', array( 'jquery' ), '3.5.2', true );
351 - wp_register_style( 'wp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/select2.css', array(), '3.5.2' );
399 + wp_register_script( 'wp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/js/select2.full.min.js', array( 'jquery' ), '4.0.13', true );
400 + wp_register_style( 'wp-stream-select2', $this->plugin->locations['url'] . 'ui/lib/select2/css/select2.min.css', array(), '4.0.13' );
352 401 wp_register_script( 'wp-stream-timeago', $this->plugin->locations['url'] . 'ui/lib/timeago/jquery.timeago.js', array(), '1.4.1', true );
353 402
354 403 $locale = strtolower( substr( get_locale(), 0, 2 ) );
355 404 $file_tmpl = 'ui/lib/timeago/locales/jquery.timeago.%s.js';
@@ -354,14 +403,27 @@
354 403 $locale = strtolower( substr( get_locale(), 0, 2 ) );
355 404 $file_tmpl = 'ui/lib/timeago/locales/jquery.timeago.%s.js';
356 405
357 406 if ( file_exists( $this->plugin->locations['dir'] . sprintf( $file_tmpl, $locale ) ) ) {
358 - wp_register_script( 'wp-stream-timeago-locale', $this->plugin->locations['url'] . sprintf( $file_tmpl, $locale ), array( 'wp-stream-timeago' ), '1' );
407 + wp_register_script(
408 + 'wp-stream-timeago-locale',
409 + $this->plugin->locations['url'] . sprintf( $file_tmpl, $locale ),
410 + array( 'wp-stream-timeago' ),
411 + '1',
412 + false
413 + );
359 414 } else {
360 - wp_register_script( 'wp-stream-timeago-locale', $this->plugin->locations['url'] . sprintf( $file_tmpl, 'en' ), array( 'wp-stream-timeago' ), '1' );
415 + wp_register_script(
416 + 'wp-stream-timeago-locale',
417 + $this->plugin->locations['url'] . sprintf( $file_tmpl, 'en' ),
418 + array( 'wp-stream-timeago' ),
419 + '1',
420 + false
421 + );
361 422 }
362 423
363 - wp_enqueue_style( 'wp-stream-admin', $this->plugin->locations['url'] . 'ui/css/admin.css', array(), $this->plugin->get_version() );
424 + $min = wp_stream_min_suffix();
425 + wp_enqueue_style( 'wp-stream-admin', $this->plugin->locations['url'] . 'ui/css/admin.' . $min . 'css', array(), $this->plugin->get_version() );
364 426
365 427 $script_screens = array( 'plugins.php' );
366 428
367 429 if ( in_array( $hook, $this->screen_id, true ) || in_array( $hook, $script_screens, true ) ) {
@@ -370,10 +432,38 @@
370 432
371 433 wp_enqueue_script( 'wp-stream-timeago' );
372 434 wp_enqueue_script( 'wp-stream-timeago-locale' );
373 435
374 - wp_enqueue_script( 'wp-stream-admin', $this->plugin->locations['url'] . 'ui/js/admin.js', array( 'jquery', 'wp-stream-select2' ), $this->plugin->get_version() );
375 - wp_enqueue_script( 'wp-stream-live-updates', $this->plugin->locations['url'] . 'ui/js/live-updates.js', array( 'jquery', 'heartbeat' ), $this->plugin->get_version() );
436 + wp_enqueue_script(
437 + 'wp-stream-admin',
438 + $this->plugin->locations['url'] . 'ui/js/admin.' . $min . 'js',
439 + array(
440 + 'jquery',
441 + 'wp-stream-select2',
442 + ),
443 + $this->plugin->get_version(),
444 + false
445 + );
446 + wp_enqueue_script(
447 + 'wp-stream-admin-exclude',
448 + $this->plugin->locations['url'] . 'ui/js/exclude.' . $min . 'js',
449 + array(
450 + 'jquery',
451 + 'wp-stream-select2',
452 + ),
453 + $this->plugin->get_version(),
454 + false
455 + );
456 + wp_enqueue_script(
457 + 'wp-stream-live-updates',
458 + $this->plugin->locations['url'] . 'ui/js/live-updates.' . $min . 'js',
459 + array(
460 + 'jquery',
461 + 'heartbeat',
462 + ),
463 + $this->plugin->get_version(),
464 + false
465 + );
376 466
377 467 wp_localize_script(
378 468 'wp-stream-admin',
379 469 'wp_stream',
@@ -378,11 +468,10 @@
378 468 'wp-stream-admin',
379 469 'wp_stream',
380 470 array(
381 471 'i18n' => array(
382 - 'confirm_purge' => esc_html__( 'Are you sure you want to delete all Stream activity records from the database? This cannot be undone.', 'stream' ),
383 - 'confirm_defaults' => esc_html__( 'Are you sure you want to reset all site settings to default? This cannot be undone.', 'stream' ),
384 - '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' ),
472 + 'confirm_purge' => esc_html__( 'Are you sure you want to delete all Stream activity records from the database? This cannot be undone.', 'stream' ),
473 + 'confirm_defaults' => esc_html__( 'Are you sure you want to reset all site settings to default? This cannot be undone.', 'stream' ),
385 474 ),
386 475 'locale' => esc_js( $locale ),
387 476 'gmt_offset' => get_option( 'gmt_offset' ),
388 477 )
@@ -387,17 +476,21 @@
387 476 'gmt_offset' => get_option( 'gmt_offset' ),
388 477 )
389 478 );
390 479
480 + $order_types = array( 'asc', 'desc' );
481 +
391 482 wp_localize_script(
392 483 'wp-stream-live-updates',
393 484 'wp_stream_live_updates',
394 485 array(
395 486 'current_screen' => $hook,
396 - 'current_page' => isset( $_GET['paged'] ) ? esc_js( $_GET['paged'] ) : '1', // input var okay
397 - 'current_order' => isset( $_GET['order'] ) ? esc_js( $_GET['order'] ) : 'desc', // input var okay
398 - 'current_query' => wp_stream_json_encode( $_GET ), // input var okay
399 - 'current_query_count' => count( $_GET ), // input var okay
487 + 'current_page' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : '1', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
488 + 'current_order' => isset( $_GET['order'] ) && in_array( strtolower( $_GET['order'] ), $order_types, true ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
489 + ? esc_js( $_GET['order'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
490 + : 'desc',
491 + 'current_query' => wp_json_encode( $_GET ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
492 + 'current_query_count' => count( $_GET ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
400 493 )
401 494 );
402 495 }
403 496
@@ -414,15 +507,23 @@
414 507 * @return int
415 508 */
416 509 $bulk_actions_threshold = apply_filters( 'wp_stream_bulk_actions_threshold', 100 );
417 510
418 - wp_enqueue_script( 'wp-stream-global', $this->plugin->locations['url'] . 'ui/js/global.js', array( 'jquery' ), $this->plugin->get_version() );
511 + wp_enqueue_script(
512 + 'wp-stream-global',
513 + $this->plugin->locations['url'] . 'ui/js/global.' . $min . 'js',
514 + array( 'jquery' ),
515 + $this->plugin->get_version(),
516 + false
517 + );
518 +
419 519 wp_localize_script(
420 520 'wp-stream-global',
421 521 'wp_stream_global',
422 522 array(
423 - 'bulk_actions' => array(
424 - 'i18n' => array(
523 + 'bulk_actions' => array(
524 + 'i18n' => array(
525 + /* translators: %s: a number of items (e.g. "1,742") */
425 526 '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 ) ) ),
426 527 ),
427 528 'threshold' => absint( $bulk_actions_threshold ),
428 529 ),
@@ -436,12 +537,23 @@
436 537 *
437 538 * @return bool
438 539 */
439 540 public function is_stream_screen() {
440 - if ( is_admin() && false !== strpos( wp_stream_filter_input( INPUT_GET, 'page' ), $this->records_page_slug ) ) {
541 + if ( ! is_admin() ) {
542 + return false;
543 + }
544 +
545 + $page = wp_stream_filter_input( INPUT_GET, 'page' );
546 + if ( is_string( $page ) && false !== strpos( $page, $this->records_page_slug ) ) {
441 547 return true;
442 548 }
443 549
550 + if ( is_admin() && function_exists( 'get_current_screen' ) ) {
551 + $screen = get_current_screen();
552 +
553 + return ( Alerts::POST_TYPE === $screen->post_type );
554 + }
555 +
444 556 return false;
445 557 }
446 558
447 559 /**
@@ -446,9 +558,9 @@
446 558
447 559 /**
448 560 * Add a specific body class to all Stream admin screens
449 561 *
450 - * @param string $classes CSS classes to output to body
562 + * @param string $classes CSS classes to output to body.
451 563 *
452 564 * @filter admin_body_class
453 565 *
454 566 * @return string
@@ -458,10 +570,10 @@
458 570
459 571 if ( $this->is_stream_screen() ) {
460 572 $stream_classes[] = $this->admin_body_class;
461 573
462 - if ( isset( $_GET['page'] ) ) {
463 - $stream_classes[] = sanitize_key( $_GET['page'] ); // input var okay
574 + if ( isset( $_GET['page'] ) ) { // // phpcs:ignore WordPress.Security.NonceVerification.Recommended
575 + $stream_classes[] = sanitize_key( $_GET['page'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
464 576 }
465 577 }
466 578
467 579 /**
@@ -482,16 +594,17 @@
482 594 *
483 595 * @action admin_enqueue_scripts
484 596 */
485 597 public function admin_menu_css() {
486 - wp_register_style( 'wp-stream-datepicker', $this->plugin->locations['url'] . 'ui/css/datepicker.css', array(), $this->plugin->get_version() );
598 + $min = wp_stream_min_suffix();
599 + wp_register_style( 'wp-stream-datepicker', $this->plugin->locations['url'] . 'ui/css/datepicker.' . $min . 'css', array(), $this->plugin->get_version() );
487 600 wp_register_style( 'wp-stream-icons', $this->plugin->locations['url'] . 'ui/stream-icons/style.css', array(), $this->plugin->get_version() );
488 601
489 - // Make sure we're working off a clean version
602 + // Make sure we're working off a clean version.
490 603 if ( ! file_exists( ABSPATH . WPINC . '/version.php' ) ) {
491 604 return;
492 605 }
493 - include( ABSPATH . WPINC . '/version.php' );
606 + include ABSPATH . WPINC . '/version.php';
494 607
495 608 if ( ! isset( $wp_version ) ) {
496 609 return;
497 610 }
@@ -550,10 +663,15 @@
550 663
551 664 \wp_add_inline_style( 'wp-admin', $css );
552 665 }
553 666
667 + /**
668 + * Handle the reset AJAX request to reset logs.
669 + *
670 + * @return bool
671 + */
554 672 public function wp_ajax_reset() {
555 - check_ajax_referer( 'stream_nonce', 'wp_stream_nonce' );
673 + check_ajax_referer( 'stream_nonce_reset', 'wp_stream_nonce_reset' );
556 674
557 675 if ( ! current_user_can( $this->settings_cap ) ) {
558 676 wp_die(
559 677 esc_html__( "You don't have sufficient privileges to do this action.", 'stream' )
@@ -565,9 +683,9 @@
565 683 if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
566 684 return true;
567 685 }
568 686
569 - wp_redirect(
687 + wp_safe_redirect(
570 688 add_query_arg(
571 689 array(
572 690 'page' => is_network_admin() ? $this->network->network_settings_page_slug : $this->settings_page_slug,
573 691 'message' => 'data_erased',
@@ -578,14 +696,19 @@
578 696
579 697 exit;
580 698 }
581 699
700 + /**
701 + * Clears stream records from the database.
702 + *
703 + * @return void
704 + */
582 705 private function erase_stream_records() {
583 706 global $wpdb;
584 707
585 708 $where = '';
586 709
587 - if ( is_multisite() && ! is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) {
710 + if ( is_multisite() && ! $this->plugin->is_network_activated() ) {
588 711 $where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() );
589 712 }
590 713
591 714 $wpdb->query(
@@ -596,8 +719,13 @@
596 719 WHERE 1=1 {$where};" // @codingStandardsIgnoreLine $where already prepared
597 720 );
598 721 }
599 722
723 + /**
724 + * Schedules a purge of records.
725 + *
726 + * @return void
727 + */
600 728 public function purge_schedule_setup() {
601 729 if ( ! wp_next_scheduled( 'wp_stream_auto_purge' ) ) {
602 730 wp_schedule_event( time(), 'twicedaily', 'wp_stream_auto_purge' );
603 731 }
@@ -602,41 +730,48 @@
602 730 wp_schedule_event( time(), 'twicedaily', 'wp_stream_auto_purge' );
603 731 }
604 732 }
605 733
734 + /**
735 + * Executes a scheduled purge
736 + *
737 + * @return void
738 + */
606 739 public function purge_scheduled_action() {
607 740 global $wpdb;
608 741
609 - // Don't purge when in Network Admin unless Stream is network activated
742 + // Don't purge when in Network Admin unless Stream is network activated.
610 743 if (
611 744 is_multisite()
612 745 &&
613 746 is_network_admin()
614 747 &&
615 - ! is_plugin_active_for_network( $this->plugin->locations['plugin'] )
748 + ! $this->plugin->is_network_activated()
616 749 ) {
617 750 return;
618 751 }
619 752
620 - if ( is_multisite() && is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) {
621 - $options = (array) get_site_option( 'wp_stream_network', array() );
753 + $defaults = $this->plugin->settings->get_defaults();
754 + if ( is_multisite() && $this->plugin->is_network_activated() ) {
755 + $options = (array) get_site_option( 'wp_stream_network', $defaults );
622 756 } else {
623 - $options = (array) get_option( 'wp_stream', array() );
757 + $options = (array) get_option( 'wp_stream', $defaults );
624 758 }
625 759
626 - if ( isset( $options['general_keep_records_indefinitely'] ) || ! isset( $options['general_records_ttl'] ) ) {
760 + if ( ! empty( $options['general_keep_records_indefinitely'] ) || ! isset( $options['general_records_ttl'] ) ) {
627 761 return;
628 762 }
629 763
630 - $days = $options['general_records_ttl'];
631 - $date = new DateTime( 'now', $timezone = new DateTimeZone( 'UTC' ) );
764 + $days = $options['general_records_ttl'];
765 + $timezone = new DateTimeZone( 'UTC' );
766 + $date = new DateTime( 'now', $timezone );
632 767
633 768 $date->sub( DateInterval::createFromDateString( "$days days" ) );
634 769
635 770 $where = $wpdb->prepare( ' AND `stream`.`created` < %s', $date->format( 'Y-m-d H:i:s' ) );
636 771
637 - // Multisite but NOT network activated, only purge the current blog
638 - if ( is_multisite() && ! is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) {
772 + // Multisite but NOT network activated, only purge the current blog.
773 + if ( is_multisite() && ! $this->plugin->is_network_activated() ) {
639 774 $where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() );
640 775 }
641 776
642 777 $wpdb->query(
@@ -648,13 +783,15 @@
648 783 );
649 784 }
650 785
651 786 /**
652 - * @param array $links
653 - * @param string $file
787 + * Returns the admin action links.
654 788 *
655 789 * @filter plugin_action_links
656 790 *
791 + * @param array $links Action links.
792 + * @param string $file Plugin file.
793 + *
657 794 * @return array
658 795 */
659 796 public function plugin_action_links( $links, $file ) {
660 797 if ( plugin_basename( $this->plugin->locations['dir'] . 'stream.php' ) !== $file ) {
@@ -660,31 +797,31 @@
660 797 if ( plugin_basename( $this->plugin->locations['dir'] . 'stream.php' ) !== $file ) {
661 798 return $links;
662 799 }
663 800
664 - // Also don't show links in Network Admin if Stream isn't network enabled
665 - if ( is_network_admin() && is_multisite() && ! is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) {
801 + // Also don't show links in Network Admin if Stream isn't network enabled.
802 + if ( is_network_admin() && is_multisite() && ! $this->plugin->is_network_activated() ) {
666 803 return $links;
667 804 }
668 805
669 806 if ( is_network_admin() ) {
670 - $admin_page_url = add_query_arg( array( 'page' => $this->network->network_settings_page_slug ), network_admin_url( $this->admin_parent_page ) );
807 + $admin_page_url = add_query_arg(
808 + array(
809 + 'page' => $this->network->network_settings_page_slug,
810 + ),
811 + network_admin_url( $this->admin_parent_page )
812 + );
671 813 } else {
672 - $admin_page_url = add_query_arg( array( 'page' => $this->settings_page_slug ), admin_url( $this->admin_parent_page ) );
814 + $admin_page_url = add_query_arg(
815 + array(
816 + 'page' => $this->settings_page_slug,
817 + ),
818 + admin_url( $this->admin_parent_page )
819 + );
673 820 }
674 821
675 822 $links[] = sprintf( '<a href="%s">%s</a>', esc_url( $admin_page_url ), esc_html__( 'Settings', 'default' ) );
676 823
677 - $url = add_query_arg(
678 - array(
679 - 'action' => 'wp_stream_uninstall',
680 - 'wp_stream_nonce' => wp_create_nonce( 'stream_nonce' ),
681 - ),
682 - admin_url( 'admin-ajax.php' )
683 - );
684 -
685 - $links[] = sprintf( '<span id="wp_stream_uninstall" class="delete"><a href="%s">%s</a></span>', esc_url( $url ), esc_html__( 'Uninstall', 'stream' ) );
686 -
687 824 return $links;
688 825 }
689 826
690 827 /**
@@ -693,12 +830,12 @@
693 830 public function render_list_table() {
694 831 $this->list_table->prepare_items();
695 832 ?>
696 833 <div class="wrap">
697 - <h1><?php echo esc_html( get_admin_page_title() ) ?></h1>
698 - <?php $this->list_table->display() ?>
834 + <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
835 + <?php $this->list_table->display(); ?>
699 836 </div>
700 - <?php
837 + <?php
701 838 }
702 839
703 840 /**
704 841 * Render settings page
@@ -710,28 +847,28 @@
710 847 $page_description = apply_filters( 'wp_stream_settings_form_description', '' );
711 848
712 849 $sections = $this->plugin->settings->get_fields();
713 850 $active_tab = wp_stream_filter_input( INPUT_GET, 'tab' );
714 -
715 - wp_enqueue_script( 'wp-stream-settings', $this->plugin->locations['url'] . 'ui/js/settings.js', array( 'jquery' ), $this->plugin->get_version(), true );
851 + $min = wp_stream_min_suffix();
852 + wp_enqueue_script( 'wp-stream-settings', $this->plugin->locations['url'] . 'ui/js/settings.' . $min . 'js', array( 'jquery' ), $this->plugin->get_version(), true );
716 853 ?>
717 854 <div class="wrap">
718 - <h1><?php echo esc_html( get_admin_page_title() ) ?></h1>
855 + <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
719 856
720 857 <?php if ( ! empty( $page_description ) ) : ?>
721 - <p><?php echo esc_html( $page_description ) ?></p>
858 + <p><?php echo esc_html( $page_description ); ?></p>
722 859 <?php endif; ?>
723 860
724 - <?php settings_errors() ?>
861 + <?php settings_errors(); ?>
725 862
726 863 <?php if ( count( $sections ) > 1 ) : ?>
727 864 <h2 class="nav-tab-wrapper">
728 - <?php $i = 0 ?>
865 + <?php $i = 0; ?>
729 866 <?php foreach ( $sections as $section => $data ) : ?>
730 - <?php $i ++ ?>
731 - <?php $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section ) ?>
732 - <a href="<?php echo esc_url( add_query_arg( 'tab', $section ) ) ?>" class="nav-tab<?php if ( $is_active ) { echo esc_attr( ' nav-tab-active' ); } ?>">
733 - <?php echo esc_html( $data['title'] ) ?>
867 + <?php ++$i; ?>
868 + <?php $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section ); ?>
869 + <a href="<?php echo esc_url( add_query_arg( 'tab', $section ) ); ?>" class="nav-tab <?php echo $is_active ? esc_attr( ' nav-tab-active' ) : ''; ?>">
870 + <?php echo esc_html( $data['title'] ); ?>
734 871 </a>
735 872 <?php endforeach; ?>
736 873 </h2>
737 874 <?php endif; ?>
@@ -736,29 +873,29 @@
736 873 </h2>
737 874 <?php endif; ?>
738 875
739 876 <div class="nav-tab-content" id="tab-content-settings">
740 - <form method="post" action="<?php echo esc_attr( $form_action ) ?>" enctype="multipart/form-data">
877 + <form method="post" action="<?php echo esc_attr( $form_action ); ?>" enctype="multipart/form-data">
741 878 <div class="settings-sections">
742 - <?php
743 - $i = 0;
744 - foreach ( $sections as $section => $data ) {
745 - $i++;
879 + <?php
880 + $i = 0;
881 + foreach ( $sections as $section => $data ) {
882 + ++$i;
746 883
747 - $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section );
884 + $is_active = ( ( 1 === $i && ! $active_tab ) || $active_tab === $section );
748 885
749 - if ( $is_active ) {
750 - settings_fields( $option_key );
751 - do_settings_sections( $option_key );
752 - }
753 - }
754 - ?>
886 + if ( $is_active ) {
887 + settings_fields( $option_key );
888 + do_settings_sections( $option_key );
889 + }
890 + }
891 + ?>
755 892 </div>
756 - <?php submit_button() ?>
893 + <?php submit_button(); ?>
757 894 </form>
758 895 </div>
759 896 </div>
760 - <?php
897 + <?php
761 898 }
762 899
763 900 /**
764 901 * Instantiate the list table
@@ -763,15 +900,20 @@
763 900 /**
764 901 * Instantiate the list table
765 902 */
766 903 public function register_list_table() {
767 - $this->list_table = new List_Table( $this->plugin, array( 'screen' => $this->screen_id['main'] ) );
904 + $this->list_table = new List_Table(
905 + $this->plugin,
906 + array(
907 + 'screen' => $this->screen_id['main'],
908 + )
909 + );
768 910 }
769 911
770 912 /**
771 913 * Check if a particular role has access
772 914 *
773 - * @param string $role
915 + * @param string $role User role.
774 916 *
775 917 * @return bool
776 918 */
777 919 private function role_can_view( $role ) {
@@ -784,12 +926,12 @@
784 926
785 927 /**
786 928 * Filter user caps to dynamically grant our view cap based on allowed roles
787 929 *
788 - * @param $allcaps
789 - * @param $caps
790 - * @param $args
791 - * @param $user
930 + * @param array $allcaps All capabilities.
931 + * @param array $caps Required caps.
932 + * @param array $args Unused.
933 + * @param WP_User $user User.
792 934 *
793 935 * @filter user_has_cap
794 936 *
795 937 * @return array
@@ -834,11 +976,11 @@
834 976 * Filter role caps to dynamically grant our view cap based on allowed roles
835 977 *
836 978 * @filter role_has_cap
837 979 *
838 - * @param $allcaps
839 - * @param $cap
840 - * @param $role
980 + * @param array $allcaps All capabilities.
981 + * @param string $cap Require cap.
982 + * @param string $role User role.
841 983 *
842 984 * @return array
843 985 */
844 986 public function filter_role_caps( $allcaps, $cap, $role ) {
@@ -851,15 +993,27 @@
851 993 return $allcaps;
852 994 }
853 995
854 996 /**
997 + * Ajax callback for return a user list.
998 + *
855 999 * @action wp_ajax_wp_stream_filters
856 1000 */
857 1001 public function ajax_filters() {
1002 + if ( ! defined( 'DOING_AJAX' ) || ! current_user_can( $this->plugin->admin->settings_cap ) ) {
1003 + wp_die( '-1' );
1004 + }
1005 +
1006 + check_ajax_referer( 'stream_filters_user_search_nonce', 'nonce' );
1007 +
858 1008 switch ( wp_stream_filter_input( INPUT_GET, 'filter' ) ) {
859 1009 case 'user_id':
860 1010 $users = array_merge(
861 - array( 0 => (object) array( 'display_name' => 'WP-CLI' ) ),
1011 + array(
1012 + 0 => (object) array(
1013 + 'display_name' => 'WP-CLI',
1014 + ),
1015 + ),
862 1016 get_users()
863 1017 );
864 1018
865 1019 $search = wp_stream_filter_input( INPUT_GET, 'q' );
@@ -866,9 +1020,9 @@
866 1020 if ( $search ) {
867 1021 // `search` arg for get_users() is not enough
868 1022 $users = array_filter(
869 1023 $users,
870 - function( $user ) use ( $search ) {
1024 + function ( $user ) use ( $search ) {
871 1025 return false !== mb_strpos( mb_strtolower( $user->display_name ), mb_strtolower( $search ) );
872 1026 }
873 1027 );
874 1028 }
@@ -876,9 +1030,9 @@
876 1030 if ( count( $users ) > $this->preload_users_max ) {
877 1031 $users = array_slice( $users, 0, $this->preload_users_max );
878 1032 }
879 1033
880 - // Get gravatar / roles for final result set
1034 + // Get gravatar / roles for final result set.
881 1035 $results = $this->get_users_record_meta( $users );
882 1036
883 1037 break;
884 1038 }
@@ -883,68 +1037,32 @@
883 1037 break;
884 1038 }
885 1039
886 1040 if ( isset( $results ) ) {
887 - echo wp_stream_json_encode( array_values( $results ) ); // xss ok
1041 + echo wp_json_encode( $results );
888 1042 }
889 1043
890 - if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
891 - return;
892 - }
893 -
894 1044 die();
895 1045 }
896 1046
897 1047 /**
898 - * @action wp_ajax_wp_stream_get_filter_value_by_id
1048 + * Return relevant user meta data.
1049 + *
1050 + * @param array $authors Author data.
1051 + * @return array
899 1052 */
900 - public function get_filter_value_by_id() {
901 - $filter = wp_stream_filter_input( INPUT_POST, 'filter' );
902 -
903 - switch ( $filter ) {
904 - case 'user_id':
905 - $id = wp_stream_filter_input( INPUT_POST, 'id' );
906 -
907 - if ( '0' === $id ) {
908 - $value = 'WP-CLI';
909 -
910 - break;
911 - }
912 -
913 - $user = get_userdata( $id );
914 -
915 - if ( ! $user || is_wp_error( $user ) ) {
916 - $value = '';
917 - } else {
918 - $value = $user->display_name;
919 - }
920 -
921 - break;
922 - default:
923 - $value = '';
924 - }
925 -
926 - echo wp_stream_json_encode( $value ); // xss ok
927 -
928 - if ( defined( 'WP_STREAM_TESTS' ) && WP_STREAM_TESTS ) {
929 - return;
930 - }
931 -
932 - die();
933 - }
934 -
935 1053 public function get_users_record_meta( $authors ) {
936 1054 $authors_records = array();
937 1055
938 1056 foreach ( $authors as $user_id => $args ) {
939 - $author = new Author( $user_id );
1057 + $author = new Author( $args->ID );
940 1058
941 1059 $authors_records[ $user_id ] = array(
942 - 'text' => $author->get_display_name(),
943 - 'id' => $user_id,
944 - 'label' => $author->get_display_name(),
945 - 'icon' => $author->get_avatar_src( 32 ),
946 - 'title' => '',
1060 + 'text' => $author->get_display_name(),
1061 + 'id' => $author->id,
1062 + 'label' => $author->get_display_name(),
1063 + 'icon' => $author->get_avatar_src( 32 ),
1064 + 'title' => '',
947 1065 );
948 1066 }
949 1067
950 1068 return $authors_records;
@@ -952,18 +1070,15 @@
952 1070
953 1071 /**
954 1072 * Get user meta in a way that is also safe for VIP
955 1073 *
956 - * @param int $user_id
957 - * @param string $meta_key
958 - * @param bool $single (optional)
1074 + * @param int $user_id User ID.
1075 + * @param string $meta_key Meta key.
1076 + * @param bool $single Return first found meta value connected to the meta key (optional).
959 1077 *
960 1078 * @return mixed
961 1079 */
962 - function get_user_meta( $user_id, $meta_key, $single = true ) {
963 - if ( wp_stream_is_vip() && function_exists( 'get_user_attribute' ) ) {
964 - return get_user_attribute( $user_id, $meta_key );
965 - }
1080 + public function get_user_meta( $user_id, $meta_key, $single = true ) {
966 1081 return get_user_meta( $user_id, $meta_key, $single );
967 1082 }
968 1083
969 1084 /**
@@ -968,19 +1083,16 @@
968 1083
969 1084 /**
970 1085 * Update user meta in a way that is also safe for VIP
971 1086 *
972 - * @param int $user_id
973 - * @param string $meta_key
974 - * @param mixed $meta_value
975 - * @param mixed $prev_value (optional)
1087 + * @param int $user_id User ID.
1088 + * @param string $meta_key Meta key.
1089 + * @param mixed $meta_value Meta value.
1090 + * @param mixed $prev_value Previous meta value being overwritten (optional).
976 1091 *
977 1092 * @return int|bool
978 1093 */
979 - function update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) {
980 - if ( wp_stream_is_vip() && function_exists( 'update_user_attribute' ) ) {
981 - return update_user_attribute( $user_id, $meta_key, $meta_value );
982 - }
1094 + public function update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) {
983 1095 return update_user_meta( $user_id, $meta_key, $meta_value, $prev_value );
984 1096 }
985 1097
986 1098 /**
@@ -985,17 +1097,14 @@
985 1097
986 1098 /**
987 1099 * Delete user meta in a way that is also safe for VIP
988 1100 *
989 - * @param int $user_id
990 - * @param string $meta_key
991 - * @param mixed $meta_value (optional)
1101 + * @param int $user_id User ID.
1102 + * @param string $meta_key Meta key.
1103 + * @param mixed $meta_value Meta value (optional).
992 1104 *
993 1105 * @return bool
994 1106 */
995 - function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) {
996 - if ( wp_stream_is_vip() && function_exists( 'delete_user_attribute' ) ) {
997 - return delete_user_attribute( $user_id, $meta_key, $meta_value );
998 - }
1107 + public function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) {
999 1108 return delete_user_meta( $user_id, $meta_key, $meta_value );
1000 1109 }
1001 1110 }