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 / trackbacks.php

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

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