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']); } 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_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', [ 'before_section' => '' . esc_attr__('Configure the core options for managing and offloading your media files to cloud storage.', 'advanced-media-offloader') . '
' . '' . '
'; echo '' . esc_html__('Add a common prefix to organize offloaded media files from this site in your cloud storage bucket.', 'advanced-media-offloader') . '
'; 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 '' . esc_html__('When enabled, deleting a media file in WordPress will also remove it from your cloud storage.', 'advanced-media-offloader') . '
'; 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 '' . 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 '' . 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 '' . 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 general_settings_page_view() { advmo_get_view('admin/general_settings'); } public function enqueue_scripts() { if (!advmo_is_settings_page()) { return; } if (advmo_is_settings_page('general')) { 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'), 'i18n' => [ 'test_connection' => __('Test Connection', 'advanced-media-offloader'), 'recheck' => __('Re-Check', 'advanced-media-offloader'), 'connected' => __('Connected.', 'advanced-media-offloader'), 'last_check' => __('Last check:', 'advanced-media-offloader') ] ]); } if (advmo_is_settings_page('media-overview')) { wp_enqueue_script('advmo_bulkoffload', ADVMO_URL . 'assets/js/advmo_bulkoffload.js', [], ADVMO_VERSION, true); wp_localize_script('advmo_bulkoffload', 'advmo_ajax_object', [ 'ajax_url' => admin_url('admin-ajax.php'), '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']) : ''; $current_time = current_time('d/m/Y - h:i A'); if (!wp_verify_nonce($security_nonce, 'advmo_test_connection')) { wp_send_json_error([ 'message' => __('Invalid nonce!', 'advanced-media-offloader'), 'last_check' => $current_time ]); } // 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) { update_option('advmo_last_connection_check', $current_time); wp_send_json_success([ 'message' => __('Connection successful!', 'advanced-media-offloader'), 'last_check' => $current_time ]); } else { update_option('advmo_last_connection_check', $current_time); wp_send_json_error([ 'message' => __('Connection failed!', 'advanced-media-offloader'), 'last_check' => $current_time ], 401); } } catch (\Exception $e) { // Log the error message for debugging (optional) error_log('Advanced Media Offloader Connection Error: ' . $e->getMessage()); update_option('advmo_last_connection_check', $current_time); // 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()), 'last_check' => $current_time ], 500); } } else { wp_send_json_error([ 'message' => __('Invalid Cloud Provider!', 'advanced-media-offloader'), 'last_check' => $current_time ]); } } // Prevent cloning of the instance private function __clone() {} // Prevent unserializing of the instance public function __wakeup() {} }