| @@ -1,0 +1,194 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Logs Exporters | |
| 4 | + * | |
| 5 | + * @package GamiPress\Privacy\Exporters\Logs | |
| 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 logs. | |
| 14 | + * | |
| 15 | + * @since 1.5.0 | |
| 16 | + * | |
| 17 | + * @param array $exporters | |
| 18 | + * | |
| 19 | + * @return array | |
| 20 | + */ | |
| 21 | +function gamipress_privacy_register_logs_exporters( $exporters ) { | |
| 22 | + | |
| 23 | + $exporters[] = array( | |
| 24 | + 'exporter_friendly_name' => __( 'Activity Logs', 'gamipress' ), | |
| 25 | + 'callback' => 'gamipress_privacy_logs_exporter', | |
| 26 | + ); | |
| 27 | + | |
| 28 | + return $exporters; | |
| 29 | + | |
| 30 | +} | |
| 31 | +add_filter( 'wp_privacy_personal_data_exporters', 'gamipress_privacy_register_logs_exporters' ); | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * Exporter for user logs. | |
| 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_logs_exporter( $email_address, $page = 1 ) { | |
| 44 | + | |
| 45 | + global $wpdb; | |
| 46 | + | |
| 47 | + // Setup query vars | |
| 48 | + $logs = GamiPress()->db->logs; | |
| 49 | + | |
| 50 | + // Important: keep always SELECT *, %d is user ID, and limit/offset will added automatically | |
| 51 | + $query = "SELECT * FROM {$logs} 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 logs | |
| 65 | + $user_logs = $wpdb->get_results( $wpdb->prepare( | |
| 66 | + $query . " LIMIT {$offset}, {$limit}", | |
| 67 | + $user->ID | |
| 68 | + ) ); | |
| 69 | + | |
| 70 | + if( is_array( $user_logs ) ) { | |
| 71 | + | |
| 72 | + foreach( $user_logs as $user_log ) { | |
| 73 | + | |
| 74 | + // Add the user log to the exported items array | |
| 75 | + $export_items[] = array( | |
| 76 | + 'group_id' => 'gamipress-logs', | |
| 77 | + 'group_label' => __( 'Activity Logs', 'gamipress' ), | |
| 78 | + 'item_id' => "gamipress-logs-{$user_log->log_id}", | |
| 79 | + 'data' => gamipress_privacy_get_log_data( $user_log ), | |
| 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 log data. | |
| 105 | + * | |
| 106 | + * @since 1.5.0 | |
| 107 | + * | |
| 108 | + * @param stdClass $log | |
| 109 | + * | |
| 110 | + * @return array | |
| 111 | + */ | |
| 112 | +function gamipress_privacy_get_log_data( $log ) { | |
| 113 | + | |
| 114 | + // Prefix for meta data | |
| 115 | + $prefix = '_gamipress_'; | |
| 116 | + | |
| 117 | + // Setup CT table | |
| 118 | + ct_setup_table( 'gamipress_logs' ); | |
| 119 | + | |
| 120 | + $data = array(); | |
| 121 | + | |
| 122 | + // Log title | |
| 123 | + | |
| 124 | + $data['title'] = array( | |
| 125 | + 'name' => __( 'Log', 'gamipress' ), | |
| 126 | + 'value' => $log->title, | |
| 127 | + ); | |
| 128 | + | |
| 129 | + // Log type | |
| 130 | + | |
| 131 | + $log_types = gamipress_get_log_types(); | |
| 132 | + | |
| 133 | + $data['type'] = array( | |
| 134 | + 'name' => __( 'Type', 'gamipress' ), | |
| 135 | + 'value' => isset( $log_types[$log->type] ) ? $log_types[$log->type] : $log->type, | |
| 136 | + ); | |
| 137 | + | |
| 138 | + if( $log->type === 'event_trigger' ) { | |
| 139 | + | |
| 140 | + // Log event | |
| 141 | + | |
| 142 | + $data['trigger_type'] = array( | |
| 143 | + 'name' => __( 'Event', 'gamipress' ), | |
| 144 | + 'value' => gamipress_get_activity_trigger_label( $log->trigger_type ), | |
| 145 | + ); | |
| 146 | + | |
| 147 | + } | |
| 148 | + | |
| 149 | + // If is a specific activity trigger, then add the achievement_post field | |
| 150 | + if( in_array( $log->trigger_type, array_keys( gamipress_get_specific_activity_triggers() ) ) ) { | |
| 151 | + | |
| 152 | + $post_id = ct_get_object_meta( $log->log_id, $prefix . 'achievement_post', true ); | |
| 153 | + $post_site_id = ct_get_object_meta( $log->log_id, $prefix . 'achievement_post_site_id', true ); | |
| 154 | + | |
| 155 | + // Log assigned post | |
| 156 | + | |
| 157 | + $data['post'] = array( | |
| 158 | + 'name' => __( 'Assigned Post', 'gamipress' ), | |
| 159 | + 'value' => gamipress_get_specific_activity_trigger_post_title( $post_id, $log->trigger_type, $post_site_id ), | |
| 160 | + ); | |
| 161 | + | |
| 162 | + // Log assigned post permalink | |
| 163 | + | |
| 164 | + $permalink = gamipress_get_specific_activity_trigger_permalink( $post_id, $log->trigger_type, $post_site_id ); | |
| 165 | + | |
| 166 | + if( $permalink ) { | |
| 167 | + | |
| 168 | + $data['post_url'] = array( | |
| 169 | + 'name' => __( 'Assigned Post URL', 'gamipress' ), | |
| 170 | + 'value' => $permalink, | |
| 171 | + ); | |
| 172 | + | |
| 173 | + } | |
| 174 | + | |
| 175 | + | |
| 176 | + } | |
| 177 | + | |
| 178 | + // Log date | |
| 179 | + | |
| 180 | + $data['date'] = array( | |
| 181 | + 'name' => __( 'Date', 'gamipress' ), | |
| 182 | + 'value' => $log->date, | |
| 183 | + ); | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * User log to export | |
| 187 | + * | |
| 188 | + * @param array $data The user logs data to export | |
| 189 | + * @param int $user_id The user ID | |
| 190 | + * @param stdClass $log The log object | |
| 191 | + */ | |
| 192 | + return apply_filters( 'gamipress_privacy_get_log_data', $data, $log->user_id, $log ); | |
| 193 | + | |
| 194 | +} | |