Index: src/wp-includes/comment.php
===================================================================
--- src/wp-includes/comment.php	(revision 43377)
+++ src/wp-includes/comment.php	(working copy)
@@ -2579,24 +2579,52 @@
 function do_all_pings() {
 	global $wpdb;
 
+	// Determine which classes support trackbacks and pingbacks
+	$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 ) {
+		delete_post_meta( $ping, '_pingme' );
+		pingback( null, $ping );
 	}
 
 	// 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 );
-		do_enclose( $enclosure->post_content, $enclosure->ID );
+        $enclosures = get_posts(
+		array(
+			'post_type' => get_post_types( array( 'publicly_queryable' => true ) ),
+			'suppress_filters' => false,
+			'nopaging' => true,
+			'meta_value' => '_encloseme',
+			'fields' => 'ids'
+		)
+	);
+	foreach ( $enclosures as $enclosure ) {
+		delete_post_meta( $enclosure, '_encloseme' );
+		do_enclose( null, $enclosure );
 	}
 
 	// 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
Index: src/wp-includes/functions.php
===================================================================
--- src/wp-includes/functions.php	(revision 43377)
+++ src/wp-includes/functions.php	(working copy)
@@ -562,27 +562,35 @@
  * pingbacks and trackbacks.
  *
  * @since 1.5.0
+ * @since 4.9.7 $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 ( ! $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 );
 
 	foreach ( $pung as $link_test ) {
 		if ( ! in_array( $link_test, $post_links_temp ) ) { // link no longer in post
-			$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 ) . '%' ) );
+			$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 ) . '%' ) );
 			foreach ( $mids as $mid ) {
 				delete_metadata_by_mid( 'post', $mid );
 			}
@@ -591,7 +599,7 @@
 
 	foreach ( (array) $post_links_temp as $link_test ) {
 		if ( ! in_array( $link_test, $pung ) ) { // If we haven't pung it already
-			$test = @parse_url( $link_test );
+			$test = @wp_parse_url( $link_test );
 			if ( false === $test ) {
 				continue;
 			}
@@ -612,12 +620,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;
@@ -640,7 +648,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" );
 				}
 			}
 		}
