LLMS_Abstract_Generator_Posts::create_post( string $type, array $raw = array(), int $author_id = null )

Generate a new LLMS_Post_Mdel


Parameters Parameters

$type

(string) (Required) The LLMS_Post_Model post type type. For example "course" for an LLMS_Course or membership for LLMS_Membership.

$raw

(array) (Optional) Array of raw, used to create the post.

Default value: array()

$author_id

(int) (Optional) Fallback author ID, used when now author data can be found in $raw.

Default value: null


Top ↑

Return Return

(LLMS_Post_Model)


Top ↑

Source Source

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

	protected function create_post( $type, $raw = array(), $author_id = null ) {

		$class_name = sprintf( 'LLMS_%s', implode( '_', array_map( 'ucfirst', explode( '_', $type ) ) ) );
		if ( ! class_exists( $class_name ) ) {
			throw new Exception( sprintf( __( 'The class "%s" does not exist.', 'lifterlms' ), $class_name ), self::ERROR_INVALID_POST );
		}

		// Insert the object.
		$post = new $class_name(
			'new',
			array(
				'post_author'   => $this->get_author_id_from_raw( $raw, $author_id ),
				'post_content'  => isset( $raw['content'] ) ? $raw['content'] : '',
				'post_date'     => isset( $raw['date'] ) ? $this->format_date( $raw['date'] ) : null,
				'post_excerpt'  => isset( $raw['excerpt'] ) ? $raw['excerpt'] : '',
				'post_modified' => isset( $raw['modified'] ) ? $this->format_date( $raw['modified'] ) : null,
				'post_status'   => isset( $raw['status'] ) ? $raw['status'] : $this->get_default_post_status(),
				'post_title'    => $raw['title'],
			)
		);

		if ( ! $post->get( 'id' ) ) {
			// Translators: %s = post type name.
			throw new Exception( sprintf( __( 'Error creating the %s post object.', 'lifterlms' ), $type ), self::ERROR_CREATE_POST );
		}

		// Store the temp id if it exists.
		$this->store_temp_id( $raw, $post );

		// Don't set these values again.
		unset( $raw['id'], $raw['author'], $raw['content'], $raw['date'], $raw['excerpt'], $raw['modified'], $raw['name'], $raw['status'], $raw['title'] );

		$this->set_metadata( $post, $raw );
		$this->set_featured_image( $raw, $post->get( 'id' ) );
		$this->add_custom_values( $post->get( 'id' ), $raw );
		$this->sideload_images( $post, $raw );
		$this->handle_reusable_blocks( $post, $raw );

		return $post;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
4.7.1 Set the post's excerpt during the initial insert instead of during metadata updates after creation.
4.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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