/home/vianto5/.trash/cititower.mx/wp-content/plugins/ml-slider/lib/appsero/src
Edit: /home/vianto5/.trash/cititower.mx/wp-content/plugins/ml-slider/lib/appsero/src/License.php (26462B)
client = $client;
$this->option_key = 'appsero_' . md5( $this->client->slug ) . '_manage_license';
$this->schedule_hook = $this->client->slug . '_license_check_event';
// Creating WP Ajax Endpoint to refresh license remotely
add_action( 'wp_ajax_appsero_refresh_license_' . $this->client->hash, [ $this, 'refresh_license_api' ] );
// Run hook to check license status daily
add_action( $this->schedule_hook, [ $this, 'check_license_status' ] );
// Active/Deactive corn schedule
$this->run_schedule();
}
/**
* Set the license option key.
*
* If someone wants to override the default generated key.
*
* @param string $key
*
* @since 1.3.0
*
* @return License
*/
public function set_option_key( $key ) {
$this->option_key = $key;
return $this;
}
/**
* Get the license key
*
* @since 1.3.0
*
* @return string|null
*/
public function get_license() {
return get_option( $this->option_key, null );
}
/**
* Check license
*
* @return array
*/
public function check( $license_key ) {
$route = 'public/license/' . $this->client->hash . '/check';
return $this->send_request( $license_key, $route );
}
/**
* Active a license
*
* @return array
*/
public function activate( $license_key ) {
$route = 'public/license/' . $this->client->hash . '/activate';
return $this->send_request( $license_key, $route );
}
/**
* Deactivate a license
*
* @return array
*/
public function deactivate( $license_key ) {
$route = 'public/license/' . $this->client->hash . '/deactivate';
return $this->send_request( $license_key, $route );
}
/**
* Send common request
*
* @return array
*/
protected function send_request( $license_key, $route ) {
$params = [
'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 [
'success' => false,
'error' => $response->get_error_message(),
];
}
$response = json_decode( wp_remote_retrieve_body( $response ), true );
if ( empty( $response ) || isset( $response['exception'] ) ) {
return [
'success' => false,
'error' => $this->client->__trans( 'Unknown error occurred, Please try again.' ),
];
}
if ( isset( $response['errors'] ) && isset( $response['errors']['license_key'] ) ) {
$response = [
'success' => false,
'error' => $response['errors']['license_key'][0],
];
}
return $response;
}
/**
* License Refresh Endpoint
*/
public function refresh_license_api() {
$this->check_license_status();
wp_send_json_success(
[
'message' => 'License refreshed successfully.',
],
200
);
}
/**
* Add settings page for license
*
* @param array $args
*
* @return void
*/
public function add_settings_page( $args = [] ) {
$defaults = [
'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', [ $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() {
// process form data if submitted
if ( isset( $_POST['_nonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_nonce'] ) ), $this->client->name ) ) {
$form_data = [
'_nonce' => sanitize_key( wp_unslash( $_POST['_nonce'] ) ),
'_action' => isset( $_POST['_action'] ) ? sanitize_text_field( wp_unslash( $_POST['_action'] ) ) : '',
'license_key' => isset( $_POST['license_key'] ) ? sanitize_text_field( wp_unslash( $_POST['license_key'] ) ) : '',
];
$this->license_form_submit( $form_data );
}
$license = $this->get_license();
$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( $license ); ?>
client->__trans( 'Activate %s by your license key to get professional support and automatic update from your WordPress dashboard.' ), $this->client->name ); ?>
show_active_license_info( $license );
}
?>
client->name ) ) {
$this->error = $this->client->__trans( 'Nonce vefification failed.' );
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
$this->error = $this->client->__trans( 'You don\'t have permission to manage license.' );
return;
}
$license_key = ! empty( $form_data['license_key'] ) ? sanitize_text_field( wp_unslash( $form_data['license_key'] ) ) : '';
$action = ! empty( $form_data['_action'] ) ? sanitize_text_field( wp_unslash( $form_data['_action'] ) ) : '';
switch ( $action ) {
case 'active':
$this->active_client_license( $license_key );
break;
case 'deactive':
$this->deactive_client_license();
break;
case 'refresh':
$this->refresh_client_license();
break;
}
}
/**
* Check license status on schedule
*/
public function check_license_status() {
$license = $this->get_license();
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_license ) {
return $this->is_valid_license;
}
$license = $this->get_license();
if ( ! empty( $license['key'] ) && isset( $license['status'] ) && $license['status'] === 'activate' ) {
$this->is_valid_license = true;
} else {
$this->is_valid_license = false;
}
return $this->is_valid_license;
}
/**
* Check this is a valid license
*/
public function is_valid_by( $option, $value ) {
$license = $this->get_license();
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() {
?>
client->_etrans( 'Activations Remaining' ); ?>
client->_etrans( 'Unlimited' ); ?>
client->__trans( '%1$d out of %2$d' ), $license['remaining'], $license['activation_limit'] ); ?>
client->_etrans( 'Expires in' ); ?>
21 ? '' : 'occupied';
echo '
' . $license['expiry_days'] . ' days
';
} else {
echo '
' . $this->client->__trans( 'Never' ) . '
';
}
?>
error ) ) {
?>
success ) ) {
?>
';
}
/**
* Card header
*/
private function show_license_page_card_header( $license ) {
?>
client->__trans( 'Activate License' ); ?>
client->__trans( 'Refresh License' ); ?>
error = $this->client->__trans( 'The license key field is required.' );
return;
}
$response = $this->activate( $license_key );
if ( ! $response['success'] ) {
$this->error = $response['error'] ? $response['error'] : $this->client->__trans( 'Unknown error occurred.' );
return;
}
$data = [
'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 = $this->client->__trans( 'License activated successfully.' );
}
/**
* Deactive client license
*/
private function deactive_client_license() {
$license = $this->get_license();
if ( empty( $license['key'] ) ) {
$this->error = $this->client->__trans( 'License key not found.' );
return;
}
$response = $this->deactivate( $license['key'] );
$data = [
'key' => '',
'status' => 'deactivate',
];
update_option( $this->option_key, $data, false );
if ( ! $response['success'] ) {
$this->error = $response['error'] ? $response['error'] : $this->client->__trans( 'Unknown error occurred.' );
return;
}
$this->success = $this->client->__trans( 'License deactivated successfully.' );
}
/**
* Refresh Client License
*/
private function refresh_client_license() {
$license = $this->get_license();
if ( ! $license || ! isset( $license['key'] ) || empty( $license['key'] ) ) {
$this->error = $this->client->__trans( 'License key not found' );
return;
}
$this->check_license_status();
$this->success = $this->client->__trans( 'License refreshed 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'],
[ $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'],
[ $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'],
[ $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, [ $this, 'schedule_cron_event' ] );
register_deactivation_hook( $this->client->file, [ $this, 'clear_scheduler' ] );
break;
case 'theme':
add_action( 'after_switch_theme', [ $this, 'schedule_cron_event' ] );
add_action( 'switch_theme', [ $this, 'clear_scheduler' ] );
break;
}
}
/**
* Get input license key
*
* @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 '';
}
}