-
Notifications
You must be signed in to change notification settings - Fork 92
gpld-limit-year-dropdown-options.php: Added snippet to limit date dropdown year options based on configured min/max.
#1185
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
…ropdown year options based on configured min/max.
WalkthroughIntroduces a new PHP class Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes
Suggested reviewers
Pre-merge checks and finishing touches✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. 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.
Actionable comments posted: 0
🧹 Nitpick comments (2)
gp-limit-dates/gpld-limit-year-dropdown-options.php (2)
43-43: Align variable assignments per coding standards.PHPCS requires equals signs to be aligned with surrounding assignments for consistency.
As per coding guidelines.
Apply this diff to fix the alignment:
- $options = gp_limit_dates()->get_limit_dates_field_options( $field ); - $date_key = $type . 'Date'; - $mod_key = $type . 'DateMod'; + $options = gp_limit_dates()->get_limit_dates_field_options( $field ); + $date_key = $type . 'Date'; + $mod_key = $type . 'DateMod';- $date_value = $options[ $date_key ]; - $modifier = rgar( $options, $mod_key ); + $date_value = $options[ $date_key ]; + $modifier = rgar( $options, $mod_key );Also applies to: 45-45, 52-52
54-54: Use strict comparison for type safety.Prefer strict comparison (
===) over loose comparison (==) when comparing to string literals.Apply this diff:
- if ( $date_value == '{today}' ) { + if ( $date_value === '{today}' ) {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gp-limit-dates/gpld-limit-year-dropdown-options.php(1 hunks)
🧰 Additional context used
🪛 GitHub Actions: PHP Lint (PR)
gp-limit-dates/gpld-limit-year-dropdown-options.php
[warning] 43-43: Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space (PHPCS: Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
[warning] 45-45: Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space (PHPCS: Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
[warning] 52-52: Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space (PHPCS: Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
[error] 67-67: date() is affected by runtime timezone changes which can cause date/time to be incorrectly displayed. Use gmdate() instead. (PHPCS: WordPress.DateTime.RestrictedFunctions.date_date)
🪛 GitHub Check: PHPCS (Files Changed)
gp-limit-dates/gpld-limit-year-dropdown-options.php
[failure] 67-67:
date() is affected by runtime timezone changes which can cause date/time to be incorrectly displayed. Use gmdate() instead.
[warning] 52-52:
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
[warning] 45-45:
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
[warning] 43-43:
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
🔇 Additional comments (6)
gp-limit-dates/gpld-limit-year-dropdown-options.php (6)
10-28: LGTM! Clean and flexible design.The constructor properly handles optional scoping and registers the necessary Gravity Forms filters.
30-36: LGTM! Simple and clean delegation.The public filter methods correctly delegate to the shared helper with the appropriate type parameter.
38-72: LGTM! Core filtering logic is sound.The year calculation and clamping logic is correct:
- For
min: usesmax()to ensure the calculated minimum doesn't go below the form's default minimum- For
max: usesmin()to ensure the calculated maximum doesn't go above the form's default maximumThe handling of different date value types ('{today}', numeric field references, and date strings) is appropriate, and edge cases (failed
strtotime()calls) are properly handled.
74-90: LGTM! Proper applicability checks.The method correctly validates prerequisites (plugin availability, field type) and applies optional scoping filters.
95-98: Remember to customize the configuration values.The hardcoded
form_id(123) andfield_id(4) are example values that should be replaced with the actual IDs from your Gravity Forms setup.To find the correct form and field IDs, edit your form in Gravity Forms admin and inspect the field settings. Form IDs are shown in the form list, and field IDs appear in the field settings panel.
67-67: Usegmdate()to fix PHPCS warning—year extraction is timezone-agnostic.Line 67 correctly flags the PHPCS issue with
date(). Sincestrtotime()returns Unix timestamps (timezone-independent) and you're extracting only the year,gmdate()is the appropriate fix. It satisfies PHPCS standards and maintains consistency with the rest of the codebase.The
wp_date()alternative is unnecessary here since WordPress date functions aren't used elsewhere in this file, and year extraction doesn't require timezone-aware handling.- $calculated_year = date( 'Y', $timestamp ); + $calculated_year = gmdate( 'Y', $timestamp );
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/3122496162/91955?viewId=8172236
Summary
This snippet allows the user to limit the year options in Date Dropdown fields based on their min/max settings.
As noted in the snippet, this does not currently support min/max based on other date fields.
Quick examples: https://www.loom.com/share/0e0383b29876457a84060c66fc6fc29b