| 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')) exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* WPAdminify |
| 17 |
* @package Module: Disable Comments |
| 18 |
* |
| 19 |
* @author Jewel Theme <support@jeweltheme.com> |
| 20 |
*/ |
| 21 |
|
| 22 |
class DisableComments extends AdminSettingsModel |
| 23 |
{ |
| 24 |
|
| 25 |
public $url; |
| 26 |
public function __construct() |
| 27 |
{ |
| 28 |
$this->url = WP_ADMINIFY_URL . 'Inc/Modules/DisableComments'; |
| 29 |
|
| 30 |
$this->options = (array) AdminSettings::get_instance()->get(); |
| 31 |
|
| 32 |
// Disable Comments |
| 33 |
add_action('admin_init', [$this, 'jltma_adminify_disable_comments_post_type_support']); |
| 34 |
|
| 35 |
add_action('admin_init', array($this, 'jltma_adminify_disable_comments_dashboard_widget')); |
| 36 |
add_action('admin_init', array($this, 'jltma_adminify_disable_comments_admin_menu_redirect')); |
| 37 |
add_action('admin_menu', [$this, 'jltma_adminify_disable_comments_menu']); |
| 38 |
add_action('admin_menu', [$this, 'jltma_adminify_disable_discussion_menu']); |
| 39 |
|
| 40 |
// Close comments on the front-end |
| 41 |
if (!empty($this->options['disable_comments_close_front'])) { |
| 42 |
add_filter('comments_open', '__return_false', 20, 2); |
| 43 |
add_filter('pings_open', '__return_false', 20, 2); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
// Remove comments links from admin bar |
| 48 |
// add_action('init', [$this, 'jltma_adminify_disable_comments_admin_bar_menu']); |
| 49 |
add_action('wp_before_admin_bar_render', [$this, 'jltma_adminify_remove_admin_bar_menus'], 0); |
| 50 |
add_filter('comment_form_default_fields', [$this, 'remove_url_field_comment_form']); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
/** Redirect any user trying to access comments page. */ |
| 56 |
public function jltma_adminify_disable_comments_admin_menu_redirect() |
| 57 |
{ |
| 58 |
if (!empty($this->options['disable_comments_menu_redirect'])) { |
| 59 |
// Redirect any user trying to access comments page |
| 60 |
global $pagenow; |
| 61 |
if ($pagenow === 'edit-comments.php' || $pagenow === 'options-discussion.php') { |
| 62 |
wp_safe_redirect(admin_url()); |
| 63 |
exit; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/* Remove from the administration bar */ |
| 69 |
public function jltma_adminify_remove_admin_bar_menus() |
| 70 |
{ |
| 71 |
if (!empty($this->options['disable_comments_admin_bar'])) { |
| 72 |
global $wp_admin_bar; |
| 73 |
$wp_admin_bar->remove_menu('comments'); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Remove comments page in menu |
| 78 |
public function jltma_adminify_disable_comments_menu() |
| 79 |
{ |
| 80 |
if (!empty($this->options['disable_comments_admin_menu'])) { |
| 81 |
remove_menu_page('edit-comments.php'); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// Remove "Discussion" submenu from Settings menu |
| 86 |
public function jltma_adminify_disable_discussion_menu() |
| 87 |
{ |
| 88 |
if (!empty($this->options['disable_comments_discussion_menu'])) { |
| 89 |
remove_submenu_page('options-general.php', 'options-discussion.php'); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
// Remove Comments Menu |
| 95 |
public function jltma_adminify_disable_comments_admin_bar_menu() |
| 96 |
{ |
| 97 |
if (is_admin_bar_showing()) { |
| 98 |
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
public function jltma_adminify_disable_comments_post_type_support() |
| 104 |
{ |
| 105 |
if (!empty($this->options['disable_comments_post_types'])) { |
| 106 |
$post_types = $this->options['disable_comments_post_types']; |
| 107 |
} else { |
| 108 |
$post_types = get_post_types(); |
| 109 |
} |
| 110 |
|
| 111 |
// Disable support for comments and trackbacks in post types |
| 112 |
foreach ($post_types as $post_type) { |
| 113 |
if (post_type_supports($post_type, 'comments')) { |
| 114 |
remove_post_type_support($post_type, 'comments'); |
| 115 |
remove_post_type_support($post_type, 'trackbacks'); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// Remove comments metabox from dashboard |
| 121 |
public function jltma_adminify_disable_comments_dashboard_widget() |
| 122 |
{ |
| 123 |
if (!empty($this->options['disable_comments_dashboard_widget'])) { |
| 124 |
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal'); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
/** |
| 130 |
* Remove url field from comment form |
| 131 |
* |
| 132 |
* @param $fields |
| 133 |
* |
| 134 |
* @return mixed |
| 135 |
*/ |
| 136 |
|
| 137 |
public function remove_url_field_comment_form($fields) |
| 138 |
{ |
| 139 |
if (!empty($this->options['disable_comments_url_field'])) { |
| 140 |
if (isset($fields['url'])) { |
| 141 |
unset($fields['url']); |
| 142 |
} |
| 143 |
} |
| 144 |
return $fields; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
/** |
| 150 |
* Convert links into span pseudo links |
| 151 |
* |
| 152 |
* @param $text |
| 153 |
* |
| 154 |
* @return mixed |
| 155 |
*/ |
| 156 |
|
| 157 |
public function convert_to_pheudo($text) |
| 158 |
{ |
| 159 |
return preg_replace_callback('/<a[^>]+href=[\'"](https?:\/\/[^"\']+)[\'"][^>]+>(.*?)<\/a>/i', [$this, 'jltwp_adminify_links_replace'], $text); |
| 160 |
} |
| 161 |
|
| 162 |
public function jltwp_adminify_links_replace($matches) |
| 163 |
{ |
| 164 |
if ($matches[1] == get_home_url()) { |
| 165 |
return $matches[0]; |
| 166 |
} |
| 167 |
|
| 168 |
return '<span class="wp-adminify-author-link-to-data-uri" data-adminify-comment-uri="' . $matches[1] . '" > ' . $matches[2] . '</span>'; |
| 169 |
} |
| 170 |
} |
| 171 |
|