-
Notifications
You must be signed in to change notification settings - Fork 91
gw-require-alt-text-description-post-image.php
: Added snippet for requiring Alt Text and Description in Post Image Field.
#1077
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
…equiring Alt Text and Description in Post Image Field.
WalkthroughA new Gravity Forms plugin file has been added to require the Alt Text and Description subfields for Post Image fields when those fields are set as required. The plugin uses the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant GravityForms
participant Plugin
User->>GravityForms: Submit form with Post Image field
GravityForms->>Plugin: Trigger gform_field_validation filter
Plugin->>GravityForms: Validate Alt Text and Description subfields
alt Both subfields missing
Plugin-->>GravityForms: Mark field invalid, return combined error
else Only Alt Text missing
Plugin-->>GravityForms: Mark field invalid, return Alt Text error
else Only Description missing
Plugin-->>GravityForms: Mark field invalid, return Description error
else Both present
Plugin-->>GravityForms: Mark field valid
end
GravityForms-->>User: Display validation result
Tip ⚡💬 Agentic Chat (Pro Plan, General Availability)
✨ 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:
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: 1
🧹 Nitpick comments (3)
gravity-forms/gw-require-alt-text-description-post-image.php (3)
17-18
: Consider using strict comparison for field type check.The field type comparison should use strict comparison (
===
) instead of loose comparison (==
) to avoid potential type juggling issues in PHP.- if ( $field->type == 'post_image' && $field->displayAlt && $field->displayDescription && $field->wasRequired ) { + if ( $field->type === 'post_image' && $field->displayAlt && $field->displayDescription && $field->wasRequired ) {
20-21
: Document the meaning of array indices for better code maintainability.The hard-coded array keys
.2
and.7
aren't immediately clear. Consider adding a comment explaining that these are Gravity Forms' specific indices for Alt Text and Description fields.+ // Gravity Forms stores Alt Text at index .2 and Description at index .7 for Post Image fields $alt_text = $value[ $field->id . '.2' ]; $description = $value[ $field->id . '.7' ];
17-36
: Consider field-specific customization options for more flexibility.The current implementation uses the same error messages for all Post Image fields. For greater flexibility, consider adding options to customize error messages per field or specify which fields should enforce this validation using field IDs.
add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) { - if ( $field->type === 'post_image' && $field->displayAlt && $field->displayDescription && $field->wasRequired ) { + // Optional: Define specific field IDs that should enforce this validation + $enforce_for_fields = apply_filters( 'gw_require_alt_desc_field_ids', array() ); + + if ( $field->type === 'post_image' && $field->wasRequired && + ( $field->displayAlt && $field->displayDescription || in_array( $field->id, $enforce_for_fields ) ) ) { $alt_text = $value[ $field->id . '.2' ]; $description = $value[ $field->id . '.7' ]; + + // Optional: Allow customizing error messages per field + $messages = apply_filters( 'gw_require_alt_desc_messages', array( + 'both' => esc_html__( 'Check Post Image. Please enter Alt Text and Description.', 'gw-require-alt-text' ), + 'alt_text' => esc_html__( 'Check Post Image. Please enter Alt Text.', 'gw-require-alt-text' ), + 'description' => esc_html__( 'Check Post Image. Please enter Description.', 'gw-require-alt-text' ), + ), $field );
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gravity-forms/gw-require-alt-text-description-post-image.php
(1 hunks)
🔇 Additional comments (2)
gravity-forms/gw-require-alt-text-description-post-image.php (2)
1-16
: Good documentation and metadata structure in the plugin header.The plugin header follows WordPress standards with clear description, version information, and includes an instruction video link which is helpful for users.
17-36
:❓ Verification inconclusive
Verify image field validation logic is consistent with user expectations.
The current implementation validates Alt Text and Description only when both display options are enabled and the field is required. However, there could be cases where users want to validate these fields individually based on their own display settings.
🏁 Script executed:
#!/bin/bash # Check if there are any similar snippets in the repository that handle Post Image validation fd -e php -x grep -l "post_image.*validation\|gform_field_validation.*post_image" {}Length of output: 88
Action Required: Verify Post Image Field Validation Logic
The current implementation validates both Alt Text and Description only when both display options (displayAlt and displayDescription) are enabled and the field is required. This means that if users intend to validate only one of the options (for instance, when only one display option is active), the validation won’t be triggered.
Please verify if this behavior meets user expectations or if the logic should allow independent validation when only one of the options is enabled. If independent validation is desired, update the conditions accordingly.
if ( ! $alt_text && ! $description ) { | ||
$result['is_valid'] = false; | ||
$result['message'] = 'Check Post Image. Please enter Alt Text and Description.'; | ||
} elseif ( ! $alt_text ) { | ||
$result['is_valid'] = false; | ||
$result['message'] = 'Check Post Image. Please enter Alt Text.'; | ||
} elseif ( ! $description ) { | ||
$result['is_valid'] = false; | ||
$result['message'] = 'Check Post Image. Please enter Description.'; | ||
} |
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
Implement internationalization for user-facing messages.
Error messages should use WordPress internationalization functions to support translation. Also, consider using more robust empty value checking with empty()
instead of just negation.
- if ( ! $alt_text && ! $description ) {
+ if ( empty( $alt_text ) && empty( $description ) ) {
$result['is_valid'] = false;
- $result['message'] = 'Check Post Image. Please enter Alt Text and Description.';
+ $result['message'] = esc_html__( 'Check Post Image. Please enter Alt Text and Description.', 'gw-require-alt-text' );
- } elseif ( ! $alt_text ) {
+ } elseif ( empty( $alt_text ) ) {
$result['is_valid'] = false;
- $result['message'] = 'Check Post Image. Please enter Alt Text.';
+ $result['message'] = esc_html__( 'Check Post Image. Please enter Alt Text.', 'gw-require-alt-text' );
- } elseif ( ! $description ) {
+ } elseif ( empty( $description ) ) {
$result['is_valid'] = false;
- $result['message'] = 'Check Post Image. Please enter Description.';
+ $result['message'] = esc_html__( 'Check Post Image. Please enter Description.', 'gw-require-alt-text' );
}
📝 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.
if ( ! $alt_text && ! $description ) { | |
$result['is_valid'] = false; | |
$result['message'] = 'Check Post Image. Please enter Alt Text and Description.'; | |
} elseif ( ! $alt_text ) { | |
$result['is_valid'] = false; | |
$result['message'] = 'Check Post Image. Please enter Alt Text.'; | |
} elseif ( ! $description ) { | |
$result['is_valid'] = false; | |
$result['message'] = 'Check Post Image. Please enter Description.'; | |
} | |
if ( empty( $alt_text ) && empty( $description ) ) { | |
$result['is_valid'] = false; | |
$result['message'] = esc_html__( 'Check Post Image. Please enter Alt Text and Description.', 'gw-require-alt-text' ); | |
} elseif ( empty( $alt_text ) ) { | |
$result['is_valid'] = false; | |
$result['message'] = esc_html__( 'Check Post Image. Please enter Alt Text.', 'gw-require-alt-text' ); | |
} elseif ( empty( $description ) ) { | |
$result['is_valid'] = false; | |
$result['message'] = esc_html__( 'Check Post Image. Please enter Description.', 'gw-require-alt-text' ); | |
} |
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/2907346978/82160
Summary
Required Alt Text & Description for Post Image Field by leveraging
gform_field_validation
for the logic.How this update works:
https://www.loom.com/share/6d0e70da14c64f5ea40d0aa0f918684d