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
Changes from 11 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
69 changes: 64 additions & 5 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,56 @@ 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.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'];
} 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 @@ -522,9 +577,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'] . '}';
JoryHogeveen marked this conversation as resolved.
Show resolved Hide resolved
} else {
$path = explode( '.', $field );
$field = array_pop( $path );
$ID = '{@' . implode( '.', $path ) . '.' . $pod->api->pod_data['field_id'] . '}';
JoryHogeveen marked this conversation as resolved.
Show resolved Hide resolved
}
}
$atts = ' id="' . $ID . '" pod="@pod" field="' . $field . '"';
if ( ! empty( $value ) ) {
Expand Down