Completed
Push — master ( 31131b...6176aa )
by Mike
15:27
created

SystemStatus   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 598
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 429
dl 0
loc 598
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A register_routes() 0 14 1
A get_items_permissions_check() 0 5 2
A prepare_item_for_response() 0 13 1
A get_collection_params() 0 3 1
A get_item_mappings() 0 19 1
A get_items() 0 12 2
B get_item_schema() 0 471 1
1
<?php
2
/**
3
 * REST API WC System Status controller
4
 *
5
 * Handles requests to the /system_status endpoint.
6
 *
7
 * @package WooCommerce/RestApi
8
 */
9
10
namespace WooCommerce\RestApi\Controllers\Version4;
11
12
defined( 'ABSPATH' ) || exit;
13
14
/**
15
 * REST API System Status controller class.
16
 */
17
class SystemStatus extends AbstractController {
18
19
	/**
20
	 * Route base.
21
	 *
22
	 * @var string
23
	 */
24
	protected $rest_base = 'system_status';
25
26
	/**
27
	 * Register the route for /system_status
28
	 */
29
	public function register_routes() {
30
		register_rest_route(
31
			$this->namespace,
32
			'/' . $this->rest_base,
33
			array(
34
				array(
35
					'methods'             => \WP_REST_Server::READABLE,
36
					'callback'            => array( $this, 'get_items' ),
37
					'permission_callback' => array( $this, 'get_items_permissions_check' ),
38
					'args'                => $this->get_collection_params(),
39
				),
40
				'schema' => array( $this, 'get_public_item_schema' ),
41
			),
42
			true
43
		);
44
	}
45
46
	/**
47
	 * Check whether a given request has permission to view system status.
48
	 *
49
	 * @param  \WP_REST_Request $request Full details about the request.
50
	 * @return \WP_Error|boolean
51
	 */
52
	public function get_items_permissions_check( $request ) {
53
		if ( ! wc_rest_check_manager_permissions( 'system_status', 'read' ) ) {
54
			return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
55
		}
56
		return true;
57
	}
58
59
	/**
60
	 * Get a system status info, by section.
61
	 *
62
	 * @param \WP_REST_Request $request Full details about the request.
63
	 * @return \WP_Error|\WP_REST_Response
64
	 */
65
	public function get_items( $request ) {
66
		$schema   = $this->get_item_schema();
67
		$mappings = $this->get_item_mappings();
68
		$response = array();
69
70
		foreach ( $mappings as $section => $values ) {
71
			$response[ $section ] = $values;
72
		}
73
74
		$response = $this->prepare_item_for_response( $response, $request );
75
76
		return rest_ensure_response( $response );
77
	}
78
79
	/**
80
	 * Get the system status schema, conforming to JSON Schema.
81
	 *
82
	 * @return array
83
	 */
84
	public function get_item_schema() {
85
		$schema = array(
86
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
87
			'title'      => 'system_status',
88
			'type'       => 'object',
89
			'properties' => array(
90
				'environment'    => array(
91
					'description' => __( 'Environment.', 'woocommerce' ),
92
					'type'        => 'object',
93
					'context'     => array( 'view' ),
94
					'readonly'    => true,
95
					'properties'  => array(
96
						'home_url'                  => array(
97
							'description' => __( 'Home URL.', 'woocommerce' ),
98
							'type'        => 'string',
99
							'format'      => 'uri',
100
							'context'     => array( 'view' ),
101
							'readonly'    => true,
102
						),
103
						'site_url'                  => array(
104
							'description' => __( 'Site URL.', 'woocommerce' ),
105
							'type'        => 'string',
106
							'format'      => 'uri',
107
							'context'     => array( 'view' ),
108
							'readonly'    => true,
109
						),
110
						'wc_version'                => array(
111
							'description' => __( 'WooCommerce version.', 'woocommerce' ),
112
							'type'        => 'string',
113
							'context'     => array( 'view' ),
114
							'readonly'    => true,
115
						),
116
						'log_directory'             => array(
117
							'description' => __( 'Log directory.', 'woocommerce' ),
118
							'type'        => 'string',
119
							'context'     => array( 'view' ),
120
							'readonly'    => true,
121
						),
122
						'log_directory_writable'    => array(
123
							'description' => __( 'Is log directory writable?', 'woocommerce' ),
124
							'type'        => 'boolean',
125
							'context'     => array( 'view' ),
126
							'readonly'    => true,
127
						),
128
						'wp_version'                => array(
129
							'description' => __( 'WordPress version.', 'woocommerce' ),
130
							'type'        => 'string',
131
							'context'     => array( 'view' ),
132
							'readonly'    => true,
133
						),
134
						'wp_multisite'              => array(
135
							'description' => __( 'Is WordPress multisite?', 'woocommerce' ),
136
							'type'        => 'boolean',
137
							'context'     => array( 'view' ),
138
							'readonly'    => true,
139
						),
140
						'wp_memory_limit'           => array(
141
							'description' => __( 'WordPress memory limit.', 'woocommerce' ),
142
							'type'        => 'integer',
143
							'context'     => array( 'view' ),
144
							'readonly'    => true,
145
						),
146
						'wp_debug_mode'             => array(
147
							'description' => __( 'Is WordPress debug mode active?', 'woocommerce' ),
148
							'type'        => 'boolean',
149
							'context'     => array( 'view' ),
150
							'readonly'    => true,
151
						),
152
						'wp_cron'                   => array(
153
							'description' => __( 'Are WordPress cron jobs enabled?', 'woocommerce' ),
154
							'type'        => 'boolean',
155
							'context'     => array( 'view' ),
156
							'readonly'    => true,
157
						),
158
						'language'                  => array(
159
							'description' => __( 'WordPress language.', 'woocommerce' ),
160
							'type'        => 'string',
161
							'context'     => array( 'view' ),
162
							'readonly'    => true,
163
						),
164
						'server_info'               => array(
165
							'description' => __( 'Server info.', 'woocommerce' ),
166
							'type'        => 'string',
167
							'context'     => array( 'view' ),
168
							'readonly'    => true,
169
						),
170
						'php_version'               => array(
171
							'description' => __( 'PHP version.', 'woocommerce' ),
172
							'type'        => 'string',
173
							'context'     => array( 'view' ),
174
							'readonly'    => true,
175
						),
176
						'php_post_max_size'         => array(
177
							'description' => __( 'PHP post max size.', 'woocommerce' ),
178
							'type'        => 'integer',
179
							'context'     => array( 'view' ),
180
							'readonly'    => true,
181
						),
182
						'php_max_execution_time'    => array(
183
							'description' => __( 'PHP max execution time.', 'woocommerce' ),
184
							'type'        => 'integer',
185
							'context'     => array( 'view' ),
186
							'readonly'    => true,
187
						),
188
						'php_max_input_vars'        => array(
189
							'description' => __( 'PHP max input vars.', 'woocommerce' ),
190
							'type'        => 'integer',
191
							'context'     => array( 'view' ),
192
							'readonly'    => true,
193
						),
194
						'curl_version'              => array(
195
							'description' => __( 'cURL version.', 'woocommerce' ),
196
							'type'        => 'string',
197
							'context'     => array( 'view' ),
198
							'readonly'    => true,
199
						),
200
						'suhosin_installed'         => array(
201
							'description' => __( 'Is SUHOSIN installed?', 'woocommerce' ),
202
							'type'        => 'boolean',
203
							'context'     => array( 'view' ),
204
							'readonly'    => true,
205
						),
206
						'max_upload_size'           => array(
207
							'description' => __( 'Max upload size.', 'woocommerce' ),
208
							'type'        => 'integer',
209
							'context'     => array( 'view' ),
210
							'readonly'    => true,
211
						),
212
						'mysql_version'             => array(
213
							'description' => __( 'MySQL version.', 'woocommerce' ),
214
							'type'        => 'string',
215
							'context'     => array( 'view' ),
216
							'readonly'    => true,
217
						),
218
						'mysql_version_string'             => array(
219
							'description' => __( 'MySQL version string.', 'woocommerce' ),
220
							'type'        => 'string',
221
							'context'     => array( 'view' ),
222
							'readonly'    => true,
223
						),
224
						'default_timezone'          => array(
225
							'description' => __( 'Default timezone.', 'woocommerce' ),
226
							'type'        => 'string',
227
							'context'     => array( 'view' ),
228
							'readonly'    => true,
229
						),
230
						'fsockopen_or_curl_enabled' => array(
231
							'description' => __( 'Is fsockopen/cURL enabled?', 'woocommerce' ),
232
							'type'        => 'boolean',
233
							'context'     => array( 'view' ),
234
							'readonly'    => true,
235
						),
236
						'soapclient_enabled'        => array(
237
							'description' => __( 'Is SoapClient class enabled?', 'woocommerce' ),
238
							'type'        => 'boolean',
239
							'context'     => array( 'view' ),
240
							'readonly'    => true,
241
						),
242
						'domdocument_enabled'       => array(
243
							'description' => __( 'Is DomDocument class enabled?', 'woocommerce' ),
244
							'type'        => 'boolean',
245
							'context'     => array( 'view' ),
246
							'readonly'    => true,
247
						),
248
						'gzip_enabled'              => array(
249
							'description' => __( 'Is GZip enabled?', 'woocommerce' ),
250
							'type'        => 'boolean',
251
							'context'     => array( 'view' ),
252
							'readonly'    => true,
253
						),
254
						'mbstring_enabled'          => array(
255
							'description' => __( 'Is mbstring enabled?', 'woocommerce' ),
256
							'type'        => 'boolean',
257
							'context'     => array( 'view' ),
258
							'readonly'    => true,
259
						),
260
						'remote_post_successful'    => array(
261
							'description' => __( 'Remote POST successful?', 'woocommerce' ),
262
							'type'        => 'boolean',
263
							'context'     => array( 'view' ),
264
							'readonly'    => true,
265
						),
266
						'remote_post_response'      => array(
267
							'description' => __( 'Remote POST response.', 'woocommerce' ),
268
							'type'        => 'string',
269
							'context'     => array( 'view' ),
270
							'readonly'    => true,
271
						),
272
						'remote_get_successful'     => array(
273
							'description' => __( 'Remote GET successful?', 'woocommerce' ),
274
							'type'        => 'boolean',
275
							'context'     => array( 'view' ),
276
							'readonly'    => true,
277
						),
278
						'remote_get_response'       => array(
279
							'description' => __( 'Remote GET response.', 'woocommerce' ),
280
							'type'        => 'string',
281
							'context'     => array( 'view' ),
282
							'readonly'    => true,
283
						),
284
					),
285
				),
286
				'database'       => array(
287
					'description' => __( 'Database.', 'woocommerce' ),
288
					'type'        => 'object',
289
					'context'     => array( 'view' ),
290
					'readonly'    => true,
291
					'properties'  => array(
292
						'wc_database_version'    => array(
293
							'description' => __( 'WC database version.', 'woocommerce' ),
294
							'type'        => 'string',
295
							'context'     => array( 'view' ),
296
							'readonly'    => true,
297
						),
298
						'database_prefix'        => array(
299
							'description' => __( 'Database prefix.', 'woocommerce' ),
300
							'type'        => 'string',
301
							'context'     => array( 'view' ),
302
							'readonly'    => true,
303
						),
304
						'maxmind_geoip_database' => array(
305
							'description' => __( 'MaxMind GeoIP database.', 'woocommerce' ),
306
							'type'        => 'string',
307
							'context'     => array( 'view' ),
308
							'readonly'    => true,
309
						),
310
						'database_tables'        => array(
311
							'description' => __( 'Database tables.', 'woocommerce' ),
312
							'type'        => 'array',
313
							'context'     => array( 'view' ),
314
							'readonly'    => true,
315
							'items'       => array(
316
								'type' => 'string',
317
							),
318
						),
319
					),
320
				),
321
				'active_plugins' => array(
322
					'description' => __( 'Active plugins.', 'woocommerce' ),
323
					'type'        => 'array',
324
					'context'     => array( 'view' ),
325
					'readonly'    => true,
326
					'items'       => array(
327
						'type' => 'string',
328
					),
329
				),
330
				'inactive_plugins' => array(
331
					'description' => __( 'Inactive plugins.', 'woocommerce' ),
332
					'type'        => 'array',
333
					'context'     => array( 'view' ),
334
					'readonly'    => true,
335
					'items'       => array(
336
						'type' => 'string',
337
					),
338
				),
339
				'dropins_mu_plugins' => array(
340
					'description' => __( 'Dropins & MU plugins.', 'woocommerce' ),
341
					'type'        => 'array',
342
					'context'     => array( 'view' ),
343
					'readonly'    => true,
344
					'items'       => array(
345
						'type' => 'string',
346
					),
347
				),
348
				'theme'          => array(
349
					'description' => __( 'Theme.', 'woocommerce' ),
350
					'type'        => 'object',
351
					'context'     => array( 'view' ),
352
					'readonly'    => true,
353
					'properties'  => array(
354
						'name'                    => array(
355
							'description' => __( 'Theme name.', 'woocommerce' ),
356
							'type'        => 'string',
357
							'context'     => array( 'view' ),
358
							'readonly'    => true,
359
						),
360
						'version'                 => array(
361
							'description' => __( 'Theme version.', 'woocommerce' ),
362
							'type'        => 'string',
363
							'context'     => array( 'view' ),
364
							'readonly'    => true,
365
						),
366
						'version_latest'          => array(
367
							'description' => __( 'Latest version of theme.', 'woocommerce' ),
368
							'type'        => 'string',
369
							'context'     => array( 'view' ),
370
							'readonly'    => true,
371
						),
372
						'author_url'              => array(
373
							'description' => __( 'Theme author URL.', 'woocommerce' ),
374
							'type'        => 'string',
375
							'format'      => 'uri',
376
							'context'     => array( 'view' ),
377
							'readonly'    => true,
378
						),
379
						'is_child_theme'          => array(
380
							'description' => __( 'Is this theme a child theme?', 'woocommerce' ),
381
							'type'        => 'boolean',
382
							'context'     => array( 'view' ),
383
							'readonly'    => true,
384
						),
385
						'has_woocommerce_support' => array(
386
							'description' => __( 'Does the theme declare WooCommerce support?', 'woocommerce' ),
387
							'type'        => 'boolean',
388
							'context'     => array( 'view' ),
389
							'readonly'    => true,
390
						),
391
						'has_woocommerce_file'    => array(
392
							'description' => __( 'Does the theme have a woocommerce.php file?', 'woocommerce' ),
393
							'type'        => 'boolean',
394
							'context'     => array( 'view' ),
395
							'readonly'    => true,
396
						),
397
						'has_outdated_templates'  => array(
398
							'description' => __( 'Does this theme have outdated templates?', 'woocommerce' ),
399
							'type'        => 'boolean',
400
							'context'     => array( 'view' ),
401
							'readonly'    => true,
402
						),
403
						'overrides'               => array(
404
							'description' => __( 'Template overrides.', 'woocommerce' ),
405
							'type'        => 'array',
406
							'context'     => array( 'view' ),
407
							'readonly'    => true,
408
							'items'       => array(
409
								'type' => 'string',
410
							),
411
						),
412
						'parent_name'             => array(
413
							'description' => __( 'Parent theme name.', 'woocommerce' ),
414
							'type'        => 'string',
415
							'context'     => array( 'view' ),
416
							'readonly'    => true,
417
						),
418
						'parent_version'          => array(
419
							'description' => __( 'Parent theme version.', 'woocommerce' ),
420
							'type'        => 'string',
421
							'context'     => array( 'view' ),
422
							'readonly'    => true,
423
						),
424
						'parent_author_url'       => array(
425
							'description' => __( 'Parent theme author URL.', 'woocommerce' ),
426
							'type'        => 'string',
427
							'format'      => 'uri',
428
							'context'     => array( 'view' ),
429
							'readonly'    => true,
430
						),
431
					),
432
				),
433
				'settings'       => array(
434
					'description' => __( 'Settings.', 'woocommerce' ),
435
					'type'        => 'object',
436
					'context'     => array( 'view' ),
437
					'readonly'    => true,
438
					'properties'  => array(
439
						'api_enabled'              => array(
440
							'description' => __( 'REST API enabled?', 'woocommerce' ),
441
							'type'        => 'boolean',
442
							'context'     => array( 'view' ),
443
							'readonly'    => true,
444
						),
445
						'force_ssl'                => array(
446
							'description' => __( 'SSL forced?', 'woocommerce' ),
447
							'type'        => 'boolean',
448
							'context'     => array( 'view' ),
449
							'readonly'    => true,
450
						),
451
						'currency'                 => array(
452
							'description' => __( 'Currency.', 'woocommerce' ),
453
							'type'        => 'string',
454
							'context'     => array( 'view' ),
455
							'readonly'    => true,
456
						),
457
						'currency_symbol'          => array(
458
							'description' => __( 'Currency symbol.', 'woocommerce' ),
459
							'type'        => 'string',
460
							'context'     => array( 'view' ),
461
							'readonly'    => true,
462
						),
463
						'currency_position'        => array(
464
							'description' => __( 'Currency position.', 'woocommerce' ),
465
							'type'        => 'string',
466
							'context'     => array( 'view' ),
467
							'readonly'    => true,
468
						),
469
						'thousand_separator'       => array(
470
							'description' => __( 'Thousand separator.', 'woocommerce' ),
471
							'type'        => 'string',
472
							'context'     => array( 'view' ),
473
							'readonly'    => true,
474
						),
475
						'decimal_separator'        => array(
476
							'description' => __( 'Decimal separator.', 'woocommerce' ),
477
							'type'        => 'string',
478
							'context'     => array( 'view' ),
479
							'readonly'    => true,
480
						),
481
						'number_of_decimals'       => array(
482
							'description' => __( 'Number of decimals.', 'woocommerce' ),
483
							'type'        => 'integer',
484
							'context'     => array( 'view' ),
485
							'readonly'    => true,
486
						),
487
						'geolocation_enabled'      => array(
488
							'description' => __( 'Geolocation enabled?', 'woocommerce' ),
489
							'type'        => 'boolean',
490
							'context'     => array( 'view' ),
491
							'readonly'    => true,
492
						),
493
						'taxonomies'               => array(
494
							'description' => __( 'Taxonomy terms for product/order statuses.', 'woocommerce' ),
495
							'type'        => 'array',
496
							'context'     => array( 'view' ),
497
							'readonly'    => true,
498
							'items'       => array(
499
								'type' => 'string',
500
							),
501
						),
502
						'product_visibility_terms' => array(
503
							'description' => __( 'Terms in the product visibility taxonomy.', 'woocommerce' ),
504
							'type'        => 'array',
505
							'context'     => array( 'view' ),
506
							'readonly'    => true,
507
							'items'       => array(
508
								'type' => 'string',
509
							),
510
						),
511
					),
512
				),
513
				'security'       => array(
514
					'description' => __( 'Security.', 'woocommerce' ),
515
					'type'        => 'object',
516
					'context'     => array( 'view' ),
517
					'readonly'    => true,
518
					'properties'  => array(
519
						'secure_connection' => array(
520
							'description' => __( 'Is the connection to your store secure?', 'woocommerce' ),
521
							'type'        => 'boolean',
522
							'context'     => array( 'view' ),
523
							'readonly'    => true,
524
						),
525
						'hide_errors'       => array(
526
							'description' => __( 'Hide errors from visitors?', 'woocommerce' ),
527
							'type'        => 'boolean',
528
							'context'     => array( 'view' ),
529
							'readonly'    => true,
530
						),
531
					),
532
				),
533
				'pages'          => array(
534
					'description' => __( 'WooCommerce pages.', 'woocommerce' ),
535
					'type'        => 'array',
536
					'context'     => array( 'view' ),
537
					'readonly'    => true,
538
					'items'       => array(
539
						'type' => 'string',
540
					),
541
				),
542
				'post_type_counts' => array(
543
					'description' => __( 'Post type counts.', 'woocommerce' ),
544
					'type'        => 'array',
545
					'context'     => array( 'view' ),
546
					'readonly'    => true,
547
					'items'       => array(
548
						'type' => 'string',
549
					),
550
				),
551
			),
552
		);
553
554
		return $this->add_additional_fields_schema( $schema );
555
	}
556
557
	/**
558
	 * Return an array of sections and the data associated with each.
559
	 *
560
	 * @return array
561
	 */
562
	public function get_item_mappings() {
563
		$plugin_info     = new \WooCommerce\RestApi\Utilities\PluginInformation();
564
		$theme_info      = new \WooCommerce\RestApi\Utilities\ThemeInformation();
565
		$server          = new \WooCommerce\RestApi\Utilities\ServerEnvironment();
566
		$database        = new \WooCommerce\RestApi\Utilities\DatabaseInformation();
567
		$wp_environment  = new \WooCommerce\RestApi\Utilities\WPEnvironment();
568
		$woo_environment = new \WooCommerce\RestApi\Utilities\WooEnvironment();
569
570
		return array(
571
			'environment'        => $server->get_environment_info(),
572
			'database'           => $database->get_database_info(),
573
			'active_plugins'     => $plugin_info->get_active_plugin_data(),
574
			'inactive_plugins'   => $plugin_info->get_inactive_plugin_data(),
575
			'dropins_mu_plugins' => $plugin_info->get_dropin_and_mu_plugin_data(),
576
			'theme'              => $theme_info->get_theme_info(),
577
			'settings'           => $woo_environment->get_settings(),
578
			'security'           => $wp_environment->get_security_info(),
579
			'pages'              => $wp_environment->get_pages(),
580
			'post_type_counts'   => $wp_environment->get_post_type_counts(),
581
		);
582
	}
583
584
	/**
585
	 * Get any query params needed.
586
	 *
587
	 * @return array
588
	 */
589
	public function get_collection_params() {
590
		return array(
591
			'context' => $this->get_context_param( array( 'default' => 'view' ) ),
592
		);
593
	}
594
595
	/**
596
	 * Prepare the system status response
597
	 *
598
	 * @param  array            $system_status System status data.
599
	 * @param  \WP_REST_Request $request       Request object.
600
	 * @return \WP_REST_Response
601
	 */
602
	public function prepare_item_for_response( $system_status, $request ) {
603
		$data     = $this->add_additional_fields_to_object( $system_status, $request );
604
		$data     = $this->filter_response_by_context( $data, 'view' );
605
		$response = rest_ensure_response( $data );
606
607
		/**
608
		 * Filter the system status returned from the REST API.
609
		 *
610
		 * @param \WP_REST_Response   $response The response object.
611
		 * @param mixed              $system_status System status
612
		 * @param \WP_REST_Request    $request  Request object.
613
		 */
614
		return apply_filters( 'woocommerce_rest_prepare_system_status', $response, $system_status, $request );
615
	}
616
}
617