| 177 | | $post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null; |
| 178 | | if ( ! empty( $this->post_ID ) ) // Potentially set by WP_Embed::cache_oembed() |
| 179 | | $post_ID = $this->post_ID; |
| 180 | | |
| 181 | | // Unknown URL format. Let oEmbed have a go. |
| 182 | | if ( $post_ID ) { |
| 183 | | |
| 184 | | // Check for a cached result (stored in the post meta) |
| 185 | | $key_suffix = md5( $url . serialize( $attr ) ); |
| 186 | | $cachekey = '_oembed_' . $key_suffix; |
| 187 | | $cachekey_time = '_oembed_time_' . $key_suffix; |
| 188 | | |
| 189 | | /** |
| 190 | | * Filter the oEmbed TTL value (time to live). |
| 191 | | * |
| 192 | | * @since 4.0.0 |
| 193 | | * |
| 194 | | * @param int $time Time to live (in seconds). |
| 195 | | * @param string $url The attempted embed URL. |
| 196 | | * @param array $attr An array of shortcode attributes. |
| 197 | | * @param int $post_ID Post ID. |
| 198 | | */ |
| 199 | | $ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID ); |
| 200 | | |
| 201 | | $cache = get_post_meta( $post_ID, $cachekey, true ); |
| 202 | | $cache_time = get_post_meta( $post_ID, $cachekey_time, true ); |
| 203 | | |
| 204 | | if ( ! $cache_time ) { |
| 205 | | $cache_time = 0; |
| 206 | | } |
| 207 | | |
| 208 | | $cached_recently = ( time() - $cache_time ) < $ttl; |
| 209 | | |
| 210 | | if ( $this->usecache || $cached_recently ) { |
| 211 | | // Failures are cached. Serve one if we're using the cache. |
| 212 | | if ( '{{unknown}}' === $cache ) |
| 213 | | return $this->maybe_make_link( $url ); |
| 214 | | |
| 215 | | if ( ! empty( $cache ) ) { |
| 216 | | /** |
| 217 | | * Filter the cached oEmbed HTML. |
| 218 | | * |
| 219 | | * @since 2.9.0 |
| 220 | | * |
| 221 | | * @see WP_Embed::shortcode() |
| 222 | | * |
| 223 | | * @param mixed $cache The cached HTML result, stored in post meta. |
| 224 | | * @param string $url The attempted embed URL. |
| 225 | | * @param array $attr An array of shortcode attributes. |
| 226 | | * @param int $post_ID Post ID. |
| 227 | | */ |
| 228 | | return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID ); |
| 229 | | } |
| 230 | | } |
| | 184 | // Check for a cached result (stored in the post meta) |
| | 185 | $cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); |
| 244 | | $html = wp_oembed_get( $url, $attr ); |
| 245 | | |
| 246 | | // Maybe cache the result |
| 247 | | if ( $html ) { |
| 248 | | update_post_meta( $post_ID, $cachekey, $html ); |
| 249 | | update_post_meta( $post_ID, $cachekey_time, time() ); |
| 250 | | } elseif ( ! $cache ) { |
| 251 | | update_post_meta( $post_ID, $cachekey, '{{unknown}}' ); |
| 252 | | } |
| | 200 | $cache = wp_oembed_get( $url, $attr ); |
| | 201 | } |
| 254 | | // If there was a result, return it |
| 255 | | if ( $html ) { |
| 256 | | /** This filter is documented in wp-includes/class-wp-embed.php */ |
| 257 | | return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID ); |
| | 203 | if ( $this->usecache || false !== $cache ) { |
| | 204 | // Failures are cached. Serve one if we're using the cache. |
| | 205 | if ( '{{unknown}}' === $cache ) |
| | 206 | return $this->maybe_make_link( $url ); |
| | 207 | |
| | 208 | if ( ! empty( $cache ) ) { |
| | 209 | /** |
| | 210 | * Filter the oEmbed TTL value (time to live). |
| | 211 | * |
| | 212 | * @since 4.0.0 |
| | 213 | * |
| | 214 | * @param int $time Time to live (in seconds). |
| | 215 | * @param string $url The attempted embed URL. |
| | 216 | * @param array $attr An array of shortcode attributes. |
| | 217 | * @param int $post_ID Post ID when set, otherwise 0. |
| | 218 | */ |
| | 219 | $ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, ( isset( $post_ID ) ? $post_ID : 0 ) ); |
| | 220 | |
| | 221 | /** |
| | 222 | * Set the oEmbed cache with the generated html |
| | 223 | * |
| | 224 | * @since 4.4 |
| | 225 | * |
| | 226 | * @param string $cachekey The generated cachekey based on $url and $atts |
| | 227 | * @param string $cache The generated oEmbed HTML |
| | 228 | * @param int $ttl The time to live for the transient |
| | 229 | */ |
| | 230 | set_transient( $cachekey, $cache, $ttl ); |
| | 231 | |
| | 232 | /** |
| | 233 | * Filter the cached oEmbed HTML. |
| | 234 | * |
| | 235 | * @since 2.9.0 |
| | 236 | * |
| | 237 | * @see WP_Embed::shortcode() |
| | 238 | * |
| | 239 | * @param mixed $cache The cached HTML result, stored in post meta. |
| | 240 | * @param string $url The attempted embed URL. |
| | 241 | * @param array $attr An array of shortcode attributes. |
| | 242 | * @param int $post_ID Post ID. |
| | 243 | */ |
| | 244 | return apply_filters( 'embed_oembed_html', $cache, $url, $attr, ( isset( $post_ID ) ? $post_ID : 0 ) ); |