Skip to content

Commit

Permalink
Merge branch 'release/2.0.0' into bug/query-per-page
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusborne committed Nov 28, 2024
2 parents 4f451f5 + cb4b42b commit 0d73df6
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 157 deletions.
2 changes: 1 addition & 1 deletion dist/packages-rtl.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/packages.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array(), 'version' => '7dd2e91ab685b87a6310');
<?php return array('dependencies' => array(), 'version' => '9a76e7c6626702af14b6');
2 changes: 1 addition & 1 deletion dist/packages.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/styles-builder-imported.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('generateblocks-components', 'lodash', 'react', 'wp-api-fetch', 'wp-block-editor', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-dom', 'wp-edit-post', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-primitives'), 'version' => 'b7373c5686a17a71b6b7');
<?php return array('dependencies' => array('generateblocks-components', 'lodash', 'react', 'wp-api-fetch', 'wp-block-editor', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-dom', 'wp-edit-post', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-primitives'), 'version' => '37ddc3cf04701fdd18b7');
2 changes: 1 addition & 1 deletion dist/styles-builder.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array(), 'version' => '698d4001ed9ff675d239');
<?php return array('dependencies' => array(), 'version' => '40623ddff9e2e3df81dd');
16 changes: 8 additions & 8 deletions dist/styles-builder.js

Large diffs are not rendered by default.

227 changes: 83 additions & 144 deletions includes/dynamic-tags/class-register-dynamic-tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct( $args ) {
* @return array
*/
private static function parse_options( $options_string, $tag_name ) {
$pairs = explode( '|', $options_string );
$pairs = $options_string ? explode( '|', $options_string ) : [];
$result = [
'tag_name' => $tag_name, // Make it so the tag name is available to us in $options.
];
Expand Down Expand Up @@ -88,154 +88,93 @@ public static function get_tags() {
* @return string
*/
public static function replace_tags( $content, $block, $instance ) {
foreach ( self::$tags as $tag_name => $data ) {
$opening_tag = '{{' . $tag_name;

if ( ! generateblocks_str_contains( $content, $opening_tag ) ) {
continue;
}

$full_tag = $opening_tag . '}}';
$supports = $data['supports'];

if ( generateblocks_str_contains( $content, $full_tag ) ) {
$full_tag = self::maybe_prepend_protocol( $content, $full_tag );
$replacement = $data['return']( [], $block, $instance );
$og_replacement = $replacement; // Keep a copy of this in case it's manipulated via filter.

/**
* Allow developers to filter the replacement.
*
* @since 2.0.0
*
* @param string $replacement The replacement.
* @param string $full_tag The full tag.
* @param mixed $content The replacement.
* @param array $block The block.
* @param Object $instance The block instance.
*/
$replacement = apply_filters(
'generateblocks_dynamic_tag_replacement',
$replacement,
[
'full_tag' => $full_tag,
'tag' => $tag_name,
'content' => $content,
'block' => $block,
'instance' => $instance,
'supports' => $supports,
'options' => [],
]
);

// Tags are required to have a value by default. Since this tag has no options,
// we can remove the block and break out of the loop if there is no replacement.
if ( ! $replacement ) {
$content = '';
break;
}

/**
* Allow developers to filter the content before dynamic tag replacement.
*
* @since 2.0.0
*
* @param string $content The content.
* @param string $full_tag The full tag.
* @param mixed $replacement The replacement.
* @param array $block The block.
* @param Object $instance The block instance.
*/
$content = apply_filters(
'generateblocks_before_dynamic_tag_replace',
$content,
[
'tag' => $tag_name,
'full_tag' => $full_tag,
'replacement' => $replacement,
'og_replacement' => $og_replacement,
'block' => $block,
'instance' => $instance,
'supports' => $supports,
'options' => [],
]
);

$content = str_replace( $full_tag, (string) $replacement, $content );
} else {
$pattern = '/\{{' . $tag_name . '(\s+([^}]+))*\}}/';
preg_match_all( $pattern, $content, $matches, PREG_SET_ORDER );

foreach ( $matches as $match ) {
$full_tag = $match[0];
$full_tag = self::maybe_prepend_protocol( $content, $full_tag );
$options_string = $match[2] ?? '';
$options = self::parse_options( $options_string, $tag_name );
$replacement = $data['return']( $options, $block, $instance );
$og_replacement = $replacement; // Keep a copy of this in case it's manipulated via filter.
$required = isset( $options['required'] ) && 'false' === $options['required'] ? false : true;
if ( ! generateblocks_str_contains( $content, '{{' ) ) {
return $content;
}

/**
* Allow developers to filter the replacement.
*
* @since 2.0.0
*
* @param string $replacement The replacement.
* @param string $full_tag The full tag.
* @param mixed $content The replacement.
* @param array $block The block.
* @param Object $instance The block instance.
*/
$replacement = apply_filters(
'generateblocks_dynamic_tag_replacement',
$replacement,
[
'tag' => $tag_name,
'full_tag' => $full_tag,
'content' => $content,
'block' => $block,
'instance' => $instance,
'options' => $options,
'supports' => $supports,
]
);
$pattern = '/\{{(' . implode( '|', array_keys( self::$tags ) ) . ')(\s+[^}]+)?}}/';
preg_match_all( $pattern, $content, $matches, PREG_SET_ORDER );

// If this tag is required for the block to render and there is no replacement,
// we can remove the block and break out of the loop.
if ( $required && ! $replacement ) {
$content = '';
break;
}
foreach ( $matches as $match ) {
$tag_name = $match[1] ?? '';

/**
* Allow developers to filter the content before dynamic tag replacement.
*
* @since 2.0.0
*
* @param string $content The content.
* @param string $full_tag The full tag.
* @param mixed $replacement The replacement.
* @param array $block The block.
* @param Object $instance The block instance.
*/
$content = apply_filters(
'generateblocks_before_dynamic_tag_replace',
$content,
[
'full_tag' => $full_tag,
'tag' => $tag_name,
'replacement' => $replacement,
'og_replacement' => $og_replacement,
'block' => $block,
'instance' => $instance,
'options' => $options,
'supports' => $supports,
]
);
if ( ! isset( self::$tags[ $tag_name ] ) ) {
continue;
}

$content = str_replace( $full_tag, (string) $replacement, $content );
}
$data = self::$tags[ $tag_name ];
$full_tag = $match[0];
$full_tag = self::maybe_prepend_protocol( $content, $full_tag );
$options_string = isset( $match[2] ) ? trim( $match[2], ' ' ) : '';
$options = self::parse_options( $options_string, $tag_name );
$replacement = $data['return']( $options, $block, $instance );
$og_replacement = $replacement;
$supports = $data['supports'];
$required = ! isset( $options['required'] ) || 'false' !== $options['required'];

/**
* Allow developers to filter the replacement.
*
* @since 2.0.0
*
* @param string $replacement The replacement.
* @param string $full_tag The full tag.
* @param mixed $content The replacement.
* @param array $block The block.
* @param Object $instance The block instance.
* @param array $options The options.
* @param array $supports The supports.
*/
$replacement = apply_filters(
'generateblocks_dynamic_tag_replacement',
$replacement,
[
'tag' => $tag_name,
'full_tag' => $full_tag,
'content' => $content,
'block' => $block,
'instance' => $instance,
'options' => $options,
'supports' => $supports,
]
);

// If this tag is required for the block to render and there is no replacement, bail.
if ( $required && ! $replacement ) {
return '';
}

/**
* Allow developers to filter the content before dynamic tag replacement.
*
* @since 2.0.0
*
* @param string $content The content.
* @param string $full_tag The full tag.
* @param string $tag The tag.
* @param mixed $replacement The replacement.
* @param mixed $og_replacement The original replacement.
* @param array $block The block.
* @param Object $instance The block instance.
* @param array $options The options.
* @param array $supports The supports.
*/
$content = apply_filters(
'generateblocks_before_dynamic_tag_replace',
$content,
[
'full_tag' => $full_tag,
'tag' => $tag_name,
'replacement' => $replacement,
'og_replacement' => $og_replacement,
'block' => $block,
'instance' => $instance,
'options' => $options,
'supports' => $supports,
]
);

$content = str_replace( $full_tag, (string) $replacement, $content );
}

return $content;
Expand Down

0 comments on commit 0d73df6

Please sign in to comment.