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

Deprecated WordPress Functions #77

Merged
merged 1 commit into from
Mar 25, 2017
Merged

Deprecated WordPress Functions #77

merged 1 commit into from
Mar 25, 2017

Conversation

khacoder
Copy link
Contributor

Initial commit.
Issue #69

* latest 4 versions and ERRORS before that.
*/
$current_wp_version = 4.6;
$cut_off = $current_wp_version - 0.3;
Copy link
Member

@grappler grappler Sep 18, 2016

Choose a reason for hiding this comment

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

This is a cool feature. This should also be a public variable too as other projects may have a different minimum supported version. What about naming the variable $num_supported_versions?

* The current cut off is 3 versions back, meaning WARNINGS for the
* latest 4 versions and ERRORS before that.
*/
$current_wp_version = 4.6;
Copy link
Member

Choose a reason for hiding this comment

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

It would be good if we could define the version in the XML file or pass the variable via the command line. We can do this by defining a public variable in the class. e.g. https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/blob/develop/WordPress/Sniffs/WP/I18nSniff.php#L40


$checks = array(
// start wp-includes deprecated.
array( 'get_postdata' => 'get_post()', '1.5.1' ),
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to add spacing in between the arguments so that it is a bit easier to read. e.g.

array( 'get_postdata' => 'get_post()', '1.5.1' ),
array( 'start_wp'     => 'the Loop',   '1.5' ),

$key = key( $check );
$alt = $check[ $key ];
if ( trim( $token['content'] ) === $key ) {
if ( intval( $check[0] < $cut_off ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

We could simplify and reduce the amount of code by doing something like this:

if ( empty( $alt ) ) {
    $notice = $key . '() is deprecated since version ' . $check[0] . ', please find an alternative.';
} else {
    $notice = $key . '() is deprecated since version ' . $check[0] . ', ' . $alt . 'could be used as a replacement.';
}
if ( intval( $check[0] < $cut_off ) ) {
    $phpcsFile->addError( $notice , $stackPtr, 'DeprecatedWPFunctions' );
} else {
    $phpcsFile->addWarning( $notice , $stackPtr, 'DeprecatedWPFunctions' );
}

@@ -0,0 +1,234 @@
<?php
get_postdata();
Copy link
Member

Choose a reason for hiding this comment

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

Please add $this->get_link() as the check is falsely giving an error for it.

Copy link
Contributor

@JDGrimes JDGrimes left a comment

Choose a reason for hiding this comment

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

The array of deprecated functions can be restructured to provide much better performance.


$checks = array(
// start wp-includes deprecated.
array( 'get_postdata' => 'get_post()', '1.5.1' ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Really, the function names should be the keys in the main array, like this:

$checks = array(
     'get_postdata' => array( 'version' => '1.5.1', 'alt' => 'get_post()` ),
);

This will allow you to check for the existence of a function within the array much more easily.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, because this is such a large array, it may become expensive to re-declare it each time the function is called. It should be moved into a class property instead.

array( 'popuplinks' => '', '4.5' ),
array( 'get_currentuserinfo' => 'wp_get_current_user', '4.5' ),
);
foreach ( $checks as $alt => $check ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

By changing the format of the array, here you wouldn't need to loop through all of the functions (which will become terrible for performance). Instead, you can just do something like:

if ( isset( $checks[ trim( $token['content'] ) ] ) ) {
     //...
}

No need for a loop! 😄

@grappler
Copy link
Member

I have started to move this to a pull request upstream as this can be useful for other projects. WordPress/WordPress-Coding-Standards#633

@grappler grappler merged commit 47f447c into WPTT:feature/theme-review-sniffs Mar 25, 2017
@grappler grappler deleted the feature/deprecated-wp-functions branch March 25, 2017 19:55
@jrfnl jrfnl added this to the 0.1.0 milestone Aug 20, 2018
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.

4 participants