Index: src/wp-includes/comment.php
===================================================================
--- src/wp-includes/comment.php	(revision 41352)
+++ src/wp-includes/comment.php	(working copy)
@@ -2440,29 +2440,53 @@
  * Perform all pingbacks, enclosures, trackbacks, and send to pingback services.
  *
  * @since 2.1.0
- *
- * @global wpdb $wpdb WordPress database abstraction object.
  */
 function do_all_pings() {
-	global $wpdb;
+
+	$ping_types = get_post_types_by_support( 'trackbacks' );
 
 	// Do pingbacks
-	while ($ping = $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 = '_pingme' LIMIT 1")) {
-		delete_metadata_by_mid( 'post', $ping->meta_id );
-		pingback( $ping->post_content, $ping->ID );
+	$pings = get_posts(
+		array(
+			'post_type'        => $ping_types,
+			'suppress_filters' => false,
+			'nopaging'         => true,
+			'meta_value'       => '_pingme',
+			'fields'           => 'ids'
+		) 
+	);
+	foreach ( $pings as $ping ) {
+		pingback( null, $ping );
+		delete_post_meta( $ping, '_pingme' );
 	}
 
 	// 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 );
+	$enclosures = get_posts(
+		array(
+			'post_type'        => get_post_types( array( 'publicly_queryable' => true ) ),
+			'suppress_filters' => false,
+			'nopaging'         => true,
+			'meta_value'       => '_encloseme'
+		)
+	);
+	foreach ( $enclosures as $enclosure ) {
 		do_enclose( $enclosure->post_content, $enclosure->ID );
+		delete_post_meta( $enclosure->ID, '_encloseme' );
 	}
 
 	// Do Trackbacks
-	$trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
-	if ( is_array($trackbacks) )
-		foreach ( $trackbacks as $trackback )
-			do_trackbacks($trackback);
+	$trackbacks = get_posts( 
+		array(
+			'post_type' => $ping_types,
+			'suppress_filters' => false,
+			'nopaging' => true,
+			'to_ping' => true,
+			'fields' => 'ids'
+		)
+	);
+	foreach ( $trackbacks as $trackback ) {
+		do_trackbacks( $trackback );
+	}
 
 	//Do Update Services/Generic Pings
 	generic_ping();
@@ -3013,7 +3037,7 @@
 		 * @param int $comment_post_ID Post ID.
 		 */
 		do_action( 'comment_on_draft', $comment_post_ID );
-		
+
 		if ( current_user_can( 'read_post', $comment_post_ID ) ) {
 			return new WP_Error( 'comment_on_draft', __( 'Sorry, comments are not allowed for this item.' ), 403 );
 		} else {
Index: src/wp-includes/functions.php
===================================================================
--- src/wp-includes/functions.php	(revision 41352)
+++ src/wp-includes/functions.php	(working copy)
@@ -550,21 +550,30 @@
  * pingbacks and trackbacks.
  *
  * @since 1.5.0
+ * @since 4.9.0 $post_id can be 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 empty will retrieve from post.
+ * @param int|WP_Post    $post_id Post object or ID.
  */
-function do_enclose( $content, $post_ID ) {
+function do_enclose( $content, $post_id ) {
 	global $wpdb;
+	$post = get_post( $post_id );
+	if ( ! $post ) {
+		return false;
+	}
 
+	if ( empty( $content ) ) {
+		$content = $post->post_content;
+	}
+	 
 	//TODO: Tidy this ghetto code up and make the debug code optional
 	include_once( ABSPATH . WPINC . '/class-IXR.php' );
 
 	$post_links = array();
 
-	$pung = get_enclosed( $post_ID );
+	$pung = get_enclosed( $post->ID );
 
 	$post_links_temp = wp_extract_urls( $content );
 
@@ -597,12 +606,12 @@
 	 * @since 4.4.0
 	 *
 	 * @param array $post_links An array of enclosure links.
-	 * @param int   $post_ID    Post ID.
+	 * @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 ) . '%' ) ) ) {
 
 			if ( $headers = wp_get_http_headers( $url) ) {
 				$len = isset( $headers['content-length'] ) ? (int) $headers['content-length'] : 0;
@@ -625,7 +634,7 @@
 				}
 
 				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" );
 				}
 			}
 		}
