<?php
/**
 * Plugin Name: Rizwan Image Alt Text WEBP Optimizer
 * Description: AI-powered image SEO optimizer for WordPress.
 * Version: 1.0.2
 * Author: Rizwan
 * Author URI: https://profiles.wordpress.org/rizwanplugins
 * License: GPLv2 or later
 * Text Domain: rizwan-image-alt-text-webp-optimizer
 */

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

/* FREEMIUS CODE START */

if ( ! function_exists( 'ais_fs' ) ) {

    function ais_fs() {

        global $ais_fs;

        if ( ! isset( $ais_fs ) ) {

            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';

            $ais_fs = fs_dynamic_init( array(
                'id'                  => '29506',
                'slug'                => 'rizora-image-seo',
                'premium_slug'        => 'rizora-image-seo-premium',
                'type'                => 'plugin',
                'public_key'          => 'pk_6fcd89d9ce13b6cc11f45dc724fab',
                'is_premium'          => false,
                'premium_suffix'      => 'Pro',
                'has_premium_version' => true,
                'has_addons'          => false,
                'has_paid_plans'      => true,
                'is_org_compliant'    => true,
                'menu'                => array(
                    'support' => false,
                ),
            ) );
        }

        return $ais_fs;
    }

    ais_fs();

    do_action( 'ais_fs_loaded' );
}

/* FREEMIUS CODE END */

/**
 * Plugin Constants
 */
define(
    'RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_VERSION',
    '1.0.2'
);

define(
    'RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_PLUGIN_URL',
    plugin_dir_url(__FILE__)
);

define(
    'RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_PLUGIN_PATH',
    plugin_dir_path(__FILE__)
);

define(
    'RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_PRO_VERSION',
    false
);

/**
 * Include Files
 */
require_once RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_PLUGIN_PATH .
'includes/class-rizwan-image-alt-text-webp-optimizer-image-handler.php';

require_once RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_PLUGIN_PATH .
'includes/class-rizwan-image-alt-text-webp-optimizer-webp-serving.php';

require_once RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_PLUGIN_PATH .
'includes/class-rizwan-image-alt-text-webp-optimizer-seo-score.php';

require_once RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_PLUGIN_PATH .
'includes/class-rizwan-image-alt-text-webp-optimizer-performance.php';

/**
 * Admin Menu
 */
add_action(
    'admin_menu',
    'rizwan_image_alt_text_webp_optimizer_admin_menu'
);

function rizwan_image_alt_text_webp_optimizer_admin_menu() {

    add_menu_page(
        'Rizwan Image Alt Text and WebP Optimizer',
        'Rizwan Image Alt Text and WebP Optimizer',
        'manage_options',
        'rizwan-image-alt-text-webp-optimizer',
        'rizwan_image_alt_text_webp_optimizer_dashboard_page',
        'dashicons-format-image'
    );


    

    add_submenu_page(
    'rizwan-image-alt-text-webp-optimizer',
    'Dashboard',
    'Dashboard',
    'manage_options',
    'rizwan-image-alt-text-webp-optimizer',
    'rizwan_image_alt_text_webp_optimizer_dashboard_page'
);


add_submenu_page(
    'rizwan-image-alt-text-webp-optimizer',
    'Settings',
    'Settings',
    'manage_options',
    'rizwan-image-alt-text-webp-optimizer-settings',
    'rizwan_image_alt_text_webp_optimizer_settings_page'
);

add_submenu_page(
        'rizwan-image-alt-text-webp-optimizer',
        'Upgrade To Pro',
        '🚀 Upgrade To Pro',
        'manage_options',
        'rizwan-image-alt-text-webp-optimizer-upgrade',
        'rizwan_image_alt_text_webp_optimizer_upgrade_page'
    );
}

function rizwan_image_alt_text_webp_optimizer_upgrade_page() {
?>

<div class="wrap">

    <div style="
        max-width:900px;
        margin:30px auto;
        background:linear-gradient(135deg,#0f172a,#1e293b);
        border-radius:20px;
        padding:40px;
        color:#fff;
        box-shadow:0 15px 40px rgba(0,0,0,0.25);
    ">

        <h1 style="
            color:#fff;
            font-size:36px;
            margin-bottom:10px;
        ">
            🚀 Upgrade To Pro
        </h1>

        <p style="
            font-size:18px;
            color:#cbd5e1;
            margin-bottom:30px;
        ">
            Unlock powerful image SEO automation and advanced optimization tools.
        </p>

        <div style="
            display:grid;
            grid-template-columns:repeat(auto-fit,minmax(250px,1fr));
            gap:15px;
            margin-bottom:35px;
        ">

            <div style="background:#111827;padding:15px;border-radius:12px;">
                ✅ SEO Scanner
            </div>

            <div style="background:#111827;padding:15px;border-radius:12px;">
                ✅ Bulk Optimization
            </div>

            <div style="background:#111827;padding:15px;border-radius:12px;">
                ✅ WebP + AVIF Conversion
            </div>

            <div style="background:#111827;padding:15px;border-radius:12px;">
                ✅ Automatic WebP + AVIF Serving
            </div>

            <div style="background:#111827;padding:15px;border-radius:12px;">
                ✅ Advanced AI Features
            </div>

            <div style="background:#111827;padding:15px;border-radius:12px;">
                ✅ Priority Updates
            </div>

        </div>

        <h2 style="
            color:#fff;
            margin-bottom:15px;
        ">
            Free vs Pro
        </h2>

        <table style="
            width:100%;
            border-collapse:collapse;
            margin-bottom:30px;
            background:#111827;
            border-radius:12px;
            overflow:hidden;
        ">

            <tr>
                <th style="padding:15px;text-align:left;">Feature</th>
                <th style="padding:15px;">Free</th>
                <th style="padding:15px;">Pro</th>
            </tr>

            <tr>
                <td style="padding:15px;">Auto Alt Text</td>
                <td align="center">✅</td>
                <td align="center">✅</td>
            </tr>

            <tr>
                <td style="padding:15px;">WebP Conversion</td>
                <td align="center">✅</td>
                <td align="center">✅</td>
            </tr>

            <tr>
                <td style="padding:15px;">AVIF Conversion</td>
                <td align="center">❌</td>
                <td align="center">✅</td>
            </tr>

            <tr>
                <td style="padding:15px;">SEO Scanner</td>
                <td align="center">❌</td>
                <td align="center">✅</td>
            </tr>

            <tr>
                <td style="padding:15px;">Bulk Optimization</td>
                <td align="center">❌</td>
                <td align="center">✅</td>
            </tr>

        </table>

        <a
            href="<?php echo esc_url( ais_fs()->get_upgrade_url() ); ?>"
            target="_blank"
            style="
                display:inline-block;
                background:linear-gradient(135deg,#6366f1,#8b5cf6);
                color:#fff;
                text-decoration:none;
                padding:18px 40px;
                border-radius:14px;
                font-size:20px;
                font-weight:700;
                box-shadow:0 10px 25px rgba(99,102,241,0.4);
            "
        >
            🚀 Upgrade Now
        </a>

    </div>

</div>

<?php
}

/**
 * Load Admin Assets
 */
add_action(
    'admin_enqueue_scripts',
    'rizwan_image_alt_text_webp_optimizer_admin_assets'
);

function rizwan_image_alt_text_webp_optimizer_admin_assets(
    $hook
) {

    /**
     * Admin CSS
     */
    wp_enqueue_style(
        'rizwan-image-alt-text-webp-optimizer-admin-css',
        RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_PLUGIN_URL .
        'assets/css/admin.css',
        [],
        RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_VERSION
    );

    /**
     * Chart JS
     */
    wp_enqueue_script(
        'rizwan-image-alt-text-webp-optimizer-chart-js',
        RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_PLUGIN_URL .
        'assets/js/chart.umd.min.js',
        [],
        RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_VERSION,
        true
    );

    /**
     * Media Refresh Script
     */
    if (
        $hook === 'upload.php' ||
        $hook === 'media-new.php'
    ) {

        wp_enqueue_script(
            'rizwan-image-alt-text-webp-optimizer-media-refresh',
            RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_PLUGIN_URL .
            'assets/js/media-auto-refresh.js',
            ['jquery'],
            RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_VERSION,
            true
        );
    }
}


/**
 * Dashboard Page
 */
function rizwan_image_alt_text_webp_optimizer_dashboard_page() {

    include RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_PLUGIN_PATH . 'admin/dashboard.php';
}

/**
 * Settings Page
 */
function rizwan_image_alt_text_webp_optimizer_settings_page() {

    include RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_PLUGIN_PATH . 'admin/settings-page.php';
}

/**
 * Register Settings
 */
add_action(
    'admin_init',
    'rizwan_image_alt_text_webp_optimizer_register_settings'
);

function rizwan_image_alt_text_webp_optimizer_register_settings() {

    register_setting(
        'rizwan_image_alt_text_webp_optimizer_settings_group',
        'rizwan_image_alt_text_webp_optimizer_auto_alt',
        [
            'sanitize_callback' => 'absint',
        ]
    );

    register_setting(
        'rizwan_image_alt_text_webp_optimizer_settings_group',
        'rizwan_image_alt_text_webp_optimizer_auto_title',
        [
            'sanitize_callback' => 'absint',
        ]
    );

    register_setting(
        'rizwan_image_alt_text_webp_optimizer_settings_group',
        'rizwan_image_alt_text_webp_optimizer_auto_caption',
        [
            'sanitize_callback' => 'absint',
        ]
    );

    register_setting(
        'rizwan_image_alt_text_webp_optimizer_settings_group',
        'rizwan_image_alt_text_webp_optimizer_auto_webp',
        [
            'sanitize_callback' => 'absint',
        ]
    );
}

add_filter(
    'upload_mimes',
    'rizwan_image_alt_text_webp_optimizer_upload_mimes'
);

function rizwan_image_alt_text_webp_optimizer_upload_mimes(
    $mimes
) {

    $mimes['webp'] = 'image/webp';

    return $mimes;
}



if (class_exists('RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_Image_Handler')) {
    new RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_Image_Handler();
}

if (class_exists('RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_SEO_Score')) {
    new RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_SEO_Score();
}

if (class_exists('RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_Performance')) {
    new RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_Performance();
}

if (class_exists('RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_WebP_Serving')) {
    new RIZWAN_IMAGE_ALT_TEXT_WEBP_OPTIMIZER_WebP_Serving();
}