Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions gp-terms-of-service/gptos-terms-only-modifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Gravity Perks // Terms of Service // Add a `:terms_only` modifier.
* https://gravitywiz.com/documentation/gravity-forms-terms-of-service/
*
* Add a `:terms_only` modifier to the {all_fields} merge tag to only display the terms.
*
* Instruction Video: https://www.loom.com/share/d69c48bea2d1429ab019310d2bc6c1e6
*/
add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $options, $field ) {
if ( $field['type'] !== 'tos' ) {
return $value;
}

$options = explode( ',', $options );
if ( ! in_array( 'terms_only', $options ) ) {
return $value;
}

if ( $merge_tag !== 'all_fields' ) {
$value = '<ul><li>' . $value . '</li></ul>';
}

$value = wpautop( $field->get_terms( GFAPI::get_form( $field->formId ) ) );

return $value;
Comment on lines +24 to +26
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;

}, 11, 4 );
Loading