-
Notifications
You must be signed in to change notification settings - Fork 1
/
nav-menu-v5.php
189 lines (162 loc) · 4.83 KB
/
nav-menu-v5.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
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Nav Menu Field v5
*
* @package ACF Nav Menu Field
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* ACF_Field_Nav_Menu_V5 Class
*
* This class contains all the custom workings for the Nav Menu Field for ACF v5
*/
class ACF_Field_Nav_Menu_V5 extends acf_field {
/**
* Sets up some default values and delegats work to the parent constructor.
*/
public function __construct() {
$this->name = 'nav_menu';
$this->label = __( 'Nav Menu' );
$this->category = 'relational';
$this->defaults = array(
'save_format' => 'id',
'allow_null' => 0,
'container' => 'div',
);
parent::__construct();
}
/**
* Renders the Nav Menu Field options seen when editing a Nav Menu Field.
*
* @param array $field The array representation of the current Nav Menu Field.
*/
public function render_field_settings( $field ) {
// Register the Return Value format setting
acf_render_field_setting( $field, array(
'label' => __( 'Return Value' ),
'instructions' => __( 'Specify the returned value on front end' ),
'type' => 'radio',
'name' => 'save_format',
'layout' => 'horizontal',
'choices' => array(
'object' => __( 'Nav Menu Object' ),
'menu' => __( 'Nav Menu HTML' ),
'id' => __( 'Nav Menu ID' ),
),
) );
// Register the Menu Container setting
acf_render_field_setting( $field, array(
'label' => __( 'Menu Container' ),
'instructions' => __( "What to wrap the Menu's ul with (when returning HTML only)" ),
'type' => 'select',
'name' => 'container',
'choices' => $this->get_allowed_nav_container_tags(),
) );
// Register the Allow Null setting
acf_render_field_setting( $field, array(
'label' => __( 'Allow Null?' ),
'type' => 'radio',
'name' => 'allow_null',
'layout' => 'horizontal',
'choices' => array(
1 => __( 'Yes' ),
0 => __( 'No' ),
),
) );
}
/**
* Get the allowed wrapper tags for use with wp_nav_menu().
*
* @return array An array of allowed wrapper tags.
*/
private function get_allowed_nav_container_tags() {
$tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
$formatted_tags = array(
'0' => 'None',
);
foreach ( $tags as $tag ) {
$formatted_tags[$tag] = ucfirst( $tag );
}
return $formatted_tags;
}
/**
* Renders the Nav Menu Field.
*
* @param array $field The array representation of the current Nav Menu Field.
*/
public function render_field( $field ) {
$allow_null = $field['allow_null'];
$nav_menus = $this->get_nav_menus( $allow_null );
if ( empty( $nav_menus ) ) {
return;
}
?>
<select id="<?php esc_attr( $field['id'] ); ?>" class="<?php echo esc_attr( $field['class'] ); ?>" name="<?php echo esc_attr( $field['name'] ); ?>">
<?php foreach( $nav_menus as $nav_menu_id => $nav_menu_name ) : ?>
<option value="<?php echo esc_attr( $nav_menu_id ); ?>" <?php selected( $field['value'], $nav_menu_id ); ?>>
<?php echo esc_html( $nav_menu_name ); ?>
</option>
<?php endforeach; ?>
</select>
<?php
}
/**
* Gets a list of Nav Menus indexed by their Nav Menu IDs.
*
* @param bool $allow_null If true, prepends the null option.
*
* @return array An array of Nav Menus indexed by their Nav Menu IDs.
*/
private function get_nav_menus( $allow_null = false ) {
$navs = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
$nav_menus = array();
if ( $allow_null ) {
$nav_menus[''] = ' - Select - ';
}
foreach ( $navs as $nav ) {
$nav_menus[ $nav->term_id ] = $nav->name;
}
return $nav_menus;
}
/**
* Renders the Nav Menu Field.
*
* @param int $value The Nav Menu ID selected for this Nav Menu Field.
* @param int $post_id The Post ID this $value is associated with.
* @param array $field The array representation of the current Nav Menu Field.
*
* @return mixed The Nav Menu ID, or the Nav Menu HTML, or the Nav Menu Object, or false.
*/
public function format_value( $value, $post_id, $field ) {
// bail early if no value
if ( empty( $value ) ) {
return false;
}
// check format
if ( 'object' == $field['save_format'] ) {
$wp_menu_object = wp_get_nav_menu_object( $value );
if( empty( $wp_menu_object ) ) {
return false;
}
$menu_object = new stdClass;
$menu_object->ID = $wp_menu_object->term_id;
$menu_object->name = $wp_menu_object->name;
$menu_object->slug = $wp_menu_object->slug;
$menu_object->count = $wp_menu_object->count;
return $menu_object;
} elseif ( 'menu' == $field['save_format'] ) {
ob_start();
wp_nav_menu( array(
'menu' => $value,
'container' => $field['container']
) );
return ob_get_clean();
}
// Just return the Nav Menu ID
return $value;
}
}
new ACF_Field_Nav_Menu_V5();