name pairs. */ function thirty_year_weather_get_cities() { // Popular cities - this list can be extended return array( 'prague' => __( 'Prague, Czech Republic', '30yearweather-widget' ), 'paris' => __( 'Paris, France', '30yearweather-widget' ), 'london' => __( 'London, United Kingdom', '30yearweather-widget' ), 'new-york' => __( 'New York, USA', '30yearweather-widget' ), 'tokyo' => __( 'Tokyo, Japan', '30yearweather-widget' ), 'barcelona' => __( 'Barcelona, Spain', '30yearweather-widget' ), 'rome' => __( 'Rome, Italy', '30yearweather-widget' ), 'amsterdam' => __( 'Amsterdam, Netherlands', '30yearweather-widget' ), 'dubai' => __( 'Dubai, UAE', '30yearweather-widget' ), 'bali' => __( 'Bali, Indonesia', '30yearweather-widget' ), 'sydney' => __( 'Sydney, Australia', '30yearweather-widget' ), 'bangkok' => __( 'Bangkok, Thailand', '30yearweather-widget' ), 'lisbon' => __( 'Lisbon, Portugal', '30yearweather-widget' ), 'berlin' => __( 'Berlin, Germany', '30yearweather-widget' ), 'vienna' => __( 'Vienna, Austria', '30yearweather-widget' ), 'budapest' => __( 'Budapest, Hungary', '30yearweather-widget' ), 'athens' => __( 'Athens, Greece', '30yearweather-widget' ), 'istanbul' => __( 'Istanbul, Turkey', '30yearweather-widget' ), 'marrakech' => __( 'Marrakech, Morocco', '30yearweather-widget' ), 'cape-town' => __( 'Cape Town, South Africa', '30yearweather-widget' ), 'singapore' => __( 'Singapore', '30yearweather-widget' ), 'hong-kong' => __( 'Hong Kong, China', '30yearweather-widget' ), 'seoul' => __( 'Seoul, South Korea', '30yearweather-widget' ), 'kyoto' => __( 'Kyoto, Japan', '30yearweather-widget' ), 'dublin' => __( 'Dublin, Ireland', '30yearweather-widget' ), 'stockholm' => __( 'Stockholm, Sweden', '30yearweather-widget' ), 'copenhagen' => __( 'Copenhagen, Denmark', '30yearweather-widget' ), 'florence' => __( 'Florence, Italy', '30yearweather-widget' ), 'venice' => __( 'Venice, Italy', '30yearweather-widget' ), 'santorini' => __( 'Santorini, Greece', '30yearweather-widget' ), 'dubrovnik' => __( 'Dubrovnik, Croatia', '30yearweather-widget' ), 'cancun' => __( 'Cancun, Mexico', '30yearweather-widget' ), 'miami' => __( 'Miami, USA', '30yearweather-widget' ), 'los-angeles' => __( 'Los Angeles, USA', '30yearweather-widget' ), 'san-francisco' => __( 'San Francisco, USA', '30yearweather-widget' ), 'vancouver' => __( 'Vancouver, Canada', '30yearweather-widget' ), 'toronto' => __( 'Toronto, Canada', '30yearweather-widget' ), 'rio-de-janeiro' => __( 'Rio de Janeiro, Brazil', '30yearweather-widget' ), 'buenos-aires' => __( 'Buenos Aires, Argentina', '30yearweather-widget' ), ); } /** * Sanitize city slug. * * @since 1.0.0 * @param string $city The city slug to sanitize. * @return string Sanitized city slug. */ function thirty_year_weather_sanitize_city( $city ) { $city = sanitize_text_field( $city ); $city = preg_replace( '/[^a-z0-9-]/', '', strtolower( $city ) ); return $city; } /** * Build the embed URL with parameters. * * @since 1.0.0 * @param array $atts Widget attributes. * @return string The embed URL. */ function thirty_year_weather_build_embed_url( $atts ) { $city = thirty_year_weather_sanitize_city( $atts['city'] ); if ( empty( $city ) ) { $city = 'prague'; } $url = THIRTY_YEAR_WEATHER_EMBED_BASE . $city; $params = array(); // Theme parameter. if ( ! empty( $atts['theme'] ) && in_array( $atts['theme'], array( 'dark', 'light' ), true ) ) { $params['theme'] = $atts['theme']; } // Layout parameter. if ( ! empty( $atts['layout'] ) && in_array( $atts['layout'], array( 'classic', 'visual' ), true ) ) { $params['layout'] = $atts['layout']; } // Content sections parameter. if ( ! empty( $atts['content'] ) ) { $valid_sections = array( 'bestMonths', 'climate', 'temperatures', 'rain', 'humidity', 'tourism' ); $sections = array_map( 'trim', explode( ',', $atts['content'] ) ); $sections = array_filter( $sections, function( $s ) use ( $valid_sections ) { return in_array( $s, $valid_sections, true ); }); if ( ! empty( $sections ) ) { $params['content'] = implode( ',', $sections ); } } if ( ! empty( $params ) ) { $url .= '?' . http_build_query( $params ); } return esc_url( $url ); } /** * Register the shortcode. * * Usage: [30yearweather city="prague" theme="dark" layout="classic" width="400" height="450"] * * @since 1.0.0 * @param array $atts Shortcode attributes. * @return string The widget HTML. */ function thirty_year_weather_shortcode( $atts ) { $atts = shortcode_atts( array( 'city' => 'prague', 'theme' => 'dark', 'layout' => 'classic', 'content' => 'bestMonths,climate,temperatures', 'width' => '400', 'height' => '450', ), $atts, '30yearweather' ); $embed_url = thirty_year_weather_build_embed_url( $atts ); // Sanitize dimensions. $width = absint( $atts['width'] ); $height = absint( $atts['height'] ); if ( $width < 200 ) { $width = 200; } if ( $width > 800 ) { $width = 800; } if ( $height < 150 ) { $height = 150; } if ( $height > 800 ) { $height = 800; } $border_color = ( 'dark' === $atts['theme'] ) ? '#374151' : '#e5e7eb'; $output = sprintf( '
', esc_url( $embed_url ), esc_attr( $width ), esc_attr( $height ), esc_attr( $border_color ), /* translators: %s: city name */ esc_attr( sprintf( __( 'Weather widget for %s', '30yearweather-widget' ), ucfirst( str_replace( '-', ' ', $atts['city'] ) ) ) ) ); return $output; } add_shortcode( '30yearweather', 'thirty_year_weather_shortcode' ); /** * Register the Gutenberg block. * * @since 1.0.0 */ function thirty_year_weather_register_block() { // Skip if Gutenberg is not available. if ( ! function_exists( 'register_block_type' ) ) { return; } // Register block script. wp_register_script( 'thirty-year-weather-block-editor', THIRTY_YEAR_WEATHER_PLUGIN_URL . 'blocks/weather-widget/index.js', array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n' ), THIRTY_YEAR_WEATHER_VERSION, true ); // Localize script with cities data. wp_localize_script( 'thirty-year-weather-block-editor', 'thirtyYearWeatherData', array( 'cities' => thirty_year_weather_get_cities(), 'embedBase' => THIRTY_YEAR_WEATHER_EMBED_BASE, ) ); // Register block styles. wp_register_style( 'thirty-year-weather-block-editor', THIRTY_YEAR_WEATHER_PLUGIN_URL . 'blocks/weather-widget/editor.css', array( 'wp-edit-blocks' ), THIRTY_YEAR_WEATHER_VERSION ); // Register the block. register_block_type( 'thirty-year-weather/widget', array( 'editor_script' => 'thirty-year-weather-block-editor', 'editor_style' => 'thirty-year-weather-block-editor', 'render_callback' => 'thirty_year_weather_block_render', 'attributes' => array( 'city' => array( 'type' => 'string', 'default' => 'prague', ), 'theme' => array( 'type' => 'string', 'default' => 'dark', ), 'layout' => array( 'type' => 'string', 'default' => 'classic', ), 'content' => array( 'type' => 'string', 'default' => 'bestMonths,climate,temperatures', ), 'width' => array( 'type' => 'number', 'default' => 400, ), 'height' => array( 'type' => 'number', 'default' => 450, ), ), ) ); } add_action( 'init', 'thirty_year_weather_register_block' ); /** * Render callback for the Gutenberg block. * * @since 1.0.0 * @param array $attributes Block attributes. * @return string The block HTML. */ function thirty_year_weather_block_render( $attributes ) { return thirty_year_weather_shortcode( $attributes ); } /** * Add plugin action links. * * @since 1.0.0 * @param array $links Existing links. * @return array Modified links. */ function thirty_year_weather_plugin_action_links( $links ) { $settings_link = sprintf( '%s', esc_url( 'https://30yearweather.com/widgets' ), esc_html__( 'Get More Cities', '30yearweather-widget' ) ); array_unshift( $links, $settings_link ); return $links; } add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'thirty_year_weather_plugin_action_links' ); /** * Add a widget to the WordPress dashboard. * * @since 1.0.0 */ function thirty_year_weather_add_dashboard_widget() { wp_add_dashboard_widget( 'thirty_year_weather_dashboard', __( '30YearWeather Quick Add', '30yearweather-widget' ), 'thirty_year_weather_dashboard_widget_content' ); } add_action( 'wp_dashboard_setup', 'thirty_year_weather_add_dashboard_widget' ); /** * Dashboard widget content. * * @since 1.0.0 */ function thirty_year_weather_dashboard_widget_content() { ?>
[30yearweather city="prague" theme="dark"]