Index: src/wp-admin/admin-ajax.php
===================================================================
--- src/wp-admin/admin-ajax.php	(revision 37740)
+++ src/wp-admin/admin-ajax.php	(working copy)
@@ -64,7 +64,7 @@
 	'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'press-this-save-post',
 	'press-this-add-category', 'crop-image', 'generate-password', 'save-wporg-username', 'delete-plugin',
 	'search-plugins', 'search-install-plugins', 'activate-plugin', 'update-theme', 'delete-theme',
-	'install-theme',
+	'install-theme', 'test_url',
 );
 
 // Deprecated
Index: src/wp-admin/includes/ajax-actions.php
===================================================================
--- src/wp-admin/includes/ajax-actions.php	(revision 37740)
+++ src/wp-admin/includes/ajax-actions.php	(working copy)
@@ -3832,3 +3832,45 @@
 
 	wp_send_json_success( $status );
 }
+
+/**
+ * Ajax handler for testing if an URL exists. Used in the editor.
+ *
+ * @since 4.6.0
+ */
+function wp_ajax_test_url() {
+	if ( ! current_user_can( 'edit_posts' ) || ! wp_verify_nonce( $_POST['nonce'], 'wp-test-url' ) ) {
+		wp_send_json_error();
+	}
+
+	$href = esc_url_raw( $_POST['href'] );
+
+	// Relative URL
+	if ( strpos( $href, '//' ) !== 0 && in_array( $href[0], array( '/', '#', '?' ), true ) ) {
+		$href = get_bloginfo( 'url' ) . $href;
+	}
+
+	$response = wp_safe_remote_get( $href, array(
+		'timeout' => 15,
+		// Use an explicit user-agent
+		'user-agent' => 'WordPress URL Test',
+	) );
+
+	$message = null;
+
+	if ( is_wp_error( $response ) ) {
+		$error = $response->get_error_message();
+
+		if ( strpos( $message, 'resolve host' ) !== false ) {
+			$message = array( 'error' => __( 'Invalid host name.' ) );
+		}
+
+		wp_send_json_error( $message );
+	}
+
+	if ( wp_remote_retrieve_response_code( $response ) === 404 ) {
+		wp_send_json_error( array( 'error' => __( 'Not found, HTTP error 404.' ) ) );
+	}
+
+	wp_send_json_success();
+}
Index: src/wp-includes/class-wp-editor.php
===================================================================
--- src/wp-includes/class-wp-editor.php	(revision 37740)
+++ src/wp-includes/class-wp-editor.php	(working copy)
@@ -1063,6 +1063,7 @@
 			'Ctrl + letter:' => __( 'Ctrl + letter:' ),
 			'Letter' => __( 'Letter' ),
 			'Action' => __( 'Action' ),
+			'Invalid host name.' => __( 'Invalid host name.' ),
 			'To move focus to other buttons use Tab or the arrow keys. To return focus to the editor press Escape or use one of the buttons.' =>
 				__( 'To move focus to other buttons use Tab or the arrow keys. To return focus to the editor press Escape or use one of the buttons.' ),
 			'When starting a new paragraph with one of these formatting shortcuts followed by a space, the formatting will be applied automatically. Press Backspace or Escape to undo.' =>
@@ -1283,8 +1284,15 @@
 		</script>
 		<?php
 
-		if ( in_array( 'wplink', self::$plugins, true ) || in_array( 'link', self::$qt_buttons, true ) )
+		$has_wplink = in_array( 'wplink', self::$plugins, true );
+
+		if ( $has_wplink ) {
+			echo '<input type="hidden" id="_wplink_urltest_nonce" value="' . wp_create_nonce( 'wp-test-url' ) . '" />';
+		}
+
+		if ( $has_wplink || in_array( 'link', self::$qt_buttons, true ) ) {
 			self::wp_link_dialog();
+		}
 
 		/**
 		 * Fires after any core TinyMCE editor instances are created.
Index: src/wp-includes/css/editor.css
===================================================================
--- src/wp-includes/css/editor.css	(revision 37740)
+++ src/wp-includes/css/editor.css	(working copy)
@@ -1757,6 +1757,10 @@
 	cursor: pointer;
 }
 
+div.wp-link-preview a.wplink-url-error {
+	color: #dc3232;
+}
+
 div.wp-link-input {
 	float: left;
 	margin: 2px;
Index: src/wp-includes/js/tinymce/plugins/wplink/plugin.js
===================================================================
--- src/wp-includes/js/tinymce/plugins/wplink/plugin.js	(revision 37740)
+++ src/wp-includes/js/tinymce/plugins/wplink/plugin.js	(working copy)
@@ -93,6 +93,7 @@
 		var doingUndoRedo;
 		var doingUndoRedoTimer;
 		var $ = window.jQuery;
+		var urlErrors = {};
 
 		function getSelectedLink() {
 			var href, html,
@@ -131,11 +132,62 @@
 		}
 
 		function removePlaceholderStrings( content, dataAttr ) {
-			if ( dataAttr ) {
-				content = content.replace( / data-wplink-edit="true"/g, '' );
+			return content.replace( /(<a [^>]+>)([\s\S]*?)<\/a>/g, function( all, tag, text ) {
+				if ( tag.indexOf( ' href="_wp_link_placeholder"' ) > -1 ) {
+					return text;
+				}
+
+				if ( dataAttr ) {
+					tag = tag.replace( / data-wplink-edit="true"/g, '' );
+				}
+
+				tag = tag.replace( / data-wplink-url-error="true"/g, '' );
+
+				return tag + text + '</a>';
+			});
+		}
+
+		function checkLink( node ) {
+			var $link = editor.$( node );
+			var href = $link.attr( 'href' );
+
+			if ( ! href || typeof $ === 'undefined' ) {
+				return;
 			}
 
-			return content.replace( /<a [^>]*?href="_wp_link_placeholder"[^>]*>([\s\S]+)<\/a>/g, '$1' );
+			// Early check
+			if ( /^http/i.test( href ) && ! /\.[a-z]{2,63}(\/|$)/i.test( href ) ) {
+				urlErrors[href] = tinymce.translate( 'Invalid host name.' );
+			}
+
+			if ( urlErrors.hasOwnProperty( href ) ) {
+				$link.attr( 'data-wplink-url-error', 'true' );
+				return;
+			} else {
+				$link.removeAttr( 'data-wplink-url-error' );
+			}
+
+			$.post(
+				window.ajaxurl, {
+					action: 'test_url',
+					nonce: $( '#_wplink_urltest_nonce' ).val(),
+					href: href
+				},
+				'json'
+			).done( function( response ) {
+				if ( response.success ) {
+					return;
+				}
+
+				if ( response.data && response.data.error ) {
+					urlErrors[href] = response.data.error;
+					$link.attr( 'data-wplink-url-error', 'true' );
+
+					if ( toolbar && toolbar.visible() ) {
+						toolbar.$el.find( '.wp-link-preview a' ).addClass( 'wplink-url-error' ).attr( 'title', editor.dom.encode( response.data.error ) );
+					}
+				}
+			});
 		}
 
 		editor.on( 'preinit', function() {
@@ -231,6 +283,8 @@
 				if ( ! tinymce.trim( linkNode.innerHTML ) ) {
 					editor.$( linkNode ).text( text || href );
 				}
+
+				checkLink( linkNode );
 			}
 
 			inputInstance.reset();
@@ -473,7 +527,7 @@
 
 		editor.on( 'wptoolbar', function( event ) {
 			var linkNode = editor.dom.getParent( event.element, 'a' ),
-				$linkNode, href, edit;
+				$linkNode, href, edit, title;
 
 			if ( typeof window.wpLink !== 'undefined' && window.wpLink.modalOpen ) {
 				editToolbar.tempHide = true;
@@ -498,6 +552,13 @@
 					previewInstance.setURL( href );
 					event.element = linkNode;
 					event.toolbar = toolbar;
+					title = urlErrors.hasOwnProperty( href ) ? editor.dom.encode( urlErrors[ href ] ) : null;
+
+					if ( $linkNode.attr( 'data-wplink-url-error' ) === 'true' ) {
+						toolbar.$el.find( '.wp-link-preview a' ).addClass( 'wplink-url-error' ).attr( 'title', title );
+					} else {
+						toolbar.$el.find( '.wp-link-preview a' ).removeClass( 'wplink-url-error' ).attr( 'title', null );
+					}
 				}
 			}
 		} );
@@ -555,7 +616,8 @@
 			close: function() {
 				editToolbar.tempHide = false;
 				editor.execCommand( 'wp_link_cancel' );
-			}
+			},
+			checkLink: checkLink
 		};
 	} );
 } )( window.tinymce );
Index: src/wp-includes/js/tinymce/skins/wordpress/wp-content.css
===================================================================
--- src/wp-includes/js/tinymce/skins/wordpress/wp-content.css	(revision 37740)
+++ src/wp-includes/js/tinymce/skins/wordpress/wp-content.css	(working copy)
@@ -205,6 +205,24 @@
 	visibility: hidden;
 }
 
+a[data-wplink-url-error],
+a[data-wplink-url-error]:hover,
+a[data-wplink-url-error]:focus {
+	outline: 2px dotted #dc3232;
+	position: relative;
+}
+
+a[data-wplink-url-error]:before {
+	content: '';
+	display: block;
+	position: absolute;
+	top: -2px;
+	left: -2px;
+	right: -2px;
+	bottom: -2px;
+	outline: 2px dotted #fff;
+}
+
 /**
  * WP Views
  */
Index: src/wp-includes/js/wplink.js
===================================================================
--- src/wp-includes/js/wplink.js	(revision 37740)
+++ src/wp-includes/js/wplink.js	(working copy)
@@ -434,6 +434,10 @@
 			editor.focus();
 			editor.nodeChanged();
 
+			if ( link && editor.plugins.wplink ) {
+				editor.plugins.wplink.checkLink( link );
+			}
+
 			// Audible confirmation message when a link has been inserted in the Editor.
 			wp.a11y.speak( wpLinkL10n.linkInserted );
 		},
Index: tests/phpunit/tests/term/query.php
===================================================================
--- tests/phpunit/tests/term/query.php	(working copy)
+++ tests/phpunit/tests/term/query.php	(working copy)
@@ -3,421 +3,62 @@
 /**
  * @group taxonomy
  */
-class Tests_Tax_Query extends WP_UnitTestCase {
-	protected $q;
-
-	public function setUp() {
-		parent::setUp();
-		unset( $this->q );
-		$this->q = new WP_Query();
-	}
-
-	public function test_construct_with_relation_default() {
-		$tq = new WP_Tax_Query( array() );
-		$this->assertSame( 'AND', $tq->relation );
-	}
-
-	public function test_construct_with_relation_or_lowercase() {
-		$tq = new WP_Tax_Query( array(
-			'relation' => 'or',
-		) );
-		$this->assertSame( 'OR', $tq->relation );
-	}
-
-	public function test_construct_with_relation_or_uppercase() {
-		$tq = new WP_Tax_Query( array(
-			'relation' => 'OR',
-		) );
-		$this->assertSame( 'OR', $tq->relation );
-	}
-
-	public function test_construct_with_relation_other() {
-		$tq = new WP_Tax_Query( array(
-			'relation' => 'foo',
-		) );
-		$this->assertSame( 'AND', $tq->relation );
-	}
-
-	public function test_construct_fill_missing_query_params() {
-		$tq = new WP_Tax_Query( array(
-			array(),
-		) );
-
-		$expected = array(
-			'taxonomy' => '',
-			'terms' => array(),
-			'include_children' => true,
-			'field' => 'term_id',
-			'operator' => 'IN',
-		);
-
-		$this->assertEquals( $expected, $tq->queries[0] );
-	}
-
-	public function test_construct_fill_missing_query_params_merge_with_passed_values() {
-		$tq = new WP_Tax_Query( array(
-			array(
-				'taxonomy' => 'foo',
-				'include_children' => false,
-				'foo' => 'bar',
-			),
-		) );
-
-		$expected = array(
-			'taxonomy' => 'foo',
-			'terms' => array(),
-			'include_children' => false,
-			'field' => 'term_id',
-			'operator' => 'IN',
-			'foo' => 'bar',
-		);
-
-		$this->assertEquals( $expected, $tq->queries[0] );
-	}
-
-	public function test_construct_cast_terms_to_array() {
-		$tq = new WP_Tax_Query( array(
-			array(
-				'terms' => 'foo',
-			),
-		) );
-
-		$this->assertEquals( array( 'foo', ), $tq->queries[0]['terms'] );
-	}
-
+class Tests_Term_Query extends WP_UnitTestCase {
 	/**
-	 * @ticket 30117
+	 * @ticket 37074
 	 */
-	public function test_construct_empty_strings_array_members_should_be_discarded() {
-		$q = new WP_Tax_Query( array(
-			'',
-			array(
-				'taxonomy' => 'post_tag',
-				'terms' => 'foo',
-			),
-		) );
-
-		$this->assertSame( 1, count( $q->queries ) );
-	}
-
-	public function test_transform_query_terms_empty() {
-		$tq = new WP_Tax_Query( array(
-			array(),
-		) );
-		$query = $tq->queries[0];
-
-		$tq->transform_query( $tq->queries[0], 'term_id' );
-
-		$this->assertSame( $query, $tq->queries[0] );
-	}
-
-	public function test_transform_query_field_same_as_resulting_field() {
-		$tq = new WP_Tax_Query( array(
-			array(
-				'field' => 'term_id',
-			),
-		) );
-		$query = $tq->queries[0];
-
-		$tq->transform_query( $tq->queries[0], 'term_id' );
-
-		$this->assertSame( $query, $tq->queries[0] );
-	}
-
-	public function test_transform_query_resulting_field_sanitized() {
-		$t1 = self::factory()->category->create( array( 'slug' => 'foo', ) );
-		$t2 = self::factory()->category->create( array( 'slug' => 'bar', ) );
-		$p = self::factory()->post->create();
-		wp_set_post_categories( $p, $t1 );
-
-		$tq1 = new WP_Tax_Query( array(
-			array(
-				'terms' => array( 'foo' ),
-				'field' => 'slug',
-			),
-		) );
-		$tq1->transform_query( $tq1->queries[0], 'term_taxonomy_id' );
-
-		$tq2 = new WP_Tax_Query( array(
-			array(
-				'terms' => array( 'foo' ),
-				'field' => 'slug',
-			),
-		) );
-		$tq2->transform_query( $tq2->queries[0], 'TERM_ ta%xonomy_id' );
-
-		$this->assertSame( $tq1->queries[0], $tq2->queries[0] );
-	}
-
-	public function test_transform_query_field_slug() {
-		$t1 = self::factory()->category->create( array( 'slug' => 'foo', ) );
-		$p = self::factory()->post->create();
-		$tt_ids = wp_set_post_categories( $p, $t1 );
-
-		$tq = new WP_Tax_Query( array(
-			array(
-				'taxonomy' => 'category',
-				'terms' => array( 'foo' ),
-				'field' => 'slug',
-			),
-		) );
-		$tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
-
-		$this->assertSame( $tt_ids, $tq->queries[0]['terms'] );
-		$this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
-	}
-
-	public function test_transform_query_field_name() {
-		$t1 = self::factory()->category->create( array( 'slug' => 'foo', 'name' => 'Foo', ) );
-		$p = self::factory()->post->create();
-		$tt_ids = wp_set_post_categories( $p, $t1 );
-
-		$tq = new WP_Tax_Query( array(
-			array(
-				'taxonomy' => 'category',
-				'terms' => array( 'Foo' ),
-				'field' => 'name',
-			),
-		) );
-		$tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
-
-		$this->assertSame( $tt_ids, $tq->queries[0]['terms'] );
-		$this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
-	}
-
-	public function test_transform_query_field_term_taxonomy_id() {
-		$t1 = self::factory()->category->create( array( 'slug' => 'foo', 'name' => 'Foo', ) );
-		$p = self::factory()->post->create();
-		$tt_ids = wp_set_post_categories( $p, $t1 );
-
-		$tq = new WP_Tax_Query( array(
-			array(
-				'taxonomy' => 'category',
-				'terms' => $tt_ids,
-				'field' => 'term_taxonomy_id',
-			),
-		) );
-		$tq->transform_query( $tq->queries[0], 'term_id' );
-
-		$this->assertEquals( array( $t1 ), $tq->queries[0]['terms'] );
-		$this->assertSame( 'term_id', $tq->queries[0]['field'] );
-	}
-
-	public function test_transform_query_field_term_taxonomy_default() {
-		$t1 = self::factory()->category->create( array( 'slug' => 'foo', 'name' => 'Foo', ) );
-		$p = self::factory()->post->create();
-		$tt_ids = wp_set_post_categories( $p, $t1 );
-
-		$tq = new WP_Tax_Query( array(
-			array(
-				'taxonomy' => 'category',
-				'terms' => array( $t1 ),
-				'field' => 'foo', // Anything defaults to term_id
-			),
-		) );
-		$tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
-
-		$this->assertEquals( $tt_ids, $tq->queries[0]['terms'] );
-		$this->assertSame( 'term_taxonomy_id', $tq->queries[0]['field'] );
-	}
-
-	public function test_transform_query_nonexistent_terms() {
-		$tq = new WP_Tax_Query( array(
-			array(
-				'terms' => array( 'foo' ),
-				'field' => 'slug',
-				'operator' => 'AND',
-			),
-		) );
-		$tq->transform_query( $tq->queries[0], 'term_taxonomy_id' );
-
-		$this->assertTrue( is_wp_error( $tq->queries[0] ) );
-	}
-
-	/**
-	 * @ticket 18105
-	 */
-	public function test_get_sql_relation_or_operator_in() {
-		register_taxonomy( 'wptests_tax', 'post' );
-
-		$t1 = self::factory()->term->create( array(
-			'taxonomy' => 'wptests_tax',
-		) );
-		$t2 = self::factory()->term->create( array(
-			'taxonomy' => 'wptests_tax',
-		) );
-		$t3 = self::factory()->term->create( array(
-			'taxonomy' => 'wptests_tax',
-		) );
-
-		$tq = new WP_Tax_Query( array(
-			'relation' => 'OR',
-			array(
-				'taxonomy' => 'wptests_tax',
-				'field' => 'term_id',
-				'terms' => $t1,
-			),
-			array(
-				'taxonomy' => 'wptests_tax',
-				'field' => 'term_id',
-				'terms' => $t2,
-			),
-			array(
-				'taxonomy' => 'wptests_tax',
-				'field' => 'term_id',
-				'terms' => $t3,
-			),
-		) );
-
+	public function test_term_taxonomy_id_single() {
 		global $wpdb;
-		$sql = $tq->get_sql( $wpdb->posts, 'ID' );
-
-		// Only one JOIN is required with OR + IN.
-		$this->assertSame( 1, substr_count( $sql['join'], 'JOIN' ) );
-
-		_unregister_taxonomy( 'wptests_tax' );
-	}
 
-	/**
-	 * @ticket 18105
-	 */
-	public function test_get_sql_relation_and_operator_in() {
 		register_taxonomy( 'wptests_tax', 'post' );
 
-		$t1 = self::factory()->term->create( array(
-			'taxonomy' => 'wptests_tax',
-		) );
-		$t2 = self::factory()->term->create( array(
-			'taxonomy' => 'wptests_tax',
-		) );
-		$t3 = self::factory()->term->create( array(
-			'taxonomy' => 'wptests_tax',
-		) );
-
-		$tq = new WP_Tax_Query( array(
-			'relation' => 'AND',
-			array(
-				'taxonomy' => 'wptests_tax',
-				'field' => 'term_id',
-				'terms' => $t1,
-			),
-			array(
-				'taxonomy' => 'wptests_tax',
-				'field' => 'term_id',
-				'terms' => $t2,
-			),
-			array(
-				'taxonomy' => 'wptests_tax',
-				'field' => 'term_id',
-				'terms' => $t3,
-			),
-		) );
+		$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
 
-		global $wpdb;
-		$sql = $tq->get_sql( $wpdb->posts, 'ID' );
+		// Manually change the term_taxonomy_id to something else.
+		$wpdb->update(
+			$wpdb->term_taxonomy,
+			array( 'term_taxonomy_id' => 12345 ),
+			array( 'term_id' => $terms[0] )
+		);
 
-		$this->assertSame( 3, substr_count( $sql['join'], 'JOIN' ) );
+		$q = new WP_Term_Query( array(
+			'term_taxonomy_id' => 12345,
+			'fields' => 'ids',
+			'hide_empty' => false,
+		) );
 
-		_unregister_taxonomy( 'wptests_tax' );
+		$this->assertEqualSets( array( $terms[0] ), $q->terms );
 	}
 
 	/**
-	 * @ticket 18105
+	 * @ticket 37074
 	 */
-	public function test_get_sql_nested_relation_or_operator_in() {
-		register_taxonomy( 'wptests_tax', 'post' );
-
-		$t1 = self::factory()->term->create( array(
-			'taxonomy' => 'wptests_tax',
-		) );
-		$t2 = self::factory()->term->create( array(
-			'taxonomy' => 'wptests_tax',
-		) );
-		$t3 = self::factory()->term->create( array(
-			'taxonomy' => 'wptests_tax',
-		) );
-
-		$tq = new WP_Tax_Query( array(
-			'relation' => 'OR',
-			array(
-				'taxonomy' => 'wptests_tax',
-				'field' => 'term_id',
-				'terms' => $t1,
-			),
-			array(
-				'relation' => 'OR',
-				array(
-					'taxonomy' => 'wptests_tax',
-					'field' => 'term_id',
-					'terms' => $t2,
-				),
-				array(
-					'taxonomy' => 'wptests_tax',
-					'field' => 'term_id',
-					'terms' => $t3,
-				),
-			),
-		) );
-
+	public function test_term_taxonomy_id_array() {
 		global $wpdb;
-		$sql = $tq->get_sql( $wpdb->posts, 'ID' );
-
-		$this->assertSame( 2, substr_count( $sql['join'], 'JOIN' ) );
 
-		_unregister_taxonomy( 'wptests_tax' );
-	}
-
-	/**
-	 * @ticket 29738
-	 */
-	public function test_get_sql_operator_not_in_empty_terms() {
 		register_taxonomy( 'wptests_tax', 'post' );
 
-		$tq = new WP_Tax_Query( array(
-			'relation' => 'OR',
-			array(
-				'taxonomy' => 'wptests_tax',
-				'field' => 'term_id',
-				'operator' => 'NOT IN',
-				'terms' => array(),
-			),
-		) );
+		$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
 
-		global $wpdb;
-		$expected = array(
-			'join' => '',
-			'where' => '',
+		// Manually change the term_taxonomy_id to something else.
+		$wpdb->update(
+			$wpdb->term_taxonomy,
+			array( 'term_taxonomy_id' => 12345 ),
+			array( 'term_id' => $terms[0] )
 		);
 
-		$this->assertSame( $expected, $tq->get_sql( $wpdb->posts, 'ID' ) );
-
-		_unregister_taxonomy( 'wptests_tax' );
-	}
-
-	/**
-	 * @ticket 29738
-	 */
-	public function test_get_sql_operator_and_empty_terms() {
-		register_taxonomy( 'wptests_tax', 'post' );
-
-		$tq = new WP_Tax_Query( array(
-			'relation' => 'OR',
-			array(
-				'taxonomy' => 'wptests_tax',
-				'field' => 'term_id',
-				'operator' => 'AND',
-				'terms' => array(),
-			),
-		) );
-
-		global $wpdb;
-		$expected = array(
-			'join' => '',
-			'where' => '',
+		$wpdb->update(
+			$wpdb->term_taxonomy,
+			array( 'term_taxonomy_id' => 6789 ),
+			array( 'term_id' => $terms[2] )
 		);
 
-		$this->assertSame( $expected, $tq->get_sql( $wpdb->posts, 'ID' ) );
+		$q = new WP_Term_Query( array(
+			'term_taxonomy_id' => array( 12345, 6789 ),
+			'fields' => 'ids',
+			'hide_empty' => false,
+		) );
 
-		_unregister_taxonomy( 'wptests_tax' );
+		$this->assertEqualSets( array( $terms[0], $terms[2] ), $q->terms );
 	}
 }
