disable-comments-rb
Last commit date
assets
9 months ago
languages
9 months ago
classDisableComments.php
9 months ago
disable-comments-rb.php
9 months ago
information.php
9 months ago
options.php
9 months ago
readme.txt
9 months ago
screenshot-1.png
9 months ago
screenshot-2.png
9 months ago
classDisableComments.php
285 lines
| 1 | <?php |
| 2 | /* |
| 3 | * RB Disable Comments |
| 4 | * Version: 1.0.9 - 38451 |
| 5 | * Author: RBS |
| 6 | * Date: 03 02 2020 12:11:29 GMT |
| 7 | */ |
| 8 | |
| 9 | if( !defined('WPINC') || !defined("ABSPATH") ){ |
| 10 | die(); |
| 11 | } |
| 12 | |
| 13 | |
| 14 | class classDisableComments { |
| 15 | |
| 16 | private static $instance = null; |
| 17 | |
| 18 | private $options; |
| 19 | private $options_name = 'rb_disable_comments'; |
| 20 | private $modified_types = array(); |
| 21 | |
| 22 | public static function get_instance() { |
| 23 | if ( is_null( self::$instance ) ) { |
| 24 | self::$instance = new self; |
| 25 | } |
| 26 | return self::$instance; |
| 27 | } |
| 28 | |
| 29 | public function __construct() { |
| 30 | $this->options = get_option( $this->options_name , array() ); |
| 31 | $this->hooks(); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | |
| 36 | private function save_options() { |
| 37 | update_option( $this->options_name, $this->options ); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | private function get_disabled_post_types() { |
| 42 | $types = ''; |
| 43 | if(isset($this->options['disabled_post_types']) && $this->options['disabled_post_types'] ){ |
| 44 | $types = $this->options['disabled_post_types']; |
| 45 | } |
| 46 | return $types; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | private function hooks() { |
| 51 | if( isset($this->options['remove_everywhere']) && $this->options['remove_everywhere'] ) { |
| 52 | add_action( 'widgets_init', array( $this, 'disable_rc_widget' ) ); |
| 53 | add_filter( 'wp_headers', array( $this, 'filter_wp_headers' ) ); |
| 54 | add_action( 'template_redirect', array( $this, 'filter_query' ), 9 ); |
| 55 | |
| 56 | add_action( 'template_redirect', array( $this, 'filter_admin_bar' ) ); |
| 57 | add_action( 'admin_init', array( $this, 'filter_admin_bar' ) ); |
| 58 | } |
| 59 | add_action( 'plugins_loaded', array( $this, 'register_text_domain' ) ); |
| 60 | add_action( 'wp_loaded', array( $this, 'wp_load_hooks' ) ); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | public function register_text_domain() { |
| 65 | load_plugin_textdomain( 'disable-comments-rb', false, RB_DISABLE_COMMENTS_PATH . 'languages' ); |
| 66 | } |
| 67 | |
| 68 | public function wp_load_hooks(){ |
| 69 | |
| 70 | $this->install_plugin(); |
| 71 | |
| 72 | $disabled_post_types = $this->get_disabled_post_types(); |
| 73 | |
| 74 | if( !empty( $disabled_post_types ) ) { |
| 75 | foreach( $disabled_post_types as $type ) { |
| 76 | if( post_type_supports( $type, 'comments' ) ) { |
| 77 | $this->modified_types[] = $type; |
| 78 | remove_post_type_support( $type, 'comments' ); |
| 79 | remove_post_type_support( $type, 'trackbacks' ); |
| 80 | } |
| 81 | } |
| 82 | add_filter( 'comments_array', array( $this, 'filter_existing_comments' ), 20, 2 ); |
| 83 | add_filter( 'comments_open', array( $this, 'filter_comment_status' ), 20, 2 ); |
| 84 | add_filter( 'pings_open', array( $this, 'filter_comment_status' ), 20, 2 ); |
| 85 | } |
| 86 | elseif( is_admin() ) { |
| 87 | add_action( 'all_admin_notices', array( $this, 'setup_notice' ) ); |
| 88 | } |
| 89 | |
| 90 | if( is_admin() ) { |
| 91 | |
| 92 | add_action( 'admin_menu', array( $this, 'settingsMenu' ) ); |
| 93 | add_filter( 'plugin_action_links', array( $this, 'plugin_actions_links'), 10, 2 ); |
| 94 | |
| 95 | if( isset($this->options['remove_everywhere']) && $this->options['remove_everywhere'] ) { |
| 96 | add_action( 'admin_menu', array( $this, 'filter_admin_menu' ), 9999 ); |
| 97 | add_action( 'admin_print_footer_scripts-index.php', array( $this, 'dashboard_js' ) ); |
| 98 | add_action( 'wp_dashboard_setup', array( $this, 'filter_dashboard' ) ); |
| 99 | add_filter( 'pre_option_default_pingback_flag', '__return_zero' ); |
| 100 | } |
| 101 | } else { |
| 102 | add_action( 'template_redirect', array( $this, 'check_comment_template' ) ); |
| 103 | |
| 104 | if( isset($this->options['remove_everywhere']) && $this->options['remove_everywhere'] ) { |
| 105 | add_filter( 'feed_links_show_comments_feed', '__return_false' ); |
| 106 | add_action( 'wp_footer', array( $this, 'hide_meta_widget_link' ), 100 ); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public function check_comment_template() { |
| 112 | if( is_singular() && ( isset($this->options['remove_everywhere']) && $this->options['remove_everywhere'] ) ) { |
| 113 | wp_deregister_script( 'comment-reply' ); |
| 114 | remove_action( 'wp_head', 'feed_links_extra', 3 ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public function empty_template_for_comments() { return ''; } |
| 119 | |
| 120 | |
| 121 | public function filter_wp_headers( $headers ) { |
| 122 | unset( $headers['X-Pingback'] ); |
| 123 | return $headers; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | public function filter_query() { |
| 128 | if( is_comment_feed() ) { |
| 129 | wp_die( __( 'Comments are closed.' ), '', array( 'response' => 403 ) ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | public function filter_admin_bar() { |
| 134 | if( is_admin_bar_showing() ) { |
| 135 | remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 ); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | |
| 140 | private function settings_page_url() { |
| 141 | return add_query_arg( 'page', 'rb_disable_comments_settings', admin_url( 'options-general.php' ) ); |
| 142 | //return add_query_arg('page', 'rb_disable_comments_settings', admin_url('admin.php')); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | public function setup_notice(){ |
| 147 | if( strpos( get_current_screen()->id, 'settings_page_rb_disable_comments_settings' ) === 0 ) |
| 148 | return; |
| 149 | |
| 150 | if( strpos( get_current_screen()->id, 'rb-plugins_page_rb_disable_comments_settings' ) === 0 ) |
| 151 | return; |
| 152 | |
| 153 | $hascaps = current_user_can( 'manage_options' ); |
| 154 | if( $hascaps ) { |
| 155 | echo '<div class="updated fade"><p>' . sprintf( __( 'The <em>Disable Comments</em> plugin is active, but isn\'t configured to do anything yet. Visit the <a href="%s">configuration page</a> to choose which post types to disable comments on.', 'disable-comments-rb'), esc_attr( $this->settings_page_url() ) ) . '</p></div>'; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | |
| 160 | public function filter_admin_menu(){ |
| 161 | global $pagenow; |
| 162 | |
| 163 | if ( $pagenow == 'comment.php' || $pagenow == 'edit-comments.php' || $pagenow == 'options-discussion.php' ) |
| 164 | wp_die( __( 'Comments are closed.' ), '', array( 'response' => 403 ) ); |
| 165 | |
| 166 | remove_menu_page( 'edit-comments.php' ); |
| 167 | remove_submenu_page( 'options-general.php', 'options-discussion.php' ); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | public function filter_dashboard(){ |
| 172 | remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' ); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | public function dashboard_js(){ |
| 177 | echo '<script> |
| 178 | jQuery(function($){ |
| 179 | $("#dashboard_right_now .comment-count, #latest-comments").hide(); |
| 180 | $("#welcome-panel .welcome-comments").parent().hide(); |
| 181 | }); |
| 182 | </script>'; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | public function hide_meta_widget_link(){ |
| 187 | if ( is_active_widget( false, false, 'meta', true ) && wp_script_is( 'jquery', 'enqueued' ) ) { |
| 188 | echo '<script> jQuery(function($){ $(".widget_meta a[href=\'' . esc_url( get_bloginfo( 'comments_rss2_url' ) ) . '\']").parent().remove(); }); </script>'; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
| 193 | public function filter_existing_comments($comments, $post_id) { |
| 194 | $post = get_post( $post_id ); |
| 195 | return ( $this->options['remove_everywhere'] ) ? array() : $comments; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | public function filter_comment_status( $open, $post_id ) { |
| 200 | $post = get_post( $post_id ); |
| 201 | return ( $this->options['remove_everywhere'] ) ? false : $open; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | public function disable_rc_widget() { |
| 206 | unregister_widget( 'WP_Widget_Recent_Comments' ); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | public function plugin_actions_links( $links, $file ) { |
| 211 | static $plugin; |
| 212 | |
| 213 | if( $file == 'disable-comments-rb/disable-comments-rb.php' && current_user_can('manage_options') ) { |
| 214 | array_unshift( |
| 215 | $links, |
| 216 | sprintf( '<a href="%s">%s</a>', esc_attr( $this->settings_page_url() ), __( 'Settings' ) ) |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | return $links; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | public function pluginsListPage() |
| 225 | { |
| 226 | echo "empty page"; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | public function settingsMenu() |
| 231 | { |
| 232 | /*$menu_exits = menu_page_url( 'rb_plugins_settings', false ); |
| 233 | if(!$menu_exits){ |
| 234 | $title_plugins = __('RB Plugins', 'disable-right-click-rb'); |
| 235 | add_menu_page( $title_plugins, $title_plugins, null, 'rb_plugins_settings', array($this, 'pluginsListPage'), 'dashicons-rest-api', 20); |
| 236 | add_action('admin_head', array($this, 'addAdminCss')); |
| 237 | }*/ |
| 238 | |
| 239 | $title = __('Disable Comments RB', 'disable-comments-rb'); |
| 240 | add_submenu_page('options-general.php', $title, $title, 'manage_options', 'rb_disable_comments_settings', array($this, 'options')); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | public function addAdminCss() |
| 245 | { |
| 246 | echo '<style> |
| 247 | #adminmenu .toplevel_page_rb_plugins_settings > .wp-menu-image.dashicons-rest-api:before{ |
| 248 | color: #99d000; |
| 249 | } |
| 250 | </style>'; |
| 251 | } |
| 252 | |
| 253 | |
| 254 | |
| 255 | public function options() { |
| 256 | include( RB_DISABLE_COMMENTS_PATH .'information.php'); |
| 257 | include( RB_DISABLE_COMMENTS_PATH .'options.php'); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | public function install_plugin() { |
| 262 | if( |
| 263 | isset($_GET['rb_forms_install_plugin']) && |
| 264 | $_GET['rb_forms_install_plugin'] |
| 265 | ){ |
| 266 | //$action = 'activate'; |
| 267 | $action = 'install-plugin'; |
| 268 | $plugin = 'forms-rb'; |
| 269 | $url = sprintf( admin_url( 'plugins.php?action=%s&plugin=%s&plugin_status=all&paged=1&s' ), $action, $plugin ); |
| 270 | $_REQUEST['plugin'] = $plugin; |
| 271 | $url = wp_nonce_url( $url, $action . '-plugin_' . $plugin ); |
| 272 | echo $url; |
| 273 | //return $url; |
| 274 | //sanitize_key( $_GET['rb_forms_install_plugin'] ) |
| 275 | |
| 276 | |
| 277 | echo esc_url( |
| 278 | wp_nonce_url( |
| 279 | self_admin_url('update.php?action=install-plugin&plugin=' . $plugin), |
| 280 | 'install-plugin_' . $plugin) |
| 281 | ); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | } |