Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions assets/src/media/__tests__/admin-page.test.js
Original file line number Diff line number Diff line change
@@ -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 = '<div class="om-dam-loader"></div><iframe id="om-dam" style="display: none;"></iframe>';
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' }, '*' );
});
});
14 changes: 9 additions & 5 deletions assets/src/media/admin-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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' }, '*' );
});
4 changes: 2 additions & 2 deletions inc/dam.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,12 @@ public function render_dashboard_page() {
display: none !important;
}
</style>
<iframe id="om-dam" style="display: none;" src="<?php echo( $this->build_iframe_url() ); ?>" allow="clipboard-read; clipboard-write"></iframe>
<div class="om-dam-loader">
<img src="<?php echo esc_url( OPTML_URL . 'assets/img/logo.png' ); ?>" alt="Optimole Logo"
class="om-dam-logo">
<p><?php echo esc_html__( 'Loading', 'optimole-wp' ); ?>...</p>
</div>
<iframe id="om-dam" style="display: none;" src="<?php echo( $this->build_iframe_url() ); ?>" allow="clipboard-read; clipboard-write"></iframe>
Comment on lines 429 to +437

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note The DAM markup this change reorders is also printed inside the media modal template, which Elementor's editor loads. Issue #828 crashed when a Cloud Library image was picked for the Elementor "Image Box" widget. The fix for that lookup is untouched. Please pick a Cloud Library image in an Image Box widget once. Safe: the widget renders the image. Broken: the editor shows a TypeError or a fatal error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The #828 fix (7fc2d2d) only changed the Elementor image-size lookup in alter_elementor_image_size() (now inc/dam.php:573-612, $all_sizes[ $image_size_key ]). That is PHP that runs when the widget renders, and it doesn't read this markup. In the media modal, the iframe is found with this.$el.find( '#om-dam' ) (mediaFrame.js:75) and the loader with document.querySelector( '.om-dam-loader:not([style])' ) (messageHandler.js:58). Neither depends on the order of the two elements, and admin-page.js is only loaded on the optimole_page_optimole-dam screen, not in the modal or the Elementor editor.

<?php
}

Expand Down Expand Up @@ -521,7 +521,7 @@ public function enqueue_admin_page_scripts() {
OPTML_URL . 'assets/build/media/admin-page.js',
$asset_file['dependencies'],
$asset_file['version'],
true
false
);

wp_localize_script(
Expand Down
13 changes: 13 additions & 0 deletions tests/test-dam.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ public function test_iframe_url_all_cloud_sites_enabled() {
$this->assertEmpty( $data['sites'] );
}

public function test_dashboard_page_renders_loader_before_iframe_issue_1211() {
ob_start();
$this->dam->render_dashboard_page();
$html = ob_get_clean();

$loader = strpos( $html, 'class="om-dam-loader"' );
$iframe = strpos( $html, 'id="om-dam"' );

$this->assertNotFalse( $loader );
$this->assertNotFalse( $iframe );
$this->assertLessThan( $iframe, $loader );
}

public function test_insert_attachments() {
foreach ( $this->inserted_ids as $index => $id ) {
$this->assertIsInt( $id );
Expand Down
Loading