| @@ -1,0 +1,347 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Triggers | |
| 4 | + * | |
| 5 | + * @package GamiPress\WP_Job_Manager\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 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @param array $triggers | |
| 18 | + * @return mixed | |
| 19 | + */ | |
| 20 | +function gamipress_wp_job_manager_activity_triggers( $triggers ) { | |
| 21 | + | |
| 22 | + $triggers[__( 'WP Job Manager', 'gamipress' )] = array( | |
| 23 | + 'gamipress_publish_job_listing' => __( 'Publish a job', 'gamipress' ), | |
| 24 | + 'gamipress_wp_job_manager_publish_job_specific_type' => __( 'Publish a job of a specific type', 'gamipress' ), | |
| 25 | + 'gamipress_wp_job_manager_mark_filled' => __( 'Mark a job of as filled', 'gamipress' ), | |
| 26 | + 'gamipress_wp_job_manager_mark_filled_specific_type' => __( 'Mark a job of a specific type of as filled', 'gamipress' ), | |
| 27 | + 'gamipress_wp_job_manager_mark_not_filled' => __( 'Mark a job of as not filled', 'gamipress' ), | |
| 28 | + 'gamipress_wp_job_manager_mark_not_filled_specific_type' => __( 'Mark a job of a specific type of as not filled', 'gamipress' ), | |
| 29 | + // Applications | |
| 30 | + 'gamipress_wp_job_manager_job_application' => __( 'Apply to job', 'gamipress' ), | |
| 31 | + 'gamipress_wp_job_manager_job_application_specific_type' => __( 'Apply to job of a specific type', 'gamipress' ), | |
| 32 | + 'gamipress_wp_job_manager_get_job_application' => __( 'Receive an application', 'gamipress' ), | |
| 33 | + 'gamipress_wp_job_manager_get_job_application_specific_type' => __( 'Receive an application on a job of a specific type', 'gamipress' ), | |
| 34 | + 'gamipress_wp_job_manager_job_application_hired' => __( 'Get hired on a job', 'gamipress' ), | |
| 35 | + 'gamipress_wp_job_manager_job_application_hired_specific_type' => __( 'Get hired on a job of a specific type', 'gamipress' ), | |
| 36 | + 'gamipress_wp_job_manager_job_application_rejected' => __( 'Get an application rejected', 'gamipress' ), | |
| 37 | + 'gamipress_wp_job_manager_job_application_rejected_specific_type' => __( 'Get an application rejected on a job of a specific type', 'gamipress' ), | |
| 38 | + ); | |
| 39 | + | |
| 40 | + return $triggers; | |
| 41 | + | |
| 42 | +} | |
| 43 | +add_filter( 'gamipress_activity_triggers', 'gamipress_wp_job_manager_activity_triggers' ); | |
| 44 | + | |
| 45 | +/** | |
| 46 | + * Build custom activity trigger label | |
| 47 | + * | |
| 48 | + * @param string $title | |
| 49 | + * @param integer $requirement_id | |
| 50 | + * @param array $requirement | |
| 51 | + * | |
| 52 | + * @return string | |
| 53 | + */ | |
| 54 | +function gamipress_wp_job_manager_activity_trigger_label( $title, $requirement_id, $requirement ) { | |
| 55 | + | |
| 56 | + switch( $requirement['trigger_type'] ) { | |
| 57 | + // Type | |
| 58 | + case 'gamipress_wp_job_manager_publish_job_specific_type': | |
| 59 | + case 'gamipress_wp_job_manager_mark_filled_specific_type': | |
| 60 | + case 'gamipress_wp_job_manager_mark_not_filled_specific_type': | |
| 61 | + // Applications | |
| 62 | + case 'gamipress_wp_job_manager_job_application_specific_type': | |
| 63 | + case 'gamipress_wp_job_manager_get_job_application_specific_type': | |
| 64 | + case 'gamipress_wp_job_manager_job_application_hired_specific_type': | |
| 65 | + case 'gamipress_wp_job_manager_job_application_rejected_specific_type': | |
| 66 | + $type_id = ( isset( $requirement['wp_job_manager_type_id'] ) ) ? absint( $requirement['wp_job_manager_type_id'] ) : 0; | |
| 67 | + | |
| 68 | + if( $type_id !== 0 ) { | |
| 69 | + | |
| 70 | + // Setup the pattern based on trigger type given | |
| 71 | + switch( $requirement['trigger_type'] ) { | |
| 72 | + case 'gamipress_wp_job_manager_mark_filled_specific_type': | |
| 73 | + $pattern = __( 'Mark a job of "%s" type as filled', 'gamipress' ); | |
| 74 | + break; | |
| 75 | + case 'gamipress_wp_job_manager_mark_not_filled_specific_type': | |
| 76 | + $pattern = __( 'Mark a job of "%s" type as not filled', 'gamipress' ); | |
| 77 | + break; | |
| 78 | + case 'gamipress_wp_job_manager_job_application_specific_type': | |
| 79 | + $pattern = __( 'Apply to job of "%s" type', 'gamipress' ); | |
| 80 | + break; | |
| 81 | + case 'gamipress_wp_job_manager_get_job_application_specific_type': | |
| 82 | + $pattern = __( 'Receive an application on a job of "%s" type', 'gamipress' ); | |
| 83 | + break; | |
| 84 | + case 'gamipress_wp_job_manager_job_application_hired_specific_type': | |
| 85 | + $pattern = __( 'Get hired on a job of "%s" type', 'gamipress' ); | |
| 86 | + break; | |
| 87 | + case 'gamipress_wp_job_manager_job_application_rejected_specific_type': | |
| 88 | + $pattern = __( 'Get an application rejected on a job of "%s" type', 'gamipress' ); | |
| 89 | + break; | |
| 90 | + default: | |
| 91 | + $pattern = __( 'Publish a job of "%s" type', 'gamipress' ); | |
| 92 | + break; | |
| 93 | + } | |
| 94 | + | |
| 95 | + $type = get_term_by( 'term_id', $type_id, 'job_listing_type' ); | |
| 96 | + | |
| 97 | + // Return the custom title | |
| 98 | + return sprintf( $pattern, $type->name ); | |
| 99 | + } | |
| 100 | + break; | |
| 101 | + | |
| 102 | + } | |
| 103 | + | |
| 104 | + return $title; | |
| 105 | +} | |
| 106 | +add_filter( 'gamipress_activity_trigger_label', 'gamipress_wp_job_manager_activity_trigger_label', 10, 3 ); | |
| 107 | + | |
| 108 | +/** | |
| 109 | + * Get user for a given trigger action. | |
| 110 | + * | |
| 111 | + * @since 1.0.0 | |
| 112 | + * | |
| 113 | + * @param integer $user_id user ID to override. | |
| 114 | + * @param string $trigger Trigger name. | |
| 115 | + * @param array $args Passed trigger args. | |
| 116 | + * @return integer User ID. | |
| 117 | + */ | |
| 118 | +function gamipress_wp_job_manager_trigger_get_user_id( $user_id, $trigger, $args ) { | |
| 119 | + | |
| 120 | + switch ( $trigger ) { | |
| 121 | + case 'gamipress_publish_job_listing': // Internal GamiPress listener | |
| 122 | + case 'gamipress_wp_job_manager_publish_job_specific_type': | |
| 123 | + case 'gamipress_wp_job_manager_mark_filled': | |
| 124 | + case 'gamipress_wp_job_manager_mark_filled_specific_type': | |
| 125 | + case 'gamipress_wp_job_manager_mark_not_filled': | |
| 126 | + case 'gamipress_wp_job_manager_mark_not_filled_specific_type': | |
| 127 | + // Applications | |
| 128 | + case 'gamipress_wp_job_manager_job_application': | |
| 129 | + case 'gamipress_wp_job_manager_job_application_specific_type': | |
| 130 | + case 'gamipress_wp_job_manager_get_job_application': | |
| 131 | + case 'gamipress_wp_job_manager_get_job_application_specific_type': | |
| 132 | + case 'gamipress_wp_job_manager_job_application_hired': | |
| 133 | + case 'gamipress_wp_job_manager_job_application_hired_specific_type': | |
| 134 | + case 'gamipress_wp_job_manager_job_application_rejected': | |
| 135 | + case 'gamipress_wp_job_manager_job_application_rejected_specific_type': | |
| 136 | + $user_id = $args[1]; | |
| 137 | + break; | |
| 138 | + } | |
| 139 | + | |
| 140 | + return $user_id; | |
| 141 | + | |
| 142 | +} | |
| 143 | +add_filter( 'gamipress_trigger_get_user_id', 'gamipress_wp_job_manager_trigger_get_user_id', 10, 3 ); | |
| 144 | + | |
| 145 | +/** | |
| 146 | + * Extended meta data for event trigger logging | |
| 147 | + * | |
| 148 | + * @since 1.0.0 | |
| 149 | + * | |
| 150 | + * @param array $log_meta | |
| 151 | + * @param integer $user_id | |
| 152 | + * @param string $trigger | |
| 153 | + * @param integer $site_id | |
| 154 | + * @param array $args | |
| 155 | + * | |
| 156 | + * @return array | |
| 157 | + */ | |
| 158 | +function gamipress_wp_job_manager_log_event_trigger_meta_data( $log_meta, $user_id, $trigger, $site_id, $args ) { | |
| 159 | + | |
| 160 | + switch ( $trigger ) { | |
| 161 | + case 'gamipress_publish_job_listing': // Internal GamiPress listener | |
| 162 | + case 'gamipress_wp_job_manager_mark_filled': | |
| 163 | + case 'gamipress_wp_job_manager_mark_not_filled': | |
| 164 | + // Add the post ID | |
| 165 | + $log_meta['post_id'] = $args[0]; | |
| 166 | + break; | |
| 167 | + case 'gamipress_wp_job_manager_publish_job_specific_type': | |
| 168 | + case 'gamipress_wp_job_manager_mark_filled_specific_type': | |
| 169 | + case 'gamipress_wp_job_manager_mark_not_filled_specific_type': | |
| 170 | + // Add the post and term IDs | |
| 171 | + $log_meta['post_id'] = $args[0]; | |
| 172 | + $log_meta['term_id'] = $args[2]; | |
| 173 | + break; | |
| 174 | + // Applications | |
| 175 | + case 'gamipress_wp_job_manager_job_application': | |
| 176 | + case 'gamipress_wp_job_manager_job_application_hired': | |
| 177 | + case 'gamipress_wp_job_manager_job_application_rejected': | |
| 178 | + // Add the post and application IDs | |
| 179 | + $log_meta['post_id'] = $args[0]; | |
| 180 | + $log_meta['application_id'] = $args[2]; | |
| 181 | + break; | |
| 182 | + case 'gamipress_wp_job_manager_job_application_specific_type': | |
| 183 | + case 'gamipress_wp_job_manager_job_application_hired_specific_type': | |
| 184 | + case 'gamipress_wp_job_manager_job_application_rejected_specific_type': | |
| 185 | + // Add the post, term and application IDs | |
| 186 | + $log_meta['post_id'] = $args[0]; | |
| 187 | + $log_meta['term_id'] = $args[2]; | |
| 188 | + $log_meta['application_id'] = $args[3]; | |
| 189 | + break; | |
| 190 | + case 'gamipress_wp_job_manager_get_job_application': | |
| 191 | + // Add the post, candidate and application IDs | |
| 192 | + $log_meta['post_id'] = $args[0]; | |
| 193 | + $log_meta['application_id'] = $args[2]; | |
| 194 | + $log_meta['candidate_id'] = $args[3]; | |
| 195 | + break; | |
| 196 | + case 'gamipress_wp_job_manager_get_job_application_specific_type': | |
| 197 | + // Add the post, candidate and application IDs | |
| 198 | + $log_meta['post_id'] = $args[0]; | |
| 199 | + $log_meta['term_id'] = $args[2]; | |
| 200 | + $log_meta['application_id'] = $args[3]; | |
| 201 | + $log_meta['candidate_id'] = $args[4]; | |
| 202 | + break; | |
| 203 | + } | |
| 204 | + | |
| 205 | + return $log_meta; | |
| 206 | +} | |
| 207 | +add_filter( 'gamipress_log_event_trigger_meta_data', 'gamipress_wp_job_manager_log_event_trigger_meta_data', 10, 5 ); | |
| 208 | + | |
| 209 | +/** | |
| 210 | + * Override the meta data to filter the logs count | |
| 211 | + * | |
| 212 | + * @since 1.0.0 | |
| 213 | + * | |
| 214 | + * @param array $log_meta The meta data to filter the logs count | |
| 215 | + * @param int $user_id The given user's ID | |
| 216 | + * @param string $trigger The given trigger we're checking | |
| 217 | + * @param int $since The since timestamp where retrieve the logs | |
| 218 | + * @param int $site_id The desired Site ID to check | |
| 219 | + * @param array $args The triggered args or requirement object | |
| 220 | + * | |
| 221 | + * @return array The meta data to filter the logs count | |
| 222 | + */ | |
| 223 | +function gamipress_wp_job_manager_get_user_trigger_count_log_meta( $log_meta, $user_id, $trigger, $since, $site_id, $args ) { | |
| 224 | + | |
| 225 | + switch( $trigger ) { | |
| 226 | + // Type | |
| 227 | + case 'gamipress_wp_job_manager_publish_job_specific_type': | |
| 228 | + case 'gamipress_wp_job_manager_mark_filled_specific_type': | |
| 229 | + case 'gamipress_wp_job_manager_mark_not_filled_specific_type': | |
| 230 | + // Applications | |
| 231 | + case 'gamipress_wp_job_manager_job_application_specific_type': | |
| 232 | + case 'gamipress_wp_job_manager_get_job_application_specific_type': | |
| 233 | + case 'gamipress_wp_job_manager_job_application_hired_specific_type': | |
| 234 | + case 'gamipress_wp_job_manager_job_application_rejected_specific_type': | |
| 235 | + if( isset( $args[2] ) ) { | |
| 236 | + // Add the type ID | |
| 237 | + $log_meta['term_id'] = $args[2]; | |
| 238 | + } | |
| 239 | + | |
| 240 | + // $args could be a requirement object | |
| 241 | + if( isset( $args['wp_job_manager_type_id'] ) ) | |
| 242 | + $log_meta['term_id'] = $args['wp_job_manager_type_id']; | |
| 243 | + break; | |
| 244 | + } | |
| 245 | + | |
| 246 | + return $log_meta; | |
| 247 | + | |
| 248 | +} | |
| 249 | +add_filter( 'gamipress_get_user_trigger_count_log_meta', 'gamipress_wp_job_manager_get_user_trigger_count_log_meta', 10, 6 ); | |
| 250 | + | |
| 251 | +/** | |
| 252 | + * Extra data fields | |
| 253 | + * | |
| 254 | + * @since 1.0.0 | |
| 255 | + * | |
| 256 | + * @param array $fields | |
| 257 | + * @param int $log_id | |
| 258 | + * @param string $type | |
| 259 | + * | |
| 260 | + * @return array | |
| 261 | + */ | |
| 262 | +function gamipress_wp_job_manager_log_extra_data_fields( $fields, $log_id, $type ) { | |
| 263 | + | |
| 264 | + $prefix = '_gamipress_'; | |
| 265 | + | |
| 266 | + $log = ct_get_object( $log_id ); | |
| 267 | + $trigger = $log->trigger_type; | |
| 268 | + | |
| 269 | + if( $type !== 'event_trigger' ) { | |
| 270 | + return $fields; | |
| 271 | + } | |
| 272 | + | |
| 273 | + switch( $trigger ) { | |
| 274 | + // Type | |
| 275 | + case 'gamipress_wp_job_manager_publish_job_specific_type': | |
| 276 | + case 'gamipress_wp_job_manager_mark_filled_specific_type': | |
| 277 | + case 'gamipress_wp_job_manager_mark_not_filled_specific_type': | |
| 278 | + // Applications | |
| 279 | + case 'gamipress_wp_job_manager_job_application_specific_type': | |
| 280 | + case 'gamipress_wp_job_manager_get_job_application_specific_type': | |
| 281 | + case 'gamipress_wp_job_manager_job_application_hired_specific_type': | |
| 282 | + case 'gamipress_wp_job_manager_job_application_rejected_specific_type': | |
| 283 | + | |
| 284 | + // Get types stored and turn them into an array of options | |
| 285 | + $types = get_terms( array( | |
| 286 | + 'taxonomy' => 'job_listing_type', | |
| 287 | + 'hide_empty' => false, | |
| 288 | + ) ); | |
| 289 | + | |
| 290 | + $options = array(); | |
| 291 | + | |
| 292 | + foreach( $types as $type ) { | |
| 293 | + $options[$type->term_id] = $type->name; | |
| 294 | + } | |
| 295 | + | |
| 296 | + $fields[] = array( | |
| 297 | + 'name' => __( 'Type', 'gamipress' ), | |
| 298 | + 'desc' => __( 'Job type attached to this log.', 'gamipress' ), | |
| 299 | + 'id' => $prefix . 'term_id', | |
| 300 | + 'type' => 'select', | |
| 301 | + 'options' => $options | |
| 302 | + ); | |
| 303 | + break; | |
| 304 | + } | |
| 305 | + | |
| 306 | + return $fields; | |
| 307 | + | |
| 308 | +} | |
| 309 | +add_filter( 'gamipress_log_extra_data_fields', 'gamipress_wp_job_manager_log_extra_data_fields', 10, 3 ); | |
| 310 | + | |
| 311 | +/** | |
| 312 | + * Extra filter to check duplicated activity | |
| 313 | + * | |
| 314 | + * @since 1.0.0 | |
| 315 | + * | |
| 316 | + * @param bool $return | |
| 317 | + * @param integer $user_id | |
| 318 | + * @param string $trigger | |
| 319 | + * @param integer $site_id | |
| 320 | + * @param array $args | |
| 321 | + * | |
| 322 | + * @return bool True if user deserves trigger, else false | |
| 323 | + */ | |
| 324 | +function gamipress_wp_job_manager_trigger_duplicity_check( $return, $user_id, $trigger, $site_id, $args ) { | |
| 325 | + | |
| 326 | + // If user doesn't deserves trigger, then bail to prevent grant access | |
| 327 | + if( ! $return ) | |
| 328 | + return $return; | |
| 329 | + | |
| 330 | + $log_meta = array( | |
| 331 | + 'type' => 'event_trigger', | |
| 332 | + 'trigger_type' => $trigger, | |
| 333 | + ); | |
| 334 | + | |
| 335 | + switch ( $trigger ) { | |
| 336 | + case 'gamipress_publish_job_listing': // Internal GamiPress listener | |
| 337 | + case 'gamipress_wp_job_manager_publish_job_specific_type': | |
| 338 | + // Prevent duplicate job publishing | |
| 339 | + $log_meta['post_id'] = $args[0]; | |
| 340 | + $return = (bool) ( gamipress_get_user_last_log( $user_id, $log_meta ) === false ); | |
| 341 | + break; | |
| 342 | + } | |
| 343 | + | |
| 344 | + return $return; | |
| 345 | + | |
| 346 | +} | |
| 347 | +add_filter( 'gamipress_user_deserves_trigger', 'gamipress_wp_job_manager_trigger_duplicity_check', 10, 5 ); | |