PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 4.2.21
Adminify – White Label, Admin Menu Editor, Login Customizer v4.2.21
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / Libs / adminify-framework / classes / nav-menu-options.class.php

nav-menu-options.class.php in Adminify – White Label, Admin Menu Editor, Login Customizer 4.2.21, at Libs/adminify-framework/classes/nav-menu-options.class.php

270 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
2 /**
3 *
4 * Nav Menu Options Class
5 *
6 * @since 1.0.0
7 * @version 1.0.0
8 *
9 */
10 if ( ! class_exists( 'ADMINIFY_Nav_Menu_Options' ) ) {
11 class ADMINIFY_Nav_Menu_Options extends ADMINIFY_Abstract{
12
13 // constans
14 public $unique = '';
15 public $abstract = 'menu';
16 public $sections = array();
17 public $pre_fields = array();
18 public $args = array(
19 'data_type' => 'serialize',
20 'class' => '',
21 'defaults' => array(),
22 );
23
24 // run menu construct
25 public function __construct( $key, $params ) {
26
27 $this->unique = $key;
28 $this->args = apply_filters( "adminify_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
29 $this->sections = apply_filters( "adminify_{$this->unique}_sections", $params['sections'], $this );
30 $this->pre_fields = $this->pre_fields( $this->sections );
31
32 add_action( 'wp_nav_menu_item_custom_fields', array( $this, 'wp_nav_menu_item_custom_fields' ), 10, 4 );
33 add_action( 'wp_update_nav_menu_item', array( $this, 'wp_update_nav_menu_item' ), 10, 3 );
34
35 add_filter( 'wp_edit_nav_menu_walker', array( $this, 'wp_edit_nav_menu_walker' ), 10, 2 );
36
37 }
38
39 // instance
40 public static function instance( $key, $params ) {
41 return new self( $key, $params );
42 }
43
44 public function wp_edit_nav_menu_walker( $class, $menu_id ) {
45
46 global $wp_version;
47
48 if( version_compare( $wp_version, '5.4.0', '<' ) ) {
49
50 if ( ! class_exists( 'ADMINIFY_Walker_Nav_Menu_Edit' ) ) {
51 ADMINIFY::include_plugin_file( 'functions/walker.php' );
52 }
53
54 return 'ADMINIFY_Walker_Nav_Menu_Edit';
55
56 }
57
58 return $class;
59
60 }
61
62 // get default value
63 public function get_default( $field ) {
64
65 $default = ( isset( $field['default'] ) ) ? $field['default'] : '';
66 $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
67
68 return $default;
69
70 }
71
72 // get meta value
73 public function get_meta_value( $menu_item_id, $field ) {
74
75 $value = null;
76
77 if ( ! empty( $menu_item_id ) && ! empty( $field['id'] ) ) {
78
79 if ( $this->args['data_type'] !== 'serialize' ) {
80 $meta = get_post_meta( $menu_item_id, $field['id'] );
81 $value = ( isset( $meta[0] ) ) ? $meta[0] : null;
82 } else {
83 $meta = get_post_meta( $menu_item_id, $this->unique, true );
84 $value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
85 }
86
87 }
88
89 $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
90 $value = ( isset( $value ) ) ? $value : $default;
91
92 return $value;
93
94 }
95
96 //
97 public function wp_nav_menu_item_custom_fields( $menu_item_id, $item, $depth, $args ) {
98
99 $errors = ( ! empty( $menu_item_id ) ) ? get_post_meta( $menu_item_id, '_adminify_errors_'. $this->unique, true ) : array();
100 $errors = ( ! empty( $errors ) ) ? $errors : array();
101 $class = ( $this->args['class'] ) ? ' '. $this->args['class'] : '';
102
103 if ( ! empty( $errors ) ) {
104 delete_post_meta( $menu_item_id, '_adminify_errors_'. $this->unique );
105 }
106
107 echo '<div class="adminify adminify-nav-menu-options'. esc_attr( $class ) .'">';
108
109 foreach ( $this->sections as $section ) {
110
111 $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-nav-menu-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
112 $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
113
114 echo '<div class="adminify-fields">';
115
116 echo ( $section_title || $section_icon ) ? '<div class="adminify-nav-menu-title"><h4>'. wp_kses_post( $section_icon ) . esc_html( $section_title ) .'</h4></div>' : '';
117 echo ( ! empty( $section['description'] ) ) ? '<div class="adminify-field adminify-section-description">'. wp_kses_post( $section['description'] ) .'</div>' : '';
118
119 if ( ! empty( $section['fields'] ) ) {
120
121 foreach ( $section['fields'] as $field ) {
122
123 if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
124 $field['_error'] = $errors['fields'][$field['id']];
125 }
126
127 if ( ! empty( $field['id'] ) ) {
128 $field['default'] = $this->get_default( $field );
129 }
130
131 ADMINIFY::field( $field, $this->get_meta_value( $menu_item_id, $field ), $this->unique .'['. $menu_item_id .']', 'menu' );
132
133 }
134
135 }
136
137 echo '</div>';
138
139 }
140
141 echo '</div>';
142
143 }
144
145 public function wp_update_nav_menu_item( $menu_id, $menu_item_db_id, $menu_item_args ) {
146
147 $count = 1;
148 $data = array();
149 $errors = array();
150 $noncekey = 'update-nav-menu-nonce';
151 $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
152
153 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'update-nav_menu' ) ) {
154 return $menu_item_db_id;
155 }
156
157 // Authorization: a valid nonce proves intent, not permission.
158 if ( ! current_user_can( 'edit_theme_options' ) ) {
159 return $menu_item_db_id;
160 }
161
162 // XSS ok.
163 // No worries, This "POST" requests is sanitizing in the below foreach.
164 $request = ( ! empty( $_POST[ $this->unique ][ $menu_item_db_id ] ) ) ? wp_unslash( $_POST[ $this->unique ][ $menu_item_db_id ] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- each field is sanitized individually by the framework's per-field sanitize handlers.
165
166 if ( ! empty( $request ) ) {
167
168 foreach ( $this->sections as $section ) {
169
170 if ( ! empty( $section['fields'] ) ) {
171
172 foreach ( $section['fields'] as $field ) {
173
174 if ( ! empty( $field['id'] ) ) {
175
176 $field_id = $field['id'];
177 $field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
178
179 // Sanitize "post" request of field.
180 if ( ! isset( $field['sanitize'] ) ) {
181
182 if( is_array( $field_value ) ) {
183 $data[$field_id] = wp_kses_post_deep( $field_value );
184 } else {
185 $data[$field_id] = wp_kses_post( $field_value );
186 }
187
188 } else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
189
190 $data[$field_id] = call_user_func( $field['sanitize'], $field_value );
191
192 } else {
193
194 // A sanitize callback was declared but is not callable; never store raw input.
195 if ( is_array( $field_value ) ) {
196 $data[$field_id] = wp_kses_post_deep( $field_value );
197 } else {
198 $data[$field_id] = wp_kses_post( $field_value );
199 }
200
201 }
202
203 // Validate "post" request of field.
204 if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
205
206 $has_validated = call_user_func( $field['validate'], $field_value );
207
208 if ( ! empty( $has_validated ) ) {
209
210 $errors['sections'][$count] = true;
211 $errors['fields'][$field_id] = $has_validated;
212 $data[$field_id] = $this->get_meta_value( $menu_item_db_id, $field );
213
214 }
215
216 }
217
218 }
219
220 }
221
222 }
223
224 $count++;
225
226 }
227
228 }
229
230 $data = apply_filters( "adminify_{$this->unique}_save", $data, $menu_item_db_id, $this );
231
232 do_action( "adminify_{$this->unique}_save_before", $data, $menu_item_db_id, $this );
233
234 if ( empty( $data ) ) {
235
236 if ( $this->args['data_type'] !== 'serialize' ) {
237 foreach ( $this->pre_fields as $field ) {
238 if ( ! empty( $field['id'] ) ) {
239 delete_post_meta( $menu_item_db_id, $field['id'] );
240 }
241 }
242 } else {
243 delete_post_meta( $menu_item_db_id, $this->unique );
244 }
245
246 } else {
247
248 if ( $this->args['data_type'] !== 'serialize' ) {
249 foreach ( $data as $key => $value ) {
250 update_post_meta( $menu_item_db_id, $key, $value );
251 }
252 } else {
253 update_post_meta( $menu_item_db_id, $this->unique, $data );
254 }
255
256 if ( ! empty( $errors ) ) {
257 update_post_meta( $menu_item_db_id, '_adminify_errors_'. $this->unique, $errors );
258 }
259
260 }
261
262 do_action( "adminify_{$this->unique}_saved", $data, $menu_item_db_id, $this );
263
264 do_action( "adminify_{$this->unique}_save_after", $data, $menu_item_db_id, $this );
265
266 }
267
268 }
269 }
270