Skip to content

Commit 3258112

Browse files
authored
Merge pull request #1719 from pbearne/354-media-model
Enhance admin media UI with dominant color support
2 parents 9c3d9e8 + 2da00d7 commit 3258112

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

plugins/dominant-color-images/hooks.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,94 @@ function dominant_color_render_generator(): void {
186186
echo '<meta name="generator" content="dominant-color-images ' . esc_attr( DOMINANT_COLOR_IMAGES_VERSION ) . '">' . "\n";
187187
}
188188
add_action( 'wp_head', 'dominant_color_render_generator' );
189+
190+
/**
191+
* Adds inline CSS for dominant color styling in the WordPress admin area.
192+
*
193+
* This function registers and enqueues a custom style handle, then adds inline CSS
194+
* to apply background color based on the dominant color for attachment previews
195+
* in the WordPress admin interface.
196+
*
197+
* @since n.e.x.t
198+
*/
199+
function dominant_color_admin_inline_style(): void {
200+
$handle = 'dominant-color-admin-styles';
201+
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Version not used since this handle is only registered for adding an inline style.
202+
wp_register_style( $handle, false );
203+
wp_enqueue_style( $handle );
204+
$custom_css = '.wp-core-ui .attachment-preview[data-dominant-color]:not(.has-transparency) { background-color: var(--dominant-color); }';
205+
wp_add_inline_style( $handle, $custom_css );
206+
}
207+
add_action( 'admin_enqueue_scripts', 'dominant_color_admin_inline_style' );
208+
209+
/**
210+
* Adds a script to the admin footer to modify the attachment template.
211+
*
212+
* This function injects a JavaScript snippet into the admin footer that modifies
213+
* the attachment template. It adds attributes for dominant color and transparency
214+
* to the template, allowing these properties to be displayed in the media library.
215+
*
216+
* @since n.e.x.t
217+
* @see wp_print_media_templates()
218+
*/
219+
function dominant_color_admin_script(): void {
220+
?>
221+
<script type="module">
222+
const tmpl = document.getElementById( 'tmpl-attachment' );
223+
if ( tmpl ) {
224+
tmpl.textContent = tmpl.textContent.replace( /^\s*<div[^>]*?(?=>)/, ( match ) => {
225+
let replaced = match.replace( /\sclass="/, " class=\"{{ data.hasTransparency ? 'has-transparency' : 'not-transparent' }} " );
226+
replaced += ' data-dominant-color="{{ data.dominantColor }}"';
227+
replaced += ' data-has-transparency="{{ data.hasTransparency }}"';
228+
let hasStyleAttr = false;
229+
const colorStyle = "{{ data.dominantColor ? '--dominant-color: #' + data.dominantColor + ';' : '' }}";
230+
replaced = replaced.replace( /\sstyle="/, ( styleMatch ) => {
231+
hasStyleAttr = true;
232+
return styleMatch + colorStyle;
233+
} );
234+
if ( ! hasStyleAttr ) {
235+
replaced += ` style="${colorStyle}"`;
236+
}
237+
return replaced;
238+
} );
239+
}
240+
</script>
241+
<?php
242+
}
243+
add_action( 'admin_print_footer_scripts', 'dominant_color_admin_script' );
244+
245+
/**
246+
* Prepares attachment data for JavaScript, adding dominant color and transparency information.
247+
*
248+
* This function enhances the attachment data for JavaScript by including information about
249+
* the dominant color and transparency of the image. It modifies the response array to include
250+
* these additional properties, which can be used in the media library interface.
251+
*
252+
* @since n.e.x.t
253+
*
254+
* @param array<mixed>|mixed $response The current response array for the attachment.
255+
* @param WP_Post $attachment The attachment post object.
256+
* @param array<mixed> $meta The attachment metadata.
257+
* @return array<mixed> The modified response array with added dominant color and transparency information.
258+
*/
259+
function dominant_color_prepare_attachment_for_js( $response, WP_Post $attachment, array $meta ): array {
260+
if ( ! is_array( $response ) ) {
261+
$response = array();
262+
}
263+
264+
$response['dominantColor'] = '';
265+
if (
266+
isset( $meta['dominant_color'] )
267+
&&
268+
1 === preg_match( '/^[0-9a-f]+$/', $meta['dominant_color'] ) // See format returned by dominant_color_rgb_to_hex().
269+
) {
270+
$response['dominantColor'] = $meta['dominant_color'];
271+
}
272+
$response['hasTransparency'] = '';
273+
if ( isset( $meta['has_transparency'] ) ) {
274+
$response['hasTransparency'] = (bool) $meta['has_transparency'];
275+
}
276+
277+
return $response;
278+
}
279+
add_filter( 'wp_prepare_attachment_for_js', 'dominant_color_prepare_attachment_for_js', 10, 3 );

0 commit comments

Comments
 (0)