tenzing.php
20.2 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
*Plugin Name: Nav Menu Custom Fields
* Version: 1.0
* Author: Tenzing Communications, Inc.
* Description: Store various Tenzing settings that can differ between dev, staging, and production environments.
*/
/*
* Saves new field to postmeta for navigation
*/
add_action('wp_update_nav_menu_item', 'custom_nav_update', 10, 3);
function custom_nav_update($menu_id, $menu_item_db_id, $args)
{
if (is_array($_REQUEST['menu-item-custom'])) {
$custom_value = $_REQUEST['menu-item-custom'][$menu_item_db_id];
update_post_meta($menu_item_db_id, '_menu_item_custom', $custom_value);
}
if (is_array($_REQUEST['menu-item-custom-image'])) {
$custom_value = $_REQUEST['menu-item-custom-image'][$menu_item_db_id];
update_post_meta($menu_item_db_id, '_menu_item_custom_image', $custom_value);
}
if (is_array($_REQUEST['menu-item-custom-text'])) {
$custom_value = $_REQUEST['menu-item-custom-text'][$menu_item_db_id];
update_post_meta($menu_item_db_id, '_menu_item_custom_text', $custom_value);
}
if (is_array($_REQUEST['menu-item-custom-url'])) {
$custom_value = $_REQUEST['menu-item-custom-url'][$menu_item_db_id];
update_post_meta($menu_item_db_id, '_menu_item_custom_url', $custom_value);
}
if (is_array($_REQUEST['menu-item-custom-shortcode'])) {
$custom_value = $_REQUEST['menu-item-custom-shortcode'][$menu_item_db_id];
update_post_meta($menu_item_db_id, '_menu_item_custom_shortcode', $custom_value);
}
}
/*
* Adds value of new field to $item object that will be passed to Walker_Nav_Menu_Edit_Custom
*/
add_filter('wp_setup_nav_menu_item', 'custom_nav_item');
function custom_nav_item($menu_item)
{
$menu_item->custom = get_post_meta($menu_item->ID, '_menu_item_custom', true);
$menu_item->custom_image = get_post_meta($menu_item->ID, '_menu_item_custom_image', true);
$menu_item->custom_text = get_post_meta($menu_item->ID, '_menu_item_custom_text', true);
$menu_item->custom_url = get_post_meta($menu_item->ID, '_menu_item_custom_url', true);
$menu_item->custom_shortcode = get_post_meta($menu_item->ID, '_menu_item_custom_shortcode', true);
return $menu_item;
}
add_filter('wp_edit_nav_menu_walker', 'custom_nav_edit_walker', 10, 2);
function custom_nav_edit_walker($walker, $menu_id)
{
return 'Walker_Nav_Menu_Edit_Custom';
}
/**
* Copied from Walker_Nav_Menu_Edit class in core
*
* Create HTML list of nav menu input items.
*
* @package WordPress
* @since 3.0.0
* @uses Walker_Nav_Menu
*/
class Walker_Nav_Menu_Edit_Custom extends Walker_Nav_Menu
{
/**
* @see Walker::start_el()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param object $args
*/
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
{
wp_enqueue_script('jquery');
wp_enqueue_media();
global $_wp_nav_menu_max_depth;
$_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;
$indent = ($depth) ? str_repeat("\t", $depth) : '';
ob_start();
$item_id = esc_attr($item->ID);
$removed_args = array(
'action',
'customlink-tab',
'edit-menu-item',
'menu-item',
'page-tab',
'_wpnonce',
);
$original_title = '';
if ('taxonomy' == $item->type) {
$original_title = get_term_field('name', $item->object_id, $item->object, 'raw');
if (is_wp_error($original_title))
$original_title = false;
} elseif ('post_type' == $item->type) {
$original_object = get_post($item->object_id);
$original_title = $original_object->post_title;
}
$classes = array(
'menu-item menu-item-depth-' . $depth,
'menu-item-' . esc_attr($item->object),
'menu-item-edit-' . ((isset($_GET['edit-menu-item']) && $item_id == $_GET['edit-menu-item']) ? 'active' : 'inactive'),
);
$title = $item->title;
if (!empty($item->_invalid)) {
$classes[] = 'menu-item-invalid';
/* translators: %s: title of menu item which is invalid */
$title = sprintf(__('%s (Invalid)'), $item->title);
} elseif (isset($item->post_status) && 'draft' == $item->post_status) {
$classes[] = 'pending';
/* translators: %s: title of menu item in draft status */
$title = sprintf(__('%s (Pending)'), $item->title);
}
$title = empty($item->label) ? $title : $item->label;
?>
<li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes); ?>">
<dl class="menu-item-bar">
<dt class="menu-item-handle">
<span class="item-title"><?php echo esc_html($title); ?></span>
<span class="item-controls">
<span class="item-type"><?php echo esc_html($item->type_label); ?></span>
<?php error_log(print_r($item, true)); ?>
<span class="item-order hide-if-js">
<a href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'move-up-menu-item',
'menu-item' => $item_id,
),
remove_query_arg($removed_args, admin_url('nav-menus.php'))
),
'move-menu_item'
);
?>" class="item-move-up"><abbr title="<?php esc_attr_e('Move up'); ?>">↑</abbr></a>
|
<a href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'move-down-menu-item',
'menu-item' => $item_id,
),
remove_query_arg($removed_args, admin_url('nav-menus.php'))
),
'move-menu_item'
);
?>" class="item-move-down"><abbr title="<?php esc_attr_e('Move down'); ?>">↓</abbr></a>
</span>
<a class="item-edit" id="edit-<?php echo $item_id; ?>" title="<?php esc_attr_e('Edit Menu Item'); ?>" href="<?php
echo (isset($_GET['edit-menu-item']) && $item_id == $_GET['edit-menu-item']) ? admin_url('nav-menus.php') : add_query_arg('edit-menu-item', $item_id, remove_query_arg($removed_args, admin_url('nav-menus.php#menu-item-settings-' . $item_id)));
?>"><?php _e('Edit Menu Item'); ?></a>
</span>
</dt>
</dl>
<div class="menu-item-settings" style="height:400px;" id="menu-item-settings-<?php echo $item_id; ?>">
<?php if ('custom' == $item->type) : ?>
<p class="field-url description description-wide">
<label for="edit-menu-item-url-<?php echo $item_id; ?>">
<?php _e('URL'); ?><br />
<input type="text" id="edit-menu-item-url-<?php echo $item_id; ?>" class="widefat code edit-menu-item-url" name="menu-item-url[<?php echo $item_id; ?>]" value="<?php echo esc_attr($item->url); ?>" />
</label>
</p>
<?php endif; ?>
<p class="description description-thin">
<label for="edit-menu-item-title-<?php echo $item_id; ?>">
<?php _e('Navigation Label'); ?><br />
<input type="text" id="edit-menu-item-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-title" name="menu-item-title[<?php echo $item_id; ?>]" value="<?php echo esc_attr($item->title); ?>" />
</label>
</p>
<p class="description description-thin">
<label for="edit-menu-item-attr-title-<?php echo $item_id; ?>">
<?php _e('Title Attribute'); ?><br />
<input type="text" id="edit-menu-item-attr-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-attr-title" name="menu-item-attr-title[<?php echo $item_id; ?>]" value="<?php echo esc_attr($item->post_excerpt); ?>" />
</label>
</p>
<p class="field-link-target description">
<label for="edit-menu-item-target-<?php echo $item_id; ?>">
<input type="checkbox" id="edit-menu-item-target-<?php echo $item_id; ?>" value="_blank" name="menu-item-target[<?php echo $item_id; ?>]" <?php checked($item->target, '_blank'); ?> />
<?php _e('Open link in a new window/tab'); ?>
</label>
</p>
<p class="field-css-classes description description-thin">
<label for="edit-menu-item-classes-<?php echo $item_id; ?>">
<?php _e('CSS Classes (optional)'); ?><br />
<input type="text" id="edit-menu-item-classes-<?php echo $item_id; ?>" class="widefat code edit-menu-item-classes" name="menu-item-classes[<?php echo $item_id; ?>]" value="<?php echo esc_attr(implode(' ', $item->classes)); ?>" />
</label>
</p>
<p class="field-xfn description description-thin">
<label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
<?php _e('Link Relationship (XFN)'); ?><br />
<input type="text" id="edit-menu-item-xfn-<?php echo $item_id; ?>" class="widefat code edit-menu-item-xfn" name="menu-item-xfn[<?php echo $item_id; ?>]" value="<?php echo esc_attr($item->xfn); ?>" />
</label>
</p>
<p class="field-description description description-wide">
<label for="edit-menu-item-description-<?php echo $item_id; ?>">
<?php _e('Description'); ?><br />
<textarea id="edit-menu-item-description-<?php echo $item_id; ?>" class="widefat edit-menu-item-description" rows="3" cols="20" name="menu-item-description[<?php echo $item_id; ?>]"><?php echo esc_html($item->description); // textarea_escaped
?></textarea>
<span class="description"><?php _e('The description will be displayed in the menu if the current theme supports it.'); ?></span>
</label>
</p>
<?php
/*
* This is the added field
*/
?>
<p class="field-custom description description-wide">
<label for="edit-menu-item-custom-<?php echo $item_id; ?>">
<?php _e('Menu Color'); ?><br />
<?php ?>
<select name="menu-item-custom[<?php echo $item_id; ?>]" id="edit-menu-item-custom-<?php echo $item_id; ?>" class="widefat code edit-menu-item-custom">
<option value="green" <?php echo $item->custom == 'green' ? 'selected' : ''; ?>>Green</option>
<option value="yellow" <?php echo $item->custom == 'yellow' ? 'selected' : ''; ?>>Yellow</option>
<option value="blue" <?php echo $item->custom == 'blue' ? 'selected' : ''; ?>>Blue</option>
<option value="pink" <?php echo $item->custom == 'pink' ? 'selected' : ''; ?>>Pink</option>
<option value="orange" <?php echo $item->custom == 'orange' ? 'selected' : ''; ?>>Orange</option>
<option value="brightyellow" <?php echo $item->custom == 'brightyellow' ? 'selected' : ''; ?>>Bright Yellow</option>
<option value="light-orange" <?php echo $item->custom == 'light-orange' ? 'selected' : ''; ?>>Light Orange</option>
</select>
</label>
</p>
<p class="field-custom description description-wide">
<label for="edit-menu-item-custom-<?php echo $item_id; ?>">
<?php _e('Menu Image'); ?><br />
<?php $img = get_post_meta($item_id, '_menu_item_custom_image', true); ?>
<?php $text = get_post_meta($item_id, '_menu_item_custom_text', true); ?>
<?php $custom_url = get_post_meta($item_id, '_menu_item_custom_url', true); ?>
<?php $shortcode = get_post_meta($item_id, '_menu_item_custom_shortcode', true); ?>
<label for="image_url">Custom Text</label>
<input type="text" value="<?php echo $text; ?>" name="menu-item-custom-text[<?php echo $item_id; ?>]" id="menu-item-custom-text[<?php echo $item_id; ?>]" class="regular-text">
<label for="image_url">Custom Url</label>
<input type="text" value="<?php echo $custom_url; ?>" name="menu-item-custom-url[<?php echo $item_id; ?>]" id="menu-item-custom-url[<?php echo $item_id; ?>]" class="regular-text">
<label for="image_url">Custom Shortcode</label>
<input type="text" value="<?php echo $shortcode; ?>" name="menu-item-custom-shortcode[<?php echo $item_id; ?>]" id="menu-item-custom-shortcode[<?php echo $item_id; ?>]" class="regular-text">
<label for="image_url">Image</label>
<input type="text" value="<?php echo $img; ?>" name="menu-item-custom-image[<?php echo $item_id; ?>]" id="menu-item-custom-image[<?php echo $item_id; ?>]" class="regular-text">
<input type="button" name="upload-btn" id="upload-btn" class="button-secondary" value="Upload Image"> </label>
</p> <?php
/*
* end added field
*/
?>
<div class="menu-item-actions description-wide submitbox">
<?php if ('custom' != $item->type && $original_title !== false) : ?>
<p class="link-to-original">
<?php printf(__('Original: %s'), '<a href="' . esc_attr($item->url) . '">' . esc_html($original_title) . '</a>'); ?>
</p>
<?php endif; ?>
<a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'delete-menu-item',
'menu-item' => $item_id,
),
remove_query_arg($removed_args, admin_url('nav-menus.php'))
),
'delete-menu_item_' . $item_id
); ?>"><?php _e('Remove'); ?></a> <span class="meta-sep"> | </span> <a class="item-cancel submitcancel" id="cancel-<?php echo $item_id; ?>" href="<?php echo esc_url(add_query_arg(array('edit-menu-item' => $item_id, 'cancel' => time()), remove_query_arg($removed_args, admin_url('nav-menus.php'))));
?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a>
</div>
<input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
<input class="menu-item-data-object-id" type="hidden" name="menu-item-object-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr($item->object_id); ?>" />
<input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr($item->object); ?>" />
<input class="menu-item-data-parent-id" type="hidden" name="menu-item-parent-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr($item->menu_item_parent); ?>" />
<input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr($item->menu_order); ?>" />
<input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr($item->type); ?>" />
</div><!-- .menu-item-settings-->
<ul class="menu-item-transport"></ul>
<script>
jQuery(document).ready(function($) {
$('#upload-btn').click(function(e) {
e.preventDefault();
var image = wp.media({
title: 'Upload Image',
// mutiple: true if you want to upload multiple files at once
multiple: false
}).open()
.on('select', function(e) {
// This will return the selected image from the Media Uploader, the result is an object
var uploaded_image = image.state().get('selection').first();
// We convert uploaded_image to a JSON object to make accessing it easier
// Output to the console uploaded_image
console.log(uploaded_image);
var image_url = uploaded_image.toJSON().url;
// Let's assign the url value to the input field
$('#menu-item-custom-image[<?php echo $item_id; ?>]').val(image_url);
});
});
});
</script>
<?php
$output .= ob_get_clean();
}
}
?>