WordPress.org

Make WordPress Core

Changeset 46175


Ignore:
Timestamp:
09/19/2019 01:48:54 AM (2 months ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r45932 r46175  
    26402640    }
    26412641
    2642     // Do Enclosures
    2643     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" ) ) {
    2644         delete_metadata_by_mid( 'post', $enclosure->meta_id );
    2645         do_enclose( $enclosure->post_content, $enclosure->ID );
     2642    // Do enclosures.
     2643    $enclosures = get_posts(
     2644        array(
     2645            'post_type'        => get_post_types(),
     2646            'suppress_filters' => false,
     2647            'nopaging'         => true,
     2648            'meta_key'         => '_encloseme',
     2649            'fields'           => 'ids',
     2650        )
     2651    );
     2652
     2653    foreach ( $enclosure as $enclosure ) {
     2654        delete_post_meta( $enclosure, '_encloseme' );
     2655        do_enclose( null, $enclosure->ID );
    26462656    }
    26472657
  • trunk/src/wp-includes/functions.php

    r46172 r46175  
    800800 *
    801801 * @since 1.5.0
     802 * @since 5.3.0 The `$content` parameter was made optional, and the `$post` parameter was
     803 *              updated to accept a post ID or a WP_Post object.
    802804 *
    803805 * @global wpdb $wpdb WordPress database abstraction object.
    804806 *
    805  * @param string $content Post Content.
    806  * @param int    $post_ID Post ID.
    807  */
    808 function do_enclose( $content, $post_ID ) {
     807 * @param string         $content Post content. If `null`, the `post_content` field from `$post` is used.
     808 * @param int|WP_Post    $post    Post ID or post object.
     809 * @return null|bool Returns false if post is not found.
     810 */
     811function do_enclose( $content = null, $post ) {
    809812    global $wpdb;
    810813
     
    812815    include_once( ABSPATH . WPINC . '/class-IXR.php' );
    813816
     817    $post = get_post( $post );
     818    if ( ! $post ) {
     819        return false;
     820    }
     821
     822    if ( null === $content ) {
     823        $content = $post->post_content;
     824    }
     825
    814826    $post_links = array();
    815827
    816     $pung = get_enclosed( $post_ID );
     828    $pung = get_enclosed( $post->ID );
    817829
    818830    $post_links_temp = wp_extract_urls( $content );
     
    820832    foreach ( $pung as $link_test ) {
    821833        if ( ! in_array( $link_test, $post_links_temp ) ) { // link no longer in post
    822             $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 ) . '%' ) );
     834            $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 ) . '%' ) );
    823835            foreach ( $mids as $mid ) {
    824836                delete_metadata_by_mid( 'post', $mid );
     
    852864     * @param int   $post_ID    Post ID.
    853865     */
    854     $post_links = apply_filters( 'enclosure_links', $post_links, $post_ID );
     866    $post_links = apply_filters( 'enclosure_links', $post_links, $post->ID );
    855867
    856868    foreach ( (array) $post_links as $url ) {
    857         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 ) . '%' ) ) ) {
     869        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 ) . '%' ) ) ) {
    858870
    859871            $headers = wp_get_http_headers( $url );
     
    879891
    880892                if ( in_array( substr( $type, 0, strpos( $type, '/' ) ), $allowed_types ) ) {
    881                     add_post_meta( $post_ID, 'enclosure', "$url\n$len\n$mime\n" );
     893                    add_post_meta( $post->ID, 'enclosure', "$url\n$len\n$mime\n" );
    882894                }
    883895            }
Note: See TracChangeset for help on using the changeset viewer.