Make WordPress Core

Changeset 12153


Ignore:
Timestamp:
11/06/2009 02:22:23 PM (15 years ago)
Author:
ryan
Message:

Add a bunch of new filters. Use one of them to remove new lines from Scribd embeds. Invalid oEmbed post meta cache without using JS. Props Viper007Bond. see #10337

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-oembed.php

    r12138 r12153  
    4848            'http://i*.photobucket.com/albums/*'    => array( 'http://photobucket.com/oembed',            false ),
    4949            'http://gi*.photobucket.com/groups/*'   => array( 'http://photobucket.com/oembed',            false ),
    50             '#http://(www\.)?scribd.com/.*#i'       => array( 'http://www.scribd.com/services/oembed',    true)
     50            '#http://(www\.)?scribd.com/.*#i'       => array( 'http://www.scribd.com/services/oembed',    true ),
    5151        ) );
     52
     53        // Fix Scribd embeds. They contain new lines in the middle of the HTML which breaks wpautop().
     54        add_filter( 'oembed_dataparse', array(&$this, 'strip_scribd_newlines'), 10, 3 );
    5255    }
    5356
     
    8891            return false;
    8992
    90         return apply_filters( 'oembed_output', $this->data2html( $data, $url ), $url, $args );
     93        return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );
    9194    }
    9295
     
    207210
    208211                $title = ( !empty($data->title) ) ? $data->title : '';
    209                 return '<img src="' . esc_attr( clean_url( $data->url ) ) . '" alt="' . esc_attr($title) . '" width="' . esc_attr($data->width) . '" height="' . esc_attr($data->height) . '" />';
     212                $return = '<img src="' . esc_attr( clean_url( $data->url ) ) . '" alt="' . esc_attr($title) . '" width="' . esc_attr($data->width) . '" height="' . esc_attr($data->height) . '" />';
     213                break;
    210214
    211215            case 'video':
    212216            case 'rich':
    213                 return ( !empty($data->html) ) ? $data->html : false;
     217                $return = ( !empty($data->html) ) ? $data->html : false;
     218                break;
    214219
    215220            case 'link':
    216                 return ( !empty($data->title) ) ? '<a href="' . clean_url($url) . '">' . esc_html($data->title) . '</a>' : false;
    217         }
    218 
    219         return false;
     221                $return = ( !empty($data->title) ) ? '<a href="' . clean_url($url) . '">' . esc_html($data->title) . '</a>' : false;
     222                break;
     223
     224            default;
     225                $return = false;
     226        }
     227
     228        // You can use this filter to add support for custom data types or to filter the result
     229        return apply_filters( 'oembed_dataparse', $return, $data, $url );
     230    }
     231
     232    /**
     233     * Strip new lines from the HTML if it's a Scribd embed.
     234     *
     235     * @param string $html Existing HTML.
     236     * @param object $data Data object from WP_oEmbed::data2html()
     237     * @param string $url The original URL passed to oEmbed.
     238     * @return string Possibly modified $html
     239     */
     240    function strip_scribd_newlines( $html, $data, $url ) {
     241        if ( preg_match( '#http://(www\.)?scribd.com/.*#i', $url ) )
     242            $html = str_replace( array( "\r\n", "\n" ), '', $html );
     243
     244        return $html;
    220245    }
    221246}
  • trunk/wp-includes/media.php

    r12136 r12153  
    928928            add_filter( 'the_content', array(&$this, 'autoembed'), 8 );
    929929
     930        // After a post is saved, invalidate the oEmbed cache
     931        add_action( 'save_post', array(&$this, 'delete_oembed_caches') );
     932
    930933        // After a post is saved, cache oEmbed items via AJAX
    931934        add_action( 'edit_form_advanced', array(&$this, 'maybe_run_ajax_cache') );
     
    10491052                if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
    10501053                    if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) )
    1051                         return $return;
     1054                        return apply_filters( 'embed_handler_html', $return, $url, $attr );
    10521055                }
    10531056            }
     
    10711074
    10721075                if ( !empty($cache) )
    1073                     return $cache;
     1076                    return apply_filters( 'embed_oembed_html', $cache, $url, $attr );
    10741077            }
    10751078
     
    10871090            // If there was a result, return it
    10881091            if ( $html )
    1089                 return $html;
     1092                return apply_filters( 'embed_oembed_html', $html, $url, $attr );
    10901093        }
    10911094
    10921095        // Still unknown
    10931096        return $this->maybe_make_link( $url );
     1097    }
     1098
     1099    /**
     1100     * Delete all oEmbed caches.
     1101     *
     1102     * @param int $post_ID Post ID to delete the caches for.
     1103     */
     1104    function delete_oembed_caches( $post_ID ) {
     1105        $post_metas = get_post_custom_keys( $post_ID );
     1106        if ( empty($post_metas) )
     1107            return;
     1108        foreach( (array) $post_metas as $post_meta_key ) {
     1109            if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) )
     1110                delete_post_meta( $post_ID, $post_meta_key );
     1111        }
    10941112    }
    10951113
     
    11021120        $post = get_post( $post_ID );
    11031121
    1104         // post_type check is incase of "save_post" usage
    11051122        if ( empty($post->ID) || !in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', array( 'post', 'page' ) ) ) )
    11061123            return;
    1107 
    1108         // Dump existing caches
    1109         $post_metas = get_post_custom_keys( $post->ID );
    1110         foreach( $post_metas as $post_meta_key ) {
    1111             if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) )
    1112                 delete_post_meta( $post->ID, $post_meta_key );
    1113         }
    11141124
    11151125        // Trigger a caching
     
    11621172     */
    11631173    function maybe_make_link( $url ) {
    1164         return ( $this->linkifunknown ) ? '<a href="' . esc_attr($url) . '">' . esc_html($url) . '</a>' : $url;
     1174        $output = ( $this->linkifunknown ) ? '<a href="' . esc_attr($url) . '">' . esc_html($url) . '</a>' : $url;
     1175        return apply_filters( 'embed_maybe_make_link', $output, $url );
    11651176    }
    11661177}
Note: See TracChangeset for help on using the changeset viewer.