PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.3.1
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.3.1
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / Extensions / LearnPress / LearnPress.php

LearnPress.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.3.1, at includes/Extensions/LearnPress/LearnPress.php

556 lines 20.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * LearnPress Extension
5 *
6 * @package NotificationX\Extensions
7 */
8
9 namespace NotificationX\Extensions\LearnPress;
10
11 use NotificationX\Core\Helper;
12 use NotificationX\Core\Rules;
13 use NotificationX\GetInstance;
14 use NotificationX\Extensions\Extension;
15 use NotificationX\Extensions\GlobalFields;
16 use NotificationX\Admin\Settings;
17
18 /**
19 * LearnPress Extension
20 * @method static LearnPress get_instance($args = null)
21 */
22 class LearnPress extends Extension {
23 /**
24 * Instance of Admin
25 *
26 * @var Admin
27 */
28 use GetInstance;
29
30 public $priority = 11;
31 public $id = 'learnpress';
32 public $img = NOTIFICATIONX_ADMIN_URL . 'images/extensions/sources/_learnpress.png';
33 public $doc_link = 'https://notificationx.com/docs/how-to-display-learnpress-course-enrollment-alert-using-notificationx/';
34 public $types = 'elearning';
35 public $module = 'modules_learnpress';
36 public $module_priority = 19;
37 public $function = 'LP'; // 'tutor_lms'
38
39 /**
40 * Initially Invoked when initialized.
41 */
42 public function __construct() {
43 parent::__construct();
44 }
45
46 public function init_extension()
47 {
48 $this->title = __('LearnPress', 'notificationx');
49 $this->module_title = __('LearnPress', 'notificationx');
50 }
51
52 /**
53 * This functions is hooked
54 *
55 * @hooked nx_public_action
56 * @return void
57 */
58 public function public_actions() {
59 parent::public_actions();
60
61 $monetize_by = 'free';
62 // $temp_monetize_by = tutils()->get_option('monetize_by');
63 // if (!empty($temp_monetize_by)) {
64 // $monetize_by = $temp_monetize_by;
65 // }
66 // public actions will be here
67 if (class_exists('WooCommerce') && $monetize_by === 'wc') {
68 add_action('woocommerce_new_order_item', array($this, 'save_new_enrollment'), 10, 3);
69 }
70 // public actions will be here
71 if (class_exists('Easy_Digital_Downloads') && $monetize_by === 'edd') {
72 add_action('edd_update_payment_status', array($this, 'save_new_enroll_payment_status'), 10, 3);
73 }
74
75 if (
76 $monetize_by === 'free' ||
77 (!class_exists('WooCommerce') && $monetize_by === 'wc') ||
78 (!class_exists('Easy_Digital_Downloads') && $monetize_by === 'edd')
79 ) {
80 add_action('learn-press/added-order-item-data', array($this, 'do_enroll'), 10, 3);
81 }
82 }
83
84 /**
85 * This functions is hooked
86 *
87 * @hooked nx_admin_action
88 * @return void
89 */
90 public function admin_actions() {
91 parent::admin_actions();
92 // $monetize_by = tutils()->get_option('monetize_by');
93 // admin actions will be here ...
94 // if (class_exists('WooCommerce') && $monetize_by == 'wc') {
95 // add_action('woocommerce_order_status_changed', array($this, 'status_transition'), 10, 4);
96 // }
97 }
98
99 /**
100 * Image action callback
101 *
102 * @param array $image_data
103 * @param array $data
104 * @param array $settings
105 * @return array
106 */
107 // @todo Something
108 public function notification_image($image_data, $data, $settings) {
109 if(!$settings['show_default_image']){
110 switch ($settings['show_notification_image']) {
111 case 'featured_image':
112 $image_data['url'] = get_the_post_thumbnail_url($data['product_id'], '_nx_notification_thumb' );
113 $image_data['alt'] = !empty($data['title']) ? $data['title'] : '';
114 break;
115 case 'gravatar':
116 if(!empty($data['user_id'])){
117 $image_data['url'] = get_avatar_url($data['user_id'], array('size' => '100'));
118 $image_data['alt'] = !empty($data['name']) ? $data['name'] : '';
119 }
120 }
121 }
122 return $image_data;
123 }
124
125 public function source_error_message($messages) {
126 if (!$this->class_exists()) {
127 $url = admin_url('plugin-install.php?s=learnpress&tab=search&type=term');
128 $messages[$this->id] = [
129 'message' => sprintf( '%s <a href="%s" target="_blank">%s</a> %s',
130 __( 'You have to install', 'notificationx' ),
131 $url,
132 __( 'LearnPress', 'notificationx' ),
133 __( 'plugin first.', 'notificationx' )
134 ),
135 'html' => true,
136 'type' => 'error',
137 'rules' => Rules::is('source', $this->id),
138 ];
139 }
140 return $messages;
141 }
142
143 /**
144 * This function is generate and save a new notification when user enroll in a new course
145 *
146 * @param int $user_id
147 * @param int $course_id
148 * @return void
149 */
150 public function save_new_enrollment($item_id, $item, $order_id) {
151 $single_notification = $this->ordered_product($item_id, $item, $order_id);
152 if (!empty($single_notification)) {
153 $key = $order_id . '-' . $item_id;
154 $this->update_notification(
155 array(
156 'source' => $this->id,
157 'entry_key' => $key,
158 'data' => $single_notification,
159 )
160 );
161 return true;
162 }
163 return false;
164 }
165
166 public function save_new_enroll_payment_status($payment_id, $new_status, $old_status) {
167 if ($new_status !== 'publish') {
168 return;
169 }
170
171 $data = array();
172 $offset = get_option('gmt_offset');
173 $payment = new \EDD_Payment($payment_id);
174 $cart_details = $payment->cart_details;
175 $user_info = $payment->user_info;
176
177 unset($user_info['id']);
178 unset($user_info['discount']);
179 unset($user_info['address']);
180
181 $user_info['name'] = $this->name($user_info['first_name'], $user_info['last_name']);
182 $user_info['timestamp'] = strtotime($payment->date) - ($offset * 60 * 60);
183 $user_info['ip'] = $payment->ip;
184
185 if (is_array($cart_details)) {
186 foreach ($cart_details as $cart_index => $download) {
187 $if_has_course = tutor_utils()->product_belongs_with_course($download['id']);
188 if ($if_has_course) {
189 $data['title'] = $download['name'];
190 $course_id = $if_has_course->post_id;
191 $data['link'] = get_permalink($course_id);
192 $data['product_id'] = $course_id;
193 $key = $payment->key . '-' . $download['id'];
194 $notification = array_merge($user_info, $data);
195 return $this->update_notification(
196 array(
197 'source' => $this->id,
198 'entry_key' => $key,
199 'data' => $notification,
200 )
201 );
202 }
203 }
204 }
205 }
206
207 public function do_enroll($course_id, $item, $id) {
208 $user_id = get_current_user_id();
209 $userdata = get_userdata($user_id);
210 $data = array();
211 if (isset($_SERVER['REMOTE_ADDR'])) {
212 $user_ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
213 $data['ip'] = $user_ip;
214 }
215 $data['first_name'] = $userdata->first_name;
216 $data['last_name'] = $userdata->last_name;
217 $data['name'] = $this->name($userdata->first_name, $userdata->last_name);
218 $data['email'] = $userdata->user_email;
219 $data['title'] = get_the_title($item['item_id']);
220 $data['link'] = get_permalink($item['item_id']);
221 $data['product_id'] = $item['item_id'];
222 $data['timestamp'] = time();
223
224 if (!empty($data)) {
225 $key = $item['item_id'] . '-' . $id;
226 return $this->update_notification(
227 array(
228 'source' => $this->id,
229 'entry_key' => $key,
230 'data' => $data,
231 )
232 );
233 }
234 }
235
236 public function ready_enrolled_data($enrolled_data) {
237 $userdata = get_userdata($enrolled_data->post_author);
238 $data = array();
239
240 $meta_info = metadata_exists('post', $enrolled_data->ID, '_tutor_enrolled_by_order_id');
241 $order_id = 0;
242 $user_data = $this->user_data_by($enrolled_data->post_author, $order_id);
243 // $order = learn_press_get_order($enrolled_data->ID);
244 // $items = $order->get_items();
245 // foreach($items as $item) {
246 $data['title'] = get_the_title($enrolled_data->post_parent);
247 $data['link'] = get_permalink($enrolled_data->post_parent);
248 $data['product_id'] = $enrolled_data->post_parent;
249 // }
250 $data['timestamp'] = get_gmt_from_date($enrolled_data->post_date);
251 $data = array_merge($data, $user_data);
252 if (!empty($data)) {
253 return $data;
254 }
255 }
256
257 protected function user_data_by($user_id = 0, $order_id = 0) {
258 $data = array();
259
260 if ($user_id != 0 && $order_id === 0) {
261 $userdata = get_userdata($user_id);
262 $data['first_name'] = $userdata->first_name;
263 $data['last_name'] = $userdata->last_name;
264 $data['name'] = ( empty( $userdata->first_name ) || empty( $userdata->last_name ) ) ? "Someone" : $this->name($userdata->first_name, $userdata->last_name);
265
266 $data['email'] = $userdata->user_email;
267 }
268
269 if ($order_id && class_exists('WC_Order')) {
270 $order = wc_get_order($order_id);
271 if ($order) {
272 $date = $order->get_date_created();
273 $first_name = $last_name = $email = '';
274 $first_name = $order->get_billing_first_name();
275 $last_name = $order->get_billing_last_name();
276 $email = $order->get_billing_email();
277 $data['first_name'] = $first_name;
278 $data['last_name'] = $last_name;
279 $data['name'] = $this->name($first_name, $last_name);
280 $data['email'] = $email;
281 $data['timestamp'] = $date->getTimestamp();
282 }
283 }
284 if ($order_id && function_exists('edd_get_payment_meta')) {
285 $offset = get_option('gmt_offset');
286 $payment_meta = edd_get_payment_meta($order_id);
287 if (is_array($payment_meta)) {
288 $user_info = $payment_meta['user_info'];
289 $date = $payment_meta['date'];
290
291 $data['name'] = $this->name($user_info['first_name'], $user_info['last_name']);
292 $data['first_name'] = $user_info['first_name'];
293 $data['last_name'] = $user_info['last_name'];
294 $data['email'] = $user_info['email'];
295 $data['timestamp'] = strtotime($date) - ($offset * 60 * 60);
296 }
297 }
298
299 return $data;
300 }
301
302 public function status_transition($id, $from, $to, $order) {
303 $items = $order->get_items();
304 $if_has_course = true;
305 $status = array('on-hold', 'cancelled', 'refunded', 'failed', 'pending', 'wcf-main-order');
306 $done = array('completed', 'processing');
307
308 if (in_array($from, $done) && in_array($to, $status)) {
309 foreach ($items as $item) {
310 if (function_exists('tutor_utils')) {
311 $if_has_course = tutor_utils()->product_belongs_with_course($item->get_product_id());
312 }
313 if (!$if_has_course) {
314 continue;
315 }
316 $key = $id . '-' . $item->get_id();
317 $this->delete_notification($key);
318 }
319 }
320
321 if (in_array($from, $status) && in_array($to, $done)) {
322 $orders = array();
323
324 foreach ($items as $item) {
325 $key = $id . '-' . $item->get_id();
326 if (function_exists('tutor_utils')) {
327 $if_has_course = tutor_utils()->product_belongs_with_course($item->get_product_id());
328 }
329 if (!$if_has_course) {
330 continue;
331 }
332
333 if (isset($this->notifications[$key])) {
334 continue;
335 }
336 $single_notification = $this->ordered_product($item->get_id(), $item, $order);
337 if (!empty($single_notification)) {
338 $this->update_notification(
339 array(
340 'source' => $this->id,
341 'entry_key' => $key,
342 'data' => $single_notification,
343 )
344 );
345 }
346 }
347 }
348
349 return;
350 }
351
352 /**
353 * This function is responsible for making ready the orders data.
354 *
355 * @param int $item_id
356 * @param WC_Order_Item_Product $item
357 * @param int $order_id
358 * @return void
359 */
360 public function ordered_product($item_id, $item, $order_id) {
361 if (!$item instanceof \WC_Order_Item_Product) {
362 return false;
363 }
364
365 $product_id = $item->get_product_id();
366 $if_has_course = tutor_utils()->product_belongs_with_course($product_id);
367 if (!$if_has_course) {
368 return false;
369 }
370
371 $new_order = array();
372 if (is_int($order_id)) {
373 $order = new \WC_Order($order_id);
374 $status = $order->get_status();
375 $done = array('completed', 'processing', 'pending');
376 if (!in_array($status, $done)) {
377 return false;
378 }
379 } else {
380 $order = $order_id;
381 }
382
383 $date = $order->get_date_created();
384 $countries = new \WC_Countries();
385 $shipping_country = $order->get_billing_country();
386 if (empty($shipping_country)) {
387 $shipping_country = $order->get_shipping_country();
388 }
389 if (!empty($shipping_country)) {
390 $new_order['country'] = isset($countries->countries[$shipping_country]) ? $countries->countries[$shipping_country] : '';
391 $shipping_state = $order->get_shipping_state();
392 if (!empty($shipping_state)) {
393 $new_order['state'] = isset($countries->states[$shipping_country], $countries->states[$shipping_country][$shipping_state]) ? $countries->states[$shipping_country][$shipping_state] : $shipping_state;
394 }
395 }
396 $new_order['city'] = $order->get_billing_city();
397 if (empty($new_order['city'])) {
398 $new_order['city'] = $order->get_shipping_city();
399 }
400
401 $new_order['ip'] = $order->get_customer_ip_address();
402 $product_data = $this->ready_product_data($item->get_data());
403 $course_id = $if_has_course->post_id;
404 if (!empty($product_data)) {
405 $new_order['id'] = is_int($order_id) ? $order_id : $order_id->get_id();
406 $new_order['product_id'] = $course_id;
407 $new_order['title'] = $product_data['title'];
408 }
409 $new_order['timestamp'] = $date->getTimestamp();
410 $new_order['link'] = get_permalink($course_id);
411
412 $data = array_merge($new_order, $this->buyer($order));
413 return $data;
414 }
415
416 /**
417 * It will take an array to make data clean
418 *
419 * @param array $data
420 * @return void
421 */
422 protected function ready_product_data($data) {
423 if (empty($data)) {
424 return;
425 }
426 return array(
427 'title' => $data['name'],
428 );
429 }
430
431 /**
432 * This function is responsible for getting
433 * the buyer name from order.
434 *
435 * @param WC_Order $order
436 * @return void
437 */
438 protected function buyer(\WC_Order $order) {
439 $first_name = $last_name = $email = '';
440 $buyer_data = array();
441
442 if (empty($first_name)) {
443 $first_name = $order->get_billing_first_name();
444 }
445
446 if (empty($last_name)) {
447 $last_name = $order->get_billing_last_name();
448 }
449
450 if (empty($email)) {
451 $email = $order->get_billing_email();
452 }
453
454 $buyer_data['first_name'] = $first_name;
455 $buyer_data['last_name'] = $last_name;
456 $buyer_data['name'] = $this->name($first_name, $last_name);
457 $buyer_data['email'] = $email;
458
459 return $buyer_data;
460 }
461
462 public function saved_post($post, $data, $nx_id) {
463 $this->delete_notification(null, $nx_id);
464 $this->get_notification_ready($data);
465 }
466
467 /**
468 * This function is responsible for making the notification ready for first time we make the notification.
469 *
470 * @param string $type
471 * @param array $data
472 * @return void
473 */
474 public function get_notification_ready($data = array()) {
475 $enrollments = $this->get_purchased_course($data);
476 if (!empty($enrollments)) {
477 // $enrollments = NotificationX_Helper::sortBy( $enrollments, 'tutor' );
478 $entries = [];
479 foreach ($enrollments as $key => $enrollment) {
480 $entries[] = array(
481 'nx_id' => $data['nx_id'],
482 'source' => $this->id,
483 'entry_key' => $key,
484 'data' => $enrollment,
485 );
486 }
487 $this->update_notifications($entries);
488 }
489 }
490 /**
491 * Get all the orders from database using a date query
492 * for limitation.
493 *
494 * @param array $data
495 * @return array
496 */
497 public function get_purchased_course($data = array()) {
498 if (empty($data)) {
499 return null;
500 }
501 $orders = array();
502 // $from = date(get_option('date_format'), strtotime('-' . intval($data['display_from']) . ' days'));
503 $from = gmdate('Y-m-d H:i:s', Helper::generate_time_string($data));
504 $enrolled = get_posts(
505 array(
506 'post_type' => 'lp_order',
507 'post_status' => 'lp-completed',
508 'date_query' => array(
509 array(
510 'after' => $from,
511 ),
512 ),
513 'numberposts' => -1,
514 'posts_per_page' => -1,
515 )
516 );
517 foreach ($enrolled as $single_enroll) {
518 $order = learn_press_get_order($single_enroll->ID);
519 $items = $order->get_items();
520 foreach($items as $item) {
521 $single_enroll->post_parent = $item['course_id'];
522 }
523 $orders[$single_enroll->post_parent . '-' . $single_enroll->ID] = $this->ready_enrolled_data($single_enroll);
524 }
525 wp_reset_postdata();
526 return $orders;
527 }
528
529 /**
530 * @param array $data
531 * @param array $saved_data
532 * @param stdClass $settings
533 * @return array
534 */
535 public function fallback_data($data, $saved_data, $settings) {
536 $data['name'] = __('Someone', 'notificationx');
537 $data['first_name'] = __('Someone', 'notificationx');
538 $data['last_name'] = __('Someone', 'notificationx');
539 $data['anonymous_title'] = __('Anonymous Product', 'notificationx');
540 $data['course_title'] = $saved_data['title'];
541 return $data;
542 }
543
544 public function doc(){
545 /* translators: %1$s: LearnPress LMS installed & configured link URL, %2$s: documentation link URL, %3$s: video tutorial link URL, %4$s: Integration with LearnPress LMS link URL */
546 return sprintf(__('<p>Make sure that you have <a href="%1$s" target="_blank">LearnPress LMS installed & configured</a> to use its campaign & course selling data. For further assistance, check out our step by step <a target="_blank" href="%2$s">documentation</a>.</p>
547 <p>🎦 Watch <a target="_blank" href="%3$s">video tutorial</a> to learn quickly</p>
548 <p>👉 NotificationX <a target="_blank" href="%4$s">Integration with LearnPress LMS</a></p>', 'notificationx'),
549 'https://wordpress.org/plugins/learnpress/',
550 'https://notificationx.com/docs/how-to-display-learnpress-course-enrollment-alert-using-notificationx/',
551 'https://notificationx.com/integrations/learnpress/',
552 'https://notificationx.com/integrations/learnpress/'
553 );
554 }
555 }
556