PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.6.1
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.6.1
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.6.1, at optimizations/trackbacks.php

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