PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 2.1.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v2.1.0
4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 3.8.0 All 110 releases
wp-optimize / wp-optimize.php

wp-optimize.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 2.1.0, at wp-optimize.php

601 lines 20.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: WP-Optimize
4 Plugin URI: http://updraftplus.com
5 Description: WP-Optimize is WordPress's #1 most installed optimization plugin. With it, you can clean up your database easily and safely, without manual queries.
6 Version: 2.1.0
7 Author: David Anderson, Ruhani Rabin, Team Updraft
8 Author URI: https://updraftplus.com
9 Text Domain: wp-optimize
10 Domain Path: /languages
11 License: GPLv2 or later
12 */
13
14 if (!defined('ABSPATH')) die('No direct access allowed');
15
16 define('WPO_VERSION', '2.1.0');
17 define('WPO_PLUGIN_URL', plugin_dir_url( __FILE__ ));
18 define('WPO_PLUGIN_MAIN_PATH', plugin_dir_path( __FILE__ ));
19
20 class WP_Optimize {
21
22 private $template_directories;
23
24 protected static $_instance = null;
25 protected static $_optimizer_instance = null;
26 protected static $_options_instance = null;
27 protected static $_notices_instance = null;
28
29 public function __construct() {
30
31 register_activation_hook(__FILE__, 'wpo_activation_actions');
32 register_deactivation_hook(__FILE__, 'wpo_deactivation_actions');
33
34 add_action('plugins_loaded', array($this, 'plugins_loaded'));
35
36 $plugin = plugin_basename(__FILE__);
37 add_filter("plugin_action_links_$plugin", array($this, 'plugin_settings_link'));
38 add_action('admin_init', array($this, 'admin_init'));
39 add_action('admin_menu', array($this, 'admin_menu'));
40 add_action('wpo_cron_event2', array($this, 'cron_action'));
41 add_filter('cron_schedules', array($this, 'cron_schedules'));
42
43 add_action('wp_ajax_wp_optimize_ajax', array($this, 'wp_optimize_ajax_handler'));
44
45 include_once(WPO_PLUGIN_MAIN_PATH.'/includes/updraftcentral.php');
46
47 }
48
49 public static function instance() {
50 if (empty(self::$_instance)) {
51 self::$_instance = new self();
52 }
53 return self::$_instance;
54 }
55
56 public static function get_optimizer() {
57 if (empty(self::$_optimizer_instance)) {
58 if (!class_exists('WP_Optimizer')) require_once(WPO_PLUGIN_MAIN_PATH.'/includes/class-wp-optimizer.php');
59 self::$_optimizer_instance = new WP_Optimizer();
60 }
61 return self::$_optimizer_instance;
62 }
63
64 public static function get_options() {
65 if (empty(self::$_options_instance)) {
66 if (!class_exists('WP_Optimize_Options')) require_once(WPO_PLUGIN_MAIN_PATH.'/includes/class-wp-optimize-options.php');
67 self::$_options_instance = new WP_Optimize_Options();
68 }
69 return self::$_options_instance;
70 }
71
72 public static function get_notices() {
73 if (empty(self::$_notices_instance)) {
74 if (!class_exists('WP_Optimize_Notices')) require_once(WPO_PLUGIN_MAIN_PATH.'/includes/wp-optimize-notices.php');
75 self::$_notices_instance = new WP_Optimize_Notices();
76 }
77 return self::$_notices_instance;
78 }
79
80 public function admin_init() {
81
82 global $pagenow;
83
84 $this->register_template_directories();
85
86 if (($pagenow == 'index.php' && current_user_can('update_plugins')) || ($pagenow == 'index.php' && defined('WP_OPTIMIZE_FORCE_DASHNOTICE') && WP_OPTIMIZE_FORCE_DASHNOTICE)) {
87
88 $options = $this->get_options();
89
90 $dismissed_until = $options->get_option('dismiss_dash_notice_until', 0);
91
92 $installed = @filemtime(WPO_PLUGIN_MAIN_PATH . '/index.html');
93 $installed_for = time() - $installed;
94
95 if (($installed && time() > $dismissed_until && $installed_for > 28*86400 && !defined('WP_OPTIMIZE_NOADS_B')) || (defined('WP_OPTIMIZE_FORCE_DASHNOTICE') && WP_OPTIMIZE_FORCE_DASHNOTICE)) {
96
97 add_action('all_admin_notices', array($this, 'show_admin_notice_upgradead') );
98 }
99 }
100 }
101
102 public function show_admin_notice_upgradead() {
103 $this->include_template('notices/thanks-for-using-main-dash.php');
104 }
105
106 public function plugins_loaded() {
107 load_plugin_textdomain('wp-optimize', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
108 }
109
110 public function capability_required() {
111 return apply_filters('wp_optimize_capability_required', 'manage_options');
112 }
113
114 public function wp_optimize_ajax_handler() {
115
116 $nonce = empty($_POST['nonce']) ? '' : $_POST['nonce'];
117
118 if (!wp_verify_nonce($nonce, 'wp-optimize-ajax-nonce') || empty($_POST['subaction'])) die('Security check');
119
120 $subaction = $_POST['subaction'];
121 $data = isset($_POST['data']) ? $_POST['data'] : null;
122
123 if (!current_user_can($this->capability_required())) die('Security check');
124
125 $optimizer = $this->get_optimizer();
126 $options = $this->get_options();
127
128 $results = array();
129
130 // Some commands that are available via AJAX only
131 if ('dismiss_dash_notice_until' == $subaction) {
132
133 $options->update_option('dismiss_dash_notice_until', time() + 366*86400);
134
135 } elseif ('dismiss_page_notice_until' == $subaction) {
136
137 $options->update_option('dismiss_page_notice_until', time() + 84*86400);
138
139 } else {
140
141 // Other commands, available for any remote method
142
143 if (!class_exists('WP_Optimize_Commands')) require_once(WPO_PLUGIN_MAIN_PATH.'includes/class-commands.php');
144
145 $commands = new WP_Optimize_Commands();
146
147 if (!method_exists($commands, $subaction)) {
148
149 error_log("WP-Optimize: ajax_handler: no such command ($command)");
150 die('No such command');
151
152 } else {
153
154 $results = call_user_func(array($commands, $subaction), $data);
155
156 if (is_wp_error($results)) {
157 $results = array(
158 'result' => false,
159 'error_code' => $results->get_error_code(),
160 'error_message' => $results->get_error_message(),
161 'error_data' => $results->get_error_data(),
162 );
163 }
164
165 }
166
167 }
168
169 echo json_encode($results);
170
171 die;
172 }
173
174 public function get_tabs() {
175 return apply_filters('wp_optimize_admin_page_tabs', array(
176 'optimize' => 'WP-Optimize',
177 'tables' => __('Table information', 'wp-optimize'),
178 'settings' => __('Settings', 'wp-optimize'),
179 'may_also' => __('Plugin family', 'wp-optimize'),
180 ));
181 }
182
183 public function wp_optimize_menu() {
184
185 $capability_required = $this->capability_required();
186
187 if (!current_user_can($capability_required)) { echo "Permission denied."; return; }
188
189 $enqueue_version = (defined('WP_DEBUG') && WP_DEBUG) ? WPO_VERSION.'.'.time() : WPO_VERSION;
190 $min_or_not = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
191
192 wp_register_script('updraft-queue-js', WPO_PLUGIN_URL.'js/queue'.$min_or_not.'.js', array(), $enqueue_version);
193
194 wp_enqueue_script('wp-optimize-admin-js', WPO_PLUGIN_URL.'js/wpadmin'.$min_or_not.'.js', array('jquery', 'updraft-queue-js'), $enqueue_version);
195
196 wp_enqueue_style('wp-optimize-admin-css', WPO_PLUGIN_URL.'css/admin'.$min_or_not.'.css', array(), $enqueue_version);
197
198 $options = $this->get_options();
199
200 $tabs = $this->get_tabs();
201
202 $default_tab = apply_filters('wp_optimize_admin_default_tab', 'optimize');
203
204 $active_tab = isset($_GET['tab']) ? substr($_GET['tab'], 12) : $default_tab;
205
206 if (!in_array($active_tab, array_keys($tabs))) $active_tab = $default_tab;
207
208 $nonce_passed = (!empty($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'wpo_optimization')) ? true : false;
209
210 if ('optimize' == $active_tab && $nonce_passed && isset($_POST['wp-optimize'])) $options->save_sent_manual_run_optimization_options($_POST, true);
211
212 echo '<div id="wp-optimize-wrap" class="wrap">';
213
214 $this->include_template('admin-page-header.php', false, array('active_tab' => $active_tab, 'tabs' => $tabs));
215
216 $optimize_db = ($nonce_passed && isset($_POST["optimize-db"])) ? true : false;
217
218 $optimizer = $this->get_optimizer();
219
220 foreach ($tabs as $tab_id => $tab_description) {
221
222 echo '<div class="wp-optimize-nav-tab-contents" id="wp-optimize-nav-tab-contents-'.$tab_id.'" '.(($tab_id == $active_tab) ? '' : 'style="display:none;"').'>';
223
224 do_action('wp_optimize_admin_tab_render_begin', $active_tab);
225
226 switch ($tab_id) {
227
228 case 'optimize':
229
230 $optimization_results = $nonce_passed ? $optimizer->do_optimizations($_POST) : false;
231
232 if (!empty($optimization_results)) {
233 echo '<div id="message" class="updated"><strong>';
234 foreach ($optimization_results as $optimization_result) {
235 if (!empty($optimization_result->output)) {
236 foreach ($optimization_result->output as $line) { echo $line."<br>"; }
237 }
238 }
239 echo '</strong></div>';
240 }
241
242 $this->include_template('optimize-table.php', false, array('optimize_db' => $optimize_db));
243
244 break;
245
246 case 'tables':
247
248 $this->include_template('tables.php', false, array('optimize_db' => $optimize_db));
249
250 break;
251
252 case 'settings':
253
254 if ('POST' == $_SERVER['REQUEST_METHOD']) {
255
256 // Nonce check
257 check_admin_referer('wpo_settings');
258
259 $output = $options->save_settings($_POST);
260
261 foreach ($output['messages'] as $item) {
262 echo '<div class="updated fade"><strong>'.$item.'</strong></div>';
263 }
264
265 foreach ($output['errors'] as $item) {
266 echo '<div class="error fade"><strong>'.$item.'</strong></div>';
267 }
268
269 }
270
271 $this->include_template('admin-settings.php');
272 break;
273
274 case 'may_also':
275 $this->include_template('may-also-like.php');
276 break;
277 }
278
279 echo '</div>';
280 }
281
282 do_action('wp_optimize_admin_tab_render_end', $active_tab);
283
284 echo '</div>';
285
286 }
287
288 public function wpo_admin_bar() {
289 global $wp_admin_bar;
290
291 if (defined('WPOPTIMIZE_ADMINBAR_DISABLE') && WPOPTIMIZE_ADMINBAR_DISABLE) return;
292
293 // Add a link called at the top admin bar
294 $args = array(
295 'id' => 'wp-optimize-node',
296 'title' => apply_filters('wpoptimize_admin_node_title', 'WP-Optimize')
297 );
298 $wp_admin_bar->add_node($args);
299
300 $tabs = $this->get_tabs();
301
302 foreach ($tabs as $tab_id => $tab_title) {
303 $args = array(
304 'id' => 'wpoptimize_admin_node_'.$tab_id,
305 'title' => ('optimize' == $tab_id) ? __('Optimize', 'wp-optimize') : $tab_title,
306 'parent' => 'wp-optimize-node',
307 'href' => menu_page_url('WP-Optimize', false). '&tab=wp_optimize_'.$tab_id,
308 );
309 $wp_admin_bar->add_node($args);
310 }
311
312 }
313
314 // Add settings link on plugin page
315 public function plugin_settings_link($links) {
316
317 $admin_page_url = $this->get_options()->admin_page_url();
318
319 $settings_link = '<a href="' . esc_url( $admin_page_url ) . '">' . __( 'Settings', 'wp-optimize' ) . '</a>';
320 array_unshift($links, $settings_link);
321
322 $optimize_link = '<a href="' . esc_url( $admin_page_url ) . '">' . __( 'Optimizer', 'wp-optimize' ) . '</a>';
323 array_unshift($links, $optimize_link);
324 return $links;
325 }
326
327 // TODO: Need to find out why the schedule time is not refreshing
328 public function cron_activate() {
329 $gmtoffset = (int) (3600 * ((double) get_option('gmt_offset')));
330
331 $options = $this->get_options();
332
333 if ($options->get_option('schedule') === false ) {
334 $options->set_default_options();
335 } else {
336 if ($options->get_option('schedule') == 'true') {
337 if (!wp_next_scheduled('wpo_cron_event2')) {
338
339 $schedule_type = $options->get_option('schedule-type', 'wpo_weekly');
340
341 $this_time = 86400*7;
342
343 switch ($schedule_type) {
344 case "wpo_daily":
345 $this_time = 86400;
346 break;
347
348 case "wpo_weekly":
349 $this_time = 86400*7;
350 break;
351
352 case "wpo_otherweekly":
353 $this_time = 86400*14;
354 break;
355
356 case "wpo_monthly":
357 $this_time = 86400*30;
358 break;
359 }
360
361 add_action('wpo_cron_event2', array($this, 'cron_action'));
362 wp_schedule_event(current_time( "timestamp", 0 ) + $this_time , $schedule_type, 'wpo_cron_event2');
363 WP_Optimize()->log('running wp_schedule_event()');
364 }
365 }
366 }
367 }
368
369
370 // scheduler public functions to update schedulers
371 public function cron_schedules( $schedules ) {
372 $schedules['wpo_daily'] = array('interval' => 86400, 'display' => 'Once Daily');
373 $schedules['wpo_weekly'] = array('interval' => 86400*7, 'display' => 'Once Weekly');
374 $schedules['wpo_otherweekly'] = array('interval' => 86400*14, 'display' => 'Once Every Other Week');
375 $schedules['wpo_monthly'] = array('interval' => 86400*30, 'display' => 'Once Every Month');
376 return $schedules;
377 }
378
379
380 public function admin_menu() {
381
382 $capability_required = $this->capability_required();
383
384 if (!current_user_can($capability_required)) return;
385
386 $icon_svg = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjxzdmcKICAgeG1sbnM6ZGM9Imh0dHA6Ly9wdXJsLm9yZy9kYy9lbGVtZW50cy8xLjEvIgogICB4bWxuczpjYz0iaHR0cDovL2NyZWF0aXZlY29tbW9ucy5vcmcvbnMjIgogICB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiCiAgIHhtbG5zOnN2Zz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiAgIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICAgdmlld0JveD0iMCAwIDE2IDE2IgogICB2ZXJzaW9uPSIxLjEiCiAgIGlkPSJzdmc0MzE2IgogICBoZWlnaHQ9IjE2IgogICB3aWR0aD0iMTYiPgogIDxkZWZzCiAgICAgaWQ9ImRlZnM0MzE4IiAvPgogIDxtZXRhZGF0YQogICAgIGlkPSJtZXRhZGF0YTQzMjEiPgogICAgPHJkZjpSREY+CiAgICAgIDxjYzpXb3JrCiAgICAgICAgIHJkZjphYm91dD0iIj4KICAgICAgICA8ZGM6Zm9ybWF0PmltYWdlL3N2Zyt4bWw8L2RjOmZvcm1hdD4KICAgICAgICA8ZGM6dHlwZQogICAgICAgICAgIHJkZjpyZXNvdXJjZT0iaHR0cDovL3B1cmwub3JnL2RjL2RjbWl0eXBlL1N0aWxsSW1hZ2UiIC8+CiAgICAgICAgPGRjOnRpdGxlPjwvZGM6dGl0bGU+CiAgICAgIDwvY2M6V29yaz4KICAgIDwvcmRmOlJERj4KICA8L21ldGFkYXRhPgogIDxnCiAgICAgaWQ9ImxheWVyMSI+CiAgICA8cGF0aAogICAgICAgc3R5bGU9ImZpbGw6I2EwYTVhYTtmaWxsLW9wYWNpdHk6MSIKICAgICAgIGlkPSJwYXRoNTciCiAgICAgICBkPSJtIDEwLjc2ODgwOSw2Ljc2MTYwNTEgMCwwIGMgLTAuMDE2ODgsLTAuMDE2ODc4IC0wLjAyNTMxLC0wLjA0MjE4MSAtMC4wMzM3NCwtMC4wNjc0OTkgLTAuMDA4NCwtMC4wMDgzOSAtMC4wMDg0LC0wLjAxNjg3OCAtMC4wMTY4OCwtMC4wMzM3NDMgQyA5Ljk5MjYxMTIsNS4xOTIzMzY2IDguMjIwODU1Nyw0LjU4NDg3ODEgNi43NDQzOTEyLDUuMjkzNTc5NyA1LjY3MjkwMDUsNS44MDgyMzI4IDUuMDU3MDA0Myw2Ljg4ODE2MTMgNS4wNjU0NDIsOC4wMDE4MzY1IDQuNDU3OTgyMiw3LjMxMDAwNzYgMy42OTg2NTg0LDYuNzk1MzU0NSAyLjg1NDk2NDIsNi40OTE2MjUzIDMuMjY4Mzc0Myw1LjA2NTc4MzEgNC4yNTU0OTYsMy44MTcxMTY2IDUuNjg5Nzc0NiwzLjEyNTI4NzggOC4zNjQyODMyLDEuODM0NDM2OCAxMS41NzAzMTksMi45Mzk2NzQ0IDEyLjg4NjQ4MSw1LjU4ODg3MjYgMTMuNDUxNzU1LDYuNzI3ODU5NiAxNC42NDk4MDEsNy4zNTIxOTIxIDE1Ljg0Nzg0Niw3LjIzNDA3NSAxNS43NjM0ODIsNi4zMzk3NiAxNS41MTg4MDUsNS40MzcwMDg2IDE1LjEwNTM5Niw0LjU3NjQ0MDQgMTMuMjE1NTIxLDAuNjg3MDEzNCA4LjUzMzAyMjYsLTAuOTQxMzE2MjcgNC42NDM1OTQzLDAuOTQwMTIxNzkgMi4zMjM0MzcsMi4wNjIyMzM0IDAuODA0Nzg4MTQsNC4xNzk5MDQ0IDAuMzU3NjMxMzIsNi41MzM4MDk4IDIuNDE2MjQzOCw2LjQyNDEyOSA0LjQzMjY3MTcsNy41MDQwNTc0IDUuNDM2NjY2Miw5LjQzNjExNjcgbCAwLjAwODM5LDAgYyAwLjc1OTMxOTIsMS4zNzUyMjAzIDIuNDcyMDE3OCwxLjk0MDQ5NTMgMy45MDYyOTYsMS4yNDg2NjczIDEuMDQ2MTc5OCwtMC41MDYyMTggMS42NTM2NDA4LC0xLjUzNTUyMzggMS42Nzg5NTA4LC0yLjYxNTQ1MTIgMC41ODIxNDgsMC43MDg3MDE4IDEuMzMzMDM1LDEuMjQ4NjY2OCAyLjE1OTg1NiwxLjU3NzcwNjQgLTAuNDM4NzIxLDEuMzU4MzQ3OCAtMS40MDA1MzMsMi41NDc5NTQ4IC0yLjc5MjYyNywzLjIxNDQ3ODggLTIuNTkwMTM4NywxLjI0ODY1OCAtNS42NzgwNTc0LDAuMjUzMTA0IC03LjA2MTcxNTEsLTIuMjI3MzU3IGwgMCwwIEMgMi43NjIxMDQ4LDkuNDUyOTg5NCAxLjUxMzQzODMsOC44MjAyMTkxIDAuMjgxNjQ1OTIsOC45NzIwODQ0IDAuMzgyODg3NjUsOS43OTg5MDQ2IDAuNjE5MTIzMzEsMTAuNjE3Mjg3IDAuOTk4Nzg1MiwxMS40MDE5MjIgYyAxLjg4MTQzNjgsMy44OTc4NjQgNi41NjM5MzcsNS41MjYxOTggMTAuNDYxODAwOCwzLjY0NDc2IDIuMjQ0MjI2LC0xLjA4ODM2OSAzLjczNzU2MiwtMy4xMDQ3OTYgNC4yMzUzNDIsLTUuMzc0MzMyMyAtMS45OTk1NTQsMC4wNDIxODEgLTMuOTQ4NDg2LC0xLjAyOTMwNjMgLTQuOTI3MTcsLTIuOTEwNzQzMyB6IgogICAgICAgY2xhc3M9InN0MTciIC8+CiAgPC9nPgo8L3N2Zz4K';
387
388 if (function_exists('add_meta_box')) {
389 add_menu_page("WP-Optimize", "WP-Optimize", $capability_required, "WP-Optimize", array($this,"wp_optimize_menu"), $icon_svg);
390 } else {
391 add_submenu_page("index.php", "WP-Optimize", "WP-Optimize", $capability_required, "WP-Optimize", array($this,"wp_optimize_menu"), $icon_svg);
392 }
393
394 $options = $this->get_options();
395
396 if ($options->get_option('enable-admin-menu', 'false' ) == 'true') {
397 add_action('wp_before_admin_bar_render', array($this, 'wpo_admin_bar'));
398 }
399
400 $options->set_default_options();
401 $this->cron_activate();
402 }
403
404 private function wp_normalize_path($path) {
405 // wp_normalize_path is not present before WP 3.9
406 if (function_exists('wp_normalize_path')) return wp_normalize_path($path);
407 // Taken from WP 4.6
408 $path = str_replace( '\\', '/', $path );
409 $path = preg_replace( '|(?<=.)/+|', '/', $path );
410 if ( ':' === substr( $path, 1, 1 ) ) {
411 $path = ucfirst( $path );
412 }
413 return $path;
414 }
415
416 public function get_templates_dir() {
417 return apply_filters('wp_optimize_templates_dir', $this->wp_normalize_path(WPO_PLUGIN_MAIN_PATH.'/templates'));
418 }
419
420 public function get_templates_url() {
421 return apply_filters('wp_optimize_templates_url', WPO_PLUGIN_MAIN_PATH.'/templates');
422 }
423
424 public function include_template($path, $return_instead_of_echo = false, $extract_these = array()) {
425 if ($return_instead_of_echo) ob_start();
426
427 if (preg_match('#^([^/]+)/(.*)$#', $path, $matches)) {
428 $prefix = $matches[1];
429 $suffix = $matches[2];
430 if (isset($this->template_directories[$prefix])) {
431 $template_file = $this->template_directories[$prefix].'/'.$suffix;
432 }
433 }
434
435 if (!isset($template_file)) {
436 $template_file = WPO_PLUGIN_MAIN_PATH.'/templates/'.$path;
437 }
438
439 $template_file = apply_filters('wp_optimize_template', $template_file, $path);
440
441 do_action('wp_optimize_before_template', $path, $template_file, $return_instead_of_echo, $extract_these);
442
443 if (!file_exists($template_file)) {
444 error_log("WP Optimize: template not found: $template_file");
445 echo __('Error:', 'wp-optimize').' '.__('template not found', 'wp-optimize')." ($path)";
446 } else {
447 extract($extract_these);
448 global $wpdb;
449 $wp_optimize = $this;
450 $optimizer = $this->get_optimizer();
451 $options = $this->get_options();
452 $wp_optimize_notices = $this->get_notices();
453 include $template_file;
454 }
455
456 do_action('wp_optimize_after_template', $path, $template_file, $return_instead_of_echo, $extract_these);
457
458 if ($return_instead_of_echo) return ob_get_clean();
459 }
460
461 private function register_template_directories() {
462
463 $template_directories = array();
464
465 $templates_dir = $this->get_templates_dir();
466
467 if ($dh = opendir($templates_dir)) {
468 while (($file = readdir($dh)) !== false) {
469 if ('.' == $file || '..' == $file) continue;
470 if (is_dir($templates_dir.'/'.$file)) {
471 $template_directories[$file] = $templates_dir.'/'.$file;
472 }
473 }
474 closedir($dh);
475 }
476
477 // This is the optimal hook for most extensions to hook into
478 $this->template_directories = apply_filters('wp_optimize_template_directories', $template_directories);
479
480 }
481
482 // TODO: Not currently used; investigate.
483 // TODO: The description does not match the actual function
484 /**
485 * send_email($sendto, $msg)
486 * @return success
487 * @param $sentdo - eg. who to send it to, abc@def.com
488 * @param $msg - the msg in text
489 */
490 public function send_email($date, $cleanedup){
491 //
492 ob_start();
493 // #TODO this need to work on - currently not using the parameter values
494 $myTime = current_time( "timestamp", 0 );
495 $myDate = gmdate(get_option('date_format') . ' ' . get_option('time_format'), $myTime );
496
497 //$formattedCleanedup = $wp_optimize->format_size($cleanedup);
498
499 $sendto = $options->get_option('email-address');
500 if (!$sendto) $sendto = get_bloginfo ( 'admin_email' );
501
502 //$thiscleanup = $wp_optimize->format_size($cleanedup);
503
504 $subject = get_bloginfo ( 'name' ).": ".__("Automatic Operation Completed","wp-optimize")." ".$myDate;
505
506 $msg = __("Scheduled optimization was executed at","wp-optimize")." ".$myDate."\r\n"."\r\n";
507 //$msg .= __("Recovered space","wp-optimize").": ".$thiscleanup."\r\n";
508 $msg .= __("You can safely delete this email.","wp-optimize")."\r\n";
509 $msg .= "\r\n";
510 $msg .= __("Regards,","wp-optimize")."\r\n";
511 $msg .= __("WP-Optimize Plugin","wp-optimize");
512
513 //wp_mail( $sendto, $subject, $msg );
514
515 ob_end_flush();
516 }
517
518 /*
519 * function log()
520 *
521 * parameters: message to debug
522 *
523 * @return none
524 */
525 public function log($message) {
526 if (defined('WP_DEBUG') && WP_DEBUG) {
527 error_log($message);
528 }
529 }
530
531 /**
532 * $wp_optimize->format_size()
533 * Function: Format Bytes Into KB/MB
534 * @param mixed $bytes
535 * @return
536 */
537 public function format_size($bytes) {
538 if ($bytes > 1073741824) {
539 return number_format_i18n($bytes/1073741824, 2) . ' '.__('GB', 'wp-optimize');
540 } elseif ($bytes > 1048576) {
541 return number_format_i18n($bytes/1048576, 1) . ' '.__('MB', 'wp-optimize');
542 } elseif ($bytes > 1024) {
543 return number_format_i18n($bytes/1024, 1) . ' '.__('KB', 'wp-optimize');
544 } else {
545 return number_format_i18n($bytes, 0) . ' '.__('bytes', 'wp-optimize');
546 }
547 }
548
549 /*
550 * function cron_action()
551 *
552 * parameters: none
553 *
554 * executed this function on cron event
555 *
556 * @return none
557 */
558 public function cron_action() {
559
560 $optimizer = $this->get_optimizer();
561 $options = $this->get_options();
562
563 $this->log('WPO: Starting cron_action()');
564
565 if ('true' == $options->get_option('schedule')) {
566
567 $this_options = $options->get_option('auto');
568
569 $optimizations = $optimizer->get_optimizations();
570
571 // TODO: The output of the optimizations is not saved/used/logged
572 $results = $optimizer->do_optimizations($this_options, 'auto');
573
574 }
575
576 }
577
578 }
579
580 function WP_Optimize() {
581 return WP_Optimize::instance();
582 }
583
584 $GLOBALS['wp_optimize'] = WP_Optimize();
585
586 // plugin activation actions
587 function wpo_activation_actions() {
588 WP_Optimize()->get_options()->set_default_options();
589 }
590
591 // plugin deactivation actions
592 function wpo_deactivation_actions() {
593 wpo_cron_deactivate();
594 WP_Optimize()->get_options()->delete_all_options();
595 }
596
597 function wpo_cron_deactivate() {
598 WP_Optimize()->log('running wpo_cron_deactivate()');
599 wp_clear_scheduled_hook('wpo_cron_event2');
600 }
601