class-learndash-model.php 8.46 KB
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
<?php
/**
 * This class provides the easy way to operate a post.
 *
 * @since 4.5.0
 *
 * @package LearnDash
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Learndash_Model' ) ) {
	/**
	 * Model class.
	 *
	 * @since 4.5.0
	 */
	abstract class Learndash_Model {
		/**
		 * Post.
		 *
		 * @since 4.5.0
		 *
		 * @var WP_Post
		 */
		protected $post;

		/**
		 * Mapped post meta fields.
		 *
		 * @since 4.5.0
		 *
		 * @var array<string,mixed>
		 */
		protected $attributes = array();

		/**
		 * Constructor.
		 *
		 * @since 4.5.0
		 *
		 * @return void
		 */
		final protected function __construct() {
			// Disallow overriding the constructor in child classes and instantiating.
		}

		/**
		 * Returns allowed post types.
		 *
		 * @since 4.5.0
		 *
		 * @return string[]
		 */
		abstract public static function get_allowed_post_types(): array;

		/**
		 * Creates a model from a post.
		 *
		 * @since 4.5.0
		 *
		 * @param WP_Post $post Post.
		 *
		 * @throws InvalidArgumentException If the post has a wrong post type.
		 *
		 * @return static
		 */
		public static function create_from_post( WP_Post $post ): self {
			$model = new static();

			$model->set_post( $post );

			// Map post meta fields to attributes.

			$post_meta = get_post_meta( $post->ID );

			if ( is_array( $post_meta ) && ! empty( $post_meta ) ) {
				$attributes = array_map(
					function( array $meta_values ) {
						$meta_values = array_map(
							function ( $meta_value ) {
								return maybe_unserialize( $meta_value );
							},
							$meta_values
						);

						return count( $meta_values ) > 1 ? $meta_values : $meta_values[0];
					},
					$post_meta
				);

				$model->set_attributes( $attributes );
			}

			return $model;
		}

		/**
		 * Returns a post ID.
		 *
		 * @since 4.5.0
		 *
		 * @return int
		 */
		public function get_id(): int {
			return $this->post->ID;
		}

		/**
		 * Returns a post property.
		 *
		 * @since 4.5.0
		 *
		 * @return WP_Post
		 */
		public function get_post(): WP_Post {
			return $this->post;
		}

		/**
		 * Returns true if an attribute exists. Otherwise, false.
		 *
		 * @since 4.5.0
		 *
		 * @param string $attribute_name Attribute name.
		 *
		 * @return bool
		 */
		public function has_attribute( string $attribute_name ): bool {
			return isset( $this->attributes[ $attribute_name ] );
		}

		/**
		 * Removes an attribute.
		 *
		 * @since 4.5.0
		 *
		 * @param string $attribute_name Attribute name.
		 *
		 * @return void
		 */
		public function remove_attribute( string $attribute_name ): void {
			unset( $this->attributes[ $attribute_name ] );
		}

		/**
		 * Removes all attributes.
		 *
		 * @since 4.5.0
		 *
		 * @return void
		 */
		public function clear_attributes(): void {
			$this->attributes = array();
		}

		/**
		 * Returns all attributes.
		 *
		 * @since 4.5.0
		 *
		 * @return array<string,mixed>
		 */
		public function get_attributes(): array {
			return $this->attributes;
		}

		/**
		 * Returns an attribute value or null if not found.
		 *
		 * @since 4.5.0
		 *
		 * @param string $attribute_name Attribute name.
		 * @param mixed  $default        Default value.
		 *
		 * @return mixed
		 */
		public function get_attribute( string $attribute_name, $default = null ) {
			return $this->attributes[ $attribute_name ] ?? $default;
		}

		/**
		 * Returns an attribute value or null if not found.
		 *
		 * @since 4.5.0
		 *
		 * @param string $attribute_name  Attribute name.
		 * @param mixed  $attribute_value Attribute value.
		 *
		 * @return void
		 */
		public function set_attribute( string $attribute_name, $attribute_value ): void {
			$this->attributes[ $attribute_name ] = $attribute_value;
		}

		/**
		 * Sets multiple attributes.
		 *
		 * @since 4.5.0
		 *
		 * @param array<string,mixed> $attributes Attributes. Keys are attribute names, values are attribute values.
		 *
		 * @return void
		 */
		public function set_attributes( array $attributes ): void {
			foreach ( $attributes as $attribute_name => $attribute_value ) {
				$this->set_attribute( (string) $attribute_name, $attribute_value );
			}
		}

		/**
		 * Returns true if it's a parent post, false otherwise.
		 *
		 * @since 4.5.0
		 *
		 * @return bool
		 */
		public function is_parent(): bool {
			/**
			 * Filters whether a model is a parent.
			 *
			 * @since 4.5.0
			 *
			 * @param bool    $is_parent True if it's a parent model, false otherwise.
			 * @param WP_Post $post      Post.
			 *
			 * @return bool True if it's a parent model, false otherwise.
			 */
			return apply_filters(
				'learndash_model_is_parent',
				0 === $this->post->post_parent,
				$this->post
			);
		}

		/**
		 * Returns a parent model.
		 *
		 * @since 4.5.0
		 *
		 * @return static|null
		 */
		public function get_parent(): ?self {
			if ( $this->is_parent() ) {
				return null;
			}

			return static::find( $this->post->post_parent );
		}

		/**
		 * Returns child models.
		 *
		 * @since 4.5.0
		 *
		 * @return static[]
		 */
		public function get_children(): array {
			if ( ! $this->is_parent() ) {
				return array();
			}

			/**
			 * Posts.
			 *
			 * @var WP_Post[] $posts
			 */
			$posts = get_children(
				array(
					'post_parent' => $this->post->ID,
					'post_type'   => static::get_allowed_post_types(),
					'numberposts' => -1,
				)
			);

			return self::map_posts_to_models( $posts );
		}

		/**
		 * Finds by ids.
		 *
		 * @since 4.5.0
		 *
		 * @param int[] $post_ids Post IDs.
		 *
		 * @return static[]
		 */
		public static function find_many( array $post_ids ): array {
			if ( empty( $post_ids ) ) {
				return array();
			}

			$posts = get_posts(
				array(
					'post__in'    => wp_parse_id_list( $post_ids ),
					'post_type'   => static::get_allowed_post_types(),
					'post_status' => 'any',
					'numberposts' => -1,
				)
			);

			return self::map_posts_to_models( $posts );
		}

		/**
		 * Finds by id.
		 *
		 * @since 4.5.0
		 *
		 * @param int $post_id Post ID.
		 *
		 * @return static|null
		 */
		public static function find( int $post_id ): ?self {
			if ( $post_id <= 0 ) {
				return null;
			}

			$models = self::find_many( array( $post_id ) );

			return ! empty( $models ) ? $models[0] : null;
		}

		/**
		 * Finds by meta
		 *
		 * @since 4.5.0
		 *
		 * @param array<string,mixed> $meta Meta list. Keys are meta keys, values are meta values.
		 *
		 * @return static[]
		 */
		public static function find_many_by_meta( array $meta = array() ): array {
			if ( empty( $meta ) ) {
				return array();
			}

			$meta_query = array();
			foreach ( $meta as $key => $value ) {
				$meta_query[] = array(
					'key'   => $key,
					'value' => $value,
				);
			}

			$posts = get_posts(
				array(
					'post_type'   => static::get_allowed_post_types(),
					'post_status' => 'any',
					'numberposts' => -1,
					'meta_query'  => $meta_query,
				)
			);

			return self::map_posts_to_models( $posts );
		}

		/**
		 * Maps posts into models.
		 *
		 * @since 4.5.0
		 *
		 * @param WP_Post[] $posts Posts.
		 *
		 * @return static[]
		 */
		protected static function map_posts_to_models( array $posts ): array {
			$posts = array_filter(
				$posts,
				function ( $post ) {
					return $post instanceof WP_Post;
				}
			);

			if ( empty( $posts ) ) {
				return array();
			}

			$models = array();

			foreach ( $posts as $post ) {
				try {
					$models[] = static::create_from_post( $post );
				} catch ( InvalidArgumentException $e ) { // @phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
					// Do nothing.
				}
			}

			return $models;
		}

		/**
		 * Sets a post property.
		 *
		 * @since 4.5.0
		 *
		 * @param WP_Post $post Post.
		 *
		 * @throws InvalidArgumentException If the post has a wrong post type.
		 *
		 * @return void
		 */
		private function set_post( WP_Post $post ): void {
			$allowed_post_types = static::get_allowed_post_types();

			/**
			 * Filters model allowed post types.
			 *
			 * @since 4.5.0
			 *
			 * @param string[] $allowed_post_types Allowed post types.
			 * @param string   $model_class        Model class.
			 * @param WP_Post  $post               Post.
			 *
			 * @return string[] Allowed post types.
			 */
			$allowed_post_types = apply_filters(
				'learndash_model_allowed_post_types',
				$allowed_post_types,
				static::class,
				$post
			);

			if ( ! in_array( $post->post_type, $allowed_post_types, true ) ) {
				throw new InvalidArgumentException(
					sprintf(
						'Invalid post type "%s". Allowed post types: "%s".',
						$post->post_type,
						implode( ', ', $allowed_post_types )
					)
				);
			}

			$this->post = $post;
		}
	}
}