Menu_Cache::disable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 7
Ratio 50 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 14
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
16
17 View Code Duplication
		if ( is_admin() ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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() ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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() {
0 ignored issues
show
Coding Style introduced by
update_menus_edited uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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