Ticket #14759: 14759_2.patch
| File 14759_2.patch, 6.3 KB (added by , 13 years ago) |
|---|
-
wp-includes/media.php
1024 1024 * 1025 1025 * @param string $url The URL that should be embedded. 1026 1026 * @param array $args Additional arguments and parameters. 1027 * @param bool $use_cache Use a cached value if available. 1028 * @param int $post_id The post ID we're embedding into, or null 1027 1029 * @return bool|string False on failure or the embed HTML on success. 1028 1030 */ 1029 function wp_oembed_get( $url, $args = '' ) {1031 function wp_oembed_get( $url, $args = '', $use_cache = true, $post_id = null ) { 1030 1032 require_once( ABSPATH . WPINC . '/class-oembed.php' ); 1031 1033 $oembed = _wp_oembed_get_object(); 1032 return $oembed->get_html( $url, $args );1034 return $oembed->get_html( $url, $args, $use_cache, $post_id ); 1033 1035 } 1034 1036 1035 1037 /** -
wp-includes/class-oembed.php
66 66 * 67 67 * @param string $url The URL to the content that should be attempted to be embedded. 68 68 * @param array $args Optional arguments. Usually passed from a shortcode. 69 * @param bool $use_cache Use a cached value if available. 70 * @param int $post_ID The post ID we're embedding into - or null 69 71 * @return bool|string False on failure, otherwise the UNSANITIZED (and potentially unsafe) HTML that should be used to embed. 70 72 */ 71 function get_html( $url, $args = '' ) { 73 function get_html( $url, $args = '', $use_cache = true, $post_ID = null ) { 74 75 // Check for a cached result (stored in the post meta) 76 $cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); 77 $expirykey = '_oemexp_' . md5( $url . serialize( $attr ) ); 78 79 if ( $use_cache && ! empty( $post_ID ) ) { 80 81 $cache = get_post_meta( $post_ID, $cachekey, true ); 82 $cache_expiry = get_post_meta( $post_ID, $expirykey, true ); 83 84 if ( ! empty( $cache ) && ! empty( $cache_expiry ) && $cache_expiry > time() ) 85 return $cache; 86 87 } 88 72 89 $provider = false; 73 90 74 91 if ( !isset($args['discover']) ) … … 92 109 if ( !$provider && $args['discover'] ) 93 110 $provider = $this->discover( $url ); 94 111 95 if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) ) 96 return false; 112 if ( !$provider || false === $data = $this->fetch( $provider, $url, $args ) ) { 97 113 98 return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args ); 114 // Cache failures. 115 $return = '{{unknown}}'; 116 $expiry_time = time() + 12 * HOUR_IN_SECONDS ; 117 118 } else { 119 120 $return = apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args ); 121 $cache_age = ! empty ( $data->cache_age ) ? (int) $data->cache_age : 30 * DAY_IN_SECONDS; 122 $expiry_time = time() + $cache_age; 123 } 124 125 if ( ! empty( $post_ID ) ) { 126 update_post_meta( $post_ID, $cachekey, $return); 127 update_post_meta( $post_ID, $expirykey, $expiry_time); 128 } 129 130 return $return; 99 131 } 100 132 101 133 /** -
wp-includes/class-wp-embed.php
25 25 // Attempts to embed all URLs in a post 26 26 add_filter( 'the_content', array( $this, 'autoembed' ), 8 ); 27 27 28 // When a post is saved, invalidate the oEmbed cache29 add_action( 'pre_post_update', array( $this, 'delete_oembed_caches' ) );28 // When a post is saved, invalidate the oEmbed cache 29 add_action( 'pre_post_update', array( $this, 'delete_oembed_caches' ) ); 30 30 31 31 // After a post is saved, cache oEmbed items via AJAX 32 32 add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) ); … … 165 165 // Unknown URL format. Let oEmbed have a go. 166 166 if ( $post_ID ) { 167 167 168 // Check for a cached result (stored in the post meta)169 $cachekey = '_oembed_' . md5( $url . serialize( $attr ) );170 if ( $this->usecache ) {171 $cache = get_post_meta( $post_ID, $cachekey, true );172 173 // Failures are cached174 if ( '{{unknown}}' === $cache )175 return $this->maybe_make_link( $url );176 177 if ( ! empty( $cache ) )178 return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );179 }180 181 168 // Use oEmbed to get the HTML 182 169 $attr['discover'] = ( apply_filters('embed_oembed_discover', false) && author_can( $post_ID, 'unfiltered_html' ) ); 183 $html = wp_oembed_get( $url, $attr );170 $html = wp_oembed_get( $url, $attr, $this->usecache, $post_ID ); 184 171 185 // Cache the result 186 $cache = ( $html ) ? $html : '{{unknown}}'; 187 update_post_meta( $post_ID, $cachekey, $cache ); 172 // If there was a result, return it 173 if ( $html ) { 174 // Failures 175 if ( '{{unknown}}' === $html ) 176 return $this->maybe_make_link( $url ); 188 177 189 // If there was a result, return it190 if ( $html )191 178 return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID ); 179 } 192 180 } 193 181 194 182 // Still unknown … … 196 184 } 197 185 198 186 /** 199 * Delete all oEmbed caches.200 *201 * @param int $post_ID Post ID to delete the caches for.202 */203 function delete_oembed_caches( $post_ID ) {204 $post_metas = get_post_custom_keys( $post_ID );205 if ( empty($post_metas) )206 return;187 * Delete all oEmbed caches. 188 * 189 * @param int $post_ID Post ID to delete the caches for. 190 */ 191 function delete_oembed_caches( $post_ID ) { 192 $post_metas = get_post_custom_keys( $post_ID ); 193 if ( empty($post_metas) ) 194 return; 207 195 208 foreach( $post_metas as $post_meta_key ) { 209 if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) ) 210 delete_post_meta( $post_ID, $post_meta_key ); 211 } 212 } 196 foreach( $post_metas as $post_meta_key ) { 197 if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) || 198 '_oemexp_' == substr( $post_meta_key, 0, 9 ) ) 199 delete_post_meta( $post_ID, $post_meta_key ); 200 } 201 } 213 202 214 /** 203 /** 204 * 215 205 * Triggers a caching of all oEmbed results. 216 206 * 217 207 * @param int $post_ID Post ID to do the caching for.