get_results( $wpdb->prepare( "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s AND option_name NOT LIKE %s ORDER BY option_name", '_transient__templately_%', '_transient_timeout_%' ) ); Helper::log( sprintf( 'Found %d Templately transients', count( $transients ) ), 'transient_management' ); return $transients ?: []; } /** * Get count of Templately transients * * @since 3.4.0 * @return int Number of Templately transients */ public static function get_templately_transients_count() { global $wpdb; $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->options} WHERE option_name LIKE %s AND option_name NOT LIKE %s", '_transient__templately_%', '_transient_timeout_%' ) ); return intval( $count ); } /** * Delete all Templately transients * * @since 3.4.0 * @return array Result array with success status, deleted count, and message */ public static function delete_templately_transients() { Helper::log( 'Starting Templately transients deletion', 'transient_management' ); // Get all Templately transients before deletion for logging $transients = self::get_templately_transients(); $total_found = count( $transients ); if ( $total_found === 0 ) { Helper::log( 'No Templately transients found to delete', 'transient_management' ); return [ 'success' => true, 'deleted_count' => 0, 'total_found' => 0, 'message' => __( 'No Templately transients found to delete.', 'templately' ), ]; } $deleted_count = 0; $failed_deletions = []; // Delete each transient using WordPress delete_transient function foreach ( $transients as $transient ) { // Extract the key by removing the '_transient_' prefix $key = self::option_name_to_transient_key( $transient->option_name ); if ( delete_transient( $key ) ) { $deleted_count++; Helper::log( "Successfully deleted transient: {$key}", 'transient_management' ); } else { $failed_deletions[] = $key; Helper::log( "Failed to delete transient: {$key}", 'transient_management' ); } } // Store deletion timestamp and statistics $deletion_info = [ 'timestamp' => current_time( 'mysql' ), 'deleted_count' => $deleted_count, 'total_found' => $total_found, 'failed_count' => count( $failed_deletions ), ]; update_option( 'templately_last_transient_cleanup', $deletion_info ); // Log summary Helper::log( sprintf( 'Transient deletion completed: %d/%d deleted successfully', $deleted_count, $total_found ), 'transient_management' ); if ( ! empty( $failed_deletions ) ) { Helper::log( sprintf( 'Failed to delete %d transients: %s', count( $failed_deletions ), implode( ', ', $failed_deletions ) ), 'transient_management' ); } $success = $deleted_count > 0; $message = $success ? sprintf( /* translators: %1$d: number of deleted transients, %2$d: total found transients */ __( 'Successfully deleted %1$d of %2$d Templately transients.', 'templately' ), $deleted_count, $total_found ) : __( 'Failed to delete any transients.', 'templately' ); return [ 'success' => $success, 'deleted_count' => $deleted_count, 'total_found' => $total_found, 'failed_count' => count( $failed_deletions ), 'failed_keys' => $failed_deletions, 'message' => $message, ]; } /** * Get last transient cleanup information * * @since 3.4.0 * @return array|false Cleanup information or false if never run */ public static function get_last_transient_cleanup() { return get_option( 'templately_last_transient_cleanup', false ); } /** * Get transient management information for API responses * * @since 3.4.0 * @return array Transient management data */ public static function get_transient_management_info() { $count = self::get_templately_transients_count(); $last_cleanup = self::get_last_transient_cleanup(); return [ 'transient_count' => $count, 'last_cleanup' => $last_cleanup, 'has_transients' => $count > 0, ]; } }