Make WordPress Core

Changeset 38852


Ignore:
Timestamp:
10/21/2016 05:59:34 AM (8 years ago)
Author:
pento
Message:

Pings: Allow ping functions to accept WP_Post objects as well as post IDs.

This removes the use of several global $wpdb instances, as well as bringing the ping functions into line with other post-related functions, which will accept a post ID or WP_Post object.

Props dshanke.
Fixes #38202.

Location:
trunk
Files:
1 added
2 edited

Legend:

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

    r38783 r38852  
    24062406 *
    24072407 * @since 1.5.0
     2408 * @since 4.7.0 $post_id can be a WP_Post object.
    24082409 *
    24092410 * @global wpdb $wpdb WordPress database abstraction object.
    24102411 *
    2411  * @param int $post_id Post ID to do trackbacks on.
    2412  */
    2413 function do_trackbacks($post_id) {
     2412 * @param int|WP_Post $post_id Post object or ID to do trackbacks on.
     2413 */
     2414function do_trackbacks( $post_id ) {
    24142415    global $wpdb;
    2415 
    24162416    $post = get_post( $post_id );
    2417     $to_ping = get_to_ping($post_id);
    2418     $pinged  = get_pung($post_id);
    2419     if ( empty($to_ping) ) {
    2420         $wpdb->update($wpdb->posts, array('to_ping' => ''), array('ID' => $post_id) );
     2417    if ( ! $post ) {
     2418        return false;
     2419    }
     2420
     2421    $to_ping = get_to_ping( $post );
     2422    $pinged  = get_pung( $post );
     2423    if ( empty( $to_ping ) ) {
     2424        $wpdb->update($wpdb->posts, array( 'to_ping' => '' ), array( 'ID' => $post->ID ) );
    24212425        return;
    24222426    }
     
    24412445            $tb_ping = trim($tb_ping);
    24422446            if ( !in_array($tb_ping, $pinged) ) {
    2443                 trackback($tb_ping, $post_title, $excerpt, $post_id);
     2447                trackback( $tb_ping, $post_title, $excerpt, $post->ID );
    24442448                $pinged[] = $tb_ping;
    24452449            } else {
    2446                 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $tb_ping, $post_id) );
     2450                $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s,
     2451                    '')) WHERE ID = %d", $tb_ping, $post->ID ) );
    24472452            }
    24482453        }
     
    24752480 *
    24762481 * @since 0.71
    2477  *
    2478  * @param string $content Post content to check for links.
    2479  * @param int $post_ID Post ID.
    2480  */
    2481 function pingback($content, $post_ID) {
     2482 * @since 4.7.0 $post_id can be a WP_Post object.
     2483 *
     2484 * @param string $content Post content to check for links. If empty will retrieve from post.
     2485 * @param int|WP_Post $post_id Post Object or ID.
     2486 */
     2487function pingback( $content, $post_id ) {
    24822488    include_once( ABSPATH . WPINC . '/class-IXR.php' );
    24832489    include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
     
    24862492    $post_links = array();
    24872493
    2488     $pung = get_pung($post_ID);
     2494    $post = get_post( $post_id );
     2495    if ( ! $post ) {
     2496        return;
     2497    }
     2498
     2499    $pung = get_pung( $post );
     2500
     2501    if ( empty( $content ) ) {
     2502        $content = $post->post_content;
     2503    }
    24892504
    24902505    // Step 1
     
    25022517
    25032518    foreach ( (array) $post_links_temp as $link_test ) :
    2504         if ( !in_array($link_test, $pung) && (url_to_postid($link_test) != $post_ID) // If we haven't pung it already and it isn't a link to itself
     2519        if ( ! in_array( $link_test, $pung ) && ( url_to_postid( $link_test ) != $post->ID ) // If we haven't pung it already and it isn't a link to itself
    25052520                && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
    25062521            if ( $test = @parse_url($link_test) ) {
     
    25232538     * @param int   $post_ID     The post ID.
    25242539     */
    2525     do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post_ID ) );
     2540    do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post->ID ) );
    25262541
    25272542    foreach ( (array) $post_links as $pagelinkedto ) {
     
    25312546            @ set_time_limit( 60 );
    25322547            // Now, the RPC call
    2533             $pagelinkedfrom = get_permalink($post_ID);
     2548            $pagelinkedfrom = get_permalink( $post );
    25342549
    25352550            // using a timeout of 3 seconds should be enough to cover slow servers
     
    25532568
    25542569            if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
    2555                 add_ping( $post_ID, $pagelinkedto );
     2570                add_ping( $post, $pagelinkedto );
    25562571        }
    25572572    }
  • trunk/src/wp-includes/post.php

    r38849 r38852  
    39923992 *
    39933993 * @since 1.5.0
     3994 * @since 4.7.0 $post_id can be a WP_Post object.
     3995 * @since 4.7.0 $uri can be an array of URIs.
    39943996 *
    39953997 * @global wpdb $wpdb WordPress database abstraction object.
    39963998 *
    3997  * @param int    $post_id Post ID.
    3998  * @param string $uri     Ping URI.
     3999 * @param int|WP_Post  $post_id Post object or ID.
     4000 * @param string|array $uri     Ping URI or array of URIs.
    39994001 * @return int|false How many rows were updated.
    40004002 */
    40014003function add_ping( $post_id, $uri ) {
    40024004    global $wpdb;
    4003     $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
    4004     $pung = trim($pung);
    4005     $pung = preg_split('/\s/', $pung);
    4006     $pung[] = $uri;
     4005
     4006    $post = get_post( $post_id );
     4007    if ( ! $post ) {
     4008        return false;
     4009    }
     4010
     4011    $pung = trim( $post->pinged );
     4012    $pung = preg_split( '/\s/', $pung );
     4013
     4014    if ( is_array( $uri ) ) {
     4015        $pung = array_merge( $pung, $uri );
     4016    }
     4017    else {
     4018        $pung[] = $uri;
     4019    }
    40074020    $new = implode("\n", $pung);
    40084021
     
    40164029    $new = apply_filters( 'add_ping', $new );
    40174030
    4018     // expected_slashed ($new).
    4019     $new = wp_unslash($new);
    4020     return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) );
     4031    $return = $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post->ID ) );
     4032    clean_post_cache( $post->ID );
     4033    return $return;
    40214034}
    40224035
     
    40604073 * @since 1.5.0
    40614074 *
    4062  * @global wpdb $wpdb WordPress database abstraction object.
    4063  *
    4064  * @param int $post_id Post ID.
     4075 * @since 4.7.0 $post_id can be a WP_Post object.
     4076 *
     4077 * @param int|WP_Post $post_id Post ID or object.
    40654078 * @return array
    40664079 */
    40674080function get_pung( $post_id ) {
    4068     global $wpdb;
    4069     $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
    4070     $pung = trim($pung);
    4071     $pung = preg_split('/\s/', $pung);
     4081    $post = get_post( $post_id );
     4082    if ( ! $post ) {
     4083        return false;
     4084    }
     4085
     4086    $pung = trim( $post->pinged );
     4087    $pung = preg_split( '/\s/', $pung );
    40724088
    40734089    /**
     
    40854101 *
    40864102 * @since 1.5.0
    4087  *
    4088  * @global wpdb $wpdb WordPress database abstraction object.
    4089  *
    4090  * @param int $post_id Post ID
     4103 * @since 4.7.0 $post_id can be a WP_Post object.
     4104 *
     4105 * @param int|WP_Post $post_id Post Object or ID
    40914106 * @return array
    40924107 */
    40934108function get_to_ping( $post_id ) {
    4094     global $wpdb;
    4095     $to_ping = $wpdb->get_var( $wpdb->prepare( "SELECT to_ping FROM $wpdb->posts WHERE ID = %d", $post_id ));
    4096     $to_ping = sanitize_trackback_urls( $to_ping );
     4109    $post = get_post( $post_id );
     4110
     4111    if ( ! $post ) {
     4112        return false;
     4113    }
     4114
     4115    $to_ping = sanitize_trackback_urls( $post->to_ping );
    40974116    $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
    40984117
Note: See TracChangeset for help on using the changeset viewer.