@@ -43,6 +43,43 @@ public function plugins_loaded() {
43
43
add_filter ( 'wp_generate_attachment_metadata ' , array ( $ this , 'filter_wp_generate_attachment_metadata ' ), 10 , 2 );
44
44
45
45
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 ;
46
83
}
47
84
48
85
/**
@@ -54,7 +91,7 @@ public function plugins_loaded() {
54
91
*
55
92
* @return mixed
56
93
*/
57
- function filter_wp_get_attachment_image_attributes ( $ attr , $ attachment , $ size ) {
94
+ public function filter_wp_get_attachment_image_attributes ( $ attr , $ attachment , $ size ) {
58
95
59
96
$ colors_hex = $ this ->get_colors_for_attachment ( $ attachment ->ID );
60
97
@@ -73,6 +110,13 @@ function filter_wp_get_attachment_image_attributes( $attr, $attachment, $size )
73
110
return $ attr ;
74
111
}
75
112
113
+ /**
114
+ * Style attribute for gradient background.
115
+ *
116
+ * @param $hex_colors
117
+ *
118
+ * @return string
119
+ */
76
120
public function get_gradient_style ( $ hex_colors ) {
77
121
78
122
foreach ( $ hex_colors as $ hex ) {
@@ -84,14 +128,21 @@ public function get_gradient_style( $hex_colors ) {
84
128
$ gradient_angles = array ( '90 ' , '0 ' , '-90 ' , '-180 ' );
85
129
86
130
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 ] );
88
132
}
89
133
90
134
$ style = 'background: ' . implode ( $ gradients , ', ' ) . '; ' ;
91
135
92
136
return $ style ;
93
137
}
94
138
139
+ /**
140
+ * Style attribute for solid backgrounds.
141
+ *
142
+ * @param $colors
143
+ *
144
+ * @return string
145
+ */
95
146
public function get_solid_style ( $ colors ) {
96
147
97
148
return 'background: ' . reset ( $ colors ) . '; ' ;
@@ -133,9 +184,9 @@ public function filter_wp_generate_attachment_metadata( $metadata, $attachment_i
133
184
134
185
/**
135
186
* Get the stored colors for the image
136
- *
137
187
* @param $id
138
- * @param array $hex_colors
188
+ *
189
+ * @return mixed
139
190
*/
140
191
public function get_colors_for_attachment ( $ id ) {
141
192
@@ -147,7 +198,7 @@ public function get_colors_for_attachment( $id ) {
147
198
* Extract the colors from the image
148
199
*
149
200
* @param $id
150
- * @param $image_path
201
+ * @param array $colors
151
202
*/
152
203
public function save_colors_for_attachment ( $ id , $ colors = array () ) {
153
204
update_post_meta ( $ id , 'hmgp_image_colors ' , $ colors );
@@ -157,7 +208,8 @@ public function save_colors_for_attachment( $id, $colors = array() ) {
157
208
* Calculate the colors from the image
158
209
*
159
210
* @param $id
160
- * @param $image_path
211
+ *
212
+ * @return array|void
161
213
*/
162
214
public function calculate_colors_for_attachment ( $ id ) {
163
215
0 commit comments