Optimize purge all schemas action
This commit is contained in:
parent
60c69fd0f6
commit
2c49e9e502
@ -157,64 +157,68 @@ add_action( 'wp_ajax_fictioneer_ajax_purge_schema', 'fictioneer_ajax_purge_schem
|
|||||||
* AJAX: Purge schemas for all posts and pages
|
* AJAX: Purge schemas for all posts and pages
|
||||||
*
|
*
|
||||||
* @since 5.7.2
|
* @since 5.7.2
|
||||||
|
* @since 5.26.0 - Refactored with only custom SQL.
|
||||||
|
*
|
||||||
|
* @global wpdb $wpdb WordPress database object.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function fictioneer_ajax_purge_all_schemas() {
|
function fictioneer_ajax_purge_all_schemas() {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
if ( ! fictioneer_validate_settings_ajax() ) {
|
if ( ! fictioneer_validate_settings_ajax() ) {
|
||||||
wp_send_json_error( array( 'notice' => __( 'Invalid request.', 'fictioneer' ) ) );
|
wp_send_json_error( array( 'notice' => __( 'Invalid request.', 'fictioneer' ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup
|
// Setup
|
||||||
|
$post_types = "'post', 'page', 'fcn_story', 'fcn_chapter', 'fcn_collection', 'fcn_recommendation'";
|
||||||
$offset = absint( $_POST['offset'] ?? 0 );
|
$offset = absint( $_POST['offset'] ?? 0 );
|
||||||
$limit = 100;
|
$limit = 400;
|
||||||
|
|
||||||
// Query
|
// Total rows
|
||||||
$args = array(
|
$total = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_type IN ({$post_types})" );
|
||||||
'post_type' => ['post', 'page', 'fcn_story', 'fcn_chapter', 'fcn_collection', 'fcn_recommendation'],
|
|
||||||
'post_status' => 'any',
|
// SQL query to get IDs
|
||||||
'fields' => 'ids',
|
$post_ids = $wpdb->get_col(
|
||||||
'ignore_sticky_posts' => 1,
|
$wpdb->prepare(
|
||||||
'offset' => $offset,
|
"SELECT ID FROM {$wpdb->posts}
|
||||||
'posts_per_page' => $limit,
|
WHERE post_type IN ({$post_types})
|
||||||
'update_post_meta_cache' => false,
|
AND post_status != 'trash'
|
||||||
'update_post_term_cache' => false
|
LIMIT %d OFFSET %d",
|
||||||
|
$limit,
|
||||||
|
$offset
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
$query = new WP_Query( $args );
|
|
||||||
|
|
||||||
// If post have been found...
|
// If post have been found...
|
||||||
if ( $query->have_posts() ) {
|
if ( ! empty( $post_ids ) ) {
|
||||||
global $wpdb;
|
// Delete meta keys for fetched post IDs
|
||||||
|
$post_placeholders = implode( ', ', array_fill( 0, count( $post_ids ), '%d' ) );
|
||||||
|
|
||||||
// Prepare SQL
|
$wpdb->query(
|
||||||
$placeholders = implode( ', ', array_fill( 0, count( $query->posts ), '%d' ) );
|
$wpdb->prepare(
|
||||||
$meta_keys = array(
|
"DELETE FROM {$wpdb->postmeta}
|
||||||
'fictioneer_schema',
|
WHERE post_id IN ($post_placeholders)
|
||||||
'fictioneer_seo_cache'
|
AND meta_key IN ('fictioneer_schema', 'fictioneer_seo_cache')",
|
||||||
|
$post_ids
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Execute
|
|
||||||
foreach ( $meta_keys as $meta_key ) {
|
|
||||||
$wpdb->query(
|
|
||||||
$wpdb->prepare(
|
|
||||||
"DELETE FROM {$wpdb->postmeta} WHERE post_id IN ($placeholders) AND meta_key = %s",
|
|
||||||
array_merge( $query->posts, [ $meta_key ] )
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Log
|
// Log
|
||||||
fictioneer_log(
|
fictioneer_log(
|
||||||
__( 'Purged all schemas graphs.', 'fictioneer' )
|
sprintf(
|
||||||
|
__( 'Purging all schema graphs... %1$s/%2$s', 'fictioneer' ),
|
||||||
|
( $offset + $limit ) < $total ? ( $offset + $limit ) : $total,
|
||||||
|
$total
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// ... continue with next batch
|
// ... continue with next batch
|
||||||
wp_send_json_success(
|
wp_send_json_success(
|
||||||
array(
|
array(
|
||||||
'finished' => false,
|
'finished' => false,
|
||||||
'processed' => count( $query->posts ),
|
'processed' => count( $post_ids ),
|
||||||
'total' => $query->found_posts,
|
'total' => $total,
|
||||||
'next_offset' => $offset + $limit
|
'next_offset' => $offset + $limit
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -222,12 +226,15 @@ function fictioneer_ajax_purge_all_schemas() {
|
|||||||
// Purge caches
|
// Purge caches
|
||||||
fictioneer_purge_all_caches();
|
fictioneer_purge_all_caches();
|
||||||
|
|
||||||
|
// Log
|
||||||
|
fictioneer_log( __( 'Finished purging all schema graphs.', 'fictioneer' ) );
|
||||||
|
|
||||||
// ... all done
|
// ... all done
|
||||||
wp_send_json_success(
|
wp_send_json_success(
|
||||||
array(
|
array(
|
||||||
'finished' => true,
|
'finished' => true,
|
||||||
'processed' => 0,
|
'processed' => 0,
|
||||||
'total' => $query->found_posts,
|
'total' => $total,
|
||||||
'next_offset' => -1
|
'next_offset' => -1
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user