class-site-health.php
14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
<?php
/**
* The following class Handles Site Health data.
*
* @since 4.4.1
*
* @package LearnDash\GDPR
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Learndash_Site_Health' ) ) {
/**
* Handles Site Health data.
*
* @since 4.4.1
*/
class Learndash_Site_Health {
private const SITE_HEALTH_KEY = 'learndash';
/**
* Instance of this class.
*
* @since 4.4.1
*
* @var self|null
*/
private static $instance = null;
/**
* Fields.
*
* @var array[]
*/
private $fields = array();
/**
* Constructor.
*
* @since 4.4.1
*
* @return void
*/
public function __construct() {
add_filter( 'debug_information', array( $this, 'add_site_health_info' ) );
}
/**
* Initialize the class.
*
* @since 4.4.1
*
* @return void
*/
public static function init(): void {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
}
/**
* Add Telemetry info to Site Health.
*
* @since 4.4.1
*
* @param array $debug_info Info.
*
* @return array Debug info.
*/
public function add_site_health_info( array $debug_info ): array {
$debug_info[ self::SITE_HEALTH_KEY ] = array(
'label' => __( 'LearnDash', 'learndash' ),
'fields' => $this->get_fields(),
);
return $debug_info;
}
/**
* Maps the telemetry data to the Site Health fields.
*
* @since 4.4.1
*
* @return array
*/
private function get_fields(): array {
if ( ! empty( $this->fields ) ) {
return $this->fields;
}
$this->fields = array_merge(
$this->map_general_fields(),
$this->map_post_count_fields(),
$this->map_settings_fields(),
$this->map_statistics_fields(),
$this->map_constant_fields()
);
return $this->fields;
}
/**
* Maps general fields.
*
* @since 4.4.1
*
* @return array
*/
private function map_general_fields(): array {
$license_is_valid = learndash_is_learndash_license_valid();
$last_updated = array_keys( learndash_data_upgrades_setting( 'version_history' ) )[0] ?? 0;
return array(
'version' => array(
'label' => __( 'Version', 'learndash' ),
'value' => LEARNDASH_VERSION,
),
'last_updated' => array(
'label' => __( 'Last updated', 'learndash' ),
'value' => $last_updated > 0 ? learndash_adjust_date_time_display( $last_updated ) : __( 'Never', 'learndash' ),
'debug' => $last_updated,
),
'previous_version' => array(
'label' => __( 'Previous version', 'learndash' ),
'value' => learndash_data_upgrades_setting( 'prior_version' ),
),
'license_validated' => array(
'label' => __( 'License validated', 'learndash' ),
'value' => $this->bool_to_yes_no_string( $license_is_valid ),
'debug' => $license_is_valid,
),
);
}
/**
* Maps fields with post counts.
*
* @since 4.4.1
*
* @return array[]
*/
private function map_post_count_fields(): array {
return array(
'course_count' => array(
'label' => __( 'Number of courses', 'learndash' ),
'value' => $this->count_by_post_type( LDLMS_Post_Types::COURSE ),
),
'group_count' => array(
'label' => __( 'Number of groups', 'learndash' ),
'value' => $this->count_by_post_type( LDLMS_Post_Types::GROUP ),
),
'lesson_count' => array(
'label' => __( 'Number of lessons', 'learndash' ),
'value' => $this->count_by_post_type( LDLMS_Post_Types::LESSON ),
),
'topic_count' => array(
'label' => __( 'Number of topics', 'learndash' ),
'value' => $this->count_by_post_type( LDLMS_Post_Types::TOPIC ),
),
'quiz_count' => array(
'label' => __( 'Number of quizzes', 'learndash' ),
'value' => $this->count_by_post_type( LDLMS_Post_Types::QUIZ ),
),
'question_count' => array(
'label' => __( 'Number of questions', 'learndash' ),
'value' => $this->count_by_post_type( LDLMS_Post_Types::QUESTION ),
),
'exam_count' => array(
'label' => __( 'Number of challenge exams', 'learndash' ),
'value' => $this->count_by_post_type( LDLMS_Post_Types::EXAM ),
),
'certificate_count' => array(
'label' => __( 'Number of certificates', 'learndash' ),
'value' => $this->count_by_post_type( LDLMS_Post_Types::CERTIFICATE ),
),
);
}
/**
* Maps settings fields.
*
* @since 4.4.1
*
* @return array
*/
private function map_settings_fields(): array {
$shared_steps_enabled = learndash_is_course_shared_steps_enabled();
$focus_mode_enabled = $this->focus_mode_is_enabled();
$is_rtl = is_rtl();
$registration_page_set = learndash_registration_page_is_set();
$currency_code = learndash_get_currency_code();
$nested_urls_enabled = 'yes' === LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_Permalinks', 'nested_urls' );
$paypal_configured = ( new Learndash_Paypal_IPN_Gateway() )->is_ready();
$stripe_configured = ( new Learndash_Stripe_Gateway() )->is_ready();
$razorpay_configured = ( new Learndash_Razorpay_Gateway() )->is_ready();
return array(
'shared_course_steps' => array(
'label' => __( 'Shared course steps', 'learndash' ),
'value' => $this->bool_to_on_off_string( $shared_steps_enabled ),
'debug' => $shared_steps_enabled,
),
'active_template' => array(
'label' => __( 'Active template', 'learndash' ),
'value' => esc_html( LearnDash_Theme_Register::get_active_theme_name() ),
'debug' => esc_attr( LearnDash_Theme_Register::get_active_theme_key() ),
),
'focus_mode' => array(
'label' => __( 'Focus mode', 'learndash' ),
'value' => $this->bool_to_on_off_string( $focus_mode_enabled ),
'debug' => $focus_mode_enabled,
),
'rtl' => array(
'label' => __( 'RTL', 'learndash' ),
'value' => $this->bool_to_yes_no_string( $is_rtl ),
'debug' => $is_rtl,
),
'registration_page' => array(
'label' => __( 'Registration page', 'learndash' ),
'value' => $this->bool_to_on_off_string( $registration_page_set ),
'debug' => $registration_page_set,
),
'currency' => array(
'label' => __( 'Currency', 'learndash' ),
'value' => $currency_code ? $currency_code : __( 'Not set', 'learndash' ),
'debug' => $currency_code,
),
'nested_urls' => array(
'label' => __( 'Nested URLs', 'learndash' ),
'value' => $this->bool_to_on_off_string( $nested_urls_enabled ),
'debug' => $nested_urls_enabled,
),
'payment_gateway_paypal_ipn' => array(
'label' => __( 'PayPal configured', 'learndash' ),
'value' => $this->bool_to_yes_no_string( $paypal_configured ),
'debug' => $paypal_configured,
),
'payment_gateway_stripe_connect' => array(
'label' => __( 'Stripe Connect configured', 'learndash' ),
'value' => $this->bool_to_yes_no_string( $stripe_configured ),
'debug' => $stripe_configured,
),
'payment_gateway_razorpay' => array(
'label' => __( 'Razorpay configured', 'learndash' ),
'value' => $this->bool_to_yes_no_string( $razorpay_configured ),
'debug' => $razorpay_configured,
),
);
}
/**
* Maps statistics fields.
*
* @since 4.4.1
*
* @return array
*/
private function map_statistics_fields(): array {
return array(
'course_using_free_form_progression_count' => array(
'label' => __( 'Number of courses using free form progression', 'learndash' ),
'value' => $this->count_by_post_type_and_setting( LDLMS_Post_Types::COURSE, 'course_disable_lesson_progression', 'on' ),
),
'course_using_linear_progression_count' => array(
'label' => __( 'Number of courses using linear progression', 'learndash' ),
'value' => $this->count_by_post_type_and_setting( LDLMS_Post_Types::COURSE, 'course_disable_lesson_progression' ),
),
'lesson_using_video_progression_count' => array(
'label' => __( 'Number of lessons using video progression', 'learndash' ),
'value' => $this->count_by_post_type_and_setting( LDLMS_Post_Types::LESSON, 'lesson_video_enabled', 'on' ),
),
'lesson_using_drip_content_count' => array(
'label' => __( 'Number of lessons using drip content', 'learndash' ),
'value' => $this->count_by_post_type_and_setting( LDLMS_Post_Types::LESSON, 'lesson_schedule', '[a-z_]+' ),
),
'topic_using_drip_content_count' => array(
'label' => __( 'Number of topics using drip content', 'learndash' ),
'value' => $this->count_by_post_type_and_setting( LDLMS_Post_Types::TOPIC, 'lesson_schedule', '[a-z_]+' ),
),
'quiz_using_randomized_question_ordering_count' => array(
'label' => __( 'Number of quizzes using randomized question ordering', 'learndash' ),
'value' => $this->count_by_post_type_and_setting( LDLMS_Post_Types::QUIZ, 'questionRandom', '1' ),
),
'enrolled_user_count' => array(
'label' => __( 'Number of enrolled users', 'learndash' ),
'value' => $this->get_enrolled_user_count(),
),
);
}
/**
* Maps fields with LD constants.
*
* @since 4.4.1
*
* @return array
*/
private function map_constant_fields(): array {
$learndash_debug = defined( 'LEARNDASH_DEBUG' ) && LEARNDASH_DEBUG; // @phpstan-ignore-line -- Constant can be true/false.
$learndash_script_debug = defined( 'LEARNDASH_SCRIPT_DEBUG' ) && LEARNDASH_SCRIPT_DEBUG; // @phpstan-ignore-line -- Constant can be true/false.
return array(
'LEARNDASH_DEBUG' => array(
'label' => __( 'LEARNDASH_DEBUG', 'learndash' ),
'value' => $this->bool_to_yes_no_string( $learndash_debug ),
'debug' => $learndash_debug,
),
'LEARNDASH_SCRIPT_DEBUG' => array(
'label' => __( 'LEARNDASH_SCRIPT_DEBUG', 'learndash' ),
'value' => $this->bool_to_yes_no_string( $learndash_script_debug ),
'debug' => $learndash_script_debug,
),
);
}
/**
* Returns a number of published posts by a post type.
*
* @since 4.4.1
*
* @param string $post_type_key Post type key.
*
* @return int
*/
protected function count_by_post_type( string $post_type_key ): int {
$post_type = LDLMS_Post_Types::get_post_type_slug( $post_type_key );
return wp_count_posts( $post_type )->publish;
}
/**
* Returns a number of published posts filtered by a post type and a setting.
*
* @since 4.4.1
*
* @param string $post_type_key Post type key.
* @param string $setting_key Setting key to filter by.
* @param string $setting_value Optional. Setting value to filter by. Can be a regular expression. Default empty string.
*
* @return int
*/
protected function count_by_post_type_and_setting( string $post_type_key, string $setting_key, string $setting_value = '' ): int {
$query_args = array(
'post_type' => LDLMS_Post_Types::get_post_type_slug( $post_type_key ),
'post_status' => 'publish',
'fields' => 'ids',
'nopaging' => true,
'meta_key' => '_' . LDLMS_Post_Types::get_post_type_slug( $post_type_key ),
'meta_value' => $this->map_meta_value_from_setting( $setting_key, $setting_value ),
'meta_compare' => 'RLIKE',
);
$query = new WP_Query( $query_args );
return $query->found_posts;
}
/**
* Returns a number of enrolled users.
*
* @since 4.4.1
*
* @return int
*/
protected function get_enrolled_user_count(): int {
// Get course IDs for courses with a price type "open".
$open_course_query = new WP_Query(
array(
'post_type' => LDLMS_Post_Types::get_post_type_slug( LDLMS_Post_Types::COURSE ),
'post_status' => 'publish',
'fields' => 'ids',
'meta_key' => '_ld_price_type',
'meta_value' => 'open',
)
);
// If there is at least one open course, just return the number of users without an administrator.
if ( $open_course_query->found_posts > 0 ) {
return learndash_students_enrolled_count();
}
// Get course IDs for all courses, except with a price type "open".
$not_open_course_query = new WP_Query(
array(
'post_type' => LDLMS_Post_Types::get_post_type_slug( LDLMS_Post_Types::COURSE ),
'post_status' => 'publish',
'fields' => 'ids',
'nopaging' => true,
'meta_key' => '_ld_price_type',
'meta_value' => 'open',
'meta_compare' => '!=',
)
);
if ( 0 === $not_open_course_query->found_posts ) {
return 0;
}
global $wpdb;
return (int) $wpdb->get_var(
// The 1st select is: user ids that were directly enrolled.
// The 2nd select is: user ids that were "enrolled" via group -> course access.
$wpdb->prepare(
"
SELECT count(user_id) FROM (
SELECT user_id FROM $wpdb->usermeta WHERE meta_key LIKE %s
UNION
SELECT user_id FROM $wpdb->usermeta WHERE meta_key LIKE %s AND meta_value in (
SELECT DISTINCT REPLACE( meta_key, %s, '' ) FROM $wpdb->postmeta WHERE meta_key LIKE %s
)
) as enrolled_users;
",
$wpdb->esc_like( 'course_' ) . '%' . $wpdb->esc_like( '_access_from' ),
$wpdb->esc_like( 'learndash_group_users_' ) . '%',
'learndash_group_enrolled_',
$wpdb->esc_like( 'learndash_group_enrolled_' ) . '%'
)
);
}
/**
* Converts a boolean value to the "On" or "Off" string.
*
* @since 4.4.1
*
* @param bool $value Value.
*
* @return string
*/
protected function bool_to_on_off_string( bool $value ): string {
return $value ? __( 'On', 'learndash' ) : __( 'Off', 'learndash' );
}
/**
* Converts a boolean value to the "Yes" or "No" string.
*
* @since 4.4.1
*
* @param bool $value Value.
*
* @return string
*/
protected function bool_to_yes_no_string( bool $value ): string {
return $value ? __( 'Yes', 'learndash' ) : __( 'No', 'learndash' );
}
/**
* Returns true if the focus mode is enabled.
*
* @since 4.4.1
*
* @return bool
*/
private function focus_mode_is_enabled(): bool {
if ( learndash_is_active_theme( 'legacy' ) ) {
return false;
}
return 'yes' === LearnDash_Settings_Section::get_section_setting(
'LearnDash_Settings_Theme_LD30',
'focus_mode_enabled'
);
}
/**
* Maps the regexp meta value from the setting key and value.
*
* @since 4.4.1
*
* @param string $setting_key Setting key.
* @param string $setting_value Setting value.
*
* @return string
*/
protected function map_meta_value_from_setting( string $setting_key, string $setting_value ): string {
// Regular expressions are explained in includes/admin/classes-filters/class-learndash-admin-filter-meta.php.
$regexp_part = '' !== $setting_value
? '";.:[^;]*:?"?' . $setting_value . '"?;'
: '";.:[^;]*:?"";';
return $setting_key . $regexp_part;
}
}
}