| 1 |
<?php |
| 2 |
/** |
| 3 |
* GamiPress 1.8.7 compatibility functions |
| 4 |
* |
| 5 |
* @package GamiPress\1.8.7 |
| 6 |
* @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.8.7 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Change all active meta from one achievement type to another. |
| 14 |
* |
| 15 |
* @deprecated Since active achievements meta is not used anymore |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
* |
| 19 |
* @param string $original_type Original achievement type. |
| 20 |
* @param string $new_type New achievement type. |
| 21 |
*/ |
| 22 |
function gamipress_update_active_meta_achievement_types( $original_type = '', $new_type = '' ) { |
| 23 |
|
| 24 |
$metas = gamipress_get_unserialized_achievement_metas( '_gamipress_active_achievements', $original_type ); |
| 25 |
|
| 26 |
if ( ! empty( $metas ) ) { |
| 27 |
foreach ( $metas as $meta ) { |
| 28 |
$meta->meta_value = gamipress_update_meta_achievement_types( $meta->meta_value, $original_type, $new_type ); |
| 29 |
|
| 30 |
gamipress_update_user_meta( $meta->user_id, $meta->meta_key, $meta->meta_value ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get a user's active achievements |
| 38 |
* |
| 39 |
* @deprecated Unused anymore |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
* |
| 43 |
* @param integer $user_id User ID |
| 44 |
* @return array An array of the user's active achievements |
| 45 |
*/ |
| 46 |
function gamipress_user_get_active_achievements( $user_id ) { |
| 47 |
|
| 48 |
// Get the user's active achievements |
| 49 |
$active_achievements = gamipress_get_cache( 'active_achievements', array(), false ); |
| 50 |
|
| 51 |
// If there are no achievements |
| 52 |
if( ! isset( $active_achievements[$user_id] ) ) { |
| 53 |
return array(); |
| 54 |
} |
| 55 |
|
| 56 |
// Otherwise, we DO have achievements and should return them cast as an array |
| 57 |
return $active_achievements[$user_id]; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Update a user's active achievements |
| 62 |
* |
| 63 |
* @deprecated Unused anymore |
| 64 |
* |
| 65 |
* @since 1.0.0 |
| 66 |
* |
| 67 |
* @param integer $user_id User ID |
| 68 |
* @param array $achievements An array of achievements to pass to meta |
| 69 |
* @param boolean $update True to update to exsiting active achievements, false to replace entire array (Default: false) |
| 70 |
* @return array The updated achievements array |
| 71 |
*/ |
| 72 |
function gamipress_user_update_active_achievements( $user_id = 0, $achievements = array(), $update = false ) { |
| 73 |
|
| 74 |
// Get the user's active achievements |
| 75 |
$active_achievements = gamipress_get_cache( 'active_achievements', array(), false ); |
| 76 |
|
| 77 |
// If there are no achievements |
| 78 |
if( ! isset( $active_achievements[$user_id] ) ) { |
| 79 |
$active_achievements[$user_id] = array(); |
| 80 |
} |
| 81 |
|
| 82 |
// If we're not replacing, append the passed array to our existing array |
| 83 |
if ( true == $update ) { |
| 84 |
$existing_achievements = gamipress_user_get_active_achievements( $user_id ); |
| 85 |
$active_achievements[$user_id] = (array) $achievements + (array) $existing_achievements; |
| 86 |
} else { |
| 87 |
$active_achievements[$user_id] = (array) $achievements; |
| 88 |
} |
| 89 |
|
| 90 |
// Update the user's active achievements |
| 91 |
gamipress_set_cache( 'active_achievements', $active_achievements ); |
| 92 |
|
| 93 |
// Return our updated achievements array |
| 94 |
return $achievements; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get a user's active achievement details |
| 99 |
* |
| 100 |
* @deprecated Unused anymore |
| 101 |
* |
| 102 |
* @since 1.0.0 |
| 103 |
* |
| 104 |
* @param integer $user_id User ID |
| 105 |
* @param integer $achievement_id Achievement post ID |
| 106 |
* @return mixed An achievement object if it exists, false if not |
| 107 |
*/ |
| 108 |
function gamipress_user_get_active_achievement( $user_id = 0, $achievement_id = 0 ) { |
| 109 |
|
| 110 |
// Get the user's active achievements |
| 111 |
$achievements = gamipress_user_get_active_achievements( $user_id ); |
| 112 |
|
| 113 |
// Return the achievement if it exists, or false if not |
| 114 |
return ( isset( $achievements[$achievement_id] ) ) ? $achievements[$achievement_id] : false; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Add an achievement to a user's active achievements |
| 119 |
* |
| 120 |
* @deprecated Unused anymore |
| 121 |
* |
| 122 |
* @since 1.0.0 |
| 123 |
* |
| 124 |
* @param integer $user_id User ID |
| 125 |
* @param integer $achievement_id Achievement post ID |
| 126 |
* @return object The active Achievement object |
| 127 |
*/ |
| 128 |
function gamipress_user_add_active_achievement( $user_id = 0, $achievement_id = 0 ) { |
| 129 |
|
| 130 |
// If achievement is a step, bail here |
| 131 |
if ( 'step' == gamipress_get_post_type( $achievement_id ) ) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
// Get the user's active achievements |
| 136 |
$achievements = gamipress_user_get_active_achievements( $user_id ); |
| 137 |
|
| 138 |
// If it doesn't exist, add the achievement to the array |
| 139 |
if ( ! isset( $achievements[$achievement_id] ) ) { |
| 140 |
$achievements[$achievement_id] = gamipress_build_achievement_object( $achievement_id, 'started' ); |
| 141 |
gamipress_user_update_active_achievements( $user_id, $achievements ); |
| 142 |
} |
| 143 |
|
| 144 |
// Send back the added achievement object |
| 145 |
return $achievements[$achievement_id]; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Update the stored data for an active achievement |
| 150 |
* |
| 151 |
* @deprecated Unused anymore |
| 152 |
* |
| 153 |
* @since 1.0.0 |
| 154 |
* |
| 155 |
* @param integer $user_id User ID |
| 156 |
* @param integer $achievement_id Achievement post ID |
| 157 |
* @param object $achievement Achievement object to insert into user meta |
| 158 |
* |
| 159 |
* @return object The final updated achievement object |
| 160 |
*/ |
| 161 |
function gamipress_user_update_active_achievement( $user_id = 0, $achievement_id = 0, $achievement = null ) { |
| 162 |
|
| 163 |
// If achievement is a step, bail here |
| 164 |
if ( 'step' === gamipress_get_post_type( $achievement_id ) ) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
// If we weren't passed an object, get the latest version from meta |
| 169 |
if ( ! is_object( $achievement ) ) |
| 170 |
$achievement = gamipress_user_get_active_achievement( $user_id, $achievement_id ); |
| 171 |
|
| 172 |
// If we still don't have an object, build one |
| 173 |
if ( ! is_object( $achievement ) ) |
| 174 |
$achievement = gamipress_build_achievement_object( $achievement_id, 'started' ); |
| 175 |
|
| 176 |
// Update our last activity date |
| 177 |
$achievement->last_activity_date = current_time( 'timestamp' ); |
| 178 |
|
| 179 |
// Available filter for manipulating the achievement object |
| 180 |
$achievement = apply_filters( 'gamipress_user_update_active_achievement', $achievement, $user_id, $achievement_id ); |
| 181 |
|
| 182 |
// Update the user's active achievements |
| 183 |
gamipress_user_update_active_achievements( $user_id, array( $achievement_id => $achievement ), true ); |
| 184 |
|
| 185 |
// Return the updated achievement object |
| 186 |
return $achievement; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Remove an achievement from a user's list of active achievements |
| 191 |
* |
| 192 |
* @deprecated Unused anymore |
| 193 |
* |
| 194 |
* @since 1.0.0 |
| 195 |
* |
| 196 |
* @param integer $user_id User ID |
| 197 |
* @param integer $achievement_id Achievement post ID |
| 198 |
* @return array The user's active achievements |
| 199 |
*/ |
| 200 |
function gamipress_user_delete_active_achievement( $user_id = 0, $achievement_id = 0 ) { |
| 201 |
|
| 202 |
// Get the user's active achievements |
| 203 |
$achievements = gamipress_user_get_active_achievements( $user_id ); |
| 204 |
|
| 205 |
// If the achievement exists, unset it |
| 206 |
if ( isset( $achievements[$achievement_id] ) ) |
| 207 |
unset( $achievements[$achievement_id] ); |
| 208 |
|
| 209 |
// Update the user's active achievements |
| 210 |
return gamipress_user_update_active_achievements( $user_id, $achievements ); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Update the user's active achievement meta with each earned achievement |
| 215 |
* |
| 216 |
* @deprecated Unused anymore |
| 217 |
* |
| 218 |
* @since 1.0.0 |
| 219 |
* |
| 220 |
* @param integer $user_id The given user's ID |
| 221 |
* @param integer $achievement_id The given achievement's post ID |
| 222 |
* @return object The final achievement object |
| 223 |
*/ |
| 224 |
function gamipress_user_update_active_achievement_on_earnings( $user_id, $achievement_id ) { |
| 225 |
|
| 226 |
// If achievement is a step, update its parent activity |
| 227 |
if ( 'step' === gamipress_get_post_type( $achievement_id ) ) { |
| 228 |
|
| 229 |
$parent_achievement = gamipress_get_step_achievement( $achievement_id ); |
| 230 |
|
| 231 |
if ( $parent_achievement ) { |
| 232 |
gamipress_user_update_active_achievement( $user_id, $parent_achievement->ID ); |
| 233 |
} |
| 234 |
|
| 235 |
// Otherwise, drop the earned achievement form the user's active achievement array |
| 236 |
} else { |
| 237 |
gamipress_user_delete_active_achievement( $user_id, $achievement_id ); |
| 238 |
} |
| 239 |
|
| 240 |
} |
| 241 |
//add_action( 'gamipress_award_achievement', 'gamipress_user_update_active_achievement_on_earnings', 10, 2 ); |