<?php
/**
 * Plugin Name: WP Reels Gallery Pro
 * Plugin URI: https://tonsite.ma
 * Description: Gérez et affichez des galeries de vidéos Reels avec recadrage d'images et paramètres avancés
 * Version: 3.5.0
 * Author: Tonsite
 * License: GPL v2 or later
 */

// Sécurité : empêcher l'accès direct
if (!defined('ABSPATH')) {
    exit;
}

// Constantes du plugin
define('WPRG_VERSION', '3.5.0');
define('WPRG_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WPRG_PLUGIN_URL', plugin_dir_url(__FILE__));

// Chargement des fichiers requis
require_once WPRG_PLUGIN_DIR . 'includes/post-type.php';
require_once WPRG_PLUGIN_DIR . 'includes/meta-boxes.php';
require_once WPRG_PLUGIN_DIR . 'includes/shortcode.php';
require_once WPRG_PLUGIN_DIR . 'includes/image-cropper.php';
require_once WPRG_PLUGIN_DIR . 'includes/cropper-ajax.php';
require_once WPRG_PLUGIN_DIR . 'includes/likes-system.php';
require_once WPRG_PLUGIN_DIR . 'includes/views-system.php';

// Activation du plugin
register_activation_hook(__FILE__, 'wprg_activate');
function wprg_activate() {
    wprg_register_post_type();
    flush_rewrite_rules();
}

// Désactivation du plugin
register_deactivation_hook(__FILE__, 'wprg_deactivate');
function wprg_deactivate() {
    flush_rewrite_rules();
}

// Charger les scripts et styles
add_action('wp_enqueue_scripts', 'wprg_enqueue_scripts');
function wprg_enqueue_scripts() {
    wp_enqueue_style('wprg-styles', WPRG_PLUGIN_URL . 'assets/css/reels-gallery.css', array(), WPRG_VERSION);
    wp_enqueue_script('wprg-scripts', WPRG_PLUGIN_URL . 'assets/js/reels-gallery.js', array('jquery'), WPRG_VERSION, true);
    
    // Script pour les likes
    wp_enqueue_script('wprg-likes', WPRG_PLUGIN_URL . 'assets/js/reels-likes.js', array('jquery'), WPRG_VERSION, true);
    wp_localize_script('wprg-likes', 'wprg_likes', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('wprg_likes_nonce'),
        'heart_icon' => WPRG_PLUGIN_URL . 'assets/images/heart.svg'
    ));
    
    // Script pour les vues
    wp_enqueue_script('wprg-views', WPRG_PLUGIN_URL . 'assets/js/reels-views.js', array('jquery'), WPRG_VERSION, true);
    wp_localize_script('wprg-views', 'wprg_views', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('wprg_views_nonce')
    ));
}

// Charger les scripts admin
add_action('admin_enqueue_scripts', 'wprg_admin_enqueue_scripts');
function wprg_admin_enqueue_scripts($hook) {
    global $post_type;
    
    if ($post_type == 'wprg_reel' && in_array($hook, array('post.php', 'post-new.php'))) {
        wp_enqueue_script('jquery');
        wp_enqueue_script('jquery-ui-core');
        wp_enqueue_script('jquery-ui-draggable');
        wp_enqueue_script('jquery-ui-resizable');
        wp_enqueue_script('jquery-ui-touch-punch');
        
        wp_enqueue_media();
        wp_enqueue_style('wprg-admin-styles', WPRG_PLUGIN_URL . 'assets/css/admin-cropper.css', array(), WPRG_VERSION);
        wp_enqueue_style('wprg-admin-taxonomy', WPRG_PLUGIN_URL . 'assets/css/admin-taxonomy.css', array(), WPRG_VERSION);
        wp_enqueue_script('wprg-cropper', WPRG_PLUGIN_URL . 'assets/js/image-cropper.js', array('jquery', 'jquery-ui-draggable', 'jquery-ui-resizable'), WPRG_VERSION, true);
        
        $post = get_post();
        $thumbnail_nonce = wp_create_nonce('set_post_thumbnail-' . $post->ID);
        
        wp_localize_script('wprg-cropper', 'wprg_cropper', array(
            'ajax_url'        => admin_url('admin-ajax.php'),
            'nonce'           => wp_create_nonce('wprg_cropper_nonce'),
            'plugin_url'      => WPRG_PLUGIN_URL,
            'thumbnail_nonce' => $thumbnail_nonce
        ));
    }
    
    if ($hook == 'edit-tags.php' || $hook == 'term.php') {
        wp_enqueue_style('wprg-admin-taxonomy', WPRG_PLUGIN_URL . 'assets/css/admin-taxonomy.css', array(), WPRG_VERSION);
    }
    
    if ($post_type == 'wprg_reel' && $hook == 'edit.php') {
        wp_enqueue_style('wprg-admin-reels', WPRG_PLUGIN_URL . 'assets/css/admin-reels.css', array(), WPRG_VERSION);
    }
}

// Fonction pour extraire l'ID YouTube
function wprg_get_youtube_id($url) {
    $pattern = '/(?:youtube\.com\/(?:shorts\/|watch\?v=)|youtu\.be\/)([^\&\?\/]+)/';
    if (preg_match($pattern, $url, $matches)) {
        return $matches[1];
    }
    return false;
}

// Fonction pour obtenir la miniature YouTube
function wprg_get_youtube_thumbnail($url) {
    $video_id = wprg_get_youtube_id($url);
    if ($video_id) {
        return 'https://img.youtube.com/vi/' . $video_id . '/maxresdefault.jpg';
    }
    return false;
}
