'Version')); $this->version = $plugin_data['Version']; } /** * Sets up the Adoovy Media Offload * * @since 1.0.0 * * @return void */ public function initialize() { // Define constants. $this->define('SMOF', true); $this->define('SMOF_PATH', plugin_dir_path(__FILE__)); $this->define('SMOF_URL', plugin_dir_url(__FILE__)); $this->define('SMOF_BASENAME', plugin_basename(__FILE__)); $this->define('SMOF_VERSION', $this->version); $this->define('SMOF_API_VERSION', 1); // Register activation hook. register_activation_hook(__FILE__, array($this, 'plugin_activated')); // Register deactivation hook. register_deactivation_hook(__FILE__, array($this, 'plugin_deactivated')); // One-time import of data created by the original "Advanced Media Offloader" plugin. $this->migrate_legacy_data(); // Set up container $this->setup_container(); // Include files and setup WordPress hooks $this->include_files(); $this->setup_hooks(); } /** * Import settings, per-attachment offload data and wp-config constants that were * created by the original "Advanced Media Offloader" (advmo_*) so already-offloaded * media keeps resolving to the bucket. Non-destructive: the old data is left in place. * To force a re-run, delete the 'smof_legacy_migrated' option. * * @return void */ private function migrate_legacy_data() { // 1) wp-config.php constants: ADVMO__ => SMOF__ $skip = array('ADVMO', 'ADVMO_PATH', 'ADVMO_URL', 'ADVMO_BASENAME', 'ADVMO_VERSION', 'ADVMO_API_VERSION'); $user_constants = get_defined_constants(true); if (!empty($user_constants['user']) && is_array($user_constants['user'])) { foreach ($user_constants['user'] as $name => $value) { if (strpos($name, 'ADVMO_') === 0 && !in_array($name, $skip, true)) { $this->define('SMOF_' . substr($name, 6), $value); } } } if (get_option('smof_legacy_migrated', null) !== null) { return; } // 2) Options foreach (array('settings', 'credentials', 'first_activated_version', 'last_connection_check') as $suffix) { $old = get_option('advmo_' . $suffix, null); if ($old !== null && get_option('smof_' . $suffix, null) === null) { add_option('smof_' . $suffix, $old); } } // 3) Attachment meta (advmo_offloaded, advmo_path, advmo_bucket, advmo_provider, ...) global $wpdb; $like_a = $wpdb->esc_like('advmo_') . '%'; $like_b = $wpdb->esc_like('_advmo_') . '%'; $wpdb->query($wpdb->prepare( "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) SELECT o.post_id, REPLACE(o.meta_key, 'advmo', 'smof'), o.meta_value FROM {$wpdb->postmeta} o WHERE (o.meta_key LIKE %s OR o.meta_key LIKE %s) AND o.meta_key <> 'advmo_tmp_file' AND NOT EXISTS ( SELECT 1 FROM {$wpdb->postmeta} n WHERE n.post_id = o.post_id AND n.meta_key = REPLACE(o.meta_key, 'advmo', 'smof') )", $like_a, $like_b )); update_option('smof_legacy_migrated', time(), true); } /** * Ask the administrator to fill in the storage details when they are missing * (neither saved in the settings page nor defined in wp-config.php). * * @return void */ public function maybe_show_setup_notice() { if (!current_user_can('manage_options') || smof_is_settings_page()) { return; } // Keep the notice limited in scope: only on the Plugins and Media Library screens. $screen = function_exists('get_current_screen') ? get_current_screen() : null; if (!$screen || !in_array($screen->base, array('plugins', 'upload'), true)) { return; } $missing = smof_get_missing_credentials(); if (empty($missing)) { return; } $settings_url = esc_url(admin_url('admin.php?page=smof')); if ($missing[0] === 'provider') { $message = sprintf( /* translators: %s: link to the settings page */ __('Adoovy Media Offload is not set up yet. Please %s and enter your cloud storage details (or define them in wp-config.php).', 'adoovy-media-offload'), '' . esc_html__('open the settings page', 'adoovy-media-offload') . '' ); } else { $message = sprintf( /* translators: 1: list of missing fields, 2: link to the settings page */ __('Adoovy Media Offload is missing required storage details: %1$s. Please %2$s to complete them.', 'adoovy-media-offload'), '' . esc_html(implode(', ', $missing)) . '', '' . esc_html__('open the settings page', 'adoovy-media-offload') . '' ); } printf('

%s

', wp_kses($message, array('a' => array('href' => array()), 'strong' => array()))); } private function setup_container() { // Include autoloader if (file_exists(SMOF_PATH . 'vendor/scoper-autoload.php')) { require_once SMOF_PATH . 'vendor/scoper-autoload.php'; } elseif (file_exists(SMOF_PATH . 'vendor/autoload.php')) { require_once SMOF_PATH . 'vendor/autoload.php'; } $this->container = new \Smart_Media_Offload\Core\Container(); $this->container->register('cloud_provider_factory', function ($c) { return new \Smart_Media_Offload\Factories\CloudProviderFactory(); }); // Register core services $this->container->register('cloud_provider', function ($c) { $cloud_provider_key = smof_get_cloud_provider_key(); if (empty($cloud_provider_key)) { return null; } if ($c->has('cloud_provider_factory')) { $cloud_provider_factory = $c->get('cloud_provider_factory'); return $cloud_provider_factory::create($cloud_provider_key); } return null; }); $this->container->register('offloader', function ($c) { if ($c->has('cloud_provider') && $c->get('cloud_provider') !== null) { return \Smart_Media_Offload\Offloader::get_instance($c->get('cloud_provider')); } return null; }); $this->container->register('settings_page', function ($c) { return \Smart_Media_Offload\Admin\GeneralSettings::create($c->get('cloud_provider_factory')); }); $this->container->register('media_overview_page', function ($c) { return \Smart_Media_Offload\Admin\MediaOverview::getInstance(); }); $this->container->register('bulk_offload_handler', function ($c) { return \Smart_Media_Offload\BulkOffloadHandler::get_instance(); }); } private function setup_hooks() { # check if AWS SDK is loaded if (!class_exists(SMOFVendor\Aws\S3\S3Client::class)) { // Show admin notice if AWS SDK is missing. add_action('admin_notices', function () { $this->notice(__('AWS SDK for PHP is required to use Adoovy Media Offload. Please install it via Composer.', 'adoovy-media-offload'), 'error'); }); return; } # Include admin if needed if (is_admin()) { add_action('admin_notices', array($this, 'maybe_show_setup_notice')); $this->container->get('settings_page'); // Initialize settings $this->container->get('media_overview_page'); // Initialize media overview new \Smart_Media_Offload\Admin\Observers\CurrentScreen(); # Add link to the settings page in the plugins list add_filter('plugin_action_links_' . plugin_basename(__FILE__), [$this, 'plugin_action_links']); } # Initialize offloader if cloud provider is configured if ($this->container->has('offloader') && $this->container->get('offloader') !== null) { $this->container->get('offloader')->initializeHooks(); } # Initialize bulk offload handler $this->container->get('bulk_offload_handler'); # Register WP CLI commands $this->register_cli_commands(); } private function include_files() { # include Utility Functions include_once SMOF_PATH . 'utility-functions.php'; } /** * Register WP CLI commands if WP CLI is available. * * @return void */ private function register_cli_commands() { if (defined('WP_CLI') && WP_CLI) { WP_CLI::add_command('smof offload', 'Smart_Media_Offload\\CLI\\OffloadCommand'); } } public function plugin_action_links($links) { $settings_page_link = '' . __('Settings', 'adoovy-media-offload') . ''; array_unshift($links, $settings_page_link); return $links; } /** * Plugin Activation Hook * * @since 1.0.0 */ public function plugin_activated() { // Set the first activated version of Adoovy Media Offload. if (null === get_option('smof_first_activated_version', null)) { update_option('smof_first_activated_version', SMOF_VERSION, true); } } /** * Plugin Deactivation Hook * * @since 3.3.4 */ public function plugin_deactivated() { // Clear any scheduled tasks wp_clear_scheduled_hook('smof_bulk_offload_cron'); wp_clear_scheduled_hook('smof_check_stalled_processes'); // Clear bulk offload related options delete_option('smof_bulk_offload_cancelled'); delete_option('smof_bulk_offload_last_update'); delete_option('smof_bulk_offload_data'); delete_option('smof_last_connection_check'); // Clear any custom cron schedules if ($this->container && $this->container->has('bulk_offload_handler')) { try { $bulk_offload_handler = $this->container->get('bulk_offload_handler'); if ($bulk_offload_handler) { remove_filter('cron_schedules', array($bulk_offload_handler, 'add_cron_interval')); } } catch (\Exception $e) { // Silently fail if handler cannot be retrieved error_log('SMOF: Error removing cron schedule filter during deactivation: ' . $e->getMessage()); } } // Note: We don't delete the cloud provider settings to preserve configuration } public function define($name, $value = true) { if (!defined($name)) { define($name, $value); } } public function notice($message, $type = 'info') { $class = 'notice notice-' . $type; printf('

%2$s

', esc_attr($class), esc_attr($message)); } } function smof() { global $smof; // Instantiate only once. if (!isset($smof)) { $smof = new SMOF(); $smof->initialize(); } return $smof; } // Instantiate. smof(); } // class_exists check