<?php

/**
 * Plugin Name: Upvimeo — Direct Uploads for Vimeo
 * Plugin URI:        https://upvimeo.com
 * Description:       Upload videos directly to Vimeo from WordPress, bypassing server limits. Features privacy controls, folder management, and Pro integrations for LearnDash, TutorLMS, Gravity & WPForms.
 * Version:           1.7.6
 * Author:            Upvimeo
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       upvimeo-direct-uploads
 * Domain Path:       /languages
 * Requires at least: 5.8
 * Requires PHP: 7.4
 * Tested up to: 7.1
 */
// If this file is called directly, abort.
if ( !defined( 'WPINC' ) ) {
    die;
}
if ( !function_exists( 'is_plugin_active' ) ) {
    include_once ABSPATH . 'wp-admin/includes/plugin.php';
}
define( 'UPVIMEO_VERSION', '1.7.6' );
define( 'UPVIMEO_DB_PROVERSION', 100 );
define( 'UPVIMEO_FILE', __FILE__ );
define( 'UPVIMEO_PATH', plugin_dir_path( __FILE__ ) );
define( 'UPVIMEO_URL', plugin_dir_url( __FILE__ ) );
define( 'UPVIMEO_BASENAME', plugin_basename( __FILE__ ) );
define( 'UPVIMEO_MIN_PHP_VERSION', '7.4.0' );
define( 'UPVIMEO_PLUGIN_ID', 561 );
define( 'UPVIMEO_INTEGRATIONS_PATH', trailingslashit( UPVIMEO_PATH ) . 'integrations' . DIRECTORY_SEPARATOR );
define( 'UPVIMEO_INTEGRATIONS_URL', UPVIMEO_URL . 'integrations/' );
if ( !function_exists( 'upvimeo_fs' ) ) {
    // Create a helper function for easy SDK access.
    function upvimeo_fs() {
        global $upvimeo_fs;
        if ( !isset( $upvimeo_fs ) ) {
            // Include Freemius SDK.
            require_once UPVIMEO_PATH . 'vendor/freemius/wordpress-sdk/start.php';
            $upvimeo_fs = fs_dynamic_init( array(
                'id'                   => '22498',
                'slug'                 => 'upvimeo',
                'type'                 => 'plugin',
                'public_key'           => 'pk_8ad8c6d9f11f0838245d9505a7810',
                'is_premium'           => false,
                'is_premium_only'      => false,
                'has_addons'           => false,
                'has_paid_plans'       => true,
                'is_wp_org_compliant'  => true,
                'after_activation_url' => admin_url( 'admin.php?page=upvimeo-library' ),
                'menu'                 => array(
                    'slug'    => 'upvimeo-library',
                    'support' => false,
                    'contact' => false,
                    'pricing' => true,
                    'account' => true,
                ),
                'is_live'              => true,
                'version'              => ( defined( 'UPVIMEO_VERSION' ) ? UPVIMEO_VERSION : '1.6.4' ),
                'trial'                => array(
                    'days'               => 14,
                    'is_require_payment' => false,
                ),
                'is_org_compliant'     => true,
            ) );
        }
        return $upvimeo_fs;
    }

    // Init Freemius.
    upvimeo_fs();
    // Signal that SDK was initiated.
    do_action( 'upvimeo_fs_loaded' );
    // Not like register_uninstall_hook(), you do NOT have to use a static function.
    upvimeo_fs()->add_action( 'after_uninstall', 'upvimeo_fs_uninstall_cleanup' );
}
if ( function_exists( 'upvimeo_fs' ) && upvimeo_fs()->is__premium_only() ) {
    define( 'UPVIMEO_IS_PRO', file_exists( UPVIMEO_PATH . 'includes/pro/class-upvimeo-pro.php' ) );
}
if ( !function_exists( 'upvimeo_fs_uninstall_cleanup' ) ) {
    function upvimeo_fs_uninstall_cleanup() {
        global $wpdb;
        // Delete all plugin options.
        $option_keys = array(
            'upvimeo_client_id',
            'upvimeo_client_secret',
            'upvimeo_access_token',
            'upvimeo_embed_domains',
            'upvimeo_view_privacy',
            'upvimeo_mce_enable_account_search',
            'upvimeo_gtb_enable_account_search',
            'upvimeo_mce_enable_local_search',
            'upvimeo_local_current_user_only',
            'upvimeo_author_uploads_only',
            'upvimeo_enable_single_pages',
            'upvimeo_exp_thumbs_support',
            'upvimeo_upload_folder',
            'upvimeo_embed_preset',
            'upvimeo_enable_privacy_option_gt',
            'upvimeo_enable_privacy_option_mce',
            'upvimeo_enable_privacy_option_media',
            'upvimeo_enable_embed_presets_management',
            'upvimeo_enable_embed_privacy_management',
            'upvimeo_enable_folders_management',
            'upvimeo_instructions_dismissed',
            'upvimeo_settings',
            'upvimeo_tutor_force_vimeo_only'
        );
        $option_keys[] = 'upvimeo_view_privacy_fe';
        $option_keys[] = 'upvimeo_upload_folder_fe';
        $option_keys[] = 'upvimeo_embed_preset_fe';
        $option_keys[] = 'upvimeo_save_frontend_uploads';
        $option_keys[] = 'upvimeo_use_pull_frontend';
        foreach ( $option_keys as $key ) {
            delete_option( $key );
        }
        // Delete all local video posts and their meta.
        $post_ids = get_posts( array(
            'post_type'      => 'upvimeo-upload',
            'posts_per_page' => -1,
            'post_status'    => 'any',
            'fields'         => 'ids',
        ) );
        foreach ( $post_ids as $post_id ) {
            wp_delete_post( $post_id, true );
        }
        // Remove any leftover transients.
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", '_transient_upvimeo_%', '_transient_timeout_upvimeo_%' ) );
    }

}
/**
 * Run on plugin acitvation
 * @return void
 */
function upvimeo_activate() {
    // Dependency check for activation (safe to use __() here as activation happens late)
    if ( !function_exists( 'curl_version' ) ) {
        deactivate_plugins( plugin_basename( __FILE__ ) );
        wp_die( esc_html__( 'The cURL PHP extension is required in order to run this plugin. Please contact your webhost to enable the cURL PHP extension.', 'upvimeo-direct-uploads' ) );
    }
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-upvimeo-activator.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-upvimeo-singleton.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-upvimeo-settings-base.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-upvimeo-settings-helper.php';
    UpVimeo_Activator::activate();
}

/**
 * Run on plugin deactivation
 * @return void
 */
function upvimeo_deactivate() {
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-upvimeo-deactivator.php';
    UpVimeo_Deactivator::deactivate();
}

register_activation_hook( __FILE__, 'upvimeo_activate' );
register_deactivation_hook( __FILE__, 'upvimeo_deactivate' );
/**
 * Bootstrap the plugin
 */
function upvimeo_run() {
    // Core Includes
    require plugin_dir_path( __FILE__ ) . 'includes/class-upvimeo.php';
    // Conditional Pro Loading
    if ( file_exists( plugin_dir_path( __FILE__ ) . 'includes/pro/class-upvimeo-pro.php' ) ) {
        if ( function_exists( 'upvimeo_fs' ) && upvimeo_fs()->is__premium_only() ) {
            require_once plugin_dir_path( __FILE__ ) . 'includes/pro/class-upvimeo-pro.php';
        }
    }
    $plugin = new UpVimeo();
    $plugin->run();
}

function upvimeo_curl_missing_notice() {
    $class = 'notice notice-error is-dismissible';
    $plugin = 'Upvimeo';
    $message = __( 'The cURL PHP extension is required in order to run this plugin. Please contact your webhost to enable the cURL PHP extension.', 'upvimeo-direct-uploads' );
    printf(
        '<div class="%1$s"><p><strong>%2$s</strong>: %3$s</p></div>',
        esc_attr( $class ),
        esc_html( $plugin ),
        esc_html( $message )
    );
}

// Execute on plugins_loaded to avoid JIT textdomain notices and ensure dependencies
add_action( 'plugins_loaded', 'upvimeo_run', 10 );