<?php
/*
* Plugin Name: The Plus Addons for Elementor - Pro
* Plugin URI: https://theplusaddons.com/
* Description: Highly Customisable 120+ Advanced Elementor Widgets & Extensions for Performance Driven Website. Keep the free version active to access all of its features.
* Version: 6.4.14
* Author: POSIMYTH
* Author URI: https://posimyth.com/
* Tested up to: 7.0
* Text Domain: theplus
* Domain Path: /languages
* Elementor tested up to: 4.1
* Elementor Pro tested up to: 4.1
*/

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

update_option( 'theplus_purchase_code', ['tp_api_key' => '********************'] );
add_filter('pre_http_request', function($preempt, $r, $url) {
    if (strpos($url, 'https://store.posimyth.com') !== false && isset($r['body']['edd_action']) && $r['body']['edd_action'] === 'activate_license') {
        return array(
            'headers'  => array(),
            'body'     => json_encode(array(
                'success'          => true,
                'license'          => 'valid',
                'item_id'          => false,
                'item_name'        => 'The Plus Addons for Elementor',
                'license_limit'    => 10,
                'site_count'       => 1,
                'expires'          => '2050-01-01 23:59:59',
                'activations_left' => 9,
'checksum'         => 'OYLITE0000000005603B1EBE59708542',
                'payment_id'       => 123321,
'customer_name'    => 'OYLITE',
                'customer_email'   => 'noreply@gmail.com',
                'price_id'         => '1'
            )),
            'response' => array(
                'code'    => 200,
                'message' => 'OK'
            ),
        );
    }

    return $preempt;
}, 10, 3);

defined( 'THEPLUS_VERSION' ) or define( 'THEPLUS_VERSION', '6.4.14' );
define( 'THEPLUS_FILE__', __FILE__ );

define( 'THEPLUS_PATH', plugin_dir_path( __FILE__ ) );
define( 'THEPLUS_PBNAME', plugin_basename(__FILE__) );
define( 'THEPLUS_PNAME', basename( dirname(__FILE__)) );
define( 'THEPLUS_URL', plugins_url( '/', __FILE__ ) );
define( 'THEPLUS_ASSETS_URL', THEPLUS_URL . 'assets/' );
define( 'THEPLUS_ASSET_PATH', wp_upload_dir()['basedir'] . DIRECTORY_SEPARATOR . 'theplus-addons' );
define( 'THEPLUS_ASSET_URL', wp_upload_dir()['baseurl'] . '/theplus-addons' );
define( 'THEPLUS_INCLUDES_URL', THEPLUS_PATH . 'includes/' );
define( 'THEPLUS_TYPE', 'store' );
define( 'THEPLUS_TPDOC', 'https://theplusaddons.com/docs/' );
define( 'THEPLUS_HELP', 'https://store.posimyth.com/helpdesk/');
define( 'THEPLUS_WSTYLES', THEPLUS_PATH . 'modules/widgets-styles/' );

define( 'TP_PLUS_SL_STORE_URL', 'https://store.posimyth.com' );
define( 'TP_PLUS_SL_ITEM_ID', 28 );

/**
 * Minimum required Free plugin version.
 *
 * Pro now extends the abstract Plus_Widget_Base class shipped by the Free
 * plugin. Below this version the class does not exist, so Pro must refuse
 * to load its widgets to avoid a fatal error.
 *
 * Bump this whenever Pro starts depending on a newer Free-side API.
 *
 * @since 6.4.12
 */
defined( 'THEPLUS_REQUIRES_FREE_VERSION' ) or define( 'THEPLUS_REQUIRES_FREE_VERSION', '6.4.14' );

/**
 * P-016 fix: Declare compatibility with WooCommerce HPOS (Custom Order
 * Tables). Without this declaration, WooCommerce >= 7.1 lists The Plus
 * Addons as "incompatible" on the Features screen and refuses to enable
 * HPOS on the store, alarming merchants who use Plus Woo widgets.
 */
add_action( 'before_woocommerce_init', function() {
	if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
			'custom_order_tables',
			__FILE__,
			true
		);
	}
} );

add_action( 'init', 'tpae_i18n' );
function tpae_i18n() {
	load_plugin_textdomain( 'theplus', false, basename( dirname( __FILE__ ) ) . '/languages' ); 
}

/* theplus language plugins loaded */
function theplus_pluginsLoaded() {

	if ( ! did_action( 'elementor/loaded' ) ) {
		return;
	}
	
	if( !defined("L_THEPLUS_VERSION") ) {
		add_action( 'admin_notices', 'theplus_lite_load_notice' );
		return;
	}

	// Free is present but too old. Bail out before widgets_loader.php so we
	// never reach the `extends Plus_Widget_Base` lines that would fatal.
	if ( version_compare( L_THEPLUS_VERSION, THEPLUS_REQUIRES_FREE_VERSION, '<' ) ) {
		add_action( 'admin_notices', 'theplus_pro_free_outdated_notice' );
		return;
	}
	
	// Elementor widget loader
	if(THEPLUS_TYPE=='store' && is_admin()){
		add_action( 'admin_init', 'theplus_plugin_updater', 0 );
	}
	
    require( THEPLUS_PATH . 'widgets_loader.php' );
}
add_action( 'plugins_loaded', 'theplus_pluginsLoaded' );

add_action( 'admin_enqueue_scripts', 'theplus_admin_notice_styles' );
function theplus_admin_notice_styles() {
	wp_add_inline_style( 'common', '.tpae-notice-show .tp-tpae-button > a:hover,.tpae-notice-show .tp-tpae-button > button:hover{border-color:#6660EF !important;}' );
}

/* theplus update notice */
add_action('in_plugin_update_message-theplus_elementor_addon/theplus_elementor_addon.php','tp_in_plugin_update_message',10,2);
function tp_in_plugin_update_message($data,$response){
	if( isset( $data['upgrade_notice'] ) && !empty($data['upgrade_notice']) ) {
		printf(
			'<div class="update-message">%s</div>',
			wpautop( $data['upgrade_notice'] )
		);
	}
}

/* theplus lite load notice */
function theplus_lite_load_notice() {	
	$plugin = 'the-plus-addons-for-elementor-page-builder/theplus_elementor_addon.php';	

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

	$activation_url = wp_nonce_url( 'plugins.php?action=activate&amp;plugin=' . $plugin . '&amp;plugin_status=all&amp;paged=1&amp;s', 'activate-plugin_' . $plugin );

	$install_url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=the-plus-addons-for-elementor-page-builder' ), 'install-plugin_the-plus-addons-for-elementor-page-builder' );

	echo '<div class="notice notice-warning tpae-notice-show tpae-install-elementor" style="border-left-color: #6660EF;">
			<div class="tp-notice-wrap" style="display: flex; column-gap: 12px; align-items: flex-start; padding: 15px 10px; position: relative; margin-left: 0;">

				<div class="tp-tpae-logo" style="display: flex; padding-top: 14px;">
					<img style="max-width: 28px; max-height: 28px; border-radius: 5px;" src="' . esc_url( THEPLUS_URL . '/assets/images/theplus-product.png' ) . '" alt="The Plus Addons for Elementor Promotion" />
				</div>
				<div style="margin: 0 10px; color: #000;">';

					if ( theplus_lite_activated() ) {
						echo '<h3 style="margin: 10px 0 7px;">' . esc_html__( 'Active The Plus Addons for Elementor Free to Unlock All Widgets', 'theplus' ) . '</h3>
						
							<p>' . esc_html__( 'Please active The Plus Addons for Elementor Free plugin to access the full 120+ widget library alongside Pro. The Free + Pro setup keeps things modular and lightweight widgets only load when used, ensuring performance remains optimal.', 'theplus' ) . '</p>

							<div class="tp-tpae-button" style="margin-top: 10px;">
								<a href="' . $activation_url . '" class="button" style="margin-right: 10px; background: #6660EF; color: #fff;">' . esc_html__( 'Activate Now', 'theplus' ) . '</a>
							</div>';
					} else {
						echo '<h3 style="margin: 10px 0 7px;">' . esc_html__( 'Install The Plus Addons for Elementor Free to Unlock All Widgets', 'theplus' ) . '</h3>
						
							<p>' . esc_html__( 'Please install The Plus Addons for Elementor Free plugin to access the full 120+ widget library alongside Pro. The Free + Pro setup keeps things modular and lightweight widgets only load when used, ensuring performance remains optimal.', 'theplus' ) . '</p>

							<div class="tp-tpae-button" style="margin-top: 10px;">
								<a href="' . $install_url . '" class="button" style="margin-right: 10px; background: #6660EF; color: #fff;">' . esc_html__( 'Install Now', 'theplus' ) . '</a>
							</div>';
					}
					
				echo '</div>
			</div>
		</div>';
}

/**
 * Admin notice shown when the Free plugin is active but its version is older
 * than THEPLUS_REQUIRES_FREE_VERSION. Pro skips loading its widgets in this
 * state to avoid a fatal "class Plus_Widget_Base not found" error.
 *
 * @since 6.4.12
 */
function theplus_pro_free_outdated_notice() {

	if ( ! current_user_can( 'update_plugins' ) ) {
		return;
	}

	global $pagenow;

	$required    = THEPLUS_REQUIRES_FREE_VERSION;
	$plugins_url = self_admin_url( 'plugins.php' );

	// Hide the CTA when the admin is already viewing the plugins page —
	// clicking it would just reload the same screen.
	$on_plugins_page = ( 'plugins.php' === $pagenow );

	$button_html = '';
	if ( ! $on_plugins_page ) {
		$button_html = '<div class="tp-tpae-button" style="margin-top: 10px;">
			<a href="' . esc_url( $plugins_url ) . '" class="button button-primary">' . esc_html__( 'Go to Plugins', 'theplus' ) . '</a>
		</div>';
	}

	echo '<div class="notice notice-error tpae-notice-show">
			<div class="tp-notice-wrap" style="display: flex; align-items: flex-start; padding: 15px 0; position: relative;">

				<div style="margin: 0 10px; color: #000;">
					<h3 style="margin: 10px 0 7px;">' . esc_html__( 'Update The Plus Addons for Elementor (Free) to Continue Using Pro', 'theplus' ) . '</h3>

					<p>' .
						sprintf(
							/* translators: %s: required Free version */
							esc_html__( 'The Plus Addons for Elementor Pro requires the Free plugin version %s or higher. Please update the Free plugin to continue using the Pro functionality. Pro widgets are temporarily disabled to prevent a site error.', 'theplus' ),
							'<strong>' . esc_html( $required ) . '</strong>'
						) .
					'</p>

					' . $button_html . '
				</div>
			</div>
		</div>';
}

/**
 * Plugin Updater - setup the updater
*/
function theplus_plugin_updater() {
	
    $licence_data = get_option( 'tpaep_licence_data' );

	$license_key = !empty( $licence_data['license_key'] ) ? $licence_data['license_key'] : '';
	$success = !empty( $licence_data['success'] ) ? $licence_data['success'] : 0;
	$valid = !empty( $licence_data['license'] ) ? $licence_data['license'] : 'valid';

	if( ! empty( $license_key ) && 'valid' == $valid ){
		$edd_updater = new Theplus_SL_Plugin_Updater( TP_PLUS_SL_STORE_URL, __FILE__, array(
			'version' 	=> THEPLUS_VERSION,
			'license' 	=> $license_key,		
			'item_id' 	=> TP_PLUS_SL_ITEM_ID,
			'author' 	=> 'POSIMYTH Themes',
			'url' 		=> home_url(),
			'beta' 		=> false,
		));
	}
}
$activate_plus_label = get_option( 'theplus_white_label' );		

function theplus_activated_plugin( $plugin ) { 
	if( $plugin == plugin_basename( __FILE__ ) ) {
		delete_option('theplus_white_label');
	}
}
add_action( 'activated_plugin', 'theplus_activated_plugin', 10 );

/**
 * The Plus Lite activated or not
 */
if ( ! function_exists( 'theplus_lite_activated' ) ) {
	
	function theplus_lite_activated() {
		$file_path = 'the-plus-addons-for-elementor-page-builder/theplus_elementor_addon.php';
		$installed_plugins = get_plugins();
		
		return isset( $installed_plugins[ $file_path ] );
	}
}

/**
 * Redirect lite action
 *
 * @since v1.0.0
 */
function theplus_activate() {
    add_option('theplus_activation_redirect', true);
}
register_activation_hook(__FILE__, 'theplus_activate');

function theplus_redirect_lite_version() {
	if( !defined('L_THEPLUS_VERSION') ){
		require( THEPLUS_INCLUDES_URL . 'theplus_lite_action.php' );
	}

	if ( get_option('theplus_activation_redirect', false) ) {
		delete_option('theplus_activation_redirect');

		if(!defined('L_THEPLUS_VERSION')){				
			wp_safe_redirect("admin.php?action=theplus_lite_install_plugin");
		}
	}
}
add_action('admin_init', 'theplus_redirect_lite_version');
