PluginProbe
Disable Comments & Delete All Comments / 1.0.9
Disable Comments & Delete All Comments v1.0.9
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.9, at includes/classes/class.configurate-comments.php

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