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

Loading enhancement for blocks and slotfills #90

Closed
wants to merge 10 commits into from

Conversation

renatonascalves
Copy link
Contributor

@renatonascalves renatonascalves commented Oct 30, 2023

Summary

  • Enhance the loading logic to account for headless sites, where the post type is not available;
  • Use declare(strict_types=1);;
  • Improves phpDoc;
  • wp_curate_duduplication_slotfill_post_types fallbacks to the default supported post types;

The previous approach didn't work very well with headless sites, where we need to access the context of the block. Mainly because get_current_post_type returns empty in those instances and there was no way of hooking into that.

This approach keeps the same logic but allows devs to load the block in headless context, if necessary, by using wp_curate_load.

Summary by CodeRabbit

Refactor:

  • Enhanced code quality by introducing strict types in various PHP files.
  • Improved the logic for loading blocks and slotfills in the wp-curate package, now using the load() method of the Supported_Post_Types class.

New Features:

  • Added two new filter hooks, wp_curate_allowed_post_types and wp_curate_allowed_taxonomies, providing more customization options.

Tests:

  • Introduced a new test class Supported_Post_Types_Test to ensure the functionality of the Supported_Post_Types class.

Chores:

  • Excluded the "tests/" directory from code analysis to focus on production code.

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

coderabbitai bot commented Oct 30, 2023

Walkthrough

The changes primarily focus on enhancing the code quality and maintainability of the PHP files in the project. The modifications include the introduction of strict types, the addition of a new load() method in the Supported_Post_Types class, and the alteration of conditions in several functions to use this new method. Furthermore, new filter hooks have been added, and the test coverage has been expanded with a new test class.

Changes

File(s) Summary
.phpcs.xml Excludes the "tests/" directory from code analysis.
blocks/post/index.php, blocks/query/index.php, entries/slotfills/index.php Declare strict types. Modify conditions to use load() method of Supported_Post_Types class. Add filter hooks in blocks/query/index.php.
src/class-supported-post-types.php Declare strict types. Add load() method. Update get_current_post_type() method.
tests/feature/class-supported-post-types-test.php Add Supported_Post_Types_Test test class with four test methods.

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
Member

@attackant attackant left a comment

Choose a reason for hiding this comment

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

Asked a couple q's for review before explicit approval.

namespace Alley\WP\WP_Curate;

/**
* Registers all slotfill assets so that they can be enqueued through Gutenberg in
* the corresponding context.
*/
function register_slotfills_scripts(): void {
$supported_post_types = new Supported_Post_Types();

Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to use a singleton for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does!

src/class-supported-post-types.php Show resolved Hide resolved
Base automatically changed from feature/post-support to develop October 31, 2023 10:53
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: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ce2e532 and c0008e2.
Files selected for processing (6)
  • .phpcs.xml (1 hunks)
  • blocks/post/index.php (1 hunks)
  • blocks/query/index.php (2 hunks)
  • entries/slotfills/index.php (1 hunks)
  • src/class-supported-post-types.php (4 hunks)
  • tests/feature/class-supported-post-types-test.php (1 hunks)
Files skipped from review due to trivial changes (1)
  • .phpcs.xml
Additional comments: 11
blocks/query/index.php (3)
  • 8-8: Adding declare(strict_types=1); is a good practice for enforcing strict typing in PHP. This will help catch type-related errors at compile time.

  • 16-20: The load() method is used to load the supported post types. If it fails, the function returns early. This is a good practice for error handling.

  • 35-42: The addition of the wp_curate_allowed_post_types and wp_curate_allowed_taxonomies filters is a good practice. It allows developers to modify the allowed post types and taxonomies for the Query block.

blocks/post/index.php (2)
  • 8-8: Adding declare(strict_types=1); is a good practice for enforcing strict typing in PHP. This can help prevent type-related bugs in your code.

  • 16-20: The change from checking if the current post type is in the supported post types array to using the load() method of the $supported_post_types object is a good abstraction. It encapsulates the logic of checking supported post types within the Supported_Post_Types class, making the code more maintainable and easier to understand.

However, ensure that the load() method of the Supported_Post_Types class is implemented correctly and handles all edge cases. Also, verify that the load() method does not have any side effects that could affect the rest of the code.

entries/slotfills/index.php (4)
  • 10-10: The use of declare(strict_types=1); is a good practice for enforcing strict typing in PHP. This can help prevent bugs related to type coercion.

  • 19-19: The Supported_Post_Types class is instantiated without any arguments. Ensure that the class constructor does not require any mandatory arguments.

  • 26-26: The apply_filters function allows plugins to modify the list of post types that will show the "Enable Deduplication" slotfill. This is a good use of WordPress's hooks system to allow for extensibility.

  • 28-30: The load method is used to check if the current post type is in the list of allowed post types. If it's not, the function returns early. This is a good use of the early return pattern to avoid unnecessary computation.

src/class-supported-post-types.php (2)
  • 8-8: The use of declare(strict_types=1); is a good practice for enforcing strict typing in PHP. This can help prevent type-related bugs in the code.

  • 55-81: The load() method is a good addition to the class. It provides a way to load a block or slotfill using either the supported post types or custom ones. The use of an optional parameter $post_types with a default value of an empty array is a good practice. It makes the method more flexible and easier to use.

tests/feature/class-supported-post-types-test.php Outdated Show resolved Hide resolved
src/class-supported-post-types.php Show resolved Hide resolved
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 c0008e2 and 5ab85df.
Files selected for processing (1)
  • src/class-supported-post-types.php (4 hunks)
Additional comments: 5
src/class-supported-post-types.php (5)
  • 8-8: The use of declare(strict_types=1); is a good practice for enforcing strict typing in PHP. This can help prevent type-related bugs in the code.

  • 52-52: The use of apply_filters allows for the modification of the supported post types by other parts of the application or plugins. This is a good practice for extensibility.

  • 63-81: The load method checks if the current post type is in the list of post types passed to it (or the supported post types if none are passed) and returns false if it is not. This is a good way to prevent blocks or slotfills from being loaded in unsupported post types. However, it's important to ensure that this method is called in the correct context (i.e., when a block or slotfill is being loaded) and that the return value is properly handled.

  • 97-107: The use of $_GET superglobal is a security risk as it can be manipulated by the user. It's recommended to use WordPress's filter_input() function to access these superglobals. This was already pointed out in a previous review comment and the recommendation still stands.

- if ( 'post.php' === $pagenow && ! empty( $_GET['post'] ) ) {
+ $post_id = filter_input(INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT);
+ if ( 'post.php' === $pagenow && ! empty( $post_id ) ) {
- if ( ! empty( $_GET['post_type'] ) ) {
+ $post_type = filter_input(INPUT_GET, 'post_type', FILTER_SANITIZE_STRING);
+ if ( ! empty( $post_type ) ) {
  • 116-116: The get_current_post_type method now always returns a string, which can make it easier to use in the rest of the code. However, it's important to ensure that all uses of this method in the codebase have been updated to handle this change.

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 5ab85df and b449f6a.
Files selected for processing (1)
  • tests/feature/class-supported-post-types-test.php (1 hunks)
Files skipped from review due to trivial changes (1)
  • tests/feature/class-supported-post-types-test.php

@renatonascalves renatonascalves deleted the feature/new-load branch October 31, 2023 19:25
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.

3 participants