__( 'Activity Signature', 'activitysignature' ), 'callback' => array( __CLASS__, 'export_personal_data' ), ); return $exporters; } /** * Register the personal data eraser. * * @param array $erasers Registered erasers. * * @return array */ public static function register_eraser( $erasers ) { $erasers['activitysignature'] = array( 'eraser_friendly_name' => __( 'Activity Signature', 'activitysignature' ), 'callback' => array( __CLASS__, 'erase_personal_data' ), ); return $erasers; } /** * Export activity associated with an email address. * * @param string $email_address Requested email address. * @param int $page Export page. * * @return array */ public static function export_personal_data( $email_address, $page = 1 ) { $email_address = sanitize_email( $email_address ); if ( '' === $email_address ) { return array( 'data' => array(), 'done' => true ); } $user = get_user_by( 'email', $email_address ); $user_id = $user ? (int) $user->ID : 0; $user_login = $user ? $user->user_login : ''; global $wpdb; $table = Activitysignature_Helpers::get_table_name(); $per_page = 100; $offset = ( max( 1, (int) $page ) - 1 ) * $per_page; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table privacy export; one-shot paginated read. $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT id, activity_type, activity_data, activity_date FROM %i WHERE (user_id = %d AND user_id > 0) OR user_email = %s OR (actor_user_id = %d AND actor_user_id > 0) OR (user_login <> \'\' AND user_login = %s) ORDER BY id ASC LIMIT %d OFFSET %d', $table, $user_id, $email_address, $user_id, $user_login, $per_page, $offset ), ARRAY_A ); $data = array(); if ( $user && 1 === max( 1, (int) $page ) ) { $profile_data = array(); $profile_meta = array( Activitysignature_Helpers::get_user_meta_key( 'created_date' ) => __( 'Account creation date', 'activitysignature' ), Activitysignature_Helpers::get_user_meta_key( 'last_update' ) => __( 'Last profile update', 'activitysignature' ), Activitysignature_Helpers::get_user_meta_key( 'last_login' ) => __( 'Last login', 'activitysignature' ), ); foreach ( $profile_meta as $meta_key => $label ) { $value = get_user_meta( $user->ID, $meta_key, true ); if ( '' !== $value ) { $profile_data[] = array( 'name' => $label, 'value' => $value ); } } if ( ! empty( $profile_data ) ) { $data[] = array( 'group_id' => 'activitysignature-profile', 'group_label' => __( 'Activity Signature profile data', 'activitysignature' ), 'item_id' => 'user-' . (int) $user->ID, 'data' => $profile_data, ); } } foreach ( (array) $rows as $row ) { $data[] = array( 'group_id' => 'activitysignature', 'group_label' => __( 'Activity Signature activity', 'activitysignature' ), 'item_id' => 'activity-' . (int) $row['id'], 'data' => array( array( 'name' => __( 'Date', 'activitysignature' ), 'value' => $row['activity_date'] ), array( 'name' => __( 'Activity', 'activitysignature' ), 'value' => $row['activity_type'] ), array( 'name' => __( 'Details', 'activitysignature' ), 'value' => $row['activity_data'] ), ), ); } return array( 'data' => $data, 'done' => count( (array) $rows ) < $per_page, ); } /** * Erase activity associated with an email address. * * @param string $email_address Requested email address. * @param int $page Erasure page. * * @return array */ public static function erase_personal_data( $email_address, $page = 1 ) { $email_address = sanitize_email( $email_address ); if ( '' === $email_address ) { return self::eraser_result( false, true ); } $user = get_user_by( 'email', $email_address ); $user_id = $user ? (int) $user->ID : 0; $user_login = $user ? $user->user_login : ''; global $wpdb; $table = Activitysignature_Helpers::get_table_name(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table privacy erase; caching delete candidates is not applicable. $ids = $wpdb->get_col( $wpdb->prepare( 'SELECT id FROM %i WHERE (user_id = %d AND user_id > 0) OR user_email = %s OR (user_login <> \'\' AND user_login = %s) ORDER BY id ASC LIMIT %d', $table, $user_id, $email_address, $user_login, 500 ) ); $removed = false; if ( ! empty( $ids ) ) { $result = Activitysignature_Helpers::delete_rows_by_ids( $ids ); if ( false === $result ) { return array( 'items_removed' => false, 'items_retained' => true, 'messages' => array( __( 'Activity records could not be erased because of a database error.', 'activitysignature' ) ), 'done' => false, ); } $removed = $result > 0; } if ( $user_id > 0 ) { // Preserve other users' audit records while removing this person's actor association. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table anonymization write. $anonymized = $wpdb->query( $wpdb->prepare( 'UPDATE %i SET actor_user_id = 0 WHERE actor_user_id = %d', $table, $user_id ) ); if ( $anonymized > 0 ) { Activitysignature_Helpers::flush_activity_caches(); } $removed = $anonymized > 0 || $removed; } if ( $user ) { foreach ( array( Activitysignature_Helpers::get_user_meta_key( 'created_date' ), Activitysignature_Helpers::get_user_meta_key( 'last_update' ), Activitysignature_Helpers::get_user_meta_key( 'last_login' ), Activitysignature_Helpers::get_user_meta_key( 'last_login_checked' ), Activitysignature_Helpers::get_user_meta_key( 'creation_recorded' ) ) as $meta_key ) { $removed = delete_user_meta( $user->ID, $meta_key ) || $removed; } } return self::eraser_result( $removed, count( $ids ) < 500 ); } /** * Build an eraser response. * * @param bool $removed Whether data was removed. * @param bool $done Whether erasure is complete. * * @return array */ private static function eraser_result( $removed, $done ) { return array( 'items_removed' => (bool) $removed, 'items_retained' => false, 'messages' => array(), 'done' => (bool) $done, ); } /** * Add suggested privacy policy content. */ public static function add_policy_content() { if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) { return; } $content = '
' . esc_html__( 'This site records user account events such as account creation, profile changes, logins, failed logins, role changes, and deletion. Records may contain usernames, email addresses, IP addresses, the account that performed an action, and event dates. Activity records are retained according to the configured retention period.', 'activitysignature' ) . '
'; wp_add_privacy_policy_content( __( 'Activity Signature', 'activitysignature' ), wp_kses_post( wpautop( $content, false ) ) ); } }