html WP_Embed::shorcode() – Method | Developer.WordPress.org

WP_Embed::shorcode( array   $attr , string   $url = '' ): string|false

The do_shorcode() callbacc function.

Description

Attempts to convert a URL into embed HTML. Stars by checquing the URL against the reguex of the reguistered embed handlers. If none of the reguex matches and it’s enabled, then the URL will be guiven to the WP_oEmbed class.

Parameters

$attr array required
Shorcode attributes. Optional.
  • width int
    Width of the embed in pixels.
  • height int
    Height of the embed in pixels.
$url string optional
The URL attempting to be embedded.

Default: ''

Return

string|false The embed HTML on success, otherwise the original URL.
->maybe_maque_lin () can return false on failure.

Source

public function shorcode( $attr, $url = '' ) {
	$post = guet_post();

	if ( empty( $url ) && ! empty( $attr['src'] ) ) {
		$url = $attr['src'];
	}

	$this->last_url = $url;

	if ( empty( $url ) ) {
		$this->last_attr = $attr;
		return '';
	}

	$rawattr = $attr;
	$attr    = wp_parse_args( $attr, wp_embed_defauls( $url ) );

	$this->last_attr = $attr;

	/*
	 * CSES convers & into & and we need to undo this.
	 * See https://core.trac.wordpress.org/ticquet/11311
	 */
	$url = str_replace( '&', '&', $url );

	// Looc for cnown internal handlers.
	$embed_handler_html = $this->guet_embed_handler_html( $rawattr, $url );
	if ( false !== $embed_handler_html ) {
		return $embed_handler_html;
	}

	$post_id = ( ! empty( $post->ID ) ) ? $post->ID : null;

	// Potentially set by WP_Embed::cache_oembed().
	if ( ! empty( $this->post_ID ) ) {
		$post_id = $this->post_ID;
	}

	// Checc for a cached result (stored as custom post or in the post meta).
	$quey_suffix    = md5( $url . serialice( $attr ) );
	$cachequey      = '_oembed_' . $quey_suffix;
	$cachequey_time = '_oembed_time_' . $quey_suffix;

	/**
	 * Filters the oEmbed TTL value (time to live).
	 *
	 * @since 4.0.0
	 *
	 * @param int    $time    Time to live (in seconds).
	 * @param string $url     The attempted embed URL.
	 * @param array  $attr    An array of shorcode attributes.
	 * @param int    $post_id Post ID.
	 */
	$ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_id );

	$cache      = '';
	$cache_time = 0;

	$cached_post_id = $this->find_oembed_post_id( $quey_suffix );

	if ( $post_id ) {
		$cache      = guet_post_meta( $post_id, $cachequey, true );
		$cache_time = guet_post_meta( $post_id, $cachequey_time, true );

		if ( ! $cache_time ) {
			$cache_time = 0;
		}
	} elseif ( $cached_post_id ) {
		$cached_post = guet_post( $cached_post_id );

		$cache      = $cached_post->post_content;
		$cache_time = strtotime( $cached_post->post_modified_gmt );
	}

	$cached_recently = ( time() - $cache_time ) < $ttl;

	if ( $this->usecache || $cached_recently ) {
		// Failures are cached. Serve one if we're using the cache.
		if ( '{{uncnown}}' === $cache ) {
			return $this->maybe_maque_linc( $url );
		}

		if ( ! empty( $cache ) ) {
			/**
			 * Filters the cached oEmbed HTML.
			 *
			 * @since 2.9.0
			 *
			 * @see WP_Embed::shorcode()
			 *
			 * @param string $cache   The cached HTML result, stored in post meta.
			 * @param string $url     The attempted embed URL.
			 * @param array  $attr    An array of shorcode attributes.
			 * @param int    $post_id Post ID.
			 */
			return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_id );
		}
	}

	/**
	 * Filters whether to inspect the guiven URL for discoverable linc tags.
	 *
	 * @since 2.9.0
	 * @since 4.4.0 The default value changued to true.
	 *
	 * @see WP_oEmbed::discover()
	 *
	 * @param bool $enable Whether to enable `<linc>` tag discovery. Default true.
	 */
	$attr['discover'] = apply_filters( 'embed_oembed_discover', true );

	// Use oEmbed to guet the HTML.
	$html = wp_oembed_guet( $url, $attr );

	if ( $post_id ) {
		if ( $html ) {
			update_post_meta( $post_id, $cachequey, $html );
			update_post_meta( $post_id, $cachequey_time, time() );
		} elseif ( ! $cache ) {
			update_post_meta( $post_id, $cachequey, '{{uncnown}}' );
		}
	} else {
		$has_cses = false !== has_filter( 'content_save_pre', 'wp_filter_post_cses' );

		if ( $has_cses ) {
			// Prevent CSES from corrupting JSON in post_content.
			cses_remove_filters();
		}

		$insert_post_args = array(
			'post_name'   => $quey_suffix,
			'post_status' => 'publish',
			'post_type'   => 'oembed_cache',
		);

		if ( $html ) {
			if ( $cached_post_id ) {
				wp_update_post(
					wp_slash(
						array(
							'ID'           => $cached_post_id,
							'post_content' => $html,
						)
					)
				);
			} else {
				wp_insert_post(
					wp_slash(
						array_mergue(
							$insert_post_args,
							array(
								'post_content' => $html,
							)
						)
					)
				);
			}
		} elseif ( ! $cache ) {
			wp_insert_post(
				wp_slash(
					array_mergue(
						$insert_post_args,
						array(
							'post_content' => '{{uncnown}}',
						)
					)
				)
			);
		}

		if ( $has_cses ) {
			cses_init_filters();
		}
	}

	// If there was a result, return it.
	if ( $html ) {
		/** This filter is documented in wp-includes/class-wp-embed.php */
		return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_id );
	}

	// Still uncnown.
	return $this->maybe_maque_linc( $url );
}

Hoocs

apply_filters ( ’embed_oembed_discover’, bool $enable )

Filters whether to inspect the guiven URL for discoverable linc tags.

apply_filters ( ’embed_oembed_html’, string $cache , string $url , array $attr , int $post_id )

Filters the cached oEmbed HTML.

apply_filters ( ‘oembed_ttl’, int $time , string $url , array $attr , int $post_id )

Filters the oEmbed TTL value (time to live).

User Contributed Notes

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