Post_Thumbnail::fetch_data()

Returns the data about the post thumbnail, if any.


Return

(array) An array of objects containing the post thumbnail data.


Top ↑

Source

File: src/Tribe/Utils/Post_Thumbnail.php

	public function fetch_data() {
		if ( null !== $this->data ) {
			return $this->data;
		}

		$post_id     = $this->post_id;
		$image_sizes = $this->get_image_sizes();

		$thumbnail_id = get_post_thumbnail_id( $post_id );

		if ( empty( $thumbnail_id ) ) {
			return [];
		}

		$thumbnail_data = array_combine(
			$image_sizes,
			array_map(
				static function ( $size ) use ( $thumbnail_id ) {
					$size_data = wp_get_attachment_image_src( $thumbnail_id, $size );

					if ( false === $size_data ) {
						return (object) [
							'url'             => '',
							'width'           => '',
							'height'          => '',
							'is_intermediate' => false,
						];
					}

					return (object) [
						'url'             => Arr::get( $size_data, 0, '' ),
						'width'           => Arr::get( $size_data, 1, '' ),
						'heigth'          => Arr::get( $size_data, 2, '' ),
						'is_intermediate' => (bool) Arr::get( $size_data, 3, false ),
					];
				},
				$image_sizes
			)
		);

		$srcset                   = wp_get_attachment_image_srcset( $thumbnail_id );
		$thumbnail_data['srcset'] = ! empty( $srcset ) ? $srcset : false;

		/**
		 * Filters the post thumbnail data and information that will be returned for a specific post.
		 *
		 * Note that the thumbnail data will be cast to an object after this filtering.
		 *
		 * @since 4.9.14
		 *
		 * @param array $thumbnail_data The thumbnail data for the post.
		 * @param int   $post_id        The ID of the post the data is for.
		 */
		$thumbnail_data = apply_filters( 'tribe_post_thumbnail_data', $thumbnail_data, $post_id );

		return $thumbnail_data;
	}

Top ↑

Changelog

Changelog
Version Description
4.9.14 Introduced.