PluginProbe
Booking Calendar / 11.4
Booking Calendar v11.4
11.8.4 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 All 204 releases
booking / includes / publish / wpbc-create-pages.php

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

1,071 lines 39.6 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 $template_record['form_slug'] = $form_slug;
648 $template_record['status'] = 'published';
649 $template_record['scope'] = 'global';
650 $template_record['owner_user_id'] = 0;
651 $template_record['booking_resource_id'] = null;
652 $template_record['is_default'] = 0;
653 $template_record['title'] = $config['form_title'];
654 $template_record['picture_url'] = isset( $template_record['picture_url'] ) ? (string) $template_record['picture_url'] : '';
655 if ( function_exists( 'wpbc_bfb_resolve_picture_url' ) ) {
656 $template_record['picture_url'] = wpbc_bfb_resolve_picture_url( $template_record['picture_url'] );
657 }
658
659 $booking_form_id = WPBC_BFB_Form_Storage::save_form( $template_record );
660 if ( ! empty( $booking_form_id ) ) {
661 $is_created = true;
662 }
663 }
664
665 return $is_created;
666 }
667
668
669 /**
670 * Build starter page content with a booking shortcode.
671 *
672 * @param string $shortcode Booking shortcode.
673 *
674 * @return string
675 */
676 function wpbc_get_activation_booking_page_content( $shortcode ) {
677
678 $shortcode = trim( (string) $shortcode );
679
680 if ( WPBC_IS_PLAYGROUND ) {
681 $shortcode = '<style type="text/css"> h1, h2, h3, h4, h5, h6 { font-weight: 500; font-family: var(--wp--preset--font-family--body); } </style>' . $shortcode;
682 }
683
684 return $shortcode;
685 }
686
687
688 /**
689 * Create new starter pages with booking forms.
690 *
691 * @param array $default_options_to_add Unused. Kept for backward-compatible direct calls.
692 *
693 * @return void
694 */
695 function wpbc_create_page_with_booking_form( $default_options_to_add = array() ) { // FixIn: 9.6.2.11.
696
697 global $wp_rewrite;
698 if ( is_null( $wp_rewrite ) ) { // FixIn: 9.7.1.1.
699
700 // Maybe it was not init, yet.
701 if ( ! has_action( 'init', 'wpbc_create_page_with_booking_form' ) ) {
702 add_action( 'init', 'wpbc_create_page_with_booking_form', 99 ); // <- priority to load it last
703 }
704
705 return false;
706 }
707
708 // -----------------------------------------------------------------------------------------------------------------
709
710 wpbc_create_activation_custom_booking_forms();
711
712 $configs = wpbc_get_activation_booking_form_page_configs();
713 if ( empty( $configs ) || ! is_array( $configs ) ) {
714 return;
715 }
716
717 $front_page_id = 0;
718
719 foreach ( $configs as $config ) {
720
721 $form_slug = sanitize_text_field( (string) $config['form_slug'] );
722 $shortcode = "[booking resource_id=1 form_type='{$form_slug}']";
723 $content = wpbc_get_activation_booking_page_content( $shortcode );
724
725 $result_arr = wpbc_add_shortcode_into_page(
726 array(
727 'page_post_name' => $config['page_slug'],
728 'post_title' => $config['page_title'],
729 'shortcode' => $content,
730 'check_exist_shortcode' => array(
731 $shortcode,
732 '[booking resource_id=1 form_type="' . $form_slug . '"]',
733 ),
734 'resource_id' => 1,
735 )
736 );
737
738 if ( WPBC_IS_PLAYGROUND && empty( $front_page_id ) && ! empty( $result_arr['relative_url'] ) ) {
739 $wp_post = get_page_by_path( $config['page_slug'] );
740 if ( ! empty( $wp_post ) ) {
741 $front_page_id = $wp_post->ID;
742 }
743 }
744 }
745
746 if ( WPBC_IS_PLAYGROUND && ! empty( $front_page_id ) ) {
747 update_option( 'show_on_front', 'page' );
748 update_option( 'page_on_front', $front_page_id );
749 }
750 }
751 add_action( 'wpbc_bfb_activation__form_structures_table__after_create', 'wpbc_create_page_with_booking_form', 20 );
752 add_action( 'wpbc_bfb_activation__form_structures_table__table_already_exists', 'wpbc_create_page_with_booking_form', 20 );
753
754
755 /**
756 * Get published starter pages that contain their expected booking form shortcodes.
757 *
758 * @return array
759 */
760 function wpbc_get_published_activation_booking_pages() {
761
762 $pages = array();
763 $configs = wpbc_get_activation_booking_form_page_configs();
764
765 if ( empty( $configs ) || ! is_array( $configs ) ) {
766 return $pages;
767 }
768
769 foreach ( $configs as $key => $config ) {
770
771 $wp_post = get_page_by_path( $config['page_slug'] );
772 if ( empty( $wp_post ) ) {
773 continue;
774 }
775
776 $form_slug = sanitize_text_field( (string) $config['form_slug'] );
777 $shortcode_single_quote = "[booking resource_id=1 form_type='{$form_slug}']";
778 $shortcode_double_quote = '[booking resource_id=1 form_type="' . $form_slug . '"]';
779
780 if (
781 ! wpbc_is_shortcode_exist_in_page_with_id( $wp_post->ID, $shortcode_single_quote ) &&
782 ! wpbc_is_shortcode_exist_in_page_with_id( $wp_post->ID, $shortcode_double_quote )
783 ) {
784 continue;
785 }
786
787 $post_url = get_permalink( $wp_post->ID );
788 if ( empty( $post_url ) ) {
789 continue;
790 }
791
792 $pages[ $key ] = array(
793 'url' => $post_url,
794 'button_title' => $config['button_title'],
795 'page_title' => $config['page_title'],
796 );
797 }
798
799 return $pages;
800 }
801
802 // ---------------------------------------------------------------------------------------------------------------------
803
804
805 /**
806 * Create new page and get URL of this page
807 *
808 * @param $default_options_to_add
809 *
810 * @return void
811 */
812 function wpbc_create_page_thank_you( $default_options_to_add ) { // FixIn: 9.6.2.11.
813
814 global $wp_rewrite;
815 if ( is_null( $wp_rewrite ) ) { // FixIn: 9.7.1.1.
816
817 // Maybe it was not init, yet
818 if ( ! has_action( 'init', 'wpbc_create_page_thank_you' ) ) {
819 add_action( 'init', 'wpbc_create_page_thank_you', 99 ); // <- priority to load it last
820 }
821 return false;
822 }
823
824 // -----------------------------------------------------------------------------------------------------------------
825
826 $thank_you_page_url = get_bk_option( 'booking_thank_you_page_URL' );
827
828 if ( ( empty( $thank_you_page_url ) ) // If No 'Thank you' page in Settings, or it's set as Empty.
829 || (
830 ( '/thank-you' == wpbc_make_link_relative( get_bk_option( 'booking_thank_you_page_URL' ) ) )
831 && ( empty( get_page_by_path( 'thank-you' ) ) ) // FixIn: 9.9.0.27.
832 )
833 || ( '/' == wpbc_make_link_relative( get_bk_option( 'booking_thank_you_page_URL' ) ) )
834 ){
835 $wp_post = get_page_by_path( 'wpbc-booking-received' );
836
837 $post_url = '';
838
839 if ( empty( $wp_post ) ) { // No default page. Create it.
840
841 $page_params = array(
842 'post_title' => esc_html( __( 'Booking Received', 'booking' ) ),
843 'post_content' => esc_html( __( 'Thank you for your booking. Your booking has been successfully received.', 'booking' ) )
844 . "\r\n" . ' [booking_confirm]',
845 'post_name' => 'wpbc-booking-received'
846 );
847 $post_id = wpbc_create_page( $page_params );
848
849 if ( ! empty( $post_id ) ) {
850 $post_url = wpbc_make_link_relative( get_permalink( $post_id ) );
851 }
852
853 } else {
854 $post_url = wpbc_make_link_relative( get_permalink( $wp_post->ID ) ); // Page already exist, so we need to update the
855 }
856
857 if ( ! empty( $post_url ) ) {
858 update_bk_option( 'booking_thank_you_page_URL', $post_url );
859 }
860 }
861
862 // -----------------------------------------------------------------------------------------------------------------
863
864 // Check if existing page has our shortcode
865 $relative_url = wpbc_make_link_relative( get_bk_option( 'booking_thank_you_page_URL' ) );
866 $is_sh_added = wpbc_add_shortcode_to_exist_page( $relative_url, '[booking_confirm]' );
867
868 }
869 add_bk_action( 'wpbc_before_activation__add_options', 'wpbc_create_page_thank_you' );
870
871
872
873 /**
874 * Create pages for [bookingedit] and [bookingcustomerlisting] shortcodes,
875 *
876 * if previously was not defined options
877 * 'booking_url_bookings_edit_by_visitors' and 'booking_url_bookings_listing_by_customer'
878 * to some pages different from homepage.
879 *
880 * @return void
881 */
882 function wpbc_create_page_bookingedit(){ // FixIn: 9.6.2.10.
883
884 global $wp_rewrite;
885 if ( is_null( $wp_rewrite ) ) { // FixIn: 9.7.1.1.
886 // Maybe it was not init, yet
887 if ( ! has_action( 'init', 'wpbc_create_page_bookingedit' ) ) {
888 add_action( 'init', 'wpbc_create_page_bookingedit', 99 ); // <- priority to load it last
889 }
890 return false;
891 }
892
893 // Booking Edit page - set default page and URL ---------------------------------------------------------------
894 $url_booking_edit = get_bk_option( 'booking_url_bookings_edit_by_visitors' );
895 if (
896 ( site_url() == $url_booking_edit )
897 && ( empty( get_page_by_path( 'wpbc-my-booking' ) ) )
898 ){
899 $page_params = array(
900 'post_content' => '[bookingedit]', // The post content
901 'post_name' => 'wpbc-my-booking', // sanitize_title( $post_title )
902 'post_title' => esc_html__( 'My Booking', 'booking' ) // Title
903 );
904
905 $post_id = wpbc_create_page( $page_params );
906
907 if ( ! empty( $post_id ) ) {
908 $post_url = get_permalink( $post_id );
909 update_bk_option( 'booking_url_bookings_edit_by_visitors', $post_url );
910 }
911 } else if (
912 (
913 ( site_url() == $url_booking_edit )
914 || ( empty( $url_booking_edit ) )
915 )
916 && ( ! empty( get_page_by_path( 'wpbc-my-booking' ) ) )
917 ){
918 $wp_post = get_page_by_path( 'wpbc-my-booking' );
919 if ( ! empty( $wp_post ) ) {
920 update_bk_option( 'booking_url_bookings_edit_by_visitors', get_permalink( $wp_post->ID ) );
921 }
922 }
923
924 // -----------------------------------------------------------------------------------------------------------------
925 // Check if existing page has our shortcode
926 // -----------------------------------------------------------------------------------------------------------------
927 $booking_edit_relative_url = wpbc_make_link_relative( get_bk_option( 'booking_url_bookings_edit_by_visitors' ) );
928 $is_added = wpbc_add_shortcode_to_exist_page( $booking_edit_relative_url, '[bookingedit]' );
929
930
931 // =================================================================================================================
932 // =================================================================================================================
933 // =================================================================================================================
934
935
936 // Booking Listing page - set default page and URL ---------------------------------------------------------------
937 $url_booking_edit = get_bk_option( 'booking_url_bookings_listing_by_customer' );
938 if (
939 ( site_url() == $url_booking_edit )
940 && ( empty( get_page_by_path( 'wpbc-my-bookings-listing' ) ) )
941 ){
942
943 $page_params = array(
944 'post_content' => '[bookingcustomerlisting]', // The post content
945 'post_name' => 'wpbc-my-bookings-listing', // sanitize_title( $post_title )
946 'post_title' => esc_html__( 'My Bookings Listing', 'booking' ) // Title
947 );
948
949 $post_id = wpbc_create_page( $page_params );
950
951 if ( ! empty( $post_id ) ) {
952 $post_url = get_permalink( $post_id );
953 update_bk_option( 'booking_url_bookings_listing_by_customer', $post_url );
954 }
955 } else if (
956 (
957 ( site_url() == $url_booking_edit )
958 || ( empty( $url_booking_edit ) )
959 )
960 && ( ! empty( get_page_by_path( 'wpbc-my-bookings-listing' ) ) )
961 ){
962 $wp_post = get_page_by_path( 'wpbc-my-bookings-listing' );
963 if ( ! empty( $wp_post ) ){
964 update_bk_option( 'booking_url_bookings_listing_by_customer', get_permalink( $wp_post->ID ) );
965 }
966 }
967
968
969 // -----------------------------------------------------------------------------------------------------------------
970 // Check if existing page has our shortcode
971 // -----------------------------------------------------------------------------------------------------------------
972 $booking_listing_relative_url = wpbc_make_link_relative( get_bk_option( 'booking_url_bookings_listing_by_customer' ) );
973 $is_added = wpbc_add_shortcode_to_exist_page( $booking_listing_relative_url, '[bookingcustomerlisting]' );
974
975 }
976
977
978 function wpbc_create_page_booking_payment_status(){ // FixIn: 9.6.2.13.
979
980 global $wp_rewrite;
981 if ( is_null( $wp_rewrite ) ) { // FixIn: 9.7.1.1.
982 return false;
983 }
984
985 // Successful Payment page options ---------------------------------------------------------------------------------
986
987 $post_url_relative = false;
988 $slug = 'wpbc-booking-payment-successful';
989 if ( empty( get_page_by_path( $slug ) ) ) {
990 // Create page
991 $page_params = array(
992 'post_title' => esc_html( __( 'Booking Payment Confirmation', 'booking' ) ),
993 'post_content' => esc_html( __( 'Thank you for your booking. Your payment for the booking has been successfully received.', 'booking' ) ),
994 'post_name' => $slug
995 );
996
997 $post_id = wpbc_create_page( $page_params );
998
999 if ( ! empty( $post_id ) ) {
1000 $post_url = get_permalink( $post_id );
1001 $post_url_relative = str_replace( site_url(), '', $post_url );
1002 }
1003 }
1004 if ( ! empty( $post_url_relative ) ) {
1005
1006 //$post_url_relative = wpbc_make_link_absolute( $post_url_relative );
1007
1008 $payment_systems = array(
1009 'booking_stripe_v3_order_successful',
1010 'booking_paypal_return_url',
1011 'booking_authorizenet_order_successful',
1012 'booking_sage_order_successful',
1013 'booking_ideal_return_url',
1014 'booking_ipay88_return_url'
1015 );
1016
1017 foreach ( $payment_systems as $payment_system ) {
1018
1019 if (
1020 ( false === get_bk_option( $payment_system ) )
1021 || ( '/successful' == wpbc_make_link_relative( get_bk_option( $payment_system ) ) )
1022 ) {
1023 update_bk_option( $payment_system, wpbc_make_link_relative( $post_url_relative ) );
1024 }
1025 }
1026 }
1027
1028 // Failed Payment page options -------------------------------------------------------------------------------------
1029
1030 $post_url_relative = false;
1031 $slug = 'wpbc-booking-payment-failed';
1032 if ( empty( get_page_by_path( $slug ) ) ) {
1033 // Create page
1034 $page_params = array(
1035 'post_title' => esc_html( __( 'Booking Payment Failed', 'booking' ) ),
1036 'post_content' => esc_html( __( 'Payment Unsuccessful. Please contact us for assistance.', 'booking' ) ),
1037 'post_name' => $slug
1038 );
1039
1040 $post_id = wpbc_create_page( $page_params );
1041
1042 if ( ! empty( $post_id ) ) {
1043 $post_url = get_permalink( $post_id );
1044 $post_url_relative = str_replace( site_url(), '', $post_url );
1045 }
1046 }
1047 if ( ! empty( $post_url_relative ) ) {
1048
1049 //$post_url_relative = wpbc_make_link_absolute( $post_url_relative );
1050
1051 $payment_systems = array(
1052 'booking_stripe_v3_order_failed',
1053 'booking_paypal_cancel_return_url',
1054 'booking_authorizenet_order_failed',
1055 'booking_sage_order_failed',
1056 'booking_ideal_cancel_return_url',
1057 'booking_ipay88_cancel_return_url'
1058 );
1059
1060 foreach ( $payment_systems as $payment_system ) {
1061
1062 if (
1063 ( false === get_bk_option( $payment_system ) )
1064 || ( '/failed' == wpbc_make_link_relative( get_bk_option( $payment_system ) ) )
1065 ) {
1066 update_bk_option( $payment_system, wpbc_make_link_relative( $post_url_relative ) );
1067 }
1068 }
1069 }
1070 }
1071