| 1 |
<?php /** |
| 2 |
* @version 1.0 |
| 3 |
* @description Booking Hash Functions |
| 4 |
* @category Booking Hash |
| 5 |
* @author wpdevelop |
| 6 |
* |
| 7 |
* @web-site http://oplugins.com/ |
| 8 |
* @email info@oplugins.com |
| 9 |
* |
| 10 |
* @modified 2022-08-05 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 14 |
|
| 15 |
// H A S H // FixIn: 9.2.3.3. |
| 16 |
|
| 17 |
/** |
| 18 |
* Get booking ID and resource ID by booking HASH |
| 19 |
* |
| 20 |
* @param $booking_hash |
| 21 |
* |
| 22 |
* @return array|false - array( $booking_id, $resource_id ) | false if not found |
| 23 |
*/ |
| 24 |
function wpbc_hash__get_booking_id__resource_id( $booking_hash ) { |
| 25 |
|
| 26 |
if ( '' == $booking_hash ) { |
| 27 |
return false; |
| 28 |
} |
| 29 |
global $wpdb; |
| 30 |
|
| 31 |
if ( class_exists( 'wpdev_bk_personal' ) ) { |
| 32 |
|
| 33 |
$sql = $wpdb->prepare( "SELECT booking_id as id, booking_type as type FROM {$wpdb->prefix}booking as bk WHERE bk.hash = %s", $booking_hash ); |
| 34 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared |
| 35 |
$res = $wpdb->get_results( $sql ); |
| 36 |
|
| 37 |
if ( ( ! empty( $res ) ) && ( is_array( $res ) ) && ( isset( $res[0]->id ) ) && ( isset( $res[0]->type ) ) ) { // FixIn: 8.1.2.13. |
| 38 |
return array( $res[0]->id, $res[0]->type ); |
| 39 |
} |
| 40 |
} else { |
| 41 |
|
| 42 |
$sql = $wpdb->prepare( "SELECT booking_id as id FROM {$wpdb->prefix}booking as bk WHERE bk.hash = %s", $booking_hash ); |
| 43 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared |
| 44 |
$res = $wpdb->get_results( $sql ); |
| 45 |
|
| 46 |
if ( ( ! empty( $res ) ) && ( is_array( $res ) ) && ( isset( $res[0]->id ) ) ) { // FixIn: 8.1.2.13. |
| 47 |
return array( $res[0]->id, 1 ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Get booking HASH and resource ID |
| 57 |
* by booking ID |
| 58 |
* |
| 59 |
* @param $booking_id |
| 60 |
* |
| 61 |
* @return array|false - array( $hash, $resource_id ) | false if not found |
| 62 |
*/ |
| 63 |
function wpbc_hash__get_booking_hash__resource_id( $booking_id ) { |
| 64 |
|
| 65 |
if ( '' == $booking_id ) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
global $wpdb; |
| 69 |
|
| 70 |
if ( class_exists( 'wpdev_bk_personal' ) ) { |
| 71 |
|
| 72 |
$sql = $wpdb->prepare( "SELECT hash, booking_type as type FROM {$wpdb->prefix}booking as bk WHERE bk.booking_id = %d", $booking_id ); |
| 73 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared |
| 74 |
$res = $wpdb->get_results( $sql ); |
| 75 |
|
| 76 |
if ( ( ! empty( $res ) ) && ( is_array( $res ) ) && ( isset( $res[0]->hash ) ) && ( isset( $res[0]->type ) ) ) { |
| 77 |
return array( $res[0]->hash, $res[0]->type ); |
| 78 |
} |
| 79 |
} else { |
| 80 |
|
| 81 |
$sql = $wpdb->prepare( "SELECT hash FROM {$wpdb->prefix}booking as bk WHERE bk.booking_id = %d", $booking_id ); |
| 82 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared |
| 83 |
$res = $wpdb->get_results( $sql ); |
| 84 |
|
| 85 |
if ( ( ! empty( $res ) ) && ( is_array( $res ) ) && ( isset( $res[0]->hash ) ) ) { |
| 86 |
return array( $res[0]->hash, 1 ); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Update booking hash to newly generated. Run after creation/modification of booking in post request |
| 96 |
* |
| 97 |
* @param $booking_id |
| 98 |
* @param $resource_id |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
function wpbc_hash__update_booking_hash( $booking_id, $resource_id = '1' ) { |
| 103 |
global $wpdb; |
| 104 |
|
| 105 |
$update_sql = $wpdb->prepare( |
| 106 |
"UPDATE {$wpdb->prefix}booking AS bk SET bk.hash = MD5(%s) WHERE bk.booking_id = %d" |
| 107 |
, time() . '_' . wp_rand( 1000, 1000000 ) |
| 108 |
, $booking_id |
| 109 |
); |
| 110 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared |
| 111 |
if ( false === $wpdb->query( $update_sql ) ) { |
| 112 |
?> |
| 113 |
<script type="text/javascript"> document.getElementById('submiting<?php echo esc_attr( $resource_id ); ?>').innerHTML = '<div style="height:20px;width:100%;text-align:center;margin:15px auto;"><?php debuge_error( 'Error during updating hash in BD', __FILE__, __LINE__ ); ?></div>'; </script> <?php |
| 114 |
die(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* Get JavaScript for start dates selection in calendar after 1.5 second |
| 121 |
* |
| 122 |
* @param array $to_select__dates_sql_arr |
| 123 |
* @param int $resource_id |
| 124 |
* |
| 125 |
* @return string - JavaScript code as text |
| 126 |
*/ |
| 127 |
function wpbc_get_dates_selection_js_code( $to_select__dates_sql_arr, $resource_id ){ |
| 128 |
|
| 129 |
$dates_selection_js_code = '<script type="text/javascript"> ' . wpbc_jq_ready_start(); // FixIn: 10.1.3.7. |
| 130 |
|
| 131 |
// FixIn: 10.0.0.50. |
| 132 |
$dates_selection_js_code .= ' var select_dates_in_calendar_id = ' . intval( $resource_id ) . ';'; |
| 133 |
$dates_selection_js_code .= " jQuery( 'body' ).on( 'wpbc_calendar_ajx__loaded_data', function ( event, loaded_resource_id ){ "; // Fire on all booking dates loaded |
| 134 |
$dates_selection_js_code .= " if ( loaded_resource_id == select_dates_in_calendar_id ){ "; |
| 135 |
|
| 136 |
$string__dates_sql_arr = array_map( function ( $value ) { |
| 137 |
$value = explode( ' ', $value ); |
| 138 |
$new_value = "'" . $value[0] . "'"; |
| 139 |
return $new_value; |
| 140 |
}, $to_select__dates_sql_arr ); |
| 141 |
$string__dates_sql_arr = array_unique($string__dates_sql_arr); |
| 142 |
$string__dates_sql_str = implode( ',', $string__dates_sql_arr ); |
| 143 |
|
| 144 |
$dates_selection_js_code .= " setTimeout( function (){ wpbc_auto_select_dates_in_calendar( select_dates_in_calendar_id, [" . $string__dates_sql_str . "] ); }, 500 );"; |
| 145 |
$dates_selection_js_code .= " } "; |
| 146 |
$dates_selection_js_code .= " } ); "; |
| 147 |
|
| 148 |
$dates_selection_js_code .= wpbc_jq_ready_end() . '</script>'; // FixIn: 10.1.3.7. |
| 149 |
|
| 150 |
return $dates_selection_js_code; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
/** |
| 155 |
* Get booking ID, from the $_GET['booking_hash'] in url |
| 156 |
* |
| 157 |
* @param $booking_hash if default false, then get hash from $_GET['booking_hash'] Otherwise get it from this parameter |
| 158 |
* |
| 159 |
* @return int|string ID of booking or '', if not found |
| 160 |
*/ |
| 161 |
function wpbc_get_booking_id__from_hash_in_url( $booking_hash = false ){ |
| 162 |
|
| 163 |
$result_arr = wpbc_get_booking_arr__from_hash_in_url( $booking_hash ); |
| 164 |
|
| 165 |
return $result_arr['booking_id']; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* Get booking ID, from the $_GET['booking_hash'] in url |
| 171 |
* |
| 172 |
* @param $booking_hash if default false, then get hash from $_GET['booking_hash'] Otherwise get it from this parameter |
| 173 |
* |
| 174 |
* @return int|string ID of ooking resource or '', if not found |
| 175 |
*/ |
| 176 |
function wpbc_get_resource_id__from_hash_in_url( $booking_hash = false ){ |
| 177 |
|
| 178 |
$result_arr = wpbc_get_booking_arr__from_hash_in_url( $booking_hash ); |
| 179 |
|
| 180 |
return $result_arr['resource_id']; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
/** |
| 186 |
* Get booking data ['booking_id' => 2453, 'resource_id' => 4 ], from the URL -- $_GET['booking_hash'] |
| 187 |
* |
| 188 |
* @param $booking_hash if default false, then get hash from $_REQUEST['booking_hash'] Otherwise get it from this parameter |
| 189 |
* |
| 190 |
* @return array if not found then ['booking_id' => '', 'resource_id' => ''] |
| 191 |
* otherwise ['booking_id' => 2453, 'resource_id' => 4 ] |
| 192 |
*/ |
| 193 |
function wpbc_get_booking_arr__from_hash_in_url( $booking_hash = false ){ |
| 194 |
|
| 195 |
if ( empty( $booking_hash ) ) { |
| 196 |
$booking_hash = ( isset( $_REQUEST['booking_hash'] ) ) ? sanitize_text_field( wp_unslash( $_REQUEST['booking_hash'] ) ) : ''; /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ |
| 197 |
} |
| 198 |
|
| 199 |
$booking_id = ''; |
| 200 |
$resource_id = ''; |
| 201 |
|
| 202 |
if ( ! empty( $booking_hash ) ) { |
| 203 |
$my_booking_id_type = wpbc_hash__get_booking_id__resource_id( $booking_hash ); |
| 204 |
|
| 205 |
if (false !== $my_booking_id_type ){ |
| 206 |
list( $booking_id, $resource_id ) = $my_booking_id_type; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
$result = array( |
| 211 |
'booking_id' => $booking_id, |
| 212 |
'resource_id' => $resource_id |
| 213 |
); |
| 214 |
|
| 215 |
return $result; |
| 216 |
|
| 217 |
} |