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

Kirki_Telemetry::dismiss_notice()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
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' );
69
		if ( ! $sent ) {
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
		$url = add_query_arg( $data, 'https://wpmu.test/?action=kirki-stats' );
94
95
		// Ping remote server.
96
		wp_remote_get( $url );
97
	}
98
99
	/**
100
	 * The admin-notice.
101
	 *
102
	 * @access private
103
	 * @since 3.0.34
104
	 * @return void
105
	 */
106
	public function admin_notice() {
107
108
		// Early exit if the user has dismissed the consent, or if they have opted-in.
109
		if ( get_option( 'kirki_telemetry_no_consent' ) || get_option( 'kirki_telemetry_optin' ) ) {
110
			return;
111
		}
112
		$data = $this->get_data();
113
		?>
114
		<div class="notice notice-info kirki-telemetry">
115
			<p><strong><?php esc_html_e( 'Help Kirki improve with usage tracking.', 'kirki' ); ?></strong></p>
116
			<p><?php esc_html_e( 'Gathering usage data about the theme you are using allows us to know which themes are most-used and work closer with developers of your theme to improve both the theme you use and the Kirki framework. We will never collect any personal information about you or your site.', 'kirki' ); ?></p>
117
			<table class="data-to-send hidden widefat">
118
				<thead>
119
					<tr>
120
						<th colspan="2"><?php esc_html_e( 'Data that will be sent', 'kirki' ); ?></th>
121
					</tr>
122
				</thead>
123
				<tbody>
124
					<tr>
125
						<td>PHP Version</td>
126
						<td><code><?php echo esc_attr( $data['phpVer'] ); ?></code></td>
127
					</tr>
128
					<tr>
129
						<td>ID</td>
130
						<td><code><?php echo esc_attr( $data['siteID'] ); ?></code></td>
131
					</tr>
132
					<tr>
133
						<td>Theme Name</td>
134
						<td><code><?php echo esc_attr( $data['themeName'] ); ?></code></td>
135
					</tr>
136
					<tr>
137
						<td>Theme Author</td>
138
						<td><code><?php echo esc_attr( $data['themeAuthor'] ); ?></code></td>
139
					</tr>
140
					<tr>
141
						<td>Theme URI</td>
142
						<td><code><?php echo esc_attr( $data['themeURI'] ); ?></code></td>
143
					</tr>
144
					<tr>
145
						<td>Theme Version</td>
146
						<td><code><?php echo esc_attr( $data['themeVersion'] ); ?></code></td>
147
					</tr>
148
				</tbody>
149
			</table>
150
			<p class="actions">
151
152
				<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>
153
				<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>
154
				<a class="button button-link alignright details"><?php esc_html_e( 'Show me the data you send', 'kirki' ); ?></a>
155
			</p>
156
			<script>
157
			jQuery( '.kirki-telemetry a.consent' ).on( 'click', function() {
158
159
			});
160
			jQuery( '.kirki-telemetry a.dismiss' ).on( 'click', function() {
161
162
			});
163
			jQuery( '.kirki-telemetry a.details' ).on( 'click', function() {
164
				jQuery( '.kirki-telemetry .data-to-send' ).removeClass( 'hidden' );
165
				jQuery( '.kirki-telemetry a.details' ).hide();
166
			});
167
			</script>
168
		</div>
169
		<?php
170
	}
171
172
	/**
173
	 * Builds and returns the data or uses cached if data already exists.
174
	 *
175
	 * @access private
176
	 * @since 3.0.34
177
	 * @return array
178
	 */
179
	private function get_data() {
180
		// Get the theme.
181
		$theme = wp_get_theme();
182
183
		// Build data and return the array.
184
		return array(
185
			'phpVer'       => phpversion( 'tidy' ),
186
			'siteID'       => md5( home_url() ),
187
			'themeName'    => $theme->get( 'Name' ),
188
			'themeAuthor'  => $theme->get( 'Author' ),
189
			'themeURI'     => $theme->get( 'ThemeURI' ),
190
			'themeVersion' => $theme->get( 'Version' ),
191
		);
192
	}
193
194
	/**
195
	 * Dismisses the notice.
196
	 *
197
	 * @access private
198
	 * @since 3.0.34
199
	 * @return void
200
	 */
201
	private function dismiss_notice() {
202
203
		// Check if this is the request we want.
204
		if ( isset( $_GET['_wpnonce'] ) && isset( $_GET['kirki-hide-notice'] ) ) {
205
			if ( 'telemetry' === sanitize_text_field( wp_unslash( $_GET['kirki-hide-notice'] ) ) ) {
206
				// Check the wp-nonce.
207
				if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) ) ) {
208
					// All good, we can save the option to dismiss this notice.
209
					update_option( 'kirki_telemetry_no_consent', true );
210
				}
211
			}
212
		}
213
	}
214
215
	/**
216
	 * Dismisses the notice.
217
	 *
218
	 * @access private
219
	 * @since 3.0.34
220
	 * @return void
221
	 */
222
	private function consent() {
223
224
		// Check if this is the request we want.
225
		if ( isset( $_GET['_wpnonce'] ) && isset( $_GET['kirki-consent-notice'] ) ) {
226
			if ( 'telemetry' === sanitize_text_field( wp_unslash( $_GET['kirki-consent-notice'] ) ) ) {
227
				// Check the wp-nonce.
228
				if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) ) ) {
229
					// All good, we can save the option to dismiss this notice.
230
					update_option( 'kirki_telemetry_optin', true );
231
				}
232
			}
233
		}
234
	}
235
}
236