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

Add check for deprecated define variables #162

Merged
merged 3 commits into from
Sep 11, 2016
Merged

Add check for deprecated define variables #162

merged 3 commits into from
Sep 11, 2016

Conversation

ernilambar
Copy link
Member

See #152

Current limitation:

Theme's functions.php

define('HEADER_IMAGE_WIDTH',1200);
define('HEADER_IMAGE_HEIGHT',600);

Output:

REQUIRED: HEADER_IMAGE_WIDTH found in the file functions.php. Deprecated since version 3.4. Use add_theme_support( 'custom-header' ) instead.
Line 39: define('HEADER_IMAGE_WIDTH',1200);
REQUIRED: HEADER_IMAGE_HEIGHT found in the file functions.php. Deprecated since version 3.4. Use add_theme_support( 'custom-header' ) instead.
Line 40: define('HEADER_IMAGE_HEIGHT',600);
REQUIRED: HEADER_IMAGE found in the file functions.php. Deprecated since version 3.4. Use add_theme_support( 'custom-header' ) instead.
Line 39: define('HEADER_IMAGE_WIDTH',1200);
Line 40: define('HEADER_IMAGE_HEIGHT',600);

Although there is no HEADER_IMAGE variable, regex matches with HEADER_IMAGE_WIDTH. Any suggestion how to tighten the regex check?

$ret = true;

$checks = array(
array( "NO_HEADER_TEXT" => "add_theme_support( 'custom-header' )", '3.4' ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WP coding standards is to use single quotes in array instead of double.

@grappler
Copy link
Member

grappler commented Jun 8, 2016

It may be easier to extend an existing class constants.php

checkcount();
$key = key( $check );
$alt = $check[ $key ];
if ( preg_match( '/' . $key . '/', $phpfile, $matches ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could try preg_match( '/(?:'|")' . $key . '(?:'|")/', $phpfile, $matches )

@grappler grappler merged commit 323f266 into WordPress:master Sep 11, 2016
@ernilambar ernilambar deleted the 152-deprecated-define branch September 11, 2016 14:55
jrfnl added a commit to WordPress/WordPress-Coding-Standards that referenced this pull request Aug 5, 2017
This sniff is based on a sniff previously pulled to the TRT fork of WPCS as `Theme.DeprecatedConstants`.

Related issue: WPTT/WPThemeReview/issues/23
Original PR: WPTT/WPThemeReview/pull/30
Covers the list of constants found in: WordPress/theme-check/pull/162

Differences between that sniff and this one:
* This sniff is a **_lot_** more comprehensive in preventing false positives.
    The original sniff was one of the first ones I wrote and it's kind of sweet to look back at that code which I wrote over a year ago and see how much I've learned about writing sniffs in the mean time.
* The original sniff was called `Theme.DeprecatedConstants`, but in reality, most of these constants aren't officially deprecated, though their usage is discouraged and the constants being addressed are not 100% related to themes either. With that in mind, I've renamed the sniff to `WP.DiscouragedConstants` and changed the error level from Error to Warning.
    Also see: https://core.trac.wordpress.org/ticket/18298
* This version of the sniff also addresses the concerns raised in WPTT/WPThemeReview/issues/110 about themes `define`-ing any of these constants and leverages the `AbstractFunctionParameterSniff` to do so.

Includes quite extensive unit tests. More are definitely welcome, especially to prevent false positives.

I've added the sniff to the `WordPress-Extra` ruleset.

Fixes 97
Will also fix WPTT/WPThemeReview/issues/110 once the original sniff gets removed from the TRT fork and this sniff is added to the TRT ruleset instead.
jrfnl added a commit to WordPress/WordPress-Coding-Standards that referenced this pull request Aug 5, 2017
This sniff is based on a sniff previously pulled to the TRT fork of WPCS as `Theme.DeprecatedConstants`.

Related issue: WPTT/WPThemeReview/issues/23
Original PR: WPTT/WPThemeReview/pull/30
Covers the list of constants found in: WordPress/theme-check/pull/162

Differences between that sniff and this one:
* This sniff is a **_lot_** more comprehensive in preventing false positives.
    The original sniff was one of the first ones I wrote and it's kind of sweet to look back at that code which I wrote over a year ago and see how much I've learned about writing sniffs in the mean time.
* The original sniff was called `Theme.DeprecatedConstants`, but in reality, most of these constants aren't officially deprecated, though their usage is discouraged and the constants being addressed are not 100% related to themes either. With that in mind, I've renamed the sniff to `WP.DiscouragedConstants` and changed the error level from Error to Warning.
    Also see: https://core.trac.wordpress.org/ticket/18298
* This version of the sniff also addresses the concerns raised in WPTT/WPThemeReview/issues/110 about themes `define`-ing any of these constants and leverages the `AbstractFunctionParameterSniff` to do so.

Includes quite extensive unit tests. More are definitely welcome, especially to prevent false positives.

I've added the sniff to the `WordPress-Extra` ruleset.

Based on https://vip.wordpress.com/documentation/code-review-what-we-look-for/#using-theme-constants and https://core.trac.wordpress.org/ticket/18298#comment:2, I've also added the sniff to the `WordPress-VIP` ruleset. /cc @sboisvert @david-binda @ntwb

Fixes 97
Will also fix WPTT/WPThemeReview/issues/110 once the original sniff gets removed from the TRT fork and this sniff is added to the TRT ruleset instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants