← All changes
|
Inc/Modules/DisableComments/DisableComments.php
+85
-144
4.2.13
→
3.2.4.1
View file →
| @@ -1,10 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace PXLBSAdminify\Inc\Modules\DisableComments; | |
| 3 | +namespace WPAdminify\Inc\Modules\DisableComments; | |
| 4 | 4 | |
| 5 | -use PXLBSAdminify\Inc\Admin\AdminSettings; | |
| 6 | -use PXLBSAdminify\Inc\Admin\AdminSettingsModel; | |
| 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; | |
| 7 | 10 | |
| 8 | 11 | |
| 9 | 12 | // no direct access allowed |
| 10 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -11,9 +14,9 @@ | ||
| 11 | 14 | exit; |
| 12 | 15 | } |
| 13 | 16 | |
| 14 | 17 | /** |
| 15 | - * PXLBSAdminify | |
| 18 | + * WPAdminify | |
| 16 | 19 | * |
| 17 | 20 | * @package Module: Disable Comments |
| 18 | 21 | * |
| 19 | 22 | * @author Jewel Theme <support@jeweltheme.com> |
| @@ -20,201 +23,139 @@ | ||
| 20 | 23 | */ |
| 21 | 24 | |
| 22 | 25 | class DisableComments extends AdminSettingsModel { |
| 23 | 26 | |
| 24 | - public $options; | |
| 25 | - public $disable_comments; | |
| 26 | 27 | |
| 28 | + public $url; | |
| 27 | 29 | public function __construct() { |
| 30 | + $this->url = WP_ADMINIFY_URL . 'Inc/Modules/DisableComments'; | |
| 31 | + | |
| 28 | 32 | $this->options = (array) AdminSettings::get_instance()->get(); |
| 29 | - $this->disable_comments = $this->options['disable_comments']; | |
| 30 | 33 | |
| 31 | - | |
| 32 | - if (!empty($this->disable_comments['disable_comments']['enable_disable_comments'])) { | |
| 33 | - return; | |
| 34 | - } | |
| 35 | - | |
| 36 | 34 | // 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 | - } | |
| 35 | + add_action( 'admin_init', [ $this, 'jltma_adminify_disable_comments_post_type_support' ] ); | |
| 40 | 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 | 41 | |
| 42 | 42 | // Close comments on the front-end |
| 43 | - if (!empty($this->disable_comments['apply_for']) && in_array('close_front', $this->disable_comments['apply_for'])) { | |
| 43 | + if ( ! empty( $this->options['disable_comments_close_front'] ) ) { | |
| 44 | 44 | add_filter( 'comments_open', '__return_false', 20, 2 ); |
| 45 | 45 | add_filter( 'pings_open', '__return_false', 20, 2 ); |
| 46 | 46 | } |
| 47 | 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 | - } | |
| 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 | + } | |
| 52 | 53 | |
| 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 | 54 | |
| 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 | 55 | |
| 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' ] ); | |
| 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 | + } | |
| 68 | 65 | } |
| 66 | + } | |
| 69 | 67 | |
| 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'] ); | |
| 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 | 73 | } |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - /** | |
| 77 | - * Remove Discussion Submenu | |
| 78 | - */ | |
| 79 | - public function remove_discussion_submenu() { | |
| 80 | - remove_submenu_page('options-general.php', 'options-discussion.php'); | |
| 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 | 81 | } |
| 82 | 82 | |
| 83 | - /** | |
| 84 | - * Remove Comments Menu | |
| 85 | - */ | |
| 86 | - public function remove_comments_admin_menu(){ | |
| 87 | - remove_menu_page('edit-comments.php'); | |
| 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 | 88 | } |
| 89 | 89 | |
| 90 | - /** | |
| 91 | - * Disable Comments Settings | |
| 92 | - * | |
| 93 | - * @return void | |
| 94 | - */ | |
| 95 | - public function disable_comments_settings() { | |
| 96 | 90 | |
| 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 | - } | |
| 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 ); | |
| 111 | 95 | } |
| 96 | + } | |
| 112 | 97 | |
| 113 | 98 | |
| 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' ); | |
| 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(); | |
| 119 | 104 | } |
| 120 | 105 | |
| 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; | |
| 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' ); | |
| 127 | 111 | } |
| 128 | 112 | } |
| 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 | 113 | } |
| 136 | 114 | |
| 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']); | |
| 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 | + } | |
| 142 | 120 | } |
| 143 | 121 | |
| 144 | 122 | |
| 145 | 123 | /** |
| 146 | - * Return blank comment before inserting to DB | |
| 124 | + * Remove url field from comment form | |
| 147 | 125 | * |
| 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 | |
| 126 | + * @param $fields | |
| 158 | 127 | * |
| 159 | - * @link https://plugins.trac.wordpress.org/browser/disable-comments/tags/2.4.5/disable-comments.php | |
| 128 | + * @return mixed | |
| 160 | 129 | */ |
| 161 | - public function disable_rest_api_comments_endpoints($endpoints) | |
| 162 | - { | |
| 163 | - if (isset($endpoints['comments'])) { | |
| 164 | - unset($endpoints['comments']); | |
| 165 | - } | |
| 166 | 130 | |
| 167 | - if (isset($endpoints['/wp/v2/comments'])) { | |
| 168 | - unset($endpoints['/wp/v2/comments']); | |
| 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 | + } | |
| 169 | 136 | } |
| 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; | |
| 137 | + return $fields; | |
| 176 | 138 | } |
| 177 | 139 | |
| 178 | - public function disable_xmlrpc_comments($methods) | |
| 179 | - { | |
| 180 | - unset($methods['wp.newComment']); | |
| 181 | - return $methods; | |
| 182 | - } | |
| 183 | 140 | |
| 184 | - // Remove Comment Notes | |
| 185 | - public function remove_comments_notes($defaults){ | |
| 186 | - $defaults['comment_notes_before'] = ''; | |
| 187 | 141 | |
| 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 | 142 | /** |
| 206 | - * Remove url field from comment form | |
| 143 | + * Convert links into span pseudo links | |
| 207 | 144 | * |
| 208 | - * @param $fields | |
| 145 | + * @param $text | |
| 209 | 146 | * |
| 210 | 147 | * @return mixed |
| 211 | 148 | */ |
| 212 | 149 | |
| 213 | - public function remove_url_field_comment_form( $fields ) { | |
| 150 | + public function convert_to_pheudo( $text ) { | |
| 151 | + return preg_replace_callback( '/<a[^>]+href=[\'"](https?:\/\/[^"\']+)[\'"][^>]+>(.*?)<\/a>/i', [ $this, 'jltwp_adminify_links_replace' ], $text ); | |
| 152 | + } | |
| 214 | 153 | |
| 215 | - if ( isset( $fields['url'] ) ) { | |
| 216 | - unset( $fields['url'] ); | |
| 154 | + public function jltwp_adminify_links_replace( $matches ) { | |
| 155 | + if ( $matches[1] == get_home_url() ) { | |
| 156 | + return $matches[0]; | |
| 217 | 157 | } |
| 218 | - return $fields; | |
| 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>'; | |
| 219 | 160 | } |
| 220 | 161 | } |