| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace WP_Stream; |
| 3 | 4 | |
| 4 | 5 | use \WP_Roles; |
| 5 | 6 | use \WP_User; |
| @@ -5,10 +6,12 @@ | ||
| 5 | 6 | use \WP_User; |
| 6 | 7 | use \WP_User_Query; |
| 7 | 8 | |
| 8 | 9 | class Settings { |
| 10 | + | |
| 9 | 11 | /** |
| 10 | 12 | * Hold Plugin class |
| 13 | + * | |
| 11 | 14 | * @var Plugin |
| 12 | 15 | */ |
| 13 | 16 | public $plugin; |
| 14 | 17 | |
| @@ -54,12 +57,26 @@ | ||
| 54 | 57 | // Register settings, and fields |
| 55 | 58 | add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 56 | 59 | |
| 57 | 60 | // Remove records when records TTL is shortened |
| 58 | - add_action( 'update_option_' . $this->option_key, array( $this, 'updated_option_ttl_remove_records' ), 10, 2 ); | |
| 61 | + add_action( | |
| 62 | + 'update_option_' . $this->option_key, | |
| 63 | + array( | |
| 64 | + $this, | |
| 65 | + 'updated_option_ttl_remove_records', | |
| 66 | + ), | |
| 67 | + 10, | |
| 68 | + 2 | |
| 69 | + ); | |
| 59 | 70 | |
| 60 | 71 | // Apply label translations for settings |
| 61 | - add_filter( 'wp_stream_serialized_labels', array( $this, 'get_settings_translations' ) ); | |
| 72 | + add_filter( | |
| 73 | + 'wp_stream_serialized_labels', | |
| 74 | + array( | |
| 75 | + $this, | |
| 76 | + 'get_settings_translations', | |
| 77 | + ) | |
| 78 | + ); | |
| 62 | 79 | |
| 63 | 80 | // Ajax callback function to search users |
| 64 | 81 | add_action( 'wp_ajax_stream_get_users', array( $this, 'get_users' ) ); |
| 65 | 82 | |
| @@ -83,19 +100,32 @@ | ||
| 83 | 100 | 'status' => false, |
| 84 | 101 | 'message' => esc_html__( 'There was an error in the request', 'stream' ), |
| 85 | 102 | ); |
| 86 | 103 | |
| 87 | - $search = wp_unslash( trim( wp_stream_filter_input( INPUT_POST, 'find' ) ) ); | |
| 104 | + $search = ''; | |
| 105 | + $input = wp_stream_filter_input( INPUT_POST, 'find' ); | |
| 88 | 106 | |
| 107 | + if ( ! isset( $input['term'] ) ) { | |
| 108 | + $search = wp_unslash( trim( $input['term'] ) ); | |
| 109 | + } | |
| 110 | + | |
| 89 | 111 | $request = (object) array( |
| 90 | 112 | 'find' => $search, |
| 91 | 113 | ); |
| 92 | 114 | |
| 93 | - add_filter( 'user_search_columns', array( $this, 'add_display_name_search_columns' ), 10, 3 ); | |
| 115 | + add_filter( | |
| 116 | + 'user_search_columns', | |
| 117 | + array( | |
| 118 | + $this, | |
| 119 | + 'add_display_name_search_columns', | |
| 120 | + ), | |
| 121 | + 10, | |
| 122 | + 3 | |
| 123 | + ); | |
| 94 | 124 | |
| 95 | 125 | $users = new WP_User_Query( |
| 96 | 126 | array( |
| 97 | - 'search' => "*{$request->find}*", | |
| 127 | + 'search' => "*{$request->find}*", | |
| 98 | 128 | 'search_columns' => array( |
| 99 | 129 | 'user_login', |
| 100 | 130 | 'user_nicename', |
| 101 | 131 | 'user_email', |
| @@ -100,14 +130,21 @@ | ||
| 100 | 130 | 'user_nicename', |
| 101 | 131 | 'user_email', |
| 102 | 132 | 'user_url', |
| 103 | 133 | ), |
| 104 | - 'orderby' => 'display_name', | |
| 105 | - 'number' => $this->plugin->admin->preload_users_max, | |
| 134 | + 'orderby' => 'display_name', | |
| 135 | + 'number' => $this->plugin->admin->preload_users_max, | |
| 106 | 136 | ) |
| 107 | 137 | ); |
| 108 | 138 | |
| 109 | - remove_filter( 'user_search_columns', array( $this, 'add_display_name_search_columns' ), 10 ); | |
| 139 | + remove_filter( | |
| 140 | + 'user_search_columns', | |
| 141 | + array( | |
| 142 | + $this, | |
| 143 | + 'add_display_name_search_columns', | |
| 144 | + ), | |
| 145 | + 10 | |
| 146 | + ); | |
| 110 | 147 | |
| 111 | 148 | if ( 0 === $users->get_total() ) { |
| 112 | 149 | wp_send_json_error( $response ); |
| 113 | 150 | } |
| @@ -115,16 +152,17 @@ | ||
| 115 | 152 | |
| 116 | 153 | if ( is_multisite() && is_super_admin() ) { |
| 117 | 154 | $super_admins = get_super_admins(); |
| 118 | 155 | foreach ( $super_admins as $admin ) { |
| 119 | - $user = get_user_by( 'login', $admin ); | |
| 156 | + $user = get_user_by( 'login', $admin ); | |
| 120 | 157 | $users_array[] = $user; |
| 121 | 158 | } |
| 122 | 159 | } |
| 123 | 160 | |
| 124 | - $response->status = true; | |
| 125 | - $response->message = ''; | |
| 126 | - $response->users = array(); | |
| 161 | + $response->status = true; | |
| 162 | + $response->message = ''; | |
| 163 | + $response->roles = $this->get_roles(); | |
| 164 | + $response->users = array(); | |
| 127 | 165 | $users_added_to_response = array(); |
| 128 | 166 | |
| 129 | 167 | foreach ( $users_array as $key => $user ) { |
| 130 | 168 | // exclude duplications: |
| @@ -142,8 +180,9 @@ | ||
| 142 | 180 | ); |
| 143 | 181 | |
| 144 | 182 | $args['tooltip'] = esc_attr( |
| 145 | 183 | sprintf( |
| 184 | + // translators: Placeholders refers to a user ID, a username, an email address, and a user role (e.g. "42", "administrator", "foo@bar.com", "subscriber"). | |
| 146 | 185 | __( 'ID: %1$d\nUser: %2$s\nEmail: %3$s\nRole: %4$s', 'stream' ), |
| 147 | 186 | $author->id, |
| 148 | 187 | $author->user_login, |
| 149 | 188 | $author->user_email, |
| @@ -157,15 +196,15 @@ | ||
| 157 | 196 | } |
| 158 | 197 | |
| 159 | 198 | usort( |
| 160 | 199 | $response->users, |
| 161 | - function( $a, $b ) { | |
| 200 | + function ( $a, $b ) { | |
| 162 | 201 | return strcmp( $a['text'], $b['text'] ); |
| 163 | 202 | } |
| 164 | 203 | ); |
| 165 | 204 | |
| 166 | 205 | if ( empty( $search ) || preg_match( '/wp|cli|system|unknown/i', $search ) ) { |
| 167 | - $author = new Author( 0 ); | |
| 206 | + $author = new Author( 0 ); | |
| 168 | 207 | $response->users[] = array( |
| 169 | 208 | 'id' => '0', |
| 170 | 209 | 'text' => $author->get_display_name(), |
| 171 | 210 | 'icon' => $author->get_avatar_src( 32 ), |
| @@ -176,10 +215,10 @@ | ||
| 176 | 215 | wp_send_json_success( $response ); |
| 177 | 216 | } |
| 178 | 217 | |
| 179 | 218 | /** |
| 180 | - * Ajax callback function to search IP addresses, used on exclude setting page | |
| 181 | - */ | |
| 219 | + * Ajax callback function to search IP addresses, used on exclude setting page | |
| 220 | + */ | |
| 182 | 221 | public function get_ips() { |
| 183 | 222 | if ( ! defined( 'DOING_AJAX' ) || ! current_user_can( $this->plugin->admin->settings_cap ) ) { |
| 184 | 223 | return; |
| 185 | 224 | } |
| @@ -189,11 +228,14 @@ | ||
| 189 | 228 | $ips = $this->plugin->db->existing_records( 'ip' ); |
| 190 | 229 | $find = wp_stream_filter_input( INPUT_POST, 'find' ); |
| 191 | 230 | |
| 192 | 231 | if ( isset( $find['term'] ) && '' !== $find['term'] ) { |
| 193 | - $ips = array_filter( $ips, function ( $ip ) use ( $find ) { | |
| 194 | - return 0 === strpos( $ip, $find['term'] ); | |
| 195 | - } ); | |
| 232 | + $ips = array_filter( | |
| 233 | + $ips, | |
| 234 | + function ( $ip ) use ( $find ) { | |
| 235 | + return 0 === strpos( $ip, $find['term'] ); | |
| 236 | + } | |
| 237 | + ); | |
| 196 | 238 | } |
| 197 | 239 | |
| 198 | 240 | if ( $ips ) { |
| 199 | 241 | wp_send_json_success( $ips ); |
| @@ -204,11 +246,11 @@ | ||
| 204 | 246 | |
| 205 | 247 | /** |
| 206 | 248 | * Filter the columns to search in a WP_User_Query search. |
| 207 | 249 | * |
| 208 | - * @param array $search_columns Array of column names to be searched. | |
| 209 | - * @param string $search Text being searched. | |
| 210 | - * @param \WP_User_Query $query current WP_User_Query instance. | |
| 250 | + * @param array $search_columns Array of column names to be searched. | |
| 251 | + * @param string $search Text being searched. | |
| 252 | + * @param \WP_User_Query $query current WP_User_Query instance. | |
| 211 | 253 | * |
| 212 | 254 | * @return array |
| 213 | 255 | */ |
| 214 | 256 | public function add_display_name_search_columns( $search_columns, $search, $query ) { |
| @@ -247,18 +289,18 @@ | ||
| 247 | 289 | * @return array |
| 248 | 290 | */ |
| 249 | 291 | public function get_fields() { |
| 250 | 292 | $fields = array( |
| 251 | - 'general' => array( | |
| 293 | + 'general' => array( | |
| 252 | 294 | 'title' => esc_html__( 'General', 'stream' ), |
| 253 | 295 | 'fields' => array( |
| 254 | 296 | array( |
| 255 | - 'name' => 'role_access', | |
| 256 | - 'title' => esc_html__( 'Role Access', 'stream' ), | |
| 257 | - 'type' => 'multi_checkbox', | |
| 258 | - 'desc' => esc_html__( 'Users from the selected roles above will have permission to view Stream Records. However, only site Administrators can access Stream Settings.', 'stream' ), | |
| 259 | - 'choices' => $this->get_roles(), | |
| 260 | - 'default' => array( 'administrator' ), | |
| 297 | + 'name' => 'role_access', | |
| 298 | + 'title' => esc_html__( 'Role Access', 'stream' ), | |
| 299 | + 'type' => 'multi_checkbox', | |
| 300 | + 'desc' => esc_html__( 'Users from the selected roles above will have permission to view Stream Records. However, only site Administrators can access Stream Settings.', 'stream' ), | |
| 301 | + 'choices' => $this->get_roles(), | |
| 302 | + 'default' => array( 'administrator' ), | |
| 261 | 303 | ), |
| 262 | 304 | array( |
| 263 | 305 | 'name' => 'records_ttl', |
| 264 | 306 | 'title' => esc_html__( 'Keep Records for', 'stream' ), |
| @@ -280,18 +322,18 @@ | ||
| 280 | 322 | 'default' => 0, |
| 281 | 323 | ), |
| 282 | 324 | ), |
| 283 | 325 | ), |
| 284 | - 'exclude' => array( | |
| 326 | + 'exclude' => array( | |
| 285 | 327 | 'title' => esc_html__( 'Exclude', 'stream' ), |
| 286 | 328 | 'fields' => array( |
| 287 | 329 | array( |
| 288 | - 'name' => 'rules', | |
| 289 | - 'title' => esc_html__( 'Exclude Rules', 'stream' ), | |
| 290 | - 'type' => 'rule_list', | |
| 291 | - 'desc' => esc_html__( 'Create rules to exclude certain kinds of activity from being recorded by Stream.', 'stream' ), | |
| 292 | - 'default' => array(), | |
| 293 | - 'nonce' => 'stream_get_ips', | |
| 330 | + 'name' => 'rules', | |
| 331 | + 'title' => esc_html__( 'Exclude Rules', 'stream' ), | |
| 332 | + 'type' => 'rule_list', | |
| 333 | + 'desc' => esc_html__( 'Create rules to exclude certain kinds of activity from being recorded by Stream.', 'stream' ), | |
| 334 | + 'default' => array(), | |
| 335 | + 'nonce' => 'stream_get_ips', | |
| 294 | 336 | ), |
| 295 | 337 | ), |
| 296 | 338 | ), |
| 297 | 339 | 'advanced' => array( |
| @@ -305,22 +347,22 @@ | ||
| 305 | 347 | 'after_field' => esc_html__( 'Enabled', 'stream' ), |
| 306 | 348 | 'default' => 0, |
| 307 | 349 | ), |
| 308 | 350 | array( |
| 309 | - 'name' => 'delete_all_records', | |
| 310 | - 'title' => esc_html__( 'Reset Stream Database', 'stream' ), | |
| 311 | - 'type' => 'link', | |
| 312 | - 'href' => add_query_arg( | |
| 351 | + 'name' => 'delete_all_records', | |
| 352 | + 'title' => esc_html__( 'Reset Stream Database', 'stream' ), | |
| 353 | + 'type' => 'link', | |
| 354 | + 'href' => add_query_arg( | |
| 313 | 355 | array( |
| 314 | - 'action' => 'wp_stream_reset', | |
| 315 | - 'wp_stream_nonce' => wp_create_nonce( 'stream_nonce' ), | |
| 356 | + 'action' => 'wp_stream_reset', | |
| 357 | + 'wp_stream_nonce_reset' => wp_create_nonce( 'stream_nonce_reset' ), | |
| 316 | 358 | ), |
| 317 | 359 | admin_url( 'admin-ajax.php' ) |
| 318 | 360 | ), |
| 319 | - 'class' => 'warning', | |
| 320 | - 'desc' => esc_html__( 'Warning: This will delete all activity records from the database.', 'stream' ), | |
| 321 | - 'default' => 0, | |
| 322 | - 'sticky' => 'bottom', | |
| 361 | + 'class' => 'warning', | |
| 362 | + 'desc' => esc_html__( 'Warning: This will delete all activity records from the database.', 'stream' ), | |
| 363 | + 'default' => 0, | |
| 364 | + 'sticky' => 'bottom', | |
| 323 | 365 | ), |
| 324 | 366 | ), |
| 325 | 367 | ), |
| 326 | 368 | ); |
| @@ -338,21 +380,18 @@ | ||
| 338 | 380 | |
| 339 | 381 | array_push( $fields['advanced']['fields'], $akismet_tracking ); |
| 340 | 382 | } |
| 341 | 383 | |
| 342 | - // If WP Cron is enabled, allow Admins to opt-in to WP Cron tracking | |
| 343 | - if ( wp_stream_is_cron_enabled() ) { | |
| 344 | - $wp_cron_tracking = array( | |
| 345 | - 'name' => 'wp_cron_tracking', | |
| 346 | - 'title' => esc_html__( 'WP Cron Tracking', 'stream' ), | |
| 347 | - 'type' => 'checkbox', | |
| 348 | - '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' ), | |
| 349 | - 'after_field' => esc_html__( 'Enabled', 'stream' ), | |
| 350 | - 'default' => 0, | |
| 351 | - ); | |
| 384 | + $wp_cron_tracking = array( | |
| 385 | + 'name' => 'wp_cron_tracking', | |
| 386 | + 'title' => esc_html__( 'WP Cron Tracking', 'stream' ), | |
| 387 | + 'type' => 'checkbox', | |
| 388 | + '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' ), | |
| 389 | + 'after_field' => esc_html__( 'Enabled', 'stream' ), | |
| 390 | + 'default' => 0, | |
| 391 | + ); | |
| 352 | 392 | |
| 353 | - array_push( $fields['advanced']['fields'], $wp_cron_tracking ); | |
| 354 | - } | |
| 393 | + array_push( $fields['advanced']['fields'], $wp_cron_tracking ); | |
| 355 | 394 | |
| 356 | 395 | /** |
| 357 | 396 | * Filter allows for modification of options fields |
| 358 | 397 | * |
| @@ -431,9 +470,16 @@ | ||
| 431 | 470 | */ |
| 432 | 471 | public function register_settings() { |
| 433 | 472 | $sections = $this->get_fields(); |
| 434 | 473 | |
| 435 | - register_setting( $this->option_key, $this->option_key, array( $this, 'sanitize_settings' ) ); | |
| 474 | + register_setting( | |
| 475 | + $this->option_key, | |
| 476 | + $this->option_key, | |
| 477 | + array( | |
| 478 | + $this, | |
| 479 | + 'sanitize_settings', | |
| 480 | + ) | |
| 481 | + ); | |
| 436 | 482 | |
| 437 | 483 | foreach ( $sections as $section_name => $section ) { |
| 438 | 484 | add_settings_section( |
| 439 | 485 | $section_name, |
| @@ -449,14 +495,18 @@ | ||
| 449 | 495 | |
| 450 | 496 | add_settings_field( |
| 451 | 497 | $field['name'], |
| 452 | 498 | $field['title'], |
| 453 | - ( isset( $field['callback'] ) ? $field['callback'] : array( $this, 'output_field' ) ), | |
| 499 | + ( isset( $field['callback'] ) ? $field['callback'] : array( | |
| 500 | + $this, | |
| 501 | + 'output_field', | |
| 502 | + ) ), | |
| 454 | 503 | $this->option_key, |
| 455 | 504 | $section_name, |
| 456 | 505 | $field + array( |
| 457 | 506 | 'section' => $section_name, |
| 458 | - 'label_for' => sprintf( '%s_%s_%s', $this->option_key, $section_name, $field['name'] ), // xss ok | |
| 507 | + 'label_for' => sprintf( '%s_%s_%s', $this->option_key, $section_name, $field['name'] ), | |
| 508 | + // xss ok | |
| 459 | 509 | ) |
| 460 | 510 | ); |
| 461 | 511 | } |
| 462 | 512 | } |
| @@ -485,9 +535,9 @@ | ||
| 485 | 535 | if ( empty( $type ) || ! isset( $input[ $name ] ) || '' === $input[ $name ] ) { |
| 486 | 536 | continue; |
| 487 | 537 | } |
| 488 | 538 | |
| 489 | - // Sanitize depending on the type of field | |
| 539 | + // Sanitize depending on the type of field. | |
| 490 | 540 | switch ( $type ) { |
| 491 | 541 | case 'number': |
| 492 | 542 | $output[ $name ] = is_numeric( $input[ $name ] ) ? intval( trim( $input[ $name ] ) ) : ''; |
| 493 | 543 | break; |
| @@ -497,14 +547,17 @@ | ||
| 497 | 547 | default: |
| 498 | 548 | if ( is_array( $input[ $name ] ) ) { |
| 499 | 549 | $output[ $name ] = $input[ $name ]; |
| 500 | 550 | |
| 501 | - // Support all values in multidimentional arrays too | |
| 502 | - array_walk_recursive( $output[ $name ], function( &$v, $k ) { | |
| 503 | - $v = trim( $v ); | |
| 504 | - } ); | |
| 551 | + // Support all values in multidimentional arrays too. | |
| 552 | + array_walk_recursive( | |
| 553 | + $output[ $name ], | |
| 554 | + function ( &$v ) { | |
| 555 | + $v = sanitize_text_field( trim( $v ) ); | |
| 556 | + } | |
| 557 | + ); | |
| 505 | 558 | } else { |
| 506 | - $output[ $name ] = trim( $input[ $name ] ); | |
| 559 | + $output[ $name ] = sanitize_text_field( trim( $input[ $name ] ) ); | |
| 507 | 560 | } |
| 508 | 561 | } |
| 509 | 562 | } |
| 510 | 563 | } |
| @@ -557,11 +610,9 @@ | ||
| 557 | 610 | if ( ! $type || ! $section || ! $name ) { |
| 558 | 611 | return ''; |
| 559 | 612 | } |
| 560 | 613 | |
| 561 | - if ( 'multi_checkbox' === $type | |
| 562 | - && ( empty( $field['choices'] ) || ! is_array( $field['choices'] ) ) | |
| 563 | - ) { | |
| 614 | + if ( 'multi_checkbox' === $type && ( empty( $field['choices'] ) || ! is_array( $field['choices'] ) ) ) { | |
| 564 | 615 | return ''; |
| 565 | 616 | } |
| 566 | 617 | |
| 567 | 618 | switch ( $type ) { |
| @@ -620,10 +671,10 @@ | ||
| 620 | 671 | esc_attr( $option_key ), |
| 621 | 672 | esc_attr( $section ), |
| 622 | 673 | esc_attr( $name ) |
| 623 | 674 | ); |
| 624 | - // Fallback if nothing is selected | |
| 625 | - $output .= sprintf( | |
| 675 | + // Fallback if nothing is selected. | |
| 676 | + $output .= sprintf( | |
| 626 | 677 | '<input type="hidden" name="%1$s[%2$s_%3$s][]" value="__placeholder__" />', |
| 627 | 678 | esc_attr( $option_key ), |
| 628 | 679 | esc_attr( $section ), |
| 629 | 680 | esc_attr( $name ) |
| @@ -628,9 +679,9 @@ | ||
| 628 | 679 | esc_attr( $section ), |
| 629 | 680 | esc_attr( $name ) |
| 630 | 681 | ); |
| 631 | 682 | $current_value = (array) $current_value; |
| 632 | - $choices = $field['choices']; | |
| 683 | + $choices = $field['choices']; | |
| 633 | 684 | if ( is_callable( $choices ) ) { |
| 634 | 685 | $choices = call_user_func( $choices ); |
| 635 | 686 | } |
| 636 | 687 | foreach ( $choices as $value => $label ) { |
| @@ -695,9 +746,9 @@ | ||
| 695 | 746 | esc_attr( $href ), |
| 696 | 747 | esc_attr( $title ) |
| 697 | 748 | ); |
| 698 | 749 | break; |
| 699 | - case 'select2' : | |
| 750 | + case 'select2': | |
| 700 | 751 | if ( ! isset( $current_value ) ) { |
| 701 | 752 | $current_value = ''; |
| 702 | 753 | } |
| 703 | 754 | |
| @@ -714,16 +765,26 @@ | ||
| 714 | 765 | $child_values = array(); |
| 715 | 766 | if ( isset( $value['children'] ) ) { |
| 716 | 767 | $child_values = array(); |
| 717 | 768 | foreach ( $value['children'] as $child_key => $child_value ) { |
| 718 | - $child_values[] = array( 'id' => $child_key, 'text' => $child_value ); | |
| 769 | + $child_values[] = array( | |
| 770 | + 'id' => $child_key, | |
| 771 | + 'text' => $child_value, | |
| 772 | + ); | |
| 719 | 773 | } |
| 720 | 774 | } |
| 721 | 775 | if ( isset( $value['label'] ) ) { |
| 722 | - $data_values[] = array( 'id' => $key, 'text' => $value['label'], 'children' => $child_values ); | |
| 776 | + $data_values[] = array( | |
| 777 | + 'id' => $key, | |
| 778 | + 'text' => $value['label'], | |
| 779 | + 'children' => $child_values, | |
| 780 | + ); | |
| 723 | 781 | } |
| 724 | 782 | } else { |
| 725 | - $data_values[] = array( 'id' => $key, 'text' => $value ); | |
| 783 | + $data_values[] = array( | |
| 784 | + 'id' => $key, | |
| 785 | + 'text' => $value, | |
| 786 | + ); | |
| 726 | 787 | } |
| 727 | 788 | } |
| 728 | 789 | $class .= ' with-source'; |
| 729 | 790 | } |
| @@ -735,8 +796,9 @@ | ||
| 735 | 796 | esc_attr( $name ), |
| 736 | 797 | esc_attr( wp_stream_json_encode( $data_values ) ), |
| 737 | 798 | esc_attr( $current_value ), |
| 738 | 799 | esc_attr( $class ), |
| 800 | + // translators: Placeholder refers to the title of the dropdown menu (e.g. "users") | |
| 739 | 801 | sprintf( esc_html__( 'Any %s', 'stream' ), $title ) |
| 740 | 802 | ); |
| 741 | 803 | |
| 742 | 804 | $output = sprintf( |
| @@ -747,14 +809,15 @@ | ||
| 747 | 809 | $input_html |
| 748 | 810 | ); |
| 749 | 811 | |
| 750 | 812 | break; |
| 751 | - case 'rule_list' : | |
| 752 | - $form = new Form_Generator; | |
| 813 | + case 'rule_list': | |
| 814 | + $users = count_users(); | |
| 815 | + $form = new Form_Generator(); | |
| 753 | 816 | $output = '<p class="description">' . esc_html( $description ) . '</p>'; |
| 754 | 817 | |
| 755 | - $actions_top = sprintf( '<input type="button" class="button" id="%1$s_new_rule" value="+ %2$s" />', esc_attr( $section . '_' . $name ), esc_html__( 'Add New Rule', 'stream' ) ); | |
| 756 | - $actions_bottom = sprintf( '<input type="button" class="button" id="%1$s_remove_rules" value="%2$s" />', esc_attr( $section . '_' . $name ), esc_html__( 'Delete Selected Rules', 'stream' ) ); | |
| 818 | + $actions_top = sprintf( '<input type="button" class="button" id="%1$s_new_rule" value="+ %2$s" />', esc_attr( $section . '_' . $name ), esc_html__( 'Add New Rule', 'stream' ) ); | |
| 819 | + $actions_bottom = sprintf( '<input type="button" class="button" id="%1$s_remove_rules" value="%2$s" />', esc_attr( $section . '_' . $name ), esc_html__( 'Delete Selected Rules', 'stream' ) ); | |
| 757 | 820 | |
| 758 | 821 | $output .= sprintf( '<div class="tablenav top">%1$s</div>', $actions_top ); |
| 759 | 822 | $output .= '<table class="wp-list-table widefat fixed stream-exclude-list">'; |
| 760 | 823 | |
| @@ -778,13 +841,18 @@ | ||
| 778 | 841 | ); |
| 779 | 842 | |
| 780 | 843 | $exclude_rows = array(); |
| 781 | 844 | |
| 782 | - // Prepend an empty row | |
| 783 | - $current_value['exclude_row'] = array( 'helper' => '' ) + ( isset( $current_value['exclude_row'] ) ? $current_value['exclude_row'] : array() ); | |
| 845 | + // Account for when no rules have been added yet. | |
| 846 | + if ( ! is_array( $current_value ) ) { | |
| 847 | + $current_value = array(); | |
| 848 | + } | |
| 784 | 849 | |
| 850 | + // Prepend an empty row. | |
| 851 | + $current_value['exclude_row'] = ( isset( $current_value['exclude_row'] ) ? $current_value['exclude_row'] : array() ) + array( 'helper' => '' ); | |
| 852 | + | |
| 785 | 853 | foreach ( $current_value['exclude_row'] as $key => $value ) { |
| 786 | - // Prepare values | |
| 854 | + // Prepare values. | |
| 787 | 855 | $author_or_role = isset( $current_value['author_or_role'][ $key ] ) ? $current_value['author_or_role'][ $key ] : ''; |
| 788 | 856 | $connector = isset( $current_value['connector'][ $key ] ) ? $current_value['connector'][ $key ] : ''; |
| 789 | 857 | $context = isset( $current_value['context'][ $key ] ) ? $current_value['context'][ $key ] : ''; |
| 790 | 858 | $action = isset( $current_value['action'][ $key ] ) ? $current_value['action'][ $key ] : ''; |
| @@ -794,13 +862,16 @@ | ||
| 794 | 862 | $author_or_role_values = array(); |
| 795 | 863 | $author_or_role_selected = array(); |
| 796 | 864 | |
| 797 | 865 | foreach ( $this->get_roles() as $role_id => $role ) { |
| 798 | - $args = array( 'value' => $role_id, 'text' => $role ); | |
| 799 | - $users = count_users(); | |
| 866 | + $args = array( | |
| 867 | + 'value' => $role_id, | |
| 868 | + 'text' => $role, | |
| 869 | + ); | |
| 800 | 870 | $count = isset( $users['avail_roles'][ $role_id ] ) ? $users['avail_roles'][ $role_id ] : 0; |
| 801 | 871 | |
| 802 | 872 | if ( ! empty( $count ) ) { |
| 873 | + // translators: Placeholder refers to a number of users (e.g. "42") | |
| 803 | 874 | $args['user_count'] = sprintf( _n( '%d user', '%d users', absint( $count ), 'stream' ), absint( $count ) ); |
| 804 | 875 | } |
| 805 | 876 | |
| 806 | 877 | if ( $role_id === $author_or_role ) { |
| @@ -813,23 +884,29 @@ | ||
| 813 | 884 | |
| 814 | 885 | if ( empty( $author_or_role_selected ) && is_numeric( $author_or_role ) ) { |
| 815 | 886 | $user = new WP_User( $author_or_role ); |
| 816 | 887 | $display_name = ( 0 === $user->ID ) ? esc_html__( 'N/A', 'stream' ) : $user->display_name; |
| 817 | - $author_or_role_selected = array( 'value' => $user->ID, 'text' => $display_name ); | |
| 888 | + $author_or_role_selected = array( | |
| 889 | + 'value' => $user->ID, | |
| 890 | + 'text' => $display_name, | |
| 891 | + ); | |
| 818 | 892 | $author_or_role_values[] = $author_or_role_selected; |
| 819 | 893 | } |
| 820 | 894 | |
| 821 | - $author_or_role_input = $form->render_field( 'select2', array( | |
| 822 | - 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]' , $option_key, $section, $name, 'author_or_role' ) ), | |
| 823 | - 'options' => $author_or_role_values, | |
| 824 | - 'classes' => 'author_or_role', | |
| 825 | - 'data' => array( | |
| 826 | - 'placeholder' => esc_html__( 'Any Author or Role', 'stream' ), | |
| 827 | - 'nonce' => esc_attr( wp_create_nonce( 'stream_get_users' ) ), | |
| 828 | - 'selected-id' => isset( $author_or_role_selected['value'] ) ? esc_attr( $author_or_role_selected['value'] ) : '', | |
| 829 | - 'selected-text' => isset( $author_or_role_selected['text'] ) ? esc_attr( $author_or_role_selected['text'] ) : '', | |
| 830 | - ), | |
| 831 | - ) ); | |
| 895 | + $author_or_role_input = $form->render_field( | |
| 896 | + 'select2', | |
| 897 | + array( | |
| 898 | + 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]', $option_key, $section, $name, 'author_or_role' ) ), | |
| 899 | + 'options' => $author_or_role_values, | |
| 900 | + 'classes' => 'author_or_role', | |
| 901 | + 'data' => array( | |
| 902 | + 'placeholder' => esc_html__( 'Any Author or Role', 'stream' ), | |
| 903 | + 'nonce' => esc_attr( wp_create_nonce( 'stream_get_users' ) ), | |
| 904 | + 'selected-id' => isset( $author_or_role_selected['value'] ) ? esc_attr( $author_or_role_selected['value'] ) : '', | |
| 905 | + 'selected-text' => isset( $author_or_role_selected['text'] ) ? esc_attr( $author_or_role_selected['text'] ) : '', | |
| 906 | + ), | |
| 907 | + ) | |
| 908 | + ); | |
| 832 | 909 | |
| 833 | 910 | // Context dropdown menu |
| 834 | 911 | $context_values = array(); |
| 835 | 912 | |
| @@ -838,69 +915,98 @@ | ||
| 838 | 915 | $child_values = array(); |
| 839 | 916 | if ( isset( $context_data['children'] ) ) { |
| 840 | 917 | $child_values = array(); |
| 841 | 918 | foreach ( $context_data['children'] as $child_id => $child_value ) { |
| 842 | - $child_values[] = array( 'value' => $context_id . '-' . $child_id, 'text' => $child_value, 'parent' => $context_id ); | |
| 919 | + $child_values[] = array( | |
| 920 | + 'value' => $context_id . '-' . $child_id, | |
| 921 | + 'text' => $child_value, | |
| 922 | + 'parent' => $context_id, | |
| 923 | + ); | |
| 843 | 924 | } |
| 844 | 925 | } |
| 845 | 926 | if ( isset( $context_data['label'] ) ) { |
| 846 | - $context_values[] = array( 'value' => $context_id, 'text' => $context_data['label'], 'children' => $child_values ); | |
| 927 | + $context_values[] = array( | |
| 928 | + 'value' => $context_id, | |
| 929 | + 'text' => $context_data['label'], | |
| 930 | + 'children' => $child_values, | |
| 931 | + ); | |
| 847 | 932 | } |
| 848 | 933 | } else { |
| 849 | - $context_values[] = array( 'value' => $context_id, 'text' => $context_data ); | |
| 934 | + $context_values[] = array( | |
| 935 | + 'value' => $context_id, | |
| 936 | + 'text' => $context_data, | |
| 937 | + ); | |
| 850 | 938 | } |
| 851 | 939 | } |
| 852 | 940 | |
| 853 | - $connector_or_context_input = $form->render_field( 'select2', array( | |
| 854 | - 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]' , $option_key, $section, $name, 'connector_or_context' ) ), | |
| 855 | - 'options' => $context_values, | |
| 856 | - 'classes' => 'connector_or_context', | |
| 857 | - 'data' => array( | |
| 858 | - 'group' => 'connector', | |
| 859 | - 'placeholder' => __( 'Any Context', 'stream' ), | |
| 860 | - ), | |
| 861 | - ) ); | |
| 941 | + $connector_or_context_input = $form->render_field( | |
| 942 | + 'select2', | |
| 943 | + array( | |
| 944 | + 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]', $option_key, $section, $name, 'connector_or_context' ) ), | |
| 945 | + 'options' => $context_values, | |
| 946 | + 'classes' => 'connector_or_context', | |
| 947 | + 'data' => array( | |
| 948 | + 'group' => 'connector', | |
| 949 | + 'placeholder' => __( 'Any Context', 'stream' ), | |
| 950 | + ), | |
| 951 | + ) | |
| 952 | + ); | |
| 862 | 953 | |
| 863 | - $connector_input = $form->render_field( 'hidden', array( | |
| 864 | - 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]' , $option_key, $section, $name, 'connector' ) ), | |
| 865 | - 'value' => $connector, | |
| 866 | - 'classes' => 'connector', | |
| 867 | - ) ); | |
| 954 | + $connector_input = $form->render_field( | |
| 955 | + 'hidden', | |
| 956 | + array( | |
| 957 | + 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]', $option_key, $section, $name, 'connector' ) ), | |
| 958 | + 'value' => $connector, | |
| 959 | + 'classes' => 'connector', | |
| 960 | + ) | |
| 961 | + ); | |
| 868 | 962 | |
| 869 | - $context_input = $form->render_field( 'hidden', array( | |
| 870 | - 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]' , $option_key, $section, $name, 'context' ) ), | |
| 871 | - 'value' => $context, | |
| 872 | - 'classes' => 'context', | |
| 873 | - ) ); | |
| 963 | + $context_input = $form->render_field( | |
| 964 | + 'hidden', | |
| 965 | + array( | |
| 966 | + 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]', $option_key, $section, $name, 'context' ) ), | |
| 967 | + 'value' => $context, | |
| 968 | + 'classes' => 'context', | |
| 969 | + ) | |
| 970 | + ); | |
| 874 | 971 | |
| 875 | 972 | // Action dropdown menu |
| 876 | 973 | $action_values = array(); |
| 877 | 974 | |
| 878 | 975 | foreach ( $this->get_terms_labels( 'action' ) as $action_id => $action_data ) { |
| 879 | - $action_values[] = array( 'value' => $action_id, 'text' => $action_data ); | |
| 976 | + $action_values[] = array( | |
| 977 | + 'value' => $action_id, | |
| 978 | + 'text' => $action_data, | |
| 979 | + ); | |
| 880 | 980 | } |
| 881 | 981 | |
| 882 | - $action_input = $form->render_field( 'select2', array( | |
| 883 | - 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]' , $option_key, $section, $name, 'action' ) ), | |
| 884 | - 'value' => $action, | |
| 885 | - 'options' => $action_values, | |
| 886 | - 'classes' => 'action', | |
| 887 | - 'data' => array( | |
| 888 | - 'placeholder' => __( 'Any Action', 'stream' ), | |
| 889 | - ), | |
| 890 | - ) ); | |
| 982 | + $action_input = $form->render_field( | |
| 983 | + 'select2', | |
| 984 | + array( | |
| 985 | + 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]', $option_key, $section, $name, 'action' ) ), | |
| 986 | + 'value' => $action, | |
| 987 | + 'options' => $action_values, | |
| 988 | + 'classes' => 'action', | |
| 989 | + 'data' => array( | |
| 990 | + 'placeholder' => __( 'Any Action', 'stream' ), | |
| 991 | + ), | |
| 992 | + ) | |
| 993 | + ); | |
| 891 | 994 | |
| 892 | 995 | // IP Address input |
| 893 | - $ip_address_input = $form->render_field( 'select2', array( | |
| 894 | - 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]' , $option_key, $section, $name, 'ip_address' ) ), | |
| 895 | - 'value' => $ip_address, | |
| 896 | - 'classes' => 'ip_address', | |
| 897 | - 'data' => array( | |
| 898 | - 'placeholder' => esc_attr__( 'Any IP Address', 'stream' ), | |
| 899 | - 'nonce' => esc_attr( wp_create_nonce( 'stream_get_ips' ) ), | |
| 900 | - ), | |
| 901 | - 'multiple' => true, | |
| 902 | - ) ); | |
| 996 | + $ip_address_input = $form->render_field( | |
| 997 | + 'select2', | |
| 998 | + array( | |
| 999 | + 'name' => esc_attr( sprintf( '%1$s[%2$s_%3$s][%4$s][]', $option_key, $section, $name, 'ip_address' ) ), | |
| 1000 | + 'value' => $ip_address, | |
| 1001 | + 'classes' => 'ip_address', | |
| 1002 | + 'data' => array( | |
| 1003 | + 'placeholder' => esc_attr__( 'Any IP Address', 'stream' ), | |
| 1004 | + 'nonce' => esc_attr( wp_create_nonce( 'stream_get_ips' ) ), | |
| 1005 | + ), | |
| 1006 | + 'multiple' => true, | |
| 1007 | + ) | |
| 1008 | + ); | |
| 903 | 1009 | |
| 904 | 1010 | // Hidden helper input |
| 905 | 1011 | $helper_input = sprintf( |
| 906 | 1012 | '<input type="hidden" name="%1$s[%2$s_%3$s][%4$s][]" value="" />', |
| @@ -918,10 +1024,10 @@ | ||
| 918 | 1024 | <td>%9$s</td> |
| 919 | 1025 | <td>%10$s</td> |
| 920 | 1026 | <th scope="row" class="actions-column">%11$s</th> |
| 921 | 1027 | </tr>', |
| 922 | - ( 0 !== $key % 2 ) ? 'alternate' : '', | |
| 923 | - ( 'helper' === $key ) ? 'hidden helper' : '', | |
| 1028 | + ( 0 !== (int) $key % 2 ) ? 'alternate' : '', | |
| 1029 | + ( 'helper' === (string) $key ) ? 'hidden helper' : '', | |
| 924 | 1030 | '<input class="cb-select" type="checkbox" />', |
| 925 | 1031 | $helper_input, |
| 926 | 1032 | $author_or_role_input, |
| 927 | 1033 | $connector_or_context_input, |
| @@ -1029,10 +1135,10 @@ | ||
| 1029 | 1135 | * @param array $old_value |
| 1030 | 1136 | * @param array $new_value |
| 1031 | 1137 | */ |
| 1032 | 1138 | public function updated_option_ttl_remove_records( $old_value, $new_value ) { |
| 1033 | - $ttl_before = isset( $old_value['general_records_ttl'] ) ? (int) $old_value['general_records_ttl'] : -1; | |
| 1034 | - $ttl_after = isset( $new_value['general_records_ttl'] ) ? (int) $new_value['general_records_ttl'] : -1; | |
| 1139 | + $ttl_before = isset( $old_value['general_records_ttl'] ) ? (int) $old_value['general_records_ttl'] : - 1; | |
| 1140 | + $ttl_after = isset( $new_value['general_records_ttl'] ) ? (int) $new_value['general_records_ttl'] : - 1; | |
| 1035 | 1141 | |
| 1036 | 1142 | if ( $ttl_after < $ttl_before ) { |
| 1037 | 1143 | /** |
| 1038 | 1144 | * Action assists in purging when TTL is shortened |