コード例 #1
0
 /**
  * @param string $template Template slug based on value passed by `wunderground_render_template` filter
  * @param array $data
  */
 function render($template = NULL, $data = array())
 {
     // The translation text
     $data['strings'] = $this->strings();
     // The base URL for the weather icons
     $data['user_icon_url'] = wunderground_get_icon($data['iconset']);
     // The required logo
     $data['logo'] = wunderground_get_logo();
     $language = isset($data['language']) ? $data['language'] : NULL;
     // The subdomain used by the Wunderground logo
     $data['subdomain'] = wunderground_get_subdomain($language);
     // Map the keys so that they are consistent instead of having some
     // using key => key and others using index => key
     foreach ((array) $data['showdata'] as $key => $value) {
         $data['showdata'][$value] = $value;
     }
     // Enqueue the scripts
     do_action('wunderground_print_scripts', true);
     /**
      * Filter the data passed to the template
      * @var array
      */
     $data = apply_filters('wunderground_template_data', apply_filters('wunderground_template_data_' . $template, $data));
     // Generate a cache key based on the result. Only get the first 43 characters because of the transient key length limit.
     $cache_key = substr('wut_' . sha1(serialize($data)), 0, 43);
     $output = get_transient($cache_key);
     // If there's no cached result or caching is disabled
     if (empty($output) || is_wp_error($output) || isset($_GET['cache']) && current_user_can('manage_options')) {
         $output = $this->twig->render("{$template}.html", $data);
         /**
          * Modify the number of seconds to cache the request for.
          *
          * Default: cache the request for one hour, since we're dealing with changing conditions
          *
          * @var int
          */
         $cache_time = apply_filters('wunderground_cache_time', HOUR_IN_SECONDS);
         // The nice thing is that the cache is invalidated when the forecast results change, so there's no need for the cache time to be exact.
         set_transient($cache_key, $output, $cache_time * 2);
     }
     /**
      * Modify the HTML output of the forecast
      * @param string $output HTML of the forecast
      * @param string $template Template slug based on value passed by `wunderground_render_template` filter
      * @param array $data Template data array, with keys `strings`, `showdata`, `subdomain`, `logo`, `user_icon_url`, `language`
      */
     $output = apply_filters('wp_wunderground_forecast', $output, $template, $data);
     echo $output;
 }
コード例 #2
0
    /**
     * The form for the widget.
     * @param  array $instance Widget instance
     */
    function form($instance)
    {
        $instance = wunderground_parse_atts($instance);
        extract($instance);
        ?>
	<div class="wunderground-settings">

		<div class="setting-wrapper">
			<label for="<?php 
        echo $this->get_field_id('title');
        ?>
">
				<h3><?php 
        esc_html_e('Title', 'wunderground');
        ?>
</h3>
				<input type="text" class="widefat" id="<?php 
        echo $this->get_field_id('title');
        ?>
" name="<?php 
        echo $this->get_field_name('title');
        ?>
" value="<?php 
        echo esc_attr($title);
        ?>
" placeholder="<?php 
        esc_attr_e('Leave empty to hide the widget title.', 'wunderground');
        ?>
" />
			</label>
		</div>

		<div class="setting-wrapper autocomplete" id="<?php 
        echo $this->get_field_id('autocomplete');
        ?>
">
			<label for="<?php 
        echo $this->get_field_id('city');
        ?>
">
				<h3><?php 
        esc_html_e('Location', 'wunderground');
        ?>
</h3>
				<p class="description"><?php 
        esc_html_e('Locations will autoload, but you may also define custom locations.', 'wunderground');
        ?>
</p>
				<input type="text" class="wu-autocomplete widefat" autocomplete="false" id="<?php 
        echo $this->get_field_id('city');
        ?>
" name="<?php 
        echo $this->get_field_name('city');
        ?>
" value="<?php 
        echo esc_attr($city);
        ?>
" placeholder="<?php 
        esc_attr_e('Enter the name of a location.', 'wunderground');
        ?>
" />
			</label>
			<input type="hidden" class="wu-location-data" id="<?php 
        echo $this->get_field_id('location_data');
        ?>
" name="<?php 
        echo $this->get_field_name('location_data');
        ?>
" value="<?php 
        esc_attr_e($location_data);
        ?>
" />
		</div>

		<div class="setting-wrapper">
			<label for="<?php 
        echo $this->get_field_id('location_title');
        ?>
">
				<h3><?php 
        esc_html_e('Location Title', 'wunderground');
        ?>
</h3>
				<p class="description"><?php 
        esc_attr_e('Change how the location is displayed in the widget search field.', 'wunderground');
        ?>
</p>
				<input type="text" class="widefat" id="<?php 
        echo $this->get_field_id('location_title');
        ?>
" name="<?php 
        echo $this->get_field_name('location_title');
        ?>
" value="<?php 
        esc_attr_e($location_title);
        ?>
" placeholder="<?php 
        esc_attr_e('Leave empty to use the location name.', 'wunderground');
        ?>
" />
				<span class="howto"><?php 
        esc_attr_e('Example: if the Location is set to "Denver, Colorado", you may prefer to set the Location Title as "Denver", which is simpler.', 'wunderground');
        ?>
</span>
			</label>
		</div>

		<div class="setting-wrapper">
		<?php 
        $days_select = wunderground_render_select($this->get_field_name('numdays'), $this->get_field_id('numdays'), array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10), $numdays);
        echo sprintf('<label for="%s"><h3>%s</h3> %s</label>', $this->get_field_id('numdays'), __('# of Days in Forecast', 'wunderground'), $days_select);
        ?>
			<p>
				<label>
					<input type="checkbox" value="current" name="<?php 
        echo $this->get_field_name('showdata');
        ?>
[current]" <?php 
        checked(in_array('current', (array) $showdata), true);
        ?>
 />
					<span class="title"><?php 
        esc_html_e('Include Current Conditions', 'wunderground');
        ?>
</span>
					<span class="howto"><?php 
        esc_html_e('Add the current conditions to the forecast.', 'wunderground');
        ?>
</span>
				</label>
			</p>
			<p>
				<label>
					<input type="checkbox" value="night" name="<?php 
        echo $this->get_field_name('showdata');
        ?>
[night]" <?php 
        checked(in_array('night', (array) $showdata), true);
        ?>
 />
					<span class="title"><?php 
        esc_html_e('Include Night Forecasts', 'wunderground');
        ?>
</span>
					<span class="howto"><?php 
        esc_html_e('This will result in double the number of forecasts shown.', 'wunderground');
        ?>
</span>
				</label>
			</p>
		</div>

		<div class="setting-wrapper icons">
			<h3><?php 
        esc_html_e('Icon Set', 'wunderground');
        ?>
</h3>
			<p class="description"><?php 
        esc_html_e('Choose the look and feel of the images that will represent the weather.', 'wunderground');
        ?>
</p>

			<ul>
		<?php 
        $icons = wunderground_get_icons(true);
        foreach ($icons as $name => $key) {
            $checked = checked($iconset, $name, false);
            echo sprintf('
				<li class="alignleft">
					<label>
						<input class="alignleft" type="radio" value="%s" name="%s" id="%s" %s />
						<span class="title">%s</span>
						<span class="alignleft icon">
							<img src="%s/clear.gif" alt="" />
						</span>
					</label>
				</li>', $name, $this->get_field_name('iconset'), $this->get_field_id('iconset'), $checked, esc_html($name), wunderground_get_icon($name));
        }
        ?>
			</ul>
			<div class="clear"></div>
		</div>

		<div class="setting-wrapper layout">
			<h3 class="layout-title"><?php 
        esc_html_e('Widget Template', 'wunderground');
        ?>
</h3>
			<h4><?php 
        esc_html_e('Choose how you would like to display the forecast.', 'wunderground');
        ?>
</h4>
		<?php 
        echo $this->render_input_template($layout);
        ?>
		</div>

		<div class="setting-wrapper forecast">
			<h3><?php 
        esc_html_e('Show in Forecast', 'wunderground');
        ?>
</h3>
			<ul>
			<?php 
        $boxes = array('search' => array('label' => __('Search Form', 'wunderground'), 'description' => __('Allow searching weather forecasts.', 'wunderground')), 'daynames' => array('label' => __('Weekday Labels', 'wunderground'), 'description' => __('Show the names of the days of the week.', 'wunderground')), 'date' => array('label' => __('Date', 'wunderground'), 'description' => __('Display the date numerically ("09/14").', 'wunderground')), 'icon' => array('label' => __('Weather Icon', 'wunderground'), 'description' => __('Icon representing the forecast conditions.', 'wunderground')), 'pop' => array('label' => __('Chance of Rain', 'wunderground'), 'description' => __('Display the percent chance of rain.', 'wunderground')), 'highlow' => array('label' => __('High & Low Temp', 'wunderground'), 'description' => __('Show the high & low temperatures for forecast.', 'wunderground')), 'conditions' => array('label' => __('Condition Title', 'wunderground'), 'description' => __('Short summary of conditions ("Clear", "Partly Cloudy", etc.).', 'wunderground')), 'text' => array('label' => __('Forecast Text', 'wunderground'), 'description' => __('Display a description of the forecast, normally in sentence format.', 'wunderground')), 'alerts' => array('label' => __('Weather Alerts &amp; Warnings', 'wunderground'), 'description' => __('Display Severe Weather alerts and warnings.', 'wunderground')));
        foreach ($boxes as $value => $box) {
            $label = esc_html($box['label']);
            $description = esc_html($box['description']);
            printf('<li><label><input type="checkbox" value="%s" name="%s[%s]" %s /> <span class="title">%s</span>%s</label></li>', $value, $this->get_field_name('showdata'), $value, checked(in_array($value, (array) $showdata), true, false), $label, '<span class="howto">' . $description . '</span>');
        }
        ?>
			</ul>
		</div>

		<div class="setting-wrapper">
			<label for="<?php 
        echo $this->get_field_id('language');
        ?>
">
			<h3><?php 
        esc_html_e('Forecast Language', 'wunderground');
        ?>
</h3>
			<?php 
        $languages = wp_list_pluck(wunderground_get_languages(), 'label', 'key');
        echo wunderground_render_select($this->get_field_name('language'), $this->get_field_id('language'), $languages, $language);
        ?>
			</label>
		</div>

		<div class="setting-wrapper">
			<h3><?php 
        esc_html_e('Measurements', 'wunderground');
        ?>
</h3>
			<ul>
				<li>
					<label class="radio"><input type="radio" class="radio" id="<?php 
        echo $this->get_field_id('measurement_f');
        ?>
" name="<?php 
        echo $this->get_field_name('measurement');
        ?>
" value="english" <?php 
        checked('english', $measurement);
        ?>
 /> <span class="title"><?php 
        esc_html_e('Fahrenheit &amp; Inches', 'wunderground');
        ?>
</span></label>
				</li>
				<li>
					<label class="radio"><input type="radio" class="radio" id="<?php 
        echo $this->get_field_id('measurement_c');
        ?>
" name="<?php 
        echo $this->get_field_name('measurement');
        ?>
" value="metric" <?php 
        checked('metric', $measurement);
        ?>
 /> <span class="title"><?php 
        esc_html_e('Celsius &amp; Meters', 'wunderground');
        ?>
</span></label>
				</li>
			</ul>
		</div>

	</div>
<?php 
    }