Пример #1
0
/**
 * Handle edgecases and validation for shortcode attributes.
 *
 * @link http://codex.wordpress.org/Formatting_Date_and_Time Date & Time formatting
 * @param  array      $passed_atts   Array of values to parse
 * @param  array      $shortcode Name of shortcode being used (`wunderground`)
 * @return array                [description]
 */
function wunderground_parse_atts($passed_atts, $shortcode = 'wunderground')
{
    $defaults = array('title' => __('Weather Forecast', 'wunderground'), 'location_title' => NULL, 'location_data' => '', 'city' => '', 'location' => '', 'iconset' => 'Incredible', 'numdays' => 5, 'class' => 'wp_wunderground', 'layout' => 'table-vertical', 'measurement' => 'english', 'datelabel' => 'm/d', 'language' => wunderground_get_language(), 'showdata' => array('search', 'alerts', 'daynames', 'pop', 'icon', 'text', 'conditions', 'date'), 'hidedata' => array());
    // Use previous settings as defaults to better support backward compatibility
    $defaults = wp_parse_args(get_option('wp_wunderground', array()), $defaults);
    if (!empty($shortcode)) {
        $atts = shortcode_atts($defaults, $passed_atts, $shortcode);
    } else {
        $atts = wp_parse_args((array) $passed_atts, $defaults);
    }
    $atts['datelabel'] = wunderground_get_date_format($atts['datelabel']);
    // If there was no numdays passed,
    // 4 is a better default for table-horizontal layout
    if (empty($passed_atts['numdays'])) {
        switch ($atts['layout']) {
            case 'table-horizontal':
                $atts['numdays'] = 4;
                break;
        }
    }
    // Convert comma-separated value to array
    $atts['showdata'] = is_string($atts['showdata']) ? explode(',', $atts['showdata']) : $atts['showdata'];
    if (!is_numeric($atts['numdays'])) {
        Wunderground_Plugin::log_error(sprintf('"numdays" was set not a number: %s. Changed to the default: %d. wunderground_shortcode', $atts['numdays'], $defaults['numdays']));
        $atts['numdays'] = $defaults['numdays'];
    } else {
        if (absint($atts['numdays']) > 10) {
            Wunderground_Plugin::log_error(sprintf('"numdays" set too high in shortcode: %s. It was changed to the max: 10. wunderground_shortcode', $atts['numdays']));
            $atts['numdays'] = 10;
        }
    }
    // What to show in the search bar
    if (empty($atts['location_title'])) {
        $atts['location_title'] = $atts['location'];
    }
    // Process hidedata/showdata
    if (!empty($atts['hidedata'])) {
        $hidedata = is_array($atts['hidedata']) ? $hidedata : explode(',', $atts['hidedata']);
        // For each hidedata, unset showdata.
        foreach ($hidedata as $value) {
            foreach ($atts['showdata'] as $k => $v) {
                if ($v === $value) {
                    unset($atts['showdata'][$k]);
                }
            }
        }
    }
    return $atts;
}
 /**
  * Generate and output the widget
  *
  * @see Wunderground_Template in class-template.php to generate data
  *
  * @param  array $args     Widget args
  * @param  array $instance Widget settings
  */
 function widget($args, $instance)
 {
     $this->args = $args;
     $instance['location_data'] = json_decode($instance['location_data'], true);
     $this->instance = $instance;
     $title = apply_filters('widget_title', !isset($instance['title']) ? NULL : $instance['title'], $instance, $this->id_base);
     extract($args);
     $location = $this->getLocation();
     $request = new Wunderground_Request($location, null, $instance['language'], $instance['measurement']);
     $forecast = new Wunderground_Forecast($request);
     $data = $instance;
     $data['widget'] = $args;
     $data['class'] = isset($data['class']) ? $data['class'] : 'wp_wunderground';
     $language_details = wunderground_get_language($data['language'], true);
     if (!empty($language_details['rtl'])) {
         $data['class'] .= ' wu-rtl';
     }
     $data['forecast'] = $forecast;
     $data['location'] = $instance['city'];
     $data['location_title'] = empty($instance['location_title']) ? $data['location'] : $instance['location_title'];
     $data['wunderground'] = new KWS_Wunderground($request);
     $data['datelabel'] = isset($data['datelabel']) ? $data['datelabel'] : wunderground_get_date_format();
     // PWS is offline or something.
     if (!empty($data['wunderground']->response->error)) {
         $this->maybe_display_error($data['wunderground']->response->error);
         do_action('wunderground_log_debug', 'There was an error in the Wunderground response:', $data['wunderground']->response->error);
         return;
     }
     echo $before_widget;
     if (!empty($title)) {
         echo $before_title . $title . $after_title;
     }
     /**
      * @see Wunderground_Template in class-template.php
      */
     do_action('wunderground_render_template', $instance['layout'], $data);
     echo $after_widget;
 }