← All changes
|
integrations/learndash/includes/actions/mark-lesson.php
+114
-0
5.7.3
→
6.0.3
View file →
| @@ -1,0 +1,114 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Mark Lesson | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\LearnDash\Actions\Mark_Lesson | |
| 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_LearnDash_Mark_Lesson extends AutomatorWP_Integration_Action { | |
| 13 | + | |
| 14 | + public $integration = 'learndash'; | |
| 15 | + public $action = 'learndash_mark_lesson'; | |
| 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' => __( 'Mark lesson as completed', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'Mark lesson as <strong>completed</strong>', 'automatorwp' ), | |
| 28 | + /* translators: %1$s: Post title. */ | |
| 29 | + 'edit_label' => sprintf( __( 'Mark %1$s as completed', 'automatorwp' ), '{post}' ), | |
| 30 | + /* translators: %1$s: Post title. */ | |
| 31 | + 'log_label' => sprintf( __( 'Mark %1$s as completed', 'automatorwp' ), '{post}' ), | |
| 32 | + 'options' => array( | |
| 33 | + 'post' => automatorwp_utilities_post_option( array( | |
| 34 | + 'name' => __( 'Lesson:', 'automatorwp' ), | |
| 35 | + 'option_none_label' => __( 'all lessons', 'automatorwp' ), | |
| 36 | + 'option_custom' => true, | |
| 37 | + 'option_custom_desc' => __( 'Lesson ID', 'automatorwp' ), | |
| 38 | + 'post_type' => 'sfwd-lessons', | |
| 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 | + $lesson_id = $action_options['post']; | |
| 59 | + | |
| 60 | + // Courses have the following format: array( $course_id => array( $lesson_id ) ) | |
| 61 | + // This way has been used to reduce the number of queries needed at the end of this execution | |
| 62 | + $courses = array(); | |
| 63 | + | |
| 64 | + // Check specific lesson | |
| 65 | + if( $lesson_id !== 'any' ) { | |
| 66 | + | |
| 67 | + $lesson = get_post( $lesson_id ); | |
| 68 | + | |
| 69 | + // Bail if lesson doesn't exists | |
| 70 | + if( ! $lesson ) { | |
| 71 | + return; | |
| 72 | + } | |
| 73 | + | |
| 74 | + $course_id = learndash_get_course_id( $lesson_id ); | |
| 75 | + | |
| 76 | + $courses = array( $course_id => array( $lesson_id ) ); | |
| 77 | + | |
| 78 | + } else { | |
| 79 | + | |
| 80 | + // Get all user courses | |
| 81 | + $user_courses = get_user_meta( $user_id, '_sfwd-course_progress', true ); | |
| 82 | + | |
| 83 | + foreach( $user_courses as $course_id => $user_course ) { | |
| 84 | + | |
| 85 | + // Loop all lessons completed | |
| 86 | + foreach( $user_course['lessons'] as $lesson_id => $completed ) { | |
| 87 | + | |
| 88 | + // Initialize course if not exists | |
| 89 | + if( ! isset( $courses[$course_id] ) ) { | |
| 90 | + $courses[$course_id] = array(); | |
| 91 | + } | |
| 92 | + | |
| 93 | + $courses[$course_id][] = $lesson_id; | |
| 94 | + | |
| 95 | + } | |
| 96 | + } | |
| 97 | + | |
| 98 | + } | |
| 99 | + | |
| 100 | + // Loop courses | |
| 101 | + foreach( $courses as $course_id => $course_lessons ) { | |
| 102 | + | |
| 103 | + // Mark lessons as completed | |
| 104 | + foreach( $course_lessons as $lesson_id ) { | |
| 105 | + automatorwp_learndash_mark_lesson_as_completed( $user_id, $lesson_id, $course_id ); | |
| 106 | + } | |
| 107 | + | |
| 108 | + } | |
| 109 | + | |
| 110 | + } | |
| 111 | + | |
| 112 | +} | |
| 113 | + | |
| 114 | +new AutomatorWP_LearnDash_Mark_Lesson(); | |