model.llms.post.instructors.php 4.1 KB
Newer Older
cyrille's avatar
cyrille committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
<?php
/**
 * LLMS Post Instructors
 *
 * @package LifterLMS/Models/Classes
 *
 * @since 3.13.0
 * @version 4.2.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * LLMS_Post_Instructors class
 *
 * Allow interactions with the custom multi-author functionality
 * currently enabled for Courses and Memberships only.
 *
 * Rather than instantiating this class directly
 * you should use LLMS_Course->instructors() or LLMS_Membership()->instructors()
 *
 * @since 3.13.0
 * @since 3.30.3 Explicitly define class properties.
 * @since 4.0.0  Remove deprecated method `get_defaults()`.
 * @since 4.2.0 Normalized return structure in `get_instructors()` when no instructor set.
 */
class LLMS_Post_Instructors {

	/**
	 * WP Post ID
	 *
	 * @var int
	 */
	public $id;

	/**
	 * Instance of the post (course or membership)
	 *
	 * @var LLMS_Post_Model
	 */
	public $post;

	/**
	 * Constructor
	 *
	 * @since 3.13.0
	 *
	 * @param LLMS_Post_Model|WP_Post|int $post Post object or ID.
	 */
	public function __construct( $post ) {

		// Setup a post if post id of WP_Post is passed in.
		if ( is_numeric( $post ) || is_a( $post, 'WP_Post' ) ) {
			$post = llms_get_post( $post );
		}

		// Double check we have an LLMS_Post.
		if ( is_subclass_of( $post, 'LLMS_Post_Model' ) ) {
			$this->post = $post;
			$this->id   = $post->get( 'id' );
		}

	}

	/**
	 * Retrieve course instructor information
	 *
	 * @since 3.13.0
	 * @since 3.23.0 Unknown.
	 * @since 4.2.0 Normalize return data when no instructor data is saved.
	 *
	 * @param boolean $exclude_hidden If true, excludes hidden instructors from the return array.
	 * @return array[] {
	 *     Array or instructor data arrays.
	 *
	 *     @type int    $id         WP_User ID of the instructor user.
	 *     @type string $visibility Display visibility option for the instructor.
	 *     @type string $label      User input display noun for the instructor. EG: "Author" or "Coach" or "Instructor".
	 *     @type string $name       WP_User Display Name.
	 * }
	 */
	public function get_instructors( $exclude_hidden = false ) {

		$instructors = $this->post->get( 'instructors' );

		// If empty, respond with the course author in an array.
		if ( ! $instructors ) {
			$author_id   = $this->post->get( 'author' );
			$author      = get_userdata( $author_id );
			$instructors = array(
				wp_parse_args(
					array(
						'id'   => $author_id,
						'name' => $author ? $author->display_name : '',
					),
					llms_get_instructors_defaults()
				),
			);
		}

		if ( $exclude_hidden ) {
			foreach ( $instructors as $key => $instructor ) {
				if ( 'hidden' === $instructor['visibility'] ) {
					unset( $instructors[ $key ] );
				}
			}
		}

		return $instructors;

	}

	/**
	 * Format an instructors array for saving to the db.
	 *
	 * @since 3.25.0
	 *
	 * @param array $instructors Array of full (or partial) instructor data.
	 * @return array
	 */
	public function pre_set_instructors( $instructors = array() ) {

		/**
		 * We cannot allow no instructors to exist
		 * so we'll revert to the default `post_author`.
		 */
		if ( ! $instructors ) {
			// Clear so the getter will retrieve the default author.
			$this->post->set( 'instructors', array() );
			$instructors = $this->get_instructors();
		}

		// Allow partial arrays to be passed & we'll fill em up with defaults.
		foreach ( $instructors as $i => &$instructor ) {

			$instructor       = wp_parse_args( $instructor, llms_get_instructors_defaults() );
			$instructor['id'] = absint( $instructor['id'] );

			// Remove instructors without an ID.
			if ( empty( $instructor['id'] ) ) {
				unset( $instructors[ $i ] );
			}
		}

		return array_values( $instructors );

	}

	/**
	 * Save instructor information
	 *
	 * @since 3.13.0
	 * @since 3.25.0 Unknown.
	 *
	 * @param array $instructors Array of course instructor information.
	 */
	public function set_instructors( $instructors = array() ) {

		$instructors = $this->pre_set_instructors( $instructors );

		// Set the post_author to be the first author in the array.
		$this->post->set( 'author', $instructors[0]['id'] );

		// Save the instructors array.
		$this->post->set( 'instructors', $instructors );

		return $instructors;

	}

}