client = $client;
$this->option_key = 'appsero_' . md5( $this->client->slug ) . '_manage_license';
$this->schedule_hook = $this->client->slug . '_license_check_event';
// Run hook to check license status daily
add_action( $this->schedule_hook, array( $this, 'check_license_status' ) );
// Active/Deactive corn schedule
$this->run_schedule();
}
/**
* Check license
*
* @return boolean
*/
public function check( $license_key ) {
$route = 'public/license/' . $this->client->hash . '/check';
return $this->send_request( $license_key, $route );
}
/**
* Active a license
*
* @return boolean
*/
public function activate( $license_key ) {
$route = 'public/license/' . $this->client->hash . '/activate';
return $this->send_request( $license_key, $route );
}
/**
* Deactivate a license
*
* @return boolean
*/
public function deactivate( $license_key ) {
$route = 'public/license/' . $this->client->hash . '/deactivate';
return $this->send_request( $license_key, $route );
}
/**
* Send common request
*
* @param $license_key
* @param $route
*
* @return array
*/
protected function send_request( $license_key, $route ) {
$params = array(
'license_key' => $license_key,
'url' => esc_url( home_url() ),
'is_local' => $this->client->is_local_server(),
);
$response = $this->client->send_request( $params, $route, true );
if ( is_wp_error( $response ) ) {
return array(
'success' => false,
'error' => $response->get_error_message()
);
}
$response = json_decode( wp_remote_retrieve_body( $response ), true );
if ( empty( $response ) || isset( $response['exception'] )) {
return array(
'success' => false,
'error' => 'Unknown error occurred, Please try again.'
);
}
if ( isset( $response['errors'] ) && isset( $response['errors']['license_key'] ) ) {
$response = array(
'success' => false,
'error' => $response['errors']['license_key'][0]
);
}
return $response;
}
/**
* Add settings page for license
*
* @param array $args
*
* @return void
*/
public function add_settings_page( $args = array() ) {
$defaults = array(
'type' => 'menu', // Can be: menu, options, submenu
'page_title' => 'Manage License',
'menu_title' => 'Manage License',
'capability' => 'manage_options',
'menu_slug' => $this->client->slug . '-manage-license',
'icon_url' => '',
'position' => null,
'parent_slug' => '',
);
$this->menu_args = wp_parse_args( $args, $defaults );
add_action( 'admin_menu', array( $this, 'admin_menu' ), 99 );
}
/**
* Admin Menu hook
*
* @return void
*/
public function admin_menu() {
switch ( $this->menu_args['type'] ) {
case 'menu':
$this->create_menu_page();
break;
case 'submenu':
$this->create_submenu_page();
break;
case 'options':
$this->create_options_page();
break;
}
}
/**
* License menu output
*/
public function menu_output() {
if ( isset( $_POST['submit'] ) ) {
$this->license_form_submit( $_POST );
}
$license = get_option( $this->option_key, null );
$action = ( $license && isset( $license['status'] ) && 'activate' == $license['status'] ) ? 'deactive' : 'active';
$this->licenses_style();
?>
License Settings
show_license_page_notices();
do_action( 'before_appsero_license_section' );
?>
show_license_page_card_header(); ?>
Activate client->name; ?> by your license key to get professional support and automatic update from your WordPress dashboard.
show_active_license_info( $license );
}
?>
error = "Please add all information";
return;
}
if ( ! wp_verify_nonce( $form['_nonce'], $this->client->name ) ) {
$this->error = "You don't have permission to manage license.";
return;
}
switch ( $form['_action'] ) {
case 'active':
$this->active_client_license( $form );
break;
case 'deactive':
$this->deactive_client_license( $form );
break;
}
}
/**
* Check license status on schedule
*/
public function check_license_status() {
$license = get_option( $this->option_key, null );
if ( isset( $license['key'] ) && ! empty( $license['key'] ) ) {
$response = $this->check( $license['key'] );
if ( isset( $response['success'] ) && $response['success'] ) {
$license['status'] = 'activate';
$license['remaining'] = $response['remaining'];
$license['activation_limit'] = $response['activation_limit'];
$license['expiry_days'] = $response['expiry_days'];
$license['title'] = $response['title'];
$license['source_id'] = $response['source_identifier'];
$license['recurring'] = $response['recurring'];
} else {
$license['status'] = 'deactivate';
$license['expiry_days'] = 0;
}
update_option( $this->option_key, $license, false );
}
}
/**
* Check this is a valid license
*/
public function is_valid() {
if ( null !== $this->is_valid_licnese ) {
return $this->is_valid_licnese;
}
$license = get_option( $this->option_key, null );
if ( ! empty( $license['key'] ) && isset( $license['status'] ) && $license['status'] == 'activate' ) {
$this->is_valid_licnese = true;
} else {
$this->is_valid_licnese = false;
}
return $this->is_valid_licnese;
}
/**
* Check this is a valid license
*/
public function is_valid_by( $option, $value ) {
$license = get_option( $this->option_key, null );
if ( ! empty( $license['key'] ) && isset( $license['status'] ) && $license['status'] == 'activate' ) {
if ( isset( $license[ $option ] ) && $license[ $option ] == $value ) {
return true;
}
}
return false;
}
/**
* Styles for licenses page
*/
private function licenses_style() {
?>
Activation Remaining
Unlimited
out of
Expires in
10 ? '' : 'occupied';
echo '
' . $license['expiry_days'] . ' days
';
} else {
echo '
Never
';
}
?>
error ) ) :
?>
success ) ) :
?>
';
}
/**
* Card header
*/
private function show_license_page_card_header() {
?>
error = 'The license key field is required.';
return;
}
$license_key = sanitize_text_field( $form['license_key'] );
$response = $this->activate( $license_key );
if ( ! $response['success'] ) {
$this->error = $response['error'] ? $response['error'] : 'Unknown error occurred.';
return;
}
$data = array(
'key' => $license_key,
'status' => 'activate',
'remaining' => $response['remaining'],
'activation_limit' => $response['activation_limit'],
'expiry_days' => $response['expiry_days'],
'title' => $response['title'],
'source_id' => $response['source_identifier'],
'recurring' => $response['recurring'],
);
update_option( $this->option_key, $data, false );
$this->success = 'License activated successfully.';
}
/**
* Deactive client license
*/
private function deactive_client_license( $form ) {
$license = get_option( $this->option_key, null );
if ( empty( $license['key'] ) ) {
$this->error = 'License key not found.';
return;
}
$response = $this->deactivate( $license['key'] );
$data = array(
'key' => '',
'status' => 'deactivate',
);
update_option( $this->option_key, $data, false );
if ( ! $response['success'] ) {
$this->error = $response['error'] ? $response['error'] : 'Unknown error occurred.';
return;
}
$this->success = 'License deactivated successfully.';
}
/**
* Add license menu page
*/
private function create_menu_page() {
call_user_func(
'add_' . 'menu' . '_page',
$this->menu_args['page_title'],
$this->menu_args['menu_title'],
$this->menu_args['capability'],
$this->menu_args['menu_slug'],
array( $this, 'menu_output' ),
$this->menu_args['icon_url'],
$this->menu_args['position']
);
}
/**
* Add submenu page
*/
private function create_submenu_page() {
call_user_func(
'add_' . 'submenu' . '_page',
$this->menu_args['parent_slug'],
$this->menu_args['page_title'],
$this->menu_args['menu_title'],
$this->menu_args['capability'],
$this->menu_args['menu_slug'],
array( $this, 'menu_output' ),
$this->menu_args['position']
);
}
/**
* Add submenu page
*/
private function create_options_page() {
call_user_func(
'add_' . 'options' . '_page',
$this->menu_args['page_title'],
$this->menu_args['menu_title'],
$this->menu_args['capability'],
$this->menu_args['menu_slug'],
array( $this, 'menu_output' ),
$this->menu_args['position']
);
}
/**
* Schedule daily sicense checker event
*/
public function schedule_cron_event() {
if ( ! wp_next_scheduled( $this->schedule_hook ) ) {
wp_schedule_event( time(), 'daily', $this->schedule_hook );
wp_schedule_single_event( time() + 20, $this->schedule_hook );
}
}
/**
* Clear any scheduled hook
*/
public function clear_scheduler() {
wp_clear_scheduled_hook( $this->schedule_hook );
}
/**
* Enable/Disable schedule
*/
private function run_schedule() {
switch ( $this->client->type ) {
case 'plugin':
register_activation_hook( $this->client->file, array( $this, 'schedule_cron_event' ) );
register_deactivation_hook( $this->client->file, array( $this, 'clear_scheduler' ) );
break;
case 'theme':
add_action( 'after_switch_theme', array( $this, 'schedule_cron_event' ) );
add_action( 'switch_theme', array( $this, 'clear_scheduler' ) );
break;
}
}
/**
* Form action URL
*/
private function formActionUrl() {
echo add_query_arg(
array( 'page' => $_GET['page'] ),
admin_url( basename( $_SERVER['SCRIPT_NAME'] ) )
);
}
/**
* Get input license key
* @param $action
* @return $license
*/
private function get_input_license_value( $action, $license ) {
if ( 'active' == $action ) {
return isset( $license['key'] ) ? $license['key'] : '';
}
if ( 'deactive' == $action ) {
$key_length = strlen( $license['key'] );
return str_pad(
substr( $license['key'], 0, $key_length / 2 ), $key_length, '*'
);
}
return '';
}
}