| 1 |
<?php |
| 2 |
|
| 3 |
namespace PXLBSAdminify\Inc\Modules\DisableComments; |
| 4 |
|
| 5 |
use PXLBSAdminify\Inc\Admin\AdminSettings; |
| 6 |
use PXLBSAdminify\Inc\Admin\AdminSettingsModel; |
| 7 |
|
| 8 |
|
| 9 |
// no direct access allowed |
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* PXLBSAdminify |
| 16 |
* |
| 17 |
* @package Module: Disable Comments |
| 18 |
* |
| 19 |
* @author Jewel Theme <support@jeweltheme.com> |
| 20 |
*/ |
| 21 |
|
| 22 |
class DisableComments extends AdminSettingsModel { |
| 23 |
|
| 24 |
public $options; |
| 25 |
public $disable_comments; |
| 26 |
|
| 27 |
public function __construct() { |
| 28 |
$this->options = (array) AdminSettings::get_instance()->get(); |
| 29 |
$this->disable_comments = $this->options['disable_comments']; |
| 30 |
|
| 31 |
|
| 32 |
if (!empty($this->disable_comments['disable_comments']['enable_disable_comments'])) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// Disable Comments |
| 37 |
if(!empty($this->disable_comments['post_types'])){ |
| 38 |
add_action( 'admin_init', [ $this, 'disable_comments_settings' ] ); // Supported Hooks: 'init', 'admin_init', 'wp_loaded', 'do_meta_boxes' |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
// Close comments on the front-end |
| 43 |
if (!empty($this->disable_comments['apply_for']) && in_array('close_front', $this->disable_comments['apply_for'])) { |
| 44 |
add_filter( 'comments_open', '__return_false', 20, 2 ); |
| 45 |
add_filter( 'pings_open', '__return_false', 20, 2 ); |
| 46 |
} |
| 47 |
|
| 48 |
// Remove comment notes |
| 49 |
if (!empty($this->disable_comments['apply_for']) && in_array('comments_notes', $this->disable_comments['apply_for'])) { |
| 50 |
add_filter('comment_form_defaults', [$this, 'remove_comments_notes']); |
| 51 |
} |
| 52 |
|
| 53 |
// Remove comments links from admin bar |
| 54 |
if (!empty($this->disable_comments['apply_for']) && in_array('admin_bar', $this->disable_comments['apply_for'])) { |
| 55 |
// add_action('init', [$this, 'disable_comments_admin_bar_menu']); |
| 56 |
add_action( 'wp_before_admin_bar_render', [ $this, 'remove_admin_bar_menus' ], 0 ); |
| 57 |
} |
| 58 |
|
| 59 |
// URL field remove from Comments |
| 60 |
if (!empty($this->disable_comments['apply_for']) && in_array('comments_url_field', $this->disable_comments['apply_for'])) { |
| 61 |
add_filter( 'comment_form_default_fields', [ $this, |
| 62 |
'remove_url_field_comment_form' ] ); |
| 63 |
} |
| 64 |
|
| 65 |
// Remove comments page in menu |
| 66 |
if (!empty($this->disable_comments['apply_for']) && in_array('admin_menu', $this->disable_comments['apply_for'])) { |
| 67 |
add_action( 'admin_menu', [ $this, 'remove_comments_admin_menu' ] ); |
| 68 |
} |
| 69 |
|
| 70 |
// Remove "Discussion" submenu from Settings menu |
| 71 |
if (!empty($this->disable_comments['apply_for']) && in_array('discussion_menu', $this->disable_comments['apply_for'])) { |
| 72 |
add_action( 'admin_menu', [$this, 'remove_discussion_submenu'] ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Remove Discussion Submenu |
| 78 |
*/ |
| 79 |
public function remove_discussion_submenu() { |
| 80 |
remove_submenu_page('options-general.php', 'options-discussion.php'); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Remove Comments Menu |
| 85 |
*/ |
| 86 |
public function remove_comments_admin_menu(){ |
| 87 |
remove_menu_page('edit-comments.php'); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Disable Comments Settings |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public function disable_comments_settings() { |
| 96 |
|
| 97 |
// Disable support for comments and trackbacks in post types |
| 98 |
$post_types = $this->disable_comments['post_types']; |
| 99 |
foreach ($post_types as $post_type) { |
| 100 |
if (post_type_supports($post_type, 'comments')) { |
| 101 |
remove_post_type_support($post_type, 'comments'); |
| 102 |
remove_post_type_support($post_type, 'trackbacks'); |
| 103 |
\remove_meta_box('commentstatusdiv', $post_type, 'normal'); |
| 104 |
\remove_meta_box('commentstatusdiv', $post_type, 'side'); |
| 105 |
remove_meta_box('commentsdiv', $post_type, 'normal'); |
| 106 |
remove_meta_box('commentsdiv', $post_type, 'side'); |
| 107 |
remove_meta_box('trackbacksdiv', $post_type, 'normal'); |
| 108 |
remove_meta_box('trackbacksdiv', $post_type, 'side'); |
| 109 |
// wp_dequeue_script('admin-comments'); // edit-comments.js |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
// Remove styles for .recentcomments |
| 116 |
if (!empty($this->disable_comments['apply_for']) && in_array('recentcomments', $this->disable_comments['apply_for'])) { |
| 117 |
add_action('widgets_init', [$this, 'remove_recent_comments_style']); |
| 118 |
add_filter( 'show_recent_comments_widget_style', '__return_false' ); |
| 119 |
} |
| 120 |
|
| 121 |
// Redirect any user trying to access comments page. |
| 122 |
if (!empty($this->disable_comments['apply_for']) && in_array('menu_redirect', $this->disable_comments['apply_for'])) { |
| 123 |
global $pagenow; |
| 124 |
if ($pagenow === 'edit-comments.php') { |
| 125 |
wp_safe_redirect(admin_url()); |
| 126 |
exit; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// Updated Options |
| 131 |
add_filter('xmlrpc_allow_anonymous_comments', '__return_false'); |
| 132 |
add_filter('xmlrpc_methods', [$this, 'disable_xmlrpc_comments']); |
| 133 |
add_filter('rest_endpoints', [$this, 'disable_rest_api_comments_endpoints']); |
| 134 |
add_filter('rest_pre_insert_comment', [$this, 'return_blank_comment'], 10, 2); |
| 135 |
} |
| 136 |
|
| 137 |
// Remove styles for .recentcomments |
| 138 |
public function remove_recent_comments_style() |
| 139 |
{ |
| 140 |
global $wp_widget_factory; |
| 141 |
remove_action('wp_head', [$wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style']); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
/** |
| 146 |
* Return blank comment before inserting to DB |
| 147 |
* |
| 148 |
* @link https://plugins.trac.wordpress.org/browser/disable-comments/tags/2.4.5/disable-comments.php |
| 149 |
*/ |
| 150 |
public function return_blank_comment($prepared_comment, $request) |
| 151 |
{ |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
/** |
| 157 |
* Disables comments endpoint in REST API |
| 158 |
* |
| 159 |
* @link https://plugins.trac.wordpress.org/browser/disable-comments/tags/2.4.5/disable-comments.php |
| 160 |
*/ |
| 161 |
public function disable_rest_api_comments_endpoints($endpoints) |
| 162 |
{ |
| 163 |
if (isset($endpoints['comments'])) { |
| 164 |
unset($endpoints['comments']); |
| 165 |
} |
| 166 |
|
| 167 |
if (isset($endpoints['/wp/v2/comments'])) { |
| 168 |
unset($endpoints['/wp/v2/comments']); |
| 169 |
} |
| 170 |
|
| 171 |
if (isset($endpoints['/wp/v2/comments/(?P<id>[\d]+)'])) { |
| 172 |
unset($endpoints['/wp/v2/comments/(?P<id>[\d]+)']); |
| 173 |
} |
| 174 |
|
| 175 |
return $endpoints; |
| 176 |
} |
| 177 |
|
| 178 |
public function disable_xmlrpc_comments($methods) |
| 179 |
{ |
| 180 |
unset($methods['wp.newComment']); |
| 181 |
return $methods; |
| 182 |
} |
| 183 |
|
| 184 |
// Remove Comment Notes |
| 185 |
public function remove_comments_notes($defaults){ |
| 186 |
$defaults['comment_notes_before'] = ''; |
| 187 |
|
| 188 |
return $defaults; |
| 189 |
} |
| 190 |
|
| 191 |
/* Remove from the administration bar */ |
| 192 |
public function remove_admin_bar_menus() { |
| 193 |
global $wp_admin_bar; |
| 194 |
$wp_admin_bar->remove_menu( 'comments' ); |
| 195 |
} |
| 196 |
|
| 197 |
// Remove Comments Menu |
| 198 |
public function disable_comments_admin_bar_menu() { |
| 199 |
if ( is_admin_bar_showing() ) { |
| 200 |
remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 ); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
/** |
| 206 |
* Remove url field from comment form |
| 207 |
* |
| 208 |
* @param $fields |
| 209 |
* |
| 210 |
* @return mixed |
| 211 |
*/ |
| 212 |
|
| 213 |
public function remove_url_field_comment_form( $fields ) { |
| 214 |
|
| 215 |
if ( isset( $fields['url'] ) ) { |
| 216 |
unset( $fields['url'] ); |
| 217 |
} |
| 218 |
return $fields; |
| 219 |
} |
| 220 |
} |
| 221 |
|