| @@ -1,0 +1,193 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Triggers | |
| 4 | + * | |
| 5 | + * @package GamiPress\WP_Courseware\Triggers | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Register WP Courseware specific triggers | |
| 14 | + * | |
| 15 | + * @param array $triggers | |
| 16 | + * @return mixed | |
| 17 | + */ | |
| 18 | +function gamipress_wpcw_activity_triggers( $triggers ) { | |
| 19 | + | |
| 20 | + | |
| 21 | + $triggers[__( 'WP Courseware', 'gamipress' )] = array( | |
| 22 | + 'gamipress_wpcw_complete_unit' => __( 'Complete a unit', 'gamipress' ), | |
| 23 | + 'gamipress_wpcw_complete_specific_unit' => __( 'Complete a specific unit', 'gamipress' ), | |
| 24 | + | |
| 25 | + 'gamipress_wpcw_complete_module' => __( 'Complete a module', 'gamipress' ), | |
| 26 | + 'gamipress_wpcw_complete_specific_module' => __( 'Complete a specific module', 'gamipress' ), | |
| 27 | + | |
| 28 | + 'gamipress_wpcw_complete_course' => __( 'Complete a course', 'gamipress' ), | |
| 29 | + 'gamipress_wpcw_complete_specific_course' => __( 'Complete a specific course', 'gamipress' ), | |
| 30 | + ); | |
| 31 | + | |
| 32 | + return $triggers; | |
| 33 | + | |
| 34 | +} | |
| 35 | +add_filter( 'gamipress_activity_triggers', 'gamipress_wpcw_activity_triggers' ); | |
| 36 | + | |
| 37 | +/** | |
| 38 | + * Register WP Courseware specific activity triggers | |
| 39 | + * | |
| 40 | + * @since 1.0.0 | |
| 41 | + * | |
| 42 | + * @param array $specific_activity_triggers | |
| 43 | + * @return array | |
| 44 | + */ | |
| 45 | +function gamipress_wpcw_specific_activity_triggers( $specific_activity_triggers ) { | |
| 46 | + | |
| 47 | + $specific_activity_triggers['gamipress_wpcw_complete_specific_unit'] = array( 'course_unit' ); | |
| 48 | + $specific_activity_triggers['gamipress_wpcw_complete_specific_module'] = array( 'wpcw_modules' ); | |
| 49 | + //$specific_activity_triggers['gamipress_wpcw_complete_specific_course'] = array( 'wpcw_courses' ); | |
| 50 | + $specific_activity_triggers['gamipress_wpcw_complete_specific_course'] = array( 'wpcw_course' ); | |
| 51 | + | |
| 52 | + return $specific_activity_triggers; | |
| 53 | +} | |
| 54 | +add_filter( 'gamipress_specific_activity_triggers', 'gamipress_wpcw_specific_activity_triggers' ); | |
| 55 | + | |
| 56 | +/** | |
| 57 | + * Get plugin specific activity trigger post title | |
| 58 | + * | |
| 59 | + * @since 1.0.0 | |
| 60 | + * | |
| 61 | + * @param string $post_title | |
| 62 | + * @param integer $specific_id | |
| 63 | + * @param string $trigger_type | |
| 64 | + * | |
| 65 | + * @return string | |
| 66 | + */ | |
| 67 | +function gamipress_wpcw_specific_activity_trigger_post_title( $post_title, $specific_id, $trigger_type ) { | |
| 68 | + | |
| 69 | + switch( $trigger_type ) { | |
| 70 | + case 'gamipress_wpcw_complete_specific_module': | |
| 71 | + if( absint( $specific_id ) !== 0 ) { | |
| 72 | + // Get the module title | |
| 73 | + $module_title = gamipress_wpcw_get_module_title( $specific_id ); | |
| 74 | + | |
| 75 | + $post_title = $module_title; | |
| 76 | + } | |
| 77 | + break; | |
| 78 | + } | |
| 79 | + | |
| 80 | + return $post_title; | |
| 81 | + | |
| 82 | +} | |
| 83 | +add_filter( 'gamipress_specific_activity_trigger_post_title', 'gamipress_wpcw_specific_activity_trigger_post_title', 10, 3 ); | |
| 84 | + | |
| 85 | +/** | |
| 86 | + * Register WP Courseware specific activity triggers labels | |
| 87 | + * | |
| 88 | + * @since 1.0.0 | |
| 89 | + * | |
| 90 | + * @param array $specific_activity_trigger_labels | |
| 91 | + * @return array | |
| 92 | + */ | |
| 93 | +function gamipress_wpcw_specific_activity_trigger_label( $specific_activity_trigger_labels ) { | |
| 94 | + | |
| 95 | + $specific_activity_trigger_labels['gamipress_wpcw_complete_specific_unit'] = __( 'Complete the unit %s', 'gamipress' ); | |
| 96 | + $specific_activity_trigger_labels['gamipress_wpcw_complete_specific_module'] = __( 'Complete the module %s', 'gamipress' ); | |
| 97 | + $specific_activity_trigger_labels['gamipress_wpcw_complete_specific_course'] = __( 'Complete the course %s', 'gamipress' ); | |
| 98 | + | |
| 99 | + return $specific_activity_trigger_labels; | |
| 100 | +} | |
| 101 | +add_filter( 'gamipress_specific_activity_trigger_label', 'gamipress_wpcw_specific_activity_trigger_label' ); | |
| 102 | + | |
| 103 | +/** | |
| 104 | + * Get user for a given trigger action. | |
| 105 | + * | |
| 106 | + * @since 1.0.0 | |
| 107 | + * | |
| 108 | + * @param integer $user_id user ID to override. | |
| 109 | + * @param string $trigger Trigger name. | |
| 110 | + * @param array $args Passed trigger args. | |
| 111 | + * @return integer User ID. | |
| 112 | + */ | |
| 113 | +function gamipress_wpcw_trigger_get_user_id( $user_id, $trigger, $args ) { | |
| 114 | + | |
| 115 | + switch ( $trigger ) { | |
| 116 | + case 'gamipress_wpcw_complete_unit': | |
| 117 | + case 'gamipress_wpcw_complete_specific_unit': | |
| 118 | + case 'gamipress_wpcw_complete_module': | |
| 119 | + case 'gamipress_wpcw_complete_specific_module': | |
| 120 | + case 'gamipress_wpcw_complete_course': | |
| 121 | + case 'gamipress_wpcw_complete_specific_course': | |
| 122 | + $user_id = $args[1]; | |
| 123 | + break; | |
| 124 | + } | |
| 125 | + | |
| 126 | + return $user_id; | |
| 127 | + | |
| 128 | +} | |
| 129 | +add_filter( 'gamipress_trigger_get_user_id', 'gamipress_wpcw_trigger_get_user_id', 10, 3 ); | |
| 130 | + | |
| 131 | +/** | |
| 132 | + * Get the id for a given specific trigger action. | |
| 133 | + * | |
| 134 | + * @since 1.0.0 | |
| 135 | + * | |
| 136 | + * @param integer $specific_id Specific ID. | |
| 137 | + * @param string $trigger Trigger name. | |
| 138 | + * @param array $args Passed trigger args. | |
| 139 | + * | |
| 140 | + * @return integer Specific ID. | |
| 141 | + */ | |
| 142 | +function gamipress_wpcw_specific_trigger_get_id( $specific_id, $trigger = '', $args = array() ) { | |
| 143 | + | |
| 144 | + switch ( $trigger ) { | |
| 145 | + case 'gamipress_wpcw_complete_specific_unit': | |
| 146 | + case 'gamipress_wpcw_complete_specific_module': | |
| 147 | + case 'gamipress_wpcw_complete_specific_course': | |
| 148 | + $specific_id = $args[0]; | |
| 149 | + break; | |
| 150 | + } | |
| 151 | + | |
| 152 | + return $specific_id; | |
| 153 | +} | |
| 154 | +add_filter( 'gamipress_specific_trigger_get_id', 'gamipress_wpcw_specific_trigger_get_id', 10, 3 ); | |
| 155 | + | |
| 156 | +/** | |
| 157 | + * Extended meta data for event trigger logging | |
| 158 | + * | |
| 159 | + * @since 1.0.0 | |
| 160 | + * | |
| 161 | + * @param array $log_meta | |
| 162 | + * @param integer $user_id | |
| 163 | + * @param string $trigger | |
| 164 | + * @param integer $site_id | |
| 165 | + * @param array $args | |
| 166 | + * | |
| 167 | + * @return array | |
| 168 | + */ | |
| 169 | +function gamipress_wpcw_log_event_trigger_meta_data( $log_meta, $user_id, $trigger, $site_id, $args ) { | |
| 170 | + | |
| 171 | + switch ( $trigger ) { | |
| 172 | + case 'gamipress_wpcw_complete_unit': | |
| 173 | + case 'gamipress_wpcw_complete_specific_unit': | |
| 174 | + // Add the quiz and course IDs | |
| 175 | + $log_meta['unit_id'] = $args[0]; | |
| 176 | + $log_meta['module_id'] = $args[2]; | |
| 177 | + $log_meta['course_id'] = $args[3]; | |
| 178 | + break; | |
| 179 | + case 'gamipress_wpcw_complete_module': | |
| 180 | + case 'gamipress_wpcw_complete_specific_module': | |
| 181 | + // Add the module IDs | |
| 182 | + $log_meta['module_id'] = $args[0]; | |
| 183 | + break; | |
| 184 | + case 'gamipress_wpcw_complete_course': | |
| 185 | + case 'gamipress_wpcw_complete_specific_course': | |
| 186 | + // Add the course IDs | |
| 187 | + $log_meta['course_id'] = $args[0]; | |
| 188 | + break; | |
| 189 | + } | |
| 190 | + | |
| 191 | + return $log_meta; | |
| 192 | +} | |
| 193 | +add_filter( 'gamipress_log_event_trigger_meta_data', 'gamipress_wpcw_log_event_trigger_meta_data', 10, 5 ); | |