<?php
/**
 * Plugin Name: Advance Universal Modern Portfolio and Gallery (AUMP)
 * Plugin URI: https://myexpertdev.com/
 * Description: Advance Universal Modern Portfolio and Gallery (AUMP) is a cutting-edge WordPress plugin that lets you create sophisticated portfolio and gallery showcases.
 * Version: 4.5.0
 * Author: Muhammad Abbas
 * Author URI: https://myexpertdev.com/about-muhammad-abbas/
 * License: GPL2
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: advanced-universal-modern-portfolio-and-gallery
 * Domain Path: /languages
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

// Define plugin constants
define('AUMP_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('AUMP_PLUGIN_URL', plugin_dir_url(__FILE__));

// Load plugin text domain for translations
function aump_load_textdomain() {
    load_plugin_textdomain('aump', false, dirname(plugin_basename(__FILE__)) . '/languages/');
}
add_action('plugins_loaded', 'aump_load_textdomain');

// Include necessary files
require_once AUMP_PLUGIN_DIR . 'includes/options-page.php';
require_once AUMP_PLUGIN_DIR . 'includes/custom-post-type.php';
require_once AUMP_PLUGIN_DIR . 'includes/popup.php';
require_once AUMP_PLUGIN_DIR . 'includes/functions.php';
require_once AUMP_PLUGIN_DIR . 'includes/user-options-update.php';


/**
 * Load custom template for single portfolio items
 */
function aump_load_single_portfolio_template($template) {
    if (is_singular('aump_portfolio')) {
        $plugin_template = plugin_dir_path(__FILE__) . 'templates/single-portfolio.php';
        if (file_exists($plugin_template)) {
            return $plugin_template;
        }
    }
    return $template;
}
add_filter('single_template', 'aump_load_single_portfolio_template');

// Enqueue frontend scripts and styles
function aump_enqueue_frontend_assets() {
    $posts_per_page = get_option('aump_posts_per_page', 6); // Default to 6
    $max_pages = (new WP_Query(['post_type' => 'aump_portfolio', 'posts_per_page' => $posts_per_page]))->max_num_pages;
    wp_enqueue_style('aump-frontend-css', AUMP_PLUGIN_URL . 'assets/css/frontend.css', array(), time());
    wp_enqueue_script('aump-frontend-js', AUMP_PLUGIN_URL . 'assets/js/frontend.js', array('jquery'), time(), true);
      wp_localize_script('aump-frontend-js', 'aump_loadmore', [
        'ajaxurl'      => admin_url('admin-ajax.php'),
        'nonce'        => wp_create_nonce('aump_load_more_nonce'),
        'maxPages'     => $max_pages,
        'postsPerPage' => $posts_per_page
    ]);
	// Get the option value
$aump_auto_scroll_disabled = get_option('aump_auto_scroll', 'no'); // Default to 'no'
$aump_open_in_popup = get_option('aump_open_in_popup', 'no'); // Default to 'no'

// Pass the value to JavaScript
wp_localize_script('aump-frontend-js', 'aumpSettings', [
    'autoScrollDisabled' => $aump_auto_scroll_disabled === 'yes' ? true : false,
    'aump_open_in_popup' => $aump_open_in_popup === 'yes' ? true : false,

]);
}
add_action('wp_enqueue_scripts', 'aump_enqueue_frontend_assets');

// Enqueue admin scripts and styles
function aump_enqueue_admin_assets() {
    // Enqueue existing admin CSS
    wp_enqueue_style('aump-admin-css', AUMP_PLUGIN_URL . 'assets/css/admin.css', array(), time());

    // Enqueue Pickr Styles
    //wp_enqueue_style('pickr-css', 'https://cdn.jsdelivr.net/npm/@simonwep/pickr/dist/themes/classic.min.css', array(), '1.8.2');
    wp_enqueue_style('pickr-css', AUMP_PLUGIN_URL . 'assets/css/classic.min.css', array(), time());

    // Enqueue Pickr JS
    //wp_enqueue_script('pickr-js', 'https://cdn.jsdelivr.net/npm/@simonwep/pickr/dist/pickr.min.js', array(), '1.8.2', true);
    wp_enqueue_script(
        'pickr-js',
        AUMP_PLUGIN_URL . 'assets/js/pickr.min.js',
        time(),
        true
    );	
    // Enqueue existing admin JS with Pickr dependency
    wp_enqueue_script(
        'aump-admin-js',
        AUMP_PLUGIN_URL . 'assets/js/admin.js',
        array('jquery', 'pickr-js'),
        time(),
        true
    );
}
add_action('admin_enqueue_scripts', 'aump_enqueue_admin_assets');


// Activation hook function
function aump_activate() {
    // Activation logic here
}
register_activation_hook(__FILE__, 'aump_activate');

// Deactivation hook function
function aump_deactivate() {
    // Deactivation logic here
}
register_deactivation_hook(__FILE__, 'aump_deactivate');

//Freemius 
if ( ! function_exists( 'aumpag_fs' ) ) {
    // Helper function for easy SDK access.
    function aumpag_fs() {
        global $aumpag_fs;

        if ( ! isset( $aumpag_fs ) ) {
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
            $aumpag_fs = fs_dynamic_init( array(
                'id'                  => '18862',
                'slug'                => 'advanced-universal-modern-portfolio-and-gallery',
                'type'                => 'plugin',
                'public_key'          => 'pk_85356ce1a839d5851546caf56dd91',
                'is_premium'          => false,
                'premium_suffix'      => 'Aump Pro',
                // If your plugin is a serviceware, set this option to false.
                'has_premium_version' => true,
                'has_addons'          => false,
                'has_paid_plans'      => true,
                'menu'                => array(
                    'slug'           => 'edit.php?post_type=aump_portfolio',
                    'support'        => false,
                ),
            ) );
        }

        return $aumpag_fs;
    }
    aumpag_fs();
    //SDK initiated.
    do_action( 'aumpag_fs_loaded' );
}