LLMS_Form_Handler::maybe_modify_edit_account_field_settings( array $posted_data, string $location, array[] $fields )

Modify LifterLMS Fields prior to performing submit handler validations.


Parameters Parameters

$posted_data

(array) (Required) User submitted form data (passed by reference).

$location

(string) (Required) Form location ID.

$fields

(array[]) (Required) Array of LifterLMS Form Fields (passed by reference).


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/forms/class-llms-form-handler.php

	public function maybe_modify_edit_account_field_settings( &$posted_data, $location, &$fields ) {

		if ( 'account' !== $location ) {
			return;
		}

		/**
		 * If email address and passwords aren't submitted we can mark them as "optional" fields.
		 *
		 * These fields are dynamically toggled and disabled if they're not modified.
		 * Process `password_current` as last as it depends on `password` field submission.
		 */
		foreach ( array( 'email_address', 'password', 'password_current' ) as $field_id ) {

			// If the field exists and it's not included (or empty) in the posted data.
			$index = LLMS_Forms::instance()->get_field_by( $fields, 'id', $field_id, 'index' );
			if ( false !== $index && empty( $posted_data[ $fields[ $index ]['name'] ] ) ) {

				// When updating a password, the `password_current` is mandatory.
				if ( 'account' === $location && 'password_current' === $field_id ) {
					// Get `password` field.
					$password_index = LLMS_Forms::instance()->get_field_by( $fields, 'id', 'password', 'index' );
					// If a `passowrd` feld has been submitted then the `password_current` cannot be skipped.
					if ( false !== $password_index &&
							! empty( $posted_data[ $fields[ $password_index ]['name'] ] ) ) {
						continue;
					}
				}

				// Remove the field so we don't accidentally save an empty value later.
				unset( $posted_data[ $fields[ $index ]['name'] ] );

				// Mark the field as optional (for validation purposes).
				$fields[ $index ]['required'] = false;

				// Check if there's a confirm field and do the same.
				$con_index = LLMS_Forms::instance()->get_field_by( $fields, 'id', "{$field_id}_confirm", 'index' );
				if ( false !== $con_index && empty( $posted_data[ $fields[ $con_index ]['name'] ] ) ) {
					unset( $posted_data[ $fields[ $con_index ]['name'] ] );
					$fields[ $con_index ]['required'] = false;
				}
			}
		}

	}

Top ↑

Changelog Changelog

Changelog
Version Description
5.1.0 Do not allow submitting a password change without providing a password_current
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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