LLMS_Post_Model::toArray()

Coverts the object to an associative array


Description Description

Any property returned by $this->get_properties() will be retrieved via $this->get() and added to the array.

Extending classes can add additional properties to the array by overriding $this->toArrayAfter().

This function is also utilized to serialize the object to JSON.


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/abstracts/abstract.llms.post.model.php

	public function toArray() {

		$arr = array(
			'id' => $this->get( 'id' ),
		);

		foreach ( $this->get_to_array_properties() as $prop ) {

			if ( in_array( $prop, array( 'content', 'excerpt', 'title' ), true ) ) {
				$post_prop    = "post_{$prop}";
				$arr[ $prop ] = $this->post->$post_prop;
			} else {
				$arr[ $prop ] = $this->get( $prop );
			}
		}

		// Add the featured image if the post type supports it.
		if ( post_type_supports( $this->db_post_type, 'thumbnail' ) ) {
			$arr['featured_image'] = $this->get_image( 'full', 'thumbnail' );
		}

		// Expand instructors if instructors are supported.
		if ( ! empty( $arr['instructors'] ) && method_exists( $this, 'instructors' ) ) {

			foreach ( $arr['instructors'] as &$data ) {
				$instructor = llms_get_instructor( $data['id'] );
				if ( $instructor ) {
					$data = array_merge( $data, $instructor->toArray() );
				}
			}
		} elseif ( ! empty( $arr['author'] ) ) {

			$instructor = llms_get_instructor( $arr['author'] );
			if ( $instructor ) {
				$arr['author'] = $instructor->toArray();
			}
		}

		/**
		 * Filter whether or not "extra" content should be included in the post array
		 *
		 * `__return_true` (with priority 99) is used to force the filter on during exports.
		 *
		 * @since 4.8.0
		 *
		 * @param boolean         $include Whether or not to include extra data. Default is `false`, except on during exports.
		 * @param LLMS_Post_Model $model   Post model instance.
		 */
		$add_array_extra = apply_filters( 'llms_post_model_to_array_add_extras', false, $this );

		/**
		 * Filter whether or not "extra" content should be included in the post array
		 *
		 * The dynamic portion of this hook, `$this->model_post_type`, refers to the model's post type. For example "course",
		 * "lesson", "membership", etc...
		 *
		 * @since 4.7.0
		 *
		 * @param boolean         $include Whether or not to include extra data.
		 * @param LLMS_Post_Model $model   Post model instance.
		 */
		$add_array_extra = apply_filters( "llms_{$this->model_post_type}_to_array_add_extras", $add_array_extra, $this );

		if ( $add_array_extra ) {
			$arr = $this->to_array_extra( $arr );
		}

		// Add custom fields.
		$arr = $this->toArrayCustom( $arr );

		// Allow extending classes to add properties easily without overriding the class.
		$arr = $this->toArrayAfter( $arr );

		$cpt_data = $this->get_post_type_data();
		if ( $cpt_data->public ) {
			$arr['permalink'] = get_permalink( $this->get( 'id' ) );
		}

		ksort( $arr ); // Because i'm anal...

		/**
		 * Filter the final post array created when converting the object to an array
		 *
		 * The dynamic portion of this hook, `$this->model_post_type`, refers to the model's post type. For example "course",
		 * "lesson", "membership", etc...
		 *
		 * @since 4.7.0
		 *
		 * @param array           $arr   Associative array of the model.
		 * @param LLMS_Post_Model $model Post model instance.
		 */
		return apply_filters( "llms_{$this->model_post_type}_to_array", $arr, $this );

	}

Top ↑

Changelog Changelog

Changelog
Version Description
5.4.1 Load properties to be used to generate the array from the new get_to_array_properties() method.
4.8.0 Exclude extra data by default. Added llms_post_model_to_array_add_extras filter.
4.7.0 Add exporting of extra data (images and blocks).
3.3.0
3.17.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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