Passed
Push — master ( d2d71f...319ffa )
by Benjamin
02:54
created

API::get_plugins()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 9.2
cc 4
eloc 7
nc 6
nop 0
crap 4.0312
1
<?php
2
/**
3
 * Custom routes and endpoints for the plugin.
4
 *
5
 * @package WPSiteMonitor
6
 * @link https://github.com/BWibrew/WP-Site-Monitor/
7
 * @author Benjamin Wibrew <[email protected]>
8
 * @since 1.0.0
9
 */
10
11
namespace WPSiteMonitor;
12
13
use WP_REST_Controller;
14
use WP_REST_Server;
15
16
/**
17
 * Class API
18
 *
19
 * @package WPSiteMonitor
20
 */
21
class API extends WP_REST_Controller {
22
23
	/**
24
	 * Register the routes for the objects of the controller.
25
	 *
26
	 * @since 1.0.0
27
	 */
28 10
	public function register_routes() {
29 10
		$namespace = 'wp-site-monitor/v1';
30
31 10
		register_rest_route(
32 10
			$namespace, '/wp-version', array(
33
				array(
34 10
					'methods'             => WP_REST_Server::READABLE,
35 10
					'callback'            => array( $this, 'get_wp_version' ),
36 10
					'permission_callback' => array( $this, 'check_permissions' ),
37
				),
38
			)
39
		);
40
41 10
		register_rest_route(
42 10
			$namespace, '/plugins', array(
43
				array(
44 10
					'methods'             => WP_REST_Server::READABLE,
45 10
					'callback'            => array( $this, 'get_plugins' ),
46 10
					'permission_callback' => array( $this, 'check_permissions' ),
47
				),
48
			)
49
		);
50 10
	}
51
52
	/**
53
	 * Get the current WordPress version number.
54
	 *
55
	 * @return string
56
	 * @since 1.0.0
57
	 */
58 1
	public function get_wp_version() {
59 1
		global $wp_version;
60
61 1
		return $wp_version;
62
	}
63
64
	/**
65
	 * Get the currently installed plugins.
66
	 */
67 2
	public function get_plugins() {
68 2
		if ( ! function_exists( 'get_plugins' ) ) {
69
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
70
		}
71
72 2
		$plugins        = get_plugins();
73 2
		$active_plugins = get_option( 'active_plugins' );
74
75 2
		foreach ( $plugins as $name => &$data ) {
76 2
			$data['Active'] = in_array( $name, $active_plugins, true ) ? true : false;
77
		}
78
79 2
		return $plugins;
80
	}
81
82
	/**
83
	 * Check if the authenticated user has permission to use an endpoint.
84
	 *
85
	 * @return bool
86
	 * @since 1.0.0
87
	 */
88 1
	public function check_permissions() {
89 1
		return current_user_can( 'manage_options' );
90
	}
91
}
92