<?php
/**
 * Plugin Name: Titanium Nav Menu Roles & Visibility Rules
 * Plugin URI:  https://adminplugins.com/advanced-menu-rules/
 * Description: Advanced menu visibility rules for WordPress menus.
 * Version:     1.0.2
 * Author:      AdminPlugins
 * Author URI:  https://adminplugins.com/
 * License:     GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: titanium-advanced-menu-rules
 * Domain Path: /languages
 * Requires at least: 5.8
 * Requires PHP: 7.4
 */

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

if ( function_exists( 'tiamr_fs' ) ) {
    tiamr_fs()->set_basename( true, __FILE__ );
} elseif ( ! function_exists( 'tiamr_fs' ) ) {
    /**
     * Create a helper function for easy Freemius SDK access.
     */
    function tiamr_fs() {
        global $tiamr_fs;

        if ( ! isset( $tiamr_fs ) ) {
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';

            $tiamr_fs = fs_dynamic_init(
                array(
                    'id'                  => '32028',
                    'slug'                => 'titanium-advanced-menu-rules',
                    'premium_slug'        => 'titanium-advanced-menu-rules-pro',
                    'type'                => 'plugin',
                    'public_key'          => 'pk_5571f63bb4256d51416cc00cd2c25',
                    'is_premium'          => false,
                    'premium_suffix'      => 'Pro',
                    'has_premium_version' => true,
                    'has_addons'          => false,
                    'has_paid_plans'      => true,
                    'is_org_compliant'    => true,
                    'menu'                => array(
                        'slug'    => 'titanium-advanced-menu-rules',
                        'support' => false,
                    ),
                )
            );
        }

        if ( method_exists( $tiamr_fs, 'add_filter' ) ) {
            $tiamr_fs->add_filter( 'pricing_url', 'tiamr_custom_freemius_pricing_url' );
        }

        return $tiamr_fs;
    }

    tiamr_fs();
    do_action( 'tiamr_fs_loaded' );
}

if ( ! function_exists( 'tiamr_custom_freemius_pricing_url' ) ) {
    /**
     * Keep Freemius pricing links on the custom Upgrade to Pro page.
     *
     * @param string $url Original Freemius pricing URL.
     * @return string
     */
    function tiamr_custom_freemius_pricing_url( $url ) {
        if ( ! is_admin() ) {
            return $url;
        }

        return admin_url( 'admin.php?page=titanium-advanced-menu-rules-upgrade' );
    }
}


if ( ! function_exists( 'tiamr_fs_hide_default_pricing_page' ) ) {
    /**
     * Hides the SDK-generated pricing submenu because this plugin uses its own pricing page.
     */
    function tiamr_fs_hide_default_pricing_page() {
        return false;
    }
}

if ( function_exists( 'tiamr_fs' ) ) {
    tiamr_fs()->add_filter( 'is_pricing_page_visible', 'tiamr_fs_hide_default_pricing_page' );
}

if ( ! function_exists( 'tiamr_fs_uninstall_cleanup' ) ) {
    /**
     * Cleans Titanium Nav Menu Roles & Visibility Rules data after Freemius has reported the uninstall event.
     */
    function tiamr_fs_uninstall_cleanup() {
        $delete_data = (bool) get_option( 'tiamr_delete_data_on_uninstall', false );

        delete_option( 'tiamr_delete_data_on_uninstall' );
        delete_option( 'tiamr_do_activation_redirect' );
        delete_option( 'tiamr_show_menu_location_warnings' );
        delete_option( 'tiamr_beta_updates' );
        delete_option( 'tiamr_nav_menu_roles_import_notice_dismissed' );
        delete_option( 'tiamr_nav_menu_roles_import_completed' );

        if ( ! $delete_data ) {
            return;
        }

        $meta_keys = array(
            '_tiamr_condition',
            '_tiamr_action',
            '_tiamr_audience',
            '_tiamr_location',
            '_tiamr_roles',
            '_tiamr_pages',
            '_tiamr_posts',
            '_tiamr_pro_action',
            '_tiamr_pro_condition_groups',
            '_tiamr_pro_enabled',
            '_tiamr_pro_protect_linked_content',
            '_tiamr_pro_denied_redirect_page',
            '_tiamr_pro_denied_redirect_type',
            '_tiamr_pro_denied_action_override',
            '_tiamr_pro_denied_404_source',
        );

        foreach ( $meta_keys as $meta_key ) {
            delete_post_meta_by_key( $meta_key );
        }

        delete_option( 'tiamr_pro_global_page_protection' );
        delete_option( 'tiamr_pro_denied_redirect_type' );
        delete_option( 'tiamr_pro_denied_redirect_page' );
        delete_option( 'tiamr_pro_denied_404_source' );
        delete_option( 'tiamr_pro_skip_membership_system_pages' );
        delete_option( 'tiamr_pro_defer_to_other_redirects' );
        delete_option( 'tiamr_pro_excluded_paths' );
        delete_option( 'tiamr_pro_has_menu_item_page_protection' );
        delete_transient( 'tiamr_pro_page_protection_index_v1' );
        delete_option( 'tiamr_pro_page_protection_index_v2' );
        delete_option( 'tiamr_pro_page_protection_index_v4' );
        delete_option( 'tiamr_pro_page_protection_index_v5' );
    }
}

if ( function_exists( 'tiamr_fs' ) ) {
    tiamr_fs()->add_action( 'after_uninstall', 'tiamr_fs_uninstall_cleanup' );
}

if ( ! defined( 'TIAMR_VERSION' ) ) {
    define( 'TIAMR_VERSION', '1.0.2' );
}

define( 'TIAMR_PLUGIN_FILE', __FILE__ );
define( 'TIAMR_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'TIAMR_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'TIAMR_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );

spl_autoload_register(
    static function ( $class ) {
        $prefix = 'Tiamr\\';

        if ( 0 !== strpos( $class, $prefix ) ) {
            return;
        }

        $relative_class = substr( $class, strlen( $prefix ) );
        $relative_path  = str_replace( '\\', '/', $relative_class );
        $file           = TIAMR_PLUGIN_DIR . 'includes/' . $relative_path . '.php';

        if ( is_readable( $file ) ) {
            require_once $file;
        }
    }
);

/**
 * Returns the Titanium Nav Menu Roles & Visibility Rules core instance.
 *
 * @return Tiamr\Plugin
 */
if ( ! function_exists( 'tiamr' ) ) {
    function tiamr() {
        return Tiamr\Plugin::instance();
    }
}

register_activation_hook(
    __FILE__,
    static function () {
        update_option( 'tiamr_do_activation_redirect', 1, false );
        delete_option( 'tiamr_nav_menu_roles_import_notice_dismissed' );
        delete_option( 'tiamr_nav_menu_roles_import_completed' );
    }
);
add_action(
    'plugins_loaded',
    static function () {
        if ( ! class_exists( 'Tiamr\\Plugin' ) ) {
            return;
        }

        tiamr();
    },
    20
);
