PluginProbe
Optimize Database after Deleting Revisions / 5.2
Optimize Database after Deleting Revisions v5.2
3.4.7 3.4.8 3.4.9 3.5 3.5.1 4.0 4.0.1 4.0.2 4.0.3 4.1 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.3 All 144 releases
rvg-optimize-database / classes / odb-cleaner.php

odb-cleaner.php in Optimize Database after Deleting Revisions 5.2, at classes/odb-cleaner.php

2,000 lines 72.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /************************************************************************************************
3 *
4 * CLEANER CLASS: DOING THE REAL CLEANING / OPTIMIZATION
5 *
6 ************************************************************************************************/
7 ?>
8 <?php
9 class ODB_Cleaner {
10 var $start_size;
11 var $log_arr;
12 var $nr_of_optimized_tables;
13 var $grand_total = 0;
14
15
16 /********************************************************************************************
17 *
18 * CONSTRUCTOR
19 *
20 ********************************************************************************************/
21 function __construct() {
22 } // __construct()
23
24
25 /********************************************************************************************
26 *
27 * RUN CLEANER
28 *
29 ********************************************************************************************/
30 function odb_run_cleaner($scheduler, $action = 'run_detail') {
31 global $odb_class;
32
33 if(!$scheduler) {
34 if ($action == 'analyze_summary' || $action == 'analyze_detail') {
35 if ($action == 'analyze_summary') {
36 $txt = 'Analyzing (summary)';
37 } else if ($action == 'analyze_detail') {
38 $txt = 'Analyzing (detail)';
39 }
40 echo '
41 <div id="odb-cleaner" class="odb-padding-left">
42 <div class="odb-title-bar">
43 <h2>'.__($txt, 'rvg-optimize-database').'</h2>
44 </div>
45 <br>
46 <br>
47 ';
48 } else {
49 echo '
50 <div id="odb-cleaner" class="odb-padding-left">
51 <div class="odb-title-bar">
52 <h2>'.__('Cleaning Database', 'rvg-optimize-database').'</h2>
53 </div>
54 <br>
55 <br>
56 ';
57 }
58 } // if(!$scheduler)
59
60 // GET THE SIZE OF THE DATABASE BEFORE OPTIMIZATION
61 $this->start_size = $odb_class->odb_utilities_obj->odb_get_db_size();
62
63 // TIMESTAMP FOR LOG FILE - v4.6
64 $ct = ($scheduler) ? ' (cron)' : '';
65
66 $odb_class->log_arr["timestamp"] = current_time('YmdHis', 0);
67 $odb_class->log_arr["after"] = 0;
68 $odb_class->log_arr["before"] = 0;
69 $odb_class->log_arr["pingbacks"] = 0;
70 $odb_class->log_arr["oembeds"] = 0;
71 $odb_class->log_arr["orphans"] = 0;
72 $odb_class->log_arr["revisions"] = 0;
73 $odb_class->log_arr["savings"] = 0;
74 $odb_class->log_arr["spam"] = 0;
75 $odb_class->log_arr["tables"] = 0;
76 $odb_class->log_arr["tags"] = 0;
77 $odb_class->log_arr["transients"] = 0;
78 $odb_class->log_arr["trash"] = 0;
79
80 /****************************************************************************************
81 *
82 * DELETE REVISIONS
83 *
84 ****************************************************************************************/
85 if($odb_class->odb_rvg_options['delete_older'] == 'Y' || $odb_class->odb_rvg_options['rvg_revisions'] == 'Y') {
86
87 // FIND REVISIONS
88 $results_older_than = array();
89 if($odb_class->odb_rvg_options['delete_older'] == 'Y') {
90 $results_older_than = $this->odb_get_revisions_older_than();
91 } // if($odb_class->odb_rvg_options['delete_older'] == 'Y')
92
93 $results_keep_revisions = array();
94 if($odb_class->odb_rvg_options['rvg_revisions'] == 'Y') {
95 $results_keep_revisions = $this->odb_get_revisions_keep_revisions();
96 } // if($odb_class->odb_rvg_options['rvg_revisions'] == 'Y')
97
98 if (count($results_older_than) > 0 || count($results_keep_revisions) > 0) {
99 // REVISIONS FOUND
100 $this->grand_total += count($results_older_than) + count($results_keep_revisions);
101
102 if ($action == 'analyze_summary') {
103 ?>
104 <div class="odb-found-number">
105 <?php _e('REVISIONS found to delete: ', 'rvg-optimize-database');?>
106 <?php echo (count($results_older_than) + count($results_keep_revisions)) . '<br>';?>
107 </div>
108 <?php
109 } // if ($action == 'analyze_summary')
110
111 if ($action == 'run_summary') {
112 ?>
113 <div class="odb-found-number">
114 <?php _e('REVISIONS deleted: ', 'rvg-optimize-database');?>
115 <?php echo (count($results_older_than) + count($results_keep_revisions)) . '<br>';?>
116 </div>
117 <?php
118 } // if ($action == 'run_summary')
119
120 if ($action == 'analyze_detail' || $action == 'run_detail') {
121 // 'ANALYZE_DETAIL', 'RUN_SUMMARY' or 'RUN_DETAIL'
122 if ($action == 'analyze_detail') {
123 $msg1 = __('REVISIONS', 'rvg-optimize-database');
124 $msg2 = __('total number of revisions', 'rvg-optimize-database');
125 } else {
126 // 'RUN_SUMMARY' or 'RUN_DETAIL'
127 if ($action == 'run_detail') {
128 $msg1 = __('DELETED REVISIONS', 'rvg-optimize-database');
129 $msg2 = __('total number of revisions deleted', 'rvg-optimize-database');
130 } // if ($action == 'run_detail')
131 } // if ($action == 'analyze_detail')
132 ?>
133 <table border="0" cellspacing="8" cellpadding="2" class="odb-result-table">
134 <tr>
135 <td colspan="4"><div class="odb-found">
136 <?php echo $msg1 ?>
137 </div></td>
138 </tr>
139 <tr>
140 <th align="right" class="odb-border-bottom">#</th>
141 <th align="left" class="odb-border-bottom"><?php _e('prefix', 'rvg-optimize-database');?></th>
142 <th align="left" class="odb-border-bottom"><?php _e('post / page', 'rvg-optimize-database');?></th>
143 <th align="left" class="odb-border-bottom"><?php _e('revision date', 'rvg-optimize-database');?></th>
144 <th align="right" class="odb-border-bottom"><?php echo $msg2?></th>
145 </tr>
146 <?php
147 } // if ($action == 'analyze_detail' || $action == 'run_detail')
148
149 // LOOP THROUGH THE REVISIONS AND DELETE THEM
150 $total_deleted = $this->odb_delete_revisions($scheduler, $action);
151
152 if ($action == 'analyze_detail' || $action == 'run_detail') {
153 ?>
154 <tr>
155 <td colspan="5" align="right" class="odb-border-top odb-bold"><?php echo $msg2 ?>: <?php echo $total_deleted?></td>
156 </tr>
157 </table>
158 <?php
159 } // if ($action == 'analyze_detail' || $action == 'run_detail')
160
161 if ($scheduler) $odb_class->log_arr["revisions"] = $total_deleted;
162
163 if ($action == 'run_summary' || $action == 'run_detail') {
164 // NUMBER OF DELETED REVISIONS FOR LOG FILE
165 $odb_class->log_arr["revisions"] = $total_deleted;
166 } // if ($action == 'analyze_detail' || $action == 'run_summary' || $action == 'run_detail')
167 } else {
168 // NO REVISIONS FOUND
169 if (!$scheduler) {
170 ?>
171 <div class="odb-not-found">
172 <?php _e('No REVISIONS found to delete', 'rvg-optimize-database');?>
173 </div>
174 <?php
175 } // if (!$scheduler)
176 } // if (count($results) > 0)
177 } // if($odb_class->odb_rvg_options['delete_older'] == 'Y' || $odb_class->odb_rvg_options['rvg_revisions'] == 'Y')
178
179
180 /****************************************************************************************
181 *
182 * DELETE TRASHED ITEMS
183 *
184 ****************************************************************************************/
185 if ($odb_class->odb_rvg_options['clear_trash'] == 'Y') {
186 $results = $this->odb_get_trash();
187 if (count($results) > 0) {
188 // TRASHED ITEMS FOUNE
189 $this->grand_total += count($results);
190
191 // TRASH FOUND
192 if ($action == 'analyze_summary') {
193 ?>
194 <div class="odb-found-number">
195 <?php _e('TRASHED ITEMS found to delete: ', 'rvg-optimize-database');?>
196 <?php echo count($results) . '<br>';?>
197 </div>
198 <?php
199 } // if ($action == 'analyze_summary')
200
201 if ($action == 'run_summary') {
202 ?>
203 <div class="odb-found-number">
204 <?php _e('TRASHED ITEMS deleted: ', 'rvg-optimize-database');?>
205 <?php echo count($results) . '<br>';?>
206 </div>
207 <?php
208 } // if ($action == 'run_summary')
209
210 if ($action == 'analyze_detail' || $action == 'run_detail') {
211 // 'RUN' or 'ANALYZE_DETAIL
212 if ($action == 'analyze_detail') {
213 $msg1 = __('TRASHED ITEMS', 'rvg-optimize-database');
214 $msg2 = __('total number of trashed items', 'rvg-optimize-database');
215 } else {
216 // ACTION = 'RUN_DETAIL'
217 $msg1 = __('DELETED TRASHED ITEMS', 'rvg-optimize-database');
218 $msg2 = __('total number of trashed items deleted', 'rvg-optimize-database');
219 } // if ($action == 'analyze_detail')
220 ?>
221 <table border="0" cellspacing="8" cellpadding="2" class="odb-result-table">
222 <tr>
223 <td colspan="4"><div class="odb-found">
224 <?php echo $msg1 ?>
225 </div></td>
226 </tr>
227 <tr>
228 <th align="right" class="odb-border-bottom">#</th>
229 <th align="left" class="odb-border-bottom"><?php _e('prefix', 'rvg-optimize-database');?></th>
230 <th align="left" class="odb-border-bottom"><?php _e('type', 'rvg-optimize-database');?></th>
231 <th align="left" class="odb-border-bottom"><?php _e('IP address / title', 'rvg-optimize-database');?></th>
232 <th align="left" nowrap="nowrap" class="odb-border-bottom"><?php _e('date', 'rvg-optimize-database');?></th>
233 </tr>
234 <?php
235 } // if ($action == 'analyze_detail' || $action == 'run_detail')
236
237 // LOOP THROUGH THE TRASHED ITEMS AND DELETE THEM
238 $total_deleted = $this->odb_delete_trash($results, $scheduler, $action);
239
240 if ($scheduler) $odb_class->log_arr["trash"] = $total_deleted;
241
242 if ($action == 'run_summary' || $action == 'run_detail') {
243 // NUMBER OF DELETED TRASH FOR LOG FILE
244 $odb_class->log_arr["trash"] = $total_deleted;
245 } // if (!$scheduler && ($action == 'run_summary' || $action == 'run_detail'))
246
247 if ($action == 'analyze_detail' || $action == 'run_detail') {
248 ?>
249 </table>
250 <?php
251 } // if ($action == 'analyze_detail' || $action == 'run_detail')
252 } else {
253 // NO TRASH FOUND
254 if (!$scheduler) {
255 ?>
256 <div class="odb-not-found">
257 <?php _e('No TRASHED ITEMS found to delete', 'rvg-optimize-database');?>
258 </div>
259 <?php
260 } // if (!$scheduler)
261 } // if (count($results) > 0)
262 } // if($odb_class->odb_rvg_options['clear_trash'] == 'Y')
263
264
265 /****************************************************************************************
266 *
267 * DELETE SPAMMED ITEMS
268 *
269 ****************************************************************************************/
270 if ($odb_class->odb_rvg_options['clear_spam'] == 'Y') {
271 $results = $this->odb_get_spam();
272
273 if (count($results) > 0) {
274 // SPAM FOUND
275 $this->grand_total += count($results);
276
277 if ($action == 'analyze_summary') {
278 ?>
279 <div class="odb-found-number">
280 <?php _e('SPAMMED ITEMS found to delete: ', 'rvg-optimize-database');?>
281 <?php echo count($results) . '<br>';?>
282 </div>
283 <?php
284 } // if ($action == 'analyze_summary')
285
286 if ($action == 'run_summary') {
287 ?>
288 <div class="odb-found-number">
289 <?php _e('SPAMMED ITEMS deleted: ', 'rvg-optimize-database');?>
290 <?php echo count($results) . '<br>';?>
291 </div>
292 <?php
293 } // if ($action == 'run_summary')
294
295 if ($action == 'analyze_detail' || $action == 'run_detail') {
296 if ($action == 'analyze_detail') {
297 $msg1 = __('SPAMMED ITEMS', 'rvg-optimize-database');
298 $msg2 = __('total number of spammed items', 'rvg-optimize-database');
299 } // if ($action == 'analyze_detail')
300
301 if ($action == 'run_detail') {
302 $msg1 = __('DELETED SPAMMED ITEMS', 'rvg-optimize-database');
303 $msg2 = __('total number of spammed items deleted', 'rvg-optimize-database');
304 } // if ($action == 'run_detail')
305 ?>
306 <table border="0" cellspacing="8" cellpadding="2" class="odb-result-table">
307 <tr>
308 <td colspan="4"><div class="odb-found">
309 <?php echo $msg1 ?>
310 </div></td>
311 </tr>
312 <tr>
313 <th align="right" class="odb-border-bottom">#</th>
314 <th align="left" class="odb-border-bottom"><?php _e('prefix', 'rvg-optimize-database');?></th>
315 <th align="left" class="odb-border-bottom"><?php _e('comment author', 'rvg-optimize-database');?></th>
316 <th align="left" class="odb-border-bottom"><?php _e('comment author email', 'rvg-optimize-database');?></th>
317 <th align="left" nowrap="nowrap" class="odb-border-bottom"><?php _e('comment date', 'rvg-optimize-database');?></th>
318 </tr>
319 <?php
320 } // if ($action == 'analyze_detail' || $action == 'run_detail')
321
322 // LOOP THROUGH THE SPAMMED ITEMS AND DELETE THEM
323 $total_deleted = $this->odb_delete_spam($results, $scheduler, $action);
324
325 if ($scheduler) $odb_class->log_arr["spam"] = $total_deleted;
326
327 if ($action == 'run_summary' || $action == 'run_detail') {
328 // NUMBER OF DELETED SPAM FOR LOG FILE
329 $odb_class->log_arr["spam"] = $total_deleted;
330 } // if (!$scheduler && ($action == 'run_summary' || $action == 'run_detail'))
331
332 if ($action == 'analyze_detail' || $action == 'run_detail') {
333 ?>
334 </table>
335 <?php
336 } // if ($action == 'analyze_detail' || $action == 'run_detail')
337 } else {
338 // NO SPAM FOUND
339 if (!$scheduler) {
340 ?>
341 <div class="odb-not-found">
342 <?php _e('No SPAMMED ITEMS found to delete', 'rvg-optimize-database');?>
343 </div>
344 <?php
345 } // if (!$scheduler)
346 } // if (count($results) > 0)
347 } // if ($odb_class->odb_rvg_options['clear_spam'] == 'Y')
348
349
350 /****************************************************************************************
351 *
352 * DELETE UNUSED TAGS
353 *
354 ****************************************************************************************/
355 if ($odb_class->odb_rvg_options['clear_tags'] == 'Y') {
356 // GET UNUSED TAGS
357 $results = $this->odb_get_unused_tags();
358
359 if (count($results) > 0) {
360 // UNUSED TAGS FOUND
361 $this->grand_total += count($results);
362
363 if ($action == 'analyze_summary' || $action == 'run_summary') {
364 if ($action == 'analyze_summary') {
365 ?>
366 <div class="odb-found-number">
367 <?php _e('UNUSED TAGS found to delete: ', 'rvg-optimize-database');?>
368 <?php echo count($results) . '<br>';?>
369 </div>
370 <?php
371 } // if ($action == 'analyze_summary')
372
373 if ($action == 'run_summary') {
374 ?>
375 <div class="odb-found-number">
376 <?php _e('UNUSED TAGS deleted: ', 'rvg-optimize-database');?>
377 <?php echo count($results) . '<br>';?>
378 </div>
379 <?php
380 } // if ($action == 'run_summary')
381 } // if ($action == 'analyze_summary' || $action == 'run_summary')
382
383 if ($action == 'analyze_detail' || $action == 'run_detail') {
384 if ($action == 'analyze_detail') {
385 $msg1 = __('UNUSED TAGS', 'rvg-optimize-database');
386 $msg2 = __('total number of unused tags', 'rvg-optimize-database');
387 } else {
388 // ACTION = 'RUN_detail'
389 $msg1 = __('DELETED UNUSED TAGS', 'rvg-optimize-database');
390 $msg2 = __('total number of unused tags deleted', 'rvg-optimize-database');
391 } // if ($action == 'analyze_detail')
392 ?>
393 <table border="0" cellspacing="8" cellpadding="2" class="odb-result-table">
394 <tr>
395 <td colspan="4"><div class="odb-found">
396 <?php echo $msg1 ?>
397 </div></td>
398 </tr>
399 <tr>
400 <th align="right" class="odb-border-bottom">#</th>
401 <th align="left" class="odb-border-bottom"><?php _e('prefix', 'rvg-optimize-database');?></th>
402 <th align="left" class="odb-border-bottom"><?php _e('tag', 'rvg-optimize-database');?></th>
403 </tr>
404 <?php
405 } // if ($action == 'analyze_detail' || $action == 'run_detail')
406
407 // LOOP THROUGH THE UNUSED TAGS AND DELETE THEM
408 $total_deleted = $this->odb_delete_unused_tags($results, $scheduler, $action);
409
410 if ($scheduler) $odb_class->log_arr["tags"] = $total_deleted;
411
412 if ($action == 'run_summary' || $action == 'run_detail') {
413 // NUMBER OF UNUSED TAGS FOR LOG FILE
414 $odb_class->log_arr["tags"] = $total_deleted;
415 } // if (!$scheduler && ($action == 'run_summary' || $action == 'run_detail'))
416
417 if ($action == 'analyze_detail' || $action == 'run_detail') {
418 ?>
419 </table>
420 <?php
421 } // if ($action == 'analyze_detail' || $action == 'run_detail')
422 } else {
423 // NO UNUSED TAGS FOUND
424 if (!$scheduler) {
425 ?>
426 <div class="odb-not-found">
427 <?php _e('No UNUSED TAGS found to delete', 'rvg-optimize-database');?>
428 </div>
429 <?php
430 } // if (!$scheduler)
431 } // if (count($results) > 0)
432 } // if ($odb_class->odb_rvg_options['clear_tags'] == 'Y')
433
434
435 /****************************************************************************************
436 *
437 * DELETE TRANSIENTS
438 *
439 ****************************************************************************************/
440 if ($odb_class->odb_rvg_options['clear_transients'] !== 'N') {
441
442 if ($odb_class->odb_rvg_options['clear_transients'] == 'Y') {
443 $type = 'EXPIRED';
444 $typeLc = 'expired';
445 $option = 'Y';
446 } else {
447 $type = '';
448 $typeLc = '';
449 $option = 'A';
450 }
451
452 $results = $this->odb_get_transients($option);
453
454 if (count($results) > 0) {
455 // TRANSIENTS FOUND
456 $this->grand_total += count($results);
457
458 if ($action == 'analyze_summary' || $action == 'run_summary') {
459 if ($action == 'analyze_summary') {
460 ?>
461 <div class="odb-found-number">
462 <?php _e($type . ' TRANSIENTS found to delete: ', 'rvg-optimize-database');?>
463 <?php echo count($results) . '<br>';?>
464 </div>
465 <?php
466 } // if ($action == 'analyze_summary')
467
468 if ($action == 'run_summary') {
469 ?>
470 <div class="odb-found-number">
471 <?php _e($type . ' TRANSIENTS deleted: ', 'rvg-optimize-database');?>
472 <?php echo count($results) . '<br>';?>
473 </div>
474 <?php
475 } // if ($action == 'run_summary')
476 } // if ($action == 'analyze_summary' || $action == 'run_summary')
477
478 if ($action == 'analyze_detail' || $action == 'run_detail') {
479 // 'RUN' or 'ANALYZE_DETAIL
480 if ($action == 'analyze_detail') {
481 $msg1 = __($type . ' TRANSIENTS', 'rvg-optimize-database');
482 $msg2 = __('total number of ' . $typeLc . ' transients', 'rvg-optimize-database');
483 } else {
484 // ACTION = 'RUN_DETAIL'
485 $msg1 = __('DELETED ' . $type . ' TRANSIENTS', 'rvg-optimize-database');
486 $msg2 = __('total number of ' . $typeLc . ' deleted', 'rvg-optimize-database');
487 } // if ($action == 'analyze_detail')
488 ?>
489 <table border="0" cellspacing="8" cellpadding="2" class="odb-result-table">
490 <tr>
491 <td colspan="4"><div class="odb-found">
492 <?php echo $msg1 ?>
493 </div></td>
494 </tr>
495 <tr>
496 <th align="right" class="odb-border-bottom">#</th>
497 <th align="left" class="odb-border-bottom"><?php _e('prefix', 'rvg-optimize-database');?></th>
498 <th align="left" class="odb-border-bottom"><?php _e('option name', 'rvg-optimize-database');?></th>
499 </tr>
500 <?php
501 } // if ($action == 'analyze_detail' || $action == 'run_detail')
502
503 // LOOP THROUGH THE TRANSIENTS AND DELETE THEM
504 $total_deleted = $this->odb_delete_transients($results, $scheduler, $action, $option);
505
506 if ($scheduler) $odb_class->log_arr["transients"] = $total_deleted;
507
508 if ($action == 'run_summary' || $action == 'run_detail') {
509 // NUMBER OF DELETED SPAM FOR LOG FILE
510 $odb_class->log_arr["transients"] = $total_deleted;
511 } // if (!$scheduler && ($action == 'run_summary' || $action == 'run_detail'))
512
513 if ($action == 'analyze_detail' || $action == 'run_detail') {
514 ?>
515 </table>
516 <?php
517 } // if ($action == 'analyze_detail' || $action == 'run_detail')
518 } else {
519 // NO TRANSIENTS FOUND
520 if (!$scheduler) {
521 if ($option == 'Y') {
522 $msg = 'No EXPIRED TRANSIENTS found to delete';
523 } else {
524 $msg = 'No TRANSIENTS found to delete';
525 } // if ($option == 'Y')
526 ?>
527 <div class="odb-not-found">
528 <?php _e($msg, 'rvg-optimize-database');?>
529 </div>
530 <?php
531 } // if (!$scheduler)
532 } // if (count($results) > 0)
533 } // if ($odb_class->odb_rvg_options['clear_transients'] !== 'N')
534
535
536 /****************************************************************************************
537 *
538 * DELETE PINGBACKS AND TRACKBACKS
539 *
540 ****************************************************************************************/
541 if ($odb_class->odb_rvg_options['clear_pingbacks'] == 'Y') {
542 // GET PINGBACKS AND TRACKBACKS
543 $results = $this->odb_get_pingbacks();
544
545 if (count($results) > 0) {
546 // PINGBACKS AND/OR TRACKBACKS FOUND FOUND
547 $this->grand_total += count($results);
548
549 if ($action == 'analyze_summary' || $action == 'run_summary') {
550 if ($action == 'analyze_summary') {
551 ?>
552 <div class="odb-found-number">
553 <?php _e('PINGBACKS AND/OR TRACKBACKS found to delete: ', 'rvg-optimize-database');?>
554 <?php echo count($results) . '<br>';?>
555 </div>
556 <?php
557 } // if ($action == 'analyze_summary')
558
559 if ($action == 'run_summary') {
560 ?>
561 <div class="odb-found-number">
562 <?php _e('PINGBACKS AND/OR TRACKBACKS deleted: ', 'rvg-optimize-database');?>
563 <?php echo count($results) . '<br>';?>
564 </div>
565 <?php
566 } // if ($action == 'run_summary')
567 } // if ($action == 'analyze_summary' || $action == 'run_summary')
568
569 if ($action == 'analyze_detail' || $action == 'run_detail') {
570 if ($action == 'analyze_detail') {
571 $msg1 = __('PINGBACKS AND TRACKBACKS', 'rvg-optimize-database');
572 $msg2 = __('total number of pingbacks and trackbacks', 'rvg-optimize-database');
573 } else {
574 // ACTION = 'RUN'
575 $msg1 = __('DELETED PINGBACKS AND TRACKBACKS', 'rvg-optimize-database');
576 $msg2 = __('total number of pingbacks and trackbacks deleted', 'rvg-optimize-database');
577 } // if ($action == 'analyze_detail')
578 ?>
579 <table border="0" cellspacing="8" cellpadding="2" class="odb-result-table">
580 <tr>
581 <td colspan="4"><div class="odb-found">
582 <?php echo $msg1 ?>
583 </div></td>
584 </tr>
585 <tr>
586 <th align="right" class="odb-border-bottom">#</th>
587 <th align="left" class="odb-border-bottom"><?php _e('prefix', 'rvg-optimize-database');?></th>
588 <th align="left" class="odb-border-bottom"><?php _e('type', 'rvg-optimize-database');?></th>
589 <th align="left" class="odb-border-bottom"><?php _e('comment_author', 'rvg-optimize-database');?></th>
590 <th align="left" nowrap="nowrap" class="odb-border-bottom"><?php _e('date', 'rvg-optimize-database');?></th>
591 </tr>
592 <?php
593 } // if ($action == 'analyze_detail' || $action == 'run_detail')
594
595 // LOOP THROUGH THE PINGBACKS AND TRACKBACKS AND DELETE THEM
596 $total_deleted = $this->odb_delete_pingbacks($results, $scheduler, $action);
597
598 if ($scheduler) $odb_class->log_arr["pingbacks"] = $total_deleted;
599
600 if ($action == 'run_summary' || $action == 'run_detail') {
601 // NUMBER OF DELETED PINGBACKS AND TRACKBACKS FOR LOG FILE
602 $odb_class->log_arr["pingbacks"] = $total_deleted;
603 } // if (!$scheduler && ($action == 'run_summary' || $action == 'run_detail'))
604
605 if ($action == 'analyze_detail' || $action == 'run_detail') {
606 ?>
607 </table>
608 <?php
609 } // if ($action == 'analyze_detail' || $action == 'run_detail')
610
611 } else {
612 // NO PINGBACKS NOR TRACKBACKS FOUND
613 if (!$scheduler) {
614 ?>
615 <div class="odb-not-found">
616 <?php _e('No PINGBACKS nor TRACKBACKS found to delete', 'rvg-optimize-database');?>
617 </div>
618 <?php
619 } // if (!$scheduler)
620 } // if (count($results) > 0)
621 } // if ($odb_class->odb_rvg_options['clear_pingbacks'] == 'Y')
622
623
624 /****************************************************************************************
625 *
626 * DELETE OEMBED CACHE
627 *
628 ****************************************************************************************/
629 if ($odb_class->odb_rvg_options['clear_oembed'] == 'Y') {
630
631 // GET OEMBED CACHE
632 $results = $this->odb_get_oembed();
633
634 if (count($results) > 0) {
635 // OEMBED CACHE FOUND
636 $this->grand_total += count($results);
637
638 if ($action == 'analyze_summary' || $action == 'run_summary') {
639 if ($action == 'analyze_summary') {
640 ?>
641 <div class="odb-found-number">
642 <?php _e('OEMBED CACHE ITEMS found to delete: ', 'rvg-optimize-database');?>
643 <?php echo count($results) . '<br>';?>
644 </div>
645 <?php
646 } // if ($action == 'analyze_summary')
647
648 if ($action == 'run_summary') {
649 ?>
650 <div class="odb-found-number">
651 <?php _e('OEMBED CACHE ITEMS deleted: ', 'rvg-optimize-database');?>
652 <?php echo count($results) . '<br>';?>
653 </div>
654 <?php
655 } // if ($action == 'run_summary')
656 } // if ($action == 'analyze_summary' || $action == 'run_summary')
657
658 if ($action == 'analyze_detail' || $action == 'run_detail') {
659 if ($action == 'analyze_detail') {
660 $msg1 = __('OEMBED CACHE items', 'rvg-optimize-database');
661 $msg2 = __('total number of oembed cache items', 'rvg-optimize-database');
662 } else {
663 // ACTION = 'RUN'
664 $msg1 = __('DELETED OEMBED CACHE ITEMS', 'rvg-optimize-database');
665 $msg2 = __('total number of oembed cache items deleted', 'rvg-optimize-database');
666 } // if ($action == 'analyze_detail')
667 ?>
668 <table border="0" cellspacing="8" cellpadding="2" class="odb-result-table">
669 <tr>
670 <td colspan="4"><div class="odb-found">
671 <?php echo $msg1 ?>
672 </div></td>
673 </tr>
674 <tr>
675 <th align="right" class="odb-border-bottom">#</th>
676 <th align="left" class="odb-border-bottom"><?php _e('prefix', 'rvg-optimize-database');?></th>
677 <th align="left" class="odb-border-bottom"><?php _e('meta key', 'rvg-optimize-database');?></th>
678 <th align="left" class="odb-border-bottom"><?php _e('meta value', 'rvg-optimize-database');?></th>
679 </tr>
680 <?php
681 } // if ($action == 'analyze_detail' || $action == 'run_detail')
682
683 // LOOP THROUGH THE OEMBEDS
684 $total_deleted = $this->odb_delete_oembed($results, $scheduler, $action);
685
686 if ($scheduler) $odb_class->log_arr["oembeds"] = $total_deleted;
687
688 if ($action == 'run_summary' || $action == 'run_detail') {
689 // NUMBER OF OEMBED CACHE ITEMS FOR LOG FILE
690 $odb_class->log_arr["oembeds"] = $total_deleted;
691 } // if (!$scheduler && ($action == 'run_summary' || $action == 'run_detail')
692
693 if ($action == 'analyze_detail' || $action == 'run_detail') {
694 ?>
695 </table>
696 <?php
697 } // if ($action == 'analyze_detail' || $action == 'run_detail')
698 } else {
699 // NO OEMBED CACHE ITEMS FOUND
700 if (!$scheduler) {
701 ?>
702 <div class="odb-not-found">
703 <?php _e('No OEMBED CACHE ITEMS found to delete', 'rvg-optimize-database');?>
704 </div>
705 <?php
706 } // if (!$scheduler)
707 } // if (count($results) > 0)
708 } // if ($odb_class->odb_rvg_options['clear_oembed'] == 'Y')
709
710
711 /****************************************************************************************
712 *
713 * DELETE ORPHANS
714 *
715 ****************************************************************************************/
716 if($odb_class->odb_rvg_options['clear_orphans'] == 'Y') {
717
718 // POSTMETA AND MEDIA ORPHANS
719 $results = $this->odb_get_orphans($scheduler);
720
721 if (count($results) > 0) {
722 // ORPHANS FOUND
723 $this->grand_total += count($results);
724
725 if ($action == 'analyze_summary' || $action == 'run_summary') {
726 if ($action == 'analyze_summary') {
727 ?>
728 <div class="odb-found-number">
729 <?php _e('ORPHANS found to delete: ', 'rvg-optimize-database');?>
730 <?php echo count($results) . '<br>';?>
731 </div>
732 <?php
733 } // if ($action == 'analyze_summary')
734
735 if ($action == 'run_summary') {
736 ?>
737 <div class="odb-found-number">
738 <?php _e('ORPHANS deleted: ', 'rvg-optimize-database');?>
739 <?php echo count($results) . '<br>';?>
740 </div>
741 <?php
742 } // if ($action == 'run_summary')
743 } // if ($action == 'analyze_summary' || $action == 'run_summary')
744
745 if ($action == 'analyze_detail' || $action == 'run_detail') {
746
747 if ($action == 'analyze_detail') {
748 $msg1 = __('ORPHANS', 'rvg-optimize-database');
749 $msg2 = __('total number of orphans', 'rvg-optimize-database');
750 } else {
751 // ACTION = 'RUN_DETAIL'
752 $msg1 = __('DELETED ORPHANS', 'rvg-optimize-database');
753 $msg2 = __('total number of orphans deleted', 'rvg-optimize-database');
754 } // if ($action == 'analyze_detail')
755 ?>
756 <table border="0" cellspacing="8" cellpadding="2" class="odb-result-table">
757 <tr>
758 <td colspan="4"><div class="odb-found">
759 <?php echo $msg1 ?>
760 </div></td>
761 </tr>
762 <tr>
763 <th align="right" class="odb-border-bottom">#</th>
764 <th align="left" class="odb-border-bottom"><?php _e('prefix', 'rvg-optimize-database');?></th>
765 <th align="left" class="odb-border-bottom"><?php _e('type', 'rvg-optimize-database');?></th>
766 <th align="left" class="odb-border-bottom"><?php _e('id', 'rvg-optimize-database');?></th>
767 <th align="left" class="odb-border-bottom"><?php _e('title', 'rvg-optimize-database');?></th>
768 <th align="left" nowrap="nowrap" class="odb-border-bottom"><?php _e('modified', 'rvg-optimize-database');?></th>
769 <th align="left" class="odb-border-bottom"><?php _e('taxonomy', 'rvg-optimize-database');?></th>
770 <th align="left" class="odb-border-bottom"><?php _e('meta key', 'rvg-optimize-database');?></th>
771 <th align="left" class="odb-border-bottom"><?php _e('meta value', 'rvg-optimize-database');?></th>
772 </tr>
773 <?php
774 } // if ($action == 'analyze_detail' || $action == 'run_detail')
775
776 // LOOP THROUGH THE ORPHANS AND DELETE THEM
777 $total_deleted = $this->odb_delete_orphans($results, $scheduler, $action);
778
779 if ($scheduler) $odb_class->log_arr["orphans"] = $total_deleted;
780
781 if ($action == 'run_summary' || $action == 'run_detail') {
782 // NUMBER OF ORPHANS FOR LOG FILE
783 $odb_class->log_arr["orphans"] = $total_deleted;
784 } // if (!$scheduler && ($action == 'run_summary' || $action == 'run_detail')
785 ?>
786 </table>
787 <?php
788 } else {
789 // NO ORPHANS FOUND
790 if (!$scheduler) {
791 ?>
792 <div class="odb-not-found">
793 <?php _e('No ORPHANS found to delete', 'rvg-optimize-database');?>
794 </div>
795 <?php
796 } // if (!$scheduler)
797 } // if (count($results) > 0)
798 } // if($odb_class->odb_rvg_options['clear_orphans'] == 'Y')
799 ?>
800 <div class="odb-found-number">
801 <?php _e('TOTAL NUMBER OF ITEMS: ', 'rvg-optimize-database');?>
802 <?php echo '<span class="odb-blue">' . $this->grand_total . '</span><br>';?>
803 </div>
804 <?php
805 } // odb_run_cleaner()
806
807
808 /********************************************************************************************
809 *
810 * RUN OPTIMIZER
811 *
812 ********************************************************************************************/
813 function odb_run_optimizer($scheduler, $action) {
814 global $odb_class;
815
816 if(!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')) {
817 ?>
818 <div class="odb-optimizing-table" class="odb-padding-left">
819 <div class="odb-title-bar">
820 <h2><?php _e('Optimizing Database Tables', 'rvg-optimize-database');?></h2>
821 </div>
822 <br>
823 <br>
824 <table border="0" cellspacing="8" cellpadding="2">
825 <tr>
826 <th class="odb-border-bottom" align="right">#</th>
827 <th class="odb-border-bottom" align="left"><?php _e('table name', 'rvg-optimize-database');?></th>
828 <th class="odb-border-bottom" align="left"><?php _e('optimization result', 'rvg-optimize-database');?></th>
829 <th class="odb-border-bottom" align="left"><?php _e('engine', 'rvg-optimize-database');?></th>
830 <th class="odb-border-bottom" align="right"><?php _e('table rows', 'rvg-optimize-database');?></th>
831 <th class="odb-border-bottom" align="right"><?php _e('table size', 'rvg-optimize-database');?></th>
832 </tr>
833 <?php
834 } // if(!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail'))
835
836 # OPTIMIZE THE DATABASE TABLES
837 $this->nr_of_optimized_tables = $this->odb_optimize_tables($scheduler, $action);
838
839 if(!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')) {
840 ?>
841 </table>
842 </div><!-- /odb-optimizing-table -->
843 <?php
844 } // if(!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail'))
845 } // odb_run_optimizer()
846
847
848 /********************************************************************************************
849 *
850 * CALCULATE AND DISPLAY SAVINGS
851 *
852 ********************************************************************************************/
853 function odb_savings($scheduler, $action) {
854 global $odb_class;
855 global $odb_logger_obj;
856
857 // NUMBER OF TABLES
858 $odb_class->log_arr["tables"] = $this->nr_of_optimized_tables;
859 // DATABASE SIZE BEFORE OPTIMIZATION
860 $odb_class->log_arr["before"] = $odb_class->odb_utilities_obj->odb_format_size($this->start_size,3);
861 // DATABASE SIZE AFTER OPTIMIZATION
862 $end_size = $odb_class->odb_utilities_obj->odb_get_db_size();
863 $odb_class->log_arr["after"] = $odb_class->odb_utilities_obj->odb_format_size($end_size,3);
864 // TOTAL SAVING
865 $odb_class->log_arr["savings"] = $odb_class->odb_utilities_obj->odb_format_size(($this->start_size - $end_size),3);
866
867 if ($scheduler || $action == 'run_summary' || $action == 'run_detail') {
868 // WRITE RESULTS TO THE DATABASE - v4.6
869 $odb_class->odb_logger_obj->odb_add_log($odb_class->log_arr);
870 }
871
872 $total_savings = $odb_class->odb_rvg_options['total_savings'];
873 $total_savings += ($this->start_size - $end_size);
874 $odb_class->odb_rvg_options['total_savings'] = $total_savings;
875
876 $odb_class->odb_multisite_obj->odb_ms_update_option('odb_rvg_options', $odb_class->odb_rvg_options);
877
878 if(!$scheduler) {
879 ?>
880 <div id="odb-savings" class="odb-padding-left">
881 <div class="odb-title-bar">
882 <h2><?php _e('Savings', 'rvg-optimize-database');?></h2>
883 </div>
884 <br>
885 <br>
886 <table border="0" cellspacing="8" cellpadding="2">
887 <tr>
888 <th>&nbsp;</th>
889 <th class="odb-border-bottom"><?php _e('size of the database', 'rvg-optimize-database');?></th>
890 </tr>
891 <tr>
892 <td align="right"><?php _e('BEFORE optimization', 'rvg-optimize-database');?></td>
893 <td align="right" class="odb-bold"><?php echo $odb_class->odb_utilities_obj->odb_format_size($this->start_size,3); ?></td>
894 </tr>
895 <tr>
896 <td align="right"><?php _e('AFTER optimization', 'rvg-optimize-database');?></td>
897 <td align="right" class="odb-bold"><?php echo $odb_class->odb_utilities_obj->odb_format_size($end_size,3); ?></td>
898 </tr>
899 <tr>
900 <td align="right" class="odb-bold"><?php _e('SAVINGS THIS TIME', 'rvg-optimize-database');?></td>
901 <td align="right" class="odb-border-top odb-bold"><?php echo $odb_class->odb_utilities_obj->odb_format_size(($this->start_size - $end_size),3); ?></td>
902 </tr>
903 <tr>
904 <td align="right" class="odb-bold"><?php _e('TOTAL SAVINGS SINCE THE FIRST RUN', 'rvg-optimize-database');?></td>
905 <td align="right" class="odb-border-top odb-bold"><?php echo $odb_class->odb_utilities_obj->odb_format_size($total_savings,3); ?></td>
906 </tr>
907 </table>
908 </div><!-- /odb-savings -->
909 <?php
910 } // if(!$scheduler)
911 } // odb_savings()
912
913
914 /********************************************************************************************
915 *
916 * SHOW LOADING TIME
917 *
918 ********************************************************************************************/
919 function odb_done($analyze = false) {
920 global $odb_class;
921
922 $time = microtime();
923 $time = explode(' ', $time);
924 $time = $time[1] + $time[0];
925 $finish = $time;
926
927 $total_time = round(($finish - $odb_class->odb_start_time), 4);
928 ?>
929 <div id="odb-done" class="odb-padding-left">
930 <div class="odb-title-bar">
931 <h2>
932 <?php _e('DONE!', 'rvg-optimize-database');?>
933 </h2>
934 </div>
935 <br>
936 <br>
937 <?php
938 if (!$analyze) {
939 ?>
940 <span class="odb-padding-left"><?php _e('Optimization took', 'rvg-optimize-database')?>&nbsp;<strong><?php echo $total_time;?></strong>&nbsp;<?php _e('seconds', 'rvg-optimize-database')?>.</span>
941 <?php
942 } // if ($analyze)
943 ?>
944 <?php
945 // v4.5.1
946 $odb_class->odb_last_run_seconds = $total_time;
947
948 if($odb_class->odb_logger_obj->odb_log_count() > 0) {
949 ?>
950 <script>
951 function odb_confirm_delete() {
952 <?php
953 $nonce = wp_create_nonce( 'rvg-optimize-database' );
954 // v4.6.2
955 $msg = str_replace("'", "\'", __('Clear the log?', 'rvg-optimize-database'));
956 ?>
957 if(confirm('<?php echo $msg?>')) {
958 self.location = 'tools.php?page=rvg-optimize-database&action=clear_log&_wpnonce=<?php echo esc_attr( $nonce ); ?>'
959 return;
960 }
961 } // odb_confirm_delete()
962 </script>
963 <br><br>
964 &nbsp;
965 <input class="button odb-normal" type="button" name="view_log" value="<?php _e('View Log', 'rvg-optimize-database');?>" onclick="self.location='tools.php?page=rvg-optimize-database&action=view_log&_wpnonce=<?php echo esc_attr( $nonce ); ?>'" />
966 &nbsp;
967 <input class="button odb-normal" type="button" name="clear_log" value="<?php _e('Clear Log', 'rvg-optimize-database');?>" onclick="return odb_confirm_delete();" />
968 <?php
969 } // if($odb_class->odb_logger_obj->odb_log_count() > 0)
970 ?>
971 </div><!-- /odb-done -->
972 <?php
973 } // odb_done()
974
975
976 /********************************************************************************************
977 *
978 * GET REVISIONS (OLDER THAN x DAYS)
979 *
980 ********************************************************************************************/
981 function odb_get_revisions_older_than() {
982 global $odb_class, $wpdb;
983
984 $res_arr = array();
985
986 // CUSTOM POST TYPES (from v4.4)
987 $rel_posttypes = $odb_class->odb_rvg_options['post_types'];
988 $in = [];
989 foreach ($rel_posttypes as $posttype => $value) {
990 if ($value == 'Y') {
991 $in[] = $wpdb->prepare( '%s', $posttype );
992 } // if ($value == 'Y')
993 } // foreach($rel_posttypes as $posttypes)
994
995 $where = '';
996 if( ! empty( $in ) ) {
997 $where = " AND p2.`post_type` IN (" . implode( ', ', $in ) . ")";
998 } else {
999 // NO POST TYPES TO DELETE REVISIONS FOR... SKIP!
1000 return $res_arr;
1001 } // if($in != '')
1002
1003 $older_than = $odb_class->odb_rvg_options['older_than'];
1004
1005 $index = 0;
1006
1007 // LOOP THROUGH THE SITES (IF MULTI SITE)
1008 for($i=0; $i<count($odb_class->odb_ms_prefixes); $i++) {
1009 $prefix = $odb_class->odb_ms_prefixes[$i];
1010
1011 $sql = $wpdb->prepare("
1012 SELECT %s AS site,
1013 p1.`ID`,
1014 p1.`post_parent`,
1015 p1.`post_title`,
1016 p1.`post_modified`
1017 FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'posts' ) . " p1, " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'posts' ) . " p2
1018 WHERE p1.`post_type` = 'revision'
1019 AND p1.`post_parent` = p2.ID
1020 {$where}
1021 AND p1.`post_modified` < date_sub(now(), INTERVAL %d DAY)
1022 ORDER BY UCASE(p1.`post_title`)
1023 ",
1024 $prefix,
1025 $older_than);
1026
1027 //echo 'OLDER: '.$sql.'<br>';
1028
1029 $res = $wpdb->get_results($sql, ARRAY_A);
1030
1031 for($j=0; $j<count($res); $j++) {
1032 if(isset($res[$j]) && !$this->odb_post_is_excluded($res[$j]['post_parent'])) {
1033 $res_arr[$index] = $res[$j];
1034 $index++;
1035 } // if(isset($res[$j]) && !$this->odb_post_is_excluded($res[$j]['post_parent']))
1036 } // for($j=0; $j<count($res); $j++)
1037
1038 } // for($i=0; $i<count($odb_class->odb_ms_prefixes); $i++)
1039
1040 return $res_arr;
1041 } // odb_get_revisions_older_than()
1042
1043
1044 /********************************************************************************************
1045 *
1046 * GET REVISIONS (KEEP MAX NUMBER OF REVISIONS)
1047 *
1048 ********************************************************************************************/
1049 function odb_get_revisions_keep_revisions() {
1050 global $odb_class, $wpdb;
1051
1052 $res_arr = array();
1053
1054 // CUSTOM POST TYPES (from v4.4)
1055 $rel_posttypes = $odb_class->odb_rvg_options['post_types'];
1056 $in = [];
1057 foreach ($rel_posttypes as $posttype => $value) {
1058 if ($value == 'Y') {
1059 $in[] = $wpdb->prepare( '%s', $posttype );
1060 } // if ($value == 'Y')
1061 } // foreach($rel_posttypes as $posttypes)
1062
1063 $where1 = '';
1064 if ( ! empty( $in ) ) {
1065 $where1 = " AND p2.`post_type` IN (" . implode( ', ', $in ) . ")";
1066 } else {
1067 // NO POST TYPES TO DELETE REVISIONS FOR... SKIP!
1068 return $res_arr;
1069 } // if($in != '')
1070
1071 // MAX NUMBER OF REVISIONS TO KEEP
1072 $max_revisions = $odb_class->odb_rvg_options['nr_of_revisions'];
1073
1074 $index = 0;
1075
1076 // SKIP REVISIONS THAT WILL BE DELETED BY THE 'OLDER THAN' OPTION
1077 $where2 = '';
1078 if($odb_class->odb_rvg_options['delete_older'] == 'Y') {
1079 $older_than = $odb_class->odb_rvg_options['older_than'];
1080 $where2 = $wpdb->prepare( "AND p1.`post_modified` >= date_sub(now(), INTERVAL %d DAY)", $older_than );
1081 }
1082
1083 for($i=0; $i<count($odb_class->odb_ms_prefixes); $i++) {
1084 $prefix = $odb_class->odb_ms_prefixes[$i];
1085
1086 $sql = $wpdb->prepare("
1087 SELECT %s AS site,
1088 p1.`ID`,
1089 p1.`post_parent`,
1090 p1.`post_title`,
1091 p1.`post_modified`,
1092 COUNT(*) cnt
1093 FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'posts' ) . " p1, " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'posts' ) . " p2
1094 WHERE p1.`post_type` = 'revision'
1095 AND p1.`post_parent` = p2.ID
1096 {$where1}
1097 {$where2}
1098 GROUP BY p1.`post_parent`
1099 HAVING COUNT(*) > %d
1100 ORDER BY UCASE(p1.`post_title`)
1101 ",
1102 $prefix,
1103 $max_revisions);
1104
1105 //echo 'KEEP: '.$sql.'<br>';
1106
1107 $res = $wpdb->get_results($sql, ARRAY_A);
1108 for($j=0; $j<count($res); $j++) {
1109 if(isset($res[$j]) && !$this->odb_post_is_excluded($res[$j]['post_parent'])) {
1110 $res_arr[$index] = $res[$j];
1111 $index++;
1112 }
1113 } // for($j=0; $j<count($res); $j++)
1114 } // for($i=0; $i<count($odb_class->odb_ms_prefixes); $i++)
1115
1116 return $res_arr;
1117 } // odb_get_revisions_keep_revisions()
1118
1119
1120 /********************************************************************************************^
1121 *
1122 * DELETE THE REVISIONS
1123 *
1124 ********************************************************************************************/
1125 function odb_delete_revisions($scheduler, $action = 'run_detail') {
1126 global $odb_class, $wpdb;
1127
1128 $total_deleted = 0;
1129
1130 // v4.8.1
1131 if($scheduler && $action == '') {
1132 $action = 'run_detail';
1133 } // if($scheduler && $action == '')
1134
1135 if($odb_class->odb_rvg_options['delete_older'] == 'Y') {
1136 // DELETE REVISIONS OLDER THAN x DAYS
1137 $results = $this->odb_get_revisions_older_than();
1138 $older_than = $odb_class->odb_rvg_options['older_than'];
1139 $total_deleted += count($results);
1140
1141 for($i = 0; $i < count($results); $i++) {
1142 if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')) {
1143 ?>
1144 <tr>
1145 <td align="right" valign="top"><?php echo ($i + 1)?>.</td>
1146 <td align="left" valign="top"><?php echo $results[$i]['site']?></td>
1147 <td valign="top" class="odb-bold"><?php echo $results[$i]['post_title']?></td>
1148 <td valign="top" class="odb-bold"><?php echo $results[$i]['post_modified']?></td><?php
1149 } // if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail'))
1150
1151 if ($scheduler || $action == 'run_summary' || $action == 'run_detail') {
1152 $sql_delete = $wpdb->prepare("
1153 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$i]['site'] . 'posts' ) . "
1154 WHERE `ID` = %d
1155 ", $results[$i]['ID']);
1156
1157 $wpdb->get_results($sql_delete);
1158 } // if ($action == 'run')
1159
1160 if(!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')) {
1161 ?>
1162 <td align="right" valign="top" class="odb-bold">1</td>
1163 </tr>
1164 <?php
1165 } // if(!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')
1166 } // for($i=0; $i<count($results); $i++)
1167 } // if($odb_class->odb_rvg_options['delete_older'] == 'Y')
1168
1169 if($odb_class->odb_rvg_options['rvg_revisions'] == 'Y') {
1170 // KEEP MAX NUMBER OF REVISIONS
1171 $results = $this->odb_get_revisions_keep_revisions();
1172 $max_revisions = $odb_class->odb_rvg_options['nr_of_revisions'];
1173
1174 for($i = 0; $i < count($results); $i++) {
1175 $nr_to_delete = $results[$i]['cnt'] - $max_revisions;
1176 $total_deleted += $nr_to_delete;
1177
1178 if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')) {
1179 ?>
1180 <tr>
1181 <td align="right" valign="top"><?php echo ($i + 1)?>.</td>
1182 <td align="left" valign="top"><?php echo $results[$i]['site']?></td>
1183 <td valign="top" class="odb-bold"><?php echo $results[$i]['post_title']?></td>
1184 <td valign="top" class="odb-bold"><?php echo $results[$i]['post_modified']?></td><?php
1185 } // if (!$scheduler && $action == 'analyze_detail' || $action == 'run_detail'))
1186
1187 $sql_get_posts = $wpdb->prepare( "
1188 SELECT `ID`, `post_modified`
1189 FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$i]['site'] . 'posts' ) . "
1190 WHERE `post_parent` = %d
1191 AND `post_type` = 'revision'
1192 ORDER BY `post_modified` ASC
1193 ", $results[$i]['post_parent']);
1194
1195 $results_get_posts = $wpdb->get_results($sql_get_posts);
1196
1197 for($j = 0; $j < $nr_to_delete; $j++) {
1198 // if(!$scheduler) echo $results_get_posts[$j]->post_modified.'<br>';
1199 if ($scheduler || $action == 'run_summary' || $action == 'run_detail') {
1200 $sql_delete = $wpdb->prepare("
1201 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$i]['site'] . 'posts' ) . "
1202 WHERE `ID` = %d
1203 ", $results_get_posts[$j]->ID);
1204
1205 $wpdb->get_results($sql_delete);
1206 } // if ($action == 'run')
1207 } // for($j = 0; $j < $nr_to_delete; $j++)
1208
1209 if(!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')) {
1210 ?></td>
1211 <td align="right" valign="top" class="odb-bold"><?php echo $nr_to_delete?> <?php _e('of', 'rvg-optimize-database')?> <?php echo $results[$i]['cnt'];?></td>
1212 </tr>
1213 <?php
1214 } // if(!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail'))
1215 } // for($i = 0; $i < count($results); $i++)
1216 } // if($odb_class->odb_rvg_options['rvg_revisions'] == 'Y')
1217
1218 return $total_deleted;
1219 } // function odb_delete_revisions()
1220
1221
1222 /********************************************************************************************
1223 *
1224 * CHECK IF POST IS EXCLUDED BY A CUSTOM FIELD ('keep_revisions')
1225 *
1226 ********************************************************************************************/
1227 function odb_post_is_excluded($parent_id) {
1228 $keep_revisions = get_post_meta($parent_id, 'keep_revisions', true);
1229 return ($keep_revisions === 'Y');
1230 } // odb_post_is_exclude()
1231
1232
1233 /********************************************************************************************
1234 *
1235 * GET TRASHED POSTS / PAGES AND COMMENTS
1236 *
1237 ********************************************************************************************/
1238 function odb_get_trash() {
1239 global $wpdb, $odb_class;
1240
1241 $res_arr = array();
1242
1243 $index = 0;
1244 // LOOP TROUGH SITES
1245 for($i=0; $i<count($odb_class->odb_ms_prefixes); $i++) {
1246 $prefix = $odb_class->odb_ms_prefixes[$i];
1247
1248 $sql = $wpdb->prepare("
1249 SELECT %s AS site,
1250 `ID` AS id,
1251 'post' AS post_type,
1252 `post_title` AS title,
1253 `post_modified` AS modified
1254 FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'posts' ) . "
1255 WHERE `post_status` = 'trash'
1256 UNION ALL
1257 SELECT %s AS site,
1258 `comment_ID` AS id,
1259 'comment' AS post_type,
1260 `comment_author_IP` AS title,
1261 `comment_date` AS modified
1262 FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'comments' ) . "
1263 WHERE `comment_approved` = 'trash'
1264 ORDER BY post_type, UCASE(title)
1265 ", $prefix, $prefix);
1266
1267 $res = $wpdb->get_results($sql, ARRAY_A);
1268
1269 if($res != null) {
1270 $res_arr[$index] = $res[0];
1271 $index++;
1272 } // if($res != null)
1273 } // for($i=0; $i<count($odb_class->odb_ms_prefixes); $i++)
1274
1275 return $res_arr;
1276 } // odb_get_trash()
1277
1278
1279 /********************************************************************************************
1280 *
1281 * DELETE TRASHED POSTS AND PAGES
1282 *
1283 ********************************************************************************************/
1284 function odb_delete_trash($results, $scheduler, $action) {
1285 global $wpdb, $odb_class;
1286
1287 $total_deleted = count($results);
1288
1289 for ($i = 0; $i < $total_deleted; $i++) {
1290 if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')) {
1291 ?>
1292 <tr>
1293 <td align="right" valign="top"><?php echo ($i + 1); ?></td>
1294 <td align="left" valign="top"><?php echo $results[$i]['site']?></td>
1295 <td valign="top"><?php echo $results[$i]['post_type']; ?></td>
1296 <td valign="top"><?php echo $results[$i]['title']; ?></td>
1297 <td valign="top" nowrap="nowrap"><?php echo $results[$i]['modified']; ?></td>
1298 </tr>
1299 <?php
1300 } // if (!$scheduler && ($action == 'analyze_detail' || $action = 'run_detail'))
1301
1302 if($results[$i]['post_type'] == 'comment') {
1303 // DELETE META DATA (IF ANY...)
1304 if ($scheduler || $action == 'run_summary' || $action == 'run_detail') {
1305 $sql_delete = $wpdb->prepare("
1306 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$i]['site'] . 'commentmeta' ) . "
1307 WHERE `comment_id` = %d
1308 ", $results[$i]['id']);
1309 $wpdb->get_results($sql_delete);
1310 } // if ($action == 'run_summary' || $action == 'run_detail')
1311 } // if($results[$i]['post_type'] == 'comment')
1312
1313 if ($scheduler || $action == 'run_summary' || $action == 'run_detail') {
1314 // DELETE TRASHED POSTS / PAGES
1315 $sql_delete = $wpdb->prepare("
1316 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$i]['site'] . 'posts' ) . "
1317 WHERE `post_status` = %s
1318 ", 'trash');
1319 $wpdb->get_results($sql_delete);
1320
1321 // DELETE TRASHED COMMENTS
1322 $sql_delete = $wpdb->prepare("
1323 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$i]['site'] . 'comments' ) . "
1324 WHERE `comment_approved` = %s
1325 ", 'trash');
1326 $wpdb->get_results($sql_delete);
1327 } // if ($action == 'run_summary' || $action == 'run_detail')
1328 } // for ($i = 0; $i < $total_deleted; $i++)
1329
1330 return $total_deleted;
1331 } // odb_delete_trash()
1332
1333
1334 /********************************************************************************************
1335 *
1336 * GET SPAMMED COMMENTS
1337 *
1338 ********************************************************************************************/
1339 function odb_get_spam() {
1340 global $wpdb, $odb_class;
1341
1342 $res_arr = array();
1343
1344 // LOOP THROUGH SITES
1345 for ($i = 0; $i < count($odb_class->odb_ms_prefixes); $i++) {
1346 $prefix = $odb_class->odb_ms_prefixes[$i];
1347
1348 $sql = $wpdb->prepare("
1349 SELECT %s AS site,
1350 `comment_ID`,
1351 `comment_author`,
1352 `comment_author_email`,
1353 `comment_date`
1354 FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'comments' ) . "
1355 WHERE `comment_approved` = 'spam'
1356 ORDER BY UCASE(`comment_author`)
1357 ", $prefix);
1358
1359 $res = $wpdb->get_results($sql, ARRAY_A);
1360
1361 for($j = 0; $j < count($res); $j++) {
1362 array_push($res_arr, $res[$j]);
1363 } // for($j = 0; $j < count($res); $j++)
1364 } // for ($i = 0; $i < count($odb_class->odb_ms_prefixes); $i++)
1365 return $res_arr;
1366 } // odb_get_spam()
1367
1368
1369 /********************************************************************************************
1370 *
1371 * DELETE SPAMMED ITEMS
1372 *
1373 ********************************************************************************************/
1374 function odb_delete_spam($results, $scheduler, $action = 'run') {
1375 global $wpdb, $odb_class;
1376
1377 $total_deleted = count($results);
1378
1379 for ($i = 0; $i < count($results); $i++) {
1380 if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')) {
1381 ?>
1382 <tr>
1383 <td align="right" valign="top"><?php echo ($i + 1); ?></td>
1384 <td align="left" valign="top"><?php echo $results[$i]['site']?></td>
1385 <td valign="top"><?php echo $results[$i]['comment_author']; ?></td>
1386 <td valign="top"><?php echo $results[$i]['comment_author_email']; ?></td>
1387 <td valign="top" nowrap="nowrap"><?php echo $results[$i]['comment_date']; ?></td>
1388 </tr>
1389 <?php
1390 } // if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail'))
1391
1392 if ($scheduler || $action == 'run_summary' || $action == 'run_detail') {
1393 $sql_delete = $wpdb->prepare("
1394 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$i]['site'] . 'commentmeta' ) . "
1395 WHERE `comment_id` = %d
1396 ", $results[$i]['comment_ID']);
1397 $wpdb->get_results($sql_delete);
1398
1399 $sql_delete = $wpdb->prepare("
1400 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$i]['site'] . 'comments' ) . "
1401 WHERE `comment_approved` = %s
1402 ", 'spam');
1403 $wpdb->get_results($sql_delete);
1404 } // if ($action == 'run_summary' || $action == 'run_detail')
1405 } // for ($i = 0; $i < count($results); $i++)
1406
1407 return $total_deleted;
1408 } // odb_delete_spam()
1409
1410
1411 /********************************************************************************************
1412 *
1413 * GET UNUSED TAGS
1414 *
1415 ********************************************************************************************/
1416 function odb_get_unused_tags() {
1417 global $wpdb, $odb_class;
1418
1419 $res_arr = array();
1420
1421 // LOOP THROUGH SITES
1422 for($i=0; $i<count($odb_class->odb_ms_prefixes); $i++) {
1423 $prefix = $odb_class->odb_ms_prefixes[$i];
1424
1425 $sql = $wpdb->prepare("
1426 SELECT %s AS site,
1427 a.term_id AS term_id, a.name AS name
1428 FROM `" . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'terms' ) . "` a, `" . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'term_taxonomy' ) . "` b
1429 WHERE a.term_id = b.term_id
1430 AND b.taxonomy = 'post_tag'
1431 AND b.term_taxonomy_id NOT IN (
1432 SELECT term_taxonomy_id
1433 FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'term_relationships' ) . "
1434 )
1435 ORDER BY name
1436 ", $prefix);
1437
1438 $res = $wpdb->get_results($sql, ARRAY_A);
1439
1440 for($j = 0; $j < count($res); $j++) {
1441 array_push($res_arr, $res[$j]);
1442 } // for($j = 0; $j < count($res); $j++)
1443 } // for($i=0; $i<count($odb_class->odb_ms_prefixes); $i++)
1444 return $res_arr;
1445 } // odb_get_unused_tags
1446
1447
1448 /********************************************************************************************
1449 *
1450 * DELETE UNUSED TAGS
1451 *
1452 ********************************************************************************************/
1453 function odb_delete_unused_tags($results, $scheduler, $action = 'run') {
1454 global $wpdb, $odb_class;
1455
1456 $total_deleted = count($results);
1457
1458 for ($i = 0; $i < $total_deleted; $i++) {
1459 if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')) {
1460 ?>
1461 <tr>
1462 <td align="right" valign="top"><?php echo ($i + 1); ?></td>
1463 <td align="left" valign="top"><?php echo $results[$i]['site']?></td>
1464 <td valign="top"><?php echo $results[$i]['name']; ?></td>
1465 </tr>
1466 <?php
1467 } // if (!$scheduler && ($action = 'analyze_detail' || $action == 'run_detail'))
1468
1469 if ($scheduler || $action == 'run_summary' || $action == 'run_detail') {
1470 $sql_del = $wpdb->prepare("
1471 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$i]['site'] . 'term_taxonomy' ) . "
1472 WHERE term_id = %d
1473 ", $results[$i]['term_id']);
1474 $wpdb->get_results($sql_del);
1475
1476 $sql_del = $wpdb->prepare("
1477 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$i]['site'] . 'terms' ) . "
1478 WHERE term_id = %d
1479 ", $results[$i]['term_id']);
1480 $wpdb->get_results($sql_del);
1481 } // if ($action == 'run_summary' || $action == 'run_detail')
1482 } // for ($i = 0; $i < $total_deleted; $i++)
1483
1484 return $total_deleted;
1485 } // odb_delete_unused_tags()
1486
1487
1488 /********************************************************************************************
1489 *
1490 * IS THE UNUSED TAG USED IN ONE OR MORE SCHEDULED POSTS?
1491 *
1492 ********************************************************************************************/
1493 function odb_delete_tags_is_scheduled($term_id, $odb_prefix) {
1494 global $wpdb, $odb_class;
1495
1496 $sql_get_posts = $wpdb->prepare("
1497 SELECT p.post_status
1498 FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $odb_prefix . 'term_relationships' ) . " t, " . $odb_class->odb_utilities_obj->odb_sanitize_key( $odb_prefix . 'posts' ) . " p
1499 WHERE t.term_taxonomy_id = %s
1500 AND t.object_id = p.ID
1501 ", $term_id);
1502
1503 $results_get_posts = $wpdb->get_results($sql_get_posts);
1504 for($i=0; $i<count($results_get_posts); $i++)
1505 if($results_get_posts[$i]->post_status == 'future') return true;
1506
1507 return false;
1508 } // odb_delete_tags_is_scheduled()
1509
1510
1511 /********************************************************************************************
1512 *
1513 * GET TRANSIENTS v4.8.2
1514 *
1515 ********************************************************************************************/
1516 function odb_get_transients($option = "Y") {
1517 // $option == "A": delete all transients
1518 // %option == "Y": only delete EXPIRED transients
1519
1520 global $wpdb, $odb_class;
1521
1522 $res_arr = array();
1523
1524 // ONE MINUTE DELAY
1525 $delay = time() - 60;
1526
1527 // LOOP THROUGH SITES
1528 for($i=0; $i<count($odb_class->odb_ms_prefixes); $i++) {
1529 $prefix = $odb_class->odb_ms_prefixes[$i];
1530
1531 $table = $prefix . 'options';
1532 // v4.8.2
1533 if ($option == 'Y') {
1534 // EXPIRED ONLY
1535 $sql = $wpdb->prepare("
1536 SELECT %s AS site,
1537 `option_name`
1538 FROM `" . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'options' ) . "`
1539 WHERE (
1540 option_name LIKE '_transient_timeout_%%'
1541 OR option_name LIKE '_site_transient_timeout_%%'
1542 )
1543 AND option_value < %d
1544 ORDER BY `option_name`
1545 ", $prefix, $delay);
1546 //echo $sql . '<br>';
1547 } else {
1548 // ALL
1549 $sql = $wpdb->prepare("
1550 SELECT %s AS site,
1551 `option_name`
1552 FROM `" . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'options' ) . "`
1553 WHERE (
1554 option_name LIKE '_transient_timeout_%%'
1555 OR option_name LIKE '_site_transient_timeout_%%'
1556 )
1557 ORDER BY `option_name`
1558 ", $prefix );
1559 }
1560 //echo $sql . '<br>';
1561 $res = $wpdb->get_results($sql, ARRAY_A);
1562
1563 for($j = 0; $j < count($res); $j++) {
1564 array_push($res_arr, $res[$j]);
1565 } // for($j = 0; $j < count($res); $j++)
1566 } // for($i=0; $i<count($odb_class->odb_ms_prefixes); $i++)
1567 return $res_arr;
1568 } // function odb_get_transients()
1569
1570
1571 /********************************************************************************************
1572 *
1573 * DELETE TRANSIENTS (v4.8.2)
1574 *
1575 ********************************************************************************************/
1576 function odb_delete_transients($results, $scheduler, $action = 'run', $option = 'Y') {
1577 global $wpdb, $odb_class;
1578
1579 // ONE MINUTE DELAY
1580 $delay = time() - MINUTE_IN_SECONDS;
1581
1582 $total_deleted = count($results);
1583
1584 // LOOP THROUGH SITES
1585 for($i = 0; $i < count($odb_class->odb_ms_prefixes); $i++) {
1586 $prefix = $odb_class->odb_ms_prefixes[$i];
1587
1588 for ($j = 0; $j < count($results); $j++) {
1589 if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')) {
1590 ?>
1591 <tr>
1592 <td align="right" valign="top"><?php echo ($j + 1); ?></td>
1593 <td align="left" valign="top"><?php echo $results[$j]['site']?></td>
1594 <td valign="top"><?php echo $results[$j]['option_name']; ?></td>
1595 </tr>
1596 <?php
1597 } // if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail'))
1598
1599 // v4.8.2
1600 if ($scheduler || $action == 'run_summary' || $action == 'run_detail') {
1601 $timeout = $results[$j]['option_name'];
1602 $trans = str_replace('_timeout', '', $timeout);
1603 $sqldel = $wpdb->prepare("
1604 DELETE FROM `" . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'options' ) . "`
1605 WHERE (
1606 option_name = %s
1607 OR option_name = %s
1608 )
1609 ", $timeout, $trans);
1610
1611 //echo $sqldel . '<br>';
1612 $resdel = $wpdb->get_results($sqldel, ARRAY_A);
1613 } // if ($action == 'run_summary' || $action == 'run_detail'))
1614 } // for($i = 0; $i < count($odb_class->odb_ms_prefixes); $i++)
1615 } // for($i = 0; $i < count($odb_class->odb_ms_prefixes); $i++)
1616
1617 return $total_deleted;
1618 } // odb_delete_transients()
1619
1620
1621 /********************************************************************************************
1622 *
1623 * GET PINGBACKS AND TRACKBACKS
1624 *
1625 ********************************************************************************************/
1626 function odb_get_pingbacks() {
1627
1628 global $wpdb, $odb_class;
1629
1630 $res_arr = array();
1631
1632 // LOOP THROUGH SITES
1633 for($i = 0; $i < count($odb_class->odb_ms_prefixes); $i++) {
1634 $prefix = $odb_class->odb_ms_prefixes[$i];;
1635
1636 $sql = $wpdb->prepare("
1637 SELECT %s AS site,
1638 `comment_ID`,
1639 `comment_type`,
1640 `comment_author`,
1641 `comment_date`
1642 FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'comments' ) . "
1643 WHERE (
1644 `comment_type` = 'pingback' OR `comment_type` = 'trackback'
1645 )
1646 ORDER BY `comment_type`, `comment_author`
1647 ", $prefix);
1648
1649 $res = $wpdb->get_results($sql, ARRAY_A);
1650
1651 for($j = 0; $j < count($res); $j++) {
1652 array_push($res_arr, $res[$j]);
1653 } // for($j = 0; $j < count($res); $j++)
1654 } // for($i = 0; $i < count($odb_class->odb_ms_prefixes); $i++)
1655 return $res_arr;
1656 } // odb_get_pingbacks
1657
1658
1659 /********************************************************************************************
1660 *
1661 * DELETE PINGBACKS AND TRACKBACKS
1662 *
1663 ********************************************************************************************/
1664 function odb_delete_pingbacks ($results, $scheduler, $action = 'run') {
1665 global $wpdb, $odb_class;
1666
1667 $total_deleted = count($results);
1668
1669 for ($i = 0; $i < count($results); $i++) {
1670 if (!$scheduler && ($action == 'analyze_detail' | $action == 'run_detail')) {
1671 ?>
1672 <tr>
1673 <td align="right" valign="top"><?php echo ($i + 1); ?></td>
1674 <td align="left" valign="top"><?php echo $results[$i]['site']?></td>
1675 <td valign="top"><?php echo $results[$i]['comment_type']?></td>
1676 <td valign="top"><?php echo $results[$i]['comment_author']?></td>
1677 <td valign="top" nowrap="nowrap"><?php echo $results[$i]['comment_date']; ?></td>
1678 </tr>
1679 <?php
1680 } // if (!$scheduler && ($action == 'analyze_detail' | $action == 'run_detail'))
1681
1682 if ($scheduler || $action == 'run_summary' || $action == 'run_detail') {
1683 for($j = 0; $j < count($results); $j++) {
1684 // DELETE METADATA FOR THIS COMMENT (IF ANY)
1685 $sql = $wpdb->prepare("
1686 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$j]['site'] . 'commentmeta' ) . "
1687 WHERE `comment_id` = %d
1688 ", $results[$j]['comment_ID']);
1689 $wpdb->get_results($sql);
1690
1691 $sql = $wpdb->prepare("
1692 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$j]['site'] . 'comments' ) . "
1693 WHERE (
1694 `comment_type` = %s
1695 OR `comment_type` = %s
1696 )
1697 ", 'pingback', 'trackback');
1698 $wpdb->get_results($sql);
1699 } // for($j = 0; $j < count($results); $j++)
1700 } // if ($action == 'run_summary' || $action == 'run_detail')
1701 } // for ($i = 0; $i < count($results); $i++)
1702
1703 return $total_deleted;
1704 } // odb_delete_pingbacks()
1705
1706
1707 /********************************************************************************************
1708 *
1709 * GET OEMBED CACHE
1710 *
1711 ********************************************************************************************/
1712 function odb_get_oembed() {
1713 global $wpdb, $odb_class;
1714
1715 $res_arr = array();
1716
1717 // LOOP THROUGH SITES
1718 for($i = 0; $i < count($odb_class->odb_ms_prefixes); $i++) {
1719 $prefix = $odb_class->odb_ms_prefixes[$i];
1720
1721 $sql = $wpdb->prepare("
1722 SELECT %s AS site,
1723 `meta_id`,
1724 `meta_key`,
1725 `meta_value`
1726 FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'postmeta' ) . "
1727 WHERE `meta_key` LIKE '_oembed_%%'
1728 ORDER BY `meta_key`
1729 ", $prefix);
1730
1731 $res = $wpdb->get_results($sql, ARRAY_A);
1732
1733 for($j = 0; $j < count($res); $j++) {
1734 array_push($res_arr, $res[$j]);
1735 } // for($j = 0; $j < count($res); $j++)
1736 } // for($i = 0; $i < count($odb_class->odb_ms_prefixes); $i++)
1737 return $res_arr;
1738 } // odb_get_oembed()
1739
1740
1741 /********************************************************************************************
1742 *
1743 * CLEAR OEMBED CACHE
1744 *
1745 ********************************************************************************************/
1746 function odb_delete_oembed($results, $scheduler, $action = 'run') {
1747 global $wpdb, $odb_class;
1748
1749 $total_deleted = count($results);
1750
1751 for($i = 0; $i < $total_deleted; $i++) {
1752 if (!$scheduler && ($action == 'analyze_detail' | $action == 'run_detail')) {
1753 ?>
1754 <tr>
1755 <td align="right" valign="top"><?php echo ($i + 1); ?></td>
1756 <td align="left" valign="top"><?php echo $results[$i]['site']?></td>
1757 <td valign="top"><?php echo $results[$i]['meta_key']?></td>
1758 <td valign="top"><?php echo $results[$i]['meta_value']?></td>
1759 </tr>
1760 <?php
1761 } // if (!$scheduler && ($action == 'analyze_detail' | $action == 'run_detail'))
1762
1763 if ($scheduler || $action == 'run_summary' || $action == 'run_detail') {
1764 // DELETE COMMENTS
1765 $wpdb->get_results("
1766 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$i]['site'] . 'postmeta' ) . "
1767 WHERE `meta_key` LIKE '_oembed_%%'
1768 ");
1769 } // if ($action == 'run_summary' || $action == 'run_detail')
1770 } // for($i = 0; $i < $total_deleted; $i++)
1771
1772 return $total_deleted;
1773 } // odb_delete_oembed()
1774
1775
1776 /********************************************************************************************
1777 *
1778 * GET ORPHAN POSTMETA AND MEDIA RECORDS
1779 *
1780 ********************************************************************************************/
1781 function odb_get_orphans() {
1782 global $wpdb, $odb_class;
1783
1784 $res_arr = array();
1785
1786 // LOOP THROUGH SITES
1787 for($i = 0; $i < count($odb_class->odb_ms_prefixes); $i++) {
1788 $prefix = $odb_class->odb_ms_prefixes[$i];
1789
1790 $sql = $wpdb->prepare("
1791 SELECT %s AS site,
1792 `ID`,
1793 'post' AS type,
1794 `post_title`,
1795 `post_modified`,
1796 '' AS term_taxonomy_id,
1797 '' AS meta_key,
1798 '' AS meta_value
1799 FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'posts' ) . "
1800 WHERE ID NOT IN (SELECT post_id FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'postmeta' ) . ")
1801 AND post_status = 'auto-draft'
1802 ORDER BY `ID`
1803 ", $prefix);
1804
1805 $results = $wpdb->get_results($sql, ARRAY_A);
1806 for ($j = 0; $j < count($results); $j++) {
1807 array_push($res_arr, $results[$j]);
1808 } // for ($j = 0; $j < count($results); $j++)
1809
1810 // FIND POSTMETA ORPHANS
1811 $sql = $wpdb->prepare("
1812 SELECT %s AS site,
1813 `post_id` AS ID,
1814 'post meta' AS type,
1815 '' AS post_title,
1816 '' AS post_modified,
1817 '' AS term_taxonomy_id,
1818 `meta_key`,
1819 `meta_value`
1820 FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'postmeta' ) . "
1821 WHERE post_id NOT IN (SELECT ID FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $prefix . 'posts' ) . ")
1822 ORDER BY `meta_key`
1823 ", $prefix);
1824 //echo $sql . '<br>';
1825
1826 $results = $wpdb->get_results($sql, ARRAY_A);
1827 for ($j = 0; $j < count($results); $j++) {
1828 array_push($res_arr, $results[$j]);
1829 } // for ($j = 0; $j < count($results); $j++)
1830
1831 $results = $wpdb->get_results($sql, ARRAY_A);
1832 for ($j = 0; $j < count($results); $j++) {
1833 array_push($res_arr, $results[$j]);
1834 } // for ($j = 0; $j < count($results); $j++)
1835 } // for($i = 0; $i < count($odb_class->odb_ms_prefixes); $i++)
1836 return $res_arr;
1837 } // odb_get_orphans()
1838
1839
1840 /********************************************************************************************
1841 *
1842 * DELETE ORPHAN POSTMETA AND MEDIA RECORDS
1843 *
1844 ********************************************************************************************/
1845 function odb_delete_orphans($results, $scheduler, $action = 'run_detail') {
1846
1847 global $wpdb, $odb_class;
1848
1849 $total_deleted = count($results);
1850
1851 for($i = 0; $i < count($results); $i++) {
1852 if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')) {
1853 ?>
1854 <tr>
1855 <td valign="top" align="right"><?php echo ($i + 1); ?></td>
1856 <td valign="top" align="left"><?php echo $results[$i]['site']?></td>
1857 <td valign="top"><?php echo $results[$i]['type']?></td>
1858 <td valign="top"><?php echo $results[$i]['ID']?></td>
1859 <td valign="top"><?php echo $results[$i]['post_title']?></td>
1860 <td valign="top" nowrap="nowrap"><?php echo substr($results[$i]['post_modified'], 0, 10); ?></td>
1861 <td valign="top" align="left"><?php echo $results[$i]['term_taxonomy_id']?></td>
1862 <td valign="top" nowrap="nowrap"><?php echo $results[$i]['meta_key']; ?></td>
1863 <td valign="top" nowrap="nowrap"><?php echo $results[$i]['meta_value']; ?></td>
1864 </tr>
1865 <?php
1866 } // if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail'))
1867
1868 if ($scheduler || $action == 'run_summary' || $action == 'run_detail') {
1869 $sql = "";
1870 for($j = 0; $j < count($results); $j++) {
1871 // DELETE METADATA FOR THIS COMMENT (IF ANY)
1872 if ($results[$j]['type'] == 'post meta') {
1873 $sql = $wpdb->prepare("
1874 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$j]['site'] . 'postmeta' ) . "
1875 WHERE `post_id` = %d
1876 ", $results[$j]['ID']);
1877 } else if ($results[$j]['type'] == 'post') {
1878 $sql = $wpdb->prepare("
1879 DELETE FROM " . $odb_class->odb_utilities_obj->odb_sanitize_key( $results[$j]['site'] . 'posts' ) . "
1880 WHERE `ID` = %d
1881 ", $results[$j]['ID']);
1882 } // for($j = 0; $j < count($results); $j++)
1883 $wpdb->get_results($sql);
1884 } // for($j = 0; $j < count($results); $j++)
1885 } // if ($action == 'run_summary' || $action == 'run_detail')
1886 } // for($i = 0; $i < count($results); $i++)
1887 return $total_deleted;
1888 } // odb_delete_orphans()
1889
1890
1891 /********************************************************************************************
1892 *
1893 * OPTIMIZE DATABASE TABLES
1894 *
1895 ********************************************************************************************/
1896 function odb_optimize_tables($scheduler, $action) {
1897 global $odb_class, $wpdb;
1898
1899 $cnt = 0;
1900 for ($i = 0; $i < count($odb_class->odb_tables); $i++) {
1901 if(!isset($odb_class->odb_rvg_excluded_tabs[$odb_class->odb_tables[$i][0]])) {
1902 # TABLE NOT EXCLUDED
1903 $cnt++;
1904
1905 $sql = $wpdb->prepare("
1906 SELECT ENGINE, (data_length + index_length) AS size, TABLE_ROWS
1907 FROM information_schema.TABLES
1908 WHERE table_schema = %s
1909 AND table_name = %s
1910 ", DB_NAME, $odb_class->odb_tables[$i][0]);
1911 //echo $sql.'<br>';
1912 $table_info = $wpdb->get_results($sql);
1913 //print_r($table_info);
1914
1915 if($odb_class->odb_rvg_options["optimize_innodb"] == 'N' && strtolower($table_info[0]->ENGINE) == 'innodb') {
1916 // SKIP InnoDB tables
1917 $msg = __('InnoDB table: skipped...', 'rvg-optimize-database');
1918 } else {
1919 // v4.6.3
1920 if (@strtolower($table_info[0]->ENGINE) == 'myisam') {
1921 $result = $this->odb_optimize_myisam($odb_class->odb_tables[$i][0]);
1922 $msg = $result[0]->Msg_text;
1923 if ($msg == 'OK') {
1924 $msg = __('<span class="odb-optimized">TABLE OPTIMIZED</span>', 'rvg-optimize-database');
1925 } else if ($msg == 'Table is already up to date') {
1926 $msg = __('Table is already up to date', 'rvg-optimize-database');
1927 }
1928 } else {
1929 $result = $this->odb_optimize_innodb($odb_class->odb_tables[$i][0]);
1930 $msg = $result[0]->Msg_text;
1931 if ($msg == 'Table is already up to date') {
1932 $msg = __('Table is already up to date', 'rvg-optimize-database');
1933 } else {
1934 $msg = __('<span class="odb-optimized">TABLE OPTIMIZED</span>', 'rvg-optimize-database');
1935 }
1936 } // if (strtolower($table_info[0]->ENGINE) == 'myisam')
1937 } // if($odb_class->odb_rvg_options["optimize_innodb"] == 'N' && strtolower($table_info[0]->ENGINE) == 'innodb')
1938
1939 if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail')) {
1940 ?>
1941 <tr>
1942 <td align="right" valign="top"><?php echo $cnt?>.</td>
1943 <td valign="top" class="odb-bold"><?php echo $odb_class->odb_tables[$i][0] ?></td>
1944 <td valign="top"><?php echo $msg ?></td>
1945 <td valign="top"><?php echo $table_info[0]->ENGINE ?></td>
1946 <td align="right" valign="top"><?php echo $table_info[0]->TABLE_ROWS ?></td>
1947 <td align="right" valign="top"><?php echo $odb_class->odb_utilities_obj->odb_format_size($table_info[0]->size) ?></td>
1948 </tr>
1949 <?php
1950 } // if (!$scheduler && ($action == 'analyze_detail' || $action == 'run_detail'))
1951 } // if(!$excluded)
1952 } // for ($i = 0; $i < count($odb_class->odb_tables); $i++)
1953 return $cnt;
1954 } // odb_optimize_tables()
1955
1956
1957 /********************************************************************************************
1958 *
1959 * OPTIMIZE A MyISAM TABLE
1960 *
1961 ********************************************************************************************/
1962 function odb_optimize_myisam($table_name) {
1963 global $wpdb;
1964 $query = "OPTIMIZE TABLE " . $table_name;
1965 return $wpdb->get_results($query);
1966 } // odb_optimize_myisam()
1967
1968
1969 /********************************************************************************************
1970 *
1971 * OPTIMIZE AN InnoDB TABLE
1972 *
1973 ********************************************************************************************/
1974 function odb_optimize_innodb($table_name) {
1975 global $wpdb;
1976
1977 $query = "OPTIMIZE TABLE " . $table_name;
1978 return $wpdb->get_results($query);
1979
1980 /* // https://www.percona.com/blog/2010/12/09/mysql-optimize-tables-innodb-stop/
1981 $query = "SHOW KEYS FROM " . $table_name . " WHERE Key_name <> 'PRIMARY'";
1982 $result = $wpdb->get_results($query);
1983 if (count($result) > 0) {
1984 for ($i = 0; $i < count($result); $i++) {
1985 $key_name = $result[$i]->Key_name;
1986
1987 $query = "ALTER TABLE " . $table_name . " DROP KEY " . $key_name;
1988 $result = $wpdb->get_results($query);
1989
1990 $query = "ALTER TABLE " . $table_name . " add key(" . $key_name . ")";
1991 $result = $wpdb->get_results($query);
1992 } // for ($i = 0; $i < count($result); $i++)
1993 return __('<span class="odb-optimized">TABLE OPTIMIZED</span>', 'rvg-optimize-database');
1994 } else {
1995 return __('Table is already up to date', 'rvg-optimize-database');
1996 } // if (count($result) > 0*/
1997 } // odb_optimize_innodb()
1998
1999 } // ODB_Cleaner
2000 ?>