← All changes
|
integrations/learnpress/includes/actions/user-course.php
+149
-0
5.6.1
→
6.0.3
View file →
| @@ -1,0 +1,149 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * User Course | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\LearnPress\Actions\User_Course | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_LearnPress_User_Course extends AutomatorWP_Integration_Action { | |
| 13 | + | |
| 14 | + public $integration = 'learnpress'; | |
| 15 | + public $action = 'learnpress_user_course'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Register the trigger | |
| 19 | + * | |
| 20 | + * @since 1.0.0 | |
| 21 | + */ | |
| 22 | + public function register() { | |
| 23 | + | |
| 24 | + automatorwp_register_action( $this->action, array( | |
| 25 | + 'integration' => $this->integration, | |
| 26 | + 'label' => __( 'Enroll user from a course', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'Enroll user from <strong>a course</strong>', 'automatorwp' ), | |
| 28 | + /* translators: %1$s: Post title. */ | |
| 29 | + 'edit_label' => sprintf( __( 'Enroll user to %1$s', 'automatorwp' ), '{post}' ), | |
| 30 | + /* translators: %1$s: Post title. */ | |
| 31 | + 'log_label' => sprintf( __( 'Enroll user to %1$s', 'automatorwp' ), '{post}' ), | |
| 32 | + 'options' => array( | |
| 33 | + 'post' => automatorwp_utilities_post_option( array( | |
| 34 | + 'name' => __( 'Course:', 'automatorwp' ), | |
| 35 | + 'option_none_label' => __( 'all courses', 'automatorwp' ), | |
| 36 | + 'option_custom' => true, | |
| 37 | + 'option_custom_desc' => __( 'Course ID', 'automatorwp' ), | |
| 38 | + 'post_type' => LP_COURSE_CPT, | |
| 39 | + ) ), | |
| 40 | + ), | |
| 41 | + ) ); | |
| 42 | + | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Action execution function | |
| 47 | + * | |
| 48 | + * @since 1.0.0 | |
| 49 | + * | |
| 50 | + * @param stdClass $action The action object | |
| 51 | + * @param int $user_id The user ID | |
| 52 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 53 | + * @param stdClass $automation The action's automation object | |
| 54 | + */ | |
| 55 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 56 | + | |
| 57 | + // Shorthand | |
| 58 | + $course_id = $action_options['post']; | |
| 59 | + | |
| 60 | + $user = learn_press_get_user( $user_id ); | |
| 61 | + | |
| 62 | + // Bail if can't find the user | |
| 63 | + if( ! $user ) { | |
| 64 | + return; | |
| 65 | + } | |
| 66 | + | |
| 67 | + $courses = array(); | |
| 68 | + | |
| 69 | + // Check specific course | |
| 70 | + if( $course_id !== 'any' ) { | |
| 71 | + | |
| 72 | + $course = get_post( $course_id ); | |
| 73 | + | |
| 74 | + // Bail if course doesn't exists | |
| 75 | + if( ! $course ) { | |
| 76 | + return; | |
| 77 | + } | |
| 78 | + | |
| 79 | + $courses = array( $course_id ); | |
| 80 | + | |
| 81 | + } else { | |
| 82 | + // If enrolling to all courses, get all courses | |
| 83 | + | |
| 84 | + $query = new WP_Query( array( | |
| 85 | + 'post_type' => LP_COURSE_CPT, | |
| 86 | + 'post_status' => 'publish', | |
| 87 | + 'fields' => 'ids', | |
| 88 | + 'nopaging' => true, | |
| 89 | + ) ); | |
| 90 | + | |
| 91 | + $courses = $query->get_posts(); | |
| 92 | + } | |
| 93 | + | |
| 94 | + // Enroll user in courses | |
| 95 | + foreach( $courses as $course_id ) { | |
| 96 | + | |
| 97 | + // Skip if user is already on this course | |
| 98 | + if( $user->has_enrolled_course( $course_id ) ) { | |
| 99 | + continue; | |
| 100 | + } | |
| 101 | + | |
| 102 | + $course = learn_press_get_course( $course_id ); | |
| 103 | + | |
| 104 | + // Skip if course not exists | |
| 105 | + if( ! $course ) { | |
| 106 | + continue; | |
| 107 | + } | |
| 108 | + | |
| 109 | + // Skip if course not exists | |
| 110 | + if( ! $course->exists() ) { | |
| 111 | + continue; | |
| 112 | + } | |
| 113 | + | |
| 114 | + // Create a new order | |
| 115 | + $order = new LP_Order(); | |
| 116 | + $order->set_customer_note( __( 'Order created by AutomatorWP', 'automatorwp' ) ); | |
| 117 | + $order->set_status( LP_ORDER_PENDING ); | |
| 118 | + $order->set_user_id( $user_id ); | |
| 119 | + $order->set_user_ip_address( learn_press_get_ip() ); | |
| 120 | + $order->set_user_agent( learn_press_get_user_agent() ); | |
| 121 | + $order->set_created_via( 'AutomatorWP' ); | |
| 122 | + $order->set_subtotal( 0 ); | |
| 123 | + $order->set_total( 0 ); | |
| 124 | + | |
| 125 | + // Save the order | |
| 126 | + $order_id = $order->save(); | |
| 127 | + | |
| 128 | + // Add the course as order item | |
| 129 | + $order_item = array( | |
| 130 | + 'order_item_name' => $course->get_title(), | |
| 131 | + 'item_id' => $course_id, | |
| 132 | + 'quantity' => 1, | |
| 133 | + 'subtotal' => 0, | |
| 134 | + 'total' => 0, | |
| 135 | + ); | |
| 136 | + | |
| 137 | + // Save the order item | |
| 138 | + $item_id = $order->add_item( $order_item, 1 ); | |
| 139 | + | |
| 140 | + // Force the order status update | |
| 141 | + $order->update_status( 'completed' ); | |
| 142 | + | |
| 143 | + } | |
| 144 | + | |
| 145 | + } | |
| 146 | + | |
| 147 | +} | |
| 148 | + | |
| 149 | +new AutomatorWP_LearnPress_User_Course(); | |