<?php
/**
 * Plugin Name: SmartPress PRO
 * Plugin URI: https://smartpress.10g8.com
 * Description: WordPress Just got Smarter! Advanced AI writing, image generation, and a personal AI assistant, all inside your dashboard.
 * Version: 1.2.8
 * Author: TenGate
 * Author URI: https://10g8.com
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: smartpress-pro
 */

 // Prevent direct access to this file
if (!defined('ABSPATH')) {
    exit;
}

////////////////////////////////////////////////////////////
// ENVIRONMENT VARIABLES
////////////////////////////////////////////////////////////

define('SMARTPRESS_ENABLE_FRONTEND', false);
define('SMARTPRESS_ENABLE_BACKEND', true);
define('SMARTPRESS_ENABLE_BACKEND_GLOBAL', true);

define('SMARTPRESS_DEV_MODE', false);
define('SMARTPRESS_ENABLE_DEBUG', false);

define('SMARTPRESS_MENU_SLUG', "smartpress");

define('SMARTPRESS_APP_VERSION', '1.2.8');

// Define plugin constants
define('SMARTPRESS_PATH', plugin_dir_path(__FILE__));
define('SMARTPRESS_URL', plugin_dir_url(__FILE__));


////////////////////////////////////////////////////////////
// END ENVIRONMENT VARIABLES
////////////////////////////////////////////////////////////




// Include environment loader
require_once SMARTPRESS_PATH . 'includes/env-loader.php';


// Enqueue custom CSS for the smart-press plugin
add_action('admin_enqueue_scripts', 'smartpress_custom_admin_css');
function smartpress_custom_admin_css() {
    wp_enqueue_style('smartpress_admin_css', plugins_url('css/admin-style.css', __FILE__), array(), SMARTPRESS_APP_VERSION);
}

// Include plugin components
require_once SMARTPRESS_PATH . 'admin/admin-menu.php';
require_once SMARTPRESS_PATH . 'includes/enqueue-scripts.php';
require_once SMARTPRESS_PATH . 'includes/mount-points.php';
require_once SMARTPRESS_PATH . 'includes/settings-api.php';
require_once SMARTPRESS_PATH . 'includes/custom-fields.php';
require_once SMARTPRESS_PATH . 'includes/media-upload.php';

// Register activation hook to set a transient for redirection
register_activation_hook(__FILE__, 'smartpress_activation_redirect');

// Redirect function
function smartpress_activation_redirect() {
    // Set a transient that expires after 30 seconds
    set_transient('smartpress_activation_redirect', true, 30);
}

// Hook into admin_init to perform the redirect with a late priority to avoid conflicts
add_action('admin_init', 'smartpress_redirect_on_activation', 999);

function smartpress_redirect_on_activation() {
    // Check if our transient exists
    if (get_transient('smartpress_activation_redirect')) {
        // Delete the transient to prevent redirect loops
        delete_transient('smartpress_activation_redirect');
        
        // Only redirect if not doing AJAX and we're not in a typical activation flow
        if (!wp_doing_ajax() && 
            (!isset($_GET['activate-multi']) && !isset($_GET['activated']))) {
            
            // Safely redirect to our get-started page
            wp_safe_redirect(admin_url('admin.php?page=smartpress#/get-started'));
            exit;
        }
    }
}

// Add a "Visit Website" link to the plugins page
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'smartpress_add_action_links');

function smartpress_add_action_links($links) {
    $website_link = '<a href="https://smartpress.10g8.com" target="_blank">Visit Website</a>';
    array_unshift($links, $website_link);
    return $links;
} 

