This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
wp-optimize-common.php
943 lines (777 loc) · 33.5 KB
/
wp-optimize-common.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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
<?php
# --------------------------------------- #
# prevent file from being accessed directly
# --------------------------------------- #
if ( ! defined( 'WPINC' ) ) {
die;
}
// common functions
if (! defined('WPO_PLUGIN_MAIN_PATH'))
define('WPO_PLUGIN_MAIN_PATH', plugin_dir_path( __FILE__ ));
if (! defined('WPO_PLUGIN_URL'))
define('WPO_PLUGIN_URL', plugin_dir_url( __FILE__ ));
if (! defined('OPTION_NAME_SCHEDULE'))
define('OPTION_NAME_SCHEDULE', 'wp-optimize-schedule');
if (! defined('OPTION_NAME_SCHEDULE_TYPE'))
define('OPTION_NAME_SCHEDULE_TYPE', 'wp-optimize-schedule-type');
if (! defined('OPTION_NAME_RETENTION_ENABLED'))
define('OPTION_NAME_RETENTION_ENABLED', 'wp-optimize-retention-enabled');
if (! defined('OPTION_NAME_RETENTION_PERIOD'))
define('OPTION_NAME_RETENTION_PERIOD', 'wp-optimize-retention-period');
if (! defined('OPTION_NAME_LAST_OPT'))
define('OPTION_NAME_LAST_OPT', 'wp-optimize-last-optimized');
if (! defined('OPTION_NAME_ENABLE_ADMIN_MENU'))
define('OPTION_NAME_ENABLE_ADMIN_MENU', 'wp-optimize-enable-admin-menu');
if (! defined('OPTION_NAME_TOTAL_CLEANED'))
define('OPTION_NAME_TOTAL_CLEANED', 'wp-optimize-total-cleaned');
if (! defined('OPTION_NAME_CURRENT_CLEANED'))
define('OPTION_NAME_CURRENT_CLEANED', 'wp-optimize-current-cleaned');
if (! defined('OPTION_NAME_ENABLE_EMAIL_ADDRESS'))
define('OPTION_NAME_ENABLE_EMAIL_ADDRESS', 'wp-optimize-email-address');
if (! defined('OPTION_NAME_ENABLE_EMAIL'))
define('OPTION_NAME_ENABLE_EMAIL', 'wp-optimize-email');
/**
* wpo_sendemail($sendto, $msg)
* @return success
* @param $sentdo - eg. who to send it to, abc@def.com
* @param $msg - the msg in text
*/
function wpo_sendEmail($date, $cleanedup){
//
ob_start();
// #TODO this need to work on - currently not using the parameter values
$myTime = current_time( "timestamp", 0 );
$myDate = gmdate(get_option('date_format') . ' ' . get_option('time_format'), $myTime );
//$formattedCleanedup = wpo_format_size($cleanedup);
if ( get_option( OPTION_NAME_ENABLE_EMAIL_ADDRESS ) !== "" ) {
//
$sendto = get_option( OPTION_NAME_ENABLE_EMAIL_ADDRESS );
}
else{
$sendto = get_bloginfo ( 'admin_email' );
}
//$thiscleanup = wpo_format_size($cleanedup);
$subject = get_bloginfo ( 'name' ).": ".__("Automatic Operation Completed","wp-optimize")." ".$myDate;
$msg = __("Scheduled optimization was executed at","wp-optimize")." ".$myDate."\r\n"."\r\n";
//$msg .= __("Recovered space","wp-optimize").": ".$thiscleanup."\r\n";
$msg .= __("You can safely delete this email.","wp-optimize")."\r\n";
$msg .= "\r\n";
$msg .= __("Regards,","wp-optimize")."\r\n";
$msg .= __("WP-Optimize Plugin","wp-optimize");
//wp_mail( $sendto, $subject, $msg );
ob_end_flush();
}
/**
* wpo_readFeed($rss_url, $number_of_itmes)
* @return RSS items
* @param $rss_url - url of RSS feed
* @param number of items - number of items to return
*/
function wpo_readFeed($rss_url, $number_of_itmes){
include_once( ABSPATH . WPINC . '/feed.php' );
$rss = fetch_feed( $rss_url );
if ( ! is_wp_error( $rss ) ) { // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity( $number_of_itmes );
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items( 0, $maxitems );
if ($maxitems > 0) {
return $rss_items;
}
}
else {
$rss_items = NULL;
return $rss_items;
}
}
/**
* wpo_detectDBType()
* this function is redundant
* @return void
*/
function wpo_detectDBType() {
global $wpdb;
//global $table_prefix;
$tablestype = $wpdb->get_results("SHOW TABLE STATUS WHERE Name = `$wpdb->options`");
foreach($tablestype as $tabletype) {
$table_engine = $tabletype->Engine;
}
$wpo_table_type = strtolower(strval($table_engine));
//if (! defined('WPO_TABLE_TYPE'))
// define( WPO_TABLE_TYPE,$wpo_table_type);
return $wpo_table_type;
}
/*
* function wpo_getRetainInfo()
*
* parameters: none
*
* it returns 2 options values
*
* @return (array of enabled state, period)
*/
function wpo_getRetainInfo(){
$retain_enabled = get_option(OPTION_NAME_RETENTION_ENABLED, 'false' );
if ($retain_enabled){
$retain_period = get_option(OPTION_NAME_RETENTION_PERIOD, '2');
}
return array ($retain_enabled, $retain_period);
}
/*
* function wpo_disable_linkbacks()
*
* parameters: message to debug
*
*
*
* @return none
*/
function wpo_disableLinkbacks($type) {
global $wpdb;
switch ($type) {
case "trackbacks":
$thissql = "UPDATE `$wpdb->posts` SET ping_status='closed' WHERE post_status = 'publish' AND post_type = 'post'";
$thissql .= ';';
$trackbacks = $wpdb->query( $thissql );
break;
case "comments":
$thissql = "UPDATE `$wpdb->posts` SET comment_status='closed' WHERE post_status = 'publish' AND post_type = 'post'";
$thissql .= ';';
$comments = $wpdb->query( $thissql );
break;
default:
//;
break;
}
}
/*
* function wpo_disable_linkbacks()
*
* parameters: message to debug
*
*
*
* @return none
*/
function wpo_enableLinkbacks($type) {
global $wpdb;
switch ($type) {
case "trackbacks":
$thissql = "UPDATE `$wpdb->posts` SET ping_status='open' WHERE post_status = 'publish' AND post_type = 'post'";
$thissql .= ';';
$trackbacks = $wpdb->query( $thissql );
break;
case "comments":
$thissql = "UPDATE `$wpdb->posts` SET comment_status='open' WHERE post_status = 'publish' AND post_type = 'post'";
$thissql .= ';';
$comments = $wpdb->query( $thissql );
break;
default:
//;
break;
}
}
/*
* function wpo_debugLog()
*
* parameters: message to debug
*
*
*
* @return none
*/
function wpo_debugLog($message) {
if (WP_DEBUG === true) {
if (is_array($message) || is_object($message)) {
error_log(print_r($message, true));
} else {
error_log($message);
}
}
}
/*
* function wpo_headerImage()
*
* parameters: none
*
* it returns header image and fb code
*
* @return $text
*/
function wpo_headerImage(){
$text = '<img src="'.WPO_PLUGIN_URL.'/wp-optimize.png" border="0" alt="WP-Optimize" title="WP-Optimize" width="310px" height="auto"/><br />';
//$text .='<iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.ruhanirabin.com%2Fwp-optimize%2F&width=310&height=46&colorscheme=light&layout=standard&action=like&show_faces=false&send=true&" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:310px; height:46px;" allowTransparency="true"></iframe>';
echo $text;
}
/*
* function wpo_removeOptions()
*
* parameters: none
*
* deletes all option values from wp_options table
*
* @return none
*/
function wpo_removeOptions(){
delete_option( 'wp-optimize-weekly-schedule' );
delete_option( OPTION_NAME_SCHEDULE );
delete_option( OPTION_NAME_RETENTION_ENABLED );
delete_option( OPTION_NAME_RETENTION_PERIOD );
delete_option( OPTION_NAME_LAST_OPT );
delete_option( OPTION_NAME_ENABLE_ADMIN_MENU );
delete_option( OPTION_NAME_SCHEDULE_TYPE );
delete_option( OPTION_NAME_TOTAL_CLEANED );
delete_option( OPTION_NAME_CURRENT_CLEANED );
delete_option( OPTION_NAME_ENABLE_EMAIL_ADDRESS );
delete_option( OPTION_NAME_ENABLE_EMAIL );
delete_option( 'wp-optimize-auto' );
delete_option( 'wp-optimize-settings' );
}
/*
* function wpo_cron_action()
*
* parameters: none
*
* executed this function on cron event
*
* @return none
*/
function wpo_cron_action() {
global $wpdb;
list ($retention_enabled, $retention_period) = wpo_getRetainInfo();
wpo_debugLog('Starting wpo_cron_action()');
if ( get_option(OPTION_NAME_SCHEDULE) == 'true') {
$this_options = get_option('wp-optimize-auto');
// revisions
if ($this_options['revisions'] == 'true'){
$clean = "DELETE FROM `$wpdb->posts` WHERE post_type = 'revision'";
if ($retention_enabled == 'true') {
$clean .= ' and post_modified < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$clean .= ';';
$revisions = $wpdb->query( $clean );
}
// auto drafts
if ($this_options['drafts'] == 'true'){
$clean = "DELETE FROM `$wpdb->posts` WHERE post_status = 'auto-draft'";
if ($retention_enabled == 'true') {
$clean .= ' and post_modified < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$clean .= ';';
$autodraft = $wpdb->query( $clean );
// trash posts
// TODO: query trashed posts and cleanup metadata
$clean = "DELETE FROM `$wpdb->posts` WHERE post_status = 'trash'";
if ($retention_enabled == 'true') {
$clean .= ' and post_modified < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$clean .= ';';
$posttrash = $wpdb->query( $clean );
}
// spam comments
if ($this_options['spams'] == 'true'){
$clean = "DELETE FROM `$wpdb->comments` WHERE comment_approved = 'spam'";
if ($retention_enabled == 'true') {
$clean .= ' and comment_date < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$clean .= ';';
$comments = $wpdb->query( $clean );
// trashed comments
// TODO: query trashed comments and cleanup metadata
$clean = "DELETE FROM `$wpdb->comments` WHERE comment_approved = 'trash'";
if ($retention_enabled == 'true') {
$clean .= ' and comment_date < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$clean .= ';';
$commentstrash = $wpdb->query( $clean );
// TODO: still need to test now cleaning up comments meta tables
$clean = "DELETE FROM `$wpdb->commentmeta` WHERE comment_id NOT IN ( SELECT comment_id FROM `$wpdb->comments` )";
$clean .= ';';
//$commentstrash1 = $wpdb->query( $clean );
}
// transient options
if ($this_options['transient'] == 'true'){
$clean = "DELETE FROM `$wpdb->options` WHERE option_name LIKE '_site_transient_browser_%' OR option_name LIKE '_site_transient_timeout_browser_%' OR option_name LIKE '_transient_feed_%' OR option_name LIKE '_transient_timeout_feed_%'";
$clean .= ';';
$transient = $wpdb->query( $clean );
}
// postmeta
// TODO: refactor this with proper query
if ($this_options['postmeta'] == 'true'){
$clean = "DELETE pm FROM `$wpdb->postmeta` pm LEFT JOIN `$wpdb->posts` wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL";
$clean .= ';';
//$postmeta = $wpdb->query( $clean );
}
// unused tags
if ($this_options['tags'] == 'true'){
//$clean = "DELETE t,tt FROM `$wpdb->terms` t INNER JOIN `$wpdb->term_taxonomy` tt ON t.term_id=tt.term_id WHERE tt.taxonomy='post_tag' AND tt.count=0";
//$clean .= ';';
//$tags = $wpdb->query( $clean );
}
//db optimize part - optimize
if ($this_options['optimize'] == 'true'){
$db_tables = $wpdb->get_results('SHOW TABLES',ARRAY_A);
foreach ($db_tables as $table){
$t = array_values($table);
$wpdb->query("OPTIMIZE TABLE `".$t[0]."`");
wpo_debugLog('optimizing .... '.$t[0]);
}
ob_start();
list($part1, $part2) = wpo_getCurrentDBSize();
$thistime = current_time( "timestamp", 0 );
$thedate = gmdate(get_option('date_format') . ' ' . get_option('time_format'), $thistime );
update_option( OPTION_NAME_LAST_OPT, $thedate );
wpo_updateTotalCleaned(strval($part2));
// Sending notification email
if ( get_option( OPTION_NAME_ENABLE_EMAIL ) !== false ) {
//#TODO need to fix the problem with variable value not passing through
if ( get_option( OPTION_NAME_ENABLE_EMAIL_ADDRESS ) !== '' ) {
//wpo_sendEmail($thedate, $part2);
}
}
else{
//
}
ob_end_flush();
} // endif $this_options['optimize']
} // end if ( get_option(OPTION_NAME_SCHEDULE) == 'true')
}
/*
* function wpo_PluginOptionsSetDefaults()
*
* parameters: none
*
* setup options if not exists already
*
* @return none
*/
function wpo_PluginOptionsSetDefaults() {
$deprecated = null;
$autoload = 'no';
if ( get_option( OPTION_NAME_SCHEDULE ) !== false ) {
// The option already exists, so we just update it.
} else {
// The option hasn't been added yet. We'll add it with $autoload set to 'no'.
add_option( OPTION_NAME_SCHEDULE, 'false', $deprecated, $autoload );
add_option( OPTION_NAME_LAST_OPT, 'Never', $deprecated, $autoload );
add_option( OPTION_NAME_SCHEDULE_TYPE, 'wpo_weekly', $deprecated, $autoload );
// deactivate cron
wpo_cron_deactivate();
}
if ( get_option( OPTION_NAME_RETENTION_ENABLED ) !== false ) {
//
}
else{
add_option( OPTION_NAME_RETENTION_ENABLED, 'false', $deprecated, $autoload );
add_option( OPTION_NAME_RETENTION_PERIOD, '2', $deprecated, $autoload );
}
if ( get_option( OPTION_NAME_ENABLE_ADMIN_MENU ) !== false ) {
//
}
else{
add_option( OPTION_NAME_ENABLE_ADMIN_MENU, 'false', $deprecated, $autoload );
}
// ---------
if ( get_option( OPTION_NAME_ENABLE_EMAIL ) !== false ) {
//
}
else{
//add_option( OPTION_NAME_ENABLE_EMAIL, 'true', $deprecated, $autoload );
}
// ---------
if ( get_option( OPTION_NAME_ENABLE_EMAIL_ADDRESS ) !== '' ) {
//
}
else{
//add_option( OPTION_NAME_ENABLE_EMAIL_ADDRESS, get_bloginfo ( 'admin_email' ), $deprecated, $autoload );
}
if ( get_option( OPTION_NAME_TOTAL_CLEANED ) !== false ) {
//
}
else{
add_option( OPTION_NAME_TOTAL_CLEANED, '0', $deprecated, $autoload );
}
if ( get_option( 'wp-optimize-auto' ) !== false ) {
// The option already exists, so we just update it.
} else {
// 'revisions', 'drafts', 'spams', 'unapproved', 'transient', 'postmeta', 'tags'
$new_options = array(
'revisions' => 'true',
'drafts' => 'true',
'spams' => 'true',
'unapproved' => 'false',
'transient' => 'false',
'postmeta' => 'false',
'tags' => 'false',
'optimize' => 'true'
);
update_option( 'wp-optimize-auto', $new_options );
}
// settings for main screen
if ( get_option( 'wp-optimize-settings' ) !== false ) {
// The option already exists, so we just update it.
} else {
// 'revisions', 'drafts', 'spams', 'unapproved', 'transient', 'postmeta', 'tags'
$new_options_main = array(
'user-revisions' => 'true',
'user-drafts' => 'true',
'user-spams' => 'true',
'user-unapproved' => 'true',
'user-transient' => 'false',
'user-optimize' => 'true'
);
update_option( 'wp-optimize-settings', $new_options_main );
}
}
/**
* wpo_format_size()
* Function: Format Bytes Into KB/MB
* @param mixed $rawSize
* @return
*/
if(!function_exists('wpo_format_size')) {
function wpo_format_size($rawSize) {
if($rawSize / 1073741824 > 1)
return number_format_i18n($rawSize/1073741824, 2) . ' '.__('GB', 'wp-optimize');
else if ($rawSize / 1048576 > 1)
return number_format_i18n($rawSize/1048576, 1) . ' '.__('MB', 'wp-optimize');
else if ($rawSize / 1024 > 1)
return number_format_i18n($rawSize/1024, 1) . ' '.__('KB', 'wp-optimize');
else
return number_format_i18n($rawSize, 0) . ' '.__('bytes', 'wp-optimize');
}
}
/*
* function wpo_getCurrentDBSize()
*
* parameters: none
*
* this function will return total database size and a possible gain of db in KB
*
* @return array $total size, $gain
*/
function wpo_getCurrentDBSize(){
global $wpdb;
$total_gain = 0;
$total_size = 0;
$no = 0;
$row_usage = 0;
$data_usage = 0;
$index_usage = 0;
$overhead_usage = 0;
$tablesstatus = $wpdb->get_results("SHOW TABLE STATUS");
wpo_debugLog('Checking DB size .... ');
foreach($tablesstatus as $tablestatus) {
$row_usage += $tablestatus->Rows;
$data_usage += $tablestatus->Data_length;
$index_usage += $tablestatus->Index_length;
if ($tablestatus->Engine != 'InnoDB'){
$overhead_usage += $tablestatus->Data_free;
$total_gain += $tablestatus->Data_free;
}
}
$total_size = $data_usage + $index_usage;
wpo_debugLog('Total Size .... '.$total_size);
wpo_debugLog('Total Gain .... '.$total_gain);
return array (wpo_format_size($total_size), wpo_format_size($total_gain));
//$wpdb->flush();
}
// end of function wpo_getCurrentDBSize
/*
* function wpo_updateTotalCleaned($current)
*
* parameters: a string value
*
* this function will return total saved data in KB
*
* @return total size
*/
function wpo_updateTotalCleaned($current){
$previously_saved = get_option(OPTION_NAME_TOTAL_CLEANED,'0');
$previously_saved = floatval($previously_saved);
$converted_current = floatval($current);
$total_now = $previously_saved + $converted_current;
$total_now = strval($total_now);
update_option(OPTION_NAME_TOTAL_CLEANED, $total_now);
update_option(OPTION_NAME_CURRENT_CLEANED, $current);
return $total_now;
} // end of function wpo_updateTotalCleaned
/*
* function wpo_cleanUpSystem($cleanupType)
*
* parameters: cleanup type
*
* this function will do the cleanup
*
* @return $message
*/
function wpo_cleanUpSystem($cleanupType){
global $wpdb;
$clean = ""; $message = "";
list ($retention_enabled, $retention_period) = wpo_getRetainInfo();
switch ($cleanupType) {
case "transient":
// backticks
$clean = "DELETE FROM `$wpdb->options` WHERE option_name LIKE '_site_transient_browser_%' OR option_name LIKE '_site_transient_timeout_browser_%' OR option_name LIKE '_transient_feed_%' OR option_name LIKE '_transient_timeout_feed_%'";
//$clean .= ';';
$transient = $wpdb->query( $clean );
$message .= sprintf(_n('%d transient option deleted', '%d transient options deleted', $transient, 'wp-optimize'), number_format_i18n($transient)).'<br>';
break;
// TODO: need to use proper query
case "postmeta":
$clean = "DELETE pm FROM `$wpdb->postmeta` pm LEFT JOIN `$wpdb->posts` wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL";
$clean .= ';';
$postmeta = $wpdb->query( $clean );
$message .= sprintf(_n('%d orphaned postmeta deleted', '%d orphaned postmeta deleted', $postmeta, 'wp-optimize'), number_format_i18n($postmeta)).'<br>';
break;
case "commentmeta":
$clean = "DELETE FROM `$wpdb->commentmeta` WHERE comment_id NOT IN (SELECT comment_id FROM `$wpdb->comments`)";
$clean .= ';';
$commentstrash_meta = $wpdb->query( $clean );
$message .= sprintf(_n('%d unused comment metadata item removed', '%d unused comment metadata items removed', $commentstrash_meta, 'wp-optimize'), number_format_i18n($commentstrash_meta)).'<br>';
// TODO: still need to test now cleaning up comments meta tables - removing akismet related settings
$clean = "DELETE FROM `$wpdb->commentmeta` WHERE meta_key LIKE '%akismet%'";
$clean .= ';';
$commentstrash_meta2 = $wpdb->query( $clean );
$message .= sprintf(_n('%d unused akismet comment metadata item removed', '%d unused akismet comment metadata items removed', $commentstrash_meta2, 'wp-optimize'), number_format_i18n($commentstrash_meta2)).'<br>';
break;
case "orphandata":
$clean = "DELETE FROM `$wpdb->term_relationships` WHERE term_taxonomy_id=1 AND object_id NOT IN (SELECT id FROM `$wpdb->posts`)";
$clean .= ';';
$orphandata = $wpdb->query( $clean );
$message .= sprintf(_n('%d orphaned meta data deleted', '%d orphaned meta data deleted', $orphandata, 'wp-optimize'), number_format_i18n($orphandata)).'<br>';
break;
case "tags":
// $clean = "DELETE t,tt FROM `$wpdb->terms` t INNER JOIN `$wpdb->term_taxonomy` tt ON t.term_id=tt.term_id WHERE tt.taxonomy='post_tag' AND tt.count=0";
// $clean .= ';';
//
// $tags = $wpdb->query( $clean );
// $message .= sprintf(_n('%d unused tag deleted', '%d unused tags deleted', $tags, 'wp-optimize'), number_format_i18n($tags)).'<br>';
break;
case "revisions":
$clean = "DELETE FROM `$wpdb->posts` WHERE post_type = 'revision'";
if ($retention_enabled == 'true') {
$clean .= ' and post_modified < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$clean .= ';';
$revisions = $wpdb->query( $clean );
$message .= sprintf(_n('%d post revision deleted', '%d post revisions deleted', $revisions, 'wp-optimize'), number_format_i18n($revisions)).'<br>';
break;
case "autodraft":
$clean = "DELETE FROM `$wpdb->posts` WHERE post_status = 'auto-draft'";
if ($retention_enabled == 'true') {
$clean .= ' and post_modified < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$clean .= ';';
$autodraft = $wpdb->query( $clean );
$message .= sprintf(_n('%d auto draft deleted', '%d auto drafts deleted', $autodraft, 'wp-optimize'), number_format_i18n($autodraft)).'<br>';
// TODO: query trashed posts and cleanup metadata
$clean = "DELETE FROM `$wpdb->posts` WHERE post_status = 'trash'";
if ($retention_enabled == 'true') {
$clean .= ' and post_modified < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$clean .= ';';
$posttrash = $wpdb->query( $clean );
$message .= sprintf(_n('%d item removed from Trash', '%d items removed from Trash', $posttrash, 'wp-optimize'), number_format_i18n($posttrash)).'<br>';
break;
case "spam":
$clean = "DELETE FROM `$wpdb->comments` WHERE comment_approved = 'spam'";
if ($retention_enabled == 'true') {
$clean .= ' and comment_date < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$clean .= ';';
$comments = $wpdb->query( $clean );
$message .= sprintf(_n('%d spam comment deleted', '%d spam comments deleted', $comments, 'wp-optimize'), number_format_i18n($comments)).'<br>';
// TODO: query trashed comments and cleanup metadata
$clean = "DELETE FROM `$wpdb->comments` WHERE comment_approved = 'trash'";
if ($retention_enabled == 'true') {
$clean .= ' and comment_date < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$clean .= ';';
$commentstrash = $wpdb->query( $clean );
$message .= sprintf(_n('%d comment removed from Trash', '%d comments removed from Trash', $commentstrash, 'wp-optimize'), number_format_i18n($commentstrash)).'<br>';
break;
case "unapproved":
$clean = "DELETE FROM `$wpdb->comments` WHERE comment_approved = '0'";
if ($retention_enabled == 'true') {
$clean .= ' and comment_date < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$clean .= ';';
$comments = $wpdb->query( $clean );
$message .= sprintf(_n('%d unapproved comment deleted', '%d unapproved comments deleted', $comments, 'wp-optimize'), number_format_i18n($comments)).'<br>';
break;
case "pingbacks":
$clean = "DELETE FROM `$wpdb->comments` WHERE comment_type = 'pingback';";
$comments = $wpdb->query( $clean );
$message .= sprintf(_n('%d pingback deleted', '%d pingbacks deleted', $comments, 'wp-optimize'), number_format_i18n($comments)).'<br>';
break;
case "trackbacks":
$clean = "DELETE FROM `$wpdb->comments` WHERE comment_type = 'trackback';";
$comments = $wpdb->query( $clean );
$message .= sprintf(_n('%d trackback deleted', '%d trackbacks deleted', $comments, 'wp-optimize'), number_format_i18n($comments)).'<br>';
break;
case "enable-weekly":
update_option( OPTION_NAME_SCHEDULE, 'true' );
$comments = '';
$message .= $comments.' '.__('Enabled weekly processing', 'wp-optimize').'<br>';
break;
case "disable-weekly":
update_option( OPTION_NAME_SCHEDULE, 'false' );
$comments = '';
$message .= $comments.' '.__('Disabled weekly processing', 'wp-optimize').'<br>';
break;
//case "optimize-db":
//optimizeTables(true);
//$message .= "Database ".DB_NAME." Optimized!<br>";
//break;
default:
$message .= __('NO Actions Taken', 'wp-optimize').'<br>';
break;
} // end of switch
return $message;
} // end of function
/*
* function wpo_getInfo($cleanupType)
*
* parameters: cleanup type
*
* this function will do the cleanup
*
* @return $message
*/
function wpo_getInfo($cleanupType){
global $wpdb;
$sql = ""; $message = "";
list ($retention_enabled, $retention_period) = wpo_getRetainInfo();
switch ($cleanupType) {
case "transient":
$sql = "SELECT COUNT(*) FROM `$wpdb->options` WHERE option_name LIKE '_site_transient_browser_%' OR option_name LIKE '_site_transient_timeout_browser_%' OR option_name LIKE '_transient_feed_%' OR option_name LIKE '_transient_timeout_feed_%'";
//$sql .= ';';
$transient = $wpdb->get_var( $sql );
if(!$transient == 0 || !$transient == NULL){
$message .= ' '.sprintf(_n('%d transient option in your database', '%d transient options in your database', $transient, 'wp-optimize'), number_format_i18n($transient));
}
else $message .=' '.__('No transient options found', 'wp-optimize');
break;
case "postmeta":
$sql = "SELECT COUNT(*) FROM `$wpdb->postmeta` pm LEFT JOIN `$wpdb->posts` wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL";
$sql .= ';';
$postmeta = $wpdb->get_var( $sql );
if(!$postmeta == 0 || !$postmeta == NULL){
$message .= ' '.sprintf(_n('%d orphaned post meta data in your database', '%d orphaned postmeta in your database', $postmeta, 'wp-optimize'), number_format_i18n($postmeta));
}
else $message .=' '.__('No orphaned post meta data in your database', 'wp-optimize');
break;
case "commentmeta":
$sql = "SELECT COUNT(*) FROM `$wpdb->commentmeta` WHERE comment_id NOT IN (SELECT comment_id FROM `$wpdb->comments`)";
$sql .= ';';
$commentmeta = $wpdb->get_var( $sql );
if(!$commentmeta == 0 || !$commentmeta == NULL){
$message .= ' '.sprintf(_n('%d orphaned comment meta data in your database', '%d orphaned comment meta data in your database', $commentmeta, 'wp-optimize'), number_format_i18n($commentmeta));
}
else $message .=' '.__('No orphaned comment meta data in your database', 'wp-optimize');
break;
case "orphandata":
$sql = "SELECT COUNT(*) FROM `$wpdb->term_relationships` WHERE term_taxonomy_id=1 AND object_id NOT IN (SELECT id FROM `$wpdb->posts`)";
$sql .= ';';
$orphandata = $wpdb->get_var( $sql );
if(!$orphandata == 0 || !$orphandata == NULL){
$message .= ' '.sprintf(_n('%d orphaned relationship data in your database', '%d orphaned relationship data in your database', $orphandata, 'wp-optimize'), number_format_i18n($orphandata));
}
else $message .=' '.__('No orphaned relationship data in your database', 'wp-optimize');
break;
// not used
/*case "transient":
$sql = "SELECT COUNT(*) FROM `$wpdb->options` WHERE option_name LIKE '_site_transient_browser_%' OR option_name LIKE '_site_transient_timeout_browser_%' OR option_name LIKE '_transient_feed_%' OR option_name LIKE '_transient_timeout_feed_%'";
$sql .= ';';
$transient = $wpdb->get_var( $sql );
if(!$transient == 0 || !$transient == NULL){
$message .= ' '.sprintf(_n('%d transient data in your database', '%d transient data in your database', $transient, 'wp-optimize'), number_format_i18n($transient));
}
else $message .=' '.__('No transient data found', 'wp-optimize');
break;*/
case "revisions":
$sql = "SELECT COUNT(*) FROM `$wpdb->posts` WHERE post_type = 'revision'";
if ($retention_enabled == 'true') {
$sql .= ' and post_modified < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$sql .= ';';
$revisions = $wpdb->get_var( $sql );
if(!$revisions == 0 || !$revisions == NULL){
$message .= ' '.sprintf(_n('%d post revision in your database', '%d post revisions in your database', $revisions, 'wp-optimize'), number_format_i18n($revisions));
}
else $message .=' '.__('No post revisions found', 'wp-optimize');
break;
case "autodraft":
$sql = "SELECT COUNT(*) FROM `$wpdb->posts` WHERE post_status = 'auto-draft'";
if ($retention_enabled == 'true') {
$sql .= ' and post_modified < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$sql .= ';';
$autodraft = $wpdb->get_var( $sql );
if(!$autodraft == 0 || !$autodraft == NULL){
$message .= ' '.sprintf(_n('%d auto draft post in your database', '%d auto draft posts in your database', $autodraft, 'wp-optimize'), number_format_i18n($autodraft));
}
else $message .=' '.__('No auto draft posts found', 'wp-optimize');
break;
case "spam":
$sql = "SELECT COUNT(*) FROM `$wpdb->comments` WHERE comment_approved = 'spam'";
if ($retention_enabled == 'true') {
$sql .= ' and comment_date < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$sql .= ';';
$comments = $wpdb->get_var( $sql );
if(!$comments == NULL || !$comments == 0){
$message .= ' '.sprintf(_n('%d spam comment found', '%d spam comments found', $comments, 'wp-optimize'), number_format_i18n($comments)).' | <a href="edit-comments.php?comment_status=spam">'.' '.__('Review', 'wp-optimize').'</a>';
} else
$message .=' '.__('No spam comments found', 'wp-optimize');
break;
case "unapproved":
$sql = "SELECT COUNT(*) FROM `$wpdb->comments` WHERE comment_approved = '0'";
if ($retention_enabled == 'true') {
$sql .= ' and comment_date < NOW() - INTERVAL ' . $retention_period . ' WEEK';
}
$sql .= ';';
$comments = $wpdb->get_var( $sql );
if(!$comments == NULL || !$comments == 0){
$message .= ' '.sprintf(_n('%d unapproved comment found', '%d unapproved comments found', $comments, 'wp-optimize'), number_format_i18n($comments)).' | <a href="edit-comments.php?comment_status=moderated">'.' '.__('Review', 'wp-optimize').'</a>';;
} else
$message .= ' '.__('No unapproved comments found', 'wp-optimize');
break;
case "pingbacks":
$sql = "SELECT COUNT(*) FROM `$wpdb->comments` WHERE comment_type='pingback';";
$comments = $wpdb->get_var( $sql );
if(!$comments == NULL || !$comments == 0){
$message .= ' '.sprintf(_n('%d Pingback found', '%d Pingbacks found', $comments, 'wp-optimize'), number_format_i18n($comments));
} else
$message .= ' '.__('No pingbacks found', 'wp-optimize');
break;
case "trackbacks":
$sql = "SELECT COUNT(*) FROM `$wpdb->comments` WHERE comment_type='trackback';";
$comments = $wpdb->get_var( $sql );
if(!$comments == NULL || !$comments == 0){
$message .= ' '.sprintf(_n('%d Trackback found', '%d Trackbacks found', $comments, 'wp-optimize'), number_format_i18n($comments));
} else
$message .= ' '.__('No trackbacks found', 'wp-optimize');
break;
default:
$message .= __('nothing', 'wp-optimize');
break;
} // end of switch
return $message;
} // end of function
/*
* function showStatus($text)
*
* parameters: $text
*
* this function will show a yellow status msg
*
* @return none
*/
function showStatus($text){
echo '<div id="message" class="updated fade">';
echo '<strong>'.$text.'</strong></div>';
}
// end of file
?>