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
882 lines (725 loc) · 28.9 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
<?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');
/**
* 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){
//
//
//
$formattedCleanedup = wpo_format_size($cleanedup);
$sendto = get_bloginfo ( 'admin_email' );
$subject = get_bloginfo ( 'name' ).': Automatic Operation Completed '.$date;
$msg = 'Optimization result: \n\n';
$msg .= 'Total Space Saved: '.$formattedCleanedup.' \n';
$msg .= '\n';
$msg .= 'Regards,\n';
$msg .= 'WP-Optimize Plugin \n';
wp_mail( $sendto, $subject, $msg );
}
/**
* 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( '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 );
// 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 .= ';';
$commentstrash2 = $wpdb->query( $clean );
}
// transient options
if ($this_options['transient'] == 'true'){
$clean = "DELETE FROM `$wpdb->options` WHERE option_name LIKE '_transient_%' OR option_name LIKE '_site_transient_%'";
$clean .= ';';
$transient_options = $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]);
}
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));
wpo_sendEmail($thedate, $part2); //send email to admin
wpo_debugLog('Updating options with value +'.$part2);
} // 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_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);
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_options":
// backticks
$clean = "DELETE FROM `$wpdb->options` WHERE option_name LIKE '_transient_%' OR option_name LIKE '_site_transient_%'";
$clean .= ';';
$transient_options = $wpdb->query( $clean );
$message .= $transient_options.' '.__('transient options deleted', 'wp-optimize').'<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 .= $postmeta.' '.__('orphaned postmeta deleted', 'wp-optimize').'<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 .= $tags.' '.__('unused tags deleted', 'wp-optimize').'<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 .= $revisions.' '.__('post revisions deleted', 'wp-optimize').'<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 .= $autodraft.' '.__('auto drafts deleted', 'wp-optimize').'<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 .= $posttrash.' '.__('items removed from Trash', 'wp-optimize').'<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 .= $comments.' '.__('spam comments deleted', 'wp-optimize').'<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 .= $commentstrash.' '.__('comments removed from Trash', 'wp-optimize').'<br>';
// 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 .= ';';
$commentstrash_meta = $wpdb->query( $clean );
$message .= $commentstrash_meta.' '.__('unused comment metadata items removed', 'wp-optimize').'<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 .= $commentstrash_meta2.' '.__('unused akismet comment metadata items removed', 'wp-optimize').'<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 .= $comments.' '.__('unapproved comments deleted', 'wp-optimize').'<br>';
break;
case "pingbacks":
$clean = "DELETE FROM `$wpdb->comments` WHERE comment_type = 'pingback';";
$comments = $wpdb->query( $clean );
$message .= $comments.' '.__('pingbacks deleted', 'wp-optimize').'<br>';
break;
case "trackbacks":
$clean = "DELETE FROM `$wpdb->comments` WHERE comment_type = 'trackback';";
$comments = $wpdb->query( $clean );
$message .= $comments.' '.__('trackbacks deleted', 'wp-optimize').'<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_options":
$sql = "SELECT COUNT(*) FROM `$wpdb->options` WHERE option_name LIKE '_transient_%' OR option_name LIKE '_site_transient_%'";
$sql .= ';';
$transient_options = $wpdb->get_var( $sql );
if(!$transient_options == 0 || !$transient_options == NULL){
$message .= ' '.$transient_options.' '.__('transient options in your database', 'wp-optimize');
}
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 .= ' '.$postmeta.' '.__('orphaned postmeta in your database', 'wp-optimize');
}
else $message .=' '.__('No orphaned postmeta in your database', 'wp-optimize');
break;
case "tags":
$sql = "SELECT COUNT(*) 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";
$sql .= ';';
$tags = $wpdb->get_var( $sql );
if(!$tags == 0 || !$tags == NULL){
$message .= ' '.$tags.' '.__('unused tags in your database', 'wp-optimize');
}
else $message .=' '.__('No unused tags 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 .= ' '.$revisions.' '.__('post revisions in your database', 'wp-optimize');
}
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 .= ' '.$autodraft.' '.__('auto draft post(s) in your database', 'wp-optimize');
}
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 .= ' '.$comments.' '.__('spam comments found', 'wp-optimize').' | <a href="edit-comments.php?comment_status=spam">'.' '.__('Review', 'wp-optimize').'</a>';
} else
$message .=' '.__('No spam comments found', 'wp-optimize');
// TODO: still need to test 2 more sections for info - still need to test
// $sql = "SELECT * FROM $wpdb->commentmeta WHERE comment_id NOT IN ( SELECT comment_id FROM $wpdb->comments )";
// $sql .= ';';
// $comments_meta = $wpdb->query( $sql );
// if(!$comments_meta == NULL || !$comments_meta == 0){
// $message .= ' | '.$comments_meta.' '.__('Unused comment meta found', 'wp-optimize');
// }
//
//
// $sql = "SELECT * FROM $wpdb->commentmeta WHERE meta_key LIKE '%akismet%'";
// $sql .= ';';
// $comments_meta2 = $wpdb->query( $sql );
// if(!$comments_meta2 == NULL || !$comments_meta2 == 0){
// $message .= ' | '.$comments_meta2.' '.__('additional Akismet junk data 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 .= ' '.$comments.' '.__('unapproved comments found', 'wp-optimize').' | <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 .= ' '.$comments.' '.__('Pingbacks found', 'wp-optimize');
} 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 .= ' '.$comments.' '.__('Trackbacks found', 'wp-optimize');
} 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
?>