Make WordPress Core

Ticket #14493: 14493.diff

File 14493.diff, 6.6 KB (added by mdawaffe, 15 years ago)
  • wp-includes/default-filters.php

     
    229229add_action( 'transition_post_status',     '_transition_post_status',  5, 3 );
    230230add_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce'        );
    231231add_action( 'wp_scheduled_delete',        'wp_scheduled_delete'            );
     232add_action( 'pre_enclose',                'dont_enclose_root_urls'         );
    232233
    233234// Navigation menu actions
    234235add_action( 'delete_post',                '_wp_delete_post_menu_item'      );
  • wp-includes/functions.php

     
    11601160 * @param string $content Post Content
    11611161 * @param int $post_ID Post ID
    11621162 */
    1163 function do_enclose( $content, $post_ID ) {
     1163function do_enclose( $content, $post_id ) {
    11641164        global $wpdb;
    11651165        include_once( ABSPATH . WPINC . '/class-IXR.php' );
    11661166
    11671167        $log = debug_fopen( ABSPATH . 'enclosures.log', 'a' );
    1168         $post_links = array();
    11691168        debug_fwrite( $log, 'BEGIN ' . date( 'YmdHis', time() ) . "\n" );
    11701169
    1171         $pung = get_enclosed( $post_ID );
     1170        $pung = get_enclosed( $post_id );
    11721171
    11731172        $ltrs = '\w';
    11741173        $gunk = '/#~:.?+=&%@!\-';
     
    11771176
    11781177        preg_match_all( "{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp );
    11791178
     1179        // Don't ping the same link multiple times
     1180        $post_links = array_unique( $post_links_temp[0] );
     1181        // Only links we haven't pung
     1182        $post_links = array_diff( $post_links, $pung );
     1183        // Only links that are URLs
     1184        $post_links = array_filter( $post_links, 'parse_url' ); // This isn't a good check: only catches odd stuff (bool) parse_url( "url" ), for example, is true
     1185
    11801186        debug_fwrite( $log, 'Post contents:' );
    11811187        debug_fwrite( $log, $content . "\n" );
    11821188
    11831189        foreach ( $pung as $link_test ) {
    1184                 if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
    1185                         $mid = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $link_test . '%') );
     1190                if ( !in_array( $link_test, $post_links ) ) { // link no longer in post
     1191                        $mid = $wpdb->get_col( $wpdb->prepare(
     1192                                "SELECT meta_id FROM `$wpdb->postmeta` WHERE `post_id` = %d AND `meta_key` = 'enclosure' AND `meta_value` LIKE (%s)",
     1193                                $post_id,
     1194                                like_escape( $link_test ) . '%' // Delete everything for which this URL is a prefix?
     1195                        ) );
    11861196                        do_action( 'delete_postmeta', $mid );
    1187                         $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $mid ) ) );
     1197                        $_mid = join( ',', array_filter( array_map( 'absint', $mid ) ) );
     1198                        $wpdb->query( "DELETE FROM `$wpdb->postmeta` WHERE `meta_id` IN($_mid)" );
    11881199                        do_action( 'deleted_postmeta', $mid );
    11891200                }
    11901201        }
    11911202
    1192         foreach ( (array) $post_links_temp[0] as $link_test ) {
    1193                 if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already
    1194                         $test = @parse_url( $link_test );
    1195                         if ( false === $test )
    1196                                 continue;
    1197                         if ( isset( $test['query'] ) )
    1198                                 $post_links[] = $link_test;
    1199                         elseif ( $test['path'] != '/' && $test['path'] != '' )
    1200                                 $post_links[] = $link_test;
    1201                 }
    1202         }
     1203        do_action_ref_array( 'pre_enclose', array( &$post_links, &$pung ) );
    12031204
     1205        $allowed_types = apply_filters( 'enclosure_mime_types', array( 'video', 'audio' ) );
     1206        $extension_map = get_allowed_mime_types();
     1207        $meta_key = 'enclosure';
     1208
    12041209        foreach ( (array) $post_links as $url ) {
    1205                 if ( $url != '' && !$wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $url . '%' ) ) ) {
     1210                if ( !$url || !$url_parts = parse_url( $url ) )
     1211                        continue;
    12061212
    1207                         if ( $headers = wp_get_http_headers( $url) ) {
    1208                                 $len = (int) $headers['content-length'];
    1209                                 $type = $headers['content-type'];
    1210                                 $allowed_types = array( 'video', 'audio' );
     1213                // Done it or something like it
     1214                if ( $wpdb->get_var( $wpdb->prepare(
     1215                        "SELECT `post_id` FROM `$wpdb->postmeta` WHERE `post_id` = %d AND `meta_key` = 'enclosure' AND `meta_value` LIKE (%s)",
     1216                        $post_id,
     1217                        like_escape( $url ) . '%'
     1218                ) ) )
     1219                        continue;
    12111220
    1212                                 // Check to see if we can figure out the mime type from
    1213                                 // the extension
    1214                                 $url_parts = @parse_url( $url );
    1215                                 if ( false !== $url_parts ) {
    1216                                         $extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
    1217                                         if ( !empty( $extension ) ) {
    1218                                                 foreach ( get_allowed_mime_types( ) as $exts => $mime ) {
    1219                                                         if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
    1220                                                                 $type = $mime;
    1221                                                                 break;
    1222                                                         }
    1223                                                 }
     1221                if ( !$headers = wp_get_http_headers( $url ) )
     1222                        continue;
     1223
     1224                $len = (int) $headers['content-length'];
     1225                $type = $headers['content-type'];
     1226
     1227                // Check to see if we can figure out the mime type from
     1228                // the extension
     1229                if ( false !== $url_parts ) {
     1230                        $extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
     1231                        if ( !empty( $extension ) ) {
     1232                                foreach ( $extension_map as $exts => $mime ) {
     1233                                        if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
     1234                                                $type = $mime;
     1235                                                break;
    12241236                                        }
    12251237                                }
    1226 
    1227                                 if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
    1228                                         $meta_value = "$url\n$len\n$type\n";
    1229                                         $wpdb->insert($wpdb->postmeta, array('post_id' => $post_ID, 'meta_key' => 'enclosure', 'meta_value' => $meta_value) );
    1230                                         do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, 'enclosure', $meta_value );
    1231                                 }
    12321238                        }
    12331239                }
     1240
     1241                list( $type ) = explode( '/', $type, 2 );
     1242
     1243                if ( !in_array( $type, $allowed_types ) )
     1244                        continue;
     1245
     1246                $meta_value = "$url\n$len\n$type\n";
     1247                $wpdb->insert( $wpdb->postmeta, compact( 'post_id', 'meta_key', 'meta_value' ) );
     1248                do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, $meta_key, $meta_value );
    12341249        }
    12351250}
    12361251
    12371252/**
     1253 * Remove root URLs from the list of URLs to check for inclusion as enclosures.
     1254 *
     1255 * @package WordPress
     1256 * @since 3.1
     1257 *
     1258 * @param array $post_links Reference to array of URLs to check for inclusion as enclosures
     1259 */
     1260function dont_enclose_root_urls( &$post_links ) {
     1261        $r = array();
     1262        foreach ( (array) $post_links as $post_link ) {
     1263                // Only ping enclosures for URLs with a Query or Path - not Root URLs
     1264                $path = parse_url( $post_link, PHP_URL_PATH );
     1265                if ( $path && '/' != $path )
     1266                        $r[] = $post_link;
     1267                else if ( parse_url( $post_link, PHP_URL_QUERY ) )
     1268                        $r[] = $post_link;
     1269        }
     1270        $post_links = $r;
     1271}
     1272
     1273/**
    12381274 * Perform a HTTP HEAD or GET request.
    12391275 *
    12401276 * If $file_path is a writable filename, this will do a GET request and write