Skip to content

Commit

Permalink
WP Helper functions: ensure parameter names are in line with WP Core
Browse files Browse the repository at this point in the history
... for compatibility with code under test using PHP 8.0+ function calls with named arguments.

For some of these functions, WP has renamed the parameter(s) over the past year or so, for some, the names were never in line.

Includes verification of the default values used for various parameters and correcting these if needed.
  • Loading branch information
jrfnl committed May 1, 2023
1 parent 96a8944 commit 226f3b0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 56 deletions.
4 changes: 2 additions & 2 deletions inc/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ function stubTranslationFunctions()
'_nx_noop' => static function ($singular, $plural) {
return compact('singular', 'plural');
},
'translate_nooped_plural' => static function($nooped, $number) {
return ($number === 1) ? $nooped['singular'] : $nooped['plural'];
'translate_nooped_plural' => static function($nooped_plural, $count) {
return ($count === 1) ? $nooped_plural['singular'] : $nooped_plural['plural'];
},
]
);
Expand Down
20 changes: 10 additions & 10 deletions inc/wp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,30 @@ function __return_empty_string()
}

if ( ! function_exists('untrailingslashit')) {
function untrailingslashit($string)
function untrailingslashit($value)
{
return rtrim($string, '/\\');
return rtrim($value, '/\\');
}
}

if ( ! function_exists('trailingslashit')) {
function trailingslashit($string)
function trailingslashit($value)
{
return rtrim($string, '/\\').'/';
return rtrim($value, '/\\').'/';
}
}

if ( ! function_exists('user_trailingslashit')) {
function user_trailingslashit($string)
function user_trailingslashit($url)
{
return trailingslashit($string);
return trailingslashit($url);
}
}

if ( ! function_exists('absint')) {
function absint($number)
function absint($maybeint)
{
return abs((int)$number);
return abs((int)$maybeint);
}
}

Expand All @@ -102,9 +102,9 @@ function is_wp_error($thing)
}

if ( ! function_exists('wp_validate_boolean')) {
function wp_validate_boolean($var)
function wp_validate_boolean($value)
{
return (is_string($var) && (strtolower($var) === 'false')) ? false : (bool)$var;
return (is_string($value) && (strtolower($value) === 'false')) ? false : (bool)$value;
}
}

Expand Down
88 changes: 44 additions & 44 deletions inc/wp-hook-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,144 +15,144 @@
use Brain\Monkey;

if ( ! function_exists('add_action')) {
function add_action($action, $function, $priority = 10, $accepted_args = 1)
function add_action($hook_name, $callback, $priority = 10, $accepted_args = 1)
{
$args = [$function, $priority, $accepted_args];
$args = [$callback, $priority, $accepted_args];
$container = Monkey\Container::instance();
$container->hookStorage()->pushToAdded(Monkey\Hook\HookStorage::ACTIONS, $action, $args);
$container->hookExpectationExecutor()->executeAddAction($action, $args);
$container->hookStorage()->pushToAdded(Monkey\Hook\HookStorage::ACTIONS, $hook_name, $args);
$container->hookExpectationExecutor()->executeAddAction($hook_name, $args);

return true;
}
}

if ( ! function_exists('add_filter')) {
function add_filter($filter, $function, $priority = 10, $accepted_args = 1)
function add_filter($hook_name, $callback, $priority = 10, $accepted_args = 1)
{
$args = [$function, $priority, $accepted_args];
$args = [$callback, $priority, $accepted_args];
$container = Monkey\Container::instance();
$container->hookStorage()->pushToAdded(Monkey\Hook\HookStorage::FILTERS, $filter, $args);
$container->hookExpectationExecutor()->executeAddFilter($filter, $args);
$container->hookStorage()->pushToAdded(Monkey\Hook\HookStorage::FILTERS, $hook_name, $args);
$container->hookExpectationExecutor()->executeAddFilter($hook_name, $args);

return true;
}
}

if ( ! function_exists('do_action')) {
function do_action($action, ...$args)
function do_action($hook_name, ...$args)
{
$container = Monkey\Container::instance();
$container->hookStorage()->pushToDone(Monkey\Hook\HookStorage::ACTIONS, $action, $args);
$container->hookExpectationExecutor()->executeDoAction($action, $args);
$container->hookStorage()->pushToDone(Monkey\Hook\HookStorage::ACTIONS, $hook_name, $args);
$container->hookExpectationExecutor()->executeDoAction($hook_name, $args);
}
}

if ( ! function_exists('do_action_ref_array')) {
function do_action_ref_array($action, array $args)
function do_action_ref_array($hook_name, array $args)
{
$container = Monkey\Container::instance();
$container->hookStorage()->pushToDone(Monkey\Hook\HookStorage::ACTIONS, $action, $args);
$container->hookExpectationExecutor()->executeDoAction($action, $args);
$container->hookStorage()->pushToDone(Monkey\Hook\HookStorage::ACTIONS, $hook_name, $args);
$container->hookExpectationExecutor()->executeDoAction($hook_name, $args);
}
}

if ( ! function_exists('do_action_deprecated')) {
function do_action_deprecated($action, array $args, $version, $replacement, $message = null)
function do_action_deprecated($hook_name, array $args, $version, $replacement = '', $message = '')
{
$container = Monkey\Container::instance();
$container->hookStorage()->pushToDone(Monkey\Hook\HookStorage::ACTIONS, $action, $args);
$container->hookExpectationExecutor()->executeDoAction($action, $args);
$container->hookStorage()->pushToDone(Monkey\Hook\HookStorage::ACTIONS, $hook_name, $args);
$container->hookExpectationExecutor()->executeDoAction($hook_name, $args);
}
}

if ( ! function_exists('apply_filters')) {
function apply_filters($filter, ...$args)
function apply_filters($hook_name, ...$args)
{
$container = Monkey\Container::instance();
$container->hookStorage()->pushToDone(Monkey\Hook\HookStorage::FILTERS, $filter, $args);
$container->hookStorage()->pushToDone(Monkey\Hook\HookStorage::FILTERS, $hook_name, $args);

return $container->hookExpectationExecutor()->executeApplyFilters($filter, $args);
return $container->hookExpectationExecutor()->executeApplyFilters($hook_name, $args);
}
}

if ( ! function_exists('apply_filters_ref_array')) {
function apply_filters_ref_array($filter, array $args)
function apply_filters_ref_array($hook_name, array $args)
{
$container = Monkey\Container::instance();
$container->hookStorage()->pushToDone(Monkey\Hook\HookStorage::FILTERS, $filter, $args);
$container->hookStorage()->pushToDone(Monkey\Hook\HookStorage::FILTERS, $hook_name, $args);

return $container->hookExpectationExecutor()->executeApplyFilters($filter, $args);
return $container->hookExpectationExecutor()->executeApplyFilters($hook_name, $args);
}
}

if ( ! function_exists('apply_filters_deprecated')) {
function apply_filters_deprecated($filter, array $args, $version, $replacement, $message = null)
function apply_filters_deprecated($hook_name, array $args, $version, $replacement = '', $message = '')
{
$container = Monkey\Container::instance();
$container->hookStorage()->pushToDone(Monkey\Hook\HookStorage::FILTERS, $filter, $args);
$container->hookStorage()->pushToDone(Monkey\Hook\HookStorage::FILTERS, $hook_name, $args);

return $container->hookExpectationExecutor()->executeApplyFilters($filter, $args);
return $container->hookExpectationExecutor()->executeApplyFilters($hook_name, $args);
}
}

if ( ! function_exists('has_action')) {
function has_action($action, $callback = null)
function has_action($hook_name, $callback = false)
{
return Monkey\Actions\has($action, $callback);
return Monkey\Actions\has($hook_name, $callback);
}
}

if ( ! function_exists('has_filter')) {
function has_filter($filter, $callback = null)
function has_filter($hook_name, $callback = false)
{
return Monkey\Filters\has($filter, $callback);
return Monkey\Filters\has($hook_name, $callback);
}
}

if ( ! function_exists('did_action')) {
function did_action($action)
function did_action($hook_name)
{
return Monkey\Actions\did($action);
return Monkey\Actions\did($hook_name);
}
}

if ( ! function_exists('remove_action')) {
function remove_action($action, $function, $priority = 10)
function remove_action($hook_name, $callback, $priority = 10)
{
$container = Monkey\Container::instance();
$storage = $container->hookStorage();
$args = [$function, $priority];
$args = [$callback, $priority];

$container->hookExpectationExecutor()->executeRemoveAction($action, $args);
$container->hookExpectationExecutor()->executeRemoveAction($hook_name, $args);

return $storage->removeFromAdded(Monkey\Hook\HookStorage::ACTIONS, $action, $args);
return $storage->removeFromAdded(Monkey\Hook\HookStorage::ACTIONS, $hook_name, $args);
}
}

if ( ! function_exists('remove_filter')) {
function remove_filter($filter, $function, $priority = 10)
function remove_filter($hook_name, $callback, $priority = 10)
{
$container = Monkey\Container::instance();
$storage = $container->hookStorage();
$args = [$function, $priority];
$args = [$callback, $priority];

$container->hookExpectationExecutor()->executeRemoveFilter($filter, $args);
$container->hookExpectationExecutor()->executeRemoveFilter($hook_name, $args);

return $storage->removeFromAdded(Monkey\Hook\HookStorage::FILTERS, $filter, $args);
return $storage->removeFromAdded(Monkey\Hook\HookStorage::FILTERS, $hook_name, $args);
}
}

if ( ! function_exists('doing_action')) {
function doing_action($action)
function doing_action($hook_name = null)
{
return Monkey\Actions\doing($action);
return Monkey\Actions\doing($hook_name);
}
}

if ( ! function_exists('doing_filter')) {
function doing_filter($filter)
function doing_filter($hook_name = null)
{
return Monkey\Filters\doing($filter);
return Monkey\Filters\doing($hook_name);
}
}

Expand Down

0 comments on commit 226f3b0

Please sign in to comment.