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

Generate a new LLMS_Post_Model.


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 ) ) {
			/* translators: %s: Name of class. */
			throw new Exception( esc_html( sprintf( __( 'The class "%s" does not exist.', 'lifterlms' ), $class_name ) ), intval( self::ERROR_INVALID_POST ) );
		}

		// Don't create useless revision on "cloning".
		add_filter( 'wp_revisions_to_keep', '__return_zero', 999 );

		// Insert the object.
		$post = new $class_name(
			'new',
			/**
			 * Filter the data used to generate a new post.
			 *
			 * @since 7.4.0
			 *
			 * @param array $new_post_data New post data array.
			 * @param array $raw           Original raw post data array.
			 */
			apply_filters(
				'llms_generator_new_post_data',
				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'],
				),
				$raw
			)
		);

		if ( ! $post->get( 'id' ) ) {
			// Translators: %s = post type name.
			throw new Exception( esc_html( sprintf( __( 'Error creating the %s post object.', 'lifterlms' ), $type ) ), intval( 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'] );
		/**
		 * Skip adding the `generated_from_id` meta from the original post:
		 * this is the case when cloning a cloned post.
		 */
		unset( $raw['custom'][ $post->get( 'meta_prefix' ) . 'generated_from_id' ] );

		$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 );

		// Remove revision prevention.
		remove_filter( 'wp_revisions_to_keep', '__return_zero', 999 );

		return $post;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
7.3.0 Skip adding the generated_from_id meta from the original post: this is the case when cloning a cloned post.<br> Also skip creating revisions.
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.