WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/17/2016 08:40:04 PM (4 years ago)
Author:
azaozz
Message:

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

Props iseulde, azaozz.
See #36638.

File:
1 edited

Legend:

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

    r37714 r37741  
    38333833    wp_send_json_success( $status );
    38343834}
     3835
     3836/**
     3837 * Ajax handler for testing if an URL exists. Used in the editor.
     3838 *
     3839 * @since 4.6.0
     3840 */
     3841function wp_ajax_test_url() {
     3842    if ( ! current_user_can( 'edit_posts' ) || ! wp_verify_nonce( $_POST['nonce'], 'wp-test-url' ) ) {
     3843        wp_send_json_error();
     3844    }
     3845
     3846    $href = esc_url_raw( $_POST['href'] );
     3847
     3848    // Relative URL
     3849    if ( strpos( $href, '//' ) !== 0 && in_array( $href[0], array( '/', '#', '?' ), true ) ) {
     3850        $href = get_bloginfo( 'url' ) . $href;
     3851    }
     3852
     3853    $response = wp_safe_remote_get( $href, array(
     3854        'timeout' => 15,
     3855        // Use an explicit user-agent
     3856        'user-agent' => 'WordPress URL Test',
     3857    ) );
     3858
     3859    $message = null;
     3860
     3861    if ( is_wp_error( $response ) ) {
     3862        $error = $response->get_error_message();
     3863
     3864        if ( strpos( $message, 'resolve host' ) !== false ) {
     3865            $message = array( 'error' => __( 'Invalid host name.' ) );
     3866        }
     3867
     3868        wp_send_json_error( $message );
     3869    }
     3870
     3871    if ( wp_remote_retrieve_response_code( $response ) === 404 ) {
     3872        wp_send_json_error( array( 'error' => __( 'Not found, HTTP error 404.' ) ) );
     3873    }
     3874
     3875    wp_send_json_success();
     3876}
Note: See TracChangeset for help on using the changeset viewer.