Skip to content

Commit cf3d3fc

Browse files
committed
just leave it for outdated MYSQL
1 parent 9db28a1 commit cf3d3fc

File tree

1 file changed

+80
-18
lines changed

1 file changed

+80
-18
lines changed

php/init.php

+80-18
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function init(): void {
1515
add_action(
1616
'wp_loaded',
1717
function (): void {
18-
delete_oembed_cache();
18+
delete_oembed_cache( '', 'arve-cachetime' );
1919
}
2020
);
2121
}
@@ -104,15 +104,12 @@ function uninstall(): void {
104104
global $wpdb;
105105

106106
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>', '' )" );
111108
}
112109
}
113110

114111
/**
115-
* Deletes the oEmbed cache for all posts.
112+
* Deletes the oEmbed caches.
116113
*
117114
* @link https://github.com/wp-cli/embed-command/blob/c868ec31c65ffa1a61868a91c198a5d815b5bafa/src/Cache_Command.php
118115
* @author Nicolas Jonas <https://nextgenthemes.com>
@@ -122,19 +119,31 @@ function uninstall(): void {
122119
*
123120
* @return int|false The number of rows deleted or false on failure.
124121
*/
125-
function delete_oembed_cache( string $contains = '' ): string {
122+
function delete_oembed_cache( string $like = '', string $not_like = '' ): string {
126123

127124
global $wpdb, $wp_embed;
128125

129126
$message = '';
130127

131128
// Get post meta oEmbed caches
132-
if ( $contains ) {
129+
if ( $like ) {
133130
$oembed_post_meta_post_ids = (array) $wpdb->get_col(
134131
$wpdb->prepare(
135132
"SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key LIKE %s AND meta_value LIKE %s",
136133
$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 ) . '%'
138147
)
139148
);
140149
} else {
@@ -147,11 +156,18 @@ function delete_oembed_cache( string $contains = '' ): string {
147156
}
148157

149158
// Get posts oEmbed caches
150-
if ( $contains ) {
159+
if ( $like ) {
151160
$oembed_post_post_ids = (array) $wpdb->get_col(
152161
$wpdb->prepare(
153162
"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 ) . '%'
155171
)
156172
);
157173
} else {
@@ -161,12 +177,20 @@ function delete_oembed_cache( string $contains = '' ): string {
161177
}
162178

163179
// Get transient oEmbed caches
164-
if ( $contains ) {
180+
if ( $like ) {
165181
$oembed_transients = $wpdb->get_col(
166182
$wpdb->prepare(
167183
"SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s AND option_value LIKE %s",
168184
$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 ) . '%'
170194
)
171195
);
172196
} else {
@@ -231,12 +255,50 @@ function ( $items ) {
231255
$message .= esc_html__( 'No oEmbed caches to clear!', 'advanced-responsive-video-embedder' );
232256
}
233257

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+
}
237260

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+
);
239283
}
240284

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' );
242304
}

0 commit comments

Comments
 (0)