Skip to content

Commit 510af47

Browse files
author
paul de wouters
committed
Filters editor images
1 parent 2296511 commit 510af47

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

inc/class-plugin.php

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,43 @@ public function plugins_loaded() {
4343
add_filter( 'wp_generate_attachment_metadata', array( $this, 'filter_wp_generate_attachment_metadata' ), 10, 2 );
4444

4545
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'filter_wp_get_attachment_image_attributes' ), 10, 3 );
46+
47+
add_filter( 'image_send_to_editor', array( $this, 'filter_image_send_to_editor' ), 10, 8 );
48+
}
49+
50+
/**
51+
* Adds the style attribute to the image HTML.
52+
*
53+
* @param $html
54+
* @param $id
55+
* @param $caption
56+
* @param $title
57+
* @param $align
58+
* @param $url
59+
* @param $size
60+
* @param $alt
61+
*
62+
* @return mixed
63+
*/
64+
public function filter_image_send_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt ) {
65+
66+
$colors_hex = $this->get_colors_for_attachment( $id );
67+
68+
if ( ! $colors_hex ) {
69+
return $html;
70+
}
71+
72+
$display_type = get_site_option( 'hmip_placeholder_type' );
73+
74+
if ( 'gradient' === $display_type ) {
75+
$style = $this->get_gradient_style( $colors_hex );
76+
} else {
77+
$style = $this->get_solid_style( $colors_hex );
78+
}
79+
80+
$html = preg_replace( '/<img/', '<img style="' . $style . '" ', $html );
81+
82+
return $html;
4683
}
4784

4885
/**
@@ -54,7 +91,7 @@ public function plugins_loaded() {
5491
*
5592
* @return mixed
5693
*/
57-
function filter_wp_get_attachment_image_attributes( $attr, $attachment, $size ) {
94+
public function filter_wp_get_attachment_image_attributes( $attr, $attachment, $size ) {
5895

5996
$colors_hex = $this->get_colors_for_attachment( $attachment->ID );
6097

@@ -73,6 +110,13 @@ function filter_wp_get_attachment_image_attributes( $attr, $attachment, $size )
73110
return $attr;
74111
}
75112

113+
/**
114+
* Style attribute for gradient background.
115+
*
116+
* @param $hex_colors
117+
*
118+
* @return string
119+
*/
76120
public function get_gradient_style( $hex_colors ) {
77121

78122
foreach ( $hex_colors as $hex ) {
@@ -84,14 +128,21 @@ public function get_gradient_style( $hex_colors ) {
84128
$gradient_angles = array( '90', '0', '-90', '-180' );
85129

86130
foreach ( $gradient_angles as $key => $gradient_angle ) {
87-
$gradients[] = sprintf( "linear-gradient(%sdeg, rgba(%s) 0%%, rgba(%s) 100%%, rgba(%s))", $gradient_angle, $colors[ $key ], $colors[ $key + 1 ], $colors[ $key + 1 ] );
131+
$gradients[] = sprintf( "linear-gradient(%sdeg, rgba(%s) 0%%, rgba(%s) 100%%, rgba(%s) 100%%)", $gradient_angle, $colors[ $key ], $colors[ $key + 1 ], $colors[ $key + 1 ] );
88132
}
89133

90134
$style = 'background:' . implode( $gradients, ', ' ) . ';';
91135

92136
return $style;
93137
}
94138

139+
/**
140+
* Style attribute for solid backgrounds.
141+
*
142+
* @param $colors
143+
*
144+
* @return string
145+
*/
95146
public function get_solid_style( $colors ) {
96147

97148
return 'background:' . reset( $colors ) . ';';
@@ -133,9 +184,9 @@ public function filter_wp_generate_attachment_metadata( $metadata, $attachment_i
133184

134185
/**
135186
* Get the stored colors for the image
136-
*
137187
* @param $id
138-
* @param array $hex_colors
188+
*
189+
* @return mixed
139190
*/
140191
public function get_colors_for_attachment( $id ) {
141192

@@ -147,7 +198,7 @@ public function get_colors_for_attachment( $id ) {
147198
* Extract the colors from the image
148199
*
149200
* @param $id
150-
* @param $image_path
201+
* @param array $colors
151202
*/
152203
public function save_colors_for_attachment( $id, $colors = array() ) {
153204
update_post_meta( $id, 'hmgp_image_colors', $colors );
@@ -157,7 +208,8 @@ public function save_colors_for_attachment( $id, $colors = array() ) {
157208
* Calculate the colors from the image
158209
*
159210
* @param $id
160-
* @param $image_path
211+
*
212+
* @return array|void
161213
*/
162214
public function calculate_colors_for_attachment( $id ) {
163215

0 commit comments

Comments
 (0)