Skip to content

Commit 6a54914

Browse files
authored
Merge pull request #1222 from cloudinary/fix/wp-7-1-compatibility
Ensure compatibility with WordPress 7.1
2 parents b5fb4e7 + 0335980 commit 6a54914

12 files changed

Lines changed: 103 additions & 44 deletions

File tree

‎js/block-editor.asset.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => '558667256f0d3fc475cd');
1+
<?php return array('dependencies' => array('wp-api-fetch', 'wp-block-editor', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '58afb0a9ee7fa6faecd7');

‎js/block-editor.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎js/gallery-block.asset.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-components/build-style/style.css', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => 'b1a6274664dedd209b5d');
1+
<?php return array('dependencies' => array('wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-components/build-style/style.css', 'wp-data', 'wp-element', 'wp-i18n'), 'version' => 'c2bfb5ad5742b3b9b98f');

‎js/gallery-block.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package-lock.json‎

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"@wordpress/element": "^6.34.0",
8080
"@wordpress/env": "^10.12.0",
8181
"@wordpress/eslint-plugin": "^25.7.0",
82+
"@wordpress/hooks": "^4.52.0",
8283
"@wordpress/i18n": "^6.7.0",
8384
"@wordpress/scripts": "^33.0.0",
8485
"copy-webpack-plugin": "^14.0.0",

‎php/media/class-gallery.php‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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 );

‎php/media/class-video.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,15 @@ public function filter_video_shortcode( $html, $attr ) {
153153
* Enqueue BLock Assets
154154
*/
155155
public function enqueue_block_assets() {
156-
wp_enqueue_script( 'cloudinary-block', $this->media->plugin->dir_url . 'js/block-editor.js', array(), $this->media->plugin->version, true );
156+
$asset = require $this->media->plugin->dir_path . 'js/block-editor.asset.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
157+
158+
wp_enqueue_script(
159+
'cloudinary-block',
160+
$this->media->plugin->dir_url . 'js/block-editor.js',
161+
$asset['dependencies'],
162+
$asset['version'],
163+
true
164+
);
157165
wp_add_inline_script( 'cloudinary-block', 'var CLD_VIDEO_PLAYER = ' . wp_json_encode( $this->config ), 'before' );
158166
}
159167

‎src/js/components/featured-image.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { __ } from '@wordpress/i18n';
55

66
import { ToggleControl } from '@wordpress/components';
77
import { withDispatch, withSelect } from '@wordpress/data';
8+
import { addFilter } from '@wordpress/hooks';
89

910
// Set our component.
1011
let FeaturedTransformationsToggle = ( props ) => {
@@ -72,7 +73,7 @@ const Featured = {
7273
// the media object, to determine if an asset has transformations.
7374
// Also adds deeper support for other image types within Guttenberg.
7475
// @todo: find other locations (i.e Video poster).
75-
wp.hooks.addFilter(
76+
addFilter(
7677
'editor.MediaUpload',
7778
'cloudinary/filter-featured-image',
7879
cldFilterFeatured

‎src/js/components/video.js‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import { __ } from '@wordpress/i18n';
44
import { withSelect } from '@wordpress/data';
5+
import { useEffect } from '@wordpress/element';
56
import { PanelBody, ToggleControl } from '@wordpress/components';
7+
import { addFilter } from '@wordpress/hooks';
8+
import { InspectorControls } from '@wordpress/block-editor';
69

710
const Video = {
811
_init() {
@@ -11,7 +14,7 @@ const Video = {
1114
}
1215

1316
// Gutenberg Video Settings
14-
wp.hooks.addFilter(
17+
addFilter(
1518
'blocks.registerBlockType',
1619
'Cloudinary/Media/Video',
1720
function ( settings, name ) {
@@ -57,7 +60,7 @@ const cldAddToggle = function ( settings, name ) {
5760
return settings;
5861
};
5962

60-
wp.hooks.addFilter(
63+
addFilter(
6164
'blocks.registerBlockType',
6265
'cloudinary/addAttributes',
6366
cldAddToggle
@@ -87,11 +90,16 @@ const TransformationsToggle = ( props ) => {
8790

8891
let ImageInspectorControls = ( props ) => {
8992
const { setAttributes, media } = props;
90-
const { InspectorControls } = wp.editor;
9193

92-
if ( media && media.transformations ) {
93-
setAttributes( { transformations: true } );
94-
}
94+
// Flag the block as having transformations after render. Calling
95+
// setAttributes during render triggers a React "Cannot update a component
96+
// while rendering a different component" warning under React 19
97+
// (WordPress 7.1).
98+
useEffect( () => {
99+
if ( media && media.transformations ) {
100+
setAttributes( { transformations: true } );
101+
}
102+
}, [ media, setAttributes ] );
95103

96104
return (
97105
<InspectorControls>
@@ -124,7 +132,7 @@ const cldFilterBlocksEdit = ( BlockEdit ) => {
124132
};
125133
};
126134

127-
wp.hooks.addFilter(
135+
addFilter(
128136
'editor.BlockEdit',
129137
'cloudinary/filterEdit',
130138
cldFilterBlocksEdit,

0 commit comments

Comments
 (0)