-
Notifications
You must be signed in to change notification settings - Fork 29
/
per-post-nav-menu.php
176 lines (146 loc) · 4.76 KB
/
per-post-nav-menu.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Plugin Name: Per Post Nav Menu
* Plugin URI: http://wordpress.stackexchange.com/q/85243/6035
* Description: Change nav menu based on theme location and a per-post selection.
* Version: 1.0
* Text Domain: wpse
* Author: Christopher Davis
* Author URI: http://christopherdavis.me
* License: GPL-2.0+
*
* Copyright 2013 Christopher Davis <http://christopherdavis.me>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category WordPress
* @author Christopher Davis <http://christopherdavis.me>
* @copyright 2013 Christopher Davis
* @license http://opensource.org/licenses/GPL-2.0 GPL-2.0+
*/
!defined('ABSPATH') && exit;
PerPostNavMenu::init();
class PerPostNavMenu
{
const NONCE = 'wpse85243_nav_nonce';
const FIELD = '_wpse85243_per_post_menu';
const LOC = 'primary'; // the location for twenty twelve
private static $ins = null;
public static function instance()
{
if (is_null(self::$ins)) {
self::$ins = new self;
}
return self::$ins;
}
public static function init()
{
add_action('plugins_loaded', array(self::instance(), '_setup'));
}
public function _setup()
{
add_action('add_meta_boxes', array($this, 'addBox'));
add_action('save_post', array($this, 'save'), 10, 2);
add_filter('wp_nav_menu_args', array($this, 'switchMenu'));
}
public function addBox($post_type)
{
if (!post_type_exists($post_type)) {
return;
}
add_meta_box(
'wpse85243_nav_menu',
__('Nav Menu', 'wpse'),
array($this, 'boxCallback'),
$post_type,
'side',
'low'
);
}
public function boxCallback($post)
{
$menu = get_post_meta($post->ID, static::FIELD, true);
wp_nonce_field(static::NONCE . $post->ID, static::NONCE, false);
printf(
'<label for="%s">%s</label>',
esc_attr(static::FIELD),
esc_html__('Nav Menu', 'wpse')
);
echo '<br />';
printf('<select name="%1$s" id="%1$s">', esc_attr(static::FIELD));
foreach ($this->getNavMenus() as $id => $name) {
printf(
'<option value="%s" %s>%s</option>',
esc_attr($id),
selected($menu, $id, false),
esc_html($name)
);
}
echo '</select>';
}
public function save($post_id, $post)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (
!isset($_POST[static::NONCE]) ||
!wp_verify_nonce($_POST[static::NONCE], static::NONCE . $post_id)
) {
return;
}
$type = get_post_type_object($post->post_type);
if (!current_user_can($type->cap->edit_post, $post_id)) {
return;
}
$menu = isset($_POST[static::FIELD]) ? $_POST[static::FIELD] : false;
if ($menu && '-' !== $menu) {
update_post_meta($post_id, static::FIELD, absint($menu));
} else {
delete_post_meta($post_id, static::FIELD);
}
}
public function switchMenu($args)
{
// we can only deal with singular pages
if (!is_singular()) {
return;
}
$switch = apply_filters(
'per_post_nav_menus_switch',
isset($args['theme_location']) && static::LOC === $args['theme_location'],
$args
);
// if we're allowed to switch, the the `menu` argument to
// the correct menu ID.
if ($switch) {
$menu = get_post_meta(get_queried_object_id(), static::FIELD, true);
if ('-' !== $menu) {
$args['menu'] = absint($menu);
}
}
return $args;
}
private function getNavMenus()
{
$terms = get_terms('nav_menu');
$menus = array('-' => __('Default', 'wpse'));
if ($terms && !is_wp_error($terms)) {
foreach($terms as $t) {
$menus[$t->term_id] = $t->name;
}
}
return apply_filters('per_post_nav_menus_list', $menus);
}
}