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'] = '/()/is'; + } + } + if ( ! isset( $patterns[ $block['blockName'] ] ) ) { + return $content; + } + + return preg_replace_callback( + $patterns[ $block['blockName'] ], + function ( $matches ) { + return $matches[1] . $this->escape_comment_magic_tags( $matches[2] ) . $matches[3]; + }, + $content + ); + } + + /** + * Preserve native search-form sentinels while protecting rendered markup. + * + * @param string|false|null $form Rendered form or a filter sentinel. + * @return string|false|null + */ + public function escape_search_form_magic_tags( $form ) { + return is_string( $form ) ? $this->escape_comment_magic_tags( $form ) : $form; + } + + /** + * Protect tag-cloud text and accessibility labels without changing term data. + * + * @param array $items Tag-cloud display data. + * @return array + */ + public function escape_tag_cloud_magic_tags( $items ) { + foreach ( $items as $index => $item ) { + $items[ $index ]['name'] = $this->escape_comment_magic_tags( $item['name'] ); + $items[ $index ]['aria_label'] = $this->escape_comment_magic_tags( $item['aria_label'] ); + } + + return $items; + } + + /** + * Keep inherited taxonomy menu labels literal while preserving authored labels. + * + * @param \WP_Post|\WP_Term|\stdClass $item Decorated menu item. + * @return \WP_Post|\WP_Term|\stdClass + */ + public function escape_taxonomy_menu_magic_tags( $item ) { + if ( isset( $item->type, $item->title ) && 'taxonomy' === $item->type && ( ! isset( $item->post_title ) || '' === $item->post_title ) ) { + $item->title = $this->escape_comment_magic_tags( $item->title ); + } + + return $item; + } + + /** + * Keep breadcrumb labels literal without changing their links or allowed markup. + * + * @param array $items Breadcrumb items. + * @return array + */ + public function escape_breadcrumb_magic_tags( $items ) { + foreach ( $items as $index => $item ) { + $items[ $index ]['label'] = $this->escape_comment_magic_tags( $item['label'] ); + } + + return $items; + } + + /** + * Keep visitor and resolved dynamic markers literal across nested block renders. + * + * @param string $content Untrusted field or markup being displayed. + * @return string + */ + public function escape_comment_magic_tags( $content ) { + return str_replace( + array_keys( self::$literal_marker_escapes ), + array_values( self::$literal_marker_escapes ), + $content + ); + } + + /** + * Generate a placeholder that neither authored nor resolved content can predict. + * + * @param string $prefix Placeholder prefix. + * @return string + */ + private static function unguessable_placeholder( $prefix ) { + return $prefix . wp_generate_password( 32, false ); + } + + /** + * Replace literals with unguessable placeholders, recording the text each one restores. + * + * @param string $value Text to protect. + * @param array $escapes Literal => text restored in its place. + * @param array $markers Placeholder => restored text, extended in place. + * @return string + */ + private function protect_literals( $value, $escapes, &$markers ) { + foreach ( $escapes as $literal => $restored ) { + if ( false === strpos( $value, $literal ) ) { + continue; + } + $marker = self::unguessable_placeholder( 'otter-literal-' ); + $value = str_replace( $literal, $marker, $value ); + $markers[ $marker ] = $restored; + } + return $value; + } + + /** + * Hide the live dynamic markers in a value so a later escape keeps them. + * + * @param string $value Text to protect. + * @param array $markers Placeholder => restored text, extended in place. + * @return string + */ + private function protect_markers( $value, &$markers ) { + $prefixes = array_keys( self::$literal_marker_escapes ); + return $this->protect_literals( $value, array_combine( $prefixes, $prefixes ), $markers ); + } + + /** + * Escape markers that only form once resolved values, delimited by $seam, join their neighbors. + * + * @param string $content Markup whose resolved values hold no live marker. + * @param string $seam Delimiter placed around each resolved value. + * @return string + */ + private function escape_assembled_markers( $content, $seam ) { + $markers = array(); + $content = str_replace( $seam, '', $this->protect_markers( $content, $markers ) ); + return strtr( $this->escape_comment_magic_tags( $content ), $markers ); } /** @@ -62,6 +251,7 @@ public function apply_dynamic_content( $content ) { return $content; } + $seam = self::unguessable_placeholder( 'otter-seam-' ); foreach ( $matches as $match ) { $replacement = $this->apply_data( $match ); $string_to_replace = $match[0]; @@ -83,18 +273,18 @@ public function apply_dynamic_content( $content ) { $_content = array_fill( 0, count( $replacement ), $content ); $content = ''; foreach ( $replacement as $key => $value ) { - $updated_content = substr_replace( $_content[ $key ], $value, $position, strlen( $string_to_replace ) ); + $updated_content = substr_replace( $_content[ $key ], $seam . $value . $seam, $position, strlen( $string_to_replace ) ); if ( isset( $link ) && is_array( $link ) && isset( $link[ $key ] ) ) { - $updated_content = str_replace( $matches[0], $link[ $key ], $updated_content ); + $updated_content = str_replace( $matches[0], $seam . $link[ $key ] . $seam, $updated_content ); } $content .= $updated_content; } } else { - $content = substr_replace( $content, $replacement, $position, strlen( $string_to_replace ) ); + $content = substr_replace( $content, $seam . $replacement . $seam, $position, strlen( $string_to_replace ) ); } } - return $content; + return $this->escape_assembled_markers( $content, $seam ); } /** @@ -132,13 +322,23 @@ public function apply_dynamic_magic_tags( $content ) { } // A whole start tag (quoted values may hold ">") or a text Magic Tag, in one pass so resolved values are never re-scanned. - $re = '/(?P<[a-zA-Z\/!][^>"\']*(?:(?:"[^"]*"|\'[^\']*\')[^>"\']*)*>)|' . self::MAGIC_TAG_REGEX . '/'; + $re = '/(?P<[a-zA-Z\/!][^>"\']*(?:(?:"[^"]*"|\'[^\']*\')[^>"\']*)*>)|' . self::MAGIC_TAG_REGEX . '/'; + $seam = self::unguessable_placeholder( 'otter-seam-' ); - return preg_replace_callback( $re, array( $this, 'apply_magic_tags_in_markup' ), $content ); + $content = preg_replace_callback( + $re, + function ( $matches ) use ( $seam ) { + $value = $this->apply_magic_tags_in_markup( $matches ); + return isset( $matches[2] ) ? $seam . $value . $seam : $value; + }, + $content + ); + + return $this->escape_assembled_markers( $content, $seam ); } /** - * Resolve a text Magic Tag, or attribute-escape every Magic Tag inside a start tag. + * Resolve a text Magic Tag, or resolve and sanitize complete attribute values. * * @param array $matches Start tag in `tag`, or the text Magic Tag query in `2`. * @@ -149,15 +349,69 @@ public function apply_magic_tags_in_markup( $matches ) { return $this->apply_magic_tags( array( 1 => $matches[2] ) ); } - return preg_replace_callback( - '/' . self::MAGIC_TAG_REGEX . '/', - function ( $token ) { - $value = $this->apply_magic_tags( $token ); + if ( false === strpos( $matches['tag'], '{otterDynamic?' ) || ! preg_match( '/^<([a-zA-Z][^\s\/>]*)/', $matches['tag'], $tag ) ) { + return $matches['tag']; + } - return is_string( $value ) ? esc_attr( $value ) : $value; - }, - $matches['tag'] - ); + $literal_markers = array(); + $escaped_prefixes = array_values( self::$literal_marker_escapes ); + $escaped_literals = array(); + foreach ( $escaped_prefixes as $prefix ) { + $escaped_literals[ $prefix ] = $prefix; + $escaped_literals[ wp_kses_normalize_entities( $prefix ) ] = $prefix; + } + $markup = $this->protect_literals( $matches['tag'], $escaped_literals, $literal_markers ); + + $closing_tag = ''; + $processor = new \WP_HTML_Tag_Processor( $markup . $closing_tag ); + if ( ! $processor->next_tag() ) { + return $matches['tag']; + } + + foreach ( (array) $processor->get_attribute_names_with_prefix( '' ) as $attribute ) { + $value = $processor->get_attribute( $attribute ); + if ( ! is_string( $value ) || false === strpos( $value, '{otterDynamic?' ) ) { + continue; + } + + if ( 0 === strpos( $attribute, 'on' ) || 'srcdoc' === $attribute ) { + $processor->remove_attribute( $attribute ); + continue; + } + + $value = preg_replace_callback( + '/({otterDynamic\?[^}]*})|otterDynamicLink|otter\/v1\/dynamic/', + function ( $token ) use ( &$literal_markers ) { + return isset( $token[1] ) ? $token[0] : $this->protect_markers( $token[0], $literal_markers ); + }, + $value + ); + + $value = preg_replace_callback( + '/{(otterDynamic\?[^}]*)}/', + function ( $token ) use ( &$literal_markers ) { + $value = html_entity_decode( (string) $this->apply_magic_tags( $token ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); + return $this->protect_literals( $value, self::$literal_marker_escapes, $literal_markers ); + }, + $value + ); + $value = $this->protect_literals( $value, self::$literal_marker_escapes, $literal_markers ); + + // SVG links still follow xlink:href, which core's URI list omits; it may hold a bare relative reference. + if ( 'xlink:href' === $attribute ) { + $value = wp_kses_bad_protocol( $value, wp_allowed_protocols() ); + } elseif ( in_array( $attribute, wp_kses_uri_attributes(), true ) ) { + $value = esc_url_raw( $value ); + } elseif ( 'style' === $attribute ) { + $value = safecss_filter_attr( $value ); + } + + $processor->set_attribute( $attribute, $value ); + } + + $markup = substr( $processor->get_updated_html(), 0, -strlen( $closing_tag ) ); + $markup = str_replace( '{otterDynamic?', self::$literal_marker_escapes['{otterDynamic?'], $markup ); + return strtr( $markup, $literal_markers ); } /** @@ -265,14 +519,26 @@ public function apply_dynamic_link_button( $content ) { return preg_replace_callback( $re, array( $this, 'apply_link_button' ), $content ); } + /** + * Resolve dynamic image URLs for CSS, which does not decode HTML entities. + * + * @param string $content CSS image URL. + * + * @return string + */ + public function apply_dynamic_css_images( $content ) { + return $this->apply_dynamic_images( $content, false ); + } + /** * Filter post content for dynamic images. * - * @param string $content Post content. + * @param string $content Post content. + * @param bool $escape_markers Whether resolved URLs keep dynamic markers literal for later block renders. * * @return string */ - public function apply_dynamic_images( $content ) { + public function apply_dynamic_images( $content, $escape_markers = true ) { if ( false === strpos( $content, 'otter/v1/dynamic' ) ) { return $content; } @@ -290,7 +556,12 @@ public function apply_dynamic_images( $content ) { $resolved = array(); $clone_count = 1; foreach ( $matches as $match ) { - $value = $this->apply_images( $match ); + $value = $this->apply_images( $match ); + if ( $escape_markers && is_array( $value ) ) { + $value = array_map( array( $this, 'escape_comment_magic_tags' ), $value ); + } elseif ( $escape_markers && is_string( $value ) ) { + $value = $this->escape_comment_magic_tags( $value ); + } $resolved[] = $value; if ( is_array( $value ) ) { $clone_count = max( $clone_count, count( $value ) ); @@ -335,7 +606,7 @@ function ( $data ) use ( &$resolved, &$index ) { * * @param array $data Dynamic request. * - * @return string|string[]|void + * @return string|list|false|void */ public function apply_images( $data ) { if ( ! isset( $data[0] ) ) { @@ -351,21 +622,23 @@ public function apply_images( $data ) { /** * Apply dynamic image. * - * @param array $data Query array. + * @param array $data Query array. * - * @return string + * @return string|list|false */ public function get_image( $data ) { $value = OTTER_BLOCKS_URL . 'assets/images/placeholder.jpg'; - global $post; + $id = ( ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( isset( $data['context'] ) && 'query' === $data['context'] ) ) ? (int) get_the_ID() : get_queried_object_id(); - $id = ( ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( isset( $data['context'] ) && 'query' === $data['context'] ) ) ? $post->ID : get_queried_object_id(); - - if ( isset( $data['context'] ) && ( 0 === $data['context'] || 'query' === $data['context'] || ( is_singular() && $data['context'] !== $id ) ) ) { + if ( isset( $data['context'] ) && ( 'query' === $data['context'] || ( is_singular() && $data['context'] !== $id ) ) ) { $data['context'] = $id; } + if ( empty( $data['context'] ) ) { + $data['context'] = (int) get_the_ID(); + } + if ( isset( $data['fallback'] ) && ! empty( $data['fallback'] ) ) { $value = esc_url( $data['fallback'] ); } @@ -374,6 +647,15 @@ public function get_image( $data ) { return $value; } + $server = \ThemeIsle\GutenbergBlocks\Server\Dynamic_Content_Server::instance(); + if ( ! in_array( $data['type'], array( 'logo', 'loggedInUser' ), true ) && ! $server->check_permission( (int) $data['context'] ) ) { + return $value; + } + + if ( 'product' === $data['type'] && ! empty( $data['id'] ) && ! $server->check_permission( (int) $data['id'] ) ) { + return $value; + } + if ( 'featuredImage' === $data['type'] ) { $image = get_the_post_thumbnail_url( $data['context'] ); @@ -411,14 +693,18 @@ public function get_image( $data ) { /** * Apply dynamic data. * - * @param array $data Dynamic request. - * @param bool $magic_tags Is a request for Magic Tags. + * @param array $data Dynamic request. + * @param bool $magic_tags Is a request for Magic Tags. * - * @return string|string[] + * @return int|string|list|false */ public function apply_data( $data, $magic_tags = false ) { $value = $this->get_data( $data, $magic_tags ); + if ( is_string( $value ) ) { + $value = $this->escape_comment_magic_tags( $value ); + } + if ( is_array( $value ) ) { $updated_value = array(); foreach ( $value as $key => $val ) { @@ -457,15 +743,16 @@ public function apply_data( $data, $magic_tags = false ) { * @return string */ public function apply_formatting( $value, $data ) { + $markers = array(); if ( isset( $data['before'] ) ) { - $value = esc_html( $data['before'] ) . $value; + $value = $this->protect_markers( esc_html( $data['before'] ), $markers ) . $value; } if ( isset( $data['after'] ) ) { - $value = $value . esc_html( $data['after'] ); + $value = $value . $this->protect_markers( esc_html( $data['after'] ), $markers ); } - return $value; + return strtr( $this->escape_comment_magic_tags( $value ), $markers ); } /** @@ -828,7 +1115,7 @@ public function apply_link( $data, $key = null ) { foreach ( $link as $link_item ) { $value[] = sprintf( '%s', - esc_url( $link_item ), + $this->esc_resolved_url( $link_item ), $attrs, wp_kses_post( $data['text'] ) ); @@ -842,7 +1129,7 @@ public function apply_link( $data, $key = null ) { } $value = sprintf( '%s', - esc_url( $link ), + $this->esc_resolved_url( $link ), $attrs, wp_kses_post( $data['text'] ) ); @@ -872,10 +1159,20 @@ public function apply_link_button( $data ) { } if ( is_array( $link ) ) { - return array_map( 'esc_url', $link ); + return array_map( array( $this, 'esc_resolved_url' ), $link ); } - return esc_url( $link ); + return $this->esc_resolved_url( $link ); + } + + /** + * Escape a resolved URL, keeping its dynamic markers literal while it still decodes to the same URL. + * + * @param string $url Resolved URL. + * @return string + */ + private function esc_resolved_url( $url ) { + return $this->escape_comment_magic_tags( esc_url( $url ) ); } /** diff --git a/inc/render/class-posts-grid-block.php b/inc/render/class-posts-grid-block.php index 4e1afe4f1..623109101 100644 --- a/inc/render/class-posts-grid-block.php +++ b/inc/render/class-posts-grid-block.php @@ -7,6 +7,7 @@ namespace ThemeIsle\GutenbergBlocks\Render; +use ThemeIsle\GutenbergBlocks\Plugins\Dynamic_Content; use ThemeIsle\GutenbergBlocks\Registration; /** @@ -161,7 +162,7 @@ public function get_post_fields( $id, $attributes ) { $html .= sprintf( '', esc_url( get_category_link( $category[0]->term_id ) ), - esc_html( $category[0]->name ) + esc_html( Dynamic_Content::instance()->escape_comment_magic_tags( $category[0]->name ) ) ); } } @@ -227,7 +228,7 @@ public function get_post_fields( $id, $attributes ) { $output .= sprintf( '%2$s', esc_url( get_category_link( $cat->term_id ) ), - esc_html( $cat->name ) + esc_html( Dynamic_Content::instance()->escape_comment_magic_tags( $cat->name ) ) ) . $separator; } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 642bc2a93..07ecccbbf 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1488,12 +1488,6 @@ parameters: count: 1 path: inc/plugins/class-dashboard.php - - - rawMessage: 'Method ThemeIsle\GutenbergBlocks\Plugins\Dynamic_Content::apply_data() has parameter $data with no value type specified in iterable type array.' - identifier: missingType.iterableValue - count: 1 - path: inc/plugins/class-dynamic-content.php - - rawMessage: 'Method ThemeIsle\GutenbergBlocks\Plugins\Dynamic_Content::apply_formatting() has parameter $data with no value type specified in iterable type array.' identifier: missingType.iterableValue @@ -1566,12 +1560,6 @@ parameters: count: 1 path: inc/plugins/class-dynamic-content.php - - - rawMessage: 'Method ThemeIsle\GutenbergBlocks\Plugins\Dynamic_Content::get_image() has parameter $data with no value type specified in iterable type array.' - identifier: missingType.iterableValue - count: 1 - path: inc/plugins/class-dynamic-content.php - - rawMessage: 'Method ThemeIsle\GutenbergBlocks\Plugins\Dynamic_Content::get_link() has parameter $data with no value type specified in iterable type array.' identifier: missingType.iterableValue diff --git a/plugins/blocks-animation/composer.lock b/plugins/blocks-animation/composer.lock index 77697de49..b138c8725 100644 --- a/plugins/blocks-animation/composer.lock +++ b/plugins/blocks-animation/composer.lock @@ -8,16 +8,16 @@ "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" } ], "packages-dev": [], diff --git a/plugins/blocks-css/composer.lock b/plugins/blocks-css/composer.lock index d3431034b..0b1d9fbd1 100644 --- a/plugins/blocks-css/composer.lock +++ b/plugins/blocks-css/composer.lock @@ -8,16 +8,16 @@ "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" } ], "packages-dev": [], diff --git a/src/blocks/blocks/form/common.tsx b/src/blocks/blocks/form/common.tsx index d3c7735c6..678bb10bb 100644 --- a/src/blocks/blocks/form/common.tsx +++ b/src/blocks/blocks/form/common.tsx @@ -13,7 +13,7 @@ import { createBlock } from '@wordpress/blocks'; import { dispatch } from '@wordpress/data'; import type { BlockProps } from '../../helpers/blocks'; -import { changeActiveStyle, getActiveStyle, getChoice } from '../../helpers/helper-functions'; +import { changeActiveStyle, docsURL, getActiveStyle, getChoice } from '../../helpers/helper-functions'; import { Fragment } from '@wordpress/element'; import { RichText } from '@wordpress/block-editor'; import { SortableDragHandle, useSortableRow } from '../../components/sortable/index.js'; @@ -235,7 +235,7 @@ export const selectAllFieldsFromForm = ( children: any[]) : ({ parentClientId: s export const mappedNameInfo = ( {__( 'Allow easy identification of the field in features like Webhooks.', 'otter-blocks' )} - { __( 'Learn More', 'otter-blocks' ) } + { __( 'Learn More', 'otter-blocks' ) } ); diff --git a/src/blocks/blocks/form/inspector.js b/src/blocks/blocks/form/inspector.js index 2c7414fa3..7dffa0ee2 100644 --- a/src/blocks/blocks/form/inspector.js +++ b/src/blocks/blocks/form/inspector.js @@ -59,7 +59,7 @@ import { import { useResponsiveAttributes } from '../../helpers/utility-hooks.js'; import { makeBox } from '../../plugins/copy-paste/utils'; -import { _px, setUtm } from '../../helpers/helper-functions.js'; +import { _px, docsURL, setUtm } from '../../helpers/helper-functions.js'; import { SortableInputField } from './sortable-input-fields'; import AutoDisableSyncAttr from '../../components/auto-disable-sync-attr/index'; import { selectAllFieldsFromForm } from './common'; @@ -346,7 +346,7 @@ const FormOptions = ({ formOptions, setFormOption, attributes, setAttributes }) < br /> - + { __( 'Learn more about webhooks.', 'otter-blocks' ) } @@ -1123,7 +1123,7 @@ const Inspector = ({ { __( 'In order for the Form to work properly, make sure your SMTP server is set up. The test email will be send to the address from the Email To field in Form Options.', 'otter-blocks' ) } { __( 'Learn more.', 'otter-blocks' ) } diff --git a/src/blocks/components/prompt/index.tsx b/src/blocks/components/prompt/index.tsx index e4a4926be..45b0c7a70 100644 --- a/src/blocks/components/prompt/index.tsx +++ b/src/blocks/components/prompt/index.tsx @@ -13,6 +13,7 @@ import { Fragment, useEffect, useState } from '@wordpress/element'; */ import useSettings from '../../helpers/use-settings'; +import { docsURL } from '../../helpers/helper-functions'; import { PromptConversation, PromptsData, @@ -445,7 +446,7 @@ const PromptPlaceholder = ( props: PromptPlaceholderProps ) => { { __( 'More Info', 'otter-blocks' ) } diff --git a/src/blocks/helpers/helper-functions.js b/src/blocks/helpers/helper-functions.js index 835dcd107..d091c79fa 100644 --- a/src/blocks/helpers/helper-functions.js +++ b/src/blocks/helpers/helper-functions.js @@ -488,6 +488,33 @@ export const setUtm = ( urlAddress, linkArea, content ) => { return urlLink.toString(); }; +/** + * Add the Themeisle UTM parameters to a docs link. + * + * Mirrors the SDK's tsdk_utmify() in PHP, so links built in JS report the + * same utm_source, utm_medium, utm_campaign and utm_content values. + * + * @param {string} url The docs page URL. + * @param {string} utmCampaign The feature or placement the link belongs to. + * @param {string} utmMedium The screen the link is shown on. + * @return {string} The URL with UTM parameters. + */ +export const docsURL = ( url, utmCampaign, utmMedium ) => { + const content = [ 'https://', 'themeisle.com', '/themes/', '/plugins/', '/upgrade' ] + .reduce( ( value, part ) => value.split( part ).join( '' ), url ) + .replace( /^\/+|\/+$/g, '' ) + .toLowerCase() + .replace( /[^a-z0-9_-]/g, '' ); + + const urlLink = new URL( url ); + urlLink.searchParams.set( 'utm_source', 'wpadmin' ); + urlLink.searchParams.set( 'utm_medium', utmMedium ); + urlLink.searchParams.set( 'utm_campaign', utmCampaign ); + urlLink.searchParams.set( 'utm_content', content ); + + return urlLink.toString(); +}; + /** * Build a responsive wrapper around `setAttributes` * diff --git a/src/blocks/plugins/ai-content/index.tsx b/src/blocks/plugins/ai-content/index.tsx index 1e9d05ed4..c344227d6 100644 --- a/src/blocks/plugins/ai-content/index.tsx +++ b/src/blocks/plugins/ai-content/index.tsx @@ -30,6 +30,7 @@ import { isAIBackendConfigured } from '../../helpers/prompt'; import useSettings from '../../helpers/use-settings'; import { openAiAPIKeyName } from '../../components/prompt'; import type { BlockProps } from '../../helpers/blocks'; +import { docsURL } from '../../helpers/helper-functions'; import { AIToolbarAction, getEnabledActions, @@ -109,7 +110,7 @@ const AIToolbarMenu = ({ - + { __( 'Go to docs', 'otter-blocks' ) } diff --git a/src/blocks/plugins/dynamic-content/link/fields.js b/src/blocks/plugins/dynamic-content/link/fields.js index af8aaa8fa..267d0d998 100644 --- a/src/blocks/plugins/dynamic-content/link/fields.js +++ b/src/blocks/plugins/dynamic-content/link/fields.js @@ -27,6 +27,7 @@ import { applyFilters } from '@wordpress/hooks'; import options from './options.js'; import Notice from '../../../components/notice/index.js'; +import { docsURL } from '../../../helpers/helper-functions.js'; const Fields = ({ activeAttributes, @@ -49,7 +50,7 @@ const Fields = ({ { __( 'Learn more about Dynamic Links', 'otter-blocks' ) } diff --git a/src/blocks/plugins/dynamic-content/value/fields.js b/src/blocks/plugins/dynamic-content/value/fields.js index 506ed14f9..3a08b2fbd 100644 --- a/src/blocks/plugins/dynamic-content/value/fields.js +++ b/src/blocks/plugins/dynamic-content/value/fields.js @@ -37,7 +37,7 @@ import options from './options.js'; import Notice from './../../../components/notice/index.js'; -import { getQueryStringFromObject, setUtm } from '../../../helpers/helper-functions.js'; +import { decodeHTMLEntities, docsURL, getQueryStringFromObject, setUtm } from '../../../helpers/helper-functions.js'; let hasSettingsPanel = [ 'postExcerpt', @@ -79,7 +79,7 @@ const Fields = ({ setLoading( true ); apiFetch({ path: 'otter/v1/dynamic/preview/?' + getQueryStringFromObject({ context, type }) }) .then( data => { - setPreview( data ); + setPreview( 'string' === typeof data ? decodeHTMLEntities( data ) : data ); }) .catch( () => { setPreview( undefined ); @@ -109,7 +109,7 @@ const Fields = ({ { __( 'Learn more about Dynamic Values', 'otter-blocks' ) } diff --git a/src/blocks/plugins/live-search/index.js b/src/blocks/plugins/live-search/index.js index fc6f19df2..daa1abf4f 100644 --- a/src/blocks/plugins/live-search/index.js +++ b/src/blocks/plugins/live-search/index.js @@ -12,10 +12,10 @@ import { __ } from '@wordpress/i18n'; * Internal dependencies */ import Notice from '../../components/notice'; -import { setUtm } from '../../helpers/helper-functions'; +import { docsURL, setUtm } from '../../helpers/helper-functions'; import './editor.scss'; -const liveSearchDocUrl = 'https://docs.themeisle.com/article/1747-the-live-search-feature-otter-features-library'; +const liveSearchDocUrl = docsURL( 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/the-live-search-feature-otter-features-library', 'live-search', 'block-editor' ); const liveSearchUpsell = createHigherOrderComponent( BlockEdit => { return props => { diff --git a/src/blocks/plugins/sticky/edit.tsx b/src/blocks/plugins/sticky/edit.tsx index 8d297cfd0..410fbe85f 100644 --- a/src/blocks/plugins/sticky/edit.tsx +++ b/src/blocks/plugins/sticky/edit.tsx @@ -32,7 +32,7 @@ import { applyFilters } from '@wordpress/hooks'; * Internal dependencies. */ import Notice from '../../components/notice/index.js'; -import { setUtm } from '../../helpers/helper-functions.js'; +import { docsURL, setUtm } from '../../helpers/helper-functions.js'; import type { BlockProps, OtterBlock } from '../../helpers/blocks.js'; const FILTER_OPTIONS = { @@ -417,7 +417,7 @@ const Edit = ({ { __( 'Learn more about Sticky', 'otter-blocks' ) } diff --git a/src/blocks/plugins/welcome-guide/index.js b/src/blocks/plugins/welcome-guide/index.js index d9383acd5..4853658fe 100644 --- a/src/blocks/plugins/welcome-guide/index.js +++ b/src/blocks/plugins/welcome-guide/index.js @@ -29,7 +29,7 @@ import { registerPlugin } from '@wordpress/plugins'; */ import './editor.scss'; import useSettings from './../../helpers/use-settings.js'; -import { setUtm } from '../../helpers/helper-functions.js'; +import { docsURL, setUtm } from '../../helpers/helper-functions.js'; const WelcomeGuide = () => { const [ getOption, updateOption, status ] = useSettings(); @@ -158,7 +158,7 @@ const WelcomeGuide = () => {

{ __( 'You are all setup to use Otter Pro', 'otter-blocks' ) }

{ __( 'With Otter PRO, you have access to our advanced features, like Dynamic Content and the Premium Blocks.', 'otter-blocks' ) } - { __( 'Learn more', 'otter-blocks' ) } + { __( 'Learn more', 'otter-blocks' ) }

) : ( diff --git a/src/blocks/test/e2e/blocks/dynamic-content-frontend.spec.js b/src/blocks/test/e2e/blocks/dynamic-content-frontend.spec.js index 039a42c91..41fa91ea8 100644 --- a/src/blocks/test/e2e/blocks/dynamic-content-frontend.spec.js +++ b/src/blocks/test/e2e/blocks/dynamic-content-frontend.spec.js @@ -3,6 +3,79 @@ */ import { test, expect } from '@wordpress/e2e-test-utils-playwright'; +test( 'keeps anonymous comment magic tags inert in nested latest comments', async({ browser, baseURL, requestUtils }) => { + const marker = `Comment link ${ Date.now() }`; + const magicTag = '{otterDynamic?x=/?&default=javascript:alert(1)}'; + const post = await requestUtils.createPost({ + title: marker, + status: 'publish', + comment_status: 'open', + content: '
' + }); + const visitor = await browser.newContext({ baseURL, storageState: { cookies: [], origins: [] }}); + + try { + const response = await visitor.request.post( '/wp-comments-post.php', { + form: { + comment_post_ID: post.id, + author: marker, + email: `otter-${ post.id }@example.org`, + comment: `${ marker }` + } + }); + expect( response.ok() ).toBeTruthy(); + + const comments = await requestUtils.rest({ + path: '/wp/v2/comments', + params: { post: post.id, status: 'all', context: 'edit' } + }); + expect( comments ).toHaveLength( 1 ); + expect( comments[0].author ).toBe( 0 ); + await requestUtils.rest({ + method: 'POST', + path: `/wp/v2/comments/${ comments[0].id }`, + data: { status: 'approved' } + }); + + const page = await visitor.newPage(); + await page.goto( `/?p=${ post.id }` ); + const link = page.locator( '.wp-block-latest-comments__comment-excerpt' ).getByRole( 'link', { name: marker, exact: true }); + await expect( link ).toHaveAttribute( 'href', magicTag ); + } finally { + await visitor.close(); + await requestUtils.rest({ + method: 'DELETE', + path: `/wp/v2/posts/${ post.id }`, + params: { force: true } + }); + } +}); + +test( 'preserves authored links without resolving assembled dynamic markers', async({ page, requestUtils }) => { + const marker = `Dynamic marker ${ Date.now() }`; + const post = await requestUtils.createPost({ + title: marker, + status: 'publish', + content: `` + }); + + try { + await page.goto( `/?p=${ post.id }` ); + const authored = page.getByRole( 'link', { name: `Authored ${ marker }`, exact: true }); + const assembled = page.getByRole( 'link', { name: `Assembled ${ marker }`, exact: true }); + await expect( authored ).toHaveAttribute( 'href', new URL( post.link ).origin ); + await expect( authored ).toHaveAttribute( 'title', marker ); + await expect( assembled ).toHaveAttribute( 'href', '#otterDynamicLink?type=siteURL' ); + await expect( assembled ).toHaveAttribute( 'title', marker ); + } finally { + await requestUtils.rest({ + method: 'DELETE', + path: `/wp/v2/posts/${ post.id }`, + params: { force: true } + }); + } +}); + /** * Frontend rendering of the postContent dynamic tag (issue #2929). * diff --git a/src/blocks/test/unit/dynamic-value-preview.test.js b/src/blocks/test/unit/dynamic-value-preview.test.js new file mode 100644 index 000000000..0303295e9 --- /dev/null +++ b/src/blocks/test/unit/dynamic-value-preview.test.js @@ -0,0 +1,74 @@ +/** + * @param {string} response Preview REST response. + * @return {Promise} Container holding the rendered fields. + */ +const renderFieldsWithPreview = async( response ) => { + jest.resetModules(); + + jest.doMock( '@wordpress/i18n', () => ({ + __: text => text + }), { virtual: true }); + + jest.doMock( '@wordpress/api-fetch', () => () => Promise.resolve( response ), { virtual: true }); + + jest.doMock( '@wordpress/data', () => ({ + select: () => ({ getCurrentPostId: () => 1 }) + }), { virtual: true }); + + jest.doMock( '@wordpress/components', () => ({ + BaseControl: ({ children }) => children, + Button: () => null, + ButtonGroup: () => null, + ExternalLink: () => null, + SelectControl: () => null, + TextControl: () => null, + ToggleControl: () => null, + PanelBody: ({ children }) => children, + Spinner: () => null + }), { virtual: true }); + + jest.doMock( '../../components/notice/index.js', () => () => null ); + + window.themeisleGutenberg = { hasPro: true }; + + const Fields = require( '../../plugins/dynamic-content/value/fields.js' ).default; + + // Same React instance as the module under test, which loaded after resetModules(). + const { act, createElement } = require( 'react' ); + const { createRoot } = require( 'react-dom/client' ); + const container = document.createElement( 'div' ); + const root = createRoot( container ); + + await act( async() => { + root.render( createElement( Fields, { + activeAttributes: {}, + attributes: { type: 'postTitle' }, + changeAttributes: jest.fn(), + onChange: jest.fn(), + changeType: jest.fn(), + onRemove: jest.fn() + }) ); + }); + + return container; +}; + +describe( 'Dynamic value modal preview', () => { + beforeAll( () => { + global.IS_REACT_ACT_ENVIRONMENT = true; + }); + + afterAll( () => { + delete global.IS_REACT_ACT_ENVIRONMENT; + }); + + afterEach( () => { + delete window.themeisleGutenberg; + }); + + it( 'shows escaped markup in a resolved value as its original text', async() => { + const container = await renderFieldsWithPreview( '<o-dynamic-link data-type="siteURL">Site & more' ); + + expect( container.querySelector( '.o-dynamic-modal__preview__content' ).textContent ).toBe( 'Site & more' ); + }); +}); diff --git a/src/blocks/test/unit/helper-functions-more.test.ts b/src/blocks/test/unit/helper-functions-more.test.ts index 610199102..6a2cb2bc0 100644 --- a/src/blocks/test/unit/helper-functions-more.test.ts +++ b/src/blocks/test/unit/helper-functions-more.test.ts @@ -1,6 +1,7 @@ import { _cssBlock, _cssProp, + docsURL, findInnerBlocks, getObjectFromQueryString, getQueryStringFromObject, @@ -54,6 +55,20 @@ describe( 'setUtm', () => { }); }); +describe( 'docsURL', () => { + it( 'adds the same utm params as the SDK tsdk_utmify()', () => { + expect( docsURL( 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/block-sticky', 'sticky', 'block-editor' ) ).toBe( + 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/block-sticky?utm_source=wpadmin&utm_medium=block-editor&utm_campaign=sticky&utm_content=docsotter-page-builder-blocks-extensionsblock-sticky' + ); + }); + + it( 'keeps the anchor at the end', () => { + expect( docsURL( 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/how-to-use-webhooks-in-otter-forms#mapped-name', 'mapped-name', 'block-editor' ) ).toBe( + 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/how-to-use-webhooks-in-otter-forms?utm_source=wpadmin&utm_medium=block-editor&utm_campaign=mapped-name&utm_content=docsotter-page-builder-blocks-extensionshow-to-use-webhooks-in-otter-formsmapped-name#mapped-name' + ); + }); +}); + describe( 'css helpers', () => { it( 'builds css declarations conditionally', () => { expect( _cssProp( 'color', 'red' ) ).toBe( 'color: red;' ); diff --git a/src/dashboard/components/pages/AI.js b/src/dashboard/components/pages/AI.js index 82abb79b5..91f414b27 100644 --- a/src/dashboard/components/pages/AI.js +++ b/src/dashboard/components/pages/AI.js @@ -35,6 +35,7 @@ import { useDispatch } from '@wordpress/data'; * Internal dependencies. */ import useSettings from '../../../blocks/helpers/use-settings.js'; +import { docsURL } from '../../../blocks/helpers/helper-functions.js'; import { SortableVerticalList } from '../../../blocks/components/sortable/index.js'; import SortableToolbarAction from '../SortableToolbarAction.js'; import { @@ -388,7 +389,7 @@ const AI = () => {
{ __( 'More Info', 'otter-blocks' ) } @@ -481,7 +482,7 @@ const AI = () => { { __( 'More Info', 'otter-blocks' ) } diff --git a/src/dashboard/components/pages/Blocks.js b/src/dashboard/components/pages/Blocks.js index cd082add7..b6d939beb 100644 --- a/src/dashboard/components/pages/Blocks.js +++ b/src/dashboard/components/pages/Blocks.js @@ -47,6 +47,7 @@ import { tabsIcon, timelineIcon } from '../../../blocks/helpers/icons'; +import { docsURL } from '../../../blocks/helpers/helper-functions.js'; /** * List with all the blocks that can be disabled. @@ -58,167 +59,167 @@ const otterBlocks = [ 'slug': 'themeisle-blocks/accordion', 'name': __( 'Accordion', 'otter-blocks' ), 'icon': accordionIcon, - 'docLink': 'https://docs.themeisle.com/article/1672-accessibility-blocks#accordion' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/accessibility-blocks#accordion' }, { 'slug': 'themeisle-blocks/advanced-heading', 'name': __( 'Advanced Heading', 'otter-blocks' ), 'icon': headingIcon, - 'docLink': 'https://docs.themeisle.com/article/1686-global-defaults-blocks#advanced-heading' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/global-defaults-blocks#advanced-heading' }, { 'slug': 'themeisle-blocks/business-hours', 'name': __( 'Business Hours', 'otter-blocks' ), 'isPro': true, 'icon': businessHoursIcon, - 'docLink': 'https://docs.themeisle.com/article/1679-business-blocks#business-hours' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/business-blocks#business-hours' }, { 'slug': 'themeisle-blocks/button-group', 'name': __( 'Button Group', 'otter-blocks' ), 'icon': buttonsIcon, - 'docLink': 'https://docs.themeisle.com/article/1686-global-defaults-blocks#button-group' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/global-defaults-blocks#button-group' }, { 'slug': 'themeisle-blocks/circle-counter', 'name': __( 'Circle Counter', 'otter-blocks' ), 'icon': circleIcon, - 'docLink': 'https://docs.themeisle.com/article/1669-tracking-blocks#circle-counter' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/tracking-blocks#circle-counter' }, { 'slug': 'themeisle-blocks/countdown', 'name': __( 'Countdown', 'otter-blocks' ), 'icon': countdownIcon, - 'docLink': 'https://docs.themeisle.com/article/1669-tracking-blocks#countdown' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/tracking-blocks#countdown' }, { 'slug': 'themeisle-blocks/flip', 'name': __( 'Flip Card', 'otter-blocks' ), 'icon': flipIcon, - 'docLink': 'https://docs.themeisle.com/article/1673-popularity-block#flip-card' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/popularity-block#flip-card' }, { 'slug': 'themeisle-blocks/font-awesome-icons', 'name': __( 'Icon', 'otter-blocks' ), 'icon': faIcon, - 'docLink': 'https://docs.themeisle.com/article/1676-useful-blocks#icon' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/useful-blocks#icon' }, { 'slug': 'themeisle-blocks/form', 'name': __( 'Form', 'otter-blocks' ), 'icon': formIcon, - 'docLink': 'https://docs.themeisle.com/article/1674-form-blocks' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/form-blocks' }, { 'slug': 'themeisle-blocks/google-map', 'name': __( 'Google Maps', 'otter-blocks' ), 'icon': googleMapIcon, - 'docLink': 'https://docs.themeisle.com/article/1675-location-blocks#google-maps' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/location-blocks#google-maps' }, { 'slug': 'themeisle-blocks/icon-list', 'name': __( 'Icon List', 'otter-blocks' ), 'icon': iconListIcon, - 'docLink': 'https://docs.themeisle.com/article/1676-useful-blocks#icon-list' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/useful-blocks#icon-list' }, { 'slug': 'themeisle-blocks/leaflet-map', 'name': __( 'Maps', 'otter-blocks' ), 'icon': mapIcon, - 'docLink': 'https://docs.themeisle.com/article/1675-location-blocks#maps' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/location-blocks#maps' }, { 'slug': 'themeisle-blocks/modal', 'name': __( 'Modal', 'otter-blocks' ), 'icon': popupIcon, - 'docLink': 'https://docs.themeisle.com/article/2050-modal-block', + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/modal-block', 'isPro': true }, { 'slug': 'themeisle-blocks/lottie', 'name': __( 'Lottie Animation', 'otter-blocks' ), 'icon': lottieIcon, - 'docLink': 'https://docs.themeisle.com/article/1668-image-related-blocks#lottie-animation' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/image-related-blocks#lottie-animation' }, { 'slug': 'themeisle-blocks/popup', 'name': __( 'Popup', 'otter-blocks' ), 'icon': popupIcon, - 'docLink': 'https://docs.themeisle.com/article/1551-the-popup-block-otter-blocks-library' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/the-popup-block-otter-blocks-library' }, { 'slug': 'themeisle-blocks/posts-grid', 'name': __( 'Posts', 'otter-blocks' ), 'icon': postsIcon, - 'docLink': 'https://docs.themeisle.com/article/1530-posts-block' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/posts-block' }, { 'slug': 'themeisle-blocks/progress-bar', 'name': __( 'Progress Bar', 'otter-blocks' ), 'icon': progressIcon, - 'docLink': 'https://docs.themeisle.com/article/1669-tracking-blocks#progress-bar' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/tracking-blocks#progress-bar' }, { 'slug': 'themeisle-blocks/review', 'name': __( 'Product Review', 'otter-blocks' ), 'icon': reviewIcon, - 'docLink': 'https://docs.themeisle.com/article/1671-shop-related-blocks#product-review' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/shop-related-blocks#product-review' }, { 'slug': 'themeisle-blocks/review-comparison', 'name': __( 'Review Comparison Table', 'otter-blocks' ), 'isPro': true, 'icon': comparisonTableIcon, - 'docLink': 'https://docs.themeisle.com/article/1671-shop-related-blocks#comparison-table' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/shop-related-blocks#review-comparison-table' }, { 'slug': 'themeisle-blocks/advanced-columns', 'name': __( 'Section', 'otter-blocks' ), 'icon': columnsIcon, - 'docLink': 'https://docs.themeisle.com/article/1833-the-section-block-otter-blocks-library' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/the-section-block-otter-blocks-library' }, { 'slug': 'themeisle-blocks/sharing-icons', 'name': __( 'Sharing Icons', 'otter-blocks' ), 'icon': sharingIcon, - 'docLink': 'https://docs.themeisle.com/article/1673-popularity-block#sharing-icons' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/popularity-block#sharing-icons' }, { 'slug': 'themeisle-blocks/slider', 'name': __( 'Image Slider', 'otter-blocks' ), 'icon': sliderIcon, - 'docLink': 'https://docs.themeisle.com/article/1668-image-related-blocks#slider' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/image-related-blocks#slider' }, { 'slug': 'themeisle-blocks/stripe-checkout', 'name': __( 'Stripe Checkout', 'otter-blocks' ), 'icon': cartIcon, - 'docLink': 'https://docs.themeisle.com/article/1688-integrations-related-blocks#stripe-checkout' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/integrations-related-blocks#stripe-checkout' }, { 'slug': 'themeisle-blocks/tabs', 'name': __( 'Tabs', 'otter-blocks' ), 'icon': tabsIcon, - 'docLink': 'https://docs.themeisle.com/article/1672-accessibility-blocks#tabs' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/accessibility-blocks#tabs' }, { 'slug': 'themeisle-gutenberg/live-search', // TODO: find why this can not be disabled. 'name': __( 'Live Search', 'otter-blocks' ), 'isPro': true, 'icon': searchIcon, - 'docLink': 'https://docs.themeisle.com/article/1747-the-live-search-feature-otter-features-library' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/the-live-search-feature-otter-features-library' }, { 'slug': 'themeisle-blocks/content-generator', 'name': __( 'AI Block', 'otter-blocks' ), 'icon': () => aiGeneration, - 'docLink': 'https://docs.themeisle.com/article/1917-the-ai-block-otter-blocks-library#content-generator' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/the-ai-block-otter-blocks-library#ai-content-generator' }, { 'slug': 'themeisle-blocks/timeline', 'name': __( 'Timeline', 'otter-blocks' ), 'icon': timelineIcon, - 'docLink': 'https://docs.themeisle.com/article/2051-timeline-block' + 'docLink': 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/global-defaults-blocks#timeline' } ].sort( ( a, b ) => a.name.localeCompare( b.name ) ); @@ -236,7 +237,7 @@ const BlockCard = ({ block, isLoading, onToggle }) => {

{ block.name }

- + { __( 'Learn More', 'otter-blocks' ) } { diff --git a/src/dashboard/components/pages/Integrations.js b/src/dashboard/components/pages/Integrations.js index 9daea5097..ad86fbf74 100644 --- a/src/dashboard/components/pages/Integrations.js +++ b/src/dashboard/components/pages/Integrations.js @@ -26,6 +26,7 @@ import { applyFilters } from '@wordpress/hooks'; * Internal dependencies. */ import useSettings from '../../../blocks/helpers/use-settings.js'; +import { docsURL } from '../../../blocks/helpers/helper-functions.js'; import TCPanel from '../template-cloud/TCPanel'; const Integrations = ({ setTab }) => { @@ -263,7 +264,7 @@ const Integrations = ({ setTab }) => {
{ __( 'More Info', 'otter-blocks' ) } diff --git a/src/pro/components/webhook-editor/index.tsx b/src/pro/components/webhook-editor/index.tsx index eeacf31b3..448efc0ba 100644 --- a/src/pro/components/webhook-editor/index.tsx +++ b/src/pro/components/webhook-editor/index.tsx @@ -24,6 +24,7 @@ import { arrowRight, closeSmall } from '@wordpress/icons'; * Internal dependencies */ import useSettings from '../../../blocks/helpers/use-settings'; +import { docsURL } from '../../../blocks/helpers/helper-functions'; type WebhookEditorProps = { webhookId: string, @@ -384,7 +385,7 @@ const WebhookEditor = ( props: WebhookEditorProps ) => { { __( 'Edit Webhooks', 'otter-pro' ) } < br /> - + { __( 'Learn more about webhooks.', 'otter-pro' ) } diff --git a/tests/test-dynamic-content.php b/tests/test-dynamic-content.php index 45fb80b02..c7bedd680 100644 --- a/tests/test-dynamic-content.php +++ b/tests/test-dynamic-content.php @@ -1483,6 +1483,1077 @@ public function test_dynamic_magic_tag_value_is_escaped_in_attribute(): void { $this->assertStringContainsString( '>' . $name . '

', $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( 'Link' ); + $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' ); + $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', $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( '
' . get_comment_text( $comment ) . '

Authored link

' ); + $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( '

' . $text . '

' ); + $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( '

' . $link . '

' ); + $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( '

' . $source . '

' ); + $this->assertStringContainsString( $element, html_entity_decode( $result ), $result ); + $this->assertStringNotContainsString( 'assertStringNotContainsString( get_bloginfo( 'name' ), $result ); + } + + $result = do_blocks( '

Title

' ); + $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( '

' . $token . '

' ); + $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 = '

' . $token . '

'; + $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( '

' . $token . '

' ); + $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( '

' . $token . '

' ); + $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( '

' . $token . '

' ); + } 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( '

' . $query . '

' ); + } 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( '

Value

' ); + $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. *