Editor: after inserting a link detect if the URL is broken, first run.

Props iseulde, azaozz.
See #36638.
Built from https://develop.svn.wordpress.org/trunk@37741


git-svn-id: http://core.svn.wordpress.org/trunk@37706 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2016-06-17 20:41:28 +00:00
parent 8776694a33
commit accf240d37
14 changed files with 151 additions and 12 deletions

View File

@ -64,7 +64,7 @@ $core_actions_post = array(
'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

View File

@ -3832,3 +3832,45 @@ function wp_ajax_search_install_plugins() {
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();
}

View File

@ -1063,6 +1063,7 @@ final class _WP_Editors {
'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 @@ final class _WP_Editors {
</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.

View File

@ -1757,6 +1757,14 @@ div.wp-link-preview a {
cursor: pointer;
}
div.wp-link-preview a.wplink-url-error {
color: #a00;
}
div.wp-link-preview a.wplink-url-error:hover {
color: #f00;
}
div.wp-link-input {
float: right;
margin: 2px;

File diff suppressed because one or more lines are too long

View File

@ -1757,6 +1757,14 @@ div.wp-link-preview a {
cursor: pointer;
}
div.wp-link-preview a.wplink-url-error {
color: #a00;
}
div.wp-link-preview a.wplink-url-error:hover {
color: #f00;
}
div.wp-link-input {
float: left;
margin: 2px;

File diff suppressed because one or more lines are too long

View File

@ -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 );

File diff suppressed because one or more lines are too long

View File

@ -205,6 +205,13 @@ audio {
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;
}
/**
* WP Views
*/

View File

@ -434,6 +434,10 @@ var wpLink;
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 );
},

File diff suppressed because one or more lines are too long

View File

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