@@ -240,10 +240,12 @@ public function enqueue_admin_scripts() {
240240 /**
241241 * Retrieve asset dependencies.
242242 *
243+ * @param string $script The script slug to get the generated asset file for.
244+ *
243245 * @return array
244246 */
245- private function get_asset () {
246- $ asset = require $ this ->plugin ->dir_path . 'js/gallery .asset.php ' ; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
247+ private function get_asset ( $ script = ' gallery ' ) {
248+ $ asset = require $ this ->plugin ->dir_path . 'js/ ' . $ script . ' .asset.php ' ; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
247249
248250 $ asset ['dependencies ' ] = array_filter (
249251 $ asset ['dependencies ' ],
@@ -261,24 +263,40 @@ static function ( $dependency ) {
261263 public function block_editor_scripts_styles () {
262264 $ this ->enqueue_gallery_library ();
263265
264- wp_enqueue_style (
265- 'cloudinary-gallery-block-css ' ,
266- $ this ->plugin ->dir_url . 'css/gallery-block.css ' ,
267- array (),
268- $ this ->plugin ->version
269- );
266+ $ asset = $ this ->get_asset ( 'gallery-block ' );
270267
271268 wp_enqueue_script (
272269 'cloudinary-gallery-block-js ' ,
273270 $ this ->plugin ->dir_url . 'js/gallery-block.js ' ,
274- array ( ' wp-blocks ' , ' wp-editor ' , ' wp-element ' , self ::GALLERY_LIBRARY_HANDLE ),
275- $ this -> plugin -> version ,
271+ array_merge ( $ asset [ ' dependencies ' ], array ( self ::GALLERY_LIBRARY_HANDLE ) ),
272+ $ asset [ ' version ' ] ,
276273 true
277274 );
278275
279276 wp_add_inline_script ( 'cloudinary-gallery-block-js ' , 'var CLD_REST_ENDPOINT = "/ ' . REST_API ::BASE . '"; ' , 'before ' );
280277 }
281278
279+ /**
280+ * Enqueue the gallery block editor styles.
281+ *
282+ * Registered on `enqueue_block_assets` so the styles are injected into the
283+ * iframed block editor correctly. As of WordPress 7.1 the post editor is
284+ * always iframed, and styles added via `enqueue_block_editor_assets` are
285+ * flagged as being added to the iframe incorrectly.
286+ */
287+ public function block_editor_styles () {
288+ if ( ! is_admin () ) {
289+ return ;
290+ }
291+
292+ wp_enqueue_style (
293+ 'cloudinary-gallery-block-css ' ,
294+ $ this ->plugin ->dir_url . 'css/gallery-block.css ' ,
295+ array (),
296+ $ this ->plugin ->version
297+ );
298+ }
299+
282300 /**
283301 * Fetches image public id and transformations.
284302 *
@@ -410,13 +428,13 @@ public function register_settings( $pages ) {
410428 );
411429 }
412430
431+ // The `js/gallery.js` app script is enqueued in `enqueue_admin_scripts()`
432+ // with its generated asset dependencies. Adding a `script` here would
433+ // enqueue the same file under a second handle, executing the bundle twice
434+ // and calling `createRoot()` twice on the same container.
413435 $ panel [] = array (
414- 'type ' => 'react ' ,
415- 'slug ' => 'gallery_config ' ,
416- 'script ' => array (
417- 'slug ' => 'gallery-widget ' ,
418- 'src ' => $ this ->plugin ->dir_url . 'js/gallery.js ' ,
419- ),
436+ 'type ' => 'react ' ,
437+ 'slug ' => 'gallery_config ' ,
420438 );
421439
422440 $ panel [] = array (
@@ -688,6 +706,7 @@ public function inject_glb_metadata( $metadata, $attachment_id ) {
688706 public function setup_hooks () {
689707 add_filter ( 'cloudinary_api_rest_endpoints ' , array ( $ this , 'rest_endpoints ' ) );
690708 add_action ( 'enqueue_block_editor_assets ' , array ( $ this , 'block_editor_scripts_styles ' ) );
709+ add_action ( 'enqueue_block_assets ' , array ( $ this , 'block_editor_styles ' ) );
691710 add_action ( 'wp_enqueue_scripts ' , array ( $ this , 'enqueue_gallery_library ' ) );
692711 add_action ( 'admin_enqueue_scripts ' , array ( $ this , 'enqueue_admin_scripts ' ) );
693712 add_filter ( 'render_block ' , array ( $ this , 'prepare_block_render ' ), 10 , 2 );
0 commit comments