<?php

/**
 * Plugin Name:       Customer Cloud Gallery
 * Description:       Image galleries served directly from a Google Drive or Dropbox folder. Lazy-load grid, lightbox, favorites, single-file and full-gallery downloads, optional password protection, optional print-order shop (Pro).
 * Version:           1.13.0
 * Requires at least: 6.2
 * Requires PHP:      7.4
 * Author:            Customer Cloud Gallery
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       customer-cloud-gallery
 * Domain Path:       /languages
 *
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// Prevent fatal "cannot redeclare class" errors when both the Free build (from wp.org)
// and the Premium build (downloaded manually from freemius.com) end up installed in
// parallel under different plugin directories (customer-cloud-gallery/ vs.
// customer-cloud-gallery-premium/). WordPress would load both main files, both would
// require the same class files via different paths, and PHP would fatal on the second
// class declaration. The first instance to load wins; the second one bails out and
// surfaces a clear admin notice so the site owner can deactivate the duplicate.
if ( defined( 'CCGAL_PLUGIN_LOADED' ) ) {
    add_action( 'admin_notices', function () {
        if ( !current_user_can( 'manage_options' ) ) {
            return;
        }
        echo '<div class="notice notice-error"><p><strong>Customer Cloud Gallery:</strong> ';
        echo esc_html__( 'Both the Free and the Pro versions of this plugin appear to be active at the same time. They cannot run in parallel — please go to Plugins and deactivate one of them. The Pro version supersedes the Free version; deactivate Free if you have a Pro license.', 'customer-cloud-gallery' );
        echo '</p></div>';
    } );
    return;
}
define( 'CCGAL_PLUGIN_LOADED', true );
define( 'CCGAL_VERSION', '1.13.0' );
define( 'CCGAL_FILE', __FILE__ );
define( 'CCGAL_DIR', plugin_dir_path( __FILE__ ) );
define( 'CCGAL_URL', plugin_dir_url( __FILE__ ) );
define( 'CCGAL_BASENAME', plugin_basename( __FILE__ ) );
define( 'CCGAL_PREFIX', 'ccgal_' );
if ( !function_exists( 'ccgal_fs' ) ) {
    function ccgal_fs() {
        global $ccgal_fs;
        if ( !isset( $ccgal_fs ) ) {
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
            $ccgal_fs = fs_dynamic_init( array(
                'id'               => '28689',
                'slug'             => 'customer-cloud-gallery',
                'type'             => 'plugin',
                'public_key'       => 'pk_c599faf0e2f422f2fe1ef9a502979',
                'is_premium'       => false,
                'premium_suffix'   => 'Pro',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => array(
                    'slug'       => 'edit.php?post_type=ccgal_gallery',
                    'first-path' => 'edit.php?post_type=ccgal_gallery',
                    'support'    => false,
                ),
                'is_live'          => true,
            ) );
        }
        return $ccgal_fs;
    }

    ccgal_fs();
    do_action( 'ccgal_fs_loaded' );
}
if ( !function_exists( 'ccgal_is_pro' ) ) {
    function ccgal_is_pro() {
        return function_exists( 'ccgal_fs' ) && ccgal_fs()->can_use_premium_code();
    }

}
if ( !function_exists( 'ccgal_pro_upgrade_url' ) ) {
    function ccgal_pro_upgrade_url() {
        if ( function_exists( 'ccgal_fs' ) ) {
            return ccgal_fs()->get_upgrade_url();
        }
        return '#';
    }

}
require_once CCGAL_DIR . 'includes/class-activator.php';
require_once CCGAL_DIR . 'includes/class-deactivator.php';
require_once CCGAL_DIR . 'includes/class-uninstaller.php';
require_once CCGAL_DIR . 'includes/class-welcome.php';
require_once CCGAL_DIR . 'includes/class-privacy.php';
require_once CCGAL_DIR . 'includes/class-encryption.php';
require_once CCGAL_DIR . 'includes/class-cpt.php';
require_once CCGAL_DIR . 'includes/class-storage-adapter.php';
require_once CCGAL_DIR . 'includes/class-storage.php';
require_once CCGAL_DIR . 'includes/class-google-oauth.php';
require_once CCGAL_DIR . 'includes/class-google-drive-adapter.php';
require_once CCGAL_DIR . 'includes/class-dropbox-oauth.php';
require_once CCGAL_DIR . 'includes/class-dropbox-adapter.php';
require_once CCGAL_DIR . 'includes/class-cache.php';
require_once CCGAL_DIR . 'includes/class-favorites.php';
require_once CCGAL_DIR . 'includes/class-gallery-renderer.php';
require_once CCGAL_DIR . 'includes/class-rest-api.php';
require_once CCGAL_DIR . 'includes/class-downloads.php';
require_once CCGAL_DIR . 'includes/class-zip-stream.php';
require_once CCGAL_DIR . 'includes/class-admin.php';
// Pro classes are physically shipped only in the Premium build (Freemius __premium_only suffix — note: TWO underscores).
// In the Free build these files do not exist on disk and must not be required.
// Defensive file_exists() guard: covers the edge case where a user holds a Pro
// license (so ccgal_is_pro() returns true) but only the Free build is on disk
// (e.g. after deactivating Premium and installing Free without removing the
// license state). Without the guard, require_once on a missing file is a fatal.
if ( function_exists( 'ccgal_is_pro' ) && ccgal_is_pro() ) {
    $ccgal_premium_files = array(
        'class-tracking__premium_only.php',
        'class-dashboard__premium_only.php',
        'class-print-admin__premium_only.php',
        'class-print-settings__premium_only.php',
        'class-orders__premium_only.php',
        'class-print-rest__premium_only.php',
        'class-print-page__premium_only.php'
    );
    $ccgal_pro_disk_ok = true;
    foreach ( $ccgal_premium_files as $ccgal_pf ) {
        if ( !file_exists( CCGAL_DIR . 'includes/' . $ccgal_pf ) ) {
            $ccgal_pro_disk_ok = false;
            break;
        }
    }
    if ( $ccgal_pro_disk_ok ) {
        foreach ( $ccgal_premium_files as $ccgal_pf ) {
            require_once CCGAL_DIR . 'includes/' . $ccgal_pf;
        }
    } else {
        // User has a Pro license but only the Free build is on disk. Show a
        // dismissible admin notice with instructions.
        add_action( 'admin_notices', function () {
            if ( !current_user_can( 'manage_options' ) ) {
                return;
            }
            echo '<div class="notice notice-warning"><p><strong>Customer Cloud Gallery:</strong> ';
            echo esc_html__( 'A Pro license is active but the Pro build is not installed. Please download the Pro plugin from your Freemius account and install it to access the Pro features. Free features continue to work normally.', 'customer-cloud-gallery' );
            echo '</p></div>';
        } );
    }
    unset($ccgal_premium_files, $ccgal_pro_disk_ok, $ccgal_pf);
}
require_once CCGAL_DIR . 'includes/class-text-overrides.php';
require_once CCGAL_DIR . 'includes/class-plugin.php';
register_activation_hook( __FILE__, array('CCGAL_Activator', 'activate') );
register_deactivation_hook( __FILE__, array('CCGAL_Deactivator', 'deactivate') );
ccgal_fs()->add_action( 'after_uninstall', array('CCGAL_Uninstaller', 'cleanup') );
add_action( 'plugins_loaded', array('CCGAL_Plugin', 'instance') );