PluginProbe
Slope Widgets / trunk
Slope Widgets vtrunk
trunk 1.0 1.1 1.1.1 1.1.2 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.4.0 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 3.0.0 3.0.1 3.0.2 3.1.0 3.1.1 3.2.0 4.0.0 4.0.1 4.1.0 All 45 releases
slope-widgets / slope-widgets.php

slope-widgets.php in Slope Widgets trunk, at slope-widgets.php

258 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Plugin Name: Slope Widgets
4 * Description: Aggiungi i widget di Slope al sito web WordPress della tua struttura ricettiva! Questo plugin ti permette di mostrare la barra delle prenotazioni, i pacchetti e le promozioni tramite shortcode personalizzabili.
5 * Version: 4.3.5
6 * Author: Slope
7 * Author URI: https://www.slope.it/
8 * Text Domain: slope-widgets
9 * Domain Path: /languages
10 * License: GPLv2 or later
11 */
12
13 if (!defined('ABSPATH')) {
14 exit; // Exit if accessed directly.
15 }
16
17 // We use this constant to invalidate the cache of the plugin's assets
18 const SLOPE_WIDGETS_CACHE_BUST_VERSION = '4.3.5';
19
20 // Current version of the database schema. It is used to check if the database schema needs to be updated.
21 const SLOPE_SCHEMA_VERSION = '1.0';
22 // This is the option name used to store the current version of the database schema
23 const SLOPE_DB_VERSION_OPTION_KEY = 'slope_db_schema_version';
24
25 // TODO: At the moment this is used only for the new promotions. Legacy promotions have hardcoded values inside
26 // `slope-widgets.js`. We should refactor this to use the same constant in the future.
27 define(
28 'SLOPE_BOOKING_ENGINE_BASE_URL',
29 getenv('SLOPE_BOOKING_ENGINE_BASE_URL') !== false ? getenv('SLOPE_BOOKING_ENGINE_BASE_URL') : 'https://booking.slope.it'
30 );
31
32 // Promotions are cached to reduce API request volume and improve performance. Cache expiration time is a balance
33 // between maintaining data freshness while preventing to many requests. We may need to tweak the value once we have
34 // more data on actual requests.
35 define(
36 'SLOPE_PROMOTIONS_CACHE_EXPIRATION_SECONDS',
37 getenv('SLOPE_PROMOTIONS_CACHE_EXPIRATION_SECONDS') !== false ? getenv('SLOPE_PROMOTIONS_CACHE_EXPIRATION_SECONDS') : 120
38 );
39
40 // This is the path to the plugin directory. It is used to load the plugin's assets and files.
41 define('SLOPE_WIDGETS_PATH', plugin_dir_path(__FILE__));
42
43 // Register PSR-4 autoloader
44 require_once(SLOPE_WIDGETS_PATH . 'includes/Autoloader.php');
45 SlopeWidgets\Autoloader::register();
46
47 // Use statements for classes we'll instantiate
48 use SlopeWidgets\Admin\WelcomePage;
49 use SlopeWidgets\Admin\GlobalSettings;
50 use SlopeWidgets\Admin\Reservations;
51 use SlopeWidgets\Admin\Promotions;
52 use SlopeWidgets\DBUpgrader;
53 use SlopeWidgets\Settings\ReservationsSettingsProvider;
54
55 // NOTE: The order of instantiations is important to render the item of the admin menu in the right order. As we are
56 // using a mixed approach (class and functions) we need to instantiate the new class before the menu is created by
57 // `slope_add_page`.
58 $slope_widgets_welcome_page = new WelcomePage();
59 $slope_widgets_global_settings = new GlobalSettings();
60 $slope_widgets_reservations_page = new Reservations();
61
62 // Hook
63 add_action('admin_init', 'slope_init');
64 add_action('admin_menu', 'slope_add_page');
65 add_action('admin_enqueue_scripts', 'slope_color_picker');
66 add_action('admin_enqueue_scripts', 'slope_load_admin');
67 add_action('init', 'slope_load_js_modules');
68 add_action('wp_enqueue_scripts', 'slope_load_widgets');
69 add_action('plugins_loaded', 'slope_load_textdomain');
70 add_action('plugin_action_links_' . plugin_basename(__FILE__), 'slope_action_links');
71 add_action('init', 'slope_load_reservations_block');
72
73 // NOTE: The order of instantiations is important (see comment above). We want new Promotions class after the old
74 // menu is created by `slope_add_page`.
75 $slope_widgets_promotions_page = new Promotions();
76
77 /**
78 * When the plugin is activated, we store the current version of the database schema.
79 */
80 register_activation_hook(__FILE__, function () {
81 add_option(SLOPE_DB_VERSION_OPTION_KEY, SLOPE_SCHEMA_VERSION);
82 });
83
84 add_action('plugins_loaded', function () {
85 $dbUpgrader = new DBUpgrader();
86 $dbUpgrader->upgradeIfNeeded();
87 });
88
89 /**
90 * Redirect to the welcome page after plugin activation
91 */
92 add_action('activated_plugin', function ($plugin) {
93 if ($plugin == plugin_basename(__FILE__)) {
94 wp_safe_redirect(admin_url('/admin.php?page=slope_welcome'));
95 exit();
96 }
97 });
98
99 function slope_load_js_modules()
100 {
101 wp_enqueue_script(
102 'slope_modules_js',
103 plugins_url('js/slope-modules.js', __FILE__),
104 [],
105 SLOPE_WIDGETS_CACHE_BUST_VERSION,
106 ['in_footer' => false]
107 );
108
109 $scriptOptions = [
110 'force_mobile_layout' => ReservationsSettingsProvider::isMobileLayoutForced(),
111 ];
112 wp_add_inline_script(
113 'slope_modules_js',
114 'const slpWidgetOptions = ' . wp_json_encode($scriptOptions),
115 'before'
116 );
117 }
118
119 function slope_color_picker()
120 {
121 wp_enqueue_style('wp-color-picker', '', [], SLOPE_WIDGETS_CACHE_BUST_VERSION);
122 wp_enqueue_script(
123 'slope-color-picker',
124 plugins_url('js/slope-colorpicker.js', __FILE__),
125 ['wp-color-picker'],
126 SLOPE_WIDGETS_CACHE_BUST_VERSION,
127 ['in_footer' => true]
128 );
129 }
130
131 // callback slope-widgets-admin
132 function slope_load_admin()
133 {
134 wp_enqueue_style(
135 'slope-admin-css',
136 plugins_url('css/slope-admin.css', __FILE__),
137 [],
138 SLOPE_WIDGETS_CACHE_BUST_VERSION
139 );
140 wp_enqueue_script(
141 'slope-admin-js',
142 plugins_url('js/slope-admin.js', __FILE__),
143 [],
144 SLOPE_WIDGETS_CACHE_BUST_VERSION,
145 ['in_footer' => false]
146 );
147 }
148
149 function slope_load_widgets()
150 {
151 wp_enqueue_style(
152 'slope_widgets_css',
153 plugins_url('css/slope-widgets.css', __FILE__),
154 [],
155 SLOPE_WIDGETS_CACHE_BUST_VERSION
156 );
157 wp_enqueue_script(
158 'slope_js',
159 plugins_url('js/slope-widgets.js', __FILE__),
160 ['jquery-ui-core'],
161 SLOPE_WIDGETS_CACHE_BUST_VERSION,
162 true
163 );
164
165 slope_legacy_promotions_style();
166 slope_legacy_promotions_layout_select();
167 }
168
169 // Register the settings of Slope fields
170 function slope_init()
171 {
172 // Slope promotions legacy
173 register_setting(
174 'slope_promotions_options',
175 'slope_promotions_options',
176 ['sanitize_callback' => 'slope_sanitize_legacy_promotions_options']
177 );
178 add_settings_section('promotions_section', '', 'slope_promotions_intro', 'slope_promotions_page');
179 }
180
181 /**
182 * TODO: This is going to be removed in a future release. As legacy form is disabled it should never be submitted, but
183 * we need to provide a sanitize callback to avoid issues with the settings API.
184 * @param mixed[]|mixed $input
185 * @return mixed[]
186 */
187 function slope_sanitize_legacy_promotions_options($input): array
188 {
189 if (!is_array($input)) {
190 return [];
191 }
192
193 return $input;
194 }
195
196 // Setting of the domain of the translations
197 function slope_load_textdomain()
198 {
199 load_plugin_textdomain('slope-widgets', false, basename(dirname(__FILE__)) . '/languages/');
200 }
201
202 // Adds the entries to the lateral menu
203 function slope_add_page()
204 {
205 // Slug is an alphanumeric string used to identify the page in the menu. Should be unique for this menu page so we use the plugin's main file path.
206 $slug = 'slope-widgets/slope-widgets.php';
207 add_submenu_page($slug, 'Promotions [Deprecato]', 'Promotions [Deprecato]', 'manage_options', 'slope_promotions', 'slope_promotions_options_page');
208 }
209
210 // Add the settings link on the plugin listing page.
211 function slope_action_links($links)
212 {
213 return array_merge(
214 [
215 '<a href="' . esc_url(admin_url('/admin.php?page=slope_settings')) . '">' . __('Settings', 'slope-widgets') . '</a>'
216 ],
217 $links
218 );
219 }
220
221 function slope_load_reservations_block()
222 {
223 wp_register_script(
224 'slope-reservations-block',
225 plugins_url('js/slope-reservations-block.js', __FILE__),
226 ['wp-blocks', 'wp-element'],
227 SLOPE_WIDGETS_CACHE_BUST_VERSION,
228 ['in_footer' => false]
229 );
230
231 global $wp_version;
232
233 if (version_compare($wp_version, '5', '>=')) {
234 register_block_type('slope-plugins/slope-reservations', array(
235 'editor_script' => 'slope-reservations-block',
236 ));
237 }
238 }
239
240 /**
241 * Include plugin PHP files through a single guarded entry point.
242 */
243 function slope_include_file(string $filePath)
244 {
245 if (!defined('ABSPATH')) {
246 exit;
247 }
248
249 include($filePath);
250 }
251
252 //include plugin promotions functionalities
253 // TODO: This is going to be removed in a future release
254 slope_include_file(SLOPE_WIDGETS_PATH . 'slope-promotions-legacy.php');
255
256 // Include promotion list partials
257 slope_include_file(SLOPE_WIDGETS_PATH . 'includes/pages/promotions/partials/promotionsList.ajax.php');
258