-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadmin.php
369 lines (344 loc) · 11.5 KB
/
admin.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/*
* This file is part of DateProgress by valo.media.
*
* DateProgress is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* DateProgress is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with DateProgress. If not, see
* <https://www.gnu.org/licenses/>.
*/
/*
Backoffice functionality for date-progress.
This file implements all the functionality available in the admin panel.
*/
/*
* Settings
*/
/*
* Configure the settings for the plugin.
*
* This will add all settings for date-progress, along with registering the necessary callbacks. Currently, there is
* only one setting, which is the license key.
*/
function date_progress_settings_init()
{
register_setting('date_progress', 'date_progress_license');
add_settings_section(
'date_progress_settings_section',
'DateProgress Settings',
'date_progress_settings_section_callback',
'date_progress'
);
add_settings_field(
'date_progress_license_field',
'DateProgress License',
'date_progress_license_field_callback',
'date_progress',
'date_progress_settings_section'
);
}
/*
* Callback for generating the settings section.
*
* This will output the markup for the additional content (other than the heading and the actual settings) of the
* settings section for date progress. Currently, this is either a link to our shop, if the user hasn't yet entered
* a license key, or information about whether the license key is valid otherwise.
*/
function date_progress_settings_section_callback()
{
if (get_option( 'date_progress_license' )) {
echo date_progress_check_license()
? '<p>Your license key is valid! You are good to go :-)</p>'
: '
<p>
Your license key seems to be invalid :-(
</p>
<p>
Please <a href="https://valo.media/en-us/contact">contact us</a>, if you believe this is a mistake.
</p>';
} else {
echo '
<p>
DateProgress is a paid plugin. You can buy a license
<a href="https://shop.valo.media/l/dateprogress">here</a>.
</p>';
}
}
/*
* Callback for generating the license field.
*
* This will output the markup for the input field for the license key.
*/
function date_progress_license_field_callback()
{
$setting = get_option('date_progress_license');
$value = $setting ? esc_attr($setting) : '';
echo "<input type=\"text\" name=\"date_progress_license\" value=\"{$value}\">";
}
/*
* Callback for generating the shortcode generator.
*
* This will output the markup for the shortcode generator.
*/
function date_progress_shortcode_generator_callback()
{
echo '
<p>
Use this form to generate your shortcode. For detailed instructions, see
<a href="https://valo.media/en-us/dateprogress">here</a>.
</p>
<form id="date_progress_shortcode_generator">
<table class="form-table" role="presentation">
<tbody>
<tr>
<th scope="row">
<label for="date_progress_label" class="form-label">Label</label>
</th>
<td>
<input
type="text"
name="date_progress_label"
id="date_progress_label"
placeholder="{$remaining} of {$total} days remaining">
</td>
</tr>
<tr>
<th scope="row">
<label for="date_progress_format">Format</label>
</th>
<td>
<input type="text" name="date_progress_format" id="date_progress_format" placeholder="%a">
</td>
</tr>
<tr>
<th scope="row">
<label for="date_progress_color">Color</label>
</th>
<td>
<input type="color" name="date_progress_color" id="date_progress_color" value="#007bff">
</td>
<td>
<input type="checkbox" name="date_progress_striped" id="date_progress_striped">
<label for="date_progress_striped">Striped</label>
</td>
<td>
<input type="checkbox" name="date_progress_animated" id="date_progress_animated">
<label for="date_progress_animated">Animated</label>
</td>
</tr>
<tr>
<th scope="row">
<label for="date_progress_start">Start date</label>
</th>
<td>
<input type="date" name="date_progress_start" id="date_progress_start">
</td>
</tr>
<tr>
<th scope="row">
<input
type="radio"
id="date_progress_radio_end"
name="date_progress_radio"
value="end"
checked>
<label for="date_progress_radio_end">End date</label>
</th>
<td>
<input type="date" name="date_progress_end" id="date_progress_end">
</td>
</tr>
<tr>
<th scope="row">
<input
type="radio"
id="date_progress_radio_duration"
name="date_progress_radio"
value="duration">
<label for="date_progress_radio_duration">Duration</label>
</th>
<td>
<input
type="text"
name="date_progress_duration"
id="date_progress_duration"
placeholder="1 week + 3 days">
</td>
<td>
<input
type="checkbox"
name="date_progress_repeating"
id="date_progress_repeating"
value="date_progress_repeating">
<label for="date_progress_repeating">Repeating</label>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<input
type="submit"
id="date_progress_generate_shortcode"
class="button button-primary"
value="Generate Shortcode">
</p>
<input type="text" name="date_progress_shortcode" id="date_progress_shortcode" placeholder="Shortcode">
</form>';
}
/*
* Callback for generating the options page.
*
* This function will generate the options page and add the necessary stylesheet and script for the shortcode
* generator to work.
*/
function date_progress_options_page_html()
{
global $DATE_PROGRESS_PLUGIN_LICENSE_TRANSIENT;
wp_enqueue_style('date_progress_admin_style', plugins_url('style.css', __FILE__));
wp_enqueue_script('date_progress_admin_script', plugins_url('script.js', __FILE__));
if (isset($_POST['date_progress_license'])) {
add_option('date_progress_license', $_POST['date_progress_license'])
|| update_option('date_progress_license', $_POST['date_progress_license']);
delete_transient($DATE_PROGRESS_PLUGIN_LICENSE_TRANSIENT);
}
echo '<div class="wrap">';
echo '<h1>' . esc_html(get_admin_page_title()) . '</h1>';
if (current_user_can('manage_options')) {
/** @noinspection HtmlUnknownTarget */
echo '<form action="tools.php?page=date_progress" method="post">';
do_settings_sections( 'date_progress' );
submit_button( 'Save Settings' );
echo '</form>';
}
echo '<h2>Shortcode Generator</h2>';
date_progress_shortcode_generator_callback();
echo '</div>';
}
/*
* Register the options page.
*
* This will register the actual options page for date-progress with WordPress.
*/
function date_progress_options_page()
{
add_submenu_page(
'tools.php',
'DateProgress Options',
'DateProgress',
'manage_options',
'date_progress',
'date_progress_options_page_html'
);
}
add_action('admin_init', 'date_progress_settings_init');
add_action('admin_menu', 'date_progress_options_page');
/*
* Auto-Update
*/
/*
* Fetch the plugin information for the current version.
*
* This will check for the current version of the plugin, as well as fetching some metadata for display in the admin
* panel. The results will be cached for one day.
*/
function date_progress_plugin_information() {
global $DATE_PROGRESS_PLUGIN_INFORMATION_URL;
global $DATE_PROGRESS_PLUGIN_INFORMATION_TRANSIENT;
$transient = get_transient($DATE_PROGRESS_PLUGIN_INFORMATION_TRANSIENT);
if ($transient) {
$result = json_decode($transient);
} else {
$remote = wp_remote_get($DATE_PROGRESS_PLUGIN_INFORMATION_URL);
if (is_wp_error($remote) || wp_remote_retrieve_response_code($remote) !== 200) { return false; }
$body = wp_remote_retrieve_body($remote);
if (empty($body)) { return false; }
set_transient($DATE_PROGRESS_PLUGIN_INFORMATION_TRANSIENT, $body, DAY_IN_SECONDS);
$result = json_decode($body);
}
return $result;
}
/*
* Hook for the plugins API.
*
* This as a hook for the plugins API, that will fetch the plugin information about the current version from
* cdn.valo.media instead of wordpress.org when querying for the plugin information for date-progress.
*/
function date_progress_plugins_api($result, $action, $args)
{
// Only proceed if getting plugin information for this plugin.
if ('plugin_information' !== $action || plugin_basename(__DIR__) !== $args->slug) { return $result; }
// Check for updates.
$plugin_information = date_progress_plugin_information();
if ($plugin_information) {
// Need to rebuild the object, since it needs to contain arrays where jsons_decode creates objects. Can't
// make use of the spread operator, since this would break PHP 7.3 compatibility.
return (object) array(
'author' => $plugin_information->author,
'banners' => array(
'high' => $plugin_information->banners->high,
'low' => $plugin_information->banners->low
),
'download_link' => $plugin_information->download_link,
'name' => $plugin_information->name,
'sections' => array(
'description' => $plugin_information->sections->description,
'installation' => $plugin_information->sections->installation,
'screenshots' => $plugin_information->sections->screenshots
),
'slug' => $args->slug,
'requires' => $plugin_information->requires,
'requires_php' => $plugin_information->requires_php,
'tested' => $plugin_information->tested,
'trunk' => $plugin_information->download_link,
'version' => $plugin_information->version,
);
} else {
return $result;
}
}
/*
* Hook for updating date-progress.
*
* This is a hook for the update system, that will use cdn.valo.media instead of wordpress.org when checking for or
* performing updates for date-progress.
*/
function date_progress_update_plugins($transient) {
global $PLUGIN_FILE;
if (empty($transient->checked)) { return $transient; }
$plugin_information = date_progress_plugin_information();
if (
$plugin_information
&& version_compare($plugin_information->version, get_plugin_data($PLUGIN_FILE)['Version'], '>')
&& version_compare($plugin_information->requires, get_bloginfo('version'), '<=')
&& version_compare($plugin_information->requires_php, PHP_VERSION, '<')
) {
$transient->response[plugin_basename($PLUGIN_FILE)] = (object) array(
'slug' => plugin_basename(__DIR__),
'plugin' => plugin_basename($PLUGIN_FILE),
'new_version' => $plugin_information->version,
'tested' => $plugin_information->tested,
'package' => $plugin_information->download_link
);
}
return $transient;
}
/*
* Hook for upgrade completion.
*
* This is a hook for the plugin upgrade system, that will clear the cache after the plugin gets upgraded.
*/
function date_progress_upgrader_process_complete($upgrader, $hook_extra)
{
global $DATE_PROGRESS_PLUGIN_INFORMATION_TRANSIENT;
if ($hook_extra['action'] === 'update' && $hook_extra['type'] === 'plugin') {
delete_transient($DATE_PROGRESS_PLUGIN_INFORMATION_TRANSIENT);
}
}
add_filter('plugins_api', 'date_progress_plugins_api', 20, 3);
add_filter('site_transient_update_plugins', 'date_progress_update_plugins');
add_action('upgrader_process_complete', 'date_progress_upgrader_process_complete', 10, 2);