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 Fixes and Collapsable section updates. #462

Merged
merged 8 commits into from
Dec 19, 2024

Conversation

krugazul
Copy link
Collaborator

@krugazul krugazul commented Dec 19, 2024

PHPCS Fixes and Collapsable section updates for the single tour, accommodation and destination

Summary by CodeRabbit

  • New Feature: Introduced responsive CSS adjustments for smaller screens, enhancing readability and user experience on mobile devices.
  • Refactor: Updated JavaScript configuration for the lsx-itinerary-wrapper class and improved function closure.
  • Security: Enhanced data security by implementing sanitization and unslash input data using wp_unslash and sanitize_text_field functions. Improved redirection process with wp_safe_redirect.
  • Chore: Performed PHPCS fixes and updates to a collapsible section. Removed unused form retrieval functions related to various plugins.
  • Style: Added phpcs:ignoreFile directives to bypass PHPCS checks for non-enqueued image functions in several PHP files.
  • Test: Added // phpcs:disable and // phpcs:enable comments to ignore WordPress.DB sniff for a database query in the lsx_to_item_has_children function.

@krugazul krugazul added the [Type] Enhancement A suggestion for improvement label Dec 19, 2024
@krugazul krugazul added this to the 2.0.0 milestone Dec 19, 2024
@krugazul krugazul self-assigned this Dec 19, 2024
Copy link

github-actions bot commented Dec 19, 2024

Image description CodeRabbit

Walkthrough

This update introduces responsive CSS adjustments for better mobile readability, JavaScript enhancements for collapsible sections, and security improvements through data sanitization in PHP. It also includes PHPCS fixes and removal of unused functions, while ignoring certain PHPCS checks where necessary.

Changes

Files Summary
assets/css/scss/_collapse.scss, assets/css/style.css, assets/css/scss/_update-fixes.scss, assets/css/style.scss Introduced responsive CSS adjustments for smaller screens, including styling changes for toggle buttons, SVG icons, and layout properties. Removed CSS styling related to WP 6.3 fixes.
assets/js/blocks/tour.js, assets/js/src/custom.js Added a new property tagName: "section" for the lsx-itinerary-wrapper class. Removed commented-out code and ensured proper closure of a function.
includes/classes/admin/class-settings.php, includes/classes/legacy/class-admin.php, includes/partials/navigation.php Improved security by sanitizing and unslashing input data using wp_unslash and sanitize_text_field. Updated redirection process to use wp_safe_redirect. Verified nonce and handled meta fields during saving.
includes/classes/blocks/class-registration.php, includes/classes/legacy/class-maps.php, includes/classes/legacy/class-tour-operator.php Made PHPCS fixes and updates to a collapsible section. Updated user agent determination logic in is_a_bot function. Removed functions related to form retrieval for various plugins.
includes/patterns/accommodation-card.php, includes/patterns/destination-card.php, includes/patterns/gallery.php, includes/patterns/itinerary-list.php, includes/patterns/room-card.php, includes/patterns/tour-card.php, includes/patterns/travel-information.php Added a phpcs:ignoreFile directive to bypass PHPCS checks for non-enqueued image functions.
includes/template-tags/helpers.php Added comments to ignore WordPress.DB sniff for a database query in the lsx_to_item_has_children function.

"In the land of code, where logic is king,
A rabbit hopped by, making updates sing 🎵.
With CSS so sleek, and JavaScript neat,
Security tightened, oh what a feat! 🎉
PHPCS fixes, unused functions gone,
The rabbit hops on, at the break of dawn 🌅."


Uplevel your code reviews with CodeRabbit Pro

CodeRabbit Pro

If you like this project, please support us by purchasing the Pro version. The Pro version has advanced context, superior noise reduction and several proprietary improvements compared to the open source version. Moreover, CodeRabbit Pro is free for open source projects.

@krugazul krugazul merged commit 266c72d into 2.0-beta-3 Dec 19, 2024
3 of 5 checks passed
@krugazul krugazul deleted the collapsible-sections branch December 19, 2024 20:42
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Image description CodeRabbit

Commits Files that changed from the base of the PR and between 426fc4b and 5fc203b commits.
Files selected (22)
  • assets/css/scss/_collapse.scss (1)
  • assets/css/scss/_update-fixes.scss (1)
  • assets/css/scss/layout/_single-sections.scss (1)
  • assets/css/style.css (1)
  • assets/css/style.scss (1)
  • assets/js/blocks/query-loops.js (28)
  • assets/js/blocks/tour.js (1)
  • assets/js/src/custom.js (2)
  • includes/classes/admin/class-settings.php (4)
  • includes/classes/blocks/class-registration.php (1)
  • includes/classes/legacy/class-admin.php (3)
  • includes/classes/legacy/class-maps.php (1)
  • includes/classes/legacy/class-tour-operator.php (2)
  • includes/partials/navigation.php (1)
  • includes/patterns/accommodation-card.php (1)
  • includes/patterns/destination-card.php (1)
  • includes/patterns/gallery.php (1)
  • includes/patterns/itinerary-list.php (1)
  • includes/patterns/room-card.php (1)
  • includes/patterns/tour-card.php (1)
  • includes/patterns/travel-information.php (1)
  • includes/template-tags/helpers.php (2)
Files not summarized due to errors (2)
  • assets/css/scss/layout/_single-sections.scss (diff tokens exceeds limit)
  • assets/js/blocks/query-loops.js (diff tokens exceeds limit)
Files not reviewed due to errors (5)
  • assets/css/scss/_collapse.scss (no response)
  • assets/css/scss/layout/_single-sections.scss (no response)
  • assets/js/blocks/query-loops.js (no response)
  • includes/classes/admin/class-settings.php (no response)
  • includes/partials/navigation.php (no response)
Files skipped from review due to trivial changes (7)
  • includes/patterns/accommodation-card.php
  • includes/patterns/destination-card.php
  • includes/patterns/gallery.php
  • includes/patterns/itinerary-list.php
  • includes/patterns/room-card.php
  • includes/patterns/tour-card.php
  • includes/patterns/travel-information.php
Review comments generated (13)
  • Review: 11
  • LGTM: 2

Tips

Chat with Image description CodeRabbit Bot (@coderabbitai)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging @coderabbitai in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Pausing incremental reviews

  • Add @coderabbitai: ignore anywhere in the PR description to pause further reviews from the bot.

@@ -5,6 +5,7 @@
@import 'scss/icons';
@import 'scss/additional';
@import 'scss/slider';
@import 'scss/collapse';

Choose a reason for hiding this comment

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

Image description CodeRabbit

The import statement for 'scss/collapse' has been added. Ensure that the file 'collapse.scss' exists in the 'scss' directory and it doesn't contain any syntax errors or undefined variables/mixins. If there are dependencies between SCSS files, make sure they are imported in the correct order.

+ @import 'scss/collapse';

Comment on lines +427 to +465
@media (min-width: 800px) {
.single-tour-operator .toggle-button {
display: none;
}
}
/* Optional: Responsive adjustments for smaller screens */
@media (max-width: 768px) {
.single-tour-operator {
/* Styling for the toggle button */
/* Styling for the SVG icons */
/* Collapsed state for the second wp-block-group */
}
.single-tour-operator .toggle-button {
display: inline-flex;
align-items: center;
padding: 5px;
background: none;
border: none;
cursor: pointer;
font-size: 14px;
color: var(--wp--preset--color--contrast);
}
.single-tour-operator .toggle-icon {
width: 16px;
height: 16px;
fill: var(--wp--preset--color--contrast);
margin-left: 5px;
}
.single-tour-operator section.wp-block-group .wp-block-group.collapsed {
display: none; /* Hide the content */
}
.fast-facts-wrapper > .wp-block-group {
flex-wrap: wrap; /* Allow wrapping for better readability */
gap: 5px;
}
.fast-facts-wrapper > .wp-block-group .wp-block-group.is-layout-flex:last-child {
flex-wrap: wrap; /* Allow text to wrap on smaller screens */
}
}

Choose a reason for hiding this comment

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

Image description CodeRabbit

The CSS changes for responsive design look good. However, there are comments indicating that there should be styles applied to .single-tour-operator within the @media (max-width: 768px) query, but no styles are present. If these styles are not needed, consider removing the comments to avoid confusion.

-    /* Styling for the toggle button */
-    /* Styling for the SVG icons */
-    /* Collapsed state for the second wp-block-group */

If they are needed, please add the appropriate styles.

@@ -396,7 +396,7 @@ if ( window.location.hash ) {
});
}
});
});
});*/

Choose a reason for hiding this comment

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

Image description CodeRabbit

The commented-out code has been removed, which is a good practice for maintaining clean and readable code. However, please ensure that this removal does not affect any functionality or dependencies in the code.

- 	});*/
+ 	

@@ -444,6 +444,6 @@
});
}
});
});*/
});

Choose a reason for hiding this comment

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

Image description CodeRabbit

The closing brackets and parentheses have been uncommented. This seems to be a correction of the previous state where it was mistakenly commented out. Please ensure that this change aligns with the intended structure of your JavaScript code.

-	});*/
+	});

@@ -102,6 +102,7 @@ public function enqueue_admin_stylescripts( $hook ) {
}

//deregister scripts
// @phpcs:ignore WordPress.Security.NonceVerification.Recommended

Choose a reason for hiding this comment

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

Image description CodeRabbit

The @phpcs:ignore directive is used to ignore the nonce verification check. However, it's important to note that ignoring this check can lead to potential security vulnerabilities as nonces are used in WordPress to prevent CSRF attacks. It would be better to verify the nonce before using the $_GET['post'] value.

- // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
- if ( isset( $_GET['post'] ) && in_array( get_post_type( $_GET['post'] ), array( 'team', 'special', 'review', 'destination', 'accommodation', 'tour' ) ) ) {
+ if ( isset( $_GET['post'] ) && wp_verify_nonce($_REQUEST['_wpnonce'], 'my-nonce-action') && in_array( get_post_type( $_GET['post'] ), array( 'team', 'special', 'review', 'destination', 'accommodation', 'tour' ) ) ) {

Comment on lines +298 to +300
// @phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_POST['tagline'] ) && ! empty( $_POST['tagline'] ) ) {
$meta = sanitize_text_field( $_POST['tagline'] );
$meta = sanitize_text_field( wp_unslash( $_POST['tagline'] ) );

Choose a reason for hiding this comment

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

Image description CodeRabbit

Again, ignoring nonce verification checks can lead to potential security vulnerabilities. It's recommended to verify the nonce before using the $_POST['tagline'] value.

- // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
- if ( isset( $_POST['tagline'] ) && ! empty( $_POST['tagline'] ) ) {
+ if ( isset( $_POST['tagline'] ) && wp_verify_nonce($_REQUEST['_wpnonce'], 'my-nonce-action') && ! empty( $_POST['tagline'] ) ) {

Please replace 'my-nonce-action' with the appropriate nonce action for your application.

Comment on lines 587 to +593
if ( isset( $_GET['debug_bot'] ) ) {
$user_agent = sanitize_text_field( $_GET['debug_bot'] );
} else {
$user_agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] );
// @phpcs:ignore WordPress.Security.NonceVerification.Recommended
$user_agent = sanitize_text_field( wp_unslash( $_GET['debug_bot'] ) );
// @phpcs:ignore WordPress.Security.NonceVerification.Recommended
} else if ( isset( $_GET['HTTP_USER_AGENT'] ) ) {
// @phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
$user_agent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );

Choose a reason for hiding this comment

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

Image description CodeRabbit

There seems to be a misunderstanding in the use of $_GET and $_SERVER superglobals. The HTTP_USER_AGENT is not typically found in the $_GET array, but rather in the $_SERVER array. Also, it's important to validate and sanitize all user inputs, including those from $_SERVER['HTTP_USER_AGENT']. Here's how you can correct this:

- if ( isset( $_GET['debug_bot'] ) ) {
-     // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
-     $user_agent = sanitize_text_field( wp_unslash( $_GET['debug_bot'] ) );
-     // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
- } else if ( isset( $_GET['HTTP_USER_AGENT'] ) ) {
-     // @phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
-     $user_agent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
+ if ( isset( $_GET['debug_bot'] ) ) {
+     $user_agent = sanitize_text_field( wp_unslash( $_GET['debug_bot'] ) );
+ } else if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
+     $user_agent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
  }

Please note that ignoring PHPCS warnings related to nonce verification and input sanitization might lead to security vulnerabilities. It would be better to address these issues directly instead of suppressing the warnings.

Comment on lines +437 to +444
// phpcs:disable WordPress.DB -- Start ignoring
$query = "SELECT COUNT(ID)
FROM $wpdb->posts
WHERE 1=1
AND ID IN (%s)
AND post_status IN ('draft', 'publish')";

// @phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
return (int) $wpdb->get_var( $wpdb->prepare( $query, $ids ) );
$result = (int) $wpdb->get_var( $wpdb->prepare( $query, $ids ) );
// phpcs:enable -- Stop ignoring

Choose a reason for hiding this comment

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

Image description CodeRabbit

The use of wpdb->prepare here is incorrect. The %s placeholder in the SQL query expects a string, but you're passing an array ($ids). This could lead to unexpected results or SQL errors. You should create a list of placeholders that matches the number of elements in $ids, and then use call_user_func_array to pass the parameters to wpdb->prepare.

Here's how you can do it:

- $ids = wp_unslash( sanitize_post_field( 'id', $ids, 0, 'db' ) );
+ $ids = array_map('intval', explode(',', wp_unslash( sanitize_text_field( $_POST['ids'] ) )));

- $query = "SELECT COUNT(ID)
-           FROM $wpdb->posts
-           WHERE 1=1
-           AND ID IN (%s)
-           AND post_status IN ('draft', 'publish')";
+ $placeholders = implode(', ', array_fill(0, count($ids), '%d'));
+ $query = $wpdb->prepare("SELECT COUNT(ID)
+           FROM $wpdb->posts
+           WHERE 1=1
+           AND ID IN ($placeholders)
+           AND post_status IN ('draft', 'publish')", ...$ids);

This way, each ID will be properly escaped, and the query will work as expected.

Comment on lines 104 to 114
AND post_parent = %d
ORDER BY post_title ASC
LIMIT 100",
$post_type,$post_id
$post_type,
$post_id
)
);
// phpcs:enable -- Stop ignoring

if ( count( $children ) > 0 ) {
return $children;

Choose a reason for hiding this comment

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

Image description CodeRabbit

Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [98-111]

While it's good that you've added PHPCS ignore comments around the direct database query, it would be better to use WordPress's built-in functions for querying the database. This will make your code more maintainable and less prone to SQL injection attacks. Here's how you could rewrite this using get_posts:

- // phpcs:disable WordPress.DB -- Start ignoring
- $children = $wpdb->get_results(
- 	$wpdb->prepare(
- 		"SELECT ID
- 		FROM $wpdb->posts
- 		WHERE post_type = %s
- 		AND post_status = 'publish'
- 		AND post_parent = %d
- 		ORDER BY post_title ASC
- 		LIMIT 100",
- 		$post_type,
- 		$post_id
- 	)
- );
- // phpcs:enable -- Stop ignoring
+ $args = array(
+     'post_type' => $post_type,
+     'post_status' => 'publish',
+     'post_parent' => $post_id,
+     'orderby' => 'title',
+     'order' => 'ASC',
+     'numberposts' => 100,
+ );
+ $children = get_posts($args);

This way, you're leveraging WordPress's built-in functionality and reducing the risk of SQL injection.

Comment on lines +223 to +227
// @phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! is_network_admin() && ! isset( $_GET['activate-multi'] ) ) {
set_transient( '_tour_operators_flush_rewrite_rules', 1, 30 );
}

// @phpcs:ignore WordPress.Security.NonceVerification.Recommended

Choose a reason for hiding this comment

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

Image description CodeRabbit

The use of @phpcs:ignore is generally discouraged as it can lead to overlooking potential security issues. In this case, the $_GET['activate-multi'] parameter is not being sanitized before use which could lead to a potential security vulnerability. It would be better to sanitize the input using filter_input(INPUT_GET, 'activate-multi').

- // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
- if ( ! is_network_admin() && ! isset( $_GET['activate-multi'] ) ) {
+ if ( ! is_network_admin() && ! isset( filter_input(INPUT_GET, 'activate-multi') ) ) {
    set_transient( '_tour_operators_flush_rewrite_rules', 1, 30 );
 }
- // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
- if ( ! is_network_admin() && ! isset( $_GET['activate-multi'] ) ) {
+ if ( ! is_network_admin() && ! isset( filter_input(INPUT_GET, 'activate-multi') ) ) {
    set_transient( '_tour_operators_welcome_redirect', 1, 30 );
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Enhancement A suggestion for improvement
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Drop Support for Older PHP Versions & Fix Version Mismatches Parent: Tour Operator Plugin Tasks
1 participant