LLMS_REST_API_Keys::is_data_valid( array $data )

Validate data supplied for creating/updating a key.


Parameters Parameters

$data

(array) (Required) Associative array of data to set to a key's properties.

  • 'description'
    (string) A friendly name for the key.
  • 'user_id'
    (int) WP_User ID of the key's owner.
  • 'permissions'
    (string) Permission string for the key. Accepts read, write, or read_write.


Top ↑

Return Return

(WP_Error|true) When data is invalid will return a WP_Error with information about the invalid properties, otherwise true denoting data is valid.


Top ↑

Source Source

File: libraries/lifterlms-rest/includes/class-llms-rest-api-keys.php

	protected function is_data_valid( $data ) {

		// First conditions prevents '', '0', 0, etc... & second prevents invalid / non existant user ids.
		if ( ( isset( $data['user_id'] ) && empty( $data['user_id'] ) ) || ( ! empty( $data['user_id'] ) && ! get_user_by( 'id', $data['user_id'] ) ) ) {
			// Translators: %s = Invalid user id.
			return new WP_Error( 'llms_rest_key_invalid_user_id', sprintf( __( '"%s" is not a valid user ID.', 'lifterlms' ), $data['user_id'] ) );
		}

		// Prevent blank/empty descriptions.
		if ( isset( $data['description'] ) && empty( $data['description'] ) ) {
			return new WP_Error( 'llms_rest_key_invalid_description', __( 'An API Description is required.', 'lifterlms' ) );
		}

		// Validate Permissions.
		if ( ! empty( $data['permissions'] ) && ! in_array( $data['permissions'], array_keys( $this->get_permissions() ), true ) ) {
			// Translators: %s = Invalid permission string.
			return new WP_Error( 'llms_rest_key_invalid_permissions', sprintf( __( '"%s" is not a valid permission.', 'lifterlms' ), $data['permissions'] ) );
		}

		return true;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0-beta.1 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.