PluginProbe
Booking Calendar / 11.6.1
Booking Calendar v11.6.1
11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 All 203 releases
booking / includes / publish / wpbc-create-pages.php

wpbc-create-pages.php in Booking Calendar 11.6.1, at includes/publish/wpbc-create-pages.php

1,184 lines 43.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version 1.0
4 * @package Booking Calendar
5 * @subpackage Create pages Functions
6 * @category Functions
7 *
8 * @author wpdevelop
9 * @link https://wpbookingcalendar.com/
10 * @email info@wpbookingcalendar.com
11 *
12 * @modified 2023-05-10
13 * @file ../includes/publish/wpbc-create-pages.php
14 */
15
16 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
17
18
19 /**
20 * Create page for Booking Calendar
21 *
22 * @param array $page_params = array(
23 'post_content' => '[bookingedit]',
24 'post_name' => 'wpbc-booking-edit',
25 'post_title' => esc_html__('Booking edit','booking')
26 )
27 *
28 * @return false|int - ID of the page.
29 */
30 function wpbc_create_page( $page_params = array() ){ // FixIn: 9.6.2.10.
31
32 /* Live demos must never create WordPress content through Booking Calendar. */
33 if ( wpbc_is_booking_form_publishing_restricted() ) {
34 return false;
35 }
36
37 global $wp_rewrite;
38 if ( is_null( $wp_rewrite ) ) { // FixIn: 9.7.1.1.
39 return false;
40 }
41
42 // Create post object
43 $defaults = array(
44 //'menu_order' => 0, // The order the post should be displayed in. Default 0. If new post is a page, it sets the order in which it should appear in the tabs.
45 //'pinged' => '', // Space or carriage return-separated list of URLs that have been pinged. Default empty.
46 //'post_author' => get_current_user_id(), // The user ID number of the author. The ID of the user who added the post. Default is the current user ID.
47 //'post_category' => array(), // Array of category IDs. Defaults to value of the 'default_category' option. post_category no longer exists, try wp_set_post_terms() for setting a post's categories
48 //'post_date' => [ Y-m-d H:i:s ] // The date of the post. Default is the current time. The time post was made.
49 //'post_date_gmt' => [ Y-m-d H:i:s ] // The time post was made, in GMT.
50 //'post_excerpt' => '' //The post excerpt. Default empty. For all your post excerpt needs.
51 //'post_parent' => 0, // Set this for the post it belongs to, if any. Default 0. Sets the parent of the new post.
52 //'post_password' => '', //The password to access the post. Default empty. password for post?
53 //'tags_input' => '', // Array of tag names, slugs, or IDs. Default empty. [ '<tag>, <tag>, <...>' ] //For tags.
54 //'to_ping' => '', // Space or carriage return-separated list of URLs to ping. Default empty. Space or carriage return-separated list of URLs to ping. Default empty.
55 //'tax_input' => '', // Default empty. [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies.
56 'ID' => 0, // The post ID. If equal to something other than 0, the post with that ID will be updated. Default 0.
57 'post_type' => 'page', // The post type. Default 'post'. [ 'post' | 'page' | 'link' | 'nav_menu_item' | 'custom_post_type' ] //You may want to insert a regular post, page, link, a menu item or some custom post type
58 'post_status' => 'publish', // [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | 'custom_registered_status' ] // The post status. Default 'draft'. Set the status of the new post.
59 'comment_status' => 'closed', // [ 'closed' | 'open' ] // 'closed' means no comments.
60 'ping_status' => 'closed', // [ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off
61 'post_content' => '[bookingedit]', // The post content. Default empty. The full text of the post.
62 'post_name' => 'wpbc-booking-edit', // sanitize_title( $post_title ), -- By default, converts accent characters to ASCII characters and further limits the output to alphanumeric characters, underscore (_) and dash (-) // The post name. Default is the sanitized post title when creating a new post. The name (slug) for your post
63 'post_title' => esc_html__('Booking edit','booking') // The post title. Default empty. The title of your post.
64 );
65
66
67 $my_post = wp_parse_args( $page_params, $defaults );
68
69
70 $post_id = wp_insert_post( $my_post, true ); // Insert the post into the database
71
72 if ( ( ! is_wp_error( $post_id ) ) && ( ! empty( $post_id ) ) ) {
73
74 // Success
75
76 // $post = get_post( $post_id );
77 // $post_url = get_permalink( $post_id );
78 wpbc_try_assign_full_width_template( $post_id,
79 array(
80 'excluded_title_parts' => array( 'wide image' ),
81 'excluded_classic_template_files' => array( 'elementor_header_footer' ),
82 'force_elementor_default_template' => true,
83 ) );
84
85 } else {
86 $post_id = false;
87 }
88
89 return $post_id;
90 }
91
92
93 /**
94 * Check whether a template label looks like a usable "Full Width" template.
95 *
96 * Excludes labels that contain unwanted fragments such as "wide image".
97 *
98 * @param string $template_label Human-readable template label.
99 * @param array $excluded_fragments Lowercase fragments to exclude.
100 *
101 * @return bool
102 */
103 function wpbc_is_full_width_template_candidate( $template_label, $excluded_fragments = array() ) {
104
105 $template_label = strtolower( wp_strip_all_tags( (string) $template_label ) );
106
107 if (
108 ( false === strpos( $template_label, 'full' ) ) ||
109 ( false === strpos( $template_label, 'width' ) )
110 ) {
111 return false;
112 }
113
114 foreach ( $excluded_fragments as $excluded_fragment ) {
115 $excluded_fragment = strtolower( (string) $excluded_fragment );
116
117 if ( '' === $excluded_fragment ) {
118 continue;
119 }
120
121 if ( false !== strpos( $template_label, $excluded_fragment ) ) {
122 return false;
123 }
124 }
125
126 return true;
127 }
128
129
130 /**
131 * Attempt to assign a "Full Width" template to a page.
132 *
133 * Supports both classic page templates and block theme templates.
134 * Can skip specific templates, such as Elementor "Elementor Full Width"
135 * or templates containing "Wide Image".
136 *
137 * @param int $post_id Page ID.
138 * @param array $args Optional arguments:
139 * - excluded_title_parts array Fragments excluded from template labels.
140 * - excluded_classic_template_files array Classic template file names to skip.
141 * - force_elementor_default_template bool Whether to force Elementor "Theme/Default".
142 *
143 * @return bool True if a template was assigned, or Elementor default was forced as fallback.
144 */
145 function wpbc_try_assign_full_width_template( $post_id, $args = array() ) {
146
147 if ( empty( $post_id ) || is_wp_error( $post_id ) ) {
148 return false;
149 }
150
151 $post = get_post( $post_id );
152
153 if ( ( ! $post ) || ( 'page' !== $post->post_type ) ) {
154 return false;
155 }
156
157 $defaults = array(
158 'excluded_title_parts' => array( 'wide image' ),
159 'excluded_classic_template_files' => array( 'elementor_header_footer' ),
160 'force_elementor_default_template' => false,
161 );
162
163 $args = wp_parse_args( $args, $defaults );
164
165 $excluded_title_parts = array_map( 'strtolower', (array) $args['excluded_title_parts'] );
166 $excluded_classic_template_files = array_map( 'strtolower', (array) $args['excluded_classic_template_files'] );
167
168 $is_elementor_full_width_skipped = false;
169
170 // -------------------------------------------------------------------------
171 // Classic themes: scan page templates.
172 // get_page_templates() returns template file names keyed by template header.
173 // -------------------------------------------------------------------------
174 if ( function_exists( 'get_page_templates' ) ) {
175
176 $classic_templates = get_page_templates( $post, 'page' );
177
178 foreach ( $classic_templates as $template_name => $template_file ) {
179
180 if ( ! wpbc_is_full_width_template_candidate( $template_name, $excluded_title_parts ) ) {
181 continue;
182 }
183
184 $template_file_lc = strtolower( (string) $template_file );
185
186 if ( in_array( $template_file_lc, $excluded_classic_template_files, true ) ) {
187
188 if ( 'elementor_header_footer' === $template_file_lc ) {
189 $is_elementor_full_width_skipped = true;
190 }
191
192 continue;
193 }
194
195 update_post_meta( $post_id, '_wp_page_template', $template_file );
196
197 return true;
198 }
199 }
200
201 // -------------------------------------------------------------------------
202 // Block themes: scan block templates for pages.
203 // -------------------------------------------------------------------------
204 if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() && function_exists( 'get_block_templates' ) ) {
205
206 $theme_slug = get_stylesheet();
207 $templates = get_block_templates(
208 array(
209 'post_type' => 'page',
210 ),
211 'wp_template'
212 );
213
214 foreach ( $templates as $template ) {
215
216 if ( empty( $template->title ) || empty( $template->slug ) ) {
217 continue;
218 }
219
220 if ( $theme_slug !== $template->theme ) {
221 continue;
222 }
223
224 if ( ! wpbc_is_full_width_template_candidate( $template->title, $excluded_title_parts ) ) {
225 continue;
226 }
227
228 wp_update_post(
229 array(
230 'ID' => $post_id,
231 'page_template' => $template->slug,
232 )
233 );
234
235 return true;
236 }
237 }
238
239 /*
240 * If the only match we found was Elementor Full Width, optionally force
241 * Elementor's default page template instead.
242 */
243 if ( $args['force_elementor_default_template'] && $is_elementor_full_width_skipped ) {
244 return wpbc_set_elementor_default_page_template( $post_id );
245 }
246
247 return false;
248 }
249
250
251 /**
252 * Set Elementor page layout to its default "Theme" template.
253 *
254 * This should be treated as a fallback for Elementor-managed pages.
255 *
256 * @param int $post_id Page ID.
257 *
258 * @return bool
259 */
260 function wpbc_set_elementor_default_page_template( $post_id ) {
261
262 if ( empty( $post_id ) || is_wp_error( $post_id ) ) {
263 return false;
264 }
265
266 // Make sure WordPress itself does not have a forced classic template.
267 delete_post_meta( $post_id, '_wp_page_template' );
268
269 // If Elementor is not active, nothing else to do.
270 if ( ( ! defined( 'ELEMENTOR_VERSION' ) ) && ( ! did_action( 'elementor/loaded' ) ) ) {
271 return true;
272 }
273
274 $elementor_page_settings = get_post_meta( $post_id, '_elementor_page_settings', true );
275
276 if ( ! is_array( $elementor_page_settings ) ) {
277 $elementor_page_settings = array();
278 }
279
280 $elementor_page_settings['default_page_template'] = 'default';
281
282 update_post_meta( $post_id, '_elementor_page_settings', $elementor_page_settings );
283
284 return true;
285 }
286
287
288 /**
289 * Is shortcode or some text exist in specific page
290 *
291 * @param string $relative_url relative URL of the page, if we have absolute url, then get it using: wpbc_make_link_relative( 'https://...' );
292 * @param string $shortcode_to_add shortcode to check '[booking'
293 *
294 * @return bool
295 */
296 function wpbc_is_shortcode_exist_in_page( $relative_url, $shortcode_to_add ) {
297
298 if ( ! empty( $relative_url ) ) {
299
300 $post_obj = get_page_by_path( $relative_url );
301
302 if ( ! empty( $post_obj ) ) {
303 if ( false !== strpos( $post_obj->post_content, $shortcode_to_add ) ) {
304 return true;
305 }
306 }
307 }
308
309 return false;
310 }
311
312 /**
313 * Is shortcode or some text exist in specific page (got by ID)
314 *
315 * @param string $relative_url relative URL of the page, if we have absolute url, then get it using: wpbc_make_link_relative( 'https://...' );
316 * @param string $shortcode_to_add shortcode to check '[booking'
317 *
318 * @return bool
319 */
320 function wpbc_is_shortcode_exist_in_page_with_id( $page_id, $shortcode_to_add ) {
321
322 // FixIn: 10.12.3.1.
323
324 if ( ! empty( $page_id ) ) {
325
326 $post_obj = get_post( $page_id );
327
328 if ( ! empty( $post_obj ) ) {
329 if ( false !== strpos( $post_obj->post_content, $shortcode_to_add ) ) {
330 return true;
331 }
332 }
333 }
334
335 return false;
336 }
337
338
339 /**
340 * Add shortcode, if it does not exist yet, to the page
341 *
342 * @param string $relative_url relative URL of the page, if we have absolute url, then get it using: wpbc_make_link_relative( 'https://...' );
343 * @param string $shortcode_to_add shortcode '[bookingedit]'
344 *
345 * @return bool
346 */
347 function wpbc_add_shortcode_to_exist_page( $relative_url, $shortcode_to_add ) {
348
349 if ( ! empty( $relative_url ) ) {
350
351 $post_obj = get_page_by_path( $relative_url );
352
353 if ( ! empty( $post_obj ) ) {
354 if ( false === strpos( $post_obj->post_content, $shortcode_to_add ) ) { // No such shortcode in this page. So we need to Add it.
355
356 $my_post = array(
357 'ID' => $post_obj->ID,
358 'post_content' => $post_obj->post_content . "\r\n" . $shortcode_to_add
359 );
360 wp_update_post( $my_post ); // Update the post into the database
361
362 return true;
363 }
364 }
365 }
366
367 return false;
368 }
369
370
371 // ---------------------------------------------------------------------------------------------------------------------
372
373 /**
374 * Create new page or add to existing page the shortcode
375 *
376 * @param array $params
377 *
378 * @return array Success or failure response. Publishing restrictions return a
379 * failure before WordPress page lookup or mutation.
380 *
381 * Example:
382 * $result_arr = wpbc_add_shortcode_into_page( array(
383 * 'shortcode' => '[booking resource_id=1]',
384 * 'post_title' => 'Booking Form'
385 * ) );
386 */
387 function wpbc_add_shortcode_into_page( $params = array() ) {
388 /* Enforce the publishing policy at the canonical writer for every caller. */
389 if ( wpbc_is_booking_form_publishing_restricted() ) {
390 return array(
391 'result' => false,
392 'message' => __( 'In the demo versions this operation is not allowed.', 'booking' ),
393 );
394 }
395
396 $defaults = array(
397 'page_post_name' => '', // 'wpbc-booking',
398 'post_title' => esc_html( __( 'Booking Form', 'booking' ) ),
399 'shortcode' => '[booking resource_id=1]',
400 'check_exist_shortcode' => '[booking', // can be an array: array( '[booking resource_id=1]', '[booking type=1]', '[booking]' )
401 'page_id' => 0,
402 'resource_id' => 0 // Optional
403 );
404 $params = wp_parse_args( $params, $defaults );
405
406 if ( empty( $params['page_post_name'] ) ) {
407 $params['page_post_name'] = sanitize_title( $params['post_title'] ); // Get slug for the page
408 }
409
410
411 global $wp_rewrite;
412 if ( is_null( $wp_rewrite ) ) {
413 return array( 'result' => false, 'message' => 'Sorry, we are unable to insert the shortcode into the page. The reason is that wp_rewrite is not initialized.' );
414 }
415 // -----------------------------------------------------------------------------------------------------------------
416
417 $params['page_id'] = intval( $params['page_id'] );
418 if ( ! empty( $params['page_id'] ) ) {
419 $wp_post = get_post( $params['page_id'] );
420 } else {
421 $wp_post = get_page_by_path( $params['page_post_name'] );
422 }
423
424 $relative_post_url = '';
425
426 $post_title = '';
427 $post_url = '';
428 if ( ! empty( $wp_post ) ) {
429 $post_title = $wp_post->post_title;
430 $post_url = get_permalink( $wp_post->ID );
431 }
432
433 if ( empty( $wp_post ) ) { // No default page. Create it.
434
435 $page_params = array(
436 'post_title' => $params['post_title'],
437 'post_content' => $params['shortcode'],
438 'post_name' => $params['page_post_name']
439 );
440 $post_id = wpbc_create_page( $page_params );
441
442 if ( ! empty( $post_id ) ) {
443
444 $new_post = get_post( $post_id );
445 if ( ! empty( $new_post ) ) {
446 $post_title = $new_post->post_title;
447 $post_url = get_permalink( $new_post->ID );
448 }
449
450 $relative_post_url = wpbc_make_link_relative( get_permalink( $post_id ) );
451
452 // Scroll to the booking form.
453 if ( ! empty( $params['resource_id'] ) ) {
454 $post_url .= '#bklnk' . $params['resource_id'];
455 }
456
457 return array( 'result' => true,
458 'relative_url' => $relative_post_url,
459 /* translators: 1: ... */
460 'message' => __( 'A new page has been created.', 'booking' ) . ' ' . sprintf( __( 'The booking form shortcode %1$s has been embedded into the page %2$s', 'booking' )
461 , "<code class='wpbc_inserted_shortcode_view'>{$params['shortcode']}</code>"
462 , "<a class='wpbc_open_page_as_new_tab' href='" . esc_url( $post_url ) . "'>{$post_title}</a>"
463 . " <a target='_blank' class='wpbc_open_page_as_new_tab tooltip_top wpbc-bi-arrow-up-right-square' href='" . esc_url( $post_url ) . "' title='" . esc_attr( __( 'Open in new window', 'booking' ) ) . "'></a>"
464 )
465 );
466 }
467
468 } else {
469 $relative_post_url = wpbc_make_link_relative( get_permalink( $wp_post->ID ) ); // Page already exist, so we need to update the
470 }
471
472 // Check if the shortcode in the page
473 $is_shortcode_already_in_page = false;
474 if ( is_array( $params['check_exist_shortcode'] ) ) {
475 foreach ( $params['check_exist_shortcode'] as $check_shortcode ) {
476 $is_shortcode_already_in_page = wpbc_is_shortcode_exist_in_page( $relative_post_url, $check_shortcode );
477 if ( $is_shortcode_already_in_page ) {
478 break;
479 }
480 }
481 } else {
482 $is_shortcode_already_in_page = wpbc_is_shortcode_exist_in_page( $relative_post_url, $params['check_exist_shortcode'] );
483 }
484
485 // Scroll to the booking form.
486 if ( ! empty( $params['resource_id'] ) ) {
487 $post_url .= '#bklnk' . $params['resource_id'];
488 }
489
490 // Check if existing page has our shortcode. We are checking for 'booking' because it can be '[booking]' or '[booking type=1]' ...
491 if (
492 ( ! $is_shortcode_already_in_page )
493 && ( ! wpbc_is_shortcode_exist_in_page( $relative_post_url, $params['shortcode'] ) )
494 ) {
495 $is_sh_added = wpbc_add_shortcode_to_exist_page( $relative_post_url, $params['shortcode'] );
496
497 if ( $is_sh_added ) {
498 return array( 'result' => true,
499 'relative_url' => $relative_post_url,
500 /* translators: 1: ... */
501 'message' => sprintf( __( 'The booking form shortcode %1$s has been embedded into the page %2$s', 'booking' )
502 , "<code class='wpbc_inserted_shortcode_view'>{$params['shortcode']}</code>"
503 , "<a class='wpbc_open_page_as_new_tab' href='" . esc_url( $post_url ) . "'>{$post_title}</a>"
504 . " <a target='_blank' class='wpbc_open_page_as_new_tab tooltip_top wpbc-bi-arrow-up-right-square' href='" . esc_url( $post_url ) . "' title='" . esc_attr( __( 'Open in new window', 'booking' ) ) . "'></a>"
505
506 )
507 );
508
509 } else {
510 return array( 'result' => false,
511 /* translators: 1: ... */
512 'message' => sprintf( __( 'We are unable to embed the booking form shortcode %1$s into the page %2$s.', 'booking' )
513 , "<code class='wpbc_inserted_shortcode_view'>{$params['shortcode']}</code>"
514 , "<a class='wpbc_open_page_as_new_tab' href='" . esc_url( $post_url ) . "'>{$post_title}</a>"
515 . " <a target='_blank' class='wpbc_open_page_as_new_tab tooltip_top wpbc-bi-arrow-up-right-square' href='" . esc_url( $post_url ) . "' title='" . esc_attr( __( 'Open in new window', 'booking' ) ) . "'></a>"
516 )
517 );
518 }
519 } else {
520
521 return array( 'result' => false,
522 /* translators: 1: ... */
523 'message' => sprintf( __( 'The Booking Calendar shortcode %1$s is already present on this page %2$s.', 'booking' )
524 , "<code class='wpbc_inserted_shortcode_view'>{$params['shortcode']}</code>"
525 , "<a class='wpbc_open_page_as_new_tab' href='" . esc_url( $post_url ) . "'>{$post_title}</a>"
526 . " <a target='_blank' class='wpbc_open_page_as_new_tab tooltip_top wpbc-bi-arrow-up-right-square' href='" . esc_url( $post_url ) . "' title='" . esc_attr( __( 'Open in new window', 'booking' ) ) . "'></a>"
527 )
528 );
529
530 }
531
532 }
533
534
535 // ---------------------------------------------------------------------------------------------------------------------
536 // ---------------------------------------------------------------------------------------------------------------------
537 // ---------------------------------------------------------------------------------------------------------------------
538
539
540 /**
541 * Get starter booking form/page definitions created during plugin activation.
542 *
543 * @return array
544 */
545 function wpbc_get_activation_booking_form_page_configs() {
546
547 return array(
548 'full_day_booking' => array(
549 'template_key' => 'dates_form_with_inline_hints',
550 'form_slug' => 'full_day_booking',
551 'form_title' => esc_html__( 'Full Day Booking Form', 'booking' ),
552 'page_slug' => 'wp-booking-calendar-full-day',
553 'page_title' => esc_html__( 'Full Day Booking', 'booking' ),
554 'button_title' => esc_html__( 'Full day booking form', 'booking' ),
555 ),
556 'time_slots_booking' => array(
557 'template_key' => 'time_slots_20_min_3_steps_review_with_hints',
558 'form_slug' => 'time_slots_booking',
559 'form_title' => esc_html__( 'Time Slots Booking Form', 'booking' ),
560 'page_slug' => 'wp-booking-calendar-time-slots',
561 'page_title' => esc_html__( 'Time Slots Booking', 'booking' ),
562 'button_title' => esc_html__( 'Time slots booking form', 'booking' ),
563 ),
564 'time_appointments_booking' => array(
565 'template_key' => 'time_appointments_3_steps_review_with_hints',
566 'form_slug' => 'time_appointments_booking',
567 'form_title' => esc_html__( 'Time Appointments Booking Form', 'booking' ),
568 'create_page' => false,
569 ),
570 'appointment_services_booking' => array(
571 'template_key' => 'appointments_services_flow',
572 'form_slug' => 'appointment_services_booking',
573 'form_title' => esc_html__( 'Appointment Services Booking Form', 'booking' ),
574 'create_page' => false,
575 'assign_to_default_appointment_service' => true,
576 ),
577 'contact_form' => array(
578 'template_key' => 'contact_form_simple',
579 'form_slug' => 'contact_form',
580 'form_title' => esc_html__( 'Contact Form', 'booking' ),
581 'page_slug' => 'wp-booking-calendar-contact',
582 'page_title' => esc_html__( 'Contact Form', 'booking' ),
583 'button_title' => esc_html__( 'Contact form', 'booking' ),
584 ),
585 );
586 }
587
588
589 /**
590 * Get starter page definitions created during plugin activation.
591 *
592 * Legacy starter pages keep using their dedicated custom booking forms. The
593 * old Time Appointments form and the dedicated Appointment Service form are
594 * form-only fixtures because the Appointment workflow has one canonical page.
595 *
596 * @return array Starter page definitions keyed by stable purpose.
597 */
598 function wpbc_get_activation_booking_page_configs() {
599
600 $page_configs = wpbc_get_activation_booking_form_page_configs();
601
602 foreach ( $page_configs as $page_key => $page_config ) {
603 if ( isset( $page_config['create_page'] ) && false === (bool) $page_config['create_page'] ) {
604 unset( $page_configs[ $page_key ] );
605 continue;
606 }
607
608 $form_slug = '';
609 if ( isset( $page_config['form_slug'] ) ) {
610 $form_slug = sanitize_text_field( (string) $page_config['form_slug'] );
611 }
612
613 if ( '' === $form_slug ) {
614 unset( $page_configs[ $page_key ] );
615 continue;
616 }
617
618 $shortcode = "[booking resource_id=1 form_type='{$form_slug}']";
619 $page_configs[ $page_key ]['shortcode'] = $shortcode;
620 $page_configs[ $page_key ]['shortcode_checks'] = array(
621 $shortcode,
622 '[booking resource_id=1 form_type="' . $form_slug . '"]',
623 );
624 $page_configs[ $page_key ]['resource_id'] = 1;
625 }
626
627 $page_configs['appointment_booking'] = array(
628 'page_slug' => 'wpbc-appointment-booking',
629 'page_title' => esc_html__( 'Book an Appointment', 'booking' ),
630 'button_title' => esc_html__( 'Appointment booking form', 'booking' ),
631 'shortcode' => '[booking_appointment]',
632 'shortcode_checks' => array( '[booking_appointment]', '[booking_appointment ' ),
633 'resource_id' => 0,
634 );
635
636 return $page_configs;
637 }
638
639
640 /**
641 * Get a bundled BFB template record by template key.
642 *
643 * @param string $template_key Template key.
644 *
645 * @return array
646 */
647 function wpbc_get_bfb_template_record_by_key( $template_key ) {
648
649 if (
650 ! function_exists( 'wpbc_bfb_activation__get_templates_registry' ) ||
651 ! function_exists( 'wpbc_bfb_activation__normalize_template_config' )
652 ) {
653 return array();
654 }
655
656 $template_key = sanitize_key( (string) $template_key );
657 $templates = wpbc_bfb_activation__get_templates_registry();
658
659 if ( empty( $templates ) || ! is_array( $templates ) ) {
660 return array();
661 }
662
663 foreach ( $templates as $template_config ) {
664
665 $template_config = wpbc_bfb_activation__normalize_template_config( $template_config );
666
667 if ( $template_key === $template_config['template_key'] ) {
668 return $template_config['record'];
669 }
670 }
671
672 return array();
673 }
674
675
676 /**
677 * Create starter custom booking forms from bundled BFB templates.
678 *
679 * @return bool
680 */
681 function wpbc_create_activation_custom_booking_forms() {
682
683 if (
684 ! function_exists( 'wpbc_is_table_exists' ) ||
685 ! wpbc_is_table_exists( 'booking_form_structures' ) ||
686 ! class_exists( 'WPBC_BFB_Form_Storage' )
687 ) {
688 return false;
689 }
690
691 $configs = wpbc_get_activation_booking_form_page_configs();
692 if ( empty( $configs ) || ! is_array( $configs ) ) {
693 return false;
694 }
695
696 $is_created = false;
697
698 foreach ( $configs as $config ) {
699
700 $form_slug = sanitize_text_field( (string) $config['form_slug'] );
701 if ( '' === $form_slug ) {
702 continue;
703 }
704
705 $existing_form = WPBC_BFB_Form_Storage::get_current_form_by_key( $form_slug, 0, 'published' );
706 if ( ! empty( $existing_form ) ) {
707 continue;
708 }
709
710 $template_record = wpbc_get_bfb_template_record_by_key( $config['template_key'] );
711 if ( empty( $template_record ) ) {
712 continue;
713 }
714
715 // The predefined Time Slots form must select one calendar day independently, // FixIn: 11.4.3.1
716 // from the global/default Calendar Settings day-selection mode.
717 if ( 'time_slots_booking' === $form_slug ) {
718 $form_settings = array();
719 if ( ! empty( $template_record['settings_json'] ) ) {
720 $decoded_settings = json_decode( (string) $template_record['settings_json'], true );
721 if ( is_array( $decoded_settings ) ) {
722 $form_settings = $decoded_settings;
723 }
724 }
725
726 if ( empty( $form_settings['options'] ) || ! is_array( $form_settings['options'] ) ) {
727 $form_settings['options'] = array();
728 }
729
730 $form_settings['options']['booking_type_of_day_selections'] = 'single';
731 $template_record['settings_json'] = function_exists( 'wpbc_form_config__encode_json' )
732 ? wpbc_form_config__encode_json( $form_settings )
733 : wp_json_encode( $form_settings );
734 }
735
736 $template_record['form_slug'] = $form_slug;
737 $template_record['status'] = 'published';
738 $template_record['scope'] = 'global';
739 $template_record['owner_user_id'] = 0;
740 $template_record['booking_resource_id'] = null;
741 $template_record['is_default'] = 0;
742 $template_record['title'] = $config['form_title'];
743 $template_record['picture_url'] = isset( $template_record['picture_url'] ) ? (string) $template_record['picture_url'] : '';
744 if ( function_exists( 'wpbc_bfb_resolve_picture_url' ) ) {
745 $template_record['picture_url'] = wpbc_bfb_resolve_picture_url( $template_record['picture_url'] );
746 }
747
748 $booking_form_id = WPBC_BFB_Form_Storage::save_form( $template_record );
749 if ( ! empty( $booking_form_id ) ) {
750 $is_created = true;
751 }
752 }
753
754 return $is_created;
755 }
756
757
758 /**
759 * Build starter page content with a booking shortcode.
760 *
761 * @param string $shortcode Booking shortcode.
762 *
763 * @return string
764 */
765 function wpbc_get_activation_booking_page_content( $shortcode ) {
766
767 $shortcode = trim( (string) $shortcode );
768
769 if ( WPBC_IS_PLAYGROUND ) {
770 $shortcode = '<style type="text/css"> h1, h2, h3, h4, h5, h6 { font-weight: 500; font-family: var(--wp--preset--font-family--body); } </style>' . $shortcode;
771 }
772
773 return $shortcode;
774 }
775
776
777 /**
778 * Create new starter pages with booking forms.
779 *
780 * @param array $default_options_to_add Unused. Kept for backward-compatible direct calls.
781 *
782 * @return void
783 */
784 function wpbc_create_page_with_booking_form( $default_options_to_add = array() ) { // FixIn: 9.6.2.11.
785
786 global $wp_rewrite;
787 if ( is_null( $wp_rewrite ) ) { // FixIn: 9.7.1.1.
788
789 // Maybe it was not init, yet.
790 if ( ! has_action( 'init', 'wpbc_create_page_with_booking_form' ) ) {
791 add_action( 'init', 'wpbc_create_page_with_booking_form', 99 ); // <- priority to load it last
792 }
793
794 return false;
795 }
796
797 // -----------------------------------------------------------------------------------------------------------------
798
799 wpbc_create_activation_custom_booking_forms();
800
801 /**
802 * Fires after activation starter custom forms are available.
803 *
804 * Appointment Services uses this boundary to finish its pending starter
805 * Service seed with the newly created form ID.
806 *
807 * @since 11.5.0
808 */
809 do_action( 'wpbc_activation_custom_booking_forms_created' );
810
811 $configs = wpbc_get_activation_booking_page_configs();
812 if ( empty( $configs ) || ! is_array( $configs ) ) {
813 return;
814 }
815
816 $front_page_id = 0;
817
818 foreach ( $configs as $config ) {
819
820 $shortcode = isset( $config['shortcode'] ) ? trim( (string) $config['shortcode'] ) : '';
821 $shortcode_checks = isset( $config['shortcode_checks'] ) && is_array( $config['shortcode_checks'] )
822 ? $config['shortcode_checks']
823 : array( $shortcode );
824 $resource_id = isset( $config['resource_id'] ) ? absint( $config['resource_id'] ) : 0;
825
826 if ( '' === $shortcode ) {
827 continue;
828 }
829
830 $content = wpbc_get_activation_booking_page_content( $shortcode );
831
832 $result_arr = wpbc_add_shortcode_into_page(
833 array(
834 'page_post_name' => $config['page_slug'],
835 'post_title' => $config['page_title'],
836 'shortcode' => $content,
837 'check_exist_shortcode' => $shortcode_checks,
838 'resource_id' => $resource_id,
839 )
840 );
841
842 if ( WPBC_IS_PLAYGROUND && empty( $front_page_id ) && ! empty( $result_arr['relative_url'] ) ) {
843 $wp_post = get_page_by_path( $config['page_slug'] );
844 if ( ! empty( $wp_post ) ) {
845 $front_page_id = $wp_post->ID;
846 }
847 }
848 }
849
850 if ( WPBC_IS_PLAYGROUND && ! empty( $front_page_id ) ) {
851 update_option( 'show_on_front', 'page' );
852 update_option( 'page_on_front', $front_page_id );
853 }
854 }
855 add_action( 'wpbc_bfb_activation__form_structures_table__after_create', 'wpbc_create_page_with_booking_form', 20 );
856 add_action( 'wpbc_bfb_activation__form_structures_table__table_already_exists', 'wpbc_create_page_with_booking_form', 20 );
857
858
859 /**
860 * Get published starter pages that contain their expected booking form shortcodes.
861 *
862 * @return array
863 */
864 function wpbc_get_published_activation_booking_pages() {
865
866 $pages = array();
867 $configs = wpbc_get_activation_booking_page_configs();
868
869 if ( empty( $configs ) || ! is_array( $configs ) ) {
870 return $pages;
871 }
872
873 foreach ( $configs as $key => $config ) {
874
875 $wp_post = get_page_by_path( $config['page_slug'] );
876 if ( empty( $wp_post ) ) {
877 continue;
878 }
879
880 $shortcode_checks = isset( $config['shortcode_checks'] ) && is_array( $config['shortcode_checks'] )
881 ? $config['shortcode_checks']
882 : array();
883 $is_shortcode_found = false;
884
885 foreach ( $shortcode_checks as $shortcode_check ) {
886 if ( '' === (string) $shortcode_check ) {
887 continue;
888 }
889
890 if ( wpbc_is_shortcode_exist_in_page_with_id( $wp_post->ID, $shortcode_check ) ) {
891 $is_shortcode_found = true;
892 break;
893 }
894 }
895
896 if ( ! $is_shortcode_found ) {
897 continue;
898 }
899
900 $post_url = get_permalink( $wp_post->ID );
901 if ( empty( $post_url ) ) {
902 continue;
903 }
904
905 $pages[ $key ] = array(
906 'url' => $post_url,
907 'button_title' => $config['button_title'],
908 'page_title' => $config['page_title'],
909 );
910 }
911
912 return $pages;
913 }
914
915 // ---------------------------------------------------------------------------------------------------------------------
916
917
918 /**
919 * Create new page and get URL of this page
920 *
921 * @param $default_options_to_add
922 *
923 * @return void
924 */
925 function wpbc_create_page_thank_you( $default_options_to_add ) { // FixIn: 9.6.2.11.
926
927 global $wp_rewrite;
928 if ( is_null( $wp_rewrite ) ) { // FixIn: 9.7.1.1.
929
930 // Maybe it was not init, yet
931 if ( ! has_action( 'init', 'wpbc_create_page_thank_you' ) ) {
932 add_action( 'init', 'wpbc_create_page_thank_you', 99 ); // <- priority to load it last
933 }
934 return false;
935 }
936
937 // -----------------------------------------------------------------------------------------------------------------
938
939 $thank_you_page_url = get_bk_option( 'booking_thank_you_page_URL' );
940
941 if ( ( empty( $thank_you_page_url ) ) // If No 'Thank you' page in Settings, or it's set as Empty.
942 || (
943 ( '/thank-you' == wpbc_make_link_relative( get_bk_option( 'booking_thank_you_page_URL' ) ) )
944 && ( empty( get_page_by_path( 'thank-you' ) ) ) // FixIn: 9.9.0.27.
945 )
946 || ( '/' == wpbc_make_link_relative( get_bk_option( 'booking_thank_you_page_URL' ) ) )
947 ){
948 $wp_post = get_page_by_path( 'wpbc-booking-received' );
949
950 $post_url = '';
951
952 if ( empty( $wp_post ) ) { // No default page. Create it.
953
954 $page_params = array(
955 'post_title' => esc_html( __( 'Booking Received', 'booking' ) ),
956 'post_content' => esc_html( __( 'Thank you for your booking. Your booking has been successfully received.', 'booking' ) )
957 . "\r\n" . ' [booking_confirm]',
958 'post_name' => 'wpbc-booking-received'
959 );
960 $post_id = wpbc_create_page( $page_params );
961
962 if ( ! empty( $post_id ) ) {
963 $post_url = wpbc_make_link_relative( get_permalink( $post_id ) );
964 }
965
966 } else {
967 $post_url = wpbc_make_link_relative( get_permalink( $wp_post->ID ) ); // Page already exist, so we need to update the
968 }
969
970 if ( ! empty( $post_url ) ) {
971 update_bk_option( 'booking_thank_you_page_URL', $post_url );
972 }
973 }
974
975 // -----------------------------------------------------------------------------------------------------------------
976
977 // Check if existing page has our shortcode
978 $relative_url = wpbc_make_link_relative( get_bk_option( 'booking_thank_you_page_URL' ) );
979 $is_sh_added = wpbc_add_shortcode_to_exist_page( $relative_url, '[booking_confirm]' );
980
981 }
982 add_bk_action( 'wpbc_before_activation__add_options', 'wpbc_create_page_thank_you' );
983
984
985
986 /**
987 * Create pages for [bookingedit] and [bookingcustomerlisting] shortcodes,
988 *
989 * if previously was not defined options
990 * 'booking_url_bookings_edit_by_visitors' and 'booking_url_bookings_listing_by_customer'
991 * to some pages different from homepage.
992 *
993 * @return void
994 */
995 function wpbc_create_page_bookingedit(){ // FixIn: 9.6.2.10.
996
997 global $wp_rewrite;
998 if ( is_null( $wp_rewrite ) ) { // FixIn: 9.7.1.1.
999 // Maybe it was not init, yet
1000 if ( ! has_action( 'init', 'wpbc_create_page_bookingedit' ) ) {
1001 add_action( 'init', 'wpbc_create_page_bookingedit', 99 ); // <- priority to load it last
1002 }
1003 return false;
1004 }
1005
1006 // Booking Edit page - set default page and URL ---------------------------------------------------------------
1007 $url_booking_edit = get_bk_option( 'booking_url_bookings_edit_by_visitors' );
1008 if (
1009 ( site_url() == $url_booking_edit )
1010 && ( empty( get_page_by_path( 'wpbc-my-booking' ) ) )
1011 ){
1012 $page_params = array(
1013 'post_content' => '[bookingedit]', // The post content
1014 'post_name' => 'wpbc-my-booking', // sanitize_title( $post_title )
1015 'post_title' => esc_html__( 'My Booking', 'booking' ) // Title
1016 );
1017
1018 $post_id = wpbc_create_page( $page_params );
1019
1020 if ( ! empty( $post_id ) ) {
1021 $post_url = get_permalink( $post_id );
1022 update_bk_option( 'booking_url_bookings_edit_by_visitors', $post_url );
1023 }
1024 } else if (
1025 (
1026 ( site_url() == $url_booking_edit )
1027 || ( empty( $url_booking_edit ) )
1028 )
1029 && ( ! empty( get_page_by_path( 'wpbc-my-booking' ) ) )
1030 ){
1031 $wp_post = get_page_by_path( 'wpbc-my-booking' );
1032 if ( ! empty( $wp_post ) ) {
1033 update_bk_option( 'booking_url_bookings_edit_by_visitors', get_permalink( $wp_post->ID ) );
1034 }
1035 }
1036
1037 // -----------------------------------------------------------------------------------------------------------------
1038 // Check if existing page has our shortcode
1039 // -----------------------------------------------------------------------------------------------------------------
1040 $booking_edit_relative_url = wpbc_make_link_relative( get_bk_option( 'booking_url_bookings_edit_by_visitors' ) );
1041 $is_added = wpbc_add_shortcode_to_exist_page( $booking_edit_relative_url, '[bookingedit]' );
1042
1043
1044 // =================================================================================================================
1045 // =================================================================================================================
1046 // =================================================================================================================
1047
1048
1049 // Booking Listing page - set default page and URL ---------------------------------------------------------------
1050 $url_booking_edit = get_bk_option( 'booking_url_bookings_listing_by_customer' );
1051 if (
1052 ( site_url() == $url_booking_edit )
1053 && ( empty( get_page_by_path( 'wpbc-my-bookings-listing' ) ) )
1054 ){
1055
1056 $page_params = array(
1057 'post_content' => '[bookingcustomerlisting]', // The post content
1058 'post_name' => 'wpbc-my-bookings-listing', // sanitize_title( $post_title )
1059 'post_title' => esc_html__( 'My Bookings Listing', 'booking' ) // Title
1060 );
1061
1062 $post_id = wpbc_create_page( $page_params );
1063
1064 if ( ! empty( $post_id ) ) {
1065 $post_url = get_permalink( $post_id );
1066 update_bk_option( 'booking_url_bookings_listing_by_customer', $post_url );
1067 }
1068 } else if (
1069 (
1070 ( site_url() == $url_booking_edit )
1071 || ( empty( $url_booking_edit ) )
1072 )
1073 && ( ! empty( get_page_by_path( 'wpbc-my-bookings-listing' ) ) )
1074 ){
1075 $wp_post = get_page_by_path( 'wpbc-my-bookings-listing' );
1076 if ( ! empty( $wp_post ) ){
1077 update_bk_option( 'booking_url_bookings_listing_by_customer', get_permalink( $wp_post->ID ) );
1078 }
1079 }
1080
1081
1082 // -----------------------------------------------------------------------------------------------------------------
1083 // Check if existing page has our shortcode
1084 // -----------------------------------------------------------------------------------------------------------------
1085 $booking_listing_relative_url = wpbc_make_link_relative( get_bk_option( 'booking_url_bookings_listing_by_customer' ) );
1086 $is_added = wpbc_add_shortcode_to_exist_page( $booking_listing_relative_url, '[bookingcustomerlisting]' );
1087
1088 }
1089
1090
1091 function wpbc_create_page_booking_payment_status(){ // FixIn: 9.6.2.13.
1092
1093 global $wp_rewrite;
1094 if ( is_null( $wp_rewrite ) ) { // FixIn: 9.7.1.1.
1095 return false;
1096 }
1097
1098 // Successful Payment page options ---------------------------------------------------------------------------------
1099
1100 $post_url_relative = false;
1101 $slug = 'wpbc-booking-payment-successful';
1102 if ( empty( get_page_by_path( $slug ) ) ) {
1103 // Create page
1104 $page_params = array(
1105 'post_title' => esc_html( __( 'Booking Payment Confirmation', 'booking' ) ),
1106 'post_content' => esc_html( __( 'Thank you for your booking. Your payment for the booking has been successfully received.', 'booking' ) ),
1107 'post_name' => $slug
1108 );
1109
1110 $post_id = wpbc_create_page( $page_params );
1111
1112 if ( ! empty( $post_id ) ) {
1113 $post_url = get_permalink( $post_id );
1114 $post_url_relative = str_replace( site_url(), '', $post_url );
1115 }
1116 }
1117 if ( ! empty( $post_url_relative ) ) {
1118
1119 //$post_url_relative = wpbc_make_link_absolute( $post_url_relative );
1120
1121 $payment_systems = array(
1122 'booking_stripe_v3_order_successful',
1123 'booking_paypal_return_url',
1124 'booking_authorizenet_order_successful',
1125 'booking_sage_order_successful',
1126 'booking_ideal_return_url',
1127 'booking_ipay88_return_url'
1128 );
1129
1130 foreach ( $payment_systems as $payment_system ) {
1131
1132 if (
1133 ( false === get_bk_option( $payment_system ) )
1134 || ( '/successful' == wpbc_make_link_relative( get_bk_option( $payment_system ) ) )
1135 ) {
1136 update_bk_option( $payment_system, wpbc_make_link_relative( $post_url_relative ) );
1137 }
1138 }
1139 }
1140
1141 // Failed Payment page options -------------------------------------------------------------------------------------
1142
1143 $post_url_relative = false;
1144 $slug = 'wpbc-booking-payment-failed';
1145 if ( empty( get_page_by_path( $slug ) ) ) {
1146 // Create page
1147 $page_params = array(
1148 'post_title' => esc_html( __( 'Booking Payment Failed', 'booking' ) ),
1149 'post_content' => esc_html( __( 'Payment Unsuccessful. Please contact us for assistance.', 'booking' ) ),
1150 'post_name' => $slug
1151 );
1152
1153 $post_id = wpbc_create_page( $page_params );
1154
1155 if ( ! empty( $post_id ) ) {
1156 $post_url = get_permalink( $post_id );
1157 $post_url_relative = str_replace( site_url(), '', $post_url );
1158 }
1159 }
1160 if ( ! empty( $post_url_relative ) ) {
1161
1162 //$post_url_relative = wpbc_make_link_absolute( $post_url_relative );
1163
1164 $payment_systems = array(
1165 'booking_stripe_v3_order_failed',
1166 'booking_paypal_cancel_return_url',
1167 'booking_authorizenet_order_failed',
1168 'booking_sage_order_failed',
1169 'booking_ideal_cancel_return_url',
1170 'booking_ipay88_cancel_return_url'
1171 );
1172
1173 foreach ( $payment_systems as $payment_system ) {
1174
1175 if (
1176 ( false === get_bk_option( $payment_system ) )
1177 || ( '/failed' == wpbc_make_link_relative( get_bk_option( $payment_system ) ) )
1178 ) {
1179 update_bk_option( $payment_system, wpbc_make_link_relative( $post_url_relative ) );
1180 }
1181 }
1182 }
1183 }
1184