LLMS_Form_Handler::prepare_data_for_insert( array $posted_data, array[] $fields, string $action )

Prepares user-submitted data for insertion into the database.


Parameters Parameters

$posted_data

(array) (Required) Sanitized & validated user-submitted form data.

$fields

(array[]) (Required) LifterLMS form fields list.

$action

(string) (Required) Insert action, either "registration" for new users or "update" for existing, users.


Top ↑

Return Return

(array)


Top ↑

Source Source

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

	protected function prepare_data_for_insert( $posted_data, $fields, $action ) {

		$prepared = array();

		foreach ( $fields as $field ) {

			if ( empty( $field['data_store_key'] ) ) {
				continue;
			}

			// We need to account for fields that are part of the form but are not present in the `$posted_data`
			// e.g. unchecked check boxes.
			if ( isset( $posted_data[ $field['name'] ] ) || 'checkbox' === $field['type'] ) {

				if ( ! isset( $prepared[ $field['data_store'] ] ) ) {
					$prepared[ $field['data_store'] ] = array();
				}

				$prepared[ $field['data_store'] ][ $field['data_store_key'] ] = isset( $posted_data[ $field['name'] ] ) ? $posted_data[ $field['name'] ] : array();
			}
		}

		if ( 'registration' === $action ) {

			$defaults = array(
				'role'                 => 'student',
				'show_admin_bar_front' => false,
			);

			// Add a username if we don't have a user_login field.
			if ( empty( $prepared['users']['user_login'] ) ) {
				$defaults['user_login'] = LLMS_Person_Handler::generate_username( $posted_data['email_address'] );
			}

			// Add a password if we don't have a password field.
			if ( empty( $prepared['users']['user_pass'] ) ) {
				$defaults['user_pass'] = wp_generate_password( 32, true, true );
			}

			$prepared['users'] = wp_parse_args( $prepared['users'], $defaults );

		} elseif ( 'update' === $action ) {

			$prepared['users']['ID'] = empty( $posted_data['user_id'] ) ? get_current_user_id() : absint( $posted_data['user_id'] );

		}

		// Record an IP Address.
		$prepared['usermeta']['llms_ip_address'] = llms_get_ip_address();

		// If terms have been agreed to, record a time stamp for the agreement.
		if ( isset( $posted_data['llms_agress_to_terms'] ) ) {
			$prepared['usermeta']['llms_agress_to_terms'] = current_time( 'mysql' );
		}

		/**
		 * Filter data added to the wp_users data via `wp_insert_user()` or `wp_update_user()`.
		 *
		 * The dynamic portion of this hook, `$action`, can be either "registration" or "update".
		 *
		 * @since 3.0.0
		 * @since 5.0.0 Moved from `LLMS_Person_Handler::insert_data()`.
		 *
		 * @param array  $user_data   Array of user data.
		 * @param array  $posted_data Array of user-submitted data.
		 * @param string $action      Submission action, either "registration" or "update".
		 */
		$prepared['users'] = apply_filters( "lifterlms_user_{$action}_insert_user", $prepared['users'], $posted_data, $action );

		/**
		 * Filter meta data to be added for the user.
		 *
		 * The dynamic portion of this hook, `$action`, can be either "registration" or "update".
		 *
		 * @since 3.0.0
		 * @since 5.0.0 Moved from `LLMS_Person_Handler::insert_data()`.
		 *
		 * @param array  $user_meta   Array of user meta data.
		 * @param array  $posted_data Array of user-submitted data.
		 * @param string $action      Submission action, either "registration" or "update".
		 */
		$prepared['usermeta'] = apply_filters( "lifterlms_user_{$action}_insert_user_meta", $prepared['usermeta'], $posted_data, $action );

		return $prepared;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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