Completed
Pull Request — develop (#1933)
by Aristeides
04:57
created

Kirki_Telemetry::dismiss_notice()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 4
nc 4
nop 0
dl 0
loc 9
rs 9.6111
c 1
b 0
f 0
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
		// $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...
69
		if ( ! $sent ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sent seems to be never defined.
Loading history...
70
			$this->send_data();
71
			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...
72
		}
73
	}
74
75
	/**
76
	 * Sends data.
77
	 *
78
	 * @access private
79
	 * @since 3.0.34
80
	 * @return void
81
	 */
82
	private function send_data() {
83
84
		// The data.
85
		$data = array_merge(
86
			array(
87
				'action' => 'kirki-stats',
88
			),
89
			$this->get_data()
90
		);
91
92
		// Build the URL with the arguments.
93
		// TODO: ADD URL.
94
		$url = add_query_arg( $data, 'https://localhost/?action=kirki-stats' );
95
96
		// Ping remote server.
97
		wp_remote_get( $url );
98
	}
99
100
	/**
101
	 * The admin-notice.
102
	 *
103
	 * @access private
104
	 * @since 3.0.34
105
	 * @return void
106
	 */
107
	public function admin_notice() {
108
109
		// Early exit if the user has dismissed the consent, or if they have opted-in.
110
		if ( get_option( 'kirki_telemetry_no_consent' ) || get_option( 'kirki_telemetry_optin' ) ) {
111
			return;
112
		}
113
		$data = $this->get_data();
114
		?>
115
		<div class="notice notice-info kirki-telemetry">
116
			<h3><strong><?php esc_html_e( 'Help us improve Kirki.', 'kirki' ); ?></strong></h3>
117
			<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>
118
			<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...
119
			<table class="data-to-send hidden widefat">
120
				<thead>
121
					<tr>
122
						<th colspan="2"><?php esc_html_e( 'Data that will be sent', 'kirki' ); ?></th>
123
					</tr>
124
				</thead>
125
				<tbody>
126
					<tr>
127
						<td style="min-width: 200px;"><?php esc_html_e( 'PHP Version', 'kirki' ); ?></td>
128
						<td><code><?php echo esc_html( $data['phpVer'] ); ?></code></td>
129
					</tr>
130
					<tr>
131
						<td><?php esc_html_e( 'ID', 'kirki' ); ?></td>
132
						<td><code><?php echo esc_html( $data['siteID'] ); ?></code></td>
133
					</tr>
134
					<tr>
135
						<td><?php esc_html_e( 'Theme Name', 'kirki' ); ?></td>
136
						<td><code><?php echo esc_html( $data['themeName'] ); ?></code></td>
137
					</tr>
138
					<tr>
139
						<td><?php esc_html_e( 'Theme Author', 'kirki' ); ?></td>
140
						<td><code><?php echo esc_html( $data['themeAuthor'] ); ?></code></td>
141
					</tr>
142
					<tr>
143
						<td><?php esc_html_e( 'Theme URI', 'kirki' ); ?></td>
144
						<td><code><?php echo esc_html( $data['themeURI'] ); ?></code></td>
145
					</tr>
146
					<tr>
147
						<td><?php esc_html_e( 'Theme Version', 'kirki' ); ?></td>
148
						<td><code><?php echo esc_html( $data['themeVersion'] ); ?></code></td>
149
					</tr>
150
					<tr>
151
						<td><?php esc_html_e( 'Field Types Used', 'kirki' ); ?></td>
152
						<td><code><?php echo esc_html( $data['fieldTypes'] ); ?></code></td>
153
					</tr>
154
				</tbody>
155
				<tfoot>
156
					<tr>
157
						<th colspan="2">
158
							<?php
159
							printf(
160
								/* translators: %1$s: URL to the server plugin code. %2$s: URL to the stats page. */
161
								__( '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...
162
								// TODO: ADD URL.
163
								'',
164
								// TODO: ADD URL.
165
								''
166
							);
167
							?>
168
						</th>
169
					</tr>
170
				</tfoot>
171
			</table>
172
			<p class="actions">
173
174
				<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>
175
				<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>
176
				<a class="button button-link alignright details"><?php esc_html_e( 'Show me the data you send', 'kirki' ); ?></a>
177
			</p>
178
			<script>
179
			jQuery( '.kirki-telemetry a.consent' ).on( 'click', function() {
180
181
			});
182
			jQuery( '.kirki-telemetry a.dismiss' ).on( 'click', function() {
183
184
			});
185
			jQuery( '.kirki-telemetry a.details' ).on( 'click', function() {
186
				jQuery( '.kirki-telemetry .data-to-send' ).removeClass( 'hidden' );
187
				jQuery( '.kirki-telemetry a.details' ).hide();
188
			});
189
			</script>
190
		</div>
191
		<?php
192
	}
193
194
	/**
195
	 * Builds and returns the data or uses cached if data already exists.
196
	 *
197
	 * @access private
198
	 * @since 3.0.34
199
	 * @return array
200
	 */
201
	private function get_data() {
202
		// Get the theme.
203
		$theme = wp_get_theme();
204
205
		// Format the PHP version.
206
		$php_version = phpversion( 'tidy' );
207
		if ( ! $php_version ) {
208
			$php_version = array_merge( explode( '.', phpversion() ), array( 0, 0 ) );
209
			$php_version = "{$php_version[0]}.{$php_version[1]}";
210
		}
211
212
		// Build data and return the array.
213
		return array(
214
			'phpVer'       => $php_version,
215
			'siteID'       => md5( home_url() ),
216
			'themeName'    => $theme->get( 'Name' ),
217
			'themeAuthor'  => $theme->get( 'Author' ),
218
			'themeURI'     => $theme->get( 'ThemeURI' ),
219
			'themeVersion' => $theme->get( 'Version' ),
220
			'fieldTypes'   => $this->get_field_types(),
221
		);
222
	}
223
224
	/**
225
	 * Get the field-types used.
226
	 *
227
	 * @access private
228
	 * @since 3.0.36
229
	 * @return array
230
	 */
231
	public function get_field_types() {
232
		$types = array();
233
		foreach ( Kirki::$fields as $field ) {
234
			if ( isset( $field['type'] ) ) {
235
				$types[] = $field['type'];
236
			}
237
		}
238
		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...
239
	}
240
241
	/**
242
	 * Dismisses the notice.
243
	 *
244
	 * @access private
245
	 * @since 3.0.34
246
	 * @return void
247
	 */
248
	private function dismiss_notice() {
249
250
		// Check if this is the request we want.
251
		if ( isset( $_GET['_wpnonce'] ) && isset( $_GET['kirki-hide-notice'] ) ) {
252
			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...
253
				// Check the wp-nonce.
254
				if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) ) ) {
255
					// All good, we can save the option to dismiss this notice.
256
					update_option( 'kirki_telemetry_no_consent', true );
257
				}
258
			}
259
		}
260
	}
261
262
	/**
263
	 * Dismisses the notice.
264
	 *
265
	 * @access private
266
	 * @since 3.0.34
267
	 * @return void
268
	 */
269
	private function consent() {
270
271
		// Check if this is the request we want.
272
		if ( isset( $_GET['_wpnonce'] ) && isset( $_GET['kirki-consent-notice'] ) ) {
273
			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...
274
				// Check the wp-nonce.
275
				if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) ) ) {
276
					// All good, we can save the option to dismiss this notice.
277
					update_option( 'kirki_telemetry_optin', true );
278
				}
279
			}
280
		}
281
	}
282
}
283