| @@ -1,0 +1,149 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * User Data Exporters | |
| 4 | + * | |
| 5 | + * @package GamiPress\Privacy\Exporters\User_Data | |
| 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 meta data. | |
| 14 | + * | |
| 15 | + * @since 1.5.0 | |
| 16 | + * | |
| 17 | + * @param array $exporters | |
| 18 | + * | |
| 19 | + * @return array | |
| 20 | + */ | |
| 21 | +function gamipress_privacy_register_user_data_exporters( $exporters ) { | |
| 22 | + | |
| 23 | + $exporters[] = array( | |
| 24 | + 'exporter_friendly_name' => __( 'Gamification Data', 'gamipress' ), | |
| 25 | + 'callback' => 'gamipress_privacy_user_data_exporter', | |
| 26 | + ); | |
| 27 | + | |
| 28 | + return $exporters; | |
| 29 | + | |
| 30 | +} | |
| 31 | +add_filter( 'wp_privacy_personal_data_exporters', 'gamipress_privacy_register_user_data_exporters' ); | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * Exporter for user meta data. | |
| 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_data_exporter( $email_address, $page = 1 ) { | |
| 44 | + | |
| 45 | + // Setup vars | |
| 46 | + $export_items = array(); | |
| 47 | + $points_types = gamipress_get_points_types(); | |
| 48 | + $achievement_types = gamipress_get_achievement_types(); | |
| 49 | + $rank_types = gamipress_get_rank_types(); | |
| 50 | + | |
| 51 | + $user = get_user_by( 'email', $email_address ); | |
| 52 | + | |
| 53 | + if ( $user && $user->ID ) { | |
| 54 | + | |
| 55 | + /* -------------------------- | |
| 56 | + * Points types | |
| 57 | + -------------------------- */ | |
| 58 | + | |
| 59 | + $user_points = array(); | |
| 60 | + | |
| 61 | + // Get all user points types and their hidden meta data | |
| 62 | + foreach( $points_types as $points_type => $points_type_data ) { | |
| 63 | + | |
| 64 | + // User balance | |
| 65 | + $user_points[$points_type] = array( | |
| 66 | + 'name' => $points_type_data['plural_name'], | |
| 67 | + 'value' => gamipress_get_user_points( $user->ID, $points_type ), | |
| 68 | + ); | |
| 69 | + | |
| 70 | + // Awarded | |
| 71 | + $user_points[$points_type . '_awarded'] = array( | |
| 72 | + 'name' => sprintf( __( '%s Awarded', 'gamipress' ), $points_type_data['plural_name'] ), | |
| 73 | + 'value' => gamipress_get_user_points_awarded( $user->ID, $points_type ), | |
| 74 | + ); | |
| 75 | + | |
| 76 | + // Deducted | |
| 77 | + $user_points[$points_type . '_deducted'] = array( | |
| 78 | + 'name' => sprintf( __( '%s Deducted', 'gamipress' ), $points_type_data['plural_name'] ), | |
| 79 | + 'value' => gamipress_get_user_points_deducted( $user->ID, $points_type ), | |
| 80 | + ); | |
| 81 | + | |
| 82 | + // Expended | |
| 83 | + $user_points[$points_type . '_expended'] = array( | |
| 84 | + 'name' => sprintf( __( '%s Expended', 'gamipress' ), $points_type_data['plural_name'] ), | |
| 85 | + 'value' => gamipress_get_user_points_expended( $user->ID, $points_type ), | |
| 86 | + ); | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * User points balance to export | |
| 90 | + * | |
| 91 | + * @param array $user_points The user points data to export | |
| 92 | + * @param int $user_id The user ID | |
| 93 | + * @param string $points_type The points type slug | |
| 94 | + */ | |
| 95 | + $user_points = apply_filters( "gamipress_privacy_get_user_points_{$points_type}", $user_points, $user->ID, $points_type ); | |
| 96 | + | |
| 97 | + } | |
| 98 | + | |
| 99 | + $export_items[] = array( | |
| 100 | + 'group_id' => 'gamipress-points-types', | |
| 101 | + 'group_label' => __( 'Points Balances', 'gamipress' ), | |
| 102 | + 'item_id' => "gamipress-points-types-{$user->ID}", | |
| 103 | + 'data' => $user_points, | |
| 104 | + ); | |
| 105 | + | |
| 106 | + /* -------------------------- | |
| 107 | + * Rank types | |
| 108 | + -------------------------- */ | |
| 109 | + | |
| 110 | + $user_ranks = array(); | |
| 111 | + | |
| 112 | + // Get all user rank types and their hidden meta data | |
| 113 | + foreach( $rank_types as $rank_type => $rank_type_data ) { | |
| 114 | + | |
| 115 | + $user_rank = gamipress_get_user_rank( $user->ID, $rank_type ); | |
| 116 | + | |
| 117 | + // User Rank | |
| 118 | + $user_ranks[$rank_type] = array( | |
| 119 | + 'name' => $rank_type_data['singular_name'], | |
| 120 | + 'value' => $user_rank->post_title, | |
| 121 | + ); | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * User ranks to export | |
| 125 | + * | |
| 126 | + * @param array $user_rank The user rank data to export | |
| 127 | + * @param int $user_id The user ID | |
| 128 | + * @param string $rank_type The rank type slug | |
| 129 | + */ | |
| 130 | + $user_ranks = apply_filters( "gamipress_privacy_get_user_rank_{$rank_type}", $user_ranks, $user->ID, $rank_type ); | |
| 131 | + | |
| 132 | + } | |
| 133 | + | |
| 134 | + $export_items[] = array( | |
| 135 | + 'group_id' => 'gamipress-rank-types', | |
| 136 | + 'group_label' => __( 'Ranks', 'gamipress' ), | |
| 137 | + 'item_id' => "gamipress-rank-types-{$user->ID}", | |
| 138 | + 'data' => $user_ranks, | |
| 139 | + ); | |
| 140 | + | |
| 141 | + } | |
| 142 | + | |
| 143 | + // Return our exported items | |
| 144 | + return array( | |
| 145 | + 'data' => $export_items, | |
| 146 | + 'done' => true | |
| 147 | + ); | |
| 148 | + | |
| 149 | +} | |