| @@ -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 | |
| @@ -84,21 +101,31 @@ | ||
| 84 | 101 | 'message' => esc_html__( 'There was an error in the request', 'stream' ), |
| 85 | 102 | ); |
| 86 | 103 | |
| 87 | 104 | $search = ''; |
| 88 | - if ( isset( $_POST['find'] ) ) { | |
| 89 | - // @TODO: Make this pass phpcs | |
| 90 | - $search = esc_html( wp_unslash( trim( $_POST['find'] ) ) ); // input var okay | |
| 105 | + $input = wp_stream_filter_input( INPUT_POST, 'find' ); | |
| 106 | + | |
| 107 | + if ( ! isset( $input['term'] ) ) { | |
| 108 | + $search = wp_unslash( trim( $input['term'] ) ); | |
| 91 | 109 | } |
| 110 | + | |
| 92 | 111 | $request = (object) array( |
| 93 | 112 | 'find' => $search, |
| 94 | 113 | ); |
| 95 | 114 | |
| 96 | - 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 | + ); | |
| 97 | 124 | |
| 98 | 125 | $users = new WP_User_Query( |
| 99 | 126 | array( |
| 100 | - 'search' => "*{$request->find}*", | |
| 127 | + 'search' => "*{$request->find}*", | |
| 101 | 128 | 'search_columns' => array( |
| 102 | 129 | 'user_login', |
| 103 | 130 | 'user_nicename', |
| 104 | 131 | 'user_email', |
| @@ -103,24 +130,49 @@ | ||
| 103 | 130 | 'user_nicename', |
| 104 | 131 | 'user_email', |
| 105 | 132 | 'user_url', |
| 106 | 133 | ), |
| 107 | - 'orderby' => 'display_name', | |
| 108 | - 'number' => $this->plugin->admin->preload_users_max, | |
| 134 | + 'orderby' => 'display_name', | |
| 135 | + 'number' => $this->plugin->admin->preload_users_max, | |
| 109 | 136 | ) |
| 110 | 137 | ); |
| 111 | 138 | |
| 112 | - 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 | + ); | |
| 113 | 147 | |
| 114 | 148 | if ( 0 === $users->get_total() ) { |
| 115 | 149 | wp_send_json_error( $response ); |
| 116 | 150 | } |
| 151 | + $users_array = $users->results; | |
| 117 | 152 | |
| 118 | - $response->status = true; | |
| 119 | - $response->message = ''; | |
| 120 | - $response->users = array(); | |
| 153 | + if ( is_multisite() && is_super_admin() ) { | |
| 154 | + $super_admins = get_super_admins(); | |
| 155 | + foreach ( $super_admins as $admin ) { | |
| 156 | + $user = get_user_by( 'login', $admin ); | |
| 157 | + $users_array[] = $user; | |
| 158 | + } | |
| 159 | + } | |
| 121 | 160 | |
| 122 | - foreach ( $users->results as $key => $user ) { | |
| 161 | + $response->status = true; | |
| 162 | + $response->message = ''; | |
| 163 | + $response->roles = $this->get_roles(); | |
| 164 | + $response->users = array(); | |
| 165 | + $users_added_to_response = array(); | |
| 166 | + | |
| 167 | + foreach ( $users_array as $key => $user ) { | |
| 168 | + // exclude duplications: | |
| 169 | + if ( array_key_exists( $user->ID, $users_added_to_response ) ) { | |
| 170 | + continue; | |
| 171 | + } else { | |
| 172 | + $users_added_to_response[ $user->ID ] = true; | |
| 173 | + } | |
| 174 | + | |
| 123 | 175 | $author = new Author( $user->ID ); |
| 124 | 176 | |
| 125 | 177 | $args = array( |
| 126 | 178 | 'id' => $author->ID, |
| @@ -128,9 +180,10 @@ | ||
| 128 | 180 | ); |
| 129 | 181 | |
| 130 | 182 | $args['tooltip'] = esc_attr( |
| 131 | 183 | sprintf( |
| 132 | - __( "ID: %d\nUser: %s\nEmail: %s\nRole: %s", 'stream' ), | |
| 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"). | |
| 185 | + __( 'ID: %1$d\nUser: %2$s\nEmail: %3$s\nRole: %4$s', 'stream' ), | |
| 133 | 186 | $author->id, |
| 134 | 187 | $author->user_login, |
| 135 | 188 | $author->user_email, |
| 136 | 189 | ucwords( $author->get_role() ) |
| @@ -141,12 +194,19 @@ | ||
| 141 | 194 | |
| 142 | 195 | $response->users[] = $args; |
| 143 | 196 | } |
| 144 | 197 | |
| 198 | + usort( | |
| 199 | + $response->users, | |
| 200 | + function ( $a, $b ) { | |
| 201 | + return strcmp( $a['text'], $b['text'] ); | |
| 202 | + } | |
| 203 | + ); | |
| 204 | + | |
| 145 | 205 | if ( empty( $search ) || preg_match( '/wp|cli|system|unknown/i', $search ) ) { |
| 146 | - $author = new Author( 0 ); | |
| 206 | + $author = new Author( 0 ); | |
| 147 | 207 | $response->users[] = array( |
| 148 | - 'id' => $author->id, | |
| 208 | + 'id' => '0', | |
| 149 | 209 | 'text' => $author->get_display_name(), |
| 150 | 210 | 'icon' => $author->get_avatar_src( 32 ), |
| 151 | 211 | 'tooltip' => esc_html__( 'Actions performed by the system when a user is not logged in (e.g. auto site upgrader, or invoking WP-CLI without --user)', 'stream' ), |
| 152 | 212 | ); |
| @@ -155,10 +215,10 @@ | ||
| 155 | 215 | wp_send_json_success( $response ); |
| 156 | 216 | } |
| 157 | 217 | |
| 158 | 218 | /** |
| 159 | - * Ajax callback function to search IP addresses, used on exclude setting page | |
| 160 | - */ | |
| 219 | + * Ajax callback function to search IP addresses, used on exclude setting page | |
| 220 | + */ | |
| 161 | 221 | public function get_ips() { |
| 162 | 222 | if ( ! defined( 'DOING_AJAX' ) || ! current_user_can( $this->plugin->admin->settings_cap ) ) { |
| 163 | 223 | return; |
| 164 | 224 | } |
| @@ -164,10 +224,20 @@ | ||
| 164 | 224 | } |
| 165 | 225 | |
| 166 | 226 | check_ajax_referer( 'stream_get_ips', 'nonce' ); |
| 167 | 227 | |
| 168 | - $ips = $this->plugin->db->existing_records( 'ip' ); | |
| 228 | + $ips = $this->plugin->db->existing_records( 'ip' ); | |
| 229 | + $find = wp_stream_filter_input( INPUT_POST, 'find' ); | |
| 169 | 230 | |
| 231 | + if ( isset( $find['term'] ) && '' !== $find['term'] ) { | |
| 232 | + $ips = array_filter( | |
| 233 | + $ips, | |
| 234 | + function ( $ip ) use ( $find ) { | |
| 235 | + return 0 === strpos( $ip, $find['term'] ); | |
| 236 | + } | |
| 237 | + ); | |
| 238 | + } | |
| 239 | + | |
| 170 | 240 | if ( $ips ) { |
| 171 | 241 | wp_send_json_success( $ips ); |
| 172 | 242 | } else { |
| 173 | 243 | wp_send_json_error(); |
| @@ -176,11 +246,11 @@ | ||
| 176 | 246 | |
| 177 | 247 | /** |
| 178 | 248 | * Filter the columns to search in a WP_User_Query search. |
| 179 | 249 | * |
| 180 | - * @param array $search_columns Array of column names to be searched. | |
| 181 | - * @param string $search Text being searched. | |
| 182 | - * @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. | |
| 183 | 253 | * |
| 184 | 254 | * @return array |
| 185 | 255 | */ |
| 186 | 256 | public function add_display_name_search_columns( $search_columns, $search, $query ) { |
| @@ -219,18 +289,18 @@ | ||
| 219 | 289 | * @return array |
| 220 | 290 | */ |
| 221 | 291 | public function get_fields() { |
| 222 | 292 | $fields = array( |
| 223 | - 'general' => array( | |
| 293 | + 'general' => array( | |
| 224 | 294 | 'title' => esc_html__( 'General', 'stream' ), |
| 225 | 295 | 'fields' => array( |
| 226 | 296 | array( |
| 227 | - 'name' => 'role_access', | |
| 228 | - 'title' => esc_html__( 'Role Access', 'stream' ), | |
| 229 | - 'type' => 'multi_checkbox', | |
| 230 | - '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' ), | |
| 231 | - 'choices' => $this->get_roles(), | |
| 232 | - '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' ), | |
| 233 | 303 | ), |
| 234 | 304 | array( |
| 235 | 305 | 'name' => 'records_ttl', |
| 236 | 306 | 'title' => esc_html__( 'Keep Records for', 'stream' ), |
| @@ -252,18 +322,18 @@ | ||
| 252 | 322 | 'default' => 0, |
| 253 | 323 | ), |
| 254 | 324 | ), |
| 255 | 325 | ), |
| 256 | - 'exclude' => array( | |
| 326 | + 'exclude' => array( | |
| 257 | 327 | 'title' => esc_html__( 'Exclude', 'stream' ), |
| 258 | 328 | 'fields' => array( |
| 259 | 329 | array( |
| 260 | - 'name' => 'rules', | |
| 261 | - 'title' => esc_html__( 'Exclude Rules', 'stream' ), | |
| 262 | - 'type' => 'rule_list', | |
| 263 | - 'desc' => esc_html__( 'Create rules to exclude certain kinds of activity from being recorded by Stream.', 'stream' ), | |
| 264 | - 'default' => array(), | |
| 265 | - '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', | |
| 266 | 336 | ), |
| 267 | 337 | ), |
| 268 | 338 | ), |
| 269 | 339 | 'advanced' => array( |
| @@ -277,22 +347,22 @@ | ||
| 277 | 347 | 'after_field' => esc_html__( 'Enabled', 'stream' ), |
| 278 | 348 | 'default' => 0, |
| 279 | 349 | ), |
| 280 | 350 | array( |
| 281 | - 'name' => 'delete_all_records', | |
| 282 | - 'title' => esc_html__( 'Reset Stream Database', 'stream' ), | |
| 283 | - 'type' => 'link', | |
| 284 | - '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( | |
| 285 | 355 | array( |
| 286 | - 'action' => 'wp_stream_reset', | |
| 287 | - 'wp_stream_nonce' => wp_create_nonce( 'stream_nonce' ), | |
| 356 | + 'action' => 'wp_stream_reset', | |
| 357 | + 'wp_stream_nonce_reset' => wp_create_nonce( 'stream_nonce_reset' ), | |
| 288 | 358 | ), |
| 289 | 359 | admin_url( 'admin-ajax.php' ) |
| 290 | 360 | ), |
| 291 | - 'class' => 'warning', | |
| 292 | - 'desc' => esc_html__( 'Warning: This will delete all activity records from the database.', 'stream' ), | |
| 293 | - 'default' => 0, | |
| 294 | - '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', | |
| 295 | 365 | ), |
| 296 | 366 | ), |
| 297 | 367 | ), |
| 298 | 368 | ); |
| @@ -310,21 +380,18 @@ | ||
| 310 | 380 | |
| 311 | 381 | array_push( $fields['advanced']['fields'], $akismet_tracking ); |
| 312 | 382 | } |
| 313 | 383 | |
| 314 | - // If WP Cron is enabled, allow Admins to opt-in to WP Cron tracking | |
| 315 | - if ( wp_stream_is_cron_enabled() ) { | |
| 316 | - $wp_cron_tracking = array( | |
| 317 | - 'name' => 'wp_cron_tracking', | |
| 318 | - 'title' => esc_html__( 'WP Cron Tracking', 'stream' ), | |
| 319 | - 'type' => 'checkbox', | |
| 320 | - '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' ), | |
| 321 | - 'after_field' => esc_html__( 'Enabled', 'stream' ), | |
| 322 | - 'default' => 0, | |
| 323 | - ); | |
| 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 | + ); | |
| 324 | 392 | |
| 325 | - array_push( $fields['advanced']['fields'], $wp_cron_tracking ); | |
| 326 | - } | |
| 393 | + array_push( $fields['advanced']['fields'], $wp_cron_tracking ); | |
| 327 | 394 | |
| 328 | 395 | /** |
| 329 | 396 | * Filter allows for modification of options fields |
| 330 | 397 | * |
| @@ -403,9 +470,16 @@ | ||
| 403 | 470 | */ |
| 404 | 471 | public function register_settings() { |
| 405 | 472 | $sections = $this->get_fields(); |
| 406 | 473 | |
| 407 | - 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 | + ); | |
| 408 | 482 | |
| 409 | 483 | foreach ( $sections as $section_name => $section ) { |
| 410 | 484 | add_settings_section( |
| 411 | 485 | $section_name, |
| @@ -421,14 +495,18 @@ | ||
| 421 | 495 | |
| 422 | 496 | add_settings_field( |
| 423 | 497 | $field['name'], |
| 424 | 498 | $field['title'], |
| 425 | - ( isset( $field['callback'] ) ? $field['callback'] : array( $this, 'output_field' ) ), | |
| 499 | + ( isset( $field['callback'] ) ? $field['callback'] : array( | |
| 500 | + $this, | |
| 501 | + 'output_field', | |
| 502 | + ) ), | |
| 426 | 503 | $this->option_key, |
| 427 | 504 | $section_name, |
| 428 | 505 | $field + array( |
| 429 | 506 | 'section' => $section_name, |
| 430 | - '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 | |
| 431 | 509 | ) |
| 432 | 510 | ); |
| 433 | 511 | } |
| 434 | 512 | } |
| @@ -457,9 +535,9 @@ | ||
| 457 | 535 | if ( empty( $type ) || ! isset( $input[ $name ] ) || '' === $input[ $name ] ) { |
| 458 | 536 | continue; |
| 459 | 537 | } |
| 460 | 538 | |
| 461 | - // Sanitize depending on the type of field | |
| 539 | + // Sanitize depending on the type of field. | |
| 462 | 540 | switch ( $type ) { |
| 463 | 541 | case 'number': |
| 464 | 542 | $output[ $name ] = is_numeric( $input[ $name ] ) ? intval( trim( $input[ $name ] ) ) : ''; |
| 465 | 543 | break; |
| @@ -469,14 +547,17 @@ | ||
| 469 | 547 | default: |
| 470 | 548 | if ( is_array( $input[ $name ] ) ) { |
| 471 | 549 | $output[ $name ] = $input[ $name ]; |
| 472 | 550 | |
| 473 | - // Support all values in multidimentional arrays too | |
| 474 | - array_walk_recursive( $output[ $name ], function( &$v, $k ) { | |
| 475 | - $v = trim( $v ); | |
| 476 | - } ); | |
| 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 | + ); | |
| 477 | 558 | } else { |
| 478 | - $output[ $name ] = trim( $input[ $name ] ); | |
| 559 | + $output[ $name ] = sanitize_text_field( trim( $input[ $name ] ) ); | |
| 479 | 560 | } |
| 480 | 561 | } |
| 481 | 562 | } |
| 482 | 563 | } |
| @@ -529,11 +610,9 @@ | ||
| 529 | 610 | if ( ! $type || ! $section || ! $name ) { |
| 530 | 611 | return ''; |
| 531 | 612 | } |
| 532 | 613 | |
| 533 | - if ( 'multi_checkbox' === $type | |
| 534 | - && ( empty( $field['choices'] ) || ! is_array( $field['choices'] ) ) | |
| 535 | - ) { | |
| 614 | + if ( 'multi_checkbox' === $type && ( empty( $field['choices'] ) || ! is_array( $field['choices'] ) ) ) { | |
| 536 | 615 | return ''; |
| 537 | 616 | } |
| 538 | 617 | |
| 539 | 618 | switch ( $type ) { |
| @@ -592,10 +671,10 @@ | ||
| 592 | 671 | esc_attr( $option_key ), |
| 593 | 672 | esc_attr( $section ), |
| 594 | 673 | esc_attr( $name ) |
| 595 | 674 | ); |
| 596 | - // Fallback if nothing is selected | |
| 597 | - $output .= sprintf( | |
| 675 | + // Fallback if nothing is selected. | |
| 676 | + $output .= sprintf( | |
| 598 | 677 | '<input type="hidden" name="%1$s[%2$s_%3$s][]" value="__placeholder__" />', |
| 599 | 678 | esc_attr( $option_key ), |
| 600 | 679 | esc_attr( $section ), |
| 601 | 680 | esc_attr( $name ) |
| @@ -600,9 +679,9 @@ | ||
| 600 | 679 | esc_attr( $section ), |
| 601 | 680 | esc_attr( $name ) |
| 602 | 681 | ); |
| 603 | 682 | $current_value = (array) $current_value; |
| 604 | - $choices = $field['choices']; | |
| 683 | + $choices = $field['choices']; | |
| 605 | 684 | if ( is_callable( $choices ) ) { |
| 606 | 685 | $choices = call_user_func( $choices ); |
| 607 | 686 | } |
| 608 | 687 | foreach ( $choices as $value => $label ) { |
| @@ -613,9 +692,9 @@ | ||
| 613 | 692 | esc_attr( $option_key ), |
| 614 | 693 | esc_attr( $section ), |
| 615 | 694 | esc_attr( $name ), |
| 616 | 695 | esc_attr( $value ), |
| 617 | - checked( in_array( $value, $current_value ), true, false ) | |
| 696 | + checked( in_array( $value, $current_value, true ), true, false ) | |
| 618 | 697 | ), |
| 619 | 698 | esc_html( $label ) |
| 620 | 699 | ); |
| 621 | 700 | } |
| @@ -667,9 +746,9 @@ | ||
| 667 | 746 | esc_attr( $href ), |
| 668 | 747 | esc_attr( $title ) |
| 669 | 748 | ); |
| 670 | 749 | break; |
| 671 | - case 'select2' : | |
| 750 | + case 'select2': | |
| 672 | 751 | if ( ! isset( $current_value ) ) { |
| 673 | 752 | $current_value = ''; |
| 674 | 753 | } |
| 675 | 754 | |
| @@ -686,16 +765,26 @@ | ||
| 686 | 765 | $child_values = array(); |
| 687 | 766 | if ( isset( $value['children'] ) ) { |
| 688 | 767 | $child_values = array(); |
| 689 | 768 | foreach ( $value['children'] as $child_key => $child_value ) { |
| 690 | - $child_values[] = array( 'id' => $child_key, 'text' => $child_value ); | |
| 769 | + $child_values[] = array( | |
| 770 | + 'id' => $child_key, | |
| 771 | + 'text' => $child_value, | |
| 772 | + ); | |
| 691 | 773 | } |
| 692 | 774 | } |
| 693 | 775 | if ( isset( $value['label'] ) ) { |
| 694 | - $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 | + ); | |
| 695 | 781 | } |
| 696 | 782 | } else { |
| 697 | - $data_values[] = array( 'id' => $key, 'text' => $value ); | |
| 783 | + $data_values[] = array( | |
| 784 | + 'id' => $key, | |
| 785 | + 'text' => $value, | |
| 786 | + ); | |
| 698 | 787 | } |
| 699 | 788 | } |
| 700 | 789 | $class .= ' with-source'; |
| 701 | 790 | } |
| @@ -707,8 +796,9 @@ | ||
| 707 | 796 | esc_attr( $name ), |
| 708 | 797 | esc_attr( wp_stream_json_encode( $data_values ) ), |
| 709 | 798 | esc_attr( $current_value ), |
| 710 | 799 | esc_attr( $class ), |
| 800 | + // translators: Placeholder refers to the title of the dropdown menu (e.g. "users") | |
| 711 | 801 | sprintf( esc_html__( 'Any %s', 'stream' ), $title ) |
| 712 | 802 | ); |
| 713 | 803 | |
| 714 | 804 | $output = sprintf( |
| @@ -719,13 +809,15 @@ | ||
| 719 | 809 | $input_html |
| 720 | 810 | ); |
| 721 | 811 | |
| 722 | 812 | break; |
| 723 | - case 'rule_list' : | |
| 813 | + case 'rule_list': | |
| 814 | + $users = count_users(); | |
| 815 | + $form = new Form_Generator(); | |
| 724 | 816 | $output = '<p class="description">' . esc_html( $description ) . '</p>'; |
| 725 | 817 | |
| 726 | - $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' ) ); | |
| 727 | - $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' ) ); | |
| 728 | 820 | |
| 729 | 821 | $output .= sprintf( '<div class="tablenav top">%1$s</div>', $actions_top ); |
| 730 | 822 | $output .= '<table class="wp-list-table widefat fixed stream-exclude-list">'; |
| 731 | 823 | |
| @@ -749,13 +841,18 @@ | ||
| 749 | 841 | ); |
| 750 | 842 | |
| 751 | 843 | $exclude_rows = array(); |
| 752 | 844 | |
| 753 | - // Prepend an empty row | |
| 754 | - $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 | + } | |
| 755 | 849 | |
| 850 | + // Prepend an empty row. | |
| 851 | + $current_value['exclude_row'] = ( isset( $current_value['exclude_row'] ) ? $current_value['exclude_row'] : array() ) + array( 'helper' => '' ); | |
| 852 | + | |
| 756 | 853 | foreach ( $current_value['exclude_row'] as $key => $value ) { |
| 757 | - // Prepare values | |
| 854 | + // Prepare values. | |
| 758 | 855 | $author_or_role = isset( $current_value['author_or_role'][ $key ] ) ? $current_value['author_or_role'][ $key ] : ''; |
| 759 | 856 | $connector = isset( $current_value['connector'][ $key ] ) ? $current_value['connector'][ $key ] : ''; |
| 760 | 857 | $context = isset( $current_value['context'][ $key ] ) ? $current_value['context'][ $key ] : ''; |
| 761 | 858 | $action = isset( $current_value['action'][ $key ] ) ? $current_value['action'][ $key ] : ''; |
| @@ -765,19 +862,22 @@ | ||
| 765 | 862 | $author_or_role_values = array(); |
| 766 | 863 | $author_or_role_selected = array(); |
| 767 | 864 | |
| 768 | 865 | foreach ( $this->get_roles() as $role_id => $role ) { |
| 769 | - $args = array( 'id' => $role_id, 'text' => $role ); | |
| 770 | - $users = count_users(); | |
| 866 | + $args = array( | |
| 867 | + 'value' => $role_id, | |
| 868 | + 'text' => $role, | |
| 869 | + ); | |
| 771 | 870 | $count = isset( $users['avail_roles'][ $role_id ] ) ? $users['avail_roles'][ $role_id ] : 0; |
| 772 | 871 | |
| 773 | 872 | if ( ! empty( $count ) ) { |
| 774 | - $args['user_count'] = sprintf( _n( '1 user', '%s users', absint( $count ), 'stream' ), absint( $count ) ); | |
| 873 | + // translators: Placeholder refers to a number of users (e.g. "42") | |
| 874 | + $args['user_count'] = sprintf( _n( '%d user', '%d users', absint( $count ), 'stream' ), absint( $count ) ); | |
| 775 | 875 | } |
| 776 | 876 | |
| 777 | 877 | if ( $role_id === $author_or_role ) { |
| 778 | - $author_or_role_selected['id'] = $role_id; | |
| 779 | - $author_or_role_selected['text'] = $role; | |
| 878 | + $author_or_role_selected['value'] = $role_id; | |
| 879 | + $author_or_role_selected['text'] = $role; | |
| 780 | 880 | } |
| 781 | 881 | |
| 782 | 882 | $author_or_role_values[] = $args; |
| 783 | 883 | } |
| @@ -784,22 +884,28 @@ | ||
| 784 | 884 | |
| 785 | 885 | if ( empty( $author_or_role_selected ) && is_numeric( $author_or_role ) ) { |
| 786 | 886 | $user = new WP_User( $author_or_role ); |
| 787 | 887 | $display_name = ( 0 === $user->ID ) ? esc_html__( 'N/A', 'stream' ) : $user->display_name; |
| 788 | - $author_or_role_selected = array( 'id' => $user->ID, 'text' => $display_name ); | |
| 888 | + $author_or_role_selected = array( | |
| 889 | + 'value' => $user->ID, | |
| 890 | + 'text' => $display_name, | |
| 891 | + ); | |
| 892 | + $author_or_role_values[] = $author_or_role_selected; | |
| 789 | 893 | } |
| 790 | 894 | |
| 791 | - $author_or_role_input = sprintf( | |
| 792 | - '<input type="hidden" name="%1$s[%2$s_%3$s][%4$s][]" data-values=\'%5$s\' data-selected-id=\'%6$s\' data-selected-text=\'%7$s\' value="%6$s" class="select2-select %4$s" data-placeholder="%8$s" data-nonce="%9$s" />', | |
| 793 | - esc_attr( $option_key ), | |
| 794 | - esc_attr( $section ), | |
| 795 | - esc_attr( $name ), | |
| 796 | - 'author_or_role', | |
| 797 | - esc_attr( wp_stream_json_encode( $author_or_role_values ) ), | |
| 798 | - isset( $author_or_role_selected['id'] ) ? esc_attr( $author_or_role_selected['id'] ) : '', | |
| 799 | - isset( $author_or_role_selected['text'] ) ? esc_attr( $author_or_role_selected['text'] ) : '', | |
| 800 | - esc_html__( 'Any Author or Role', 'stream' ), | |
| 801 | - esc_attr( wp_create_nonce( 'stream_get_users' ) ) | |
| 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 | + ) | |
| 802 | 908 | ); |
| 803 | 909 | |
| 804 | 910 | // Context dropdown menu |
| 805 | 911 | $context_values = array(); |
| @@ -809,68 +915,97 @@ | ||
| 809 | 915 | $child_values = array(); |
| 810 | 916 | if ( isset( $context_data['children'] ) ) { |
| 811 | 917 | $child_values = array(); |
| 812 | 918 | foreach ( $context_data['children'] as $child_id => $child_value ) { |
| 813 | - $child_values[] = array( '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 | + ); | |
| 814 | 924 | } |
| 815 | 925 | } |
| 816 | 926 | if ( isset( $context_data['label'] ) ) { |
| 817 | - $context_values[] = array( 'id' => $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 | + ); | |
| 818 | 932 | } |
| 819 | 933 | } else { |
| 820 | - $context_values[] = array( 'id' => $context_id, 'text' => $context_data ); | |
| 934 | + $context_values[] = array( | |
| 935 | + 'value' => $context_id, | |
| 936 | + 'text' => $context_data, | |
| 937 | + ); | |
| 821 | 938 | } |
| 822 | 939 | } |
| 823 | 940 | |
| 824 | - $connector_input = sprintf( | |
| 825 | - '<input type="hidden" name="%1$s[%2$s_%3$s][%4$s][]" class="%4$s" value="%5$s">', | |
| 826 | - esc_attr( $option_key ), | |
| 827 | - esc_attr( $section ), | |
| 828 | - esc_attr( $name ), | |
| 829 | - esc_attr( 'connector' ), | |
| 830 | - esc_attr( $connector ) | |
| 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 | + ) | |
| 831 | 952 | ); |
| 832 | 953 | |
| 833 | - $context_input = sprintf( | |
| 834 | - '<input type="hidden" name="%1$s[%2$s_%3$s][%4$s][]" data-values=\'%5$s\' value="%6$s" class="select2-select with-source %4$s" data-placeholder="%7$s" data-group="%8$s" />', | |
| 835 | - esc_attr( $option_key ), | |
| 836 | - esc_attr( $section ), | |
| 837 | - esc_attr( $name ), | |
| 838 | - 'context', | |
| 839 | - esc_attr( wp_stream_json_encode( $context_values ) ), | |
| 840 | - esc_attr( $context ), | |
| 841 | - esc_html__( 'Any Context', 'stream' ), | |
| 842 | - 'connector' | |
| 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 | + ) | |
| 843 | 961 | ); |
| 844 | 962 | |
| 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 | + ); | |
| 971 | + | |
| 845 | 972 | // Action dropdown menu |
| 846 | 973 | $action_values = array(); |
| 847 | 974 | |
| 848 | 975 | foreach ( $this->get_terms_labels( 'action' ) as $action_id => $action_data ) { |
| 849 | - $action_values[] = array( 'id' => $action_id, 'text' => $action_data ); | |
| 976 | + $action_values[] = array( | |
| 977 | + 'value' => $action_id, | |
| 978 | + 'text' => $action_data, | |
| 979 | + ); | |
| 850 | 980 | } |
| 851 | 981 | |
| 852 | - $action_input = sprintf( | |
| 853 | - '<input type="hidden" name="%1$s[%2$s_%3$s][%4$s][]" data-values=\'%5$s\' value="%6$s" class="select2-select with-source %4$s" data-placeholder="%7$s" />', | |
| 854 | - esc_attr( $option_key ), | |
| 855 | - esc_attr( $section ), | |
| 856 | - esc_attr( $name ), | |
| 857 | - 'action', | |
| 858 | - esc_attr( wp_stream_json_encode( $action_values ) ), | |
| 859 | - esc_attr( $action ), | |
| 860 | - esc_html__( 'Any Action', 'stream' ) | |
| 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 | + ) | |
| 861 | 993 | ); |
| 862 | 994 | |
| 863 | 995 | // IP Address input |
| 864 | - $ip_address_input = sprintf( | |
| 865 | - '<input type="hidden" name="%1$s[%2$s_%3$s][%4$s][]" value="%5$s" class="select2-select %4$s" data-placeholder="%6$s" data-nonce="%7$s" />', | |
| 866 | - esc_attr( $option_key ), | |
| 867 | - esc_attr( $section ), | |
| 868 | - esc_attr( $name ), | |
| 869 | - 'ip_address', | |
| 870 | - esc_attr( $ip_address ), | |
| 871 | - esc_html__( 'Any IP Address', 'stream' ), | |
| 872 | - esc_attr( wp_create_nonce( 'stream_get_ips' ) ) | |
| 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 | + ) | |
| 873 | 1008 | ); |
| 874 | 1009 | |
| 875 | 1010 | // Hidden helper input |
| 876 | 1011 | $helper_input = sprintf( |
| @@ -884,18 +1019,19 @@ | ||
| 884 | 1019 | $exclude_rows[] = sprintf( |
| 885 | 1020 | '<tr class="%1$s %2$s"> |
| 886 | 1021 | <th scope="row" class="check-column">%3$s %4$s</th> |
| 887 | 1022 | <td>%5$s</td> |
| 888 | - <td>%6$s %7$s</td> | |
| 889 | - <td>%8$s</td> | |
| 1023 | + <td>%6$s %7$s %8$s</td> | |
| 890 | 1024 | <td>%9$s</td> |
| 891 | - <th scope="row" class="actions-column">%10$s</th> | |
| 1025 | + <td>%10$s</td> | |
| 1026 | + <th scope="row" class="actions-column">%11$s</th> | |
| 892 | 1027 | </tr>', |
| 893 | - ( 0 !== $key % 2 ) ? 'alternate' : '', | |
| 894 | - ( 'helper' === $key ) ? 'hidden helper' : '', | |
| 1028 | + ( 0 !== (int) $key % 2 ) ? 'alternate' : '', | |
| 1029 | + ( 'helper' === (string) $key ) ? 'hidden helper' : '', | |
| 895 | 1030 | '<input class="cb-select" type="checkbox" />', |
| 896 | 1031 | $helper_input, |
| 897 | 1032 | $author_or_role_input, |
| 1033 | + $connector_or_context_input, | |
| 898 | 1034 | $connector_input, |
| 899 | 1035 | $context_input, |
| 900 | 1036 | $action_input, |
| 901 | 1037 | $ip_address_input, |
| @@ -999,10 +1135,10 @@ | ||
| 999 | 1135 | * @param array $old_value |
| 1000 | 1136 | * @param array $new_value |
| 1001 | 1137 | */ |
| 1002 | 1138 | public function updated_option_ttl_remove_records( $old_value, $new_value ) { |
| 1003 | - $ttl_before = isset( $old_value['general_records_ttl'] ) ? (int) $old_value['general_records_ttl'] : -1; | |
| 1004 | - $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; | |
| 1005 | 1141 | |
| 1006 | 1142 | if ( $ttl_after < $ttl_before ) { |
| 1007 | 1143 | /** |
| 1008 | 1144 | * Action assists in purging when TTL is shortened |