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.


Built from https://develop.svn.wordpress.org/trunk@38852


git-svn-id: http://core.svn.wordpress.org/trunk@38795 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast 2016-10-21 06:00:31 +00:00
parent 2e04d2a192
commit 16f0c6877e
3 changed files with 73 additions and 39 deletions

View File

@ -2405,19 +2405,23 @@ function do_all_pings() {
* Perform trackbacks. * Perform trackbacks.
* *
* @since 1.5.0 * @since 1.5.0
* @since 4.7.0 $post_id can be a WP_Post object.
* *
* @global wpdb $wpdb WordPress database abstraction object. * @global wpdb $wpdb WordPress database abstraction object.
* *
* @param int $post_id Post ID to do trackbacks on. * @param int|WP_Post $post_id Post object or ID to do trackbacks on.
*/ */
function do_trackbacks($post_id) { function do_trackbacks( $post_id ) {
global $wpdb; global $wpdb;
$post = get_post( $post_id ); $post = get_post( $post_id );
$to_ping = get_to_ping($post_id); if ( ! $post ) {
$pinged = get_pung($post_id); return false;
if ( empty($to_ping) ) { }
$wpdb->update($wpdb->posts, array('to_ping' => ''), array('ID' => $post_id) );
$to_ping = get_to_ping( $post );
$pinged = get_pung( $post );
if ( empty( $to_ping ) ) {
$wpdb->update($wpdb->posts, array( 'to_ping' => '' ), array( 'ID' => $post->ID ) );
return; return;
} }
@ -2440,10 +2444,11 @@ function do_trackbacks($post_id) {
foreach ( (array) $to_ping as $tb_ping ) { foreach ( (array) $to_ping as $tb_ping ) {
$tb_ping = trim($tb_ping); $tb_ping = trim($tb_ping);
if ( !in_array($tb_ping, $pinged) ) { if ( !in_array($tb_ping, $pinged) ) {
trackback($tb_ping, $post_title, $excerpt, $post_id); trackback( $tb_ping, $post_title, $excerpt, $post->ID );
$pinged[] = $tb_ping; $pinged[] = $tb_ping;
} else { } else {
$wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $tb_ping, $post_id) ); $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s,
'')) WHERE ID = %d", $tb_ping, $post->ID ) );
} }
} }
} }
@ -2474,18 +2479,28 @@ function generic_ping( $post_id = 0 ) {
* Pings back the links found in a post. * Pings back the links found in a post.
* *
* @since 0.71 * @since 0.71
* @since 4.7.0 $post_id can be a WP_Post object.
* *
* @param string $content Post content to check for links. * @param string $content Post content to check for links. If empty will retrieve from post.
* @param int $post_ID Post ID. * @param int|WP_Post $post_id Post Object or ID.
*/ */
function pingback($content, $post_ID) { function pingback( $content, $post_id ) {
include_once( ABSPATH . WPINC . '/class-IXR.php' ); include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' ); include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
// original code by Mort (http://mort.mine.nu:8080) // original code by Mort (http://mort.mine.nu:8080)
$post_links = array(); $post_links = array();
$pung = get_pung($post_ID); $post = get_post( $post_id );
if ( ! $post ) {
return;
}
$pung = get_pung( $post );
if ( empty( $content ) ) {
$content = $post->post_content;
}
// Step 1 // Step 1
// Parsing the post, external links (if any) are stored in the $post_links array // Parsing the post, external links (if any) are stored in the $post_links array
@ -2501,7 +2516,7 @@ function pingback($content, $post_ID) {
// We don't wanna ping first and second types, even if they have a valid <link/> // We don't wanna ping first and second types, even if they have a valid <link/>
foreach ( (array) $post_links_temp as $link_test ) : foreach ( (array) $post_links_temp as $link_test ) :
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 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
&& !is_local_attachment($link_test) ) : // Also, let's never ping local attachments. && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
if ( $test = @parse_url($link_test) ) { if ( $test = @parse_url($link_test) ) {
if ( isset($test['query']) ) if ( isset($test['query']) )
@ -2522,7 +2537,7 @@ function pingback($content, $post_ID) {
* @param array &$pung Whether a link has already been pinged, passed by reference. * @param array &$pung Whether a link has already been pinged, passed by reference.
* @param int $post_ID The post ID. * @param int $post_ID The post ID.
*/ */
do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post_ID ) ); do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post->ID ) );
foreach ( (array) $post_links as $pagelinkedto ) { foreach ( (array) $post_links as $pagelinkedto ) {
$pingback_server_url = discover_pingback_server_uri( $pagelinkedto ); $pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
@ -2530,7 +2545,7 @@ function pingback($content, $post_ID) {
if ( $pingback_server_url ) { if ( $pingback_server_url ) {
@ set_time_limit( 60 ); @ set_time_limit( 60 );
// Now, the RPC call // Now, the RPC call
$pagelinkedfrom = get_permalink($post_ID); $pagelinkedfrom = get_permalink( $post );
// using a timeout of 3 seconds should be enough to cover slow servers // using a timeout of 3 seconds should be enough to cover slow servers
$client = new WP_HTTP_IXR_Client($pingback_server_url); $client = new WP_HTTP_IXR_Client($pingback_server_url);
@ -2552,7 +2567,7 @@ function pingback($content, $post_ID) {
$client->debug = false; $client->debug = false;
if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
add_ping( $post_ID, $pagelinkedto ); add_ping( $post, $pagelinkedto );
} }
} }
} }

View File

@ -3991,19 +3991,32 @@ function wp_transition_post_status( $new_status, $old_status, $post ) {
* Add a URL to those already pinged. * Add a URL to those already pinged.
* *
* @since 1.5.0 * @since 1.5.0
* @since 4.7.0 $post_id can be a WP_Post object.
* @since 4.7.0 $uri can be an array of URIs.
* *
* @global wpdb $wpdb WordPress database abstraction object. * @global wpdb $wpdb WordPress database abstraction object.
* *
* @param int $post_id Post ID. * @param int|WP_Post $post_id Post object or ID.
* @param string $uri Ping URI. * @param string|array $uri Ping URI or array of URIs.
* @return int|false How many rows were updated. * @return int|false How many rows were updated.
*/ */
function add_ping( $post_id, $uri ) { function add_ping( $post_id, $uri ) {
global $wpdb; global $wpdb;
$pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id ));
$pung = trim($pung); $post = get_post( $post_id );
$pung = preg_split('/\s/', $pung); if ( ! $post ) {
$pung[] = $uri; return false;
}
$pung = trim( $post->pinged );
$pung = preg_split( '/\s/', $pung );
if ( is_array( $uri ) ) {
$pung = array_merge( $pung, $uri );
}
else {
$pung[] = $uri;
}
$new = implode("\n", $pung); $new = implode("\n", $pung);
/** /**
@ -4015,9 +4028,9 @@ function add_ping( $post_id, $uri ) {
*/ */
$new = apply_filters( 'add_ping', $new ); $new = apply_filters( 'add_ping', $new );
// expected_slashed ($new). $return = $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post->ID ) );
$new = wp_unslash($new); clean_post_cache( $post->ID );
return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) ); return $return;
} }
/** /**
@ -4059,16 +4072,19 @@ function get_enclosed( $post_id ) {
* *
* @since 1.5.0 * @since 1.5.0
* *
* @global wpdb $wpdb WordPress database abstraction object. * @since 4.7.0 $post_id can be a WP_Post object.
* *
* @param int $post_id Post ID. * @param int|WP_Post $post_id Post ID or object.
* @return array * @return array
*/ */
function get_pung( $post_id ) { function get_pung( $post_id ) {
global $wpdb; $post = get_post( $post_id );
$pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id )); if ( ! $post ) {
$pung = trim($pung); return false;
$pung = preg_split('/\s/', $pung); }
$pung = trim( $post->pinged );
$pung = preg_split( '/\s/', $pung );
/** /**
* Filters the list of already-pinged URLs for the given post. * Filters the list of already-pinged URLs for the given post.
@ -4084,16 +4100,19 @@ function get_pung( $post_id ) {
* Retrieve URLs that need to be pinged. * Retrieve URLs that need to be pinged.
* *
* @since 1.5.0 * @since 1.5.0
* @since 4.7.0 $post_id can be a WP_Post object.
* *
* @global wpdb $wpdb WordPress database abstraction object. * @param int|WP_Post $post_id Post Object or ID
*
* @param int $post_id Post ID
* @return array * @return array
*/ */
function get_to_ping( $post_id ) { function get_to_ping( $post_id ) {
global $wpdb; $post = get_post( $post_id );
$to_ping = $wpdb->get_var( $wpdb->prepare( "SELECT to_ping FROM $wpdb->posts WHERE ID = %d", $post_id ));
$to_ping = sanitize_trackback_urls( $to_ping ); if ( ! $post ) {
return false;
}
$to_ping = sanitize_trackback_urls( $post->to_ping );
$to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY); $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY);
/** /**

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.7-alpha-38851'; $wp_version = '4.7-alpha-38852';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.