💎 PREMIUM: Changeset/ - Full Archive

Changeset 2679871


Ignore:
Timestamp:
02/16/2022 01:23:41 PM (4 years ago)
Author:
pandammonium
Message:

Improves error handling.

Location:
blipper-widget/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • blipper-widget/trunk/blipper-widget.php

    r2428330 r2679871  
    1111  * Plugin URI:         http://pandammonium.org/wordpress-dev/blipper-widget/
    1212  * Description:        Display your latest blip in a widget.  Requires a Blipfoto account (available free of charge).
    13   * Version:            1.1.1
     13  * Version:            1.1.2
    1414  * Author:             Caity Ross
    1515  * Author URI:         http://pandammonium.org/
     
    4848use blipper_widget\blipper_widget_settings;
    4949
     50// -------------------------------------------------------------------------- //
     51
     52define( 'BW_DEBUG', true );
     53
    5054// --- Action hooks --------------------------------------------------------- //
    5155
     
    8185  */
    8286function blipper_widget_exception( $e ) {
    83   error_log( 'Error.  ' . $e->getMessage() . '.' );
    84   echo '<p>An unexpected error has occurred.  ' . $e->getMessage() . '.  Sorry about that.  Please check your settings or try again later.</p>';
     87  if ( BW_DEBUG ) { error_log( 'Blipper Widget: ' . $e->getMessage() . '.' ); }
     88  echo '<p class="blipper-widget error">Blipper Widget: An unexpected error has occurred.  ' . $e->getMessage() . '.  Please check your settings or try again later.</p>';
    8589}
    8690set_exception_handler('blipper_widget_exception');
     
    119123      'display-date'           => 'show',                  // 'hide'
    120124      'display-journal-title'  => 'hide',                  // 'show'
     125      'display-powered-by'     => 'hide',                  // 'show'
    121126      'add-link-to-blip'       => 'hide',                  // 'show'
    122       'display-powered-by'     => 'hide',                  // 'show'
    123127    ),
    124128);
     
    212216  public function form( $settings ) {
    213217
    214     error_log( 'Blipper_Widget::form( $settings: ' . json_encode( $settings, JSON_PRETTY_PRINT ) . ' )' );
     218    if ( BW_DEBUG ) { error_log( 'Blipper_Widget::form( $settings: ' . json_encode( $settings, JSON_PRETTY_PRINT ) . ' )' ); }
    215219    $this->blipper_widget_display_form( $this->blipper_widget_get_display_values( $settings ) );
    216220
     
    261265    $settings['style-control']          = $style_control;
    262266
    263     error_log( 'Blipper_Widget::update( $new_settings: ' . json_encode( $new_settings, JSON_PRETTY_PRINT ) . ' $old_settings: ' . json_encode( $old_settings, JSON_PRETTY_PRINT ) . ' )' );
    264     error_log( 'Actual settings: ' . json_encode( $settings, JSON_PRETTY_PRINT ) );
     267    if ( BW_DEBUG ) { error_log( 'Blipper_Widget::update( $new_settings: ' . json_encode( $new_settings, JSON_PRETTY_PRINT ) . ' $old_settings: ' . json_encode( $old_settings, JSON_PRETTY_PRINT ) . ' )' ); }
     268    if ( BW_DEBUG ) { error_log( 'Actual settings: ' . json_encode( $settings, JSON_PRETTY_PRINT ) ); }
    265269
    266270    return $settings;
     
    280284public function blipper_widget_shortcode_blip_display( $atts, $content=null, $shortcode="", $print=false) {
    281285
    282   error_log( "Blipper_Widget::blipper_widget_shortcode_blip_display( \$atts: " . json_encode( $atts, JSON_PRETTY_PRINT ) . "), \$content: '" . $content . "', \$shortcode: " . $shortcode . ")\n" );
     286  if ( BW_DEBUG ) { error_log( "Blipper_Widget::blipper_widget_shortcode_blip_display( \$atts: " . json_encode( $atts, JSON_PRETTY_PRINT ) . "), \$content: '" . $content . "', \$shortcode: " . $shortcode . ")\n" ); }
    283287
    284288  $settings = array_merge( $this->default_setting_values['shortcode'], $this->default_setting_values['common'] );
     
    286290  $args = shortcode_atts( $settings, $atts, $shortcode );
    287291  extract( $args );
    288   error_log( "Collated arguments: " . json_encode( $args, JSON_PRETTY_PRINT ) . "\n" );
     292  if ( BW_DEBUG ) { error_log( "Collated arguments: " . json_encode( $args, JSON_PRETTY_PRINT ) . "\n" ); }
    289293
    290294   $the_title = '';
     
    323327  private function blipper_widget_validate( $new_settings, $old_settings, $setting_field ) {
    324328
    325     error_log( "Blipper_Widget::blipper_widget_validate( $setting_field )" );
    326     error_log( "\tCurrent value:   " . ( array_key_exists( $setting_field, $old_settings ) ? $old_settings[$setting_field] : "undefined" ) );
    327     error_log( "\tProposed value:  " . ( array_key_exists( $setting_field, $new_settings ) ? $new_settings[$setting_field] : "undefined" ) );
     329    if ( BW_DEBUG ) { error_log( "Blipper_Widget::blipper_widget_validate( $setting_field )" ); }
     330    if ( BW_DEBUG ) { error_log( "\tCurrent value:   " . ( array_key_exists( $setting_field, $old_settings ) ? $old_settings[$setting_field] : "undefined" ) ); }
     331    if ( BW_DEBUG ) { error_log( "\tProposed value:  " . ( array_key_exists( $setting_field, $new_settings ) ? $new_settings[$setting_field] : "undefined" ) ); }
    328332
    329333    if ( array_key_exists( $setting_field, $old_settings ) && array_key_exists( $setting_field, $new_settings ) ) {
    330334      if ( $new_settings[$setting_field] === $old_settings[$setting_field] ) {
    331335        $settings =  $old_settings[$setting_field];
    332         error_log( "\tValue unchanged\n" );
     336        if ( BW_DEBUG ) { error_log( "\tValue unchanged\n" ); }
    333337        return $settings;
    334338      }
     
    367371    }
    368372
    369     error_log( "\tNew value:       $settings\n" );
     373    if ( BW_DEBUG ) { error_log( "\tNew value:       $settings\n" ); }
    370374
    371375    return $settings;
     
    384388  private function blipper_widget_get_display_values( $settings ) {
    385389
    386     error_log( 'Blipper_Widget::blipper_widget_get_display_values (' . json_encode( $settings, JSON_PRETTY_PRINT ) . ')');
     390    if ( BW_DEBUG ) { error_log( 'Blipper_Widget::blipper_widget_get_display_values (' . json_encode( $settings, JSON_PRETTY_PRINT ) . ')'); }
    387391
    388392    $new_settings = array();
     
    419423  private function blipper_widget_get_display_value( $setting, $settings ) {
    420424
    421     error_log( 'Blipper_Widget::blipper_widget_get_display_value (' . json_encode( $setting, JSON_PRETTY_PRINT ) . ', ' . json_encode( $settings, JSON_PRETTY_PRINT ) . ')');
     425    if ( BW_DEBUG ) { error_log( 'Blipper_Widget::blipper_widget_get_display_value (' . json_encode( $setting, JSON_PRETTY_PRINT ) . ', ' . json_encode( $settings, JSON_PRETTY_PRINT ) . ')'); }
    422426
    423427    try {
     
    432436          } else {
    433437            throw new ErrorException( 'Invalid setting requested:  <strong>' . $setting . '</strong>.' );
    434             error_log( 'Invalid setting requested:' . $setting );
     438            if ( BW_DEBUG ) { error_log( 'Invalid setting requested:' . $setting ); }
    435439            return '';
    436440          }
     
    555559        } else {
    556560          $client_ok = true;
    557           error_log( 'Blipper_Widget::blipper_widget_create_blipfoto_client( \$settings: ' . json_encode( $settings, JSON_PRETTY_PRINT ) . ' )' );
    558           error_log( 'Client: ' . json_encode( $this->client, JSON_PRETTY_PRINT ) );
     561          if ( BW_DEBUG ) { error_log( 'Blipper_Widget::blipper_widget_create_blipfoto_client( \$settings: ' . json_encode( $settings, JSON_PRETTY_PRINT ) . ' )' ); }
     562          if ( BW_DEBUG ) { error_log( 'Client: ' . json_encode( $this->client, JSON_PRETTY_PRINT ) ); }
    559563        }
    560564
     
    607611      }
    608612    } else {
    609       error_log( 'CLIENT IS NOT OK' );
     613      if ( BW_DEBUG ) { error_log( 'CLIENT IS NOT OK' ); }
    610614    }
    611615    return $client_ok;
     
    10081012
    10091013      } finally {
    1010         error_log( "The completed blip:\n" . $the_blip );
     1014        if ( BW_DEBUG ) { error_log( "The completed blip:\n" . $the_blip ); }
    10111015      }
    10121016
     
    10361040private function blipper_widget_get_styling( $element, $is_widget, $style_control, $settings ) {
    10371041
    1038   error_log( 'Blipper_Widget::blipper_widget_get_styling( ' . $element . ', ' . (int)$is_widget . ', ' . (int)$style_control . ' )' );
     1042  if ( BW_DEBUG ) { error_log( 'Blipper_Widget::blipper_widget_get_styling( ' . $element . ', ' . (int)$is_widget . ', ' . (int)$style_control . ' )' ); }
    10391043
    10401044  // If the blip is not to be displayed in a widget or if the widget is to be
     
    11051109private function blipper_widget_sanitise_html( $html ) {
    11061110
    1107   return wp_kses( $html, ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'b', 'em', 'div'] );
     1111  $allowed_html = array(
     1112    'p' => array(),
     1113    'h1' => array(),
     1114    'h2' => array(),
     1115    'h3' => array(),
     1116    'h4' => array(),
     1117    'h5' => array(),
     1118    'h6' => array(),
     1119    'i' => array(),
     1120    'b' => array(),
     1121    'em' => array(),
     1122    'div' => array(),
     1123    'br' => array(),
     1124    'a' => array(
     1125      'href' => array(),
     1126      'title' => array(),
     1127    ),
     1128  );
     1129  error_log( "Dirty HTML: $html\nClean HTML: " . wp_kses( $html, $allowed_html ) );
     1130  return wp_kses( $html, $allowed_html );
    11081131
    11091132}
     
    11201143private function blipper_widget_sanitise_url( $url ) {
    11211144
    1122   error_log( "Blipper_Widget::blipper_widget_sanitise_url:\n$url ⟹\n" . esc_url( $url ) );
     1145  if ( BW_DEBUG ) { error_log( "Blipper_Widget::blipper_widget_sanitise_url:\n$url ⟹\n" . esc_url( $url ) ); }
    11231146
    11241147  return esc_url( $url );
     
    11571180  private function blipper_widget_display_form( $settings ) {
    11581181
    1159     error_log( "Blipper_Widget::blipper_widget_display_form( " . json_encode( $settings, JSON_PRETTY_PRINT ) . ')' );
     1182    if ( BW_DEBUG ) { error_log( "Blipper_Widget::blipper_widget_display_form( " . json_encode( $settings, JSON_PRETTY_PRINT ) . ')' ); }
    11601183
    11611184    $oauth_settings = $this->settings->blipper_widget_get_settings();
     
    12841307      </p>
    12851308
    1286       <?php error_log(
     1309      <?php if ( BW_DEBUG ) { error_log(
    12871310             'NAME: ' . $this->get_field_name( 'style-control' )
    12881311          . ' ID: ' . $this->get_field_name( 'style-control' )
    12891312          . ' DEFAULT VALUE: ' . $this->default_setting_values['widget']['style-control']
    1290           . ' ACTUAL VALUE: ' . $settings['style-control'] );
     1313          . ' ACTUAL VALUE: ' . $settings['style-control'] ); }
    12911314      ?>
    12921315
     
    15211544          )
    15221545        : ( "No key, no value; adding default (common): " . $this->default_setting_values['common']['display-journal-title'] );