PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.0.9
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.0.9
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 / optimizations / postmeta.php

postmeta.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.0.9, at optimizations/postmeta.php

134 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('WPO_VERSION')) die('No direct access allowed');
4
5 class WP_Optimization_postmeta extends WP_Optimization {
6
7 public $ui_sort_order = 8000;
8
9 public $available_for_auto = false;
10
11 public $available_for_saving = true;
12
13 public $auto_default = false;
14
15 /**
16 * Prepare data for preview widget.
17 *
18 * @param array $params
19 *
20 * @return array
21 */
22 public function preview($params) {
23 // get data requested for preview.
24 $sql = $this->wpdb->prepare(
25 "SELECT pm.* FROM `" . $this->wpdb->postmeta . "` pm".
26 " LEFT JOIN `" . $this->wpdb->posts . "` wp ON wp.ID = pm.post_id".
27 " WHERE wp.ID IS NULL".
28 " ORDER BY pm.meta_id LIMIT %d, %d;",
29 array(
30 $params['offset'],
31 $params['limit'],
32 )
33 );
34
35 $posts = $this->wpdb->get_results($sql, ARRAY_A);
36
37 // get total count post meta for optimization.
38 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->postmeta . "` pm LEFT JOIN `" . $this->wpdb->posts . "` wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;";
39
40 $total = $this->wpdb->get_var($sql);
41
42 return array(
43 'id_key' => 'meta_id',
44 'columns' => array(
45 'meta_id' => __('ID', 'wp-optimize'),
46 'post_id' => __('Post ID', 'wp-optimize'),
47 'meta_key' => __('Meta Key', 'wp-optimize'),
48 'meta_value' => __('Meta Value', 'wp-optimize'),
49 ),
50 'offset' => $params['offset'],
51 'limit' => $params['limit'],
52 'total' => $total,
53 'data' => $this->htmlentities_array($posts, array('ID')),
54 'message' => $total > 0 ? '' : __('No orphaned post meta data in your database', 'wp-optimize'),
55 );
56 }
57
58 /**
59 * Do actions after optimize() function.
60 */
61 public function after_optimize() {
62 $message = sprintf(_n('%s orphaned post meta data deleted', '%s orphaned post meta data deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count));
63
64 if ($this->is_multisite_mode()) {
65 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
66 }
67
68 $this->logger->info($message);
69 $this->register_output($message);
70 }
71
72 /**
73 * Do optimization.
74 */
75 public function optimize() {
76 $clean = "DELETE pm FROM `" . $this->wpdb->postmeta . "` pm LEFT JOIN `" . $this->wpdb->posts . "` wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL";
77
78 // if posted ids in params, then remove only selected items. used by preview widget.
79 if (isset($this->data['ids'])) {
80 $clean .= ' AND pm.meta_id in ('.join(',', $this->data['ids']).')';
81 }
82
83 $clean .= ";";
84
85 $postmeta = $this->query($clean);
86 $this->processed_count += $postmeta;
87 }
88
89 /**
90 * Do actions after get_info() function.
91 */
92 public function after_get_info() {
93 if ($this->found_count) {
94 $message = sprintf(_n('%s orphaned post meta data in your database', '%s orphaned post meta data in your database', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count));
95 } else {
96 $message = __('No orphaned post meta data in your database', 'wp-optimize');
97 }
98
99 if ($this->is_multisite_mode()) {
100 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
101 }
102
103 // add preview link to message.
104 if ($this->found_count > 0) {
105 $message = $this->get_preview_link($message);
106 }
107
108 $this->register_output($message);
109 }
110
111 /**
112 * Get count of unoptimized items.
113 */
114 public function get_info() {
115 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->postmeta . "` pm LEFT JOIN `" . $this->wpdb->posts . "` wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;";
116 $postmeta = $this->wpdb->get_var($sql);
117
118 $this->found_count += $postmeta;
119 }
120
121 public function settings_label() {
122 return __('Clean post meta data', 'wp-optimize');
123 }
124
125 /**
126 * N.B. This is not currently used; it was commented out in 1.9.1
127 *
128 * @return string Returns the description once auto remove option has ran
129 */
130 public function get_auto_option_description() {
131 return __('Remove orphaned post meta', 'wp-optimize');
132 }
133 }
134