| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
// If this file is called directly, abort. |
| 5 |
if (!defined('WPINC')) { |
| 6 |
die; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Options service |
| 11 |
*/ |
| 12 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound |
| 13 |
class EAOptions |
| 14 |
{ |
| 15 |
/** |
| 16 |
* @var wpdb |
| 17 |
*/ |
| 18 |
protected $wpdb; |
| 19 |
|
| 20 |
protected $current_options; |
| 21 |
|
| 22 |
/** |
| 23 |
* EAOptions constructor. |
| 24 |
* @param $wpdb |
| 25 |
*/ |
| 26 |
public function __construct($wpdb) |
| 27 |
{ |
| 28 |
$this->wpdb = $wpdb; |
| 29 |
|
| 30 |
add_action('easy_ea_update_options', array($this, 'manage_gdpr_cron')); |
| 31 |
add_filter('easy-appointments-user-ajax-capabilities', array($this, 'manage_capabilities'), 2, 1000); |
| 32 |
add_filter('easy-appointments-user-menu-capabilities', array($this, 'manage_page_capabilities'), 2, 1000); |
| 33 |
} |
| 34 |
|
| 35 |
public function manage_capabilities($default_capability, $page) { |
| 36 |
$option_name = "user.access.{$page}"; |
| 37 |
|
| 38 |
$options_value = $this->get_option_value($option_name, ''); |
| 39 |
|
| 40 |
return !empty($options_value) ? $options_value : $default_capability; |
| 41 |
} |
| 42 |
|
| 43 |
public function manage_page_capabilities($default_capability, $slug) { |
| 44 |
// easy_app_ |
| 45 |
$page = substr($slug, 9); |
| 46 |
|
| 47 |
if ($slug === 'easy_app_top_level') { |
| 48 |
$page = 'appointments'; |
| 49 |
} |
| 50 |
|
| 51 |
if ($slug === 'easy_app_reports' || $slug === 'easy_app_new_reports') { |
| 52 |
$page = 'reports'; |
| 53 |
} |
| 54 |
|
| 55 |
$option_name = "user.access.{$page}"; |
| 56 |
|
| 57 |
$options_value = $this->get_option_value($option_name, ''); |
| 58 |
|
| 59 |
return !empty($options_value) ? $options_value : $default_capability; |
| 60 |
} |
| 61 |
|
| 62 |
public function manage_gdpr_cron($options) { |
| 63 |
$set_cron = false; |
| 64 |
|
| 65 |
foreach ($options as $option) { |
| 66 |
if ($option['ea_key'] === 'gdpr.auto_remove' && !empty($option['ea_value'])) { |
| 67 |
$set_cron = true; |
| 68 |
break; |
| 69 |
} |
| 70 |
} |
| 71 |
// remove cron |
| 72 |
wp_clear_scheduled_hook('ea_gdpr_auto_delete'); |
| 73 |
|
| 74 |
if ($set_cron) { |
| 75 |
// set cron |
| 76 |
wp_schedule_event(time(), 'daily', 'ea_gdpr_auto_delete'); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public function get_default_options() { |
| 81 |
return array( |
| 82 |
'mail.pending' => 'pending', |
| 83 |
'mail.reservation' => 'reservation', |
| 84 |
'mail.canceled' => 'canceled', |
| 85 |
'mail.confirmed' => 'confirmed', |
| 86 |
'mail.admin' => '', |
| 87 |
'mail.admin.pending' => '', |
| 88 |
'mail.admin.confirmed' => '', |
| 89 |
'mail.admin.canceled' => '', |
| 90 |
'mail.admin.reservation' => '', |
| 91 |
'mail.action.two_step' => '0', |
| 92 |
'mail.send_email_notification' => '1', |
| 93 |
'trans.service' => 'Service', |
| 94 |
'trans.location' => 'Location', |
| 95 |
'trans.worker' => 'Worker', |
| 96 |
'trans.service_option' => 'Select Service', |
| 97 |
'trans.location_option' => 'Select Location', |
| 98 |
'trans.worker_option' => 'Select Worker', |
| 99 |
'trans.customer_search_label' => 'Search Customer', |
| 100 |
'trans.done_message' => 'Done', |
| 101 |
'trans.submit_button_text' => 'Submit', |
| 102 |
'trans.booking_message' => 'Your appointment has been successfully submitted. You will receive an update shortly', |
| 103 |
'trans.done_message_front' => 'Your appointment has been successfully submitted. You will receive an update shortly', |
| 104 |
'trans.create_new_booking' => 'Create New Booking', |
| 105 |
'time_format' => '00-24', |
| 106 |
'trans.currency' => '$', |
| 107 |
'pending.email' => '', |
| 108 |
'price.hide' => '0', |
| 109 |
'price.hide.service' => '0', |
| 110 |
'hide.decimal_in_price' => '0', |
| 111 |
'datepicker' => 'en-US', |
| 112 |
'send.user.email' => '0', |
| 113 |
'send.user.pending_email' => '0', |
| 114 |
'send.user.reservation_email' => '0', |
| 115 |
'send.user.cancelled_email' => '0', |
| 116 |
'send.user.confirmed_email' => '0', |
| 117 |
'custom.css' => '', |
| 118 |
'form.label.above' => '0', |
| 119 |
'show.iagree' => '0', |
| 120 |
'show.display_thankyou_note' => '0', |
| 121 |
'show.customer_search_front' => '0', |
| 122 |
'customer_search_password_only' => '0', |
| 123 |
'delete_data_on_uninstall' => '0', |
| 124 |
'customer_search_roles' => '[]', |
| 125 |
'cancel.scroll' => 'calendar', |
| 126 |
'multiple.work' => '1', |
| 127 |
'compatibility.mode' => '0', |
| 128 |
'pending.subject.email' => 'New Reservation #id#', |
| 129 |
'send.from.email' => '', |
| 130 |
'enable_status_subjects' => '0', |
| 131 |
'pending_subject_admin' => 'New Reservation #id#', |
| 132 |
'confirmed_subject_admin' => 'New Reservation #id#', |
| 133 |
'cancelled_subject_admin' => 'New Reservation #id#', |
| 134 |
'reservation_subject_admin' => 'New Reservation #id#', |
| 135 |
'pending_subject_visitor' => 'Reservation #id#', |
| 136 |
'confirmed_subject_visitor' => 'Reservation #id#', |
| 137 |
'cancelled_subject_visitor' => 'Reservation #id#', |
| 138 |
'reservation_subject_visitor' => 'Reservation #id#', |
| 139 |
'css.off' => '0', |
| 140 |
'submit.redirect' => '', |
| 141 |
'advance.redirect' => '[]', |
| 142 |
'advance_cancel.redirect' => '[]', |
| 143 |
'pending.subject.visitor.email' => 'Reservation #id#', |
| 144 |
'admin_reply_to_address' => '', |
| 145 |
'visitor_reply_to_address' => '', |
| 146 |
'block.time' => '0', |
| 147 |
'cancel_time' => '0', |
| 148 |
'max.appointments' => '5', |
| 149 |
'pre.reservation' => '0', |
| 150 |
'default.status' => 'pending', |
| 151 |
'send.worker.email' => '0', |
| 152 |
'send.worker.pending_email' => '0', |
| 153 |
'send.worker.reservation_email' => '0', |
| 154 |
'send.worker.cancelled_email' => '0', |
| 155 |
'send.worker.confirmed_email' => '0', |
| 156 |
'currency.before' => '0', |
| 157 |
'nonce.off' => '0', |
| 158 |
'gdpr.on' => '0', |
| 159 |
'gdpr.label' => 'By using this form you agree with the storage and handling of your data by this website.', |
| 160 |
'gdpr.link' => '', |
| 161 |
'gdpr.message' => 'You need to accept the privacy checkbox', |
| 162 |
'gdpr.auto_remove' => '0', |
| 163 |
'sort.workers-by' => 'id', |
| 164 |
'sort.services-by' => 'id', |
| 165 |
'sort.locations-by' => 'id', |
| 166 |
'order.workers-by' => 'DESC', |
| 167 |
'order.services-by' => 'DESC', |
| 168 |
'order.locations-by' => 'DESC', |
| 169 |
'captcha.site-key' => '', |
| 170 |
'captcha3.site-key' => '', |
| 171 |
'captcha.secret-key' => '', |
| 172 |
'captcha3.secret-key' => '', |
| 173 |
'fullcalendar.public' => '0', |
| 174 |
'fullcalendar.my_booking' => '0', |
| 175 |
'fullcalendar.my_booking_full_calendar' => '0', |
| 176 |
'fullcalendar.event.show' => '0', |
| 177 |
'fullcalendar.event.title_fields' => '', |
| 178 |
'fullcalendar.manage_appointment.show' => '0', |
| 179 |
'fullcalendar.event.template' => '', |
| 180 |
'shortcode.compress' => '1', |
| 181 |
'label.from_to' => '0', |
| 182 |
'user.access.services' => '', |
| 183 |
'user.access.workers' => '', |
| 184 |
'user.access.locations' => '', |
| 185 |
'user.access.connections' => '', |
| 186 |
'user.access.reports' => '', |
| 187 |
'max.appointments_by_user' => '0', |
| 188 |
'is_multiple_booking_allowed' => '0', |
| 189 |
'webhook.endpoints' => '[]', |
| 190 |
'pending_message' => 'Your appointment has been submitted and is currently pending approval. You will be notified once it is confirmed.', |
| 191 |
'confirmed_message' => 'Your appointment has been confirmed. Thank you!', |
| 192 |
'reservation_message' => 'Your appointment has been reserved. You will be notified once it is confirmed.', |
| 193 |
'trans.confirmation-title' => 'Thank You for Booking!', |
| 194 |
); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get data that are going to be inserted to database |
| 199 |
* |
| 200 |
* @return array |
| 201 |
*/ |
| 202 |
public function get_insert_options() |
| 203 |
{ |
| 204 |
$options = $this->get_default_options(); |
| 205 |
$output = array(); |
| 206 |
|
| 207 |
foreach ($options as $key => $value) { |
| 208 |
$output[] = array( |
| 209 |
'ea_key' => $key, |
| 210 |
'ea_value' => $value, |
| 211 |
'type' => 'default' |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
return $output; |
| 216 |
} |
| 217 |
|
| 218 |
public function get_mixed_options() |
| 219 |
{ |
| 220 |
$missing = array(); |
| 221 |
|
| 222 |
$defaults = $this->get_insert_options(); |
| 223 |
$current = $this->cache_options(); |
| 224 |
|
| 225 |
foreach ($defaults as $default) { |
| 226 |
$is_missing = true; |
| 227 |
|
| 228 |
foreach ($current as $option) { |
| 229 |
if ($option['ea_key'] == $default['ea_key']) { |
| 230 |
$is_missing = false; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
if ($is_missing) { |
| 235 |
$missing[] = $default; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
return array_merge($current, $missing); |
| 240 |
|
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Options for cache inline usage on front-end |
| 245 |
* |
| 246 |
* @return array |
| 247 |
*/ |
| 248 |
public function cache_options() |
| 249 |
{ |
| 250 |
$options = $this->get_options(); |
| 251 |
|
| 252 |
$output = array(); |
| 253 |
|
| 254 |
foreach ($options as $key => $value) { |
| 255 |
$output[] = array( |
| 256 |
'ea_key' => $key, |
| 257 |
'ea_value' => $value, |
| 258 |
'type' => 'default' |
| 259 |
); |
| 260 |
} |
| 261 |
|
| 262 |
return $output; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Get option from database |
| 267 |
* |
| 268 |
* @param $key |
| 269 |
* @param null $default |
| 270 |
* @return null |
| 271 |
*/ |
| 272 |
public function get_option_value($key, $default = null) |
| 273 |
{ |
| 274 |
// load options if there are not cached |
| 275 |
if (empty($this->current_options)) { |
| 276 |
$this->current_options = $this->get_options_from_db(); |
| 277 |
} |
| 278 |
|
| 279 |
if (!array_key_exists($key, $this->current_options)) { |
| 280 |
return $default; |
| 281 |
} |
| 282 |
|
| 283 |
return $this->current_options[$key]; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Get all EA options [key => value] |
| 288 |
* |
| 289 |
* @return array |
| 290 |
*/ |
| 291 |
public function get_options() |
| 292 |
{ |
| 293 |
// load options if there are not cached |
| 294 |
if (empty($this->current_options)) { |
| 295 |
$this->current_options = $this->get_options_from_db(); |
| 296 |
} |
| 297 |
|
| 298 |
return $this->current_options; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Get options, default options are overwritten by db ones |
| 303 |
* |
| 304 |
* @return array |
| 305 |
*/ |
| 306 |
protected function get_options_from_db() |
| 307 |
{ |
| 308 |
$table_name = esc_sql( $this->wpdb->prefix . 'ea_options' ); |
| 309 |
|
| 310 |
/* phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter */ |
| 311 |
$query = "SELECT ea_key, ea_value FROM {$table_name}"; |
| 312 |
|
| 313 |
/* phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared */ |
| 314 |
$output = $this->wpdb->get_results($query, OBJECT_K); |
| 315 |
|
| 316 |
$db_options = array(); |
| 317 |
|
| 318 |
foreach ($output as $key => $value) { |
| 319 |
$db_options[$key] = $value->ea_value; |
| 320 |
} |
| 321 |
|
| 322 |
$default = $this->get_default_options(); |
| 323 |
|
| 324 |
// combine options from db and defaults |
| 325 |
return array_merge($default, $db_options); |
| 326 |
} |
| 327 |
} |
| 328 |
|