generate_cache_key('get_scannable_posts_count', array( 'post_types' => $post_types, 'user_id' => get_current_user_id(), 'post_statuses' => $post_statuses, )); $cached_result = $this->get_cached_data($cache_key); if (false !== $cached_result) { return (int) $cached_result; } // Core caches counts by type and includes posts whose media lives only in custom fields. $result = 0; foreach ($post_types as $post_type) { $counts = wp_count_posts($post_type, get_current_user_id() ? 'readable' : ''); foreach ($post_statuses as $post_status) { $result += isset($counts->$post_status) ? (int) $counts->$post_status : 0; } } $this->set_cached_data($cache_key, $result); return $result; } /** * Get database statistics for the diagnostic tool. * * @return array */ public function get_database_stats() { global $wpdb; $cache_key = $this->generate_cache_key('get_database_stats', array()); $cached_result = $this->get_cached_data($cache_key); if (false !== $cached_result) { return $cached_result; } $stats = array( 'posts' => array( /* phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- caching implemented below */ 'total' => (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts}"), /* phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- caching implemented below */ 'published' => (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_status = %s", 'publish')), ), 'post_meta' => array( /* phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- caching implemented below */ 'total' => (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->postmeta}"), /* phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- caching implemented below */ 'snaggly_meta' => (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->postmeta} WHERE meta_key LIKE %s", $wpdb->esc_like('_snaggly_') . '%')), ), 'transients' => array( /* phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- caching implemented below */ 'snaggly_transients' => (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like('_transient_snaggly_') . '%')), ), 'meta_indexes' => $this->check_meta_indexes(), ); // Cache briefly: this feeds the on-demand diagnostic tool, not a // hot path, so a short TTL keeps counts reasonably fresh. $this->set_cached_data($cache_key, $stats, 300); return $stats; } /** * Check if meta indexes exist * * @since 1.0.0 * @return array */ public function check_meta_indexes() { global $wpdb; $cache_key = 'meta_indexes_status'; $cached_result = $this->get_cached_data($cache_key); if (false !== $cached_result) { return $cached_result; } $indexes = array(); $meta_keys = array('_snaggly_original_url', '_snaggly_download_date'); foreach ($meta_keys as $meta_key) { $index_name = 'snaggly_meta_' . str_replace('_', '', $meta_key); /* phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Checking index existence requires direct query; caching not applicable for schema checks */ $exists = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = %s AND table_name = %s AND index_name = %s", DB_NAME, $wpdb->postmeta, $index_name )); $indexes[$meta_key] = array( 'index_name' => $index_name, 'exists' => (bool) $exists, ); } $this->set_cached_data($cache_key, $indexes, 3600); return $indexes; } /** * Monitor database query performance for a Snaggly operation. * * @since 1.0.0 * @param string $operation * @param callable $callback * @param array $params * @return array */ public function monitor_query_performance($operation, $callback, $params = array()) { $start_time = microtime(true); $start_memory = memory_get_usage(true); global $wpdb; $old_save_queries = $wpdb->save_queries; $wpdb->save_queries = true; $wpdb->queries = array(); try { $result = call_user_func_array($callback, $params); $performance = array( 'operation' => $operation, 'execution_time' => round((microtime(true) - $start_time) * 1000, 2), 'memory_used' => memory_get_usage(true) - $start_memory, 'peak_memory' => memory_get_peak_usage(true), 'query_count' => count($wpdb->queries), 'queries' => $wpdb->queries, 'success' => true, ); if ($performance['execution_time'] > 1000 && defined('WP_DEBUG') && WP_DEBUG && defined('WP_DEBUG_LOG') && WP_DEBUG_LOG && function_exists('wp_debug_log')) { wp_debug_log("Snaggly: Slow operation detected - {$operation}: {$performance['execution_time']}ms"); } return array('result' => $result, 'performance' => $performance); } catch (Exception $e) { $performance = array( 'operation' => $operation, 'execution_time' => round((microtime(true) - $start_time) * 1000, 2), 'memory_used' => memory_get_usage(true) - $start_memory, 'peak_memory' => memory_get_peak_usage(true), 'query_count' => count($wpdb->queries), 'queries' => $wpdb->queries, 'success' => false, 'error' => $e->getMessage(), ); if (defined('WP_DEBUG') && WP_DEBUG && defined('WP_DEBUG_LOG') && WP_DEBUG_LOG && function_exists('wp_debug_log')) { wp_debug_log("Snaggly: Operation failed - {$operation}: " . $e->getMessage()); } return array('result' => null, 'performance' => $performance); } finally { $wpdb->save_queries = $old_save_queries; } } }