PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 3.1.9
Adminify – White Label, Admin Menu Editor, Login Customizer v3.1.9
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 3.1.9, at Libs/adminify-framework/classes/nav-menu-options.class.php

211 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if ( ! defined( 'ABSPATH' ) ) {
2 die; } // Cannot access directly.
3 /**
4 *
5 * Nav Menu Options Class
6 *
7 * @since 1.0.0
8 * @version 1.0.0
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 = [];
17 public $args = [
18 'data_type' => 'serialize',
19 'class' => '',
20 'defaults' => [],
21 ];
22
23 // run menu construct
24 public function __construct( $key, $params ) {
25 $this->unique = $key;
26 $this->args = apply_filters( "adminify_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
27 $this->sections = apply_filters( "adminify_{$this->unique}_sections", $params['sections'], $this );
28
29 add_action( 'wp_nav_menu_item_custom_fields', [ $this, 'wp_nav_menu_item_custom_fields' ], 10, 4 );
30 add_action( 'wp_update_nav_menu_item', [ $this, 'wp_update_nav_menu_item' ], 10, 3 );
31
32 add_filter( 'wp_edit_nav_menu_walker', [ $this, 'wp_edit_nav_menu_walker' ], 10, 2 );
33 }
34
35 // instance
36 public static function instance( $key, $params ) {
37 return new self( $key, $params );
38 }
39
40 public function wp_edit_nav_menu_walker( $class, $menu_id ) {
41 global $wp_version;
42
43 if ( version_compare( $wp_version, '5.4.0', '<' ) ) {
44 if ( ! class_exists( 'ADMINIFY_Walker_Nav_Menu_Edit' ) ) {
45 ADMINIFY::include_plugin_file( 'functions/walker.php' );
46 }
47
48 return 'ADMINIFY_Walker_Nav_Menu_Edit';
49 }
50
51 return $class;
52 }
53
54 // get default value
55 public function get_default( $field ) {
56 $default = ( isset( $field['default'] ) ) ? $field['default'] : '';
57 $default = ( isset( $this->args['defaults'][ $field['id'] ] ) ) ? $this->args['defaults'][ $field['id'] ] : $default;
58
59 return $default;
60 }
61
62 // get meta value
63 public function get_meta_value( $menu_item_id, $field ) {
64 $value = null;
65
66 if ( ! empty( $menu_item_id ) && ! empty( $field['id'] ) ) {
67 if ( $this->args['data_type'] !== 'serialize' ) {
68 $meta = get_post_meta( $menu_item_id, $field['id'] );
69 $value = ( isset( $meta[0] ) ) ? $meta[0] : null;
70 } else {
71 $meta = get_post_meta( $menu_item_id, $this->unique, true );
72 $value = ( isset( $meta[ $field['id'] ] ) ) ? $meta[ $field['id'] ] : null;
73 }
74 }
75
76 $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
77 $value = ( isset( $value ) ) ? $value : $default;
78
79 return $value;
80 }
81
82 public function wp_nav_menu_item_custom_fields( $menu_item_id, $item, $depth, $args ) {
83 $errors = ( ! empty( $menu_item_id ) ) ? get_post_meta( $menu_item_id, '_adminify_errors_' . $this->unique, true ) : [];
84 $errors = ( ! empty( $errors ) ) ? $errors : [];
85 $class = ( $this->args['class'] ) ? ' ' . $this->args['class'] : '';
86
87 if ( ! empty( $errors ) ) {
88 delete_post_meta( $menu_item_id, '_adminify_errors_' . $this->unique );
89 }
90
91 echo '<div class="adminify adminify-nav-menu-options' . esc_attr( $class ) . '">';
92
93 foreach ( $this->sections as $section ) {
94 $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-nav-menu-icon ' . esc_attr( $section['icon'] ) . '"></i>' : '';
95 $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
96
97 echo '<div class="adminify-fields">';
98
99 echo ( $section_title || $section_icon ) ? '<div class="adminify-nav-menu-title"><h4>' . esc_html( $section_icon ) . esc_html( $section_title ) . '</h4></div>' : '';
100 echo ( ! empty( $section['description'] ) ) ? '<div class="adminify-field adminify-section-description">' . esc_html( $section['description'] ) . '</div>' : '';
101
102 if ( ! empty( $section['fields'] ) ) {
103 foreach ( $section['fields'] as $field ) {
104 if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][ $field['id'] ] ) ) {
105 $field['_error'] = $errors['fields'][ $field['id'] ];
106 }
107
108 if ( ! empty( $field['id'] ) ) {
109 $field['default'] = $this->get_default( $field );
110 }
111
112 ADMINIFY::field( $field, $this->get_meta_value( $menu_item_id, $field ), $this->unique . '[' . $menu_item_id . ']', 'menu' );
113 }
114 }
115
116 echo '</div>';
117 }
118
119 echo '</div>';
120 }
121
122 public function wp_update_nav_menu_item( $menu_id, $menu_item_db_id, $menu_item_args ) {
123 $count = 1;
124 $data = [];
125 $errors = [];
126 $noncekey = 'update-nav-menu-nonce';
127 $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
128
129 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'update-nav_menu' ) ) {
130 return $menu_item_db_id;
131 }
132
133 if ( ! empty( $_POST[ $this->unique ][ $menu_item_db_id ] ) ) {
134 $request = sanitize_text_field( wp_unslash( $_POST[ $this->unique ][ $menu_item_db_id ] ) );
135 } else {
136 $request = [];
137 }
138
139 if ( ! empty( $request ) ) {
140 foreach ( $this->sections as $section ) {
141 if ( ! empty( $section['fields'] ) ) {
142 foreach ( $section['fields'] as $field ) {
143 if ( ! empty( $field['id'] ) ) {
144 $field_id = $field['id'];
145 $field_value = isset( $request[ $field_id ] ) ? $request[ $field_id ] : '';
146
147 // Sanitize "post" request of field.
148 if ( ! isset( $field['sanitize'] ) ) {
149 if ( is_array( $field_value ) ) {
150 $data[ $field_id ] = wp_kses_post_deep( $field_value );
151 } else {
152 $data[ $field_id ] = wp_kses_post( $field_value );
153 }
154 } elseif ( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
155 $data[ $field_id ] = call_user_func( $field['sanitize'], $field_value );
156 } else {
157 $data[ $field_id ] = $field_value;
158 }
159
160 // Validate "post" request of field.
161 if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
162 $has_validated = call_user_func( $field['validate'], $field_value );
163
164 if ( ! empty( $has_validated ) ) {
165 $errors['sections'][ $count ] = true;
166 $errors['fields'][ $field_id ] = $has_validated;
167 $data[ $field_id ] = $this->get_meta_value( $menu_item_db_id, $field );
168 }
169 }
170 }
171 }
172 }
173
174 $count++;
175 }
176 }
177
178 $data = apply_filters( "adminify_{$this->unique}_save", $data, $menu_item_db_id, $this );
179
180 do_action( "adminify_{$this->unique}_save_before", $data, $menu_item_db_id, $this );
181
182 if ( empty( $data ) ) {
183 if ( $this->args['data_type'] !== 'serialize' ) {
184 foreach ( $data as $key => $value ) {
185 delete_post_meta( $menu_item_db_id, $key );
186 }
187 } else {
188 delete_post_meta( $menu_item_db_id, $this->unique );
189 }
190 } else {
191 if ( $this->args['data_type'] !== 'serialize' ) {
192 foreach ( $data as $key => $value ) {
193 update_post_meta( $menu_item_db_id, $key, $value );
194 }
195 } else {
196 update_post_meta( $menu_item_db_id, $this->unique, $data );
197 }
198
199 if ( ! empty( $errors ) ) {
200 update_post_meta( $menu_item_db_id, '_adminify_errors_' . $this->unique, $errors );
201 }
202 }
203
204 do_action( "adminify_{$this->unique}_saved", $data, $menu_item_db_id, $this );
205
206 do_action( "adminify_{$this->unique}_save_after", $data, $menu_item_db_id, $this );
207 }
208
209 }
210 }
211