<?php
/*
	Plugin Name: WooCommerce Easy Checkout Field Editor, Fees & Discounts
	Plugin URI: https://www.sysbasics.com/product/woocommerce-easy-checkout-field-editor/
	Description: lets you Add/edit/delete checkout fields for woocoomerce. 
    Version: 3.8.0
	Author: SysBasics
	Author URI: https://sysbasics.com
	Text Domain: pcfme
	Domain Path: /languages
	Requires at least: 3.3
    Tested up to: 6.5.4
    WC requires at least: 3.0.0
    WC tested up to: 9.0.0
*/

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly


 if( !defined( 'pcfme_PLUGIN_URL' ) )
define( 'pcfme_PLUGIN_URL', plugin_dir_url( __FILE__ ) );



load_plugin_textdomain( 'pcfme', false, basename( dirname(__FILE__) ).'/languages' );

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

if (is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
	
  include dirname( __FILE__ ) . '/include/pcmfe_core_functions.php';
  include dirname( __FILE__ ) . '/include/update_checkout_fields_class.php';
  include dirname( __FILE__ ) . '/include/add_order_meta_fields_class.php';
  include dirname( __FILE__ ) . '/include/manage_extrafield_class.php';
  include dirname( __FILE__ ) . '/include/add_fields_to_myaccount.php';
  include dirname( __FILE__ ) . '/include/admin/pcfme_admin_settings.php';
  include dirname( __FILE__ ) . '/easy-checkout-file-upload/easy-checkout-file-upload.php';

}

add_action( 'before_woocommerce_init', function() {
    if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
    }
} );

 
function pcfme_plugin_row_meta( $links, $file ) {    
    if ( plugin_basename( __FILE__ ) == $file ) {
        $row_meta = array(
          'docs'    => '<a href="' . esc_url( 'https://woomatrix.com/knowledge-base/category/woocommerce-easy-checkout-field-editor/' ) . '" target="_blank" aria-label="' . esc_html__( 'Docs', 'pcfme' ) . '" style="color:green;">' . esc_html__( 'Docs', 'pcfme' ) . '</a>',
          'support'    => '<a href="' . esc_url( 'https://sysbasics.com/support/' ) . '" target="_blank" aria-label="' . esc_html__( 'Support', 'pcfme' ) . '" style="color:green;">' . esc_html__( 'Support', 'pcfme' ) . '</a>'
        );

 
        return array_merge( $links, $row_meta );
    }
    return (array) $links;
}



register_activation_hook(__FILE__, 'pcfme_plugin_activation_hook');

add_action('admin_init', 'pcfme_admin_plugin_redirect');

/**
 * Get account menu item classes.
 *
 * @since 1.0.0
 */

if (!function_exists('pcfme_plugin_activation_hook')) {

    function pcfme_plugin_activation_hook() {

        // Don't forget to exit() because wp_redirect doesn't exit automatically
        add_option('pcfme_do_activation_redirect', true);

    }

}


/**
 * Get account menu item classes.
 *
 * @since 1.0.0
 */

if (!function_exists('pcfme_admin_plugin_redirect')) {

    function pcfme_admin_plugin_redirect() {

        if (get_option('pcfme_do_activation_redirect', false)) {
            delete_option('pcfme_do_activation_redirect');
            wp_redirect("admin.php?page=pcfme_plugin_options");
         //wp_redirect() does not exit automatically and should almost always be followed by exit.
            exit;
        }

    }

}


add_filter( 'plugin_row_meta', 'pcfme_plugin_row_meta', 10, 2 );

register_activation_hook( __FILE__, 'pcfme_subscriber_check_activation_hook' );
function pcfme_subscriber_check_activation_hook() {
    set_transient( 'pcfme-admin-notice-activation', true, 5 );
}

$plugin_data = get_plugin_data( __FILE__ );
$plugin_version = $plugin_data['Version'];


require 'libupdate/libupdate.php';
use YahnisElsts\PluginUpdateChecker\v5\PucFactory;

$myUpdateChecker = PucFactory::buildUpdateChecker(
    'https://www.sysbasics.com/updates2/?action=get_metadata&slug=phppoet-checkout-fields',
    __FILE__, //Full path to the main plugin file or functions.php.
    'phppoet-checkout-fields',
    48
);




$myUpdateChecker->addFilter('pre_inject_update', function($metadata) { 



    $pcfme_install_e = get_option('pcfme_install_e');

    

    if (isset($pcfme_install_e) && ($pcfme_install_e == "64")) {

        $pcfme_license_settings    = (array) get_option('pcfme_license_settings');

        $license_key = '';

        if (isset($pcfme_license_settings['license_key']) ) { 
            $license_key=$pcfme_license_settings['license_key']; 
        }

        $input = $_SERVER['SERVER_NAME'];


        $input = trim($input, '/');


        if (!preg_match('#^http(s)?://#', $input)) {
            $input = 'http://' . $input;
        }

        $urlParts = parse_url($input);


        $domain_name = preg_replace('/^www\./', '', $urlParts['host']);


        $siteurl = pcfme_get_siteurl();


        
        $metadata->download_url = 'https://www.sysbasics.com/updates2/?action=download&slug=phppoet-checkout-fields&domain='.$domain_name.'&code='.$license_key.'&siteurl='.$siteurl.'';

        

        return $metadata;


    } else {
        $metadata->download_url = '';

       
        return $metadata; 
    }
    
     
    return $metadata; 

    }
);

?>