|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Functionality specific to the admin |
| 4 | + * |
| 5 | + * @package Uncomment |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Uncomment\Admin; |
| 9 | + |
| 10 | +// Do not check for plugin updates. |
| 11 | +add_filter( 'site_transient_update_plugins', __NAMESPACE__ . '\remove_update_nag' ); |
| 12 | + |
| 13 | +// Remove admin pages. |
| 14 | +add_action( 'admin_init', __NAMESPACE__ . '\remove_admin_pages' ); |
| 15 | + |
| 16 | +// Remove admin menu items. |
| 17 | +add_action( 'admin_menu', __NAMESPACE__ . '\remove_menu_items' ); |
| 18 | + |
| 19 | +// Remove the commentsdiv meta box. |
| 20 | +add_action( 'admin_init', __NAMESPACE__ . '\remove_commentsdiv_meta_box' ); |
| 21 | + |
| 22 | +// Remove "Turn comments on or off" from the Welcome Panel. |
| 23 | +add_action( 'admin_footer-index.php', __NAMESPACE__ . '\remove_welcome_panel_item' ); |
| 24 | + |
| 25 | +// Remove Keyboard Shortcuts options from the profile page. |
| 26 | +add_action( 'personal_options', __NAMESPACE__ . '\remove_profile_items' ); |
| 27 | + |
| 28 | +// Remove the 'Discussion Settings' help tab from the post edit screen. |
| 29 | +add_action( 'admin_head-post.php', __NAMESPACE__ . '\remove_help_tabs', 10, 3 ); |
| 30 | + |
| 31 | +// Unregister the core comment widget. |
| 32 | +add_action( 'widgets_init', __NAMESPACE__ . '\unregister_comment_widget', 1 ); |
| 33 | + |
| 34 | +// Unregister the core comment widget styles. |
| 35 | +add_filter( 'show_recent_comments_widget_style', '__return_false' ); |
| 36 | + |
| 37 | +// Enqueue editor script. |
| 38 | +add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\remove_comments_block' ); |
| 39 | + |
| 40 | +/** |
| 41 | + * Do not check for plugin updates. |
| 42 | + * |
| 43 | + * @param array|string $value Value of site transient. |
| 44 | + * @return array|string |
| 45 | + */ |
| 46 | +function remove_update_nag( $value ) { |
| 47 | + |
| 48 | + if ( isset( $value->response[ plugin_basename( __FILE__ ) ] ) |
| 49 | + && ! empty( $value->response[ plugin_basename( __FILE__ ) ] ) |
| 50 | + ) { |
| 51 | + unset( $value->response[ plugin_basename( __FILE__ ) ] ); |
| 52 | + } |
| 53 | + |
| 54 | + return $value; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Remove admin pages. |
| 59 | + * |
| 60 | + * @return void |
| 61 | + */ |
| 62 | +function remove_admin_pages() { |
| 63 | + |
| 64 | + global $pagenow; |
| 65 | + |
| 66 | + if ( in_array( |
| 67 | + $pagenow, |
| 68 | + array( |
| 69 | + 'comment.php', |
| 70 | + 'edit-comments.php', |
| 71 | + 'moderation.php', |
| 72 | + 'options-discussion.php', |
| 73 | + ), |
| 74 | + true |
| 75 | + ) ) { |
| 76 | + wp_die( |
| 77 | + esc_html__( 'Comments are closed.', 'default' ), |
| 78 | + '', |
| 79 | + array( 'response' => 403 ) |
| 80 | + ); |
| 81 | + exit(); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Remove admin menu items. |
| 87 | + * |
| 88 | + * @return void |
| 89 | + */ |
| 90 | +function remove_menu_items() { |
| 91 | + |
| 92 | + remove_menu_page( 'edit-comments.php' ); |
| 93 | + remove_submenu_page( 'options-general.php', 'options-discussion.php' ); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Remove the commentsdiv meta box. |
| 98 | + * |
| 99 | + * Removing post type support for comments removes the commentstatusdiv |
| 100 | + * and trackbacksdiv, but does not affect commentsdiv as it might still |
| 101 | + * be registered if $post->comment_count is bigger than 0. |
| 102 | + * |
| 103 | + * @todo Find a better place than admin_init to run this hook. |
| 104 | + * |
| 105 | + * @return void |
| 106 | + */ |
| 107 | +function remove_commentsdiv_meta_box() { |
| 108 | + |
| 109 | + foreach ( get_post_types() as $post_type ) { |
| 110 | + remove_meta_box( 'commentsdiv', $post_type, 'normal' ); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Remove "Turn comments on or off" from the Welcome Panel. |
| 116 | + * |
| 117 | + * @return void |
| 118 | + */ |
| 119 | +function remove_welcome_panel_item() { |
| 120 | + |
| 121 | + ?> |
| 122 | + <script type="text/javascript"> |
| 123 | + //<![CDATA[ |
| 124 | + document.addEventListener( 'DOMContentLoaded', function() { |
| 125 | + var el = document.querySelector( '.welcome-comments' ); |
| 126 | + if ( 'undefined' !== typeof el ) { |
| 127 | + el.parentNode.remove(); |
| 128 | + } |
| 129 | + }); |
| 130 | + //]]> |
| 131 | + </script> |
| 132 | + <?php |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Remove Keyboard Shortcuts options from the profile page. |
| 137 | + * |
| 138 | + * @return void |
| 139 | + */ |
| 140 | +function remove_profile_items() { |
| 141 | + |
| 142 | + echo '<style type="text/css">.user-comment-shortcuts-wrap{display:none;}</style>'; |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Remove the 'Discussion Settings' help tab from the post edit screen. |
| 147 | + * |
| 148 | + * @return void |
| 149 | + */ |
| 150 | +function remove_help_tabs() { |
| 151 | + |
| 152 | + $current_screen = get_current_screen(); |
| 153 | + |
| 154 | + if ( $current_screen->get_help_tab( 'discussion-settings' ) ) { |
| 155 | + $current_screen->remove_help_tab( 'discussion-settings' ); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Unregister the core comment widget. |
| 161 | + * |
| 162 | + * @return void |
| 163 | + */ |
| 164 | +function unregister_comment_widget() { |
| 165 | + |
| 166 | + unregister_widget( 'WP_Widget_Recent_Comments' ); |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * Unregister the core recent comments block. |
| 171 | + * |
| 172 | + * @return void |
| 173 | + */ |
| 174 | +function remove_comments_block() { |
| 175 | + |
| 176 | + $script = <<<TAG |
| 177 | +(function(){ |
| 178 | + wp.hooks.addFilter('blocks.registerBlockType', 'uncomment/exclude-blocks', function(settings, name) { |
| 179 | + if ( 'core/latest-comments' === name ) { |
| 180 | + return Object.assign({}, settings, { |
| 181 | + supports: Object.assign({}, settings.supports, {inserter: false}) |
| 182 | + }); |
| 183 | + } |
| 184 | + return settings; |
| 185 | + }); |
| 186 | +})(); |
| 187 | +TAG; |
| 188 | + |
| 189 | + wp_add_inline_script( 'wp-blocks', $script, 'after' ); |
| 190 | +} |
0 commit comments