/** * Output the styles and scripts necessary * * @param boolean $force Will be empty string when passed by `wp_enqueue_scripts`, but will be `true` when passed by `wunderground_print_scripts` * @return [type] [description] */ function print_scripts($force = false) { global $post, $pagenow; // Is the widget active? $widget = is_active_widget(false, false, 'wunderground_forecast_widget') ? true : false; // Check if the content has the shortcode $content = false; if (!empty($post->post_content) && function_exists('has_shortcode') && has_shortcode($post->post_content, 'wunderground')) { $content = true; } $admin = is_admin() && in_array($pagenow, array('widgets.php', 'customize.php')); if ($admin || $widget || $content || $force === true) { // Only show the front-end display on the front-end if (!$admin) { wp_enqueue_style('wunderground', plugins_url('assets/css/wunderground.css', Wunderground_Plugin::$file), array('dashicons'), Wunderground_Plugin::version); } else { // And the backend on the backend wp_enqueue_style('wunderground-admin', plugins_url('assets/css/admin.css', Wunderground_Plugin::$file)); } // If using SCRIPT_DEBUG, don't use the minified version. $min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; wp_enqueue_script('wunderground-widget', plugins_url('assets/js/widget' . $min . '.js', Wunderground_Plugin::$file), array('jquery-ui-autocomplete'), Wunderground_Plugin::version); wp_localize_script('wunderground-widget', 'WuWidget', array('apiKey' => esc_attr(Wunderground_Plugin::$api_key), '_wpnonce' => wp_create_nonce('wunderground-aq'), 'ajaxurl' => admin_url('admin-ajax.php'), 'is_admin' => is_admin(), 'subdomain' => wunderground_get_subdomain())); } }
/** * @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; }