This repository has been archived by the owner on May 9, 2022. It is now read-only.
forked from jamiechong/revisionize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.php
569 lines (485 loc) · 17.6 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<?php
/*
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// TODO: Really need to tidy this file up and organize it better.
namespace Revisionize;
require_once 'addon.php';
add_action('init', __NAMESPACE__.'\\plugin_init');
add_action('admin_init', __NAMESPACE__.'\\settings_admin_init');
add_action('admin_menu', __NAMESPACE__.'\\settings_menu');
add_action('network_admin_menu', __NAMESPACE__.'\\network_settings_menu');
add_action('network_admin_edit_revisionize_network_settings', __NAMESPACE__.'\\network_update_settings');
add_filter('plugin_action_links_'.REVISIONIZE_BASE, __NAMESPACE__.'\\settings_link');
add_filter('network_admin_plugin_action_links_'.REVISIONIZE_BASE, __NAMESPACE__.'\\network_settings_link');
function plugin_init() {
load_addons();
check_for_addon_updates();
}
function settings_admin_init() {
if (is_on_settings_page()) {
set_setting('has_seen_settings', true);
} else if (get_setting('has_seen_settings', false) === false) {
add_action('admin_notices', __NAMESPACE__.'\\notify_new_settings');
}
if (is_on_network_settings_page() && isset($_GET['updated'])) {
add_action('network_admin_notices', __NAMESPACE__.'\\notify_updated_settings');
}
}
function settings_menu() {
add_submenu_page (
'options-general.php',
'Revisionize Settings',
'Revisionize',
'manage_options',
'revisionize',
__NAMESPACE__.'\\settings_page'
);
register_setting('revisionize', 'revisionize_settings', array(
"sanitize_callback" => __NAMESPACE__.'\\on_settings_saved'
));
setup_basic_settings();
if (!is_multisite()) {
setup_addon_settings();
}
}
function network_settings_menu() {
add_submenu_page (
'settings.php',
'Revisionize Network Settings',
'Revisionize',
'manage_network_options',
'revisionize',
__NAMESPACE__.'\\network_settings_page'
);
register_setting('revisionize_network', 'revisionize_network_settings', array(
"sanitize_callback" => __NAMESPACE__.'\\on_settings_saved'
));
setup_addon_settings('revisionize_network');
}
function network_update_settings() {
check_admin_referer('revisionize_network-options');
// save files.
on_settings_saved();
if (isset($_POST['revisionize_network_settings'])) {
update_site_option('revisionize_network_settings', $_POST['revisionize_network_settings']);
}
// need to schedule an admin notice.
wp_redirect(add_query_arg(array('page'=>'revisionize', 'updated'=>'true'), network_admin_url('settings.php')));
exit;
}
function settings_page() {
if (!current_user_can('manage_options')) {
echo 'Not Allowed.';
return;
}
?>
<div class="wrap">
<?php settings_css(); ?>
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form action="options.php" enctype="multipart/form-data" method="post" class="rvz-settings-form">
<?php
settings_fields('revisionize');
do_fields_section('revisionize_section_basic');
// settings from Addons
do_action('revisionize_settings_fields');
do_fields_section('revisionize_section_addons');
submit_button('Save Settings');
if (!is_multisite()) {
addons_html();
submit_button('Save Settings');
}
?>
</form>
</div>
<?php
}
function network_settings_page() {
if (!current_user_can('manage_network_options')) {
echo 'Not Allowed.';
return;
}
?>
<div class="wrap">
<?php settings_css(); ?>
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<p>Note that site specific settings for Revisionize can be found when viewing a site. Such as <a href="<?php echo admin_url('options-general.php?page=revisionize')?>">here</a>.</p>
<form action="edit.php?action=revisionize_network_settings" enctype="multipart/form-data" method="post" class="rvz-settings-form">
<?php
settings_fields('revisionize_network');
do_fields_section('revisionize_section_addons', 'revisionize_network');
submit_button('Save Settings');
addons_html();
submit_button('Save Settings');
?>
</form>
</div>
<?php
}
function do_fields_section($key, $group="revisionize") {
echo '<table class="form-table">';
do_settings_fields($group, $key);
echo '</table>';
}
function setup_basic_settings() {
add_settings_section('revisionize_section_basic', '', '__return_null', 'revisionize');
input_setting('checkbox', 'Keep Backup', 'keep_backup', "After publishing the revision, the previously live post will be kept around and marked as a backup revision of the new version.", true, 'revisionize_section_basic', 'revisionize_keep_original_on_publish', __NAMESPACE__.'\\filter_keep_backup');
input_setting('checkbox', 'Preserve Date', 'preserve_date', "The date of the original post will be maintained even if the revisionized post date changes. In particular, a scheduled revision won't modify the post date once it's published.", true, 'revisionize_section_basic', 'revisionize_preserve_post_date', __NAMESPACE__.'\\filter_preserve_date');
}
function setup_addon_settings($group="revisionize") {
add_settings_section('revisionize_section_addons', '', '__return_null', $group);
// These fields are displayed
add_settings_field('revisionize_addon_file', __('Upload Addon', REVISIONIZE_I18N_DOMAIN), __NAMESPACE__.'\\settings_addon_file_html', $group, 'revisionize_section_addons', array('label_for' => 'revisionize_addon_file'));
}
function settings_addon_file_html($args) {
$id = esc_attr($args['label_for']);
?>
<div>
<input id="<?php echo $id?>" type="file" name="revisionize_addon_file" style="width:320px" accept=".rvz"/>
<p>To install or update an addon, choose a <em>.rvz</em> file and click <em>Save Settings</em></p>
</div>
<?php
}
function addons_html() {
$hasUpdates = false;
?>
<h1>Revisionize Addons</h1>
<p>Improve the free Revisionize plugin with these official addons.<br/>Visit <a href="https://revisionize.pro" target="_blank">revisionize.pro</a> for more info.</p>
<div class="rvz-addons rvz-cf">
<?php foreach (get_available_addons() as $addon) {
addon_html($addon);
$hasUpdates = $hasUpdates || $addon["update_available"];
}?>
</div>
<?php if ($hasUpdates): ?>
<p>* To install an addon update, visit <a href="https://revisionize.pro/account/" target="_blank">https://revisionize.pro/account/</a> to login to your account.
<br/>Find the relevant purchase confirmation and download the updated <em>.rvz</em> file.
<br/>Come back here and upload the addon.</p>
<?php
endif;
}
function addon_html($addon) {
$id = $addon['id'];
$active = "addon_${id}_active";
$remove = "addon_${id}_delete";
$active_checked = is_addon_active($id) ? 'checked' : '';
$group = is_multisite() ? "revisionize_network_settings" : "revisionize_settings";
?>
<div class="rvz-addon-col">
<div class="rvz-addon<?php if ($addon['installed']) echo " rvz-installed" ?>">
<h3><a href="<?php echo $addon['url']?>" target="_blank"><?php echo $addon['name'];?></a></h3>
<?php echo $addon['description']; ?>
<div class="rvz-meta rvz-cf">
<?php if ($addon['installed']): ?>
<label>Installed: <?php echo $addon['installed']?></label>
<label>
<input type="hidden" name="<?php echo $group?>[_<?php echo $active?>_set]" value="1"/>
<input type="checkbox" name="<?php echo $group?>[<?php echo $active?>]" <?php echo $active_checked?> /> Active
</label>
<label>
<input type="hidden" name="<?php echo $group?>[_<?php echo $remove?>_set]" value="1"/>
<input type="checkbox" name="<?php echo $group?>[<?php echo $remove?>]" /> Delete
</label>
<?php if ($addon["update_available"]): ?>
<div class="rvz-update-available rvz-cf">
<a class="rvz-button button" href="https://revisionize.pro/account/" target="_blank">Update Available: <?php echo $addon['version']?></a>
</div>
<?php endif; ?>
<?php else: ?>
<a class="rvz-button button" href="<?php echo $addon['url']?>" target="_blank"><?php echo $addon['price']?> - <?php echo $addon['button']?></a>
<?php endif; ?>
</div>
</div>
</div>
<?php
}
// access settings
function get_setting($key, $default='', $multisite=false) {
$settings = $multisite ? get_site_option('revisionize_network_settings') : get_option('revisionize_settings');
return !empty($settings[$key]) ? $settings[$key] : $default;
}
function set_setting($key, $value) {
$settings = get_option('revisionize_settings');
$settings[$key] = $value;
update_option('revisionize_settings', $settings);
}
function remove_setting($keys, $multisite=false) {
$settings = $multisite ? get_site_option('revisionize_network_settings') : get_option('revisionize_settings');
if (!is_array($keys)) {
$keys = array($keys);
}
foreach ($keys as $key) {
unset($settings[$key]);
}
if ($multisite) {
update_site_option('revisionize_network_settings', $settings);
} else {
update_option('revisionize_settings', $settings);
}
}
function is_on_settings_page() {
global $pagenow;
return $pagenow == 'options-general.php' && $_GET['page'] == 'revisionize';
}
function is_on_network_settings_page() {
global $pagenow;
return $pagenow == 'settings.php' && $_GET['page'] == 'revisionize';
}
function on_settings_saved($settings=null) {
if (!empty($_FILES['revisionize_addon_file']['tmp_name'])) {
install_addon($_FILES['revisionize_addon_file']['tmp_name']);
}
return $settings;
}
function install_addon($filename) {
// make sure the directory exists
$target_path = get_addons_root();
wp_mkdir_p($target_path);
$data = file_get_contents($filename);
$data = json_decode(base64_decode($data), true);
// TODO: check to see if addon already installed and if this version is newer. Maybe send warning if not (downgrading)
file_put_contents($target_path.'/'.$data['name'].'.php', base64_decode($data['code']));
$installed = get_installed_addons();
$installed[] = $data['name'];
set_installed_addons($installed);
}
function uninstall_addon($id, $file) {
remove_setting(array(
"addon_${id}_active",
"addon_${id}_delete",
"_addon_${id}_active_set",
"_addon_${id}_delete_set",
), is_multisite());
unlink($file);
$installed = get_installed_addons();
if (($key = array_search($id, $installed)) !== false) {
array_splice($installed, $key, 1);
}
set_installed_addons($installed);
}
function get_available_addons() {
$registered = apply_filters('revisionize_registered_addons', array());
$addons = get_transient('revisionize_available_addons');
if ($addons === false) {
$payload = json_decode(file_get_contents("https://revisionize.pro/rvz-addons/"), true);
$addons = $payload['addons'];
set_transient('revisionize_available_addons', $addons, 6 * 60 * 60); // cache for 6 hours
}
foreach ($addons as &$addon) {
$addon["installed"] = array_key_exists($addon["id"], $registered) ? $registered[$addon["id"]] : false;
$addon["update_available"] = $addon["installed"] && version_compare($addon["version"], $addon["installed"]) > 0;
}
return $addons;
}
function check_for_addon_updates() {
$addons = get_available_addons();
foreach ($addons as $addon) {
if ($addon["update_available"]) {
add_action(is_multisite() ? 'network_admin_notices' : 'admin_notices', __NAMESPACE__.'\\notify_needs_update');
}
}
}
function get_installed_addons() {
$addons = is_multisite() ? get_site_option('revisionize_installed_addons', array()) : get_option('revisionize_installed_addons', array());
return $addons ? $addons : array();
}
function get_addons_root() {
return apply_filters('revisionize_addons_root', REVISIONIZE_ROOT.'/addons');
}
function set_installed_addons($installed) {
if (is_multisite()) {
update_site_option('revisionize_installed_addons', array_unique($installed));
} else {
update_option('revisionize_installed_addons', array_unique($installed));
}
}
function load_addons() {
$addons = get_installed_addons();
foreach ($addons as $id) {
$file = get_addons_root().'/'.$id.'.php';
if (file_exists($file)) {
if (is_addon_pending_delete($id)) {
uninstall_addon($id, $file);
} else {
require_once $file;
\RevisionizeAddon::create($id);
}
}
}
do_action('revisionize_addons_loaded');
}
function is_addon_active($id) {
return is_checkbox_checked('addon_'.$id.'_active', true, is_multisite());
}
function is_addon_pending_delete($id) {
return is_checkbox_checked('addon_'.$id.'_delete', false, is_multisite());
}
function filter_keep_backup($b) {
return is_checkbox_checked('keep_backup', $b);
}
function filter_preserve_date($b) {
return is_checkbox_checked('preserve_date', $b);
}
// basic inputs for now
// $type: text|email|number|checkbox
function input_setting($type, $name, $key, $description, $default, $section, $filter=null, $handler=null) {
add_settings_field('revisionize_setting_'.$key, $name, __NAMESPACE__.'\\field_input', 'revisionize', $section, array(
'type' => $type,
'label_for' => 'revisionize_setting_'.$key,
'key' => $key,
'description' => $description,
'default' => $default
));
if ($filter && $handler) {
add_filter($filter, $handler);
}
}
function field_input($args) {
$type = $args['type'];
$id = esc_attr($args['label_for']);
$key = esc_attr($args['key']);
$value = '';
if ($type == 'checkbox') {
if (is_checkbox_checked($key, $args['default'])) {
$value = 'checked';
}
} else {
$value = 'value="'.get_setting($key, $args['default']).'"';
}
?>
<div>
<?php if ($type=="checkbox"): ?>
<input type="hidden" name="revisionize_settings[_<?php echo $key?>_set]" value="1"/>
<?php endif; ?>
<label>
<input id="<?php echo $id?>" type="<?php echo $type?>" name="revisionize_settings[<?php echo $key?>]" <?php echo $value?>/>
<?php echo $args['description']?>
</label>
</div>
<?php
}
function is_checkbox_checked($key, $default, $multisite=false) {
return is_checkbox_set($key, $multisite) ? is_checkbox_on($key, $multisite) : $default;
}
function is_checkbox_on($key, $multisite=false) {
return get_setting($key, '', $multisite) == "on";
}
function is_checkbox_set($key, $multisite=false) {
return get_setting('_'.$key.'_set', '', $multisite) == "1";
}
function notify_new_settings() {
$notice = '<strong>Revisionize</strong> has a new settings panel. <strong><a href="'.admin_url('options-general.php?page=revisionize').'">Check it out!</a></strong>';
echo '<div class="notice notice-info is-dismissible"><p>'.$notice.'</p></div>';
}
function notify_updated_settings() {
echo '<div class="notice updated is-dismissible"><p><strong>Settings saved.</strong></p></div>';
}
function notify_needs_update() {
if (!is_on_settings_page() && !is_on_network_settings_page()) {
$url = is_multisite() ? network_admin_url('settings.php?page=revisionize') : admin_url('options-general.php?page=revisionize');
echo '<div class="notice updated is-dismissible"><p>Revisionize has 1 or more updates available for your installed addons. <a href="'.$url.'">View settings</a> for details.</p></div>';
}
}
function settings_css() {
?>
<style type="text/css">
.rvz-cf:after {
content: "";
display: table;
clear: both;
}
.rvz-settings-form {
margin-top: 15px;
}
.rvz-settings-form .form-table {
margin-top: 0;
}
.rvz-settings-form .form-table th, .rvz-settings-form .form-table td {
padding-top: 12px;
padding-bottom: 12px;
}
.rvz-settings-form .form-table p {
margin-top: 0;
}
.rvz-addons { margin-top: 30px; }
.rvz-addons * { box-sizing: border-box; }
.rvz-addon-col {
float: left;
width: 100%;
padding: 0;
}
@media (min-width: 783px) {
.rvz-addon-col {
padding-right: 25px;
width: 50%;
}
}
@media (min-width: 1366px) {
.rvz-addon-col {
width: 33.3333%;
}
}
@media (min-width: 1600px) {
.rvz-addon-col {
width: 25%;
}
}
.rvz-addon {
background-color: #e0e0e0;
border-radius: 4px;
padding: 15px 15px 55px;
min-height: 300px;
position: relative;
margin-bottom: 20px;
}
.rvz-addon h3 {
margin-top: 0;
line-height: 30px;
text-transform: uppercase;
width: 100%;
}
.rvz-addon ul {
list-style: disc;
padding-bottom: 15px;
padding-left: 25px;
}
.rvz-addon .rvz-meta {
position: absolute;
bottom: 0;
left: 0;
padding: 15px;
width: 100%;
text-align: right;
}
.rvz-addon .rvz-meta label {
line-height: 22px;
margin-left: 15px;
}
.rvz-addon .rvz-meta label:first-child {
margin-left: 0;
float: left;
}
.rvz-update-available {
clear: both;
margin-top: 8px;
text-align: center;
}
</style>
<?php
}
function settings_link($links) {
return array_merge($links, array('<a href="'.admin_url('options-general.php?page=revisionize').'">Settings</a>'));
}
function network_settings_link($links) {
return array_merge($links, array('<a href="'.network_admin_url('settings.php?page=revisionize').'">Settings</a>'));
}