GC_Embed::shortcode()

最后更新于:2021-11-27 14:41:31

GC_Embed::shortcode( array$attr, string$url=”)

The do_shortcode() callback function.

参数

$attr

(array) (Required) Shortcode 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 value: ”

响应

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

源文件

文件: gc-includes/class-gc-embed.php

	public function shortcode( $attr, $url = '' ) {
		$post = get_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    = gc_parse_args( $attr, gc_embed_defaults( $url ) );

		$this->last_attr = $attr;

		// KSES converts & into & and we need to undo this.
		// See https://core.trac.gechiui.org/ticket/11311
		$url = str_replace( '&', '&', $url );

		// Look for known internal handlers.
		$embed_handler_html = $this->get_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 GC_Embed::cache_oembed().
		if ( ! empty( $this->post_ID ) ) {
			$post_ID = $this->post_ID;
		}

		// Check for a cached result (stored as custom post or in the post meta).
		$key_suffix    = md5( $url . serialize( $attr ) );
		$cachekey      = '_oembed_' . $key_suffix;
		$cachekey_time = '_oembed_time_' . $key_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 shortcode 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( $key_suffix );

		if ( $post_ID ) {
			$cache      = get_post_meta( $post_ID, $cachekey, true );
			$cache_time = get_post_meta( $post_ID, $cachekey_time, true );

			if ( ! $cache_time ) {
				$cache_time = 0;
			}
		} elseif ( $cached_post_id ) {
			$cached_post = get_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 ( '{{unknown}}' === $cache ) {
				return $this->maybe_make_link( $url );
			}

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

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

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

		if ( $post_ID ) {
			if ( $html ) {
				update_post_meta( $post_ID, $cachekey, $html );
				update_post_meta( $post_ID, $cachekey_time, time() );
			} elseif ( ! $cache ) {
				update_post_meta( $post_ID, $cachekey, '{{unknown}}' );
			}
		} else {
			$has_kses = false !== has_filter( 'content_save_pre', 'gc_filter_post_kses' );

			if ( $has_kses ) {
				// Prevent KSES from corrupting JSON in post_content.
				kses_remove_filters();
			}

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

			if ( $html ) {
				if ( $cached_post_id ) {
					gc_update_post(
						gc_slash(
							array(
								'ID'           => $cached_post_id,
								'post_content' => $html,
							)
						)
					);
				} else {
					gc_insert_post(
						gc_slash(
							array_merge(
								$insert_post_args,
								array(
									'post_content' => $html,
								)
							)
						)
					);
				}
			} elseif ( ! $cache ) {
				gc_insert_post(
					gc_slash(
						array_merge(
							$insert_post_args,
							array(
								'post_content' => '{{unknown}}',
							)
						)
					)
				);
			}

			if ( $has_kses ) {
				kses_init_filters();
			}
		}

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

		// Still unknown.
		return $this->maybe_make_link( $url );
	}