Make WordPress Core

Changeset 38126


Ignore:
Timestamp:
07/21/2016 03:41:17 AM (10 years ago)
Author:
azaozz
Message:

TinyMCE, link check:

  • Use wp.a11y.speak() to announce bad URLs.
  • Do not add a title to the link toolbar.
  • Better error message.

Props afercia, azaozz.
See #36638.

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r38119 r38126  
    39073907        }
    39083908
     3909        // No redirects
    39093910        $response = wp_safe_remote_get( $href, array(
    39103911                'timeout' => 15,
    … …  
    39133914        ) );
    39143915
    3915         $message = null;
     3916        $error = false;
    39163917
    39173918        if ( is_wp_error( $response ) ) {
    3918                 $error = $response->get_error_message();
    3919 
    3920                 if ( strpos( $message, 'resolve host' ) !== false ) {
    3921                         $message = array( 'error' => __( 'Invalid host name.' ) );
    3922                 }
    3923 
    3924                 wp_send_json_error( $message );
    3925         }
    3926 
    3927         if ( wp_remote_retrieve_response_code( $response ) === 404 ) {
    3928                 wp_send_json_error( array( 'error' => __( 'Not found, HTTP error 404.' ) ) );
     3919                if ( strpos( $response->get_error_message(), 'resolve host' ) !== false ) {
     3920                        $error = true;
     3921                }
     3922        } elseif ( wp_remote_retrieve_response_code( $response ) === 404 ) {
     3923                $error = true;
     3924        }
     3925
     3926        if ( $error ) {
     3927                wp_send_json_error( array( 'httpError' => true ) );
    39293928        }
    39303929
  • trunk/src/wp-includes/class-wp-editor.php

    r38110 r38126  
    10661066                        'Letter' => __( 'Letter' ),
    10671067                        'Action' => __( 'Action' ),
    1068                         'Invalid host name.' => __( 'Invalid host name.' ),
     1068                        'Warning: the link has been inserted but the destination cannot be reached.' => __( 'Warning: the link has been inserted but the destination cannot be reached.' ),
    10691069                        '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.' =>
    10701070                                __( '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.' ),
  • trunk/src/wp-includes/js/tinymce/plugins/wplink/plugin.js

    r37927 r38126  
    9595                var $ = window.jQuery;
    9696                var urlErrors = {};
     97                var emailRegex = /^(mailto:)?[a-z0-9._%+-]+@[a-z0-9][a-z0-9.-]*\.[a-z]{2,63}$/i;
     98                var speak = ( typeof window.wp !== 'undefined' && window.wp.a11y && window.wp.a11y.speak ) ? window.wp.a11y.speak : function() {};
     99                var hasLinkError = false;
    97100
    98101                function getSelectedLink() {
    … …  
    148151                }
    149152
     153                function setLinkError( $link ) {
     154                        hasLinkError = true;
     155                        $link.attr( 'data-wplink-url-error', 'true' );
     156                        speak( editor.translate( 'Warning: the link has been inserted but the destination cannot be reached.' ), 'assertive' );
     157
     158                        if ( toolbar && toolbar.visible() ) {
     159                                toolbar.$el.find( '.wp-link-preview a' ).addClass( 'wplink-url-error' );
     160                        }
     161                }
     162
    150163                function checkLink( node ) {
    151164                        var $link = editor.$( node );
    … …  
    156169                        }
    157170
    158                         // Early check
    159                         if ( /^http/i.test( href ) && ! /\.[a-z]{2,63}(\/|$)/i.test( href ) ) {
    160                                 urlErrors[href] = tinymce.translate( 'Invalid host name.' );
     171                        hasLinkError = false;
     172
     173                        if ( /^http/i.test( href ) && ! /^https?:\/\/[a-z0-9][a-z0-9.-]*\.[a-z]{2,63}(\/|$)/i.test( href ) ) {
     174                                urlErrors[href] = true;
    161175                        }
    162176
    163177                        if ( urlErrors.hasOwnProperty( href ) ) {
    164                                 $link.attr( 'data-wplink-url-error', 'true' );
     178                                setLinkError( $link );
    165179                                return;
    166180                        } else {
    … …  
    180194                                }
    181195
    182                                 if ( response.data && response.data.error ) {
    183                                         urlErrors[href] = response.data.error;
    184                                         $link.attr( 'data-wplink-url-error', 'true' );
    185 
    186                                         if ( toolbar && toolbar.visible() ) {
    187                                                 toolbar.$el.find( '.wp-link-preview a' ).addClass( 'wplink-url-error' ).attr( 'title', editor.dom.encode( response.data.error ) );
    188                                         }
     196                                if ( response.data && response.data.httpError ) {
     197                                        urlErrors[href] = true;
     198                                        setLinkError( $link );
    189199                                }
    190200                        });
    … …  
    275285                                }
    276286
    277                                 if ( ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( href ) ) {
     287                                if ( ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( href ) && ! emailRegex.test( href ) ) {
    278288                                        href = 'http://' + href;
    279289                                }
    … …  
    292302
    293303                        // Audible confirmation message when a link has been inserted in the Editor.
    294                         if ( typeof window.wp !== 'undefined' && window.wp.a11y && typeof window.wpLinkL10n !== 'undefined' ) {
    295                                 window.wp.a11y.speak( window.wpLinkL10n.linkInserted );
     304                        if ( typeof window.wpLinkL10n !== 'undefined' && ! hasLinkError ) {
     305                                speak( window.wpLinkL10n.linkInserted );
    296306                        }
    297307                } );
    … …  
    450460                                                        $( element.firstChild.nextSibling ).val( ui.item.title );
    451461
    452                                                         if ( 9 === event.keyCode && typeof window.wp !== 'undefined' &&
    453                                                                 window.wp.a11y && typeof window.wpLinkL10n !== 'undefined' ) {
     462                                                        if ( 9 === event.keyCode && typeof window.wpLinkL10n !== 'undefined' ) {
    454463                                                                // Audible confirmation message when a link has been selected.
    455                                                                 window.wp.a11y.speak( window.wpLinkL10n.linkSelected );
     464                                                                speak( window.wpLinkL10n.linkSelected );
    456465                                                        }
    457466
    … …  
    537546                editor.on( 'wptoolbar', function( event ) {
    538547                        var linkNode = editor.dom.getParent( event.element, 'a' ),
    539                                 $linkNode, href, edit, title;
     548                                $linkNode, href, edit;
    540549
    541550                        if ( typeof window.wpLink !== 'undefined' && window.wpLink.modalOpen ) {
    … …  
    562571                                        event.element = linkNode;
    563572                                        event.toolbar = toolbar;
    564                                         title = urlErrors.hasOwnProperty( href ) ? editor.dom.encode( urlErrors[ href ] ) : null;
    565573
    566574                                        if ( $linkNode.attr( 'data-wplink-url-error' ) === 'true' ) {
    567                                                 toolbar.$el.find( '.wp-link-preview a' ).addClass( 'wplink-url-error' ).attr( 'title', title );
     575                                                toolbar.$el.find( '.wp-link-preview a' ).addClass( 'wplink-url-error' );
    568576                                        } else {
    569                                                 toolbar.$el.find( '.wp-link-preview a' ).removeClass( 'wplink-url-error' ).attr( 'title', null );
     577                                                toolbar.$el.find( '.wp-link-preview a' ).removeClass( 'wplink-url-error' );
     578                                                hasLinkError = false;
    570579                                        }
    571580                                }
  • trunk/src/wp-includes/js/wplink.js

    r37741 r38126  
    33( function( $, wpLinkL10n, wp ) {
    44        var editor, searchTimer, River, Query, correctedURL, linkNode,
    5                 emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,
    6                 urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i,
     5                emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,63}$/i,
     6                urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,63}[^ "]*$/i,
    77                inputs = {},
    88                rivers = {},
Note: See TracChangeset for help on using the changeset viewer.