| 1118 | /** |
| 1119 | * Get headers from theme's style.css and readme.txt files. |
| 1120 | * |
| 1121 | * Uses `get_file_data()` to get and combine the theme headers |
| 1122 | * from the theme's style.css and readme.txt files. Then checks |
| 1123 | * the Theme API for WP and PHP compatibility data. |
| 1124 | * |
| 1125 | * Precedence order: API > readme.txt > style.css |
| 1126 | * |
| 1127 | * @since 5.x.x |
| 1128 | * |
| 1129 | * @param string $theme_dir Directory of the theme within the theme_root. |
| 1130 | * @param string $theme_file Main theme file. |
| 1131 | * |
| 1132 | * @return array Theme headers. |
| 1133 | */ |
| 1134 | public function get_theme_headers( $theme_dir, $theme_file ) { |
| 1135 | $theme_headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' ); |
| 1136 | |
| 1137 | $readme = $this->theme_root . '/'. $theme_dir . '/readme.txt'; |
| 1138 | if ( file_exists( $readme ) ) { |
| 1139 | $readme_headers = get_file_data( $readme, self::$file_headers, 'theme' ); |
| 1140 | |
| 1141 | // Precedence to headers in readme.txt over style.css file. |
| 1142 | foreach ( $theme_headers as $header => $value ) { |
| 1143 | if ( ! empty( $readme_headers[ $header ] ) ) { |
| 1144 | $theme_headers[ $header ] = $readme_headers[ $header ]; |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | // Get Theme API data. |
| 1150 | $response = wp_remote_get( "https://api.wordpress.org/themes/info/1.2/?action=theme_information&request[slug]=$theme_dir" ); |
| 1151 | $response = wp_remote_retrieve_body( $response ); |
| 1152 | $response = json_decode( $response ); |
| 1153 | |
| 1154 | if ( ! property_exists( $response, 'error' ) ) { |
| 1155 | // Precedence to Theme API. |
| 1156 | $theme_headers['Requires'] = ! empty( $response->requires ) ? $response->requires : $theme_headers['Requires']; |
| 1157 | $theme_headers['RequiresPHP'] = ! empty( $response->requires ) ? $response->requires_php : $theme_headers['RequiresPHP']; |
| 1158 | } |
| 1159 | |
| 1160 | return $theme_headers; |
| 1161 | } |
| 1162 | |