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

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