<?php
/**
* The WooCommerce API Manager PHP Client Library is designed to be droppped into a WordPress plugin or theme.
* This version is designed to be used with the WooCommerce API Manager version 2.x.
*
* Intellectual Property rights, and copyright, reserved by Todd Lahman, LLC as allowed by law include,
* but are not limited to, the working concept, function, and behavior of this software,
* the logical code structure and expression as written.
*
* @package WooCommerce API Manager plugin and theme library
* @author Todd Lahman LLC https://www.toddlahman.com/
* @copyright Copyright (c) Todd Lahman LLC (support@toddlahman.com)
* @since 2.5
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WC_AM_Client_25' ) ) {
class WC_AM_Client_25 {
/**
* Class args
*
* @var string
*/
public $api_url = '';
public $data_key = '';
public $file = '';
public $plugin_name = '';
public $plugin_or_theme = '';
public $product_id = '';
public $slug = '';
public $software_title = '';
public $software_version = '';
public $text_domain = ''; // For language translation.
/**
* Class properties.
*
* @var string
*/
public $data = array();
public $identifier = '';
public $no_product_id = false;
public $product_id_chosen = 0;
public $wc_am_activated_key = '';
public $wc_am_activation_tab_key = '';
public $wc_am_api_key_key = '';
public $wc_am_deactivate_checkbox_key = '';
public $wc_am_deactivation_tab_key = '';
public $wc_am_domain = '';
public $wc_am_instance_id = '';
public $wc_am_instance_key = '';
public $wc_am_menu_tab_activation_title = '';
public $wc_am_menu_tab_deactivation_title = '';
public $wc_am_plugin_name = '';
public $wc_am_product_id = '';
public $wc_am_renew_license_url = '';
public $wc_am_settings_menu_title = '';
public $wc_am_settings_title = '';
public $wc_am_software_version = '';
public function __construct( $file, $product_id, $software_version, $plugin_or_theme, $api_url, $software_title = '', $text_domain = '' ) {
$this->no_product_id = empty( $product_id ) ? true : false;
if ( $this->no_product_id ) {
$this->identifier = dirname( untrailingslashit( plugin_basename( $file ) ) );
$product_id = strtolower( str_ireplace( array( ' ', '_', '&', '?', '-' ), '_', $this->identifier ) );
$this->wc_am_product_id = 'wc_am_product_id_' . $product_id;
$this->product_id_chosen = get_option( $this->wc_am_product_id );
} else {
/**
* Preserve the value of $product_id to use for API requests. Pre 2.0 product_id is a string, and >= 2.0 is an integer.
*/
if ( is_int( $product_id ) ) {
$this->product_id = absint( $product_id );
} else {
$this->product_id = esc_attr( $product_id );
}
}
// If the product_id was not provided, but was saved by the customer, used the saved product_id.
if ( empty( $this->product_id ) && ! empty( $this->product_id_chosen ) ) {
$this->product_id = $this->product_id_chosen;
}
$this->file = $file;
$this->software_title = esc_attr( $software_title );
$this->software_version = esc_attr( $software_version );
$this->plugin_or_theme = esc_attr( $plugin_or_theme );
$this->api_url = esc_url( $api_url );
$this->text_domain = esc_attr( $text_domain );
/**
* If the product_id is a pre 2.0 string, format it to be used as an option key, otherwise it will be an integer if >= 2.0.
*/
$this->data_key = 'wc_am_client_' . strtolower( str_ireplace( array( ' ', '_', '&', '?', '-' ), '_', $product_id ) );
$this->wc_am_activated_key = $this->data_key . '_activated';
if ( is_admin() ) {
if ( ! empty( $this->plugin_or_theme ) && $this->plugin_or_theme == 'theme' ) {
add_action( 'admin_init', array( $this, 'activation' ) );
}
if ( ! empty( $this->plugin_or_theme ) && $this->plugin_or_theme == 'plugin' ) {
register_activation_hook( $this->file, array( $this, 'activation' ) );
}
add_action( 'admin_menu', array( $this, 'register_menu' ) );
add_action( 'admin_init', array( $this, 'load_settings' ) );
// Check for external connection blocking
add_action( 'admin_notices', array( $this, 'check_external_blocking' ) );
/**
* Set all data defaults here
*/
$this->wc_am_api_key_key = $this->data_key . '_api_key';
$this->wc_am_instance_key = $this->data_key . '_instance';
/**
* Set all admin menu data
*/
$this->wc_am_deactivate_checkbox_key = $this->data_key . '_deactivate_checkbox';
$this->wc_am_activation_tab_key = $this->data_key . '_dashboard';
$this->wc_am_deactivation_tab_key = $this->data_key . '_deactivation';
$this->wc_am_settings_menu_title = $this->software_title . esc_html__( ' Activation', $this->text_domain );
$this->wc_am_settings_title = $this->software_title . esc_html__( ' API Key Activation', $this->text_domain );
$this->wc_am_menu_tab_activation_title = esc_html__( 'API Key Activation', $this->text_domain );
$this->wc_am_menu_tab_deactivation_title = esc_html__( 'API Key Deactivation', $this->text_domain );
/**
* Set all software update data here
*/
$this->data = get_option( $this->data_key );
$this->wc_am_plugin_name = $this->plugin_or_theme == 'plugin' ? untrailingslashit( plugin_basename( $this->file ) ) : get_stylesheet(); // same as plugin slug. if a theme use a theme name like 'twentyeleven'
$this->wc_am_renew_license_url = $this->api_url . 'my-account'; // URL to renew an API Key. Trailing slash in the upgrade_url is required.
$this->wc_am_instance_id = get_option( $this->wc_am_instance_key ); // Instance ID (unique to each blog activation)
/**
* Some web hosts have security policies that block the : (colon) and // (slashes) in http://,
* so only the host portion of the URL can be sent. For example the host portion might be
* www.example.com or example.com. http://www.example.com includes the scheme http,
* and the host www.example.com.
* Sending only the host also eliminates issues when a client site changes from http to https,
* but their activation still uses the original scheme.
* To send only the host, use a line like the one below:
*
* $this->wc_am_domain = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); // blog domain name
*/
$this->wc_am_domain = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); // blog domain name
$this->wc_am_software_version = $this->software_version; // The software version
// Check if data has been migrated from pre-2.0.
$this->migrate_pre_2_0_data( $product_id, $software_title );
/**
* Check for software updates
*/
$this->check_for_update();
if ( ! empty( $this->wc_am_activated_key ) && get_option( $this->wc_am_activated_key ) != 'Activated' ) {
add_action( 'admin_notices', array( $this, 'inactive_notice' ) );
}
}
/**
* Deletes all data if plugin deactivated
*/
if ( $this->plugin_or_theme == 'plugin' ) {
register_deactivation_hook( $this->file, array( $this, 'uninstall' ) );
}
if ( $this->plugin_or_theme == 'theme' ) {
add_action( 'switch_theme', array( $this, 'uninstall' ) );
}
}
/**
* Migrates pre 2.0 data to prevent breaking old software activations.
*
* @since 2.0
*
* @param int $product_id
* @param string $software_title
*/
public function migrate_pre_2_0_data( $product_id, $software_title ) {
$upraded_postfix = strtolower( str_ireplace( array( ' ', '_', '&', '?', '-' ), '_', $product_id ) );
$upraded = get_option( 'wc_client_20_ugrade_attempt_' . $upraded_postfix );
if ( $upraded != 'yes' ) {
$title = is_int( $product_id ) ? strtolower( $software_title ) : strtolower( $product_id );
$title = str_ireplace( array( ' ', '_', '&', '?' ), '_', $title );
$old_data_key = $title . '_data';
$data = get_option( $old_data_key );
$instance = get_option( $title . '_instance' );
if ( ! empty( $data ) && ! empty( $instance ) ) {
$api_key = array(
$this->wc_am_api_key_key => $data[ 'api_key' ],
);
update_option( $this->data_key, $api_key );
update_option( $this->wc_am_instance_key, $instance );
! empty( $instance ) ? update_option( $this->wc_am_deactivate_checkbox_key, 'off' ) : update_option( $this->wc_am_deactivate_checkbox_key, 'on' );
! empty( $instance ) ? update_option( $this->wc_am_activated_key, 'Activated' ) : update_option( $this->wc_am_activated_key, 'Deactivated' );
// Success!
update_option( 'wc_client_20_ugrade_attempt_' . $upraded_postfix, 'yes' );
} else {
if ( empty( $this->wc_am_instance_id ) ) {
// Failed migration. :( Cue the violins to play a sad song.
add_action( 'admin_notices', array( $this, 'migrate_error_notice' ) );
}
}
}
}
/**
* Provides one-time instructions for customer to reactivate the API Key if the migration fails.
*
* @since 2.0
*/
public function migrate_error_notice() { ?>
<div class="notice notice-error">
<p>
<?php esc_html_e( 'Attempt to migrate data failed. Deactivate then reactive this plugin or theme, then enter your API Key on the settings screen to receive software updates. Contact support if assistance is required.', $this->text_domain ); ?>
</p>
</div>
<?php
}
/**
* Register submenu specific to this product.
*/
public function register_menu() {
add_options_page( esc_html__( $this->wc_am_settings_menu_title, $this->text_domain ), esc_html__( $this->wc_am_settings_menu_title, $this->text_domain ), 'manage_options', $this->wc_am_activation_tab_key, array(
$this,
'config_page'
) );
}
/**
* Generate the default data arrays
*/
public function activation() {
if ( get_option( $this->data_key ) === false || get_option( $this->wc_am_instance_key ) === false || empty( get_option( $this->wc_am_instance_key ) ) ) {
//$api_key = array(
// $this->wc_am_api_key_key => '',
//);
//
//update_option( $this->data_key, $api_key );
update_option( $this->wc_am_instance_key, wp_generate_password( 12, false ) );
update_option( $this->wc_am_deactivate_checkbox_key, 'on' );
update_option( $this->wc_am_activated_key, 'Deactivated' );
}
}
/**
* Deletes all data if plugin deactivated
*/
public function uninstall() {
if ( apply_filters( 'wc_am_uninstall_disable', true ) ) {
return;
}
global $blog_id;
$this->license_key_deactivation();
// Remove options pre API Manager 2.0
if ( is_multisite() ) {
switch_to_blog( $blog_id );
foreach (
array(
//$this->data_key,
$this->wc_am_instance_key,
$this->wc_am_deactivate_checkbox_key,
$this->wc_am_activated_key,
) as $option
) {
delete_option( $option );
}
restore_current_blog();
} else {
foreach (
array(
//$this->data_key,
$this->wc_am_instance_key,
$this->wc_am_deactivate_checkbox_key,
$this->wc_am_activated_key
) as $option
) {
delete_option( $option );
}
}
}
/**
* Deactivates the license on the API server
*/
public function license_key_deactivation() {
$activation_status = get_option( $this->wc_am_activated_key );
$api_key = $this->data[ $this->wc_am_api_key_key ];
$args = array(
'api_key' => $api_key,
);
if ( $activation_status == 'Activated' && $api_key != '' ) {
$this->deactivate( $args ); // reset API Key activation
}
}
/**
* Displays an inactive notice when the software is inactive.
*/
public function inactive_notice() { ?>
<?php
if ( apply_filters( 'wc_am_inactive_notice_override', false ) ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( isset( $_GET[ 'page' ] ) && $this->wc_am_activation_tab_key == $_GET[ 'page' ] ) {
return;
} ?>
<div class="notice notice-error">
<p><?php printf( __( 'The <strong>%s</strong> API Key has not been activated, so the %s is inactive! %sClick here%s to activate <strong>%s</strong>.', $this->text_domain ), esc_attr( $this->software_title ), esc_attr( $this->plugin_or_theme ), '<a href="' . esc_url( admin_url( 'options-general.php?page=' . $this->wc_am_activation_tab_key ) ) . '">', '</a>', esc_attr( $this->software_title ) ); ?></p>
</div>
<?php
}
/**
* Check for external blocking contstant.
*/
public function check_external_blocking() {
// show notice if external requests are blocked through the WP_HTTP_BLOCK_EXTERNAL constant
if ( defined( 'WP_HTTP_BLOCK_EXTERNAL' ) && WP_HTTP_BLOCK_EXTERNAL === true ) {
Loading ...