WordPress.org

Plugin Directory

Changeset 622215


Ignore:
Timestamp:
11/07/12 18:12:04 (18 months ago)
Author:
andy
Message:

Jetpack Stats: improve error messaging when stats retrieval fails

File:
1 edited

Legend:

Unmodified
Added
Removed
  • jetpack/trunk/modules/stats.php

    r618676 r622215  
    412412    $get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) ); 
    413413    $get_code = wp_remote_retrieve_response_code( $get ); 
    414     $get_code_type = intval( $get_code / 100 ); 
    415     if ( is_wp_error( $get ) || ( 2 != $get_code_type && 304 != $get_code ) ) { 
    416         // @todo nicer looking error 
    417         if ( 3 == $get_code_type ) { 
    418             echo '<p>' . __( 'We were unable to get your stats just now (too many redirects). Please try again.', 'jetpack' ) . '</p>'; 
    419         } else { 
    420             echo '<p>' . __( 'We were unable to get your stats just now. Please try again.', 'jetpack' ) . '</p>'; 
    421         } 
     414    if ( is_wp_error( $get ) || ( 2 != intval( $get_code / 100 ) && 304 != $get_code ) || empty( $get['body'] ) ) { 
     415        stats_print_wp_remote_error( $get, $url ); 
    422416    } else { 
    423417        if ( !empty( $get['headers']['content-type'] ) ) { 
     
    873867    $get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) ); 
    874868    $get_code = wp_remote_retrieve_response_code( $get ); 
    875     $get_code_type = intval( $get_code / 100 ); 
    876     if ( is_wp_error( $get ) || ( 2 != $get_code_type && 304 != $get_code ) || empty( $get['body'] ) ) { 
    877         // @todo 
    878         if ( 3 == $get_code_type ) { 
    879             echo '<p>' . __( 'We were unable to get your stats just now (too many redirects). Please try again.', 'jetpack' ) . '</p>'; 
    880         } else { 
    881             echo '<p>' . __( 'We were unable to get your stats just now. Please try again.', 'jetpack' ) . '</p>'; 
    882         } 
     869    if ( is_wp_error( $get ) || ( 2 != intval( $get_code / 100 ) && 304 != $get_code ) || empty( $get['body'] ) ) { 
     870        stats_print_wp_remote_error( $get, $url ); 
    883871    } else { 
    884872        $body = stats_convert_post_titles($get['body']); 
     
    953941} 
    954942 
     943function stats_print_wp_remote_error( $get, $url ) { 
     944?> 
     945    <div class="wrap"> 
     946    <p><?php printf( __( 'We were unable to get your stats just now. Please reload this page to try again. If this error persists, please <a href="%1$s">contact support</a>. In your report please include the information below.' ), 'http://support.wordpress.com/contact/?jetpack=needs-service' ); ?></p> 
     947    <pre> 
     948    User Agent: "<?php print htmlspecialchars( $_SERVER['HTTP_USER_AGENT'] ); ?>" 
     949    Page URL: "http<?php print (is_ssl()?'s':'') . '://' . htmlspecialchars( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); ?>" 
     950    API URL: "<?php print clean_url( $url ); ?>" 
     951<?php 
     952    if ( is_wp_error( $get ) ) { 
     953        foreach ( $get->get_error_codes() as $code ) { 
     954            foreach ( $get->get_error_messages($code) as $message ) { 
     955                ?> 
     956    <?php print $code . ': "' . $message . '"' ?> 
     957 
     958<?php 
     959            } 
     960        } 
     961    } else { 
     962        $get_code = wp_remote_retrieve_response_code( $get ); 
     963        $content_length = strlen( wp_remote_retrieve_body( $get ) ); 
     964        ?> 
     965    Response code: "<?php print $get_code ?>" 
     966    Content length: "<?php print $content_length ?>" 
     967 
     968<?php 
     969    } 
     970    ?></pre> 
     971    </div> 
     972    <?php 
     973} 
     974 
    955975function stats_get_csv( $table, $args = null ) { 
    956976    $defaults = array( 'end' => false, 'days' => false, 'limit' => 3, 'post_id' => false, 'summarize' => '' ); 
Note: See TracChangeset for help on using the changeset viewer.