-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSystemInfo.php
More file actions
167 lines (141 loc) · 4.14 KB
/
Copy pathSystemInfo.php
File metadata and controls
167 lines (141 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* System information helpers.
*
* @package Accessibility_Checker
*/
namespace EqualizeDigital\AccessibilityChecker\SystemInfo;
use WP_Theme;
/**
* Collects active plugin and theme information.
*/
class SystemInfo {
/**
* Returns active plugins with name, slug, and version.
*
* @return array<int, array<string, string>>
*/
public static function get_active_plugins() {
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$active_plugins = [];
$plugins = wp_get_active_and_valid_plugins();
if ( is_array( $plugins ) ) {
foreach ( $plugins as $plugin_path ) {
$plugin_data = get_plugin_data( $plugin_path, false, false );
if ( is_array( $plugin_data ) ) {
$active_plugins[] = [
'name' => $plugin_data['Name'] ?? '',
'slug' => self::get_plugin_slug_from_path( $plugin_path ),
'version' => $plugin_data['Version'] ?? '',
];
}
}
}
return $active_plugins;
}
/**
* Gets the plugin slug from a plugin file path.
*
* @param string $plugin_path Path to a plugin file.
* @return string
*/
public static function get_plugin_slug_from_path( $plugin_path ) {
if ( ! is_string( $plugin_path ) || '' === $plugin_path ) {
return '';
}
$relative = plugin_basename( $plugin_path );
$dir = dirname( $relative );
if ( '.' !== $dir ) {
return $dir;
}
return basename( $relative, '.php' );
}
/**
* Returns active theme information, including parent data for child themes.
*
* @return array<string, mixed>
*/
public static function get_active_theme() {
$theme_data = wp_get_theme();
$active_theme = self::get_theme_data_collection( $theme_data );
$active_theme['accessibility_ready'] = self::is_theme_accessibility_ready( $theme_data );
$active_theme['parent_theme'] = [];
if ( $theme_data->parent() ) {
$active_theme['parent_theme'] = self::get_theme_data_collection( $theme_data->parent() );
}
return $active_theme;
}
/**
* Gets theme details as a normalized collection.
*
* @param mixed $theme Theme object.
* @return array<string, mixed>
*/
public static function get_theme_data_collection( $theme ) {
if ( ! is_a( $theme, '\\WP_Theme' ) ) {
return [];
}
return [
'name' => $theme->get( 'Name' ) ?? '',
'slug' => $theme->get_stylesheet() ?? '',
'version' => $theme->get( 'Version' ) ?? '',
'tags' => $theme->get( 'Tags' ) ?? [],
];
}
/**
* Checks whether theme or parent theme is tagged accessibility-ready.
*
* @param WP_Theme $theme_data Theme object to check.
* @return bool
*/
public static function is_theme_accessibility_ready( WP_Theme $theme_data ) {
$tags = is_array( $theme_data->get( 'Tags' ) ) ? $theme_data->get( 'Tags' ) : [];
if ( $theme_data->parent() ) {
$parent_tags = $theme_data->parent()->get( 'Tags' );
$tags = array_merge( $tags, is_array( $parent_tags ) ? $parent_tags : [] );
}
return in_array( 'accessibility-ready', $tags, true );
}
/**
* Gets the current WordPress environment type.
*
* @return string
*/
public static function get_environment_type() {
return function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production';
}
/**
* Gets the current WordPress version.
*
* @return string
*/
public static function get_wordpress_version() {
return (string) get_bloginfo( 'version' );
}
/**
* Gets the current PHP version.
*
* @return string
*/
public static function get_php_version() {
return (string) phpversion();
}
/**
* Gets a payload-ready set of system fields for license API requests.
*
* @return array<string, mixed>
*/
public static function get_license_request_context() {
$active_plugins = wp_json_encode( self::get_active_plugins() );
$active_theme = wp_json_encode( self::get_active_theme() );
return [
'environment' => self::get_environment_type(),
'wp_version' => self::get_wordpress_version(),
'php_version' => self::get_php_version(),
'active_plugins' => false !== $active_plugins ? $active_plugins : '[]',
'active_theme' => false !== $active_theme ? $active_theme : '{}',
];
}
}