C:\xampp\htdocs\landing\wp-content\plugins\wp-optimize\includes\class-wp-optimizer.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
<?php

if (!defined('WPO_VERSION')) die('No direct access allowed');

/**
 * This class invokes optimiazations. The optimizations themselves live in the 'optimizations' sub-directory of the plugin.  The proper way to obtain access to the instance is via WP_Optimize()->get_optimizer()
 */
class WP_Optimizer {
    
    public function 
get_retain_info() {
    
        
$options WP_Optimize()->get_options();
    
        
$retain_enabled $options->get_option('retention-enabled''false');
        
$retain_period = (($retain_enabled) ? $options->get_option('retention-period''2') : null);

        return array(
$retain_enabled$retain_period);
    }
    
    public function 
get_optimizations_list() {
    
        
$optimizations = array();
        
        
$optimizations_dir WPO_PLUGIN_MAIN_PATH.'optimizations';
        
        if (
$dh opendir($optimizations_dir)) {
            while ((
$file readdir($dh)) !== false) {
                if (
'.' == $file || '..' == $file || '.php' != substr($file, -44) || !is_file($optimizations_dir.'/'.$file) || 'inactive-' == substr($file09)) continue;
                
$optimizations[] = substr($file0, (strlen($file) - 4));
            }
            
closedir($dh);
        }
        
        return 
apply_filters('wp_optimize_get_optimizations_list'$optimizations);

    }
    
    
/**
     * Currently, there is only one sort rule (so, the parameter's value is ignored)
     *
     * @param  array  $optimizations An array of optimizations (i.e. WP_Optimization instances).
     * @param  string $sort_on       Specify sort.
     * @param  string $sort_rule     Sort Rule.
     * @return array
     */
    
public function sort_optimizations($optimizations$sort_on 'ui_sort_order'$sort_rule 'traditional') {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
        
if ('run_sort_order' == $sort_on) {
            
uasort($optimizations, array($this'sort_optimizations_run_traditional'));
        } else {
            
uasort($optimizations, array($this'sort_optimizations_ui_traditional'));
        }
        return 
$optimizations;
    }
    
    public function 
sort_optimizations_ui_traditional($a$b) {
        return 
$this->sort_optimizations_traditional($a$b'ui_sort_order');
    }
    
    public function 
sort_optimizations_run_traditional($a$b) {
        return 
$this->sort_optimizations_traditional($a$b'run_sort_order');
    }
    
    public function 
sort_optimizations_traditional($a$b$sort_on 'ui_sort_order') {
    
        if (!
is_a($a'WP_Optimization')) return (!is_a($b'WP_Optimization')) ? 1;
        if (!
is_a($b'WP_Optimization')) return -1;
    
        
$sort_order_a = empty($a->$sort_on) ? $a->$sort_on;
        
$sort_order_b = empty($b->$sort_on) ? $b->$sort_on;
    
        if (
$sort_order_a == $sort_order_b) return 0;
        
        return (
$sort_order_a $sort_order_b) ? (-1) : 1;
    
    }
    
    
/**
     * This method returns an array of available optimizations.
     * Each array key is an optimization ID, and the value is an object,
     * as returned by get_optimization()
     *
     * @return [array] array of optimizations or WP_Error objects
     */
    
public function get_optimizations() {
    
        
$optimizations $this->get_optimizations_list();
    
        
$optimization_objects = array();
        
        foreach (
$optimizations as $optimization) {
            
$optimization_object $this->get_optimization($optimization);
            if (
is_wp_error($optimization_object)) {
                
WP_Optimize()->log('Failed to load optimization ' $optimization ' - ' $optimization_object->get_error_message());
            } else {
                
$optimization_objects[$optimization] = $optimization_object;
            }
        }
    
        return 
apply_filters('wp_optimize_get_optimizations'$optimization_objects);
    
    }
    
    
/**
     * This method returns an object for a specific optimization.
     *
     * @param  string $which_optimization An optimization ID.
     * @param  array  $data               An array of anny options $data.
     * @return array                      WP_Error Will return the optimization, or a WP_Error object if it was not found.
     */
    
public function get_optimization($which_optimization$data = array()) {

        
$optimization_class apply_filters('wp_optimize_optimization_class''WP_Optimization_'.$which_optimization);
        
        if (!
class_exists('WP_Optimization')) include_once(WPO_PLUGIN_MAIN_PATH.'includes/class-wp-optimization.php');
    
        if (!
class_exists($optimization_class)) {
            
$optimization_file WPO_PLUGIN_MAIN_PATH.'optimizations/'.$which_optimization.'.php';
            
$class_file apply_filters('wp_optimize_optimization_class_file'$optimization_file);
            if (!
preg_match('/^[a-z]+$/'$which_optimization) || !file_exists($class_file)) {
                return new 
WP_Error('no_such_optimization'__('No such optimization''wp-optimize'), $which_optimization);
            }
            
            include_once(
$class_file);
            
            if (!
class_exists($optimization_class)) {
                return new 
WP_Error('no_such_optimization'__('No such optimization''wp-optimize'), $which_optimization);
            }
        }

        
// set sites option for Multisite cron job.
        
if (defined('DOING_CRON') && DOING_CRON && is_multisite()) {
            
$options WP_Optimize()->get_options();
            
$data['optimization_sites'] = $options->get_option('wpo-sites-cron', array('all'));
        }
        
        
$optimization = new $optimization_class($data);
        
        return 
$optimization;
    
    }

    
/**
     * The method to call to perform an optimization.
     *
     * @param  string|object $which_optimization An optimization ID, or a WP_Optimization object.
     * @return array                             Array of results from the optimization.
     */
    
public function do_optimization($which_optimization) {
        
        
$optimization = (is_object($which_optimization) && is_a($which_optimization'WP_Optimization')) ? $which_optimization $this->get_optimization($which_optimization);

        if (
is_wp_error($optimization)) {
            
WP_Optimize()->log('Error occurred. Unknown optimization.');
            return 
$optimization;
        }

        
WP_Optimize()->change_time_limit();

        
$optimization->init();
    
        if (
apply_filters('wp_optimize_do_optimization'true$which_optimization$optimization)) {

            
$optimization->before_optimize();

            if (
$optimization->run_multisite) {
                foreach (
$optimization->blogs_ids as $blog_id) {
                    
$optimization->switch_to_blog($blog_id);
                    
$optimization->optimize();
                    
$optimization->restore_current_blog();
                }
            } else {
                
$optimization->optimize();
            }

            
$optimization->after_optimize();

        }
        
        
do_action('wp_optimize_after_optimization'$which_optimization$optimization);

        
$results $optimization->get_results();
            
        return 
$results;
    }
    
    
/**
     * The method to call to get information about an optimization.
     * As with do_optimization, it is somewhat modelled after the template interface
     *
     * @param  string|object $which_optimization An optimization ID, or a WP_Optimization object.
     * @return array                             returns the optimization information
     */
    
public function get_optimization_info($which_optimization) {
    
        
$optimization = (is_object($which_optimization) && is_a($which_optimization'WP_Optimization')) ? $which_optimization $this->get_optimization($which_optimization);
        
        if (
is_wp_error($optimization)) return $optimization;

        
WP_Optimize()->change_time_limit();

        
$optimization->before_get_info();

        if (
$optimization->run_multisite) {
            foreach (
$optimization->blogs_ids as $blog_id) {
                
$optimization->switch_to_blog($blog_id);
                
$optimization->get_info();
                
$optimization->restore_current_blog();
            }
        } else {
            
$optimization->get_info();
        }

        
$optimization->after_get_info();

        return 
$optimization->get_results();
    }
    
    
/**
     * THis runs the list of optimizations.
     *
     * @param  array  $optimization_options Whether to do an optimization depends on what keys are set (legacy - can be changed hopefully).
     * @param  string $which_option         Specify which option.
     * @return array                        Returns an array of result objects.
     */
    
public function do_optimizations($optimization_options$which_option 'dom') {
    
        
$results = array();
        
        if (empty(
$optimization_options)) return $results;
    
        
$optimizations $this->sort_optimizations($this->get_optimizations(), 'run_sort_order');

        foreach (
$optimizations as $optimization_id => $optimization) {
            
$option_id call_user_func(array($optimization'get_'.$which_option.'_id'));
            
            if (isset(
$optimization_options[$option_id])) {
                
// if options saved as a string then compare with string (for support different versions)
                
if (is_string($optimization_options[$option_id]) && 'false' === $optimization_options[$option_id]) continue;

                if (
'auto' == $which_option && empty($optimization->available_for_auto)) continue;

                
WP_Optimize()->change_time_limit();

                
$results[$optimization_id] = $this->do_optimization($optimization);
            }
        }

        
// Run action after all optimizations completed.
        
do_action('wp_optimize_after_optimizations');

        return 
$results;
        
    }
    
    public function 
get_table_prefix($allow_override false) {
        
$wpdb $GLOBALS['wpdb'];
        if (
is_multisite() && !defined('MULTISITE')) {
            
// In this case (which should only be possible on installs upgraded from pre WP 3.0 WPMU), $wpdb->get_blog_prefix() cannot be made to return the right thing. $wpdb->base_prefix is not explicitly marked as public, so we prefer to use get_blog_prefix if we can, for future compatibility.
            
$prefix $wpdb->base_prefix;
        } else {
            
$prefix $wpdb->get_blog_prefix(0);
        }
        return (
$allow_override) ? apply_filters('wp_optimize_get_table_prefix'$prefix) : $prefix;
    }

    
/**
     * Returns information about database tables.
     *
     * @param bool $update refresh or no cached data
     *
     * @return mixed
     */
    
public function get_tables($update false) {
        static 
$tables_info null;

        if (
false === $update && null !== $tables_info) return $tables_info;

        
$table_status WP_Optimize()->get_db_info()->get_show_table_status($update);

        
// Filter on the site's DB prefix (was not done in releases up to 1.9.1).
        
$table_prefix $this->get_table_prefix();
        
        if (
is_array($table_status)) {

            
$corrupted_tables_count 0;

            foreach (
$table_status as $index => $table) {
                
$table_name $table->Name;
                
                
$include_table = (=== stripos($table_name$table_prefix));
                
                
$include_table apply_filters('wp_optimize_get_tables_include_table'$include_table$table_name$table_prefix);

                if (!
$include_table && '' !== $table_prefix) {
                    unset(
$table_status[$index]);
                    continue;
                }

                
$table_status[$index]->Engine WP_Optimize()->get_db_info()->get_table_type($table_name);

                
$table_status[$index]->is_optimizable WP_Optimize()->get_db_info()->is_table_optimizable($table_name);
                
$table_status[$index]->is_type_supported WP_Optimize()->get_db_info()->is_table_type_optimize_supported($table_name);
                
// add information about corrupted tables.
                
$is_needing_repair WP_Optimize()->get_db_info()->is_table_needing_repair($table_name);
                
$table_status[$index]->is_needing_repair $is_needing_repair;
                if (
$is_needing_repair$corrupted_tables_count++;

                
$table_status[$index] = $this->join_plugin_information($table_name$table_status[$index]);
            }

            
WP_Optimize()->get_options()->update_option('corrupted-tables-count'$corrupted_tables_count);
        }

        
$tables_info apply_filters('wp_optimize_get_tables'$table_status);
        return 
$tables_info;
    }

    
/**
     * Returns information about single table by table name.
     *
     * @param String $table_name table name
     * @return Object|Boolean table information object.
     */
    
public function get_table($table_name) {
    
        
$db_info WP_Optimize()->get_db_info();
    
        
$table $db_info->get_table_status($table_name);
        
        if (
false === $table) return false;

        
$table->is_optimizable $db_info->is_table_optimizable($table_name);
        
$table->is_type_supported $db_info->is_table_type_optimize_supported($table_name);
        
$table->is_needing_repair $db_info->is_table_needing_repair($table_name);

        
// add information about plugins.
        
$table $this->join_plugin_information($table_name$table);

        
$table apply_filters('wp_optimize_get_table'$table);
        return 
$table;
    }

    
/**
     * Add information about relationship database tables with plugins.
     *
     * @param {string} $table_name
     * @param {object} $table_obj
     *
     * @return {object}
     */
    
public function join_plugin_information($table_name$table_obj) {
        
// set can be removed flag.
        
$can_be_removed false;
        
// set WP core table flag.
        
$wp_core_table false;
        
// set WP actionscheduler table flag.
        
$wp_actionscheduler_table = (false !== stripos($table_name'actionscheduler_'));
        
// add information about using table by any of installed plugins.
        
$table_obj->is_using WP_Optimize()->get_db_info()->is_table_using_by_plugin($table_name);
        
// if table belongs to any plugin then add plugins status.
        
$plugins WP_Optimize()->get_db_info()->get_table_plugin($table_name);

        if (
false !== $plugins) {
            
// if belongs to any of plugin then we can remove table if plugin not active.
            
$can_be_removed true;

            
$plugin_status = array();
            foreach (
$plugins as $plugin) {
                
$status WP_Optimize()->get_db_info()->get_plugin_status($plugin);

                if (
__('WordPress core''wp-optimize') == $plugin$wp_core_table true;
                
// if plugin is active then we can't remove.
                
if ($wp_core_table || $status['active'] || $wp_actionscheduler_table$can_be_removed false;

                if (
$status['installed'] || $status['active'] || !$table_obj->is_using) {
                    
$plugin_status[] = array(
                        
'plugin' => $plugin,
                        
'status' => $status,
                    );
                }
            }

            
$table_obj->plugin_status $plugin_status;
        }

        
$table_obj->wp_core_table $wp_core_table;
        
$table_obj->can_be_removed $can_be_removed;

        return 
$table_obj;
    }

    
/**
     * This function grabs a list of tables
     * and information regarding each table and returns
     * the results to optimizations-table.php and optimizationstable.php
     *
     * @return Array - an array of data such as table list, innodb info and data free
     */
    
public function get_table_information() {
        
// Get table information.
        
$tablesstatus $this->get_tables();

        
// Set defaults.
        
$table_information = array();
        
$table_information['total_gain'] = 0;
        
$table_information['inno_db_tables'] = 0;
        
$table_information['non_inno_db_tables'] = 0;
        
$table_information['table_list'] = '';
        
$table_information['is_optimizable'] = true;

        
// Make a list of tables to optimize.
        
foreach ($tablesstatus as $each_table) {
            
$table_information['table_list'] .= $each_table->Name.'|';

            
// check if table type supported.
            
if (!$each_table->is_type_supported) continue;

            
// check if table is optimizable.
            
if (!$each_table->is_optimizable) {
                
$table_information['is_optimizable'] = false;
            }

            
// calculate total gain value.
            
$table_information['total_gain'] += $each_table->Data_free;

            
// count InnoDB tables.
            
if ('InnoDB' == $each_table->Engine) {
                
$table_information['inno_db_tables']++;
            } else {
                
$table_information['non_inno_db_tables']++;
            }
        }
        return 
$table_information;
    }
    
    
/**
     * What sort of linkback to enable or disable: valid values are 'trackbacks' or 'comments', and whether to enable or disable.
     *
     * @param string  $type   Specify the type of linkbacks.
     * @param boolean $enable If it is enabled or disabled.
     */
    
public function enable_linkbacks($type$enable true) {
    
        
$wpdb $GLOBALS['wpdb'];
        
$wpo_options WP_Optimize()->get_options();
        
        
$new_status $enable 'open' 'closed';
        
        switch (
$type) {
            case 
"trackbacks":
            
$thissql "UPDATE `".$wpdb->posts."` SET ping_status='".$new_status."' WHERE post_status = 'publish' AND post_type = 'post';";
            
$wpdb->query($thissql);
                break;

            case 
"comments":
            
$thissql "UPDATE `".$wpdb->posts."` SET comment_status='".$new_status."' WHERE post_status = 'publish' AND post_type = 'post';";
            
$wpdb->query($thissql);
                break;

            default:
                break;
        }
        
$wpo_options->update_option($type.'_action', array('action' => $enable'timestamp' => time()));

    }
    
    
/**
     * This function will return total database size and a possible gain of db in KB.
     *
     * @param boolean $update - Wether to update the values or not
     * @return string total db size gained.
     */
    
public function get_current_db_size($update false) {
        
        if (!
$update && $db_size get_transient('wpo_get_current_db_size')) {
            return 
$db_size;
        }

        
$wp_optimize WP_Optimize();

        
$total_gain 0;
        
$total_size 0;
        
$row_usage 0;// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Used in the foreach below
        
$data_usage 0;
        
$index_usage 0;
        
$overhead_usage 0// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Used in the foreach below
        
        
$tablesstatus $this->get_tables();

        foreach (
$tablesstatus as $tablestatus) {
            
$row_usage += $tablestatus->Rows;
            
$data_usage += $tablestatus->Data_length;
            
$index_usage += $tablestatus->Index_length;

            if (
'InnoDB' != $tablestatus->Engine) {
                
$overhead_usage += $tablestatus->Data_free;
                
$total_gain += $tablestatus->Data_free;
            }
        }

        
$total_size = ($data_usage $index_usage);
        
$db_size = array($wp_optimize->format_size($total_size), $wp_optimize->format_size($total_gain));
        
set_transient('wpo_get_current_db_size'$db_size3600);
        return 
$db_size;
    }

    
/**
     * This function will return total saved data in KB.
     *
     * @param  string $current How big the data is.
     * @return string          Returns new total value.
     */
    
public function update_total_cleaned($current) {

        
$options WP_Optimize()->get_options();
    
        
$previously_saved floatval($options->get_option('total-cleaned''0'));
        
$converted_current floatval($current);

        
$total_now strval($previously_saved $converted_current);

        
$options->update_option('total-cleaned'$total_now);

        return 
$total_now;
    }
    
    
    public function 
trackback_comment_actions($options) {
    
        
$output = array();
        
$messages = array();
    
        if (isset(
$options['comments'])) {
            if (!
$options['comments']) {
                
$this->enable_linkbacks('comments'false);
                
$output[] = __('Comments have now been disabled on all current and previously published posts.''wp-optimize');
                
$messages[] =  sprintf(__('All comments on existing posts were disabled at %s.''wp-optimize'), WP_Optimize()->format_date_time(time()));
            } else {
                
$this->enable_linkbacks('comments');
                
$output[] = __('Comments have now been enabled on all current and previously published posts.''wp-optimize');
                
$messages[] =  sprintf(__('All comments on existing posts were enabled at %s.''wp-optimize'), WP_Optimize()->format_date_time(time()));
            }
        }
        
        if (isset(
$options['trackbacks'])) {
            if (!
$options['trackbacks']) {
                
$this->enable_linkbacks('trackbacks'false);
                
$output[] = __('Trackbacks have now been disabled on all current and previously published posts.''wp-optimize');
                
$messages[] =  sprintf(__('All trackbacks on existing posts were disabled at %s.''wp-optimize'), WP_Optimize()->format_date_time(time()));
            } else {
                
$this->enable_linkbacks('trackbacks');
                
$output[] = __('Trackbacks have now been enabled on all current and previously published posts.''wp-optimize');
                
$messages[] =  sprintf(__('All trackbacks on existing posts were enabled at %s.''wp-optimize'), WP_Optimize()->format_date_time(time()));
            }
        }
        
        return array(
'output' => $output,'messages' => $messages);
    }

    
/**
     * Wether InnoDB tables require confirmation to be optimized
     *
     * @return boolean
     */
    
public function show_innodb_force_optimize() {
        
$tablesstatus $this->get_table_information();
        return 
false === $tablesstatus['is_optimizable'] && $tablesstatus['inno_db_tables'] > 0;
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586