From bfa83f29ccff06f81fc0e4998e19032d1e8ac765 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Tue, 17 Sep 2019 14:00:54 +0200 Subject: [PATCH 01/11] Always make sure required keys exist --- components/Templates/includes/functions-view_template.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/components/Templates/includes/functions-view_template.php b/components/Templates/includes/functions-view_template.php index 984da8b611..cfd8cefc9d 100644 --- a/components/Templates/includes/functions-view_template.php +++ b/components/Templates/includes/functions-view_template.php @@ -91,6 +91,12 @@ function frontier_decode_template( $code, $atts ) { */ function frontier_if_block( $atts, $code ) { + $atts = wp_parse_args( $atts, array( + 'pod' => null, + 'id' => null, + 'field' => null, + ) ); + $pod = pods( $atts['pod'], $atts['id'] ); if ( ! $pod || ! $pod->valid() || ! $pod->exists() ) { From adcf3b6c4d8019e7ee24412b9d5a6be393fb665e Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Tue, 17 Sep 2019 14:13:46 +0200 Subject: [PATCH 02/11] Pass entityID instead of path ID for [if] shortcodes --- .../Templates/includes/functions-view_template.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/components/Templates/includes/functions-view_template.php b/components/Templates/includes/functions-view_template.php index cfd8cefc9d..fad6d404cf 100644 --- a/components/Templates/includes/functions-view_template.php +++ b/components/Templates/includes/functions-view_template.php @@ -528,9 +528,13 @@ function frontier_prefilter_template( $code, $template, $pod ) { $field = trim( $matches[2][ $key ] ); } if ( false !== strpos( $field, '.' ) ) { - $path = explode( '.', $field ); - $field = array_pop( $path ); - $ID = '{@' . implode( '.', $path ) . '.' . $pod->api->pod_data['field_id'] . '}'; + if ( 'if' === $command ) { + $ID = '{@' . $pod->api->pod_data['field_id'] . '}'; + } else { + $path = explode( '.', $field ); + $field = array_pop( $path ); + $ID = '{@' . implode( '.', $path ) . '.' . $pod->api->pod_data['field_id'] . '}'; + } } $atts = ' id="' . $ID . '" pod="@pod" field="' . $field . '"'; if ( ! empty( $value ) ) { From 6b7559f094d28d98220c84331496a53117150371 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Tue, 17 Sep 2019 14:14:15 +0200 Subject: [PATCH 03/11] Iterate over recursive relationship fields --- .../includes/functions-view_template.php | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/components/Templates/includes/functions-view_template.php b/components/Templates/includes/functions-view_template.php index fad6d404cf..4ebd1aa591 100644 --- a/components/Templates/includes/functions-view_template.php +++ b/components/Templates/includes/functions-view_template.php @@ -122,9 +122,53 @@ function frontier_if_block( $atts, $code ) { break; } } else { - $field_data = $pod->field( $atts['field'] ); - $field_type = $pod->fields( $atts['field'], 'type' ); + /** + * @since 2.7.16 Iterate recursively over magic tag fields (relationships). + */ + $fields = explode( '.', $atts['field'] ); + $field_pod = $pod; + $total = count( $fields ); + $counter = 0; + + foreach ( $fields as $field_name ) { + $field = $field_name; + if ( ++$counter !== $total ) { + + if ( 'pick' !== $field_pod->fields( $field, 'type' ) ) { + // Relationship type required. + break; + } + + $entries = $field_pod->field( $field ); + $rel_pod = $field_pod->fields( $field, 'pick_val' ); + + if ( ! $entries || ! $rel_pod ) { + // No relationships or pod name found. + break; + } + + $entry_id = null; + if ( isset( $entries['ID'] ) ) { + $entry_id = $entries['ID']; + } elseif ( isset( $entries['term_id'] ) ) { + $entry_id = $entries['term_id']; + } elseif ( isset( $entries[0]['ID'] ) ) { + $entry_id = $entries[0]['term_id']; + } elseif ( isset( $entries[0]['term_id'] ) ) { + $entry_id = $entries[0]['term_id']; + } + + $new_pod = pods( $rel_pod, $entry_id ); + if ( $new_pod && $new_pod->valid() && $new_pod->exists() ) { + $field_pod = $new_pod; + } + + } else { + $field_data = $field_pod->field( $field ); + $field_type = $field_pod->fields( $field, 'type' ); + } + } } $is_empty = true; From 9e7c05d9791686006bf17564c45cb3855d6cbad3 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Tue, 17 Sep 2019 14:50:18 +0200 Subject: [PATCH 04/11] Multiple relationship fields (not sure why people would want that) --- .../Templates/includes/functions-view_template.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/components/Templates/includes/functions-view_template.php b/components/Templates/includes/functions-view_template.php index 4ebd1aa591..4c20e26bda 100644 --- a/components/Templates/includes/functions-view_template.php +++ b/components/Templates/includes/functions-view_template.php @@ -133,7 +133,7 @@ function frontier_if_block( $atts, $code ) { foreach ( $fields as $field_name ) { $field = $field_name; - if ( ++$counter !== $total ) { + if ( ++$counter < $total ) { if ( 'pick' !== $field_pod->fields( $field, 'type' ) ) { // Relationship type required. @@ -153,10 +153,14 @@ function frontier_if_block( $atts, $code ) { $entry_id = $entries['ID']; } elseif ( isset( $entries['term_id'] ) ) { $entry_id = $entries['term_id']; - } elseif ( isset( $entries[0]['ID'] ) ) { - $entry_id = $entries[0]['term_id']; - } elseif ( isset( $entries[0]['term_id'] ) ) { - $entry_id = $entries[0]['term_id']; + } else { + // Multiple relationships. + $entries = reset( $entries ); + if ( isset( $entries['ID'] ) ) { + $entry_id = $entries['ID']; + } elseif ( isset( $entries['term_id'] ) ) { + $entry_id = $entries['term_id']; + } } $new_pod = pods( $rel_pod, $entry_id ); From 57fb4ebc2d43336a175345589dc8978f15ce65f5 Mon Sep 17 00:00:00 2001 From: Jory Hogeveen Date: Mon, 4 Nov 2019 17:50:37 +0100 Subject: [PATCH 05/11] tr1b0t Codestyle --- .../Templates/includes/functions-view_template.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/components/Templates/includes/functions-view_template.php b/components/Templates/includes/functions-view_template.php index 4c20e26bda..09669e0344 100644 --- a/components/Templates/includes/functions-view_template.php +++ b/components/Templates/includes/functions-view_template.php @@ -91,11 +91,13 @@ function frontier_decode_template( $code, $atts ) { */ function frontier_if_block( $atts, $code ) { - $atts = wp_parse_args( $atts, array( + $defaults = array( 'pod' => null, 'id' => null, 'field' => null, - ) ); + ); + + $atts = wp_parse_args( $atts, $defaults ); $pod = pods( $atts['pod'], $atts['id'] ); @@ -140,8 +142,8 @@ function frontier_if_block( $atts, $code ) { break; } - $entries = $field_pod->field( $field ); - $rel_pod = $field_pod->fields( $field, 'pick_val' ); + $entries = $field_pod->field( $field ); + $rel_pod = $field_pod->fields( $field, 'pick_val' ); if ( ! $entries || ! $rel_pod ) { // No relationships or pod name found. From 699c3bd7666ce061fd81fc4378b6e1939ca1fca7 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sun, 9 Feb 2020 22:38:15 -0600 Subject: [PATCH 06/11] Update components/Templates/includes/functions-view_template.php --- components/Templates/includes/functions-view_template.php | 1 - 1 file changed, 1 deletion(-) diff --git a/components/Templates/includes/functions-view_template.php b/components/Templates/includes/functions-view_template.php index 09669e0344..d2c6ca1a21 100644 --- a/components/Templates/includes/functions-view_template.php +++ b/components/Templates/includes/functions-view_template.php @@ -169,7 +169,6 @@ function frontier_if_block( $atts, $code ) { if ( $new_pod && $new_pod->valid() && $new_pod->exists() ) { $field_pod = $new_pod; } - } else { $field_data = $field_pod->field( $field ); $field_type = $field_pod->fields( $field, 'type' ); From 08635d227a043f765adb5d445ea76fd324b24e2a Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Mon, 19 Apr 2021 19:06:31 +0200 Subject: [PATCH 07/11] Allow field traversing in template commands Adds support for each tags --- .../Templates/includes/functions-view_template.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/components/Templates/includes/functions-view_template.php b/components/Templates/includes/functions-view_template.php index fa1bff2cb0..c3f3090a3d 100644 --- a/components/Templates/includes/functions-view_template.php +++ b/components/Templates/includes/functions-view_template.php @@ -527,7 +527,7 @@ function frontier_pseudo_magic_tags( $template, $data, $pod = null, $skip_unknow } /** - * processes template code within an each command from the base template + * Processes template code within an each command from the base template. * * @param array attributes from template * @param string template to be processed @@ -588,15 +588,6 @@ function frontier_prefilter_template( $code, $template, $pod ) { } else { $field = trim( $matches[2][ $key ] ); } - if ( false !== strpos( $field, '.' ) ) { - if ( 'if' === $command ) { - $ID = '{@' . $pod->api->pod_data['field_id'] . '}'; - } else { - $path = explode( '.', $field ); - $field = array_pop( $path ); - $ID = '{@' . implode( '.', $path ) . '.' . $pod->api->pod_data['field_id'] . '}'; - } - } $atts = ' id="' . $ID . '" pod="@pod" field="' . $field . '"'; if ( ! empty( $value ) ) { $atts .= ' value="' . $value . '"'; From 767c19802f8e06f4964629142faf868f01f95099 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Mon, 19 Apr 2021 19:19:28 +0200 Subject: [PATCH 08/11] Also support taxonomies --- components/Templates/includes/functions-view_template.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/Templates/includes/functions-view_template.php b/components/Templates/includes/functions-view_template.php index c3f3090a3d..0566c7637b 100644 --- a/components/Templates/includes/functions-view_template.php +++ b/components/Templates/includes/functions-view_template.php @@ -127,6 +127,7 @@ function frontier_if_block( $atts, $code ) { /** * @since 2.7.16 Iterate recursively over magic tag fields (relationships). + * @todo Refactor to only use the Pods::field() method. */ $fields = explode( '.', $atts['field'] ); $field_pod = $pod; @@ -137,7 +138,8 @@ function frontier_if_block( $atts, $code ) { $field = $field_name; if ( ++$counter < $total ) { - if ( 'pick' !== $field_pod->fields( $field, 'type' ) ) { + $field_type = $field_pod->fields( $field, 'type' ); + if ( ! in_array( $field_type, array( 'pick', 'taxonomy' ), true ) ) { // Relationship type required. break; } From a5a8b84fc328dab1a194591ddb1f4427c72b974c Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Mon, 19 Apr 2021 19:23:05 +0200 Subject: [PATCH 09/11] Update version --- components/Templates/includes/functions-view_template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/Templates/includes/functions-view_template.php b/components/Templates/includes/functions-view_template.php index 0566c7637b..d895d02a68 100644 --- a/components/Templates/includes/functions-view_template.php +++ b/components/Templates/includes/functions-view_template.php @@ -126,7 +126,7 @@ function frontier_if_block( $atts, $code ) { } else { /** - * @since 2.7.16 Iterate recursively over magic tag fields (relationships). + * @since 2.7.27 Iterate recursively over magic tag fields (relationships). * @todo Refactor to only use the Pods::field() method. */ $fields = explode( '.', $atts['field'] ); From 65e23c1fcbfaa9d261be6efa8023569a86238b75 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Thu, 20 May 2021 17:36:36 +0200 Subject: [PATCH 10/11] Add traversal tests for if statements --- tests/phpunit/includes/Shortcodes/Test_If.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/phpunit/includes/Shortcodes/Test_If.php b/tests/phpunit/includes/Shortcodes/Test_If.php index 8eb5926dbb..a40ed75bb9 100644 --- a/tests/phpunit/includes/Shortcodes/Test_If.php +++ b/tests/phpunit/includes/Shortcodes/Test_If.php @@ -235,4 +235,62 @@ public function test_if_related_field() { $this->assertEquals( 'first post title', do_shortcode( "[pods name='{$pod_name}' id='{$id2}'][if related_field]{@related_field.post_title}[/if][/pods]" ) ); } + + /** + * Test traversal if statements. + * Almost similar to test_if_related_field() but this traverses one level deeper and also adds the post title to the if statement. + */ + public function test_if_traversal() { + + $pod_name = self::$pod_name; + $id1 = pods( $pod_name )->add( + array( + 'post_status' => 'publish', + 'name' => 'first post title', + 'number1' => 123, + 'number2' => 456, + ) + ); + $id2 = pods( $pod_name )->add( + array( + 'post_status' => 'publish', + 'name' => 'second post title', + 'number1' => 987, + 'number2' => 654, + 'related_field' => $id1, + ) + ); + $id3 = pods( $pod_name )->add( + array( + 'post_status' => 'publish', + 'name' => 'third post title', + 'number1' => 159, + 'number2' => 753, + 'related_field' => $id2, + ) + ); + + // Not exactly related to the shortcode test but lets make sure we can at least retrieve the proper data + $this->assertEquals( '123', pods( $pod_name, $id3 )->field( 'related_field.related_field.number1' ) ); + + + // Traverse to the second relationship. + $content = base64_encode( '{@related_field.related_field.post_title}' ); + $this->assertEquals( 'first post title', do_shortcode( "[pod_if_field pod='{$pod_name}' id='{$id3}' field='related_field.related_field']{$content}[/pod_if_field]" ) ); + + $content = base64_encode( '{@related_field.related_field.post_title}' ); + $this->assertEquals( 'first post title', do_shortcode( "[pod_if_field pod='{$pod_name}' id='{$id3}' field='related_field.related_field']{$content}[/pod_if_field]" ) ); + + $this->assertEquals( 'first post title', do_shortcode( "[pods name='{$pod_name}' id='{$id3}'][if related_field.related_field]{@related_field.related_field.post_title}[/if][/pods]" ) ); + + // Traverse to the second relationship's post title + $content = base64_encode( '{@related_field.related_field.post_title}' ); + $this->assertEquals( 'first post title', do_shortcode( "[pod_if_field pod='{$pod_name}' id='{$id3}' field='related_field.related_field.post_title']{$content}[/pod_if_field]" ) ); + + $content = base64_encode( '{@related_field.related_field.post_title}' ); + $this->assertEquals( 'first post title', do_shortcode( "[pod_if_field pod='{$pod_name}' id='{$id3}' field='related_field.related_field.post_title']{$content}[/pod_if_field]" ) ); + + $this->assertEquals( 'first post title', do_shortcode( "[pods name='{$pod_name}' id='{$id3}'][if related_field.related_field.post_title]{@related_field.related_field.post_title}[/if][/pods]" ) ); + + } } From 6c7e426c0203fb04d8fcb4c80526d217722c84ae Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Thu, 20 May 2021 17:50:23 +0200 Subject: [PATCH 11/11] Add traversal tests for each statements --- .../phpunit/includes/Shortcodes/Test_Each.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/phpunit/includes/Shortcodes/Test_Each.php b/tests/phpunit/includes/Shortcodes/Test_Each.php index e94359f9e1..ea15bdca49 100644 --- a/tests/phpunit/includes/Shortcodes/Test_Each.php +++ b/tests/phpunit/includes/Shortcodes/Test_Each.php @@ -235,4 +235,45 @@ public function test_each_nested_in_external() { $content = base64_encode( '{@number1}_{@number2}' ); $this->assertEquals( '1_12_43_94_165_25', do_shortcode( "[test_each_recurse][pod_sub_template pod='{$pod_name}' id='{$main_id}' field='related_field']{$content}[/pod_sub_template][/test_each_recurse]" ) ); } + + /** + * Test traversal each statements. + * Almost similar to test_each_nested_in_external() but this traverses one level deeper. + */ + public function test_each_traversal() { + + $pod_name = self::$pod_name; + $subsub_ids = array(); + $pod = pods( $pod_name ); + for ( $x = 1; $x <= 5; $x ++ ) { + $subsub_ids[] = $pod->add( + array( + 'post_status' => 'publish', + 'name' => $x, + 'number1' => $x, + 'number2' => $x * $x, + ) + ); + } + $sub_id = $pod->add( + array( + 'post_status' => 'publish', + 'name' => 'sub post', + 'number1' => 123, + 'number2' => 456, + 'related_field' => $subsub_ids, + ) + ); + $main_id = $pod->add( + array( + 'post_status' => 'publish', + 'name' => 'main post', + 'number1' => 159, + 'number2' => 753, + 'related_field' => $sub_id, + ) + ); + $content = base64_encode( '{@number1}_{@number2}' ); + $this->assertEquals( '1_12_43_94_165_25', do_shortcode( "[test_each_recurse][pod_sub_template pod='{$pod_name}' id='{$main_id}' field='related_field.related_field']{$content}[/pod_sub_template][/test_each_recurse]" ) ); + } }