From eafe83e49c1a4f29cab1b01b58629dc97067ff84 Mon Sep 17 00:00:00 2001 From: Erick Hitter <git-contrib@ethitter.com> Date: Mon, 25 Sep 2017 13:46:18 -0700 Subject: [PATCH] Review feedback: extend filters to allow additions (and only additions) to blacklists and whitelists set in constants --- README.md | 4 ++-- includes/functions.php | 29 +++++++++++++++++++++-- languages/wp-cli-cron-control-offload.pot | 2 +- readme.txt | 4 ++-- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7107cd1..79db34c 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Yes, using the `wp_cli_cron_control_offload_is_command_allowed` filter. Note tha ### Can commands be blocked or blacklisted? ### -Yes, using either the `WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_BLACKLIST` constant or the `wp_cli_cron_control_offload_command_blacklist` filter. If defined, the constant takes precedence and the filter is ignored. +Yes, using either the `WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_BLACKLIST` constant or the `wp_cli_cron_control_offload_command_blacklist` filter. If defined, the constant takes precedence and the filter is only able to supplement the constant's blacklist. Regardless of whether the constant or filter is used, either should provide an array of top-level commands to permit: @@ -60,7 +60,7 @@ array( ### Can commands be restricted or whitelisted? ### -Yes, using either the `WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_WHITELIST` constant or the `wp_cli_cron_control_offload_command_whitelist` filter. If defined, the constant takes precedence and the filter is ignored. +Yes, using either the `WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_WHITELIST` constant or the `wp_cli_cron_control_offload_command_whitelist` filter. If defined, the constant takes precedence and the filter is only able to supplement the constant's whitelist. Regardless of whether the constant or filter is used, either should provide an array of top-level commands to block: diff --git a/includes/functions.php b/includes/functions.php index 4ef9280..566ef87 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -106,7 +106,7 @@ function command_is_whitelisted( $whitelisted, $command ) { */ function get_command_whitelist() { if ( defined( 'WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_WHITELIST' ) && is_array( \WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_WHITELIST ) ) { - return \WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_WHITELIST; + return _filter_list_allow_only_additions( \WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_WHITELIST, 'wp_cli_cron_control_offload_command_whitelist' ); } return apply_filters( 'wp_cli_cron_control_offload_command_whitelist', array() ); @@ -119,7 +119,7 @@ function get_command_whitelist() { */ function get_command_blacklist() { if ( defined( 'WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_BLACKLIST' ) && is_array( \WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_BLACKLIST ) ) { - return \WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_BLACKLIST; + return _filter_list_allow_only_additions( \WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_BLACKLIST, 'wp_cli_cron_control_offload_command_blacklist' ); } return apply_filters( 'wp_cli_cron_control_offload_command_blacklist', array() ); @@ -246,3 +246,28 @@ function _assoc_arg_array_to_string( $assoc_arg ) { return sprintf( '--%1$s=%2$s', $assoc_arg[0], $assoc_arg[1] ); } } + +/** + * Allow whitelist or blacklist to be filtered, permitting ONLY additions + * + * @param array $constant List value from constant, to be added to. + * @param string $filter_tag String for list filter. + * @return array + */ +function _filter_list_allow_only_additions( $constant, $filter_tag ) { + $list = $constant; + $list = array_values( $list ); // Keys are irrelevant, and dropping them reinforces the additive nature of the following filter. + + $additional = apply_filters( $filter_tag, array(), $list ); + + if ( ! is_array( $additional ) || empty( $additional ) ) { + return $constant; + } + + $additional = array_values( $additional ); // Stop any funny business with string keys. + + $list = array_merge( $list, $additional ); + $list = array_unique( $list, SORT_STRING ); // Force type conversion to retain value from constant if filter tries funny business. + + return empty( $list ) ? $constant : $list; +} diff --git a/languages/wp-cli-cron-control-offload.pot b/languages/wp-cli-cron-control-offload.pot index 75ed636..de27baf 100644 --- a/languages/wp-cli-cron-control-offload.pot +++ b/languages/wp-cli-cron-control-offload.pot @@ -5,7 +5,7 @@ msgstr "" "Project-Id-Version: WP-CLI Cron Control Offload 0.1.0\n" "Report-Msgid-Bugs-To: " "https://wordpress.org/support/plugin/wp-cli-cron-control-offload\n" -"POT-Creation-Date: 2017-09-25 20:14:10+00:00\n" +"POT-Creation-Date: 2017-09-25 20:41:05+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" diff --git a/readme.txt b/readme.txt index c14b160..ca11bd6 100644 --- a/readme.txt +++ b/readme.txt @@ -45,7 +45,7 @@ Yes, using the `wp_cli_cron_control_offload_is_command_allowed` filter. Note tha = Can commands be blocked or blacklisted? = -Yes, using either the `WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_BLACKLIST` constant or the `wp_cli_cron_control_offload_command_blacklist` filter. If defined, the constant takes precedence and the filter is ignored. +Yes, using either the `WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_BLACKLIST` constant or the `wp_cli_cron_control_offload_command_blacklist` filter. If defined, the constant takes precedence and the filter is only able to supplement the constant's blacklist. Regardless of whether the constant or filter is used, either should provide an array of top-level commands to permit: @@ -60,7 +60,7 @@ array( = Can commands be restricted or whitelisted? = -Yes, using either the `WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_WHITELIST` constant or the `wp_cli_cron_control_offload_command_whitelist` filter. If defined, the constant takes precedence and the filter is ignored. +Yes, using either the `WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_WHITELIST` constant or the `wp_cli_cron_control_offload_command_whitelist` filter. If defined, the constant takes precedence and the filter is only able to supplement the constant's whitelist. Regardless of whether the constant or filter is used, either should provide an array of top-level commands to block: -- GitLab