is_offloaded($attachment_id)) { return get_post_meta($attachment_id, 'advmo_path', true); } $object_version = $this->get_object_version($attachment_id); $path_prefix = $this->get_path_prefix(); $metadata = wp_get_attachment_metadata($attachment_id); $file_path = get_attached_file($attachment_id); // For images, use the metadata 'file' if available if (isset($metadata['file'])) { $dirname = advmo_is_media_organized_by_year_month() ? trailingslashit(dirname($metadata['file'])) : ""; return $path_prefix . $dirname . $object_version; } // For non-images, extract the year/month structure from the file path $upload_dir = wp_upload_dir(); $base_dir = $upload_dir['basedir']; // Remove the base upload directory from the file path $relative_path = str_replace($base_dir . '/', '', $file_path); // Extract the year/month $path_parts = explode('/', trim($relative_path, '/'), 3); $response = ''; if (count($path_parts) >= 2 && is_numeric($path_parts[0]) && is_numeric($path_parts[1])) { $response = trailingslashit($path_parts[0] . '/' . $path_parts[1]); } // Fallback: return empty string if we can't determine the structure return $path_prefix . $response . $object_version; } private function uniqueMetaDataSizes($sizes) { $uniqueSizes = []; $dimensionMap = []; foreach ($sizes as $name => $sizeInfo) { // Some metadata entries do not include filesize; treat as 0 to avoid PHP warnings. $sizeInfo['filesize'] = isset($sizeInfo['filesize']) ? (int) $sizeInfo['filesize'] : 0; $dimension = $sizeInfo['width'] . 'x' . $sizeInfo['height']; if (!isset($dimensionMap[$dimension])) { $dimensionMap[$dimension] = $name; $uniqueSizes[$name] = $sizeInfo; } else { // If this size has a larger filesize, replace the existing one $existingName = $dimensionMap[$dimension]; if ($sizeInfo['filesize'] > $uniqueSizes[$existingName]['filesize']) { unset($uniqueSizes[$existingName]); $dimensionMap[$dimension] = $name; $uniqueSizes[$name] = $sizeInfo; } } } return $uniqueSizes; } private function is_offloaded($post_id) { return (bool)get_post_meta($post_id, 'advmo_offloaded', true); } /** * Check if an attachment has offload errors. * * @param int $attachment_id The attachment ID. * @return bool True if there are errors, false otherwise. */ private function has_errors(int $attachment_id): bool { $errors = get_post_meta($attachment_id, 'advmo_error_log', true); return !empty($errors); } private function shouldDeleteLocal() { $settings = get_option('advmo_settings'); $retention_policy = isset($settings['retention_policy']) ? $settings['retention_policy'] : '0'; // Ensure the value is a string and convert it to an integer return intval((string)$retention_policy); } private function shouldDeleteCloudFiles($post) { $advmo_settings = get_option('advmo_settings'); $mirror_delete = false; if (isset($advmo_settings['mirror_delete'])) { $mirror_delete = $advmo_settings['mirror_delete'] == '1'; } return $mirror_delete && $post->post_type === 'attachment' && $this->is_offloaded($post->ID); } /** * Determine whether the current request is a WordPress image-editor * operation: an edit save (wp_save_image()) or a restore to original * (wp_restore_image()). * * The wp_update_attachment_metadata filter fires for both thumbnail * regeneration and image-editor operations. This lets the regeneration * handler defer those operations to AttachmentUpdateObserver so the two do * not both process (and, under Full Cloud Migration, race on) the same * files. On restore the originals are already in the cloud, so a * regeneration pass would only do pointless work and log noise. * * @return bool */ private function isImageEditorOperation(): bool { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10); foreach ($trace as $frame) { if (!isset($frame['function'])) { continue; } if ('wp_save_image' === $frame['function'] || 'wp_restore_image' === $frame['function']) { return true; } } return false; } /** * Get all files from a size data entry, including sources for Modern Image Formats. * * @param array $sizeData The size data array from metadata. * @return array Array of unique file names. */ private function getFilesFromSizeData(array $sizeData): array { $files = []; // Add the primary file if (!empty($sizeData['file'])) { $files[] = $sizeData['file']; } // Add files from sources array (Modern Image Formats support) if (!empty($sizeData['sources']) && is_array($sizeData['sources'])) { foreach ($sizeData['sources'] as $source) { if (!empty($source['file']) && !in_array($source['file'], $files, true)) { $files[] = $source['file']; } } } return $files; } /** * Get root-level source files from metadata (Modern Image Formats support). * * @param array $metadata The attachment metadata. * @return array Array of additional source file names (excluding the main file). */ private function getRootSourceFiles(array $metadata): array { $files = []; if (!empty($metadata['sources']) && is_array($metadata['sources'])) { $mainFile = $metadata['file'] ?? ''; foreach ($metadata['sources'] as $source) { if (!empty($source['file']) && $source['file'] !== $mainFile) { $files[] = $source['file']; } } } return $files; } }