activities_helper.php
1 year ago
agent_helper.php
1 year ago
auth_helper.php
9 months ago
blocks_helper.php
1 year ago
booking_helper.php
1 year ago
bricks_helper.php
1 year ago
bundles_helper.php
1 year ago
calendar_helper.php
9 months ago
carts_helper.php
1 year ago
connector_helper.php
9 months ago
csv_helper.php
9 months ago
customer_helper.php
9 months ago
customer_import_helper.php
9 months ago
database_helper.php
9 months ago
debug_helper.php
1 year ago
defaults_helper.php
1 year ago
elementor_helper.php
1 year ago
email_helper.php
9 months ago
encrypt_helper.php
1 year ago
events_helper.php
9 months ago
form_helper.php
9 months ago
icalendar_helper.php
1 year ago
image_helper.php
1 year ago
invoices_helper.php
9 months ago
license_helper.php
1 year ago
location_helper.php
1 year ago
marketing_systems_helper.php
1 year ago
meeting_systems_helper.php
1 year ago
menu_helper.php
9 months ago
meta_helper.php
1 year ago
migrations_helper.php
1 year ago
money_helper.php
1 year ago
notifications_helper.php
9 months ago
order_intent_helper.php
9 months ago
orders_helper.php
1 year ago
otp_helper.php
9 months ago
pages_helper.php
1 year ago
params_helper.php
1 year ago
payments_helper.php
1 year ago
price_breakdown_helper.php
1 year ago
process_jobs_helper.php
1 year ago
processes_helper.php
1 year ago
replacer_helper.php
1 year ago
resource_helper.php
1 year ago
roles_helper.php
9 months ago
router_helper.php
1 year ago
service_helper.php
1 year ago
sessions_helper.php
1 year ago
settings_helper.php
9 months ago
short_links_systems_helper.php
9 months ago
shortcodes_helper.php
9 months ago
sms_helper.php
1 year ago
steps_helper.php
9 months ago
stripe_connect_helper.php
9 months ago
styles_helper.php
9 months ago
support_topics_helper.php
1 year ago
time_helper.php
9 months ago
timeline_helper.php
9 months ago
transaction_helper.php
1 year ago
transaction_intent_helper.php
1 year ago
util_helper.php
9 months ago
version_specific_updates_helper.php
9 months ago
whatsapp_helper.php
1 year ago
work_periods_helper.php
1 year ago
wp_datetime.php
1 year ago
wp_user_helper.php
1 year ago
blocks_helper.php
460 lines
| 1 | <?php |
| 2 | |
| 3 | class OsBlockHelper { |
| 4 | public static function register_blocks() { |
| 5 | self::register_latepoint_category(); |
| 6 | self::register_block_book_button(); |
| 7 | self::register_block_book_form(); |
| 8 | self::register_block_list_of_resources(); |
| 9 | self::register_block_calendar(); |
| 10 | self::register_block_customer_dashboard(); |
| 11 | self::register_block_customer_login(); |
| 12 | } |
| 13 | |
| 14 | public static function localized_vars_for_blocks() : array { |
| 15 | |
| 16 | $has_to_pick = [ 'label' => __('Customer will pick', 'latepoint'), 'value' => '' ]; |
| 17 | $localized_block_vars = []; |
| 18 | |
| 19 | // AGENTS |
| 20 | $localized_block_vars['agents'] = []; |
| 21 | $localized_block_vars['selected_agents_options'][] = $has_to_pick; |
| 22 | $localized_block_vars['selected_agents_options'][] = [ 'label' => __('Any Available', 'latepoint'), 'value' => LATEPOINT_ANY_AGENT ]; |
| 23 | $agents = new OsAgentModel(); |
| 24 | $agents = $agents->get_results_as_models(); |
| 25 | if($agents){ |
| 26 | foreach($agents as $agent){ |
| 27 | $localized_block_vars['selected_agents_options'][] = ['label' => $agent->full_name, 'value' => $agent->id]; |
| 28 | $localized_block_vars['agents'][] = [ |
| 29 | 'name' => $agent->full_name, |
| 30 | 'id' => $agent->id, |
| 31 | 'title' => $agent->title, |
| 32 | 'short_description' => $agent->short_description, |
| 33 | 'avatar_url' => empty($agent->avatar_image_id) ? '' : $agent->get_avatar_url()]; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // SERVICES |
| 38 | $localized_block_vars['services'] = []; |
| 39 | $localized_block_vars['selected_services_options'][] = $has_to_pick; |
| 40 | $services = new OsServiceModel(); |
| 41 | $services = $services->get_results_as_models(); |
| 42 | if($services){ |
| 43 | foreach($services as $service){ |
| 44 | $localized_block_vars['selected_services_options'][] = ['label' => $service->name, 'value' => $service->id]; |
| 45 | $localized_block_vars['services'][] = [ |
| 46 | 'name' => $service->name, |
| 47 | 'id' => $service->id, |
| 48 | 'image_url' => empty($service->description_image_id) ? '' : $service->get_description_image_url(), |
| 49 | 'description' => $service->short_description, |
| 50 | 'category_id' => $service->category_id, |
| 51 | 'price_formatted' => ($service->price_min > 0) ? (($service->price_min != $service->price_max) ? __('Starts at', 'latepoint') . ' ' . $service->price_min_formatted : $service->price_min_formatted) : '' |
| 52 | ]; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | |
| 57 | // SERVICE CATEGORIES |
| 58 | $localized_block_vars['service_categories'] = []; |
| 59 | $localized_block_vars['selected_service_categories_options'][] = [ 'label' => __('Show All', 'latepoint'), 'value' => '' ]; |
| 60 | $service_categories = new OsServiceCategoryModel(); |
| 61 | $service_categories = $service_categories->get_results_as_models(); |
| 62 | if($service_categories){ |
| 63 | foreach($service_categories as $service_category){ |
| 64 | $localized_block_vars['service_categories'][] = [ |
| 65 | 'name' => $service_category->name, |
| 66 | 'id' => $service_category->id |
| 67 | ]; |
| 68 | $localized_block_vars['selected_service_categories_options'][] = ['label' => $service_category->name, 'value' => $service_category->id]; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | // LOCATIONS |
| 74 | $localized_block_vars['locations'] = []; |
| 75 | $localized_block_vars['selected_locations_options'][] = $has_to_pick; |
| 76 | $localized_block_vars['selected_locations_options'][] = [ 'label' => __('Any Available', 'latepoint'), 'value' => LATEPOINT_ANY_LOCATION ]; |
| 77 | $locations = new OsLocationModel(); |
| 78 | $locations = $locations->get_results_as_models(); |
| 79 | if($locations){ |
| 80 | foreach($locations as $location){ |
| 81 | $localized_block_vars['selected_locations_options'][] = ['label' => $location->name, 'value' => $location->id]; |
| 82 | $localized_block_vars['locations'][] = [ |
| 83 | 'name' => $location->name, |
| 84 | 'id' => $location->id, |
| 85 | 'category_id' => $location->category_id]; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | $localized_block_vars['bundles'] = []; |
| 90 | $localized_block_vars['selected_bundles_options'][] = $has_to_pick; |
| 91 | $bundles = (new OsBundleModel())->should_be_active()->get_results_as_models(); |
| 92 | if ($bundles) { |
| 93 | foreach ( $bundles as $bundle ) { |
| 94 | $localized_block_vars['selected_bundles_options'][] = ['label' => $bundle->name, 'value' => $bundle->id]; |
| 95 | $localized_block_vars['bundles'][] = [ |
| 96 | 'name' => $bundle->name, |
| 97 | 'id' => $bundle->id, |
| 98 | 'price_formatted' => $bundle->get_formatted_charge_amount(), |
| 99 | 'description' => $bundle->short_description |
| 100 | ]; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return $localized_block_vars; |
| 105 | } |
| 106 | |
| 107 | public static function register_latepoint_category() { |
| 108 | add_filter('block_categories_all', function ($categories) { |
| 109 | // Adding a new category. |
| 110 | $categories[] = [ |
| 111 | 'slug' => 'latepoint', |
| 112 | 'title' => 'LatePoint', |
| 113 | ]; |
| 114 | return $categories; |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | public static function register_block_book_button() { |
| 119 | register_block_type(LATEPOINT_ABSPATH . 'blocks/build/book-button/block.json', |
| 120 | [ |
| 121 | 'render_callback' => 'OsBlockHelper::render_book_button', |
| 122 | 'editor_script_handles' => ['latepoint-block-book-button'] |
| 123 | ]); |
| 124 | } |
| 125 | |
| 126 | public static function register_block_book_form() { |
| 127 | register_block_type(LATEPOINT_ABSPATH . 'blocks/build/book-form/block.json', |
| 128 | [ |
| 129 | 'render_callback' => 'OsBlockHelper::render_book_form', |
| 130 | 'editor_script_handles' => ['latepoint-block-book-form'] |
| 131 | ]); |
| 132 | } |
| 133 | |
| 134 | public static function register_block_list_of_resources() { |
| 135 | register_block_type(LATEPOINT_ABSPATH . 'blocks/build/list-of-resources/block.json', |
| 136 | [ |
| 137 | 'render_callback' => 'OsBlockHelper::render_list_of_resources', |
| 138 | 'editor_script_handles' => ['latepoint-block-list-of-resources'] |
| 139 | ]); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | public static function register_block_calendar(): void { |
| 144 | register_block_type( LATEPOINT_ABSPATH . 'blocks/build/calendar/block.json', |
| 145 | [ |
| 146 | 'render_callback' => 'OsBlockHelper::render_calendar', |
| 147 | 'editor_script_handles' => [ 'latepoint-block-calendar' ] |
| 148 | ] ); |
| 149 | } |
| 150 | |
| 151 | public static function register_block_customer_dashboard(): void { |
| 152 | register_block_type( LATEPOINT_ABSPATH . 'blocks/build/customer-dashboard/block.json', |
| 153 | [ |
| 154 | 'render_callback' => 'OsBlockHelper::render_customer_dashboard', |
| 155 | 'editor_script_handles' => [ 'latepoint-block-customer-dashboard' ] |
| 156 | ] ); |
| 157 | } |
| 158 | |
| 159 | public static function register_block_customer_login(): void { |
| 160 | register_block_type( LATEPOINT_ABSPATH . 'blocks/build/customer-login/block.json', |
| 161 | [ |
| 162 | 'render_callback' => 'OsBlockHelper::render_customer_login', |
| 163 | 'editor_script_handles' => [ 'latepoint-block-customer-login' ] |
| 164 | ] ); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | public static function render_book_button($attributes, $content) { |
| 169 | return do_shortcode('[latepoint_book_button ' . self::attributes_to_data_params($attributes) . ']'); |
| 170 | } |
| 171 | |
| 172 | public static function render_book_form($attributes, $content) { |
| 173 | return do_shortcode('[latepoint_book_form ' . self::attributes_to_data_params($attributes) . ']'); |
| 174 | } |
| 175 | |
| 176 | public static function add_block_styles_to_page() { |
| 177 | if (is_page() && $styles = get_option('latepoint_blocks_styles_' . get_the_ID())) { |
| 178 | wp_add_inline_style('latepoint-main-front', $styles); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | public static function render_list_of_resources($attributes, $content) { |
| 183 | return do_shortcode('[latepoint_resources ' . self::attributes_to_data_params($attributes) . ']'); |
| 184 | } |
| 185 | |
| 186 | public static function render_calendar($attributes, $content) { |
| 187 | return do_shortcode('[latepoint_calendar ' . self::attributes_to_data_params($attributes) . ']'); |
| 188 | } |
| 189 | |
| 190 | public static function render_customer_dashboard($attributes, $content) { |
| 191 | return do_shortcode('[latepoint_customer_dashboard ' . self::attributes_to_data_params($attributes) . ']'); |
| 192 | } |
| 193 | |
| 194 | public static function render_customer_login($attributes, $content) { |
| 195 | return do_shortcode('[latepoint_customer_login ' . self::attributes_to_data_params($attributes) . ']'); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Prepare data for shortcode |
| 200 | * @param array $settings |
| 201 | * @param array $allowed_params |
| 202 | * |
| 203 | * @return string |
| 204 | */ |
| 205 | public static function attributes_to_data_params( array $settings, array $allowed_params = [] ): string { |
| 206 | $data_html = ''; |
| 207 | foreach ( $settings as $key => $value ) { |
| 208 | if(empty($value)) continue; |
| 209 | |
| 210 | if ( !empty($allowed_params) && ! in_array( $key, $allowed_params ) ) { |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | if ( is_array( $value ) ) { |
| 215 | $value = implode( ',', $value ); |
| 216 | } |
| 217 | if (is_bool($value)) { |
| 218 | $value = $value ? 'yes' : 'no'; |
| 219 | } |
| 220 | |
| 221 | $data_html .= $key . '="' . esc_attr( $value ) . '" '; |
| 222 | } |
| 223 | |
| 224 | return $data_html; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * When save page - save gutenberg blocks styles to options |
| 229 | * @param $page_id |
| 230 | * @return void |
| 231 | */ |
| 232 | public static function save_blocks_styles($page_id): void { |
| 233 | if (wp_is_post_revision($page_id)) return; |
| 234 | |
| 235 | $styles = ''; |
| 236 | |
| 237 | if (has_block('latepoint/list-of-resources', $page_id) || |
| 238 | has_block('latepoint/book-button', $page_id) |
| 239 | ) { |
| 240 | $post_content = get_post_field('post_content', $page_id); |
| 241 | $blocks = parse_blocks($post_content); |
| 242 | $styles = self::generate_styles_for_blocks($blocks); |
| 243 | } |
| 244 | |
| 245 | self::save_blocks_styles_in_options($page_id, $styles); |
| 246 | } |
| 247 | |
| 248 | private static function generate_styles_for_blocks( $blocks ): string { |
| 249 | $styles = ''; |
| 250 | |
| 251 | foreach ( $blocks as $block ) { |
| 252 | switch ( $block['blockName'] ) { |
| 253 | case 'latepoint/list-of-resources': |
| 254 | $styles .= self::generate_styles_for_list_of_resources( $block['attrs'] ); |
| 255 | break; |
| 256 | case 'latepoint/book-button': |
| 257 | $styles .= self::generate_styles_for_book_button( $block['attrs'] ); |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | if ( ! empty( $block['innerBlocks'] ) ) { |
| 262 | $styles .= self::generate_styles_for_blocks( $block['innerBlocks'] ); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return $styles; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Save gutenberg blocks styles in wp options. if styles is empty - delete option |
| 271 | * @param int $page_id |
| 272 | * @param string $styles |
| 273 | * |
| 274 | * @return void |
| 275 | */ |
| 276 | private static function save_blocks_styles_in_options(int $page_id, string $styles = "" ):void { |
| 277 | $option_name = 'latepoint_blocks_styles_' . $page_id; |
| 278 | if (trim($styles) == '') { |
| 279 | if(get_option($option_name)) delete_option($option_name); |
| 280 | } else { |
| 281 | update_option($option_name, $styles); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Styles for gutenberg block "Book Button" |
| 287 | * @param $attrs |
| 288 | * |
| 289 | * @return string |
| 290 | */ |
| 291 | public static function generate_styles_for_book_button($attrs): string { |
| 292 | $styles = []; |
| 293 | if (!empty($attrs['is_inherit'])) return ""; |
| 294 | |
| 295 | $prefix = ".latepoint-book-button.latepoint-book-button-". $attrs['id']; |
| 296 | |
| 297 | $styles[$prefix . "," . $prefix . ":focus"] = [ |
| 298 | 'font-weight' => $attrs['font_weight'] ?? '', |
| 299 | 'text-transform' => $attrs['text_transform'] ?? '', |
| 300 | 'font-family' => $attrs['font_family'] ?? '', |
| 301 | 'line-height' => $attrs['line_height'] ?? '', |
| 302 | 'letter-spacing' => $attrs['letter_spacing'] ?? '', |
| 303 | 'border-color' => $attrs['border_color'] ?? '', |
| 304 | 'border-width' => $attrs['border_width'] ?? '', |
| 305 | 'border-style' => $attrs['border_style'] ?? '', |
| 306 | ]; |
| 307 | |
| 308 | if ($styles[ $prefix]['border-style'] == 'default') { |
| 309 | unset($styles[ $prefix]['border-style']); |
| 310 | } |
| 311 | |
| 312 | $styles[$prefix . ":hover"] = [ |
| 313 | 'background-color' => $attrs['bg_color_hover'] . " !important" ?? "", |
| 314 | 'color' => $attrs['text_color_hover'] . " !important" ?? '', |
| 315 | 'border-color' => $attrs['border_color_hover'] . " !important" ?? '', |
| 316 | ]; |
| 317 | return self::generate_css($styles); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Styles for gutenberg block "List of Resources" |
| 322 | * |
| 323 | * @param array $attrs |
| 324 | * @return string |
| 325 | */ |
| 326 | public static function generate_styles_for_list_of_resources(array $attrs): string { |
| 327 | |
| 328 | $styles = []; |
| 329 | if (!empty($attrs['is_inherit'])) return ""; |
| 330 | |
| 331 | $card_prefix = ".latepoint-resources-items-w .resource-item.resource-item-". $attrs['id']; |
| 332 | |
| 333 | $styles[ $card_prefix . ' .latepoint-book-button' ] = [ |
| 334 | 'background-color' => $attrs['button_bg_color'] ?? "", |
| 335 | 'color' => $attrs['button_text_color'] ?? "", |
| 336 | 'font-size' => $attrs['button_font_size'] ?? "", |
| 337 | 'font-weight' => $attrs['font_weight'] ?? "", |
| 338 | 'text-transform' => $attrs['text_transform'] ?? "", |
| 339 | 'font-family' => $attrs['font_family'] ?? "", |
| 340 | 'line-height' => $attrs['line_height'] ?? "", |
| 341 | 'letter-spacing' => $attrs['letter_spacing'] ?? "", |
| 342 | 'border-radius' => $attrs['button_border_radius'] ?? "", |
| 343 | 'border-color' => $attrs['border_color'] ?? "", |
| 344 | 'border-style' => $attrs['border_style'] ?? "", |
| 345 | 'border-width' => $attrs['border_width'] ?? "", |
| 346 | 'padding' => $attrs['padding'] ?? "" |
| 347 | ]; |
| 348 | |
| 349 | if ($styles[ $card_prefix . ' .latepoint-book-button' ]['border-style'] == 'default') { |
| 350 | unset($styles[ $card_prefix . ' .latepoint-book-button' ]['border-style']); |
| 351 | } |
| 352 | |
| 353 | $styles[ $card_prefix . ' .latepoint-book-button:hover' ] = [ |
| 354 | 'background-color' => $attrs['bg_color_hover'] ?? "", |
| 355 | 'border-color' => $attrs['border_color_hover'] ?? "", |
| 356 | 'color' => $attrs['text_color_hover'] ?? "", |
| 357 | ]; |
| 358 | $styles[ $card_prefix ] = [ |
| 359 | 'padding' => $attrs['card_padding'] ?? "", |
| 360 | 'box-shadow' => $attrs['card_box_shadow'] ?? "", |
| 361 | 'border-style' => $attrs['card_border_style'] ?? "", |
| 362 | 'border-radius' => $attrs['card_border_radius'] ?? "", |
| 363 | 'border-width' => $attrs['card_border_width'] ?? "", |
| 364 | 'border-color' => $attrs['card_border_color'] ?? "", |
| 365 | 'background-color' => $attrs['card_bg_color'] ?? "", |
| 366 | ]; |
| 367 | $styles[ $card_prefix . " .ri-name > *" ] = [ |
| 368 | 'font-size' => $attrs['title_font_size'] ?? "", |
| 369 | 'color' => $attrs['card_text_color'] ?? "" |
| 370 | ]; |
| 371 | $styles[ $card_prefix . ":hover .ri-name > *" ] = [ |
| 372 | 'color' => $attrs['card_text_color_hover'] ?? "" |
| 373 | ]; |
| 374 | $styles[ $card_prefix . " .ri-price" ] = [ |
| 375 | 'font-size' => $attrs['price_font_size'] ?? "", |
| 376 | 'color' => $attrs['card_price_color'] ?? "" |
| 377 | ]; |
| 378 | $styles[ $card_prefix . ":hover .ri-price" ] = [ |
| 379 | 'color' => $attrs['card_price_color_hover'] ?? "" |
| 380 | ]; |
| 381 | $styles[ $card_prefix . " .ri-description" ] = [ |
| 382 | 'font-size' => $attrs['descr_font_size'] ?? "", |
| 383 | 'color' => $attrs['card_descr_color'] ?? "" |
| 384 | ]; |
| 385 | $styles[ $card_prefix . ":hover .ri-description" ] = [ |
| 386 | 'color' => $attrs['card_descr_color_hover'] ?? "" |
| 387 | ]; |
| 388 | |
| 389 | if ($styles[$card_prefix]['card_border_style'] == 'default') { |
| 390 | unset($styles[$card_prefix]['border-style']); |
| 391 | } |
| 392 | |
| 393 | $styles[ $card_prefix . ":hover" ] = [ |
| 394 | 'border-color' => $attrs['card_border_color_hover'] ?? "", |
| 395 | 'background-color' => $attrs['card_bg_color_hover'] ?? "", |
| 396 | 'color' => $attrs['card_text_color_hover'] ?? "", |
| 397 | ]; |
| 398 | $styles[ $card_prefix . ":hover .ri-name h3" ] = [ |
| 399 | 'color' => $attrs['card_text_color_hover'] ?? "" |
| 400 | ]; |
| 401 | $styles[ $card_prefix . " .ri-name h3" ] = [ |
| 402 | 'color' => $attrs['card_text_color'] ?? "" |
| 403 | ]; |
| 404 | return self::generate_css($styles); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Convert array with styles to string |
| 409 | * |
| 410 | * @param array $selectors |
| 411 | * @return string |
| 412 | */ |
| 413 | public static function generate_css(array $selectors): string { |
| 414 | $styling_css = ''; |
| 415 | |
| 416 | foreach ( $selectors as $selector => $options ) { |
| 417 | |
| 418 | $css = ''; |
| 419 | |
| 420 | foreach ( $options as $option_name => $option_val ) { |
| 421 | if ( empty( $option_val ) ) { |
| 422 | continue; |
| 423 | } |
| 424 | $css .= $option_name . ': ' . $option_val . ';'; |
| 425 | } |
| 426 | if ( ! empty( $css ) ) { |
| 427 | $styling_css .= $selector . '{'; |
| 428 | $styling_css .= $css . '}'; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | return $styling_css; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Retrieve Ids by resources |
| 437 | * @param string $resource |
| 438 | * @param array $settings |
| 439 | * |
| 440 | * @return string |
| 441 | */ |
| 442 | public static function get_ids_from_resources(string $resource, array $settings): array |
| 443 | { |
| 444 | $resourceMap = [ |
| 445 | 'services' => 'item_ids_services', |
| 446 | 'locations' => 'item_ids_locations', |
| 447 | 'agents' => 'item_ids_agents', |
| 448 | 'bundles' => 'item_ids_bundles', |
| 449 | ]; |
| 450 | |
| 451 | if (!array_key_exists($resource, $resourceMap)) { |
| 452 | return []; |
| 453 | } |
| 454 | |
| 455 | $idsKey = $resourceMap[$resource]; |
| 456 | |
| 457 | return !empty($settings[$idsKey]) ? $settings[$idsKey] : []; |
| 458 | } |
| 459 | |
| 460 | } |