@@ -15,7 +15,7 @@ function init(): void {
15
15
add_action (
16
16
'wp_loaded ' ,
17
17
function (): void {
18
- delete_oembed_cache ();
18
+ delete_oembed_cache ( '' , ' arve-cachetime ' );
19
19
}
20
20
);
21
21
}
@@ -104,15 +104,12 @@ function uninstall(): void {
104
104
global $ wpdb ;
105
105
106
106
if ( version_compare ( $ wpdb ->db_version (), '8.0 ' , '>= ' ) ) {
107
- $ wpdb ->query ( "UPDATE {$ wpdb ->postmeta } SET meta_value = REGEXP_REPLACE( meta_value, '<template data-arve[^>]+></template>', '' ) " );
108
- } else {
109
- delete_oembed_cache ();
110
- delete_option ( 'arve_version ' ); // this will cause another cache clear on reinstall
107
+ $ wpdb ->query ( "UPDATE {$ wpdb ->postmeta } SET meta_value = REGEXP_REPLACE( meta_value, '<template[^>]+arve_cachetime[^>]+></template>', '' ) " );
111
108
}
112
109
}
113
110
114
111
/**
115
- * Deletes the oEmbed cache for all posts .
112
+ * Deletes the oEmbed caches .
116
113
*
117
114
* @link https://github.com/wp-cli/embed-command/blob/c868ec31c65ffa1a61868a91c198a5d815b5bafa/src/Cache_Command.php
118
115
* @author Nicolas Jonas <https://nextgenthemes.com>
@@ -122,19 +119,31 @@ function uninstall(): void {
122
119
*
123
120
* @return int|false The number of rows deleted or false on failure.
124
121
*/
125
- function delete_oembed_cache ( string $ contains = '' ): string {
122
+ function delete_oembed_cache ( string $ like = '' , string $ not_like = '' ): string {
126
123
127
124
global $ wpdb , $ wp_embed ;
128
125
129
126
$ message = '' ;
130
127
131
128
// Get post meta oEmbed caches
132
- if ( $ contains ) {
129
+ if ( $ like ) {
133
130
$ oembed_post_meta_post_ids = (array ) $ wpdb ->get_col (
134
131
$ wpdb ->prepare (
135
132
"SELECT DISTINCT post_id FROM $ wpdb ->postmeta WHERE meta_key LIKE %s AND meta_value LIKE %s " ,
136
133
$ wpdb ->esc_like ( '_oembed_ ' ) . '% ' ,
137
- '% ' . $ wpdb ->esc_like ( $ contains ) . '% '
134
+ '% ' . $ wpdb ->esc_like ( $ like ) . '% '
135
+ )
136
+ );
137
+ } elseif ( $ not_like ) {
138
+ $ oembed_post_meta_post_ids = (array ) $ wpdb ->get_col (
139
+ $ wpdb ->prepare (
140
+ "SELECT DISTINCT post_id FROM $ wpdb ->postmeta
141
+ WHERE meta_key LIKE %s
142
+ AND meta_key NOT LIKE %s
143
+ AND meta_value NOT LIKE %s " ,
144
+ $ wpdb ->esc_like ( '_oembed_ ' ) . '% ' ,
145
+ $ wpdb ->esc_like ( '_oembed_time_ ' ) . '% ' ,
146
+ '% ' . $ wpdb ->esc_like ( $ not_like ) . '% '
138
147
)
139
148
);
140
149
} else {
@@ -147,11 +156,18 @@ function delete_oembed_cache( string $contains = '' ): string {
147
156
}
148
157
149
158
// Get posts oEmbed caches
150
- if ( $ contains ) {
159
+ if ( $ like ) {
151
160
$ oembed_post_post_ids = (array ) $ wpdb ->get_col (
152
161
$ wpdb ->prepare (
153
162
"SELECT ID FROM $ wpdb ->posts WHERE post_type = 'oembed_cache' AND post_content LIKE %s " ,
154
- '% ' . $ wpdb ->esc_like ( $ contains ) . '% '
163
+ '% ' . $ wpdb ->esc_like ( $ like ) . '% '
164
+ )
165
+ );
166
+ } elseif ( $ not_like ) {
167
+ $ oembed_post_post_ids = (array ) $ wpdb ->get_col (
168
+ $ wpdb ->prepare (
169
+ "SELECT ID FROM $ wpdb ->posts WHERE post_type = 'oembed_cache' AND post_content NOT LIKE %s " ,
170
+ '% ' . $ wpdb ->esc_like ( $ not_like ) . '% '
155
171
)
156
172
);
157
173
} else {
@@ -161,12 +177,20 @@ function delete_oembed_cache( string $contains = '' ): string {
161
177
}
162
178
163
179
// Get transient oEmbed caches
164
- if ( $ contains ) {
180
+ if ( $ like ) {
165
181
$ oembed_transients = $ wpdb ->get_col (
166
182
$ wpdb ->prepare (
167
183
"SELECT option_name FROM $ wpdb ->options WHERE option_name LIKE %s AND option_value LIKE %s " ,
168
184
$ wpdb ->esc_like ( '_transient_oembed_ ' ) . '% ' ,
169
- '% ' . $ wpdb ->esc_like ( $ contains ) . '% '
185
+ '% ' . $ wpdb ->esc_like ( $ like ) . '% '
186
+ )
187
+ );
188
+ } elseif ( $ not_like ) {
189
+ $ oembed_transients = $ wpdb ->get_col (
190
+ $ wpdb ->prepare (
191
+ "SELECT option_name FROM $ wpdb ->options WHERE option_name LIKE %s AND option_value NOT LIKE %s " ,
192
+ $ wpdb ->esc_like ( '_transient_oembed_ ' ) . '% ' ,
193
+ '% ' . $ wpdb ->esc_like ( $ not_like ) . '% '
170
194
)
171
195
);
172
196
} else {
@@ -231,12 +255,50 @@ function ( $items ) {
231
255
$ message .= esc_html__ ( 'No oEmbed caches to clear! ' , 'advanced-responsive-video-embedder ' );
232
256
}
233
257
234
- if ( wp_using_ext_object_cache () ) {
235
- $ object_cache_msg = esc_html__ ( 'Oembed transients are stored in an external object cache, and ARVE only deletes those stored in the database. You must flush the cache to delete all transients. ' , 'advanced-responsive-video-embedder ' );
236
- update_option ( 'arve_object_cache_msg ' , $ object_cache_msg );
258
+ return $ message ;
259
+ }
237
260
238
- $ message .= ' ' . $ object_cache_msg ;
261
+ /**
262
+ * @global wpdb $wpdb
263
+ */
264
+ function delete_transients ( string $ prefix , string $ contains = '' ): string {
265
+
266
+ global $ wpdb ;
267
+
268
+ if ( $ contains ) {
269
+ $ transients = $ wpdb ->get_col (
270
+ $ wpdb ->prepare (
271
+ "SELECT option_name FROM $ wpdb ->options WHERE option_name LIKE %s AND option_value LIKE %s " ,
272
+ $ wpdb ->esc_like ( '_transient_ ' . $ prefix ) . '% ' ,
273
+ '% ' . $ wpdb ->esc_like ( $ contains ) . '% '
274
+ )
275
+ );
276
+ } else {
277
+ $ transients = $ wpdb ->get_col (
278
+ $ wpdb ->prepare (
279
+ "SELECT option_name FROM $ wpdb ->options WHERE option_name LIKE %s " ,
280
+ $ wpdb ->esc_like ( '_transient_ ' . $ prefix ) . '% '
281
+ )
282
+ );
239
283
}
240
284
241
- return $ message ;
285
+ $ count = 0 ;
286
+
287
+ foreach ( $ transients as $ transient_name ) {
288
+ // Strip '_transient_' to get the key for delete_transient()
289
+ $ transient_key = str_replace ( '_transient_ ' , '' , $ transient_name );
290
+ if ( delete_transient ( $ transient_key ) ) {
291
+ ++$ count ;
292
+ }
293
+ }
294
+
295
+ if ( $ count > 0 ) {
296
+ return sprintf (
297
+ // translators: %d: Number of transients deleted.
298
+ esc_html__ ( 'Deleted %d transients. ' , 'advanced-responsive-video-embedder ' ),
299
+ $ count
300
+ );
301
+ }
302
+
303
+ return esc_html__ ( 'No transients deleted. ' , 'advanced-responsive-video-embedder ' );
242
304
}
0 commit comments