From 2ab00520c16eaff8a7c12200781601ba80711534 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 3 Dec 2024 13:36:07 -0500 Subject: [PATCH 01/31] Enhance admin UI with dominant color support. Added inline CSS and JavaScript to display dominant colors and transparency in the WordPress admin area for attachment previews. This enhancement allows the media library to utilize dominant color and transparency data, providing visual feedback on image properties. Updated version to 1.1.2 to reflect these changes. --- plugins/dominant-color-images/hooks.php | 84 ++++++++++++++++++++++++ plugins/dominant-color-images/load.php | 4 +- plugins/dominant-color-images/readme.txt | 8 ++- 3 files changed, 93 insertions(+), 3 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index 1eb930dba0..bb41da22d4 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -193,3 +193,87 @@ function dominant_color_render_generator(): void { echo '' . "\n"; } add_action( 'wp_head', 'dominant_color_render_generator' ); + + + + +/** + * Adds inline CSS for dominant color styling in the WordPress admin area. + * + * This function registers and enqueues a custom style handle, then adds inline CSS + * to apply background color based on the dominant color for attachment previews + * in the WordPress admin interface. + * + * @since 1.1.2 + * + * @return void + */ +function dominant_color_admin_inline_style() { + $handle = 'dominant-color-admin-styles'; + // PHPCS ignore reason: Version not used since this handle is only registered for adding an inline style. + // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion + wp_register_style( $handle, false ); + wp_enqueue_style( $handle ); + $custom_css = '.wp-core-ui .attachment-preview[data-dominant-color]:not(.has-transparency) { background-color: var(--dominant-color); }'; + wp_add_inline_style( $handle, $custom_css ); +}; +add_action( 'admin_enqueue_scripts', 'dominant_color_admin_inline_style' ); + +/** + * Adds a script to the admin footer to modify the attachment template. + * + * This function injects a JavaScript snippet into the admin footer that modifies + * the attachment template. It adds attributes for dominant color and transparency + * to the template, allowing these properties to be displayed in the media library. + * + * @since 1.1.2 + * + * @return void + */ + function dominant_color_admin_script(){ + ?> + + Date: Tue, 3 Dec 2024 13:40:16 -0500 Subject: [PATCH 02/31] Bump version to 1.1.3 for dominant color images plugin Update version numbers and associated comments from 1.1.2 to 1.1.3 for consistency across the plugin files. This change ensures that the codebase reflects the correct version for recent updates or bug fixes included in this release. --- plugins/dominant-color-images/hooks.php | 6 +++--- plugins/dominant-color-images/load.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index 3d1fe5fb10..2d571e19a6 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -197,7 +197,7 @@ function dominant_color_render_generator(): void { * to apply background color based on the dominant color for attachment previews * in the WordPress admin interface. * - * @since 1.1.2 + * @since 1.1.3 * * @return void */ @@ -219,7 +219,7 @@ function dominant_color_admin_inline_style() { * the attachment template. It adds attributes for dominant color and transparency * to the template, allowing these properties to be displayed in the media library. * - * @since 1.1.2 + * @since 1.1.3 * * @return void */ @@ -245,7 +245,7 @@ function dominant_color_admin_script(){ * the dominant color and transparency of the image. It modifies the response array to include * these additional properties, which can be used in the media library interface. * - * @since 1.1.2 + * @since 1.1.3 * * @param array $response The current response array for the attachment. * @param WP_Post $attachment The attachment post object. diff --git a/plugins/dominant-color-images/load.php b/plugins/dominant-color-images/load.php index e2e3c0c1bc..c85ef3ea7b 100644 --- a/plugins/dominant-color-images/load.php +++ b/plugins/dominant-color-images/load.php @@ -5,7 +5,7 @@ * Description: Displays placeholders based on an image's dominant color while the image is loading. * Requires at least: 6.6 * Requires PHP: 7.2 - * Version: 1.1.2 + * Version: 1.1.3 * Author: WordPress Performance Team * Author URI: https://make.wordpress.org/performance/ * License: GPLv2 or later @@ -25,7 +25,7 @@ return; } -define( 'DOMINANT_COLOR_IMAGES_VERSION', '1.1.2' ); +define( 'DOMINANT_COLOR_IMAGES_VERSION', '1.1.3' ); require_once __DIR__ . '/helper.php'; require_once __DIR__ . '/hooks.php'; From d6d96e4b83b0119debb72da85e8205fc6a133998 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 3 Dec 2024 13:40:50 -0500 Subject: [PATCH 03/31] Update hooks.php --- plugins/dominant-color-images/hooks.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index 2d571e19a6..ee9a293d3b 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -187,9 +187,6 @@ function dominant_color_render_generator(): void { } add_action( 'wp_head', 'dominant_color_render_generator' ); - - - /** * Adds inline CSS for dominant color styling in the WordPress admin area. * @@ -237,7 +234,6 @@ function dominant_color_admin_script(){ }; add_action( 'admin_print_footer_scripts', 'dominant_color_admin_script', 1000 ); - /** * Prepares attachment data for JavaScript, adding dominant color and transparency information. * From 4abdda43e121bd5d1a136eeb494e2c1ed1d266f4 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 3 Dec 2024 13:49:02 -0500 Subject: [PATCH 04/31] Fix code indentation and spacing issues Corrected inconsistent indentation and spacing to improve code readability and maintainability in hooks.php. These changes ensure a cleaner code style without altering the functionality or logic. --- plugins/dominant-color-images/hooks.php | 42 ++++++++++++------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index ee9a293d3b..dfb90170fc 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -200,12 +200,12 @@ function dominant_color_render_generator(): void { */ function dominant_color_admin_inline_style() { $handle = 'dominant-color-admin-styles'; - // PHPCS ignore reason: Version not used since this handle is only registered for adding an inline style. - // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion - wp_register_style( $handle, false ); - wp_enqueue_style( $handle ); - $custom_css = '.wp-core-ui .attachment-preview[data-dominant-color]:not(.has-transparency) { background-color: var(--dominant-color); }'; - wp_add_inline_style( $handle, $custom_css ); + // PHPCS ignore reason: Version not used since this handle is only registered for adding an inline style. + // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion + wp_register_style( $handle, false ); + wp_enqueue_style( $handle ); + $custom_css = '.wp-core-ui .attachment-preview[data-dominant-color]:not(.has-transparency) { background-color: var(--dominant-color); }'; + wp_add_inline_style( $handle, $custom_css ); }; add_action( 'admin_enqueue_scripts', 'dominant_color_admin_inline_style' ); @@ -220,7 +220,7 @@ function dominant_color_admin_inline_style() { * * @return void */ - function dominant_color_admin_script(){ +function dominant_color_admin_script() { ?> Date: Tue, 3 Dec 2024 13:52:31 -0500 Subject: [PATCH 05/31] Fix code indentation and spacing issues Corrected inconsistent indentation and spacing to improve code readability and maintainability in hooks.php. These changes ensure a cleaner code style without altering the functionality or logic. --- plugins/dominant-color-images/hooks.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index dfb90170fc..d3f838e111 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -199,14 +199,14 @@ function dominant_color_render_generator(): void { * @return void */ function dominant_color_admin_inline_style() { - $handle = 'dominant-color-admin-styles'; + $handle = 'dominant-color-admin-styles'; // PHPCS ignore reason: Version not used since this handle is only registered for adding an inline style. // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion wp_register_style( $handle, false ); wp_enqueue_style( $handle ); $custom_css = '.wp-core-ui .attachment-preview[data-dominant-color]:not(.has-transparency) { background-color: var(--dominant-color); }'; wp_add_inline_style( $handle, $custom_css ); -}; +} add_action( 'admin_enqueue_scripts', 'dominant_color_admin_inline_style' ); /** @@ -230,7 +230,7 @@ function dominant_color_admin_script() { }()); Date: Tue, 3 Dec 2024 13:56:17 -0500 Subject: [PATCH 06/31] Add return types to dominant color functions This commit specifies return types for several functions in the dominant-color-images plugin. The `dominant_color_admin_inline_style` and `dominant_color_admin_script` functions now explicitly return void, and the `dominant_color_prepare_attachment_for_js` function now returns an array. Adding these return types improves code clarity and aligns with current PHP best practices. --- plugins/dominant-color-images/hooks.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index d3f838e111..c86376ca98 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -198,7 +198,7 @@ function dominant_color_render_generator(): void { * * @return void */ -function dominant_color_admin_inline_style() { +function dominant_color_admin_inline_style(): void { $handle = 'dominant-color-admin-styles'; // PHPCS ignore reason: Version not used since this handle is only registered for adding an inline style. // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion @@ -220,7 +220,7 @@ function dominant_color_admin_inline_style() { * * @return void */ -function dominant_color_admin_script() { +function dominant_color_admin_script(): void { ?> @@ -256,7 +257,6 @@ function dominant_color_prepare_attachment_for_js( $response, WP_Post $attachmen $response['hasTransparencyClass'] = ''; if ( isset( $meta['has_transparency'] ) ) { $response['hasTransparency'] = (bool) $meta['has_transparency']; - $response['hasTransparencyClass'] = $meta['has_transparency'] ? 'has-transparency' : 'not-transparent'; } return $response; From 5dfd56eceafb8ec6651b282389f3606f1c08a274 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Fri, 6 Dec 2024 11:01:43 -0500 Subject: [PATCH 17/31] Fix inconsistent spacing in hooks.php file Remove unnecessary whitespace around the assignment operator in the 'hasTransparency' key modification. This change ensures code consistency and readability in the dominant-color-images plugin. --- plugins/dominant-color-images/hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index 2891fd4746..bf13d1dfae 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -256,7 +256,7 @@ function dominant_color_prepare_attachment_for_js( $response, WP_Post $attachmen $response['hasTransparency'] = ''; $response['hasTransparencyClass'] = ''; if ( isset( $meta['has_transparency'] ) ) { - $response['hasTransparency'] = (bool) $meta['has_transparency']; + $response['hasTransparency'] = (bool) $meta['has_transparency']; } return $response; From 2f46b74487391bb8af779e5fb8c053be12bd3bc1 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 9 Dec 2024 09:35:44 -0500 Subject: [PATCH 18/31] Update plugins/dominant-color-images/hooks.php Co-authored-by: Weston Ruter --- plugins/dominant-color-images/hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index bf13d1dfae..cad357d0a1 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -222,7 +222,7 @@ function dominant_color_admin_script(): void { (function() { var s = jQuery( '#tmpl-attachment' ) var transparencyClassName = 'true' === data.hasTransparency ? 'has-transparency' : 'not-transparent'; - var n = s[0].innerText.replace( '{{ data.orientation }}"', '{{ data.orientation }} {{ transparencyClassName }}" data-dominant-color="{{ data.dominantColor }}" data-has-transparency="{{ data.hasTransparency }}" style="--dominant-color: #{{ data.dominantColor }};"') + var n = s[0].innerText.replace( '{{ data.orientation }}"', '{{ data.orientation }} {{ data.hasTransparency ? 'has-transparency' : 'not-transparent' }}" data-dominant-color="{{ data.dominantColor }}" data-has-transparency="{{ data.hasTransparency }}" style="--dominant-color: #{{ data.dominantColor }};"') s.replaceWith( ' From 6bc708a6af95dc76b6dc4ba2198b27051b8bcda8 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 9 Dec 2024 09:36:13 -0500 Subject: [PATCH 19/31] Update plugins/dominant-color-images/hooks.php Co-authored-by: Weston Ruter --- plugins/dominant-color-images/hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index cad357d0a1..d96026edcb 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -250,7 +250,7 @@ function dominant_color_prepare_attachment_for_js( $response, WP_Post $attachmen } $response['dominantColor'] = ''; - if ( isset( $meta['dominant_color'] ) && str_contains( '#', $meta['dominant_color'] ) ) { + if ( isset( $meta['dominant_color'] ) && str_starts_with( '#', $meta['dominant_color'] ) ) { $response['dominantColor'] = sanitize_hex_color( $meta['dominant_color'] ); } $response['hasTransparency'] = ''; From bd2d72f7298bdcebc1dd629ab934c3fb4d9809b9 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 9 Dec 2024 09:39:48 -0500 Subject: [PATCH 20/31] Update hooks.php cleaner JS code --- plugins/dominant-color-images/hooks.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index d96026edcb..6b8947d429 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -218,13 +218,14 @@ function dominant_color_admin_inline_style(): void { */ function dominant_color_admin_script(): void { ?> - Date: Tue, 10 Dec 2024 17:36:13 -0500 Subject: [PATCH 21/31] Update plugins/dominant-color-images/hooks.php Co-authored-by: Weston Ruter --- plugins/dominant-color-images/hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index 6b8947d429..1e7e58e1aa 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -229,7 +229,7 @@ function dominant_color_admin_script(): void { Date: Tue, 10 Dec 2024 17:36:45 -0500 Subject: [PATCH 22/31] Update plugins/dominant-color-images/hooks.php Co-authored-by: Weston Ruter --- plugins/dominant-color-images/hooks.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index 1e7e58e1aa..14d05ecb3c 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -221,10 +221,13 @@ function dominant_color_admin_script(): void { Date: Wed, 11 Dec 2024 09:07:15 -0500 Subject: [PATCH 23/31] Update plugins/dominant-color-images/hooks.php Co-authored-by: Weston Ruter --- plugins/dominant-color-images/hooks.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index 14d05ecb3c..f97cf1dc49 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -221,13 +221,13 @@ function dominant_color_admin_script(): void { Date: Wed, 11 Dec 2024 09:08:31 -0500 Subject: [PATCH 24/31] Update hooks.php --- plugins/dominant-color-images/hooks.php | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index f97cf1dc49..d31bc6af01 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -258,7 +258,6 @@ function dominant_color_prepare_attachment_for_js( $response, WP_Post $attachmen $response['dominantColor'] = sanitize_hex_color( $meta['dominant_color'] ); } $response['hasTransparency'] = ''; - $response['hasTransparencyClass'] = ''; if ( isset( $meta['has_transparency'] ) ) { $response['hasTransparency'] = (bool) $meta['has_transparency']; } From c58a3556e7dc0f8dfe5c237432263b80c4ecb546 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 12 Dec 2024 08:20:27 -0500 Subject: [PATCH 25/31] Update plugins/dominant-color-images/hooks.php Co-authored-by: Weston Ruter --- plugins/dominant-color-images/hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index d31bc6af01..3b9520afeb 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -257,7 +257,7 @@ function dominant_color_prepare_attachment_for_js( $response, WP_Post $attachmen if ( isset( $meta['dominant_color'] ) && str_starts_with( '#', $meta['dominant_color'] ) ) { $response['dominantColor'] = sanitize_hex_color( $meta['dominant_color'] ); } - $response['hasTransparency'] = ''; + $response['hasTransparency'] = ''; if ( isset( $meta['has_transparency'] ) ) { $response['hasTransparency'] = (bool) $meta['has_transparency']; } From 3f96e3ff68ee1d40498d3b1f5d75db658ac49fd0 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 12 Dec 2024 17:27:20 -0500 Subject: [PATCH 26/31] Update plugins/dominant-color-images/hooks.php Co-authored-by: Weston Ruter --- plugins/dominant-color-images/hooks.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index 3b9520afeb..d49058c991 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -225,7 +225,15 @@ function dominant_color_admin_script(): void { let replaced = match.replace( /\sclass="/, " class=\"{{ data.hasTransparency ? \'has-transparency\' : \'not-transparent\' }} " ); replaced += ' data-dominant-color="{{ data.dominantColor }}"'; replaced += ' data-has-transparency="{{ data.hasTransparency }}"'; - replaced += ' style="--dominant-color: #{{ data.dominantColor }};"'; // TODO: Potentially there could be a style attribute as well! + let hasStyleAttr = false; + const colorStyle = '--dominant-color: #{{ data.dominantColor }};'; + replaced = replaced.replace( /\sstyle="/, ( styleMatch ) => { + hasStyleAttr = true; + return styleMatch + colorStyle; + } ); + if ( ! hasStyleAttr ) { + replaced += ` style="${colorStyle}"`; + } return replaced; } ); } From be572252dbf6e18701d7791bfd64c87b3baed0e7 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 13 Dec 2024 11:05:34 -0800 Subject: [PATCH 27/31] Combine comments --- plugins/dominant-color-images/hooks.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index d49058c991..94b27b140a 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -198,8 +198,7 @@ function dominant_color_render_generator(): void { */ function dominant_color_admin_inline_style(): void { $handle = 'dominant-color-admin-styles'; - // PHPCS ignore reason: Version not used since this handle is only registered for adding an inline style. - // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion + // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Version not used since this handle is only registered for adding an inline style. wp_register_style( $handle, false ); wp_enqueue_style( $handle ); $custom_css = '.wp-core-ui .attachment-preview[data-dominant-color]:not(.has-transparency) { background-color: var(--dominant-color); }'; From 6c3f1aef5ff44cfc7d294b22fb13c644c3d316bc Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 13 Dec 2024 11:31:52 -0800 Subject: [PATCH 28/31] Account for dominant_color not being prefixed by hash --- plugins/dominant-color-images/hooks.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index 94b27b140a..e48044d545 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -261,14 +261,20 @@ function dominant_color_prepare_attachment_for_js( $response, WP_Post $attachmen } $response['dominantColor'] = ''; - if ( isset( $meta['dominant_color'] ) && str_starts_with( '#', $meta['dominant_color'] ) ) { - $response['dominantColor'] = sanitize_hex_color( $meta['dominant_color'] ); + if ( + isset( $meta['dominant_color'] ) + && + 1 === preg_match( '/^[0-9A-F]+$/', $meta['dominant_color'] ) // See format returned by dominant_color_rgb_to_hex(). + ) { + $response['dominantColor'] = $meta['dominant_color']; } $response['hasTransparency'] = ''; if ( isset( $meta['has_transparency'] ) ) { $response['hasTransparency'] = (bool) $meta['has_transparency']; } + $response['testMeta'] = $meta; + return $response; } add_filter( 'wp_prepare_attachment_for_js', 'dominant_color_prepare_attachment_for_js', 10, 3 ); From e4fb28f4e0ac43bf0701ed4b52fda3d0d8c7970f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 13 Dec 2024 11:40:58 -0800 Subject: [PATCH 29/31] Avoid printing --dominant-color style when there is no dominant color --- plugins/dominant-color-images/hooks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index e48044d545..dd35052db8 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -221,11 +221,11 @@ function dominant_color_admin_script(): void { const tmpl = document.getElementById( 'tmpl-attachment' ); if ( tmpl ) { tmpl.textContent = tmpl.textContent.replace( /^\s*]*?(?=>)/, ( match ) => { - let replaced = match.replace( /\sclass="/, " class=\"{{ data.hasTransparency ? \'has-transparency\' : \'not-transparent\' }} " ); + let replaced = match.replace( /\sclass="/, " class=\"{{ data.hasTransparency ? 'has-transparency' : 'not-transparent' }} " ); replaced += ' data-dominant-color="{{ data.dominantColor }}"'; replaced += ' data-has-transparency="{{ data.hasTransparency }}"'; let hasStyleAttr = false; - const colorStyle = '--dominant-color: #{{ data.dominantColor }};'; + const colorStyle = "{{ data.dominantColor ? '--dominant-color: #' + data.dominantColor + ';' : '' }}"; replaced = replaced.replace( /\sstyle="/, ( styleMatch ) => { hasStyleAttr = true; return styleMatch + colorStyle; From 92553f2743e8b27cbf190f62d8e87fe62dad1e11 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 13 Dec 2024 11:47:43 -0800 Subject: [PATCH 30/31] Fix regex for dominant_color --- plugins/dominant-color-images/hooks.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index dd35052db8..c411909e59 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -264,7 +264,7 @@ function dominant_color_prepare_attachment_for_js( $response, WP_Post $attachmen if ( isset( $meta['dominant_color'] ) && - 1 === preg_match( '/^[0-9A-F]+$/', $meta['dominant_color'] ) // See format returned by dominant_color_rgb_to_hex(). + 1 === preg_match( '/^[0-9a-f]+$/', $meta['dominant_color'] ) // See format returned by dominant_color_rgb_to_hex(). ) { $response['dominantColor'] = $meta['dominant_color']; } @@ -273,8 +273,6 @@ function dominant_color_prepare_attachment_for_js( $response, WP_Post $attachmen $response['hasTransparency'] = (bool) $meta['has_transparency']; } - $response['testMeta'] = $meta; - return $response; } add_filter( 'wp_prepare_attachment_for_js', 'dominant_color_prepare_attachment_for_js', 10, 3 ); From 2da00d76803ff11bcc450e773caa16fd6cf975b4 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 13 Dec 2024 12:00:28 -0800 Subject: [PATCH 31/31] Add reference to related core function --- plugins/dominant-color-images/hooks.php | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/dominant-color-images/hooks.php b/plugins/dominant-color-images/hooks.php index c411909e59..a841a6f1f0 100644 --- a/plugins/dominant-color-images/hooks.php +++ b/plugins/dominant-color-images/hooks.php @@ -214,6 +214,7 @@ function dominant_color_admin_inline_style(): void { * to the template, allowing these properties to be displayed in the media library. * * @since n.e.x.t + * @see wp_print_media_templates() */ function dominant_color_admin_script(): void { ?>