Skip to content
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

Support [if] shortcode fields recursively (magic-tags) #5471

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bfa83f2
Always make sure required keys exist
JoryHogeveen Sep 17, 2019
adcf3b6
Pass entityID instead of path ID for [if] shortcodes
JoryHogeveen Sep 17, 2019
6b7559f
Iterate over recursive relationship fields
JoryHogeveen Sep 17, 2019
9e7c05d
Multiple relationship fields (not sure why people would want that)
JoryHogeveen Sep 17, 2019
6021a23
Merge branch '2.x' into feature/5470-recursive-if-shortcode
sc0ttkclark Oct 25, 2019
562438b
Merge branch '2.x' into feature/5470-recursive-if-shortcode
JoryHogeveen Nov 1, 2019
5a9c940
Merge branch '2.x' into feature/5470-recursive-if-shortcode
JoryHogeveen Nov 1, 2019
57fb4eb
tr1b0t Codestyle
JoryHogeveen Nov 4, 2019
bc462d8
Merge branch '2.x' into feature/5470-recursive-if-shortcode
sc0ttkclark Feb 10, 2020
699c3bd
Update components/Templates/includes/functions-view_template.php
sc0ttkclark Feb 10, 2020
9b6f29a
Merge branch '2.x' into feature/5470-recursive-if-shortcode
sc0ttkclark Feb 17, 2020
a2fa0b6
Merge branch '2.x' into feature/5470-recursive-if-shortcode
sc0ttkclark Feb 22, 2020
f95a567
Merge branch '2.x' into feature/5470-recursive-if-shortcode
sc0ttkclark Mar 18, 2020
47522f5
Merge branch '2.x' into feature/5470-recursive-if-shortcode
sc0ttkclark Mar 25, 2020
3d1519b
Merge branch '2.x' into feature/5470-recursive-if-shortcode
sc0ttkclark Mar 25, 2020
2b40df0
Merge branch 'main' into feature/5470-recursive-if-shortcode
JoryHogeveen Apr 6, 2021
08635d2
Allow field traversing in template commands
JoryHogeveen Apr 19, 2021
767c198
Also support taxonomies
JoryHogeveen Apr 19, 2021
a5a8b84
Update version
JoryHogeveen Apr 19, 2021
6ec1ca1
Merge branch 'main' into feature/5470-recursive-if-shortcode
JoryHogeveen May 20, 2021
65e23c1
Add traversal tests for if statements
JoryHogeveen May 20, 2021
6c7e426
Add traversal tests for each statements
JoryHogeveen May 20, 2021
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
68 changes: 60 additions & 8 deletions components/Templates/includes/functions-view_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ function frontier_decode_template( $code, $atts ) {
*/
function frontier_if_block( $atts, $code ) {

$defaults = array(
'pod' => null,
'id' => null,
'field' => null,
);

$atts = wp_parse_args( $atts, $defaults );

$pod = pods( $atts['pod'], $atts['id'] );

if ( ! $pod || ! $pod->valid() || ! $pod->exists() ) {
Expand All @@ -116,9 +124,58 @@ function frontier_if_block( $atts, $code ) {
break;
}
} else {
$field_data = $pod->field( $atts['field'] );

$field_type = $pod->fields( $atts['field'], 'type' );
/**
Copy link
Member

Choose a reason for hiding this comment

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

Were the changes at https://github.com/pods-framework/pods/pull/5471/files#diff-3af4ca77fc3cd86765f464bb6fa7000725503257e80ccc885464b3ad43ab36c4L536-L540 not enough to do what you wanted here, is this part of the code still necessary? It seems that the previous field() alone would do what this does

Copy link
Member Author

Choose a reason for hiding this comment

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

No, currently only using the field method isn't enough. The field type and Pod object needs to be updated as well so traversals will work properly.
However, I do agree that this might not be the best way to do this.

* @since 2.7.27 Iterate recursively over magic tag fields (relationships).
* @todo Refactor to only use the Pods::field() method.
*/
$fields = explode( '.', $atts['field'] );
$field_pod = $pod;
$total = count( $fields );
$counter = 0;

foreach ( $fields as $field_name ) {
$field = $field_name;
if ( ++$counter < $total ) {

$field_type = $field_pod->fields( $field, 'type' );
if ( ! in_array( $field_type, array( 'pick', 'taxonomy' ), true ) ) {
// 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'];
} 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 );
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;
Expand Down Expand Up @@ -472,7 +529,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
Expand Down Expand Up @@ -533,11 +590,6 @@ function frontier_prefilter_template( $code, $template, $pod ) {
} else {
$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'] . '}';
}
$atts = ' id="' . $ID . '" pod="@pod" field="' . $field . '"';
if ( ! empty( $value ) ) {
$atts .= ' value="' . $value . '"';
Expand Down
41 changes: 41 additions & 0 deletions tests/phpunit/includes/Shortcodes/Test_Each.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]" ) );
}
}
58 changes: 58 additions & 0 deletions tests/phpunit/includes/Shortcodes/Test_If.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<a href="{@related_field.related_field.permalink}">{@related_field.related_field.post_title}</a>' );
$this->assertEquals( '<a href="http://example.org/?test_if=first-post-title">first post title</a>', 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( '<a href="{@related_field.related_field.permalink}">{@related_field.related_field.post_title}</a>' );
$this->assertEquals( '<a href="http://example.org/?test_if=first-post-title">first post title</a>', 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]" ) );

}
}