LLMS_Privacy_Exporters

LifterLMS Privacy Exporter class


Source Source

File: includes/privacy/class-llms-privacy-exporters.php

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
class LLMS_Privacy_Exporters extends LLMS_Privacy {
 
    /**
     * Export student achievement data by email address
     *
     * @since 3.18.0
     * @since 3.37.9 Added `$group_description` to the group exporter.
     *
     * @param    string $email_address  Email address of the user to retrieve data for.
     * @param    int    $page           Process page number.
     * @return   array
     */
    public static function achievement_data( $email_address, $page ) {
 
        $data = array();
 
        $student = self::get_student_by_email( $email_address );
        if ( ! $student ) {
            return self::get_return( $data );
        }
 
        $achievements = self::get_student_achievements( $student );
        if ( $achievements ) {
 
            $group_label       = __( 'Achievements', 'lifterlms' );
            $group_description = __( 'Student achievement data.', 'lifterlms' );
            foreach ( $achievements as $achievement ) {
 
                $data[] = array(
                    'group_id'          => 'lifterlms_achievements',
                    'group_label'       => $group_label,
                    'group_description' => $group_description,
                    'item_id'           => sprintf( 'achievement-%d', $achievement->get( 'id' ) ),
                    'data'              => self::get_achievement_data( $achievement ),
                );
 
            }
        }
 
        return self::get_return( $data );
 
    }
 
    /**
     * Export student certificate data by email address
     *
     * @since 3.18.0
     * @since 3.37.9 Added `$group_description` to the group exporter.
     *
     * @param    string $email_address Email address of the user to retrieve data for.
     * @param    int    $page          Process page number.
     * @return   array
     */
    public static function certificate_data( $email_address, $page ) {
 
        $data = array();
 
        $student = self::get_student_by_email( $email_address );
        if ( ! $student ) {
            return self::get_return( $data );
        }
 
        $certs = self::get_student_certificates( $student );
        if ( $certs ) {
 
            $group_label       = __( 'Certificates', 'lifterlms' );
            $group_description = __( 'Student certificate data.', 'lifterlms' );
            foreach ( $certs as $cert ) {
 
                $data[] = array(
                    'group_id'          => 'lifterlms_certificates',
                    'group_label'       => $group_label,
                    'group_description' => $group_description,
                    'item_id'           => sprintf( 'certificate-%d', $cert->get( 'id' ) ),
                    'data'              => self::get_certificate_data( $cert ),
                );
 
            }
        }
 
        return self::get_return( $data );
 
    }
 
    /**
     * Get data for a certificate
     *
     * @since    3.18.0
     *
     * @param    obj $achievement  LLMS_User_Certificate.
     * @return   array
     */
    private static function get_achievement_data( $achievement ) {
 
        $data = array();
 
        $data[] = array(
            'name'  => __( 'Title', 'lifterlms' ),
            'value' => $achievement->get( 'title' ),
        );
 
        $data[] = array(
            'name'  => __( 'Description', 'lifterlms' ),
            'value' => $achievement->get( 'content' ),
        );
 
        $data[] = array(
            'name'  => __( 'Earned Date', 'lifterlms' ),
            'value' => $achievement->get_earned_date( 'Y-m-d H:i:s' ),
        );
 
        $data[] = array(
            'name'  => __( 'Image', 'lifterlms' ),
            'value' => $achievement->get_image(),
        );
 
        return $data;
 
    }
 
 
 
    /**
     * Get data for a certificate.
     *
     * @since 3.18.0
     * @since 6.0.0 Replaced the use of the deprecated `certificate_title` meta key with the post's title property.
     *
     * @param LLMS_User_Certificate $cert Certificate object.
     * @return array
     */
    private static function get_certificate_data( $cert ) {
 
        $data = array();
 
        $title = $cert->get( 'title' );
 
        $filename = llms()->certificates()->get_export( $cert->get( 'id' ), true );
        if ( ! is_wp_error( $filename ) ) {
            $title = '<a href="certificates/' . basename( $filename ) . '">' . $title . '</a>';
        }
 
        $data[] = array(
            'name'  => __( 'Title', 'lifterlms' ),
            'value' => $title,
        );
 
        $data[] = array(
            'name'  => __( 'Earned Date', 'lifterlms' ),
            'value' => $cert->get_earned_date( 'Y-m-d H:i:s' ),
        );
 
        return $data;
 
    }
 
    /**
     * Get an array of enrollment data for a course or membership
     *
     * @since 3.18.0
     * @since 3.30.3 Fixed spelling errors.
     *
     * @param int $post_id WP Post ID of course or membership.
     * @param obj $student LLMS_Student.
     * @param obj $post_type_object WP post type object.
     * @return array
     */
    private static function get_enrollment_data( $post_id, $student, $post_type_object ) {
 
        $data = array();
 
        $data[] = array(
            // Translators: %s = post type singular name label (Course or Membership).
            'name'  => sprintf( __( '%s Title', 'lifterlms' ), $post_type_object->labels->singular_name ),
            'value' => get_the_title( $post_id ),
        );
 
        $data[] = array(
            'name'  => __( 'Enrollment Status', 'lifterlms' ),
            'value' => llms_get_enrollment_status_name( $student->get_enrollment_status( $post_id ) ),
        );
 
        $data[] = array(
            'name'  => __( 'Enrollment Date', 'lifterlms' ),
            'value' => $student->get_enrollment_date( $post_id, 'enrolled', 'Y-m-d H:i:s' ),
        );
 
        if ( 'course' === $post_type_object->name ) {
 
            $data[] = array(
                'name'  => __( 'Last Activity', 'lifterlms' ),
                'value' => $student->get_enrollment_date( $post_id, 'updated', 'Y-m-d H:i:s' ),
            );
 
            $progress = $student->get_progress( $post_id, 'course' );
            if ( is_numeric( $progress ) ) {
                $progress .= '%';
            }
            $data[] = array(
                'name'  => __( 'Progress', 'lifterlms' ),
                'value' => $progress,
            );
 
            $grade = $student->get_grade( $post_id );
            if ( is_numeric( $grade ) ) {
                $grade .= '%';
            }
            $data[] = array(
                'name'  => __( 'Grade', 'lifterlms' ),
                'value' => $grade,
            );
 
        }
 
        return apply_filters( 'llms_privacy_export_enrollment_data', $data, $post_id, $student, $post_type_object );
 
    }
 
    /**
     * Retrieve export data for a single order
     *
     * @since 3.18.0
     *
     * @param  LLMS_Order $order Order object.
     * @return array
     */
    private static function get_order_data( $order ) {
 
        $data = array();
 
        $props = self::get_order_data_props( 'export' );
 
        foreach ( $props as $prop => $name ) {
 
            $value = apply_filters( 'llms_privacy_export_order_data_prop_value', $order->get( $prop ), $prop, $order );
 
            if ( $value ) {
                $data[] = array(
                    'name'  => $name,
                    'value' => $value,
                );
            }
        }
 
        $transactions = $order->get_transactions(
            array(
                'per_page' => 500,
            )
        );
        if ( $transactions['transactions'] ) {
            $txns = array();
            foreach ( $transactions['transactions'] as $txn ) {
                $txns[] = sprintf( '%1$s &mdash; %2$s (#%3$d)', $txn->get( 'date' ), $txn->get_price( 'amount' ), $txn->get( 'id' ) );
            }
            $data[] = array(
                'name'  => __( 'Transactions', 'lifterlms' ),
                'value' => implode( '<br>', $txns ),
            );
        }
 
        return apply_filters( 'llms_privacy_export_order_data', $data, $order );
    }
 
    /**
     * Get export data for a single quiz attempt
     *
     * @since 3.18.0
     *
     * @param LLMS_Quiz_Attempt $attempt Quiz attempt object.
     * @return array
     */
    private static function get_quiz_attempt_data( $attempt ) {
 
        $data = array();
 
        $quiz = $attempt->get_quiz();
        if ( $quiz ) {
            $data[] = array(
                'name'  => __( 'Title', 'lifterlms' ),
                'value' => $quiz->get( 'title' ),
            );
        }
 
        $data[] = array(
            'name'  => __( 'Attempt ID', 'lifterlms' ),
            'value' => $attempt->get_key(),
        );
 
        $data[] = array(
            'name'  => __( 'Attempt Number', 'lifterlms' ),
            'value' => $attempt->get( 'attempt' ),
        );
 
        $data[] = array(
            'name'  => __( 'Status', 'lifterlms' ),
            'value' => $attempt->l10n( 'status' ),
        );
 
        $grade  = $attempt->get( 'grade' );
        $data[] = array(
            'name'  => __( 'Grade', 'lifterlms' ),
            'value' => is_numeric( $grade ) ? $grade . '%' : '&ndash;',
        );
 
        return $data;
    }
 
    /**
     * Return export data to an exporter
     *
     * @since 3.18.0
     *
     * @param array $data Array of data.
     * @return array
     */
    private static function get_return( $data = array(), $done = true ) {
        return array(
            'data' => $data,
            'done' => $done,
        );
    }
 
    /**
     * Get student data to export for a user
     *
     * @since 3.18.0
     *
     * @param  LLMS_Student $student Student object.
     * @return array
     */
    private static function get_student_data( $student ) {
 
        $data = array();
 
        $props = self::get_student_data_props();
 
        foreach ( $props as $prop => $name ) {
 
            $value = apply_filters( 'llms_privacy_export_student_data_prop_value', $student->get( $prop ), $prop, $student );
 
            if ( $value ) {
                $data[] = array(
                    'name'  => $name,
                    'value' => $value,
                );
            }
        }
 
        return apply_filters( 'llms_privacy_export_student_data', $data, $student );
 
    }
 
    /**
     * Export student course data by email address
     *
     * @since 3.18.0
     *
     * @param string $email_address Email address of the user to retrieve data for.
     * @param int    $page          Process page number.
     * @return array
     */
    public static function course_data( $email_address, $page ) {
        return self::enrollment_data( $email_address, $page, 'course' );
    }
 
    /**
     * General exporter for handling course and membership enrollment data
     *
     * @since 3.18.0
     * @since 3.37.9 Added `$group_description` to the group exporter.
     *
     * @param    string $email_address  Requested user's email address
     * @param    int    $page           process page number
     * @param    string $post_type      name of the post type
     * @return   array
     */
    private static function enrollment_data( $email_address, $page, $post_type ) {
 
        $data = array();
 
        $student = self::get_student_by_email( $email_address );
        if ( ! $student ) {
            return self::get_return( $data );
        }
 
        $enrollments = self::get_student_enrollments( $student, $page, $post_type );
        if ( $enrollments['results'] ) {
 
            $post_type_obj = get_post_type_object( $post_type );
            $group_id      = 'lifterlms_' . $post_type;
 
            foreach ( $enrollments['results'] as $post_id ) {
 
                $data[] = array(
                    'group_id'          => $group_id,
                    'group_label'       => $post_type_obj->labels->name,
                    /* translators: %s: The name of the enrollment post type. */
                    'group_description' => sprintf( __( 'Student %s enrollment data.', 'lifterlms' ), $post_type_obj->labels->name ),
                    'item_id'           => sprintf( '%1$s-%2$d', $post_type, $post_id ),
                    'data'              => self::get_enrollment_data( $post_id, $student, $post_type_obj ),
                );
 
            }
        }
 
        return self::get_return( $data, $enrollments['done'] );
 
    }
 
    /**
     * Add files to the zip file for a data export request.
     *
     * Adds certificate files into the `/certificates/` directory within the archive.
     *
     * @since 3.18.0
     * @since 6.0.0 Replaced the use of the deprecated `wp_get_user_request_data()` function with `wp_get_user_request()`.
     *
     * @param string $archive_pathname     Full path to the zip archive.
     * @param string $archive_url          Full URI to the zip archive.
     * @param string $html_report_pathname Full path to the .html file within the archive.
     * @param int    $request_id           WP Post ID of the export request.
     * @return void
     */
    public static function maybe_add_export_files( $archive_pathname, $archive_url, $html_report_pathname, $request_id ) {
 
        if ( ! class_exists( 'ZipArchive' ) ) {
            return;
        }
 
        $request = wp_get_user_request( $request_id );
        $student = self::get_student_by_email( $request->email );
 
        if ( ! $student ) {
            return;
        }
 
        $certs = self::get_student_certificates( $student );
        if ( ! $certs ) {
            return;
        }
 
        $zip    = new ZipArchive();
        $delete = array();
        if ( true === $zip->open( $archive_pathname ) ) {
            foreach ( $certs as $cert ) {
                $filepath                        = llms()->certificates()->get_export( $cert->get( 'id' ), true );
                $delete[ $cert->certificate_id ] = $filepath;
                if ( is_wp_error( $filepath ) ) {
                    continue;
                }
                $zip->addFile( $filepath, '/certificates/' . basename( $filepath ) );
            }
        }
 
        $zip->close();
 
        // cleanup all files
        foreach ( $delete as $id => $path ) {
            wp_delete_file( $path );
            delete_post_meta( $id, '_llms_export_filepath' );
        }
 
    }
 
    /**
     * Export student membership data by email address
     *
     * @since    3.18.0
     *
     * @param    string $email_address  email address of the user to retrieve data for
     * @param    int    $page           process page number
     * @return   array
     */
    public static function membership_data( $email_address, $page ) {
        return self::enrollment_data( $email_address, $page, 'llms_membership' );
    }
 
    /**
     * Export student orders data by email address
     *
     * @since 3.18.0
     * @since 3.37.9 Added `$group_description` to the group exporter.
     *
     * @param  string $email_address Email address of the user to retrieve data for.
     * @param  int    $page          Process page number.
     * @return array
     */
    public static function order_data( $email_address, $page ) {
 
        $data = array();
 
        $student = self::get_student_by_email( $email_address );
        if ( ! $student ) {
            return self::get_return( $data );
        }
 
        $orders = self::get_student_orders( $student, $page );
 
        $group_label       = __( 'Orders', 'lifterlms' );
        $group_description = __( 'Student orders data.', 'lifterlms' );
        foreach ( $orders['orders'] as $order ) {
 
            $data[] = array(
                'group_id'          => 'lifterlms_orders',
                'group_label'       => $group_label,
                'group_description' => $group_description,
                'item_id'           => sprintf( 'order-%d', $order->get( 'id' ) ),
                'data'              => self::get_order_data( $order ),
            );
 
        }
 
        return self::get_return( $data, $orders['done'] );
 
    }
 
    /**
     * Export student data by email address
     *
     * @since 3.18.0
     * @since 3.37.9 Added `$group_description` to the group exporter.
     *
     * @param  string $email_address Email address of the user to retrieve data for.
     * @param  int    $page          Process page number.
     * @return array
     */
    public static function student_data( $email_address, $page ) {
 
        $data = array();
 
        $student = self::get_student_by_email( $email_address );
        if ( ! $student ) {
            return self::get_return( $data );
        }
 
        $data[] = array(
            'group_id'          => 'lifterlms_student',
            'group_label'       => __( 'Personal Information', 'lifterlms' ),
            'group_description' => __( 'Student personal information data.', 'lifterlms' ),
            'item_id'           => sprintf( 'student-%d', $student->get( 'id' ) ),
            'data'              => self::get_student_data( $student ),
        );
 
        return self::get_return( $data );
 
    }
 
    /**
     * Export quiz attempt data by email address
     *
     * @since    3.18.0
     * @since    3.37.9 Added `$group_description` to the group exporter.
     *
     * @param    string $email_address Email address of the user to retrieve data for.
     * @param    int    $page          Process page number.
     * @return   array
     */
    public static function quiz_data( $email_address, $page ) {
 
        $data = array();
 
        $student = self::get_student_by_email( $email_address );
        if ( ! $student ) {
            return self::get_return( $data );
        }
 
        $query = self::get_student_quizzes( $student, $page );
        $done  = true;
        if ( $query->has_results() ) {
 
            $group_label        = __( 'Quiz Attempts', 'lifterlms' );
            $group_descriptions = __( 'Student quiz attempt data', 'lifterlms' );
            foreach ( $query->get_attempts() as $attempt ) {
 
                $data[] = array(
                    'group_id'          => 'lifterlms_quizzes',
                    'group_label'       => $group_label,
                    'group_description' => $group_description,
                    'item_id'           => sprintf( 'order-%d', $attempt->get( 'id' ) ),
                    'data'              => self::get_quiz_attempt_data( $attempt ),
                );
 
            }
 
            $done = $query->is_last_page();
 
        }
 
        return self::get_return( $data, $done );
 
    }
 
}


Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
3.37.9 Add export group descriptions.
3.30.3 Fixed spelling error.
3.18.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.