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

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

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