<?php

/**
 * Plugin Name:       UnoDock - Mobile Bottom Navigation
 * Plugin URI:         https://unoizestudio.com/unodock
 * Description:        A fully customizable bottom navigation bar for mobile, with up to 6 buttons, icons, colors, ordering and page/user visibility rules.
 * Version:             1.3.5
 * Requires at least:   5.8
 * Requires PHP:        7.4
 * Author:              UnoizeStudio
 * Author URI:          https://unoizestudio.com
 * License:             GPL v2 or later
 * License URI:         https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:         unodock
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
    // Direct access not allowed.
}
/**
 * Freemius SDK — handles licensing, updates, and the free/PRO feature gate.
 * Must be initialized as early as possible, before any other plugin code.
 */
if ( !function_exists( 'unodock_fs' ) ) {
    // Create a helper function for easy SDK access.
    function unodock_fs() {
        global $unodock_fs;
        if ( !isset( $unodock_fs ) ) {
            $freemius_start = dirname( __FILE__ ) . '/vendor/freemius/start.php';
            // Avoids a fatal error if the SDK hasn't been placed in vendor/freemius/ yet.
            if ( !file_exists( $freemius_start ) ) {
                add_action( 'admin_notices', function () {
                    echo '<div class="notice notice-error"><p>';
                    echo esc_html__( 'UnoDock: the Freemius SDK files are missing from vendor/freemius/. Licensing and updates are disabled until they are added.', 'unodock' );
                    echo '</p></div>';
                } );
                return null;
            }
            // Include Freemius SDK.
            require_once $freemius_start;
            $unodock_fs = fs_dynamic_init( array(
                'id'               => '34602',
                'slug'             => 'unodock',
                'type'             => 'plugin',
                'public_key'       => 'pk_7607ecb6a4cc2c217a92b8599e113',
                'is_premium'       => false,
                'premium_suffix'   => 'Pro',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => array(
                    'slug'    => 'unodock',
                    'support' => false,
                ),
                'is_live'          => true,
            ) );
        }
        return $unodock_fs;
    }

    // Init Freemius.
    unodock_fs();
    // Signal that SDK was initiated.
    do_action( 'unodock_fs_loaded' );
    /**
     * Cleans up UnoDock's saved data, but only if the user opted into this
     * via the "Delete all settings when the plugin is uninstalled" setting.
     * Hooked to Freemius's "after_uninstall" action instead of using a
     * standalone uninstall.php file, since Freemius needs to intercept the
     * uninstall event first (e.g. to collect optional uninstall feedback) —
     * a plain uninstall.php file would prevent that from working.
     */
    function udock_after_uninstall_cleanup() {
        $settings = get_option( UNODOCK_OPTION_KEY );
        if ( is_array( $settings ) && !empty( $settings['general']['uninstall_cleanup'] ) ) {
            delete_option( UNODOCK_OPTION_KEY );
            delete_option( 'udock_installed_at' );
            delete_option( 'udock_review_dismissed' );
            delete_option( 'udock_review_snooze_until' );
        }
    }

    if ( function_exists( 'unodock_fs' ) && unodock_fs() ) {
        unodock_fs()->add_action( 'after_uninstall', 'udock_after_uninstall_cleanup' );
    }
}
define( 'UNODOCK_VERSION', '1.3.5' );
define( 'UNODOCK_PATH', plugin_dir_path( __FILE__ ) );
define( 'UNODOCK_URL', plugin_dir_url( __FILE__ ) );
define( 'UNODOCK_BASENAME', plugin_basename( __FILE__ ) );
define( 'UNODOCK_OPTION_KEY', 'udock_settings' );
define( 'UNODOCK_MAX_BUTTONS', 6 );
/**
 * Whether the current site has an active UnoDock PRO license.
 * Falls back to false if the Freemius SDK isn't available for any reason.
 *
 * Used for informational/UI purposes only (badges, locked-control styling,
 * "Upgrade to unlock" prompts) — this code must stay present in the free
 * version so those prompts still work there. For anything that actually
 * changes the plugin's behavior or output, use udock_is_pro_functional()
 * instead.
 */
function udock_is_pro() {
    static $is_pro = null;
    if ( null !== $is_pro ) {
        return $is_pro;
    }
    if ( !function_exists( 'unodock_fs' ) ) {
        $is_pro = false;
        return $is_pro;
    }
    $fs = unodock_fs();
    if ( !$fs ) {
        $is_pro = false;
        return $is_pro;
    }
    $is_pro = $fs->can_use_premium_code();
    return $is_pro;
}

/**
 * Whether PRO functionality should actually run. Unlike udock_is_pro(),
 * this wraps Freemius's "__premium_only" method, which the Freemius
 * deployment preprocessor recognizes and physically removes from the
 * generated free package — so the underlying PRO behavior/code is not
 * merely hidden, it is entirely absent from what ships to WordPress.org.
 *
 * Use this for anything that actually produces PRO behavior or output
 * (feature limits, visual effects, restricted visibility, etc.). Never
 * use this for UI elements that should remain visible in the free
 * version (badges, locked controls, upgrade prompts).
 */
function udock_is_pro_functional() {
    static $is_pro_functional = null;
    if ( null !== $is_pro_functional ) {
        return $is_pro_functional;
    }
    $is_pro_functional = false;
    if ( function_exists( 'unodock_fs' ) && unodock_fs() ) {
    }
    return $is_pro_functional;
}

/**
 * Maximum number of buttons allowed: 4 on the free tier, 6 on PRO.
 * Uses the premium_only-safe check, so the free package's code physically
 * cannot allow more than 4, regardless of any saved or tampered data.
 */
function udock_max_buttons() {
    if ( udock_is_pro_functional() ) {
        return UNODOCK_MAX_BUTTONS;
    }
    return 4;
}

require_once UNODOCK_PATH . 'includes/class-unodock-icons.php';
require_once UNODOCK_PATH . 'includes/class-unodock-settings.php';
require_once UNODOCK_PATH . 'includes/class-unodock-admin.php';
require_once UNODOCK_PATH . 'includes/class-unodock-frontend.php';
/**
 * Returns a cache-busting version string for a plugin asset, based on the
 * file's last modification time. This means CSS/JS changes are always
 * picked up immediately, without needing a manual version bump.
 *
 * @param string $relative_path Path relative to the plugin folder, e.g. 'assets/css/admin.css'.
 */
function udock_asset_version(  $relative_path  ) {
    $file = UNODOCK_PATH . ltrim( $relative_path, '/' );
    return ( file_exists( $file ) ? (string) filemtime( $file ) : UNODOCK_VERSION );
}

/**
 * Activation: creates the default settings if they don't exist yet.
 */
function udock_activate() {
    if ( false === get_option( UNODOCK_OPTION_KEY ) ) {
        update_option( UNODOCK_OPTION_KEY, UNODOCK_Settings::get_defaults() );
    }
    if ( false === get_option( 'udock_installed_at' ) ) {
        update_option( 'udock_installed_at', time() );
    }
}

register_activation_hook( __FILE__, 'udock_activate' );
/**
 * Boots the plugin.
 */
function udock_init() {
    new UNODOCK_Admin();
    new UNODOCK_Frontend();
}

add_action( 'plugins_loaded', 'udock_init' );