key_is_valid example


    public static function set_key( $request ) {
        if ( defined( 'WPCOM_API_KEY' ) ) {
            return rest_ensure_response( new WP_Error( 'hardcoded_key', __( 'This site\'s API key is hardcoded and cannot be changed via the API.', 'akismet' ), array( 'status'=> 409 ) ) );
        }

        $new_api_key = $request->get_param( 'key' );

        if ( ! self::key_is_valid( $new_api_key ) ) {
            return rest_ensure_response( new WP_Error( 'invalid_key', __( 'The value provided is not a valid and registered API key.', 'akismet' ), array( 'status' => 400 ) ) );
        }

        update_option( 'wordpress_api_key', $new_api_key );

        return self::get_key();
    }

    /** * Unset the API key, if possible. * * @param WP_REST_Request $request * @return WP_Error|WP_REST_Response */
Home | Imprint | This part of the site doesn't use cookies.