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