-
Notifications
You must be signed in to change notification settings - Fork 2
Add support for nested sub-WHERE clauses and update hook naming #49
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
base: main
Are you sure you want to change the base?
Conversation
| $this->assertTrue( $pre_results_fired, 'stellarwp_schema_custom_table_query_pre_results action should fire' ); | ||
| $this->assertTrue( $post_results_fired, 'stellarwp_schema_custom_table_query_post_results action should fire' ); | ||
| $this->assertTrue( $results_filter_fired, 'stellarwp_schema_custom_table_query_results filter should fire' ); | ||
| $this->assertTrue( $where_filter_fired, 'stellarwp_schema_custom_table_query_where filter should fire' ); |
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.
@dpanta94 you are checking if the hooks are fired here, right? Have you considered using did_filter or did_action to check if a hook was fired? This is a pattern used in WordPress: https://github.com/WordPress/wordpress-develop/blob/trunk/tests/phpunit/tests/template.php#L596
That way, for the hook stellarwp_schema_custom_table_query_pre_results, for example, you can remove this:
$pre_results_fired = false;
[...]
add_action( 'stellarwp_schema_custom_table_query_pre_results', function() use ( &$pre_results_fired ) {
$pre_results_fired = true;
} );and make this change:
- $this->assertTrue( $pre_results_fired, 'stellarwp_schema_custom_table_query_pre_results action should fire' );
+ $this->assertSame( 1, did_action('stellarwp_schema_custom_table_query_pre_results'), 'stellarwp_schema_custom_table_query_pre_results action should fire' );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.
These tests could provide more value in their current form by asserting the values passed to them are the expected ones. Else they can be simplified as suggested by @Rahmon.
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.
Good suggestion. Thank you. I have simplified those checks. I did the patter of storing the did_action before and asserting that after is equal than stored + 1.
lucatume
left a comment
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.
Two requests:
- Add a test to cover malformed sub-WHERE inputs where one or more keys are missing or the operator is not correct.
- The changelog mentions a but-fix, is there a test for this?
| $this->assertTrue( $pre_results_fired, 'stellarwp_schema_custom_table_query_pre_results action should fire' ); | ||
| $this->assertTrue( $post_results_fired, 'stellarwp_schema_custom_table_query_post_results action should fire' ); | ||
| $this->assertTrue( $results_filter_fired, 'stellarwp_schema_custom_table_query_results filter should fire' ); | ||
| $this->assertTrue( $where_filter_fired, 'stellarwp_schema_custom_table_query_where filter should fire' ); |
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.
These tests could provide more value in their current form by asserting the values passed to them are the expected ones. Else they can be simplified as suggested by @Rahmon.
Co-authored-by: theAverageDev (Luca Tumedei) <luca@theaveragedev.com>
…t/sub-where-clauses
|
@lucatume this is ready for a second look when you can ! |
Summary
This PR introduces support for nested sub-WHERE clauses in query methods and standardizes hook naming to align with the StellarWP namespace. These changes enable more flexible and complex database queries while maintaining backward compatibility through semantic versioning.
Key Changes
Nested Sub-WHERE Clauses - Adds the ability to build complex queries with grouped conditions and custom operators (AND/OR) within the
paginate()andget_total_items()methods.Hook Naming Standardization - Renamed all hooks from
tec_common_*prefix tostellarwp_schema_*prefix for namespace consistency (breaking change, documented in CHANGELOG).Technical Details
Sub-WHERE Clause Implementation
The implementation extracts sub-where clause logic into a new private method
build_sub_wheres_from_args()that recursively builds grouped query conditions. This allows developers to construct queries like:Architecture Decision: The recursive approach allows for arbitrary nesting depth without code duplication. Each sub-where group is wrapped in parentheses and joined with its specified operator, maintaining SQL precedence rules.
Breaking Changes
Hook names have been changed to reflect the correct namespace:
tec_common_custom_table_query_pre_results→stellarwp_schema_custom_table_query_pre_resultstec_common_custom_table_query_post_results→stellarwp_schema_custom_table_query_post_resultstec_common_custom_table_query_results→stellarwp_schema_custom_table_query_resultstec_common_custom_table_query_where→stellarwp_schema_custom_table_query_whereMigration Path: Consumers using these hooks must update their code. The change is documented in CHANGELOG.md with clear before/after examples.
Documentation
Updated documentation includes:
Areas Requiring Review Attention