Completed
Push — master ( b61d9c...72f01d )
by
unknown
17:41
created

get_item_schema()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 318
Code Lines 251

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 251
c 2
b 1
f 1
nc 1
nop 0
dl 0
loc 318
rs 8.2857

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
 * REST API WC System Status controller
4
 *
5
 * Handles requests to the /system_status endpoint.
6
 *
7
 * @author   WooThemes
8
 * @category API
9
 * @package  WooCommerce/API
10
 * @since    2.7.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * @package WooCommerce/API
19
 * @extends WC_REST_Controller
20
 */
21
class WC_REST_System_Status_Controller extends WC_REST_Controller {
22
23
	/**
24
	 * Endpoint namespace.
25
	 *
26
	 * @var string
27
	 */
28
	protected $namespace = 'wc/v1';
29
30
	/**
31
	 * Route base.
32
	 *
33
	 * @var string
34
	 */
35
	protected $rest_base = 'system_status';
36
37
	/**
38
	 * Register the routes for coupons.
39
	 */
40 View Code Duplication
	public function register_routes() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
        register_rest_route( $this->namespace, '/' . $this->rest_base, array(
42
			array(
43
				'methods'             => WP_REST_Server::READABLE,
44
				'callback'            => array( $this, 'get_items' ),
45
                'permission_callback' => array( $this, 'get_items_permissions_check' ),
46
				'args'                => $this->get_collection_params(),
47
			),
48
			'schema' => array( $this, 'get_public_item_schema' ),
49
		) );
50
	}
51
52
    /**
53
	 * Check whether a given request has permission to view system status.
54
	 *
55
	 * @param  WP_REST_Request $request Full details about the request.
56
	 * @return WP_Error|boolean
57
	 */
58
	public function get_items_permissions_check( $request ) {
59
        if ( ! wc_rest_check_manager_permissions( 'system_status', 'read' ) ) {
60
        	return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
61
		}
62
		return true;
63
	}
64
65
    /**
66
	 * Get a system status info, by section.
67
	 *
68
	 * @param WP_REST_Request $request Full details about the request.
69
	 * @return WP_Error|WP_REST_Response
70
	 */
71
	public function get_items( $request ) {
72
		$schema    = $this->get_item_schema();
73
		$mappings  = $this->get_item_mappings();
74
		$response  = array();
75
76
		foreach ( $mappings as $section => $values ) {
77
			settype( $values, $schema['properties'][ $section ]['type'] );
78
			foreach ( $values as $key => $value ) {
79
				if ( isset( $schema['properties'][ $section ]['properties'][ $key ]['type'] ) ) {
80
					settype( $values[ $key ], $schema['properties'][ $section ]['properties'][ $key ]['type'] );
81
				}
82
			}
83
			$response[ $section ] = $values;
84
		}
85
86
		return rest_ensure_response( $response );
87
	}
88
89
    /**
90
	 * Get the system status schema, conforming to JSON Schema.
91
	 *
92
	 * @return array
93
	 */
94
	public function get_item_schema() {
95
		$schema = array(
96
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
97
			'title'      => 'system_status',
98
			'type'       => 'object',
99
			'properties' => array(
100
				'environment' => array(
101
					'description' => __( 'Environment', 'woocommerce' ),
102
					'type'        => 'array',
103
					'context'     => array( 'view', 'edit' ),
104
					'properties'  => array(
105
						'home_url' => array(
106
							'description' => __( 'Home URL', 'woocommerce' ),
107
							'type'        => 'string',
108
		                    'format'      => 'uri',
109
							'context'     => array( 'view', 'edit' ),
110
						),
111
		                'site_url' => array(
112
		                    'description' => __( 'Site URL', 'woocommerce' ),
113
		                    'type'        => 'string',
114
		                    'format'      => 'uri',
115
		                    'context'     => array( 'view', 'edit' ),
116
		                ),
117
		                'wc_version' => array(
118
		                    'description' => __( 'WooCommerce Version', 'woocommerce' ),
119
		                    'type'        => 'string',
120
		                    'context'     => array( 'view', 'edit' ),
121
		                ),
122
		                'log_directory' => array(
123
		                    'description' => __( 'Log Directory', 'woocommerce' ),
124
		                    'type'        => 'string',
125
		                    'context'     => array( 'view', 'edit' ),
126
		                ),
127
		                'log_directory_writable' => array(
128
		                    'description' => __( 'Is Log Directory Writable?', 'woocommerce' ),
129
		                    'type'        => 'boolean',
130
		                    'context'     => array( 'view', 'edit' ),
131
		                ),
132
		                'wp_version' => array(
133
		                    'description' => __( 'WordPress Version', 'woocommerce' ),
134
		                    'type'        => 'string',
135
		                    'context'     => array( 'view', 'edit' ),
136
		                ),
137
		                'wp_multisite' => array(
138
		                    'description' => __( 'Is WordPress Multisite?', 'woocommerce' ),
139
		                    'type'        => 'boolean',
140
		                    'context'     => array( 'view', 'edit' ),
141
		                ),
142
		                'wp_memory_limit' => array(
143
		                    'description' => __( 'WordPress Memory Limit', 'woocommerce' ),
144
		                    'type'        => 'integer',
145
		                    'context'     => array( 'view', 'edit' ),
146
		                ),
147
		                'wp_debug_mode' => array(
148
		                    'description' => __( 'Is WordPress Debug Mode Active?', 'woocommerce' ),
149
		                    'type'        => 'boolean',
150
		                    'context'     => array( 'view', 'edit' ),
151
		                ),
152
		                'wp_cron' => array(
153
		                    'description' => __( 'Are WordPress Cron Jobs Enabled?', 'woocommerce' ),
154
		                    'type'        => 'boolean',
155
		                    'context'     => array( 'view', 'edit' ),
156
		                ),
157
		                'language' => array(
158
		                    'description' => __( 'WordPress Language', 'woocommerce' ),
159
		                    'type'        => 'string',
160
		                    'context'     => array( 'view', 'edit' ),
161
		                ),
162
		                'server_info' => array(
163
		                    'description' => __( 'Server Info', 'woocommerce' ),
164
		                    'type'        => 'string',
165
		                    'context'     => array( 'view', 'edit' ),
166
		                ),
167
		                'php_version' => array(
168
		                    'description' => __( 'PHP Version', 'woocommerce' ),
169
		                    'type'        => 'string',
170
		                    'context'     => array( 'view', 'edit' ),
171
		                ),
172
		                'php_post_max_size' => array(
173
		                    'description' => __( 'PHP Post Max Size', 'woocommerce' ),
174
		                    'type'        => 'integer',
175
		                    'context'     => array( 'view', 'edit' ),
176
		                ),
177
		                'php_max_execution_time' => array(
178
		                    'description' => __( 'PHP Max Execution Time', 'woocommerce' ),
179
		                    'type'        => 'integer',
180
		                    'context'     => array( 'view', 'edit' ),
181
		                ),
182
		                'php_max_input_vars' => array(
183
		                    'description' => __( 'PHP Max Input Vars', 'woocommerce' ),
184
		                    'type'        => 'integer',
185
		                    'context'     => array( 'view', 'edit' ),
186
		                ),
187
		                'curl_version' => array(
188
		                    'description' => __( 'cURL Version', 'woocommerce' ),
189
		                    'type'        => 'string',
190
		                    'context'     => array( 'view', 'edit' ),
191
		                ),
192
						'suhosin_installed' => array(
193
							'description' => __( 'Is SUHOSIN Installed?', 'woocommerce' ),
194
							'type'        => 'boolean',
195
							'context'     => array( 'view', 'edit' ),
196
						),
197
						'max_upload_size' => array(
198
							'description' => __( 'Max Upload Size', 'woocommerce' ),
199
							'type'        => 'integer',
200
							'context'     => array( 'view', 'edit' ),
201
						),
202
						'mysql_version' => array(
203
							'description' => __( 'MySQL Version', 'woocommerce' ),
204
							'type'        => 'string',
205
							'context'     => array( 'view', 'edit' ),
206
						),
207
						'default_timezone' => array(
208
							'description' => __( 'Default Timezone', 'woocommerce' ),
209
							'type'        => 'string',
210
							'context'     => array( 'view', 'edit' ),
211
						),
212
						'fsockopen_or_curl_enabled' => array(
213
							'description' => __( 'Is fsockopen/cURL Enabled?', 'woocommerce' ),
214
							'type'        => 'boolean',
215
							'context'     => array( 'view', 'edit' ),
216
						),
217
						'soapclient_enabled' => array(
218
							'description' => __( 'Is SoapClient Class Enabled?', 'woocommerce' ),
219
							'type'        => 'boolean',
220
							'context'     => array( 'view', 'edit' ),
221
						),
222
						'domdocument_enabled' => array(
223
							'description' => __( 'Is DomDocument Class Enabled?', 'woocommerce' ),
224
							'type'        => 'boolean',
225
							'context'     => array( 'view', 'edit' ),
226
						),
227
						'gzip_enabled' => array(
228
							'description' => __( 'Is GZip Enabled?', 'woocommerce' ),
229
							'type'        => 'boolean',
230
							'context'     => array( 'view', 'edit' ),
231
						),
232
						'mbstring_enabled' => array(
233
							'description' => __( 'Is mbstring Enabled?', 'woocommerce' ),
234
							'type'        => 'boolean',
235
							'context'     => array( 'view', 'edit' ),
236
						),
237
						'remote_post_successful' => array(
238
							'description' => __( 'Remote POST Successful?', 'woocommerce' ),
239
							'type'        => 'boolean',
240
							'context'     => array( 'view', 'edit' ),
241
						),
242
						'remote_post_response' => array(
243
							'description' => __( 'Remote POST Response', 'woocommerce' ),
244
							'type'        => 'string',
245
							'context'     => array( 'view', 'edit' ),
246
						),
247
						'remote_get_successful' => array(
248
							'description' => __( 'Remote GET Successful?', 'woocommerce' ),
249
							'type'        => 'boolean',
250
							'context'     => array( 'view', 'edit' ),
251
						),
252
						'remote_get_response' => array(
253
							'description' => __( 'Remote GET Response', 'woocommerce' ),
254
							'type'        => 'string',
255
							'context'     => array( 'view', 'edit' ),
256
						),
257
					),
258
				),
259
				'database' => array(
260
					'description' => __( 'Database', 'woocommerce' ),
261
					'type'        => 'array',
262
					'context'     => array( 'view', 'edit' ),
263
					'properties'  => array(
264
						'wc_database_version' => array(
265
							'description' => __( 'WC Database Version', 'woocommerce' ),
266
							'type'        => 'string',
267
							'context'     => array( 'view', 'edit' ),
268
						),
269
						'database_prefix' => array(
270
							'description' => __( 'Database Prefix', 'woocommerce' ),
271
							'type'        => 'string',
272
							'context'     => array( 'view', 'edit' ),
273
						),
274
						'maxmind_geoip_database' => array(
275
							'description' => __( 'MaxMind GeoIP Database', 'woocommerce' ),
276
							'type'        => 'string',
277
							'context'     => array( 'view', 'edit' ),
278
						),
279
						'database_tables' => array(
280
							'description' => __( 'Database Tables', 'woocommerce' ),
281
							'type'        => 'array',
282
							'context'     => array( 'view', 'edit' ),
283
						),
284
					)
285
				),
286
				'active_plugins' => array(
287
					'description' => __( 'Active Plugins', 'woocommerce' ),
288
					'type'        => 'array',
289
					'context'     => array( 'view', 'edit' ),
290
				),
291
				'theme' => array(
292
					'description' => __( 'Theme', 'woocommerce' ),
293
					'type'        => 'array',
294
					'context'     => array( 'view', 'edit' ),
295
					'properties'  => array(
296
						'name' => array(
297
							'description' => __( 'Theme Name', 'woocommerce' ),
298
							'type'        => 'string',
299
							'context'     => array( 'view', 'edit' ),
300
						),
301
						'version' => array(
302
							'description' => __( 'Theme Version', 'woocommerce' ),
303
							'type'        => 'string',
304
							'context'     => array( 'view', 'edit' ),
305
						),
306
						'author_url' => array(
307
							'description' => __( 'Theme Author URL', 'woocommerce' ),
308
							'type'        => 'string',
309
							'format'      => 'uri',
310
							'context'     => array( 'view', 'edit' ),
311
						),
312
						'is_child_theme' => array(
313
							'description' => __( 'Is this theme a child theme?', 'woocommerce' ),
314
							'type'        => 'boolean',
315
							'context'     => array( 'view', 'edit' ),
316
						),
317
						'has_woocommerce_support' => array(
318
							'description' => __( 'Does the theme declare WooCommerce support?', 'woocommerce' ),
319
							'type'        => 'boolean',
320
							'context'     => array( 'view', 'edit' ),
321
						),
322
						'overrides' => array(
323
							'description' => __( 'Template Overrides', 'woocommerce' ),
324
							'type'        => 'array',
325
							'context'     => array( 'view', 'edit' ),
326
						),
327
						'parent_name' => array(
328
							'description' => __( 'Parent Theme Name', 'woocommerce' ),
329
							'type'        => 'string',
330
							'context'     => array( 'view', 'edit' ),
331
						),
332
						'parent_version' => array(
333
							'description' => __( 'Parent Theme Version', 'woocommerce' ),
334
							'type'        => 'string',
335
							'context'     => array( 'view', 'edit' ),
336
						),
337
						'parent_author_url' => array(
338
							'description' => __( 'Parent Theme Author URL', 'woocommerce' ),
339
							'type'        => 'string',
340
							'format'      => 'uri',
341
							'context'     => array( 'view', 'edit' ),
342
						),
343
					)
344
				),
345
				'settings' => array(
346
					'description' => __( 'Settings', 'woocommerce' ),
347
					'type'        => 'array',
348
					'context'     => array( 'view', 'edit' ),
349
					'properties'  => array(
350
						'api_enabled' => array(
351
							'description' => __( 'REST API Enabled?', 'woocommerce' ),
352
							'type'        => 'boolean',
353
							'context'     => array( 'view', 'edit' ),
354
						),
355
						'force_ssl' => array(
356
							'description' => __( 'SSL Forced?', 'woocommerce' ),
357
							'type'        => 'boolean',
358
							'context'     => array( 'view', 'edit' ),
359
						),
360
						'currency' => array(
361
							'description' => __( 'Currency', 'woocommerce' ),
362
							'type'        => 'string',
363
							'context'     => array( 'view', 'edit' ),
364
						),
365
						'currency_symbol' => array(
366
							'description' => __( 'Currency Symbol', 'woocommerce' ),
367
							'type'        => 'string',
368
							'context'     => array( 'view', 'edit' ),
369
						),
370
						'currency_position' => array(
371
							'description' => __( 'Currency Position', 'woocommerce' ),
372
							'type'        => 'string',
373
							'context'     => array( 'view', 'edit' ),
374
						),
375
						'thousand_separator' => array(
376
							'description' => __( 'Thousand Separator', 'woocommerce' ),
377
							'type'        => 'string',
378
							'context'     => array( 'view', 'edit' ),
379
						),
380
						'decimal_separator' => array(
381
							'description' => __( 'Decimal Separator', 'woocommerce' ),
382
							'type'        => 'string',
383
							'context'     => array( 'view', 'edit' ),
384
						),
385
						'number_of_decimals' => array(
386
							'description' => __( 'Number of Decimals', 'woocommerce' ),
387
							'type'        => 'integer',
388
							'context'     => array( 'view', 'edit' ),
389
						),
390
						'geolocation_enabled' => array(
391
							'description' => __( 'Geolocation Enabled?', 'woocommerce' ),
392
							'type'        => 'boolean',
393
							'context'     => array( 'view', 'edit' ),
394
						),
395
						'taxonomies' => array(
396
							'description' => __( 'Taxonomy Terms for Product/Order Statuses', 'woocommerce' ),
397
							'type'        => 'array',
398
							'context'     => array( 'view', 'edit' ),
399
						),
400
					)
401
				),
402
				'pages' => array(
403
					'description' => __( 'WooCommerce Pages', 'woocommerce' ),
404
					'type'        => 'array',
405
					'context'     => array( 'view', 'edit' ),
406
				),
407
			)
408
		);
409
410
		return $this->add_additional_fields_schema( $schema );
411
	}
412
413
    /**
414
	 * Return an array of sections and the data associated with each.
415
	 *
416
	 * @return array
417
	 */
418
	public function get_item_mappings() {
419
		return array(
420
			'environment'    => $this->get_environment_info(),
421
			'database'       => $this->get_database_info(),
422
			'active_plugins' => $this->get_active_plugins(),
423
			'theme'          => $this->get_theme_info(),
424
			'settings'       => $this->get_settings(),
425
			'pages'          => $this->get_pages(),
426
		);
427
	}
428
429
	/**
430
	 * Get array of environment information. Includes thing like software
431
	 * versions, and various server settings.
432
	 *
433
	 * @return array
434
	 */
435
	public function get_environment_info() {
436
		global $wpdb;
437
438
		// Figure out cURL version, if installed.
439
		$curl_version = '';
440
		if ( function_exists( 'curl_version' ) ) {
441
            $curl_version = curl_version();
442
            $curl_version = $curl_version['version'] . ', ' . $curl_version['ssl_version'];
443
        }
444
445
		// WP memory limit
446
        $wp_memory_limit = wc_let_to_num( WP_MEMORY_LIMIT );
447
        if ( function_exists( 'memory_get_usage' ) ) {
448
            $wp_memory_limit = max( $wp_memory_limit, wc_let_to_num( @ini_get( 'memory_limit' ) ) );
449
        }
450
451
		// Test POST requests
452
		$post_response = wp_safe_remote_post( 'https://www.paypal.com/cgi-bin/webscr', array(
453
			'timeout'     => 60,
454
			'user-agent'  => 'WooCommerce/' . WC()->version,
455
			'httpversion' => '1.1',
456
			'body'        => array(
457
				'cmd'    => '_notify-validate'
458
			)
459
		) );
460
		$post_response_successful = false;
461 View Code Duplication
		if ( ! is_wp_error( $post_response ) && $post_response['response']['code'] >= 200 && $post_response['response']['code'] < 300 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
462
			$post_response_successful = true;
463
		}
464
465
		// Test GET requests
466
		$get_response = wp_safe_remote_get( 'https://woocommerce.com/wc-api/product-key-api?request=ping&network=' . ( is_multisite() ? '1' : '0' ) );
467
		$get_response_successful = false;
468 View Code Duplication
		if ( ! is_wp_error( $post_response ) && $post_response['response']['code'] >= 200 && $post_response['response']['code'] < 300 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
469
			$get_response_successful = true;
470
		}
471
472
		// Return all environment info. Described by JSON Schema.
473
		return array(
474
            'home_url'                  => get_option( 'home' ),
475
            'site_url'                  => get_option( 'siteurl' ),
476
            'version'                => WC()->version,
477
            'log_directory'             => WC_LOG_DIR,
478
            'log_directory_writable'    => ( @fopen( WC_LOG_DIR . 'test-log.log', 'a' ) ? true : false ),
479
            'wp_version'                => get_bloginfo('version'),
480
            'wp_multisite'              => is_multisite(),
481
            'wp_memory_limit'           => $wp_memory_limit,
482
            'wp_debug_mode'             => ( defined( 'WP_DEBUG' ) && WP_DEBUG ),
483
            'wp_cron'                   => ! ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ),
484
            'language'                  => get_locale(),
485
            'server_info'               => $_SERVER['SERVER_SOFTWARE'],
486
            'php_version'               => phpversion(),
487
            'php_post_max_size'         => wc_let_to_num( ini_get( 'post_max_size' ) ),
488
            'php_max_execution_time'    => ini_get( 'max_execution_time' ),
489
            'php_max_input_vars'        => ini_get( 'max_input_vars' ),
490
            'curl_version'              => $curl_version,
491
			'suhosin_installed'         => extension_loaded( 'suhosin' ),
492
			'max_upload_size'           => wp_max_upload_size(),
493
			'mysql_version'             => ( ! empty( $wpdb->is_mysql ) ? $wpdb->db_version() : '' ),
494
			'default_timezone'          => date_default_timezone_get(),
495
			'fsockopen_or_curl_enabled' => ( function_exists( 'fsockopen' ) || function_exists( 'curl_init' ) ),
496
			'soapclient_enabled'        => class_exists( 'SoapClient' ),
497
			'domdocument_enabled'       => class_exists( 'DOMDocument' ),
498
			'gzip_enabled'              => is_callable( 'gzopen' ),
499
			'mbstring_enabled'          => extension_loaded( 'mbstring' ),
500
			'remote_post_successful'    => $post_response_successful,
501
			'remote_post_response'      => ( is_wp_error( $post_response ) ? $post_response->get_error_message() : $post_response['response']['code'] ),
502
			'remote_get_successful'     => $get_response_successful,
503
			'remote_get_response'       => ( is_wp_error( $get_response ) ? $get_response->get_error_message() : $get_response['response']['code'] ),
504
        );
505
	}
506
507
	/**
508
	 * Get array of database information. Version, prefix, and table existence.
509
	 *
510
	 * @return array
511
	 */
512
	public function get_database_info() {
513
		global $wpdb;
514
515
		// WC Core tables to check existence of
516
		$tables = array(
517
			'woocommerce_sessions',
518
			'woocommerce_api_keys',
519
			'woocommerce_attribute_taxonomies',
520
			'woocommerce_downloadable_product_permissions',
521
			'woocommerce_order_items',
522
			'woocommerce_order_itemmeta',
523
			'woocommerce_tax_rates',
524
			'woocommerce_tax_rate_locations',
525
			'woocommerce_shipping_zones',
526
			'woocommerce_shipping_zone_locations',
527
			'woocommerce_shipping_zone_methods',
528
			'woocommerce_payment_tokens',
529
			'woocommerce_payment_tokenmeta',
530
		);
531
532
		if ( get_option( 'db_version' ) < 34370 ) {
533
			$tables[] = 'woocommerce_termmeta';
534
		}
535
		$table_exists = array();
536
		foreach ( $tables as $table ) {
537
			$table_exists[ $table ] = ( $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s;", $wpdb->prefix . $table ) ) === $wpdb->prefix . $table );
538
		}
539
540
		// Return all database info. Described by JSON Schema.
541
		return array(
542
			'wc_database_version'    => get_option( 'woocommerce_db_version' ),
543
			'database_prefix'        => $wpdb->prefix,
544
			'maxmind_geoip_database' => WC_Geolocation::get_local_database_path(),
545
			'database_tables'        => $table_exists,
546
		);
547
	}
548
549
	/**
550
	 * Get a list of plugins active on the site.
551
	 *
552
	 * @return array
553
	 */
554
	public function get_active_plugins() {
555
		require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
556
557
		// Get both site plugins and network plugins
558
		$active_plugins = (array) get_option( 'active_plugins', array() );
559 View Code Duplication
		if ( is_multisite() ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
560
			$network_activated_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
561
			$active_plugins            = array_merge( $active_plugins, $network_activated_plugins );
562
		}
563
564
		$active_plugins_data = array();
565
		foreach ( $active_plugins as $plugin ) {
566
			$data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
567
			// convert plugin data to json response format.
568
			$active_plugins_data[] = array(
569
				'name'        => $data['Name'],
570
				'version'     => $data['Version'],
571
				'url'         => $data['PluginURI'],
572
				'author_name' => $data['AuthorName'],
573
				'author_url'  => esc_url_raw( $data['AuthorURI'] ),
574
			);
575
		}
576
577
		return $active_plugins_data;
578
	}
579
580
	/**
581
	 * Get info on the current active theme, info on parent theme (if presnet)
582
	 * and a list of template overrides.
583
	 *
584
	 * @return array
585
	 */
586
	public function get_theme_info() {
587
		$active_theme = wp_get_theme();
588
589
		// Get parent theme info if this theme is a child theme, otherwise
590
		// pass empty info in the response.
591
		if ( is_child_theme() ) {
592
			$parent_theme      = wp_get_theme( $active_theme->Template );
593
			$parent_theme_info = array(
594
				'parent_name'       => $parent_theme->Name,
595
				'parentversion'     => $parent_theme->Version,
596
				'parent_author_url' => $parent_theme->{'Author URI'},
597
			);
598
		} else {
599
			$parent_theme_info = array( 'parent_theme_name' => '', 'parent_theme_version' => '', 'parent_theme_author_url' => '' );
600
		}
601
602
		/**
603
		 * Scan the theme directory for all WC templates to see if our theme
604
		 * overrides any of them.
605
		 */
606
		$override_files = array();
607
		$scan_files  = WC_Admin_Status::scan_template_files( WC()->plugin_path() . '/templates/' );
608 View Code Duplication
		foreach ( $scan_files as $file ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
609
			if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
610
				$theme_file = get_stylesheet_directory() . '/' . $file;
611
			} elseif ( file_exists( get_stylesheet_directory() . '/woocommerce/' . $file ) ) {
612
				$theme_file = get_stylesheet_directory() . '/woocommerce/' . $file;
613
			} elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
614
				$theme_file = get_template_directory() . '/' . $file;
615
			} elseif ( file_exists( get_template_directory() . '/woocommerce/' . $file ) ) {
616
				$theme_file = get_template_directory() . '/woocommerce/' . $file;
617
			} else {
618
				$theme_file = false;
619
			}
620
621
			if ( ! empty( $theme_file ) ) {
622
				$override_files[] = str_replace( WP_CONTENT_DIR . '/themes/', '', $theme_file );
623
			}
624
		}
625
626
		$active_theme_info = array(
627
			'name'                    => $active_theme->Name,
628
			'version'                 => $active_theme->Version,
629
			'author_url'              => esc_url_raw( $active_theme->{'Author URI'} ),
630
			'is_child_theme'          => is_child_theme(),
631
			'has_woocommerce_support' => ( current_theme_supports( 'woocommerce' ) || in_array( $active_theme->template, wc_get_core_supported_themes() ) ),
632
			'overrides'               => $override_files,
633
		);
634
635
		return array_merge( $active_theme_info, $parent_theme_info );
636
	}
637
638
	/**
639
	 * Get some setting values for the site that are useful for debugging
640
	 * purposes. For full settings access, use the settings api.
641
	 *
642
	 * @return array
643
	 */
644
	public function get_settings() {
645
		// Get a list of terms used for product/order taxonomies
646
		$term_response = array();
647
		$terms         = get_terms( 'product_type', array( 'hide_empty' => 0 ) );
648
		foreach ( $terms as $term ) {
649
			$term_response[ $term->slug ] = strtolower( $term->name );
650
		}
651
652
		// Return array of useful settings for debugging.
653
		return array(
654
			'api_enabled'         => 'yes' === get_option( 'woocommerce_api_enabled' ),
655
			'force_ssl'           => 'yes' === get_option( 'woocommerce_force_ssl_checkout' ),
656
			'currency'            => get_woocommerce_currency(),
657
			'currency_symbol'     => get_woocommerce_currency_symbol(),
658
			'currency_position'   => get_option( 'woocommerce_currency_pos' ),
659
			'thousand_separator'  => wc_get_price_thousand_separator(),
660
			'decimal_separator'   => wc_get_price_decimal_separator(),
661
			'number_of_decimals'  => wc_get_price_decimals(),
662
			'geolocation_enabled' => in_array( get_option( 'woocommerce_default_customer_address' ), array( 'geolocation_ajax', 'geolocation' ) ),
663
			'taxonomies'          => $term_response,
664
		);
665
	}
666
667
	/**
668
	 * Returns a mini-report on WC pages and if they are configured correctly:
669
	 * Present, visible, and including the correct shortcode.
670
	 *
671
	 * @return array
672
	 */
673
	public function get_pages() {
674
		// WC pages to check against
675
		$check_pages = array(
676
			_x( 'Shop Base', 'Page setting', 'woocommerce' ) => array(
677
				'option'    => 'woocommerce_shop_page_id',
678
				'shortcode' => '',
679
			),
680
			_x( 'Cart', 'Page setting', 'woocommerce' ) => array(
681
				'option'    => 'woocommerce_cart_page_id',
682
				'shortcode' => '[' . apply_filters( 'woocommerce_cart_shortcode_tag', 'woocommerce_cart' ) . ']',
683
			),
684
			_x( 'Checkout', 'Page setting', 'woocommerce' ) => array(
685
				'option'    => 'woocommerce_checkout_page_id',
686
				'shortcode' => '[' . apply_filters( 'woocommerce_checkout_shortcode_tag', 'woocommerce_checkout' ) . ']',
687
			),
688
			_x( 'My Account', 'Page setting', 'woocommerce' ) => array(
689
				'option'    => 'woocommerce_myaccount_page_id',
690
				'shortcode' => '[' . apply_filters( 'woocommerce_my_account_shortcode_tag', 'woocommerce_my_account' ) . ']',
691
			),
692
		);
693
694
		$pages_output = array();
695
		foreach ( $check_pages as $page_name => $values ) {
696
			$errors   = array();
0 ignored issues
show
Unused Code introduced by
$errors is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
697
			$page_id  = get_option( $values['option'] );
698
			$page_set = $page_exists = $page_visible = false;
699
			$shortcode_present = $shortcode_required = false;
700
701
			// Page checks
702
			if ( $page_id ) {
703
				$page_set = true;
704
			}
705
			if ( get_post( $page_id ) ) {
706
				$page_exists = true;
707
			}
708
			if ( 'publish' === get_post_status( $page_id ) ) {
709
				$page_visible = true;
710
			}
711
712
			// Shortcode checks
713
			if ( $values['shortcode']  && get_post( $page_id ) ) {
714
				$shortcode_required = true;
715
				$page = get_post( $page_id );
716
				if ( strstr( $page->post_content, $values['shortcode'] ) ) {
717
					$shortcode_present = true;
718
				}
719
			}
720
721
			// Wrap up our findings into an output array
722
			$pages_output[] = array(
723
					'page_name'          => $page_name,
724
					'page_id'            => $page_id,
725
					'page_set'           => $page_set,
726
					'page_exists'        => $page_exists,
727
					'page_visible'       => $page_visible,
728
					'shortcode_required' => $shortcode_required,
729
					'shortcode_present'  => $shortcode_present,
730
			);
731
		}
732
733
		return $pages_output;
734
	}
735
736
	/**
737
	 * Get any query params needed.
738
	 *
739
	 * @return array
740
	 */
741
	public function get_collection_params() {
742
		return array(
743
			'context' => $this->get_context_param( array( 'default' => 'view' ) ),
744
		);
745
	}
746
747
}
748