| 1 |
<?php |
| 2 |
/** |
| 3 |
* Triggers |
| 4 |
* |
| 5 |
* @package GamiPress\Fluent_Support\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_fluent_support_activity_triggers( $triggers ) { |
| 19 |
|
| 20 |
// Fluent Support |
| 21 |
// Agent |
| 22 |
$triggers[__( 'Fluent Support - Events for agents', 'gamipress' )] = array( |
| 23 |
'gamipress_fluent_support_agent_open_ticket' => __( 'Open a new ticket', 'gamipress' ), |
| 24 |
'gamipress_fluent_support_agent_reply_ticket' => __( 'Reply to ticket', 'gamipress' ), |
| 25 |
'gamipress_fluent_support_agent_close_ticket' => __( 'Close a ticket', 'gamipress' ), |
| 26 |
); |
| 27 |
|
| 28 |
// Client |
| 29 |
$triggers[__( 'Fluent Support - Events for clients', 'gamipress' )] = array( |
| 30 |
'gamipress_fluent_support_client_open_ticket' => __( 'Open a new ticket', 'gamipress' ), |
| 31 |
'gamipress_fluent_support_client_reply_ticket' => __( 'Reply to ticket', 'gamipress' ), |
| 32 |
'gamipress_fluent_support_client_close_ticket' => __( 'Close a ticket', 'gamipress' ), |
| 33 |
); |
| 34 |
|
| 35 |
return $triggers; |
| 36 |
|
| 37 |
} |
| 38 |
add_filter( 'gamipress_activity_triggers', 'gamipress_fluent_support_activity_triggers' ); |
| 39 |
|
| 40 |
/** |
| 41 |
* Get user for a given trigger action. |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
* |
| 45 |
* @param integer $user_id user ID to override. |
| 46 |
* @param string $trigger Trigger name. |
| 47 |
* @param array $args Passed trigger args. |
| 48 |
* @return integer User ID. |
| 49 |
*/ |
| 50 |
function gamipress_fluent_support_trigger_get_user_id( $user_id, $trigger, $args ) { |
| 51 |
|
| 52 |
switch ( $trigger ) { |
| 53 |
// Agent |
| 54 |
case 'gamipress_fluent_support_agent_open_ticket': |
| 55 |
case 'gamipress_fluent_support_agent_reply_ticket': |
| 56 |
case 'gamipress_fluent_support_agent_close_ticket': |
| 57 |
// Client |
| 58 |
case 'gamipress_fluent_support_client_open_ticket': |
| 59 |
case 'gamipress_fluent_support_client_reply_ticket': |
| 60 |
case 'gamipress_fluent_support_client_close_ticket': |
| 61 |
$user_id = $args[1]; |
| 62 |
break; |
| 63 |
} |
| 64 |
|
| 65 |
return $user_id; |
| 66 |
|
| 67 |
} |
| 68 |
add_filter( 'gamipress_trigger_get_user_id', 'gamipress_fluent_support_trigger_get_user_id', 10, 3 ); |
| 69 |
|
| 70 |
/** |
| 71 |
* Extended meta data for event trigger logging |
| 72 |
* |
| 73 |
* @since 1.0.2 |
| 74 |
* |
| 75 |
* @param array $log_meta |
| 76 |
* @param integer $user_id |
| 77 |
* @param string $trigger |
| 78 |
* @param integer $site_id |
| 79 |
* @param array $args |
| 80 |
* |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
function gamipress_fluent_support_log_event_trigger_meta_data( $log_meta, $user_id, $trigger, $site_id, $args ) { |
| 84 |
|
| 85 |
switch ( $trigger ) { |
| 86 |
// Agent |
| 87 |
case 'gamipress_fluent_support_agent_open_ticket': |
| 88 |
case 'gamipress_fluent_support_agent_reply_ticket': |
| 89 |
case 'gamipress_fluent_support_agent_close_ticket': |
| 90 |
// Client |
| 91 |
case 'gamipress_fluent_support_client_open_ticket': |
| 92 |
case 'gamipress_fluent_support_client_reply_ticket': |
| 93 |
case 'gamipress_fluent_support_client_close_ticket': |
| 94 |
// Add the post ID |
| 95 |
$log_meta['post_id'] = $args[0]; |
| 96 |
break; |
| 97 |
} |
| 98 |
|
| 99 |
return $log_meta; |
| 100 |
} |
| 101 |
add_filter( 'gamipress_log_event_trigger_meta_data', 'gamipress_fluent_support_log_event_trigger_meta_data', 10, 5 ); |
| 102 |
|
| 103 |
/** |
| 104 |
* Extra filter to check duplicated activity |
| 105 |
* |
| 106 |
* @since 1.0.2 |
| 107 |
* |
| 108 |
* @param bool $return |
| 109 |
* @param integer $user_id |
| 110 |
* @param string $trigger |
| 111 |
* @param integer $site_id |
| 112 |
* @param array $args |
| 113 |
* |
| 114 |
* @return bool True if user deserves trigger, else false |
| 115 |
*/ |
| 116 |
function gamipress_fluent_support_trigger_duplicity_check( $return, $user_id, $trigger, $site_id, $args ) { |
| 117 |
|
| 118 |
// If user doesn't deserves trigger, then bail to prevent grant access |
| 119 |
if( ! $return ) |
| 120 |
return $return; |
| 121 |
|
| 122 |
$log_meta = array( |
| 123 |
'type' => 'event_trigger', |
| 124 |
'trigger_type' => $trigger, |
| 125 |
); |
| 126 |
|
| 127 |
switch ( $trigger ) { |
| 128 |
// Agent |
| 129 |
case 'gamipress_fluent_support_agent_open_ticket': |
| 130 |
case 'gamipress_fluent_support_agent_reply_ticket': |
| 131 |
case 'gamipress_fluent_support_agent_close_ticket': |
| 132 |
// Client |
| 133 |
case 'gamipress_fluent_support_client_open_ticket': |
| 134 |
case 'gamipress_fluent_support_client_reply_ticket': |
| 135 |
case 'gamipress_fluent_support_client_close_ticket': |
| 136 |
// Prevent duplicated tickets/replies |
| 137 |
$log_meta['post_id'] = gamipress_get_event_arg( $args, 'post_id', 0 ); |
| 138 |
$return = (bool) ( gamipress_get_user_last_log( $user_id, $log_meta ) === false ); |
| 139 |
break; |
| 140 |
} |
| 141 |
|
| 142 |
return $return; |
| 143 |
|
| 144 |
} |
| 145 |
add_filter( 'gamipress_user_deserves_trigger', 'gamipress_fluent_support_trigger_duplicity_check', 10, 5 ); |