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, array( $this, 'refresh_license_api' ) ); // Run hook to check license status daily add_action( $this->schedule_hook, array( $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 bool */ public function check( $license_key ) { $route = 'public/license/' . $this->client->hash . '/check'; return $this->send_request( $license_key, $route ); } /** * Active a license * * @return bool */ public function activate( $license_key ) { $route = 'public/license/' . $this->client->hash . '/activate'; return $this->send_request( $license_key, $route ); } /** * Deactivate a license * * @return bool */ 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' => $this->client->__trans( '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; } /** * License Refresh Endpoint */ public function refresh_license_api() { $this->check_license_status(); return wp_send_json( array( 'message' => 'License refreshed successfully.' ), 200 ); } /** * 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 = $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 ); } ?>
error = $this->client->__trans( 'Please add all information' ); return; } if ( ! wp_verify_nonce( $form['_nonce'], $this->client->name ) ) { $this->error = $this->client->__trans( "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; case 'refresh': $this->refresh_client_license( $form ); 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 ) ) { ?>

error; ?>

success ) ) { ?>

success; ?>

'; } /** * Card header */ private function show_license_page_card_header( $license ) { ?>
client->__trans( 'Activate License' ); ?>
error = $this->client->__trans( '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'] : $this->client->__trans( '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 = $this->client->__trans( 'License activated successfully.' ); } /** * Deactive client license */ private function deactive_client_license( $form ) { $license = $this->get_license(); if ( empty( $license['key'] ) ) { $this->error = $this->client->__trans( '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'] : $this->client->__trans( 'Unknown error occurred.' ); return; } $this->success = $this->client->__trans( 'License deactivated successfully.' ); } /** * Refresh Client License */ private function refresh_client_license( $form = null ) { $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'], 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 form_action_url() { $url = add_query_arg( $_GET, admin_url( basename( $_SERVER['SCRIPT_NAME'] ) ) ); echo apply_filters( 'appsero_client_license_form_action', $url ); } /** * 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 ''; } }