PluginProbe
Location Weather – WordPress Weather Forecast, Air Quality & Weather Widget / trunk
Location Weather – WordPress Weather Forecast, Air Quality & Weather Widget vtrunk
3.0.7 3.0.6 3.0.5 3.0.4 trunk 1.0 1.1 1.1.1 1.1.2 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.10 1.3.11 1.3.2 All 62 releases
location-weather / main.php

main.php in Location Weather – WordPress Weather Forecast, Air Quality & Weather Widget trunk, at main.php

469 lines 16.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Location Weather
4 *
5 * @package Location_Weather
6 * @author ShapedPlugin
7 * @copyright 2023 ShapedPlugin
8 * @license GPL-2.0-or-later
9 *
10 * @wordpress-plugin
11 * Plugin Name: Location Weather
12 * Description: Location Weather is the WordPress weather forecast plugin that allows you to create and display unlimited weather forecasts anywhere on your WordPress website. The plugin uses WordPress Custom Post Types and the Open Weather Map API and WeatherAPI Key.
13 * Plugin URI: https://locationweather.io/?ref=1
14 * Author: ShapedPlugin LLC
15 * Author URI: https://shapedplugin.com/
16 * Version: 3.0.7
17 * Requires at least: 5.9.0
18 * Requires PHP: 7.4
19 * License: GPL v2 or later
20 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
21 * Text Domain: location-weather
22 * Domain Path: /languages
23 */
24
25 /**
26 * Exit if entering directly.
27 */
28 if ( ! defined( 'ABSPATH' ) ) {
29 exit;
30 }
31 define( 'LOCATION_WEATHER_FILE', __FILE__ );
32 define( 'LOCATION_WEATHER_URL', plugins_url( '', LOCATION_WEATHER_FILE ) );
33 define( 'LOCATION_WEATHER_ASSETS', LOCATION_WEATHER_URL . '/assets' );
34 define( 'LOCATION_WEATHER_VERSION', '3.0.7' );
35
36 require_once ABSPATH . 'wp-admin/includes/plugin.php';
37 if ( ! ( is_plugin_active( 'location-weather-pro/main.php' ) || is_plugin_active_for_network( 'location-weather-pro/main.php' ) ) ) {
38 require_once __DIR__ . '/vendor/autoload.php';
39 }
40
41 /**
42 * The Main Class the of the plugin.
43 */
44 final class Location_Weather {
45
46 /**
47 * Suffix for minified file.
48 *
49 * @var string
50 */
51 public $suffix = '';
52
53 /**
54 * Plugin version.
55 *
56 * @var string
57 */
58 public $version = '3.0.7';
59
60 /**
61 * The unique slug of this plugin.
62 *
63 * @since 1.2.0
64 * @access protected
65 * @var string $plugin_name The string used to uniquely identify this plugin.
66 */
67 public $plugin_slug = 'location-weather';
68
69 /**
70 * Class constructor.
71 */
72 public function __construct() {
73 $this->suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? '' : '.min';
74 $this->define_constants();
75
76 include_once LOCATION_WEATHER_TEMPLATE_PATH . '/Admin/framework/classes/SPLW.php';
77 $framework = new SPLW();
78
79 add_action( 'after_setup_theme', array( $framework, 'splw_framework_config' ) );
80 add_filter( 'plugin_action_links', array( $this, 'add_plugin_action_links_location' ), 10, 2 );
81 add_action( 'plugins_loaded', array( $this, 'init_plugin' ) );
82 add_action( 'widgets_init', array( $this, 'splw_widget' ) );
83 add_action( 'activated_plugin', array( $this, 'redirect_after_activation' ), 10, 2 );
84 $import_export = new ShapedPlugin\Weather\Admin\Location_Weather_Import_Export();
85 add_action( 'wp_ajax_splw_export_shortcodes', array( $import_export, 'export_shortcodes' ) );
86 add_action( 'wp_ajax_splw_import_shortcodes', array( $import_export, 'import_shortcodes' ) );
87 add_action( 'wp_ajax_splw_ajax_location_weather', array( $this, 'splw_ajax_location_weather' ) );
88 add_action( 'wp_ajax_nopriv_splw_ajax_location_weather', array( $this, 'splw_ajax_location_weather' ) );
89 add_action( 'wp_loaded', array( $this, 'register_all_scripts' ) );
90 add_action( 'save_post', array( $this, 'delete_page_lw_option_on_save' ), 10, 2 );
91 add_action( 'admin_notices', array( $this, 'display_missing_api_key_notice' ) );
92 add_action( 'network_admin_notices', array( $this, 'display_missing_api_key_notice' ) );
93 add_filter( 'sp_open_weather_api_cache_time', array( $this, 'get_location_weather_cache_expire_time' ), 10 );
94 add_action( 'location_weather_weekly_scheduled_events', array( $this, 'init_lw_sdr' ), );
95
96 add_filter( 'plugin_row_meta', array( $this, 'lw_plugin_row_meta' ), 10, 2 );
97
98 /**
99 * Polylang plugin support for multi language support.
100 */
101 if ( class_exists( 'Polylang' ) ) {
102 /**
103 *
104 * Multi Language Support
105 *
106 * @since 2.0
107 */
108 add_filter( 'pll_get_post_types', array( $this, 'sp_splw_polylang' ), 10, 2 );
109 }
110 }
111
112 /**
113 * Initialize the lw_data_storing system.
114 *
115 * @return void
116 */
117 public function init_lw_sdr() {
118 $admin_dashboard = new ShapedPlugin\Weather\Admin\AdminDashboard\Splw_Blocks_Page_Wrapper();
119 $user_consent = get_option( 'splw_allow_anonymous_data', false );
120 if ( $user_consent ) {
121 $website_type = get_option( 'splw_site_type', '' );
122 $admin_dashboard->splw_send_data_to_lw_sdr( $website_type );
123 }
124 }
125
126 /**
127 * Show row meta on the plugin screen.
128 *
129 * @param mixed $links Plugin Row Meta.
130 * @param mixed $file Plugin Base file.
131 *
132 * @return array
133 */
134 public function lw_plugin_row_meta( $links, $file ) {
135 /**
136 * The Location Weather documentation URL.
137 *
138 * @since 2.7.0
139 */
140 $docs_url = apply_filters( 'lw_docs_url', 'https://locationweather.io/docs/' );
141
142 /**
143 * The Location Weather live demo URL.
144 *
145 * @since 2.2.0
146 */
147 $live_demo_url = apply_filters( 'lw_live_demo_url', 'https://locationweather.io/#weather-showcase' );
148
149 /**
150 * The community location weather support URL.
151 *
152 * @since 2.2.0
153 */
154 $community_support_url = apply_filters( 'lw_support_url', 'https://shapedplugin.com/create-new-ticket/' );
155
156 if ( plugin_basename( __DIR__ . '/main.php' ) === $file ) {
157 $row_meta = array(
158 'live_demo' => '<a href="' . esc_url( $live_demo_url ) . '" aria-label="' . esc_attr__( 'View location weather live demo', 'location-weather' ) . '" target=_blank">' . esc_html__( 'Live Demo', 'location-weather' ) . '</a>',
159 'docs' => '<a href="' . esc_url( $docs_url ) . '" aria-label="' . esc_attr__( 'View location weather documentation', 'location-weather' ) . '" target=_blank">' . esc_html__( 'Docs', 'location-weather' ) . '</a>',
160 'support' => '<a href="' . esc_url( $community_support_url ) . '" aria-label="' . esc_attr__( 'Visit community forums', 'location-weather' ) . '" target=_blank">' . esc_html__( 'Support', 'location-weather' ) . '</a>',
161 );
162
163 return array_merge( $links, $row_meta );
164 }
165 return $links;
166 }
167
168 /**
169 * Polylang Multi Language Support.
170 *
171 * @param array $post_types Post types.
172 * @param bool $is_settings true/false.
173 * @return array
174 */
175 public function sp_splw_polylang( $post_types, $is_settings ) {
176 if ( $is_settings ) {
177 // Hides 'location_weather' from the list of custom post types in Polylang settings.
178 unset( $post_types['location_weather'] );
179 } else {
180 // Enables language and translation management for 'location_weather'.
181 $post_types['location_weather'] = 'location_weather';
182 }
183 return $post_types;
184 }
185
186 /**
187 * Get the cache expiration time for location weather data.
188 *
189 * This function retrieves the cache expiration time based on the plugin settings.
190 *
191 * @param int $expire_time The default cache expiration time.
192 * @return int The modified cache expiration time.
193 */
194 public function get_location_weather_cache_expire_time( $expire_time ) {
195 $setting_options = get_option( 'location_weather_settings', true );
196 $enable_cache = isset( $setting_options['splw_enable_cache'] ) ? $setting_options['splw_enable_cache'] : false;
197 $cache_time = isset( $setting_options['splw_cache_time'] ) ? (int) $setting_options['splw_cache_time']['all'] : 10;
198 $expire_time = $enable_cache && $cache_time > 10 ? ( $cache_time * 60 ) : 600;
199 // Return the modified cache expiration time.
200 return $expire_time;
201 }
202 /**
203 * Location Weather ajax action.
204 */
205 public function splw_ajax_location_weather() {
206
207 $nonce = isset( $_POST['splw_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['splw_nonce'] ) ) : '';
208 if ( ! wp_verify_nonce( $nonce, 'splw_nonce' ) ) {
209 wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid nonce verification.', 'location-weather' ) ), 401 );
210 }
211 $arguments['id'] = isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : null;
212
213 $shortcode = new ShapedPlugin\Weather\Frontend\Shortcode();
214 echo $shortcode->render_shortcode( $arguments ); // phpcs:ignore
215 wp_die();
216 }
217
218 /**
219 * Initializes a singleton instance.
220 *
221 * @return \Location_Weather
222 */
223 public static function init() {
224 static $instance = false;
225
226 if ( ! $instance ) {
227 $instance = new self();
228 }
229
230 return $instance;
231 }
232 /**
233 * Location Weather lite Widget.
234 */
235 public function splw_widget() {
236 register_widget( new ShapedPlugin\Weather\Admin\LW_Widget() );
237 register_widget( new ShapedPlugin\Weather\Admin\sp_location_weather_widget_content() );
238 }
239 /**
240 * Define plugin constants.
241 *
242 * @return void
243 */
244 public function define_constants() {
245 define( 'LOCATION_WEATHER_SLUG', $this->plugin_slug );
246 define( 'LOCATION_WEATHER_PATH', __DIR__ );
247 define( 'LOCATION_WEATHER_TEMPLATE_PATH', plugin_dir_path( __FILE__ ) . 'includes/' );
248 define( 'LOCATION_WEATHER_STORE_URL', 'https://shapedplugin.com' );
249 define( 'LOCATION_WEATHER_SDR_KEY', '907eeaa164709de7eccdf90dd60d7734c8468217' );
250 }
251
252 /**
253 * Add plugin action menu
254 *
255 * @since 1.2.0
256 *
257 * @param array $links Link to the generator.
258 * @param string $file Generator linking button.
259 *
260 * @return array
261 */
262 public function add_plugin_action_links_location( $links, $file ) {
263
264 if ( plugin_basename( __FILE__ ) === $file ) {
265 $new_links = array(
266 sprintf( '<a href="%s">%s</a>', admin_url( 'edit.php?post_type=location_weather&page=splw_admin_dashboard#lw_settings' ), __( 'Settings', 'location-weather' ) ),
267 );
268 $links['go_pro'] = '<a target="_blank" href="https://locationweather.io/pricing/?ref=1" style="color: #35b747; font-weight: 700;">' . __( 'Go Pro!', 'location-weather' ) . '</a>';
269 return array_merge( $new_links, $links );
270 }
271
272 return $links;
273 }
274
275 /**
276 * Redirect after activation.
277 *
278 * @since 1.2.0
279 *
280 * @param string $file Path to the plugin file, relative to the plugin.
281 *
282 * @return void
283 */
284 public function redirect_after_activation( $file ) {
285 $is_visited_setup_wizard = get_option( 'splw_setup_wizard_visited', false );
286 if ( plugin_basename( __FILE__ ) === $file && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) ) {
287 if ( $is_visited_setup_wizard ) {
288 exit( esc_url( wp_safe_redirect( admin_url( 'edit.php?post_type=location_weather&page=splw_admin_dashboard' ) ) ) );
289 } else {
290 exit( esc_url( wp_safe_redirect( admin_url( 'edit.php?post_type=location_weather&page=splw_admin_dashboard#setupwizard' ) ) ) );
291 }
292 }
293 }
294
295 /**
296 * Initialize the plugin.
297 *
298 * @return void
299 */
300 public function init_plugin() {
301 if ( is_admin() ) {
302 new ShapedPlugin\Weather\Admin();
303 } else {
304 new ShapedPlugin\Weather\Frontend();
305 }
306
307 // Elementor shortcode block.
308 require_once ABSPATH . 'wp-admin/includes/plugin.php';
309 if ( ( is_plugin_active( 'elementor/elementor.php' ) || is_plugin_active_for_network( 'elementor/elementor.php' ) ) ) {
310 new ShapedPlugin\Weather\Admin\Location_Weather_Shortcode_Block();
311 }
312
313 // Page builder integrations.
314 ShapedPlugin\Weather\Admin\PageBuilders\Manager::init();
315
316 // Gutenberg block.
317 if ( version_compare( $GLOBALS['wp_version'], '5.3', '>=' ) ) {
318 // Initialize Shortcode block block.
319 new ShapedPlugin\Weather\Admin\Gutenberg_Block\Gutenberg_Block_Init();
320 // Initialize Location weather Blocks.
321 $weather_blocks = new ShapedPlugin\Weather\Blocks\Blocks();
322 $weather_blocks::instance();
323 // Initialize saved templates.
324 new ShapedPlugin\Weather\Admin\LW_Saved_Templates();
325 }
326 }
327
328 /**
329 * Register the all scripts of the plugin.
330 *
331 * @since 2.0
332 */
333 public function register_all_scripts() {
334 /**
335 * Register the stylesheets for the public and admin facing side of the plugin.
336 *
337 * @since 3.0.0
338 * @return void
339 */
340 $setting_options = get_option( 'location_weather_settings', true );
341 $skip_cache_for_weather = isset( $setting_options['splw_skipping_cache'] ) ? $setting_options['splw_skipping_cache'] : false;
342 wp_register_style( 'splw-fontello', LOCATION_WEATHER_ASSETS . '/css/fontello' . $this->suffix . '.css', array(), LOCATION_WEATHER_VERSION, 'all' );
343 wp_register_style( 'splw-styles', LOCATION_WEATHER_ASSETS . '/css/splw-style' . $this->suffix . '.css', array(), LOCATION_WEATHER_VERSION, 'all' );
344 wp_register_style( 'splw-old-styles', LOCATION_WEATHER_ASSETS . '/css/old-style' . $this->suffix . '.css', array(), LOCATION_WEATHER_VERSION, 'all' );
345 wp_register_style( 'splw-swiper-styles', LOCATION_WEATHER_ASSETS . '/css/swiper' . $this->suffix . '.css', array(), LOCATION_WEATHER_VERSION, 'all' );
346 wp_register_style( 'splw-admin', LOCATION_WEATHER_ASSETS . '/css/admin' . $this->suffix . '.css', array(), LOCATION_WEATHER_VERSION, 'all' );
347
348 /**
349 * Register the JavaScript for the public and admin facing side of the plugin.
350 *
351 * @since 3.0.0
352 * @return void
353 */
354 wp_register_script( 'splw-old-script', LOCATION_WEATHER_ASSETS . '/js/Old-locationWeather' . $this->suffix . '.js', array( 'jquery' ), LOCATION_WEATHER_VERSION, true );
355 wp_register_script( 'splw-swiper-scripts', LOCATION_WEATHER_ASSETS . '/js/swiper' . $this->suffix . '.js', array( 'jquery' ), LOCATION_WEATHER_VERSION, true );
356 wp_register_script( 'splw-scripts', LOCATION_WEATHER_ASSETS . '/js/lw-scripts' . $this->suffix . '.js', array( 'jquery' ), LOCATION_WEATHER_VERSION, true );
357 wp_localize_script(
358 'splw-scripts',
359 'splw_ajax_object',
360 array(
361 'ajax_url' => admin_url( 'admin-ajax.php' ),
362 'splw_nonce' => wp_create_nonce( 'splw_nonce' ),
363 'splw_skip_cache' => is_admin() ? false : $skip_cache_for_weather,
364 )
365 );
366 }
367
368 /**
369 * Delete page shortcode ids array option on save
370 *
371 * @param int $post_ID current post id.
372 * @param object $post check post type.
373 * @return void
374 */
375 public function delete_page_lw_option_on_save( $post_ID, $post ) {
376 if ( is_multisite() ) {
377 $option_key = 'sp_lw_page_id' . get_current_blog_id() . $post_ID;
378 if ( get_site_option( $option_key ) ) {
379 delete_site_option( $option_key );
380 }
381 } elseif ( get_option( 'sp_lw_page_id' . $post_ID ) ) {
382 delete_option( 'sp_lw_page_id' . $post_ID );
383 }
384
385 if ( 'location_weather' === $post->post_type ) {
386 $current_cache_key = 'sp_open_weather_data' . $post_ID;
387 $weather_api_cache_key = 'sp_weather_api_data_' . $post_ID;
388 $hourly_forecast_cache_key = 'sp_open_weather_hourly_forecast_data' . $post_ID;
389 $this->splw_delete_transient( $current_cache_key );
390 $this->splw_delete_transient( $weather_api_cache_key );
391 $this->splw_delete_transient( $hourly_forecast_cache_key );
392 }
393 }
394 /**
395 * Delete the existing shortcode ids cache option on save
396 *
397 * @param mixed $cache_key Key.
398 * @return void
399 */
400 public function splw_delete_transient( $cache_key ) {
401 if ( is_multisite() ) {
402 $cache_key = $cache_key . '_' . get_current_blog_id();
403 delete_site_transient( $cache_key );
404 } else {
405 delete_transient( $cache_key );
406 }
407 }
408 /**
409 * Display a notice if the OpenWeatherMap API key is missing in the Location Weather plugin settings.
410 */
411 public function display_missing_api_key_notice() {
412 if ( ! current_user_can( 'manage_options' ) ) {
413 return;
414 }
415 // Check if the WordPress installation is multisite and get the plugin settings accordingly.
416 $plugin_settings = get_option( 'location_weather_settings' );
417 $open_api_key = $plugin_settings['open-api-key'] ?? '';
418 $weather_api_key = $plugin_settings['weather-api-key'] ?? '';
419 // Check if the Location Weather plugin is active and the OpenWeatherMap API key is empty.
420 if ( is_plugin_active( 'location-weather/main.php' ) && empty( $open_api_key ) && empty( $weather_api_key ) ) {
421 ?>
422 <div class="notice-warning notice location-api-notice">
423 <p><strong><?php esc_html_e( 'Location Weather', 'location-weather' ); ?>: </strong> Please set your own <a href = "<?php echo esc_url( admin_url( 'edit.php?post_type=location_weather&page=splw_admin_dashboard#lw_settings' ) ); ?>" > <?php esc_html_e( 'Weather API key', 'location-weather' ); ?></a> <?php esc_html_e( 'to show the weather data smoothly.', 'location-weather' ); ?></p>
424 </div>
425 <?php
426 }
427 }
428 }
429 // End of the class.
430
431 if ( ! function_exists( 'sp_location_weather' ) ) {
432 /**
433 * Shortcode converter function
434 *
435 * @param int $post_id shortcode id.
436 * @return void
437 */
438 function sp_location_weather( $post_id ) {
439 echo do_shortcode( '[location-weather id="' . $post_id . '"]' );
440 }
441 }
442
443 /**
444 * Initialize the main plugin.
445 *
446 * @return \Location_Weather
447 */
448 function location_weather() {
449 if ( ! defined( 'SHAPEDPLIUGIN_OFFER_BANNER_LOADED' ) ) {
450 define( 'SHAPEDPLIUGIN_OFFER_BANNER_LOADED', true );
451
452 /**
453 * The file is responsible for generating admin offer banner.
454 */
455 ShapedPlugin\Weather\Admin\ShapedPlugin_Offer_Banner::instance();
456 }
457
458 return Location_Weather::init();
459 }
460
461 /**
462 * Launch the plugin.
463 *
464 * @param object The plugin object.
465 */
466 if ( ! ( is_plugin_active( 'location-weather-pro/main.php' ) || is_plugin_active_for_network( 'location-weather-pro/main.php' ) ) ) {
467 location_weather();
468 }
469