Improve database optimizer routine

Now includes comment meta.
This commit is contained in:
Tetrakern 2023-10-06 02:26:36 +02:00
parent 585d177caa
commit 929e1f1040

View File

@ -114,7 +114,7 @@ if ( ! defined( 'FICTIONEER_ADMIN_SETTINGS_NOTICES' ) ) {
'fictioneer-not-renamed-role' => __( 'Error. Role could not be renamed.', 'fictioneer' ), 'fictioneer-not-renamed-role' => __( 'Error. Role could not be renamed.', 'fictioneer' ),
'fictioneer-removed-role' => __( 'Role removed.', 'fictioneer' ), 'fictioneer-removed-role' => __( 'Role removed.', 'fictioneer' ),
'fictioneer-not-removed-role' => __( 'Error. Role could not be removed.', 'fictioneer' ), 'fictioneer-not-removed-role' => __( 'Error. Role could not be removed.', 'fictioneer' ),
'fictioneer-db-optimization-preview' => __( '%s superfluous rows found. Please backup your database before performing any optimization.', 'fictioneer' ), 'fictioneer-db-optimization-preview' => __( '%s superfluous post meta rows found. %s superfluous comment meta rows found. Please backup your database before performing any optimization.', 'fictioneer' ),
'fictioneer-db-optimization' => __( '%s superfluous rows have been deleted.', 'fictioneer' ), 'fictioneer-db-optimization' => __( '%s superfluous rows have been deleted.', 'fictioneer' ),
'fictioneer-add-story-hidden' => __( 'The "fictioneer_story_hidden" meta field has been appended with value 0.', 'fictioneer' ), 'fictioneer-add-story-hidden' => __( 'The "fictioneer_story_hidden" meta field has been appended with value 0.', 'fictioneer' ),
'fictioneer-add-story-sticky' => __( 'The "fictioneer_story_sticky" meta field has been appended with value 0.', 'fictioneer' ), 'fictioneer-add-story-sticky' => __( 'The "fictioneer_story_sticky" meta field has been appended with value 0.', 'fictioneer' ),
@ -134,29 +134,31 @@ function fictioneer_admin_settings_notices() {
$success = $_GET['success'] ?? null; $success = $_GET['success'] ?? null;
$failure = $_GET['failure'] ?? null; $failure = $_GET['failure'] ?? null;
$info = $_GET['info'] ?? null; $info = $_GET['info'] ?? null;
$data = $_GET['data'] ?? ''; $data = explode( ',', $_GET['data'] ?? '' );
$data = is_array( $data ) ? $data : [];
$data = array_map( 'esc_html', $data );
// Has success notice? // Has success notice?
if ( ! empty( $success ) && isset( FICTIONEER_ADMIN_SETTINGS_NOTICES[ $success ] ) ) { if ( ! empty( $success ) && isset( FICTIONEER_ADMIN_SETTINGS_NOTICES[ $success ] ) ) {
echo '<div class="notice notice-success is-dismissible"><p>' . sprintf( echo '<div class="notice notice-success is-dismissible"><p>' . vsprintf(
FICTIONEER_ADMIN_SETTINGS_NOTICES[ $success ], FICTIONEER_ADMIN_SETTINGS_NOTICES[ $success ],
esc_html( $data ) $data
) . '</p></div>'; ) . '</p></div>';
} }
// Has failure notice? // Has failure notice?
if ( ! empty( $failure ) && isset( FICTIONEER_ADMIN_SETTINGS_NOTICES[ $failure ] ) ) { if ( ! empty( $failure ) && isset( FICTIONEER_ADMIN_SETTINGS_NOTICES[ $failure ] ) ) {
echo '<div class="notice notice-error is-dismissible"><p>' . sprintf( echo '<div class="notice notice-error is-dismissible"><p>' . vsprintf(
FICTIONEER_ADMIN_SETTINGS_NOTICES[ $failure ], FICTIONEER_ADMIN_SETTINGS_NOTICES[ $failure ],
esc_html( $data ) $data
) . '</p></div>'; ) . '</p></div>';
} }
// Has info notice? // Has info notice?
if ( ! empty( $info ) && isset( FICTIONEER_ADMIN_SETTINGS_NOTICES[ $info ] ) ) { if ( ! empty( $info ) && isset( FICTIONEER_ADMIN_SETTINGS_NOTICES[ $info ] ) ) {
echo '<div class="notice notice-info is-dismissible"><p>' . sprintf( echo '<div class="notice notice-info is-dismissible"><p>' . vsprintf(
FICTIONEER_ADMIN_SETTINGS_NOTICES[ $info ], FICTIONEER_ADMIN_SETTINGS_NOTICES[ $info ],
esc_html( $data ) $data
) . '</p></div>'; ) . '</p></div>';
} }
} }
@ -964,7 +966,7 @@ function fictioneer_tools_optimize_database() {
global $wpdb; global $wpdb;
// Allowed // Delete post meta
$allowed_meta_keys = fictioneer_get_falsy_meta_allow_list(); $allowed_meta_keys = fictioneer_get_falsy_meta_allow_list();
$not_like_sql = ''; $not_like_sql = '';
@ -978,8 +980,7 @@ function fictioneer_tools_optimize_database() {
$not_like_sql = " AND " . implode( ' AND ', $not_like_statements ); $not_like_sql = " AND " . implode( ' AND ', $not_like_statements );
} }
// Delete and return number of rows $post_meta_count = $wpdb->query("
$count = $wpdb->query("
DELETE FROM $wpdb->postmeta DELETE FROM $wpdb->postmeta
WHERE meta_key LIKE '_fictioneer_%' WHERE meta_key LIKE '_fictioneer_%'
OR ( OR (
@ -990,11 +991,26 @@ function fictioneer_tools_optimize_database() {
) )
"); ");
// Comment meta
$comment_meta_count = $wpdb->query("
DELETE FROM $wpdb->commentmeta
WHERE (
meta_key = 'fictioneer_visibility_code'
AND SUBSTRING_INDEX(SUBSTRING_INDEX(meta_value, ';', 2), ':', -1) < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 24 HOUR))
) OR (
meta_key LIKE 'fictioneer%'
AND (meta_value = '' OR meta_value IS NULL OR meta_value = '0')
)
");
// Total rows
$total = $post_meta_count + $comment_meta_count;
// Log // Log
fictioneer_log( fictioneer_log(
sprintf( sprintf(
__( 'Optimized database and removed %s superfluous rows.', 'fictioneer' ), __( 'Optimized database and removed %s superfluous rows.', 'fictioneer' ),
$count $total
) )
); );
@ -1003,7 +1019,7 @@ function fictioneer_tools_optimize_database() {
add_query_arg( add_query_arg(
array( array(
'success' => 'fictioneer-db-optimization', 'success' => 'fictioneer-db-optimization',
'data' => $count 'data' => $total
), ),
wp_get_referer() wp_get_referer()
) )
@ -1027,7 +1043,7 @@ function fictioneer_tools_optimize_database_preview() {
global $wpdb; global $wpdb;
// Allowed // Post meta
$allowed_meta_keys = fictioneer_get_falsy_meta_allow_list(); $allowed_meta_keys = fictioneer_get_falsy_meta_allow_list();
$not_like_sql = ''; $not_like_sql = '';
@ -1041,8 +1057,7 @@ function fictioneer_tools_optimize_database_preview() {
$not_like_sql = " AND " . implode( ' AND ', $not_like_statements ); $not_like_sql = " AND " . implode( ' AND ', $not_like_statements );
} }
// Return number of rows affected $post_meta_count = $wpdb->get_var("
$count = $wpdb->get_var("
SELECT COUNT(*) FROM $wpdb->postmeta SELECT COUNT(*) FROM $wpdb->postmeta
WHERE meta_key LIKE '_fictioneer_%' WHERE meta_key LIKE '_fictioneer_%'
OR ( OR (
@ -1053,12 +1068,24 @@ function fictioneer_tools_optimize_database_preview() {
) )
"); ");
// Comment meta
$comment_meta_count = $wpdb->get_var("
SELECT COUNT(*) FROM $wpdb->commentmeta
WHERE (
meta_key = 'fictioneer_visibility_code'
AND SUBSTRING_INDEX(SUBSTRING_INDEX(meta_value, ';', 2), ':', -1) < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 24 HOUR))
) OR (
meta_key LIKE 'fictioneer%'
AND (meta_value = '' OR meta_value IS NULL OR meta_value = '0')
)
");
// Redirect // Redirect
wp_safe_redirect( wp_safe_redirect(
add_query_arg( add_query_arg(
array( array(
'info' => 'fictioneer-db-optimization-preview', 'info' => 'fictioneer-db-optimization-preview',
'data' => $count 'data' => "{$post_meta_count},{$comment_meta_count}"
), ),
wp_get_referer() wp_get_referer()
) )