PluginProbe
WP Coder – Insert & Manage Code Snippets / 4.2
WP Coder – Insert & Manage Code Snippets v4.2
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / includes / snippets / class-snippet-disabled.php

class-snippet-disabled.php in WP Coder – Insert & Manage Code Snippets 4.2, at includes/snippets/class-snippet-disabled.php

360 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || exit;
4
5 class WPCoder_Lite_Disabled_Snippets {
6
7 /**
8 * @var false|mixed|null
9 */
10 private $options;
11
12 public function __construct() {
13 $options = get_option( '_wp_coder_snippets', [] );
14 $this->options = $options;
15
16 if ( array_key_exists( 'disable_gutenberg', $options ) ) {
17 add_filter( 'gutenberg_can_edit_post', '__return_false', 5 );
18 add_filter( 'use_block_editor_for_post', '__return_false', 5 );
19 }
20
21 if ( array_key_exists( 'disable_gutenberg_css', $options ) ) {
22 add_action( 'wp_enqueue_scripts', [ $this, 'remove_wp_block_library_css' ] );
23 }
24
25
26 if ( array_key_exists( 'disable_widget_blocks', $options ) ) {
27 // Disables the block editor from managing widgets in the Gutenberg plugin.
28 add_filter( 'gutenberg_use_widgets_block_editor', '__return_false' );
29 // Disables the block editor from managing widgets.
30 add_filter( 'use_widgets_block_editor', '__return_false' );
31 }
32
33 if ( array_key_exists( 'remove_wp_version', $options ) ) {
34 add_filter( 'the_generator', '__return_empty_string' );
35 }
36
37 if ( array_key_exists( 'disable_XML_RPC', $options ) ) {
38 add_filter( 'xmlrpc_enabled', '__return_false' );
39 }
40
41 if ( array_key_exists( 'disable_admin_bar', $options ) ) {
42 add_filter( 'show_admin_bar', [ $this, 'disable_admin_bar' ] );
43 }
44
45 if ( array_key_exists( 'disable_automatic_updates', $options ) ) {
46 // Disable core auto-updates
47 add_filter( 'auto_update_core', '__return_false' );
48 // Disable auto-updates for plugins.
49 add_filter( 'auto_update_plugin', '__return_false' );
50 // Disable auto-updates for themes.
51 add_filter( 'auto_update_theme', '__return_false' );
52 }
53
54 if ( array_key_exists( 'disable_automatic_updates_emails', $options ) ) {
55 // Disable auto-update emails.
56 add_filter( 'auto_core_update_send_email', '__return_false' );
57 // Disable auto-update emails for plugins.
58 add_filter( 'auto_plugin_update_send_email', '__return_false' );
59 // Disable auto-update emails for themes.
60 add_filter( 'auto_theme_update_send_email', '__return_false' );
61 }
62
63 if ( array_key_exists( 'disable_attachment_pages', $options ) ) {
64 add_action( 'template_redirect', [ $this, 'redirect_attachment_pages' ], 1 );
65 }
66
67 if ( array_key_exists( 'disable_rest_api', $options ) ) {
68 add_filter( 'rest_authentication_errors', [ $this, 'disable_rest_api' ] );
69 }
70
71 if ( array_key_exists( 'disable_comments', $options ) ) {
72 add_action( 'admin_init', [ $this, 'disable_comments' ] );
73 // Close comments on the front-end
74 add_filter( 'comments_open', '__return_false', 20, 2 );
75 add_filter( 'pings_open', '__return_false', 20, 2 );
76
77 // Hide existing comments
78 add_filter( 'comments_array', '__return_empty_array', 10, 2 );
79 // Remove comments page in menu
80 add_action( 'admin_menu', static function () {
81 remove_menu_page( 'edit-comments.php' );
82 } );
83 // Remove comments links from admin bar
84 add_action( 'admin_bar_menu', static function () {
85 remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
86 }, 0 );
87 }
88
89 if ( array_key_exists( 'disable_automatic_trash', $options ) ) {
90 add_action( 'init', static function () {
91 remove_action( 'wp_scheduled_delete', 'wp_scheduled_delete' );
92 } );
93 }
94
95 if ( array_key_exists( 'disable_emojis', $options ) ) {
96 add_action( 'init', [ $this, 'disable_emojis' ] );
97 }
98
99 if ( array_key_exists( 'disable_screen_options', $options ) ) {
100 add_filter( 'screen_options_show_screen', '__return_false' );
101 }
102
103 if ( array_key_exists( 'disable_welcome_panel', $options ) ) {
104 add_action( 'admin_init', static function () {
105 remove_action( 'welcome_panel', 'wp_welcome_panel' );
106 }
107 );
108 }
109
110 if ( array_key_exists( 'disable_rss_feeds', $options ) ) {
111 add_action( 'do_feed_rdf', [ $this, 'disable_feed' ], 1 );
112 add_action( 'do_feed_rss', [ $this, 'disable_feed' ], 1 );
113 add_action( 'do_feed_rss2', [ $this, 'disable_feed' ], 1 );
114 add_action( 'do_feed_atom', [ $this, 'disable_feed' ], 1 );
115 add_action( 'do_feed_rss2_comments', [ $this, 'disable_feed' ], 1 );
116 add_action( 'do_feed_atom_comments', [ $this, 'disable_feed' ], 1 );
117 remove_action( 'wp_head', 'feed_links_extra', 3 );
118 remove_action( 'wp_head', 'feed_links', 2 );
119 }
120
121 if ( array_key_exists( 'disable_search', $options ) ) {
122 add_action( 'parse_query', static function ( $query, $error = true ) {
123 if ( is_search() && ! is_admin() ) {
124 $query->is_search = false;
125 $query->query_vars['s'] = false;
126 $query->query['s'] = false;
127 if ( true === $error ) {
128 $query->is_404 = true;
129 }
130 }
131 }, 15, 2 );
132 // Remove the Search Widget.
133 add_action( 'widgets_init', static function () {
134 unregister_widget( 'WP_Widget_Search' );
135 }
136 );
137 // Remove the search form.
138 add_filter( 'get_search_form', '__return_empty_string', 999 );
139 // Remove the core search block.
140 add_action( 'init', static function () {
141 if ( ! function_exists( 'unregister_block_type' ) || ! class_exists( 'WP_Block_Type_Registry' ) ) {
142 return;
143 }
144 $block = 'core/search';
145 if ( WP_Block_Type_Registry::get_instance()->is_registered( $block ) ) {
146 unregister_block_type( $block );
147 }
148 } );
149 // Remove admin bar menu search box.
150 add_action( 'admin_bar_menu', static function ( $wp_admin_bar ) {
151 $wp_admin_bar->remove_menu( 'search' );
152 }, 11 );
153 }
154
155 if ( array_key_exists( 'disable_login_language_dropdown', $options ) ) {
156 add_filter( 'login_display_language_dropdown', '__return_false' );
157 }
158
159 if ( array_key_exists( 'disable_login_by_email', $options ) ) {
160 remove_filter( 'authenticate', 'wp_authenticate_email_password', 20 );
161 }
162
163 if ( array_key_exists( 'disable_comment_from_website_url', $options ) ) {
164 add_filter( 'comment_form_default_fields', [ $this, 'comment_from_website' ], 150 );
165 }
166
167 if ( array_key_exists( 'disable_self_pingbacks', $options ) ) {
168 add_action( 'pre_ping', [ $this, 'self_pingbacks' ] );
169 }
170
171 if ( array_key_exists( 'disable_wlwmanifest_link', $options ) ) {
172 remove_action( 'wp_head', 'wlwmanifest_link' );
173 }
174
175 if ( array_key_exists( 'disable_embeds', $options ) ) {
176 add_action( 'init', [ $this, 'disable_embeds' ], 9999 );
177 }
178
179 if ( array_key_exists( 'disable_lazy_load', $options ) ) {
180 add_filter( 'wp_lazy_loading_enabled', '__return_false' );
181 }
182
183 if ( array_key_exists( 'disable_wp_shortlink', $options ) ) {
184 remove_action( 'wp_head', 'wp_shortlink_wp_head' );
185 }
186
187 if ( array_key_exists( 'disable_admin_pass_reset_email', $options ) ) {
188 remove_action( 'after_password_reset', 'wp_password_change_notification' );
189 }
190
191 if ( array_key_exists( 'disable_trackbacks_pingbacks', $options ) ) {
192 add_filter('xmlrpc_methods', static function($methods) {
193 unset($methods['pingback.ping']);
194 return $methods;
195 });
196 }
197
198 if ( array_key_exists( 'disable_comments_html', $options ) ) {
199 remove_filter('comment_text', 'make_clickable', 9);
200 add_filter('pre_comment_content', 'wp_strip_all_tags');
201 }
202
203 }
204
205 public function disable_embeds(): void {
206 // Remove the REST API endpoint.
207 remove_action( 'rest_api_init', 'wp_oembed_register_route' );
208 // Turn off oEmbed auto discovery.
209 add_filter( 'embed_oembed_discover', '__return_false' );
210 // Don't filter oEmbed results.
211 remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
212 // Remove oEmbed discovery links.
213 remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
214 // Remove oEmbed-specific JavaScript from the front-end and back-end.
215 remove_action( 'wp_head', 'wp_oembed_add_host_js' );
216 add_filter( 'tiny_mce_plugins', static function ( $plugins ) {
217 return array_diff( $plugins, array( 'wpembed' ) );
218 } );
219 // Remove all embeds rewrite rules.
220 add_filter( 'rewrite_rules_array', static function ( $rules ) {
221 foreach ( $rules as $rule => $rewrite ) {
222 if ( false !== strpos( $rewrite, 'embed=true' ) ) {
223 unset( $rules[ $rule ] );
224 }
225 }
226
227 return $rules;
228 } );
229 // Remove filter of the oEmbed result before any HTTP requests are made.
230 remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 );
231 }
232
233 public function self_pingbacks( &$links ): void {
234
235 $home = get_option( 'home' );
236 foreach ( $links as $l => $link ) {
237 if ( 0 === strpos( $link, $home ) ) {
238 unset( $links[ $l ] );
239 }
240 }
241 }
242
243 public function comment_from_website( $fields ) {
244 if ( isset( $fields['url'] ) ) {
245 unset( $fields['url'] );
246 }
247
248 return $fields;
249 }
250
251 public function disable_feed(): void {
252 wp_die(
253 sprintf(
254 // Translators: Placeholders for the homepage link.
255 esc_html__( 'No feed available, please visit our %1$shomepage%2$s!', 'wp-coder' ),
256 ' <a href="' . esc_url( home_url( '/' ) ) . '">',
257 '</a>'
258 )
259 );
260 }
261
262 public function disable_emojis(): void {
263 remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
264 remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
265 remove_action( 'wp_print_styles', 'print_emoji_styles' );
266 remove_action( 'admin_print_styles', 'print_emoji_styles' );
267 remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
268 remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
269 remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
270
271 add_filter( 'tiny_mce_plugins', function ( $plugins ) {
272 if ( is_array( $plugins ) ) {
273 return array_diff( $plugins, array( 'wpemoji' ) );
274 } else {
275 return array();
276 }
277 } );
278
279 // Remove from dns-prefetch.
280 add_filter( 'wp_resource_hints', function ( $urls, $relation_type ) {
281 if ( 'dns-prefetch' === $relation_type ) {
282 $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
283 $urls = array_diff( $urls, array( $emoji_svg_url ) );
284 }
285
286 return $urls;
287 }, 10, 2 );
288 }
289
290 public function remove_wp_block_library_css(): void {
291 wp_dequeue_style( 'wp-block-library' );
292 wp_dequeue_style( 'wp-block-library-theme' );
293 }
294
295 public function disable_comments(): void {
296 global $pagenow;
297
298 if ( $pagenow === 'edit-comments.php' ) {
299 wp_safe_redirect( admin_url() );
300 exit;
301 }
302 // Remove comments metabox from dashboard
303 remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
304 // Disable support for comments and trackbacks in post types
305 foreach ( get_post_types() as $post_type ) {
306 if ( post_type_supports( $post_type, 'comments' ) ) {
307 remove_post_type_support( $post_type, 'comments' );
308 remove_post_type_support( $post_type, 'trackbacks' );
309 }
310 }
311 }
312
313 public function disable_rest_api( $access ): WP_Error {
314 return new WP_Error(
315 'rest_disabled',
316 'The WordPress REST API has been disabled.',
317 array(
318 'status' => rest_authorization_required_code(),
319 )
320 );
321 }
322
323 public function redirect_attachment_pages(): void {
324 global $post;
325 if ( ! is_attachment() || ! isset( $post->post_parent ) || ! is_numeric( $post->post_parent ) ) {
326 return;
327 }
328 // Does the attachment have a parent post?
329 // If the post is trashed, fallback to redirect to homepage.
330 if ( 0 !== $post->post_parent && 'trash' !== get_post_status( $post->post_parent ) ) {
331 // Redirect to the attachment parent.
332 wp_safe_redirect( get_permalink( $post->post_parent ), 301 );
333 } else {
334 // For attachment without a parent redirect to homepage.
335 wp_safe_redirect( get_bloginfo( 'wpurl' ), 302 );
336 }
337 exit;
338 }
339
340 public function disable_admin_bar( $show_admin_bar ): bool {
341 $admin_bar = false;
342
343 $user = wp_get_current_user();
344
345 if ( ! empty( $user->roles ) ) {
346 foreach ( $user->roles as $role ) {
347 if ( array_key_exists( 'disable_admin_bar_user_' . $role, $this->options ) ) {
348 $admin_bar = true;
349 break;
350 }
351 }
352 }
353
354 return $admin_bar;
355 }
356
357
358 }
359
360 new WPCoder_Lite_Disabled_Snippets;