<?php

/*
 * Plugin Name:       Minifly
 * Plugin URI:        https://brainywp.com/minifly/
 * Description:       Minifly is your favorite, lightweight companion for better performance. Supercharge your site with tiny tools that make a big difference.
 * Version:           1.2.11
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            Brainywp
 * Author URI:		  https://brainywp.com/minifly/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       minifly
 * Domain Path:       /languages
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'sapmfly_minfly_fs' ) ) {
    sapmfly_minfly_fs()->set_basename( false, __FILE__ );
} else {
    /**
     * DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE
     * `function_exists` CALL ABOVE TO PROPERLY WORK.
     */
    // The key prefix Upper-case: SAPMFLY, lower-case: sapmfly
    if ( !function_exists( 'sapmfly_minfly_fs' ) ) {
        // Create a helper function for easy SDK access.
        function sapmfly_minfly_fs() {
            global $sapmfly_minfly_fs;
            if ( !isset( $sapmfly_minfly_fs ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $sapmfly_minfly_fs = fs_dynamic_init( array(
                    'id'               => '19727',
                    'slug'             => 'minifly',
                    'premium_slug'     => 'minifly-pro',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_7d953f5ede969245ca187b715d166',
                    'is_premium'       => false,
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'menu'             => array(
                        'slug'    => 'minifly',
                        'support' => false,
                        'contact' => false,
                    ),
                    'is_live'          => true,
                    'is_org_compliant' => true,
                ) );
            }
            return $sapmfly_minfly_fs;
        }

        // Init Freemius.
        sapmfly_minfly_fs();
        // Signal that SDK was initiated.
        do_action( 'sapmfly_minfly_fs_loaded' );
    }
    // Define constants
    $sapmfly_plugin_data = get_file_data( __FILE__, array(
        'Version' => 'Version',
    ) );
    define( 'SAPMFLY_VERSION', $sapmfly_plugin_data['Version'] );
    define( 'SAPMFLY_ASSETS', plugin_dir_url( __FILE__ ) . 'assets/' );
    define( 'SAPMFLY_TEMPLATES', plugin_dir_path( __FILE__ ) . 'templates/' );
    // Enqueue admin CSS and JS files
    function sapmfly_enqueue_admin_assets() {
        // Enqueue CSS file
        wp_enqueue_style(
            'sapmfly-global-style',
            SAPMFLY_ASSETS . 'css/global-style.css',
            [],
            SAPMFLY_VERSION,
            'all'
        );
        wp_enqueue_style(
            'sapmfly-admin-style',
            SAPMFLY_ASSETS . 'css/admin-style.css',
            [],
            SAPMFLY_VERSION,
            'all'
        );
        // Enqueue JavaScript file
        wp_enqueue_script(
            'sapmfly-global-script',
            SAPMFLY_ASSETS . 'js/global-script.js',
            ['jquery'],
            SAPMFLY_VERSION,
            true
        );
        wp_enqueue_script(
            'sapmfly-admin-script',
            SAPMFLY_ASSETS . 'js/admin-script.js',
            [],
            SAPMFLY_VERSION,
            true
        );
    }

    add_action( 'admin_enqueue_scripts', 'sapmfly_enqueue_admin_assets' );
    // Enqueue Front End CSS and JS files
    function sapmfly_enqueue_user_assets() {
        // Enqueue CSS file
        wp_enqueue_style(
            'sapmfly-user-style',
            SAPMFLY_ASSETS . 'css/user-style.css',
            [],
            SAPMFLY_VERSION,
            'all'
        );
        // Enqueue JavaScript file
        wp_enqueue_script(
            'sapmfly-user-script',
            SAPMFLY_ASSETS . 'js/user-script.js',
            [],
            SAPMFLY_VERSION,
            true
        );
    }

    add_action( 'wp_enqueue_scripts', 'sapmfly_enqueue_user_assets' );
    // Admin dashboard main menu hook
    add_action( 'admin_menu', 'sapmfly_register_menus' );
    /**
     * Register the Minifly menu and submenus
     */
    function sapmfly_register_menus() {
        $sapmfly_capability = 'manage_options';
        $sapmfly_parent_slug = 'minifly';
        // Conditional menu for upgrade to pro or license page
        if ( !function_exists( 'is_plugin_active' ) ) {
            include_once ABSPATH . 'wp-admin/includes/plugin.php';
        }
        $sapmfly_pro_plugin_file = 'minifly-pro/minifly-pro.php';
        $sapmfly_pro_active = is_plugin_active( $sapmfly_pro_plugin_file );
        // Plugin top-level menu
        add_menu_page(
            esc_html__( 'Minifly', 'minifly' ),
            esc_html__( 'Minifly', 'minifly' ),
            $sapmfly_capability,
            $sapmfly_parent_slug,
            'sapmfly_admin_setup',
            'dashicons-airplane',
            6
        );
        // Submenus
        add_submenu_page(
            $sapmfly_parent_slug,
            esc_html__( 'Admin', 'minifly' ),
            esc_html__( 'Admin', 'minifly' ),
            $sapmfly_capability,
            $sapmfly_parent_slug,
            'sapmfly_admin_setup'
        );
        add_submenu_page(
            $sapmfly_parent_slug,
            esc_html__( 'Website', 'minifly' ),
            esc_html__( 'Website', 'minifly' ),
            $sapmfly_capability,
            $sapmfly_parent_slug . '-user',
            'sapmfly_user_setup'
        );
        add_submenu_page(
            $sapmfly_parent_slug,
            esc_html__( 'eCommerce', 'minifly' ),
            esc_html__( 'eCommerce', 'minifly' ),
            $sapmfly_capability,
            $sapmfly_parent_slug . '-store',
            'sapmfly_ecommerce_setup'
        );
        add_submenu_page(
            $sapmfly_parent_slug,
            esc_html__( 'White Label', 'minifly' ),
            // Page title always same
            esc_html__( 'White Label', 'minifly' ),
            // Page title always same
            $sapmfly_capability,
            $sapmfly_parent_slug . '-white-label',
            'sapmfly_white_label'
        );
        add_submenu_page(
            $sapmfly_parent_slug,
            esc_html__( 'Configuration', 'minifly' ),
            esc_html__( 'Configuration', 'minifly' ),
            $sapmfly_capability,
            $sapmfly_parent_slug . '-configuration',
            'sapmfly_configuration'
        );
        add_submenu_page(
            $sapmfly_parent_slug,
            esc_html__( 'Help Center', 'minifly' ),
            esc_html__( 'Help Center', 'minifly' ),
            $sapmfly_capability,
            $sapmfly_parent_slug . '-help-center',
            '__return_null'
        );
        add_submenu_page(
            $sapmfly_parent_slug,
            esc_html__( 'Power Tools', 'minifly' ),
            esc_html__( 'Power Tools', 'minifly' ),
            $sapmfly_capability,
            $sapmfly_parent_slug . '-more-tools',
            'sapmfly_advanced_tools'
        );
    }

    // Include all the template file here
    $sapmfly_templates = [
        'help-center.php',
        'advanced-tools.php',
        'user/user-setup.php',
        'admin/admin-settings.php',
        // configuration
        'configuration/config-design.php',
        'ecommerce/ecommerce-settings.php',
        'ecommerce/ecommerce-hooks.php',
        // White label options
        'admin/all-admin-hooks.php',
        'admin/switch-user-hook.php',
        'white-label/white-label.php',
        'white-label/white-label-hooks.php',
        'extra-hooks.php',
    ];
    foreach ( $sapmfly_templates as $sapmfly_template ) {
        $sapmfly_template_path = SAPMFLY_TEMPLATES . $sapmfly_template;
        if ( file_exists( $sapmfly_template_path ) ) {
            require_once $sapmfly_template_path;
        }
    }
    // Add some update while active the plugin
    register_activation_hook( __FILE__, 'sapmfly_set_default_back_top_color' );
    function sapmfly_set_default_back_top_color() {
        $currentbg = get_option( 'sapmfly_back_top_bg_color' );
        $currenthv = get_option( 'sapmfly_back_top_hv_color' );
        if ( empty( $currentbg ) ) {
            update_option( 'sapmfly_back_top_bg_color', '#4CAF50' );
        }
        if ( empty( $currenthv ) ) {
            update_option( 'sapmfly_back_top_hv_color', '#4CAF50' );
        }
    }

    // RestAPI related code
    /**
     * Register a custom REST API endpoint
     */
    add_action( 'rest_api_init', function () {
        register_rest_route( 'myplugin/v1', '/info', array(
            'methods'             => 'GET',
            'callback'            => 'sapmfly_myplugin_get_info',
            'permission_callback' => '__return_true',
        ) );
    } );
    /**
     * Callback function to return plugin data
     */
    function sapmfly_myplugin_get_info(  $request  ) {
        // Path to this plugin's main file
        $sapmfly_plugin_file = plugin_basename( __FILE__ );
        // Get all plugin data from header
        if ( !function_exists( 'get_plugin_data' ) ) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }
        $sapmfly_plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $sapmfly_plugin_file );
        return array(
            'site_url'       => get_site_url(),
            'plugin_name'    => $sapmfly_plugin_data['Name'],
            'plugin_version' => $sapmfly_plugin_data['Version'],
        );
    }

}