|
| 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