<?php
namespace WPStaging\Backend\Pro\Licensing;
if( !defined( "WPINC" ) ) {
die;
}
class Licensing {
private $licensekey;
public function __construct() {
add_action( 'admin_notices', array($this, 'admin_notices') );
add_action( 'admin_init', array($this, 'activate_license') );
add_action( 'admin_init', array($this, 'deactivate_license') );
add_action( 'wpstg_weekly_event', array($this, 'weekly_license_check') );
if( !defined( 'WPSTG_STORE_URL' ) )
if( !defined( 'WPSTG_ITEM_NAME' ) )
define( 'WPSTG_ITEM_NAME', 'WP STAGING PRO' );
$this->plugin_updater();
$this->licensekey = trim( get_option( 'wpstg_license_key' ) );
}
public function plugin_updater() {
$license_key = trim( get_option( 'wpstg_license_key' ) );
$edd_updater = new \WPStaging\Backend\Pro\Licensing\EDD_SL_Plugin_Updater( WPSTG_STORE_URL, WPSTGPRO_PLUGIN_FILE, array(
'version' => WPSTGPRO_VERSION,
'license' => $license_key,
'item_name' => WPSTG_ITEM_NAME,
'author' => 'Rene Hermenau',
'beta' => false
)
);
}
public function activate_license() {
if( isset( $_POST['wpstg_activate_license'] ) && !empty( $_POST['wpstg_license_key'] ) ) {
if( !check_admin_referer( 'wpstg_license_nonce', 'wpstg_license_nonce' ) )
return;
update_option( 'wpstg_license_key', $_POST['wpstg_license_key'] );
$license = trim( get_option( 'wpstg_license_key' ) );
$api_params = array(
'edd_action' => 'activate_license',
'license' => $license,
'item_name' => urlencode( WPSTG_ITEM_NAME ),
'url' => home_url()
);
$response = array('response'=>array('code'=>200));
if( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
if( is_wp_error( $response ) ) {
$message = $response->get_error_message();
} else {
$message = __( 'An error occurred, please try again.' );
}
} else {
$license_data = (object)array('success'=>true, 'license'=>'valid', 'expires'=>'2048-06-06 23:59:59');
if( false === $license_data->success ) {
switch ( $license_data->error ) {
case 'expired' :
$message = sprintf(
__( 'Your license key expired on %s.' ), date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) )
);
break;
case 'revoked' :
$message = __( 'Your license key has been disabled.' );
break;
case 'missing' :
$message = __( 'WP Staging license key is invalid.' );
break;
case 'invalid' :
case 'site_inactive' :
$message = __( 'Your license is not active for this URL.' );
break;
case 'item_name_mismatch' :
$message = sprintf( __( 'This appears to be an invalid license key for %s.' ), WPSTG_ITEM_NAME );
break;
case 'no_activations_left':
$message = __( 'Your license key has reached its activation limit.' );
break;
default :
$message = __( 'An error occurred, please try again.' );
break;
}
}
}
if( !empty( $message ) ) {
$base_url = admin_url( 'admin.php?page=wpstg-license' );
$redirect = add_query_arg( array('wpstg_licensing' => 'false', 'message' => urlencode( $message )), $base_url );
update_option( 'wpstg_license_status', $license_data );
wp_redirect( $redirect );
exit();
}
update_option( 'wpstg_license_status', $license_data );
wp_redirect( admin_url( 'admin.php?page=wpstg-license' ) );
exit();
}
}
public function deactivate_license() {
if( isset( $_POST['wpstg_deactivate_license'] ) ) {
if( !check_admin_referer( 'wpstg_license_nonce', 'wpstg_license_nonce' ) )
return;
$license = trim( get_option( 'wpstg_license_key' ) );
$api_params = array(
'edd_action' => 'deactivate_license',
'license' => $license,
'item_name' => urlencode( WPSTG_ITEM_NAME ),
'url' => home_url()
);
$response = array('response'=>array('code'=>200));
if( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
if( is_wp_error( $response ) ) {
$message = $response->get_error_message();
} else {
$message = __( 'An error occurred, please try again.' );
}
$base_url = admin_url( 'admin.php?page=wpstg-license' );
$redirect = add_query_arg( array('wpstg_licensing' => 'false', 'message' => urlencode( $message )), $base_url );
wp_redirect( $redirect );
exit();
}
$license_data = (object)array('success'=>true, 'license'=>'valid', 'expires'=>date('Y-m-d', strtotime('+5 years')));
if( $license_data->license == 'deactivated' || $license_data->license == 'failed' ) {
delete_option( 'wpstg_license_status' );
}
wp_redirect( admin_url( 'admin.php?page=wpstg-license' ) );
exit();
}
}
public function weekly_license_check() {
return;
if( empty( $this->licensekey ) ) {
return;
}
$api_params = array(
'edd_action' => 'check_license',
'license' => $this->licensekey,
'item_name' => urlencode( WPSTG_ITEM_NAME ),
'url' => home_url()
);
$response = wp_remote_post(
WPSTG_STORE_URL, array(
'timeout' => 15,
'sslverify' => false,
'body' => $api_params
)
);
if( is_wp_error( $response ) ) {
return false;
}
$license_data = (object)array('success'=>true, 'license'=>'valid', 'expires'=>date('Y-m-d', strtotime('+5 years')));
update_option( 'wpstg_license_status', $license_data );
}
public function admin_notices() {
if( isset( $_GET['wpstg_licensing'] ) && !empty( $_GET['message'] ) ) {
switch ( $_GET['wpstg_licensing'] ) {
case 'false':
$message = urldecode( $_GET['message'] );
?>
<div class="wpstg-error" style="font-weight:500;">
<p><?php _e('WP Staging - Can not activate license key! ','wp-staging'); echo $message; ?></p>
</div>
<?php
break;
case 'true':
default:
?>
<?php
break;
}
}
}
}