<?php

/**
 * @wordpress-plugin
 * Plugin Name:       Upcasted S3 Offload - AWS S3, Digital Ocean Spaces, Backblaze, Minio and more
 * Plugin URI:        https://upcasted.com/upcasted-s3-offload
 * Description:       Seamless sync between your WordPress Media Library and AWS S3 now in a top notch WordPress plugin with easy licensing and no limitations.
 * Version:           3.1.0
 * Author:            Upcasted
 * Author URI:        https://upcasted.com
 * License:           GPLv3
 * License URI:       https://www.gnu.org/licenses/gpl-3.0.html
 * Text Domain:       upcasted-s3-offload
 * Domain Path:       /languages
 * 
 */
// If this file is called directly, abort.
if ( !defined( 'WPINC' ) ) {
    die;
}
if ( function_exists( 'uso_fs' ) ) {
    uso_fs()->set_basename( false, __FILE__ );
} else {
    // DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE `function_exists` CALL ABOVE TO PROPERLY WORK.
    if ( !function_exists( 'uso_fs' ) ) {
        // Create a helper function for easy SDK access.
        function uso_fs() {
            global $uso_fs;
            if ( !isset( $uso_fs ) ) {
                // Include Freemius SDK.
                // require_once __DIR__ . '/vendor/autoload.php';
                require_once dirname( __FILE__ ) . '/vendor/freemius/wordpress-sdk/start.php';
                $uso_fs = fs_dynamic_init( array(
                    'id'             => '6128',
                    'slug'           => 'upcasted-s3-offload',
                    'premium_slug'   => 'upcasted-s3-offload-pro',
                    'type'           => 'plugin',
                    'public_key'     => 'pk_1fb61de9679c1069993d1ba9f2166',
                    'is_premium'     => false,
                    'premium_suffix' => 'Pro',
                    'has_addons'     => false,
                    'has_paid_plans' => true,
                    'menu'           => array(
                        'slug'    => 'upcasted-s3-offload-panel',
                        'support' => false,
                        'parent'  => array(
                            'slug' => 'upload.php',
                        ),
                    ),
                    'is_live'        => true,
                ) );
            }
            return $uso_fs;
        }

        // Init Freemius.
        uso_fs();
        // Signal that SDK was initiated.
        do_action( 'uso_fs_loaded' );
    }
    if ( !class_exists( '\\Aws\\S3\\S3Client' ) ) {
        // Require AWS Autoloader file.
        require_once dirname( __FILE__ ) . '/vendor/autoload.php';
    }
    define( 'UPCASTED_S3_OFFLOAD_VERSION', '3.1.0' );
    if ( !defined( 'UPCASTED_S3_OFFLOAD_SETTINGS' ) ) {
        define( 'UPCASTED_S3_OFFLOAD_SETTINGS', 'upcasted_s3_offload_settings' );
    }
    if ( !defined( 'UPCASTED_S3_OFFLOAD_ACCESS_KEY_ID' ) ) {
        define( 'UPCASTED_S3_OFFLOAD_ACCESS_KEY_ID', 'upcasted_s3_offload_access_key_id' );
    }
    if ( !defined( 'UPCASTED_S3_OFFLOAD_SECRET_ACCESS_KEY' ) ) {
        define( 'UPCASTED_S3_OFFLOAD_SECRET_ACCESS_KEY', 'upcasted_s3_offload_secret_access_key' );
    }
    if ( !defined( 'UPCASTED_OFFLOAD_REGION' ) ) {
        define( 'UPCASTED_OFFLOAD_REGION', 'upcasted_offload_region' );
    }
    if ( !defined( 'UPCASTED_CUSTOM_ENDPOINT' ) ) {
        define( 'UPCASTED_CUSTOM_ENDPOINT', 'upcasted_custom_endpoint' );
    }
    if ( !defined( 'UPCASTED_CUSTOM_BATCH_SIZE' ) ) {
        define( 'UPCASTED_CUSTOM_BATCH_SIZE', 'upcasted_custom_batch_size' );
    }
    if ( !defined( 'UPCASTED_KEEP_LOCAL_FILES' ) ) {
        define( 'UPCASTED_KEEP_LOCAL_FILES', 'upcasted_keep_local_files' );
    }
    if ( !defined( 'UPCASTED_KEEP_S3_FILES' ) ) {
        define( 'UPCASTED_KEEP_S3_FILES', 'upcasted_keep_s3_files' );
    }
    if ( !defined( 'UPCASTED_AUTO_UPLOAD_TO_S3' ) ) {
        define( 'UPCASTED_AUTO_UPLOAD_TO_S3', 'upcasted_auto_upload_to_s3' );
    }
    if ( !defined( 'UPCASTED_S3_OFFLOAD_INCLUDED_FILETYPES' ) ) {
        define( 'UPCASTED_S3_OFFLOAD_INCLUDED_FILETYPES', 'upcasted_s3_offload_included_filetypes' );
    }
    if ( !defined( 'UPCASTED_OFFLOAD_REGION' ) ) {
        define( 'UPCASTED_OFFLOAD_REGION', '' );
    }
    if ( !defined( 'UPCASTED_S3_OFFLOAD_BUCKET' ) ) {
        define( 'UPCASTED_S3_OFFLOAD_BUCKET', 'upcasted_s3_offload_bucket' );
    }
    if ( !defined( 'UPCASTED_S3_OFFLOAD_CUSTOM_DOMAIN' ) ) {
        define( 'UPCASTED_S3_OFFLOAD_CUSTOM_DOMAIN', 'upcasted_custom_domain' );
    }
    if ( !defined( 'UPCASTED_S3_OFFLOAD_PROTOCOL' ) ) {
        define( 'UPCASTED_S3_OFFLOAD_PROTOCOL', 'upcasted_s3_offload_protocol' );
    }
    if ( !defined( 'UPCASTED_CRON_LOCAL_TO_S3' ) ) {
        define( 'UPCASTED_CRON_LOCAL_TO_S3', 'upcasted_cron_local_to_s3' );
    }
    if ( !defined( 'UPCASTED_CRON_S3_TO_LOCAL' ) ) {
        define( 'UPCASTED_CRON_S3_TO_LOCAL', 'upcasted_cron_s3_to_local' );
    }
    if ( !defined( 'UPCASTED_SELECTED_S3_PROVIDER' ) ) {
        define( 'UPCASTED_SELECTED_S3_PROVIDER', 'upcasted_selected_s3_provider' );
    }
    /**
     * This code shows a settings button on the plugins page to find the settings easier.
     */
    function upcasted_s3_offload_settings_link(  $links  ) {
        $settings_link = '<a href="upload.php?page=upcasted-s3-offload-panel">Settings</a>';
        array_unshift( $links, $settings_link );
        return $links;
    }

    $plugin = plugin_basename( __FILE__ );
    add_filter( "plugin_action_links_{$plugin}", 'upcasted_s3_offload_settings_link' );
    /**
     * The core plugin class that is used to define internationalization,
     * admin-specific hooks, and public-facing site hooks.
     */
    require plugin_dir_path( __FILE__ ) . 'includes/class-upcasted-offload.php';
    /**
     * Begins execution of the plugin.
     *
     * Since everything within the plugin is registered via hooks,
     * then kicking off the plugin from this point in the file does
     * not affect the page life cycle.
     *
     * @since    1.0.0
     */
    function run_upcasted_s3_offload() {
        $plugin = new Upcasted_Offload();
        $plugin->run();
    }

    /**
     * Initialize default premium settings after upgrade from free to premium
     * 
     * @since 3.1.0
     */
    function upcasted_initialize_defaults() {
        $settings = get_option( UPCASTED_S3_OFFLOAD_SETTINGS, array() );
        $needs_update = false;
        // Only initialize if settings exist (plugin is configured)
        if ( empty( $settings ) || !isset( $settings[UPCASTED_S3_OFFLOAD_BUCKET] ) ) {
            return;
            // Plugin not configured yet, skip initialization
        }
        // Set MIME types to all common types if not set (Premium only feature)
        if ( uso_fs()->is__premium_only() && !isset( $settings[UPCASTED_S3_OFFLOAD_INCLUDED_FILETYPES] ) ) {
            // Get all WordPress supported MIME types
            $mime_types = wp_get_mime_types();
            $settings[UPCASTED_S3_OFFLOAD_INCLUDED_FILETYPES] = array_values( $mime_types );
            $needs_update = true;
        }
        // Set "Keep files on S3" to NO by default (delete from S3 after migration) - Premium only
        if ( uso_fs()->is__premium_only() && !isset( $settings[UPCASTED_KEEP_S3_FILES] ) ) {
            $settings[UPCASTED_KEEP_S3_FILES] = 'no';
            $needs_update = true;
        }
        // Set "Keep files locally" to NO by default (keep local after upload) - Available for all users
        if ( !isset( $settings[UPCASTED_KEEP_LOCAL_FILES] ) ) {
            $settings[UPCASTED_KEEP_LOCAL_FILES] = 'no';
            $needs_update = true;
        }
        if ( $needs_update ) {
            update_option( UPCASTED_S3_OFFLOAD_SETTINGS, $settings );
        }
    }

    // Initialize defaults for all users when bucket is configured
    add_action( 'admin_init', 'upcasted_initialize_defaults' );
    // Also hook into Freemius after premium activation/upgrade for premium-specific defaults
    uso_fs()->add_action( 'after_premium_version_activation', 'upcasted_initialize_defaults' );
    run_upcasted_s3_offload();
}