| 1 |
<?php |
| 2 |
namespace WPEXtra\Modules\Common; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use WPEXtra\Settings; |
| 9 |
use WPEXtra\Helper; |
| 10 |
use WPEXtra\Base; |
| 11 |
|
| 12 |
class Comments extends Base { |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
parent::__construct(); |
| 16 |
} |
| 17 |
|
| 18 |
protected $features = [ |
| 19 |
'cm_antispam', |
| 20 |
'disable_comments', |
| 21 |
'cm_media', |
| 22 |
]; |
| 23 |
|
| 24 |
public function cm_antispam() { |
| 25 |
add_action('init', [$this, 'antispam_blacklist']); |
| 26 |
add_filter('preprocess_comment', [$this, 'antispam_comment']); |
| 27 |
} |
| 28 |
|
| 29 |
public function disable_comments() { |
| 30 |
add_action('widgets_init', [$this, 'disableRecentComments']); |
| 31 |
add_action('template_redirect', [$this, 'disableCommentsFeed'], 9); |
| 32 |
add_action('template_redirect', [$this, 'removeCommentAdminBar']); |
| 33 |
add_action('admin_init', [$this, 'removeCommentAdminBar']); |
| 34 |
add_action('wp_loaded', [$this, 'loadedDisableComments']); |
| 35 |
} |
| 36 |
|
| 37 |
public function cm_media() { |
| 38 |
add_filter('comments_open', [$this, 'filter_media_comment_status'], 10, 2); |
| 39 |
add_filter('manage_media_columns', [$this, 'hide_media_comments_column']); |
| 40 |
} |
| 41 |
|
| 42 |
public function antispam_blacklist() { |
| 43 |
remove_filter('comment_text', 'make_clickable', 9); |
| 44 |
} |
| 45 |
|
| 46 |
public function antispam_comment($comment_data) { |
| 47 |
$comment_content = $comment_data['comment_content'] ?? ''; |
| 48 |
if (strpos($comment_content, 'http://') !== false || strpos($comment_content, 'https://') !== false) { |
| 49 |
wp_die(esc_html__('Comments containing links or URLs are not allowed.', 'wp-extra'), esc_html__('Comment Blocked', 'wp-extra'), ['response' => 403, 'back_link' => true]); |
| 50 |
} |
| 51 |
return $comment_data; |
| 52 |
} |
| 53 |
|
| 54 |
public function disableRecentComments() { |
| 55 |
unregister_widget('WP_Widget_Recent_Comments'); |
| 56 |
add_filter('show_recent_comments_widget_style', '__return_false'); |
| 57 |
} |
| 58 |
|
| 59 |
public function disableCommentsFeed() { |
| 60 |
if (is_comment_feed()) { |
| 61 |
wp_die(esc_html__('Comments are closed.', 'wp-extra'), '', ['response' => 403]); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function removeCommentAdminBar() { |
| 66 |
if (is_admin_bar_showing()) { |
| 67 |
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public function loadedDisableComments() { |
| 72 |
$post_types = get_post_types(['public' => true], 'names'); |
| 73 |
if (!empty($post_types)) { |
| 74 |
foreach ($post_types as $post_type) { |
| 75 |
if (post_type_supports($post_type, 'comments')) { |
| 76 |
remove_post_type_support($post_type, 'comments'); |
| 77 |
remove_post_type_support($post_type, 'trackbacks'); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
add_filter('comments_array', '__return_empty_array', 20); |
| 83 |
add_filter('comments_open', '__return_false', 20); |
| 84 |
add_filter('pings_open', '__return_false', 20); |
| 85 |
|
| 86 |
if (is_admin()) { |
| 87 |
add_action('admin_menu', [$this, 'removeCommentsMenu'], 9999); |
| 88 |
add_action('admin_print_styles-index.php', [$this, 'hideDashboardComments']); |
| 89 |
add_action('admin_print_styles-profile.php', [$this, 'hideProfileComments']); |
| 90 |
add_action('wp_dashboard_setup', [$this, 'removeRecentCommentsMeta']); |
| 91 |
add_filter('pre_option_default_pingback_flag', '__return_zero'); |
| 92 |
} else { |
| 93 |
add_filter('comments_template', [$this, 'BlankCommentsTemplate'], 20); |
| 94 |
wp_deregister_script('comment-reply'); |
| 95 |
add_filter('feed_links_show_comments_feed', '__return_false'); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public function removeCommentsMenu() { |
| 100 |
global $pagenow; |
| 101 |
remove_menu_page('edit-comments.php'); |
| 102 |
remove_submenu_page('options-general.php', 'options-discussion.php'); |
| 103 |
if ($pagenow === 'comment.php' || $pagenow === 'edit-comments.php' || $pagenow === 'options-discussion.php') { |
| 104 |
wp_die(esc_html__('Comments are closed.', 'wp-extra'), '', ['response' => 403]); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
public function hideDashboardComments() { |
| 109 |
echo '<style>#dashboard_right_now .comment-count,#dashboard_right_now .comment-mod-count,#latest-comments,#welcome-panel .welcome-comments{display:none!important;}</style>'; |
| 110 |
} |
| 111 |
|
| 112 |
public function hideProfileComments() { |
| 113 |
echo '<style>.user-comment-shortcuts-wrap{display:none!important;}</style>'; |
| 114 |
} |
| 115 |
|
| 116 |
public function removeRecentCommentsMeta() { |
| 117 |
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal'); |
| 118 |
} |
| 119 |
|
| 120 |
public function BlankCommentsTemplate() { |
| 121 |
return __DIR__ . '/empty-comments.php'; |
| 122 |
} |
| 123 |
|
| 124 |
public function filter_media_comment_status($open, $post_id) { |
| 125 |
$post = get_post($post_id); |
| 126 |
if ($post && $post->post_type === 'attachment') { |
| 127 |
return false; |
| 128 |
} |
| 129 |
return $open; |
| 130 |
} |
| 131 |
|
| 132 |
public function hide_media_comments_column($columns) { |
| 133 |
if (isset($columns['comments'])) { |
| 134 |
unset($columns['comments']); |
| 135 |
} |
| 136 |
return $columns; |
| 137 |
} |
| 138 |
|
| 139 |
} |