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); $output = "" . __('Number of attachments with errors:', 'advanced-media-offloader') . " {$count}
"; if ($count > 0) { $output .= "" . __('Download Errors CSV', 'advanced-media-offloader') . "
"; } echo $output; } private function get_attachments_with_errors() { global $wpdb; $meta_key = 'advmo_error_log'; $query = $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s", $meta_key ); return $wpdb->get_col($query); } 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); 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() { echo '' . __('Note:', 'advanced-media-offloader') . ' '; echo __('The offloading process handles up to 50 media files per batch. If you have more than 50 files, you’ll need to run the bulk offload multiple times. This process runs in the background—you can close this page after starting.', 'advanced-media-offloader') . '
' . sprintf( _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 '' . __('Offload them to cloud storage now to free up space and enhance your website\'s performance.', 'advanced-media-offloader') . '
'; echo ''; } $display_style = $is_offloading ? 'block' : 'none'; $progress_width = $is_offloading ? $progress : 0; $progress_text = $is_offloading ? round($progress) . '%' : '0%'; if ($is_offloading && $bulk_offload_data['total'] == 0) { $progress_text = __('Preparing...', 'advanced-media-offloader'); } $progress_status = $is_offloading ? 'processing' : 'idle'; $processed = $bulk_offload_data['processed'] ?? 0; $total = $bulk_offload_data['total'] ?? 0; echo '' . sprintf( __('Offloading media files to cloud storage (%1$s of %2$s)', 'advanced-media-offloader'), '' . esc_html($processed) . '', '' . esc_html($total) . '' ) . '
'; echo ' '; printf('%s
', esc_html($progress_text)); if (get_option("advmo_bulk_offload_cancelled") === false) { echo ''; } else { echo "Canceling the bulk offload process…
"; } echo '' . __('All media files are currently stored in the cloud.', 'advanced-media-offloader') . '
'; } } }