Skip to content

gcgs-gppa-custom-query-builder-args.php: Added instruction video and scoping to specific field-form IDs. #1048

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions gc-google-sheets/gcgs-gppa-custom-query-builder-args.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php
/**
* Gravity Connect // Google Sheets // Custom Query Build Arguments
*
* Instruction Video: https://www.loom.com/share/a7bf1139baef48fc8b9b31209827cd17
*
* This snippet customizes the generation of query builder arguments for Google Sheets queries.
*
* Installation:
* 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
*/
add_filter( 'gcgs_gppa_query_builder_args', function( $query_builder_args, $args, $object ) {

/** @var string|string[] */
$filter_value = null;

Expand All @@ -20,9 +23,18 @@
/** @var string */
$property_id = null;

/** @var object */
$field = null;

// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
extract( $args );

// TODO : UPDATE 4 to your targetted field ID.
// TODO : UPDATE 141 to your targetted form ID.
if ( $field->id != '4' || $field->formId != 141 || $filter['operator'] != 'is' ) {
return $query_builder_args;
}
Comment on lines +32 to +36
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding null checks and making the targeting configurable

The current implementation has a few potential issues:

  1. There's no null check for $field before accessing its properties, which could cause PHP warnings
  2. There's no check if $filter['operator'] exists
  3. The field ID is compared as a string ('4') while form ID as an integer (141)
- // TODO : UPDATE 4 to your targetted field ID.
- // TODO : UPDATE 141 to your targetted form ID.
- if ( $field->id != '4' || $field->formId != 141 || $filter['operator'] != 'is' ) {
+ // Define your target field and form IDs
+ $target_field_id = '4'; // Field ID to target
+ $target_form_id = 141;  // Form ID to target
+ 
+ // Skip processing if not matching our targeted field/form or if required variables aren't set
+ if ( !isset($field) || !is_object($field) || 
+      $field->id != $target_field_id || 
+      $field->formId != $target_form_id || 
+      !isset($filter['operator']) || 
+      $filter['operator'] != 'is' ) {

This change improves:

  • Safety by adding null checks
  • Maintainability by moving hardcoded values to variables at the top
  • Consistency in how we handle the comparison
📝 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
// TODO : UPDATE 4 to your targetted field ID.
// TODO : UPDATE 141 to your targetted form ID.
if ( $field->id != '4' || $field->formId != 141 || $filter['operator'] != 'is' ) {
return $query_builder_args;
}
// Define your target field and form IDs
$target_field_id = '4'; // Field ID to target
$target_form_id = 141; // Form ID to target
// Skip processing if not matching our targeted field/form or if required variables aren't set
if ( !isset($field) || !is_object($field) ||
$field->id != $target_field_id ||
$field->formId != $target_form_id ||
!isset($filter['operator']) ||
$filter['operator'] != 'is' ) {
return $query_builder_args;
}


$column_letter = $object->get_column_letter( $args['primary_property_value'], $property_id );

if ( ! empty( $filter_value ) ) {
Expand Down
Loading