Skip to content
Snippets Groups Projects
Commit eafe83e4 authored by Erick Hitter's avatar Erick Hitter
Browse files

Review feedback: extend filters to allow additions (and only additions) to...

Review feedback: extend filters to allow additions (and only additions) to blacklists and whitelists set in constants
parent 1c14761e
No related branches found
No related tags found
No related merge requests found
......@@ -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:
......
......@@ -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;
}
......@@ -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"
......
......@@ -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:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment