diff --git a/composer.json b/composer.json index a06437e3c..e2bfee152 100644 --- a/composer.json +++ b/composer.json @@ -73,7 +73,7 @@ }, "minimum-stability": "dev", "require": { - "codeinwp/themeisle-sdk": "^3.3.65", + "codeinwp/themeisle-sdk": "^3.3.70", "tubalmartin/cssmin": "^4.1", "wptt/webfont-loader": "^1.1", "sabberworm/php-css-parser": "^8.4", diff --git a/composer.lock b/composer.lock index d848135e5..67a965ca1 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ed7d2f988ffce5c2a5150505b5329207", + "content-hash": "f7d4a66bc361fcc3052821cc86d88804", "packages": [ { "name": "codeinwp/themeisle-sdk", @@ -977,16 +977,16 @@ }, { "name": "php-stubs/woocommerce-stubs", - "version": "v11.2.1", + "version": "v11.2.2", "source": { "type": "git", "url": "https://github.com/php-stubs/woocommerce-stubs.git", - "reference": "8cfdb86cad90da64441317e1bebe5f71a437a074" + "reference": "d5c232c183c09689d75c177b01bef60e6dd53639" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-stubs/woocommerce-stubs/zipball/8cfdb86cad90da64441317e1bebe5f71a437a074", - "reference": "8cfdb86cad90da64441317e1bebe5f71a437a074", + "url": "https://api.github.com/repos/php-stubs/woocommerce-stubs/zipball/d5c232c183c09689d75c177b01bef60e6dd53639", + "reference": "d5c232c183c09689d75c177b01bef60e6dd53639", "shasum": "" }, "require": { @@ -1015,9 +1015,9 @@ ], "support": { "issues": "https://github.com/php-stubs/woocommerce-stubs/issues", - "source": "https://github.com/php-stubs/woocommerce-stubs/tree/v11.2.1" + "source": "https://github.com/php-stubs/woocommerce-stubs/tree/v11.2.2" }, - "time": "2026-10-09T20:26:55+00:00" + "time": "2026-11-04T21:47:17+00:00" }, { "name": "php-stubs/wordpress-stubs", @@ -3143,5 +3143,5 @@ "platform-overrides": { "php": "7.4" }, - "plugin-api-version": "2.9.0" + "plugin-api-version": "2.11.0" } diff --git a/inc/class-blocks-css.php b/inc/class-blocks-css.php index 12fd7f4c1..01189c49c 100644 --- a/inc/class-blocks-css.php +++ b/inc/class-blocks-css.php @@ -219,6 +219,19 @@ protected function is_standalone() { * @access public */ public function cycle_through_blocks( $inner_blocks, $id ) { + return $this->collect_css_from_blocks( $inner_blocks, $id, array() ); + } + + /** + * Collect custom CSS while tracking the current synced-pattern chain. + * + * @param array> $inner_blocks Blocks to inspect. + * @param int $id Post ID. + * @param array $seen_refs Synced-pattern IDs in the current chain. + * + * @return string + */ + private function collect_css_from_blocks( $inner_blocks, $id, $seen_refs ) { $style = ''; foreach ( $inner_blocks as $block ) { @@ -232,7 +245,13 @@ public function cycle_through_blocks( $inner_blocks, $id ) { } if ( 'core/block' === $block['blockName'] && ! empty( $block['attrs']['ref'] ) ) { - $reusable_block = get_post( $block['attrs']['ref'] ); + $reference_id = (int) $block['attrs']['ref']; + + if ( $reference_id <= 0 || isset( $seen_refs[ $reference_id ] ) ) { + continue; + } + + $reusable_block = get_post( $reference_id ); if ( ! $reusable_block || 'wp_block' !== $reusable_block->post_type ) { return ''; @@ -242,13 +261,15 @@ public function cycle_through_blocks( $inner_blocks, $id ) { return ''; } - $blocks = parse_blocks( $reusable_block->post_content ); + $blocks = parse_blocks( $reusable_block->post_content ); + $next_seen_refs = $seen_refs; + $next_seen_refs[ $reference_id ] = true; - $style .= $this->cycle_through_blocks( $blocks, $reusable_block->ID ); + $style .= $this->collect_css_from_blocks( $blocks, $reusable_block->ID, $next_seen_refs ); } if ( ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) { - $style .= $this->cycle_through_blocks( $block['innerBlocks'], $id ); + $style .= $this->collect_css_from_blocks( $block['innerBlocks'], $id, $seen_refs ); } } diff --git a/inc/css/class-block-frontend.php b/inc/css/class-block-frontend.php index 7d738d6a7..b50cac172 100644 --- a/inc/css/class-block-frontend.php +++ b/inc/css/class-block-frontend.php @@ -46,6 +46,13 @@ class Block_Frontend extends Base_CSS { */ private $total_inline_size = 0; + /** + * Synced-pattern IDs being expanded in the current chain, per loader method. + * + * @var array> + */ + private $expanding_refs = array(); + /** * Initialize the class */ @@ -207,13 +214,39 @@ public function get_fonts( $fonts_list = array() ) { * @access public */ public function enqueue_reusable_fonts( $blocks ) { + $this->expand_reusable_blocks( $blocks, 'enqueue_google_fonts' ); + } + + /** + * Run a loader method for each synced pattern referenced in the blocks, + * skipping patterns that method is already expanding in the current chain. + * + * @param array> $blocks Blocks to inspect. + * @param 'enqueue_google_fonts'|'enqueue_styles' $loader Method that receives the pattern ID. + * + * @return void + */ + private function expand_reusable_blocks( $blocks, $loader ) { foreach ( $blocks as $block ) { if ( 'core/block' === $block['blockName'] && ! empty( $block['attrs']['ref'] ) ) { - $this->enqueue_google_fonts( $block['attrs']['ref'] ); + $reference_id = (int) $block['attrs']['ref']; + + // A non-positive ID is malformed; WordPress meta lookups would read it as its absolute value. + if ( $reference_id <= 0 || isset( $this->expanding_refs[ $loader ][ $reference_id ] ) ) { + continue; + } + + $this->expanding_refs[ $loader ][ $reference_id ] = true; + + try { + $this->$loader( $reference_id ); + } finally { + unset( $this->expanding_refs[ $loader ][ $reference_id ] ); + } } if ( isset( $block['innerBlocks'] ) && ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) { - $this->enqueue_reusable_fonts( $block['innerBlocks'] ); + $this->expand_reusable_blocks( $block['innerBlocks'], $loader ); } } } @@ -353,15 +386,7 @@ function () use ( $file_name, $file_url ) { * @access public */ public function enqueue_reusable_styles( $blocks ) { - foreach ( $blocks as $block ) { - if ( 'core/block' === $block['blockName'] && ! empty( $block['attrs']['ref'] ) ) { - $this->enqueue_styles( $block['attrs']['ref'] ); - } - - if ( isset( $block['innerBlocks'] ) && ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) { - $this->enqueue_reusable_styles( $block['innerBlocks'] ); - } - } + $this->expand_reusable_blocks( $blocks, 'enqueue_styles' ); } /** diff --git a/inc/plugins/class-template-cloud.php b/inc/plugins/class-template-cloud.php index 52c51dc37..8b268a074 100644 --- a/inc/plugins/class-template-cloud.php +++ b/inc/plugins/class-template-cloud.php @@ -246,8 +246,8 @@ public static function sync_sources() { foreach ( $sources as $source ) { $url = trailingslashit( $source['url'] ) . self::API_ENDPOINT_SUFFIX; $args = array( - 'sslverify' => false, - 'headers' => array( + 'reject_unsafe_urls' => true, + 'headers' => array( 'X-API-KEY' => $source['key'], ), ); @@ -255,7 +255,7 @@ public static function sync_sources() { if ( function_exists( 'vip_safe_wp_remote_get' ) ) { $response = vip_safe_wp_remote_get( $url, '', 3, 1, 20, $args ); } else { - $response = wp_remote_get( $url, $args ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get + $response = wp_safe_remote_get( $url, $args ); } if ( is_wp_error( $response ) ) { diff --git a/inc/server/class-template-cloud-server.php b/inc/server/class-template-cloud-server.php index 7b3fa90aa..5eb6fb388 100644 --- a/inc/server/class-template-cloud-server.php +++ b/inc/server/class-template-cloud-server.php @@ -99,7 +99,8 @@ public function register_routes() { private function validate_source_and_get_name( $url, $key ) { $url = trailingslashit( $url ) . self::API_ENDPOINT_SUFFIX; $args = [ - 'headers' => [ + 'reject_unsafe_urls' => true, + 'headers' => [ 'X-API-KEY' => $key, ], ]; @@ -107,7 +108,7 @@ private function validate_source_and_get_name( $url, $key ) { if ( function_exists( 'vip_safe_wp_remote_get' ) ) { $response = vip_safe_wp_remote_get( $url, '', 3, 1, 20, $args ); } else { - $response = wp_remote_get( $url, $args ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get + $response = wp_safe_remote_get( $url, $args ); } if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { diff --git a/otter-blocks.php b/otter-blocks.php index 71cb2b081..e2f405d18 100644 --- a/otter-blocks.php +++ b/otter-blocks.php @@ -90,6 +90,47 @@ function () { } ); +/* + * Opts into the SDK Support Hub: one Help button on Otter's screens and the + * deactivation window. The dashboard and Form Submissions screens already fire + * `themeisle_internal_page`, so no `pages` list is needed. + */ +add_filter( + 'otter_blocks_support_hub_metadata', + function () { + return array( + // The SDK reads this while loading Otter, before Otter Pro's Licenser adds the + // product_otter_license_status filter, so read the stored license directly. + 'is_pro' => defined( 'OTTER_PRO_BASEFILE' ) && class_exists( '\ThemeisleSDK\Modules\Licenser' ) && \ThemeisleSDK\Modules\Licenser::is_valid( OTTER_PRO_BASEFILE ), + 'links' => array( + array( + 'icon' => 'book-alt', + 'text' => __( 'Documentation', 'otter-blocks' ), + 'url' => \ThemeIsle\GutenbergBlocks\Pro::get_docs_url(), + ), + array( + 'icon' => 'groups', + 'text' => __( 'Community Forum', 'otter-blocks' ), + 'url' => 'https://wordpress.org/support/plugin/otter-blocks/', + ), + ), + ); + } +); + +/* + * Otter also fires `themeisle_internal_page` in the block editor, which would put + * the Help button on every post, site and widget editor. Keep it to Otter's screens. + */ +add_filter( + 'otter_blocks_hide_support_hub', + function ( $hide ) { + global $pagenow; + + return $hide || in_array( $pagenow, array( 'post.php', 'post-new.php', 'site-editor.php', 'widgets.php' ), true ); + } +); + add_filter( 'themeisle_sdk_compatibilities/' . basename( OTTER_BLOCKS_PATH ), function ( $compatibilities ) { diff --git a/readme.md b/readme.md index a5832f3c9..89d85c1dc 100644 --- a/readme.md +++ b/readme.md @@ -330,7 +330,7 @@ Sure, Otter is compatible with any WordPress theme. ### Where can I get help? ### -If you encounter any difficulties or if you have questions about Otter, you can create a ticket on our [support forum](https://wordpress.org/support/plugin/otter-blocks/). Also, feel free to check our [documentation](https://docs.themeisle.com/article/1478-otter-blocks-documentation) to find out more about the features within the plugin and how to use it to its full potential. +If you encounter any difficulties or if you have questions about Otter, you can ask our support team from the **Help** button on the Otter dashboard, or create a ticket on our [support forum](https://wordpress.org/support/plugin/otter-blocks/). Also, feel free to check our [documentation](https://docs.themeisle.com/article/1478-otter-blocks-documentation) to find out more about the features within the plugin and how to use it to its full potential. ### What are the Patterns and how can I use them? ### diff --git a/readme.txt b/readme.txt index c8d29109e..b6775c199 100644 --- a/readme.txt +++ b/readme.txt @@ -330,7 +330,7 @@ Sure, Otter is compatible with any WordPress theme. = Where can I get help? = -If you encounter any difficulties or if you have questions about Otter, you can create a ticket on our [support forum](https://wordpress.org/support/plugin/otter-blocks/). Also, feel free to check our [documentation](https://docs.themeisle.com/article/1478-otter-blocks-documentation) to find out more about the features within the plugin and how to use it to its full potential. +If you encounter any difficulties or if you have questions about Otter, you can ask our support team from the **Help** button on the Otter dashboard, or create a ticket on our [support forum](https://wordpress.org/support/plugin/otter-blocks/). Also, feel free to check our [documentation](https://docs.themeisle.com/article/1478-otter-blocks-documentation) to find out more about the features within the plugin and how to use it to its full potential. = What are the Patterns and how can I use them? = diff --git a/src/animation/editor.js b/src/animation/editor.js index 9429593e0..ef2ff7e84 100644 --- a/src/animation/editor.js +++ b/src/animation/editor.js @@ -32,6 +32,8 @@ import { import AnimationPopover from './components/animation-popover.js'; +import { getAnimationTimingValue } from './timing.js'; + import ControlPanelControl from './../blocks/components/control-panel-control/index.js'; import countPlaceholder from './../../assets/images/count-animation.png'; @@ -217,10 +219,14 @@ function AnimationControls({ attributes.className ?.split( ' ' ) .map( ( i ) => { - if ( i.includes( 'o-anim-value-delay-' ) ) { - return `.${ i } { animation-delay: ${ i.replace( 'o-anim-value-delay-', '' ) }; --webkit-animation-delay: ${ i.replace( 'o-anim-value-delay-', '' ) }; }`; - } else if ( i.includes( 'o-anim-value-speed-' ) ) { - return `.${ i } { animation-duration: ${ i.replace( 'o-anim-value-speed-', '' ) }; --webkit-animation-duration: ${ i.replace( 'o-anim-value-speed-', '' ) }; }`; + const delayValue = getAnimationTimingValue( i, 'o-anim-value-delay-' ); + if ( null !== delayValue ) { + return `.${ i } { animation-delay: ${ delayValue }; --webkit-animation-delay: ${ delayValue }; }`; + } + + const speedValue = getAnimationTimingValue( i, 'o-anim-value-speed-' ); + if ( null !== speedValue ) { + return `.${ i } { animation-duration: ${ speedValue }; --webkit-animation-duration: ${ speedValue }; }`; } return ''; }) diff --git a/src/animation/frontend.js b/src/animation/frontend.js index 55bc47b8e..821257a74 100644 --- a/src/animation/frontend.js +++ b/src/animation/frontend.js @@ -1,3 +1,5 @@ +import { getAnimationTimingValue } from './timing.js'; + const animations = [ 'none', 'bounce', @@ -344,7 +346,7 @@ const createCustomAnimationNode = ( elements ) => { if ( classes.contains( 'o-anim-custom-delay' ) ) { let delay; for ( const className of classes ) { - if ( className.includes( 'o-anim-value-delay-' ) ) { + if ( null !== getAnimationTimingValue( className, 'o-anim-value-delay-' ) ) { delay = className; break; } @@ -357,7 +359,7 @@ const createCustomAnimationNode = ( elements ) => { if ( classes.contains( 'o-anim-custom-speed' ) ) { let speed; for ( const className of classes ) { - if ( className.includes( 'o-anim-value-speed-' ) ) { + if ( null !== getAnimationTimingValue( className, 'o-anim-value-speed-' ) ) { speed = className; break; } @@ -379,9 +381,12 @@ const createCustomAnimationNode = ( elements ) => { // Helper to escape CSS class names (e.g., 2.3 -> 2\.3) const escapeCssClass = className => className.replace(/([!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~])/g, '\\$1'); - customValuesNode.innerHTML = customDelays.reduce( + customValuesNode.textContent = customDelays.reduce( ( accumulator, cssClass ) => { - const delayValue = cssClass.replace( 'o-anim-value-delay-', '' ); + const delayValue = getAnimationTimingValue( cssClass, 'o-anim-value-delay-' ); + if ( null === delayValue ) { + return accumulator; + } const escapedClass = escapeCssClass( cssClass ); return ( @@ -392,7 +397,10 @@ const createCustomAnimationNode = ( elements ) => { '' ) + '\n' + customSpeeds.reduce( ( accumulator, cssClass ) => { - const speedValue = cssClass.replace( 'o-anim-value-speed-', '' ); + const speedValue = getAnimationTimingValue( cssClass, 'o-anim-value-speed-' ); + if ( null === speedValue ) { + return accumulator; + } const escapedClass = escapeCssClass( cssClass ); return ( diff --git a/src/animation/timing.js b/src/animation/timing.js new file mode 100644 index 000000000..069ba5617 --- /dev/null +++ b/src/animation/timing.js @@ -0,0 +1,18 @@ +const TIMING_VALUE_PATTERN = /^[0-9]*\.?[0-9]+(?:ms|s)$/; + +/** + * Read a supported animation timing value from a class name. + * + * @param {string} className The class name to inspect. + * @param {string} prefix The timing class prefix. + * @return {string|null} The timing value, or null when it is unsupported. + */ +export const getAnimationTimingValue = ( className, prefix ) => { + if ( ! className.startsWith( prefix ) ) { + return null; + } + + const value = className.slice( prefix.length ); + + return TIMING_VALUE_PATTERN.test( value ) ? value : null; +}; diff --git a/src/blocks/test/e2e/blocks/animations.spec.js b/src/blocks/test/e2e/blocks/animations.spec.js index 2fb1f2adf..b9a386553 100644 --- a/src/blocks/test/e2e/blocks/animations.spec.js +++ b/src/blocks/test/e2e/blocks/animations.spec.js @@ -127,6 +127,30 @@ test.describe( 'Animations', () => { await expect( editor.canvas.locator( '.o-anim-custom-speed.o-anim-value-speed-2s' ).first() ).toBeVisible(); }); + test( 'ignores unsupported custom values in editor styles', async({ editor, page }) => { + await editor.insertBlock({ + name: 'core/paragraph', + attributes: { + content: 'Animated paragraph', + className: [ + 'animated headShake', + 'o-anim-custom-delay o-anim-value-delay-250ms o-anim-value-delay-1second', + 'o-anim-custom-speed o-anim-value-speed-1.5s o-anim-value-speed-fast' + ].join( ' ' ) + } + }); + + await page.getByRole( 'button', { name: 'Animations' }).click(); + + const animationStyles = editor.canvas.locator( 'style[data-generator="otter-blocks"]' ); + const styleText = async() => ( await animationStyles.allTextContents() ).join( '\n' ); + + await expect.poll( styleText ).toContain( 'animation-delay: 250ms' ); + await expect.poll( styleText ).toContain( 'animation-duration: 1.5s' ); + await expect.poll( styleText ).not.toContain( '1second' ); + await expect.poll( styleText ).not.toContain( 'fast' ); + }); + test( 'count and typing help popovers show their own image', async({ editor, page }) => { await editor.insertBlock({ name: 'core/paragraph' }); diff --git a/src/blocks/test/e2e/blocks/dashboard.spec.js b/src/blocks/test/e2e/blocks/dashboard.spec.js index e10d35b24..b62d72f18 100644 --- a/src/blocks/test/e2e/blocks/dashboard.spec.js +++ b/src/blocks/test/e2e/blocks/dashboard.spec.js @@ -170,4 +170,38 @@ test.describe( 'Dashboard', () => { await countdownCard.getByRole( 'checkbox' ).click(); await expect( countdownCard.getByRole( 'checkbox' ) ).toBeChecked(); }); + + test( 'free sidebar opens the Help panel and keeps the community forum', async({ admin, otterUtils, page }) => { + // Global setup stubs a valid Pro license, which shows Contact Pro Support instead. + await otterUtils.deactivatePro(); + + try { + await admin.visitAdminPage( 'admin.php?page=otter' ); + + const contact = page.locator( '.otter-info-links' ).getByRole( 'link', { name: 'Contact Support' }); + + await expect( contact ).toHaveAttribute( 'data-ti-support', 'dashboard' ); + await expect( page.locator( '.otter-info-links' ).getByRole( 'link', { name: 'Community Forum' }) ).toHaveAttribute( 'href', 'https://wordpress.org/support/plugin/otter-blocks/' ); + + await contact.click(); + + await expect( page.locator( '.ti-support-panel' ) ).toBeVisible(); + } finally { + await otterUtils.activatePro(); + } + }); + + test( 'licensed Pro sidebar keeps Contact Pro Support', async({ page }) => { + await expect( page.locator( '.otter-info-links' ).getByRole( 'link', { name: 'Contact Pro Support' }) ).toHaveAttribute( 'href', 'https://store.themeisle.com/' ); + await expect( page.locator( '.otter-info-links' ).getByRole( 'link', { name: 'Contact Support' }) ).toHaveCount( 0 ); + }); + + test( 'Help launcher shows on the dashboard but not in the block editor', async({ admin, page }) => { + await expect( page.locator( '.ti-support-launcher' ) ).toBeVisible(); + + await admin.createNewPost(); + + // The SDK prints this container in admin_footer whenever the hub loads. + await expect( page.locator( '#ti-support-hub' ) ).toHaveCount( 0 ); + }); }); diff --git a/src/blocks/test/unit/animation-timing.test.js b/src/blocks/test/unit/animation-timing.test.js new file mode 100644 index 000000000..db1eef85e --- /dev/null +++ b/src/blocks/test/unit/animation-timing.test.js @@ -0,0 +1,42 @@ +describe( 'Animation timing values', () => { + afterEach( () => { + document.body.innerHTML = ''; + jest.resetModules(); + } ); + + it( 'ignores malformed custom timing values', () => { + document.body.innerHTML = [ + '
Valid
', + '
Valid
', + '
Invalid
', + '
Invalid
' + ].join( '' ); + + require( '../../../animation/frontend' ); + window.dispatchEvent( new Event( 'load' ) ); + + const timingStyles = document.querySelector( '#o-anim-custom-values' ); + + expect( timingStyles.textContent ).toContain( 'animation-delay: 250ms' ); + expect( timingStyles.textContent ).toContain( 'animation-duration: 1.5s' ); + expect( timingStyles.textContent ).not.toContain( '1second' ); + expect( timingStyles.textContent ).not.toContain( 'fast' ); + } ); + + it( 'keeps supported timing values after unsupported classes', () => { + document.body.innerHTML = [ + '
Delay
', + '
Speed
' + ].join( '' ); + + require( '../../../animation/frontend' ); + window.dispatchEvent( new Event( 'load' ) ); + + const timingStyles = document.querySelector( '#o-anim-custom-values' ); + + expect( timingStyles.textContent ).toContain( 'animation-delay: 250ms' ); + expect( timingStyles.textContent ).toContain( 'animation-duration: 1.5s' ); + expect( timingStyles.textContent ).not.toContain( '1second' ); + expect( timingStyles.textContent ).not.toContain( 'fast' ); + } ); +} ); diff --git a/src/dashboard/components/Sidebar.js b/src/dashboard/components/Sidebar.js index 2c94f7f5e..482317428 100644 --- a/src/dashboard/components/Sidebar.js +++ b/src/dashboard/components/Sidebar.js @@ -76,9 +76,7 @@ const Sidebar = ({ setTab }) => { const isProLicenseActive = Boolean( window.otterObj.hasPro ) && 'valid' === window.otterObj?.license?.valid; - - const supportLink = isProLicenseActive ? 'https://store.themeisle.com/' : 'https://wordpress.org/support/plugin/otter-blocks'; - const supportLabel = isProLicenseActive ? __( 'Contact Pro Support', 'otter-blocks' ) : __( 'Support', 'otter-blocks' ); + const forumLink = 'https://wordpress.org/support/plugin/otter-blocks/'; return ( @@ -122,7 +120,15 @@ const Sidebar = ({ title={ __( 'Useful links', 'otter-blocks' ) } > diff --git a/tests/fixtures/vip-http.php b/tests/fixtures/vip-http.php new file mode 100644 index 000000000..5385b56cb --- /dev/null +++ b/tests/fixtures/vip-http.php @@ -0,0 +1,79 @@ + + */ + public static function request( $url, $fallback_value, $threshold, $timeout, $retry, $args ) { + $GLOBALS['otter_vip_requests'][] = array( + 'url' => $url, + 'fallback_value' => $fallback_value, + 'threshold' => $threshold, + 'timeout' => $timeout, + 'retry' => $retry, + 'args' => $args, + ); + + return array( + 'response' => array( 'code' => 200 ), + 'body' => \wp_json_encode( + array( + 'success' => true, + 'data' => array(), + 'key_name' => 'Test Source', + ) + ), + ); + } + } +} + +namespace ThemeIsle\GutenbergBlocks\Server { + use ThemeIsle\GutenbergBlocks\Tests\VIP_HTTP_Stub; + + function function_exists( $function ) { + if ( 'vip_safe_wp_remote_get' === $function && ! empty( $GLOBALS['otter_vip_stub_enabled'] ) ) { + return true; + } + + return \function_exists( $function ); + } + + function vip_safe_wp_remote_get( $url, $fallback_value = '', $threshold = 3, $timeout = 1, $retry = 20, $args = array() ) { + return VIP_HTTP_Stub::request( $url, $fallback_value, $threshold, $timeout, $retry, $args ); + } +} + +namespace ThemeIsle\GutenbergBlocks\Plugins { + use ThemeIsle\GutenbergBlocks\Tests\VIP_HTTP_Stub; + + function function_exists( $function ) { + if ( 'vip_safe_wp_remote_get' === $function && ! empty( $GLOBALS['otter_vip_stub_enabled'] ) ) { + return true; + } + + return \function_exists( $function ); + } + + function vip_safe_wp_remote_get( $url, $fallback_value = '', $threshold = 3, $timeout = 1, $retry = 20, $args = array() ) { + return VIP_HTTP_Stub::request( $url, $fallback_value, $threshold, $timeout, $retry, $args ); + } +} diff --git a/tests/test-blocks-css.php b/tests/test-blocks-css.php index 26c60754e..6e82f9fc8 100644 --- a/tests/test-blocks-css.php +++ b/tests/test-blocks-css.php @@ -240,4 +240,96 @@ public function test_archive_left_to_otter_when_active() { $this->assertStringNotContainsString( '.ticss-second{color:blue}', $this->render( new Blocks_CSS() ) ); } + + /** + * Synced patterns stop expanding when they reference themselves. + */ + public function test_synced_pattern_cycle_is_ignored() { + $pattern_id = self::factory()->post->create( + array( + 'post_type' => 'wp_block', + 'post_status' => 'publish', + ) + ); + + wp_update_post( + array( + 'ID' => $pattern_id, + 'post_content' => $this->block_with_css( '.ticss-pattern{color:red}' ) . '' . $this->block_with_css( '.ticss-after{color:green}' ), + ) + ); + + $blocks = parse_blocks( '' ); + $css = ( new Blocks_CSS() )->cycle_through_blocks( $blocks, $pattern_id ); + + $this->assertSame( '.ticss-pattern{color:red}.ticss-after{color:green}', $css ); + } + + /** + * Synced patterns stop expanding when a nested block references a pattern earlier in the chain. + */ + public function test_indirect_synced_pattern_cycle_is_ignored() { + $first_id = self::factory()->post->create( + array( + 'post_type' => 'wp_block', + 'post_status' => 'publish', + ) + ); + $second_id = self::factory()->post->create( + array( + 'post_type' => 'wp_block', + 'post_status' => 'publish', + 'post_content' => $this->block_with_css( '.ticss-second{color:blue}' ) . '
' . $this->block_with_css( '.ticss-nested{color:green}' ) . '
', + ) + ); + + wp_update_post( + array( + 'ID' => $first_id, + 'post_content' => $this->block_with_css( '.ticss-first{color:red}' ) . '', + ) + ); + + $blocks = parse_blocks( '' ); + $css = ( new Blocks_CSS() )->cycle_through_blocks( $blocks, $first_id ); + + $this->assertSame( '.ticss-first{color:red}.ticss-second{color:blue}.ticss-nested{color:green}', $css ); + } + + /** + * A negative reference is skipped: it loads neither the pattern with the matching positive ID nor drops its siblings. + */ + public function test_negative_synced_pattern_reference_is_not_resolved() { + $pattern_id = self::factory()->post->create( + array( + 'post_type' => 'wp_block', + 'post_status' => 'publish', + 'post_content' => $this->block_with_css( '.ticss-pattern{color:red}' ), + ) + ); + + $blocks = parse_blocks( $this->block_with_css( '.ticss-before{color:blue}' ) . '' . $this->block_with_css( '.ticss-after{color:green}' ) ); + $css = ( new Blocks_CSS() )->cycle_through_blocks( $blocks, $pattern_id ); + + $this->assertSame( '.ticss-before{color:blue}.ticss-after{color:green}', $css ); + } + + /** + * Separate references to one synced pattern each contribute their CSS. + */ + public function test_repeated_synced_pattern_references_are_preserved() { + $pattern_id = self::factory()->post->create( + array( + 'post_type' => 'wp_block', + 'post_status' => 'publish', + 'post_content' => $this->block_with_css( '.ticss-pattern{color:red}' ), + ) + ); + + $reference = ''; + $blocks = parse_blocks( $reference . $reference ); + $css = ( new Blocks_CSS() )->cycle_through_blocks( $blocks, $pattern_id ); + + $this->assertSame( '.ticss-pattern{color:red}.ticss-pattern{color:red}', $css ); + } } diff --git a/tests/test-css-handler.php b/tests/test-css-handler.php index d83575105..3e44695fa 100644 --- a/tests/test-css-handler.php +++ b/tests/test-css-handler.php @@ -161,6 +161,209 @@ public function test_widgets_css_fallback_print_escapes_style_tag(): void { $this->assertStringContainsString( '\\3c\\2fstyle', $output ); } + /** + * Font loading stops at a nested reference back to an earlier pattern and goes on with later references. + */ + public function test_synced_pattern_cycle_fonts_stop_at_the_cycle(): void { + list( $first_id, $second_id, $third_id ) = $this->create_pattern_cycle(); + foreach ( array( $first_id, $second_id, $third_id ) as $pattern_id ) { + update_post_meta( $pattern_id, '_themeisle_gutenberg_block_fonts', array( array( 'fontfamily' => 'Roboto', 'fontvariant' => array( '400' ) ) ) ); + } + + Block_Frontend::instance()->enqueue_reusable_fonts( parse_blocks( '' ) ); + + $this->assertTrue( wp_style_is( 'otter-google-fonts-' . $first_id, 'enqueued' ) ); + $this->assertTrue( wp_style_is( 'otter-google-fonts-' . $second_id, 'enqueued' ) ); + $this->assertTrue( wp_style_is( 'otter-google-fonts-' . $third_id, 'enqueued' ) ); + } + + /** + * Style loading stops at a nested reference back to an earlier pattern and goes on with later references. + */ + public function test_synced_pattern_cycle_styles_stop_at_the_cycle(): void { + list( $first_id, $second_id, $third_id ) = $this->create_pattern_cycle(); + $file_paths = array( + $this->seed_post_stylesheet( $first_id, '.ticss-first{color:red}' ), + $this->seed_post_stylesheet( $second_id, '.ticss-second{color:blue}' ), + $this->seed_post_stylesheet( $third_id, '.ticss-third{color:green}' ), + ); + + remove_all_actions( 'wp_footer' ); + + try { + Block_Frontend::instance()->enqueue_reusable_styles( parse_blocks( '' ) ); + + ob_start(); + do_action( 'wp_footer' ); + $output = (string) ob_get_clean(); + } finally { + array_map( 'unlink', $file_paths ); + } + + $this->assertStringContainsString( '.ticss-first{color:red}', $output ); + // Once from the third pattern's own loader, once from the second pattern's saved-CSS read, which does not expand references. + $this->assertSame( 2, substr_count( $output, '.ticss-third{color:green}' ), $output ); + } + + /** + * A pattern whose fonts are loading can still have its styles loaded. + */ + public function test_style_loading_is_not_blocked_by_font_loading_of_the_same_pattern(): void { + $pattern_id = $this->create_pattern( '

Pattern

' ); + $reference = parse_blocks( '' ); + $file_path = $this->seed_post_stylesheet( $pattern_id, '.ticss-nested{color:red}' ); + update_post_meta( $pattern_id, '_themeisle_gutenberg_block_fonts', array( array( 'fontfamily' => 'Roboto', 'fontvariant' => array( '400' ) ) ) ); + + remove_all_actions( 'wp_footer' ); + add_filter( + 'otter_blocks_google_fonts_url', + function ( $url ) use ( $reference ) { + Block_Frontend::instance()->enqueue_reusable_styles( $reference ); + + return $url; + } + ); + + try { + Block_Frontend::instance()->enqueue_reusable_fonts( $reference ); + + ob_start(); + do_action( 'wp_footer' ); + $output = (string) ob_get_clean(); + } finally { + unlink( $file_path ); + } + + $this->assertStringContainsString( '.ticss-nested{color:red}', $output ); + } + + /** + * A font load that throws does not leave the pattern marked as being expanded. + */ + public function test_failed_font_load_does_not_skip_the_pattern_later(): void { + $pattern_id = $this->create_pattern( '

Pattern

' ); + $reference = parse_blocks( '' ); + $throw = true; + update_post_meta( $pattern_id, '_themeisle_gutenberg_block_fonts', array( array( 'fontfamily' => 'Roboto', 'fontvariant' => array( '400' ) ) ) ); + + add_filter( + 'otter_blocks_google_fonts_url', + function ( $url ) use ( &$throw ) { + if ( $throw ) { + $throw = false; + throw new RuntimeException( 'Font URL failed.' ); + } + + return $url; + } + ); + + try { + Block_Frontend::instance()->enqueue_reusable_fonts( $reference ); + } catch ( RuntimeException $exception ) { + $this->assertSame( 'Font URL failed.', $exception->getMessage() ); + } + + $this->assertFalse( $throw, 'The first font load must reach the throwing filter.' ); + + Block_Frontend::instance()->enqueue_reusable_fonts( $reference ); + + $this->assertTrue( wp_style_is( 'otter-google-fonts-' . $pattern_id, 'enqueued' ) ); + } + + /** + * A negative reference does not load the fonts of the pattern with the matching positive ID. + */ + public function test_negative_synced_pattern_reference_loads_no_fonts(): void { + $pattern_id = $this->create_pattern( '

Pattern

' ); + update_post_meta( $pattern_id, '_themeisle_gutenberg_block_fonts', array( array( 'fontfamily' => 'Roboto', 'fontvariant' => array( '400' ) ) ) ); + + Block_Frontend::instance()->enqueue_reusable_fonts( parse_blocks( '' ) ); + + $this->assertFalse( wp_style_is( 'otter-google-fonts-' . $pattern_id, 'enqueued' ) ); + $this->assertFalse( wp_style_is( 'otter-google-fonts--' . $pattern_id, 'enqueued' ) ); + } + + /** + * Separate references to one synced pattern each load its styles. + */ + public function test_repeated_synced_pattern_styles_are_loaded_per_reference(): void { + $pattern_id = $this->create_pattern( '

Pattern

' ); + $file_path = $this->seed_post_stylesheet( $pattern_id, '.ticss-repeat{color:blue}' ); + $reference = ''; + + remove_all_actions( 'wp_footer' ); + + try { + Block_Frontend::instance()->enqueue_reusable_styles( parse_blocks( $reference . $reference ) ); + + ob_start(); + do_action( 'wp_footer' ); + $output = (string) ob_get_clean(); + } finally { + unlink( $file_path ); + } + + $this->assertSame( 2, substr_count( $output, '.ticss-repeat{color:blue}' ), $output ); + } + + /** + * Create a published synced pattern. + * + * @param string $content Pattern content. + * @return int Pattern ID. + */ + private function create_pattern( string $content = '' ): int { + return self::factory()->post->create( + array( + 'post_type' => 'wp_block', + 'post_status' => 'publish', + 'post_content' => $content, + ) + ); + } + + /** + * Create patterns first -> second -> first, where the second also references a third pattern after the cycle. + * Both references of the second pattern sit inside one Group block, the reference back to the first one first. + * + * @return int[] First, second and third pattern IDs. + */ + private function create_pattern_cycle(): array { + $paragraph = '

Pattern

'; + $first_id = $this->create_pattern(); + $third_id = $this->create_pattern( $paragraph ); + $second_id = $this->create_pattern( '
' ); + + wp_update_post( + array( + 'ID' => $first_id, + 'post_content' => $paragraph . '', + ) + ); + + return array( $first_id, $second_id, $third_id ); + } + + /** + * Give a post the saved CSS and stylesheet file a previous save would have left. + * + * @param int $post_id Post ID. + * @param string $css Saved CSS. + * @return string The stylesheet path. + */ + private function seed_post_stylesheet( int $post_id, string $css ): string { + $file_name = 'post-v2-' . $post_id . '-test'; + $dir = wp_upload_dir( null, false )['basedir'] . '/themeisle-gutenberg/'; + + update_post_meta( $post_id, '_themeisle_gutenberg_block_styles', $css ); + update_post_meta( $post_id, '_themeisle_gutenberg_block_stylesheet', $file_name ); + wp_mkdir_p( $dir ); + file_put_contents( $dir . $file_name . '.css', $css ); + + return $dir . $file_name . '.css'; + } + /** * Remove the stylesheet and meta generate_css_file() wrote for a post. * diff --git a/tests/test-support-hub.php b/tests/test-support-hub.php new file mode 100644 index 000000000..65159c621 --- /dev/null +++ b/tests/test-support-hub.php @@ -0,0 +1,94 @@ +assertIsArray( $data ); + $this->assertSame( array( 'is_pro', 'links' ), array_keys( $data ) ); + } + + /** + * The footer links point to Otter's docs and wordpress.org forum. + */ + public function test_links_point_to_otter() { + $links = apply_filters( 'otter_blocks_support_hub_metadata', array() )['links']; + + $this->assertSame( + array( + 'https://docs.themeisle.com/otter-page-builder-blocks-extensions/otter-blocks-documentation', + 'https://wordpress.org/support/plugin/otter-blocks/', + ), + wp_list_pluck( $links, 'url' ) + ); + foreach ( $links as $link ) { + $this->assertNotEmpty( $link['icon'] ); + $this->assertNotEmpty( $link['text'] ); + } + } + + /** + * Messages are tagged as Pro only while the stored Pro license is valid, whatever + * the order in which the SDK loads Otter and Otter Pro. + */ + public function test_is_pro_reads_the_stored_pro_license() { + if ( ! defined( 'OTTER_PRO_BASEFILE' ) ) { + define( 'OTTER_PRO_BASEFILE', dirname( __DIR__ ) . '/plugins/otter-pro/otter-pro.php' ); + } + // The test bootstrap loads Composer before WordPress, so the SDK's load.php skips itself. + $sdk = dirname( __DIR__ ) . '/vendor/codeinwp/themeisle-sdk/src/'; + foreach ( array( 'Loader.php', 'Product.php', 'Common/Abstract_module.php', 'Modules/Licenser.php' ) as $file ) { + require_once $sdk . $file; + } + // No Licenser filter is registered, as when Otter loads before Otter Pro. + remove_all_filters( 'product_otter_license_status' ); + + update_option( 'otter_pro_license_data', (object) array( 'license' => 'expired' ) ); + $this->assertFalse( apply_filters( 'otter_blocks_support_hub_metadata', array() )['is_pro'] ); + + update_option( 'otter_pro_license_data', (object) array( 'license' => 'valid' ) ); + $this->assertTrue( apply_filters( 'otter_blocks_support_hub_metadata', array() )['is_pro'] ); + } + + /** + * The Help button stays off the block editors, which also count as Otter screens, and shows on Otter's own screens. + * + * @dataProvider admin_screens + * + * @param string $pagenow Admin page file. + * @param bool $hidden Whether the Help button is hidden there. + */ + public function test_hub_is_hidden_only_in_block_editors( $pagenow, $hidden ) { + $GLOBALS['pagenow'] = $pagenow; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + + $this->assertSame( $hidden, apply_filters( 'otter_blocks_hide_support_hub', false ) ); + } + + /** + * Admin pages and whether the Help button is hidden on them. + * + * @return array + */ + public function admin_screens() { + return array( + 'post editor' => array( 'post.php', true ), + 'new post' => array( 'post-new.php', true ), + 'site editor' => array( 'site-editor.php', true ), + 'widget editor' => array( 'widgets.php', true ), + 'Otter screen' => array( 'admin.php', false ), + 'post list' => array( 'edit.php', false ), + ); + } +} diff --git a/tests/test-template-cloud.php b/tests/test-template-cloud.php index 91d4b8ec7..1efd5d03b 100644 --- a/tests/test-template-cloud.php +++ b/tests/test-template-cloud.php @@ -10,6 +10,9 @@ class Test_Template_Cloud extends WP_UnitTestCase { public function set_up(): void { parent::set_up(); + require_once __DIR__ . '/fixtures/vip-http.php'; + $GLOBALS['otter_vip_stub_enabled'] = false; + /** @var \WP_REST_Server $wp_rest_server */ global $wp_rest_server; $wp_rest_server = new \Spy_REST_Server; @@ -22,6 +25,8 @@ public function set_up(): void { } public function tear_down(): void { + $GLOBALS['otter_vip_stub_enabled'] = false; + unset( $GLOBALS['otter_vip_requests'] ); delete_option( Template_Cloud::SOURCES_SETTING_KEY ); parent::tear_down(); } @@ -138,6 +143,70 @@ public function test_validate_source_does_not_disable_ssl_verification() { $method->invoke( $this->server, 'https://example.com', 'test-key' ); $this->assertNotFalse( $captured['sslverify'] ); + $this->assertTrue( $captured['reject_unsafe_urls'] ); + } + + public function test_sync_sources_uses_safe_http_defaults() { + Template_Cloud::save_pattern_sources( + [ + [ + 'key' => 'test-key', + 'url' => 'https://example.com', + 'name' => 'Test Source', + ], + ] + ); + + $captured = array(); + $stub = function ( $preempt, $args ) use ( &$captured ) { + $captured = $args; + + return array( + 'response' => array( 'code' => 200 ), + 'body' => wp_json_encode( + array( + 'success' => true, + 'data' => array(), + 'key_name' => 'Test Source', + ) + ), + ); + }; + + add_filter( 'pre_http_request', $stub, 10, 2 ); + Template_Cloud::sync_sources(); + remove_filter( 'pre_http_request', $stub, 10 ); + + $this->assertTrue( $captured['sslverify'] ); + $this->assertTrue( $captured['reject_unsafe_urls'] ); + } + + public function test_vip_requests_preserve_request_defaults() { + $GLOBALS['otter_vip_stub_enabled'] = true; + $GLOBALS['otter_vip_requests'] = array(); + + $reflection = new ReflectionClass( $this->server ); + $method = $reflection->getMethod( 'validate_source_and_get_name' ); + $method->setAccessible( true ); + $method->invoke( $this->server, 'https://example.com', 'test-key' ); + + Template_Cloud::save_pattern_sources( + array( + array( + 'key' => 'test-key', + 'url' => 'https://example.com', + 'name' => 'Test Source', + ), + ) + ); + Template_Cloud::sync_sources(); + + $this->assertCount( 2, $GLOBALS['otter_vip_requests'] ); + + foreach ( $GLOBALS['otter_vip_requests'] as $request ) { + $this->assertTrue( $request['args']['reject_unsafe_urls'] ); + $this->assertSame( 1, $request['timeout'] ); + } } public function test_remove_source() {