Skip to content

gptos-terms-only-modifier.php: Added snippet to only display terms with the TOS checkbox. #1051

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

Merged
merged 2 commits into from
Mar 27, 2025

Conversation

saifsultanc
Copy link
Contributor

@saifsultanc saifsultanc commented Mar 19, 2025

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/2876696517/79496

https://gravitywiz.slack.com/archives/GMP0ZMNSE/p1741974702635409

Summary

Added a :terms_only merge tag modifier that would allow you to display the terms in notifications or confirmations without also having to display the value /text of the checkbox.

https://www.loom.com/share/d69c48bea2d1429ab019310d2bc6c1e6

Copy link

coderabbitai bot commented Mar 19, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

A new PHP file has been added to introduce a :terms_only modifier within the Gravity Forms Terms of Service functionality. The file hooks into gform_merge_tag_filter to detect when a Terms of Service field is processed and, if applicable, returns only the terms for that field. The implementation checks if the field type is "tos", verifies the presence of the terms_only option, conditionally wraps output in an unordered list, retrieves the terms using get_terms, and finally formats the terms with wpautop.

Changes

File Summary of Changes
gp-terms-of-service/gptos-terms-only-modifier.php Introduces a new PHP file that adds a :terms_only modifier to the {all_fields} merge tag. It hooks into gform_merge_tag_filter, processes fields of type "tos", conditionally formats the output, and retrieves and formats the terms via get_terms and wpautop.

Sequence Diagram(s)

sequenceDiagram
    participant GF as Gravity Forms
    participant Modifier as TermsOnlyModifier
    participant Terms as get_terms
    GF->>Modifier: Trigger gform_merge_tag_filter($value, merge_tag, options, field)
    alt Field type != "tos"
        Modifier-->>GF: Return original value
    else Field type == "tos"
        Modifier->>Modifier: Check for "terms_only" in options
        alt "terms_only" not present
            Modifier-->>GF: Return original value
        else "terms_only" present
            alt merge_tag != "all_fields"
                Modifier->>Modifier: Wrap value in unordered list
            end
            Modifier->>Terms: Retrieve terms (get_terms)
            Terms-->>Modifier: Return terms data
            Modifier->>Modifier: Format terms (wpautop)
            Modifier-->>GF: Return formatted terms
        end
    end
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f887336 and a30de6b.

📒 Files selected for processing (1)
  • gp-terms-of-service/gptos-terms-only-modifier.php (1 hunks)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.

Generated by 🚫 dangerJS against f887336

Copy link

@coderabbitai coderabbitai bot left a 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 (1)
gp-terms-of-service/gptos-terms-only-modifier.php (1)

10-27: Consider adding defensive coding around form retrieval

The code doesn't handle potential failures when retrieving the form with GFAPI::get_form(). If the form doesn't exist, this could cause errors.

-	$value = wpautop( $field->get_terms( GFAPI::get_form( $field->formId ) ) );
+	$form = GFAPI::get_form( $field->formId );
+	if ( ! $form || is_wp_error( $form ) ) {
+		return $value;
+	}
+	$value = wpautop( $field->get_terms( $form ) );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 62668ca and f887336.

📒 Files selected for processing (1)
  • gp-terms-of-service/gptos-terms-only-modifier.php (1 hunks)
🔇 Additional comments (4)
gp-terms-of-service/gptos-terms-only-modifier.php (4)

1-9: Documentation looks good

The file header clearly explains the purpose of this snippet - adding a :terms_only modifier to display only the terms content from a Terms of Service field. The inclusion of documentation links and an instruction video is helpful for users.


10-13: Early validation check looks appropriate

Good use of early return pattern to ensure only Terms of Service fields are processed by this modifier.


15-18: Clean validation for the modifier

Properly checks for the presence of the 'terms_only' modifier in the options before proceeding.


20-22: Clarify the purpose of the conditional list wrapping

The logic for non-'all_fields' merge tags is unclear. If this modifier is specifically for the 'all_fields' merge tag (as indicated in the comments), why are we handling other merge tags differently?

Can you explain the reasoning behind wrapping the value in a list item only when the merge tag is not 'all_fields'? This seems counterintuitive to the stated purpose.

Comment on lines +24 to +26
$value = wpautop( $field->get_terms( GFAPI::get_form( $field->formId ) ) );

return $value;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Potential type error in field access

Line 24 assumes $field is an object by calling $field->get_terms(), but line 11 treats it as an array with $field['type']. This inconsistency could cause a PHP fatal error.

-	$value = wpautop( $field->get_terms( GFAPI::get_form( $field->formId ) ) );
+	$value = wpautop( rgar( $field, 'type' ) === 'tos' ? $field->get_terms( GFAPI::get_form( $field->formId ) ) : '' );

Additionally, there's no error handling for the GFAPI::get_form() call - what if the form isn't found?

📝 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.

Suggested change
$value = wpautop( $field->get_terms( GFAPI::get_form( $field->formId ) ) );
return $value;
$value = wpautop( rgar( $field, 'type' ) === 'tos' ? $field->get_terms( GFAPI::get_form( $field->formId ) ) : '' );
return $value;

Copy link
Contributor

@veryspry veryspry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a couple optional suggestions because I prefer strict comparison when type juggling doesn't make sense in the comparison.

@saifsultanc saifsultanc merged commit 45ab4d6 into master Mar 27, 2025
0 of 3 checks passed
@saifsultanc saifsultanc deleted the saif/add/79496-add-tos-only-terms branch March 27, 2025 04:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants