PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / integrations / tutor-booking.php

tutor-booking.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/integrations/tutor-booking.php

180 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Tutor Booking Integration.
4 *
5 * @noinspection PhpUndefinedNamespaceInspection
6 * @noinspection PhpFullyQualifiedNameUsageInspection
7 * @noinspection PhpUndefinedClassInspection
8 */
9
10
11 namespace StoreEngine\Integrations;
12
13 use StoreEngine\Classes\Integration;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 class TutorBooking extends AbstractIntegration {
20 const ID = 'storeengine/tutor-booking';
21
22 public function setup(): void {
23 $this->id = static::ID;
24 $this->label = __( 'Academy Tutor Booking', 'storeengine' );
25 $this->items_label = __( 'Booking Access', 'storeengine' );
26 $this->logo = STOREENGINE_ASSETS_URI . 'images/integrations/academy-lms.svg';
27 $this->isEnabled = defined( 'ACADEMY_VERSION' );
28 }
29
30 public function dispatch_hooks(): void {
31 add_action( 'storeengine/integrations/created', [ $this, 'add_course_meta' ] );
32 add_filter( 'academy_pro_booking/storeengine/get_product_price', [ $this, 'get_product_price' ], 10, 2 );
33 }
34
35 public function add_course_meta( Integration $integration ) {
36 if ( $this->get_id() !== $integration->get_provider() ) {
37 return;
38 }
39
40 $course_id = $integration->get_integration_id();
41 $product = get_post_meta( $course_id, '_academy_booking_product_id', true );
42 if ( empty( $product ) ) {
43 update_post_meta( $course_id, '_academy_booking_product_id', $integration->get_product_id() );
44 update_post_meta( $course_id, '_academy_booking_type', 'paid' );
45 update_post_meta( $integration->get_product_id(), '_academy_booking_id', $course_id );
46 }
47 }
48
49 public function get_product_price( $args, $booking_id ) {
50 $prices = $this->get_integration_repository( $booking_id );
51
52 /** @noinspection SpellCheckingInspection */
53 $schedule_time = get_user_meta( $args['user_id'], 'booking_schdule_time_' . $booking_id, true );
54 if ( $schedule_time !== $args['booked_schedule_date_time'] ) {
55 /** @noinspection SpellCheckingInspection */
56 add_user_meta( $args['user_id'], 'booking_schdule_time_' . $booking_id, $args['booked_schedule_date_time'] );
57 }
58
59 foreach ( $prices as $price ) {
60 return $price->price->get_id();
61 }
62
63 return false;
64 }
65
66 public function get_items( array $args = [] ): array {
67 $args = wp_parse_args( $args, [
68 'search' => '',
69 ] );
70 $search = $args['search'];
71
72 $course_query = new \WP_Query(
73 [
74 'post_type' => 'academy_booking',
75 'post_status' => 'publish',
76 's' => $search,
77 'per_page' => 10,
78 ]
79 );
80
81 $items = [];
82
83 if ( $course_query->have_posts() ) {
84 $items = array_map(
85 function ( $post ) {
86 return (object) [
87 'value' => $post->ID,
88 'label' => $post->post_title,
89 ];
90 },
91 $course_query->posts
92 );
93 }
94
95 return $items;
96 }
97
98 protected function purchase_created( $integration, $order ) {
99 $user_id = $order->get_customer_id();
100 $booking_id = $integration->get_integration_id();
101 $order_id = $order->get_id();
102
103 // Try to get the scheduled time from user meta first
104 /** @noinspection SpellCheckingInspection */
105 $schedule_time = get_user_meta( $user_id, 'booking_schdule_time_' . $booking_id, true );
106
107 // Fallback to booked post meta if user meta is empty
108 if ( empty( $schedule_time ) ) {
109 $booked_id = get_post_meta( $order_id, '_academy_order_for_booking_id_' . $booking_id, true );
110 $schedule_time = $booked_id ? get_post_meta( $booked_id, '_academy_booked_schedule_time', true ) : '';
111 }
112
113 // Proceed only if we have a valid schedule time
114 if ( ! empty( $schedule_time ) ) {
115 $booked_id = \AcademyProTutorBooking\Helper::do_booked(
116 $booking_id,
117 $user_id,
118 $schedule_time,
119 $order_id
120 );
121
122 if ( $booked_id ) {
123 // Remove user meta for this booking
124 /** @noinspection SpellCheckingInspection */
125 delete_user_meta( $user_id, 'booking_schdule_time_' . $booking_id, $schedule_time );
126
127 // Clear post cache for the booked post
128 clean_post_cache( $booked_id );
129 }
130 }
131 }
132
133 protected function purchase_removed( $integration, $order ) {
134 global $wpdb;
135
136 if ( ! \AcademyProTutorBooking\Helper::is_booked( $integration->get_integration_id(), $order->get_customer_id() ) ) {
137 return;
138 }
139
140 $booked_id = get_post_meta( $order->get_id(), '_academy_order_for_booking_id_' . $integration->get_integration_id(), true );
141 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
142 $wpdb->query(
143 $wpdb->prepare(
144 "
145 UPDATE {$wpdb->posts}
146 SET post_status='on-hold'
147 WHERE post_author=%d
148 AND post_parent=%d
149 AND post_type='academy_booked'
150 AND ID=%d;
151 ",
152 $order->get_customer_id(),
153 $integration->get_integration_id(),
154 $booked_id
155 )
156 );
157
158 $ids = $wpdb->get_col(
159 $wpdb->prepare(
160 "
161 SELECT ID FROM {$wpdb->posts}
162 WHERE post_status='on-hold'
163 AND post_author=%d
164 AND post_parent=%d
165 AND post_type='academy_booked'
166 AND ID=%d;
167 ",
168 $order->get_customer_id(),
169 $integration->get_integration_id(),
170 $booked_id
171 )
172 );
173 // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
174
175 if ( ! empty( $ids ) ) {
176 array_map( 'clean_post_cache', $ids );
177 }
178 }
179 }
180