<?php

/**
 * Plugin Name: PW Loyalty Points and Rewards for WooCommerce
 * Plugin URI: https://www.pimwick.com/pw-loyalty-points-and-rewards-for-woocommerce/
 * Description: Provide your customers with points that can be redeemed for instant rewards.
 * Version: 1.10
 * Author: Pimwick, LLC
 * Author URI: https://www.pimwick.com
 * Text Domain: pw-loyalty-points-and-rewards-for-woocommerce
 * Domain Path: /languages
 * WC requires at least: 4.0
 * WC tested up to: 10.6
 * Requires Plugins: woocommerce
 * License: GPLv2
*/

/*
Copyright (C) Pimwick, LLC

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

defined( 'ABSPATH' ) or exit;

register_activation_hook( __FILE__, function() {
    // Deactivate this plugin if the Pro version is installed.
    if ( class_exists( 'PW_WooCommerce_Loyalty_Points_And_Rewards_Pro' ) ) {
        deactivate_plugins( plugin_basename( __FILE__ ) );
        wp_die( esc_html_e( 'The PW Loyalty Points and Rewards for WooCommerce plugin cannot be activated because the Pro version is already installed. You can safely delete this version.', 'pw-loyalty-points-and-rewards-for-woocommerce' ) );
    }
} );

add_action( 'before_woocommerce_init', function() {
    // WooCommerce High Performance Order Storage (HPOS) compatibility declaration.
    if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
    }
} );

add_action( 'plugins_loaded', function() {
    if ( class_exists( 'PW_WooCommerce_Loyalty_Points_And_Rewards_Pro' ) ) {
        return;
    }

    define( 'PWLPAR_VERSION', '1.10' );

    function pwlpar_define( $constant_name, $default_value ) {
        defined( $constant_name ) or define( $constant_name, $default_value );
    }

    pwlpar_define( 'PWLPAR_PLUGIN_NAME', 'PW Loyalty Points and Rewards for WooCommerce' );
    pwlpar_define( 'PWLPAR_PLUGIN_SHORT_NAME', 'PW Rewards' );
    pwlpar_define( 'PWLPAR_REQUIRES_PRIVILEGE', 'manage_woocommerce' );
    pwlpar_define( 'PWLPAR_WC_VERSION_MINIMUM', '4.0' );
    pwlpar_define( 'PWLPAR_PLUGIN_FILE', __FILE__ );
    pwlpar_define( 'PWLPAR_PLUGIN_DIR', untrailingslashit( plugin_dir_path( PWLPAR_PLUGIN_FILE ) ) );
    pwlpar_define( 'PWLPAR_HOME_SHORTCODE', 'pwlpar_home' );
    pwlpar_define( 'PWLPAR_JOIN_SHORTCODE', 'pwlpar_join' );
    pwlpar_define( 'PWLPAR_DEFAULT_LOGO_INDEX_DARK', 8 );
    pwlpar_define( 'PWLPAR_DEFAULT_LOGO_INDEX_LIGHT', 9 );
    pwlpar_define( 'PWLPAR_DEFAULT_EARN_LOGO_INDEX_DARK', 2 );
    pwlpar_define( 'PWLPAR_DEFAULT_EARN_LOGO_INDEX_LIGHT', 3 );
    pwlpar_define( 'PWLPAR_DEFAULT_OFFER_LOGO_INDEX_DARK', 0 );
    pwlpar_define( 'PWLPAR_DEFAULT_OFFER_LOGO_INDEX_LIGHT', 1 );
    pwlpar_define( 'PWLPAR_USER_META_KEY_IS_MEMBER', 'pwlpar_is_member' );
    pwlpar_define( 'PWLPAR_USER_META_KEY_JOIN_DATE', 'pwlpar_join_date_gmt' );

    class PWLPAR_WooCommerce_Loyalty_Points_And_Rewards {

        public $actions;
        public $admin;
        public $assets;
        public $dashboard;
        public $db;
        public $hooks;
        public $settings;

        function __construct() {
            require_once( PWLPAR_PLUGIN_DIR . '/includes/pwlpar-core-functions.php' );
            require_once( PWLPAR_PLUGIN_DIR . '/includes/pwlpar-earning-functions.php' );
            require_once( PWLPAR_PLUGIN_DIR . '/includes/pwlpar-member-functions.php' );
            require_once( PWLPAR_PLUGIN_DIR . '/includes/pwlpar-offer-functions.php' );
            require_once( PWLPAR_PLUGIN_DIR . '/includes/pwlpar-reward-functions.php' );
            require_once( PWLPAR_PLUGIN_DIR . '/includes/pwlpar-settings-functions.php' );
            require_once( PWLPAR_PLUGIN_DIR . '/includes/pwlpar-template-functions.php' );
            require_once( PWLPAR_PLUGIN_DIR . '/includes/pwlpar-transaction-functions.php' );

            require_once( PWLPAR_PLUGIN_DIR . '/includes/class-pwlpar-member.php' );
            require_once( PWLPAR_PLUGIN_DIR . '/includes/class-pwlpar-offer.php' );
            require_once( PWLPAR_PLUGIN_DIR . '/includes/class-pwlpar-reward.php' );
            require_once( PWLPAR_PLUGIN_DIR . '/includes/class-pwlpar-transaction.php' );

            $this->actions      = require_once( PWLPAR_PLUGIN_DIR . '/includes/class-pwlpar-actions.php' );
            $this->assets       = require_once( PWLPAR_PLUGIN_DIR . '/includes/class-pwlpar-assets.php' );
            $this->dashboard    = require_once( PWLPAR_PLUGIN_DIR . '/includes/class-pwlpar-dashboard.php' );
            $this->db           = require_once( PWLPAR_PLUGIN_DIR . '/includes/class-pwlpar-db.php' );
            $this->hooks        = require_once( PWLPAR_PLUGIN_DIR . '/includes/class-pwlpar-hooks.php' );
            $this->settings     = require_once( PWLPAR_PLUGIN_DIR . '/includes/class-pwlpar-settings.php' );

            register_deactivation_hook( PWLPAR_PLUGIN_FILE, array( $this, 'plugin_deactivate' ) );

            add_action( 'pwlpar_installed', array( $this, 'maybe_create_pages' ) );
            add_action( 'woocommerce_create_page_id', array( $this, 'woocommerce_create_page_id' ), 10, 3 );
            add_action( 'pwlpar_installed', array( $this, 'flush_rewrite_rules' ) );
            add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ), 900 );
            add_action( 'woocommerce_init', array( $this, 'woocommerce_init' ) );
        }

        function flush_rewrite_rules() {
            flush_rewrite_rules();
        }

        function plugin_deactivate() {
            if ( ! current_user_can( 'activate_plugins' ) ) {
                return;
            }

            delete_option( 'pwlpar_version' );

            flush_rewrite_rules();
        }

        function wp_enqueue_scripts() {
            wp_enqueue_style( 'pwlpar-header' );

            wp_enqueue_style( 'pwlpar-offer' );
            wp_enqueue_script( 'pwlpar-offer' );

            wp_enqueue_style( 'pwlpar-reward' );
            wp_enqueue_script( 'pwlpar-reward' );

            wp_enqueue_style( 'pwlpar-frontend' );
            wp_enqueue_script( 'pwlpar-frontend' );
        }

        function woocommerce_init() {
            $pwlpar_version = get_option( 'pwlpar_version' );
            if ( $pwlpar_version != PWLPAR_VERSION ) {

                if ( empty( $pwlpar_version ) ) {
                    do_action( 'pwlpar_installed' );
                } else {
                    do_action( 'pwlpar_updated' );
                }

                update_option( 'pwlpar_version', PWLPAR_VERSION );

            }

            if ( is_admin() ) {
                $this->admin = require_once( PWLPAR_PLUGIN_DIR . '/admin/class-pwlpar-admin.php' );

                add_action( 'pimwick_system_status_report', function() { pimwick_system_status_report_license_info( __FILE__ ); } );
            }
        }

        function maybe_create_pages() {

            include_once dirname( WC_PLUGIN_FILE ) . '/includes/admin/wc-admin-functions.php';

            // Set the locale to the store locale to ensure pages are created in the correct language.
            wc_switch_to_site_locale();

            $home_page_content = '<!-- wp:shortcode -->[' . PWLPAR_HOME_SHORTCODE . ']<!-- /wp:shortcode -->';

            $join_page_content = '<!-- wp:paragraph --><p>' . __( "Sign up for our free program and you will <strong>earn 1 Point for every $1 spent</strong>, receive exclusive offers, and more!", 'pw-loyalty-points-and-rewards-for-woocommerce' ) . '</p><!-- /wp:paragraph -->';
            $join_page_content .= '<!-- wp:shortcode -->[' . PWLPAR_JOIN_SHORTCODE . ']<!-- /wp:shortcode -->';

            $pages = apply_filters(
                'pwlpar_create_pages',
                array(
                    'home'           => array(
                        'name'    => _x( 'rewards', 'Page slug', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                        'title'   => _x( 'Rewards', 'Page title', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                        'content' => $home_page_content,
                    ),
                    'join'           => array(
                        'name'    => _x( 'join-rewards', 'Page slug', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                        'title'   => _x( 'Join Rewards', 'Page title', 'pw-loyalty-points-and-rewards-for-woocommerce' ),
                        'content' => $join_page_content,
                    ),
                )
            );

            foreach ( $pages as $key => $page ) {
                $result = wc_create_page(
                    esc_sql( $page['name'] ),
                    'pwlpar_' . $key . '_page_id',
                    $page['title'],
                    $page['content'],
                    ! empty( $page['parent'] ) ? wc_get_page_id( $page['parent'] ) : '',
                    ! empty( $page['post_status'] ) ? $page['post_status'] : 'publish'
                );
            }

            // Restore the locale to the default locale.
            wc_restore_locale();
        }

        function woocommerce_create_page_id( $valid_page_found, $slug, $page_content ) {
            global $wpdb;

            // Fixes an issue where wc_create_page() searched by the content OR the slug. Prevents it from finding existing join-rewards page and then we create a duplicate.
            if ( !$valid_page_found && $slug == 'join-rewards' ) {
                // Direct query is done to find the specific page ID during setup.
                // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
                $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' )  AND post_name = %s LIMIT 1;", $slug ) );
            }

            return $valid_page_found;
        }
    }

    global $pwlpar;
    $pwlpar = new PWLPAR_WooCommerce_Loyalty_Points_And_Rewards();

    global $pwlpardb;
    $pwlpardb = $pwlpar->db;
} );
