self::key() ? 'unknown' : 'none', 'message' => '', ); /** * License status for display. * * @param array $status state: none|unknown|active|grace|expired|invalid, message: free text. * @param string $key Stored key. */ $status = apply_filters( 'alpeg_forms_license_status', $default, self::key() ); return wp_parse_args( is_array( $status ) ? $status : array(), $default ); } /** * Handle the "License" form (admin-post). */ public static function handle_save() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'alpeg-forms' ), 403 ); } check_admin_referer( 'alpeg_forms_license' ); $do = isset( $_POST['af_license_action'] ) ? sanitize_key( wp_unslash( $_POST['af_license_action'] ) ) : 'activate'; $notice = array( 'type' => 'success', 'message' => '', ); if ( 'deactivate' === $do ) { /** * Deactivate the license on the license server. * * @param array|\WP_Error|null $result Result. * @param string $key Key. */ $result = apply_filters( 'alpeg_forms_license_deactivate', null, self::key() ); if ( is_wp_error( $result ) ) { $notice = array( 'type' => 'error', 'message' => $result->get_error_message(), ); } else { delete_option( self::OPTION ); $notice['message'] = __( 'The license has been deactivated on this website.', 'alpeg-forms' ); } } else { $key = isset( $_POST['af_license_key'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['af_license_key'] ) ) ) : ''; update_option( self::OPTION, $key, false ); /** * Activate the license on the license server. * Return array( 'message' => '...' ) on success or WP_Error. * * @param array|\WP_Error|null $result Result. * @param string $key Key. * @param string $domain Domain of this website. */ $result = apply_filters( 'alpeg_forms_license_activate', null, $key, wp_parse_url( home_url(), PHP_URL_HOST ) ); if ( is_wp_error( $result ) ) { $notice = array( 'type' => 'error', 'message' => $result->get_error_message(), ); } elseif ( null === $result ) { $notice = array( 'type' => 'info', 'message' => __( 'Key saved. The Pro version is not available yet – as soon as it is released, your key will be used automatically.', 'alpeg-forms' ), ); } else { $notice['message'] = isset( $result['message'] ) ? (string) $result['message'] : __( 'License activated. Enjoy Pro!', 'alpeg-forms' ); } } set_transient( 'alpeg_forms_license_notice_' . get_current_user_id(), $notice, 60 ); wp_safe_redirect( admin_url( 'admin.php?page=alpeg-forms-license' ) ); exit; } /** * Render the "License" page. */ public static function render_page() { $status = self::status(); $key = self::key(); $notice = get_transient( 'alpeg_forms_license_notice_' . get_current_user_id() ); delete_transient( 'alpeg_forms_license_notice_' . get_current_user_id() ); $labels = array( 'none' => array( __( 'No license', 'alpeg-forms' ), 'neutral' ), 'unknown' => array( __( 'Key saved', 'alpeg-forms' ), 'neutral' ), 'active' => array( __( 'Active', 'alpeg-forms' ), 'good' ), 'grace' => array( __( 'Expired – grace period', 'alpeg-forms' ), 'warn' ), 'expired' => array( __( 'Expired', 'alpeg-forms' ), 'warn' ), 'invalid' => array( __( 'Invalid', 'alpeg-forms' ), 'bad' ), ); $label = isset( $labels[ $status['state'] ] ) ? $labels[ $status['state'] ] : $labels['unknown']; ?>