0 ? min( 100, (int) round( ( $aggregate_done / $aggregate_total ) * 100 ) ) : ( ! empty( $aggregate['complete'] ) ? 100 : 0 ); $metadata_job = get_option( 'ai_o_optimiser_metadata_scan_job', array() ); $metadata_job = is_array( $metadata_job ) ? $metadata_job : array(); $metadata_status = ! empty( $metadata_job['status'] ) ? sanitize_key( (string) $metadata_job['status'] ) : 'idle'; $metadata_total = max( 0, (int) ( $metadata_job['total'] ?? 0 ) ); $metadata_done = min( $metadata_total > 0 ? $metadata_total : PHP_INT_MAX, max( 0, (int) ( $metadata_job['processed'] ?? 0 ) ) ); $metadata_pct = $metadata_total > 0 ? min( 100, (int) round( ( $metadata_done / $metadata_total ) * 100 ) ) : 0; if ( 'completed' === $metadata_status ) { $metadata_pct = 100; } $schema_report = class_exists( 'AI_O_Optimiser_Schema_Review' ) ? AI_O_Optimiser_Schema_Review::report() : get_option( 'ai_o_optimiser_schema_review_cache', array() ); $schema_report = is_array( $schema_report ) ? $schema_report : array(); $coverage = isset( $schema_report['coverage'] ) && is_array( $schema_report['coverage'] ) ? $schema_report['coverage'] : array(); $schema_total = max( 0, (int) ( $coverage['total'] ?? 0 ) ); $schema_done = min( $schema_total > 0 ? $schema_total : PHP_INT_MAX, max( 0, (int) ( $coverage['scanned'] ?? 0 ) ) ); $schema_pct = $schema_total > 0 ? min( 100, (int) round( ( $schema_done / $schema_total ) * 100 ) ) : 0; $schema_status = empty( $schema_report['checked_at'] ) ? 'not_checked' : ( ! empty( $coverage['complete'] ) ? 'complete' : 'partial' ); $cleanup_next = wp_next_scheduled( 'ai_o_optimiser_daily_cleanup' ); return array( 'storage' => array( 'total_bytes' => max( 0, (int) ( $storage['total_bytes'] ?? 0 ) ), 'table_bytes' => max( 0, (int) ( $storage['table_bytes'] ?? 0 ) ), 'schema_option_bytes' => max( 0, (int) ( $storage['schema_option_bytes'] ?? 0 ) ), 'option_bytes' => max( 0, (int) ( $storage['option_bytes'] ?? ( $storage['schema_option_bytes'] ?? 0 ) ) ), 'tables' => $tables, 'raw_rows' => isset( $tables['ai_o_optimiser_crawler_log']['rows'] ) ? max( 0, (int) $tables['ai_o_optimiser_crawler_log']['rows'] ) : 0, 'aggregate_rows' => isset( $tables['ai_o_optimiser_visibility_daily']['rows'] ) ? max( 0, (int) $tables['ai_o_optimiser_visibility_daily']['rows'] ) : 0, 'metadata_rows' => isset( $tables['ai_o_optimiser_metadata_queue']['rows'] ) ? max( 0, (int) $tables['ai_o_optimiser_metadata_queue']['rows'] ) : 0, ), 'visibility_migration' => array( 'complete' => ! empty( $aggregate['complete'] ), 'processed' => $aggregate_done, 'total' => $aggregate_total, 'progress' => $aggregate_pct, 'updated_at' => isset( $aggregate['updated_at'] ) ? sanitize_text_field( (string) $aggregate['updated_at'] ) : '', ), 'metadata_scan' => array( 'status' => $metadata_status, 'processed' => $metadata_done, 'total' => $metadata_total, 'progress' => $metadata_pct, 'queued' => max( 0, (int) ( $metadata_job['created'] ?? 0 ) ), 'errors' => max( 0, (int) ( $metadata_job['errors'] ?? 0 ) ), 'updated_at' => isset( $metadata_job['updated_at'] ) ? sanitize_text_field( (string) $metadata_job['updated_at'] ) : '', ), 'schema_audit' => array( 'status' => $schema_status, 'processed' => $schema_done, 'total' => $schema_total, 'progress' => $schema_pct, 'overall' => isset( $schema_report['overall'] ) ? sanitize_key( (string) $schema_report['overall'] ) : 'not_checked', 'checked_at' => isset( $schema_report['checked_at'] ) ? sanitize_text_field( (string) $schema_report['checked_at'] ) : '', ), 'cleanup' => array( 'scheduled' => (bool) $cleanup_next, 'next' => $cleanup_next ? (int) $cleanup_next : 0, ), ); } /** Add newer Local/Schema stores without forcing AI_O_Optimiser_DB's legacy report API to change. */ private static function supplement_storage_report( $storage ) { global $wpdb; $resources_file = AI_O_Optimiser_PLUGIN_DIR . 'includes/ai-o-optimiser-owned-resources.php'; $resources = file_exists( $resources_file ) ? require $resources_file : array(); $storage['tables'] = isset( $storage['tables'] ) && is_array( $storage['tables'] ) ? $storage['tables'] : array(); $storage['table_bytes'] = max( 0, (int) ( $storage['table_bytes'] ?? 0 ) ); $schema_table = $wpdb->prefix . 'ai_o_optimiser_schema_activity'; if ( ! isset( $storage['tables']['ai_o_optimiser_schema_activity'] ) && AI_O_Optimiser_Persistence::table_exists( $schema_table ) ) { $status = $wpdb->get_row( $wpdb->prepare( 'SHOW TABLE STATUS LIKE %s', $wpdb->esc_like( $schema_table ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Diagnostic schema metadata is fetched once on the System Impact screen. $bytes = is_array( $status ) ? (int) ( ( $status['Data_length'] ?? 0 ) + ( $status['Index_length'] ?? 0 ) ) : 0; $rows = (int) AI_O_Optimiser_Persistence::cached_var( 'schema_activity_row_count', $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $schema_table ), 60 ); $storage['tables']['ai_o_optimiser_schema_activity'] = array( 'table' => $schema_table, 'rows' => $rows, 'bytes' => $bytes ); $storage['table_bytes'] += $bytes; } $option_bytes = 0; foreach ( isset( $resources['material_options'] ) && is_array( $resources['material_options'] ) ? $resources['material_options'] : array() as $option ) { $value = get_option( (string) $option, null ); if ( null !== $value ) { $option_bytes += strlen( maybe_serialize( $value ) ); } } $storage['option_bytes'] = $option_bytes; $storage['total_bytes'] = $storage['table_bytes'] + $option_bytes; return $storage; } /** Render only on the canonical Settings screen. */ public function render() { $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- screen routing only. if ( 'ai-o-optimiser-settings' !== $page || ! current_user_can( AI_O_Optimiser_SENSITIVE_CAPABILITY ) ) { return; } $state = self::snapshot(); $storage = $state['storage']; $migration = $state['visibility_migration']; $metadata = $state['metadata_scan']; $schema = $state['schema_audit']; $cleanup = $state['cleanup']; $attention = $storage['total_bytes'] > ( 75 * MB_IN_BYTES ) || ! $cleanup['scheduled']; ?>

0 ? esc_html( sprintf( /* translators: Placeholder values are replaced with contextual values shown in this message. */ __( '%1$s%% · %2$s / %3$s checked', 'ai-o-optimiser' ), number_format_i18n( $metadata['progress'] ), number_format_i18n( $metadata['processed'] ), number_format_i18n( $metadata['total'] ) ) ) : esc_html__( 'No active saved scan', 'ai-o-optimiser' ); ?>
0 ? esc_html( sprintf( /* translators: Placeholder values are replaced with contextual values shown in this message. */ __( '%1$s%% · %2$s / %3$s covered', 'ai-o-optimiser' ), number_format_i18n( $schema['progress'] ), number_format_i18n( $schema['processed'] ), number_format_i18n( $schema['total'] ) ) ) : esc_html__( 'No saved audit coverage yet', 'ai-o-optimiser' ); ?>

$table ) : ?>
—
__( 'Idle', 'ai-o-optimiser' ), 'running' => __( 'Running', 'ai-o-optimiser' ), 'paused' => __( 'Paused', 'ai-o-optimiser' ), 'completed' => __( 'Complete', 'ai-o-optimiser' ), 'complete' => __( 'Complete', 'ai-o-optimiser' ), 'cancelled' => __( 'Cancelled', 'ai-o-optimiser' ), 'partial' => __( 'Partial', 'ai-o-optimiser' ), 'not_checked' => __( 'Not checked', 'ai-o-optimiser' ), ); return isset( $labels[ $status ] ) ? $labels[ $status ] : ucfirst( str_replace( '_', ' ', (string) $status ) ); } }