| 1 |
<?php |
| 2 |
/** |
| 3 |
* @version 1.0 |
| 4 |
* @description Booking Manager - Shortcodes |
| 5 |
* |
| 6 |
* @author wpdevelop |
| 7 |
* @link https://oplugins.com/ |
| 8 |
* @email info@oplugins.com |
| 9 |
* |
| 10 |
* @modified 2017-07-16 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 14 |
|
| 15 |
|
| 16 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 17 |
// I M P O R T |
| 18 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 19 |
|
| 20 |
/** Shortcode [] - Import ICS feed and create bookings from this feed. |
| 21 |
* |
| 22 |
* @param array $attr |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
function wpbm_ics_import_shortcode( $attr ) { |
| 26 |
|
| 27 |
if ( ( function_exists( 'wpbc_is_on_edit_page' ) ) && ( wpbc_is_on_edit_page() ) ) { |
| 28 |
return wpbc_get_preview_for_shortcode( 'booking-manager-import', $attr ); //FixIn: 9.9.0.39 |
| 29 |
} |
| 30 |
|
| 31 |
if ( wpbm_block_shortcodes_if_unpublished() ) { return ''; } // FixIn: 2.1.15.1. |
| 32 |
|
| 33 |
ob_start(); |
| 34 |
|
| 35 |
$import_status = wpbm_ics_import_start( $attr ); |
| 36 |
|
| 37 |
$echo_results = ob_get_contents(); |
| 38 |
|
| 39 |
ob_end_clean(); |
| 40 |
|
| 41 |
if ( ! empty( $attr['silence'] ) ) { //FixIn: m.2.0.1.2 - do not show 'Import XX bookings' message, if paremeter silence=1 in shortcode |
| 42 |
$import_status_echo = ''; |
| 43 |
} else { |
| 44 |
$import_status_echo = sprintf( __( 'Imported %s bookings', 'booking-manager' ), '<strong>' . ( (int) $import_status ) . '</strong>' ); |
| 45 |
} |
| 46 |
|
| 47 |
return $import_status_echo . $echo_results; |
| 48 |
} |
| 49 |
add_shortcode('booking-manager-import', 'wpbm_ics_import_shortcode' ); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 55 |
// L I S T I N G |
| 56 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 57 |
// [booking-manager-listing url='https://server.com/feed.ics' from='2017-08-06' until='week' until_offset='4h' max=500] |
| 58 |
// [booking-manager-listing url='https://server.com/feed.ics' from='today' from_offset='5d' until='year-end' until_offset='4h' max=500] |
| 59 |
// 'now' => __( 'Now', 'booking-manager' ) |
| 60 |
// , 'today' => __( '00:00 Today', 'booking-manager' ) |
| 61 |
// , 'week' => __( 'Start of current week', 'booking-manager' ) |
| 62 |
// , 'month-start' => __( 'Start of current month', 'booking-manager' ) |
| 63 |
// , 'month-end' => __( 'End of current month', 'booking-manager' ) |
| 64 |
// , 'year-start' => __( 'Start of current year', 'booking-manager' ) |
| 65 |
// , 'any' => __( 'The start of time', 'booking-manager' ) |
| 66 |
// , 'date' => __( 'Specific date / time', 'booking-manager' ) |
| 67 |
|
| 68 |
/** Shortcode [booking-manager-listing ...] for showing listing from .ics feed |
| 69 |
* |
| 70 |
* @param array $attr |
| 71 |
*/ |
| 72 |
function wpbm_ics_listing_shortcode( $attr ) { |
| 73 |
|
| 74 |
if ( ( function_exists( 'wpbc_is_on_edit_page' ) ) && ( wpbc_is_on_edit_page() ) ) { |
| 75 |
return wpbc_get_preview_for_shortcode( 'booking-manager-listing', $attr ); //FixIn: 9.9.0.39 |
| 76 |
} |
| 77 |
|
| 78 |
if ( wpbm_block_shortcodes_if_unpublished() ) { return ''; } // FixIn: 2.1.15.1. |
| 79 |
|
| 80 |
$listing = wpbm_ics_get_listing( $attr ); |
| 81 |
|
| 82 |
return $listing; |
| 83 |
|
| 84 |
} |
| 85 |
add_shortcode('booking-manager-listing', 'wpbm_ics_listing_shortcode' ); //[booking-manager-listing url='https://calendar.google.com/calendar/ical/pkqi3dnhq1l4j2qu0id3as984k%40group.calendar.google.com/public/basic.ics' from='2016-08-01' until='2018-08-30'] |
| 86 |
|
| 87 |
|
| 88 |
//FixIn: 2.0.22.1 |
| 89 |
// [booking-manager-delete resource_id=5] |
| 90 |
// [booking-manager-delete resource_id=5 action='delete'] |
| 91 |
function wpbm_ics_delete_shortcode( $attr ) { |
| 92 |
|
| 93 |
if ( ( function_exists( 'wpbc_is_on_edit_page' ) ) && ( wpbc_is_on_edit_page() ) ) { |
| 94 |
return wpbc_get_preview_for_shortcode( 'booking-manager-delete', $attr ); //FixIn: 9.9.0.39 |
| 95 |
} |
| 96 |
|
| 97 |
if ( wpbm_block_shortcodes_if_unpublished() ) { return ''; } // FixIn: 2.1.15.1. |
| 98 |
|
| 99 |
ob_start(); |
| 100 |
|
| 101 |
if ( ! isset( $attr['resource_id'] ) ) { |
| 102 |
$attr['resource_id'] = 1; |
| 103 |
} |
| 104 |
|
| 105 |
if ( ! isset( $attr['action'] ) ) { |
| 106 |
$attr['action'] = 'trash'; |
| 107 |
} |
| 108 |
|
| 109 |
global $wpdb; |
| 110 |
$is_trash = 1; |
| 111 |
$resource_id = intval( $attr['resource_id'] ); //FixIn: |
| 112 |
|
| 113 |
if ( 'trash' == $attr['action'] ) { |
| 114 |
|
| 115 |
$my_sql = "UPDATE {$wpdb->prefix}booking AS bk SET bk.trash = {$is_trash} WHERE sync_gid != '' AND trash != 1 AND booking_type = {$resource_id}"; |
| 116 |
|
| 117 |
if ( false === $wpdb->query( $my_sql ) ) { |
| 118 |
?> |
| 119 |
<script type="text/javascript"> |
| 120 |
var my_message = '<?php echo html_entity_decode( esc_js( get_debuge_error( 'Error during trash booking in DB', __FILE__, __LINE__ ) ), ENT_QUOTES ); ?>'; |
| 121 |
wpbc_admin_show_message( my_message, 'error', 30000 ); |
| 122 |
</script> <?php |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if ( 'delete' == $attr['action'] ) { |
| 127 |
|
| 128 |
$bookings_obj_arr = $wpdb->get_results( "SELECT booking_id as ID FROM {$wpdb->prefix}booking WHERE sync_gid != '' AND trash != 1 AND booking_type = {$resource_id} LIMIT 0, 1000" ); |
| 129 |
|
| 130 |
$id_arr = array(); |
| 131 |
foreach ( $bookings_obj_arr as $booking_obj ) { |
| 132 |
$id_arr[] = $booking_obj->ID; |
| 133 |
} |
| 134 |
$string_id = implode( ',', $id_arr ); |
| 135 |
|
| 136 |
if ( $string_id != '' ) { |
| 137 |
|
| 138 |
// D E L E T E Dates |
| 139 |
if ( false === $wpdb->query( "DELETE FROM {$wpdb->prefix}bookingdates WHERE booking_id IN ({$string_id})" ) ) { ?> <script type="text/javascript"> var my_message = '<?php echo html_entity_decode( esc_js( get_debuge_error( 'Error during deleting booking in DB', __FILE__, __LINE__ ) ), ENT_QUOTES ); ?>'; wpbc_admin_show_message( my_message, 'error', 30000 ); </script> <?php die(); } |
| 140 |
// D E L E T E Bookings |
| 141 |
if ( false === $wpdb->query( "DELETE FROM {$wpdb->prefix}booking WHERE booking_id IN ({$string_id})" ) ){ ?> <script type="text/javascript"> var my_message = '<?php echo html_entity_decode( esc_js( get_debuge_error( 'Error during deleting booking in DB', __FILE__, __LINE__ ) ), ENT_QUOTES ); ?>'; wpbc_admin_show_message( my_message, 'error', 30000 ); </script> <?php die(); } |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
$echo_results = ob_get_contents(); |
| 146 |
|
| 147 |
ob_end_clean(); |
| 148 |
|
| 149 |
return ''; |
| 150 |
} |
| 151 |
add_shortcode( 'booking-manager-delete', 'wpbm_ics_delete_shortcode' ); |
| 152 |
|
| 153 |
|
| 154 |
/** |
| 155 |
* Return true if shortcode should NOT run for the current post context. |
| 156 |
* Allowed statuses are filterable via 'wpbm_shortcode_allowed_statuses' (default: ['publish']). |
| 157 |
* |
| 158 |
* @return bool |
| 159 |
*/ |
| 160 |
function wpbm_block_shortcodes_if_unpublished() { |
| 161 |
|
| 162 |
// FixIn: 2.1.15.1. |
| 163 |
$post = get_post(); |
| 164 |
if ( ! $post ) { |
| 165 |
return true; // no post context -> suppress. |
| 166 |
} |
| 167 |
|
| 168 |
// e.g. add 'private'. |
| 169 |
$allowed_statuses = apply_filters( 'wpbm_shortcode_allowed_statuses', array( 'publish' ) ); |
| 170 |
|
| 171 |
// Do not execute the shortcode. |
| 172 |
if ( ! in_array( $post->post_status, $allowed_statuses, true ) ) { |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
// Optional: allow front-end preview for editors. |
| 177 |
/* |
| 178 |
if ( is_preview() && current_user_can( 'edit_post', $post->ID ) ) { |
| 179 |
return false; // allow |
| 180 |
} |
| 181 |
*/ |
| 182 |
|
| 183 |
// Optional: suppress in REST/admin universally. |
| 184 |
/* |
| 185 |
if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { |
| 186 |
return true; |
| 187 |
} |
| 188 |
*/ |
| 189 |
|
| 190 |
// Execute the shortcode normally. |
| 191 |
return false; |
| 192 |
} |