PluginProbe
Disable Comments & Delete All Comments / 1.0.0
Disable Comments & Delete All Comments v1.0.0
1.3.3 1.3.2 trunk 1.0.0 1.0.4 1.0.5 1.0.8 1.0.9 1.1.1 1.1.3 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.3.0 1.3.1
comments-plus / includes / classes / class.configurate-comments.php

class.configurate-comments.php in Disable Comments & Delete All Comments 1.0.0, at includes/classes/class.configurate-comments.php

379 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This class configures the parameters advanced
5 * @author Webcraftic <wordpress.webraftic@gmail.com>
6 * @copyright (c) 2017 Webraftic Ltd
7 * @version 1.0
8 */
9 class WbcrCmp_ConfigComments extends WbcrFactoryClearfy_Configurate {
10
11 private $modified_types = array();
12
13 public function registerActionsAndFilters()
14 {
15 // Removes the server responses a reference to the xmlrpc file.
16 if( $this->getOption('remove_x_pingback') || $this->isDisabledAllPosts() ) {
17 add_filter('template_redirect', array($this, 'removeXPingbackHeaders'));
18 add_filter('wp_headers', array($this, 'removeXPingback'));
19
20 add_action('template_redirect', array($this, 'linkRelBufferStart'), -1);
21 add_action('get_header', array($this, 'linkRelBufferStart'));
22 add_action('wp_head', array($this, 'linkRelBufferEnd'), 999);
23 }
24
25 // These need to happen now
26 if( $this->isDisabledAllPosts() ) {
27 add_action('widgets_init', array($this, 'disableRcWidget'));
28 add_action('template_redirect', array($this, 'filterQuery'), 9); // before redirect_canonical
29
30 // Admin bar filtering has to happen here since WP 3.6
31 add_action('template_redirect', array($this, 'filterAdminBar'));
32 add_action('admin_init', array($this, 'filterAdminBar'));
33 } else {
34 if( $this->getOption('remove_url_from_comment_form') ) {
35 add_filter('comment_form_default_fields', array($this, 'removeUrlFromCommentForm'));
36 }
37
38 if( $this->getOption('comment_text_convert_links_pseudo') ) {
39 add_filter('comment_text', array($this, 'commentTextConvertLinksPseudo'));
40 }
41
42 if( $this->getOption('pseudo_comment_author_link') ) {
43 add_filter('get_comment_author_link', array($this, 'pseudoCommentAuthorLink'), 100, 3);
44 }
45 }
46
47 // These can happen later
48 //add_action('plugins_loaded', array($this, 'register_text_domain'));
49 add_action('wp_loaded', array($this, 'initWploadedFilters'));
50 }
51
52 private function isDisabledAllPosts()
53 {
54 return $this->getOption('disable_comments', 'enable_comments') == 'disable_comments';
55 }
56
57 private function isDisabledCertainPostTypes()
58 {
59 return $this->getOption('disable_comments', 'enable_comments') == 'disable_certain_post_types_comments';
60 }
61
62 private function isEnabledComments()
63 {
64 return $this->getOption('disable_comments', 'enable_comments') == 'enable_comments';
65 }
66
67
68 /*
69 * Get an array of disabled post type.
70 */
71 private function getDisabledPostTypes()
72 {
73 $post_types = $this->getOption('disable_comments_for_post_types');
74
75 if( $this->isDisabledAllPosts() ) {
76 $all_post_types = get_post_types(array('public' => true), 'objects');
77
78 return array_keys($all_post_types);
79 }
80
81 if( is_array($post_types) ) {
82 return $post_types;
83 }
84
85 return explode(',', $post_types);
86 }
87
88 /*
89 * Check whether comments have been disabled on a given post type.
90 */
91 private function isPostTypeDisabled($type)
92 {
93 return $this->isDisabledCertainPostTypes() && in_array($type, $this->getDisabledPostTypes());
94 }
95
96 public function initWploadedFilters()
97 {
98 $disabled_post_types = $this->getDisabledPostTypes();
99
100 if( !empty($disabled_post_types) && !$this->isEnabledComments() ) {
101 foreach($disabled_post_types as $type) {
102 // we need to know what native support was for later
103 if( post_type_supports($type, 'comments') ) {
104 $this->modified_types[] = $type;
105 remove_post_type_support($type, 'comments');
106 remove_post_type_support($type, 'trackbacks');
107 }
108 }
109 add_filter('comments_array', array($this, 'filterExistingComments'), 20, 2);
110 add_filter('comments_open', array($this, 'filterCommentStatus'), 20, 2);
111 add_filter('pings_open', array($this, 'filterCommentStatus'), 20, 2);
112 }
113
114 // Filters for the admin only
115 if( is_admin() ) {
116 add_action('admin_print_footer_scripts', array($this, 'discussionNotice'));
117 //add_filter('plugin_row_meta', array($this, 'set_plugin_meta'), 10, 2);
118
119 // if only certain types are disabled, remember the original post status
120 if( !$this->isDisabledAllPosts() ) {
121 add_action('edit_form_advanced', array($this, 'editFormInputs'));
122 add_action('edit_page_form', array($this, 'editFormInputs'));
123 } else {
124 add_action('admin_menu', array($this, 'filterAdminMenu'), 9999); // do this as late as possible
125 add_action('admin_print_footer_scripts-index.php', array($this, 'dashboardJs'));
126 add_action('wp_dashboard_setup', array($this, 'filterDashboard'));
127 add_filter('pre_option_default_pingback_flag', '__return_zero');
128 }
129 } // Filters for front end only
130 else {
131 add_action('template_redirect', array($this, 'checkCommentTemplate'));
132
133 if( $this->isDisabledAllPosts() ) {
134 add_filter('feed_links_show_comments_feed', '__return_false');
135 }
136 }
137 }
138
139 /*
140 * Replace the theme's comment template with a blank one.
141 * To prevent this, define DISABLE_COMMENTS_REMOVE_COMMENTS_TEMPLATE
142 * and set it to True
143 */
144 public function checkCommentTemplate()
145 {
146 if( is_singular() && ($this->isDisabledAllPosts() || $this->isPostTypeDisabled(get_post_type())) ) {
147 if( !defined('DISABLE_COMMENTS_REMOVE_COMMENTS_TEMPLATE') || DISABLE_COMMENTS_REMOVE_COMMENTS_TEMPLATE == true ) {
148 // Kill the comments template.
149 add_filter('comments_template', array($this, 'dummyCommentsTemplate'), 20);
150 }
151 // Remove comment-reply script for themes that include it indiscriminately
152 wp_deregister_script('comment-reply');
153 // feed_links_extra inserts a comments RSS link
154 remove_action('wp_head', 'feed_links_extra', 3);
155 }
156 }
157
158 public function dummyCommentsTemplate()
159 {
160 return WBCR_CMP_PLUGIN_DIR . '/includes/comments-template.php';
161 }
162
163 /*
164 * Issue a 403 for all comment feed requests.
165 */
166 public function filterQuery()
167 {
168 if( is_comment_feed() ) {
169 wp_die(__('Comments are closed.'), '', array('response' => 403));
170 }
171 }
172
173 /*
174 * Remove comment links from the admin bar.
175 */
176 public function filterAdminBar()
177 {
178 if( is_admin_bar_showing() ) {
179 // Remove comments links from admin bar
180 remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
181 }
182 }
183
184 public function editFormInputs()
185 {
186 global $post;
187 // Without a dicussion meta box, comment_status will be set to closed on new/updated posts
188 if( in_array($post->post_type, $this->modified_types) ) {
189 echo '<input type="hidden" name="comment_status" value="' . $post->comment_status . '" /><input type="hidden" name="ping_status" value="' . $post->ping_status . '" />';
190 }
191 }
192
193 public function discussionNotice()
194 {
195 $disabled_post_types = $this->getDisabledPostTypes();
196 if( get_current_screen()->id == 'options-discussion' && !empty($disabled_post_types) ) {
197 $names = array();
198 foreach($disabled_post_types as $type)
199 $names[$type] = get_post_type_object($type)->labels->name;
200 ?>
201 <script>
202 jQuery(document).ready(function($) {
203 $(".wrap h2").first().after(<?php echo json_encode( '<div style="color: #900"><p>' . sprintf( __( 'Note: The <em>%s</em> plugin is currently active, and comments are completely disabled on: %s. Many of the settings below will not be applicable for those post types.', 'comments-plus' ), $this->plugin->pluginTitle, implode(', ', $names ) ) . '</p></div>' );?>);
204 });
205 </script>
206 <?php
207 }
208 }
209
210 public function filterAdminMenu()
211 {
212 global $pagenow;
213
214 if( $pagenow == 'comment.php' || $pagenow == 'edit-comments.php' || $pagenow == 'options-discussion.php' ) {
215 wp_die(__('Comments are closed.'), '', array('response' => 403));
216 }
217
218 remove_menu_page('edit-comments.php');
219 remove_submenu_page('options-general.php', 'options-discussion.php');
220 }
221
222 public function filterDashboard()
223 {
224 remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
225 }
226
227 public function dashboardJs()
228 {
229 echo '<script>
230 jQuery(function($){
231 $("#dashboard_right_now .comment-count, #latest-comments").hide();
232 $("#welcome-panel .welcome-comments").parent().hide();
233 });
234 </script>';
235 }
236
237 public function filterExistingComments($comments, $post_id)
238 {
239 $post = get_post($post_id);
240
241 return ($this->isDisabledAllPosts() || $this->isPostTypeDisabled($post->post_type))
242 ? array()
243 : $comments;
244 }
245
246 public function filterCommentStatus($open, $post_id)
247 {
248 $post = get_post($post_id);
249
250 return ($this->isDisabledAllPosts() || $this->isPostTypeDisabled($post->post_type))
251 ? false
252 : $open;
253 }
254
255 public function disableRcWidget()
256 {
257 unregister_widget('WP_Widget_Recent_Comments');
258 }
259
260 /**
261 * Convert links in comment text into span pseudo links
262 *
263 * @param $comment_text
264 * @return mixed
265 */
266
267 public function commentTextConvertLinksPseudo($comment_text)
268 {
269
270 return $this->convertLinksPseudo($comment_text);
271 }
272
273 /**
274 * Convert links into span pseudo links
275 *
276 * @param $text
277 * @return mixed
278 */
279
280 public function convertLinksPseudo($text)
281 {
282
283 return preg_replace_callback('/<a[^>]+href=[\'"](https?:\/\/[^"\']+)[\'"][^>]+>(.*?)<\/a>/i', array(
284 $this,
285 'replaceLinks'
286 ), $text);
287 }
288
289 public function replaceLinks($matches)
290 {
291 if( $matches[1] == get_home_url() ) {
292 return $matches[0];
293 }
294
295 return '<span class="wbcr-clearfy-pseudo-link" data-uri="' . $matches[1] . '" > ' . $matches[2] . '</span>';
296 }
297
298 /**
299 * Convert author link to pseudo link
300 *
301 * @return string
302 */
303
304 public function pseudoCommentAuthorLink($return, $author, $comment_ID)
305 {
306 $url = get_comment_author_url($comment_ID);
307 $author = get_comment_author($comment_ID);
308
309 if( empty($url) || 'http://' == $url ) {
310 $return = $author;
311 } else {
312 $return = '<span class="wbcr-clearfy-pseudo-link" data-uri="' . $url . '">' . $author . '</span>';
313 }
314
315 return $return;
316 }
317
318 /**
319 * Remove url field from comment form
320 *
321 * @param $fields
322 * @return mixed
323 */
324
325 public function removeUrlFromCommentForm($fields)
326 {
327 if( isset($fields['url']) ) {
328 unset($fields['url']);
329 }
330
331 return $fields;
332 }
333
334 /**
335 * Remove X-Pingback
336 * https://github.com/nickyurov/
337
338 */
339
340 public function removeXPingback($headers)
341 {
342 unset($headers['X-Pingback']);
343
344 return $headers;
345 }
346
347 public function removeXPingbackHeaders($headers)
348 {
349 if( function_exists('header_remove') ) {
350 header_remove('X-Pingback');
351 header_remove('Server');
352 }
353 }
354
355 //https://wordpress.stackexchange.com/questions/158700/how-to-remove-pingback-from-head
356
357 public function linkRelBufferCallback($buffer)
358 {
359 $old_buffer = $buffer;
360 $buffer = preg_replace('/(<link.*?rel=("|\')pingback("|\').*?href=("|\')(.*?)("|\')(.*?)?\/?>|<link.*?href=("|\')(.*?)("|\').*?rel=("|\')pingback("|\')(.*?)?\/?>)/i', '', $buffer);
361
362 // todo: fixed bug when return buffer null
363 if( empty($buffer) ) {
364 return $old_buffer;
365 }
366
367 return $buffer;
368 }
369
370 public function linkRelBufferStart()
371 {
372 ob_start(array($this, "linkRelBufferCallback"));
373 }
374
375 public function linkRelBufferEnd()
376 {
377 ob_flush();
378 }
379 }