-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathacf_location_nav_menu.php
237 lines (174 loc) · 5.23 KB
/
acf_location_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
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
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class acf_location_nav_menu extends acf_location {
/*
* Construct
*
* @description:
* @since: 1.0
* @created: 2/07/15
* @updated 7/1/16
*/
function __construct() {
add_filter( 'acf/location/rule_match/menu', array( $this, 'acf_location_nav_menu_rule_matches' ), 1, 3 );
add_filter( 'acf/location/rule_types', array( $this, 'acf_location_nav_menu_rule_type' ), 1, 1 );
add_filter( 'acf/location/rule_values/menu', array( $this, 'acf_location_nav_menu_rule_value' ), 1, 1 );
add_action('acf/field_group/admin_head', array( $this, 'admin_head' ), 0, 0 );
wp_register_style( 'acf-location-nav-menu', plugin_dir_url( __FILE__ ).'css/acf-location-nav-menu.css' );
wp_register_script( 'google-map-fix-randering', plugin_dir_url( __FILE__ ).'js/google.map.fix.randering.js', array( 'jquery' ) );
wp_register_script( 'acf-setup-fields-in-right-level', plugin_dir_url( __FILE__ ).'js/acf.setup.fields.in.right.level.js', array( 'jquery' ) );
}
/*
* acf_location_nav_menu_rule_matches
*
* This function will match a location rule and return true or false
*
* @type filter
* @date 2/07/15
* @since 1.0
*
* @param $match (boolean)
* @param $rule (array)
* @return $options (array)
*/
function acf_location_nav_menu_rule_matches( $match, $rule, $options ) {
$selected_menu_id = $rule['value'];
$menu = isset($options['menu']) ? $options['menu'] : '';
if( $menu ) {
if( $rule['operator'] == "==" ) {
$match = ( $menu == $selected_menu_id );
// override for "all"
if( $selected_menu_id === 'all' ) {
$match = true;
}
} elseif( $rule['operator'] == "!=" ) {
$match = ( $menu != $selected_menu_id );
// override for "all"
if( $selected_menu_id === 'all' ) {
$match = false;
}
}
}
// return
return $match;
}
/*
* acf_location_nav_menu_rule_type
*
* This function will define a location rule type
*
* @type filter
* @date 2/07/15
* @since 1.0
*
* @param $choices (array)
* @return $choices (array)
*/
function acf_location_nav_menu_rule_type( $choices ) {
$choices['Forms']['menu'] = __( 'Nav Menu', 'acf-location-nav-menu' );
return $choices;
}
/*
* acf_location_nav_menu_rule_type
*
* This function will add list of nav menus to location rules
*
* @type filter
* @date 2/07/15
* @since 1.0
*
* @param $choices (array)
* @return $choices (array)
*/
function acf_location_nav_menu_rule_value( $choices ) {
$choices = array(
'all' => __( 'All', 'acf-location-nav-menu' ),
);
$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
foreach( $menus as $menu ) {
$choices[ $menu->term_id ] = $menu->name;
}
return $choices;
}
/*
* admin_head
*
* This function will add metabox to fields group settings screen
*
* @type action
* @date 25/12/15
* @since 1.1
*
*/
function admin_head() {
add_meta_box('acf-menu-location-settings', __("Menu Location Settings",'acf'), array($this, 'render_menu_location_settings'), 'acf-field-group', 'side', 'default');
}
/*
* admin_head
*
* This function will add metabox to fields group settings screen
*
* @date 25/12/15
* @since 1.1
* @update 7/1/16
*/
function render_menu_location_settings( $field_group ) {
global $field_group;
// field key (leave in for compatibility)
if( !acf_is_field_group_key( $field_group['key']) ) {
$field_group['key'] = uniqid('group_');
} ?>
<?php // menu_item_level
acf_render_field_wrap(array(
'label' => __('Menu Item Level','acf-location-nav-menu'),
'instructions' => '',
'type' => 'select',
'name' => 'menu_item_level',
'prefix' => 'acf_field_group',
'value' => isset( $field_group['menu_item_level'] ) ? $field_group['menu_item_level'] : 'all',
'choices' => array(
'all' => __("All",'acf-location-nav-menu'),
'first' => __("First level only",'acf-location-nav-menu'),
'second' => __("Second level only",'acf-location-nav-menu'),
'third' => __("Third level only",'acf-location-nav-menu'),
'not-first' => __("All except first",'acf-location-nav-menu'),
)
)); ?>
<div class="acf-hidden">
<input type="hidden" name="acf_field_group[key]" value="<?php echo $field_group['key']; ?>" />
</div>
<script type="text/javascript">
//added 7/1/16
jQuery(function() {
show_location_settings();
});
if( typeof acf !== 'undefined' ) {
acf.postbox.render({
'id': 'acf-menu-location-settings',
'label': 'left'
});
}
//added 7/1/16
function show_location_settings() {
jQuery('#acf-menu-location-settings').hide();
jQuery('.location-rule-param').each(function() {
var locationRuleParam = jQuery(this);
if (locationRuleParam.val() == 'menu') {
jQuery('#acf-menu-location-settings').show();
}
locationRuleParam.on('change', function() {
if (jQuery(this).val() == 'menu') {
jQuery('#acf-menu-location-settings').show();
} else {
jQuery('#acf-menu-location-settings').hide();
}
});
});
}
</script>
<?php }
}
new acf_location_nav_menu();