| 1 |
<?php |
| 2 |
/** |
| 3 |
* User Earnings Exporters |
| 4 |
* |
| 5 |
* @package GamiPress\Privacy\Exporters\User_Earnings |
| 6 |
* @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.5.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Register exporter for user user earnings. |
| 14 |
* |
| 15 |
* @since 1.5.0 |
| 16 |
* |
| 17 |
* @param array $exporters |
| 18 |
* |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
function gamipress_privacy_register_user_earnings_exporters( $exporters ) { |
| 22 |
|
| 23 |
$exporters[] = array( |
| 24 |
'exporter_friendly_name' => __( 'User Earnings', 'gamipress' ), |
| 25 |
'callback' => 'gamipress_privacy_user_earnings_exporter', |
| 26 |
); |
| 27 |
|
| 28 |
return $exporters; |
| 29 |
|
| 30 |
} |
| 31 |
add_filter( 'wp_privacy_personal_data_exporters', 'gamipress_privacy_register_user_earnings_exporters' ); |
| 32 |
|
| 33 |
/** |
| 34 |
* Exporter for user user earnings. |
| 35 |
* |
| 36 |
* @since 1.5.0 |
| 37 |
* |
| 38 |
* @param string $email_address |
| 39 |
* @param int $page |
| 40 |
* |
| 41 |
* @return array |
| 42 |
*/ |
| 43 |
function gamipress_privacy_user_earnings_exporter( $email_address, $page = 1 ) { |
| 44 |
|
| 45 |
global $wpdb; |
| 46 |
|
| 47 |
// Setup query vars |
| 48 |
$user_earnings = GamiPress()->db->user_earnings; |
| 49 |
|
| 50 |
// Important: keep always SELECT *, %d is user ID, and limit/offset will added automatically |
| 51 |
$query = "SELECT * FROM {$user_earnings} WHERE user_id = %d"; |
| 52 |
$count_query = str_replace( "SELECT *", "SELECT COUNT(*)", $query ); |
| 53 |
|
| 54 |
// Setup vars |
| 55 |
$export_items = array(); |
| 56 |
$limit = 500; |
| 57 |
$offset = $limit * ( $page - 1 ); |
| 58 |
$done = true; |
| 59 |
|
| 60 |
$user = get_user_by( 'email', $email_address ); |
| 61 |
|
| 62 |
if ( $user && $user->ID ) { |
| 63 |
|
| 64 |
// Get user earnings |
| 65 |
$earnings = $wpdb->get_results( $wpdb->prepare( |
| 66 |
$query . " LIMIT {$offset}, {$limit}", |
| 67 |
$user->ID |
| 68 |
) ); |
| 69 |
|
| 70 |
if( is_array( $earnings ) ) { |
| 71 |
|
| 72 |
foreach( $earnings as $earning ) { |
| 73 |
|
| 74 |
// Add the user log to the exported items array |
| 75 |
$export_items[] = array( |
| 76 |
'group_id' => 'gamipress-user-earnings', |
| 77 |
'group_label' => __( 'User Earnings', 'gamipress' ), |
| 78 |
'item_id' => "gamipress-user-earnings-{$earning->user_earning_id}", |
| 79 |
'data' => gamipress_privacy_get_user_earning_data( $earning ), |
| 80 |
); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
// Check remaining items |
| 87 |
$exported_items_count = $limit * $page; |
| 88 |
$items_count = absint( $wpdb->get_var( $wpdb->prepare( $count_query, $user->ID ) ) ); |
| 89 |
|
| 90 |
// Process done! |
| 91 |
$done = (bool) ( $exported_items_count >= $items_count ); |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
// Return our exported items |
| 96 |
return array( |
| 97 |
'data' => $export_items, |
| 98 |
'done' => $done |
| 99 |
); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Function to retrieve user earning data. |
| 105 |
* |
| 106 |
* @since 1.5.0 |
| 107 |
* |
| 108 |
* @param stdClass $user_earning |
| 109 |
* |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
function gamipress_privacy_get_user_earning_data( $user_earning ) { |
| 113 |
|
| 114 |
// Prefix for meta data |
| 115 |
$prefix = '_gamipress_'; |
| 116 |
|
| 117 |
// Setup vars |
| 118 |
$requirement_types = gamipress_get_requirement_types(); |
| 119 |
$points_types = gamipress_get_points_types(); |
| 120 |
$achievement_types = gamipress_get_achievement_types(); |
| 121 |
$rank_types = gamipress_get_rank_types(); |
| 122 |
|
| 123 |
// Setup CT table |
| 124 |
ct_setup_table( 'gamipress_user_earnings' ); |
| 125 |
|
| 126 |
$data = array(); |
| 127 |
|
| 128 |
// User earning title |
| 129 |
|
| 130 |
$data['title'] = array( |
| 131 |
'name' => __( 'Title', 'gamipress' ), |
| 132 |
'value' => $user_earning->title, |
| 133 |
); |
| 134 |
|
| 135 |
// User earning type and assigned item |
| 136 |
|
| 137 |
if( in_array( $user_earning->post_type, gamipress_get_requirement_types_slugs() ) ) { |
| 138 |
|
| 139 |
if( $user_earning->post_type === 'step' && $achievement = gamipress_get_step_achievement( $user_earning->post_id ) ) { |
| 140 |
// Step |
| 141 |
|
| 142 |
$data['type'] = array( |
| 143 |
'name' => __( 'Type', 'gamipress' ), |
| 144 |
'value' => sprintf( __( '%s Step', 'gamipress' ), $achievement_types[$achievement->post_type]['singular_name'] ), |
| 145 |
); |
| 146 |
|
| 147 |
$data['achievement'] = array( |
| 148 |
'name' => $achievement_types[$achievement->post_type]['singular_name'], |
| 149 |
'value' => gamipress_get_post_field( 'post_title', $achievement->ID ), |
| 150 |
); |
| 151 |
|
| 152 |
$data['achievement_url'] = array( |
| 153 |
'name' => sprintf( __( '%s URL', 'gamipress' ), $achievement_types[$achievement->post_type]['singular_name'] ), |
| 154 |
'value' => get_post_permalink( $achievement->ID ), |
| 155 |
); |
| 156 |
|
| 157 |
} else if( ( $user_earning->post_type === 'points-award' || $user_earning->post_type === 'points-deduct' ) && $points_type = gamipress_get_points_award_points_type( $user_earning->post_id ) ) { |
| 158 |
// Points Award and Deduction |
| 159 |
|
| 160 |
$format = ( $user_earning->post_type === 'points-award' ? __( '%s Award', 'gamipress' ) : __( '%s Deduction', 'gamipress' ) ); |
| 161 |
|
| 162 |
$data['type'] = array( |
| 163 |
'name' => __( 'Type', 'gamipress' ), |
| 164 |
'value' => sprintf( $format, $points_types[$points_type->post_name]['plural_name'] ), |
| 165 |
); |
| 166 |
|
| 167 |
} else if( $user_earning->post_type === 'rank-requirement' && $rank = gamipress_get_rank_requirement_rank( $user_earning->post_id ) ) { |
| 168 |
// Rank requirement |
| 169 |
|
| 170 |
$data['type'] = array( |
| 171 |
'name' => __( 'Type', 'gamipress' ), |
| 172 |
'value' => sprintf( __( '%s Requirement', 'gamipress' ), $rank_types[$rank->post_type]['singular_name'] ), |
| 173 |
); |
| 174 |
|
| 175 |
$data['rank'] = array( |
| 176 |
'name' => $rank_types[$rank->post_type]['singular_name'], |
| 177 |
'value' => gamipress_get_post_field( 'post_title', $rank->ID ), |
| 178 |
); |
| 179 |
|
| 180 |
$data['rank_url'] = array( |
| 181 |
'name' => sprintf( __( '%s URL', 'gamipress' ), $rank_types[$rank->post_type]['singular_name'] ), |
| 182 |
'value' => get_post_permalink( $rank->ID ), |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
} else if( in_array( $user_earning->post_type, gamipress_get_achievement_types_slugs() ) ) { |
| 187 |
// Achievement |
| 188 |
|
| 189 |
$data['type'] = array( |
| 190 |
'name' => __( 'Type', 'gamipress' ), |
| 191 |
'value' => $achievement_types[$user_earning->post_type]['singular_name'], |
| 192 |
); |
| 193 |
|
| 194 |
$data['achievement'] = array( |
| 195 |
'name' => $achievement_types[$user_earning->post_type]['singular_name'], |
| 196 |
'value' => gamipress_get_post_field( 'post_title', $user_earning->post_id ), |
| 197 |
); |
| 198 |
|
| 199 |
$data['achievement_url'] = array( |
| 200 |
'name' => sprintf( __( '%s URL', 'gamipress' ), $achievement_types[$user_earning->post_type]['singular_name'] ), |
| 201 |
'value' => get_post_permalink( $user_earning->post_id ), |
| 202 |
); |
| 203 |
|
| 204 |
} else if( in_array( $user_earning->post_type, gamipress_get_rank_types_slugs() ) ) { |
| 205 |
// Rank |
| 206 |
|
| 207 |
$data['type'] = array( |
| 208 |
'name' => __( 'Type', 'gamipress' ), |
| 209 |
'value' => $rank_types[$user_earning->post_type]['singular_name'], |
| 210 |
); |
| 211 |
|
| 212 |
$data['rank'] = array( |
| 213 |
'name' => $rank_types[$user_earning->post_type]['singular_name'], |
| 214 |
'value' => gamipress_get_post_field( 'post_title', $user_earning->post_id ), |
| 215 |
); |
| 216 |
|
| 217 |
$data['rank_url'] = array( |
| 218 |
'name' => sprintf( __( '%s URL', 'gamipress' ), $rank_types[$user_earning->post_type]['singular_name'] ), |
| 219 |
'value' => get_post_permalink( $user_earning->post_id ), |
| 220 |
); |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
// User earning points |
| 225 |
|
| 226 |
$points = absint( $user_earning->points ); |
| 227 |
|
| 228 |
// Just add the points if points type exists |
| 229 |
if( $points > 0 && isset( $points_types[$user_earning->points_type] ) ) { |
| 230 |
|
| 231 |
$points_type = $points_types[$user_earning->points_type]; |
| 232 |
|
| 233 |
// Check if is a points deduction or award |
| 234 |
if( $user_earning->post_type === 'points-deduct' ) { |
| 235 |
$name_format = __( '%s Deducted', 'gamipress' ); |
| 236 |
} else { |
| 237 |
$name_format = __( '%s Awarded', 'gamipress' ); |
| 238 |
} |
| 239 |
|
| 240 |
$data['points'] = array( |
| 241 |
'name' => sprintf( $name_format, $points_type['plural_name'] ), |
| 242 |
'value' => $points, |
| 243 |
); |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
// User earning date |
| 248 |
|
| 249 |
$data['date'] = array( |
| 250 |
'name' => __( 'Date', 'gamipress' ), |
| 251 |
'value' => $user_earning->date, |
| 252 |
); |
| 253 |
|
| 254 |
/** |
| 255 |
* User earning to export |
| 256 |
* |
| 257 |
* @param array $data The user earning data to export |
| 258 |
* @param int $user_id The user ID |
| 259 |
* @param stdClass $log The user earning object |
| 260 |
*/ |
| 261 |
return apply_filters( 'gamipress_privacy_get_user_earning_data', $data, $user_earning->user_id, $user_earning ); |
| 262 |
|
| 263 |
} |