| @@ -1,0 +1,176 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package GamiPress\AmeliaBooking\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_ameliabooking_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( 'ameliabooking_appointment', $_REQUEST['post_type'] ) ) { | |
| 36 | + | |
| 37 | + // Get the services | |
| 38 | + $services = $wpdb->get_results( $wpdb->prepare( | |
| 39 | + "SELECT a.id, a.name | |
| 40 | + FROM {$wpdb->prefix}amelia_services AS a | |
| 41 | + WHERE a.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->id, | |
| 50 | + 'post_title' => $service->name, | |
| 51 | + ); | |
| 52 | + | |
| 53 | + } | |
| 54 | + | |
| 55 | + // Return our results | |
| 56 | + wp_send_json_success( $results ); | |
| 57 | + die; | |
| 58 | + | |
| 59 | + } else if( isset( $_REQUEST['post_type'] ) && in_array( 'ameliabooking_event', $_REQUEST['post_type'] ) ) { | |
| 60 | + | |
| 61 | + // Get the events | |
| 62 | + $events = $wpdb->get_results( $wpdb->prepare( | |
| 63 | + "SELECT e.id, e.name, e.status, e.maxCapacity, e.bookingCloses | |
| 64 | + FROM {$wpdb->prefix}amelia_events AS e | |
| 65 | + WHERE e.name LIKE %s", | |
| 66 | + "%%{$search}%%" | |
| 67 | + ) ); | |
| 68 | + | |
| 69 | + // Get current date to compare with end period | |
| 70 | + $date = date( "Y-m-d H:i:s", current_time('timestamp') ); | |
| 71 | + | |
| 72 | + foreach ( $events as $event ) { | |
| 73 | + | |
| 74 | + if ( $event->status !== 'rejected' ){ | |
| 75 | + | |
| 76 | + $event_periodStart = gamipress_ameliabooking_get_date_close( $event->id ); | |
| 77 | + | |
| 78 | + $event_capacity = gamipress_ameliabooking_check_capacity( $event->id ); | |
| 79 | + // To check if event is full | |
| 80 | + if ($event_capacity < $event->maxCapacity) { | |
| 81 | + // To compare if event is opened | |
| 82 | + if ( $date < $event->bookingCloses || $date < $event_periodStart){ | |
| 83 | + | |
| 84 | + $results[] = array( | |
| 85 | + 'ID' => $event->id, | |
| 86 | + 'post_title' => $event->name, | |
| 87 | + ); | |
| 88 | + | |
| 89 | + } | |
| 90 | + } | |
| 91 | + | |
| 92 | + } | |
| 93 | + | |
| 94 | + } | |
| 95 | + | |
| 96 | + // Return our results | |
| 97 | + wp_send_json_success( $results ); | |
| 98 | + die; | |
| 99 | + } | |
| 100 | + | |
| 101 | +} | |
| 102 | +add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_ameliabooking_ajax_get_posts', 5 ); | |
| 103 | + | |
| 104 | +// Get the service title | |
| 105 | +function gamipress_ameliabooking_get_service_title( $service_id ) { | |
| 106 | + | |
| 107 | + if( absint( $service_id ) === 0 ) return ''; | |
| 108 | + | |
| 109 | + global $wpdb; | |
| 110 | + | |
| 111 | + return $wpdb->get_var( $wpdb->prepare( | |
| 112 | + "SELECT a.name FROM {$wpdb->prefix}amelia_services AS a WHERE a.id = %d", | |
| 113 | + absint( $service_id ) | |
| 114 | + ) ); | |
| 115 | + | |
| 116 | +} | |
| 117 | + | |
| 118 | +// Get the event title | |
| 119 | +function gamipress_ameliabooking_get_event_title( $event_id ) { | |
| 120 | + | |
| 121 | + if( absint( $event_id ) === 0 ) return ''; | |
| 122 | + | |
| 123 | + global $wpdb; | |
| 124 | + | |
| 125 | + return $wpdb->get_var( $wpdb->prepare( | |
| 126 | + "SELECT e.name FROM {$wpdb->prefix}amelia_events AS e WHERE e.id = %d", | |
| 127 | + absint( $event_id ) | |
| 128 | + ) ); | |
| 129 | + | |
| 130 | +} | |
| 131 | + | |
| 132 | +/** | |
| 133 | + * Get Amelia event date close. An event is closed when start if a date is not specified in Amelia settings | |
| 134 | + * | |
| 135 | + * @since 1.0.0 | |
| 136 | + * | |
| 137 | + * @param int $event_id ID event | |
| 138 | + * | |
| 139 | + * @return string | |
| 140 | + * | |
| 141 | + */ | |
| 142 | +function gamipress_ameliabooking_get_date_close( $event_id ) { | |
| 143 | + | |
| 144 | + global $wpdb; | |
| 145 | + | |
| 146 | + $event_periodStart = $wpdb->get_var( $wpdb->prepare( | |
| 147 | + "SELECT periodStart FROM {$wpdb->prefix}amelia_events_periods WHERE eventId = %d", | |
| 148 | + absint( $event_id ) | |
| 149 | + ) ); | |
| 150 | + | |
| 151 | + return $event_periodStart; | |
| 152 | + | |
| 153 | +} | |
| 154 | + | |
| 155 | +/** | |
| 156 | + * Check if event is full | |
| 157 | + * | |
| 158 | + * @since 1.0.0 | |
| 159 | + * | |
| 160 | + * @param int $event_id ID event | |
| 161 | + * | |
| 162 | + * @return int | |
| 163 | + * | |
| 164 | + */ | |
| 165 | +function gamipress_ameliabooking_check_capacity( $event_id ) { | |
| 166 | + | |
| 167 | + global $wpdb; | |
| 168 | + | |
| 169 | + $event_counter = $wpdb->get_var( $wpdb->prepare( | |
| 170 | + "SELECT COUNT(id) FROM {$wpdb->prefix}amelia_customer_bookings_to_events_periods WHERE eventPeriodId = %d", | |
| 171 | + absint( $event_id ) | |
| 172 | + ) ); | |
| 173 | + | |
| 174 | + return absint( $event_counter ); | |
| 175 | + | |
| 176 | +} | |