<?php
/**
 * Plugin Name: Flavor – Restaurant Menu
 * Plugin URI: https://flavor-menu.com
 * Description: A modern, elegant restaurant menu plugin for WordPress. Supports WPBakery, Elementor, Gutenberg and the [flavor_menu] shortcode.
 * Version: 1.5.11
 * Author: Flavor Studio
 * Requires at least: 6.0
 * Tested up to: 7.0
 * Requires PHP: 7.4
 * Text Domain: flavor-restaurant-menu
 * Domain Path: /languages
 * License: GPL v2 or later
 */

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

define( 'FLAVOR_MENU_VERSION', '1.5.11' );
define( 'FLAVOR_MENU_PATH', plugin_dir_path( __FILE__ ) );
define( 'FLAVOR_MENU_URL', plugin_dir_url( __FILE__ ) );

/**
 * Freemius SDK init.
 * The outer if/else enables auto-deactivation of the free version
 * when the premium version is activated (Freemius set_basename mechanism).
 */
if ( function_exists( 'flavor_fs' ) ) {
    flavor_fs()->set_basename( false, __FILE__ );
} else {

if ( ! function_exists( 'flavor_fs' ) ) {
    function flavor_fs() {
        global $flavor_fs;

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

            $flavor_fs = fs_dynamic_init( array(
                'id'                  => '27618',
                'slug'                => 'flavor-restaurant-menu',
                'premium_slug'        => 'flavor-restaurant-menu-premium',
                'type'                => 'plugin',
                'public_key'          => 'pk_a1d7ee4772f812973acb726c2376d',
                'is_premium'          => false,
                'has_premium_version' => true,
                'has_addons'          => false,
                'has_paid_plans'      => true,
                'is_org_compliant'    => true,
                'menu'                => array(
                    'slug'    => 'edit.php?post_type=flavor_menu_item',
                    'contact' => false,
                    'support' => false,
                ),
            ) );
        }

        return $flavor_fs;
    }

    flavor_fs();
    do_action( 'flavor_fs_loaded' );
}

/**
 * Outputs a Documentation link button for admin page headers.
 *
 * @param string $anchor  Optional anchor to append to the docs URL (e.g. '#shortcode').
 */
function flavor_docs_link_html( $anchor = '' ) {
    $url = 'https://flavor-menu.com/knowledge-base' . ( $anchor ? $anchor : '' );
    ?>
    <a href="<?php echo esc_url( $url ); ?>" class="page-title-action" target="_blank" rel="noopener">
        <?php esc_html_e( 'Documentation', 'flavor-restaurant-menu' ); ?> ↗
    </a>
    <?php
}

/**
 * Adds a Documentation link to the plugin row in the Plugins list.
 *
 * @param array  $links  Existing row meta links.
 * @param string $file   Plugin basename.
 * @return array
 */
function flavor_plugin_row_meta( $links, $file ) {
    if ( plugin_basename( __FILE__ ) === $file ) {
        $links[] = '<a href="https://flavor-menu.com/knowledge-base" target="_blank" rel="noopener">' . esc_html__( 'Documentation', 'flavor-restaurant-menu' ) . '</a>';
    }
    return $links;
}
add_filter( 'plugin_row_meta', 'flavor_plugin_row_meta', 10, 2 );

/**
 * Initialize core classes (always loaded, independent of any page builder).
 */
function flavor_menu_init() {
    require_once FLAVOR_MENU_PATH . 'includes/class-flavor-cpt.php';
    require_once FLAVOR_MENU_PATH . 'includes/class-flavor-metaboxes.php';
    require_once FLAVOR_MENU_PATH . 'includes/class-flavor-shortcode.php';
    require_once FLAVOR_MENU_PATH . 'includes/class-flavor-category-sort.php';
    require_once FLAVOR_MENU_PATH . 'includes/class-flavor-upsell.php';
    require_once FLAVOR_MENU_PATH . 'includes/class-flavor-review-notice.php';
    require_once FLAVOR_MENU_PATH . 'includes/class-flavor-onboarding.php';

    Flavor_CPT::init();
    Flavor_Metaboxes::init();
    Flavor_Shortcode::init();
    Flavor_Category_Sort::init();
    Flavor_Upsell::init();
    Flavor_Review_Notice::init();
    Flavor_Onboarding::init();

    // WPBakery
    if ( defined( 'WPB_VC_VERSION' ) ) {
        require_once FLAVOR_MENU_PATH . 'includes/class-flavor-wpbakery-element.php';
        Flavor_WPBakery_Element::init();
    }

    // Elementor
    add_action( 'elementor/widgets/register', 'flavor_menu_register_elementor_widget' );

    // Gutenberg (Block Editor)
    if ( function_exists( 'register_block_type' ) ) {
        require_once FLAVOR_MENU_PATH . 'includes/class-flavor-block.php';
        Flavor_Block::init();
    }
}
add_action( 'plugins_loaded', 'flavor_menu_init' );

/**
 * Register Elementor widget.
 * Runs on elementor/widgets/register — Elementor's classes are fully loaded at this point.
 */
function flavor_menu_register_elementor_widget( $widgets_manager ) {
    if ( ! class_exists( '\Elementor\Widget_Base' ) ) {
        return;
    }
    require_once FLAVOR_MENU_PATH . 'includes/class-flavor-elementor-widget.php';
    $widgets_manager->register( new Flavor_Elementor_Widget() );
}

/**
 * Enqueue frontend assets.
 */
function flavor_menu_enqueue_assets() {
    wp_register_style(
        'flavor-menu-style',
        FLAVOR_MENU_URL . 'assets/css/flavor-menu.css',
        array(),
        FLAVOR_MENU_VERSION
    );
    wp_register_script(
        'flavor-menu-script',
        FLAVOR_MENU_URL . 'assets/js/flavor-menu.js',
        array(),
        FLAVOR_MENU_VERSION,
        true
    );
}
add_action( 'wp_enqueue_scripts', 'flavor_menu_enqueue_assets' );

/**
 * Enqueue admin assets.
 */
function flavor_menu_admin_assets( $hook ) {
    global $post_type;

    // Post edit screens
    if ( 'flavor_menu_item' === $post_type ) {
        wp_enqueue_style(
            'flavor-menu-admin',
            FLAVOR_MENU_URL . 'assets/css/flavor-menu-admin.css',
            array(),
            FLAVOR_MENU_VERSION
        );
        wp_enqueue_script(
            'flavor-menu-admin',
            FLAVOR_MENU_URL . 'assets/js/flavor-menu-admin.js',
            array( 'jquery', 'jquery-ui-sortable' ),
            FLAVOR_MENU_VERSION,
            true
        );
    }

    // Category sort page
    if ( 'flavor_menu_item_page_flavor-category-sort' === $hook ) {
        wp_enqueue_style(
            'flavor-menu-admin',
            FLAVOR_MENU_URL . 'assets/css/flavor-menu-admin.css',
            array(),
            FLAVOR_MENU_VERSION
        );
        wp_enqueue_script(
            'flavor-cat-sort',
            FLAVOR_MENU_URL . 'assets/js/flavor-category-sort.js',
            array( 'jquery', 'jquery-ui-sortable' ),
            FLAVOR_MENU_VERSION,
            true
        );
        wp_localize_script( 'flavor-cat-sort', 'flavorCatSort', array(
            'ajaxUrl' => admin_url( 'admin-ajax.php' ),
            'nonce'   => wp_create_nonce( 'flavor_cat_sort_nonce' ),
        ) );
    }

    // Upsell admin pages — load base admin CSS
    $flavor_upsell_hooks = array(
        'flavor_menu_item_page_flavor-badges',
        'flavor_menu_item_page_flavor-theme-settings',
        'flavor_menu_item_page_flavor-qr-code',
        'flavor_menu_item_page_flavor-typography',
        'flavor_menu_item_page_flavor-ratings',
        'flavor_menu_item_page_flavor-csv',
    );
    if ( in_array( $hook, $flavor_upsell_hooks, true ) ) {
        wp_enqueue_style(
            'flavor-menu-admin',
            FLAVOR_MENU_URL . 'assets/css/flavor-menu-admin.css',
            array(),
            FLAVOR_MENU_VERSION
        );
    }
}
add_action( 'admin_enqueue_scripts', 'flavor_menu_admin_assets' );

/**
 * Activation hook: flush rewrite rules.
 */
function flavor_menu_activate() {
    require_once FLAVOR_MENU_PATH . 'includes/class-flavor-cpt.php';
    Flavor_CPT::register_post_type();
    Flavor_CPT::register_taxonomy();
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'flavor_menu_activate' );

/**
 * Deactivation hook.
 */
function flavor_menu_deactivate() {
    flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'flavor_menu_deactivate' );

/**
 * Uninstall cleanup — triggered by Freemius after the uninstall event.
 * Removes plugin options and any locally cached font files.
 */
function flavor_fs_uninstall_cleanup() {
    // ── Options ───────────────────────────────────────────────────────────────
    delete_option( 'flavor_review_notice_dismissed' );
    delete_option( 'flavor_review_notice_snooze' );
    delete_option( 'flavor_menu_rendered' );
    delete_option( 'flavor_onboarding_menu_page' );

    // ── Local font cache (uploads/flavor-fonts/) ──────────────────────────────
    $font_dir = wp_upload_dir()['basedir'] . '/flavor-fonts';
    if ( is_dir( $font_dir ) ) {
        // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_glob
        $files = glob( $font_dir . '/*' );
        if ( $files ) {
            foreach ( $files as $file ) {
                if ( is_file( $file ) ) {
                    // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
                    @unlink( $file );
                }
            }
        }
        @rmdir( $font_dir ); // phpcs:ignore WordPress.WP.AlternativeFunctions
    }
}
flavor_fs()->add_action( 'after_uninstall', 'flavor_fs_uninstall_cleanup' );

} // end else (Freemius set_basename)
