Completed
Pull Request — develop (#1933)
by Aristeides
05:15
created

Kirki_Telemetry::maybe_send_data()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 3
b 0
f 0
nc 3
nop 0
dl 0
loc 13
rs 10
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( 'ID', 'kirki' ); ?></td>
133
						<td><code><?php echo esc_html( $data['siteID'] ); ?></code></td>
134
					</tr>
135
					<tr>
136
						<td><?php esc_html_e( 'Theme Name', 'kirki' ); ?></td>
137
						<td><code><?php echo esc_html( $data['themeName'] ); ?></code></td>
138
					</tr>
139
					<tr>
140
						<td><?php esc_html_e( 'Theme Author', 'kirki' ); ?></td>
141
						<td><code><?php echo esc_html( $data['themeAuthor'] ); ?></code></td>
142
					</tr>
143
					<tr>
144
						<td><?php esc_html_e( 'Theme URI', 'kirki' ); ?></td>
145
						<td><code><?php echo esc_html( $data['themeURI'] ); ?></code></td>
146
					</tr>
147
					<tr>
148
						<td><?php esc_html_e( 'Theme Version', 'kirki' ); ?></td>
149
						<td><code><?php echo esc_html( $data['themeVersion'] ); ?></code></td>
150
					</tr>
151
					<tr>
152
						<td><?php esc_html_e( 'Field Types Used', 'kirki' ); ?></td>
153
						<td><code><?php echo esc_html( $data['fieldTypes'] ); ?></code></td>
154
					</tr>
155
				</tbody>
156
				<tfoot>
157
					<tr>
158
						<th colspan="2">
159
							<?php
160
							printf(
161
								/* translators: %1$s: URL to the server plugin code. %2$s: URL to the stats page. */
162
								__( '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...
163
								// TODO: ADD URL.
164
								'',
165
								// TODO: ADD URL.
166
								''
167
							);
168
							?>
169
						</th>
170
					</tr>
171
				</tfoot>
172
			</table>
173
			<p class="actions">
174
175
				<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>
176
				<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>
177
				<a class="button button-link alignright details"><?php esc_html_e( 'Show me the data you send', 'kirki' ); ?></a>
178
			</p>
179
			<script>
180
			jQuery( '.kirki-telemetry a.consent' ).on( 'click', function() {
181
182
			});
183
			jQuery( '.kirki-telemetry a.dismiss' ).on( 'click', function() {
184
185
			});
186
			jQuery( '.kirki-telemetry a.details' ).on( 'click', function() {
187
				jQuery( '.kirki-telemetry .data-to-send' ).removeClass( 'hidden' );
188
				jQuery( '.kirki-telemetry a.details' ).hide();
189
			});
190
			</script>
191
		</div>
192
		<?php
193
	}
194
195
	/**
196
	 * Builds and returns the data or uses cached if data already exists.
197
	 *
198
	 * @access private
199
	 * @since 3.0.34
200
	 * @return array
201
	 */
202
	private function get_data() {
203
		// Get the theme.
204
		$theme = wp_get_theme();
205
206
		// Format the PHP version.
207
		$php_version = phpversion( 'tidy' );
208
		if ( ! $php_version ) {
209
			$php_version = array_merge( explode( '.', phpversion() ), array( 0, 0 ) );
210
			$php_version = "{$php_version[0]}.{$php_version[1]}";
211
		}
212
213
		// Build data and return the array.
214
		return array(
215
			'phpVer'       => $php_version,
216
			'siteID'       => md5( home_url() ),
217
			'themeName'    => $theme->get( 'Name' ),
218
			'themeAuthor'  => $theme->get( 'Author' ),
219
			'themeURI'     => $theme->get( 'ThemeURI' ),
220
			'themeVersion' => $theme->get( 'Version' ),
221
			'fieldTypes'   => $this->get_field_types(),
222
		);
223
	}
224
225
	/**
226
	 * Get the field-types used.
227
	 *
228
	 * @access private
229
	 * @since 3.0.36
230
	 * @return array
231
	 */
232
	public function get_field_types() {
233
		$types = array();
234
		foreach ( Kirki::$fields as $field ) {
235
			if ( isset( $field['type'] ) ) {
236
				$types[] = $field['type'];
237
			}
238
		}
239
		return '["' . implode( '","',  array_unique( $types ) ) . '"]';
0 ignored issues
show
Coding Style introduced by
Expected 1 space instead of 2 after comma in function call.
Loading history...
240
	}
241
242
	/**
243
	 * Dismisses the notice.
244
	 *
245
	 * @access private
246
	 * @since 3.0.34
247
	 * @return void
248
	 */
249
	private function dismiss_notice() {
250
251
		// Check if this is the request we want.
252
		if ( isset( $_GET['_wpnonce'] ) && isset( $_GET['kirki-hide-notice'] ) ) {
253
			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...
254
				// Check the wp-nonce.
255
				if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) ) ) {
256
					// All good, we can save the option to dismiss this notice.
257
					update_option( 'kirki_telemetry_no_consent', true );
258
				}
259
			}
260
		}
261
	}
262
263
	/**
264
	 * Dismisses the notice.
265
	 *
266
	 * @access private
267
	 * @since 3.0.34
268
	 * @return void
269
	 */
270
	private function consent() {
271
272
		// Check if this is the request we want.
273
		if ( isset( $_GET['_wpnonce'] ) && isset( $_GET['kirki-consent-notice'] ) ) {
274
			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...
275
				// Check the wp-nonce.
276
				if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) ) ) {
277
					// All good, we can save the option to dismiss this notice.
278
					update_option( 'kirki_telemetry_optin', true );
279
				}
280
			}
281
		}
282
	}
283
}
284