diff --git a/composer.lock b/composer.lock
index 3c6fd5eb2..d848135e5 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,20 +4,20 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
- "content-hash": "1ae0a828e38ceeea92578f76f35e483a",
+ "content-hash": "ed7d2f988ffce5c2a5150505b5329207",
"packages": [
{
"name": "codeinwp/themeisle-sdk",
- "version": "3.3.71",
+ "version": "3.3.72",
"source": {
"type": "git",
"url": "https://github.com/Codeinwp/themeisle-sdk.git",
- "reference": "cd7acfc0b01b8d5712736aa3721d689591ced991"
+ "reference": "66036fa0ef8168f7e5a3f7bf06dc88733e1d46db"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/Codeinwp/themeisle-sdk/zipball/cd7acfc0b01b8d5712736aa3721d689591ced991",
- "reference": "cd7acfc0b01b8d5712736aa3721d689591ced991",
+ "url": "https://api.github.com/repos/Codeinwp/themeisle-sdk/zipball/66036fa0ef8168f7e5a3f7bf06dc88733e1d46db",
+ "reference": "66036fa0ef8168f7e5a3f7bf06dc88733e1d46db",
"shasum": ""
},
"require-dev": {
@@ -43,9 +43,9 @@
],
"support": {
"issues": "https://github.com/Codeinwp/themeisle-sdk/issues",
- "source": "https://github.com/Codeinwp/themeisle-sdk/tree/v3.3.71"
+ "source": "https://github.com/Codeinwp/themeisle-sdk/tree/v3.3.72"
},
- "time": "2026-11-04T20:22:53+00:00"
+ "time": "2026-11-06T13:01:52+00:00"
},
{
"name": "enshrined/svg-sanitize",
diff --git a/inc/class-pro.php b/inc/class-pro.php
index bbfa4427e..007658836 100644
--- a/inc/class-pro.php
+++ b/inc/class-pro.php
@@ -121,7 +121,7 @@ public static function get_reference() {
* @return string
*/
public static function get_docs_url() {
- return 'https://bit.ly/otter-docs';
+ return 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/otter-blocks-documentation';
}
/**
diff --git a/inc/plugins/class-dashboard.php b/inc/plugins/class-dashboard.php
index 110835928..b2754faff 100644
--- a/inc/plugins/class-dashboard.php
+++ b/inc/plugins/class-dashboard.php
@@ -151,7 +151,7 @@ public function uninstall_feedback_popup_after_heading() {
'' . esc_html( $pages_label ) . ''
);
- $documentation_url = Pro::get_docs_url();
+ $documentation_url = tsdk_utmify( Pro::get_docs_url(), 'documentation', 'uninstall-feedback' );
$support_url = Pro::is_pro_active()
? 'https://store.themeisle.com/'
: 'https://wordpress.org/support/plugin/otter-blocks/';
@@ -476,8 +476,8 @@ public function get_dashboard_data() {
'upgradeLink' => tsdk_translate_link( tsdk_utmify( Pro::get_url(), 'options', Pro::get_reference() ) ),
'upgradeLinkFromTc' => tsdk_utmify( Pro::get_url(), 'templatecloud' ),
'tcUpgradeLink' => tsdk_utmify( 'https://themeisle.com/plugins/templates-cloud/', 'templatecloud', 'otter-blocks' ),
- 'tcDocs' => 'https://docs.themeisle.com/article/2191-templates-cloud-collections',
- 'docsLink' => Pro::get_docs_url(),
+ 'tcDocs' => tsdk_utmify( 'https://docs.themeisle.com/wordpress/templates-cloud-collections', 'templates-cloud-collections', 'dashboard' ),
+ 'docsLink' => tsdk_utmify( Pro::get_docs_url(), 'documentation', 'dashboard' ),
'newPageUrl' => esc_url( admin_url( 'post-new.php?post_type=page' ) ),
'showFeedbackNotice' => $this->should_show_feedback_notice(),
'deal' => ! Pro::is_pro_installed() ? $offer->get_localized_data() : array(),
diff --git a/inc/plugins/class-dynamic-content.php b/inc/plugins/class-dynamic-content.php
index ee0e3e809..12e5101ea 100644
--- a/inc/plugins/class-dynamic-content.php
+++ b/inc/plugins/class-dynamic-content.php
@@ -24,6 +24,18 @@ class Dynamic_Content {
*/
const MAGIC_TAG_REGEX = '{(otterDynamic\?[^"\'<>}]*)}';
+ /**
+ * Literal representations of dynamic marker prefixes.
+ *
+ * @var array
+ */
+ private static $literal_marker_escapes = array(
+ '{otterDynamic?' => '{otterDynamic?',
+ 'otterDynamicLink' => 'otterDynamicLink',
+ 'otter/v1/dynamic' => 'otter/v1/dynamic',
+ ' '<o-dynamic',
+ );
+
/**
* Exception keys of the postContent tags currently being rendered.
*
@@ -35,12 +47,189 @@ class Dynamic_Content {
* Initialize the class
*/
public function init() {
+ add_filter( 'render_block', array( $this, 'escape_visitor_block_magic_tags' ), 9, 2 );
add_filter( 'render_block', array( $this, 'apply_dynamic_content' ) );
add_filter( 'render_block', array( $this, 'apply_dynamic_magic_tags' ) );
add_filter( 'render_block', array( $this, 'apply_dynamic_link' ) );
add_filter( 'render_block', array( $this, 'apply_dynamic_link_button' ) );
add_filter( 'render_block', array( $this, 'apply_dynamic_images' ) );
- add_filter( 'otter_apply_dynamic_image', array( $this, 'apply_dynamic_images' ) );
+ add_filter( 'otter_apply_dynamic_image', array( $this, 'apply_dynamic_css_images' ) );
+ add_filter( 'get_comment_text', array( $this, 'escape_comment_magic_tags' ) );
+ add_filter( 'get_comment_excerpt', array( $this, 'escape_comment_magic_tags' ) );
+ add_filter( 'get_comment_author', array( $this, 'escape_comment_magic_tags' ) );
+ add_filter( 'get_comment_author_url', array( $this, 'escape_comment_magic_tags' ) );
+ add_filter( 'block_core_breadcrumbs_items', array( $this, 'escape_breadcrumb_magic_tags' ) );
+ add_filter( 'get_search_form', array( $this, 'escape_search_form_magic_tags' ) );
+ add_filter( 'wp_generate_tag_cloud_data', array( $this, 'escape_tag_cloud_magic_tags' ) );
+ add_filter( 'wp_setup_nav_menu_item', array( $this, 'escape_taxonomy_menu_magic_tags' ) );
+ add_filter( 'list_cats', array( $this, 'escape_comment_magic_tags' ) );
+ foreach ( array( 'get_avatar', 'the_author', 'get_the_author_display_name', 'get_the_author_description', 'get_the_author_user_description', 'comment_form_field_author', 'comment_form_field_email', 'comment_form_field_url', 'comment_form_logged_in' ) as $hook ) {
+ add_filter( $hook, array( $this, 'escape_comment_magic_tags' ) );
+ }
+ }
+
+ /**
+ * Protect rendered visitor values before the generic Magic Tag filters run.
+ *
+ * @param string $content Rendered markup.
+ * @param array{blockName: string|null, attrs?: array{id?: string, idBase?: string}} $block Parsed block.
+ * @return string
+ */
+ public function escape_visitor_block_magic_tags( $content, $block ) {
+ if ( 'core/term-description' === $block['blockName'] ) {
+ return $this->escape_comment_magic_tags( $content );
+ }
+
+ $patterns = array(
+ 'core/avatar' => '/(]*\saria-label=")([^"]*)(")/',
+ 'core/search' => '/(]*\stype="(?:search|text)")(?=[^>]*\sname="s")[^>]*\svalue=")([^"]*)(")/',
+ 'core/query-title' => '/(>)([^<]*)(<)/',
+ 'core/post-terms' => '/(]*>)([^<]*)(<\/a>)/',
+ 'core/term-name' => '/(>)([^<]*)(<)/',
+ );
+ if ( 'core/legacy-widget' === $block['blockName'] ) {
+ $widget = isset( $block['attrs']['idBase'] ) ? $block['attrs']['idBase'] : '';
+ if ( isset( $block['attrs']['id'] ) ) {
+ $widget = $block['attrs']['id'];
+ }
+ if ( 'search' === _get_widget_id_base( $widget ) ) {
+ // A null form filter makes core use its unfiltered fallback markup.
+ $patterns['core/legacy-widget'] = '/(
', $result );
}
+ public function test_comment_magic_tag_does_not_create_script_link(): void {
+ wp_set_current_user( 0 );
+ update_option( 'comment_registration', 0 );
+ update_option( 'require_name_email', 0 );
+ wp_update_post( array( 'ID' => $this->post_id, 'comment_status' => 'open' ) );
+
+ $comment = wp_handle_comment_submission(
+ array(
+ 'comment_post_ID' => $this->post_id,
+ 'author' => 'Visitor',
+ 'comment' => 'Comment link',
+ )
+ );
+ $this->assertInstanceOf( WP_Comment::class, $comment );
+ $this->assertStringContainsString( '{otterDynamic?', $comment->comment_content );
+ wp_set_comment_status( $comment->comment_ID, 'approve' );
+
+ $block = new WP_Block(
+ array( 'blockName' => 'core/comment-content', 'attrs' => array() ),
+ array( 'commentId' => (int) $comment->comment_ID )
+ );
+ $result = do_blocks( '' . $block->render() . '
' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertNotSame( 'javascript:alert(1)', $processor->get_attribute( 'href' ), $result );
+ }
+
+ public function test_contributor_magic_tag_does_not_create_script_link(): void {
+ wp_set_current_user( self::factory()->user->create( array( 'role' => 'contributor' ) ) );
+ $post_id = self::factory()->post->create(
+ array(
+ 'post_content' => 'Post link
',
+ )
+ );
+ $content = get_post_field( 'post_content', $post_id );
+ $this->assertStringContainsString( '{otterDynamic?', $content );
+ $result = do_blocks( $content );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( '', (string) $processor->get_attribute( 'href' ), $result );
+ }
+
+ public function test_magic_tags_sanitize_every_uri_attribute(): void {
+ add_filter( 'wp_kses_uri_attributes', function ( $attributes ) {
+ $attributes[] = 'data-destination';
+ return $attributes;
+ } );
+
+ foreach ( wp_kses_uri_attributes() as $attribute ) {
+ foreach ( array( '"', "'", '' ) as $quote ) {
+ $result = $this->dynamic_content->apply_dynamic_magic_tags( 'Link' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag(), $result );
+ $this->assertSame( '', (string) $processor->get_attribute( $attribute ), $result );
+ }
+ }
+ }
+
+ public function test_magic_tags_sanitize_svg_link_attributes(): void {
+ $values = array(
+ '{otterDynamic?default=sprite.svg}' => 'sprite.svg',
+ '{otterDynamic?default=../icons/sprite.svg}' => '../icons/sprite.svg',
+ 'java{otterDynamic?default=script}:alert(1)' => 'alert(1)',
+ 'JaVa{otterDynamic?default=ScRiPt}:alert(1)' => 'alert(1)',
+ '{otterDynamic?default=data:text/html,svg}' => 'text/html,svg',
+ '{otterDynamic?default=https://example.org/svg}' => 'https://example.org/svg',
+ );
+ foreach ( $values as $value => $expected ) {
+ $result = $this->dynamic_content->apply_dynamic_magic_tags( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( $expected, (string) $processor->get_attribute( 'xlink:href' ), $result );
+ }
+ }
+
+ public function test_magic_tags_preserve_safe_url_values(): void {
+ add_filter( 'otter_blocks_evaluate_dynamic_content_text', function () { return 'https://example.org/path with spaces?a=1&b=2'; } );
+ $result = $this->dynamic_content->apply_dynamic_magic_tags( 'Link' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag(), $result );
+ $this->assertSame( 'https://example.org/path%20with%20spaces?a=1&b=2', $processor->get_attribute( 'href' ), $result );
+ }
+
+ public function test_magic_tags_quote_unquoted_attributes_and_preserve_literals(): void {
+ $value = 'Value with spaces & "quotes" {otterDynamic?default=second-pass}';
+ add_filter( 'otter_blocks_evaluate_dynamic_content_text', function () use ( $value ) { return $value; } );
+ $result = $this->dynamic_content->apply_dynamic_magic_tags( '{otterDynamic?type=testValue}
' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag(), $result );
+ $this->assertSame( $value, $processor->get_attribute( 'title' ), $result );
+ $this->assertSame( '&', $processor->get_attribute( 'data-static' ), $result );
+ $this->assertSame( array( 'title', 'data-static' ), $processor->get_attribute_names_with_prefix( '' ) );
+ $this->assertStringContainsString( '>' . $value . '', html_entity_decode( $result ), $result );
+ }
+
+ public function test_magic_tags_sanitize_raw_text_element_attributes(): void {
+ foreach ( array( 'iframe', 'script', 'style', 'textarea' ) as $tag ) {
+ $result = $this->dynamic_content->apply_dynamic_magic_tags( '<' . $tag . ' src="{otterDynamic?default=javascript:alert(1)}" title="{otterDynamic?default=Title}">Unchanged' . $tag . '>' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag(), $result );
+ $this->assertSame( '', (string) $processor->get_attribute( 'src' ), $result );
+ $this->assertSame( 'Title', $processor->get_attribute( 'title' ), $result );
+ $this->assertStringContainsString( '>Unchanged' . $tag . '>', $result );
+ }
+ }
+
+ public function test_magic_tags_sanitize_css_and_reject_executable_attributes(): void {
+ $result = $this->dynamic_content->apply_dynamic_magic_tags( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag(), $result );
+ $this->assertSame( 'color:red', $processor->get_attribute( 'style' ), $result );
+ $this->assertNull( $processor->get_attribute( 'onload' ), $result );
+ $this->assertNull( $processor->get_attribute( 'srcdoc' ), $result );
+ }
+
+ public function test_stored_comment_magic_tags_remain_literal_in_nested_blocks(): void {
+ $token = '{otterDynamic?type=postTitle}';
+ $comment_id = self::factory()->comment->create(
+ array(
+ 'comment_post_ID' => $this->post_id,
+ 'comment_content' => '' . $token . '',
+ 'comment_author' => $token,
+ 'comment_approved' => 1,
+ )
+ );
+ foreach ( array( get_comment_text( $comment_id ), get_comment_excerpt( $comment_id ), get_comment_author( $comment_id ) ) as $content ) {
+ $markup = '{otterDynamic?type=postTitle}
' . $content . '
';
+ $result = do_blocks( $markup );
+ $this->assertStringContainsString( $token, html_entity_decode( $result ), $result );
+ $this->assertStringContainsString( '>Test', $result );
+ }
+ }
+
+ public function test_encoded_comment_magic_tag_stays_literal_beside_dynamic_attribute(): void {
+ $token = '{otterDynamic?type=postTitle}';
+ $content = $this->dynamic_content->escape_comment_magic_tags( $token );
+ $result = $this->dynamic_content->apply_dynamic_magic_tags( 'Link' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag(), $result );
+ $this->assertSame( $token, $processor->get_attribute( 'href' ), $result );
+ $this->assertSame( 'Test', $processor->get_attribute( 'title' ), $result );
+ }
+
+ /**
+ * @dataProvider provide_visitor_dynamic_urls
+ */
+ public function test_visitor_comment_dynamic_links_remain_literal_in_nested_blocks( string $token ): void {
+ wp_update_post( array( 'ID' => $this->post_id, 'comment_status' => 'open' ) );
+ update_option( 'comment_registration', 0 );
+ wp_set_current_user( 0 );
+ $comment = wp_handle_comment_submission( array(
+ 'comment_post_ID' => $this->post_id,
+ 'author' => 'Visitor',
+ 'email' => 'visitor@example.org',
+ 'comment' => 'Visitor link',
+ ) );
+ $this->assertInstanceOf( WP_Comment::class, $comment );
+ $this->assertStringContainsString( $token, $comment->comment_content );
+ $result = do_blocks( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( $token, $processor->get_attribute( 'href' ), $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( site_url(), $processor->get_attribute( 'href' ), $result );
+ }
+
+ /**
+ * @dataProvider provide_visitor_dynamic_urls
+ */
+ public function test_term_description_dynamic_links_remain_literal_in_nested_blocks( string $token ): void {
+ $user = get_userdata( $this->user_id );
+ $user->set_role( 'contributor' );
+ wp_set_current_user( $this->user_id );
+ $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
+ $request->set_param( 'name', 'Literal description' );
+ $request->set_param( 'description', 'Visitor link' );
+ $response = rest_do_request( $request );
+ $this->assertSame( 201, $response->get_status(), wp_json_encode( $response->get_data() ) );
+ $term_id = $response->get_data()['id'];
+ $this->assertStringContainsString( $token, html_entity_decode( get_term( $term_id, 'post_tag' )->description ) );
+ wp_set_current_user( 0 );
+ $this->go_to( get_term_link( $term_id, 'post_tag' ) );
+ $result = do_blocks( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( $token, $processor->get_attribute( 'href' ), $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( site_url(), $processor->get_attribute( 'href' ), $result );
+ }
+
+ /**
+ * @dataProvider provide_visitor_dynamic_urls
+ */
+ public function test_encoded_dynamic_urls_stay_literal_beside_authored_attribute( string $token ): void {
+ $content = $this->dynamic_content->escape_comment_magic_tags( $token );
+ $result = do_blocks( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( $token, $processor->get_attribute( 'href' ), $result );
+ $this->assertSame( 'Test', $processor->get_attribute( 'title' ), $result );
+ }
+
+ /**
+ * @dataProvider provide_visitor_dynamic_urls
+ */
+ public function test_authored_dynamic_link_resolves_beside_dynamic_attribute( string $token ): void {
+ foreach ( array( '#otterDynamicLink?type=siteURL', '#otterDynamicLink?type=siteURL&context={otterDynamic?type=postID}' ) as $href ) {
+ $result = do_blocks( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( site_url(), $processor->get_attribute( 'href' ), $result );
+ $this->assertSame( 'Test', $processor->get_attribute( 'title' ), $result );
+ }
+ $content = $this->dynamic_content->escape_comment_magic_tags( $token );
+ foreach ( array( $content, esc_url( $content ) ) as $literal ) {
+ $result = do_blocks( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( $token . '&id=' . $this->post_id, $processor->get_attribute( 'href' ), $result );
+ }
+ }
+
+ public function test_authored_dynamic_image_resolves_beside_dynamic_attribute(): void {
+ $attachment_id = self::factory()->attachment->create_object( 'authored-image.png', $this->post_id, array( 'post_mime_type' => 'image/png' ) );
+ set_post_thumbnail( $this->post_id, $attachment_id );
+ foreach ( array( $this->post_id, '{otterDynamic?type=postID}' ) as $context ) {
+ $url = get_rest_url( null, 'otter/v1/dynamic/' );
+ $url .= ( false === strpos( $url, '?' ) ? '?' : '&' ) . 'type=featuredImage&context=' . $context;
+ $result = do_blocks( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'img' ), $result );
+ $this->assertSame( get_the_post_thumbnail_url( $this->post_id ), $processor->get_attribute( 'src' ), $result );
+ $this->assertSame( 'Test', $processor->get_attribute( 'title' ), $result );
+ }
+ }
+
+ /**
+ * @dataProvider provide_visitor_dynamic_urls
+ */
+ public function test_resolved_attribute_dynamic_urls_remain_literal_in_nested_blocks( string $token ): void {
+ wp_update_post( array( 'ID' => $this->post_id, 'post_title' => $token ) );
+ $this->go_to( get_permalink( $this->post_id ) );
+ $queries = array(
+ 'type=postTitle' => $token,
+ 'default=otter/v1/dynamic' => 'otter/v1/dynamic',
+ );
+ foreach ( $queries as $query => $expected ) {
+ $result = do_blocks( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( $expected, $processor->get_attribute( 'title' ), $result );
+ $this->assertSame( 'https://example.org/', $processor->get_attribute( 'href' ), $result );
+ }
+ }
+
+ public function provide_visitor_dynamic_urls(): array {
+ return array(
+ 'dynamic link' => array( '#otterDynamicLink?type=siteURL' ),
+ 'dynamic image link' => array( get_rest_url( null, 'otter/v1/dynamic' ) ),
+ );
+ }
+
+ /**
+ * @dataProvider provide_split_dynamic_markers
+ */
+ public function test_dynamic_markers_assembled_from_resolved_fragments_remain_literal( string $prefix, string $fragment, string $expected ): void {
+ $this->configure_pro_dynamic_content_license( true );
+ update_post_meta( $this->post_id, 'marker_prefix', $prefix );
+ wp_update_post( array( 'ID' => $this->post_id, 'post_title' => $fragment ) );
+ $this->go_to( get_permalink( $this->post_id ) );
+ foreach ( array( $prefix, '{otterDynamic?type=postMeta&metaKey=marker_prefix}' ) as $source ) {
+ $result = do_blocks( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( $expected, $processor->get_attribute( 'href' ), $result );
+ }
+
+ $texts = array(
+ '{otterDynamic?type=postMeta&metaKey=marker_prefix}{otterDynamic?type=postTitle}',
+ esc_html( $prefix ) . '{otterDynamic?type=postTitle}',
+ 'PrefixTitle',
+ 'Title',
+ );
+ foreach ( $texts as $text ) {
+ $result = do_blocks( '' );
+ $this->assertSame( $expected, html_entity_decode( trim( wp_strip_all_tags( $result ) ) ), $result );
+ }
+ }
+
+ public function provide_split_dynamic_markers(): array {
+ return array(
+ 'dynamic link' => array( '#otterDynamic', 'Link?type=siteURL', '#otterDynamicLink?type=siteURL' ),
+ 'dynamic image' => array( get_rest_url( null, 'otter/v1/' ), 'dynamic', get_rest_url( null, 'otter/v1/dynamic' ) ),
+ );
+ }
+
+ public function test_resolved_link_urls_keep_dynamic_markers_literal(): void {
+ $attachment_id = self::factory()->attachment->create_object( 'website-image.png', $this->post_id, array( 'post_mime_type' => 'image/png' ) );
+ set_post_thumbnail( $this->post_id, $attachment_id );
+ $website = get_rest_url( null, 'otter/v1/dynamic/' );
+ $website .= ( false === strpos( $website, '?' ) ? '?' : '&' ) . 'type=featuredImage';
+ wp_update_user( array( 'ID' => $this->user_id, 'user_url' => $website ) );
+ $this->go_to( get_permalink( $this->post_id ) );
+ foreach ( array( 'Website', 'Website' ) as $link ) {
+ $result = do_blocks( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( $website, $processor->get_attribute( 'href' ), $result );
+ }
+ }
+
+ public function test_resolved_link_lists_keep_dynamic_markers_literal(): void {
+ $url = get_rest_url( null, 'otter/v1/dynamic/' );
+ $url .= ( false === strpos( $url, '?' ) ? '?' : '&' ) . 'type=logo';
+ $urls = array( $url, 'https://ok.example/' );
+ add_filter(
+ 'otter_blocks_evaluate_dynamic_content_link',
+ function () use ( $urls ): array {
+ return $urls;
+ }
+ );
+
+ $links = $this->dynamic_content->apply_link_button( array( '#otterDynamicLink?type=otterTestLink' ) );
+ $this->assertStringNotContainsString( 'otter/v1/dynamic', $links[0] );
+ $this->assertSame( $urls, array_map( 'html_entity_decode', $links ) );
+
+ $result = $this->dynamic_content->apply_dynamic_link( 'Go' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertStringNotContainsString( 'otter/v1/dynamic', $result );
+ foreach ( $urls as $url ) {
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( $url, $processor->get_attribute( 'href' ), $result );
+ }
+ }
+
+ public function test_resolved_image_urls_keep_dynamic_markers_literal(): void {
+ $image = 'https://example.org/image.png#otterDynamicLink?type=siteURL';
+ add_filter(
+ 'otter_blocks_evaluate_dynamic_content_media_content',
+ function () use ( $image ): string {
+ return $image;
+ }
+ );
+ $src = get_rest_url( null, 'otter/v1/dynamic/' );
+ $src .= ( false === strpos( $src, '?' ) ? '?' : '&' ) . 'type=logo';
+
+ $result = do_blocks( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'img' ), $result );
+ $this->assertSame( $image, $processor->get_attribute( 'src' ), $result );
+ }
+
+ public function test_resolved_image_lists_keep_dynamic_markers_literal(): void {
+ $images = array( 'https://example.org/one.png#otterDynamicLink?type=siteURL', 'https://example.org/two.png' );
+ add_filter(
+ 'otter_blocks_evaluate_dynamic_content_media_content',
+ function () use ( $images ): array {
+ return $images;
+ }
+ );
+ $src = get_rest_url( null, 'otter/v1/dynamic/' );
+ $src .= ( false === strpos( $src, '?' ) ? '?' : '&' ) . 'type=logo';
+
+ $result = $this->dynamic_content->apply_dynamic_images( '
' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertStringNotContainsString( 'otterDynamicLink', $result );
+ foreach ( $images as $image ) {
+ $this->assertTrue( $processor->next_tag( 'img' ), $result );
+ $this->assertSame( $image, $processor->get_attribute( 'src' ), $result );
+ }
+ }
+
+ public function test_dynamic_background_css_keeps_resolved_image_url(): void {
+ $image = 'https://example.org/wp-content/uploads/otter/v1/dynamic/background.png';
+ add_filter(
+ 'otter_blocks_evaluate_dynamic_content_media_content',
+ function () use ( $image ): string {
+ return $image;
+ }
+ );
+ $src = get_rest_url( null, 'otter/v1/dynamic/' );
+ $src .= ( false === strpos( $src, '?' ) ? '?' : '&' ) . 'type=logo';
+
+ $css = ( new \ThemeIsle\GutenbergBlocks\CSS\Blocks\Advanced_Column_CSS() )->render_css(
+ array(
+ 'attrs' => array(
+ 'id' => 'wp-block-themeisle-blocks-advanced-column-background',
+ 'backgroundType' => 'image',
+ 'backgroundImageURL' => $src,
+ ),
+ )
+ );
+
+ $this->assertStringContainsString( 'url( ' . $image . ' )', $css );
+ }
+
+ /**
+ * @dataProvider provide_resolved_dynamic_elements
+ */
+ public function test_resolved_dynamic_elements_remain_literal_in_nested_blocks( string $element ): void {
+ wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ wp_update_post( array( 'ID' => $this->post_id, 'post_title' => $element ) );
+ $this->assertSame( $element, get_post_field( 'post_title', $this->post_id ) );
+ $this->go_to( get_permalink( $this->post_id ) );
+
+ foreach ( array( '{otterDynamic?type=postTitle}', 'Title' ) as $source ) {
+ $result = do_blocks( '' );
+ $this->assertStringContainsString( $element, html_entity_decode( $result ), $result );
+ $this->assertStringNotContainsString( 'assertStringNotContainsString( get_bloginfo( 'name' ), $result );
+ }
+
+ $result = do_blocks( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'p' ), $result );
+ $this->assertSame( $element, $processor->get_attribute( 'title' ), $result );
+ }
+
+ public function provide_resolved_dynamic_elements(): array {
+ return array(
+ 'dynamic link element' => array( 'Site' ),
+ 'dynamic content element' => array( 'Site' ),
+ );
+ }
+
+ public function test_literal_marker_collision_preserves_comment_and_existing_attribute(): void {
+ $next_id = (int) wp_unique_id() + 1;
+ $marker = 'otter-literal-magic-' . $next_id;
+ $token = '{otterDynamic?type=postTitle}';
+ $content = '{otterDynamic?type=postTitle}';
+ $result = $this->dynamic_content->apply_dynamic_magic_tags( 'Link' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag(), $result );
+ $this->assertSame( $token, $processor->get_attribute( 'href' ), $result );
+ $this->assertSame( $marker, $processor->get_attribute( 'data-marker' ), $result );
+ $this->assertSame( 'Test', $processor->get_attribute( 'title' ), $result );
+ }
+
+ public function test_resolved_marker_collision_preserves_other_attribute(): void {
+ $this->configure_pro_dynamic_content_license( true );
+ $token = '#otterDynamicLink?type=siteURL';
+ wp_update_post( array( 'ID' => $this->post_id, 'post_title' => $token ) );
+ $this->go_to( get_permalink( $this->post_id ) );
+ $marker = 'otter-literal-magic-' . ( (int) wp_unique_id() + 1 );
+ update_post_meta( $this->post_id, 'literal_marker', $marker );
+ $result = $this->dynamic_content->apply_dynamic_magic_tags( 'Value
' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'p' ), $result );
+ $this->assertSame( $token, $processor->get_attribute( 'title' ), $result );
+ $this->assertSame( $marker, $processor->get_attribute( 'data-marker' ), $result );
+
+ $marker = 'otter-literal-magic-' . ( (int) wp_unique_id() + 1 );
+ update_post_meta( $this->post_id, 'literal_marker', $marker );
+ $result = $this->dynamic_content->apply_dynamic_link_button(
+ $this->dynamic_content->apply_dynamic_magic_tags( 'Link' )
+ );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( $marker, $processor->get_attribute( 'data-marker' ), $result );
+ $this->assertSame( site_url(), $processor->get_attribute( 'href' ), $result );
+ }
+
+ public function test_comment_avatar_magic_tags_remain_literal_in_nested_blocks(): void {
+ wp_set_current_user( 0 );
+ update_option( 'comment_registration', 0 );
+ update_option( 'require_name_email', 0 );
+ update_option( 'show_avatars', 1 );
+ wp_update_post( array( 'ID' => $this->post_id, 'comment_status' => 'open' ) );
+ $token = '{otterDynamic?type=postTitle}';
+ $comment = wp_handle_comment_submission(
+ array(
+ 'comment_post_ID' => $this->post_id,
+ 'author' => $token,
+ 'url' => 'https://example.org/',
+ 'comment' => 'Visitor comment',
+ )
+ );
+ $this->assertInstanceOf( WP_Comment::class, $comment );
+ $this->assertSame( $token, $comment->comment_author );
+ foreach ( array( false, true ) as $is_link ) {
+ $block = new WP_Block(
+ array( 'blockName' => 'core/avatar', 'attrs' => array( 'isLink' => $is_link, 'linkTarget' => '_blank', 'className' => 'visitor-' . $token ) ),
+ array( 'commentId' => (int) $comment->comment_ID )
+ );
+ $avatar = $block->render();
+ $processor = new WP_HTML_Tag_Processor( $avatar );
+ $this->assertTrue( $processor->next_tag( 'div' ), $avatar );
+ $this->assertStringContainsString( 'visitor-Test', $processor->get_attribute( 'class' ), $avatar );
+ $result = do_blocks( '' . $avatar . '
' . $token . '
' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ if ( $is_link ) {
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertStringContainsString( $token, $processor->get_attribute( 'aria-label' ), $result );
+ }
+ $this->assertTrue( $processor->next_tag( 'img' ), $result );
+ $this->assertStringContainsString( $token, $processor->get_attribute( 'alt' ), $result );
+ $this->assertStringContainsString( '>Test', $result );
+ }
+ update_option( 'show_avatars', 0 );
+ $processor = new WP_HTML_Tag_Processor( $block->render() );
+ $this->assertFalse( $processor->next_tag( 'img' ) );
+ }
+
+ public function test_author_archive_magic_tags_remain_literal_in_nested_blocks(): void {
+ require_once ABSPATH . 'wp-admin/includes/user.php';
+ $token = '{otterDynamic?type=siteTitle}';
+ wp_set_current_user( $this->user_id );
+ $this->assertTrue( current_user_can( 'edit_user', $this->user_id ) );
+ $this->assertFalse( current_user_can( 'edit_users' ) );
+ $previous_post = $_POST;
+ try {
+ $_POST = array( 'display_name' => $token, 'nickname' => 'Visitor', 'email' => 'test@userrecover.com' );
+ $this->assertSame( $this->user_id, edit_user( $this->user_id ) );
+ } finally {
+ $_POST = $previous_post;
+ }
+ $this->assertSame( $token, get_userdata( $this->user_id )->display_name );
+ wp_set_current_user( 0 );
+ update_option( 'posts_per_page', 1 );
+ self::factory()->post->create( array( 'post_author' => $this->user_id ) );
+ $has_breadcrumbs = WP_Block_Type_Registry::get_instance()->is_registered( 'core/breadcrumbs' );
+ add_filter( 'block_core_breadcrumbs_items', function ( $items ) use ( $token ) {
+ $items['formatted'] = array( 'label' => '' . $token . '', 'url' => 'https://example.org/reference', 'allow_html' => true );
+ return $items;
+ }, 9 );
+ $author_url = get_author_posts_url( $this->user_id );
+ foreach ( array( $author_url, add_query_arg( 'paged', 2, $author_url ) ) as $url ) {
+ $this->go_to( $url );
+ $this->assertTrue( is_author() );
+ $GLOBALS['wp_query']->the_post();
+ $result = do_blocks( '' );
+ $this->assertSame( $has_breadcrumbs ? 3 : 1, substr_count( html_entity_decode( $result ), $token ), $result );
+ $this->assertStringContainsString( 'Author:', $result );
+ $this->assertStringContainsString( '>' . get_bloginfo( 'name' ) . '', $result );
+ if ( $has_breadcrumbs ) {
+ $this->assertStringContainsString( '>' . $token . ( is_paged() ? '' : '' ), html_entity_decode( $result ), $result );
+ $this->assertStringContainsString( 'trusted-' . get_bloginfo( 'name' ), $result );
+ $this->assertStringContainsString( '' . $token . '', html_entity_decode( $result ), $result );
+ $this->assertStringContainsString( 'href="https://example.org/reference"', $result );
+ }
+ }
+ }
+
+ public function test_profile_magic_tags_remain_literal_in_nested_blocks(): void {
+ $token = '{otterDynamic?type=postTitle}';
+ wp_update_user( array( 'ID' => $this->user_id, 'display_name' => $token, 'description' => $token ) );
+ wp_set_current_user( $this->user_id );
+ $markup = '';
+ $result = do_blocks( $markup );
+ $this->assertStringContainsString( $token, html_entity_decode( $result ), $result );
+ $this->assertStringContainsString( 'wp-block-post-author__name">' . $token, html_entity_decode( $result ), $result );
+ $this->assertStringContainsString( 'wp-block-post-author__bio">' . $token, html_entity_decode( $result ), $result );
+ $this->assertStringContainsString( 'wp-block-post-author-biography">' . $token, html_entity_decode( $result ), $result );
+ $this->assertStringContainsString( '>Test', $result );
+
+ foreach ( array( 'authorName', 'authorDescription', 'loggedInUserName', 'loggedInUserDescription' ) as $type ) {
+ $result = do_blocks( '{otterDynamic?type=' . $type . '}
' );
+ $this->assertSame( 2, substr_count( html_entity_decode( $result ), $token ), $result );
+ $this->assertStringNotContainsString( 'Test', $result );
+ }
+ }
+
+ public function test_comment_form_identity_magic_tags_remain_literal_in_nested_blocks(): void {
+ $token = '{otterDynamic?type=postTitle}';
+ $email = $token . '@example.org';
+ $this->assertSame( $email, sanitize_email( $email ) );
+ wp_update_post( array( 'ID' => $this->post_id, 'comment_status' => 'open' ) );
+ $_COOKIE[ 'comment_author_' . COOKIEHASH ] = $token;
+ $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] = $email;
+ $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] = 'https://example.org/' . $token;
+ wp_update_user( array( 'ID' => $this->user_id, 'display_name' => $token ) );
+
+ try {
+ foreach ( array( 0, $this->user_id ) as $user_id ) {
+ wp_set_current_user( $user_id );
+ $result = do_blocks( '' );
+ $this->assertStringContainsString( $token, html_entity_decode( $result ), $result );
+ $this->assertStringContainsString( '>Test', $result );
+ $this->assertStringNotContainsString( 'value="Test"', $result );
+ $this->assertStringNotContainsString( 'Logged in as Test', $result );
+ if ( 0 === $user_id ) {
+ $processor = new WP_HTML_Tag_Processor( $result );
+ while ( $processor->next_tag( 'input' ) && 'email' !== $processor->get_attribute( 'name' ) ) {
+ }
+ $this->assertSame( $email, $processor->get_attribute( 'value' ), $result );
+ }
+ }
+ } finally {
+ unset( $_COOKIE[ 'comment_author_' . COOKIEHASH ], $_COOKIE[ 'comment_author_email_' . COOKIEHASH ], $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] );
+ }
+ }
+
+ /**
+ * @dataProvider provide_literal_search_queries
+ *
+ * @param string $query Visitor search text.
+ */
+ public function test_search_magic_tags_remain_literal_in_nested_blocks( string $query ): void {
+ $token = '{otterDynamic?type=siteTitle}';
+ wp_set_current_user( 0 );
+ $this->go_to( add_query_arg( 's', rawurlencode( $query ), home_url( '/' ) ) );
+ $this->assertTrue( is_search() );
+ $this->assertSame( $query, get_query_var( 's' ) );
+ $has_breadcrumbs = WP_Block_Type_Registry::get_instance()->is_registered( 'core/breadcrumbs' );
+ $search_attributes = wp_json_encode( array( 'placeholder' => $token, 'className' => 'trusted-' . $token, 'query' => array( 'visitor' => $token ) ) );
+ $result = do_blocks( '' );
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'input' ), $result );
+ $this->assertSame( $query, $processor->get_attribute( 'value' ), $result );
+ $this->assertSame( get_bloginfo( 'name' ), $processor->get_attribute( 'placeholder' ), $result );
+ $this->assertTrue( $processor->next_tag( 'input' ), $result );
+ $this->assertSame( 'visitor', $processor->get_attribute( 'name' ), $result );
+ $this->assertSame( get_bloginfo( 'name' ), $processor->get_attribute( 'value' ), $result );
+ $this->assertStringContainsString( 'trusted-' . get_bloginfo( 'name' ), $result );
+ $this->assertSame( substr_count( $query, $token ) + ( $has_breadcrumbs ? 2 : 1 ) * substr_count( html_entity_decode( $query ), $token ), substr_count( html_entity_decode( $result ), $token ), $result );
+ $this->assertStringContainsString( '>' . get_bloginfo( 'name' ) . '', $result );
+ $this->assertSame( $query, get_query_var( 's' ) );
+ }
+
+ /**
+ * @dataProvider provide_literal_search_queries
+ *
+ * @param string $query Visitor search text.
+ */
+ public function test_legacy_search_widget_magic_tags_remain_literal_in_nested_blocks( string $query ): void {
+ $token = '{otterDynamic?type=siteTitle}';
+ wp_set_current_user( 0 );
+ $this->go_to( add_query_arg( 's', rawurlencode( $query ), home_url( '/' ) ) );
+ foreach ( array( 'html5', 'xhtml', 'single-quoted' ) as $format ) {
+ $format_filter = function () use ( $format ) { return 'single-quoted' === $format ? 'html5' : $format; };
+ $form_filter = function ( $form ) use ( $format ) {
+ return 'single-quoted' === $format ? str_replace( '"', "'", $form ) : $form;
+ };
+ add_filter( 'search_form_format', $format_filter );
+ add_filter( 'get_search_form', $form_filter, 9 );
+ try {
+ $result = do_blocks( '' );
+ } finally {
+ remove_filter( 'search_form_format', $format_filter );
+ remove_filter( 'get_search_form', $form_filter, 9 );
+ }
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'input' ), $result );
+ $this->assertSame( 'xhtml' === $format ? 'text' : 'search', $processor->get_attribute( 'type' ), $result );
+ $this->assertSame( html_entity_decode( $query ), $processor->get_attribute( 'value' ), $result );
+ $this->assertStringContainsString( '>' . get_bloginfo( 'name' ) . '', $result );
+ $this->assertSame( $query, get_search_query( false ) );
+ }
+ }
+
+ /**
+ * @dataProvider provide_literal_search_queries
+ *
+ * @param string $query Visitor search text.
+ */
+ public function test_search_links_preserve_literal_queries( string $query ): void {
+ wp_set_current_user( 0 );
+ $previous_structure = get_option( 'permalink_structure' );
+ try {
+ foreach ( array( '', '/%postname%/' ) as $structure ) {
+ $this->set_permalink_structure( $structure );
+ $this->go_to( add_query_arg( 's', rawurlencode( $query ), home_url( '/' ) ) );
+ $search_link = get_search_link();
+ $feed_link = get_search_feed_link();
+ $this->assertSame( get_search_link( $query ), $search_link );
+ $this->assertSame( get_search_feed_link( $query ), $feed_link );
+ $this->assertSame( $query, get_search_query( false ) );
+ $this->go_to( $search_link );
+ $this->assertSame( $query, get_query_var( 's' ) );
+ $this->go_to( $feed_link );
+ $this->assertTrue( is_feed() );
+ $this->assertSame( $query, get_query_var( 's' ) );
+ }
+ } finally {
+ $this->set_permalink_structure( $previous_structure );
+ }
+ }
+
+ public function test_null_search_form_filter_preserves_fallback_and_literal_queries(): void {
+ $query = '{otterDynamic?type=siteTitle}';
+ wp_set_current_user( 0 );
+ $this->go_to( add_query_arg( 's', rawurlencode( $query ), home_url( '/' ) ) );
+ add_filter( 'get_search_form', '__return_null', 9 );
+ ob_start();
+ try {
+ get_search_form();
+ } finally {
+ $form = ob_get_clean();
+ remove_filter( 'get_search_form', '__return_null', 9 );
+ }
+ $processor = new WP_HTML_Tag_Processor( $form );
+ $this->assertTrue( $processor->next_tag( 'input' ), $form );
+ $this->assertSame( $query, $processor->get_attribute( 'value' ), $form );
+
+ add_filter( 'get_search_form', '__return_null', 9 );
+ try {
+ $result = do_blocks( '' );
+ } finally {
+ remove_filter( 'get_search_form', '__return_null', 9 );
+ }
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'input' ), $result );
+ $this->assertSame( $query, $processor->get_attribute( 'value' ), $result );
+ $this->assertStringContainsString( '>' . get_bloginfo( 'name' ) . '', $result );
+ $this->assertSame( $query, get_search_query( false ) );
+ }
+
+ public function test_false_search_form_filter_preserves_disabled_form(): void {
+ add_filter( 'get_search_form', '__return_false', 9 );
+ try {
+ $this->assertFalse( get_search_form( array( 'echo' => false ) ) );
+ } finally {
+ remove_filter( 'get_search_form', '__return_false', 9 );
+ }
+ }
+
+ /**
+ * @dataProvider provide_term_label_blocks
+ *
+ * @param string $name Block name.
+ * @param array $attributes Block attributes.
+ * @param bool $has_aria_label Whether the term link includes a label.
+ */
+ public function test_term_names_remain_literal_in_nested_blocks( string $name, array $attributes, bool $has_aria_label ): void {
+ $token = '{otterDynamic?type=siteTitle}';
+ $user = get_userdata( $this->user_id );
+ $user->set_role( 'contributor' );
+ wp_set_current_user( $this->user_id );
+ $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
+ $request->set_param( 'name', $token );
+ $request->set_param( 'slug', 'literal-magic-tag' );
+ $request->set_param( 'description', '' . $token . '' );
+ $response = rest_do_request( $request );
+ $this->assertSame( 201, $response->get_status(), wp_json_encode( $response->get_data() ) );
+ $term_id = $response->get_data()['id'];
+ $this->assertNotWPError( wp_set_post_terms( $this->post_id, array( $term_id ), 'post_tag' ) );
+ wp_set_current_user( 0 );
+ $this->go_to( get_term_link( $term_id, 'post_tag' ) );
+ $GLOBALS['wp_query']->the_post();
+ if ( 'core/post-terms' === $name ) {
+ $attributes['prefix'] = 'Prefix ' . $token;
+ $attributes['suffix'] = 'Suffix ' . $token;
+ }
+ $block = get_comment_delimited_block_content( $name, $attributes, '' );
+ $result = do_blocks( '' . $block . '
' . $token . '
' );
+ $this->assertSame( 1, substr_count( wp_strip_all_tags( html_entity_decode( $result ) ), $token ), $result );
+ $this->assertStringContainsString( 'trusted-' . get_bloginfo( 'name' ), $result );
+ $this->assertStringContainsString( '>' . get_bloginfo( 'name' ) . '', $result );
+ if ( 'core/post-terms' === $name ) {
+ $this->assertStringContainsString( 'Prefix ' . get_bloginfo( 'name' ), $result );
+ $this->assertStringContainsString( 'Suffix ' . get_bloginfo( 'name' ), $result );
+ }
+ if ( $has_aria_label ) {
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertStringContainsString( $token, $processor->get_attribute( 'aria-label' ), $result );
+ }
+ if ( 'core/term-description' === $name ) {
+ $processor = new WP_HTML_Tag_Processor( $result );
+ $this->assertTrue( $processor->next_tag( 'a' ), $result );
+ $this->assertSame( $token, $processor->get_attribute( 'title' ), $result );
+ $this->assertStringContainsString( $token, get_term( $term_id, 'post_tag' )->description );
+ }
+ $this->assertSame( $token, get_term( $term_id, 'post_tag' )->name );
+ $this->assertSame( $token, get_the_terms( $this->post_id, 'post_tag' )[0]->name );
+ }
+
+ public function provide_term_label_blocks(): array {
+ $cases = array(
+ 'term description' => array( 'core/term-description', array(), false ),
+ 'post terms' => array( 'core/post-terms', array( 'term' => 'post_tag' ), false ),
+ 'tag cloud' => array( 'core/tag-cloud', array( 'showTagCounts' => true ), true ),
+ 'legacy tag cloud' => array( 'core/legacy-widget', array( 'idBase' => 'tag_cloud' ), true ),
+ );
+ $registry = WP_Block_Type_Registry::get_instance();
+ if ( $registry->is_registered( 'core/term-name' ) ) {
+ $cases['term name'] = array( 'core/term-name', array(), false );
+ $cases['linked term name'] = array( 'core/term-name', array( 'isLink' => true ), false );
+ }
+ $categories = $registry->get_registered( 'core/categories' );
+ if ( isset( $categories->attributes['taxonomy'] ) ) {
+ $cases['category list'] = array( 'core/categories', array( 'taxonomy' => 'post_tag' ), false );
+ $cases['category dropdown'] = array( 'core/categories', array( 'taxonomy' => 'post_tag', 'displayAsDropdown' => true ), false );
+ }
+ return $cases;
+ }
+
+ public function test_posts_grid_category_names_remain_literal_in_nested_blocks(): void {
+ $token = '{otterDynamic?type=siteTitle}';
+ $this->assertNotWPError( wp_update_term( $this->category_id, 'category', array( 'name' => $token ) ) );
+ foreach ( array( false, true ) as $display_meta_category ) {
+ $attributes = array(
+ 'template' => array( 'category', 'meta' ),
+ 'displayCategory' => ! $display_meta_category,
+ 'displayPostCategory' => $display_meta_category,
+ 'displayDate' => false,
+ 'displayAuthor' => false,
+ 'displayComments' => false,
+ 'displayFeaturedImage' => false,
+ );
+ $block = get_comment_delimited_block_content( 'themeisle-blocks/posts-grid', $attributes, '' );
+ $result = do_blocks( '' . $block . '
' . $token . '
' );
+ $this->assertStringContainsString( '>' . $token . '', html_entity_decode( $result ), $result );
+ $this->assertStringContainsString( '>' . get_bloginfo( 'name' ) . '', $result );
+ $this->assertSame( $token, get_term( $this->category_id, 'category' )->name );
+ }
+ }
+
+ public function test_converted_taxonomy_menu_labels_remain_literal_in_nested_blocks(): void {
+ $token = '{otterDynamic?type=siteTitle}';
+ $term_id = self::factory()->term->create( array( 'taxonomy' => 'post_tag', 'name' => $token ) );
+ foreach ( array( false, true ) as $has_children ) {
+ foreach ( array( '', 'Authored ' . $token, '0' ) as $title ) {
+ $menu_id = wp_create_nav_menu( 'Literal taxonomy menu' );
+ $this->assertNotWPError( $menu_id );
+ try {
+ $item_id = wp_update_nav_menu_item( $menu_id, 0, array(
+ 'menu-item-type' => 'taxonomy',
+ 'menu-item-object' => 'post_tag',
+ 'menu-item-object-id' => $term_id,
+ 'menu-item-title' => $title,
+ 'menu-item-status' => 'publish',
+ ) );
+ $this->assertNotWPError( $item_id );
+ $this->assertSame( $title, get_post( $item_id )->post_title );
+ if ( $has_children ) {
+ $this->assertNotWPError( wp_update_nav_menu_item( $menu_id, 0, array(
+ 'menu-item-type' => 'custom',
+ 'menu-item-title' => 'Child',
+ 'menu-item-url' => home_url( '/' ),
+ 'menu-item-parent-id' => $item_id,
+ 'menu-item-status' => 'publish',
+ ) ) );
+ }
+ $items = wp_get_nav_menu_items( $menu_id );
+ $this->assertSame( '' === $title ? str_replace( '{', '{', $token ) : $title, $items[0]->title );
+ $blocks = WP_Classic_To_Block_Menu_Converter::convert( wp_get_nav_menu_object( $menu_id ) );
+ $this->assertNotWPError( $blocks );
+ $this->assertSame( $has_children ? 'core/navigation-submenu' : 'core/navigation-link', parse_blocks( $blocks )[0]['blockName'] );
+ $result = do_blocks( '' . $blocks . '
' . $token . '
' );
+ $expected = '' === $title ? $token : str_replace( $token, get_bloginfo( 'name' ), $title );
+ if ( '0' !== $title ) {
+ $this->assertStringContainsString( '>' . $expected . '', html_entity_decode( $result ), $result );
+ }
+ $this->assertStringContainsString( '>' . get_bloginfo( 'name' ) . '', $result );
+ $this->assertSame( $token, get_term( $term_id, 'post_tag' )->name );
+ } finally {
+ wp_delete_nav_menu( $menu_id );
+ }
+ }
+ }
+ }
+
+ public function provide_literal_search_queries(): array {
+ return array(
+ 'literal token' => array( '{otterDynamic?type=siteTitle}' ),
+ 'encoded prefix' => array( '{otterDynamic?type=siteTitle}' ),
+ 'mixed prefixes' => array( '{otterDynamic?type=siteTitle} {otterDynamic?type=siteTitle}' ),
+ );
+ }
+
+ public function test_query_string_magic_tags_remain_literal_in_nested_blocks(): void {
+ $token = '{otterDynamic?type=postTitle}';
+ $this->configure_pro_dynamic_content_license( false );
+ $this->assertFalse( has_filter( 'otter_blocks_evaluate_dynamic_content_text', array( $this->dynamic_content_pro, 'evaluate_content' ) ) );
+ $this->configure_pro_dynamic_content_license( true );
+ $this->go_to( add_query_arg( 'visitor', $token, get_permalink( $this->post_id ) ) );
+ foreach ( array( 'default', 'capitalize' ) as $format ) {
+ $result = do_blocks( '{otterDynamic?type=queryString¶meter=visitor&format=' . $format . '}
' );
+ $this->assertSame( 2, substr_count( html_entity_decode( $result ), $token ), $result );
+ $this->assertStringNotContainsString( 'Test', $result );
+ }
+ }
+
+ public function test_resolved_list_magic_tags_remain_literal_in_nested_blocks(): void {
+ $token = '{otterDynamic?type=postTitle}';
+ add_filter( 'otter_blocks_evaluate_dynamic_content_text', function () use ( $token ) { return array( $token, 'Other ' . $token ); } );
+ $result = do_blocks( '' );
+ $this->assertSame( 2, substr_count( html_entity_decode( $result ), $token ), $result );
+ $this->assertStringContainsString( 'Other ' . $token, html_entity_decode( $result ), $result );
+ $this->assertStringNotContainsString( 'Test', $result );
+ }
+
+ /**
+ * @dataProvider provide_dynamic_image_post_contexts
+ *
+ * @param array{post_status: string, post_password?: string, post_type?: string} $post_args Context post.
+ */
+ public function test_archive_comment_dynamic_images_enforce_post_permissions( array $post_args ): void {
+ $this->configure_pro_dynamic_content_license( true );
+ update_option( 'comment_registration', 0 );
+ update_option( 'require_name_email', 0 );
+ wp_update_post( array( 'ID' => $this->post_id, 'comment_status' => 'open' ) );
+ $subscriber_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
+ $editor_id = self::factory()->user->create( array( 'role' => 'editor' ) );
+ $this->set_permalink_structure( '/%postname%/' );
+
+ try {
+ $context_id = self::factory()->post->create( $post_args );
+ $secret_url = 'https://example.org/confidential-' . $context_id . '.png';
+ update_post_meta( $context_id, 'confidential_image', $secret_url );
+ $url = get_rest_url( null, 'otter/v1/dynamic/' ) . '?type=postMeta&context=' . $context_id . '&meta=confidential_image&uid=1';
+ wp_set_current_user( 0 );
+ $comment = wp_handle_comment_submission(
+ array(
+ 'comment_post_ID' => $this->post_id,
+ 'author' => 'Visitor ' . $context_id,
+ 'url' => $url,
+ 'comment' => 'Visitor comment ' . $context_id,
+ )
+ );
+ $this->assertInstanceOf( WP_Comment::class, $comment );
+ $this->assertSame( $url, html_entity_decode( $comment->comment_author_url ) );
+ wp_set_comment_status( $comment->comment_ID, 'approve' );
+ $this->go_to( get_category_link( $this->category_id ) );
+ $this->assertTrue( is_archive() );
+ $block = new WP_Block(
+ array(
+ 'blockName' => 'core/latest-comments',
+ 'attrs' => array( 'commentsToShow' => 100, 'displayAvatar' => false, 'displayExcerpt' => false ),
+ )
+ );
+ $image = '
';
+ foreach ( array( 0, $subscriber_id, $editor_id ) as $user_id ) {
+ wp_set_current_user( $user_id );
+ $comment_markup = $block->render();
+ $processor = new WP_HTML_Tag_Processor( $comment_markup );
+ $this->assertTrue( $processor->next_tag( 'a' ), $comment_markup );
+ $this->assertSame( $url, $processor->get_attribute( 'href' ), $comment_markup );
+ $this->assertStringNotContainsString( $secret_url, $comment_markup );
+ $result = do_blocks( '' . $comment_markup . $image . '
' );
+ if ( $user_id === $editor_id ) {
+ $this->assertStringContainsString( $secret_url, $result );
+ } else {
+ $this->assertStringNotContainsString( $secret_url, $result );
+ $this->assertStringContainsString( 'assets/images/placeholder.jpg', $result );
+ }
+ }
+ wp_update_post( array( 'ID' => $context_id, 'post_status' => 'publish', 'post_password' => '', 'post_type' => 'post' ) );
+ wp_set_current_user( 0 );
+ $this->assertStringContainsString( $secret_url, do_blocks( $image ) );
+ } finally {
+ $this->set_permalink_structure( '' );
+ }
+ }
+
+ public function provide_dynamic_image_post_contexts(): array {
+ return array(
+ 'private' => array( array( 'post_status' => 'private' ) ),
+ 'draft' => array( array( 'post_status' => 'draft' ) ),
+ 'password protected' => array( array( 'post_status' => 'publish', 'post_password' => 'secret' ) ),
+ 'non-viewable' => array( array( 'post_status' => 'publish', 'post_type' => 'wp_block' ) ),
+ );
+ }
+
+ /**
+ * @dataProvider provide_dynamic_image_widget_contexts
+ *
+ * @param string $context_query Serialized widget context.
+ */
+ public function test_dynamic_widget_images_use_global_post_permissions( string $context_query ): void {
+ $attachment_id = self::factory()->attachment->create_object( 'widget-image.png', $this->post_id, array( 'post_mime_type' => 'image/png' ) );
+ set_post_thumbnail( $this->post_id, $attachment_id );
+ $image_url = get_the_post_thumbnail_url( $this->post_id );
+ $this->assertNotEmpty( $image_url );
+ $editor_id = self::factory()->user->create( array( 'role' => 'editor' ) );
+ foreach ( array( get_category_link( $this->category_id ), home_url( '/' ), get_author_posts_url( $this->user_id ) ) as $url ) {
+ wp_set_current_user( 0 );
+ wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'publish' ) );
+ $this->go_to( $url );
+ $this->assertFalse( is_singular() );
+ $this->assertSame( $this->post_id, get_the_ID() );
+ foreach ( array( 'featuredImage' => $image_url, 'author' => get_avatar_url( $this->user_id ) ) as $type => $expected_url ) {
+ $query = get_rest_url( null, 'otter/v1/dynamic/' );
+ $query .= ( false === strpos( $query, '?' ) ? '?' : '&' ) . 'type=' . $type . $context_query . '&uid=1';
+ $markup = '';
+ $result = do_blocks( $markup );
+ $this->assertStringContainsString( $expected_url, html_entity_decode( $result ), $result );
+ wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'private' ) );
+ $result = do_blocks( $markup );
+ $this->assertStringNotContainsString( $expected_url, html_entity_decode( $result ), $result );
+ $this->assertStringContainsString( 'assets/images/placeholder.jpg', $result );
+ wp_set_current_user( $editor_id );
+ $result = do_blocks( $markup );
+ $this->assertStringContainsString( $expected_url, html_entity_decode( $result ), $result );
+ wp_set_current_user( 0 );
+ wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'publish' ) );
+ }
+ }
+ wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'draft' ) );
+ $this->go_to( get_category_link( $this->category_id ) );
+ $this->assertFalse( get_the_ID() );
+ $this->assertStringContainsString( 'assets/images/placeholder.jpg', do_blocks( $markup ) );
+ }
+
+ public function provide_dynamic_image_widget_contexts(): array {
+ return array(
+ 'zero' => array( '&context=0' ),
+ 'empty' => array( '&context=' ),
+ 'null' => array( '&context' ),
+ 'missing' => array( '' ),
+ 'query' => array( '&context=query' ),
+ );
+ }
+
+ public function test_dynamic_product_images_require_access_to_product_and_context(): void {
+ $this->configure_pro_dynamic_content_license( true );
+ $attachment_id = self::factory()->attachment->create_object( 'product-image.png', $this->post_id, array( 'post_mime_type' => 'image/png' ) );
+ $image_url = wp_get_attachment_image_url( $attachment_id, 'full' );
+ $this->assertNotEmpty( $image_url );
+ $product = new WC_Product_Simple();
+ $product->set_status( 'private' );
+ $product->set_image_id( $attachment_id );
+ $product_id = $product->save();
+ $this->go_to( get_category_link( $this->category_id ) );
+ $query = get_rest_url( null, 'otter/v1/dynamic/' );
+ $query .= ( false === strpos( $query, '?' ) ? '?' : '&' ) . 'type=product&context=' . $this->post_id . '&id=' . $product_id . '&uid=1';
+ wp_set_current_user( 0 );
+ $result = $this->dynamic_content->apply_dynamic_images( '
' );
+ $this->assertStringNotContainsString( $image_url, $result );
+ $this->assertStringContainsString( 'assets/images/placeholder.jpg', $result );
+ $fallback_result = $this->dynamic_content->apply_dynamic_images( '
' );
+ $this->assertStringContainsString( 'https://example.org/safe-fallback.png', $fallback_result );
+ $this->assertStringNotContainsString( $image_url, $fallback_result );
+
+ $administrator = get_userdata( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ foreach ( array( 'edit_products', 'edit_others_products', 'edit_private_products' ) as $capability ) {
+ $administrator->add_cap( $capability );
+ }
+ wp_set_current_user( $administrator->ID );
+ $this->assertTrue( current_user_can( 'edit_post', $product_id ) );
+ $this->assertStringContainsString( $image_url, $this->dynamic_content->apply_dynamic_images( '
' ) );
+
+ wp_update_post( array( 'ID' => $product_id, 'post_status' => 'publish' ) );
+ wp_set_current_user( 0 );
+ $this->assertSame( array( 'publish', true, false ), array( get_post_status( $product_id ), is_post_type_viewable( get_post_type( $product_id ) ), post_password_required( $product_id ) ) );
+ $this->assertStringContainsString( $image_url, $this->dynamic_content->apply_dynamic_images( '
' ) );
+
+ wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'private' ) );
+ $this->assertStringNotContainsString( $image_url, $this->dynamic_content->apply_dynamic_images( '
' ) );
+ wp_update_post( array( 'ID' => $this->post_id, 'post_status' => 'publish' ) );
+
+ $attachment_id = self::factory()->attachment->create_object( 'site-logo.png', $this->post_id, array( 'post_mime_type' => 'image/png' ) );
+ set_theme_mod( 'custom_logo', $attachment_id );
+ $this->go_to( get_category_link( $this->category_id ) );
+ wp_set_current_user( 0 );
+ $this->assertSame( wp_get_attachment_image_url( $attachment_id, 'full' ), $this->dynamic_content->get_image( array( 'type' => 'logo', 'context' => '0' ) ) );
+ set_theme_mod( 'custom_logo', 0 );
+ $this->assertSame( OTTER_BLOCKS_URL . 'assets/images/placeholder.jpg', $this->dynamic_content->get_image( array( 'type' => 'logo', 'context' => '0' ) ) );
+ wp_set_current_user( $this->user_id );
+ $this->assertSame( get_avatar_url( $this->user_id ), $this->dynamic_content->get_image( array( 'type' => 'loggedInUser', 'context' => '0' ) ) );
+ update_option( 'show_avatars', 0 );
+ $this->assertSame( get_avatar_url( $this->user_id ), $this->dynamic_content->get_image( array( 'type' => 'loggedInUser', 'context' => '0' ) ) );
+
+ $this->go_to( get_permalink( $this->post_id ) );
+ $this->assertSame( get_avatar_url( $this->user_id ), $this->dynamic_content->get_image( array( 'type' => 'author', 'context' => '0' ) ) );
+ }
+
+ private function configure_pro_dynamic_content_license( bool $active ): void {
+ if ( ! defined( 'OTTER_PRO_BASEFILE' ) ) {
+ require_once dirname( __DIR__ ) . '/plugins/otter-pro/otter-pro.php';
+ }
+ $option_name = str_replace( '-', '_', strtolower( trim( basename( dirname( OTTER_PRO_BASEFILE ) ) ) ) ) . '_license_data';
+ if ( $active ) {
+ update_option( $option_name, (object) array( 'license' => 'valid' ) );
+ } else {
+ delete_option( $option_name );
+ }
+ $this->dynamic_content_pro->init();
+ }
+
/**
* Assert the first tag of the markup gained no attributes from the text after it.
*