From 7c56b972cc1b582995c245cd5257e8c75b8c85c9 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 19 Sep 2019 01:49:56 +0000 Subject: [PATCH] Improve `do_enclose()` logic on post publish. Removing the direct SQL query in `do_all_pings()` improves filterability. As part of this change, the signature of `do_enclose()` is changed so that a null `$content` parameter can be passed, with the `$content` then inferred from the `$post` passed in the second parameter. In addition, the second parameter was modified so that a post ID or a `WP_Post` object can be provided. These changes make it possible to trigger enclosure checks with a post ID alone (as in `do_all_pings()`) and also brings the function signature in line with `do_trackbacks()` and `pingback()`. Props dshanske, spacedmonkey, janw.oostendorp, mrmadhat, birgire. See #36824. Built from https://develop.svn.wordpress.org/trunk@46175 git-svn-id: http://core.svn.wordpress.org/trunk@45987 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/comment.php | 18 ++++++++++++++---- wp-includes/functions.php | 28 ++++++++++++++++++++-------- wp-includes/version.php | 2 +- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/wp-includes/comment.php b/wp-includes/comment.php index 4b192ea02e..304536b9d2 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -2639,10 +2639,20 @@ function do_all_pings() { pingback( $ping->post_content, $ping->ID ); } - // Do Enclosures - while ( $enclosure = $wpdb->get_row( "SELECT ID, post_content, meta_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1" ) ) { - delete_metadata_by_mid( 'post', $enclosure->meta_id ); - do_enclose( $enclosure->post_content, $enclosure->ID ); + // Do enclosures. + $enclosures = get_posts( + array( + 'post_type' => get_post_types(), + 'suppress_filters' => false, + 'nopaging' => true, + 'meta_key' => '_encloseme', + 'fields' => 'ids', + ) + ); + + foreach ( $enclosure as $enclosure ) { + delete_post_meta( $enclosure, '_encloseme' ); + do_enclose( null, $enclosure->ID ); } // Do Trackbacks diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 035347e1ed..97d8941478 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -799,27 +799,39 @@ function wp_extract_urls( $content ) { * pingbacks and trackbacks. * * @since 1.5.0 + * @since 5.3.0 The `$content` parameter was made optional, and the `$post` parameter was + * updated to accept a post ID or a WP_Post object. * * @global wpdb $wpdb WordPress database abstraction object. * - * @param string $content Post Content. - * @param int $post_ID Post ID. + * @param string $content Post content. If `null`, the `post_content` field from `$post` is used. + * @param int|WP_Post $post Post ID or post object. + * @return null|bool Returns false if post is not found. */ -function do_enclose( $content, $post_ID ) { +function do_enclose( $content = null, $post ) { global $wpdb; //TODO: Tidy this ghetto code up and make the debug code optional include_once( ABSPATH . WPINC . '/class-IXR.php' ); + $post = get_post( $post ); + if ( ! $post ) { + return false; + } + + if ( null === $content ) { + $content = $post->post_content; + } + $post_links = array(); - $pung = get_enclosed( $post_ID ); + $pung = get_enclosed( $post->ID ); $post_links_temp = wp_extract_urls( $content ); foreach ( $pung as $link_test ) { if ( ! in_array( $link_test, $post_links_temp ) ) { // link no longer in post - $mids = $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, $wpdb->esc_like( $link_test ) . '%' ) ); + $mids = $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, $wpdb->esc_like( $link_test ) . '%' ) ); foreach ( $mids as $mid ) { delete_metadata_by_mid( 'post', $mid ); } @@ -851,10 +863,10 @@ function do_enclose( $content, $post_ID ) { * @param array $post_links An array of enclosure links. * @param int $post_ID Post ID. */ - $post_links = apply_filters( 'enclosure_links', $post_links, $post_ID ); + $post_links = apply_filters( 'enclosure_links', $post_links, $post->ID ); foreach ( (array) $post_links as $url ) { - 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, $wpdb->esc_like( $url ) . '%' ) ) ) { + 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, $wpdb->esc_like( $url ) . '%' ) ) ) { $headers = wp_get_http_headers( $url ); if ( $headers ) { @@ -878,7 +890,7 @@ function do_enclose( $content, $post_ID ) { } if ( in_array( substr( $type, 0, strpos( $type, '/' ) ), $allowed_types ) ) { - add_post_meta( $post_ID, 'enclosure', "$url\n$len\n$mime\n" ); + add_post_meta( $post->ID, 'enclosure', "$url\n$len\n$mime\n" ); } } } diff --git a/wp-includes/version.php b/wp-includes/version.php index ef62ff64d9..a4700b0375 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3-alpha-46174'; +$wp_version = '5.3-alpha-46175'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.