Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/patrik-csak/fa-wp-admin-menu-icons. Font Awesome icons for WordPress custom post types and custom m wordpress.org/plugins/fa-wp-admin-menu-icons
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124<?php
namespace Fawpami;
use Exception;use JsonException;use SimpleXMLElement;
require_once 'Fawpami.php';
readonly class Icon{ private const FONT_AWESOME_CLASS_PATTERN = '/^fa-?(?<style>b|brands|r|regular|s|solid)\s+fa-(?<name>[a-z-]+)$/';
public function __construct( private string $name, private string $style, ) { }
public static function fromClass(string $class): ?self { $result = preg_match( self::FONT_AWESOME_CLASS_PATTERN, $class, $matches, );
if ($result !== 1) { return null; }
$style = match ($matches['style']) { 'b', 'brands' => 'brands', 'r', 'regular' => 'regular', 's', 'solid' => 'solid', };
return new self($matches['name'], $style); }
/** * @throws Exception */ public function getSvgDataUri(): string { if ($cached = $this->getSvgDataUriFromCache()) { return $cached; }
$svgDataUri = $this->getSvgDataUriFromGitHub();
$this->cacheSvgDataUri($svgDataUri);
return $svgDataUri; }
private function getSvgDataUriFromCache(): string|false { return get_option($this->getOptionName()); }
private function getOptionName(): string { return "fawpami_icon_{$this->name}_{$this->style}_" . Fawpami::FONT_AWESOME_VERSION; }
/** * @throws Exception */ private function getSvgDataUriFromGitHub(): string { $svg = new SimpleXMLElement($this->getSvgFromGitHub());
// Add black fill, as recommended by WordPress: // https://codex.wordpress.org/Function_Reference/register_post_type#menu_icon $svg->addAttribute('style', 'fill:black');
return 'data:image/svg+xml;base64,' . base64_encode($svg->asXML()); }
/** * @throws Exception */ private function getSvgFromGitHub(): string { $name = $this->getUnaliasedName(); $url = 'https://raw.githubusercontent.com/FortAwesome/Font-Awesome/' . Fawpami::FONT_AWESOME_VERSION . "/svgs/$this->style/$name.svg"; $response = wp_remote_get($url); $body = wp_remote_retrieve_body($response);
if (!$body || wp_remote_retrieve_response_code($response) !== 200) { $message = is_wp_error($response) ? $response->get_error_message() : "Failed to download icon from $url";
throw new Exception($message); }
return $body; }
private function cacheSvgDataUri(string $svgDataUri): void { add_option($this->getOptionName(), $svgDataUri); }
/** * @throws JsonException */ private function getUnaliasedName(): string { $aliases = json_decode( file_get_contents(__DIR__. '/aliases.json'), associative: true, flags: JSON_THROW_ON_ERROR, );
return $aliases[$this->name] ?? $this->name; }}