PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
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 / pingbacks.php

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

154 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_pingbacks extends WP_Optimization {
6
7 public $available_for_auto = true;
8
9 public $ui_sort_order = 6000;
10
11 public $available_for_saving = true;
12
13 /**
14 * Prepare data for preview widget.
15 *
16 * @param array $params
17 *
18 * @return array
19 */
20 public function preview($params) {
21
22 // get data requested for preview.
23 // `$this->wpdb` is `$wpdb`
24 // phpcs:disable
25 $sql = $this->wpdb->prepare(
26 "SELECT comment_ID, comment_author, SUBSTR(comment_content, 1, 128) AS comment_content FROM `" . $this->wpdb->comments . "`".
27 " WHERE comment_type = 'pingback'".
28 " ORDER BY `comment_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 // phpcs:enable
37
38 // fix empty revision titles.
39 if (!empty($posts)) {
40 foreach ($posts as $key => $post) {
41 $posts[$key]['post_title'] = array(
42 'text' => '' === $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'],
43 'url' => get_edit_post_link($post['ID']),
44 );
45 }
46 }
47
48 // get total count comments for optimization.
49 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_type = 'pingback';";
50
51 $total = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- SQL is safe, no user input used
52
53 return array(
54 'id_key' => 'comment_ID',
55 'columns' => array(
56 'comment_ID' => __('ID', 'wp-optimize'),
57 'comment_author' => __('Author', 'wp-optimize'),
58 'comment_content' => __('Comment', 'wp-optimize'),
59 ),
60 'offset' => $params['offset'],
61 'limit' => $params['limit'],
62 'total' => $total,
63 'data' => $this->htmlentities_array($posts, array('comment_ID')),
64 'message' => $total > 0 ? '' : __('No pingbacks found', 'wp-optimize'),
65 );
66 }
67
68 /**
69 * Do actions after optimize() function.
70 */
71 public function after_optimize() {
72 // translators: %s is number of pingback deleted
73 $message = sprintf(_n('%s pingback deleted', '%s pingbacks deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count));
74
75 if ($this->is_multisite_mode()) {
76 // translators: %s is number of sites
77 $message .= ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
78 }
79
80 $this->logger->info($message);
81 $this->register_output($message);
82 }
83
84 /**
85 * Do optimization.
86 */
87 public function optimize() {
88 $clean = "DELETE FROM `" . $this->wpdb->comments . "` WHERE comment_type = 'pingback'";
89
90 // if posted ids in params, then remove only selected items. used by preview widget.
91 if (isset($this->data['ids'])) {
92 $clean .= ' AND comment_ID in ('.join(',', $this->data['ids']).')';
93 }
94
95 $clean .= ";";
96
97 $comments = $this->query($clean);
98 $this->processed_count += $comments;
99
100 // update comment count
101 $update = "UPDATE `" . $this->wpdb->posts . "` as p
102 INNER JOIN (SELECT comment_post_ID as cid, COUNT(comment_post_ID) as cc
103 FROM `" . $this->wpdb->comments . "` GROUP BY comment_post_ID) AS c ON p.ID = c.cid
104 SET p.comment_count = c.cc
105 WHERE p.ID = c.cid";
106 $this->query($update);
107
108 // clean orphaned comment meta
109 $clean = "DELETE cm FROM `" . $this->wpdb->commentmeta . "` cm LEFT JOIN `" . $this->wpdb->comments . "` c ON cm.comment_id = c.comment_ID WHERE c.comment_ID IS NULL";
110 $this->query($clean);
111 }
112
113 /**
114 * Do actions after get_info() function.
115 */
116 public function after_get_info() {
117 if ($this->found_count > 0) {
118 // translators: %s is number of pingbacks
119 $message = sprintf(_n('%s pingback found', '%s pingbacks found', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count));
120 } else {
121 $message = __('No pingbacks found', 'wp-optimize');
122 }
123
124 if ($this->is_multisite_mode()) {
125 // translators: %s is number of sites
126 $message .= ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids));
127 }
128
129 // add preview link to message.
130 if ($this->found_count > 0) {
131 $message = $this->get_preview_link($message);
132 }
133
134 $this->register_output($message);
135 }
136
137 /**
138 * Get count of unoptimized items.
139 */
140 public function get_info() {
141 $sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_type='pingback';";
142
143 $comments = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- SQL is safe, no user input used
144 $this->found_count += $comments;
145 }
146
147 /**
148 * Get settings label.
149 */
150 public function settings_label() {
151 return __('Remove pingbacks', 'wp-optimize');
152 }
153 }
154