-
Notifications
You must be signed in to change notification settings - Fork 5
/
Settings.php
158 lines (132 loc) · 5.31 KB
/
Settings.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
<?php
namespace Iframely\UI;
use Iframely\Embed\Cache;
use Iframely\Options;
use Iframely\Plugin;
use Iframely\Reactivation;
class Settings
{
public const SAMPLE_URL = 'https://chrome.google.com/webstore/detail/oajehffbidgccdedglcogjoolbdmpjmm';
public static function run(): void
{
// Register menu
add_action('network_admin_menu', [self::class, 'register']);
add_action('admin_menu', [self::class, 'register']);
// Save settings
add_action('admin_init', [self::class, 'save']);
// Load assets
add_action('admin_enqueue_scripts', [self::class, 'enqueue']);
}
public static function register(): void
{
if (is_multisite()) {
add_submenu_page('settings.php', __('Iframely', 'iframely'), __('Iframely', 'iframely'), 'install_plugins', 'iframely', [self::class, 'render']);
} else {
add_options_page(__('Iframely', 'iframely'), __('Iframely', 'iframely'), 'manage_options', 'iframely', [self::class, 'render']);
}
}
public static function enqueue(): void
{
$screen = get_current_screen();
$screens = ['post', 'settings_page_iframely-network', 'settings_page_iframely'];
if (!($screen !== null && in_array($screen->id, $screens))) {
return;
}
wp_enqueue_style('iframely-admin', Plugin::asset('index.css'), [], IFRAMELY_VERSION);
}
public static function render(): void
{
if (!Plugin::isActivated() || Reactivation::isRequest() || Reactivation::inProgress()) {
Plugin::view('activation');
return;
}
Plugin::view('settings', self::data());
}
public static function save(): void
{
if (empty($_POST['iframely_nonce'])) {
return;
}
if (!wp_verify_nonce($_POST['iframely_nonce'], 'iframely_nonce')) {
wp_die('Your nonce could not be verified.');
}
if (is_multisite()) {
if (!current_user_can('install_plugins')) {
wp_die('You don’t have permission to access this resource.');
}
} else if (!current_user_can('manage_options')) {
wp_die('You don’t have permission to access this resource.');
}
if (Reactivation::isRequest()) {
Reactivation::start();
self::notify(__('Please enter new API key.', 'iframely'));
return;
}
if (Plugin::isActivated() && !Reactivation::inProgress()) {
self::update();
self::notify(__('Settings saved.', 'iframely'));
return;
}
self::activate();
}
public static function activate(): void
{
$key = strtolower(trim(sanitize_text_field($_POST['iframely_api_key'] ?? '')));
if (!preg_match('/^[a-z0-9]+$/', $key)) {
self::notify(sprintf(__('Invalid API key. Please check <a href="%s" target="_blank">your keys</a> and try again.', 'iframely'), Links::link('keys')), 'error');
return;
}
$origin = preg_replace('#^https?://#i', '', get_bloginfo('url'));
$query = http_build_query([
'api_key' => $key,
'url' => self::SAMPLE_URL,
'origin' => $origin,
]);
$url = IFRAMELY_API_ENDPOINT . '?' . $query;
$request = wp_remote_get($url);
if (is_wp_error($request)) {
self::notify(__('HTTP error', 'iframely'), 'error');
return;
}
$body = wp_remote_retrieve_body($request);
$data = json_decode($body, true, 512);
if (!empty($data['error'])) {
$message = __('Sorry, your API key can\'t be verified.', 'iframely');
if (!empty($data['status'])) {
$message = sprintf(__('Sorry, your API key can\'t be verified (error code %s). <a href="%s" target="_blank">Learn more</a>', 'iframely'), $data['status'], Links::link('/docs/result-codes'));
}
self::notify($message, 'error');
return;
}
Options::setApiKey($key);
if (Reactivation::inProgress()) {
Reactivation::end();
self::notify(__('API key has been updated.', 'iframely'), 'success');
return;
}
Plugin::view('partials/onboarding');
}
public static function update(): void
{
Options::setBuiltinsReplace((bool)($_POST['iframely_builtins_replace'] ?? false));
Options::setPreviewsEnhance((bool)($_POST['iframely_previews_enhance'] ?? false));
Options::setCacheRefresh((bool)($_POST['iframely_cache_refresh'] ?? false));
Options::setCacheTtl((int)($_POST['iframely_cache_ttl'] ?? 0));
Options::setApiParams(sanitize_text_field(stripcslashes($_POST['iframely_api_params'] ?? '')));
}
public static function notify(string $message = '', $status = 'success'): void
{
$action = is_multisite() ? 'network_admin_notices' : 'admin_notices';
add_action($action, function () use ($message, $status) {
Plugin::notice($message, $status);
});
}
public static function data(): array
{
$data = Options::collect();
$data['cache_ttl_presets'] = Cache::getTtlPresets();
$data['tab'] = $_GET['tab'] ?? '';
$data['action'] = $_GET['action'] ?? '';
return $data;
}
}