Completed
Pull Request — develop (#1933)
by Aristeides
03:38
created

Kirki_Telemetry   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 248
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 79
c 7
b 0
f 0
dl 0
loc 248
rs 10
wmc 25

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A init() 0 4 1
A maybe_send_data() 0 13 3
A send_data() 0 16 1
A get_data() 0 18 2
A dismiss_notice() 0 9 5
A admin_notice() 0 62 3
A consent() 0 9 5
A get_field_types() 0 8 3
1
<?php
2
/**
3
 * Telemetry implementation for Kirki.
4
 *
5
 * @package     Kirki
6
 * @category    Core
7
 * @author      Aristeides Stathopoulos
8
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
9
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
10
 * @since       3.0.34
11
 */
12
13
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
/**
19
 * Telemetry implementation.
20
 */
21
final class Kirki_Telemetry {
22
23
	/**
24
	 * Constructor.
25
	 *
26
	 * @access public
27
	 * @since 3.0.34
28
	 */
29
	public function __construct() {
30
31
		// Early exit if telemetry is disabled.
32
		if ( ! apply_filters( 'kirki_telemetry', true ) ) {
33
			return;
34
		}
35
36
		add_action( 'init', array( $this, 'init' ) );
37
		add_action( 'admin_notices', array( $this, 'admin_notice' ) );
38
	}
39
40
	/**
41
	 * Additional actions that run on init.
42
	 *
43
	 * @access public
44
	 * @since 3.0.34
45
	 * @return void
46
	 */
47
	public function init() {
48
		$this->dismiss_notice();
49
		$this->consent();
50
		$this->maybe_send_data();
51
	}
52
53
	/**
54
	 * Maybe send data.
55
	 *
56
	 * @access public
57
	 * @since 3.0.34
58
	 * @return void
59
	 */
60
	public function maybe_send_data() {
61
62
		// Check if the user has consented to the data sending.
63
		if ( ! get_option( 'kirki_telemetry_optin' ) ) {
64
			return;
65
		}
66
67
		// Only send data once/month.
68
		// TODO: Enable this before merging
69
		// $sent = get_site_transient( 'kirki_telemetry_sent' );
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
70
		if ( ! $sent ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sent seems to be never defined.
Loading history...
71
			$this->send_data();
72
			set_site_transient( 'kirki_telemetry_sent', time(), MONTH_IN_SECONDS );
0 ignored issues
show
Bug introduced by
The constant MONTH_IN_SECONDS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
73
		}
74
	}
75
76
	/**
77
	 * Sends data.
78
	 *
79
	 * @access private
80
	 * @since 3.0.34
81
	 * @return void
82
	 */
83
	private function send_data() {
84
85
		// The data.
86
		$data = array_merge(
87
			array(
88
				'action' => 'kirki-stats',
89
			),
90
			$this->get_data()
91
		);
92
93
		// Build the URL with the arguments.
94
		// TODO: ADD URL.
95
		$url = add_query_arg( $data, 'https://localhost/?action=kirki-stats' );
96
97
		// Ping remote server.
98
		wp_remote_get( $url );
99
	}
100
101
	/**
102
	 * The admin-notice.
103
	 *
104
	 * @access private
105
	 * @since 3.0.34
106
	 * @return void
107
	 */
108
	public function admin_notice() {
109
110
		// Early exit if the user has dismissed the consent, or if they have opted-in.
111
		if ( get_option( 'kirki_telemetry_no_consent' ) || get_option( 'kirki_telemetry_optin' ) ) {
112
			return;
113
		}
114
		$data = $this->get_data();
115
		?>
116
		<div class="notice notice-info kirki-telemetry">
117
			<h3><strong><?php esc_html_e( 'Help us improve Kirki.', 'kirki' ); ?></strong></h3>
118
			<p><?php esc_html_e( 'Gathering usage data about the theme you are using allows us to know which themes and field-types are most-used with the Kirki framework.', 'kirki' ); ?><br><?php esc_html_e( 'This will allow us to work closer with theme developers to improve both the theme you use and the Kirki framework.', 'kirki' ); ?></p>
119
			<p><strong><?php esc_html_e( 'The data is completely anonymous and we will never collect any identifyable information about you or your website.'); ?></strong></p>
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
120
			<table class="data-to-send hidden widefat">
121
				<thead>
122
					<tr>
123
						<th colspan="2"><?php esc_html_e( 'Data that will be sent', 'kirki' ); ?></th>
124
					</tr>
125
				</thead>
126
				<tbody>
127
					<tr>
128
						<td style="min-width: 200px;"><?php esc_html_e( 'PHP Version', 'kirki' ); ?></td>
129
						<td><code><?php echo esc_html( $data['phpVer'] ); ?></code></td>
130
					</tr>
131
					<tr>
132
						<td><?php esc_html_e( 'Theme Name', 'kirki' ); ?></td>
133
						<td><code><?php echo esc_html( $data['themeName'] ); ?></code></td>
134
					</tr>
135
					<tr>
136
						<td><?php esc_html_e( 'Theme Author', 'kirki' ); ?></td>
137
						<td><code><?php echo esc_html( $data['themeAuthor'] ); ?></code></td>
138
					</tr>
139
					<tr>
140
						<td><?php esc_html_e( 'Theme URI', 'kirki' ); ?></td>
141
						<td><code><?php echo esc_html( $data['themeURI'] ); ?></code></td>
142
					</tr>
143
					<tr>
144
						<td><?php esc_html_e( 'Field Types Used', 'kirki' ); ?></td>
145
						<td><code><?php echo esc_html( $data['fieldTypes'] ); ?></code></td>
146
					</tr>
147
				</tbody>
148
				<tfoot>
149
					<tr>
150
						<th colspan="2">
151
							<?php
152
							printf(
153
								/* translators: %1$s: URL to the server plugin code. %2$s: URL to the stats page. */
154
								__( 'We believe in complete transparency. You can see the code used on our server <a href="%1$s" rel="nofollow">here</a>, and the results of the statistics we\'re gathering on <a href="%2$s" rel="nofollow">this page</a>.', 'kirki' ),
0 ignored issues
show
introduced by
All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found '__'.
Loading history...
155
								// TODO: ADD URL.
156
								'',
157
								// TODO: ADD URL.
158
								''
159
							);
160
							?>
161
						</th>
162
					</tr>
163
				</tfoot>
164
			</table>
165
			<p class="actions">
166
167
				<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'kirki-consent-notice', 'telemetry' ) ) ); ?>" class="button button-primary consent"><?php esc_html_e( 'I agree', 'kirki' ); ?></a>
168
				<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'kirki-hide-notice', 'telemetry' ) ) ); ?>" class="button button-secondary dismiss"><?php esc_html_e( 'No thanks', 'kirki' ); ?></a>
169
				<a class="button button-link alignright details"><?php esc_html_e( 'Show me the data you send', 'kirki' ); ?></a>
170
			</p>
171
			<script>
172
			jQuery( '.kirki-telemetry a.consent' ).on( 'click', function() {
173
174
			});
175
			jQuery( '.kirki-telemetry a.dismiss' ).on( 'click', function() {
176
177
			});
178
			jQuery( '.kirki-telemetry a.details' ).on( 'click', function() {
179
				jQuery( '.kirki-telemetry .data-to-send' ).removeClass( 'hidden' );
180
				jQuery( '.kirki-telemetry a.details' ).hide();
181
			});
182
			</script>
183
		</div>
184
		<?php
185
	}
186
187
	/**
188
	 * Builds and returns the data or uses cached if data already exists.
189
	 *
190
	 * @access private
191
	 * @since 3.0.34
192
	 * @return array
193
	 */
194
	private function get_data() {
195
		// Get the theme.
196
		$theme = wp_get_theme();
197
198
		// Format the PHP version.
199
		$php_version = phpversion( 'tidy' );
200
		if ( ! $php_version ) {
201
			$php_version = array_merge( explode( '.', phpversion() ), array( 0, 0 ) );
202
			$php_version = "{$php_version[0]}.{$php_version[1]}";
203
		}
204
205
		// Build data and return the array.
206
		return array(
207
			'phpVer'       => $php_version,
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 6 space(s) between "'phpVer'" and double arrow, but found 7.
Loading history...
208
			'themeName'    => $theme->get( 'Name' ),
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 3 space(s) between "'themeName'" and double arrow, but found 4.
Loading history...
209
			'themeAuthor'  => $theme->get( 'Author' ),
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 1 space(s) between "'themeAuthor'" and double arrow, but found 2.
Loading history...
210
			'themeURI'     => $theme->get( 'ThemeURI' ),
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 4 space(s) between "'themeURI'" and double arrow, but found 5.
Loading history...
211
			'fieldTypes'   => $this->get_field_types(),
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 2 space(s) between "'fieldTypes'" and double arrow, but found 3.
Loading history...
212
		);
213
	}
214
215
	/**
216
	 * Get the field-types used.
217
	 *
218
	 * @access private
219
	 * @since 3.0.36
220
	 * @return array
221
	 */
222
	public function get_field_types() {
223
		$types = array();
224
		foreach ( Kirki::$fields as $field ) {
225
			if ( isset( $field['type'] ) ) {
226
				$types[] = $field['type'];
227
			}
228
		}
229
		return implode( '","',  $types );
0 ignored issues
show
Coding Style introduced by
Expected 1 space instead of 2 after comma in function call.
Loading history...
230
	}
231
232
	/**
233
	 * Dismisses the notice.
234
	 *
235
	 * @access private
236
	 * @since 3.0.34
237
	 * @return void
238
	 */
239
	private function dismiss_notice() {
240
241
		// Check if this is the request we want.
242
		if ( isset( $_GET['_wpnonce'] ) && isset( $_GET['kirki-hide-notice'] ) ) {
243
			if ( 'telemetry' === sanitize_text_field( wp_unslash( $_GET['kirki-hide-notice'] ) ) ) {
0 ignored issues
show
introduced by
Processing form data without nonce verification.
Loading history...
244
				// Check the wp-nonce.
245
				if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) ) ) {
246
					// All good, we can save the option to dismiss this notice.
247
					update_option( 'kirki_telemetry_no_consent', true );
248
				}
249
			}
250
		}
251
	}
252
253
	/**
254
	 * Dismisses the notice.
255
	 *
256
	 * @access private
257
	 * @since 3.0.34
258
	 * @return void
259
	 */
260
	private function consent() {
261
262
		// Check if this is the request we want.
263
		if ( isset( $_GET['_wpnonce'] ) && isset( $_GET['kirki-consent-notice'] ) ) {
264
			if ( 'telemetry' === sanitize_text_field( wp_unslash( $_GET['kirki-consent-notice'] ) ) ) {
0 ignored issues
show
introduced by
Processing form data without nonce verification.
Loading history...
265
				// Check the wp-nonce.
266
				if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) ) ) {
267
					// All good, we can save the option to dismiss this notice.
268
					update_option( 'kirki_telemetry_optin', true );
269
				}
270
			}
271
		}
272
	}
273
}
274