| 1 |
<?php |
| 2 |
|
| 3 |
namespace Bookit\Classes; |
| 4 |
|
| 5 |
use Bookit\Classes\Database\Categories; |
| 6 |
use Bookit\Classes\Database\Customers; |
| 7 |
use Bookit\Classes\Database\Services; |
| 8 |
use Bookit\Classes\Database\Staff; |
| 9 |
use Bookit\Classes\Admin\SettingsController; |
| 10 |
use Bookit\Helpers\AddonHelper; |
| 11 |
use Bookit\Helpers\TimeSlotHelper; |
| 12 |
|
| 13 |
class BookitController { |
| 14 |
|
| 15 |
/** |
| 16 |
* Bookit Calendar Frontend |
| 17 |
*/ |
| 18 |
public static function init() { |
| 19 |
add_shortcode( 'bookit', array( self::class, 'render_shortcode' ) ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Enqueue Frontend Styles & Scripts |
| 24 |
*/ |
| 25 |
/** |
| 26 |
* @param $settings |
| 27 |
*/ |
| 28 |
public static function enqueue_styles_scripts( $settings ) { |
| 29 |
wp_enqueue_script( 'bookit-app', BOOKIT_URL . 'assets/dist/frontend/js/app.js', array(), BOOKIT_VERSION ); |
| 30 |
|
| 31 |
$paymentAddon = AddonHelper::getAddonDataByName( AddonHelper::$paymentAddon ); |
| 32 |
|
| 33 |
wp_enqueue_script( 'bookit-stripe-js', 'https://js.stripe.com/v3/', array(), false, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion |
| 34 |
|
| 35 |
$styles = BOOKIT_URL . 'assets/dist/frontend/css/app.css'; |
| 36 |
|
| 37 |
if ( 'true' == $settings['custom_colors_enabled'] ) { |
| 38 |
$upload = wp_upload_dir(); |
| 39 |
$styles_path = $upload['basedir'] . '/bookit/app.css'; |
| 40 |
if ( file_exists( $styles_path ) ) { |
| 41 |
$styles = $upload['baseurl'] . '/bookit/app.css'; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
wp_enqueue_style( 'bookit-app', $styles, array(), intval( get_option( 'bookit_styles_version', BOOKIT_VERSION ) ) ); |
| 46 |
|
| 47 |
$ajax_data = array( |
| 48 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 49 |
'translations' => Translations::get_frontend_translations(), |
| 50 |
'nonces' => Nonces::get_frontend_nonces(), |
| 51 |
); |
| 52 |
|
| 53 |
wp_localize_script( 'bookit-app', 'bookit_window', $ajax_data ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Render Frontend Bookit Calendar |
| 58 |
* |
| 59 |
* @param bool $display |
| 60 |
* @param bool $is_admin |
| 61 |
* @param null $category_id |
| 62 |
* @param null $service_id |
| 63 |
* @param null $staff_id |
| 64 |
* |
| 65 |
* @return bool|string |
| 66 |
*/ |
| 67 |
public static function render_calendar( $display = false, $category_id = null, $service_id = null, $staff_id = null, $theme = null ) { |
| 68 |
|
| 69 |
$paymentAddon = AddonHelper::getAddonDataByName( AddonHelper::$paymentAddon ); |
| 70 |
$bookitPaymentsActive = $paymentAddon['isCanUse'] ?? false; |
| 71 |
|
| 72 |
$shortcodeAttributes = array( |
| 73 |
'category_id' => $category_id, |
| 74 |
'service_id' => $service_id, |
| 75 |
'staff_id' => $staff_id, |
| 76 |
); |
| 77 |
$base_data = self::get_base_data_by_attributes( $shortcodeAttributes ); |
| 78 |
$categories = $base_data['categories']; |
| 79 |
$services = $base_data['services']; |
| 80 |
$staff = $base_data['staff']; |
| 81 |
$settings = SettingsController::get_settings(); |
| 82 |
$settings['date_format'] = bookit_php_to_moment( get_option( 'date_format' ) ); |
| 83 |
$settings['time_format'] = bookit_php_to_moment( get_option( 'time_format' ) ); |
| 84 |
$settings['pro_active'] = bookit_pro_active(); |
| 85 |
$settings['payment_active'] = $bookitPaymentsActive ? true : false; |
| 86 |
$user = ( is_user_logged_in() ) ? wp_get_current_user() : null; |
| 87 |
$language = substr( get_bloginfo( 'language' ), 0, 2 ); |
| 88 |
$navigation = self::get_step_naviation(); |
| 89 |
|
| 90 |
$time_format = get_option( 'time_format' ); |
| 91 |
$service_start = 0; |
| 92 |
$service_end = TimeSlotHelper::DAY_IN_SECONDS; |
| 93 |
$time_slot_list = TimeSlotHelper::getTimeList( $service_start, $service_end ); |
| 94 |
|
| 95 |
if ( ! empty( $services ) ) { |
| 96 |
foreach ( $services as &$service ) { |
| 97 |
if ( is_array( $service ) ) { |
| 98 |
$service['icon_url'] = ( ! empty( $service['icon_id'] ) ) ? wp_get_attachment_url( $service['icon_id'] ) : null; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if ( ! empty( $user->ID ) ) { |
| 104 |
$user = (object) array_merge( (array) $user->data, array( 'customer' => Customers::get( 'wp_user_id', $user->ID ) ) ); |
| 105 |
} |
| 106 |
|
| 107 |
if ( count( $categories ) <= 1 ) { |
| 108 |
$key = array_search( 'category', array_column( $navigation, 'key' ) ); |
| 109 |
array_splice( $navigation, $key, 1 ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( ! empty( $service_id ) || ( count( $services ) == 1 && ( count( $categories ) == 1 || ! empty( $category_id ) ) ) ) { |
| 113 |
$key = array_search( 'service', array_column( $navigation, 'key' ) ); |
| 114 |
array_splice( $navigation, $key, 1 ); |
| 115 |
} |
| 116 |
|
| 117 |
self::enqueue_styles_scripts( $settings ); |
| 118 |
|
| 119 |
$data = array( |
| 120 |
'attributes' => $shortcodeAttributes, |
| 121 |
'categories' => $categories, |
| 122 |
'services' => $services, |
| 123 |
'staff' => $staff, |
| 124 |
'settings' => $settings, |
| 125 |
'user' => $user, |
| 126 |
'language' => $language, |
| 127 |
'slot_list' => $time_slot_list, |
| 128 |
'navigation' => $navigation, |
| 129 |
'theme' => $theme, // choosen in shortcode theme |
| 130 |
'time_format' => $time_format, |
| 131 |
); |
| 132 |
|
| 133 |
return Template::load_template( 'bookit-calendar', $data, $display ); |
| 134 |
} |
| 135 |
|
| 136 |
protected static function get_base_data_by_attributes( $shortcodeAttributes ) { |
| 137 |
$result = array( |
| 138 |
'staff' => array(), |
| 139 |
'services' => array(), |
| 140 |
'categories' => array(), |
| 141 |
); |
| 142 |
|
| 143 |
if ( ! empty( $shortcodeAttributes['staff_id'] ) ) { |
| 144 |
$result['staff'] = Staff::get_one( $shortcodeAttributes['staff_id'] ); |
| 145 |
$result['services'] = Services::get_staff_services( $shortcodeAttributes['staff_id'] ); |
| 146 |
$result['categories'] = Categories::get_categories_by_ids( array_unique( array_column( $result['services'], 'category_id' ) ) ); |
| 147 |
|
| 148 |
} else { |
| 149 |
$result['staff'] = Staff::get_all(); |
| 150 |
if ( ! empty( $shortcodeAttributes['service_id'] ) ) { |
| 151 |
$service = Services::get( 'id', $shortcodeAttributes['service_id'] ); |
| 152 |
$result['services'] = array( $service ); |
| 153 |
$shortcodeAttributes['category_id'] = ( property_exists( $service, 'category_id' ) && ! empty( $service->category_id ) ) ? $service->category_id : null; |
| 154 |
} else { |
| 155 |
$result['services'] = Services::get_assigned_to_staff(); |
| 156 |
} |
| 157 |
|
| 158 |
if ( ! empty( $shortcodeAttributes['category_id'] ) ) { |
| 159 |
$result['categories'] |
| 160 |
= Categories::get_one( $shortcodeAttributes['category_id'] ); |
| 161 |
} else { |
| 162 |
$result['categories'] = Categories::get_with_exist_services(); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return $result; |
| 167 |
} |
| 168 |
|
| 169 |
private static function get_step_naviation() { |
| 170 |
$step_navigation = array( |
| 171 |
array( |
| 172 |
'key' => 'category', |
| 173 |
'menu' => __( 'Category', 'bookit' ), |
| 174 |
'title' => __( 'Select Category', 'bookit' ), |
| 175 |
'requiredFields' => array(), |
| 176 |
'validation' => false, |
| 177 |
), |
| 178 |
array( |
| 179 |
'key' => 'service', |
| 180 |
'menu' => __( 'Service', 'bookit' ), |
| 181 |
'title' => __( 'Select Service', 'bookit' ), |
| 182 |
'requiredFields' => array( 'category_id' ), |
| 183 |
'validation' => false, |
| 184 |
), |
| 185 |
array( |
| 186 |
'key' => 'dateTime', |
| 187 |
'menu' => __( 'Date', 'bookit' ), |
| 188 |
'title' => __( 'Select Time & Employee', 'bookit' ), |
| 189 |
'requiredFields' => array( 'category_id', 'service_id' ), |
| 190 |
'validation' => false, |
| 191 |
), |
| 192 |
array( |
| 193 |
'key' => 'detailsForm', |
| 194 |
'menu' => __( 'Details', 'bookit' ), |
| 195 |
'title' => __( 'Details', 'bookit' ), |
| 196 |
'requiredFields' => array( |
| 197 |
'service_id', |
| 198 |
'staff_id', |
| 199 |
'date_timestamp', |
| 200 |
'start_time', |
| 201 |
), |
| 202 |
'validation' => true, |
| 203 |
), |
| 204 |
array( |
| 205 |
'key' => 'payment', |
| 206 |
'menu' => __( 'Payment', 'bookit' ), |
| 207 |
'title' => __( 'Payment', 'bookit' ), |
| 208 |
'requiredFields' => array( |
| 209 |
'service_id', |
| 210 |
'staff_id', |
| 211 |
'date_timestamp', |
| 212 |
'start_time', |
| 213 |
'end_time', |
| 214 |
'email', |
| 215 |
'full_name', |
| 216 |
), |
| 217 |
'validation' => false, |
| 218 |
), |
| 219 |
array( |
| 220 |
'key' => 'confirmation', |
| 221 |
'menu' => __( 'Confirmation', 'bookit' ), |
| 222 |
'title' => __( 'Confirmation', 'bookit' ), |
| 223 |
'requiredFields' => array( |
| 224 |
'service_id', |
| 225 |
'staff_id', |
| 226 |
'date_timestamp', |
| 227 |
'start_time', |
| 228 |
'end_time', |
| 229 |
'email', |
| 230 |
'full_name', |
| 231 |
'payment_method', |
| 232 |
), |
| 233 |
'validation' => true, |
| 234 |
), //todo |
| 235 |
array( |
| 236 |
'key' => 'result', |
| 237 |
'requiredFields' => array(), |
| 238 |
'validation' => false, |
| 239 |
), |
| 240 |
); |
| 241 |
|
| 242 |
return $step_navigation; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Shortcode |
| 247 |
* |
| 248 |
* @param $atts |
| 249 |
* |
| 250 |
* @return bool|string |
| 251 |
*/ |
| 252 |
public static function render_shortcode( $atts ) { |
| 253 |
$atts = shortcode_atts( |
| 254 |
array( |
| 255 |
'category' => null, |
| 256 |
'service' => null, |
| 257 |
'staff' => null, |
| 258 |
'theme' => null, |
| 259 |
), |
| 260 |
$atts, |
| 261 |
'bookit' |
| 262 |
); |
| 263 |
return self::render_calendar( false, $atts['category'], $atts['service'], $atts['staff'], $atts['theme'] ); |
| 264 |
} |
| 265 |
} |
| 266 |
|