Completed
Push — master ( 411175...111214 )
by Benjamin
05:17 queued 02:23
created

API::get_wp_version()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
	 * API namespace.
25
	 *
26
	 * @var string
27
	 * @since 1.0.0
28
	 */
29
	protected $namespace = 'wp-site-monitor/v1';
30
31
	/**
32
	 * Register the routes for the objects of the controller.
33
	 *
34
	 * @since 1.0.0
35
	 */
36 7
	public function register_routes() {
37 7
		if ( get_option( WP_Site_Monitor::OPTION_NAMES['wp_version'], true ) ) {
38 6
			register_rest_route(
39 6
				$this->namespace, '/wp-version', array(
40
					array(
41 6
						'methods'             => WP_REST_Server::READABLE,
42 6
						'callback'            => array( $this, 'get_wp_version' ),
43 6
						'permission_callback' => array( $this, 'check_permissions' ),
44
					),
45
				)
46
			);
47
		}
48
49 7
		if ( get_option( WP_Site_Monitor::OPTION_NAMES['plugins'], true ) ) {
50 6
			register_rest_route(
51 6
				$this->namespace, '/plugins', array(
52
					array(
53 6
						'methods'             => WP_REST_Server::READABLE,
54 6
						'callback'            => array( $this, 'get_plugins' ),
55 6
						'permission_callback' => array( $this, 'check_permissions' ),
56
					),
57
				)
58
			);
59
		}
60 7
	}
61
62
	/**
63
	 * Get the current WordPress version number.
64
	 *
65
	 * @return string
66
	 * @since 1.0.0
67
	 */
68 1
	public function get_wp_version() {
69 1
		global $wp_version;
70
71 1
		return $wp_version;
72
	}
73
74
	/**
75
	 * Get the currently installed plugins.
76
	 *
77
	 * @return array
78
	 * @since 1.0.0
79
	 */
80 2
	public function get_plugins() {
81 2
		if ( ! function_exists( 'get_plugins' ) ) {
82
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
83
		}
84
85 2
		$plugins        = get_plugins();
86 2
		$active_plugins = get_option( 'active_plugins' );
87
88 2
		foreach ( $plugins as $name => &$data ) {
89 2
			$data['Active'] = in_array( $name, $active_plugins, true ) ? true : false;
90
		}
91
92 2
		return $plugins;
93
	}
94
95
	/**
96
	 * Check if the authenticated user has permission to use an endpoint.
97
	 *
98
	 * @return bool
99
	 * @since 1.0.0
100
	 */
101 1
	public function check_permissions() {
102 1
		return current_user_can( 'manage_options' );
103
	}
104
}
105