-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplugin.php
159 lines (136 loc) · 3.92 KB
/
plugin.php
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* Plugin Name: Modern Login
* Plugin URI: https://github.com/log1x/modern-login
* Description: A whitelabeled and modernized wp-login.php
* Version: 1.0.7
* Author: Brandon Nifong
* Author URI: https://github.com/log1x
* Licence: MIT
*/
add_action('init', new class
{
/**
* The plugin URI.
*
* @var string
*/
protected $uri;
/**
* The plugin path.
*
* @var string
*/
protected $path;
/**
* The login color palette.
*
* @var array
*/
protected $colors = [
'brand' => '#0073aa',
'trim' => '#181818',
'trim-alt' => '#282828',
];
/**
* Invoke the plugin.
*
* @return void
*/
public function __invoke()
{
$this->uri = plugin_dir_url(__FILE__) . 'public';
$this->path = plugin_dir_path(__FILE__) . 'public';
$this->colors = array_merge(
$this->colors,
apply_filters('login_color_palette', $this->colors)
);
foreach ($this->colors as $label => $color) {
$this->colors[$label . '-invert'] = $this->invert($color);
}
$this->injectColors();
$this->enqueue();
}
/**
* Enqueue the login scripts.
*
* @return void
*/
public function enqueue()
{
add_action('login_enqueue_scripts', function () {
wp_enqueue_style('modern-login/login.css', $this->asset('/css/login.css'), false, null);
}, 100);
}
/**
* Resolve the URI for an asset using the manifest.
*
* @return string
*/
public function asset($asset = '', $manifest = 'mix-manifest.json')
{
if (! file_exists($manifest = $this->path . $manifest)) {
return $this->uri . $asset;
}
$manifest = json_decode(file_get_contents($manifest), true);
return $this->uri . ($manifest[$asset] ?? $asset);
}
/**
* Inject the color properties into the login head.
*
* @param array $colors
* @return string
*/
public function injectColors($colors = [])
{
if (! is_array($this->colors)) {
return;
}
foreach ($this->colors as $label => $value) {
$colors[] = "--login-{$label}: {$value};";
}
$this->colors = implode(' ', $colors);
add_filter('login_head', function () {
echo "<style>:root { {$this->colors} }</style>", PHP_EOL;
});
}
/**
* Invert a hexidecimal color to black or white depending on the luminance.
*
* The final value is compared to the following formula:
* ↪ sqrt(1.05 * 0.05) - 0.05 = 0.17912878474779
*
* @param string $hex
* @return string
*/
public function invert($hex)
{
$expression = [
3 => '/^([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])$/',
4 => '/^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])$/',
6 => '/^([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/',
7 => '/^#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/',
];
$length = strlen($hex);
$regex = $expression[$length] ?? false;
$match = [];
if (! $regex || preg_match($regex, $hex, $match) !== 1) {
return '#fff';
}
$rgb = $length > 4 ? [
(int) hexdec($match[1]),
(int) hexdec($match[2]),
(int) hexdec($match[3]),
] : [
(int) hexdec($match[1] . $match[1]),
(int) hexdec($match[2] . $match[2]),
(int) hexdec($match[3] . $match[3]),
];
foreach ($rgb as $i => $channel) {
$coef = $channel / 255;
$levels[$i] = $coef <= 0.03928 ? $coef / 12.92 : (($coef + 0.055) / 1.055) ** 2.4;
}
$levels = 0.2126 * $levels[0] + 0.7152 * $levels[1] + 0.0722 * $levels[2];
return $levels > 0.17912878474779 ? '#111' : '#fff';
}
});