| 1 |
<?php |
| 2 |
/** |
| 3 |
* Triggers |
| 4 |
* |
| 5 |
* @package GamiPress\bbPress\Triggers |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Register bbPress specific triggers |
| 14 |
* |
| 15 |
* @param array $triggers |
| 16 |
* @return mixed |
| 17 |
*/ |
| 18 |
function gamipress_bbp_activity_triggers( $triggers ) { |
| 19 |
|
| 20 |
$triggers[__( 'bbPress', 'gamipress' )] = array( |
| 21 |
'gamipress_bbp_new_forum' => __( 'Create a new forum', 'gamipress' ), |
| 22 |
|
| 23 |
'gamipress_bbp_new_topic' => __( 'Create a new topic', 'gamipress' ), |
| 24 |
'gamipress_bbp_specific_new_topic' => __( 'Create a new topic on a specific forum', 'gamipress' ), |
| 25 |
|
| 26 |
'gamipress_bbp_new_reply' => __( 'Reply to a topic', 'gamipress' ), |
| 27 |
'gamipress_bbp_specific_new_reply' => __( 'Reply to a specific topic', 'gamipress' ), |
| 28 |
'gamipress_bbp_specific_forum_reply' => __( 'Reply to any topic of a specific forum', 'gamipress' ), |
| 29 |
|
| 30 |
'gamipress_bbp_get_new_reply' => __( 'Get a reply in a topic', 'gamipress' ), |
| 31 |
'gamipress_bbp_get_specific_new_reply' => __( 'Get a reply in a specific topic', 'gamipress' ), |
| 32 |
'gamipress_bbp_get_specific_forum_reply' => __( 'Get a reply in any topic of a specific forum', 'gamipress' ), |
| 33 |
|
| 34 |
'gamipress_bbp_favorite_topic' => __( 'Favorite a topic', 'gamipress' ), |
| 35 |
'gamipress_bbp_specific_favorite_topic' => __( 'Favorite a specific topic', 'gamipress' ), |
| 36 |
'gamipress_bbp_specific_forum_favorite_topic' => __( 'Favorite any topic on a specific forum', 'gamipress' ), |
| 37 |
'gamipress_bbp_get_favorite_topic' => __( 'Get a new favorite on a topic', 'gamipress' ), |
| 38 |
|
| 39 |
'gamipress_bbp_unfavorite_topic' => __( 'Unfavorite a topic', 'gamipress' ), |
| 40 |
'gamipress_bbp_specific_unfavorite_topic' => __( 'Unfavorite a specific topic', 'gamipress' ), |
| 41 |
'gamipress_bbp_specific_forum_unfavorite_topic' => __( 'Unfavorite any topic on a specific forum', 'gamipress' ), |
| 42 |
'gamipress_bbp_get_unfavorite_topic' => __( 'Lost a new favorite on a topic', 'gamipress' ), |
| 43 |
|
| 44 |
'gamipress_bbp_delete_forum' => __( 'Delete a forum', 'gamipress' ), |
| 45 |
'gamipress_bbp_delete_topic' => __( 'Delete a topic', 'gamipress' ), |
| 46 |
'gamipress_bbp_delete_reply' => __( 'Delete a reply', 'gamipress' ), |
| 47 |
); |
| 48 |
|
| 49 |
return $triggers; |
| 50 |
|
| 51 |
} |
| 52 |
add_filter( 'gamipress_activity_triggers', 'gamipress_bbp_activity_triggers' ); |
| 53 |
|
| 54 |
/** |
| 55 |
* Register bbPress specific activity triggers |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* |
| 59 |
* @param array $specific_activity_triggers |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
function gamipress_bbp_specific_activity_triggers( $specific_activity_triggers ) { |
| 63 |
|
| 64 |
// Support for multisites if bbPress is just active on a subsite |
| 65 |
$forum = ( function_exists( 'bbp_get_forum_post_type' ) ? bbp_get_forum_post_type() : apply_filters( 'bbp_forum_post_type', 'forum' ) ); |
| 66 |
$topic = ( function_exists( 'bbp_get_topic_post_type' ) ? bbp_get_topic_post_type() : apply_filters( 'bbp_topic_post_type', 'topic' ) ); |
| 67 |
|
| 68 |
$specific_activity_triggers['gamipress_bbp_specific_new_topic'] = array( $forum ); |
| 69 |
$specific_activity_triggers['gamipress_bbp_specific_new_reply'] = array( $topic ); |
| 70 |
$specific_activity_triggers['gamipress_bbp_specific_forum_reply'] = array( $forum ); |
| 71 |
$specific_activity_triggers['gamipress_bbp_get_specific_new_reply'] = array( $topic ); |
| 72 |
$specific_activity_triggers['gamipress_bbp_get_specific_forum_reply'] = array( $forum ); |
| 73 |
$specific_activity_triggers['gamipress_bbp_specific_favorite_topic'] = array( $topic ); |
| 74 |
$specific_activity_triggers['gamipress_bbp_specific_forum_favorite_topic'] = array( $forum ); |
| 75 |
$specific_activity_triggers['gamipress_bbp_specific_unfavorite_topic'] = array( $topic ); |
| 76 |
$specific_activity_triggers['gamipress_bbp_specific_forum_unfavorite_topic'] = array( $forum ); |
| 77 |
|
| 78 |
return $specific_activity_triggers; |
| 79 |
} |
| 80 |
add_filter( 'gamipress_specific_activity_triggers', 'gamipress_bbp_specific_activity_triggers' ); |
| 81 |
|
| 82 |
/** |
| 83 |
* Register bbPress specific activity triggers labels |
| 84 |
* |
| 85 |
* @since 1.0.0 |
| 86 |
* |
| 87 |
* @param array $specific_activity_trigger_labels |
| 88 |
* @return array |
| 89 |
*/ |
| 90 |
function gamipress_bbp_specific_activity_trigger_label( $specific_activity_trigger_labels ) { |
| 91 |
|
| 92 |
$specific_activity_trigger_labels['gamipress_bbp_specific_new_topic'] = __( 'Create a topic on %s forum', 'gamipress' ); |
| 93 |
$specific_activity_trigger_labels['gamipress_bbp_specific_new_reply'] = __( 'Reply to %s topic', 'gamipress' ); |
| 94 |
$specific_activity_trigger_labels['gamipress_bbp_specific_forum_reply'] = __( 'Reply to any topic on %s forum', 'gamipress' ); |
| 95 |
$specific_activity_trigger_labels['gamipress_bbp_get_specific_new_reply'] = __( 'Get a reply in %s', 'gamipress' ); |
| 96 |
$specific_activity_trigger_labels['gamipress_bbp_get_specific_forum_reply'] = __( 'Get a reply in any topic on %s forum', 'gamipress' ); |
| 97 |
$specific_activity_trigger_labels['gamipress_bbp_specific_favorite_topic'] = __( 'Favorite %s', 'gamipress' ); |
| 98 |
$specific_activity_trigger_labels['gamipress_bbp_specific_forum_favorite_topic'] = __( 'Favorite any topic on %s forum', 'gamipress' ); |
| 99 |
$specific_activity_trigger_labels['gamipress_bbp_specific_unfavorite_topic'] = __( 'Unfavorite %s', 'gamipress' ); |
| 100 |
$specific_activity_trigger_labels['gamipress_bbp_specific_forum_unfavorite_topic'] = __( 'Unfavorite any topic on %s forum', 'gamipress' ); |
| 101 |
|
| 102 |
return $specific_activity_trigger_labels; |
| 103 |
} |
| 104 |
add_filter( 'gamipress_specific_activity_trigger_label', 'gamipress_bbp_specific_activity_trigger_label' ); |
| 105 |
|
| 106 |
/** |
| 107 |
* Get user for a given trigger action. |
| 108 |
* |
| 109 |
* @since 1.0.0 |
| 110 |
* |
| 111 |
* @param integer $user_id user ID to override. |
| 112 |
* @param string $trigger Trigger name. |
| 113 |
* @param array $args Passed trigger args. |
| 114 |
* @return integer User ID. |
| 115 |
*/ |
| 116 |
function gamipress_bbp_trigger_get_user_id( $user_id, $trigger, $args ) { |
| 117 |
|
| 118 |
switch ( $trigger ) { |
| 119 |
case 'gamipress_bbp_new_forum': |
| 120 |
case 'gamipress_bbp_new_topic': |
| 121 |
case 'gamipress_bbp_specific_new_topic': |
| 122 |
case 'gamipress_bbp_new_reply': |
| 123 |
case 'gamipress_bbp_specific_new_reply': |
| 124 |
case 'gamipress_bbp_specific_forum_reply': |
| 125 |
case 'gamipress_bbp_get_new_reply': |
| 126 |
case 'gamipress_bbp_get_specific_new_reply': |
| 127 |
case 'gamipress_bbp_get_specific_forum_reply': |
| 128 |
case 'gamipress_bbp_favorite_topic': |
| 129 |
case 'gamipress_bbp_specific_favorite_topic': |
| 130 |
case 'gamipress_bbp_specific_forum_favorite_topic': |
| 131 |
case 'gamipress_bbp_get_favorite_topic': |
| 132 |
case 'gamipress_bbp_unfavorite_topic': |
| 133 |
case 'gamipress_bbp_specific_unfavorite_topic': |
| 134 |
case 'gamipress_bbp_specific_forum_unfavorite_topic': |
| 135 |
case 'gamipress_bbp_get_unfavorite_topic': |
| 136 |
case 'gamipress_bbp_delete_forum': |
| 137 |
case 'gamipress_bbp_delete_topic': |
| 138 |
case 'gamipress_bbp_delete_reply': |
| 139 |
$user_id = $args[1]; |
| 140 |
break; |
| 141 |
} |
| 142 |
|
| 143 |
return $user_id; |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
add_filter( 'gamipress_trigger_get_user_id', 'gamipress_bbp_trigger_get_user_id', 10, 3); |
| 148 |
|
| 149 |
/** |
| 150 |
* Get the id for a given specific trigger action. |
| 151 |
* |
| 152 |
* @since 1.0.1 |
| 153 |
* |
| 154 |
* @param integer $specific_id Specific ID. |
| 155 |
* @param string $trigger Trigger name. |
| 156 |
* @param array $args Passed trigger args. |
| 157 |
* |
| 158 |
* @return integer Specific ID. |
| 159 |
*/ |
| 160 |
function gamipress_bbp_specific_trigger_get_id( $specific_id, $trigger = '', $args = array() ) { |
| 161 |
|
| 162 |
switch ( $trigger ) { |
| 163 |
case 'gamipress_bbp_specific_new_topic': |
| 164 |
case 'gamipress_bbp_specific_new_reply': |
| 165 |
case 'gamipress_bbp_get_specific_new_reply': |
| 166 |
case 'gamipress_bbp_specific_forum_favorite_topic': |
| 167 |
case 'gamipress_bbp_specific_forum_unfavorite_topic': |
| 168 |
$specific_id = $args[2]; |
| 169 |
break; |
| 170 |
case 'gamipress_bbp_specific_forum_reply': |
| 171 |
case 'gamipress_bbp_get_specific_forum_reply': |
| 172 |
$specific_id = $args[3]; |
| 173 |
break; |
| 174 |
case 'gamipress_bbp_specific_favorite_topic': |
| 175 |
case 'gamipress_bbp_specific_unfavorite_topic': |
| 176 |
$specific_id = $args[0]; |
| 177 |
break; |
| 178 |
} |
| 179 |
|
| 180 |
return $specific_id; |
| 181 |
} |
| 182 |
add_filter( 'gamipress_specific_trigger_get_id', 'gamipress_bbp_specific_trigger_get_id', 10, 3 ); |
| 183 |
|
| 184 |
/** |
| 185 |
* Extended meta data for event trigger logging |
| 186 |
* |
| 187 |
* @since 1.0.1 |
| 188 |
* |
| 189 |
* @param array $log_meta |
| 190 |
* @param integer $user_id |
| 191 |
* @param string $trigger |
| 192 |
* @param integer $site_id |
| 193 |
* @param array $args |
| 194 |
* |
| 195 |
* @return array |
| 196 |
*/ |
| 197 |
function gamipress_bbp_log_event_trigger_meta_data( $log_meta, $user_id, $trigger, $site_id, $args ) { |
| 198 |
|
| 199 |
switch ( $trigger ) { |
| 200 |
case 'gamipress_bbp_new_forum': |
| 201 |
case 'gamipress_bbp_delete_forum': |
| 202 |
// Add the forum ID |
| 203 |
$log_meta['forum_id'] = $args[0]; |
| 204 |
break; |
| 205 |
case 'gamipress_bbp_new_topic': |
| 206 |
case 'gamipress_bbp_specific_new_topic': |
| 207 |
// Add the topic and forum ID |
| 208 |
$log_meta['topic_id'] = $args[0]; |
| 209 |
$log_meta['forum_id'] = $args[2]; |
| 210 |
break; |
| 211 |
case 'gamipress_bbp_new_reply': |
| 212 |
case 'gamipress_bbp_specific_new_reply': |
| 213 |
case 'gamipress_bbp_specific_forum_reply': |
| 214 |
case 'gamipress_bbp_get_new_reply': |
| 215 |
case 'gamipress_bbp_get_specific_new_reply': |
| 216 |
case 'gamipress_bbp_get_specific_forum_reply': |
| 217 |
// Add the reply, topic and forum IDs |
| 218 |
$log_meta['reply_id'] = $args[0]; |
| 219 |
$log_meta['topic_id'] = $args[2]; |
| 220 |
$log_meta['forum_id'] = $args[3]; |
| 221 |
break; |
| 222 |
case 'gamipress_bbp_favorite_topic': |
| 223 |
case 'gamipress_bbp_specific_favorite_topic': |
| 224 |
case 'gamipress_bbp_specific_forum_favorite_topic': |
| 225 |
case 'gamipress_bbp_get_favorite_topic': |
| 226 |
case 'gamipress_bbp_unfavorite_topic': |
| 227 |
case 'gamipress_bbp_specific_unfavorite_topic': |
| 228 |
case 'gamipress_bbp_specific_forum_unfavorite_topic': |
| 229 |
case 'gamipress_bbp_get_unfavorite_topic': |
| 230 |
case 'gamipress_bbp_delete_topic': |
| 231 |
// Add the topic and forum IDs |
| 232 |
$log_meta['topic_id'] = $args[0]; |
| 233 |
$log_meta['forum_id'] = $args[2]; |
| 234 |
break; |
| 235 |
case 'gamipress_bbp_delete_reply': |
| 236 |
// Add the reply ID |
| 237 |
$log_meta['reply_id'] = $args[0]; |
| 238 |
break; |
| 239 |
} |
| 240 |
|
| 241 |
return $log_meta; |
| 242 |
} |
| 243 |
add_filter( 'gamipress_log_event_trigger_meta_data', 'gamipress_bbp_log_event_trigger_meta_data', 10, 5 ); |
| 244 |
|
| 245 |
/** |
| 246 |
* Extra filter to check duplicated activity |
| 247 |
* |
| 248 |
* @since 1.0.1 |
| 249 |
* |
| 250 |
* @param bool $return |
| 251 |
* @param integer $user_id |
| 252 |
* @param string $trigger |
| 253 |
* @param integer $site_id |
| 254 |
* @param array $args |
| 255 |
* |
| 256 |
* @return bool True if user deserves trigger, else false |
| 257 |
*/ |
| 258 |
function gamipress_bbp_trigger_duplicity_check( $return, $user_id, $trigger, $site_id, $args ) { |
| 259 |
|
| 260 |
// If user doesn't deserves trigger, then bail to prevent grant access |
| 261 |
if( ! $return ) |
| 262 |
return $return; |
| 263 |
|
| 264 |
$log_meta = array( |
| 265 |
'type' => 'event_trigger', |
| 266 |
'trigger_type' => $trigger, |
| 267 |
); |
| 268 |
|
| 269 |
switch ( $trigger ) { |
| 270 |
case 'gamipress_bbp_new_forum': |
| 271 |
// User can not create same forum more times, so check it |
| 272 |
$log_meta['forum_id'] = $args[0]; |
| 273 |
$return = (bool) ( gamipress_get_user_log_count( $user_id, $log_meta ) === 0 ); |
| 274 |
break; |
| 275 |
case 'gamipress_bbp_new_topic': |
| 276 |
case 'gamipress_bbp_specific_new_topic': |
| 277 |
// User can not create same topic more times, so check it |
| 278 |
$log_meta['topic_id'] = $args[0]; |
| 279 |
$return = (bool) ( gamipress_get_user_log_count( $user_id, $log_meta ) === 0 ); |
| 280 |
break; |
| 281 |
case 'gamipress_bbp_new_reply': |
| 282 |
case 'gamipress_bbp_specific_new_reply': |
| 283 |
case 'gamipress_bbp_specific_forum_reply': |
| 284 |
case 'gamipress_bbp_get_new_reply': |
| 285 |
case 'gamipress_bbp_get_specific_new_reply': |
| 286 |
case 'gamipress_bbp_get_specific_forum_reply': |
| 287 |
// User can not create same reply more times, so check it |
| 288 |
$log_meta['reply_id'] = $args[0]; |
| 289 |
$return = (bool) ( gamipress_get_user_log_count( $user_id, $log_meta ) === 0 ); |
| 290 |
break; |
| 291 |
} |
| 292 |
|
| 293 |
return $return; |
| 294 |
|
| 295 |
} |
| 296 |
add_filter( 'gamipress_user_deserves_trigger', 'gamipress_bbp_trigger_duplicity_check', 10, 5 ); |