Members::can_access( awsmug\Torro_Forms\DB_Objects\Forms\Form $form, awsmug\Torro_Forms\DB_Objects\Submissions\Submission|null $submission = null )

Determines whether the current user can access a specific form or submission.

Description

See also

Parameters

$form

(awsmug\Torro_Forms\DB_Objects\Forms\Form) (Required) Form object.

$submission

(awsmug\Torro_Forms\DB_Objects\Submissions\Submission|null) (Optional) Submission object, or null if no submission is set.

Default value: null

Return

(bool|WP_Error) True if the form or submission can be accessed, false or error object otherwise.

Source

File: src/modules/access-controls/members.php

	public function can_access( $form, $submission = null ) {
		if ( ! is_user_logged_in() ) {
			$message = $this->get_form_option( $form->id, 'login_required_message' );
			if ( empty( $message ) ) {
				$message = $this->get_default_login_required_message();
			}

			return new WP_Error( 'must_be_logged_in', $message );
		}

		$allowed_roles    = $this->get_form_option( $form->id, 'allowed_roles', array() );
		$allowed_user_ids = $this->get_form_option( $form->id, 'allowed_users', array() );
		$allowed_user_ids = array_map( 'absint', array_unique( array_filter( $allowed_user_ids ) ) );

		if ( empty( $allowed_roles ) && empty( $allowed_user_ids ) ) {
			return true;
		}

		if ( ! empty( $allowed_roles ) ) {
			$user = wp_get_current_user();

			$intersected_roles = array_intersect( array_values( $user->roles ), $allowed_roles );
			if ( ! empty( $intersected_roles ) ) {
				return true;
			}
		}

		if ( ! empty( $allowed_user_ids ) ) {
			if ( in_array( get_current_user_id(), $allowed_user_ids, true ) ) {
				return true;
			}
		}

		$message = $this->get_form_option( $form->id, 'not_selected_message' );
		if ( empty( $message ) ) {
			$message = $this->get_default_not_selected_message();
		}

		return new WP_Error( 'not_selected', $message );
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.