class-admin-navmenu.php
9.48 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
<?php
namespace um\admin\core;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'um\admin\core\Admin_Navmenu' ) ) {
/**
* Class Admin_Navmenu
* @package um\admin\core
*/
class Admin_Navmenu {
/**
* @var array
*/
protected static $fields = array();
/**
* Admin_Navmenu constructor.
*/
function __construct() {
global $wp_version;
self::$fields = array(
'um_nav_public' => __( 'Display Mode', 'ultimate-member' ),
'um_nav_roles' => __( 'By Role', 'ultimate-member' )
);
if ( $wp_version < '5.4' ) {
add_action( 'admin_footer-nav-menus.php', array( &$this, '_wp_template' ) );
add_action( 'load-nav-menus.php', array( &$this, 'enqueue_nav_menus_scripts' ) );
} else {
add_action( 'load-customize.php', array( &$this, 'enqueue_nav_menus_scripts' ) );
}
add_action( 'wp_update_nav_menu_item', array( &$this, '_save' ), 10, 3 );
add_action( 'wp_nav_menu_item_custom_fields', array( $this, 'wp_nav_menu_item_custom_fields' ), 20, 5 );
//add_action( 'wp_nav_menu_item_custom_fields_customize_template', array( $this, 'wp_nav_menu_item_custom_fields_customize_template' ), 20 ); //waiting wp.org answer
}
/**
* Fires just before the move buttons of a nav menu item in the menu editor.
* Adds block "Ultimate Member Menu Settings"
*
* @since WP 5.4.0
* @hook wp_nav_menu_item_custom_fields
*
* @param int $item_id Menu item ID.
* @param \WP_Post $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param \stdClass $args An object of menu item arguments.
* @param int $id Nav menu ID.
*/
function wp_nav_menu_item_custom_fields( $item_id, $item, $depth, $args, $id = null ) {
$um_nav_public = get_post_meta( $item->ID, 'menu-item-um_nav_public', true );
$_nav_roles_meta = get_post_meta( $item->ID, 'menu-item-um_nav_roles', true );
$um_nav_roles = array();
if ( $_nav_roles_meta ) {
foreach ( $_nav_roles_meta as $key => $value ) {
if ( is_int( $key ) ) {
$um_nav_roles[] = $value;
}
}
}
$options = UM()->roles()->get_roles( false );
?>
<div class="um-nav-edit">
<div class="clear"></div>
<h4 style="margin-bottom: 0.6em;"><?php _e( 'Ultimate Member Menu Settings', 'ultimate-member' ) ?></h4>
<p class="description description-wide um-nav-mode">
<label for="edit-menu-item-um_nav_public-<?php echo esc_attr( $item_id ); ?>">
<?php _e( "Who can see this menu link?", 'ultimate-member' ); ?><br/>
<select id="edit-menu-item-um_nav_public-<?php echo esc_attr( $item_id ); ?>" name="menu-item-um_nav_public[<?php echo esc_attr( $item_id ); ?>]" style="width:100%;">
<option value="0" <?php selected( $um_nav_public, 0 ); ?>><?php _e( 'Everyone', 'ultimate-member' ) ?></option>
<option value="1" <?php selected( $um_nav_public, 1 ); ?>><?php _e( 'Logged Out Users', 'ultimate-member' ) ?></option>
<option value="2" <?php selected( $um_nav_public, 2 ); ?>><?php _e( 'Logged In Users', 'ultimate-member' ) ?></option>
</select>
</label>
</p>
<p class="description description-wide um-nav-roles" <?php echo $um_nav_public == 2 ? 'style="display: block;"' : ''; ?>><?php _e( "Select the member roles that can see this link", 'ultimate-member' ) ?><br>
<?php
$i = 0;
$html = '';
$columns = apply_filters( 'wp_nav_menu_item:um_nav_columns', 2, $item_id, $item );
$per_page = ceil( count( $options ) / $columns );
while ( $i < $columns ) {
$section_fields_per_page = array_slice( $options, $i * $per_page, $per_page );
$html .= '<span class="um-form-fields-section" style="width:' . floor( 100 / $columns ) . '% !important;">';
foreach ( $section_fields_per_page as $k => $title ) {
$id_attr = ' id="edit-menu-item-um_nav_roles-' . $item_id . '_' . $k . '" ';
$for_attr = ' for="edit-menu-item-um_nav_roles-' . $item_id . '_' . $k . '" ';
$checked_attr = checked( in_array($k,$um_nav_roles), true, false );
$html .= "<label {$for_attr}> <input type='checkbox' {$id_attr} name='menu-item-um_nav_roles[{$item_id}][{$k}]' value='1' {$checked_attr} /> <span>{$title}</span> </label>";
}
$html .= '</span>';
$i++;
}
echo $html;
?>
</p>
<div class="clear"></div>
</div>
<?php
}
/**
*
*/
function wp_nav_menu_item_custom_fields_customize_template() {
?>
<div class="um-nav-edit">
<div class="clear"></div>
<h4 style="margin-bottom: 0.6em;"><?php _e( 'Ultimate Member Menu Settings', 'ultimate-member' ) ?></h4>
<# console.log( data ); #>
<div class="clear"></div>
</div>
<?php
}
/**
*
* Backward compatibility with WP < 5.4
*
*/
/**
* @param int $menu_id
* @param int $menu_item_db_id
* @param array $menu_item_args
*/
function _save( $menu_id, $menu_item_db_id, $menu_item_args ) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
if ( empty( $_POST['menu-item-db-id'] ) || ! in_array( $menu_item_db_id, $_POST['menu-item-db-id'] ) ) {
return;
}
foreach ( self::$fields as $_key => $label ) {
$key = sprintf( 'menu-item-%s', $_key );
// Sanitize
if ( ! empty( $_POST[ $key ][ $menu_item_db_id ] ) ) {
// Do some checks here...
$value = is_array( $_POST[ $key ][ $menu_item_db_id ] ) ?
array_map( 'sanitize_key', array_keys( $_POST[ $key ][ $menu_item_db_id ] ) ) : (int) $_POST[ $key ][ $menu_item_db_id ];
} else {
$value = null;
}
// Update
if ( ! is_null( $value ) ) {
update_post_meta( $menu_item_db_id, $key, $value );
} else {
delete_post_meta( $menu_item_db_id, $key );
}
}
}
/**
*
*/
function enqueue_nav_menus_scripts() {
add_action( 'admin_enqueue_scripts', array( &$this, 'admin_enqueue_scripts' ) );
}
/**
*
*/
function admin_enqueue_scripts() {
UM()->admin_enqueue()->load_nav_manus_scripts();
$menu_restriction_data = array();
$menus = get_posts( 'post_type=nav_menu_item&numberposts=-1' );
foreach ( $menus as $data ) {
$_nav_roles_meta = get_post_meta( $data->ID, 'menu-item-um_nav_roles', true );
$um_nav_roles = array();
if ( $_nav_roles_meta ) {
foreach ( $_nav_roles_meta as $key => $value ) {
if ( is_int( $key ) ) {
$um_nav_roles[] = $value;
}
}
}
$menu_restriction_data[ $data->ID ] = array(
'um_nav_public' => get_post_meta( $data->ID, 'menu-item-um_nav_public', true ),
'um_nav_roles' => $um_nav_roles,
);
}
wp_localize_script( 'um_admin_nav_manus', 'um_menu_restriction_data', $menu_restriction_data );
}
/**
*
*/
function _wp_template() {
?>
<script type="text/html" id="tmpl-um-nav-menus-fields">
<div class="um-nav-edit">
<div class="clear"></div>
<h4 style="margin-bottom: 0.6em;"><?php _e( "Ultimate Member Menu Settings", 'ultimate-member' ) ?></h4>
<p class="description description-wide um-nav-mode">
<label for="edit-menu-item-um_nav_public-{{data.menuItemID}}">
<?php _e( "Who can see this menu link?", 'ultimate-member' ); ?><br/>
<select id="edit-menu-item-um_nav_public-{{data.menuItemID}}"
name="menu-item-um_nav_public[{{data.menuItemID}}]" style="width:100%;">
<option value="0" <# if( data.restriction_data.um_nav_public == '0' ){ #>selected="selected"<# } #>>
<?php _e( 'Everyone', 'ultimate-member' ) ?>
</option>
<option value="1" <# if( data.restriction_data.um_nav_public == '1' ){ #>selected="selected"<# } #>>
<?php _e( 'Logged Out Users', 'ultimate-member' ) ?>
</option>
<option value="2" <# if( data.restriction_data.um_nav_public == '2' ){ #>selected="selected"<# } #>>
<?php _e( 'Logged In Users', 'ultimate-member' ) ?>
</option>
</select>
</label>
</p>
<p class="description description-wide um-nav-roles" <# if( data.restriction_data.um_nav_public == '2' ){ #>style="display: block;"<# } #>>
<?php _e( "Select the member roles that can see this link", 'ultimate-member' ) ?><br/>
<?php $options = UM()->roles()->get_roles( false );
$i = 0;
$html = '';
$columns = 2;
while ( $i < $columns ) {
$per_page = ceil( count( $options ) / $columns );
$section_fields_per_page = array_slice( $options, $i * $per_page, $per_page );
$html .= '<span class="um-form-fields-section" style="width:' . floor( 100 / $columns ) . '% !important;">';
foreach ( $section_fields_per_page as $k => $title ) {
$id_attr = ' id="edit-menu-item-um_nav_roles-{{data.menuItemID}}_' . $k . '" ';
$for_attr = ' for="edit-menu-item-um_nav_roles-{{data.menuItemID}}_' . $k . '" ';
$html .= "<label $for_attr>
<input type='checkbox' {$id_attr} name='menu-item-um_nav_roles[{{data.menuItemID}}][{$k}]' value='1' <# if( _.contains( data.restriction_data.um_nav_roles,'{$k}' ) ){ #>checked='checked'<# } #> />
<span>{$title}</span>
</label>";
}
$html .= '</span>';
$i++;
}
echo $html; ?>
</p>
<div class="clear"></div>
</div>
</script>
<?php
}
}
}