← All changes
|
integrations/fluentcommunity/includes/triggers/complete-course.php
+184
-0
5.7.2
→
6.0.3
View file →
| @@ -1,0 +1,184 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Complete Course | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\FluentCommunity\Triggers\Complete_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_FluentCommunity_Complete_Course extends AutomatorWP_Integration_Trigger { | |
| 13 | + | |
| 14 | + public $integration = 'fluentcommunity'; | |
| 15 | + public $trigger = 'fluentcommunity_complete_course'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Register the trigger | |
| 19 | + * | |
| 20 | + * @since 1.0.0 | |
| 21 | + */ | |
| 22 | + public function register() { | |
| 23 | + | |
| 24 | + automatorwp_register_trigger($this->trigger, array( | |
| 25 | + 'integration' => $this->integration, | |
| 26 | + 'label' => __( 'User completes a course', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'User completes a <strong>course</strong>', 'automatorwp' ), | |
| 28 | + /* translators: %1$s: Course title. %2$s: Number of times. */ | |
| 29 | + 'edit_label' => sprintf( __('User completes %1$s %2$s time(s)', 'automatorwp' ), '{course}','{times}' ), | |
| 30 | + /* translators: %1$s: Course title. */ | |
| 31 | + 'log_label' => sprintf( __('User completes %1$s', 'automatorwp' ), '{course}' ), | |
| 32 | + 'action' => 'fluent_community/course/completed', | |
| 33 | + 'function' => array($this, 'listener'), | |
| 34 | + 'priority' => 10, | |
| 35 | + 'accepted_args' => 2, | |
| 36 | + 'options' => array( | |
| 37 | + 'course' => automatorwp_utilities_ajax_selector_option( array( | |
| 38 | + 'field' => 'course', | |
| 39 | + 'name' => __( 'Course:', 'automatorwp' ), | |
| 40 | + 'option_none_value' => 'any', | |
| 41 | + 'option_none_label' => __( 'any course', 'automatorwp' ), | |
| 42 | + 'action_cb' => 'automatorwp_fluentcommunity_get_courses', | |
| 43 | + 'options_cb' => 'automatorwp_fluentcommunity_options_cb_course', | |
| 44 | + 'default' => 'any' | |
| 45 | + ) ), | |
| 46 | + 'times' => automatorwp_utilities_times_option(), | |
| 47 | + ), | |
| 48 | + 'tags' => array_merge( | |
| 49 | + automatorwp_utilities_times_tag() | |
| 50 | + ) | |
| 51 | + )); | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Trigger listener | |
| 56 | + * | |
| 57 | + * @since 1.0.0 | |
| 58 | + * | |
| 59 | + * @param object $course | |
| 60 | + * @param int $userId | |
| 61 | + */ | |
| 62 | + public function listener( $course, $userId ) { | |
| 63 | + | |
| 64 | + $course_data = $course->getOriginal(); | |
| 65 | + | |
| 66 | + // Trigger event when user completes a course | |
| 67 | + automatorwp_trigger_event(array( | |
| 68 | + 'trigger' => $this->trigger, | |
| 69 | + 'user_id' => $userId, | |
| 70 | + 'course' => $course_data['title'], | |
| 71 | + 'course_id' => $course_data['id'], | |
| 72 | + )); | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * User deserves check | |
| 77 | + * | |
| 78 | + * @since 1.0.0 | |
| 79 | + * | |
| 80 | + * @param bool $deserves_trigger True if user deserves trigger, false otherwise | |
| 81 | + * @param stdClass $trigger The trigger object | |
| 82 | + * @param int $user_id The user ID | |
| 83 | + * @param array $event Event information | |
| 84 | + * @param array $trigger_options The trigger's stored options | |
| 85 | + * @param stdClass $automation The trigger's automation object | |
| 86 | + * | |
| 87 | + * @return bool True if user deserves trigger, false otherwise | |
| 88 | + */ | |
| 89 | + | |
| 90 | + | |
| 91 | + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 92 | + | |
| 93 | + // Don't deserve if post is not received | |
| 94 | + if( ! isset( $event['course_id'] ) ) { | |
| 95 | + return false; | |
| 96 | + } | |
| 97 | + | |
| 98 | + // Don't deserve if post doesn't match with the trigger option | |
| 99 | + if( $trigger_options['course'] !== 'any' && absint( $trigger_options['course'] ) !== absint( $event['course_id'] ) ) { | |
| 100 | + return false; | |
| 101 | + } | |
| 102 | + | |
| 103 | + return $deserves_trigger; | |
| 104 | + | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Register the required hooks | |
| 109 | + * | |
| 110 | + * @since 1.0.0 | |
| 111 | + */ | |
| 112 | + public function hooks() { | |
| 113 | + | |
| 114 | + // Log meta data | |
| 115 | + add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); | |
| 116 | + | |
| 117 | + // Log fields | |
| 118 | + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); | |
| 119 | + | |
| 120 | + parent::hooks(); | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Trigger custom log meta | |
| 125 | + * | |
| 126 | + * @since 1.0.0 | |
| 127 | + * | |
| 128 | + * @param array $log_meta Log meta data | |
| 129 | + * @param stdClass $trigger The trigger object | |
| 130 | + * @param int $user_id The user ID | |
| 131 | + * @param array $event Event information | |
| 132 | + * @param array $trigger_options The trigger's stored options | |
| 133 | + * @param stdClass $automation The trigger's automation object | |
| 134 | + * | |
| 135 | + * @return array | |
| 136 | + */ | |
| 137 | + function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 138 | + | |
| 139 | + // Bail if action type don't match this action | |
| 140 | + if( $trigger->type !== $this->trigger ) { | |
| 141 | + return $log_meta; | |
| 142 | + } | |
| 143 | + | |
| 144 | + $log_meta['course'] = ( isset( $event['course'] ) ? $event['course'] : array() ); | |
| 145 | + | |
| 146 | + return $log_meta; | |
| 147 | + | |
| 148 | + } | |
| 149 | + | |
| 150 | + /** | |
| 151 | + * Action custom log fields | |
| 152 | + * | |
| 153 | + * @since 1.0.0 | |
| 154 | + * | |
| 155 | + * @param array $log_fields The log fields | |
| 156 | + * @param stdClass $log The log object | |
| 157 | + * @param stdClass $object The trigger/action/automation object attached to the log | |
| 158 | + * | |
| 159 | + * @return array | |
| 160 | + */ | |
| 161 | + public function log_fields( $log_fields, $log, $object ) { | |
| 162 | + | |
| 163 | + // Bail if log is not assigned to an trigger | |
| 164 | + if( $log->type !== 'trigger' ) { | |
| 165 | + return $log_fields; | |
| 166 | + } | |
| 167 | + | |
| 168 | + // Bail if trigger type don't match this trigger | |
| 169 | + if( $object->type !== $this->trigger ) { | |
| 170 | + return $log_fields; | |
| 171 | + } | |
| 172 | + | |
| 173 | + $log_fields['course'] = array( | |
| 174 | + 'name' => __( 'Course completed', 'automatorwp' ), | |
| 175 | + 'type' => 'text', | |
| 176 | + ); | |
| 177 | + | |
| 178 | + return $log_fields; | |
| 179 | + | |
| 180 | + } | |
| 181 | + | |
| 182 | +} | |
| 183 | + | |
| 184 | +new AutomatorWP_FluentCommunity_Complete_Course(); | |