1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Rarst\Fragment_Cache;
|
4
|
|
|
|
5
|
|
|
/**
|
6
|
|
|
* Cache navigation menus.
|
7
|
|
|
*/
|
8
|
|
|
class Menu_Cache extends Fragment_Cache {
|
9
|
|
|
|
10
|
|
|
/**
|
11
|
|
|
* @inheritDoc
|
12
|
|
|
*/
|
13
|
|
|
public function enable() {
|
14
|
|
|
|
15
|
|
|
global $wp_version;
|
|
|
|
|
16
|
|
|
|
17
|
|
View Code Duplication |
if ( is_admin() ) {
|
|
|
|
|
18
|
|
|
add_action( 'admin_footer-nav-menus.php', array( $this, 'update_menus_edited' ) );
|
19
|
|
|
add_action( 'wp_ajax_menu-locations-save', array( $this, 'update_menus_edited' ), 0 );
|
20
|
|
|
add_action( 'wp_ajax_customize_save', array( $this, 'customize_save' ), 0 );
|
21
|
|
|
|
22
|
|
|
return;
|
23
|
|
|
}
|
24
|
|
|
|
25
|
|
|
add_filter( 'pre_wp_nav_menu', array( $this, 'pre_wp_nav_menu' ), 10, 2 );
|
26
|
|
|
add_filter( 'wp_nav_menu_objects', array( $this, 'wp_nav_menu_objects' ) );
|
27
|
|
|
|
28
|
|
|
if ( version_compare( $wp_version, '3.9', '<' ) ) {
|
29
|
|
|
add_filter( 'wp_nav_menu_args', array( $this, 'wp_nav_menu_args' ), 20 );
|
30
|
|
|
}
|
31
|
|
|
}
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* @inheritDoc
|
35
|
|
|
*/
|
36
|
|
|
public function disable() {
|
37
|
|
|
|
38
|
|
View Code Duplication |
if ( is_admin() ) {
|
|
|
|
|
39
|
|
|
remove_action( 'admin_footer-nav-menus.php', array( $this, 'update_menus_edited' ) );
|
40
|
|
|
remove_action( 'wp_ajax_menu-locations-save', array( $this, 'update_menus_edited' ), 0 );
|
41
|
|
|
remove_action( 'wp_ajax_customize_save', array( $this, 'customize_save' ), 0 );
|
42
|
|
|
|
43
|
|
|
return;
|
44
|
|
|
}
|
45
|
|
|
|
46
|
|
|
remove_filter( 'pre_wp_nav_menu', array( $this, 'pre_wp_nav_menu' ), 10 );
|
47
|
|
|
remove_filter( 'wp_nav_menu_objects', array( $this, 'wp_nav_menu_objects' ) );
|
48
|
|
|
remove_filter( 'wp_nav_menu_args', array( $this, 'wp_nav_menu_args' ), 20 );
|
49
|
|
|
}
|
50
|
|
|
|
51
|
|
|
/**
|
52
|
|
|
* Return cached menu, using pre-generation hook.
|
53
|
|
|
*
|
54
|
|
|
* @param string $menu Menu HTML to return.
|
55
|
|
|
* @param object $args Menu arguments.
|
56
|
|
|
*
|
57
|
|
|
* @return string
|
58
|
|
|
*/
|
59
|
|
|
public function pre_wp_nav_menu( $menu, $args ) {
|
60
|
|
|
|
61
|
|
|
$args = get_object_vars( $args );
|
62
|
|
|
$args['echo'] = false;
|
63
|
|
|
$args['fc_menus_edited'] = get_option( 'fc_menus_edited' );
|
64
|
|
|
$name = is_object( $args['menu'] ) ? $args['menu']->slug : $args['menu'];
|
65
|
|
|
|
66
|
|
|
if ( empty( $name ) && ! empty( $args['theme_location'] ) ) {
|
67
|
|
|
$name = $args['theme_location'];
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
return $this->fetch( $name, $args, $args );
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
/**
|
74
|
|
|
* Fake no menu matches to force menu run custom callback.
|
75
|
|
|
*
|
76
|
|
|
* @deprecated
|
77
|
|
|
*
|
78
|
|
|
* @param array $args Menu arguments.
|
79
|
|
|
*
|
80
|
|
|
* @return array
|
81
|
|
|
*/
|
82
|
|
|
public function wp_nav_menu_args( $args ) {
|
83
|
|
|
|
84
|
|
|
_deprecated_function( __FUNCTION__, '1.3', 'Menu cache with arguments override unnecessary on WP >= 3.9.' );
|
85
|
|
|
|
86
|
|
|
if ( empty( $args['kessel_run'] ) ) {
|
87
|
|
|
|
88
|
|
|
add_filter( 'wp_get_nav_menus', '__return_empty_array' ); // These are not the droids you are looking for.
|
89
|
|
|
|
90
|
|
|
$args = array(
|
91
|
|
|
'menu' => '',
|
92
|
|
|
'theme_location' => '',
|
93
|
|
|
'fallback_cb' => array( $this, 'fallback_cb' ),
|
94
|
|
|
'original_args' => $args,
|
95
|
|
|
);
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
return $args;
|
99
|
|
|
}
|
100
|
|
|
|
101
|
|
|
/**
|
102
|
|
|
* Strip current* classes from menu items, since shared when cached.
|
103
|
|
|
*
|
104
|
|
|
* @param array $menu_items Array of menu item objects.
|
105
|
|
|
*
|
106
|
|
|
* @return array
|
107
|
|
|
*/
|
108
|
|
|
public function wp_nav_menu_objects( $menu_items ) {
|
109
|
|
|
|
110
|
|
|
foreach ( $menu_items as $item_key => $item ) {
|
111
|
|
|
foreach ( $item->classes as $class_key => $class ) {
|
112
|
|
|
if ( 0 === stripos( $class, 'current' ) ) {
|
113
|
|
|
unset( $menu_items[ $item_key ]->classes[ $class_key ] );
|
114
|
|
|
}
|
115
|
|
|
}
|
116
|
|
|
}
|
117
|
|
|
|
118
|
|
|
return $menu_items;
|
119
|
|
|
}
|
120
|
|
|
|
121
|
|
|
/**
|
122
|
|
|
* Save timestamp when menus were last modified for cache salt.
|
123
|
|
|
*/
|
124
|
|
|
public function update_menus_edited() {
|
|
|
|
|
125
|
|
|
|
126
|
|
|
if ( ! empty( $_POST ) ) {
|
127
|
|
|
update_option( 'fc_menus_edited', time() );
|
128
|
|
|
}
|
129
|
|
|
}
|
130
|
|
|
|
131
|
|
|
/**
|
132
|
|
|
* Invalidate menu cache on related Customizer saves.
|
133
|
|
|
*/
|
134
|
|
|
public function customize_save() {
|
135
|
|
|
|
136
|
|
|
$customized = filter_input( INPUT_POST, 'customized' );
|
137
|
|
|
|
138
|
|
|
if ( empty( $customized ) ) {
|
139
|
|
|
return;
|
140
|
|
|
}
|
141
|
|
|
|
142
|
|
|
$customized = json_decode( $customized, true );
|
143
|
|
|
$settings = array_keys( $customized );
|
144
|
|
|
|
145
|
|
|
foreach ( $settings as $setting ) {
|
146
|
|
|
|
147
|
|
|
if ( 0 === stripos( $setting, 'nav_menu' ) ) {
|
148
|
|
|
|
149
|
|
|
update_option( 'fc_menus_edited', time() );
|
150
|
|
|
|
151
|
|
|
return;
|
152
|
|
|
}
|
153
|
|
|
}
|
154
|
|
|
}
|
155
|
|
|
|
156
|
|
|
/**
|
157
|
|
|
* Restore arguments and fetch cached fragment for them.
|
158
|
|
|
*
|
159
|
|
|
* @deprecated
|
160
|
|
|
*
|
161
|
|
|
* @param array $args Arguments.
|
162
|
|
|
*
|
163
|
|
|
* @return string
|
164
|
|
|
*/
|
165
|
|
|
public function fallback_cb( $args ) {
|
166
|
|
|
|
167
|
|
|
_deprecated_function( __FUNCTION__, '1.3', 'Menu cache with arguments override unnecessary on WP >= 3.9.' );
|
168
|
|
|
|
169
|
|
|
remove_filter( 'wp_get_nav_menus', '__return_empty_array' );
|
170
|
|
|
|
171
|
|
|
$args = $args['original_args'];
|
172
|
|
|
unset( $args['original_args'] );
|
173
|
|
|
$echo = $args['echo'];
|
174
|
|
|
$args['echo'] = false;
|
175
|
|
|
$args['kessel_run'] = true;
|
176
|
|
|
$args['fc_menus_edited'] = get_option( 'fc_menus_edited' );
|
177
|
|
|
$name = is_object( $args['menu'] ) ? $args['menu']->slug : $args['menu'];
|
178
|
|
|
|
179
|
|
|
if ( empty( $name ) && ! empty( $args['theme_location'] ) ) {
|
180
|
|
|
$name = $args['theme_location'];
|
181
|
|
|
}
|
182
|
|
|
|
183
|
|
|
$output = $this->fetch( $name, $args, $args );
|
184
|
|
|
|
185
|
|
|
if ( $echo ) {
|
186
|
|
|
echo $output;
|
187
|
|
|
}
|
188
|
|
|
|
189
|
|
|
return $output;
|
190
|
|
|
}
|
191
|
|
|
|
192
|
|
|
/**
|
193
|
|
|
* Generate and timestamp menu output.
|
194
|
|
|
*
|
195
|
|
|
* @param string $name Fragment name.
|
196
|
|
|
* @param array $args Arguments.
|
197
|
|
|
*
|
198
|
|
|
* @return string
|
199
|
|
|
*/
|
200
|
|
|
protected function callback( $name, $args ) {
|
201
|
|
|
|
202
|
|
|
remove_filter( 'pre_wp_nav_menu', array( $this, 'pre_wp_nav_menu' ), 10 );
|
203
|
|
|
$output = wp_nav_menu( $args ) . $this->get_comment( $name );
|
204
|
|
|
add_filter( 'pre_wp_nav_menu', array( $this, 'pre_wp_nav_menu' ), 10, 2 );
|
205
|
|
|
|
206
|
|
|
return $output;
|
207
|
|
|
}
|
208
|
|
|
}
|
209
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state