| 1 |
<?php |
| 2 |
/** |
| 3 |
* Front-End Shortcodes (Phase #1) |
| 4 |
* |
| 5 |
* File: /wp-content/plugins/booking/includes/_front_end/class-fe-shortcodes.php |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
class WPBC_FE_Shortcodes { |
| 14 |
|
| 15 |
/** |
| 16 |
* Boot. |
| 17 |
*/ |
| 18 |
public static function init() { |
| 19 |
|
| 20 |
// Register after legacy (legacy uses init priority 9999). |
| 21 |
add_action( 'init', array( __CLASS__, 'register_shortcodes' ), 10000 ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Register the shortcodes we move in Phase #1. |
| 26 |
*/ |
| 27 |
public static function register_shortcodes() { |
| 28 |
|
| 29 |
/** |
| 30 |
* [booking] |
| 31 |
* - WPBC_FE_Shortcodes::booking_shortcode() |
| 32 |
* - WPBC_FE_Render::render_booking_form() |
| 33 |
* - WPBC_FE_Form_Body_Renderer::render() |
| 34 |
* - WPBC_FE_Form_Source::get_form_body_html() |
| 35 |
* - WPBC_FE_Form_Source_Resolver::resolve() | (engine bfb_db) |
| 36 |
* - wpbc_bfb_get_booking_form_pair() |
| 37 |
* - wpbc_render_booking_form_shortcodes() |
| 38 |
* - WPBC_BFB_FormShortcodeEngine->render(). |
| 39 |
*/ |
| 40 |
add_shortcode( 'booking', array( __CLASS__, 'booking_shortcode' ) ); |
| 41 |
add_shortcode( 'bookingcalendar', array( __CLASS__, 'booking_calendar_only_shortcode' ) ); |
| 42 |
add_shortcode( 'bookingform', array( __CLASS__, 'bookingform_shortcode' ) ); |
| 43 |
} |
| 44 |
|
| 45 |
// --------------------------------------------------------------------- |
| 46 |
// Helpers |
| 47 |
// --------------------------------------------------------------------- |
| 48 |
|
| 49 |
private static function force_attr_array( $attr ) { |
| 50 |
return is_array( $attr ) ? $attr : array(); |
| 51 |
} |
| 52 |
|
| 53 |
private static function maybe_escape_shortcode_params( $attr ) { |
| 54 |
|
| 55 |
// This function sanitize each value in attr array by using WP function: sanitize_text_field(). |
| 56 |
$attr = wpbc_escape_shortcode_params( $attr ); |
| 57 |
|
| 58 |
return is_array( $attr ) ? $attr : array(); |
| 59 |
} |
| 60 |
|
| 61 |
// --------------------------------------------------------------------- |
| 62 |
// Shortcodes |
| 63 |
// --------------------------------------------------------------------- |
| 64 |
|
| 65 |
/** |
| 66 |
* Shortcode [booking ...] |
| 67 |
* |
| 68 |
* @param array $attr |
| 69 |
* |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
public static function booking_shortcode( $attr ) { |
| 73 |
|
| 74 |
if ( wpbc_is_on_edit_page() ) { |
| 75 |
return wpbc_get_preview_for_shortcode( 'booking', $attr ); |
| 76 |
} |
| 77 |
|
| 78 |
if ( WPBC_GET_Request::has_non_empty_get( 'booking_hash' ) ) { |
| 79 |
/* translators: 1: URL to FAQ page. */ |
| 80 |
return __( 'You need to use special shortcode [bookingedit] for booking editing.', 'booking' ) . ' ' . sprintf( __( 'Please check FAQ instruction how to configure it here %s', 'booking' ), '<a href="https://wpbookingcalendar.com/faq/configure-editing-cancel-payment-bookings-for-visitors/">https://wpbookingcalendar.com/faq/configure-editing-cancel-payment-bookings-for-visitors/</a>' ); |
| 81 |
} |
| 82 |
|
| 83 |
$attr = self::force_attr_array( $attr ); |
| 84 |
$attr = self::maybe_escape_shortcode_params( $attr ); |
| 85 |
|
| 86 |
// Parse, sanitize and normilize shortcode paramaters. |
| 87 |
$shortcode_params = array( |
| 88 |
'is_echo' => 0, |
| 89 |
'resource_id' => WPBC_FE_Shortcode_Params::get_from_attr__resource_id_with_aggregate( $attr, WPBC_FE_Attr_Postprocessor::get_default_booking_resource_id() ), |
| 90 |
'cal_count' => WPBC_FE_Shortcode_Params::get_from_attr__months_count( $attr, 1 ), |
| 91 |
'start_month_calendar' => WPBC_FE_Shortcode_Params::get_from_attr__start_month( $attr, false ),// [ year, month ] | false. |
| 92 |
'calendar_dates_start' => WPBC_FE_Shortcode_Params::get_from_attr__calendar_dates_start( $attr, '' ), |
| 93 |
'calendar_dates_end' => WPBC_FE_Shortcode_Params::get_from_attr__calendar_dates_end( $attr, '' ), |
| 94 |
'selected_dates_without_calendar' => '', |
| 95 |
'custom_booking_form' => WPBC_FE_Shortcode_Params::get_from_attr__custom_form( $attr, 'standard' ), |
| 96 |
'shortcode_param__options' => WPBC_FE_Shortcode_Params::get_from_attr__options( $attr, '' ), |
| 97 |
'form_status' => WPBC_FE_Shortcode_Params::get_from_attr__form_status( $attr, 'published' ), |
| 98 |
); |
| 99 |
|
| 100 |
$shortcode_result = WPBC_FE_Render::render_booking_form( $shortcode_params ); |
| 101 |
|
| 102 |
return $shortcode_result; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* Shortcode [bookingcalendar ...] |
| 108 |
* |
| 109 |
* @param array $attr |
| 110 |
* |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public static function booking_calendar_only_shortcode( $attr ) { |
| 114 |
|
| 115 |
if ( wpbc_is_on_edit_page() ) { |
| 116 |
return wpbc_get_preview_for_shortcode( 'bookingcalendar', $attr ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( WPBC_GET_Request::has_non_empty_get( 'booking_hash' ) ) { |
| 120 |
/* translators: 1: URL to FAQ page. */ |
| 121 |
return __( 'You need to use special shortcode [bookingedit] for booking editing.', 'booking' ) . ' ' . sprintf( __( 'Please check FAQ instruction how to configure it here %s', 'booking' ), '<a href="https://wpbookingcalendar.com/faq/configure-editing-cancel-payment-bookings-for-visitors/">https://wpbookingcalendar.com/faq/configure-editing-cancel-payment-bookings-for-visitors/</a>' ); |
| 122 |
} |
| 123 |
|
| 124 |
$attr = self::force_attr_array( $attr ); |
| 125 |
$attr = self::maybe_escape_shortcode_params( $attr ); |
| 126 |
|
| 127 |
$resource_id = WPBC_FE_Shortcode_Params::get_from_attr__primary_resource_id( $attr, WPBC_FE_Attr_Postprocessor::get_default_booking_resource_id() ); |
| 128 |
|
| 129 |
// Parse, sanitize and normilize shortcode paramaters. |
| 130 |
$shortcode_params = array( |
| 131 |
'is_echo' => 0, |
| 132 |
'resource_id' => WPBC_FE_Shortcode_Params::get_from_attr__resource_id_with_aggregate( $attr, WPBC_FE_Attr_Postprocessor::get_default_booking_resource_id() ), |
| 133 |
'cal_count' => WPBC_FE_Shortcode_Params::get_from_attr__months_count( $attr, 1 ), |
| 134 |
'start_month_calendar' => WPBC_FE_Shortcode_Params::get_from_attr__start_month( $attr, false ), // [ year, month ] | false. |
| 135 |
'calendar_dates_start' => WPBC_FE_Shortcode_Params::get_from_attr__calendar_dates_start( $attr, '' ), |
| 136 |
'calendar_dates_end' => WPBC_FE_Shortcode_Params::get_from_attr__calendar_dates_end( $attr, '' ), |
| 137 |
'shortcode_param__options' => WPBC_FE_Shortcode_Params::get_from_attr__options( $attr, '' ), |
| 138 |
'selected_dates_without_calendar' => '', |
| 139 |
'custom_booking_form' => '', // WPBC_FE_Shortcode_Params::get_from_attr__custom_form( $attr, 'standard' ), //. |
| 140 |
); |
| 141 |
|
| 142 |
$shortcode_calendar_html = WPBC_FE_Render::render_calendar_only( $shortcode_params ); |
| 143 |
|
| 144 |
$shortcode_html = "<div class='wpbc_only_calendar wpbc_container'>" . |
| 145 |
"<div id='calendar_booking_unselectable" . esc_attr( intval( preg_replace( '/[^0-9].*$/', '', (string) $resource_id ) ) ) . "'></div>" . |
| 146 |
$shortcode_calendar_html . |
| 147 |
'</div>'; |
| 148 |
|
| 149 |
return $shortcode_html; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
/** |
| 154 |
* Shortcode [bookingform ...] |
| 155 |
* |
| 156 |
* @param array $attr |
| 157 |
* |
| 158 |
* @return string |
| 159 |
*/ |
| 160 |
public static function bookingform_shortcode( $attr ) { |
| 161 |
|
| 162 |
if ( wpbc_is_on_edit_page() ) { |
| 163 |
return wpbc_get_preview_for_shortcode( 'bookingform', $attr ); |
| 164 |
} |
| 165 |
|
| 166 |
if ( WPBC_GET_Request::has_non_empty_get( 'booking_hash' ) ) { |
| 167 |
/* translators: 1: URL to FAQ page. */ |
| 168 |
return __( 'You need to use special shortcode [bookingedit] for booking editing.', 'booking' ) . ' ' . sprintf( __( 'Please check FAQ instruction how to configure it here %s', 'booking' ), '<a href="https://wpbookingcalendar.com/faq/configure-editing-cancel-payment-bookings-for-visitors/">https://wpbookingcalendar.com/faq/configure-editing-cancel-payment-bookings-for-visitors/</a>' ); |
| 169 |
} |
| 170 |
|
| 171 |
$attr = self::force_attr_array( $attr ); |
| 172 |
$attr = self::maybe_escape_shortcode_params( $attr ); |
| 173 |
|
| 174 |
// Parse, sanitize and normilize shortcode paramaters. |
| 175 |
$shortcode_params = array( |
| 176 |
'is_echo' => 0, |
| 177 |
'resource_id' => WPBC_FE_Shortcode_Params::get_from_attr__resource_id_with_aggregate( $attr, WPBC_FE_Attr_Postprocessor::get_default_booking_resource_id() ), |
| 178 |
'selected_dates_without_calendar' => WPBC_FE_Shortcode_Params::get_from_attr__selected_dates_without_calendar( $attr, '' ), |
| 179 |
'custom_booking_form' => WPBC_FE_Shortcode_Params::get_from_attr__custom_form( $attr, 'standard' ), |
| 180 |
'shortcode_param__options' => WPBC_FE_Shortcode_Params::get_from_attr__options( $attr, '' ), |
| 181 |
'cal_count' => 1, |
| 182 |
'start_month_calendar' => false, |
| 183 |
'calendar_dates_start' => '', |
| 184 |
'calendar_dates_end' => '', |
| 185 |
); |
| 186 |
|
| 187 |
$shortcode_result = WPBC_FE_Render::render_booking_form( $shortcode_params ); |
| 188 |
|
| 189 |
return $shortcode_result; |
| 190 |
} |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
WPBC_FE_Shortcodes::init(); |
| 195 |
|