PluginProbe
Tutor LMS – eLearning and online course solution / 3.9.14
Tutor LMS – eLearning and online course solution v3.9.14
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
← All changes | classes/TutorEDD.php +30 -223 trunk3.9.14 View file →
@@ -9,14 +9,12 @@
9 9 */
10 10
11 11 namespace TUTOR;
12 12
13 -defined( 'ABSPATH' ) || exit;
13 +if ( ! defined( 'ABSPATH' ) ) {
14 + exit;
15 +}
14 16
15 -use Tutor\Helpers\QueryHelper;
16 -use Tutor\Helpers\UrlHelper;
17 -use Tutor\Models\EnrollmentModel;
18 -
19 17 /**
20 18 * Manage EDD integration
21 19 *
22 20 * @since 1.0.0
@@ -29,9 +27,9 @@
29 27 * @since 1.0.0
30 28 */
31 29 public function __construct() {
32 30 parent::__construct();
33 -
31 + // Add Tutor Option.
34 32 add_filter( 'tutor_monetization_options', array( $this, 'tutor_monetization_options' ) );
35 33
36 34 $monetize_by = tutils()->get_option( 'monetize_by' );
37 35 if ( 'edd' !== $monetize_by ) {
@@ -37,12 +35,8 @@
37 35 if ( 'edd' !== $monetize_by ) {
38 36 return;
39 37 }
40 38
41 - $edd_path = WP_PLUGIN_DIR . '/easy-digital-downloads/easy-digital-downloads.php';
42 - register_deactivation_hook( $edd_path, array( $this, 'edd_deactivation_handler' ) );
43 -
44 - add_filter( 'tutor_course_sell_by', fn() => 'edd' );
45 39 add_action( 'save_post_' . $this->course_post_type, array( $this, 'save_course_meta' ) );
46 40
47 41 /**
48 42 * Is Course Purchasable
@@ -48,31 +42,14 @@
48 42 * Is Course Purchasable
49 43 */
50 44 add_filter( 'is_course_purchasable', array( $this, 'is_course_purchasable' ), 10, 2 );
51 45 add_filter( 'get_tutor_course_price', array( $this, 'get_tutor_course_price' ), 10, 2 );
52 - add_action( 'edd_insert_payment', array( $this, 'edd_order_created' ), 10, 2 );
46 + add_filter( 'tutor_course_sell_by', array( $this, 'tutor_course_sell_by' ) );
47 +
53 48 add_action( 'edd_update_payment_status', array( $this, 'edd_update_payment_status' ), 10, 3 );
54 -
55 - // @since 4.0.0
56 - add_filter( 'tutor_order_history_card_template', fn( $template ) => tutor_get_template( 'dashboard.account.billing.edd-order-history-card' ) );
57 - add_filter( 'tutor_order_history_status_options', array( $this, 'filter_order_history_status_options' ), 10, 2 );
58 - add_filter( 'tutor_get_orders_by_user_id', array( $this, 'filter_tutor_get_orders_by_user_id' ), 10, 3 );
59 49 }
60 50
61 51 /**
62 - * Handle EDD deactivation.
63 - *
64 - * @since 4.0.0
65 - *
66 - * @return void
67 - */
68 - public function edd_deactivation_handler() {
69 - if ( 'edd' === tutor_utils()->get_option( 'monetize_by' ) ) {
70 - tutor_utils()->update_option( 'monetize_by', 'free' );
71 - }
72 - }
73 -
74 - /**
75 52 * Add Option for tutor
76 53 *
77 54 * @since 1.0.0
78 55 *
@@ -128,18 +105,18 @@
128 105 * @return void
129 106 */
130 107 public function save_course_meta( $post_ID ) {
131 108
132 - $product_id = Input::post( Course::COURSE_PRODUCT_ID_META, '' );
109 + $product_id = Input::post( '_tutor_course_product_id', '' );
133 110
134 111 if ( '-1' !== $product_id ) {
135 112 $product_id = (int) $product_id;
136 113 if ( $product_id ) {
137 - update_post_meta( $post_ID, Course::COURSE_PRODUCT_ID_META, $product_id );
114 + update_post_meta( $post_ID, '_tutor_course_product_id', $product_id );
138 115 update_post_meta( $product_id, '_tutor_product', 'yes' );
139 116 }
140 117 } else {
141 - delete_post_meta( $post_ID, Course::COURSE_PRODUCT_ID_META );
118 + delete_post_meta( $post_ID, '_tutor_course_product_id' );
142 119 }
143 120
144 121 do_action( 'save_tutor_course', $post_ID, get_post( $post_ID ) );
145 122 }
@@ -170,10 +147,8 @@
170 147
171 148 /**
172 149 * Get course price
173 150 *
174 - * @since 1.0.0
175 - *
176 151 * @param string $price course price.
177 152 * @param int $course_id course id.
178 153 *
179 154 * @return mixed
@@ -185,104 +160,21 @@
185 160 }
186 161 }
187 162
188 163 /**
189 - * Prepare enrollment status.
164 + * Course sell by
190 165 *
191 - * @since 4.0.0
166 + * @since 1.0.0
192 167 *
193 - * @param string $order_status order status.
194 - *
195 168 * @return string
196 169 */
197 - private static function prepare_enrollment_status( $order_status ) {
198 - return 'complete' === $order_status
199 - ? EnrollmentModel::STATUS_COMPLETED
200 - : ( 'pending' === $order_status ? EnrollmentModel::STATUS_PENDING : EnrollmentModel::STATUS_CANCEL );
170 + public function tutor_course_sell_by() {
171 + return 'edd';
201 172 }
202 173
203 174 /**
204 - * Handle enrollment.
175 + * Update payment status
205 176 *
206 - * @since 4.0.0
207 - *
208 - * @param int $order_id The order ID.
209 - * @param string $enrollment_type The enrollment type.
210 - * @param string $enrollment_status The enrollment status.
211 - *
212 - * @return void
213 - */
214 - private static function handle_enrollment( int $order_id, string $enrollment_type, string $enrollment_status ): void {
215 - $order = edd_get_order( $order_id );
216 - if ( ! $order || ! is_object( $order ) ) {
217 - return;
218 - }
219 -
220 - $order_items = edd_get_order_items( array( 'order_id' => $order->id ) );
221 - if ( empty( $order_items ) ) {
222 - return;
223 - }
224 -
225 - $user_id = $order->user_id;
226 -
227 - foreach ( $order_items as $order_item ) {
228 - $product_id = $order_item->product_id;
229 - $product_has_course = tutor_utils()->product_belongs_with_course( $product_id );
230 -
231 - if ( ! $product_has_course ) {
232 - continue;
233 - }
234 -
235 - $course_id = (int) $product_has_course->post_id;
236 -
237 - if ( 'new' === $enrollment_type ) {
238 - // New enrollment.
239 - add_filter( 'tutor_enroll_data', fn( $data ) => array_merge( $data, array( 'post_status' => $enrollment_status ) ) );
240 - EnrollmentModel::do_enroll( $course_id, $order_id, $user_id );
241 - } else {
242 - // Update enrollment.
243 - $is_enrolled = EnrollmentModel::is_enrolled( $course_id, $user_id, false );
244 - if ( $is_enrolled ) {
245 - EnrollmentModel::update_enrollments( $enrollment_status, array( $is_enrolled->ID ) );
246 - }
247 - }
248 - }
249 - }
250 -
251 - /**
252 - * Handle enrollment when a new EDD order is created.
253 - *
254 - * Creates enrollment with appropriate status when EDD 3.0+ has fully
255 - * built the order with its items. This avoids the issue where
256 - * cart_details/order_items are empty during the initial
257 - * edd_update_payment_status hook (old_status = 'new').
258 - *
259 - * @since 4.0.0
260 - *
261 - * @param int $order_id The order ID.
262 - * @param array $order_data The order data array containing cart details.
263 - *
264 - * @return void
265 - */
266 - public function edd_order_created( $order_id, $order_data ) {
267 - if ( ! $order_id ) {
268 - return;
269 - }
270 -
271 - $enrollment_status = self::prepare_enrollment_status( $order_data['status'] );
272 - self::handle_enrollment( $order_id, 'new', $enrollment_status );
273 - }
274 -
275 - /**
276 - * Update enrollment status when EDD payment status changes.
277 - *
278 - * Skips the initial order creation (old_status = 'new') since enrollment
279 - * is handled by edd_order_created. Uses EDD 3.0+ API to retrieve order
280 - * items reliably.
281 - *
282 - * @since 1.0.0
283 - * @since 4.0.0 Refactored to use EDD 3.0+ API and skip initial order creation.
284 - *
285 177 * @param int $payment_id payment id.
286 178 * @param string $new_status payment's new status.
287 179 * @param string $old_status payment's old status.
288 180 *
@@ -288,113 +180,28 @@
288 180 *
289 181 * @return void
290 182 */
291 183 public function edd_update_payment_status( $payment_id, $new_status, $old_status ) {
292 - if ( empty( $new_status ) || ! $payment_id ) {
184 + if ( 'complete' !== $new_status ) {
293 185 return;
294 186 }
295 187
296 - /**
297 - * If 'new' === $old_status, EDD does not store cart_details.
298 - * So skip the action, as enrollment is handled by edd_order_created method.
299 - *
300 - * @since 4.0.0
301 - */
302 - if ( 'new' === $old_status ) {
303 - return;
304 - }
188 + $payment = new \EDD_Payment( $payment_id );
189 + $cart_details = $payment->cart_details;
190 + $user_id = $payment->user_info['id'];
305 191
306 - $enrollment_status = self::prepare_enrollment_status( $new_status );
307 - self::handle_enrollment( $payment_id, 'update', $enrollment_status );
308 - }
192 + if ( is_array( $cart_details ) ) {
193 + foreach ( $cart_details as $cart_index => $download ) {
309 194
310 - /**
311 - * Filter order history status options.
312 - *
313 - * @since 4.0.0
314 - *
315 - * @param array $options the status options.
316 - * @param string $seleted the selected status.
317 - *
318 - * @return array<array{label: string, value: string, count: int, url: string, active: bool}>
319 - */
320 - public function filter_order_history_status_options( $options, $seleted ) {
321 - $url = get_pagenum_link();
322 - $user_id = get_current_user_id();
323 -
324 - $statuses = array_merge( array( 'all' => 'All' ), edd_get_payment_statuses() );
325 - $options = array();
326 -
327 - foreach ( $statuses as $key => $status ) {
328 - $params = array(
329 - 'meta_key' => Course::IS_TUTOR_ORDER_FOR_COURSE_META,
330 - 'user_id' => $user_id,
331 - 'status' => $key,
332 - 'count' => true,
333 - );
334 -
335 - $count_query = new \EDD_Payments_Query( $params );
336 - $count = $count_query->get_payments();
337 -
338 - $options[] = array(
339 - 'label' => $status,
340 - 'value' => $key,
341 - 'count' => $count,
342 - 'url' => UrlHelper::add_query_params( $url, array( 'data' => $key ) ),
343 - 'active' => $key === $seleted || ( empty( $key ) && 'all' === $seleted ),
344 - );
195 + $if_has_course = tutor_utils()->product_belongs_with_course( $download['id'] );
196 + if ( $if_has_course ) {
197 + $course_id = $if_has_course->post_id;
198 + $has_any_enrolled = tutor_utils()->has_any_enrolled( $course_id, $user_id );
199 + if ( ! $has_any_enrolled ) {
200 + tutor_utils()->do_enroll( $course_id, $payment_id, $user_id );
201 + }
202 + }
203 + }
204 + tutor_utils()->complete_course_enroll( $payment_id );
345 205 }
346 -
347 - return $options;
348 - }
349 -
350 - /**
351 - * Filter tutor get orders by user id.
352 - *
353 - * @since 4.0.0
354 - *
355 - * @param object $data data.
356 - * @param int $user_id the user id.
357 - * @param array $args the query args.
358 - *
359 - * @return object
360 - */
361 - public function filter_tutor_get_orders_by_user_id( $data, $user_id, $args ) {
362 - if ( ! $user_id ) {
363 - return $data;
364 - }
365 -
366 - $status = Input::sanitize( $args['status'] ?? '' );
367 - $start_date = Input::sanitize( $args['start_date'] ?? '' );
368 - $end_date = Input::sanitize( $args['end_date'] ?? '' );
369 - $order = QueryHelper::get_valid_sort_order( $args['order'] ?? 'DESC' );
370 - $limit = intval( $args['limit'] ?? 0 );
371 - $offset = intval( $args['offset'] ?? 0 );
372 -
373 - $params = array(
374 - 'meta_key' => Course::IS_TUTOR_ORDER_FOR_COURSE_META,
375 - 'user' => $user_id,
376 - 'status' => $status,
377 - 'start_date' => $start_date,
378 - 'end_date' => $end_date,
379 - 'limit' => $limit,
380 - 'offset' => $offset,
381 - 'order' => $order,
382 - );
383 -
384 - if ( empty( $status ) || 'all' === $status ) {
385 - unset( $params['status'] );
386 - }
387 -
388 - $edd_query = new \EDD_Payments_Query( $params );
389 - $results = $edd_query->get_payments();
390 -
391 - $params['count'] = true;
392 - $total_count_query = new \EDD_Payments_Query( $params );
393 - $total_count = $total_count_query->get_payments();
394 -
395 - $data->results = $results;
396 - $data->total_count = $total_count;
397 -
398 - return $data;
399 206 }
400 207 }