-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwp-cache-nav-menus.php
82 lines (57 loc) · 1.99 KB
/
wp-cache-nav-menus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Plugin Name: WP Cache Nav Menus
* Plugin URI: https://github.com/mharis/wp-cache-nav-menus
* Description: Optimize the speed of WordPress Menus
* Version: 1.0.0
* Author: Asad Khan, Muhammad Haris
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* GitHub Plugin URI: https://github.com/mharis/wp-cache-nav-menus
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
add_filter( 'wp_nav_menu_args', function( $args ) {
$post_id = get_queried_object_id();
$key = 'menu-cache-' . $args['theme_location'] . '-' . $post_id;
$cache = get_transient( $key );
if( $cache ) {
$last_updated = get_transient('menu-cache-' . $args['theme_location'] . '-last-updated');
if ( !isset($cache['time']) || empty($last_updated) || $last_updated > $cache['time'] ) {
return $args;
}
$args = array_merge( $args, array(
'fallback_cb' => 'this_pro_function',
'theme_location' => 'something-unexpected-for-a-menu-name',
'the_cache' => $cache['data']
) );
}
return $args;
}, 10, 1 );
function this_pro_function( $args ) {
if( $args['echo'] ) {
echo $args['the_cache'];
}
return $args['the_cache'];
}
add_filter( 'wp_nav_menu', function( $nav, $args ) {
$post_id = get_queried_object_id();
$last_updated = get_transient('menu-cache-' . $args->theme_location . '-last-updated');
if( ! $last_updated ) {
set_transient('menu-cache-' . $args->theme_location . '-last-updated', time());
}
if( $post_id ) {
$key = 'menu-cache-' . $args->theme_location . '-' . $post_id;
$data = array('time' => time(), 'data' => $nav);
set_transient( $key, $data );
}
return $nav;
}, 10, 2 );
add_action( 'wp_update_nav_menu', function($menu_id) {
$locations = array_flip(get_nav_menu_locations());
if( isset($locations[$menu_id]) ) {
set_transient('menu-cache-' . $locations[$menu_id] . '-last-updated', time());
}
}, 10, 1);