From 9ea26d1b775ce567f89a5e6810d656e29f70f4f1 Mon Sep 17 00:00:00 2001 From: Girish Panchal <79647963+girishpanchal30@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:32:02 +0530 Subject: [PATCH 1/3] Fixed attach cloud library listeners early (#1241) * fix: attach cloud library listeners early * ci: build assets before PHPUnit tests * fix: phpunit tests * Revert "ci: build assets before PHPUnit tests" This reverts commit 8f8b34c222dee5e05c8fbdff09bc25f241115a17. --- assets/src/media/__tests__/admin-page.test.js | 38 +++++++++++++++++++ assets/src/media/admin-page.js | 14 ++++--- inc/dam.php | 4 +- tests/test-dam.php | 13 +++++++ 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 assets/src/media/__tests__/admin-page.test.js diff --git a/assets/src/media/__tests__/admin-page.test.js b/assets/src/media/__tests__/admin-page.test.js new file mode 100644 index 000000000..fa041c329 --- /dev/null +++ b/assets/src/media/__tests__/admin-page.test.js @@ -0,0 +1,38 @@ +/** + * Tests for the Cloud Library admin page script. + */ + +jest.mock( '../scss/admin-page.scss', () => ({}) ); + +const SITE_URL = 'https://example.test'; + +describe( 'Cloud Library admin page (issue #1211)', () => { + let iframe; + let loader; + + beforeAll( () => { + window.optmlAdminPage = { siteUrl: SITE_URL }; + + // The script is printed in the head, before the iframe markup exists. + require( '../admin-page.js' ); + + document.body.innerHTML = '
'; + iframe = document.getElementById( 'om-dam' ); + loader = document.querySelector( '.om-dam-loader' ); + }); + + test( 'reveals the iframe when it loads after the script ran', () => { + iframe.dispatchEvent( new Event( 'load' ) ); + + expect( loader.style.display ).toBe( 'none' ); + expect( iframe.style.display ).toBe( '' ); + }); + + test( 'answers getUrl from an iframe added after the script ran', () => { + const postMessage = jest.spyOn( iframe.contentWindow, 'postMessage' ).mockImplementation( () => {}); + + window.dispatchEvent( new MessageEvent( 'message', { data: { type: 'om-dam', action: 'getUrl' }}) ); + + expect( postMessage ).toHaveBeenCalledWith({ siteUrl: SITE_URL, type: 'om-dam', context: 'browse' }, '*' ); + }); +}); diff --git a/assets/src/media/admin-page.js b/assets/src/media/admin-page.js index f861a2fa1..2d7a6692b 100644 --- a/assets/src/media/admin-page.js +++ b/assets/src/media/admin-page.js @@ -3,12 +3,16 @@ import './scss/admin-page.scss'; const { optmlAdminPage } = window; const { siteUrl } = optmlAdminPage; -const iframe = document.getElementById( 'om-dam' ); -iframe.addEventListener( 'load', function() { +// This runs in the head, before #om-dam exists: `load` does not bubble, so catch it in the capture phase. +document.addEventListener( 'load', function( event ) { + if ( 'om-dam' !== event.target.id ) { + return; + } + document.querySelector( '.om-dam-loader' ).style.display = 'none'; - iframe.style.display = ''; -}); + event.target.style.display = ''; +}, true ); window.addEventListener( 'message', function( event ) { if ( ! event.data ) { @@ -31,5 +35,5 @@ window.addEventListener( 'message', function( event ) { return; } - iframe.contentWindow.postMessage({ siteUrl, type: 'om-dam', context: 'browse' }, '*' ); + document.getElementById( 'om-dam' ).contentWindow.postMessage({ siteUrl, type: 'om-dam', context: 'browse' }, '*' ); }); diff --git a/inc/dam.php b/inc/dam.php index 8bbeb4c99..5744e5a39 100644 --- a/inc/dam.php +++ b/inc/dam.php @@ -429,12 +429,12 @@ public function render_dashboard_page() { display: none !important; } -
...
-
@@ -772,6 +772,8 @@ public function should_show_upgrade() {
/**
* Build upgrade URL with current user's encoded email.
*
+ * The URL is already localized, so callers must not pass it through tsdk_translate_link() again.
+ *
* @return string Upgrade URL.
*/
public static function get_upgrade_base_link() {
@@ -1741,7 +1743,7 @@ public function get_bf_notices( $user_plan ) {
'' . $promo_code . '',
'' . __( '25% discount', 'optimole-wp' ) . ''
),
- 'cta_link' => esc_url_raw( tsdk_utmify( tsdk_translate_link( self::get_upgrade_base_link() ), 'bfcm', 'sidebarnotice' ) ),
+ 'cta_link' => esc_url_raw( tsdk_utmify( self::get_upgrade_base_link(), 'bfcm', 'sidebarnotice' ) ),
];
// The banner shares the SDK dismissal, which resets every year.
@@ -1914,7 +1916,7 @@ private function get_dashboard_strings() {
'notice_disabled_account' => sprintf(
/* translators: 1 anchor tag to pricing, 2 is the ending endin anchor tag, 3 is the bold tag start, 4 ending bold tag, 5 new line tag */
__( '%3$sYour account has been disabled due to exceeding quota.%4$s All images are being redirected to the original unoptimized URL. %5$sPlease %1$supgrade%2$s to re-activate the account.', 'optimole-wp' ),
- '',
+ '',
'',
'',
'',
diff --git a/tests/test-upgrade-link.php b/tests/test-upgrade-link.php
new file mode 100644
index 000000000..f713f59ae
--- /dev/null
+++ b/tests/test-upgrade-link.php
@@ -0,0 +1,229 @@
+initial_service_data = $settings->get( 'service_data' );
+
+ $settings->update(
+ 'service_data',
+ [
+ 'cdn_key' => 'example',
+ 'cdn_secret' => 'test',
+ 'whitelist' => [ 'example.org' ],
+ 'user_email' => self::ACCOUNT_EMAIL,
+ 'plan' => 'free',
+ 'visitors_limit' => 2000,
+ 'visitors_left' => 100,
+ ]
+ );
+ }
+
+ /**
+ * Restore the settings, which outlive the database rollback because they are cached statically.
+ */
+ public function tearDown(): void {
+ ( new Optml_Settings() )->update( 'service_data', $this->initial_service_data );
+
+ remove_all_filters( 'themeisle_sdk_is_black_friday_sale' );
+ remove_all_filters( 'themeisle_sdk_current_date' );
+ set_current_screen( 'front' );
+
+ parent::tearDown();
+ }
+
+ /**
+ * Locales for which the SDK adds a language code to a link, with that code.
+ *
+ * @return array