LLMS_Abstract_Generator_Posts::get_author_id( array $raw )

Accepts raw author data and locates an existing author by email or id or creates one


Parameters Parameters

$raw

(array) (Required) Author data. If id and email are provided will use id only if it matches the email for user matching that id in the database. If no id found, attempts to locate by email. If no author found and email provided, creates new user using email. Falls back to current user id. First_name, last_name, and description can be optionally provided. When provided will be used only when creating a new user.


Top ↑

Return Return

(int) WP_User ID


Top ↑

Source Source

File: includes/abstracts/llms-abstract-generator-posts.php

	protected function get_author_id( $raw ) {

		$author_id = 0;

		// If raw is missing an ID and Email, use current user id.
		if ( ! isset( $raw['id'] ) && ! isset( $raw['email'] ) ) {
			$author_id = get_current_user_id();
		} else {

			// If id is set, check if the id matches a user in the DB.
			if ( isset( $raw['id'] ) && is_numeric( $raw['id'] ) ) {

				$user = get_user_by( 'ID', $raw['id'] );

				// User exists.
				if ( $user ) {

					// We have a raw email.
					if ( isset( $raw['email'] ) ) {

						// Raw email matches found user's email.
						if ( $user->user_email === $raw['email'] ) {
							$author_id = $user->ID;
						}
					} else {
						$author_id = $user->ID;
					}
				}
			}

			if ( ! $author_id ) {

				if ( isset( $raw['email'] ) ) {

					// See if we have a user that matches by email.
					$user = get_user_by( 'email', $raw['email'] );

					// User exists, use this user.
					if ( $user ) {
						$author_id = $user->ID;
					}
				}
			}

			// No author id, create a new one using the email.
			if ( ! $author_id && isset( $raw['email'] ) ) {

				$author_id = $this->create_user( $raw );

				if ( is_wp_error( $author_id ) ) {
					throw new Exception( $author_id->get_error_message(), self::ERROR_CREATE_USER );
				}
			}
		}

		/**
		 * Filter the author ID prior to it being used for the generation of new posts
		 *
		 * @since 4.7.0
		 *
		 * @param int   $author_id WP_User ID of the author.
		 * @param array $raw       Original raw author data.
		 */
		return apply_filters( 'llms_generator_get_author_id', $author_id, $raw );

	}


Top ↑

Changelog Changelog

Changelog
Version Description
4.7.0 Moved from LLMS_Generator and made protected instead of private.
4.3.3 Use strict string comparator.
3.3.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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