| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\BookingPress\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Overrides GamiPress AJAX Helper for selecting posts |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
function gamipress_bookingpress_ajax_get_posts() { |
| 18 |
|
| 19 |
// Security check, forces to die if not security passed |
| 20 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 21 |
|
| 22 |
// Check if user can manage GamiPress |
| 23 |
if( ! current_user_can( gamipress_get_manager_capability() ) ) { |
| 24 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
global $wpdb; |
| 28 |
|
| 29 |
$results = array(); |
| 30 |
|
| 31 |
// Pull back the search string |
| 32 |
$search = isset( $_REQUEST['q'] ) ? sanitize_text_field( $_REQUEST['q'] ) : ''; |
| 33 |
$search = $wpdb->esc_like( $search ); |
| 34 |
|
| 35 |
if( isset( $_REQUEST['post_type'] ) && in_array( 'bookingpress_service', $_REQUEST['post_type'] ) ) { |
| 36 |
|
| 37 |
// Get the services |
| 38 |
$services = $wpdb->get_results( $wpdb->prepare( |
| 39 |
"SELECT a.bookingpress_service_id, a.bookingpress_service_name |
| 40 |
FROM {$wpdb->prefix}bookingpress_services AS a |
| 41 |
WHERE a.bookingpress_service_name LIKE %s", |
| 42 |
"%%{$search}%%" |
| 43 |
) ); |
| 44 |
|
| 45 |
foreach ( $services as $service ) { |
| 46 |
|
| 47 |
// Results should meet same structure like posts |
| 48 |
$results[] = array( |
| 49 |
'ID' => $service->bookingpress_service_id, |
| 50 |
'post_title' => $service->bookingpress_service_name, |
| 51 |
); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
// Return our results |
| 56 |
wp_send_json_success( $results ); |
| 57 |
die; |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
} |
| 62 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_bookingpress_ajax_get_posts', 5 ); |
| 63 |
|
| 64 |
// Get the service title |
| 65 |
function gamipress_bookingpress_get_service_title( $service_id ) { |
| 66 |
|
| 67 |
if( absint( $service_id ) === 0 ) return ''; |
| 68 |
|
| 69 |
global $wpdb; |
| 70 |
|
| 71 |
return $wpdb->get_var( $wpdb->prepare( |
| 72 |
"SELECT a.bookingpress_service_name FROM {$wpdb->prefix}bookingpress_services AS a WHERE a.bookingpress_service_id = %d", |
| 73 |
absint( $service_id ) |
| 74 |
) ); |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get the related WordPress user ID from a BookingPress customer ID. |
| 80 |
* |
| 81 |
* @since 1.0.0 |
| 82 |
* |
| 83 |
* @param int $customer_id BookingPress customer ID. |
| 84 |
* |
| 85 |
* @return int WordPress user ID |
| 86 |
*/ |
| 87 |
function gamipress_bookingpress_get_wp_user_id_from_customer_id( $customer_id ) { |
| 88 |
|
| 89 |
global $wpdb; |
| 90 |
|
| 91 |
$customer_id = absint( $customer_id ); |
| 92 |
|
| 93 |
if( empty( $customer_id ) ) { |
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
$customer_table = $wpdb->prefix . 'bookingpress_customers'; |
| 98 |
$wp_user_id = $wpdb->get_var( |
| 99 |
$wpdb->prepare( |
| 100 |
"SELECT bookingpress_wpuser_id FROM {$customer_table} WHERE bookingpress_customer_id = %d", |
| 101 |
$customer_id |
| 102 |
) |
| 103 |
); |
| 104 |
|
| 105 |
return $wp_user_id; |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
/** |
| 111 |
* Get Service by appointment ID |
| 112 |
* |
| 113 |
* @since 1.0.0 |
| 114 |
* |
| 115 |
* @param int $appointment_id ID appointment |
| 116 |
* |
| 117 |
*/ |
| 118 |
function gamipress_bookingpress_get_service_by_appointment_id( $appointment_id ) { |
| 119 |
|
| 120 |
global $wpdb; |
| 121 |
|
| 122 |
// Empty if no ID provided |
| 123 |
if( absint( $appointment_id ) === 0 ) { |
| 124 |
return ''; |
| 125 |
} |
| 126 |
|
| 127 |
$service_id = $wpdb->get_var( "SELECT bookingpress_service_id FROM {$wpdb->prefix}bookingpress_appointment_bookings WHERE bookingpress_appointment_booking_id = {$appointment_id}" ); |
| 128 |
|
| 129 |
return $service_id; |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get Customer by appointment ID |
| 135 |
* |
| 136 |
* @since 1.0.0 |
| 137 |
* |
| 138 |
* @param int $appointment_id ID appointment |
| 139 |
* |
| 140 |
*/ |
| 141 |
function gamipress_bookingpress_get_customer_by_appointment_id( $appointment_id ) { |
| 142 |
|
| 143 |
global $wpdb; |
| 144 |
|
| 145 |
// Empty if no ID provided |
| 146 |
if( absint( $appointment_id ) === 0 ) { |
| 147 |
return ''; |
| 148 |
} |
| 149 |
|
| 150 |
$customer_id = $wpdb->get_var( "SELECT bookingpress_customer_id FROM {$wpdb->prefix}bookingpress_appointment_bookings WHERE bookingpress_appointment_booking_id = {$appointment_id}" ); |
| 151 |
|
| 152 |
return $customer_id; |
| 153 |
|
| 154 |
} |
| 155 |
|