-
Notifications
You must be signed in to change notification settings - Fork 38
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
Deprecated WordPress Functions #77
Conversation
* latest 4 versions and ERRORS before that. | ||
*/ | ||
$current_wp_version = 4.6; | ||
$cut_off = $current_wp_version - 0.3; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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' ), |
There was a problem hiding this comment.
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 ) ) { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
563cd1d
to
6671823
Compare
There was a problem hiding this 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' ), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ) { |
There was a problem hiding this comment.
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! 😄
I have started to move this to a pull request upstream as this can be useful for other projects. WordPress/WordPress-Coding-Standards#633 |
Initial commit.
Issue #69