class-ld-settings-section-telemetry.php
3.54 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
<?php
/**
* LearnDash Settings Section for Telemetry.
*
* @since 4.5.0
* @package LearnDash\Settings\Sections
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use StellarWP\Learndash\StellarWP\Telemetry\Core as Telemetry;
use StellarWP\Learndash\StellarWP\Telemetry\Opt_In\Status;
if ( class_exists( 'LearnDash_Settings_Section' ) && ! class_exists( 'LearnDash_Settings_Section_Telemetry' ) ) {
/**
* Class LearnDash Settings Section for Telemetry.
*
* @since 4.5.0
*/
class LearnDash_Settings_Section_Telemetry extends LearnDash_Settings_Section {
/**
* Constructor.
*
* @since 4.5.0
*/
protected function __construct() {
$this->settings_page_id = 'learndash_lms_advanced';
// This is the 'option_name' key used in the wp_options table.
$this->setting_option_key = 'learndash_telemetry';
// This is the HTML form field prefix used.
$this->setting_field_prefix = 'learndash_telemetry';
// Used within the Settings API to uniquely identify this section.
$this->settings_section_key = 'settings_telemetry';
// Section label/header.
$this->settings_section_label = esc_html__( 'Data Sharing', 'learndash' );
parent::__construct();
/**
* Updates the opt-in status.
*
* @since 4.5.0
*/
add_action(
'admin_init',
function() {
if (
! isset( $_POST['telemetry_opt_in_status'] )
|| ! isset( $_POST['learndash_telemetry_nonce'] )
|| ! wp_verify_nonce(
sanitize_text_field( wp_unslash( $_POST['learndash_telemetry_nonce'] ) ),
'learndash_telemetry'
)
) {
return;
}
$container = Telemetry::instance()->container();
$enabled = Status::STATUS_ACTIVE === (int) sanitize_key( (string) wp_unslash( $_POST['telemetry_opt_in_status'] ) );
/**
* Status handler.
*
* @var Status $status_handler Status handler.
*/
$status_handler = $container->get( Status::class );
$status_handler->set_status( $enabled );
}
);
}
/**
* Shows settings content.
*
* @since 4.5.0
*
* @return void
*/
public function show_meta_box(): void {
$container = Telemetry::instance()->container();
/**
* Status handler.
*
* @var Status $status_handler Status handler.
*/
$status_handler = $container->get( Status::class );
$opt_in_status_value = $status_handler->get();
?>
<div class="sfwd_options">
<?php wp_nonce_field( 'learndash_telemetry', 'learndash_telemetry_nonce' ); ?>
<div class="sfwd_input">
<label for="telemetry_opt_in_status_active" style="display: inline-block; margin-bottom: 5px;">
<input type="radio" value="<?php echo esc_attr( (string) Status::STATUS_ACTIVE ); ?>" name="telemetry_opt_in_status" id="telemetry_opt_in_status_active" <?php checked( Status::STATUS_ACTIVE, $opt_in_status_value ); ?>/>
<?php esc_html_e( 'Yes, share plugin usage data with LearnDash, part of the StellarWP family of brands', 'learndash' ); ?>
</label><br/>
<label for="telemetry_opt_in_status_inactive">
<input type="radio" value="<?php echo esc_attr( (string) Status::STATUS_INACTIVE ); ?>" name="telemetry_opt_in_status" id="telemetry_opt_in_status_inactive" <?php checked( Status::STATUS_INACTIVE, $opt_in_status_value ); ?>/>
<?php esc_html_e( 'No, do not share plugin usage data with LearnDash, part of the StellarWP family of brands', 'learndash' ); ?>
</label>
</div>
</div>
<?php
}
}
}
add_action(
'learndash_settings_sections_init',
array( LearnDash_Settings_Section_Telemetry::class, 'add_section_instance' ),
20
);