PluginProbe
Bookit — Booking & Appointment Calendar / 2.6.0.5
Bookit — Booking & Appointment Calendar v2.6.0.5
2.6.0.5 2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 All 62 releases
bookit / includes / classes / admin / SettingsController.php

SettingsController.php in Bookit — Booking & Appointment Calendar 2.6.0.5, at includes/classes/admin/SettingsController.php

386 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Bookit\Classes\Admin;
4
5 use Bookit\Classes\Base\AddonsFactory;
6 use Bookit\Classes\Base\Plugin;
7 use Bookit\Classes\Database\Categories;
8 use Bookit\Classes\Database\Services;
9 use Bookit\Classes\Database\Staff;
10 use Bookit\Classes\Template;
11 use Bookit\Classes\Vendor\Payments;
12 use Bookit\Helpers\AddonHelper;
13 use Bookit\Helpers\CleanHelper;
14 use Bookit\Helpers\FreemiusHelper;
15 use Bookit\Helpers\MailTemplateHelper;
16 use Bookit\Helpers\TimeSlotHelper;
17
18 class SettingsController extends DashboardController {
19
20 public static $default_currency = 'usd';
21 public static $default_sender_name = '';
22 public static $default_sender_email = '';
23 public static $default_view_type = 'default';
24 public static $settings_key = 'bookit_settings';
25 public static $time_slot_default_duration = 15;
26 public static $calendar_view_types = array( 'default', 'step_by_step' );
27 public static $temp_bookit_pro_slug = 'bookit-pro/bookit-pro.php';
28
29 private static function getCleanRules() {
30 return array(
31 'booking_type' => array( 'type' => 'strval' ),
32 'theme' => array( 'type' => 'strval' ),
33 'sender_name' => array( 'type' => 'strval' ),
34 'sender_email' => array( 'type' => 'strval' ),
35 'hide_header_titles' => array( 'type' => 'strval' ),
36 'currency_symbol' => array( 'type' => 'strval' ),
37 'currency_position' => array( 'type' => 'strval' ),
38 'thousands_separator' => array( 'type' => 'strval' ),
39 'decimals_separator' => array( 'type' => 'strval' ),
40 'decimals_number' => array( 'type' => 'intval' ),
41 'custom_colors_enabled' => array( 'type' => 'strval' ),
42 'hide_from_for_equal_service_price' => array( 'type' => 'strval' ),
43 'currency' => array(
44 'function' => array(
45 'custom' => false,
46 'name' => 'strtolower',
47 ),
48 ),
49 'payments' => array(
50 'function' => array(
51 'custom' => true,
52 'name' => 'custom_sanitize_json',
53 ),
54 ),
55 'emails' => array(
56 'function' => array(
57 'custom' => true,
58 'name' => 'custom_sanitize_json',
59 ),
60 ),
61 'custom_colors' => array(
62 'function' => array(
63 'custom' => true,
64 'name' => 'custom_sanitize_json',
65 ),
66 ),
67 );
68 }
69
70 /**
71 * Display Rendered Template
72 *
73 * @since 2.5.0 - Use method to get template vars.
74 *
75 * @return bool|string
76 */
77 public static function render() {
78 self::enqueue_styles_scripts();
79
80 return Template::load_template(
81 'dashboard/bookit-settings',
82 self::get_template_vars(),
83 true
84 );
85 }
86
87 /**
88 * Get template variables.
89 *
90 * @since 2.5.0
91 *
92 * @return array
93 */
94 public static function get_template_vars() {
95 $installedAddons = FreemiusHelper::get_installed_addons();
96 $existAddons = AddonsFactory::getExistAddonsList( array_column( $installedAddons, 'name' ) );
97
98 $template_vars = [
99 'settings' => self::get_settings(),
100 'gateways' => [],
101 'addons' => array_merge( $installedAddons, $existAddons ),
102 'pro_disabled' => bookit_pro_features_disabled(), //todo remove
103 'pro_installed' => AddonHelper::isProPaymentsInstalled(),
104 'woocommerce_enabled' => class_exists( 'WooCommerce' ) ? 'true' : 'false', //todo remove
105 'woocommerce_products' => self::bookit_woocommerce_get_all_products(), //todo remove
106 'categories' => Categories::get_all(),
107 'services' => Services::get_all(),
108 'staff' => Staff::get_all(),
109 'currencies' => Payments::get_currency_list(),
110 'time_slot_options' => TimeSlotHelper::TIME_SLOT_POSSIBLE_VALUES,
111 'calendar_view_options' => self::$calendar_view_types,
112 ];
113
114 /**
115 * Filter the BookIt settings template variables.
116 *
117 * @since 2.5.0
118 *
119 * @param array $template_vars The template variables array.
120 */
121 return apply_filters( 'bookit_settings_template_vars', $template_vars );
122 }
123
124
125 /**
126 * Validate data
127 */
128 public static function validate( $data ) {
129 $errors = array();
130
131 $is_currency = array_search( strtoupper( $data['currency'] ), array_column( Payments::get_currency_list(), 'value' ) );
132
133 if ( strlen( $data['currency'] ) != 3 || false === $is_currency ) {
134 $errors['currency'] = esc_html__( 'Wrong currency value', 'bookit' );
135 }
136
137 if ( count( $errors ) > 0 ) {
138 wp_send_json_error(
139 array(
140 'errors' => $errors,
141 'message' => __( 'Error occurred!', 'bookit' ),
142 )
143 );
144 }
145 }
146
147 /**
148 * Save Settings
149 */
150 public static function save() {
151 check_ajax_referer( 'bookit_save_settings', 'nonce' );
152
153 if ( ! current_user_can( 'manage_options' ) ) {
154 return false;
155 }
156
157 $data = CleanHelper::cleanData( $_POST, self::getCleanRules() );
158 self::validate( $data );
159
160 if ( ! empty( $data ) ) {
161
162 unset( $data['nonce'] );
163
164 do_action( 'bookit_before_update_setting', $data );
165
166 self::save_settings( $data );
167 /** Rewrite email templates to WPML strings if WPML installed */
168 MailTemplateHelper::registerTemplateDataToWPMLStrings();
169
170 wp_send_json_success( array( 'message' => __( 'Settings Saved!', 'bookit' ) ) );
171 }
172
173 wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) );
174 }
175
176 /**
177 * Save Default Settings
178 * @return boolean
179 */
180 public static function save_default_settings() {
181 if ( empty( (array) self::get_settings() ) ) {
182 $settings = [
183 'booking_type' => 'registered',
184 'calendar_view' => self::$default_view_type,
185 'time_slot_duration' => static::$time_slot_default_duration,
186 'hide_header_titles' => false,
187 'clean_all_on_delete' => false,
188 'currency' => static::$default_currency,
189 'currency_symbol' => '$',
190 'currency_position' => 'left',
191 'thousands_separator' => ',',
192 'decimals_separator' => '.',
193 'decimals_number' => 2,
194 'custom_colors_enabled' => false,
195 'hide_from_for_equal_service_price' => false,
196 'is_step_by_step_view' => false,
197 'sender_name' => static::$default_sender_name,
198 'sender_email' => static::$default_sender_email,
199 'custom_colors' => [
200 'base_color' => '#006666',
201 'base_bg_color' => '#f0f8f8',
202 'highlight_color' => '#ffd400',
203 'white_color' => '#ffffff',
204 'dark_color' => '#272727',
205 ],
206 'payments' => [
207 'locally' => [ 'enabled' => true ],
208 'stripeConnect' => [ 'enabled' => false ],
209 'paypal' => [ 'enabled' => false ],
210 'stripe' => [ 'enabled' => false ],
211 'woocommerce' => [ 'enabled' => false ],
212 ],
213 'emails' => MailTemplateHelper::getTemplates(),
214 ];
215
216 return self::save_settings( $settings );
217 }
218
219 return true;
220 }
221
222 /**
223 * Get Settings
224 *
225 * @since 2.5.0 - Add a filter for the settings.
226 *
227 * @return mixed
228 */
229 public static function get_settings() {
230 $settings = get_option( self::$settings_key, [] );
231
232 /**
233 * Filter the BookIt settings.
234 *
235 * @since 2.5.0
236 *
237 * @param array<string|mixed> $settings The array of settings.
238 */
239 return apply_filters( 'bookit_settings', $settings );
240 }
241
242 /**
243 * Save Settings
244 * @param $settings
245 * @return mixed
246 */
247 public static function save_settings( $settings ) {
248 update_option( self::$settings_key, $settings, self::getCleanRules() );
249 }
250
251 /**
252 * Get All Products
253 * @return array
254 */
255 ////todo remove
256 public static function bookit_woocommerce_get_all_products() {
257 $products = array();
258 $paymentAddon = AddonHelper::getAddonDataByName( AddonHelper::$paymentAddon );
259 if ( ( bookit_pro_active() || $paymentAddon['isCanUse'] ) && class_exists( 'WooCommerce' ) ) {
260 $args = array(
261 'post_type' => 'product',
262 'posts_per_page' => -1,
263 );
264 $all_products = get_posts( $args );
265
266 foreach ( $all_products as $key => $product ) {
267 $products[ $key ]['id'] = $product->ID;
268 $products[ $key ]['title'] = $product->post_title;
269 }
270 }
271
272 return $products;
273 }
274
275 public static function bookit_load_setting_icon() {
276 check_ajax_referer( 'bookit_load_icon', 'nonce' );
277
278 if ( ! current_user_can( 'manage_options' ) ) {
279 return false;
280 }
281
282 if ( ! ( is_array( $_POST ) && is_array( $_FILES ) && defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
283 wp_send_json_error(
284 array(
285 'errors' => array( 'woocommerce_icon' => __( 'No data', 'bookit' ) ),
286 'message' => __( 'Error occurred!', 'bookit' ),
287 )
288 );
289 }
290
291 if ( empty( $_FILES['file'] ) ) {
292 wp_send_json_error(
293 array(
294 'errors' => array( 'woocommerce_icon' => __( 'File is empty', 'bookit' ) ),
295 'message' => __( 'Error occurred!', 'bookit' ),
296 )
297 );
298 }
299
300 if ( ! function_exists( 'wp_handle_upload' ) ) {
301 require_once ABSPATH . 'wp-admin/includes/file.php';
302 }
303
304 $file_info = wp_handle_upload( $_FILES['file'], array( 'test_form' => false ) );
305 /** save */
306 $settings = self::get_settings();
307 $settings['payments']['woocommerce']['custom_icon'] = $file_info['url'];
308 self::save_settings( $settings );
309
310 wp_send_json_success(
311 array(
312 'message' => __( 'Icon Uploaded!', 'bookit' ),
313 'icon_url' => $file_info['url'],
314 )
315 );
316 }
317
318 public static function bookit_remove_icon() {
319 check_ajax_referer( 'bookit_load_icon', 'nonce' );
320
321 if ( ! current_user_can( 'manage_options' ) ) {
322 return false;
323 }
324
325 /** save */
326 $settings = self::get_settings();
327 $settings['payments']['woocommerce']['custom_icon'] = '';
328 self::save_settings( $settings );
329
330 wp_send_json_success( array( 'message' => __( 'Icon Removed!', 'bookit' ) ) );
331 }
332
333 /** remove duplicate menu items for old bookit pro */
334 public static function removeBookitProFreemiusSubMenuDuplicate() {
335 $installedPlugins = AddonHelper::getInstalledPluginBySlug( self::$temp_bookit_pro_slug );
336 if ( empty( $installedPlugins ) || ! is_plugin_active( self::$temp_bookit_pro_slug )
337 || ! version_compare( $installedPlugins['Version'], '2.0.0', '<' ) ) {
338 return;
339 }
340 add_action( 'admin_head', array( self::class, 'remove_bookit_pro_double_submenu_pages' ) );
341 }
342
343 /** remove contact us menu item for free plugin */
344 public static function removeBookitContactUsForFreeVersion() {
345 if ( function_exists( 'bookit_fs' ) ) {
346 $bookit_fs = bookit_fs();
347 $addons = $bookit_fs->get_installed_addons();
348 if ( empty( $addons ) ) {
349 add_action(
350 'admin_head',
351 array( self::class, 'remove_fm_contact_us_submenu_for_free' )
352 );
353 add_action(
354 'init',
355 array( self::class, 'redirect_to_main_page' )
356 );
357 }
358 }
359 }
360
361 /** redirct to main page from contact */
362 public static function redirect_to_main_page() {
363 $freemiusContactSlug = 'bookit-contact';
364 if ( isset( $_GET['page'] ) && $_GET['page'] == $freemiusContactSlug ) {
365 wp_safe_redirect( home_url() . '/wp-admin/admin.php?page=bookit' );
366 exit();
367 }
368 }
369
370 /**
371 * Remove contact us for free version
372 */
373 public static function remove_fm_contact_us_submenu_for_free() {
374 $freemiusContactSlug = 'bookit-contact';
375 remove_submenu_page( 'bookit', $freemiusContactSlug );
376 }
377 /**
378 * If bookit pro is installed remove
379 * Contact Us and Account menu dublicates
380 */
381 public static function remove_bookit_pro_double_submenu_pages() {
382 $freemiusAccountSlug = 'bookit-account';
383 remove_submenu_page( 'bookit', $freemiusAccountSlug );
384 }
385 }
386