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.



git-svn-id: https://develop.svn.wordpress.org/trunk@38852 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2016-10-21 05:59:34 +00:00
parent 9320e0775c
commit 2d6badf6ab
3 changed files with 125 additions and 38 deletions

View File

@@ -2405,19 +2405,23 @@ function do_all_pings() {
* Perform trackbacks.
*
* @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 $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;
$post = get_post( $post_id );
$to_ping = get_to_ping($post_id);
$pinged = get_pung($post_id);
if ( empty($to_ping) ) {
$wpdb->update($wpdb->posts, array('to_ping' => ''), array('ID' => $post_id) );
if ( ! $post ) {
return false;
}
$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;
}
@@ -2440,10 +2444,11 @@ function do_trackbacks($post_id) {
foreach ( (array) $to_ping as $tb_ping ) {
$tb_ping = trim($tb_ping);
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;
} 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.
*
* @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 int $post_ID Post ID.
* @param string $content Post content to check for links. If empty will retrieve from post.
* @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-wp-http-ixr-client.php' );
// original code by Mort (http://mort.mine.nu:8080)
$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
// 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/>
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.
if ( $test = @parse_url($link_test) ) {
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 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 ) {
$pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
@@ -2530,7 +2545,7 @@ function pingback($content, $post_ID) {
if ( $pingback_server_url ) {
@ set_time_limit( 60 );
// 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
$client = new WP_HTTP_IXR_Client($pingback_server_url);
@@ -2552,7 +2567,7 @@ function pingback($content, $post_ID) {
$client->debug = false;
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 );
}
}
}