Skip to content

Commit e98494e

Browse files
authored
Merge pull request #1679 from equalizedigital/william/add-extra-compatibility-checking-info
Add a SystemInfo class for getting theme, plugin and env type
2 parents fe127ba + 261f8d9 commit e98494e

3 files changed

Lines changed: 635 additions & 10 deletions

File tree

includes/classes/MyDot/Connector.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace EqualizeDigital\AccessibilityChecker\MyDot;
1212

1313
use EqualizeDigital\AccessibilityChecker\Admin\AdminPage\ConnectedServicesPage;
14+
use EqualizeDigital\AccessibilityChecker\SystemInfo\SystemInfo;
1415

1516
/**
1617
* Class Connector
@@ -264,15 +265,14 @@ private function activate_license() {
264265
}
265266

266267
$api_params = [
267-
'edd_action' => 'activate_license',
268-
'license' => $license,
269-
'item_id' => self::PRODUCT_ID,
270-
'url' => home_url(),
271-
'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production',
272-
'wp_version' => get_bloginfo( 'version' ),
273-
'php_version' => phpversion(),
268+
'edd_action' => 'activate_license',
269+
'license' => $license,
270+
'item_id' => self::PRODUCT_ID,
271+
'url' => home_url(),
274272
];
275273

274+
$api_params = array_merge( $api_params, SystemInfo::get_license_request_context() );
275+
276276
$response = wp_remote_post(
277277
self::get_api_endpoint(),
278278
[
@@ -432,12 +432,11 @@ public function periodic_check_license() {
432432
'item_id' => self::PRODUCT_ID,
433433
'item_name' => rawurlencode( self::PRODUCT_NAME ),
434434
'url' => home_url(),
435-
'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production',
436435
'edac_version' => defined( 'EDAC_VERSION' ) ? EDAC_VERSION : '0.0.0',
437-
'wp_version' => get_bloginfo( 'version' ),
438-
'php_version' => phpversion(),
439436
];
440437

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

0 commit comments

Comments
 (0)