-
Notifications
You must be signed in to change notification settings - Fork 92
gpnf-sort-nested-form-entries.php: Added support for sorting by Date fields.
#1131
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
gpnf-sort-nested-form-entries.php: Added support for sorting by Date fields.
#1131
Conversation
WalkthroughA private property and method are introduced to detect if the sort field in nested forms is a date field. Both PHP and JavaScript sorting logic are updated to handle date fields by converting values to dates for accurate sorting. The detection and sorting logic are conditionally applied based on the field type. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant GPNF_Sort_Nested_Entries (PHP)
participant JavaScript (Client)
User->>GPNF_Sort_Nested_Entries (PHP): Initiate sort on nested form entries
GPNF_Sort_Nested_Entries (PHP)->>GPNF_Sort_Nested_Entries (PHP): is_date_field()
alt Sort field is date
GPNF_Sort_Nested_Entries (PHP)->>GPNF_Sort_Nested_Entries (PHP): sort_entries_php() (convert to timestamps)
GPNF_Sort_Nested_Entries (PHP)->>JavaScript (Client): Pass isDateField flag
JavaScript (Client)->>JavaScript (Client): Sort using Date objects
else Sort field is not date
GPNF_Sort_Nested_Entries (PHP)->>GPNF_Sort_Nested_Entries (PHP): sort_entries_php() (regular comparison)
GPNF_Sort_Nested_Entries (PHP)->>JavaScript (Client): Pass isDateField flag
JavaScript (Client)->>JavaScript (Client): Sort using regular comparison
end
Possibly related PRs
Suggested reviewers
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ 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: 2
🧹 Nitpick comments (1)
gp-nested-forms/gpnf-sort-nested-form-entries.php (1)
39-52: Add error handling and consider caching the result.The method logic is correct, but there are opportunities for improvement:
- The method calls
GFAPI::get_field()andGFAPI::get_form()which may be expensive operations- No error handling for potential API failures
- The result could be cached since it won't change during execution
Consider this enhanced implementation:
private function is_date_field() { + // Cache the result to avoid repeated API calls + static $cached_result = null; + if ( $cached_result !== null ) { + return $cached_result; + } + $nested_field = GFAPI::get_field( $this->_args['parent_form_id'], $this->_args['nested_field_id'] ); if ( ! $nested_field || empty( $nested_field->gpnfForm ) ) { - return false; + return $cached_result = false; } $child_form = GFAPI::get_form( $nested_field->gpnfForm ); if ( ! $child_form ) { - return false; + return $cached_result = false; } $sort_field = GFAPI::get_field( $child_form, $this->_args['sort_field_id'] ); - return $sort_field && $sort_field->type === 'date'; + return $cached_result = $sort_field && $sort_field->type === 'date'; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gp-nested-forms/gpnf-sort-nested-form-entries.php(5 hunks)
🔇 Additional comments (4)
gp-nested-forms/gpnf-sort-nested-form-entries.php (4)
20-20: LGTM: Private property declaration is appropriate.The private property
_is_date_fieldfollows PHP naming conventions and is properly scoped for internal class use.
34-34: LGTM: Initialization logic is correctly placed.The date field detection is properly initialized in the
init()method before setting up the filters.
98-98: LGTM: JavaScript argument properly passed.The
isDateFieldflag is correctly passed to the JavaScript context for client-side sorting.
106-106: LGTM: JavaScript variable properly declared.The boolean conversion is handled correctly for JavaScript consumption.
gpnf-sort-nested-form-entries.php: Added support for sorting by Date fieldsgpnf-sort-nested-form-entries.php: Added support for sorting by Date fields.
986468d to
265bb0e
Compare
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/2995555966/86254?viewId=3808239
Summary
Issue: Date fields were being sorted alphabetically instead of chronologically, causing unexpected ordering (see Matt's loom)
Snippet now automatically detects date fields and sorts them chronologically.