PATH:
home
/
lab2454c
/
mact34.com
/
wp-content
/
plugins
/
bdthemes-element-pack
/
modules
/
weather
/
widgets
<?php namespace ElementPack\Modules\Weather\Widgets; use ElementPack\Base\Module_Base; use Elementor\Controls_Manager; use Elementor\Group_Control_Typography; if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class Weather extends Module_Base { public $weather_data = []; public $weather_api_current_url = 'http://api.weatherstack.com/current'; public function get_name() { return 'bdt-weather'; } public function get_title() { return BDTEP . esc_html__( 'Weather', 'bdthemes-element-pack' ); } public function get_icon() { return 'bdt-wi-weather'; } public function get_categories() { return [ 'element-pack' ]; } public function get_keywords() { return [ 'weather', 'cloudy', 'sunny', 'morning', 'evening' ]; } public function get_style_depends() { if ($this->ep_is_edit_mode()) { return ['ep-all-styles']; } else { return ['ep-weather']; } } public function get_custom_help_url() { return 'https://youtu.be/Vjyl4AAAufg'; } protected function _register_controls() { $this->start_controls_section( 'section_content_weather', [ 'label' => esc_html__( 'Weather', 'bdthemes-element-pack' ), ] ); $this->add_control( 'view', [ 'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'simple', 'options' => [ 'simple' => esc_html__( 'Simple', 'bdthemes-element-pack' ), 'today' => esc_html__( 'Today', 'bdthemes-element-pack' ), 'tiny' => esc_html__( 'Tiny', 'bdthemes-element-pack' ), ], 'prefix_class' => 'bdt-weather-layout-', 'render_type' => 'template', ] ); $this->add_control( 'weather_cache', [ 'label' => esc_html__( 'Cache Weather', 'bdthemes-element-pack' ) . BDTEP_NC, 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'description' => esc_html__( 'Note:- If are you using Free Plan of Weather Stack, please use this cache option to reduce your request of API Calls.', 'bdthemes-element-pack' ) ] ); $this->add_control( 'cache_refresh', array( 'label' => __( 'Reload Cache after ', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => '1', 'options' => array( '1' => __( '1 Hour', 'bdthemes-element-pack' ), '3' => __( '3 Hour', 'bdthemes-element-pack' ), '6' => __( '6 Hour', 'bdthemes-element-pack' ), '12' => __( '12 Hour', 'bdthemes-element-pack' ), '24' => __( '24 Hour', 'bdthemes-element-pack' ), ), 'condition' => [ 'weather_cache' => 'yes' ] ) ); $this->add_control( 'location', [ 'label' => esc_html__( 'Location', 'bdthemes-element-pack' ), 'description' => esc_html__( 'City and Region required, for example: Boston, MA', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true ], 'default' => 'Bogra, BD', ] ); $this->add_control( 'country', [ 'label' => esc_html__( 'Country (optional)', 'bdthemes-element-pack' ), 'description' => esc_html__( 'If you want to override country name, for example: USA', 'bdthemes-element-pack' ), 'type' => Controls_Manager::TEXT, 'dynamic' => [ 'active' => true ], ] ); $this->add_control( 'units', [ 'label' => esc_html__( 'Units', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SELECT, 'default' => 'metric', 'options' => [ 'metric' => esc_html__( 'Metric', 'bdthemes-element-pack' ), 'imperial' => esc_html__( 'Imperial', 'bdthemes-element-pack' ), ], ] ); $this->add_control( 'show_city', [ 'label' => esc_html__( 'Show City Name', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_country', [ 'label' => esc_html__( 'Show Country Name', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'condition' => [ 'view!' => ['tiny'] ] ] ); $this->add_control( 'show_temperature', [ 'label' => esc_html__( 'Show Temperature', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_weather_condition_name', [ 'label' => esc_html__( 'Show Weather Condition Name', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', ] ); $this->add_control( 'show_weather_icon', [ 'label' => esc_html__( 'Show Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ 'view!' => ['forecast'] ] ] ); $this->add_control( 'show_weather_desc', [ 'label' => esc_html__( 'Show Description', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ 'view' => ['tiny'] ] ] ); $this->add_control( 'show_today_name', [ 'label' => esc_html__( 'Show Today Name', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ 'view!' => ['tiny', 'simple'] ] ] ); $this->add_control( 'weather_details', [ 'label' => esc_html__( 'Weather Details', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'yes', 'condition' => [ 'view!' => ['tiny', 'simple'] ] ] ); $this->add_control( 'weather_value_round', [ 'label' => esc_html__( 'Round Number', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SWITCHER, 'default' => 'no', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_style_weather', [ 'label' => esc_html__( 'Weather', 'bdthemes-element-pack' ), 'tab' => Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'text_color', [ 'label' => esc_html__( 'Text Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-weather' => 'color: {{VALUE}};', ], ] ); $this->add_control( 'icon_color', [ 'label' => esc_html__( 'Icon Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-weather [class*="bdtw-"]' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'tiny_text_typography', 'selector' => '{{WRAPPER}} .bdt-weather', ] ); $this->end_controls_section(); $this->start_controls_section( 'section_location_style', [ 'label' => esc_html__( 'Location', 'bdthemes-element-pack' ) . BDTEP_NC, 'tab' => Controls_Manager::TAB_STYLE, 'conditions' => [ 'relation' => 'or', 'terms' => [ [ 'name' => 'show_city', 'value' => 'yes' ], [ 'name' => 'show_country', 'value' => 'yes' ] ] ] ] ); $this->add_control( 'location_city_name_heading', [ 'label' => esc_html__( 'City Name', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, 'condition' => [ 'show_city' => 'yes', ], ] ); $this->add_control( 'tiny_location_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-container .bdt-weather-city-name' => 'color: {{VALUE}};', ], 'condition' => [ 'show_city' => 'yes', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'location_city_name_typography', 'selector' => '{{WRAPPER}} .bdt-weather .bdt-weather-container .bdt-weather-city-name', 'condition' => [ 'show_city' => 'yes', ], ] ); $this->add_control( 'location_country_name_heading', [ 'label' => esc_html__( 'Country Name', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, 'separator' => 'before', 'condition' => [ 'show_country' => 'yes', 'view!' => 'tiny' ], ] ); $this->add_control( 'location_country_name_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-title .bdt-weather-country-name' => 'color: {{VALUE}};', ], 'condition' => [ 'show_country' => 'yes', 'view!' => 'tiny' ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'location_country_name_typography', 'selector' => '{{WRAPPER}} .bdt-weather .bdt-weather-title .bdt-weather-country-name', 'condition' => [ 'show_country' => 'yes', 'view!' => 'tiny' ], ] ); $this->add_responsive_control( 'location_country_name_spacing', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-title .bdt-weather-country-name' => 'margin-top: {{SIZE}}{{UNIT}};', ], 'condition' => [ 'show_country' => 'yes', 'view!' => 'tiny' ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_temperature_style', [ 'label' => esc_html__( 'Temperature', 'bdthemes-element-pack' ) . BDTEP_NC, 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_temperature' => 'yes' ] ] ); $this->add_control( 'tiny_temp_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-today-temp' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'temp_typography', 'selector' => '{{WRAPPER}} .bdt-weather .bdt-weather-today-temp', ] ); $this->add_responsive_control( 'temp_spacing', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-today-temp' => 'margin-top: {{SIZE}}{{UNIT}};', ], 'condition' => [ 'view!' => 'tiny' ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_today_name_style', [ 'label' => esc_html__( 'Today Name', 'bdthemes-element-pack' ) . BDTEP_NC, 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_today_name' => 'yes', 'view' => 'today' ] ] ); $this->add_control( 'today_name_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-today .bdt-weather-today-name' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'today_name_typography', 'selector' => '{{WRAPPER}} .bdt-weather .bdt-weather-today .bdt-weather-today-name', ] ); $this->add_responsive_control( 'today_name_spacing', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-today .bdt-weather-today-name' => 'margin-top: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_icon_condition_style', [ 'label' => esc_html__( 'Weather Icon / Condition Name', 'bdthemes-element-pack' ) . BDTEP_NC, 'tab' => Controls_Manager::TAB_STYLE, 'conditions' => [ 'relation' => 'or', 'terms' => [ [ 'name' => 'show_weather_condition_name', 'value' => 'yes' ], [ 'name' => 'show_weather_icon', 'value' => 'yes' ] ] ] ] ); $this->add_control( 'weather_icon_heading', [ 'label' => esc_html__( 'Weather Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, 'condition' => [ 'show_weather_icon' => 'yes', ], ] ); $this->add_control( 'tiny_icon_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-today-icon [class*="bdtw-"]' => 'color: {{VALUE}};', ], 'condition' => [ 'show_weather_icon' => 'yes', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'weather_icon_typography', 'selector' => '{{WRAPPER}} .bdt-weather .bdt-weather-today-icon [class*="bdtw-"]', 'condition' => [ 'show_weather_icon' => 'yes', ], ] ); $this->add_control( 'weather_condition_heading', [ 'label' => esc_html__( 'Weather Condition Name', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, 'separator' => 'before', 'condition' => [ 'show_weather_condition_name' => 'yes', ], ] ); $this->add_control( 'tiny_weather_desc', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-today-desc' => 'color: {{VALUE}};', ], 'condition' => [ 'show_weather_condition_name' => 'yes', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'weather_condition_typography', 'selector' => '{{WRAPPER}} .bdt-weather .bdt-weather-today-desc', 'condition' => [ 'show_weather_condition_name' => 'yes', ], ] ); $this->add_responsive_control( 'weather_condition_spacing', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-today-desc' => 'margin-top: {{SIZE}}{{UNIT}};', ], 'condition' => [ 'view!' => 'tiny', 'show_weather_condition_name' => 'yes', ], ] ); $this->end_controls_section(); $this->start_controls_section( 'section_weather_details_style', [ 'label' => esc_html__( 'Weather Details', 'bdthemes-element-pack' ) . BDTEP_NC, 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'weather_details' => 'yes', 'view' => 'today' ] ] ); $this->add_responsive_control( 'weather_details_spacing', [ 'label' => esc_html__( 'Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-details' => 'margin-top: {{SIZE}}{{UNIT}}; padding-top: {{SIZE}}{{UNIT}};', ], ] ); $this->add_control( 'weather_details_border_color', [ 'label' => esc_html__( 'Divider Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-details' => 'border-top-color: {{VALUE}};', ], ] ); $this->add_control( 'weather_details_text_heading', [ 'label' => esc_html__( 'Text', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] ); $this->add_control( 'weather_details_text_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-details [class*="bdt-weather-today-"]' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'weather_details_text_typography', 'selector' => '{{WRAPPER}} .bdt-weather .bdt-weather-details [class*="bdt-weather-today-"]', ] ); $this->add_control( 'weather_details_icon_heading', [ 'label' => esc_html__( 'Icon', 'bdthemes-element-pack' ), 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] ); $this->add_control( 'weather_details_icon_color', [ 'label' => esc_html__( 'Color', 'bdthemes-element-pack' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-details [class*="bdtw-"]' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'weather_details_icon_typography', 'selector' => '{{WRAPPER}} .bdt-weather .bdt-weather-details [class*="bdtw-"]', ] ); $this->add_responsive_control( 'weather_details_icon_spacing', [ 'label' => esc_html__( 'Icon Spacing', 'bdthemes-element-pack' ), 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 0, 'max' => 50, ], ], 'selectors' => [ '{{WRAPPER}} .bdt-weather .bdt-weather-details [class*="bdtw-"]' => 'margin-right: {{SIZE}}{{UNIT}};', ], ] ); $this->end_controls_section(); } public function get_transient_expire( $settings ) { $settings = $this->get_settings_for_display(); $expire_value = $settings['cache_refresh']; $expire_time = 1 * HOUR_IN_SECONDS; if ( '1' === $expire_value ) { $expire_time = 1 * HOUR_IN_SECONDS; } elseif ( '3' === $expire_value ) { $expire_time = 3 * HOUR_IN_SECONDS; } elseif ( '6' === $expire_value ) { $expire_time = 6 * HOUR_IN_SECONDS; } elseif ( '12' === $expire_value ) { $expire_time = 12 * HOUR_IN_SECONDS; } elseif ( '24' === $expire_value ) { $expire_time = 24 * HOUR_IN_SECONDS; } return $expire_time; } protected function render() { $settings = $this->get_settings_for_display(); $ep_api_settings = get_option( 'element_pack_api_settings' ); $api_key = !empty($ep_api_settings['weatherstack_api_key']) ? $ep_api_settings['weatherstack_api_key'] : ''; if ( ! $api_key ) { $message = esc_html__( 'Ops! I think you forget to set API key in Element Pack API settings.', 'bdthemes-element-pack' ); $this->weather_error_notice($message); return false; } $this->weather_data = $this->weather_data(); $this->add_render_attribute( 'weather', 'class', 'bdt-weather' ); ?> <div <?php echo $this->get_render_attribute_string('weather'); ?>> <div class="bdt-weather-container"> <?php if ( 'full' == $settings['view'] or 'simple' == $settings['view'] or 'today' == $settings['view'] ) : ?> <?php $this->render_weather_today(); ?> <?php elseif ( 'tiny' == $settings['view'] ) : ?> <?php $this->render_weather_tiny(); ?> <?php endif; ?> </div> </div> <?php } public function render_weather_today() { $settings = $this->get_settings_for_display(); $data = $this->weather_data; $speed_unit = ( 'metric' === $settings['units'] ) ? esc_html_x( 'km/h', 'Weather String', 'bdthemes-element-pack' ) : esc_html_x( 'm/h', 'Weather String', 'bdthemes-element-pack' ); $speed = ( 'metric' === $settings['units'] ) ? $data['today']['wind_speed']['kph'] : $data['today']['wind_speed']['mph']; ?> <div class="bdt-weather-today"> <?php if ( 'yes' == $settings['show_city'] or 'yes' == $settings['show_country'] or 'yes' == $settings['show_temperature'] or 'yes' == $settings['show_today_name'] ) : ?> <div class="bdt-grid bdt-grid-collapse bdt-flex bdt-flex-middle"> <div class="bdt-width-3-5"> <?php $this->render_weather_title(); ?> <?php if ( 'yes' == $settings['show_temperature'] ) : ?> <div class="bdt-weather-today-temp"><?php echo $this->weather_temperature( $data['today']['temp'] ); ?></div> <?php endif; ?> <?php if ( 'yes' == $settings['show_today_name'] ) : ?> <div class="bdt-weather-today-name"><?php echo esc_html($data['today']['week_day']); ?></div> <?php endif; ?> </div> <?php if ( 'yes' == $settings['show_weather_icon'] ) : ?> <div class="bdt-width-2-5 bdt-text-center"> <div class="bdt-width-1-1"> <div class="bdt-weather-today-icon"> <?php if(!empty($data)){ echo $this->weather_icon( $data['today']['code'] ); } ?> </div> <?php if ( 'yes' == $settings['show_weather_condition_name'] ) : ?> <div class="bdt-weather-today-desc"> <?php if(!empty($data)){ echo $this->weather_desc( $data['today']['code'] ); } ?> </div> <?php endif; ?> </div> </div> <?php endif; ?> </div> <?php else : ?> <div class="bdt-text-center"> <div class="bdt-weather-today-icon"><?php if(!empty($data)){ echo $this->weather_icon( $data['today']['code'] ); } ?> </div> <?php if ( 'yes' == $settings['show_weather_condition_name'] ) : ?> <div class="bdt-weather-today-desc"> <?php if(!empty($data)){ echo $this->weather_desc( $data['today']['code'] ); } ?> </div> <?php endif; ?> </div> <?php endif; ?> </div> <?php if ( 'yes' === $settings['weather_details'] ) : ?> <div class="bdt-weather-details bdt-grid bdt-grid-collapse"> <div class="bdt-width-1-3"> <div class="bdt-weather-today-humidity"> <span class="bdtw-humidity"></span> <?php echo esc_html($data['today']['humidity']); ?> </div> </div> <div class="bdt-width-1-3"> <div class="bdt-weather-today-pressure"> <span class="bdtw-pressure"></span> <?php echo $this->get_weather_pressure( $data['today']['pressure'] ); ?> </div> </div> <div class="bdt-width-1-3"> <div class="bdt-weather-today-wind"> <span class="bdtw-<?php echo element_pack_wind_code( $data['today']['wind_deg'] ); ?>"></span> <?php echo esc_html($speed) .' '. esc_html($speed_unit); ?> </div> </div> </div> <?php endif; } public function render_weather_tiny() { $settings = $this->get_settings_for_display(); $data = $this->weather_data; ?> <?php if ( 'yes' == $settings['show_city'] ) : ?> <span class="bdt-weather-city-name"><?php echo $this->weather_data['location']['city']; ?></span> <?php endif; ?> <?php if ( 'yes' == $settings['show_temperature'] ) : ?> <span class="bdt-weather-today-temp"><?php echo $this->weather_temperature( $data['today']['temp'] ); ?></span> <?php endif; ?> <?php if ( 'yes' == $settings['show_weather_icon'] ) : ?> <span class="bdt-weather-today-icon"><?php echo $this->weather_icon( $data['today']['code'] ); ?></span> <?php endif; ?> <?php if ( 'yes' == $settings['show_weather_desc'] ) : ?> <span class="bdt-weather-today-desc"><?php echo $this->weather_desc( $data['today']['code'] ); ?></span> <?php endif; ?> <?php } public function render_weather_title() { $settings = $this->get_settings_for_display(); $data = $this->weather_data; ?> <?php if ( 'yes' == $settings['show_city'] or 'yes' == $settings['show_country'] ) : ?> <div class="bdt-weather-title"> <?php if ( 'yes' == $settings['show_city'] ) : ?> <span class="bdt-weather-city-name"><?php echo $this->weather_data['location']['city']; ?></span> <?php endif; ?> <?php if ( 'yes' == $settings['show_country'] ) : ?> <span class="bdt-weather-country-name"> <?php if ( $settings['country'] ) : ?> <?php echo esc_html($settings['country']); ?> <?php else : ?> <?php echo $this->weather_data['location']['country']; ?> <?php endif; ?> </span> <?php endif; ?> </div> <?php endif; ?> <?php } public function weather_data() { $ep_api_settings = get_option( 'element_pack_api_settings' ); $api_key = !empty($ep_api_settings['weatherstack_api_key']) ? $ep_api_settings['weatherstack_api_key'] : ''; // return error message when api key not found if ( ! $api_key ) { $message = esc_html__( 'Ops! I think you forget to set API key in Element Pack API settings.', 'bdthemes-element-pack' ); $this->weather_error_notice($message); return false; } $settings = $this->get_settings_for_display(); $location = $settings['location']; if ( empty( $location ) ) { return false; } $transient_key = sprintf( 'bdt-weather-data-%s', md5( $location ) ); if($settings['weather_cache'] == 'yes'){ $data = get_transient( $transient_key ); }else{ $data = ''; } if ( ! $data ) { // Prepare request data $location = esc_attr( $location ); $api_key = esc_attr( $api_key ); $request_args = array( 'access_key' => $api_key, 'query' => urlencode( $location ), 'forecast_days' => 6, 'hourly' => 1, 'units' => '' ); $request_url = add_query_arg( $request_args, $this->weather_api_current_url ); $weather = $this->weather_remote_request( $request_url ); if ( ! $weather ) { return false; } if ( isset( $weather['error'] )) { if ( isset( $weather['error']['info'] ) ) { $message = $weather['error']['info']; } else { $message = esc_html__( 'Weather data of this location not found.', 'bdthemes-element-pack' ); } echo $this->weather_error_notice( $message ); return false; } $data = $this->transient_weather( $weather ); if ( empty( $data ) ) { return false; } $expireTime = $this->get_transient_expire($settings); if($settings['weather_cache'] == 'yes'){ set_transient( $transient_key, $data, apply_filters( 'element-pack/weather/cached-time', $expireTime ) ); } return $data; } return $data; } public function weather_remote_request( $url ) { $response = wp_remote_get( $url, array( 'timeout' => 30 ) ); if ( ! $response || is_wp_error( $response ) ) { return false; } $remote_data = wp_remote_retrieve_body( $response ); if ( ! $remote_data || is_wp_error( $remote_data ) ) { return false; } $remote_data = json_decode( $remote_data, true ); if ( empty( $remote_data ) ) { return false; } return $remote_data; } public function transient_weather( $weather = [] ) { $data = array( 'location' => array( 'city' => $weather['location']['name'], 'country' => $weather['location']['country'], ), 'today' => array( 'code' => $weather['current']['weather_code'], 'temp' => $weather['current']['temperature'], 'wind_speed' => array( 'mph' => $weather['current']['wind_speed'], 'kph' => $weather['current']['wind_speed'], ), 'wind_deg' => $weather['current']['wind_degree'], 'wind_dir' => $weather['current']['wind_dir'], 'humidity' => $weather['current']['humidity'] . '%', 'pressure' => $weather['current']['pressure'], 'week_day' => date_i18n( 'l' ), ), 'forecast' => [], ); return $data; } public function readable_week( $format = '', $date = '' ) { $date = date_create_from_format( $format, $date ); return date_i18n( 'l', date_timestamp_get( $date ) ); } public function weather_desc( $code, $is_day = true ) { $desc = element_pack_weather_code( $code, 'desc', $is_day ); if ( empty( $desc ) ) { return []; } return $desc; } public function weather_temperature( $temp ) { $units = $this->get_settings_for_display( 'units' ); $temp_unit = ( 'metric' === $units ) ? '°C' : '°F'; if ( is_array( $temp ) ) { $temp = ( 'metric' === $units ) ? $temp['c'] : $temp['f']; } $temp = ( 'metric' === $units ) ? $temp : (( $temp * 1.8 ) + 32 ); //°F = °C × 1,8 + 32 $temp_format = apply_filters( 'element-pack/weather/temperature-format', '%1$s%2$s' ); $settings = $this->get_settings_for_display(); ($settings['weather_value_round']) == 'yes' ? $temp = round($temp) : $temp; return sprintf( $temp_format, $temp, $temp_unit ); } public function get_weather_pressure( $pressure ) { $units = $this->get_settings_for_display( 'units' ); if ( is_array( $pressure ) ) { $pressure = ( 'metric' === $units ) ? $pressure['mb'] : $pressure['in']; } $format = apply_filters( 'element-pack/weather/pressure-format', '%s' ); return sprintf( $format, $pressure ); } public function weather_icon( $icon, $is_day = true ) { $icon = element_pack_weather_code( $icon, 'icon' ); $time = ($is_day) ? 'd' : 'n'; $icon_class = []; $icon_class[] = sprintf( 'bdtw-%s', esc_attr( $icon ) ); return sprintf( '<span class="%1$s%2$s"></span>', implode(' ', $icon_class), $time ); } public function weather_error_notice($message) { ?> <div class="bdt-alert-warning" data-bdt-alert> <a class="bdt-alert-close" data-bdt-close></a> <p><?php echo esc_html($message); ?></p> </div> <?php } }
[+]
..
[-] weather.php
[edit]