| 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 |
/** |
| 10 |
* Prepare data for preview widget. |
| 11 |
* |
| 12 |
* @param array $params |
| 13 |
* |
| 14 |
* @return array |
| 15 |
*/ |
| 16 |
public function preview($params) { |
| 17 |
|
| 18 |
// get data requested for preview. |
| 19 |
$sql = $this->wpdb->prepare( |
| 20 |
"SELECT comment_ID, comment_author, SUBSTR(comment_content, 1, 128) AS comment_content FROM `" . $this->wpdb->comments . "`". |
| 21 |
" WHERE comment_type = 'pingback'". |
| 22 |
" ORDER BY `comment_ID` LIMIT %d, %d;", |
| 23 |
array( |
| 24 |
$params['offset'], |
| 25 |
$params['limit'], |
| 26 |
) |
| 27 |
); |
| 28 |
|
| 29 |
$posts = $this->wpdb->get_results($sql, ARRAY_A); |
| 30 |
|
| 31 |
// fix empty revision titles. |
| 32 |
if (!empty($posts)) { |
| 33 |
foreach ($posts as $key => $post) { |
| 34 |
$posts[$key]['post_title'] = array( |
| 35 |
'text' => '' == $post['post_title'] ? '('.__('no title', 'wp-optimize').')' : $post['post_title'], |
| 36 |
'url' => get_edit_post_link($post['ID']), |
| 37 |
); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// get total count comments for optimization. |
| 42 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->comments . "` WHERE comment_type = 'pingback';"; |
| 43 |
|
| 44 |
$total = $this->wpdb->get_var($sql); |
| 45 |
|
| 46 |
return array( |
| 47 |
'id_key' => 'comment_ID', |
| 48 |
'columns' => array( |
| 49 |
'comment_ID' => __('ID', 'wp-optimize'), |
| 50 |
'comment_author' => __('Author', 'wp-optimize'), |
| 51 |
'comment_content' => __('Comment', 'wp-optimize'), |
| 52 |
), |
| 53 |
'offset' => $params['offset'], |
| 54 |
'limit' => $params['limit'], |
| 55 |
'total' => $total, |
| 56 |
'data' => $this->htmlentities_array($posts, array('comment_ID')), |
| 57 |
'message' => $total > 0 ? '' : __('No pingbacks found', 'wp-optimize'), |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Do actions after optimize() function. |
| 63 |
*/ |
| 64 |
public function after_optimize() { |
| 65 |
$message = sprintf(_n('%s pingback deleted', '%s pingbacks deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count)); |
| 66 |
|
| 67 |
if ($this->is_multisite_mode()) { |
| 68 |
$message .= ' '.sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 69 |
} |
| 70 |
|
| 71 |
$this->logger->info($message); |
| 72 |
$this->register_output($message); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Do optimization. |
| 77 |
*/ |
| 78 |
public function optimize() { |
| 79 |
$clean = "DELETE FROM `" . $this->wpdb->comments . "` WHERE comment_type = 'pingback'"; |
| 80 |
|
| 81 |
// if posted ids in params, then remove only selected items. used by preview widget. |
| 82 |
if (isset($this->data['ids'])) { |
| 83 |
$clean .= ' AND comment_ID in ('.join(',', $this->data['ids']).')'; |
| 84 |
} |
| 85 |
|
| 86 |
$clean .= ";"; |
| 87 |
|
| 88 |
$comments = $this->query($clean); |
| 89 |
$this->processed_count += $comments; |
| 90 |
|
| 91 |
// clean orphaned comment meta |
| 92 |
$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"; |
| 93 |
$this->query($clean); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Do actions after get_info() function. |
| 98 |
*/ |
| 99 |
public function after_get_info() { |
| 100 |
if ($this->found_count > 0) { |
| 101 |
$message = sprintf(_n('%s pingback found', '%s pingbacks found', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count)); |
| 102 |
} else { |
| 103 |
$message = __('No pingbacks found', 'wp-optimize'); |
| 104 |
} |
| 105 |
|
| 106 |
if ($this->is_multisite_mode()) { |
| 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->comments . "` WHERE comment_type='pingback';"; |
| 123 |
|
| 124 |
$comments = $this->wpdb->get_var($sql); |
| 125 |
$this->found_count += $comments; |
| 126 |
} |
| 127 |
|
| 128 |
public function settings_label() { |
| 129 |
return __('Remove pingbacks', 'wp-optimize'); |
| 130 |
} |
| 131 |
} |
| 132 |
|