cloud_providers = CloudProviderFactory::getAvailableProviders(); add_action('admin_menu', [$this, 'add_settings_page']); add_action('admin_init', [$this, 'initialize']); add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); add_action('wp_ajax_advmo_test_connection', [$this, 'check_connection_ajax']); BulkOffloadHandler::get_instance(); } public function initialize() { register_setting('advmo', 'advmo_settings', [$this, 'sanitize']); $this->add_settings_section(); $this->add_provider_field(); $this->add_credentials_field(); $this->add_retention_policy_field(); $this->add_path_prefix_field(); $this->add_object_versioning_field(); $this->add_non_offloaded_media_field(); $this->add_mirror_delete_field(); } private function add_settings_section() { add_settings_section( 'cloud_provider', __('Cloud Provider', 'advanced-media-offloader'), function () { echo '

' . esc_attr__('Select a cloud storage provider and provide the necessary credentials.', 'advanced-media-offloader') . '

'; }, 'advmo' ); add_settings_section( 'general_settings', __('General Settings', 'advanced-media-offloader'), function () { echo '

' . esc_attr__('Configure the core options for managing and offloading your media files to cloud storage.', 'advanced-media-offloader') . '

'; }, 'advmo' ); 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' ); } private function add_provider_field() { add_settings_field( 'cloud_provider', __('Cloud Provider', 'advanced-media-offloader'), [$this, 'cloud_provider_field'], 'advmo', 'cloud_provider' ); } private function add_credentials_field() { add_settings_field( 'advmo_cloud_provider_credentials', __('Credentials', 'advanced-media-offloader'), [$this, 'cloud_provider_credentials_field'], 'advmo', 'cloud_provider' ); } private function add_retention_policy_field() { add_settings_field( 'retention_policy', __('Retention Policy', 'advanced-media-offloader'), [$this, 'retention_policy_field'], 'advmo', 'general_settings' ); } private function add_mirror_delete_field() { add_settings_field( 'mirror_delete', __('Mirror Delete', 'advanced-media-offloader'), [$this, 'mirror_delete_field'], 'advmo', 'general_settings' ); } private function add_object_versioning_field() { add_settings_field('object_versioning', __('File Versioning', 'advanced-media-offloader'), [$this, 'object_versioning_field'], 'advmo', 'general_settings'); } private function add_path_prefix_field() { add_settings_field('path_prefix', __('Custom Path Prefix', 'advanced-media-offloader'), [$this, 'path_prefix_field'], 'advmo', 'general_settings'); } private function add_non_offloaded_media_field() { add_settings_field( 'non_offloaded_media', __('Local Media Overview', 'advanced-media-offloader'), [$this, 'non_offloaded_media_field'], 'advmo', 'media_overview' ); } public function path_prefix_field() { $options = get_option('advmo_settings'); $path_prefix = isset($options['path_prefix']) ? $options['path_prefix'] : "wp-content/uploads/"; $path_prefix_Active = isset($options['path_prefix_active']) ? $options['path_prefix_active'] : 0; echo '
'; echo ''; echo ''; echo '

' . '' . '

'; echo '

' . esc_html__('Add a common prefix to organize offloaded media files from this site in your cloud storage bucket.', 'advanced-media-offloader') . '

'; echo '
'; } public function object_versioning_field() { $options = get_option('advmo_settings'); $object_versioning = isset($options['object_versioning']) ? $options['object_versioning'] : 0; echo '
'; echo ''; echo ''; echo '

' . esc_html__('Automatically add unique timestamps to your media file paths to ensure the latest versions are always delivered. This prevents outdated content from being served due to CDN caching, even when you replace files with the same name. Eliminate manual cache invalidation and guarantee your visitors always see the most up-to-date media.', 'advanced-media-offloader') . '

'; echo '
'; } public function non_offloaded_media_field() { $bulk_offload_data = advmo_get_bulk_offload_data(); $count = advmo_get_unoffloaded_media_items_count(); $is_offloading = $bulk_offload_data['status'] === 'processing'; $progress = ($is_offloading && $bulk_offload_data['total'] > 0) ? ($bulk_offload_data['processed'] / $bulk_offload_data['total']) * 100 : 0; if ($count > 0 || $is_offloading) { if (!$is_offloading) { echo '

' . 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 ''; // Add information about batch size limitation 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') . '

'; } $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 '
'; echo '

' . sprintf( __('Offloading media files to cloud storage (%1$s of %2$s)', 'advanced-media-offloader'), '' . esc_html($processed) . '', '' . esc_html($total) . '' ) . '

'; echo '
'; printf('
', esc_html($progress_width)); echo '
'; printf('

%s

', esc_html($progress_text)); echo ''; echo '
'; } else { echo '

' . __('All media files are currently stored in the cloud.', 'advanced-media-offloader') . '

'; } } public function mirror_delete_field() { $options = get_option('advmo_settings'); $mirror_delete = isset($options['mirror_delete']) ? intval($options['mirror_delete']) : 0; echo '
'; echo ''; echo ''; echo '

' . esc_html__('When enabled, deleting a media file in WordPress will also remove it from your cloud storage.', 'advanced-media-offloader') . '

'; echo '
'; } public function retention_policy_field() { $options = get_option('advmo_settings'); $retention_policy = isset($options['retention_policy']) ? intval($options['retention_policy']) : 0; echo '
'; echo '
'; echo ''; echo ''; echo '

' . esc_html__('Keep all files on your local server after offloading to the cloud. This option provides redundancy but uses more local storage.', 'advanced-media-offloader') . '

'; echo '
'; echo '
'; echo ''; echo ''; echo '

' . esc_html__('Remove local copies after cloud offloading, but keep the original file as a backup. Balances storage efficiency with data safety.', 'advanced-media-offloader') . '

'; echo '
'; echo '
'; echo ''; echo ''; echo '

' . esc_html__('Remove all local files, including originals, after successful cloud offloading. Maximizes local storage savings but relies entirely on cloud storage.', 'advanced-media-offloader') . '

'; echo '
'; echo '
'; } public function sanitize($options) { if (!current_user_can('manage_options')) { return; } $options['cloud_provider'] = sanitize_text_field($options['cloud_provider'] ?? ''); if (!array_key_exists($options['cloud_provider'], $this->cloud_providers)) { add_settings_error('advmo_messages', 'advmo_message', __('Invalid Cloud Provider!', 'advanced-media-offloader'), 'error'); return $options; } $options['retention_policy'] = isset($options['retention_policy']) && in_array($options['retention_policy'], [0, 1, 2]) ? $options['retention_policy'] : 0; $options['object_versioning'] = isset($options['object_versioning']) ? 1 : 0; $options['path_prefix'] = isset($options['path_prefix']) ? advmo_sanitize_path($options['path_prefix']) : ''; add_settings_error('advmo_messages', 'advmo_message', __('Settings Saved', 'advanced-media-offloader'), 'updated'); return $options; } public function add_settings_page() { add_menu_page( __('Advanced Media Offloader', 'advanced-media-offloader'), __('Media Offloader', 'advanced-media-offloader'), 'manage_options', 'advmo', [$this, 'settings_page_view'], 'dashicons-cloud-upload', 100 ); } public function cloud_provider_field($args) { $options = get_option('advmo_settings'); echo ''; } /** * Get the cloud provider key from the plugin settings. * * @return string The cloud provider key or an empty string if not set. */ private function get_cloud_provider_key(): string { return advmo_get_cloud_provider_key(); } public function cloud_provider_credentials_field() { $cloud_provider_key = $this->get_cloud_provider_key(); if (!empty($cloud_provider_key)) { try { // Use the CloudProviderFactory to create an instance of the selected cloud provider /** @var CloudProviderInterface $cloud_provider_instance */ $cloud_provider_instance = CloudProviderFactory::create($cloud_provider_key); // Render the credentials fields specific to the selected cloud provider $cloud_provider_instance->credentialsField(); } catch (\Exception $e) { // Display an error message if the cloud provider is unsupported or instantiation fails echo '

' . esc_html__('Selected cloud provider is not supported or failed to initialize.', 'advanced-media-offloader') . '

'; } } else { echo '

' . esc_html__('Please select a valid cloud provider to configure credentials.', 'advanced-media-offloader') . '

'; } } public function settings_page_view() { include ADVMO_PATH . 'templates/admin/settings.php'; } public function enqueue_scripts() { if (!isset($_GET['page']) || $_GET['page'] !== 'advmo') { return; } wp_enqueue_script('advmo_settings', ADVMO_URL . 'assets/js/advmo_settings.js', [], ADVMO_VERSION, true); wp_localize_script('advmo_settings', 'advmo_ajax_object', [ 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('advmo_test_connection'), 'bulk_offload_nonce' => wp_create_nonce('advmo_bulk_offload') ]); wp_enqueue_style('advmo_admin', ADVMO_URL . 'assets/css/admin.css', [], ADVMO_VERSION); } public function check_connection_ajax() { // Verify the nonce for security $security_nonce = isset($_POST['security_nonce']) ? sanitize_text_field($_POST['security_nonce']) : ''; if (!wp_verify_nonce($security_nonce, 'advmo_test_connection')) { wp_send_json_error(['message' => __('Invalid nonce!', 'advanced-media-offloader')]); } // Retrieve plugin settings $cloud_provider_key = $this->get_cloud_provider_key(); if (!empty($cloud_provider_key)) { try { // Use the CloudProviderFactory to create an instance of the selected cloud provider /** @var CloudProviderInterface $cloud_provider_instance */ $cloud_provider_instance = CloudProviderFactory::create($cloud_provider_key); // Check the connection using the cloud provider instance $result = $cloud_provider_instance->checkConnection(); if ($result) { wp_send_json_success(['message' => __('Connection successful!', 'advanced-media-offloader')]); } else { wp_send_json_error(['message' => __('Connection failed!', 'advanced-media-offloader')], 401); } } catch (\Exception $e) { // Log the error message for debugging (optional) error_log('Advanced Media Offloader Connection Error: ' . $e->getMessage()); // Display an error message to the user wp_send_json_error([ 'message' => __('Failed to establish a connection with the selected cloud provider.', 'advanced-media-offloader') . ' ' . esc_html($e->getMessage()) ], 500); } } else { wp_send_json_error(['message' => __('Invalid Cloud Provider!', 'advanced-media-offloader')]); } } }