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