-
Notifications
You must be signed in to change notification settings - Fork 91
gpeb-filter-by-nested-field.php
: Added snippet to allow filtering data via nested entries.
#1109
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: master
Are you sure you want to change the base?
Conversation
…ata via nested entries.
WalkthroughA new PHP class, Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant FilterForm
participant GPEB_Filter_By_Nested_Entry
participant ParentEntries
participant NestedEntries
User->>FilterForm: Submits filter with value for hidden field
FilterForm->>GPEB_Filter_By_Nested_Entry: Trigger modify_filter_form
GPEB_Filter_By_Nested_Entry->>FilterForm: Replace hidden field with nested field
FilterForm->>GPEB_Filter_By_Nested_Entry: Trigger filter_entries_by_nested_value
GPEB_Filter_By_Nested_Entry->>NestedEntries: Query nested entries by target field value
NestedEntries->>GPEB_Filter_By_Nested_Entry: Return matching nested entries
GPEB_Filter_By_Nested_Entry->>ParentEntries: Fetch parent entries by parent IDs from nested entries
ParentEntries->>GPEB_Filter_By_Nested_Entry: Return filtered parent entries
GPEB_Filter_By_Nested_Entry->>FilterForm: Return filtered parent entries
FilterForm->>User: Display filtered results
Suggested reviewers
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 4
🧹 Nitpick comments (2)
gp-entry-blocks/gpeb-filter-by-nested-field.php (2)
34-34
: Use strict comparison for type safety.Using loose comparison (
==
) instead of strict comparison (===
) can lead to unexpected behavior with type coercion.Apply this diff for type safety:
-return $form_id == $this->parent_form_id; +return $form_id === $this->parent_form_id;
88-93
: Move hard-coded configuration to a more appropriate location.Hard-coding configuration values directly in the class file makes the code less reusable and harder to maintain. Consider moving this to a configuration file, plugin options, or at least adding documentation about customization.
Consider one of these approaches:
- Move to a separate configuration file:
// In a separate config file or at the top of this file $gpeb_filter_config = array( 'parent_form_id' => 4, 'nested_form_id' => 3, 'parent_hidden_field_id' => 10, 'nested_target_field_id' => 4, ); new GPEB_Filter_By_Nested_Entry( $gpeb_filter_config );
- Add configuration documentation:
+/** + * Configuration for GPEB Filter By Nested Entry + * + * To customize for your forms, update the following values: + * - parent_form_id: ID of the parent form containing the entries to filter + * - nested_form_id: ID of the nested form containing the filter criteria + * - parent_hidden_field_id: ID of the hidden field in the parent form + * - nested_target_field_id: ID of the field in the nested form to filter by + */ new GPEB_Filter_By_Nested_Entry( array( 'parent_form_id' => 4, 'nested_form_id' => 3, 'parent_hidden_field_id' => 10, 'nested_target_field_id' => 4, ) );
public function __construct( $config = array() ) { | ||
$this->parent_form_id = rgar( $config, 'parent_form_id' ); | ||
$this->nested_form_id = rgar( $config, 'nested_form_id' ); | ||
$this->parent_hidden_field_id = rgar( $config, 'parent_hidden_field_id' ); | ||
$this->nested_target_field_id = rgar( $config, 'nested_target_field_id' ); | ||
|
||
add_action( 'init', array( $this, 'init' ) ); | ||
} |
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.
🛠️ Refactor suggestion
Add input validation for required configuration parameters.
The constructor accepts configuration parameters but doesn't validate that required parameters are provided, which could lead to runtime errors when the class methods are called.
Consider adding validation in the constructor:
public function __construct( $config = array() ) {
+ // Validate required configuration parameters
+ $required_params = array( 'parent_form_id', 'nested_form_id', 'parent_hidden_field_id', 'nested_target_field_id' );
+ foreach ( $required_params as $param ) {
+ if ( empty( $config[ $param ] ) ) {
+ throw new InvalidArgumentException( "Required parameter '$param' is missing or empty." );
+ }
+ }
+
$this->parent_form_id = rgar( $config, 'parent_form_id' );
$this->nested_form_id = rgar( $config, 'nested_form_id' );
$this->parent_hidden_field_id = rgar( $config, 'parent_hidden_field_id' );
$this->nested_target_field_id = rgar( $config, 'nested_target_field_id' );
add_action( 'init', array( $this, 'init' ) );
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
public function __construct( $config = array() ) { | |
$this->parent_form_id = rgar( $config, 'parent_form_id' ); | |
$this->nested_form_id = rgar( $config, 'nested_form_id' ); | |
$this->parent_hidden_field_id = rgar( $config, 'parent_hidden_field_id' ); | |
$this->nested_target_field_id = rgar( $config, 'nested_target_field_id' ); | |
add_action( 'init', array( $this, 'init' ) ); | |
} | |
public function __construct( $config = array() ) { | |
// Validate required configuration parameters | |
$required_params = array( 'parent_form_id', 'nested_form_id', 'parent_hidden_field_id', 'nested_target_field_id' ); | |
foreach ( $required_params as $param ) { | |
if ( empty( $config[ $param ] ) ) { | |
throw new InvalidArgumentException( "Required parameter '$param' is missing or empty." ); | |
} | |
} | |
$this->parent_form_id = rgar( $config, 'parent_form_id' ); | |
$this->nested_form_id = rgar( $config, 'nested_form_id' ); | |
$this->parent_hidden_field_id = rgar( $config, 'parent_hidden_field_id' ); | |
$this->nested_target_field_id = rgar( $config, 'nested_target_field_id' ); | |
add_action( 'init', array( $this, 'init' ) ); | |
} |
🤖 Prompt for AI Agents
In gp-entry-blocks/gpeb-filter-by-nested-field.php around lines 16 to 23, the
constructor assigns configuration parameters without validating their presence,
which may cause runtime errors later. Add input validation in the constructor to
check that all required parameters like parent_form_id, nested_form_id,
parent_hidden_field_id, and nested_target_field_id are provided and not empty.
If any required parameter is missing, throw an exception or trigger an error to
prevent improper instantiation.
$nested_entries = GFAPI::get_entries( $this->nested_form_id, array( | ||
'field_filters' => array( | ||
array( | ||
'key' => $this->nested_target_field_id, | ||
'value' => $filters[ $this->parent_hidden_field_id ], | ||
), | ||
), | ||
) ); | ||
|
||
$entries = array(); | ||
$parent_entry_ids = array(); | ||
|
||
foreach ( $nested_entries as $nested_entry ) { | ||
$parent_entry_id = rgar( $nested_entry, 'gpnf_entry_parent' ); | ||
$entry = GFAPI::get_entry( $parent_entry_id ); | ||
if ( ! in_array( $parent_entry_id, $parent_entry_ids ) && $entry && ! is_wp_error( $entry ) ) { | ||
$parent_entry_ids[] = $parent_entry_id; | ||
$entries[] = $entry; | ||
} | ||
} | ||
|
||
return $entries; | ||
} |
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.
🛠️ Refactor suggestion
Optimize performance and add error handling for entry queries.
The current implementation has several performance and reliability issues:
- Multiple individual
GFAPI::get_entry()
calls in a loop - No error handling for GFAPI calls
- Complete replacement of entries array rather than filtering existing entries
Consider this optimized approach:
-$entries = array();
-$parent_entry_ids = array();
-
-foreach ( $nested_entries as $nested_entry ) {
- $parent_entry_id = rgar( $nested_entry, 'gpnf_entry_parent' );
- $entry = GFAPI::get_entry( $parent_entry_id );
- if ( ! in_array( $parent_entry_id, $parent_entry_ids ) && $entry && ! is_wp_error( $entry ) ) {
- $parent_entry_ids[] = $parent_entry_id;
- $entries[] = $entry;
- }
-}
+// Collect unique parent entry IDs
+$parent_entry_ids = array();
+foreach ( $nested_entries as $nested_entry ) {
+ $parent_entry_id = rgar( $nested_entry, 'gpnf_entry_parent' );
+ if ( $parent_entry_id && ! in_array( $parent_entry_id, $parent_entry_ids ) ) {
+ $parent_entry_ids[] = $parent_entry_id;
+ }
+}
+
+// Filter existing entries to only include those with matching parent IDs
+if ( ! empty( $parent_entry_ids ) ) {
+ $entries = array_filter( $entries, function( $entry ) use ( $parent_entry_ids ) {
+ return in_array( $entry['id'], $parent_entry_ids );
+ });
+} else {
+ $entries = array(); // No matching nested entries found
+}
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In gp-entry-blocks/gpeb-filter-by-nested-field.php around lines 63 to 85, the
code inefficiently calls GFAPI::get_entry() inside a loop without error handling
and replaces the entries array instead of filtering it. To fix this, refactor to
batch query parent entries in one call if possible, add checks for errors after
each GFAPI call, and modify the logic to filter the existing entries array
rather than replacing it entirely. This will improve performance and
reliability.
…ata via nested entries.
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.
Actionable comments posted: 0
♻️ Duplicate comments (1)
gp-entry-blocks/gpeb-filter-by-nested-field.php (1)
86-96
: Performance optimization still needed for entry retrieval.The code still has the performance issue identified in previous reviews - making individual
GFAPI::get_entry()
calls within a loop, which can be inefficient for large datasets.Consider this optimized approach to reduce database queries:
-$entries = array(); -$parent_entry_ids = array(); - -foreach ( $nested_entries as $nested_entry ) { - $parent_entry_id = rgar( $nested_entry, 'gpnf_entry_parent' ); - $entry = GFAPI::get_entry( $parent_entry_id ); - if ( ! in_array( $parent_entry_id, $parent_entry_ids ) && $entry && ! is_wp_error( $entry ) ) { - $parent_entry_ids[] = $parent_entry_id; - $entries[] = $entry; - } -} +// Collect unique parent entry IDs +$parent_entry_ids = array(); +foreach ( $nested_entries as $nested_entry ) { + $parent_entry_id = rgar( $nested_entry, 'gpnf_entry_parent' ); + if ( $parent_entry_id && ! in_array( $parent_entry_id, $parent_entry_ids ) ) { + $parent_entry_ids[] = $parent_entry_id; + } +} + +// Filter existing entries to only include those with matching parent IDs +if ( ! empty( $parent_entry_ids ) ) { + $entries = array_filter( $entries, function( $entry ) use ( $parent_entry_ids ) { + return in_array( $entry['id'], $parent_entry_ids ); + }); +} else { + $entries = array(); // No matching nested entries found +}
🧹 Nitpick comments (1)
gp-entry-blocks/gpeb-filter-by-nested-field.php (1)
102-107
: Consider making configuration more flexible.The hardcoded form and field IDs make this snippet less reusable. Consider adding configuration comments or making it easier to customize for different use cases.
Add configuration comments to make it easier for users to customize:
new GPEB_Filter_By_Nested_Entry( array( + // Parent form ID that contains the hidden field for filtering 'parent_form_id' => 4, + // Nested form ID that contains the target field to filter by 'nested_form_id' => 3, + // Hidden field ID in the parent form (will be replaced in filter UI) 'parent_hidden_field_id' => 10, + // Target field ID in the nested form to filter by 'nested_target_field_id' => 4, ) );
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gp-entry-blocks/gpeb-filter-by-nested-field.php
(1 hunks)
🔇 Additional comments (3)
gp-entry-blocks/gpeb-filter-by-nested-field.php (3)
16-28
: Good improvement on input validation!The constructor now properly validates required parameters before proceeding, which addresses the previous review comment about missing input validation. The early return approach prevents the class from being initialized with invalid configuration.
49-54
: Excellent error handling implementation!The error handling for the
GFAPI::get_field()
call has been properly implemented as suggested in the previous review. The error logging and graceful continuation prevent fatal errors when fields don't exist.
81-84
: Good error handling for nested entries query!The error handling for
GFAPI::get_entries()
has been properly implemented, checking forWP_Error
objects and providing appropriate error logging.
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/2943573391/83636
Summary
Add support to make it possible to include the child form field values to the filter block on the parent form.
Loom Demo:
https://www.loom.com/share/f795e8b5ef58489794dca96e83fcd230