| @@ -1,5 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Renders and manages the plugin Settings page. | |
| 4 | + * | |
| 5 | + * @package WP_Stream | |
| 6 | + */ | |
| 2 | 7 | |
| 3 | 8 | namespace WP_Stream; |
| 4 | 9 | |
| 5 | 10 | use \WP_Roles; |
| @@ -5,12 +10,16 @@ | ||
| 5 | 10 | use \WP_Roles; |
| 6 | 11 | use \WP_User; |
| 7 | 12 | use \WP_User_Query; |
| 8 | 13 | |
| 14 | +/** | |
| 15 | + * Class - Settings | |
| 16 | + */ | |
| 9 | 17 | class Settings { |
| 10 | 18 | |
| 11 | 19 | /** |
| 12 | - * Hold Plugin class | |
| 20 | + * Holds instance of plugin object | |
| 21 | + * | |
| 13 | 22 | * @var Plugin |
| 14 | 23 | */ |
| 15 | 24 | public $plugin; |
| 16 | 25 | |
| @@ -44,9 +53,9 @@ | ||
| 44 | 53 | |
| 45 | 54 | /** |
| 46 | 55 | * Class constructor. |
| 47 | 56 | * |
| 48 | - * @param Plugin $plugin The main Plugin class. | |
| 57 | + * @param Plugin $plugin Instance of plugin object. | |
| 49 | 58 | */ |
| 50 | 59 | public function __construct( $plugin ) { |
| 51 | 60 | $this->plugin = $plugin; |
| 52 | 61 | |
| @@ -52,31 +61,35 @@ | ||
| 52 | 61 | |
| 53 | 62 | $this->option_key = $this->get_option_key(); |
| 54 | 63 | $this->options = $this->get_options(); |
| 55 | 64 | |
| 56 | - // Register settings, and fields | |
| 65 | + // Register settings, and fields. | |
| 57 | 66 | add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 58 | 67 | |
| 59 | - // Remove records when records TTL is shortened | |
| 68 | + // Remove records when records TTL is shortened. | |
| 60 | 69 | add_action( |
| 61 | - 'update_option_' . $this->option_key, array( | |
| 70 | + 'update_option_' . $this->option_key, | |
| 71 | + array( | |
| 62 | 72 | $this, |
| 63 | 73 | 'updated_option_ttl_remove_records', |
| 64 | - ), 10, 2 | |
| 74 | + ), | |
| 75 | + 10, | |
| 76 | + 2 | |
| 65 | 77 | ); |
| 66 | 78 | |
| 67 | - // Apply label translations for settings | |
| 79 | + // Apply label translations for settings. | |
| 68 | 80 | add_filter( |
| 69 | - 'wp_stream_serialized_labels', array( | |
| 81 | + 'wp_stream_serialized_labels', | |
| 82 | + array( | |
| 70 | 83 | $this, |
| 71 | 84 | 'get_settings_translations', |
| 72 | 85 | ) |
| 73 | 86 | ); |
| 74 | 87 | |
| 75 | - // Ajax callback function to search users | |
| 88 | + // Ajax callback function to search users. | |
| 76 | 89 | add_action( 'wp_ajax_stream_get_users', array( $this, 'get_users' ) ); |
| 77 | 90 | |
| 78 | - // Ajax callback function to search IPs | |
| 91 | + // Ajax callback function to search IPs. | |
| 79 | 92 | add_action( 'wp_ajax_stream_get_ips', array( $this, 'get_ips' ) ); |
| 80 | 93 | } |
| 81 | 94 | |
| 82 | 95 | /** |
| @@ -107,12 +120,15 @@ | ||
| 107 | 120 | 'find' => $search, |
| 108 | 121 | ); |
| 109 | 122 | |
| 110 | 123 | add_filter( |
| 111 | - 'user_search_columns', array( | |
| 124 | + 'user_search_columns', | |
| 125 | + array( | |
| 112 | 126 | $this, |
| 113 | 127 | 'add_display_name_search_columns', |
| 114 | - ), 10, 3 | |
| 128 | + ), | |
| 129 | + 10, | |
| 130 | + 3 | |
| 115 | 131 | ); |
| 116 | 132 | |
| 117 | 133 | $users = new WP_User_Query( |
| 118 | 134 | array( |
| @@ -128,12 +144,14 @@ | ||
| 128 | 144 | ) |
| 129 | 145 | ); |
| 130 | 146 | |
| 131 | 147 | remove_filter( |
| 132 | - 'user_search_columns', array( | |
| 148 | + 'user_search_columns', | |
| 149 | + array( | |
| 133 | 150 | $this, |
| 134 | 151 | 'add_display_name_search_columns', |
| 135 | - ), 10 | |
| 152 | + ), | |
| 153 | + 10 | |
| 136 | 154 | ); |
| 137 | 155 | |
| 138 | 156 | if ( 0 === $users->get_total() ) { |
| 139 | 157 | wp_send_json_error( $response ); |
| @@ -154,9 +172,9 @@ | ||
| 154 | 172 | $response->users = array(); |
| 155 | 173 | $users_added_to_response = array(); |
| 156 | 174 | |
| 157 | 175 | foreach ( $users_array as $key => $user ) { |
| 158 | - // exclude duplications: | |
| 176 | + // exclude duplications. | |
| 159 | 177 | if ( array_key_exists( $user->ID, $users_added_to_response ) ) { |
| 160 | 178 | continue; |
| 161 | 179 | } else { |
| 162 | 180 | $users_added_to_response[ $user->ID ] = true; |
| @@ -170,9 +188,9 @@ | ||
| 170 | 188 | ); |
| 171 | 189 | |
| 172 | 190 | $args['tooltip'] = esc_attr( |
| 173 | 191 | sprintf( |
| 174 | - // translators: Placeholders refers to a user ID, a username, an email address, and a user role (e.g. "42", "administrator", "foo@bar.com", "subscriber"). | |
| 192 | + /* translators: %1$d: user ID, %2$s: username, %3$s: email, %4$s: user role (e.g. "42", "administrator", "foo@bar.com", "subscriber") */ | |
| 175 | 193 | __( 'ID: %1$d\nUser: %2$s\nEmail: %3$s\nRole: %4$s', 'stream' ), |
| 176 | 194 | $author->id, |
| 177 | 195 | $author->user_login, |
| 178 | 196 | $author->user_email, |
| @@ -356,9 +374,9 @@ | ||
| 356 | 374 | ), |
| 357 | 375 | ), |
| 358 | 376 | ); |
| 359 | 377 | |
| 360 | - // If Akismet is active, allow Admins to opt-in to Akismet tracking | |
| 378 | + // If Akismet is active, allow Admins to opt-in to Akismet tracking. | |
| 361 | 379 | if ( class_exists( 'Akismet' ) ) { |
| 362 | 380 | $akismet_tracking = array( |
| 363 | 381 | 'name' => 'akismet_tracking', |
| 364 | 382 | 'title' => esc_html__( 'Akismet Tracking', 'stream' ), |
| @@ -370,21 +388,18 @@ | ||
| 370 | 388 | |
| 371 | 389 | array_push( $fields['advanced']['fields'], $akismet_tracking ); |
| 372 | 390 | } |
| 373 | 391 | |
| 374 | - // If WP Cron is enabled, allow Admins to opt-in to WP Cron tracking | |
| 375 | - if ( wp_stream_is_cron_enabled() ) { | |
| 376 | - $wp_cron_tracking = array( | |
| 377 | - 'name' => 'wp_cron_tracking', | |
| 378 | - 'title' => esc_html__( 'WP Cron Tracking', 'stream' ), | |
| 379 | - 'type' => 'checkbox', | |
| 380 | - 'desc' => esc_html__( 'By default, Stream does not track activity performed by WordPress cron events unless you opt-in here. Enabling this is not necessary or recommended for most sites.', 'stream' ), | |
| 381 | - 'after_field' => esc_html__( 'Enabled', 'stream' ), | |
| 382 | - 'default' => 0, | |
| 383 | - ); | |
| 392 | + $wp_cron_tracking = array( | |
| 393 | + 'name' => 'wp_cron_tracking', | |
| 394 | + 'title' => esc_html__( 'WP Cron Tracking', 'stream' ), | |
| 395 | + 'type' => 'checkbox', | |
| 396 | + 'desc' => esc_html__( 'By default, Stream does not track activity performed by WordPress cron events unless you opt-in here. Enabling this is not necessary or recommended for most sites.', 'stream' ), | |
| 397 | + 'after_field' => esc_html__( 'Enabled', 'stream' ), | |
| 398 | + 'default' => 0, | |
| 399 | + ); | |
| 384 | 400 | |
| 385 | - array_push( $fields['advanced']['fields'], $wp_cron_tracking ); | |
| 386 | - } | |
| 401 | + array_push( $fields['advanced']['fields'], $wp_cron_tracking ); | |
| 387 | 402 | |
| 388 | 403 | /** |
| 389 | 404 | * Filter allows for modification of options fields |
| 390 | 405 | * |
| @@ -391,9 +406,9 @@ | ||
| 391 | 406 | * @return array Array of option fields |
| 392 | 407 | */ |
| 393 | 408 | $this->fields = apply_filters( 'wp_stream_settings_option_fields', $fields ); |
| 394 | 409 | |
| 395 | - // Sort option fields in each tab by title ASC | |
| 410 | + // Sort option fields in each tab by title ASC. | |
| 396 | 411 | foreach ( $this->fields as $tab => $options ) { |
| 397 | 412 | $titles = array(); |
| 398 | 413 | |
| 399 | 414 | foreach ( $options['fields'] as $field ) { |
| @@ -464,9 +479,11 @@ | ||
| 464 | 479 | public function register_settings() { |
| 465 | 480 | $sections = $this->get_fields(); |
| 466 | 481 | |
| 467 | 482 | register_setting( |
| 468 | - $this->option_key, $this->option_key, array( | |
| 483 | + $this->option_key, | |
| 484 | + $this->option_key, | |
| 485 | + array( | |
| 469 | 486 | $this, |
| 470 | 487 | 'sanitize_settings', |
| 471 | 488 | ) |
| 472 | 489 | ); |
| @@ -479,9 +496,10 @@ | ||
| 479 | 496 | $this->option_key |
| 480 | 497 | ); |
| 481 | 498 | |
| 482 | 499 | foreach ( $section['fields'] as $field_idx => $field ) { |
| 483 | - if ( ! isset( $field['type'] ) ) { // No field type associated, skip, no GUI | |
| 500 | + // No field type associated, skip, no GUI. | |
| 501 | + if ( ! isset( $field['type'] ) ) { | |
| 484 | 502 | continue; |
| 485 | 503 | } |
| 486 | 504 | |
| 487 | 505 | add_settings_field( |
| @@ -495,9 +513,9 @@ | ||
| 495 | 513 | $section_name, |
| 496 | 514 | $field + array( |
| 497 | 515 | 'section' => $section_name, |
| 498 | 516 | 'label_for' => sprintf( '%s_%s_%s', $this->option_key, $section_name, $field['name'] ), |
| 499 | - // xss ok | |
| 517 | + // xss ok. | |
| 500 | 518 | ) |
| 501 | 519 | ); |
| 502 | 520 | } |
| 503 | 521 | } |
| @@ -505,9 +523,9 @@ | ||
| 505 | 523 | |
| 506 | 524 | /** |
| 507 | 525 | * Sanitization callback for settings field values before save |
| 508 | 526 | * |
| 509 | - * @param array $input | |
| 527 | + * @param array $input Raw input. | |
| 510 | 528 | * |
| 511 | 529 | * @return array |
| 512 | 530 | */ |
| 513 | 531 | public function sanitize_settings( $input ) { |
| @@ -540,14 +558,15 @@ | ||
| 540 | 558 | $output[ $name ] = $input[ $name ]; |
| 541 | 559 | |
| 542 | 560 | // Support all values in multidimentional arrays too. |
| 543 | 561 | array_walk_recursive( |
| 544 | - $output[ $name ], function ( &$v, $k ) { | |
| 545 | - $v = trim( $v ); | |
| 562 | + $output[ $name ], | |
| 563 | + function ( &$v ) { | |
| 564 | + $v = sanitize_text_field( trim( $v ) ); | |
| 546 | 565 | } |
| 547 | 566 | ); |
| 548 | 567 | } else { |
| 549 | - $output[ $name ] = trim( $input[ $name ] ); | |
| 568 | + $output[ $name ] = sanitize_text_field( trim( $input[ $name ] ) ); | |
| 550 | 569 | } |
| 551 | 570 | } |
| 552 | 571 | } |
| 553 | 572 | } |
| @@ -557,9 +576,9 @@ | ||
| 557 | 576 | |
| 558 | 577 | /** |
| 559 | 578 | * Compile HTML needed for displaying the field |
| 560 | 579 | * |
| 561 | - * @param array $field Field settings | |
| 580 | + * @param array $field Field settings. | |
| 562 | 581 | * |
| 563 | 582 | * @return string HTML to be displayed |
| 564 | 583 | */ |
| 565 | 584 | public function render_field( $field ) { |
| @@ -786,9 +805,9 @@ | ||
| 786 | 805 | esc_attr( $name ), |
| 787 | 806 | esc_attr( wp_stream_json_encode( $data_values ) ), |
| 788 | 807 | esc_attr( $current_value ), |
| 789 | 808 | esc_attr( $class ), |
| 790 | - // translators: Placeholder refers to the title of the dropdown menu (e.g. "users") | |
| 809 | + /* translators: %s: the title of the dropdown menu (e.g. "users") */ | |
| 791 | 810 | sprintf( esc_html__( 'Any %s', 'stream' ), $title ) |
| 792 | 811 | ); |
| 793 | 812 | |
| 794 | 813 | $output = sprintf( |
| @@ -831,10 +850,15 @@ | ||
| 831 | 850 | ); |
| 832 | 851 | |
| 833 | 852 | $exclude_rows = array(); |
| 834 | 853 | |
| 854 | + // Account for when no rules have been added yet. | |
| 855 | + if ( ! is_array( $current_value ) ) { | |
| 856 | + $current_value = array(); | |
| 857 | + } | |
| 858 | + | |
| 835 | 859 | // Prepend an empty row. |
| 836 | - $current_value['exclude_row'] = array( 'helper' => '' ) + ( isset( $current_value['exclude_row'] ) ? $current_value['exclude_row'] : array() ); | |
| 860 | + $current_value['exclude_row'] = ( isset( $current_value['exclude_row'] ) ? $current_value['exclude_row'] : array() ) + array( 'helper' => '' ); | |
| 837 | 861 | |
| 838 | 862 | foreach ( $current_value['exclude_row'] as $key => $value ) { |
| 839 | 863 | // Prepare values. |
| 840 | 864 | $author_or_role = isset( $current_value['author_or_role'][ $key ] ) ? $current_value['author_or_role'][ $key ] : ''; |
| @@ -842,9 +866,9 @@ | ||
| 842 | 866 | $context = isset( $current_value['context'][ $key ] ) ? $current_value['context'][ $key ] : ''; |
| 843 | 867 | $action = isset( $current_value['action'][ $key ] ) ? $current_value['action'][ $key ] : ''; |
| 844 | 868 | $ip_address = isset( $current_value['ip_address'][ $key ] ) ? $current_value['ip_address'][ $key ] : ''; |
| 845 | 869 | |
| 846 | - // Author or Role dropdown menu | |
| 870 | + // Author or Role dropdown menu. | |
| 847 | 871 | $author_or_role_values = array(); |
| 848 | 872 | $author_or_role_selected = array(); |
| 849 | 873 | |
| 850 | 874 | foreach ( $this->get_roles() as $role_id => $role ) { |
| @@ -854,9 +878,9 @@ | ||
| 854 | 878 | ); |
| 855 | 879 | $count = isset( $users['avail_roles'][ $role_id ] ) ? $users['avail_roles'][ $role_id ] : 0; |
| 856 | 880 | |
| 857 | 881 | if ( ! empty( $count ) ) { |
| 858 | - // translators: Placeholder refers to a number of users (e.g. "42") | |
| 882 | + /* translators: %d: a number of users (e.g. "42") */ | |
| 859 | 883 | $args['user_count'] = sprintf( _n( '%d user', '%d users', absint( $count ), 'stream' ), absint( $count ) ); |
| 860 | 884 | } |
| 861 | 885 | |
| 862 | 886 | if ( $role_id === $author_or_role ) { |
| @@ -877,9 +901,10 @@ | ||
| 877 | 901 | $author_or_role_values[] = $author_or_role_selected; |
| 878 | 902 | } |
| 879 | 903 | |
| 880 | 904 | $author_or_role_input = $form->render_field( |
| 881 | - 'select2', array( | |
| 905 | + 'select2', | |
| 906 | + array( | |
| 882 | 907 | 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]', $option_key, $section, $name, 'author_or_role' ) ), |
| 883 | 908 | 'options' => $author_or_role_values, |
| 884 | 909 | 'classes' => 'author_or_role', |
| 885 | 910 | 'data' => array( |
| @@ -890,9 +915,9 @@ | ||
| 890 | 915 | ), |
| 891 | 916 | ) |
| 892 | 917 | ); |
| 893 | 918 | |
| 894 | - // Context dropdown menu | |
| 919 | + // Context dropdown menu. | |
| 895 | 920 | $context_values = array(); |
| 896 | 921 | |
| 897 | 922 | foreach ( $this->get_terms_labels( 'context' ) as $context_id => $context_data ) { |
| 898 | 923 | if ( is_array( $context_data ) ) { |
| @@ -922,9 +947,10 @@ | ||
| 922 | 947 | } |
| 923 | 948 | } |
| 924 | 949 | |
| 925 | 950 | $connector_or_context_input = $form->render_field( |
| 926 | - 'select2', array( | |
| 951 | + 'select2', | |
| 952 | + array( | |
| 927 | 953 | 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]', $option_key, $section, $name, 'connector_or_context' ) ), |
| 928 | 954 | 'options' => $context_values, |
| 929 | 955 | 'classes' => 'connector_or_context', |
| 930 | 956 | 'data' => array( |
| @@ -934,9 +960,10 @@ | ||
| 934 | 960 | ) |
| 935 | 961 | ); |
| 936 | 962 | |
| 937 | 963 | $connector_input = $form->render_field( |
| 938 | - 'hidden', array( | |
| 964 | + 'hidden', | |
| 965 | + array( | |
| 939 | 966 | 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]', $option_key, $section, $name, 'connector' ) ), |
| 940 | 967 | 'value' => $connector, |
| 941 | 968 | 'classes' => 'connector', |
| 942 | 969 | ) |
| @@ -942,9 +969,10 @@ | ||
| 942 | 969 | ) |
| 943 | 970 | ); |
| 944 | 971 | |
| 945 | 972 | $context_input = $form->render_field( |
| 946 | - 'hidden', array( | |
| 973 | + 'hidden', | |
| 974 | + array( | |
| 947 | 975 | 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]', $option_key, $section, $name, 'context' ) ), |
| 948 | 976 | 'value' => $context, |
| 949 | 977 | 'classes' => 'context', |
| 950 | 978 | ) |
| @@ -949,9 +977,9 @@ | ||
| 949 | 977 | 'classes' => 'context', |
| 950 | 978 | ) |
| 951 | 979 | ); |
| 952 | 980 | |
| 953 | - // Action dropdown menu | |
| 981 | + // Action dropdown menu. | |
| 954 | 982 | $action_values = array(); |
| 955 | 983 | |
| 956 | 984 | foreach ( $this->get_terms_labels( 'action' ) as $action_id => $action_data ) { |
| 957 | 985 | $action_values[] = array( |
| @@ -960,9 +988,10 @@ | ||
| 960 | 988 | ); |
| 961 | 989 | } |
| 962 | 990 | |
| 963 | 991 | $action_input = $form->render_field( |
| 964 | - 'select2', array( | |
| 992 | + 'select2', | |
| 993 | + array( | |
| 965 | 994 | 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]', $option_key, $section, $name, 'action' ) ), |
| 966 | 995 | 'value' => $action, |
| 967 | 996 | 'options' => $action_values, |
| 968 | 997 | 'classes' => 'action', |
| @@ -971,11 +1000,12 @@ | ||
| 971 | 1000 | ), |
| 972 | 1001 | ) |
| 973 | 1002 | ); |
| 974 | 1003 | |
| 975 | - // IP Address input | |
| 1004 | + // IP Address input. | |
| 976 | 1005 | $ip_address_input = $form->render_field( |
| 977 | - 'select2', array( | |
| 1006 | + 'select2', | |
| 1007 | + array( | |
| 978 | 1008 | 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]', $option_key, $section, $name, 'ip_address' ) ), |
| 979 | 1009 | 'value' => $ip_address, |
| 980 | 1010 | 'classes' => 'ip_address', |
| 981 | 1011 | 'data' => array( |
| @@ -985,9 +1015,9 @@ | ||
| 985 | 1015 | 'multiple' => true, |
| 986 | 1016 | ) |
| 987 | 1017 | ); |
| 988 | 1018 | |
| 989 | - // Hidden helper input | |
| 1019 | + // Hidden helper input. | |
| 990 | 1020 | $helper_input = sprintf( |
| 991 | 1021 | '<input type="hidden" name="%1$s[%2$s_%3$s][%4$s][]" value="" />', |
| 992 | 1022 | esc_attr( $option_key ), |
| 993 | 1023 | esc_attr( $section ), |
| @@ -1040,9 +1070,9 @@ | ||
| 1040 | 1070 | |
| 1041 | 1071 | /** |
| 1042 | 1072 | * Render Callback for post_types field |
| 1043 | 1073 | * |
| 1044 | - * @param array $field | |
| 1074 | + * @param array $field Field to be rendered. | |
| 1045 | 1075 | * |
| 1046 | 1076 | * @return string |
| 1047 | 1077 | */ |
| 1048 | 1078 | public function output_field( $field ) { |
| @@ -1053,9 +1083,9 @@ | ||
| 1053 | 1083 | } |
| 1054 | 1084 | |
| 1055 | 1085 | $output = $this->render_field( $field ); |
| 1056 | 1086 | |
| 1057 | - echo $output; // xss ok | |
| 1087 | + echo $output; // xss ok. | |
| 1058 | 1088 | } |
| 1059 | 1089 | |
| 1060 | 1090 | /** |
| 1061 | 1091 | * Get an array of user roles |
| @@ -1075,9 +1105,9 @@ | ||
| 1075 | 1105 | |
| 1076 | 1106 | /** |
| 1077 | 1107 | * Function will return all terms labels of given column |
| 1078 | 1108 | * |
| 1079 | - * @param string $column string Name of the column | |
| 1109 | + * @param string $column Name of the column. | |
| 1080 | 1110 | * |
| 1081 | 1111 | * @return array |
| 1082 | 1112 | */ |
| 1083 | 1113 | public function get_terms_labels( $column ) { |
| @@ -1110,10 +1140,10 @@ | ||
| 1110 | 1140 | * Remove records when records TTL is shortened |
| 1111 | 1141 | * |
| 1112 | 1142 | * @action update_option_wp_stream |
| 1113 | 1143 | * |
| 1114 | - * @param array $old_value | |
| 1115 | - * @param array $new_value | |
| 1144 | + * @param array $old_value Old value. | |
| 1145 | + * @param array $new_value New value. | |
| 1116 | 1146 | */ |
| 1117 | 1147 | public function updated_option_ttl_remove_records( $old_value, $new_value ) { |
| 1118 | 1148 | $ttl_before = isset( $old_value['general_records_ttl'] ) ? (int) $old_value['general_records_ttl'] : - 1; |
| 1119 | 1149 | $ttl_after = isset( $new_value['general_records_ttl'] ) ? (int) $new_value['general_records_ttl'] : - 1; |
| @@ -1129,8 +1159,10 @@ | ||
| 1129 | 1159 | /** |
| 1130 | 1160 | * Get translations of serialized Stream settings |
| 1131 | 1161 | * |
| 1132 | 1162 | * @filter wp_stream_serialized_labels |
| 1163 | + * | |
| 1164 | + * @param array $labels Setting labels. | |
| 1133 | 1165 | * |
| 1134 | 1166 | * @return array Multidimensional array of fields |
| 1135 | 1167 | */ |
| 1136 | 1168 | public function get_settings_translations( $labels ) { |