PluginProbe
Docket Cache – Object Cache Accelerator / trunk
Docket Cache – Object Cache Accelerator vtrunk
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 / MenuCache.php

MenuCache.php in Docket Cache – Object Cache Accelerator trunk, at includes/src/MenuCache.php

153 lines 4.1 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 MenuCache
16 {
17 public function register()
18 {
19 if ($this->is_front_end()) {
20 add_action(
21 'init',
22 function () {
23 add_filter('pre_wp_nav_menu', [$this, 'pre_wp_nav_menu'], \PHP_INT_MAX, 2);
24 add_filter('wp_nav_menu', [$this, 'wp_nav_menu'], \PHP_INT_MAX, 2);
25 },
26 \PHP_INT_MAX
27 );
28 }
29
30 add_action(
31 'admin_init',
32 function () {
33 add_action('wp_update_nav_menu', [$this, 'wp_update_nav_menu'], \PHP_INT_MAX);
34 add_action('wp_delete_nav_menu', [$this, 'wp_update_nav_menu'], \PHP_INT_MAX);
35 add_filter('pre_set_theme_mod_nav_menu_locations', [$this, 'pre_set_theme_mod_nav_menu_locations'], \PHP_INT_MAX, 2);
36 },
37 \PHP_INT_MAX
38 );
39 }
40
41 private function is_front_end()
42 {
43 if (!is_admin()
44 && (!\defined('WP_CLI') || !WP_CLI)
45 && (!\defined('DOING_AJAX') || !DOING_AJAX)) {
46 return true;
47 }
48
49 if (!empty($_SERVER['REQUEST_URI']) && \function_exists('rest_get_url_prefix')) {
50 $rest_prefix = rest_get_url_prefix();
51
52 return substr($_SERVER['REQUEST_URI'], 1, \strlen($rest_prefix)) === $rest_prefix;
53 }
54
55 return false;
56 }
57
58 private function normalize_menu_object(&$args)
59 {
60 $menu = wp_get_nav_menu_object($args->menu);
61
62 $locations = get_nav_menu_locations();
63 if (!$menu && $args->theme_location && $locations && isset($locations[$args->theme_location])) {
64 $menu = wp_get_nav_menu_object($locations[$args->theme_location]);
65 }
66
67 if (!$menu && !$args->theme_location) {
68 $menus = wp_get_nav_menus();
69 foreach ($menus as $menu_maybe) {
70 $menu_items = wp_get_nav_menu_items($menu_maybe->term_id, ['update_post_term_cache' => false]);
71 if ($menu_items) {
72 $menu = $menu_maybe;
73 break;
74 }
75 }
76 }
77
78 if (empty($args->menu) || is_numeric($args->menu)) {
79 $args->menu = $menu;
80 }
81 }
82
83 private function set_key($args)
84 {
85 return md5(wp_json_encode($args));
86 }
87
88 private function get_key($menu_key)
89 {
90 return 'docketcache-menu-'.$menu_key;
91 }
92
93 private function get_cache($menu_key)
94 {
95 return wp_cache_get($this->get_key($menu_key), 'docketcache-menu');
96 }
97
98 private function navmenu_flush_cache()
99 {
100 if (\function_exists('wp_cache_flush_group')) {
101 wp_cache_flush_group('docketcache-menu');
102 }
103 }
104
105 public function pre_wp_nav_menu($output, $args)
106 {
107 $this->normalize_menu_object($args);
108 if (isset($args->menu->term_id)) {
109 $menu_key = $this->set_key($args);
110 if ($cached_output = $this->get_cache($menu_key)) {
111 if (nwdcx_construe('SIGNATURE')) {
112 $cached_output .= '<!-- This menu is cached by Docket Cache -->'.\PHP_EOL;
113 }
114
115 return $cached_output;
116 }
117 }
118
119 return $output;
120 }
121
122 public function wp_nav_menu($nav_menu, $args)
123 {
124 if (false !== strpos($nav_menu, 'no-cache')) {
125 return $nav_menu;
126 }
127
128 if (isset($args->menu->term_id)) {
129 $menu_key = $this->set_key($args);
130 $cache_ttl = nwdcx_constval('MENUCACHE_TTL');
131 if (empty($cache_ttl) || !\is_int($cache_ttl)) {
132 $cache_ttl = 1209600; // 14 days
133 }
134
135 wp_cache_set($this->get_key($menu_key), $nav_menu, 'docketcache-menu', $cache_ttl);
136 }
137
138 return $nav_menu;
139 }
140
141 public function wp_update_nav_menu($menu_id)
142 {
143 $this->navmenu_flush_cache();
144 }
145
146 public function pre_set_theme_mod_nav_menu_locations($new, $old)
147 {
148 $this->navmenu_flush_cache();
149
150 return $new;
151 }
152 }
153