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

Add array support for dynamic tags outside of a query #1611

Open
wants to merge 1 commit into
base: release/2.0.0
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
65 changes: 52 additions & 13 deletions includes/dynamic-tags/class-dynamic-tag-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,43 @@ private static function with_wpautop( $output, $options ) {
return wpautop( $output );
}

/**
* Join (implode) array elements into a string.
*
* @param string $output The tag output.
* @param array $options The options.
*/
private static function with_join( $output, $options ) {
$join = $options['join'] ?? ', ';

if ( ! is_string( $join ) || ! is_array( $output ) ) {
return $output;
}

return implode( $options['join'], $output );
}

/**
* Make a one dimensional array from a multi-dimensional array.
*
* @param string $output The tag output.
* @param array $options The options.
*/
private static function with_list( $output, $options ) {
$key = $options['list'] ?? null;

if ( ! is_string( $key ) || ! is_array( $output ) ) {
return $output;
}

return array_map(
function ( $item ) use ( $key ) {
return $item[ $key ] ?? '';
},
$output
);
}

/**
* Output the dynamic tag.
*
Expand All @@ -215,14 +252,19 @@ public static function output( $output, $options, $instance = null ) {
// Store original output for filtering.
$raw_output = $output;

$output = self::with_trunc( $output, $options );
$output = self::with_replace( $output, $options );
$output = self::with_trim( $output, $options );
$output = self::with_case( $output, $options );
if ( is_string( $output ) ) {
$output = self::with_trunc( $output, $options );
$output = self::with_replace( $output, $options );
$output = self::with_trim( $output, $options );
$output = self::with_case( $output, $options );

// Any wrapping output filters should go after direct string transformations.
$output = self::with_wpautop( $output, $options );
$output = self::with_link( $output, $options, $instance );
// Any wrapping output filters should go after direct string transformations.
$output = self::with_wpautop( $output, $options );
$output = self::with_link( $output, $options, $instance );
} elseif ( is_array( $output ) ) {
$output = self::with_list( $output, $options );
$output = self::with_join( $output, $options );
}

$output = apply_filters( 'generateblocks_dynamic_tag_output', $output, $options, $raw_output );

Expand Down Expand Up @@ -374,16 +416,13 @@ public static function get_post_meta( $options, $block, $instance ) {
return self::output( '', $options, $instance );
}

$value = GenerateBlocks_Meta_Handler::get_post_meta( $id, $key, true );
$single = empty( $options['list'] ) && empty( $options['join'] );
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@tomusborne This is really the main thing that need to be done for the other meta/option types to support this change. Should be a small diff (your favorite).

$output = GenerateBlocks_Meta_Handler::get_post_meta( $id, $key, $single );

if ( ! $value ) {
if ( ! $output ) {
return self::output( '', $options, $instance );
}

add_filter( 'wp_kses_allowed_html', [ 'GenerateBlocks_Dynamic_Tags', 'expand_allowed_html' ], 10, 2 );
$output = wp_kses_post( $value );
remove_filter( 'wp_kses_allowed_html', [ 'GenerateBlocks_Dynamic_Tags', 'expand_allowed_html' ], 10, 2 );

return self::output( $output, $options, $instance );
}

Expand Down
Loading