PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.3
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.3
4.7.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 All 111 releases
wp-optimize / optimizations / postmeta.php

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

146 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) 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 // `$this->wpdb->prepare` is `$wpdb->prepare` from global variable.
25 // phpcs:disable
26 $sql = $this->wpdb->prepare(
27 "SELECT pm.* FROM `" . $this->wpdb->postmeta . "` pm".
28 " LEFT JOIN `" . $this->wpdb->posts . "` wp ON wp.ID = pm.post_id".
29 " WHERE wp.ID IS NULL".
30 " ORDER BY pm.meta_id LIMIT %d, %d;",
31 array(
32 $params['offset'],
33 $params['limit'],
34 )
35 );
36
37 $posts = $this->wpdb->get_results($sql, ARRAY_A);
38 // phpcs:enable
39
40 // get total count post meta for optimization.
41 $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;";
42
43 $total = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Statement is safe, no user input used
44
45 return array(
46 'id_key' => 'meta_id',
47 'columns' => array(
48 'meta_id' => __('ID', 'wp-optimize'),
49 'post_id' => __('Post ID', 'wp-optimize'),
50 'meta_key' => __('Meta Key', 'wp-optimize'), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- This is not a query
51 'meta_value' => __('Meta Value', 'wp-optimize'), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- This is not a query
52 ),
53 'offset' => $params['offset'],
54 'limit' => $params['limit'],
55 'total' => $total,
56 'data' => $this->htmlentities_array($posts, array('ID')),
57 'message' => $total > 0 ? '' : __('No orphaned post metadata in your database', 'wp-optimize'),
58 );
59 }
60
61 /**
62 * Do actions after optimize() function.
63 */
64 public function after_optimize() {
65 // translators: %s: number of orphaned post metadata records
66 $message = sprintf(_n('%s orphaned post metadata deleted', '%s orphaned post metadata deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count));
67
68 if ($this->is_multisite_mode()) {
69 // translators: %s: number of sites
70 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
71 }
72
73 $this->logger->info($message);
74 $this->register_output($message);
75 }
76
77 /**
78 * Do optimization.
79 */
80 public function optimize() {
81 $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";
82
83 // if posted ids in params, then remove only selected items. used by preview widget.
84 if (isset($this->data['ids'])) {
85 $clean .= ' AND pm.meta_id in ('.join(',', $this->data['ids']).')';
86 }
87
88 $clean .= ";";
89
90 $postmeta = $this->query($clean);
91 $this->processed_count += $postmeta;
92 }
93
94 /**
95 * Do actions after get_info() function.
96 */
97 public function after_get_info() {
98 if ($this->found_count) {
99 // translators: %s: number of orphaned post metadata records
100 $message = sprintf(_n('%s orphaned post metadata in your database', '%s orphaned post metadata in your database', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count));
101 } else {
102 $message = __('No orphaned post metadata in your database', 'wp-optimize');
103 }
104
105 if ($this->is_multisite_mode()) {
106 // translators: %s: number of sites
107 $message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
108 }
109
110 // add preview link to message.
111 if ($this->found_count > 0) {
112 $message = $this->get_preview_link($message);
113 }
114
115 $this->register_output($message);
116 }
117
118 /**
119 * Get count of unoptimized items.
120 */
121 public function get_info() {
122 $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;";
123 $postmeta = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Statement is safe, no user input used
124
125 $this->found_count += $postmeta;
126 }
127
128 /**
129 * Returns settings label
130 *
131 * @return string
132 */
133 public function settings_label() {
134 return __('Clean post metadata', 'wp-optimize');
135 }
136
137 /**
138 * N.B. This is not currently used; it was commented out in 1.9.1
139 *
140 * @return string Returns the description once auto remove option has run
141 */
142 public function get_auto_option_description() {
143 return __('Remove orphaned post meta', 'wp-optimize');
144 }
145 }
146