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

Kirki_Telemetry::admin_notice()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 60
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 3
eloc 36
c 5
b 0
f 0
nc 2
nop 0
dl 0
loc 60
rs 9.344

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
51
		// This is the last thing to run. No impact on performance or anything else.
52
		add_action( 'wp_footer', array( $this, 'maybe_send_data' ), 99999 );
53
	}
54
55
	/**
56
	 * Maybe send data.
57
	 *
58
	 * @access public
59
	 * @since 3.0.34
60
	 * @return void
61
	 */
62
	public function maybe_send_data() {
63
64
		// Check if the user has consented to the data sending.
65
		if ( ! get_option( 'kirki_telemetry_optin' ) ) {
66
			return;
67
		}
68
69
		// Only send data once/month.
70
		$sent = get_site_transient( 'kirki_telemetry_sent' );
71
		if ( ! $sent ) {
72
			$this->send_data();
73
			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...
74
		}
75
	}
76
77
	/**
78
	 * Sends data.
79
	 *
80
	 * @access private
81
	 * @since 3.0.34
82
	 * @return void
83
	 */
84
	private function send_data() {
85
86
		// Ping remote server.
87
		wp_remote_post( 'https://wplemon.com/?action=kirki-stats', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Coding Style introduced by
For multi-line function calls, each argument should be on a separate line.

For a function calls that spawns multiple lines, the coding style suggests to split arguments to separate lines like this:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
);
Loading history...
88
			'method'   => 'POST',
89
			'blocking' => false,
90
			'body'     => array_merge(
91
				array(
92
					'action' => 'kirki-stats',
93
				),
94
				$this->get_data()
95
			),
96
		] );
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
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
			<h3><strong><?php esc_html_e( 'Help us improve Kirki.', 'kirki' ); ?></strong></h3>
116
			<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>
117
			<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...
118
			<table class="data-to-send hidden widefat">
119
				<thead>
120
					<tr>
121
						<th colspan="2"><?php esc_html_e( 'Data that will be sent', 'kirki' ); ?></th>
122
					</tr>
123
				</thead>
124
				<tbody>
125
					<tr>
126
						<td style="min-width: 200px;"><?php esc_html_e( 'PHP Version', 'kirki' ); ?></td>
127
						<td><code><?php echo esc_html( $data['phpVer'] ); ?></code></td>
128
					</tr>
129
					<tr>
130
						<td><?php esc_html_e( 'Theme Name', 'kirki' ); ?></td>
131
						<td><code><?php echo esc_html( $data['themeName'] ); ?></code></td>
132
					</tr>
133
					<tr>
134
						<td><?php esc_html_e( 'Theme Author', 'kirki' ); ?></td>
135
						<td><code><?php echo esc_html( $data['themeAuthor'] ); ?></code></td>
136
					</tr>
137
					<tr>
138
						<td><?php esc_html_e( 'Theme URI', 'kirki' ); ?></td>
139
						<td><code><?php echo esc_html( $data['themeURI'] ); ?></code></td>
140
					</tr>
141
					<tr>
142
						<td><?php esc_html_e( 'Field Types Used', 'kirki' ); ?></td>
143
						<td><code><?php echo esc_html( implode( ',', $data['fieldTypes'] ) ); ?></code></td>
144
					</tr>
145
				</tbody>
146
				<tfoot>
147
					<tr>
148
						<th colspan="2">
149
							<?php
150
							printf(
151
								/* translators: %1$s: URL to the server plugin code. %2$s: URL to the stats page. */
152
								__( '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...
153
								'https://github.com/aristath/kirki-telemetry-server',
154
								'https://wplemon.com/?action=kirki-telemetry-stats'
155
							);
156
							?>
157
						</th>
158
					</tr>
159
				</tfoot>
160
			</table>
161
			<p class="actions">
162
163
				<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>
164
				<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>
165
				<a class="button button-link alignright details"><?php esc_html_e( 'Show me the data you send', 'kirki' ); ?></a>
166
			</p>
167
			<script>
168
			jQuery( '.kirki-telemetry a.consent' ).on( 'click', function() {
169
170
			});
171
			jQuery( '.kirki-telemetry a.dismiss' ).on( 'click', function() {
172
173
			});
174
			jQuery( '.kirki-telemetry a.details' ).on( 'click', function() {
175
				jQuery( '.kirki-telemetry .data-to-send' ).removeClass( 'hidden' );
176
				jQuery( '.kirki-telemetry a.details' ).hide();
177
			});
178
			</script>
179
		</div>
180
		<?php
181
	}
182
183
	/**
184
	 * Builds and returns the data or uses cached if data already exists.
185
	 *
186
	 * @access private
187
	 * @since 3.0.34
188
	 * @return array
189
	 */
190
	private function get_data() {
191
		// Get the theme.
192
		$theme = wp_get_theme();
193
194
		// Format the PHP version.
195
		$php_version = phpversion( 'tidy' );
196
		if ( ! $php_version ) {
197
			$php_version = array_merge( explode( '.', phpversion() ), array( 0, 0 ) );
198
			$php_version = "{$php_version[0]}.{$php_version[1]}";
199
		}
200
201
		// Build data and return the array.
202
		return array(
203
			'phpVer'      => $php_version,
204
			'themeName'   => $theme->get( 'Name' ),
205
			'themeAuthor' => $theme->get( 'Author' ),
206
			'themeURI'    => $theme->get( 'ThemeURI' ),
207
			'fieldTypes'  => $this->get_field_types(),
208
		);
209
	}
210
211
	/**
212
	 * Get the field-types used.
213
	 *
214
	 * @access private
215
	 * @since 3.0.36
216
	 * @return array
217
	 */
218
	public function get_field_types() {
219
		$types = array();
220
		foreach ( Kirki::$fields as $field ) {
221
			if ( isset( $field['type'] ) ) {
222
				$types[] = $field['type'];
223
			}
224
		}
225
		return $types;
226
	}
227
228
	/**
229
	 * Dismisses the notice.
230
	 *
231
	 * @access private
232
	 * @since 3.0.34
233
	 * @return void
234
	 */
235
	private function dismiss_notice() {
236
237
		// Check if this is the request we want.
238
		if ( isset( $_GET['_wpnonce'] ) && isset( $_GET['kirki-hide-notice'] ) ) {
239
			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...
240
				// Check the wp-nonce.
241
				if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) ) ) {
242
					// All good, we can save the option to dismiss this notice.
243
					update_option( 'kirki_telemetry_no_consent', true );
244
				}
245
			}
246
		}
247
	}
248
249
	/**
250
	 * Dismisses the notice.
251
	 *
252
	 * @access private
253
	 * @since 3.0.34
254
	 * @return void
255
	 */
256
	private function consent() {
257
258
		// Check if this is the request we want.
259
		if ( isset( $_GET['_wpnonce'] ) && isset( $_GET['kirki-consent-notice'] ) ) {
260
			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...
261
				// Check the wp-nonce.
262
				if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) ) ) {
263
					// All good, we can save the option to dismiss this notice.
264
					update_option( 'kirki_telemetry_optin', true );
265
				}
266
			}
267
		}
268
	}
269
}
270