| @@ -1,0 +1,259 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Triggers | |
| 4 | + * | |
| 5 | + * @package GamiPress\AmeliaBooking\Triggers | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Register plugin activity triggers | |
| 14 | + * | |
| 15 | + * @param array $triggers | |
| 16 | + * @return mixed | |
| 17 | + */ | |
| 18 | +function gamipress_ameliabooking_activity_triggers( $triggers ) { | |
| 19 | + | |
| 20 | + // AmeliaBooking | |
| 21 | + $triggers[__( 'Amelia', 'gamipress' )] = array( | |
| 22 | + // Appointments | |
| 23 | + 'gamipress_ameliabooking_user_books_appointment' => __( 'Book an appointment for any service', 'gamipress' ), | |
| 24 | + 'gamipress_ameliabooking_user_books_appointment_service' => __( 'Book an appointment for a specific service', 'gamipress' ), | |
| 25 | + 'gamipress_ameliabooking_user_cancels_appointment' => __( 'Cancel an appointment for any service', 'gamipress' ), | |
| 26 | + 'gamipress_ameliabooking_user_cancels_appointment_service' => __( 'Cancel an appointment for a specific service', 'gamipress' ), | |
| 27 | + // Events | |
| 28 | + 'gamipress_ameliabooking_user_books_event' => __( 'Book for any event', 'gamipress' ), | |
| 29 | + 'gamipress_ameliabooking_user_books_specific_event' => __( 'Book for a specific event', 'gamipress' ), | |
| 30 | + 'gamipress_ameliabooking_user_cancels_event' => __( 'Cancel for any event', 'gamipress' ), | |
| 31 | + 'gamipress_ameliabooking_user_cancels_specific_event' => __( 'Cancel for a specific event', 'gamipress' ), | |
| 32 | + ); | |
| 33 | + | |
| 34 | + return $triggers; | |
| 35 | + | |
| 36 | +} | |
| 37 | +add_filter( 'gamipress_activity_triggers', 'gamipress_ameliabooking_activity_triggers' ); | |
| 38 | + | |
| 39 | +/** | |
| 40 | + * Register specific activity triggers | |
| 41 | + * | |
| 42 | + * @since 1.0.0 | |
| 43 | + * | |
| 44 | + * @param array $specific_activity_triggers | |
| 45 | + * @return array | |
| 46 | + */ | |
| 47 | +function gamipress_ameliabooking_specific_activity_triggers( $specific_activity_triggers ) { | |
| 48 | + | |
| 49 | + $specific_activity_triggers['gamipress_ameliabooking_user_books_appointment_service'] = array( 'ameliabooking_appointment' ); | |
| 50 | + $specific_activity_triggers['gamipress_ameliabooking_user_books_specific_event'] = array( 'ameliabooking_event' ); | |
| 51 | + $specific_activity_triggers['gamipress_ameliabooking_user_cancels_appointment_service'] = array( 'ameliabooking_appointment' ); | |
| 52 | + $specific_activity_triggers['gamipress_ameliabooking_user_cancels_specific_event'] = array( 'ameliabooking_event' ); | |
| 53 | + | |
| 54 | + return $specific_activity_triggers; | |
| 55 | +} | |
| 56 | +add_filter( 'gamipress_specific_activity_triggers', 'gamipress_ameliabooking_specific_activity_triggers' ); | |
| 57 | + | |
| 58 | +/** | |
| 59 | + * Register specific activity triggers labels | |
| 60 | + * | |
| 61 | + * @since 1.0.0 | |
| 62 | + * | |
| 63 | + * @param array $specific_activity_trigger_labels | |
| 64 | + * @return array | |
| 65 | + */ | |
| 66 | +function gamipress_ameliabooking_specific_activity_trigger_label( $specific_activity_trigger_labels ) { | |
| 67 | + | |
| 68 | + $specific_activity_trigger_labels['gamipress_ameliabooking_user_books_appointment_service'] = __( 'Book an appointment for %s service', 'gamipress' ); | |
| 69 | + $specific_activity_trigger_labels['gamipress_ameliabooking_user_books_specific_event'] = __( 'Book for %s event', 'gamipress' ); | |
| 70 | + $specific_activity_trigger_labels['gamipress_ameliabooking_user_cancels_appointment_service'] = __( 'Cancel an appointment for %s service', 'gamipress' ); | |
| 71 | + $specific_activity_trigger_labels['gamipress_ameliabooking_user_cancels_specific_event'] = __( 'Cancel for %s event', 'gamipress' ); | |
| 72 | + | |
| 73 | + return $specific_activity_trigger_labels; | |
| 74 | +} | |
| 75 | +add_filter( 'gamipress_specific_activity_trigger_label', 'gamipress_ameliabooking_specific_activity_trigger_label' ); | |
| 76 | + | |
| 77 | +/** | |
| 78 | + * Get plugin specific activity trigger post title | |
| 79 | + * | |
| 80 | + * @since 1.0.0 | |
| 81 | + * | |
| 82 | + * @param string $post_title | |
| 83 | + * @param integer $specific_id | |
| 84 | + * @param string $trigger_type | |
| 85 | + * | |
| 86 | + * @return string | |
| 87 | + */ | |
| 88 | +function gamipress_ameliabooking_specific_activity_trigger_post_title( $post_title, $specific_id, $trigger_type ) { | |
| 89 | + | |
| 90 | + switch( $trigger_type ) { | |
| 91 | + case 'gamipress_ameliabooking_user_books_appointment_service': | |
| 92 | + if( absint( $specific_id ) !== 0 ) { | |
| 93 | + // Get the service title | |
| 94 | + $service_title = gamipress_ameliabooking_get_service_title( $specific_id ); | |
| 95 | + | |
| 96 | + $post_title = $service_title; | |
| 97 | + } | |
| 98 | + break; | |
| 99 | + case 'gamipress_ameliabooking_user_books_specific_event': | |
| 100 | + if( absint( $specific_id ) !== 0 ) { | |
| 101 | + // Get the event title | |
| 102 | + $event_title = gamipress_ameliabooking_get_event_title( $specific_id ); | |
| 103 | + | |
| 104 | + $post_title = $event_title; | |
| 105 | + } | |
| 106 | + break; | |
| 107 | + case 'gamipress_ameliabooking_user_cancels_appointment_service': | |
| 108 | + if( absint( $specific_id ) !== 0 ) { | |
| 109 | + // Get the service title | |
| 110 | + $service_title = gamipress_ameliabooking_get_service_title( $specific_id ); | |
| 111 | + | |
| 112 | + $post_title = $service_title; | |
| 113 | + } | |
| 114 | + break; | |
| 115 | + case 'gamipress_ameliabooking_user_cancels_specific_event': | |
| 116 | + if( absint( $specific_id ) !== 0 ) { | |
| 117 | + // Get the event title | |
| 118 | + $event_title = gamipress_ameliabooking_get_event_title( $specific_id ); | |
| 119 | + | |
| 120 | + $post_title = $event_title; | |
| 121 | + } | |
| 122 | + break; | |
| 123 | + } | |
| 124 | + | |
| 125 | + return $post_title; | |
| 126 | + | |
| 127 | +} | |
| 128 | +add_filter( 'gamipress_specific_activity_trigger_post_title', 'gamipress_ameliabooking_specific_activity_trigger_post_title', 10, 3 ); | |
| 129 | + | |
| 130 | +/** | |
| 131 | + * Get plugin specific activity trigger permalink | |
| 132 | + * | |
| 133 | + * @since 1.0.0 | |
| 134 | + * | |
| 135 | + * @param string $permalink | |
| 136 | + * @param integer $specific_id | |
| 137 | + * @param string $trigger_type | |
| 138 | + * @param integer $site_id | |
| 139 | + * | |
| 140 | + * @return string | |
| 141 | + */ | |
| 142 | +function gamipress_ameliabooking_specific_activity_trigger_permalink( $permalink, $specific_id, $trigger_type, $site_id ) { | |
| 143 | + | |
| 144 | + switch( $trigger_type ) { | |
| 145 | + case 'gamipress_ameliabooking_user_books_appointment_service': | |
| 146 | + case 'gamipress_ameliabooking_user_books_specific_event': | |
| 147 | + case 'gamipress_ameliabooking_user_cancels_appointment_service': | |
| 148 | + case 'gamipress_ameliabooking_user_cancels_specific_event': | |
| 149 | + $permalink = ''; | |
| 150 | + break; | |
| 151 | + } | |
| 152 | + | |
| 153 | + return $permalink; | |
| 154 | + | |
| 155 | +} | |
| 156 | +add_filter( 'gamipress_specific_activity_trigger_permalink', 'gamipress_ameliabooking_specific_activity_trigger_permalink', 10, 4 ); | |
| 157 | + | |
| 158 | +/** | |
| 159 | + * Get user for a given trigger action. | |
| 160 | + * | |
| 161 | + * @since 1.0.0 | |
| 162 | + * | |
| 163 | + * @param integer $user_id user ID to override. | |
| 164 | + * @param string $trigger Trigger name. | |
| 165 | + * @param array $args Passed trigger args. | |
| 166 | + * @return integer User ID. | |
| 167 | + */ | |
| 168 | +function gamipress_ameliabooking_trigger_get_user_id( $user_id, $trigger, $args ) { | |
| 169 | + | |
| 170 | + switch ( $trigger ) { | |
| 171 | + // Appointments | |
| 172 | + case 'gamipress_ameliabooking_user_books_appointment': | |
| 173 | + case 'gamipress_ameliabooking_user_books_appointment_service': | |
| 174 | + case 'gamipress_ameliabooking_user_cancels_appointment': | |
| 175 | + case 'gamipress_ameliabooking_user_cancels_appointment_service': | |
| 176 | + // Events | |
| 177 | + case 'gamipress_ameliabooking_user_books_event': | |
| 178 | + case 'gamipress_ameliabooking_user_books_specific_event': | |
| 179 | + case 'gamipress_ameliabooking_user_cancels_event': | |
| 180 | + case 'gamipress_ameliabooking_user_cancels_specific_event': | |
| 181 | + $user_id = $args[1]; | |
| 182 | + break; | |
| 183 | + } | |
| 184 | + | |
| 185 | + return $user_id; | |
| 186 | + | |
| 187 | +} | |
| 188 | +add_filter( 'gamipress_trigger_get_user_id', 'gamipress_ameliabooking_trigger_get_user_id', 10, 3 ); | |
| 189 | + | |
| 190 | +/** | |
| 191 | + * Get the id for a given specific trigger action. | |
| 192 | + * | |
| 193 | + * @since 1.0.0 | |
| 194 | + * | |
| 195 | + * @param integer $specific_id Specific ID to override. | |
| 196 | + * @param string $trigger Trigger name. | |
| 197 | + * @param array $args Passed trigger args. | |
| 198 | + * | |
| 199 | + * @return integer Specific ID. | |
| 200 | + */ | |
| 201 | +function gamipress_ameliabooking_specific_trigger_get_id( $specific_id, $trigger, $args ) { | |
| 202 | + | |
| 203 | + switch ( $trigger ) { | |
| 204 | + case 'gamipress_ameliabooking_user_books_appointment_service': | |
| 205 | + case 'gamipress_ameliabooking_user_cancels_appointment_service': | |
| 206 | + $specific_id = $args[2]; | |
| 207 | + break; | |
| 208 | + case 'gamipress_ameliabooking_user_books_specific_event': | |
| 209 | + case 'gamipress_ameliabooking_user_cancels_specific_event': | |
| 210 | + $specific_id = $args[0]; | |
| 211 | + break; | |
| 212 | + } | |
| 213 | + | |
| 214 | + return $specific_id; | |
| 215 | + | |
| 216 | +} | |
| 217 | + | |
| 218 | +add_filter( 'gamipress_specific_trigger_get_id', 'gamipress_ameliabooking_specific_trigger_get_id', 10, 3 ); | |
| 219 | + | |
| 220 | +/** | |
| 221 | + * Extended meta data for event trigger logging | |
| 222 | + * | |
| 223 | + * @since 1.0.2 | |
| 224 | + * | |
| 225 | + * @param array $log_meta | |
| 226 | + * @param integer $user_id | |
| 227 | + * @param string $trigger | |
| 228 | + * @param integer $site_id | |
| 229 | + * @param array $args | |
| 230 | + * | |
| 231 | + * @return array | |
| 232 | + */ | |
| 233 | +function gamipress_ameliabooking_log_event_trigger_meta_data( $log_meta, $user_id, $trigger, $site_id, $args ) { | |
| 234 | + | |
| 235 | + switch ( $trigger ) { | |
| 236 | + | |
| 237 | + // Appointments | |
| 238 | + case 'gamipress_ameliabooking_user_books_appointment': | |
| 239 | + case 'gamipress_ameliabooking_user_books_appointment_service': | |
| 240 | + case 'gamipress_ameliabooking_user_cancels_appointment': | |
| 241 | + case 'gamipress_ameliabooking_user_cancels_appointment_service': | |
| 242 | + // Add the appointment and service IDs | |
| 243 | + $log_meta['appointment_id'] = $args[0]; | |
| 244 | + $log_meta['service_id'] = $args[2]; | |
| 245 | + break; | |
| 246 | + | |
| 247 | + // Events | |
| 248 | + case 'gamipress_ameliabooking_user_books_event': | |
| 249 | + case 'gamipress_ameliabooking_user_books_specific_event': | |
| 250 | + case 'gamipress_ameliabooking_user_cancels_event': | |
| 251 | + case 'gamipress_ameliabooking_user_cancels_specific_event': | |
| 252 | + // Add the event ID | |
| 253 | + $log_meta['event_id'] = $args[0]; | |
| 254 | + break; | |
| 255 | + } | |
| 256 | + | |
| 257 | + return $log_meta; | |
| 258 | +} | |
| 259 | +add_filter( 'gamipress_log_event_trigger_meta_data', 'gamipress_ameliabooking_log_event_trigger_meta_data', 10, 5 ); | |