generate-menu-dashicons.php
3.79 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
<?php
/**
* This utility script generates menu icons metadata based on the Dashicons icon font included in WordPress.
*/
if ( !defined('ABSPATH') ) {
die('No direct script access');
}
if ( !constant('WP_DEBUG') || !current_user_can('edit_plugins') ) {
echo "Permission denied. You need the edit_plugins cap to run this script and WP_DEBUG must be enabled.";
return;
}
require_once dirname(__FILE__) . '/PHP-CSS-Parser/autoloader.php';
$dashiconsStylesheet = ABSPATH . WPINC . '/css/dashicons.css';
$icons = array();
$allDashiconDefinitions = '';
$ignoreIcons = array('dashboard', 'editor-bold', 'editor-italic');
$ignoreIcons = array_flip($ignoreIcons);
// phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown -- Not fetching remote data.
$parser = new Sabberworm\CSS\Parser(file_get_contents($dashiconsStylesheet));
$cssDocument = $parser->parse();
$blocks = $cssDocument->getAllDeclarationBlocks();
foreach($blocks as $block) {
/** @var Sabberworm\CSS\RuleSet\DeclarationBlock $block */
//We want the ".dashicons-*:before" selectors.
$selectors = $block->getSelectors();
foreach($selectors as $selector) {
/** @var Sabberworm\CSS\Property\Selector $selector */
if ( preg_match('/\.dashicons-(?P<name>[\w\-]+):before/', $selector->getSelector(), $matches) ) {
$name = $matches['name'];
$char = null;
//The arrow icons aren't really suitable as menu icons.
if ( preg_match('/^(arrow)-/', $name) ) {
break;
}
//Some icons are duplicates of the "admin-" icons or just wouldn't look very good in a menu.
if ( array_key_exists($name, $ignoreIcons) ) {
break;
}
$rules = $block->getRules('content'); //Expect something like "content: '\f123'".
foreach($rules as $rule) {
/** @var Sabberworm\CSS\Rule\Rule $rule */
$value = $rule->getValue();
if ($value instanceof Sabberworm\CSS\Value\CSSString) {
//The parser defaults to UTF-8. Convert the char to a hexadecimal escape code
//so we don't have to worry about our CSS charset.
$char = ltrim(bin2hex(iconv('UTF-8', 'UCS-4', $value->getString())), '0');
$icons[$name] = '\\' . $char;
}
}
if (isset($char) && ($name !== 'before')) {
$allDashiconDefinitions .= sprintf(
'%s { content: "\%s" !important; }',
implode(', ', $selectors),
$char
) . "\n";
}
break;
}
}
}
$dashiconComment = sprintf(
"/*\nThis file was automatically generated from /wp-includes/css/dashicons.css.\nLast update: %s\n*/",
gmdate('c')
);
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_file_put_contents -- Dev stuff; WP_DEBUG must be enabled to get here.
file_put_contents(
dirname(__FILE__) . '/../css/_dashicons.scss',
$dashiconComment . "\n" . $allDashiconDefinitions
);
?>
<div class="wrap">
<h2>Dashicons to Menu Icons</h2>
<style type="text/css" scoped="scoped">
.ame-debug-dashicon {
display: inline-block;
margin: 2px;
min-width: 180px;
}
</style>
<?php
ksort($icons);
$arrayDefinition = "array(\n";
$currentLine = "\t";
foreach($icons as $name => $character) {
//Output each icon for visual verification.
printf(
'<div class="ame-debug-dashicon"><div class="dashicons dashicons-%1$s"></div> %1$s</div>',
esc_html($name)
);
//Wrap the array definition at about 80 characters for legibility.
$item = "'" . $name . "', ";
if ( strlen($currentLine . $item) > 80 ) {
$arrayDefinition .= $currentLine . "\n";
$currentLine = "\t";
}
$currentLine .= $item;
}
if (strlen($currentLine) > 1) {
$arrayDefinition .= $currentLine . "\n";
}
$arrayDefinition .= ')';
echo '<div class="clear"></div><br>';
echo '<textarea cols="100" rows="20">', esc_textarea($arrayDefinition), '</textarea>';
echo '</div>';