-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoolbox.php
768 lines (644 loc) · 15 KB
/
toolbox.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
<?php
/*
Plugin Name: Toolbox
Plugin URI: http://playground.ebiene.de/toolbox-wordpress-plugin/
Description: Ersetzt oder erweitert die <em>functions.php</em> im Theme, indem Code-Schnipsel als Toolbox-Module angelegt und direkt im Plugin verwaltet werden.
Author: Sergej Müller
Author URI: http://wpcoder.de
License: GPLv2 or later
Version: 0.1
*/
/*
Copyright (C) 2011-2014 Sergej Müller
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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* Sicherheitsabfrage */
if ( ! class_exists('WP') ) {
die();
}
/**
* Toolbox
*/
final class Toolbox {
/* Init */
private static $plugin_path;
private static $modules_path;
/**
* Konstruktor der Klasse
*
* @since 0.1
* @change 0.1
*/
public static function init()
{
/* Optionen */
$options = get_option('toolbox');
/* Sicherheit */
if ( ! empty($options['secure']) ) {
if ( (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) or (defined('DOING_CRON') && DOING_CRON) or (defined('DOING_AJAX') && DOING_AJAX) or (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) ) {
return;
}
}
/* Init */
self::$plugin_path = plugin_basename(__FILE__);
self::$modules_path = plugin_dir_path(__FILE__). 'modules/';
/* Module einbinden */
self::_require_modules($options);
/* Backend */
if ( is_admin() ) {
add_action(
'wpmu_new_blog',
array(
__CLASS__,
'install_later'
)
);
add_action(
'delete_blog',
array(
__CLASS__,
'uninstall_later'
)
);
add_action(
'admin_init',
array(
__CLASS__,
'register_settings'
)
);
add_action(
'admin_menu',
array(
__CLASS__,
'add_page'
)
);
add_filter(
'plugin_row_meta',
array(
__CLASS__,
'row_meta'
),
10,
2
);
add_filter(
'plugin_action_links_' .self::$plugin_path,
array(
__CLASS__,
'action_links'
)
);
}
}
/**
* Installation des Plugins auch für MU-Blogs
*
* @since 0.1
* @change 0.1
*/
public static function install()
{
/* Multisite & Network */
if ( is_multisite() && ! empty($_GET['networkwide']) ) {
/* Blog-IDs */
$ids = self::_get_blog_ids();
/* Loopen */
foreach ($ids as $id) {
switch_to_blog( (int)$id );
self::_install_backend();
}
/* Wechsel zurück */
restore_current_blog();
} else {
self::_install_backend();
}
}
/**
* Installation des Plugins bei einem neuen MU-Blog
*
* @since 0.1
* @change 0.1
*/
public static function install_later($id)
{
/* Kein Netzwerk-Plugin */
if ( ! is_plugin_active_for_network(self::$plugin_path) ) {
return;
}
/* Wechsel */
switch_to_blog( (int)$id );
/* Installieren */
self::_install_backend();
/* Wechsel zurück */
restore_current_blog();
}
/**
* Eigentliche Installation der Option und der Tabelle
*
* @since 0.1
* @change 0.1
*/
private static function _install_backend()
{
add_option(
'toolbox',
array(
'modules' => array(),
'secure' => 1
)
);
}
/**
* Uninstallation des Plugins pro MU-Blog
*
* @since 0.1
* @change 0.1
*/
public static function uninstall()
{
/* Global */
global $wpdb;
/* Multisite & Network */
if ( is_multisite() && ! empty($_GET['networkwide']) ) {
/* Alter Blog */
$old = $wpdb->blogid;
/* Blog-IDs */
$ids = self::_get_blog_ids();
/* Loopen */
foreach ($ids as $id) {
switch_to_blog($id);
self::_uninstall_backend();
}
/* Wechsel zurück */
switch_to_blog($old);
} else {
self::_uninstall_backend();
}
}
/**
* Uninstallation des Plugins bei MU & Network-Plugin
*
* @since 0.1
* @change 0.1
*/
public static function uninstall_later($id)
{
/* Kein Netzwerk-Plugin */
if ( ! is_plugin_active_for_network(self::$plugin_path) ) {
return;
}
/* Wechsel */
switch_to_blog( (int)$id );
/* Installieren */
self::_uninstall_backend();
/* Wechsel zurück */
restore_current_blog();
}
/**
* Eigentliche Deinstallation des Plugins
*
* @since 0.1
* @change 0.1
*/
private static function _uninstall_backend()
{
delete_option('toolbox');
}
/**
* Rückgabe der IDs installierter Blogs
*
* @since 0.1
* @change 0.1
*
* @return array Blog-IDs
*/
private static function _get_blog_ids()
{
/* Global */
global $wpdb;
return $wpdb->get_col(
$wpdb->prepare("SELECT blog_id FROM `$wpdb->blogs`")
);
}
/**
* Hinzufügen der Action-Links (Einstellungen links)
*
* @since 0.1
* @change 0.1
*/
public static function action_links($data)
{
/* Rechte */
if ( ! current_user_can('manage_options') ) {
return $data;
}
return array_merge(
$data,
array(
sprintf(
'<a href="%s">%s</a>',
add_query_arg(
array(
'page' => 'toolbox'
),
admin_url('options-general.php')
),
__('Settings')
)
)
);
}
/**
* Meta-Links zum Plugin
*
* @since 0.1
* @change 0.1
*
* @param array $links Bereits vorhandene Links
* @param string $page Aktuelle Seite
* @return array $data Modifizierte Links
*/
public static function row_meta($links, $page)
{
/* Rechte */
if ( $page != self::$plugin_path ) {
return $links;
}
return array_merge(
$links,
array(
'<a href="https://flattr.com/t/457444" target="_blank">Flattr</a>',
'<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=ZAQUT9RLPW8QN" target="_blank">PayPal</a>'
)
);
}
/**
* Filtert Module je nach Bereich
*
* @since 0.1
* @change 0.1
*
* @param array $modules Alle Module
* @return array $modules Gefilterte Module
*/
private static function _filter_modules($modules)
{
if ( is_admin() ) {
return array_filter(
$modules,
create_function('$o', 'return $o == 1 or $o == 3;')
);
} else {
return array_filter(
$modules,
create_function('$o', 'return $o == 2 or $o == 3;')
);
}
}
/**
* Lädt Module je nach Bereich nach
*
* @since 0.1
* @change 0.1
*
* @param array $options Plugin-Optionen
*/
private static function _require_modules($options)
{
/* Leer */
if ( empty($options['modules']) ) {
return;
}
/* Module filtern */
if ( ! $modules = self::_filter_modules($options['modules']) ) {
return;
}
/* Module loopen */
foreach ( $modules as $module => $time) {
$file = self::$modules_path . self::_clean_module($module);
if ( is_readable($file) ) {
require_once($file);
} else {
unset($options['modules'][$module]);
}
}
/* Update */
if ( count($modules) != count($options['modules']) ) {
update_option('toolbox', $options);
}
}
/**
* Bereinigt den Modulnamen
*
* @since 0.1
* @change 0.1
*
* @param string $str Unbehandelter Name
* @return string $str Bereinigter Name
*/
private static function _clean_module($str)
{
return (string)preg_replace('/[^a-z0-9-_\.]/i', '', $str );
}
/**
* Gibt verfügbare Module zurück
*
* @since 0.1
* @change 0.1
*
* @return array $modules Array mit gefundenen Modulen
*/
private static function _list_modules()
{
/* Auslesen */
$files = glob(self::$modules_path. '*.php', GLOB_NOSORT|GLOB_ERR);
/* Leer */
if ( empty($files) ) {
return false;
}
/* Init */
$modules = array();
/* Loopen */
foreach ($files as $file) {
/* Modul */
$module = str_replace(self::$modules_path, '', $file);
/* Sicherheitsabgleich */
if ( $module == self::_clean_module($module) ) {
/* Metadaten */
$meta = self::_read_metadata($file);
$modules[] = array(
'ident' => $module,
'name' => ( empty($meta['name']) ? $module : $meta['name'] ),
'desc' => ( empty($meta['desc']) ? '' : $meta['desc'] ),
'link' => ( empty($meta['link']) ? '' : '[<a href="' .esc_url($meta['link']). '" target="_blank">?</a>]' )
);
}
}
return $modules;
}
/**
* Liest Metadaten eines Moduls ein
*
* @since 0.1
* @change 0.1
*
* @param string $file Moduldatei samt Pfad
* @return string Ermittelter Modulname
*/
private static function _read_metadata($file)
{
return get_file_data( $file, array('name' => 'Module Name', 'desc' => 'Description', 'link' => 'Module URI') );
}
/**
* Bereitet Module zum Speichern vor
*
* @since 0.1
* @change 0.1
*
* @param array $modules Array mit Modulen
* @param array $status Array mit Status-Codes
* @return array $output Zusammengebautes Array mit Modulen
*/
private static function _prepare_modules($modules, $status)
{
/* Leer */
if ( empty($modules) or empty($status) or count($modules) != count($status) ) {
return array();
}
$output = array();
foreach ($modules as $key => $module) {
if ( $module == self::_clean_module($module) ) {
$output[$module] = $status[$key];
}
}
return $output;
}
/**
* Registrierung der Optionsseite
*
* @since 0.1
* @change 0.2
*/
public static function add_page()
{
/* Anlegen */
$page = add_options_page(
'Toolbox',
'Toolbox',
'manage_options',
'toolbox',
array(
__CLASS__,
'options_page'
)
);
/* Hilfe */
add_action(
'load-' .$page,
array(
__CLASS__,
'add_help'
)
);
}
/**
* Hilfe-Tab oben rechts
*
* @since 0.1
* @change 0.1
*/
public static function add_help()
{
/* Screen */
$screen = get_current_screen();
/* Tabs */
$screen->add_help_tab(
array(
'id' => 'toolbox_modules',
'title' => 'Module',
'content' => '<p>Die Kachel listet verfügbare Toolbox-Module auf, welche im Plugin-Ordner <em>modules</em> als PHP-Dateien abgelegt sind. Einzelne Module können unterschiedliche Zustände besitzen:</p>'.
'<ul>'.
'<li><em>Deaktiviert</em><br />'.
'Setzt das Modul auf inaktiv. Die Datei lädt nie. Standardeinstellung.</li>'.
'<li><em>Laden nur im Backend</em><br />'.
'Toolbox bindet das Modul ausschliesslich im Administrationsbereich ein. Nicht im Blog-Frontend.</li>'.
'<li><em>Laden nur im Frontend</em><br />'.
'Die Ausführung des Moduls erfolgt ausnahmsweise im Blog-Frontend. Das Backend ist davon ausgeschlossen.</li>'.
'<li><em>Laden im Back- und Frontend</em><br />'.
'Das Modul ist aktiv auf allen Blogseiten. Im Admin und Blogseiten.</li>'.
'</ul>'
)
);
$screen->add_help_tab(
array(
'id' => 'toolbox_settings',
'title' => 'Einstellungen',
'content' => '<p><strong>Sicherheitsmodus aktiv</strong><br />'.
'Im aktiven Zustand verhindert die Option eine Ausführung der Module in folgenden Fällen und ist aus Sicherheitsgründen standardmässig eingeschaltet:</p>'.
'<ul>'.
'<li>Cronjobs</li>'.
'<li>AJAX-Anfragen</li>'.
'<li>Autospeicherung</li>'.
'<li>XMLRPC-Anfragen</li>'.
'</ul>'
)
);
$screen->add_help_tab(
array(
'id' => 'toolbox_manual',
'title' => 'Dokumentation',
'content' => '<p>Ausführliche Dokumentation für das Toolbox-Plugin online verfügbar:</p>'.
'<p><a href="http://playground.ebiene.de/toolbox-wordpress-plugin/" target="_blank">http://playground.ebiene.de/toolbox-wordpress-plugin/</a></p>'
)
);
/* Sidebar */
$screen->set_help_sidebar(
'<p><strong>Mehr zum Autor</strong></p>'.
'<p><a href="https://plus.google.com/110569673423509816572/" target="_blank">Google+</a></p>'.
'<p><a href="http://twitter.com/wpSEO" target="_blank">Twitter</a></p>'.
'<p><a href="http://wpcoder.de" target="_blank">Plugins</a></p>'
);
}
/**
* Registrierung der Optionen
*
* @since 0.1
* @change 0.1
*/
public static function register_settings()
{
register_setting(
'toolbox',
'toolbox',
array(
__CLASS__,
'validate_options'
)
);
}
/**
* Validierung und Speicherung der Optionen
*
* @since 0.1
* @change 0.1
*
* @param array $data Array mit Formularwerten
* @return array Array mit geprüften Werten
*/
public static function validate_options($data)
{
return array(
'secure' => (int)(!empty($data['secure'])),
'modules' => self::_prepare_modules( (array)$data['modules'], (array)$data['status'] )
);
}
/**
* Darstellung der Optionsseite
*
* @since 0.1
* @change 0.1
*/
public static function options_page()
{ ?>
<div class="wrap">
<h2>
Toolbox
</h2>
<form method="post" action="options.php">
<?php settings_fields('toolbox') ?>
<?php $options = get_option('toolbox') ?>
<table class="form-table">
<?php if ( $modules = self::_list_modules() ) {
foreach ($modules as $module) { ?>
<tr valign="top">
<th scope="row">
<?php echo esc_html($module['name']) ?>
</th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span>
<?php echo esc_html($module['name']) ?>
</span>
</legend>
<label>
<select name="toolbox[status][]">
<?php foreach ( array('Deaktiviert', 'Laden nur im Backend', 'Laden nur im Frontend', 'Laden im Back- und Frontend') as $k => $v ) { ?>
<option value="<?php echo esc_attr($k) ?>" <?php selected(@$options['modules'][$module['ident']], $k) ?>>
<?php echo esc_html($v) ?>
</option>
<?php } ?>
</select>
<input type="hidden" name="toolbox[modules][]" value="<?php echo esc_attr($module['ident']) ?>" />
</label>
<p class="description">
<?php echo esc_html($module['desc']) ?> <?php echo $module['link'] ?>
</p>
</fieldset>
</td>
</tr>
<?php }
} ?>
<tr valign="top">
<th scope="row">
Sicherheitsmodus
</th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span>
Sicherheitsmodus aktiv
</span>
</legend>
<label for="toolbox_secure">
<input type="checkbox" name="toolbox[secure]" id="toolbox_secure" value="1" <?php checked('1', $options['secure']) ?> />
Aktiv
</label>
</fieldset>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div><?php
}
}
/* Fire */
add_action(
'plugins_loaded',
array(
'Toolbox',
'init'
),
99
);
/* Install */
register_activation_hook(
__FILE__,
array(
'Toolbox',
'install'
)
);
/* Uninstall */
register_uninstall_hook(
__FILE__,
array(
'Toolbox',
'uninstall'
)
);