From 008525fe49c0c2664013c72590d4976b99c36867 Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Fri, 23 May 2025 22:58:24 +0530 Subject: [PATCH 1/2] `gpeb-filter-by-nested-field.php`: Added snippet to allow filtering data via nested entries. --- .../gpeb-filter-by-nested-field.php | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 gp-entry-blocks/gpeb-filter-by-nested-field.php diff --git a/gp-entry-blocks/gpeb-filter-by-nested-field.php b/gp-entry-blocks/gpeb-filter-by-nested-field.php new file mode 100644 index 000000000..553539bb8 --- /dev/null +++ b/gp-entry-blocks/gpeb-filter-by-nested-field.php @@ -0,0 +1,93 @@ +parent_form_id = rgar( $config, 'parent_form_id' ); + $this->nested_form_id = rgar( $config, 'nested_form_id' ); + $this->parent_hidden_field_id = rgar( $config, 'parent_hidden_field_id' ); + $this->nested_target_field_id = rgar( $config, 'nested_target_field_id' ); + + add_action( 'init', array( $this, 'init' ) ); + } + + public function init() { + add_filter( 'gpeb_filter_form', array( $this, 'modify_filter_form' ) ); + add_filter( 'gpeb_queryer_entries', array( $this, 'filter_entries_by_nested_value' ), 10, 2 ); + } + + private function is_applicable_form( $form_id = null ) { + if ( $form_id === null ) { + $form_id = rgget( 'filters_form_id' ); + } + return $form_id == $this->parent_form_id; + } + + public function modify_filter_form( $form ) { + if ( ! $this->is_applicable_form( $form['id'] ) ) { + return $form; + } + + foreach ( $form['fields'] as &$field ) { + if ( $field->id == $this->parent_hidden_field_id ) { + $nested_form_field = GFAPI::get_field( $this->nested_form_id, $this->nested_target_field_id ); + $field = $nested_form_field; + $field->id = $this->parent_hidden_field_id; + } + } + + return $form; + } + + public function filter_entries_by_nested_value( $entries, $gf_queryer ) { + if ( ! $this->is_applicable_form() ) { + return $entries; + } + + $filters = rgget( 'filters' ); + if ( ! isset( $filters[ $this->parent_hidden_field_id ] ) ) { + return $entries; + } + + $nested_entries = GFAPI::get_entries( $this->nested_form_id, array( + 'field_filters' => array( + array( + 'key' => $this->nested_target_field_id, + 'value' => $filters[ $this->parent_hidden_field_id ], + ), + ), + ) ); + + $entries = array(); + $parent_entry_ids = array(); + + foreach ( $nested_entries as $nested_entry ) { + $parent_entry_id = rgar( $nested_entry, 'gpnf_entry_parent' ); + $entry = GFAPI::get_entry( $parent_entry_id ); + if ( ! in_array( $parent_entry_id, $parent_entry_ids ) && $entry && ! is_wp_error( $entry ) ) { + $parent_entry_ids[] = $parent_entry_id; + $entries[] = $entry; + } + } + + return $entries; + } +} + +new GPEB_Filter_By_Nested_Entry( array( + 'parent_form_id' => 4, + 'nested_form_id' => 3, + 'parent_hidden_field_id' => 10, + 'nested_target_field_id' => 4, +) ); From 43d906f3ff8780cb1587199d7c92d6d9edd0f467 Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Tue, 27 May 2025 07:26:13 +0530 Subject: [PATCH 2/2] `gpeb-filter-by-nested-field.php`: Added snippet to allow filtering data via nested entries. --- gp-entry-blocks/gpeb-filter-by-nested-field.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/gp-entry-blocks/gpeb-filter-by-nested-field.php b/gp-entry-blocks/gpeb-filter-by-nested-field.php index 553539bb8..3fe0af088 100644 --- a/gp-entry-blocks/gpeb-filter-by-nested-field.php +++ b/gp-entry-blocks/gpeb-filter-by-nested-field.php @@ -14,6 +14,11 @@ class GPEB_Filter_By_Nested_Entry { private $nested_target_field_id; public function __construct( $config = array() ) { + // check if required parameters are provided + if ( empty( $config['parent_form_id'] ) || empty( $config['nested_form_id'] ) || empty( $config['parent_hidden_field_id'] ) || empty( $config['nested_target_field_id'] ) ) { + return; + } + $this->parent_form_id = rgar( $config, 'parent_form_id' ); $this->nested_form_id = rgar( $config, 'nested_form_id' ); $this->parent_hidden_field_id = rgar( $config, 'parent_hidden_field_id' ); @@ -42,6 +47,10 @@ public function modify_filter_form( $form ) { foreach ( $form['fields'] as &$field ) { if ( $field->id == $this->parent_hidden_field_id ) { $nested_form_field = GFAPI::get_field( $this->nested_form_id, $this->nested_target_field_id ); + if ( ! $nested_form_field ) { + error_log( sprintf( 'Nested form field with ID %d not found in form %d.', $this->nested_target_field_id, $this->nested_form_id ) ); + continue; + } $field = $nested_form_field; $field->id = $this->parent_hidden_field_id; } @@ -69,6 +78,11 @@ public function filter_entries_by_nested_value( $entries, $gf_queryer ) { ), ) ); + if ( is_wp_error( $nested_entries ) ) { + error_log( sprintf( 'Error retrieving nested entries: %s', $nested_entries->get_error_message() ) ); + return $entries; + } + $entries = array(); $parent_entry_ids = array();