| @@ -1,0 +1,60 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Achievement Activity Functions | |
| 4 | + * | |
| 5 | + * @package GamiPress\Activity_Functions | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Get the UNIX timestamp for the last activity on an achievement for a given user | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * @updated 1.6.1 Added special support to rank requirements getting last activity from previous earned rank | |
| 17 | + * | |
| 18 | + * @param integer $achievement_id The given achievements post ID | |
| 19 | + * @param integer $user_id The given user's ID | |
| 20 | + * @return integer The UNIX timestamp for the last reported achievement activity | |
| 21 | + */ | |
| 22 | +function gamipress_achievement_last_user_activity( $achievement_id = 0, $user_id = 0 ) { | |
| 23 | + | |
| 24 | + // Assume the user has no history with this achievement | |
| 25 | + $since = 0; | |
| 26 | + | |
| 27 | + $last_interaction = absint( get_user_meta($user_id, '_gamipress_' . $achievement_id . '_last_interaction_timestamp', true) ); | |
| 28 | + | |
| 29 | + if ( $last_interaction ) { | |
| 30 | + // Return the achievement last interaction | |
| 31 | + $since = $last_interaction + 1; | |
| 32 | + } | |
| 33 | + // Attempt to grab the achievement earned date | |
| 34 | + else if ( $date_earned = gamipress_get_last_earning_datetime( array( 'user_id' => $user_id, 'post_id' => $achievement_id ) ) ) { | |
| 35 | + | |
| 36 | + // Return the achievement date earned | |
| 37 | + $since = $date_earned + 1; | |
| 38 | + | |
| 39 | + // If hasn't earned it and is a rank requirement, then grab the rank earned date | |
| 40 | + } else if( gamipress_get_post_type( $achievement_id ) === 'rank-requirement' ) { | |
| 41 | + | |
| 42 | + $rank = gamipress_get_rank_requirement_rank( $achievement_id ); | |
| 43 | + | |
| 44 | + if( $rank ) { | |
| 45 | + | |
| 46 | + // Just get rank earned time if rank is not the lowest priority one (aka default rank) | |
| 47 | + if( ! gamipress_is_lowest_priority_rank( gamipress_get_user_rank_id( $user_id, $rank->post_type ) ) ) { | |
| 48 | + | |
| 49 | + // Set since from previous earned time rank | |
| 50 | + $since = gamipress_get_rank_earned_time( $user_id, $rank->post_type ) + 1; | |
| 51 | + } | |
| 52 | + } | |
| 53 | + | |
| 54 | + | |
| 55 | + } | |
| 56 | + | |
| 57 | + // Finally, return our time | |
| 58 | + return $since; | |
| 59 | + | |
| 60 | +} | |