| @@ -1,0 +1,344 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Triggers | |
| 4 | + * | |
| 5 | + * @package GamiPress\WP_Ulike\Triggers | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Register plugin triggers | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + * | |
| 16 | + * @param array $triggers | |
| 17 | + * @return mixed | |
| 18 | + */ | |
| 19 | +function gamipress_wp_ulike_activity_triggers( $triggers ) { | |
| 20 | + | |
| 21 | + $triggers[__( 'WP Ulike', 'gamipress' )] = array( | |
| 22 | + | |
| 23 | + // Like | |
| 24 | + 'gamipress_wp_ulike_like' => __( 'Like anything', 'gamipress' ), | |
| 25 | + 'gamipress_wp_ulike_post_like' => __( 'Like a post', 'gamipress' ), | |
| 26 | + 'gamipress_wp_ulike_post_type_like' => __( 'Like a post of a type', 'gamipress' ), | |
| 27 | + 'gamipress_wp_ulike_comment_like' => __( 'Like a comment', 'gamipress' ), | |
| 28 | + // Like (Author) | |
| 29 | + 'gamipress_wp_ulike_get_like' => __( 'Get a like anywhere', 'gamipress' ), | |
| 30 | + 'gamipress_wp_ulike_get_post_like' => __( 'Get a like on a post', 'gamipress' ), | |
| 31 | + 'gamipress_wp_ulike_get_post_type_like' => __( 'Get a like on a post of a type', 'gamipress' ), | |
| 32 | + 'gamipress_wp_ulike_get_comment_like' => __( 'Get a like on a comment', 'gamipress' ), | |
| 33 | + // Unlike | |
| 34 | + 'gamipress_wp_ulike_unlike' => __( 'Unlike anything', 'gamipress' ), | |
| 35 | + 'gamipress_wp_ulike_post_unlike' => __( 'Unlike a post', 'gamipress' ), | |
| 36 | + 'gamipress_wp_ulike_comment_unlike' => __( 'Unlike a comment', 'gamipress' ), | |
| 37 | + // Unlike (Author) | |
| 38 | + 'gamipress_wp_ulike_get_unlike' => __( 'Get an unlike anywhere', 'gamipress' ), | |
| 39 | + 'gamipress_wp_ulike_get_post_unlike' => __( 'Get an unlike on a post', 'gamipress' ), | |
| 40 | + 'gamipress_wp_ulike_get_comment_unlike' => __( 'Get an unlike on a comment', 'gamipress' ), | |
| 41 | + | |
| 42 | + ); | |
| 43 | + | |
| 44 | + // WP Ulike + BuddyPress | |
| 45 | + if ( class_exists( 'BuddyPress' ) ) { | |
| 46 | + $triggers[__( 'WP Ulike and BuddyPress', 'gamipress' )] = array( | |
| 47 | + | |
| 48 | + // Like | |
| 49 | + 'gamipress_wp_ulike_activity_like' => __( 'Like an activity', 'gamipress' ), | |
| 50 | + // Like (Author) | |
| 51 | + 'gamipress_wp_ulike_get_activity_like' => __( 'Get a like on an activity', 'gamipress' ), | |
| 52 | + // Unlike | |
| 53 | + 'gamipress_wp_ulike_activity_unlike' => __( 'Unlike an activity', 'gamipress' ), | |
| 54 | + // Unlike (Author) | |
| 55 | + 'gamipress_wp_ulike_get_activity_unlike' => __( 'Get an unlike on an activity', 'gamipress' ), | |
| 56 | + | |
| 57 | + ); | |
| 58 | + } | |
| 59 | + | |
| 60 | + // WP Ulike + bbPress | |
| 61 | + if ( class_exists( 'bbPress' ) ) { | |
| 62 | + $triggers[__( 'WP Ulike and bbPress', 'gamipress' )] = array( | |
| 63 | + | |
| 64 | + // Like | |
| 65 | + 'gamipress_wp_ulike_topic_like' => __( 'Like a topic', 'gamipress' ), | |
| 66 | + // Like (Author) | |
| 67 | + 'gamipress_wp_ulike_get_topic_like' => __( 'Get a like on a topic', 'gamipress' ), | |
| 68 | + // Unlike | |
| 69 | + 'gamipress_wp_ulike_topic_unlike' => __( 'Unlike a topic', 'gamipress' ), | |
| 70 | + // Unlike (Author) | |
| 71 | + 'gamipress_wp_ulike_get_topic_unlike' => __( 'Get an unlike on a topic', 'gamipress' ), | |
| 72 | + | |
| 73 | + ); | |
| 74 | + } | |
| 75 | + | |
| 76 | + return $triggers; | |
| 77 | + | |
| 78 | +} | |
| 79 | +add_filter( 'gamipress_activity_triggers', 'gamipress_wp_ulike_activity_triggers' ); | |
| 80 | + | |
| 81 | +/** | |
| 82 | + * Get user for a given trigger action. | |
| 83 | + * | |
| 84 | + * @since 1.0.0 | |
| 85 | + * | |
| 86 | + * @param integer $user_id user ID to override. | |
| 87 | + * @param string $trigger Trigger name. | |
| 88 | + * @param array $args Passed trigger args. | |
| 89 | + * @return integer User ID. | |
| 90 | + */ | |
| 91 | +function gamipress_wp_ulike_trigger_get_user_id( $user_id, $trigger, $args ) { | |
| 92 | + | |
| 93 | + switch ( $trigger ) { | |
| 94 | + // Like | |
| 95 | + case 'gamipress_wp_ulike_like': | |
| 96 | + case 'gamipress_wp_ulike_post_like': | |
| 97 | + //case 'gamipress_wp_ulike_post_type_like': | |
| 98 | + case 'gamipress_wp_ulike_comment_like': | |
| 99 | + // Like (Author) | |
| 100 | + case 'gamipress_wp_ulike_get_like': | |
| 101 | + case 'gamipress_wp_ulike_get_post_like': | |
| 102 | + case 'gamipress_wp_ulike_get_post_type_like': | |
| 103 | + case 'gamipress_wp_ulike_get_comment_like': | |
| 104 | + // Unlike | |
| 105 | + case 'gamipress_wp_ulike_unlike': | |
| 106 | + case 'gamipress_wp_ulike_post_unlike': | |
| 107 | + case 'gamipress_wp_ulike_comment_unlike': | |
| 108 | + // Unlike (Author) | |
| 109 | + case 'gamipress_wp_ulike_get_unlike': | |
| 110 | + case 'gamipress_wp_ulike_get_post_unlike': | |
| 111 | + case 'gamipress_wp_ulike_get_comment_unlike': | |
| 112 | + // BuddyPress | |
| 113 | + case 'gamipress_wp_ulike_activity_like': | |
| 114 | + case 'gamipress_wp_ulike_get_activity_like': | |
| 115 | + case 'gamipress_wp_ulike_activity_unlike': | |
| 116 | + case 'gamipress_wp_ulike_get_activity_unlike': | |
| 117 | + // bbPress | |
| 118 | + case 'gamipress_wp_ulike_topic_like': | |
| 119 | + case 'gamipress_wp_ulike_get_topic_like': | |
| 120 | + case 'gamipress_wp_ulike_topic_unlike': | |
| 121 | + case 'gamipress_wp_ulike_get_topic_unlike': | |
| 122 | + $user_id = $args[1]; | |
| 123 | + break; | |
| 124 | + } | |
| 125 | + | |
| 126 | + return $user_id; | |
| 127 | + | |
| 128 | +} | |
| 129 | +add_filter( 'gamipress_trigger_get_user_id', 'gamipress_wp_ulike_trigger_get_user_id', 10, 3 ); | |
| 130 | + | |
| 131 | +/** | |
| 132 | + * Build custom activity trigger label | |
| 133 | + * | |
| 134 | + * @param string $title | |
| 135 | + * @param integer $requirement_id | |
| 136 | + * @param array $requirement | |
| 137 | + * | |
| 138 | + * @return string | |
| 139 | + */ | |
| 140 | +function gamipress_wp_ulike_activity_trigger_label( $title, $requirement_id, $requirement ) { | |
| 141 | + | |
| 142 | + $post_types = get_post_types( array( 'public' => true ), 'objects' ); | |
| 143 | + $post_type = ( isset( $requirement['wp_ulike_post_type'] ) ) ? $requirement['wp_ulike_post_type'] : ''; | |
| 144 | + | |
| 145 | + switch( $requirement['trigger_type'] ) { | |
| 146 | + // Like post type | |
| 147 | + case 'gamipress_wp_ulike_post_type_like': | |
| 148 | + if( $post_type === '' ) { | |
| 149 | + return __( 'Like a post of a type', 'gamipress' ); | |
| 150 | + } else { | |
| 151 | + return sprintf( __( 'Like a post of a %s type', 'gamipress' ), $post_type ); | |
| 152 | + | |
| 153 | + } | |
| 154 | + break; | |
| 155 | + // Get like post type | |
| 156 | + case 'gamipress_wp_ulike_get_post_type_like': | |
| 157 | + if( $post_type === '' ) { | |
| 158 | + return __( 'Get a like on a post of a type', 'gamipress' ); | |
| 159 | + } else { | |
| 160 | + return sprintf( __( 'Get a like on a post of %s type', 'gamipress' ), $post_type ); | |
| 161 | + | |
| 162 | + } | |
| 163 | + break; | |
| 164 | + | |
| 165 | + } | |
| 166 | + | |
| 167 | + return $title; | |
| 168 | + | |
| 169 | +} | |
| 170 | +add_filter( 'gamipress_activity_trigger_label', 'gamipress_wp_ulike_activity_trigger_label', 10, 3 ); | |
| 171 | + | |
| 172 | +/** | |
| 173 | + * Extended meta data for event trigger logging | |
| 174 | + * | |
| 175 | + * @since 1.0.1 | |
| 176 | + * | |
| 177 | + * @param array $log_meta | |
| 178 | + * @param integer $user_id | |
| 179 | + * @param string $trigger | |
| 180 | + * @param integer $site_id | |
| 181 | + * @param array $args | |
| 182 | + * | |
| 183 | + * @return array | |
| 184 | + */ | |
| 185 | +function gamipress_wp_ulike_log_event_trigger_meta_data( $log_meta, $user_id, $trigger, $site_id, $args ) { | |
| 186 | + | |
| 187 | + switch ( $trigger ) { | |
| 188 | + // Like | |
| 189 | + case 'gamipress_wp_ulike_like': | |
| 190 | + case 'gamipress_wp_ulike_post_like': | |
| 191 | + case 'gamipress_wp_ulike_comment_like': | |
| 192 | + // Unlike | |
| 193 | + case 'gamipress_wp_ulike_unlike': | |
| 194 | + case 'gamipress_wp_ulike_post_unlike': | |
| 195 | + case 'gamipress_wp_ulike_comment_unlike': | |
| 196 | + // BuddyPress | |
| 197 | + case 'gamipress_wp_ulike_activity_like': | |
| 198 | + case 'gamipress_wp_ulike_activity_unlike': | |
| 199 | + // bbPress | |
| 200 | + case 'gamipress_wp_ulike_topic_like': | |
| 201 | + case 'gamipress_wp_ulike_topic_unlike': | |
| 202 | + // Add object ID | |
| 203 | + $log_meta['id'] = $args[0]; | |
| 204 | + break; | |
| 205 | + | |
| 206 | + // Like (Author) | |
| 207 | + case 'gamipress_wp_ulike_get_like': | |
| 208 | + case 'gamipress_wp_ulike_get_post_like': | |
| 209 | + case 'gamipress_wp_ulike_get_comment_like': | |
| 210 | + // Unlike (Author) | |
| 211 | + case 'gamipress_wp_ulike_get_unlike': | |
| 212 | + case 'gamipress_wp_ulike_get_post_unlike': | |
| 213 | + case 'gamipress_wp_ulike_get_comment_unlike': | |
| 214 | + // BuddyPress | |
| 215 | + case 'gamipress_wp_ulike_get_activity_like': | |
| 216 | + case 'gamipress_wp_ulike_get_activity_unlike': | |
| 217 | + // bbPress | |
| 218 | + case 'gamipress_wp_ulike_get_topic_like': | |
| 219 | + case 'gamipress_wp_ulike_get_topic_unlike': | |
| 220 | + // Add object ID and the user that perform the like/unlike (on author events) | |
| 221 | + $log_meta['id'] = $args[0]; | |
| 222 | + $log_meta['liker_id'] = $args[2]; | |
| 223 | + break; | |
| 224 | + // Post types | |
| 225 | + case 'gamipress_wp_ulike_post_type_like': | |
| 226 | + // Add object ID | |
| 227 | + $log_meta['id'] = $args[0]; | |
| 228 | + $log_meta['post_id'] = $args[0]; | |
| 229 | + $log_meta['post_type'] = $args[2]; // To show the post in the column | |
| 230 | + break; | |
| 231 | + case 'gamipress_wp_ulike_get_post_type_like': | |
| 232 | + // Add object ID | |
| 233 | + $log_meta['id'] = $args[0]; | |
| 234 | + $log_meta['post_id'] = $args[0]; // To show the post in the column | |
| 235 | + $log_meta['liker_id'] = $args[2]; | |
| 236 | + $log_meta['post_type'] = $args[3]; | |
| 237 | + break; | |
| 238 | + } | |
| 239 | + | |
| 240 | + return $log_meta; | |
| 241 | +} | |
| 242 | +add_filter( 'gamipress_log_event_trigger_meta_data', 'gamipress_wp_ulike_log_event_trigger_meta_data', 10, 5 ); | |
| 243 | + | |
| 244 | +/** | |
| 245 | + * Override the meta data to filter the logs count | |
| 246 | + * | |
| 247 | + * @since 1.1.5 | |
| 248 | + * | |
| 249 | + * @param array $log_meta The meta data to filter the logs count | |
| 250 | + * @param int $user_id The given user's ID | |
| 251 | + * @param string $trigger The given trigger we're checking | |
| 252 | + * @param int $since The since timestamp where retrieve the logs | |
| 253 | + * @param int $site_id The desired Site ID to check | |
| 254 | + * @param array $args The triggered args or requirement object | |
| 255 | + * | |
| 256 | + * @return array The meta data to filter the logs count | |
| 257 | + */ | |
| 258 | +function gamipress_wp_ulike_get_user_trigger_count_log_meta( $log_meta, $user_id, $trigger, $since, $site_id, $args ) { | |
| 259 | + | |
| 260 | + switch( $trigger ) { | |
| 261 | + // Post type | |
| 262 | + case 'gamipress_wp_ulike_post_type_like': | |
| 263 | + if( isset( $args[2] ) ) { | |
| 264 | + // Add the post_type | |
| 265 | + $log_meta['post_type'] = $args[2]; | |
| 266 | + } | |
| 267 | + | |
| 268 | + // $args could be a requirement object | |
| 269 | + if( isset( $args['post_type_required'] ) ) { | |
| 270 | + $log_meta['post_type'] = $args['wp_ulike_post_type']; | |
| 271 | + } | |
| 272 | + | |
| 273 | + break; | |
| 274 | + case 'gamipress_wp_ulike_get_post_type_like': | |
| 275 | + if( isset( $args[3] ) ) { | |
| 276 | + // Add the post_type | |
| 277 | + $log_meta['post_type'] = $args[3]; | |
| 278 | + } | |
| 279 | + | |
| 280 | + // $args could be a requirement object | |
| 281 | + if( isset( $args['post_type_required'] ) ) { | |
| 282 | + $log_meta['post_type'] = $args['wp_ulike_post_type']; | |
| 283 | + } | |
| 284 | + | |
| 285 | + break; | |
| 286 | + | |
| 287 | + } | |
| 288 | + | |
| 289 | + return $log_meta; | |
| 290 | + | |
| 291 | +} | |
| 292 | +add_filter( 'gamipress_get_user_trigger_count_log_meta', 'gamipress_wp_ulike_get_user_trigger_count_log_meta', 10, 6 ); | |
| 293 | + | |
| 294 | +/** | |
| 295 | + * Extra data fields | |
| 296 | + * | |
| 297 | + * @since 1.1.3 | |
| 298 | + * | |
| 299 | + * @param array $fields | |
| 300 | + * @param int $log_id | |
| 301 | + * @param string $type | |
| 302 | + * | |
| 303 | + * @return array | |
| 304 | + */ | |
| 305 | +function gamipress_wp_ulike_log_extra_data_fields( $fields, $log_id, $type ) { | |
| 306 | + | |
| 307 | + $prefix = '_gamipress_'; | |
| 308 | + | |
| 309 | + $log = ct_get_object( $log_id ); | |
| 310 | + $trigger = $log->trigger_type; | |
| 311 | + | |
| 312 | + if( $type !== 'event_trigger' ) { | |
| 313 | + return $fields; | |
| 314 | + } | |
| 315 | + | |
| 316 | + switch( $trigger ) { | |
| 317 | + | |
| 318 | + // Post type | |
| 319 | + case 'gamipress_wp_ulike_post_type_like': | |
| 320 | + case 'gamipress_wp_ulike_get_post_type_like': | |
| 321 | + | |
| 322 | + // Get public post types | |
| 323 | + $post_types = get_post_types( array( 'public' => true ), 'objects' ); | |
| 324 | + | |
| 325 | + $options = array(); | |
| 326 | + | |
| 327 | + foreach( $post_types as $key => $value ) { | |
| 328 | + $options[$key] = $value->labels->singular_name; | |
| 329 | + } | |
| 330 | + | |
| 331 | + $fields[] = array( | |
| 332 | + 'name' => __( 'Post type', 'gamipress' ), | |
| 333 | + 'desc' => __( 'Post type attached to this log.', 'gamipress' ), | |
| 334 | + 'id' => $prefix . 'post_type', | |
| 335 | + 'type' => 'select', | |
| 336 | + 'options' => $options, | |
| 337 | + ); | |
| 338 | + break; | |
| 339 | + } | |
| 340 | + | |
| 341 | + return $fields; | |
| 342 | + | |
| 343 | +} | |
| 344 | +add_filter( 'gamipress_log_extra_data_fields', 'gamipress_wp_ulike_log_extra_data_fields', 10, 3 ); | |