client = $client; $this->option_key = 'appsero_' . md5( $this->client->slug ) . '_licenses'; } /** * 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() ), ); $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 ) ) { 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' => 'manage-license', 'icon_url' => '', 'position' => null, 'parent_slug' => '', ); $this->menu_args = wp_parse_args( $args, $defaults ); add_action( 'admin_menu', array( $this, 'admin_menu' ) ); add_action( $this->client->slug . '_license_check_event', array( $this, 'check_license_status' ) ); } /** * Admin Menu hook * * @return void */ public function admin_menu() { $add_page = 'add_' . $this->menu_args['type'] . '_page'; switch ( $this->menu_args['type'] ) { case 'menu': $add_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'] ); break; case 'submenu': $add_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' ) ); break; case 'options': $add_page( $this->menu_args['page_title'], $this->menu_args['menu_title'], $this->menu_args['capability'], $this->menu_args['menu_slug'], array( $this, 'menu_output' ) ); break; } } /** * License menu output */ public function menu_output() { if ( isset( $_POST['submit'] ) ) { $this->license_page_form( $_POST ); } $license = get_option( $this->option_key, null ); $action = ( $license && isset( $license['status'] ) && 'activate' == $license['status'] ) ? 'Deactive' : 'Active'; ?>
error; ?>
success; ?>