@@ -80,42 +80,39 @@ public function clear_cache() {
8080 public function setup () {
8181 $ this ->filter = $ this ->media ->filter ;
8282 // Add filters.
83- add_filter ( 'the_content ' , array ( $ this , 'filter_local ' ) );
84- add_action ( 'save_post ' , array ( $ this , 'remove_replace_cache ' ), 10 , 2 );
83+ add_action ( 'save_post ' , array ( $ this , 'remove_replace_cache ' ) );
8584 add_action ( 'cloudinary_string_replace ' , array ( $ this , 'catch_urls ' ) );
86- add_filter ( 'post_thumbnail_html ' , array ( $ this , 'rebuild_tag ' ), 100 , 5 );
85+ add_filter ( 'post_thumbnail_html ' , array ( $ this , 'process_featured_image ' ), 100 , 3 );
8786 }
8887
8988 /**
90- * Delete the content replacement cache data.
89+ * Add classes to the featured image tag.
90+ *
91+ * @param string $html The image tah HTML to add to.
92+ * @param int $post_id Ignored.
93+ * @param int $attachment_id The attachment_id.
9194 *
92- * @param int $post_id The post ID to remove cache from.
93- * @param WP_Post $post The post object.
95+ * @return string
9496 */
95- public function remove_replace_cache ( $ post_id , $ post ) {
96- delete_post_meta ( $ post_id , self ::META_CACHE_KEY );
97+ public function process_featured_image ( $ html , $ post_id , $ attachment_id ) {
98+ // Get tag element.
99+ $ tag_element = $ this ->parse_element ( $ html );
100+ $ tag_element ['atts ' ]['class ' ][] = 'wp-image- ' . $ attachment_id ;
101+ if ( true === (bool ) $ this ->media ->get_post_meta ( $ post_id , Global_Transformations::META_FEATURED_IMAGE_KEY , true ) ) {
102+ $ tag_element ['atts ' ]['class ' ][] = 'cld-overwrite ' ;
103+ }
104+
105+ return HTML ::build_tag ( $ tag_element ['tag ' ], $ tag_element ['atts ' ] );
97106 }
98107
99108 /**
100- * Filter out the local URLS from the content.
101- *
102- * @param string $content The HTML of the content to filter.
109+ * Delete the content replacement cache data.
103110 *
104- * @return string
111+ * @param int $post_id The post ID to remove cache from.
105112 */
106- public function filter_local ( $ content ) {
107- $ post_id = get_the_ID ();
108- if ( ! empty ( $ post_id ) ) {
109- $ replacements = get_post_meta ( $ post_id , self ::META_CACHE_KEY , true );
110- if ( empty ( $ replacements ) ) {
111- $ replacements = $ this ->convert_tags ( $ post_id , $ content );
112- }
113- foreach ( $ replacements as $ search => $ replace ) {
114- String_Replace::replace ( $ search , $ replace );
115- }
116- }
117-
118- return $ content ;
113+ public function remove_replace_cache ( $ post_id ) {
114+ delete_post_meta ( $ post_id , self ::META_CACHE_KEY . '_http ' );
115+ delete_post_meta ( $ post_id , self ::META_CACHE_KEY . '_https ' );
119116 }
120117
121118 /**
@@ -194,12 +191,19 @@ public function find_attachment_size_urls( $urls ) {
194191 /**
195192 * Convert media tags from Local to Cloudinary, and register with String_Replace.
196193 *
197- * @param int $post_id The post ID.
198194 * @param string $content The HTML to find tags and prep replacement in.
199195 *
200196 * @return array
201197 */
202- public function convert_tags ( $ post_id , $ content ) {
198+ public function convert_tags ( $ content ) {
199+ if ( is_singular () ) {
200+ $ cache_key = self ::META_CACHE_KEY ;
201+ $ has_cache = get_post_meta ( get_the_ID (), $ cache_key , true );
202+ $ type = is_ssl () ? 'https ' : 'http ' ;
203+ if ( ! empty ( $ has_cache ) && ! empty ( $ has_cache [ $ type ] ) ) {
204+ return $ has_cache [ $ type ];
205+ }
206+ }
203207 $ tags = $ this ->filter ->get_media_tags ( $ content );
204208 $ replacements = array ();
205209 $ attachment_ids = array ();
@@ -209,7 +213,7 @@ public function convert_tags( $post_id, $content ) {
209213 continue ;
210214 }
211215 // Register replacement.
212- $ replacements [ $ element ] = $ this ->rebuild_tag ( $ element , null , $ attachment_id );
216+ $ replacements [ $ element ] = $ this ->rebuild_tag ( $ element , $ attachment_id );
213217 $ attachment_ids [] = $ attachment_id ;
214218 }
215219
@@ -221,7 +225,13 @@ public function convert_tags( $post_id, $content ) {
221225 }
222226 }
223227 // Update the post meta cache.
224- update_post_meta ( $ post_id , self ::META_CACHE_KEY , $ replacements );
228+ if ( isset ( $ cache_key ) && isset ( $ type ) ) {
229+ if ( empty ( $ has_cache ) ) {
230+ $ has_cache = array ();
231+ }
232+ $ has_cache [ $ type ] = $ replacements ;
233+ update_post_meta ( get_the_ID (), $ cache_key , $ has_cache );
234+ }
225235
226236 return $ replacements ;
227237 }
@@ -230,53 +240,67 @@ public function convert_tags( $post_id, $content ) {
230240 * Rebuild a tag with cloudinary urls.
231241 *
232242 * @param string $element The original HTML tag.
233- * @param null|int $post_id The optional associated post ID.
234243 * @param null|int $attachment_id The attachment ID.
235244 *
236245 * @return string
237246 */
238- public function rebuild_tag ( $ element , $ post_id , $ attachment_id ) {
247+ public function rebuild_tag ( $ element , $ attachment_id ) {
239248 // Add our filter if not already added.
240249 if ( ! has_filter ( 'wp_calculate_image_srcset ' , array ( $ this ->media , 'image_srcset ' ) ) ) {
241250 add_filter ( 'wp_calculate_image_srcset ' , array ( $ this ->media , 'image_srcset ' ), 10 , 5 );
242251 }
243- $ element = trim ( $ element , '</> ' );
244252
245- // Break element up.
246- $ atts = shortcode_parse_atts ( $ element );
247-
248- // Remove tag.
249- $ tag = array_shift ( $ atts );
253+ // Get tag element.
254+ $ tag_element = $ this ->parse_element ( $ element );
250255
251256 // Remove the old srcset if it has one.
252- if ( isset ( $ atts ['srcset ' ] ) ) {
253- unset( $ atts ['srcset ' ] );
257+ if ( isset ( $ tag_element [ ' atts ' ] ['srcset ' ] ) ) {
258+ unset( $ tag_element [ ' atts ' ] ['srcset ' ] );
254259 }
255260
256261 // Get overwrite flag.
257- $ overwrite = false ;
258- if ( is_null ( $ post_id ) && isset ( $ atts ['class ' ] ) ) {
259- $ overwrite = (bool ) strpos ( $ atts ['class ' ], 'cld-overwrite ' );
260- } elseif ( $ post_id ) {
261- $ overwrite = (bool ) $ this ->media ->get_post_meta ( $ post_id , Global_Transformations::META_FEATURED_IMAGE_KEY , true );
262- }
262+ $ overwrite = in_array ( 'cld-overwrite ' , $ tag_element ['atts ' ]['class ' ], true );
263263
264264 // Get size.
265- $ size = $ this ->get_size_from_atts ( $ atts );
265+ $ size = $ this ->get_size_from_atts ( $ tag_element [ ' atts ' ] );
266266
267267 // Get transformations if present.
268- $ transformations = $ this ->get_transformations_maybe ( $ atts ['src ' ] );
268+ $ transformations = $ this ->get_transformations_maybe ( $ tag_element [ ' atts ' ] ['src ' ] );
269269
270270 // Create new src url.
271- $ atts ['src ' ] = $ this ->media ->cloudinary_url ( $ attachment_id , $ size , $ transformations , null , $ overwrite );
271+ $ tag_element [ ' atts ' ] ['src ' ] = $ this ->media ->cloudinary_url ( $ attachment_id , $ size , $ transformations , null , $ overwrite );
272272
273273 // Setup new tag.
274- $ replace = HTML ::build_tag ( $ tag , $ atts );
274+ $ replace = HTML ::build_tag ( $ tag_element [ ' tag ' ] , $ tag_element [ ' atts ' ] );
275275
276276 // Add new srcset.
277277 return $ this ->media ->apply_srcset ( $ replace , $ attachment_id , $ overwrite );
278278 }
279279
280+ /**
281+ * Parse an html element into tag, and attributes.
282+ *
283+ * @param string $element The HTML element.
284+ *
285+ * @return array
286+ */
287+ public function parse_element ( $ element ) {
288+ // Cleanup element.
289+ $ element = trim ( $ element , '</> ' );
290+
291+ // Break element up.
292+ $ atts = shortcode_parse_atts ( $ element );
293+ if ( ! empty ( $ atts ['class ' ] ) ) {
294+ $ atts ['class ' ] = explode ( ' ' , $ atts ['class ' ] );
295+ }
296+ $ tag_element = array (
297+ 'tag ' => array_shift ( $ atts ),
298+ 'atts ' => $ atts ,
299+ );
300+
301+ return $ tag_element ;
302+ }
303+
280304 /**
281305 * Get the size from the attributes.
282306 *
@@ -322,7 +346,7 @@ protected function get_transformations_maybe( $url ) {
322346 * @param string $content The HTML to catch URLS from.
323347 */
324348 public function catch_urls ( $ content ) {
325- $ known = array ( );
349+ $ known = $ this -> convert_tags ( $ content );
326350 $ urls = wp_extract_urls ( $ content );
327351 $ dirs = wp_get_upload_dir ();
328352 $ urls = array_map (
0 commit comments