Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP/DeprecatedParameterValues: support modern PHP + update for WP 6.1 #2174

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 126 additions & 56 deletions WordPress/Sniffs/WP/DeprecatedParameterValuesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\MessageHelper;
use PHPCSUtils\Utils\PassedParameters;
use PHPCSUtils\Utils\TextStrings;
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
use WordPressCS\WordPress\Helpers\MinimumWPVersionTrait;
Expand Down Expand Up @@ -46,97 +47,165 @@ class DeprecatedParameterValuesSniff extends AbstractFunctionParameterSniff {
* Last updated for WordPress 4.9.6.
*
* @since 1.0.0
* @since 3.0.0 The format of the value has changed to support function calls
* using named parameters.
*
* @var array Multidimensional array with parameter details.
* $target_functions = array(
* (string) Function name. => array(
* (int) Target parameter position, 1-based. => array(
* (string) Parameter value. => array(
* 'alt' => (string) Suggested alternative.
* 'version' => (int) The WordPress version when deprecated.
* (string) 'name' => (string|array) Parameter name(s),
* (string) 'values' => array(
* (string) Parameter value. => array(
* 'alt' => (string) Suggested alternative.
* 'version' => (int) The WordPress version when deprecated.
* )
* )
* )
* )
* );
*/
protected $target_functions = array(
'add_option' => array(
1 => array(
'name' => 'option',
'values' => array(
'blacklist_keys' => array(
'alt' => 'disallowed_keys',
'version' => '5.5.0',
),
'comment_whitelist' => array(
'alt' => 'comment_previously_approved',
'version' => '5.5.0',
),
),
),
),
'add_settings_field' => array(
4 => array(
'misc' => array(
'alt' => 'another settings group',
'version' => '3.0.0',
),
'privacy' => array(
'alt' => 'another settings group',
'version' => '3.5.0',
'name' => 'page',
'values' => array(
'misc' => array(
'alt' => 'another settings group',
'version' => '3.0.0',
),
'privacy' => array(
'alt' => 'another settings group',
'version' => '3.5.0',
),
),
),
),
'add_settings_section' => array(
4 => array(
'misc' => array(
'alt' => 'another settings group',
'version' => '3.0.0',
),
'privacy' => array(
'alt' => 'another settings group',
'version' => '3.5.0',
'name' => 'page',
'values' => array(
'misc' => array(
'alt' => 'another settings group',
'version' => '3.0.0',
),
'privacy' => array(
'alt' => 'another settings group',
'version' => '3.5.0',
),
),
),
),
'bloginfo' => array(
1 => array(
'home' => array(
'alt' => 'the "url" argument',
'version' => '2.2.0',
),
'siteurl' => array(
'alt' => 'the "url" argument',
'version' => '2.2.0',
),
'text_direction' => array(
'alt' => 'is_rtl()',
'version' => '2.2.0',
'name' => 'show',
'values' => array(
'home' => array(
'alt' => 'the "url" argument',
'version' => '2.2.0',
),
'siteurl' => array(
'alt' => 'the "url" argument',
'version' => '2.2.0',
),
'text_direction' => array(
'alt' => 'is_rtl()',
'version' => '2.2.0',
),
),
),
),
'get_bloginfo' => array(
1 => array(
'home' => array(
'alt' => 'the "url" argument',
'version' => '2.2.0',
),
'siteurl' => array(
'alt' => 'the "url" argument',
'version' => '2.2.0',
'name' => 'show',
'values' => array(
'home' => array(
'alt' => 'the "url" argument',
'version' => '2.2.0',
),
'siteurl' => array(
'alt' => 'the "url" argument',
'version' => '2.2.0',
),
'text_direction' => array(
'alt' => 'is_rtl()',
'version' => '2.2.0',
),
),
'text_direction' => array(
'alt' => 'is_rtl()',
'version' => '2.2.0',
),
),
'get_option' => array(
1 => array(
'name' => 'option',
'values' => array(
'blacklist_keys' => array(
'alt' => 'disallowed_keys',
'version' => '5.5.0',
),
'comment_whitelist' => array(
'alt' => 'comment_previously_approved',
'version' => '5.5.0',
),
),
),
),
'register_setting' => array(
1 => array(
'misc' => array(
'alt' => 'another settings group',
'version' => '3.0.0',
),
'privacy' => array(
'alt' => 'another settings group',
'version' => '3.5.0',
'name' => 'option_group',
'values' => array(
'misc' => array(
'alt' => 'another settings group',
'version' => '3.0.0',
),
'privacy' => array(
'alt' => 'another settings group',
'version' => '3.5.0',
),
),
),
),
'unregister_setting' => array(
1 => array(
'misc' => array(
'alt' => 'another settings group',
'version' => '3.0.0',
'name' => 'option_group',
'values' => array(
'misc' => array(
'alt' => 'another settings group',
'version' => '3.0.0',
),
'privacy' => array(
'alt' => 'another settings group',
'version' => '3.5.0',
),
),
'privacy' => array(
'alt' => 'another settings group',
'version' => '3.5.0',
),
),
'update_option' => array(
1 => array(
'name' => 'option',
'values' => array(
'blacklist_keys' => array(
'alt' => 'disallowed_keys',
'version' => '5.5.0',
),
'comment_whitelist' => array(
'alt' => 'comment_previously_approved',
'version' => '5.5.0',
),
),
),
),
Expand All @@ -156,15 +225,16 @@ class DeprecatedParameterValuesSniff extends AbstractFunctionParameterSniff {
*/
public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) {
$this->get_wp_version_from_cli();
$param_count = \count( $parameters );

foreach ( $this->target_functions[ $matched_content ] as $position => $parameter_args ) {
$found_param = PassedParameters::getParameterFromStack( $parameters, $position, $parameter_args['name'] );

// Stop if the position is higher then the total number of parameters.
if ( $position > $param_count ) {
break;
// Skip if the parameter was not found.
if ( false === $found_param ) {
continue;
}

$this->process_parameter( $matched_content, $parameters[ $position ], $parameter_args );
$this->process_parameter( $matched_content, $found_param, $parameter_args['values'] );
}
}

Expand Down
22 changes: 22 additions & 0 deletions WordPress/Tests/WP/DeprecatedParameterValuesUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,25 @@ echo bloginfo( function_name() );

get_bloginfo( 'wpurl' ); // OK.
add_settings_field( '', '', '', /* deliberately not passed */ ); // Valid for PHP 7.3+.

// Safeguard support for PHP 8.0+ named parameters.
add_settings_section( $id, $title, $callback, args: $args ); // OK, optional parameter skipped.
add_settings_field( page: $page, section: $section, id: $id, title: $title, callback: $callback ); // OK, undetermined.
add_settings_section( callback: $callback, page: 'general', section: $section, id: $id, title: $title, ); // OK, not the deprecated value.
add_settings_field( page: 'misc', section: $section, id: $id, title: $title, callback: $callback ); // Error.

get_bloginfo( filter: $filter ); // OK, optional parameter skipped.
get_bloginfo( filter: $filter, show: $show, ); // OK, undetermined.
get_bloginfo( filter: $filter, show: 'admin_email', ); // OK, not the deprecated value.
get_bloginfo( filter: $filter, show: 'text_direction', ); // Error.

// Parameter values which have been deprecated in WP 5.5. All will give an error as the `minimum_wp_version` > 5.5.
add_option('blacklist_keys');
add_option('comment_whitelist', $value);
add_option(value: $value, option: 'blacklist_keys');
get_option('blacklist_keys');
get_option('comment_whitelist', $default);
get_option(default: $default, option: 'comment_whitelist');
update_option('blacklist_keys', $value);
update_option('comment_whitelist', $value);
update_option(autoload: true, value: $value, option: 'blacklist_keys');
11 changes: 11 additions & 0 deletions WordPress/Tests/WP/DeprecatedParameterValuesUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ public function getErrorList() {
16 => 1,
17 => 1,
18 => 1,
35 => 1,
40 => 1,
43 => 1,
44 => 1,
45 => 1,
46 => 1,
47 => 1,
48 => 1,
49 => 1,
50 => 1,
51 => 1,
);
}

Expand Down