PluginProbe
Booking Calendar / 11.4.3
Booking Calendar v11.4.3
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.4.3, at includes/publish/wpbc-create-pages.php

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