Cachify - Version 1.2.1

Version Description

  • Icon fr die "Cache leeren" Schaltflche in der Admin Bar
Download this release

Release Info

Developer sergej.mueller
Plugin Icon 128x128 Cachify
Version 1.2.1
Comparing to
See all releases

Version 1.2.1

cachify.original.php ADDED
@@ -0,0 +1,1205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Cachify
4
+ Description: Smarter Cache für WordPress. Reduziert die Anzahl der Datenbankabfragen und dynamischer Anweisungen. Minimiert Ladezeiten der Blogseiten.
5
+ Author: Sergej M&uuml;ller
6
+ Author URI: http://wpseo.de
7
+ Plugin URI: http://playground.ebiene.de/2652/cachify-wordpress-cache/
8
+ Version: 1.2.1
9
+ */
10
+
11
+
12
+ /* Sicherheitsabfrage */
13
+ if ( !class_exists('WP') ) {
14
+ header('Status: 403 Forbidden');
15
+ header('HTTP/1.1 403 Forbidden');
16
+ exit();
17
+ }
18
+
19
+
20
+ /**
21
+ * Cachify
22
+ */
23
+
24
+ final class Cachify {
25
+
26
+
27
+ /* Save me */
28
+ private static $base;
29
+
30
+
31
+ /**
32
+ * Konstruktor der Klasse
33
+ *
34
+ * @since 1.0
35
+ * @change 1.2
36
+ */
37
+
38
+ public static function init()
39
+ {
40
+ /* Autosave? */
41
+ if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
42
+ return;
43
+ }
44
+
45
+ /* Plugin-Base */
46
+ self::$base = plugin_basename(__FILE__);
47
+
48
+ /* Publish post */
49
+ add_action(
50
+ 'publish_post',
51
+ array(
52
+ __CLASS__,
53
+ 'publish_post'
54
+ )
55
+ );
56
+
57
+ /* Publish page */
58
+ add_action(
59
+ 'publish_page',
60
+ array(
61
+ __CLASS__,
62
+ 'publish_page'
63
+ )
64
+ );
65
+
66
+ /* Backend */
67
+ if ( is_admin() ) {
68
+ add_action(
69
+ 'wpmu_new_blog',
70
+ array(
71
+ __CLASS__,
72
+ 'install_later'
73
+ )
74
+ );
75
+ add_action(
76
+ 'delete_blog',
77
+ array(
78
+ __CLASS__,
79
+ 'uninstall_later'
80
+ )
81
+ );
82
+
83
+ add_action(
84
+ 'admin_init',
85
+ array(
86
+ __CLASS__,
87
+ 'register_settings'
88
+ )
89
+ );
90
+ add_action(
91
+ 'admin_init',
92
+ array(
93
+ __CLASS__,
94
+ 'receive_flush'
95
+ )
96
+ );
97
+ add_action(
98
+ 'admin_menu',
99
+ array(
100
+ __CLASS__,
101
+ 'add_page'
102
+ )
103
+ );
104
+ add_action(
105
+ 'admin_print_styles',
106
+ array(
107
+ __CLASS__,
108
+ 'add_css'
109
+ )
110
+ );
111
+
112
+ add_action(
113
+ 'transition_comment_status',
114
+ array(
115
+ __CLASS__,
116
+ 'touch_comment'
117
+ ),
118
+ 10,
119
+ 3
120
+ );
121
+ add_action(
122
+ 'edit_comment',
123
+ array(
124
+ __CLASS__,
125
+ 'edit_comment'
126
+ )
127
+ );
128
+ add_action(
129
+ 'admin_bar_menu',
130
+ array(
131
+ __CLASS__,
132
+ 'add_menu'
133
+ ),
134
+ 90
135
+ );
136
+
137
+ add_filter(
138
+ 'plugin_row_meta',
139
+ array(
140
+ __CLASS__,
141
+ 'row_meta'
142
+ ),
143
+ 10,
144
+ 2
145
+ );
146
+ add_filter(
147
+ 'plugin_action_links_' .self::$base,
148
+ array(
149
+ __CLASS__,
150
+ 'action_links'
151
+ )
152
+ );
153
+
154
+ /* Frontend */
155
+ } else {
156
+ add_action(
157
+ 'preprocess_comment',
158
+ array(
159
+ __CLASS__,
160
+ 'add_comment'
161
+ ),
162
+ 1
163
+ );
164
+
165
+ add_action(
166
+ 'template_redirect',
167
+ array(
168
+ __CLASS__,
169
+ 'manage_cache'
170
+ ),
171
+ 99
172
+ );
173
+ }
174
+ }
175
+
176
+
177
+ /**
178
+ * Installation des Plugins auch für MU-Blogs
179
+ *
180
+ * @since 1.0
181
+ * @change 1.0
182
+ */
183
+
184
+ public static function install()
185
+ {
186
+ /* Global */
187
+ global $wpdb;
188
+
189
+ /* Multisite & Network */
190
+ if ( is_multisite() && !empty($_GET['networkwide']) ) {
191
+ /* Blog-IDs */
192
+ $ids = self::_get_blog_ids();
193
+
194
+ /* Loopen */
195
+ foreach ($ids as $id) {
196
+ switch_to_blog( (int)$id );
197
+ self::_install_backend();
198
+ }
199
+
200
+ /* Wechsel zurück */
201
+ restore_current_blog();
202
+
203
+ } else {
204
+ self::_install_backend();
205
+ }
206
+ }
207
+
208
+
209
+ /**
210
+ * Installation des Plugins bei einem neuen MU-Blog
211
+ *
212
+ * @since 1.0
213
+ * @change 1.0
214
+ */
215
+
216
+ public static function install_later($id) {
217
+ /* Kein Netzwerk-Plugin */
218
+ if ( !is_plugin_active_for_network(self::$base) ) {
219
+ return;
220
+ }
221
+
222
+ /* Wechsel */
223
+ switch_to_blog( (int)$id );
224
+
225
+ /* Installieren */
226
+ self::_install_backend();
227
+
228
+ /* Wechsel zurück */
229
+ restore_current_blog();
230
+ }
231
+
232
+
233
+ /**
234
+ * Eigentliche Installation der Option und der Tabelle
235
+ *
236
+ * @since 1.0
237
+ * @change 1.0
238
+ */
239
+
240
+ private static function _install_backend()
241
+ {
242
+ add_option(
243
+ 'cachify',
244
+ array(
245
+ 'only_guests' => 1,
246
+ 'compress_html' => 0,
247
+ 'cache_expires' => 12,
248
+ 'without_ids' => '',
249
+ 'without_agents' => ''
250
+ ),
251
+ '',
252
+ 'no'
253
+ );
254
+
255
+ /* Flush */
256
+ self::flush_cache();
257
+ }
258
+
259
+
260
+ /**
261
+ * Uninstallation des Plugins pro MU-Blog
262
+ *
263
+ * @since 1.0
264
+ * @change 1.0
265
+ */
266
+
267
+ public static function uninstall()
268
+ {
269
+ /* Global */
270
+ global $wpdb;
271
+
272
+ /* Multisite & Network */
273
+ if ( is_multisite() && !empty($_GET['networkwide']) ) {
274
+ /* Alter Blog */
275
+ $old = $wpdb->blogid;
276
+
277
+ /* Blog-IDs */
278
+ $ids = self::_get_blog_ids();
279
+
280
+ /* Loopen */
281
+ foreach ($ids as $id) {
282
+ switch_to_blog($id);
283
+ self::_uninstall_backend();
284
+ }
285
+
286
+ /* Wechsel zurück */
287
+ switch_to_blog($old);
288
+ } else {
289
+ self::_uninstall_backend();
290
+ }
291
+ }
292
+
293
+
294
+ /**
295
+ * Uninstallation des Plugins bei MU & Network-Plugin
296
+ *
297
+ * @since 1.0
298
+ * @change 1.0
299
+ */
300
+
301
+ public static function uninstall_later($id) {
302
+ /* Global */
303
+ global $wpdb;
304
+
305
+ /* Kein Netzwerk-Plugin */
306
+ if ( !is_plugin_active_for_network(self::$base) ) {
307
+ return;
308
+ }
309
+
310
+ /* Wechsel */
311
+ switch_to_blog( (int)$id );
312
+
313
+ /* Installieren */
314
+ self::_uninstall_backend();
315
+
316
+ /* Wechsel zurück */
317
+ restore_current_blog();
318
+ }
319
+
320
+
321
+ /**
322
+ * Eigentliche Deinstallation des Plugins
323
+ *
324
+ * @since 1.0
325
+ * @change 1.0
326
+ */
327
+
328
+ private static function _uninstall_backend()
329
+ {
330
+ /* Option */
331
+ delete_option('cachify');
332
+
333
+ /* Cache leeren */
334
+ self::flush_cache();
335
+ }
336
+
337
+
338
+ /**
339
+ * Update des Plugins
340
+ *
341
+ * @since 1.0
342
+ * @change 1.0
343
+ */
344
+
345
+ public static function update()
346
+ {
347
+ /* Updaten */
348
+ self::_update_backend();
349
+ }
350
+
351
+
352
+ /**
353
+ * Eigentlicher Update des Plugins
354
+ *
355
+ * @since 1.0
356
+ * @change 1.0
357
+ */
358
+
359
+ private static function _update_backend()
360
+ {
361
+ /* Cache leeren */
362
+ self::flush_cache();
363
+ }
364
+
365
+
366
+ /**
367
+ * Rückgabe der IDs installierter Blogs
368
+ *
369
+ * @since 1.0
370
+ * @change 1.0
371
+ *
372
+ * @return array Blog-IDs
373
+ */
374
+
375
+ private static function _get_blog_ids()
376
+ {
377
+ /* Global */
378
+ global $wpdb;
379
+
380
+ return $wpdb->get_col(
381
+ $wpdb->prepare("SELECT blog_id FROM `$wpdb->blogs`")
382
+ );
383
+ }
384
+
385
+
386
+ /**
387
+ * Hinzufügen der Action-Links (Einstellungen links)
388
+ *
389
+ * @since 1.0
390
+ * @change 1.0
391
+ */
392
+
393
+ public static function action_links($data)
394
+ {
395
+ /* Rechte? */
396
+ if ( !current_user_can('manage_options') ) {
397
+ return $data;
398
+ }
399
+
400
+ return array_merge(
401
+ $data,
402
+ array(
403
+ sprintf(
404
+ '<a href="%s">%s</a>',
405
+ add_query_arg(
406
+ array(
407
+ 'page' => 'cachify'
408
+ ),
409
+ admin_url('options-general.php')
410
+ ),
411
+ __('Settings')
412
+ )
413
+ )
414
+ );
415
+ }
416
+
417
+
418
+ /**
419
+ * Meta-Links zum Plugin
420
+ *
421
+ * @since 0.5
422
+ * @change 1.1
423
+ *
424
+ * @param array $data Bereits vorhandene Links
425
+ * @param string $page Aktuelle Seite
426
+ * @return array $data Modifizierte Links
427
+ */
428
+
429
+ public static function row_meta($data, $page)
430
+ {
431
+ if ( $page == self::$base && current_user_can('manage_options') ) {
432
+ $data = array_merge(
433
+ $data,
434
+ array(
435
+ '<a href="http://flattr.com/thing/114377/Cachify-Handliches-Cache-Plugin-fur-WordPress" target="_blank">Plugin flattern</a>',
436
+ '<a href="https://plus.google.com/110569673423509816572" target="_blank">Auf Google+ folgen</a>',
437
+ sprintf(
438
+ '<a href="%s">Cache leeren</a>',
439
+ add_query_arg('_cachify', 'flush', 'plugins.php')
440
+ )
441
+ )
442
+ );
443
+ }
444
+
445
+ return $data;
446
+ }
447
+
448
+
449
+ /**
450
+ * Hinzufügen eines Admin-Bar-Menüs
451
+ *
452
+ * @since 1.2
453
+ * @change 1.2.1
454
+ */
455
+
456
+ public static function add_menu( $wp_admin_bar ) {
457
+ /* Aussteigen */
458
+ if ( !function_exists('is_admin_bar_showing') or !is_admin_bar_showing() or !is_object($wp_admin_bar) or !is_super_admin() ) {
459
+ return;
460
+ }
461
+
462
+ /* Hinzufügen */
463
+ $wp_admin_bar->add_menu(
464
+ array(
465
+ 'id' => 'cachify',
466
+ 'title' => '<span class="ab-icon"></span><span class="ab-label">Cache leeren</span>',
467
+ 'href' => add_query_arg('_cachify', 'flush')
468
+ )
469
+ );
470
+ }
471
+
472
+
473
+ /**
474
+ * Verarbeitung der Plugin-Meta-Aktionen
475
+ *
476
+ * @since 0.5
477
+ * @change 1.2
478
+ *
479
+ * @param array $data Metadaten der Plugins
480
+ */
481
+
482
+ public static function receive_flush($data)
483
+ {
484
+ /* Leer? */
485
+ if ( empty($_GET['_cachify']) or $_GET['_cachify'] !== 'flush' ) {
486
+ return;
487
+ }
488
+
489
+ /* Global */
490
+ global $wpdb;
491
+
492
+ /* Multisite & Network */
493
+ if ( is_multisite() && is_plugin_active_for_network(self::$base) ) {
494
+ /* Alter Blog */
495
+ $old = $wpdb->blogid;
496
+
497
+ /* Blog-IDs */
498
+ $ids = self::_get_blog_ids();
499
+
500
+ /* Loopen */
501
+ foreach ($ids as $id) {
502
+ switch_to_blog($id);
503
+ self::flush_cache();
504
+ }
505
+
506
+ /* Wechsel zurück */
507
+ switch_to_blog($old);
508
+
509
+ /* Notiz */
510
+ add_action(
511
+ 'network_admin_notices',
512
+ array(
513
+ __CLASS__,
514
+ 'flush_notice'
515
+ )
516
+ );
517
+ } else {
518
+ /* Leeren */
519
+ self::flush_cache();
520
+
521
+ /* Notiz */
522
+ add_action(
523
+ 'admin_notices',
524
+ array(
525
+ __CLASS__,
526
+ 'flush_notice'
527
+ )
528
+ );
529
+ }
530
+ }
531
+
532
+
533
+ /**
534
+ * Zeigt Hinweis nach erfolgreichem Cache-Leeren
535
+ *
536
+ * @since 1.2
537
+ * @change 1.2
538
+ */
539
+
540
+ public static function flush_notice() {
541
+ /* Kein Admin */
542
+ if ( !is_super_admin() ) {
543
+ return false;
544
+ }
545
+
546
+ echo '<div id="message" class="updated"><p><strong>Cachify-Cache geleert.</strong></p></div>';
547
+ }
548
+
549
+
550
+ /**
551
+ * Löschung des Cache bei neuem Kommentar
552
+ *
553
+ * @since 0.1
554
+ * @change 0.4
555
+ *
556
+ * @param integer $id ID des Kommentars
557
+ */
558
+
559
+ public static function edit_comment($id)
560
+ {
561
+ self::_delete_cache(
562
+ get_permalink(
563
+ get_comment($id)->comment_post_ID
564
+ )
565
+ );
566
+ }
567
+
568
+
569
+ /**
570
+ * Löschung des Cache bei neuem Kommentar
571
+ *
572
+ * @since 0.1
573
+ * @change 0.1
574
+ *
575
+ * @param array $comment Array mit Eigenschaften
576
+ * @return array $comment Array mit Eigenschaften
577
+ */
578
+
579
+ public static function add_comment($comment)
580
+ {
581
+ self::_delete_cache(
582
+ get_permalink($comment['comment_post_ID'])
583
+ );
584
+
585
+ return $comment;
586
+ }
587
+
588
+
589
+ /**
590
+ * Löschung des Cache beim Kommentar-Editieren
591
+ *
592
+ * @since 0.1
593
+ * @change 0.4
594
+ *
595
+ * @param string $new_status Neuer Status
596
+ * @param string $old_status Alter Status
597
+ * @param object $comment Array mit Eigenschaften
598
+ */
599
+
600
+ public static function touch_comment($new_status, $old_status, $comment)
601
+ {
602
+ if ( $new_status != $old_status ) {
603
+ self::_delete_cache(
604
+ get_permalink($comment->comment_post_ID)
605
+ );
606
+ }
607
+ }
608
+
609
+
610
+ /**
611
+ * Leerung des kompletten Cache
612
+ *
613
+ * @since 0.1
614
+ * @change 0.9.1
615
+ *
616
+ * @param intval $id ID des Beitrags
617
+ */
618
+
619
+ public static function publish_post($id)
620
+ {
621
+ /* Post */
622
+ $post = get_post($id);
623
+
624
+ /* Löschen */
625
+ if ( in_array( $post->post_status, array('publish', 'future') ) ) {
626
+ self::flush_cache();
627
+ }
628
+ }
629
+
630
+
631
+ /**
632
+ * Leerung des kompletten Cache
633
+ *
634
+ * @since 1.0
635
+ * @change 1.0
636
+ *
637
+ * @param intval $id ID des Beitrags
638
+ */
639
+
640
+ public static function publish_page($id)
641
+ {
642
+ /* Page */
643
+ $page = get_page($id);
644
+
645
+ /* Löschen */
646
+ if ( $page->post_status == 'publish' ) {
647
+ self::flush_cache();
648
+ }
649
+ }
650
+
651
+
652
+ /**
653
+ * Rückgabe des Cache-Hash-Wertes
654
+ *
655
+ * @since 0.1
656
+ * @change 1.0
657
+ *
658
+ * @param string $url URL für den Hash-Wert [optional]
659
+ * @return string Cachify-Hash-Wert
660
+ */
661
+
662
+ private static function _cache_hash($url = '')
663
+ {
664
+ /* Leer? */
665
+ if ( empty($url) ) {
666
+ $url = esc_url_raw(
667
+ sprintf(
668
+ '%s://%s%s',
669
+ (is_ssl() ? 'https' : 'http'),
670
+ $_SERVER['HTTP_HOST'],
671
+ $_SERVER['REQUEST_URI']
672
+ )
673
+ );
674
+ }
675
+
676
+ return 'cachify_' .md5($url);
677
+ }
678
+
679
+
680
+ /**
681
+ * Rückgabe der Query-Anzahl
682
+ *
683
+ * @since 0.1
684
+ * @change 1.0
685
+ *
686
+ * @return intval Query-Anzahl
687
+ */
688
+
689
+ private static function _page_queries()
690
+ {
691
+ return $GLOBALS['wpdb']->num_queries;
692
+ }
693
+
694
+
695
+ /**
696
+ * Rückgabe der Ausführungszeit
697
+ *
698
+ * @since 0.1
699
+ * @change 1.0
700
+ *
701
+ * @return intval Anzahl der Sekunden
702
+ */
703
+
704
+ private static function _page_timer()
705
+ {
706
+ return timer_stop(0, 2);
707
+ }
708
+
709
+
710
+ /**
711
+ * Rückgabe des Speicherverbrauchs
712
+ *
713
+ * @since 0.7
714
+ * @change 1.0
715
+ *
716
+ * @return string Konvertierter Größenwert
717
+ */
718
+
719
+ private static function _memory_usage()
720
+ {
721
+ return ( function_exists('memory_get_usage') ? size_format(memory_get_usage(), 2) : 0 );
722
+ }
723
+
724
+
725
+ /**
726
+ * Splittung nach Komma
727
+ *
728
+ * @since 0.9.1
729
+ * @change 1.0
730
+ *
731
+ * @param string $input Zu splittende Zeichenkette
732
+ * @return array Konvertierter Array
733
+ */
734
+
735
+ private static function _preg_split($input)
736
+ {
737
+ return (array)preg_split('/,/', $input, -1, PREG_SPLIT_NO_EMPTY);
738
+ }
739
+
740
+
741
+ /**
742
+ * Prüfung auf Index
743
+ *
744
+ * @since 0.6
745
+ * @change 1.0
746
+ *
747
+ * @return boolean TRUE bei Index
748
+ */
749
+
750
+ private static function _is_index()
751
+ {
752
+ return basename($_SERVER['SCRIPT_NAME']) != 'index.php';
753
+ }
754
+
755
+
756
+ /**
757
+ * Prüfung auf Mobile Devices
758
+ *
759
+ * @since 0.9.1
760
+ * @change 1.0
761
+ *
762
+ * @return boolean TRUE bei Mobile
763
+ */
764
+
765
+ private static function _is_mobile()
766
+ {
767
+ return ( strpos(TEMPLATEPATH, 'wptouch') or strpos(TEMPLATEPATH, 'carrington') );
768
+ }
769
+
770
+
771
+ /**
772
+ * Definition der Ausnahmen für den Cache
773
+ *
774
+ * @since 0.2
775
+ * @change 1.0
776
+ *
777
+ * @param string $type Typ der Abfrage [optional]
778
+ * @return boolean TRUE bei Ausnahmen
779
+ */
780
+
781
+ private static function _skip_cache()
782
+ {
783
+ /* Optionen */
784
+ $options = get_option('cachify');
785
+
786
+ /* Filter */
787
+ if ( self::_is_index() or is_feed() or is_trackback() or is_robots() or is_preview() or post_password_required() or ( $options['only_guests'] && is_user_logged_in() ) ) {
788
+ return true;
789
+ }
790
+
791
+ /* WP Touch */
792
+ if ( self::_is_mobile() ) {
793
+ return true;
794
+ }
795
+
796
+ /* Post IDs */
797
+ if ( $options['without_ids'] && is_singular() ) {
798
+ if ( in_array( $GLOBALS['wp_query']->get_queried_object_id(), self::_preg_split($options['without_ids']) ) ) {
799
+ return true;
800
+ }
801
+ }
802
+
803
+ /* User Agents */
804
+ if ( $options['without_agents'] && isset($_SERVER['HTTP_USER_AGENT']) ) {
805
+ if ( array_filter( self::_preg_split($options['without_agents']), create_function('$a', 'return strpos($_SERVER["HTTP_USER_AGENT"], $a);') ) ) {
806
+ return true;
807
+ }
808
+ }
809
+
810
+ return false;
811
+ }
812
+
813
+
814
+ /**
815
+ * Komprimiert den HTML-Code
816
+ *
817
+ * @since 0.9.2
818
+ * @change 1.2
819
+ *
820
+ * @param string $data Zu komprimierende Datensatz
821
+ * @return string $data Komprimierter Datensatz
822
+ */
823
+
824
+ private static function _sanitize_cache($data) {
825
+ /* Optionen */
826
+ $options = get_option('cachify');
827
+
828
+ /* Komprimieren? */
829
+ if ( !$options['compress_html'] ) {
830
+ return($data);
831
+ }
832
+
833
+ /* Verkleinern */
834
+ $cleaned = preg_replace(
835
+ array(
836
+ '/\<!--.+?--\>/s',
837
+ '/\>(\s)+(\S)/s',
838
+ '/\>[^\S ]+/s',
839
+ '/[^\S ]+\</s',
840
+ '/\>(\s)+/s',
841
+ '/(\s)+\</s',
842
+ '/\>\s+\</s'
843
+ ),
844
+ array(
845
+ '',
846
+ '>\\1\\2',
847
+ '>',
848
+ '<',
849
+ '>\\1',
850
+ '\\1<',
851
+ '> <'
852
+ ),
853
+ (string)$data
854
+ );
855
+
856
+ /* Fehlerhaft? */
857
+ if ( strlen($cleaned) <= 1 ) {
858
+ return($data);
859
+ }
860
+
861
+ return($cleaned);
862
+ }
863
+
864
+
865
+ /**
866
+ * Löschung des Cache für eine URL
867
+ *
868
+ * @since 0.1
869
+ * @change 1.0
870
+ *
871
+ * @param string $url URL für den Hash-Wert
872
+ */
873
+
874
+ private static function _delete_cache($url)
875
+ {
876
+ delete_transient(
877
+ self::_cache_hash($url)
878
+ );
879
+ }
880
+
881
+
882
+ /**
883
+ * Zurücksetzen des kompletten Cache
884
+ *
885
+ * @since 0.1
886
+ * @change 1.2
887
+ */
888
+
889
+ public static function flush_cache()
890
+ {
891
+ $GLOBALS['wpdb']->query("DELETE FROM `" .$GLOBALS['wpdb']->options. "` WHERE `option_name` LIKE ('_transient%_cachify_%')");
892
+ $GLOBALS['wpdb']->query("OPTIMIZE TABLE `" .$GLOBALS['wpdb']->options. "`");
893
+ }
894
+
895
+
896
+ /**
897
+ * Zuweisung des Cache
898
+ *
899
+ * @since 0.1
900
+ * @change 1.0
901
+ *
902
+ * @param string $data Inhalt der Seite
903
+ * @return string $data Inhalt der Seite
904
+ */
905
+
906
+ public static function set_cache($data)
907
+ {
908
+ /* Optionen */
909
+ $options = get_option('cachify');
910
+
911
+ /* Speichern */
912
+ if ( !empty($data) ) {
913
+ set_transient(
914
+ self::_cache_hash(),
915
+ array(
916
+ 'data' => self::_sanitize_cache($data),
917
+ 'queries' => self::_page_queries(),
918
+ 'timer' => self::_page_timer(),
919
+ 'memory' => self::_memory_usage(),
920
+ 'time' => current_time('timestamp')
921
+ ),
922
+ 60 * 60 * (int)$options['cache_expires']
923
+ );
924
+ }
925
+
926
+ return $data;
927
+ }
928
+
929
+
930
+ /**
931
+ * Verwaltung des Cache
932
+ *
933
+ * @since 0.1
934
+ * @change 0.9
935
+ */
936
+
937
+ public static function manage_cache()
938
+ {
939
+ /* Kein Cache? */
940
+ if ( self::_skip_cache() ) {
941
+ return;
942
+ }
943
+
944
+ /* Im Cache? */
945
+ if ( $cache = get_transient(self::_cache_hash()) ) {
946
+ if ( !empty($cache['data']) ) {
947
+ /* Content */
948
+ echo $cache['data'];
949
+
950
+ /* Signatur */
951
+ echo sprintf(
952
+ "\n\n<!--\n%s\n%s\n%s\n%s\n-->",
953
+ 'Cachify für WordPress | http://bit.ly/cachify',
954
+ sprintf(
955
+ 'Ohne Cachify: %d DB-Anfragen, %s Sekunden, %s',
956
+ $cache['queries'],
957
+ $cache['timer'],
958
+ $cache['memory']
959
+ ),
960
+ sprintf(
961
+ 'Mit Cachify: %d DB-Anfragen, %s Sekunden, %s',
962
+ self::_page_queries(),
963
+ self::_page_timer(),
964
+ self::_memory_usage()
965
+ ),
966
+ sprintf(
967
+ 'Generiert: %s zuvor',
968
+ human_time_diff($cache['time'], current_time('timestamp'))
969
+ )
970
+ );
971
+
972
+ exit;
973
+ }
974
+ }
975
+
976
+ /* Cachen */
977
+ ob_start('Cachify::set_cache');
978
+ }
979
+
980
+
981
+ /**
982
+ * Einbindung von CSS
983
+ *
984
+ * @since 1.0
985
+ * @change 1.1
986
+ */
987
+
988
+ function add_css()
989
+ {
990
+ /* Infos auslesen */
991
+ $data = get_plugin_data(__FILE__);
992
+
993
+ /* CSS registrieren */
994
+ wp_register_style(
995
+ 'cachify_css',
996
+ plugins_url('css/style.css', __FILE__),
997
+ array(),
998
+ $data['Version']
999
+ );
1000
+
1001
+ /* CSS einbinden */
1002
+ wp_enqueue_style('cachify_css');
1003
+ }
1004
+
1005
+
1006
+ /**
1007
+ * Einfüger der Optionsseite
1008
+ *
1009
+ * @since 1.0
1010
+ * @change 1.0
1011
+ */
1012
+
1013
+ function add_page()
1014
+ {
1015
+ add_options_page(
1016
+ 'Cachify',
1017
+ '<img src="' .plugins_url('cachify/img/icon.png'). '" alt="Cachify" />Cachify',
1018
+ 'manage_options',
1019
+ 'cachify',
1020
+ array(
1021
+ __CLASS__,
1022
+ 'options_page'
1023
+ )
1024
+ );
1025
+ }
1026
+
1027
+
1028
+ /**
1029
+ * Anzeige des Hilfe-Links
1030
+ *
1031
+ * @since 1.1
1032
+ * @change 1.1
1033
+ *
1034
+ * @param string $anchor Anker in der Hilfe
1035
+ */
1036
+
1037
+ function help_link($anchor) {
1038
+ echo sprintf(
1039
+ '<span>[<a href="http://playground.ebiene.de/2652/cachify-wordpress-cache/#%s" target="_blank">?</a>]</span>',
1040
+ $anchor
1041
+ );
1042
+ }
1043
+
1044
+
1045
+ /**
1046
+ * Registrierung der Settings
1047
+ *
1048
+ * @since 1.0
1049
+ * @change 1.0
1050
+ */
1051
+
1052
+ function register_settings()
1053
+ {
1054
+ register_setting(
1055
+ 'cachify',
1056
+ 'cachify',
1057
+ array(
1058
+ __CLASS__,
1059
+ 'validate_options'
1060
+ )
1061
+ );
1062
+ }
1063
+
1064
+
1065
+ /**
1066
+ * Valisierung der Optionsseite
1067
+ *
1068
+ * @since 1.0
1069
+ * @change 1.0
1070
+ *
1071
+ * @param array $data Array mit Formularwerten
1072
+ * @return array Array mit geprüften Werten
1073
+ */
1074
+
1075
+ public static function validate_options($data)
1076
+ {
1077
+ /* Cache leeren */
1078
+ self::flush_cache();
1079
+
1080
+ return array(
1081
+ 'only_guests' => (int)(!empty($data['only_guests'])),
1082
+ 'compress_html' => (int)(!empty($data['compress_html'])),
1083
+ 'cache_expires' => (int)(@$data['cache_expires']),
1084
+ 'without_ids' => (string)sanitize_text_field(@$data['without_ids']),
1085
+ 'without_agents' => (string)sanitize_text_field(@$data['without_agents'])
1086
+ );
1087
+ }
1088
+
1089
+
1090
+ /**
1091
+ * Darstellung der Optionsseite
1092
+ *
1093
+ * @since 1.0
1094
+ * @change 1.0
1095
+ */
1096
+
1097
+ public static function options_page()
1098
+ { ?>
1099
+ <div class="wrap">
1100
+ <?php screen_icon('cachify') ?>
1101
+
1102
+ <h2>
1103
+ Cachify
1104
+ </h2>
1105
+
1106
+ <form method="post" action="options.php">
1107
+ <?php settings_fields('cachify') ?>
1108
+
1109
+ <?php $options = get_option('cachify') ?>
1110
+
1111
+ <table class="form-table cachify">
1112
+ <tr>
1113
+ <th>
1114
+ Cache-Gültigkeit in Stunden <?php self::help_link('cache_expires') ?>
1115
+ </th>
1116
+ <td>
1117
+ <input type="text" name="cachify[cache_expires]" value="<?php echo $options['cache_expires'] ?>" />
1118
+ </td>
1119
+ </tr>
1120
+
1121
+ <tr>
1122
+ <th>
1123
+ Ausnahme für (Post/Pages) IDs <?php self::help_link('without_ids') ?>
1124
+ </th>
1125
+ <td>
1126
+ <input type="text" name="cachify[without_ids]" value="<?php echo $options['without_ids'] ?>" />
1127
+ </td>
1128
+ </tr>
1129
+
1130
+ <tr>
1131
+ <th>
1132
+ Ausnahme für User Agents <?php self::help_link('without_agents') ?>
1133
+ </th>
1134
+ <td>
1135
+ <input type="text" name="cachify[without_agents]" value="<?php echo $options['without_agents'] ?>" />
1136
+ </td>
1137
+ </tr>
1138
+
1139
+ <tr>
1140
+ <th>
1141
+ Komprimierung der Ausgabe <?php self::help_link('compress_html') ?>
1142
+ </th>
1143
+ <td>
1144
+ <input type="checkbox" name="cachify[compress_html]" value="1" <?php checked('1', $options['compress_html']); ?> />
1145
+ </td>
1146
+ </tr>
1147
+
1148
+ <tr>
1149
+ <th>
1150
+ Nur für nicht eingeloggte Nutzer <?php self::help_link('only_guests') ?>
1151
+ </th>
1152
+ <td>
1153
+ <input type="checkbox" name="cachify[only_guests]" value="1" <?php checked('1', $options['only_guests']); ?> />
1154
+ </td>
1155
+ </tr>
1156
+ </table>
1157
+
1158
+ <p class="submit">
1159
+ <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
1160
+ </p>
1161
+ </form>
1162
+ </div><?php
1163
+ }
1164
+ }
1165
+
1166
+
1167
+ /* Fire */
1168
+ add_action(
1169
+ 'plugins_loaded',
1170
+ array(
1171
+ 'Cachify',
1172
+ 'init'
1173
+ ),
1174
+ 99
1175
+ );
1176
+
1177
+
1178
+ /* Install */
1179
+ register_activation_hook(
1180
+ __FILE__,
1181
+ array(
1182
+ 'Cachify',
1183
+ 'install'
1184
+ )
1185
+ );
1186
+
1187
+ /* Uninstall */
1188
+ register_uninstall_hook(
1189
+ __FILE__,
1190
+ array(
1191
+ 'Cachify',
1192
+ 'uninstall'
1193
+ )
1194
+ );
1195
+
1196
+ /* Updaten */
1197
+ if ( function_exists('register_update_hook') ) {
1198
+ register_update_hook(
1199
+ __FILE__,
1200
+ array(
1201
+ 'Cachify',
1202
+ 'update'
1203
+ )
1204
+ );
1205
+ }
cachify.php ADDED
@@ -0,0 +1,641 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Cachify
4
+ Description: Smarter Cache für WordPress. Reduziert die Anzahl der Datenbankabfragen und dynamischer Anweisungen. Minimiert Ladezeiten der Blogseiten.
5
+ Author: Sergej M&uuml;ller
6
+ Author URI: http://wpseo.de
7
+ Plugin URI: http://playground.ebiene.de/2652/cachify-wordpress-cache/
8
+ Version: 1.2.1
9
+ */
10
+
11
+
12
+ if ( !class_exists('WP') ) {
13
+ header('Status: 403 Forbidden');
14
+ header('HTTP/1.1 403 Forbidden');
15
+ exit();
16
+ }
17
+ final class Cachify {
18
+ private static $base;
19
+ public static function init()
20
+ {
21
+ if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
22
+ return;
23
+ }
24
+ self::$base = plugin_basename(__FILE__);
25
+ add_action(
26
+ 'publish_post',
27
+ array(
28
+ __CLASS__,
29
+ 'publish_post'
30
+ )
31
+ );
32
+ add_action(
33
+ 'publish_page',
34
+ array(
35
+ __CLASS__,
36
+ 'publish_page'
37
+ )
38
+ );
39
+ if ( is_admin() ) {
40
+ add_action(
41
+ 'wpmu_new_blog',
42
+ array(
43
+ __CLASS__,
44
+ 'install_later'
45
+ )
46
+ );
47
+ add_action(
48
+ 'delete_blog',
49
+ array(
50
+ __CLASS__,
51
+ 'uninstall_later'
52
+ )
53
+ );
54
+ add_action(
55
+ 'admin_init',
56
+ array(
57
+ __CLASS__,
58
+ 'register_settings'
59
+ )
60
+ );
61
+ add_action(
62
+ 'admin_init',
63
+ array(
64
+ __CLASS__,
65
+ 'receive_flush'
66
+ )
67
+ );
68
+ add_action(
69
+ 'admin_menu',
70
+ array(
71
+ __CLASS__,
72
+ 'add_page'
73
+ )
74
+ );
75
+ add_action(
76
+ 'admin_print_styles',
77
+ array(
78
+ __CLASS__,
79
+ 'add_css'
80
+ )
81
+ );
82
+ add_action(
83
+ 'transition_comment_status',
84
+ array(
85
+ __CLASS__,
86
+ 'touch_comment'
87
+ ),
88
+ 10,
89
+ 3
90
+ );
91
+ add_action(
92
+ 'edit_comment',
93
+ array(
94
+ __CLASS__,
95
+ 'edit_comment'
96
+ )
97
+ );
98
+ add_action(
99
+ 'admin_bar_menu',
100
+ array(
101
+ __CLASS__,
102
+ 'add_menu'
103
+ ),
104
+ 90
105
+ );
106
+ add_filter(
107
+ 'plugin_row_meta',
108
+ array(
109
+ __CLASS__,
110
+ 'row_meta'
111
+ ),
112
+ 10,
113
+ 2
114
+ );
115
+ add_filter(
116
+ 'plugin_action_links_' .self::$base,
117
+ array(
118
+ __CLASS__,
119
+ 'action_links'
120
+ )
121
+ );
122
+ } else {
123
+ add_action(
124
+ 'preprocess_comment',
125
+ array(
126
+ __CLASS__,
127
+ 'add_comment'
128
+ ),
129
+ 1
130
+ );
131
+ add_action(
132
+ 'template_redirect',
133
+ array(
134
+ __CLASS__,
135
+ 'manage_cache'
136
+ ),
137
+ 99
138
+ );
139
+ }
140
+ }
141
+ public static function install()
142
+ {
143
+ global $wpdb;
144
+ if ( is_multisite() && !empty($_GET['networkwide']) ) {
145
+ $ids = self::_get_blog_ids();
146
+ foreach ($ids as $id) {
147
+ switch_to_blog( (int)$id );
148
+ self::_install_backend();
149
+ }
150
+ restore_current_blog();
151
+ } else {
152
+ self::_install_backend();
153
+ }
154
+ }
155
+ public static function install_later($id) {
156
+ if ( !is_plugin_active_for_network(self::$base) ) {
157
+ return;
158
+ }
159
+ switch_to_blog( (int)$id );
160
+ self::_install_backend();
161
+ restore_current_blog();
162
+ }
163
+ private static function _install_backend()
164
+ {
165
+ add_option(
166
+ 'cachify',
167
+ array(
168
+ 'only_guests'=> 1,
169
+ 'compress_html'=> 0,
170
+ 'cache_expires'=> 12,
171
+ 'without_ids'=> '',
172
+ 'without_agents' => ''
173
+ ),
174
+ '',
175
+ 'no'
176
+ );
177
+ self::flush_cache();
178
+ }
179
+ public static function uninstall()
180
+ {
181
+ global $wpdb;
182
+ if ( is_multisite() && !empty($_GET['networkwide']) ) {
183
+ $old = $wpdb->blogid;
184
+ $ids = self::_get_blog_ids();
185
+ foreach ($ids as $id) {
186
+ switch_to_blog($id);
187
+ self::_uninstall_backend();
188
+ }
189
+ switch_to_blog($old);
190
+ } else {
191
+ self::_uninstall_backend();
192
+ }
193
+ }
194
+ public static function uninstall_later($id) {
195
+ global $wpdb;
196
+ if ( !is_plugin_active_for_network(self::$base) ) {
197
+ return;
198
+ }
199
+ switch_to_blog( (int)$id );
200
+ self::_uninstall_backend();
201
+ restore_current_blog();
202
+ }
203
+ private static function _uninstall_backend()
204
+ {
205
+ delete_option('cachify');
206
+ self::flush_cache();
207
+ }
208
+ public static function update()
209
+ {
210
+ self::_update_backend();
211
+ }
212
+ private static function _update_backend()
213
+ {
214
+ self::flush_cache();
215
+ }
216
+ private static function _get_blog_ids()
217
+ {
218
+ global $wpdb;
219
+ return $wpdb->get_col(
220
+ $wpdb->prepare("SELECT blog_id FROM `$wpdb->blogs`")
221
+ );
222
+ }
223
+ public static function action_links($data)
224
+ {
225
+ if ( !current_user_can('manage_options') ) {
226
+ return $data;
227
+ }
228
+ return array_merge(
229
+ $data,
230
+ array(
231
+ sprintf(
232
+ '<a href="%s">%s</a>',
233
+ add_query_arg(
234
+ array(
235
+ 'page' => 'cachify'
236
+ ),
237
+ admin_url('options-general.php')
238
+ ),
239
+ __('Settings')
240
+ )
241
+ )
242
+ );
243
+ }
244
+ public static function row_meta($data, $page)
245
+ {
246
+ if ( $page == self::$base && current_user_can('manage_options') ) {
247
+ $data = array_merge(
248
+ $data,
249
+ array(
250
+ '<a href="http://flattr.com/thing/114377/Cachify-Handliches-Cache-Plugin-fur-WordPress" target="_blank">Plugin flattern</a>',
251
+ '<a href="https://plus.google.com/110569673423509816572" target="_blank">Auf Google+ folgen</a>',
252
+ sprintf(
253
+ '<a href="%s">Cache leeren</a>',
254
+ add_query_arg('_cachify', 'flush', 'plugins.php')
255
+ )
256
+ )
257
+ );
258
+ }
259
+ return $data;
260
+ }
261
+ public static function add_menu( $wp_admin_bar ) {
262
+ if ( !function_exists('is_admin_bar_showing') or !is_admin_bar_showing() or !is_object($wp_admin_bar) or !is_super_admin() ) {
263
+ return;
264
+ }
265
+ $wp_admin_bar->add_menu(
266
+ array(
267
+ 'id'=> 'cachify',
268
+ 'title' => '<span class="ab-icon"></span><span class="ab-label">Cache leeren</span>',
269
+ 'href'=> add_query_arg('_cachify', 'flush')
270
+ )
271
+ );
272
+ }
273
+ public static function receive_flush($data)
274
+ {
275
+ if ( empty($_GET['_cachify']) or $_GET['_cachify'] !== 'flush' ) {
276
+ return;
277
+ }
278
+ global $wpdb;
279
+ if ( is_multisite() && is_plugin_active_for_network(self::$base) ) {
280
+ $old = $wpdb->blogid;
281
+ $ids = self::_get_blog_ids();
282
+ foreach ($ids as $id) {
283
+ switch_to_blog($id);
284
+ self::flush_cache();
285
+ }
286
+ switch_to_blog($old);
287
+ add_action(
288
+ 'network_admin_notices',
289
+ array(
290
+ __CLASS__,
291
+ 'flush_notice'
292
+ )
293
+ );
294
+ } else {
295
+ self::flush_cache();
296
+ add_action(
297
+ 'admin_notices',
298
+ array(
299
+ __CLASS__,
300
+ 'flush_notice'
301
+ )
302
+ );
303
+ }
304
+ }
305
+ public static function flush_notice() {
306
+ if ( !is_super_admin() ) {
307
+ return false;
308
+ }
309
+ echo '<div id="message" class="updated"><p><strong>Cachify-Cache geleert.</strong></p></div>';
310
+ }
311
+ public static function edit_comment($id)
312
+ {
313
+ self::_delete_cache(
314
+ get_permalink(
315
+ get_comment($id)->comment_post_ID
316
+ )
317
+ );
318
+ }
319
+ public static function add_comment($comment)
320
+ {
321
+ self::_delete_cache(
322
+ get_permalink($comment['comment_post_ID'])
323
+ );
324
+ return $comment;
325
+ }
326
+ public static function touch_comment($new_status, $old_status, $comment)
327
+ {
328
+ if ( $new_status != $old_status ) {
329
+ self::_delete_cache(
330
+ get_permalink($comment->comment_post_ID)
331
+ );
332
+ }
333
+ }
334
+ public static function publish_post($id)
335
+ {
336
+ $post = get_post($id);
337
+ if ( in_array( $post->post_status, array('publish', 'future') ) ) {
338
+ self::flush_cache();
339
+ }
340
+ }
341
+ public static function publish_page($id)
342
+ {
343
+ $page = get_page($id);
344
+ if ( $page->post_status == 'publish' ) {
345
+ self::flush_cache();
346
+ }
347
+ }
348
+ private static function _cache_hash($url = '')
349
+ {
350
+ if ( empty($url) ) {
351
+ $url = esc_url_raw(
352
+ sprintf(
353
+ '%s://%s%s',
354
+ (is_ssl() ? 'https' : 'http'),
355
+ $_SERVER['HTTP_HOST'],
356
+ $_SERVER['REQUEST_URI']
357
+ )
358
+ );
359
+ }
360
+ return 'cachify_' .md5($url);
361
+ }
362
+ private static function _page_queries()
363
+ {
364
+ return $GLOBALS['wpdb']->num_queries;
365
+ }
366
+ private static function _page_timer()
367
+ {
368
+ return timer_stop(0, 2);
369
+ }
370
+ private static function _memory_usage()
371
+ {
372
+ return ( function_exists('memory_get_usage') ? size_format(memory_get_usage(), 2) : 0 );
373
+ }
374
+ private static function _preg_split($input)
375
+ {
376
+ return (array)preg_split('/,/', $input, -1, PREG_SPLIT_NO_EMPTY);
377
+ }
378
+ private static function _is_index()
379
+ {
380
+ return basename($_SERVER['SCRIPT_NAME']) != 'index.php';
381
+ }
382
+ private static function _is_mobile()
383
+ {
384
+ return ( strpos(TEMPLATEPATH, 'wptouch') or strpos(TEMPLATEPATH, 'carrington') );
385
+ }
386
+ private static function _skip_cache()
387
+ {
388
+ $options = get_option('cachify');
389
+ if ( self::_is_index() or is_feed() or is_trackback() or is_robots() or is_preview() or post_password_required() or ( $options['only_guests'] && is_user_logged_in() ) ) {
390
+ return true;
391
+ }
392
+ if ( self::_is_mobile() ) {
393
+ return true;
394
+ }
395
+ if ( $options['without_ids'] && is_singular() ) {
396
+ if ( in_array( $GLOBALS['wp_query']->get_queried_object_id(), self::_preg_split($options['without_ids']) ) ) {
397
+ return true;
398
+ }
399
+ }
400
+ if ( $options['without_agents'] && isset($_SERVER['HTTP_USER_AGENT']) ) {
401
+ if ( array_filter( self::_preg_split($options['without_agents']), create_function('$a', 'return strpos($_SERVER["HTTP_USER_AGENT"], $a);') ) ) {
402
+ return true;
403
+ }
404
+ }
405
+ return false;
406
+ }
407
+ private static function _sanitize_cache($data) {
408
+ $options = get_option('cachify');
409
+ if ( !$options['compress_html'] ) {
410
+ return($data);
411
+ }
412
+ $cleaned = preg_replace(
413
+ array(
414
+ '/\<!--.+?--\>/s',
415
+ '/\>(\s)+(\S)/s',
416
+ '/\>[^\S ]+/s',
417
+ '/[^\S ]+\</s',
418
+ '/\>(\s)+/s',
419
+ '/(\s)+\</s',
420
+ '/\>\s+\</s'
421
+ ),
422
+ array(
423
+ '',
424
+ '>\\1\\2',
425
+ '>',
426
+ '<',
427
+ '>\\1',
428
+ '\\1<',
429
+ '> <'
430
+ ),
431
+ (string)$data
432
+ );
433
+ if ( strlen($cleaned) <= 1 ) {
434
+ return($data);
435
+ }
436
+ return($cleaned);
437
+ }
438
+ private static function _delete_cache($url)
439
+ {
440
+ delete_transient(
441
+ self::_cache_hash($url)
442
+ );
443
+ }
444
+ public static function flush_cache()
445
+ {
446
+ $GLOBALS['wpdb']->query("DELETE FROM `" .$GLOBALS['wpdb']->options. "` WHERE `option_name` LIKE ('_transient%_cachify_%')");
447
+ $GLOBALS['wpdb']->query("OPTIMIZE TABLE `" .$GLOBALS['wpdb']->options. "`");
448
+ }
449
+ public static function set_cache($data)
450
+ {
451
+ $options = get_option('cachify');
452
+ if ( !empty($data) ) {
453
+ set_transient(
454
+ self::_cache_hash(),
455
+ array(
456
+ 'data'=> self::_sanitize_cache($data),
457
+ 'queries'=> self::_page_queries(),
458
+ 'timer'=> self::_page_timer(),
459
+ 'memory'=> self::_memory_usage(),
460
+ 'time'=> current_time('timestamp')
461
+ ),
462
+ 60 * 60 * (int)$options['cache_expires']
463
+ );
464
+ }
465
+ return $data;
466
+ }
467
+ public static function manage_cache()
468
+ {
469
+ if ( self::_skip_cache() ) {
470
+ return;
471
+ }
472
+ if ( $cache = get_transient(self::_cache_hash()) ) {
473
+ if ( !empty($cache['data']) ) {
474
+ echo $cache['data'];
475
+ echo sprintf(
476
+ "\n\n<!--\n%s\n%s\n%s\n%s\n-->",
477
+ 'Cachify für WordPress | http://bit.ly/cachify',
478
+ sprintf(
479
+ 'Ohne Cachify: %d DB-Anfragen, %s Sekunden, %s',
480
+ $cache['queries'],
481
+ $cache['timer'],
482
+ $cache['memory']
483
+ ),
484
+ sprintf(
485
+ 'Mit Cachify: %d DB-Anfragen, %s Sekunden, %s',
486
+ self::_page_queries(),
487
+ self::_page_timer(),
488
+ self::_memory_usage()
489
+ ),
490
+ sprintf(
491
+ 'Generiert: %s zuvor',
492
+ human_time_diff($cache['time'], current_time('timestamp'))
493
+ )
494
+ );
495
+ exit;
496
+ }
497
+ }
498
+ ob_start('Cachify::set_cache');
499
+ }
500
+ function add_css()
501
+ {
502
+ $data = get_plugin_data(__FILE__);
503
+ wp_register_style(
504
+ 'cachify_css',
505
+ plugins_url('css/style.css', __FILE__),
506
+ array(),
507
+ $data['Version']
508
+ );
509
+ wp_enqueue_style('cachify_css');
510
+ }
511
+ function add_page()
512
+ {
513
+ add_options_page(
514
+ 'Cachify',
515
+ '<img src="' .plugins_url('cachify/img/icon.png'). '" alt="Cachify" />Cachify',
516
+ 'manage_options',
517
+ 'cachify',
518
+ array(
519
+ __CLASS__,
520
+ 'options_page'
521
+ )
522
+ );
523
+ }
524
+ function help_link($anchor) {
525
+ echo sprintf(
526
+ '<span>[<a href="http://playground.ebiene.de/2652/cachify-wordpress-cache/#%s" target="_blank">?</a>]</span>',
527
+ $anchor
528
+ );
529
+ }
530
+ function register_settings()
531
+ {
532
+ register_setting(
533
+ 'cachify',
534
+ 'cachify',
535
+ array(
536
+ __CLASS__,
537
+ 'validate_options'
538
+ )
539
+ );
540
+ }
541
+ public static function validate_options($data)
542
+ {
543
+ self::flush_cache();
544
+ return array(
545
+ 'only_guests'=> (int)(!empty($data['only_guests'])),
546
+ 'compress_html'=> (int)(!empty($data['compress_html'])),
547
+ 'cache_expires'=> (int)(@$data['cache_expires']),
548
+ 'without_ids'=> (string)sanitize_text_field(@$data['without_ids']),
549
+ 'without_agents' => (string)sanitize_text_field(@$data['without_agents'])
550
+ );
551
+ }
552
+ public static function options_page()
553
+ { ?>
554
+ <div class="wrap">
555
+ <?php screen_icon('cachify') ?>
556
+ <h2>
557
+ Cachify
558
+ </h2>
559
+ <form method="post" action="options.php">
560
+ <?php settings_fields('cachify') ?>
561
+ <?php $options = get_option('cachify') ?>
562
+ <table class="form-table cachify">
563
+ <tr>
564
+ <th>
565
+ Cache-Gültigkeit in Stunden <?php self::help_link('cache_expires') ?>
566
+ </th>
567
+ <td>
568
+ <input type="text" name="cachify[cache_expires]" value="<?php echo $options['cache_expires'] ?>" />
569
+ </td>
570
+ </tr>
571
+ <tr>
572
+ <th>
573
+ Ausnahme für (Post/Pages) IDs <?php self::help_link('without_ids') ?>
574
+ </th>
575
+ <td>
576
+ <input type="text" name="cachify[without_ids]" value="<?php echo $options['without_ids'] ?>" />
577
+ </td>
578
+ </tr>
579
+ <tr>
580
+ <th>
581
+ Ausnahme für User Agents <?php self::help_link('without_agents') ?>
582
+ </th>
583
+ <td>
584
+ <input type="text" name="cachify[without_agents]" value="<?php echo $options['without_agents'] ?>" />
585
+ </td>
586
+ </tr>
587
+ <tr>
588
+ <th>
589
+ Komprimierung der Ausgabe <?php self::help_link('compress_html') ?>
590
+ </th>
591
+ <td>
592
+ <input type="checkbox" name="cachify[compress_html]" value="1" <?php checked('1', $options['compress_html']); ?> />
593
+ </td>
594
+ </tr>
595
+ <tr>
596
+ <th>
597
+ Nur für nicht eingeloggte Nutzer <?php self::help_link('only_guests') ?>
598
+ </th>
599
+ <td>
600
+ <input type="checkbox" name="cachify[only_guests]" value="1" <?php checked('1', $options['only_guests']); ?> />
601
+ </td>
602
+ </tr>
603
+ </table>
604
+ <p class="submit">
605
+ <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
606
+ </p>
607
+ </form>
608
+ </div><?php
609
+ }
610
+ }
611
+ add_action(
612
+ 'plugins_loaded',
613
+ array(
614
+ 'Cachify',
615
+ 'init'
616
+ ),
617
+ 99
618
+ );
619
+ register_activation_hook(
620
+ __FILE__,
621
+ array(
622
+ 'Cachify',
623
+ 'install'
624
+ )
625
+ );
626
+ register_uninstall_hook(
627
+ __FILE__,
628
+ array(
629
+ 'Cachify',
630
+ 'uninstall'
631
+ )
632
+ );
633
+ if ( function_exists('register_update_hook') ) {
634
+ register_update_hook(
635
+ __FILE__,
636
+ array(
637
+ 'Cachify',
638
+ 'update'
639
+ )
640
+ );
641
+ }
css/style.css ADDED
@@ -0,0 +1 @@
 
1
+ #icon-cachify{background:url(../img/icon32.png) no-repeat;}.wp-submenu a img[alt="Cachify"]{width:11px;height:9px;border:0;}table.form-table.cachify th{width:220px;line-height:26px;}table.form-table.cachify th span{font-size:11px;white-space:nowrap;}table.form-table.cachify th span a{padding:0 1px;text-decoration:none;}#wp-admin-bar-cachify .ab-label{margin-left:4px;}#wp-admin-bar-cachify .ab-icon{position:relative;float:left;width:16px;height:16px;margin-top:6px;background:url(../img/icon16.png) no-repeat 0 -1px;}
css/style.original.css ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #icon-cachify {
2
+ background: url(../img/icon32.png) no-repeat;
3
+ }
4
+
5
+ .wp-submenu a img[alt="Cachify"] {
6
+ width: 11px;
7
+ height: 9px;
8
+ border: 0;
9
+ }
10
+
11
+ table.form-table.cachify th {
12
+ width: 220px;
13
+ line-height: 26px;
14
+ }
15
+ table.form-table.cachify th span {
16
+ font-size: 11px;
17
+ white-space: nowrap;
18
+ }
19
+ table.form-table.cachify th span a {
20
+ padding: 0 1px;
21
+ text-decoration: none;
22
+ }
23
+
24
+ #wp-admin-bar-cachify .ab-label {
25
+ margin-left: 4px;
26
+ }
27
+ #wp-admin-bar-cachify .ab-icon {
28
+ position: relative;
29
+ float: left;
30
+ width: 16px;
31
+ height: 16px;
32
+ margin-top: 6px;
33
+ background: url(../img/icon16.png) no-repeat 0 -1px;
34
+ }
img/icon.png ADDED
Binary file
img/icon16.png ADDED
Binary file
img/icon32.png ADDED
Binary file
readme.txt ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Cachify ===
2
+ Contributors: sergej.mueller
3
+ Tags: performance,caching,cache
4
+ Donate link: http://flattr.com/profile/sergej.mueller
5
+ Requires at least: 3.0
6
+ Tested up to: 3.3
7
+ Stable tag: trunk
8
+
9
+ Simple und effiziente Cache-Lösung für WordPress.
10
+
11
+
12
+ == Description ==
13
+ = Unkompliziert und ausbaufähig =
14
+ Cachify optimiert die Ladezeit der Blogseiten, indem Seiteninhalte in statischer Form in der Datenbank abgelegt und beim Seitenaufruf direkt ausgegeben werden. Dabei wird die Anzahl der DB-Anfragen und PHP-Anweisungen reduziert.
15
+
16
+ = Einige Stärken =
17
+ * NEU: Adminbar-Schaltfläche "Cache leeren"
18
+ * Einfache Installation: Aktivieren, fertig
19
+ * Übersichtliche Optionsseite mit verknüpfter Online-Hilfe
20
+ * Optionale Komprimierung der HTML-Ausgabe
21
+ * Ausnahmelisten für PostIDs und User Agents
22
+ * Bis zu 80 % weniger DB-Anfragen
23
+ * Bis zu 60 % schnellere Ausführungszeiten
24
+ * Manueller und automatischer Cache-Reset
25
+ * Ausgabe der "davor, danach" Informationen im Quelltext
26
+
27
+ = Wichtige Information =
28
+ Vor der Inbetriebnahme des Plugins ist die [Dokumentation](http://playground.ebiene.de/2652/cachify-wordpress-cache/ "Cachify WordPress Cache") durchzulesen, um eine inkorrekte Funktionsweise und negative Auswirkungen des Cache-Tools zu vermeiden!
29
+
30
+ = Weiterführende Links =
31
+ * [Google+](https://plus.google.com/110569673423509816572 "Google+")
32
+ * [Other plugins](http://wordpress.org/extend/plugins/profile/sergejmueller "Other plugins")
33
+
34
+
35
+ == Changelog ==
36
+
37
+ = 1.2.1 =
38
+ * Icon für die "Cache leeren" Schaltfläche in der Admin Bar
39
+
40
+ = 1.2 =
41
+ * Schaltfläche "Cache leeren" in der Adminbar (ab WordPress 3.1)
42
+ * `flush_cache` auf public gesetzt, um von [wpSEO](http://wpseo.de "WordPress SEO Plugin") ansprechen zu können
43
+ * Ausführliche Tests unter WordPress 3.3
44
+
45
+ = 1.1 =
46
+ * Interne Prüfung auf fehlerhafte Cache-Generierung
47
+ * Anpassungen an der Code-Struktur
48
+ * Entfernung der Inline-Hilfe
49
+ * Verknüpfung der Online-Hilfe mit Optionen
50
+
51
+ = 1.0 =
52
+ * Leerung des Cache beim Aktualisieren von statischen Seiten
53
+ * Seite mit Plugin-Einstellungen
54
+ * Inline-Dokumentation in der Optionsseite
55
+ * Ausschluss von Passwort-geschützten Seiten
56
+ * WordPress 3.2 Support
57
+ * Unterstützung der WordPress Multisite Blogs
58
+ * Umstellung auf den template_redirect-Hook (Plugin-Kompatibilität)
59
+ * Interne Code-Bereinigung
60
+
61
+ = 0.9.2 =
62
+ * HTML-Kompression
63
+ * Flattr-Link
64
+
65
+ = 0.9.1 =
66
+ * Cache-Reset bei geplanten Beiträgen
67
+ * Unterstützung für das Carrington-Mobile Theme
68
+
69
+ = 0.9 =
70
+ * Workaround für Redirects
71
+
72
+ = 0.8 =
73
+ * Blacklist für PostIDs
74
+ * Blacklist für UserAgents
75
+ * Ausnahme für WP Touch
76
+ * Ausgabe des Zeitpunktes der Generierung
77
+ * Umbenennung der Konstanten
78
+
79
+ = 0.7 =
80
+ * Ausgabe des Speicherverbrauchs
81
+
82
+ = 0.6 =
83
+ * Live auf wordpress.org
84
+
85
+
86
+ == Screenshots ==
87
+
88
+ 1. Cachify Optionsseite
89
+
90
+
91
+ == Installation ==
92
+ 1. *Cachify* installieren
93
+ 1. [Dokumentation](http://playground.ebiene.de/2652/cachify-wordpress-cache/ "Cachify WordPress Cache") beachten
94
+ 1. Einstellungen vornehmen
screenshot-1.png ADDED
Binary file