settings = SnagglySettings::get_instance(); } /** * Debug logging helper * * @param string $message * @param array $context */ protected function debug_log($message, $context = array()) { $logger = SnagglyLogger::get_instance(); $logger->debug($message, $context); } /** * Scan all posts for remote links. * Automatically switches to progressive scan for large sites. * * @return array */ public function scan_all_posts() { $post_types = $this->settings->get_scan_post_types(); $total_posts = $this->get_total_scannable_posts($post_types); $large_site_threshold = $this->settings->get_large_site_threshold(); $this->debug_log("Starting scan of {$total_posts} posts across post types: " . implode(', ', $post_types)); if ($total_posts > $large_site_threshold) { $this->debug_log("Large site detected ({$total_posts} posts > {$large_site_threshold} threshold). Consider using progressive scan."); if ($this->settings->is_progressive_scan_enabled() && $this->settings->get_auto_progressive_for_large_sites()) { $this->debug_log("Auto-switching to progressive scan for large site"); return $this->scan_all_posts_progressive(); } } // Memory-efficient scanning for smaller sites return $this->scan_all_posts_batched(); } /** * Scan all posts using progressive method (for large sites) * * @return array */ protected function scan_all_posts_progressive() { $scan_session = $this->start_progressive_scan(); if (is_wp_error($scan_session)) { return array(); } $session_id = $scan_session['session_id']; // Continue scanning until complete while (!$scan_session['is_complete']) { $scan_session = $this->continue_progressive_scan($session_id); if (is_wp_error($scan_session)) { break; } // Prevent infinite loops if ($scan_session['processed_posts'] >= $scan_session['total_posts']) { break; } } return $scan_session['found_links'] ?? array(); } /** * Scan all posts using batched method (memory-efficient) * * @return array */ protected function scan_all_posts_batched() { $post_types = $this->settings->get_scan_post_types(); if (!$post_types) { return array(); } $batch_size = min($this->settings->get_batch_size(), 50); // Cap at 50 for memory safety $remote_links = array(); $offset = 0; $total_processed = 0; $post_ids = array(); do { // Check memory usage before each batch if (!$this->check_memory_availability()) { $this->debug_log("Memory limit approaching, stopping scan early. Processed {$total_processed} posts."); break; } $args = array( 'post_type' => $post_types, 'post_status' => $this->get_scannable_post_statuses(), 'perm' => get_current_user_id() ? 'readable' : '', 'orderby' => 'ID', 'order' => 'ASC', 'posts_per_page' => $batch_size, 'offset' => $offset, 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'fields' => 'ids', // Only get IDs first for memory efficiency ); $post_ids = get_posts($args); if (empty($post_ids)) { break; } // Process each post individually to minimize memory usage foreach ($post_ids as $post_id) { $post = get_post($post_id); if ($post) { $links = $this->scan_post_content($post); if (!empty($links)) { $remote_links = array_merge($remote_links, $links); } unset($post); } $total_processed++; } $offset += $batch_size; $batch_count = count($post_ids); unset($post_ids); if ($total_processed % 100 === 0 && function_exists('gc_collect_cycles')) { gc_collect_cycles(); $this->debug_log("Processed {$total_processed} posts, memory usage: " . size_format(memory_get_usage(true))); } } while ($batch_count === $batch_size); $this->debug_log("Completed batched scan. Total processed: {$total_processed}, Links found: " . count($remote_links)); return $remote_links; } /** * Get the post statuses to include when scanning, honoring the * scan_private_posts setting. Public so SnagglyDBOptimizer's scan * counts can be kept in sync with what a scan actually queries. * * @return array */ public function get_scannable_post_statuses() { $statuses = array('publish'); if ($this->settings->should_scan_private_posts()) { $statuses[] = 'private'; } return $statuses; } /** * Check if sufficient memory is available for continued processing * * @return bool */ protected function check_memory_availability() { $available = SnagglyMemoryUtils::check_memory_availability((float) $this->settings->get_memory_threshold()); if (!$available) { $this->debug_log("Memory usage critical: " . size_format(memory_get_usage(true)) . " of " . size_format(SnagglyMemoryUtils::get_memory_limit())); } return $available; } /** * Scan individual post content * * @param WP_Post $post * @return array */ public function scan_post_content($post) { $links = array(); if (!in_array($post->post_type, $this->settings->get_scan_post_types(), true) || (get_current_user_id() && !current_user_can('read_post', $post->ID))) { return $links; } $all_content = ''; // Scan main post content - both raw and rendered if (!empty($post->post_content)) { $all_content .= $post->post_content . "\n"; // Also add rendered content to catch dynamically generated URLs // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Using core WordPress filter $rendered_content = apply_filters('the_content', $post->post_content); if ($rendered_content !== $post->post_content) { $this->debug_log("Content differs after rendering - including rendered version"); $all_content .= $rendered_content . "\n"; } // Extract URLs from Gutenberg blocks using parser where available, with regex fallback $block_urls_text = $this->extract_block_urls($post->post_content); if (!empty($block_urls_text)) { $this->debug_log("Found Gutenberg block URLs"); $all_content .= $block_urls_text . "\n"; } } // Extract URLs from all content $extracted_urls = $this->extract_remote_urls($all_content); foreach ($extracted_urls as $url) { if ($this->is_valid_remote_url($url)) { $links[] = $this->build_link_entry($post, $url, 'content'); } } return $links; } /** * Build a single scan-result entry. * * @param WP_Post $post * @param string $url * @param string $found_in * @return array */ protected function build_link_entry($post, $url, $found_in) { return array( 'post_id' => $post->ID, 'post_title' => $post->post_title, 'post_type' => $post->post_type, 'post_url' => get_permalink($post->ID), 'remote_url' => $url, 'filename' => $this->get_filename_from_url($url), 'extension' => $this->get_extension_from_url($url), 'file_size' => null, 'status' => 'pending', 'found_in' => $found_in, ); } /** * Extract URLs from Gutenberg block attributes * * @param string $content * @return string */ protected function extract_block_urls($content) { $urls = ''; // Preferred: use Gutenberg block parser if (function_exists('parse_blocks')) { $blocks = parse_blocks($content); $urls_list = $this->extract_urls_from_blocks($blocks); if (!empty($urls_list)) { $urls .= implode("\n", $urls_list); } } else { // Fallback to JSON-in-comment attribute regex preg_match_all('//', $content, $matches); if (!empty($matches[1])) { foreach ($matches[1] as $json_string) { $data = json_decode($json_string, true); if ($data) { $urls_in_block = $this->extract_urls_from_array($data); if (!empty($urls_in_block)) { $urls .= $urls_in_block . "\n"; } } } } } return $urls; } /** * @param array $blocks * @return array */ protected function extract_urls_from_blocks($blocks) { $urls = array(); foreach ((array) $blocks as $block) { if (!empty($block['attrs'])) { $attr_urls_text = $this->extract_urls_from_array($block['attrs']); if (!empty($attr_urls_text)) { $urls = array_merge($urls, preg_split('/\n+/', trim($attr_urls_text))); } } if (!empty($block['innerHTML']) && is_string($block['innerHTML'])) { $urls = array_merge($urls, $this->extract_remote_urls($block['innerHTML'])); } if (!empty($block['innerContent']) && is_array($block['innerContent'])) { foreach ($block['innerContent'] as $inner) { if (is_string($inner)) { $urls = array_merge($urls, $this->extract_remote_urls($inner)); } } } if (!empty($block['innerBlocks'])) { $urls = array_merge($urls, $this->extract_urls_from_blocks($block['innerBlocks'])); } } return array_values(array_unique(array_filter($urls))); } /** * Recursively extract URLs from array (block attributes) * * @param mixed $data * @return string */ protected function extract_urls_from_array($data) { $urls = ''; if (is_array($data)) { foreach ($data as $key => $value) { if (is_string($value) && filter_var($value, FILTER_VALIDATE_URL)) { $this->debug_log("Found URL in block attribute '$key': $value"); $urls .= $value . "\n"; } elseif (is_array($value)) { $urls .= $this->extract_urls_from_array($value); } } } return $urls; } /** * Extract remote URLs from content * * @param string $content * @return array */ protected function extract_remote_urls($content) { $urls = array(); $this->debug_log("Scanning content (first 500 chars): " . substr($content, 0, 500)); // Pattern 1 & 2: href and src attributes foreach (array('href', 'src') as $attribute) { preg_match_all('/' . $attribute . '=["\']([^"\']*)["\']/', $content, $matches); if (!empty($matches[1])) { foreach ($matches[1] as $url) { $this->collect_url_if_relevant($url, $urls, $attribute); } } } // Match the complete URL before checking its extension. A regex alternation // such as doc|docx or gz|gzip can otherwise truncate a valid file URL. preg_match_all('/https?:\\/\\/[^\\s<>"\']+/i', $content, $direct_matches); foreach ($direct_matches[0] as $matched_url) { $cleaned_url = $this->clean_url($matched_url); if ($cleaned_url && $this->settings->is_allowed_extension($this->get_extension_from_url($cleaned_url))) { $urls[] = $cleaned_url; } } // Pattern 4: URLs in Gutenberg block comments (backup pattern) preg_match_all('/