<?php
/**
 * Plugin Name: R2 Media Offload (Multi-Cloud)
 * Plugin URI:  https://plugins.rise2.studio/r2-offload/
 * Description: Offload WordPress media to Amazon S3, Cloudflare R2, Backblaze B2, Wasabi, DigitalOcean Spaces, Bunny.net or Google Cloud Storage, rewrite URLs to CDN, and migrate large libraries safely.
 * Version: 1.0.1
 * Author: RISE2 Studio
 * Author URI:  https://rise2.studio
 * Requires at least: 6.0
 * Requires PHP: 8.0
 * License: GPL-2.0-or-later
 * Text Domain: r2-offload
 * Update URI:  https://plugins.rise2.studio/r2-offload/
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

define( 'R2_OFFLOAD_VERSION', '1.0.1' );
define( 'R2_OFFLOAD_FILE', __FILE__ );
define( 'R2_OFFLOAD_DIR', plugin_dir_path( __FILE__ ) );
define( 'R2_OFFLOAD_URL', plugin_dir_url( __FILE__ ) );
define( 'R2_OFFLOAD_BASENAME', plugin_basename( __FILE__ ) );

$autoload = R2_OFFLOAD_DIR . 'vendor/autoload.php';
if ( file_exists( $autoload ) ) {
	require_once $autoload;
}

spl_autoload_register(
	static function ( $class ) {
		if ( strpos( $class, 'R2_Offload\\' ) !== 0 ) {
			return;
		}
		$relative = str_replace( 'R2_Offload\\', '', $class );
		$relative = str_replace( '\\', DIRECTORY_SEPARATOR, $relative );
		$path     = R2_OFFLOAD_DIR . 'includes' . DIRECTORY_SEPARATOR . $relative . '.php';
		if ( file_exists( $path ) ) {
			require_once $path;
		}
	}
);

register_activation_hook( __FILE__, [ 'R2_Offload\\Activator', 'activate' ] );
register_deactivation_hook( __FILE__, [ 'R2_Offload\\Activator', 'deactivate' ] );

add_action(
	'plugins_loaded',
	static function () {
		R2_Offload\Plugin::instance()->boot();

		// Self-hosted updates from plugins.rise2.studio.
		new R2_Offload\Updater(
			array(
				'slug'       => 'r2-offload',
				'version'    => R2_OFFLOAD_VERSION,
				'basename'   => R2_OFFLOAD_BASENAME,
				'update_url' => 'https://plugins.rise2.studio/r2-offload/info.json',
			)
		);
	}
);
