<?php

/**
 * Plugin Name:       Product Price History
 * Plugin URI:        https://devnet.hr/plugins/product-price-history/
 * Description:       Price history tracker for WooCommerce products.
 * Version:           2.6.3
 * Requires at least: 6.4
 * Requires PHP:      7.4
 * Author:            Devnet
 * Author URI:        https://devnet.hr
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       product-price-history
 * Domain Path:       /languages
 * Requires Plugins:  woocommerce
 * WC tested up to:   10.7
 *
 */
use Devnet\PPH\Includes\Activator;
use Devnet\PPH\Includes\Deactivator;
use Devnet\PPH\Includes\PPH_PLUGIN;
use Devnet\PPH\Includes\Uninstaller;
// If this file is called directly, abort.
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'pph_fs' ) ) {
    pph_fs()->set_basename( false, __FILE__ );
} else {
    if ( !function_exists( 'pph_fs' ) ) {
        // Create a helper function for easy SDK access.
        function pph_fs() {
            global $pph_fs;
            if ( !isset( $pph_fs ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/vendor/freemius/wordpress-sdk/start.php';
                $pph_fs = fs_dynamic_init( [
                    'id'               => '12131',
                    'slug'             => 'product-price-history',
                    'premium_slug'     => 'product-price-history-pro',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_810eeff02bc575018794400239eb0',
                    'is_premium'       => false,
                    'premium_suffix'   => '(Pro)',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'trial'            => [
                        'days'               => 7,
                        'is_require_payment' => true,
                    ],
                    'menu'             => [
                        'slug'    => 'product-price-history',
                        'contact' => false,
                    ],
                    'is_live'          => true,
                ] );
            }
            return $pph_fs;
        }

        // Init Freemius.
        pph_fs();
        // Signal that SDK was initiated.
        do_action( 'pph_fs_loaded' );
    }
    /**
     * Show the contact submenu item only when the user have a valid non-expired license.
     *
     * @param $is_visible The filtered value. Whether the submenu item should be visible or not.
     * @param $menu_id    The ID of the submenu item.
     *
     * @return bool If true, the menu item should be visible.
     */
    if ( !function_exists( 'pph_is_submenu_visible' ) ) {
        function pph_is_submenu_visible(  $is_visible, $menu_id  ) {
            if ( 'contact' != $menu_id ) {
                return $is_visible;
            }
            return pph_fs()->can_use_premium_code();
        }

    }
    /*
     * TODO: do uninstall logic.
     */
    if ( !function_exists( 'pph_fs_uninstall_cleanup' ) ) {
        function pph_fs_uninstall_cleanup() {
            require_once plugin_dir_path( __FILE__ ) . 'includes/uninstaller.php';
            Uninstaller::cleanup();
        }

    }
    if ( !function_exists( 'pph_fs_custom_icon' ) ) {
        function pph_fs_custom_icon() {
            return dirname( __FILE__ ) . '/assets/images/logo.png';
        }

    }
    /*
     * Run Freemius actions and filters.
     */
    if ( function_exists( 'pph_fs' ) ) {
        pph_fs()->add_filter(
            'is_submenu_visible',
            'pph_is_submenu_visible',
            10,
            2
        );
        pph_fs()->add_action( 'after_uninstall', 'pph_fs_uninstall_cleanup' );
        pph_fs()->add_filter( 'plugin_icon', 'pph_fs_custom_icon' );
    }
    define( 'PRODUCT_PRICE_HISTORY_VERSION', '2.6.3' );
    define( 'DEVNET_PPH_NAME', 'Product Price History' );
    define( 'DEVNET_PPH_SLUG', plugin_basename( __FILE__ ) );
    define( 'DEVNET_PPH_OPTIONS', [
        'general'      => get_option( 'devnet_pph_general' ),
        'chart'        => get_option( 'devnet_pph_chart' ),
        'lowest_price' => get_option( 'devnet_pph_lowest_price' ),
    ] );
    function pph_activate_product_price_history() {
        require_once plugin_dir_path( __FILE__ ) . 'includes/activator.php';
        Activator::activate();
    }

    function pph_deactivate_product_price_history() {
        require_once plugin_dir_path( __FILE__ ) . 'includes/deactivator.php';
        Deactivator::deactivate();
    }

    register_activation_hook( __FILE__, 'pph_activate_product_price_history' );
    register_deactivation_hook( __FILE__, 'pph_deactivate_product_price_history' );
    add_action( 'plugins_loaded', function () {
        require plugin_dir_path( __FILE__ ) . 'includes/plugin.php';
        $plugin = new PPH_Plugin();
        $plugin->run();
    } );
}