PluginProbe
Docket Cache – Object Cache Accelerator / 22.07.01
Docket Cache – Object Cache Accelerator v22.07.01
26.04.05 trunk 22.07.01 22.07.02 22.07.03 22.07.04 22.07.05 23.08.01 23.08.02 24.07.01 24.07.02 24.07.03 24.07.04 24.07.05 24.07.06 24.07.07 26.04.03 26.04.04
docket-cache / includes / src / Tweaks.php

Tweaks.php in Docket Cache – Object Cache Accelerator 22.07.01, at includes/src/Tweaks.php

942 lines 32.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Docket Cache.
4 *
5 * @author Nawawi Jamili
6 * @license MIT
7 *
8 * @see https://github.com/nawawi/docket-cache
9 */
10
11 namespace Nawawi\DocketCache;
12
13 \defined('ABSPATH') || exit;
14
15 final class Tweaks
16 {
17 public function wpquery()
18 {
19 // vipcom: prevent core from doing filename lookups for media search.
20 // https://core.trac.wordpress.org/ticket/39358
21 add_action(
22 'pre_get_posts',
23 function () {
24 remove_filter('posts_clauses', '_filter_query_attachment_filenames');
25 },
26 \PHP_INT_MAX
27 );
28
29 // vipcom: improve perfomance of the _WP_Editors::wp_link_query method
30 add_filter(
31 'wp_link_query_args',
32 function ($query) {
33 $query['no_found_rows'] = true;
34
35 return $query;
36 },
37 \PHP_INT_MAX
38 );
39
40 // vipcom: disable custom fields meta box dropdown (very slow)
41 add_filter('postmeta_form_keys', '__return_false');
42 }
43
44 public function misc()
45 {
46 // wp: if only one post is found by the search results, redirect user to that post
47 add_action(
48 'template_redirect',
49 function () {
50 if (is_search()) {
51 global $wp_query;
52 if (1 === (int) $wp_query->post_count && 1 === (int) $wp_query->max_num_pages) {
53 wp_redirect(get_permalink($wp_query->posts['0']->ID));
54 exit;
55 }
56 }
57 },
58 \PHP_INT_MAX
59 );
60
61 // wp: hide update notifications to non-admin users
62 add_action(
63 'admin_head',
64 function () {
65 if (!current_user_can('update_core')) {
66 remove_action('admin_notices', 'update_nag', 3);
67 }
68 },
69 \PHP_INT_MAX
70 );
71
72 // jetpack: enables object caching for the response sent by instagram when querying for instagram image html
73 // https://developer.jetpack.com/hooks/instagram_cache_oembed_api_response_body/
74 // Removed in Jetpack 9.1.0
75 //add_filter('instagram_cache_oembed_api_response_body', '__return_true');
76
77 if (nwdcx_consfalse('TWEAKS_WPCOOKIE_DISABLED')) {
78 // wp: comment cookie lifetime, default to 30000000 second = 12 months
79 add_filter(
80 'comment_cookie_lifetime',
81 function () {
82 return 12 * HOUR_IN_SECONDS;
83 },
84 \PHP_INT_MIN
85 );
86
87 // wp: protected post, expire when browser close
88 add_filter(
89 'post_password_expires',
90 function () {
91 return 0;
92 },
93 \PHP_INT_MIN
94 );
95 }
96
97 if (nwdcx_consfalse('TWEAKS_WPLOGIN_TRANSLATIONAPI_DISABLED')) {
98 add_action(
99 'init',
100 function () {
101 add_filter(
102 'translations_api',
103 function ($type, $args) {
104 if (false !== strpos($_SERVER['REQUEST_URI'], '/wp-login.php')) {
105 return true;
106 }
107
108 return false;
109 },
110 \PHP_INT_MAX,
111 2
112 );
113 },
114 \PHP_INT_MAX
115 );
116 }
117 }
118
119 public function headerjunk()
120 {
121 // wp: header junk
122 add_action(
123 'after_setup_theme',
124 function () {
125 remove_action('wp_head', 'rsd_link');
126 remove_action('wp_head', 'wp_generator');
127 remove_action('wp_head', 'feed_links', 2);
128 remove_action('wp_head', 'feed_links_extra', 3);
129 remove_action('wp_head', 'index_rel_link');
130 remove_action('wp_head', 'wlwmanifest_link');
131 remove_action('wp_head', 'start_post_rel_link', 10, 0);
132 remove_action('wp_head', 'parent_post_rel_link', 10, 0);
133 remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
134 remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
135 remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
136 },
137 \PHP_INT_MAX
138 );
139
140 add_filter('the_generator', '__return_empty_string', \PHP_INT_MAX);
141 add_filter('x_redirect_by', '__return_false', \PHP_INT_MAX);
142 }
143
144 public function pingback()
145 {
146 // wp: disable pingback
147 add_action(
148 'pre_ping',
149 function (&$links) {
150 foreach ($links as $l => $link) {
151 if (0 === strpos($link, get_option('home'))) {
152 unset($links[$l]);
153 }
154 }
155 },
156 \PHP_INT_MAX
157 );
158
159 // wp: disable and remove do_pings
160 // https://wp-mix.com/wordpress-clean-up-do_pings/
161 add_action(
162 'plugins_loaded',
163 function () {
164 if (isset($_GET['doing_wp_cron'])) {
165 remove_action('do_pings', 'do_all_pings');
166 wp_clear_scheduled_hook('do_pings');
167 }
168 },
169 \PHP_INT_MAX
170 );
171
172 // wp: disable xmlrpc
173 // https://www.wpbeginner.com/plugins/how-to-disable-xml-rpc-in-wordpress/
174 // https://kinsta.com/blog/xmlrpc-php/
175 add_filter('xmlrpc_enabled', '__return_false');
176 add_filter('pre_update_option_enable_xmlrpc', '__return_false');
177 add_filter('pre_option_enable_xmlrpc', '__return_zero');
178
179 // additional
180 add_filter('pings_open', '__return_false');
181 add_filter('pre_option_default_ping_status', '__return_zero');
182 add_filter('pre_option_default_pingback_flag', '__return_zero');
183 add_filter(
184 'xmlrpc_methods',
185 function ($methods) {
186 unset($methods['pingback.ping']);
187 unset($methods['pingback.extensions.getPingbacks']);
188 unset($methods['wp.getUsersBlogs']);
189 unset($methods['system.multicall']);
190 unset($methods['system.listMethods']);
191 unset($methods['system.getCapabilities']);
192 unset($methods['demo.sayHello']);
193
194 return $methods;
195 }
196 );
197
198 add_action(
199 'xmlrpc_call',
200 function ($method) {
201 if ('pingback.ping' !== $method) {
202 return;
203 }
204 http_response_code(403);
205 exit('This site does not have pingback.');
206 }
207 );
208
209 add_filter(
210 'template_redirect',
211 function () {
212 header_remove('X-Pingback');
213 },
214 \PHP_INT_MAX
215 );
216
217 add_filter(
218 'wp_headers',
219 function ($headers) {
220 unset($headers['X-Pingback']);
221
222 return $headers;
223 },
224 \PHP_INT_MAX
225 );
226
227 add_action(
228 'plugins_loaded',
229 function () {
230 if (isset($_SERVER['REQUEST_URI']) && '/xmlrpc.php' === $_SERVER['REQUEST_URI']) {
231 http_response_code(403);
232 exit('xmlrpc.php not available.');
233 }
234
235 // additional
236 if (isset($_SERVER['SCRIPT_FILENAME']) && 'xmlrpc.php' === basename($_SERVER['SCRIPT_FILENAME'])) {
237 http_response_code(403);
238 exit('xmlrpc.php not available.');
239 }
240 },
241 \PHP_INT_MAX
242 );
243 }
244
245 private function has_woocommerce()
246 {
247 return isset($GLOBALS['woocommerce']) && \is_object($GLOBALS['woocommerce']);
248 }
249
250 public function woocommerce_misc()
251 {
252 // wc: action_scheduler_migration_dependencies_met
253 add_filter('action_scheduler_migration_dependencies_met', '__return_false', \PHP_INT_MAX);
254
255 // wc: disable background image regeneration
256 add_filter('woocommerce_background_image_regeneration', '__return_false', \PHP_INT_MAX);
257
258 // wc: remove marketplace suggestions
259 // https://rudrastyh.com/woocommerce/remove-marketplace-suggestions.html
260 add_filter('woocommerce_allow_marketplace_suggestions', '__return_false', \PHP_INT_MAX);
261
262 // wc: remove connect your store to WooCommerce.com admin notice
263 add_filter('woocommerce_helper_suppress_admin_notices', '__return_true', \PHP_INT_MAX);
264
265 // wc: disable the WooCommere Marketing Hub
266 add_filter(
267 'woocommerce_admin_features',
268 function ($features) {
269 $marketing = array_search('marketing', $features);
270 unset($features[$marketing]);
271
272 return $features;
273 },
274 \PHP_INT_MAX
275 );
276 add_filter('woocommerce_marketing_menu_items', '__return_empty_array', \PHP_INT_MAX);
277
278 // wc: Enable WooCommerce no-cache headers
279 // includes/class-wc-cache-helper.php
280 add_filter('woocommerce_enable_nocache_headers', '__return_false');
281
282 // wc: remove the WooCommerce usage tracker cron event
283 wp_clear_scheduled_hook('woocommerce_tracker_send_event');
284
285 // jetpack
286 add_filter('jetpack_just_in_time_msgs', '__return_false', \PHP_INT_MAX);
287 add_filter('jetpack_show_promotions', '__return_false', \PHP_INT_MAX);
288 }
289
290 public function woocommerce_admin_disabled()
291 {
292 // wc: disable the WooCommerce Admin
293 add_filter('woocommerce_admin_disabled', '__return_true', \PHP_INT_MAX);
294
295 // 09052022: line 1048 packages/woocommerce-admin/src/Loader.php -> Undefined index: id, value
296 add_filter('woocommerce_admin_preload_settings', '__return_empty_array', \PHP_INT_MAX);
297 }
298
299 public function woocommerce_dashboard_status_remove()
300 {
301 add_action(
302 'wp_dashboard_setup',
303 function () {
304 if (!$this->has_woocommerce()) {
305 return;
306 }
307
308 remove_meta_box('woocommerce_dashboard_status', 'dashboard', 'normal');
309 remove_meta_box('woocommerce_dashboard_recent_reviews', 'dashboard', 'normal');
310 remove_meta_box('woocommerce_network_orders', 'dashboard', 'normal');
311 remove_meta_box('wc_admin_dashboard_setup', 'dashboard', 'normal');
312 },
313 \PHP_INT_MAX
314 );
315 }
316
317 public function woocommerce_widget_remove()
318 {
319 add_action(
320 'widgets_init',
321 function () {
322 if (!$this->has_woocommerce()) {
323 return;
324 }
325
326 // plugins/woocommerce/includes/wc-widget-functions.php
327 $widgets = [
328 'WC_Widget_Cart',
329 'WC_Widget_Layered_Nav_Filters',
330 'WC_Widget_Layered_Nav',
331 'WC_Widget_Price_Filter',
332 'WC_Widget_Product_Categories',
333 'WC_Widget_Product_Search',
334 'WC_Widget_Product_Tag_Cloud',
335 'WC_Widget_Products',
336 'WC_Widget_Recently_Viewed',
337 'WC_Widget_Top_Rated_Products',
338 'WC_Widget_Recent_Reviews',
339 'WC_Widget_Rating_Filter',
340 ];
341 foreach ($widgets as $widget) {
342 // remove
343 unregister_widget($widget);
344
345 // prevent error notice _doing_it_wrong
346 // see wp-includes/widgets.php -> the_widget()
347 register_widget($widget, null);
348 }
349 },
350 \PHP_INT_MAX
351 );
352 }
353
354 public function woocommerce_cart_fragments_remove()
355 {
356 add_action(
357 'wp_enqueue_scripts',
358 function () {
359 $id = 'wc-cart-fragments';
360 $wp_scripts = $GLOBALS['wp_scripts'];
361 if (!\is_object($wp_scripts) || !isset($wp_scripts->registered[$id])) {
362 return;
363 }
364
365 $src = $wp_scripts->registered[$id]->src;
366 $wp_scripts->registered[$id]->src = null;
367
368 $code = '(function() {';
369 $code .= 'var checkhash = function() {';
370 $code .= 'var n = "woocommerce_cart_hash";';
371 $code .= 'var h = document.cookie.match("(^|;) ?" + n + "=([^;]*)(;|$)");';
372 $code .= 'return h ? h[2] : null;';
373 $code .= '};';
374 $code .= 'var checkscript = function() {';
375 $code .= 'var src = "'.$src.'";';
376 $code .= 'var id = "docket-cache-wccartfragment";';
377 $code .= 'if ( null !== document.getElementById(id) ) {';
378 $code .= 'return false;';
379 $code .= 'if ( checkhash() ) {';
380 $code .= 'var script = document.createElement("script");';
381 $code .= 'script.id = id;';
382 $code .= 'script.src = src;';
383 $code .= 'script.async = true;';
384 $code .= 'document.head.appendChild(script);';
385 $code .= '';
386 $code .= '}';
387 $code .= '}';
388 $code .= '};';
389 $code .= 'checkscript();';
390 $code .= 'document.addEventListener("click", function(){setTimeout(checkscript,1000);});';
391 $code .= '})();';
392 wp_add_inline_script('jquery', $code);
393 },
394 \PHP_INT_MAX
395 );
396 }
397
398 public function woocommerce_crawling_addtochart_links()
399 {
400 add_filter('robots_txt', function ($output, $public) {
401 if (!$this->has_woocommerce()) {
402 return $output;
403 }
404
405 $append = '';
406 if (!@preg_match('@^Disallow:\s+/\*add\-to\-cart=\*@is', $output)) {
407 $append .= "Disallow: /*add-to-cart=*\n";
408 }
409
410 if (!@preg_match('@^Disallow:\s+/cart/@is', $output)) {
411 $append .= "Disallow: /cart/\n";
412 } else {
413 $cart = basename(wc_get_cart_url());
414 if (!@preg_match('@^Disallow:\s+/'.$cart.'/@is', $output)) {
415 $append .= 'Disallow: /'.$cart."/\n";
416 }
417 }
418
419 if (!@preg_match('@^Disallow:\s+/checkout/@is', $output)) {
420 $append .= "Disallow: /checkout/\n";
421 } else {
422 $checkout = basename(wc_get_checkout_url());
423 if (!@preg_match('@^Disallow:\s+/'.$checkout.'/@is', $output)) {
424 $append .= 'Disallow: /'.$checkout."/\n";
425 }
426 }
427
428 if (!@preg_match('@^Disallow:\s+/my\-account/@is', $output)) {
429 $append .= "Disallow: /my-account/\n";
430 } else {
431 $myaccount = basename(wc_get_page_permalink('myaccount'));
432 if (!@preg_match('@^Disallow:\s+/'.$myaccount.'/@is', $output)) {
433 $append .= 'Disallow: /'.$myaccount."/\n";
434 }
435 }
436
437 if (!empty($append)) {
438 $addua = true;
439 if (@preg_match_all('@User-agent:\s+\S+@is', $output, $mm, \PREG_SET_ORDER)) {
440 $last = end($mm);
441 if (@preg_match('@User-agent:\s+\*@i', $last[0])) {
442 $addua = false;
443 }
444 }
445
446 $output .= "\n# Added by Docket Cache\n";
447
448 if ($addua) {
449 $output .= "User-agent: *\n";
450 }
451
452 $output .= $append;
453 }
454
455 return $output;
456 }, \PHP_INT_MAX, 2);
457 }
458
459 public function woocommerce_extensionpage_remove()
460 {
461 add_action('admin_menu', function () {
462 remove_submenu_page('woocommerce', 'wc-addons');
463 remove_submenu_page('woocommerce', 'wc-addons&section=helper');
464 }, \PHP_INT_MAX);
465 }
466
467 public function post_missed_schedule()
468 {
469 if (!nwdcx_wpdb($wpdb)) {
470 return false;
471 }
472
473 $suppress = $wpdb->suppress_errors(true);
474
475 // check
476 $query = "SELECT ID FROM `{$wpdb->posts}` WHERE post_status='future' ORDER BY ID ASC LIMIT 1";
477 $check = $wpdb->query($query);
478
479 if ($check < 1) {
480 return false;
481 }
482
483 $limit = 1000;
484 $args = [
485 'public' => true,
486 'exclude_from_search' => false,
487 '_builtin' => false,
488 ];
489
490 $post_types = get_post_types($args, 'names', 'and');
491 if (!empty($post_types) && \is_array($post_types)) {
492 $types = implode("','", $post_types);
493 $query = $wpdb->prepare("SELECT ID FROM `{$wpdb->posts}` WHERE post_type in ('post','page','%s') AND post_status='future' ORDER BY ID ASC LIMIT %d", $types, $limit);
494 } else {
495 $query = $wpdb->prepare("SELECT ID FROM `{$wpdb->posts}` WHERE post_type in ('post','page') AND post_status='future' ORDER BY ID ASC LIMIT %d", $limit);
496 }
497
498 $results = $wpdb->get_results($query, ARRAY_A);
499
500 if (!empty($results)) {
501 while ($row = @array_shift($results)) {
502 $id = $row['ID'];
503 wp_publish_post($id);
504 }
505 }
506
507 $wpdb->suppress_errors($suppress);
508
509 return true;
510 }
511
512 public function wpemoji()
513 {
514 remove_action('wp_head', 'print_emoji_detection_script', 7);
515 remove_action('admin_print_scripts', 'print_emoji_detection_script');
516 remove_action('wp_print_styles', 'print_emoji_styles');
517 remove_action('admin_print_styles', 'print_emoji_styles');
518 remove_filter('the_content_feed', 'wp_staticize_emoji');
519 remove_filter('comment_text_rss', 'wp_staticize_emoji');
520 remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
521
522 add_filter('emoji_svg_url', '__return_false');
523
524 add_filter(
525 'tiny_mce_plugins',
526 function ($plugins) {
527 if (\is_array($plugins)) {
528 return array_diff($plugins, ['wpemoji']);
529 }
530
531 return [];
532 }
533 );
534
535 add_filter(
536 'wp_resource_hints',
537 function ($urls, $relation_type) {
538 if ('dns-prefetch' === (string) $relation_type) {
539 $emoji_url = 'https://s.w.org/images/core/emoji/';
540 foreach ($urls as $key => $url) {
541 if (false !== strpos($url, $emoji_url)) {
542 unset($urls[$key]);
543 }
544 }
545 }
546
547 return $urls;
548 },
549 10,
550 2
551 );
552 }
553
554 // ref: https://wordpress.org/support/topic/syntax-error-222/
555 public function wpembed_bodyclass($classes, $class = [])
556 {
557 foreach ($classes as $num => $name) {
558 if ('wp-embed-responsive' === $name) {
559 unset($classes[$num]);
560 }
561 }
562
563 return $classes;
564 }
565
566 public function wpembed()
567 {
568 if (isset($GLOBALS['wp']) && \is_object($GLOBALS['wp']) && isset($GLOBALS['wp']->public_query_vars)) {
569 $GLOBALS['wp']->public_query_vars = array_diff($GLOBALS['wp']->public_query_vars, ['embed']);
570 }
571
572 if (isset($GLOBALS['wp_embed']) && \is_object($GLOBALS['wp_embed'])) {
573 remove_filter('the_content', [$GLOBALS['wp_embed'], 'autoembed'], 8);
574 }
575
576 remove_filter('the_content_feed', '_oembed_filter_feed_content');
577 remove_action('plugins_loaded', 'wp_maybe_load_embeds', 0);
578 add_filter('pre_option_embed_autourls', '__return_false');
579 add_filter('embed_oembed_discover', '__return_false');
580 remove_action('rest_api_init', 'wp_oembed_register_route');
581 remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request');
582 remove_action('wp_head', 'wp_oembed_add_discovery_links');
583 remove_action('wp_head', 'wp_oembed_add_host_js');
584 remove_action('embed_head', 'enqueue_embed_scripts', 1);
585 remove_action('embed_head', 'print_emoji_detection_script');
586 remove_action('embed_head', 'print_embed_styles');
587 remove_action('embed_head', 'wp_print_head_scripts', 20);
588 remove_action('embed_head', 'wp_print_styles', 20);
589 remove_action('embed_head', 'wp_no_robots');
590 remove_action('embed_head', 'rel_canonical');
591 remove_action('embed_head', 'locale_stylesheet', 30);
592 remove_action('embed_content_meta', 'print_embed_comments_button');
593 remove_action('embed_content_meta', 'print_embed_sharing_button');
594 remove_action('embed_footer', 'print_embed_sharing_dialog');
595 remove_action('embed_footer', 'print_embed_scripts');
596 remove_action('embed_footer', 'wp_print_footer_scripts', 20);
597 remove_filter('excerpt_more', 'wp_embed_excerpt_more', 20);
598 remove_filter('the_excerpt_embed', 'wptexturize');
599 remove_filter('the_excerpt_embed', 'convert_chars');
600 remove_filter('the_excerpt_embed', 'wpautop');
601 remove_filter('the_excerpt_embed', 'shortcode_unautop');
602 remove_filter('the_excerpt_embed', 'wp_embed_excerpt_attachment');
603 remove_filter('oembed_dataparse', 'wp_filter_oembed_result');
604 remove_filter('oembed_response_data', 'get_oembed_response_data_rich');
605 remove_filter('pre_oembed_result', 'wp_filter_pre_oembed_result');
606 remove_filter('woocommerce_short_description', 'wc_do_oembeds');
607
608 add_filter(
609 'tiny_mce_plugins',
610 function ($plugins) {
611 return array_diff($plugins, ['wpembed', 'wpview']);
612 }
613 );
614
615 add_filter(
616 'rewrite_rules_array',
617 function ($rules) {
618 $results = [];
619 foreach ($rules as $rule => $val) {
620 if (false !== ($pos = strpos($val, '?'))) {
621 $args = explode('&', substr($val, $pos + 1));
622 if (\in_array('embed=true', $args)) {
623 continue;
624 }
625 }
626 $results[$rule] = $val;
627 }
628
629 return $results;
630 }
631 );
632
633 if (\defined('DOCKET_CACHE_WPEMBED_BODYCLASS_FILTER') && DOCKET_CACHE_WPEMBED_BODYCLASS_FILTER) {
634 add_filter(
635 'body_class', [$this, 'wpembed_bodyclass'],
636 \PHP_INT_MAX,
637 2
638 );
639 }
640
641 add_action(
642 'wp_footer',
643 function () {
644 wp_dequeue_script('wp-embed');
645 },
646 \PHP_INT_MAX
647 );
648 }
649
650 public function wpfeed()
651 {
652 add_action(
653 'wp_loaded',
654 function () {
655 remove_action('wp_head', 'feed_links', 2);
656 remove_action('wp_head', 'feed_links_extra', 3);
657 }
658 );
659
660 add_action(
661 'init',
662 function () {
663 if (isset($GLOBALS['wp_rewrite']) && \is_object($GLOBALS['wp_rewrite']) && isset($GLOBALS['wp_rewrite']->feeds)) {
664 $GLOBALS['wp_rewrite']->feeds = [];
665 }
666 }
667 );
668
669 foreach (['rdf', 'rss', 'rss2', 'atom', 'rss2_comments', 'atom_comments'] as $feed) {
670 add_action(
671 'do_feed_'.$feed,
672 function () {
673 wp_redirect(home_url(), 302);
674 exit;
675 },
676 1
677 );
678 }
679 }
680
681 public function wplazyload()
682 {
683 add_action(
684 'init',
685 function () {
686 add_filter('wp_lazy_loading_enabled', '__return_false');
687 },
688 \PHP_INT_MAX
689 );
690 }
691
692 public function wpsitemap()
693 {
694 add_action(
695 'init',
696 function () {
697 add_filter('wp_sitemaps_enabled', '__return_false');
698 remove_filter('robots_txt', ['WP_Sitemaps', 'add_robots']);
699 },
700 \PHP_INT_MIN
701 );
702 }
703
704 public function wpapppassword()
705 {
706 add_filter('wp_is_application_passwords_available', '__return_false', \PHP_INT_MAX);
707 }
708
709 public function wpdashboardnews()
710 {
711 add_action(
712 'wp_dashboard_setup',
713 function () {
714 remove_meta_box('dashboard_primary', 'dashboard', 'side');
715 },
716 \PHP_INT_MAX
717 );
718
719 add_action(
720 'admin_init',
721 function () {
722 remove_meta_box('dashboard_primary', 'dashboard-network', 'side');
723 },
724 \PHP_INT_MAX
725 );
726 }
727
728 // reference:
729 // wp-admin/includes/dashboard.php -> wp_check_browser_version()
730 public function wpbrowsehappy()
731 {
732 if (empty($_SERVER['HTTP_USER_AGENT'])) {
733 return;
734 }
735
736 $key = md5($_SERVER['HTTP_USER_AGENT']);
737
738 // reference: wp-includes/option.php -> get_site_transient( $transient )
739 // return an array to implying it always exists and never expires.
740 add_filter('pre_site_transient_browser_'.$key, function () {
741 // return an array instead of true to avoid php error
742 // "Trying to access array offset on value of type bool".
743 return [];
744 }, \PHP_INT_MAX);
745 }
746
747 // reference:
748 // wp-admin/includes/misc.php -> wp_check_php_version()
749 public function wpservehappy()
750 {
751 $key = md5(\PHP_VERSION);
752 add_filter('pre_site_transient_php_check_'.$key, function () {
753 return [];
754 }, \PHP_INT_MAX);
755 }
756
757 public function limit_http_request()
758 {
759 add_action(
760 'admin_init',
761 function () {
762 add_filter(
763 'pre_http_request',
764 function ($preempt, $parsed_args, $url) {
765 if (!is_admin()) {
766 return false;
767 }
768
769 if (empty($GLOBALS['pagenow'])) {
770 return false;
771 }
772
773 $pagenow = $GLOBALS['pagenow'];
774 $pageok = [
775 'index.php' => 1,
776 'plugins.php' => 1,
777 'plugin-install.php' => 1,
778 'update.php' => 1,
779 'themes.php' => 1,
780 'admin.php' => 1,
781 'update-core.php' => 1,
782 'admin-ajax.php' => 1,
783 ];
784
785 if (\array_key_exists($pagenow, $pageok)) {
786 return false;
787 }
788
789 $hostme = parse_url(home_url(), \PHP_URL_HOST);
790 $hostname = parse_url($url, \PHP_URL_HOST);
791
792 if ('127.0.0.1' === $hostname || 'localhost' === $hostname || $hostme === $hostname) {
793 return false;
794 }
795
796 if ('.local' === substr($hostme, -\strlen('.local')) || '.test' === substr($hostme, -\strlen('.test'))) {
797 return false;
798 }
799
800 if ('wordpress.org' === $hostname || 'api.wordpress.org' === $hostname || 'docketcache.com' === $hostname) {
801 return false;
802 }
803
804 if ('.wordpress.org' === substr($hostname, -\strlen('.wordpress.org'))) {
805 return false;
806 }
807
808 if ('.docketcache.com' === substr($hostname, -\strlen('.docketcache.com'))) {
809 return false;
810 }
811
812 $ok = true;
813 $wkey = nwdcx_constfx('LIMITHTTPREQUEST_WHITELIST');
814 if (\defined($wkey)) {
815 $whitelist = \constant($wkey);
816 if (!empty($whitelist) && \is_array($whitelist)) {
817 while ($hosta = @array_shift($whitelist)) {
818 $hostb = nwdcx_noscheme($hosta);
819 if ($hostname === $hostb) {
820 $ok = false;
821 break;
822 }
823
824 if ('.' === $hostb[0] && $hostb === substr($hostname, -\strlen($hostb))) {
825 $ok = false;
826 break;
827 }
828 }
829 }
830 }
831
832 /*if ( $ok ) {
833 error_log('docket-cache: Tweaks::limit_http_request(): Drop -> '.$hostname);
834 }*/
835
836 return $ok;
837 },
838 \PHP_INT_MIN,
839 3
840 );
841 },
842 \PHP_INT_MAX
843 );
844 }
845
846 // wp < 5.8
847 public function http_headers_expect()
848 {
849 // https://github.com/WordPress/Requests/pull/454
850 if (version_compare($GLOBALS['wp_version'], '5.8', '<')) {
851 return false;
852 }
853
854 add_filter('http_request_args', function ($args) {
855 if (!isset($args['headers']['expect'])) {
856 $args['headers']['expect'] = '';
857
858 if (\is_array($args['body'])) {
859 $bytesize = 0;
860 $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($args['body']));
861
862 foreach ($iterator as $datum) {
863 $bytesize += \strlen((string) $datum);
864
865 if ($bytesize >= 1048576) {
866 $args['headers']['expect'] = '100-Continue';
867 break;
868 }
869 }
870 } elseif (!empty($args['body']) && \strlen((string) $args['body']) > 1048576) {
871 $args['headers']['expect'] = '100-Continue';
872 }
873 }
874
875 return $args;
876 }, \PHP_INT_MAX);
877 }
878
879 public function cache_http_response()
880 {
881 add_action('init', function () {
882 add_filter('http_response', function ($response, $args, $url) {
883 if (200 !== $response['response']['code']) {
884 return $response;
885 }
886
887 $site_host = wp_parse_url(site_url(), \PHP_URL_HOST);
888 $hostname = wp_parse_url($url, \PHP_URL_HOST);
889 if ('wordpress.org' === $hostname || '.wordpress.org' === substr($hostname, -\strlen('.wordpress.org'))
890 || 'docketcache.com' === $hostname || '.docketcache.com' === substr($hostname, -\strlen('.docketcache.com'))
891 || false !== strpos($hostname, $site_host)) {
892 return $response;
893 }
894
895 $cache_group = 'docketcache-httpresponse';
896 $cache_key = $url;
897
898 $cache_ttl = (int) nwdcx_constval('CACHEHTTPRESPONSE_TTL');
899 if (!empty($cache_ttl)) {
900 $cache_ttl = 300;
901 }
902
903 $include_list = nwdcx_constval('CACHEHTTPRESPONSE_INCLUDE');
904 $exclude_list = nwdcx_constval('CACHEHTTPRESPONSE_EXCLUDE');
905
906 if (empty($include_list) && empty($exclude_list)) {
907 wp_cache_set($cache_key, $response, $cache_group, $cache_ttl);
908
909 return $response;
910 }
911
912 if (!empty($include_list) && \is_array($include_list) && \in_array($url, $include_list)) {
913 if (!empty($exclude_list) && \is_array($exclude_list) && !\in_array($url, $exclude_list)) {
914 wp_cache_set($cache_key, $response, $cache_group, $cache_ttl);
915 }
916
917 return $response;
918 }
919
920 if (!empty($exclude_list) && \is_array($exclude_list) && !\in_array($url, $exclude_list)) {
921 wp_cache_set($cache_key, $response, $cache_group, $cache_ttl);
922
923 return $response;
924 }
925
926 return $response;
927 }, \PHP_INT_MIN, 3);
928
929 add_filter('pre_http_request', function ($preempt, $parsed_args, $url) {
930 $cache_group = 'docketcache-httpresponse';
931 $cache_key = $url;
932 $data = wp_cache_get($cache_key, $cache_group);
933 if (!empty($data) && \is_array($data)) {
934 return $data;
935 }
936
937 return $preempt;
938 }, \PHP_INT_MIN, 3);
939 }, \PHP_INT_MAX);
940 }
941 }
942