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