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

Better support for the default post behavior #85

Merged
merged 2 commits into from
Oct 31, 2023

Conversation

renatonascalves
Copy link
Contributor

@renatonascalves renatonascalves commented Oct 27, 2023

Summary

Changes to better support the default post behavior.

How to Test

Summary by CodeRabbit

New Features:

  • Enhanced the software's intuitiveness by adding a new feature that checks if the current post type is supported. This prevents users from making changes to unsupported post types, improving the user experience.
  • Added a filter in the initialize_supported_post_types function, allowing users to customize the supported post types. This provides more flexibility and control to the users.

Refactor:

  • Simplified the code by removing the get_editor_post_type function and moving certain function calls inside a conditional statement. This makes the code cleaner and easier to maintain.
  • Improved the handling of new posts by defaulting to the 'post' post type if no specific post type is provided. This makes the software more user-friendly, especially for new users.

@renatonascalves renatonascalves self-assigned this Oct 27, 2023
@coderabbitai
Copy link

coderabbitai bot commented Oct 27, 2023

Walkthrough

The changes primarily focus on enhancing the handling of supported post types in the WordPress block editor. A new class Supported_Post_Types is introduced to manage the supported post types. The changes also include the removal of the get_editor_post_type function, and the relocation of wp_register_script and wp_set_script_translations calls.

Changes

File Summary
blocks/query/index.php A new instance of the Supported_Post_Types class is created and used to check if the current post type is supported. If not, the function returns early.
entries/slotfills/index.php The register_slotfills_scripts function now uses the Supported_Post_Types class. The get_editor_post_type function is removed, and script registration and translation calls are moved inside a conditional statement. The action_enqueue_slotfills_assets function is now hooked to the enqueue_block_editor_assets action.
src/class-supported-post-types.php Changes to initialize_supported_post_types() and get_current_post_type() functions. A filter is added to allow customization of supported post types. The handling of the post-new.php page is modified to default to the 'post' post type if $_GET['post_type'] is empty.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.json

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between cdfe4e5 and 7258153.
Files selected for processing (3)
  • blocks/query/index.php (1 hunks)
  • entries/slotfills/index.php (2 hunks)
  • src/class-supported-post-types.php (2 hunks)
Additional comments: 4
src/class-supported-post-types.php (2)
  • 45-49: The addition of the comment block provides a clear explanation of the filter 'wp_curate_supported_post_types'. This is a good practice as it improves the readability and maintainability of the code.

  • 71-77: The changes in this hunk handle the case when 'post_type' is not provided in the 'post-new.php' page. It defaults to 'post' if 'post_type' is not specified in the GET parameters. This is a good practice as it provides a default behavior and prevents potential errors due to undefined 'post_type'.

- } elseif ( 'post-new.php' === $pagenow && ! empty( $_GET['post_type'] ) ) {
-    // phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.Security.NonceVerification.Recommended
-    $post_type = sanitize_text_field( $_GET['post_type'] );
+ } elseif ( 'post-new.php' === $pagenow ) {
+    if ( ! empty( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
+        $post_type = sanitize_text_field( $_GET['post_type'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
+    } else {
+        // Default to post.
+        $post_type = 'post';
+    }
  }
entries/slotfills/index.php (2)
  • 16-40: The register_slotfills_scripts function has been refactored to use the Supported_Post_Types class for determining the current post type. This is a good improvement as it encapsulates the logic for determining the current post type within a dedicated class. However, ensure that the Supported_Post_Types class is thoroughly tested, especially the get_current_post_type method, as it is now a critical part of the script registration process.

  • 50-53: The action_enqueue_slotfills_assets function is now hooked to the enqueue_block_editor_assets action. This is a good change as it ensures that the slotfill assets are enqueued only when the block editor assets are enqueued, improving the efficiency of asset management.

Comment on lines +24 to 27
$supported_post_types = new Supported_Post_Types();
if ( ! in_array( $supported_post_types->get_current_post_type(), $allowed_post_types, true ) ) {
return;
}
Copy link

Choose a reason for hiding this comment

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

The Supported_Post_Types class is being instantiated inside the register_slotfills_scripts function. This could lead to unnecessary object creation if this function is called multiple times. Consider making Supported_Post_Types a singleton or moving the instantiation to a higher scope to avoid unnecessary object creation.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7258153 and 9e1e945.
Files selected for processing (1)
  • src/class-supported-post-types.php (2 hunks)
Additional comments: 2
src/class-supported-post-types.php (2)
  • 42-51: The get_supported_post_types() function now includes a filter wp_curate_supported_post_types which allows customization of supported post types. This is a good practice as it provides flexibility for other developers to modify the behavior of your function without changing the source code. However, ensure that the filter is well-documented and its usage is clear to other developers.

  • 68-80: The get_current_post_type() function has been updated to handle the post-new.php page differently. If the post_type parameter is not provided in the URL, the default post type is set to 'post'. This is a sensible default, but it might be worth documenting this behavior in the function's docblock for clarity. Also, consider sanitizing the post_type parameter even when it's not empty to prevent potential security issues.

Here's a suggested change to sanitize the post_type parameter:

- if ( ! empty( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
-    $post_type = $_GET['post_type']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
+ if ( ! empty( $_GET['post_type'] ) ) {
+    $post_type = sanitize_text_field( $_GET['post_type'] );

@renatonascalves renatonascalves merged commit ce2e532 into develop Oct 31, 2023
6 checks passed
@renatonascalves renatonascalves deleted the feature/post-support branch October 31, 2023 10:53
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