← All changes
|
integrations/learndash/includes/actions/mark-topic.php
+119
-0
5.8.3
→
6.0.3
View file →
| @@ -1,0 +1,119 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Mark Topic | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\LearnDash\Actions\Mark_Topic | |
| 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_Topic extends AutomatorWP_Integration_Action { | |
| 13 | + | |
| 14 | + public $integration = 'learndash'; | |
| 15 | + public $action = 'learndash_mark_topic'; | |
| 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 topic as completed', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'Mark topic 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' => __( 'Topic:', 'automatorwp' ), | |
| 35 | + 'option_none_label' => __( 'all topics', 'automatorwp' ), | |
| 36 | + 'option_custom' => true, | |
| 37 | + 'option_custom_desc' => __( 'Yopic ID', 'automatorwp' ), | |
| 38 | + 'post_type' => 'sfwd-topic', | |
| 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 | + $topic_id = $action_options['post']; | |
| 59 | + | |
| 60 | + // Courses have the following format: array( $course_id => array( $topic_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 topic | |
| 65 | + if( $topic_id !== 'any' ) { | |
| 66 | + | |
| 67 | + $topic = get_post( $topic_id ); | |
| 68 | + | |
| 69 | + // Bail if topic doesn't exists | |
| 70 | + if( ! $topic ) { | |
| 71 | + return; | |
| 72 | + } | |
| 73 | + | |
| 74 | + $course_id = learndash_get_course_id( $topic_id ); | |
| 75 | + | |
| 76 | + $courses = array( $course_id => array( $topic_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 topics completed (topics are separated in lessons) | |
| 86 | + foreach( $user_course['topics'] as $lesson_id => $user_topics ) { | |
| 87 | + | |
| 88 | + // Loop all lesson topics completed | |
| 89 | + foreach( $user_topics as $topic_id => $completed ) { | |
| 90 | + | |
| 91 | + // Initialize course if not exists | |
| 92 | + if( ! isset( $courses[$course_id] ) ) { | |
| 93 | + $courses[$course_id] = array(); | |
| 94 | + } | |
| 95 | + | |
| 96 | + $courses[$course_id][] = $topic_id; | |
| 97 | + | |
| 98 | + } | |
| 99 | + | |
| 100 | + } | |
| 101 | + } | |
| 102 | + | |
| 103 | + } | |
| 104 | + | |
| 105 | + // Loop courses | |
| 106 | + foreach( $courses as $course_id => $course_topics ) { | |
| 107 | + | |
| 108 | + // Mark topics as completed | |
| 109 | + foreach( $course_topics as $topic_id ) { | |
| 110 | + automatorwp_learndash_mark_topic_as_completed( $user_id, $topic_id, $course_id ); | |
| 111 | + } | |
| 112 | + | |
| 113 | + } | |
| 114 | + | |
| 115 | + } | |
| 116 | + | |
| 117 | +} | |
| 118 | + | |
| 119 | +new AutomatorWP_LearnDash_Mark_Topic(); | |