jet-appointments-booking.php
159 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Jet Appointments Booking core integrations file |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package SureTrigger |
| 7 | */ |
| 8 | |
| 9 | namespace SureTriggers\Integrations\JetAppointmentsBooking; |
| 10 | |
| 11 | use SureTriggers\Controllers\IntegrationsController; |
| 12 | use SureTriggers\Integrations\Integrations; |
| 13 | use SureTriggers\Traits\SingletonLoader; |
| 14 | |
| 15 | /** |
| 16 | * Class JetAppointmentsBooking |
| 17 | * |
| 18 | * @package SureTriggers\Integrations\JetAppointmentsBooking |
| 19 | */ |
| 20 | class JetAppointmentsBooking extends Integrations { |
| 21 | |
| 22 | use SingletonLoader; |
| 23 | |
| 24 | /** |
| 25 | * ID |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $id = 'JetAppointmentsBooking'; |
| 30 | |
| 31 | /** |
| 32 | * SureTrigger constructor. |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | $this->name = __( 'Jet Appointments Booking', 'suretriggers' ); |
| 36 | $this->description = __( 'A WordPress plugin for creating appointment booking forms and managing appointments with JetEngine.', 'suretriggers' ); |
| 37 | $this->icon_url = SURE_TRIGGERS_URL . 'assets/icons/jet-appointments-booking.svg'; |
| 38 | parent::__construct(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Is Plugin depended plugin is installed or not. |
| 43 | * |
| 44 | * @return bool |
| 45 | */ |
| 46 | public function is_plugin_installed() { |
| 47 | return class_exists( '\JET_APB\Plugin' ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get appointment meta data. |
| 52 | * |
| 53 | * @param int $appointment_id The appointment ID. |
| 54 | * @return array The appointment meta data. |
| 55 | */ |
| 56 | public static function get_appointment_meta( $appointment_id ) { |
| 57 | if ( ! class_exists( '\JET_APB\Plugin' ) ) { |
| 58 | return []; |
| 59 | } |
| 60 | |
| 61 | global $wpdb; |
| 62 | |
| 63 | // Get and sanitize table name. |
| 64 | $meta_table = \JET_APB\Plugin::instance()->db->appointments_meta->table(); |
| 65 | if ( empty( $meta_table ) || ! is_string( $meta_table ) ) { |
| 66 | return []; |
| 67 | } |
| 68 | $meta_table = esc_sql( $meta_table ); |
| 69 | |
| 70 | // Prepare and execute the query using a placeholder for the appointment ID only. |
| 71 | $results = $wpdb->get_results( |
| 72 | $wpdb->prepare( |
| 73 | 'SELECT meta_key, meta_value FROM ' . esc_sql( $meta_table ) . ' WHERE appointment_id = %d', |
| 74 | $appointment_id |
| 75 | ), |
| 76 | ARRAY_A |
| 77 | ); |
| 78 | |
| 79 | $meta = []; |
| 80 | if ( ! empty( $results ) ) { |
| 81 | foreach ( $results as $row ) { |
| 82 | $meta[ $row['meta_key'] ] = $row['meta_value']; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return $meta; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get appointment context data. |
| 91 | * |
| 92 | * @param int $appointment_id The appointment ID. |
| 93 | * @param string $new_status New appointment status (optional). |
| 94 | * @param string $old_status Old appointment status (optional). |
| 95 | * @return array The appointment context data. |
| 96 | */ |
| 97 | public static function get_appointment_context( $appointment_id, $new_status = null, $old_status = null ) { |
| 98 | if ( ! class_exists( '\JET_APB\Plugin' ) ) { |
| 99 | return []; |
| 100 | } |
| 101 | |
| 102 | $appointment_data = \JET_APB\Plugin::instance()->db->appointments->query( [ 'ID' => $appointment_id ], 1 ); |
| 103 | if ( empty( $appointment_data ) ) { |
| 104 | return []; |
| 105 | } |
| 106 | $appointment_data = $appointment_data[0]; |
| 107 | |
| 108 | $appointment_meta = self::get_appointment_meta( $appointment_id ); |
| 109 | |
| 110 | $context = array_merge( $appointment_data, $appointment_meta ); |
| 111 | |
| 112 | // Add status information if provided. |
| 113 | if ( null !== $old_status ) { |
| 114 | $context['old_status'] = $old_status; |
| 115 | } |
| 116 | if ( null !== $new_status ) { |
| 117 | $context['new_status'] = $new_status; |
| 118 | } |
| 119 | |
| 120 | // Add service title. |
| 121 | if ( ! empty( $context['service'] ) ) { |
| 122 | $service_post = get_post( $context['service'] ); |
| 123 | if ( $service_post ) { |
| 124 | $context['service_title'] = $service_post->post_title; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Add provider title. |
| 129 | if ( ! empty( $context['provider'] ) && $context['provider'] > 0 ) { |
| 130 | $provider_post = get_post( $context['provider'] ); |
| 131 | if ( $provider_post ) { |
| 132 | $context['provider_title'] = $provider_post->post_title; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // Add user information. |
| 137 | if ( ! empty( $context['user_id'] ) ) { |
| 138 | $user = get_user_by( 'ID', $context['user_id'] ); |
| 139 | if ( $user ) { |
| 140 | $context['user_login'] = $user->user_login; |
| 141 | $context['user_email'] = $user->user_email; |
| 142 | $context['user_display_name'] = $user->display_name; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Format dates. |
| 147 | $date_format = get_option( 'date_format' ); |
| 148 | $time_format = get_option( 'time_format' ); |
| 149 | $context['date_formatted'] = is_string( $date_format ) ? date_i18n( $date_format, $context['date'] ) : ''; |
| 150 | $context['slot_formatted'] = is_string( $time_format ) ? date_i18n( $time_format, $context['slot'] ) : ''; |
| 151 | $context['slot_end_formatted'] = is_string( $time_format ) ? date_i18n( $time_format, $context['slot_end'] ) : ''; |
| 152 | |
| 153 | return $context; |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | IntegrationsController::register( JetAppointmentsBooking::class ); |
| 159 |