-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBranding.php
185 lines (153 loc) · 5.42 KB
/
Branding.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace App;
class Branding
{
private static ?self $instance = null;
public static array $config = [
'link' => 'https://think.studio',
'logo_url' => 'https://raw.githubusercontent.com/dev-think-one/branding/main/assets/studio/logo.png',
'logo_full_url' => 'https://raw.githubusercontent.com/dev-think-one/branding/main/assets/studio/logo-full.png',
'logo_full_svg_url' => 'https://raw.githubusercontent.com/dev-think-one/branding/main/assets/studio/logo-full.svg',
'screenshot_url' => 'https://raw.githubusercontent.com/dev-think-one/branding/main/assets/studio/screenshot.png',
];
private function __construct()
{
}
public static function instance(): static
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}
public static function init(): static
{
$instance = static::instance();
$instance->apply();
return $instance;
}
public static function config(?string $key = null, mixed $default = null): mixed
{
if (is_null($key)) {
return static::$config;
}
if (isset(static::$config[$key])) {
return static::$config[$key];
}
return $default;
}
protected function apply(): void
{
/* Login screen */
add_action('login_head', function () {
echo $this->customLogoStyles();
});
add_filter('login_message', [$this, 'loginMessage']);
add_filter('login_headerurl', function () {
return static::config('link');
});
/* Admin dashboard */
add_filter('admin_footer_text', function () {
echo $this->adminFooterText();
});
add_filter('admin_head', function () {
echo $this->adminStyles();
});
/* Site front */
add_filter('wp_head', function () {
echo $this->siteHeads();
});
if (get_option('branding_reading_setting_field')) {
add_filter('wp_footer', function () {
echo $this->maintainerBadge();
});
}
add_action('admin_init', [$this, 'frontendSettings']);
}
public function requestIpAddr(): ?string
{
if (!empty($_SERVER['HTTP_CLIENT_IP'] ?? null)) {
// IP from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'] ?? null)) {
// IP pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'] ?? null;
}
return $ip;
}
public function loginMessage($message): string
{
$text = sprintf(__('System detects your IP as: "%s"'), $this->requestIpAddr());
return ($message ? $message . "<br>" : '') . "<p style='text-align: center;'>" . $text . "</p>";
}
public function customLogoStyles(): string
{
return '<style>h1 a { background-image:url("' . static::config('logo_url') . '") !important; background-position: bottom !important; }</style>';
}
public function adminFooterText(): string
{
return sprintf(
__('CMS maintained by <a href="%s"><img style="%s" src="%s" alt=""></a>'),
static::config('link'),
'width: 119px; transform: translateY(2px)',
static::config('logo_full_url')
);
}
public function adminStyles(): string
{
$styles = '<style>li#wp-admin-bar-wp-logo{ display:none !important; }</style>';
if ($screenshotUrl = static::config('screenshot_url')) {
$styles .= '<style>
.theme-browser .theme .theme-screenshot,
.theme-overlay .screenshot {
background-size: cover;
background-image: url(' . $screenshotUrl . ') !important;
}
</style>';
}
return $styles;
}
public function siteHeads(): string
{
$comment = sprintf(
__('Site powered by Think Studio %s'),
static::config('link'),
);
return '<!-- ' . $comment . ' -->';
}
public function maintainerBadge(): string
{
return '
<a target="_blank"
href="'.static::config('link').'"
style="display: block; position: fixed; bottom: 36px; right: -44px; z-index: 99999; background-color: '.static::config('badge_bg', 'white').'; padding: 5px 30px; transform: rotate(-45deg);"
>
<img src="'.static::config('logo_full_svg_url').'" width="120" height="12">
</a>
';
}
public function frontendSettings(): void
{
register_setting('reading', 'branding_reading_setting_field');
add_settings_section(
'frontend_settings_section',
_('Maintainer branding'),
null,
'reading',
[]
);
add_settings_field(
'branding_reading_setting_field',
'Display maintainer badge',
[$this, 'frontendSettingEnableBadge'],
'reading',
'frontend_settings_section'
);
}
public function frontendSettingEnableBadge(): void
{
echo '<input type="checkbox" value="1" name="branding_reading_setting_field" ' . checked(get_option('branding_reading_setting_field'), true, false) . '" />';
}
}