@@ -38,6 +38,13 @@ class Delivery implements Setup {
3838 */
3939 protected $ filter ;
4040
41+ /**
42+ * The meta data cache key to store URLS.
43+ *
44+ * @var string
45+ */
46+ const META_CACHE_KEY = '_cld_replacements ' ;
47+
4148 /**
4249 * Component constructor.
4350 *
@@ -64,8 +71,42 @@ public function setup() {
6471 add_filter ( 'wp_calculate_image_srcset ' , array ( $ this ->media , 'image_srcset ' ), 10 , 5 );
6572 $ this ->filter = $ this ->media ->filter ;
6673 // Add filters.
67- add_action ( 'cloudinary_string_replace ' , array ( $ this , 'convert_tags ' ) );
74+ add_filter ( 'the_content ' , array ( $ this , 'filter_local ' ) );
6875 // @todo: Add filter for `cloudinary_string_replace` with method `convert_urls` To catch non ID's tags.
76+ add_action ( 'save_post ' , array ( $ this , 'remove_replace_cache ' ), 10 , 2 );
77+ }
78+
79+ /**
80+ * Delete the content replacement cache data.
81+ *
82+ * @param int $post_id The post ID to remove cache from.
83+ * @param string $content The post content.
84+ */
85+ public function remove_replace_cache ( $ post_id , $ content ) {
86+ delete_post_meta ( $ post_id , self ::META_CACHE_KEY );
87+ $ this ->convert_tags ( $ post_id , $ content );
88+ }
89+
90+ /**
91+ * Filter out the local URLS from the content.
92+ *
93+ * @param string $content The HTML of the content to filter.
94+ *
95+ * @return string
96+ */
97+ public function filter_local ( $ content ) {
98+ $ post_id = get_queried_object_id ();
99+ if ( ! empty ( $ post_id ) ) {
100+ $ replacements = get_post_meta ( $ post_id , self ::META_CACHE_KEY , true );
101+ if ( empty ( $ replacements ) ) {
102+ $ replacements = $ this ->convert_tags ( $ post_id , $ content );
103+ }
104+ foreach ( $ replacements as $ search => $ replace ) {
105+ String_Replace::replace ( $ search , $ replace );
106+ }
107+ }
108+
109+ return $ content ;
69110 }
70111
71112 /**
@@ -181,11 +222,13 @@ public function find_attachment_size_urls( $urls ) {
181222 /**
182223 * Convert media tags from Local to Cloudinary, and register with String_Replace.
183224 *
225+ * @param int $post_id The post ID.
184226 * @param string $content The HTML to find tags and prep replacement in.
185227 */
186- public function convert_tags ( $ content ) {
228+ public function convert_tags ( $ post_id , $ content ) {
187229
188- $ tags = $ this ->filter ->get_media_tags ( $ content );
230+ $ tags = $ this ->filter ->get_media_tags ( $ content );
231+ $ replacements = array ();
189232 foreach ( $ tags as $ element ) {
190233 $ attachment_id = $ this ->filter ->get_id_from_tag ( $ element );
191234 if ( empty ( $ attachment_id ) ) {
@@ -231,10 +274,16 @@ public function convert_tags( $content ) {
231274 $ replace = $ this ->media ->apply_srcset ( $ replace , $ attachment_id , $ overwrite );
232275
233276 // Register replacement.
234- String_Replace:: replace ( $ element, $ replace ) ;
277+ $ replacements [ $ element ] = $ replace ;
235278 }
279+
280+ // Update the post meta cache.
281+ update_post_meta ( $ post_id , self ::META_CACHE_KEY , $ replacements );
282+
236283 // Catch others.
237284 $ this ->catch_urls ( $ content );
285+
286+ return $ replacements ;
238287 }
239288
240289 /**
0 commit comments