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

PHPCS code formatting fixes #1534

Merged
merged 58 commits into from
Jul 2, 2018
Merged

PHPCS code formatting fixes #1534

merged 58 commits into from
Jul 2, 2018

Conversation

jom
Copy link
Member

@jom jom commented Jun 27, 2018

1.31.1 (possibly bumping to 1.32.0) has many input/string escaping changes already from #1495. Ideally, none of these will change/break functionality. However, it does require us to do pretty thorough testing for this release. Because of that, I'm proposing these code formatting changes to get things cleaned up.

The vast majority of this PR fixes these common PHPCS errors:

  • Whitespace changes.
  • Adds hints for translators: when placeholders are used in strings.
  • Logic/yoda swaps.
  • Formatted changes to minimize unnecessary <?php ... ?> blocks.

Additionally, the PHPCS errors that might change some behavior:

  • Continuing on the escaping work from Adds additional escaping throughout plugin (VIP) #1495.
  • Remove extract() wherever possible. All uses have been removed except for get_job_manager_template(), where it is difficult to remove.
    • I don't anticipate this breaking much functionality. The possible exception to this is the [jobs] shortcode. I'm now using/passing along the cleaned up version of all the attributes. If attributes were mixed/strings before but then are normalized to arrays within the function, those new values are always passed.

This cleans up all errors using the rules in phpcs.xml.dist config file. We are, however, ignoring three rules that will involve more significant code (WordPress.CSRF.NonceVerification.NoNonceVerification) and documentation (Squiz.Commenting.FileComment.Missing and Squiz.Commenting.FunctionComment.MissingParamComment) changes. I'll be removing those from the config post-merge so that new issues are caught in a pre-commit hook (to be added soon).

@jom jom added the [Type] Maintenance Changes to non-production files label Jun 27, 2018
@jom jom changed the title [WIP] Beast of a PHPCS fixes [WIP] Safe PHPCS code formatting fixes Jun 27, 2018
@@ -253,17 +270,27 @@ public static function input_file( $key, $field ) {
}
?>
<p class="form-field">
<label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( wp_strip_all_tags( $field['label'] ) ) ; ?>: <?php if ( ! empty( $field['description'] ) ) : ?><span class="tips" data-tip="<?php echo esc_attr( $field['description'] ); ?>">[?]</span><?php endif; ?></label>
<label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( wp_strip_all_tags( $field['label'] ) ); ?>:
<?php
Copy link
Member Author

Choose a reason for hiding this comment

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

Auto-fixes in this file look odd.

@jom jom added this to the 1.31.1 milestone Jun 29, 2018
@jom jom changed the title [WIP] Safe PHPCS code formatting fixes PHPCS code formatting fixes Jul 1, 2018
@jom jom requested a review from alexsanford July 1, 2018 13:51
Copy link
Contributor

@alexsanford alexsanford left a comment

Choose a reason for hiding this comment

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

Phew! This was a big one! 😅

)
);

if ( is_wp_error( $response ) || empty( $response['body'] ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic is no longer correct. A non-JSON error will not return a WP_Error here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in c7735d5

<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=job_listing&page=job-manager-addons' ) ); ?>" class="nav-tab
<?php
if ( ! isset( $_GET['section'] ) || 'helper' !== $_GET['section'] ) {
echo ' nav-tab-active'; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this really make it through linting? I would think that the } on the same line would be a no-no.

Copy link
Member Author

Choose a reason for hiding this comment

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

It does, somehow... fixed in a3b335e.

'date_format' => _x( 'yy-mm-dd', 'Date format for jQuery datepicker.', 'wp-job-manager' )
) );
wp_localize_script(
'job_manager_datepicker_js', 'job_manager_datepicker',
Copy link
Contributor

Choose a reason for hiding this comment

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

These should probably be on separate lines.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in 2a77dfe


if ( ! $terms ) {
return;
}

echo "<select name='job_listing_category' id='dropdown_job_listing_category'>";
echo '<option value="" ' . selected( isset( $_GET['job_listing_category'] ) ? $_GET['job_listing_category'] : '', '', false ) . '>' . esc_html__( 'Select category', 'wp-job-manager' ) . '</option>';
echo $walker->walk( $terms, 0, $r );
echo "</select>";
echo wp_kses_post( $walker->walk( $terms, 0, $r ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know much about what the Walker does. Should we be worried about breaking people's sites because of this sanitization?

Copy link
Member Author

Choose a reason for hiding this comment

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

Walker, hilariously, just outputs <option> tags. I thought there was a core function for this that we might want to replace this with.

);

echo str_replace(' id=', " data-placeholder='" . __( 'Select a page&hellip;', 'wp-job-manager' ) . "' id=", wp_dropdown_pages( $args ) );
echo str_replace( ' id=', " data-placeholder='" . __( 'Select a page&hellip;', 'wp-job-manager' ) . "' id=", wp_dropdown_pages( $args ) ); // WPCS: XSS ok.
Copy link
Contributor

Choose a reason for hiding this comment

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

XSS is not ok! 😉

We should escape the translated string.

$response = $this->get_plugin_version( $plugin_data['_filename'] );
if ( $response ) {
// If there is a new version, modify the transient to reflect an update is available.
if ( false !== $response
Copy link
Contributor

Choose a reason for hiding this comment

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

This check is redundant.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in 235549c

'search_keywords' => '',
'job_types' => array( 'potato' ),
)
),
);
$results['potato_coffee'] = array(
Copy link
Contributor

Choose a reason for hiding this comment

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

Potato Coffee is not a thing.

@@ -492,7 +507,7 @@ function wp_job_manager_notify_new_user( $user_id, $password ) {
global $wp_version;

if ( version_compare( $wp_version, '4.3.1', '<' ) ) {
wp_new_user_notification( $user_id, $password );
wp_new_user_notification( $user_id, $password ); // phpcs:ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

What's going on here? What are we ignoring?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the explicit rule. We're skipping the error because the version check catches the change of signature in this function.

7d2a6f5

*/
function get_job_manager_template( $template_name, $args = array(), $template_path = 'job_manager', $default_path = '' ) {
if ( $args && is_array( $args ) ) {
extract( $args );
// Please, forgive us.
extract( $args ); // phpcs:ignore WordPress.Functions.DontExtract.extract_extract
Copy link
Contributor

Choose a reason for hiding this comment

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

🙀

if( ! empty( $video ) ){
// FV Wordpress Flowplayer Support for advanced video formats
if ( ! empty( $video ) ) {
// FV WordPress Flowplayer Support for advanced video formats.
Copy link
Contributor

Choose a reason for hiding this comment

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

Most important linter rule...

@jom jom merged commit 4a50eb2 into master Jul 2, 2018
@jom jom deleted the change/phpcs-fixes branch July 2, 2018 09:26
@jom jom mentioned this pull request Jul 4, 2018
6 tasks
Copy link
Collaborator

@tripflex tripflex left a comment

Choose a reason for hiding this comment

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

Looks like this commit has broken priority sorting with the force of using intval on priorities ... a lot of users use decimals to allow easily sorting fields, i think we should change this back or use float

@@ -237,10 +237,10 @@ public function get_fields( $key ) {
* @return int
*/
protected function sort_by_priority( $a, $b ) {
if ( $a['priority'] == $b['priority'] ) {
if ( intval( $a['priority'] ) === intval( $b['priority'] ) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This breaks priority sorting when using decimals (which is very popular), think we should change this back or switch to floatval instead of intval

return 0;
}
return ( $a['priority'] < $b['priority'] ) ? -1 : 1;
return ( intval( $a['priority'] ) < intval( $b['priority'] ) ) ? -1 : 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as above, breaks priority sorting with decimals

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Maintenance Changes to non-production files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants