register(); } public static function getInstance(): self { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } private function register() { add_action('admin_menu', [$this, 'add_menu']); add_action('admin_init', [$this, 'initialize']); add_action('wp_ajax_advmo_download_errors_csv', array($this, 'handle_download_errors_csv')); } public function initialize() { $this->add_media_overview_fields(); $this->add_media_overview_sections(); } public function add_menu() { add_submenu_page( 'advmo', __('Media Overview', 'advanced-media-offloader'), __('Media Overview', 'advanced-media-offloader'), 'manage_options', 'advmo_media_overview', [$this, 'media_overview_page_view'] ); } public function media_overview_page_view() { advmo_get_view('admin/media_overview'); } private function add_media_overview_sections() { add_settings_section( 'media_overview', __('Media Library Overview', 'advanced-media-offloader'), function () { echo '
' . esc_attr__('Get a comprehensive overview of all media files in your WordPress library. Easily identify any files that haven’t been offloaded to the Cloud and offload them in bulk.', 'advanced-media-offloader') . '
'; }, 'advmo_media_overview', [ 'before_section' => '' . esc_html__('Bulk offload your unoffloaded media files to cloud storage. This process frees up your server storage and boosts your website\'s performance in one go!', 'advanced-media-offloader') . '
'; echo '' . esc_html(number_format_i18n($total_count)) . ' '; echo '' . esc_html__('Media Attachments', 'advanced-media-offloader') . ''; echo '
'; echo '' . esc_html__('Total number of media files stored on your server.', 'advanced-media-offloader') . '
'; } public function offloaded_media_field() { $offloaded_count = advmo_get_offloaded_media_items_count(); echo ''; echo '' . esc_html(number_format_i18n($offloaded_count)) . ' '; echo '' . esc_html__('Media Attachments', 'advanced-media-offloader') . ''; echo '
'; echo '' . esc_html__('Number of media files successfully moved to cloud storage.', 'advanced-media-offloader') . '
'; } public function non_offloaded_media_field() { $non_offloaded_count = advmo_get_unoffloaded_media_items_count(); echo ''; echo '' . esc_html(number_format_i18n($non_offloaded_count)) . ' '; if ($non_offloaded_count === 0) { echo '' . esc_html__('Media Attachments', 'advanced-media-offloader') . ''; } elseif ($non_offloaded_count === 1) { echo '' . esc_html__('Media Attachment', 'advanced-media-offloader') . ''; } else { echo '' . esc_html__('Media Attachments', 'advanced-media-offloader') . ''; } echo '
'; if ($non_offloaded_count === 0) { echo '' . esc_html__('Great job! All your media files are offloaded to cloud storage.', 'advanced-media-offloader') . '
'; } elseif ($non_offloaded_count === 1) { echo '' . esc_html__('There is 1 media file still stored on your local server.', 'advanced-media-offloader') . '
'; echo '' . esc_html__('This file can be offloaded to free up local storage space.', 'advanced-media-offloader') . '
'; } else { echo '' . esc_html__('These files can be offloaded to free up local storage space.', 'advanced-media-offloader') . '
'; } } public function offload_errors_field() { $attachments_with_errors = $this->get_attachments_with_errors(); $count = count($attachments_with_errors); $nonce = wp_create_nonce('advmo_download_errors_csv'); $download_url = admin_url('admin-ajax.php?action=advmo_download_errors_csv&nonce=' . $nonce); echo '' . esc_html__('Number of attachments with errors:', 'advanced-media-offloader') . ' ' . esc_html($count) . '
'; if ($count > 0) { echo '' . esc_html__('Download Errors CSV', 'advanced-media-offloader') . '
'; } } private function get_attachments_with_errors() { return advmo()->app()->get(\AdvancedMediaOffloader\Media\Contracts\AttachmentRepository::class)->allErrorIds(); } public function handle_download_errors_csv() { try { if (!current_user_can('manage_options')) { throw new Exception('You do not have sufficient permissions to access this page.'); } if (!check_admin_referer('advmo_download_errors_csv', 'nonce')) { throw new Exception('Invalid nonce. Please try again.'); } $attachments_with_errors = $this->get_attachments_with_errors(); if (empty($attachments_with_errors)) { throw new Exception('No attachments with errors found.'); } header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="advmo_errors.csv"'); $output = fopen('php://output', 'w'); if ($output === false) { throw new Exception('Unable to create output stream.'); } fputcsv($output, array('Attachment ID', 'Attachment Title', 'Errors')); foreach ($attachments_with_errors as $attachment_id) { $attachment = get_post($attachment_id); if (!$attachment) { continue; // Skip if attachment doesn't exist } $errors = get_post_meta($attachment_id, 'advmo_error_log', true); $errors_string = is_array($errors) ? implode("; \n", $errors) : $errors; fputcsv($output, array( $attachment_id, $attachment->post_title, $errors_string )); } fclose($output); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- This closes the php://output response stream, not a filesystem file. exit; } catch (Exception $e) { status_header(500); wp_die('Error generating CSV: ' . esc_html($e->getMessage()), 'Error', array('response' => 500)); } } public function bulk_offload_media_field() { // WP CLI Notice echo '💡 ' . esc_html__('Use WP CLI for Large Operations', 'advanced-media-offloader') . '
'; echo '' . esc_html__('For sites with hundreds or thousands of media files, our WP CLI command offers superior performance and control:', 'advanced-media-offloader') . '
'; echo 'wp advmo offload
'; echo ' ' . esc_html__('Learn more →', 'advanced-media-offloader') . ''; echo '
'; echo '' . esc_html__('Note:', 'advanced-media-offloader') . ' '; echo esc_html__('This web-based bulk offload supports up to 50 media attachments. For larger operations with hundreds or thousands of files, we recommend using WP CLI commands which provide better performance and reliability for large-scale operations.', 'advanced-media-offloader') . '
' . esc_html(sprintf( /* translators: %d: number of media files stored locally. */ _n( 'You have %d file still stored on your server.', 'You have %d files still stored on your server.', $count, 'advanced-media-offloader' ), $count )) . '
'; echo '' . esc_html__('Offload them to cloud storage now to free up space and enhance your website\'s performance.', 'advanced-media-offloader') . '
'; } echo ''; $display_style = $has_run ? 'block' : 'none'; $progress_width = $has_run ? $progress : 0; $progress_text = $has_run ? round($progress) . '%' : '0%'; if ($is_offloading && $bulk_offload_data['total'] == 0) { $progress_text = __('Preparing...', 'advanced-media-offloader'); } $progress_status = $has_run ? $bulk_offload_data['status'] : 'idle'; // When not actively offloading, use the current unoffloaded count as an estimate // so the UI can show "0 of N" immediately on start (before the first AJAX response). $processed = $has_run ? ($bulk_offload_data['processed'] ?? 0) : 0; $total = $has_run ? ($bulk_offload_data['total'] ?? 0) : $count; echo '' . wp_kses( sprintf( /* translators: 1: processed media count, 2: total media count. */ __('Offloading media files to cloud storage (%1$s of %2$s)', 'advanced-media-offloader'), '' . esc_html($processed) . '', '' . esc_html($total) . '' ), ['span' => ['id' => true]] ) . '
'; echo ' '; printf('%s
', esc_html($progress_text)); echo ''; echo '' . esc_html__('All media files are currently stored in the cloud.', 'advanced-media-offloader') . '
'; } } }