| 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( 'CSF_Nav_Menu_Options' ) ) { |
| 11 |
class CSF_Nav_Menu_Options extends CSF_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( "csf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this ); |
| 29 |
$this->sections = apply_filters( "csf_{$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( 'CSF_Walker_Nav_Menu_Edit' ) ) { |
| 51 |
CSF::include_plugin_file( 'functions/walker.php' ); |
| 52 |
} |
| 53 |
|
| 54 |
return 'CSF_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, '_csf_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, '_csf_errors_'. $this->unique ); |
| 105 |
} |
| 106 |
|
| 107 |
echo '<div class="csf csf-nav-menu-options'. esc_attr( $class ) .'">'; |
| 108 |
|
| 109 |
foreach ( $this->sections as $section ) { |
| 110 |
|
| 111 |
$section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-nav-menu-icon '. esc_attr( $section['icon'] ) .'"></i>' : ''; |
| 112 |
$section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : ''; |
| 113 |
|
| 114 |
echo '<div class="csf-fields">'; |
| 115 |
|
| 116 |
echo ( $section_title || $section_icon ) ? '<div class="csf-nav-menu-title"><h4>'. $section_icon . $section_title .'</h4></div>' : ''; |
| 117 |
echo ( ! empty( $section['description'] ) ) ? '<div class="csf-field csf-section-description">'. $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 |
CSF::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 |
// XSS ok. |
| 158 |
// No worries, This "POST" requests is sanitizing in the below foreach. |
| 159 |
$request = ( ! empty( $_POST[ $this->unique ][ $menu_item_db_id ] ) ) ? $_POST[ $this->unique ][ $menu_item_db_id ] : array(); |
| 160 |
|
| 161 |
if ( ! empty( $request ) ) { |
| 162 |
|
| 163 |
foreach ( $this->sections as $section ) { |
| 164 |
|
| 165 |
if ( ! empty( $section['fields'] ) ) { |
| 166 |
|
| 167 |
foreach ( $section['fields'] as $field ) { |
| 168 |
|
| 169 |
if ( ! empty( $field['id'] ) ) { |
| 170 |
|
| 171 |
$field_id = $field['id']; |
| 172 |
$field_value = isset( $request[$field_id] ) ? $request[$field_id] : ''; |
| 173 |
|
| 174 |
// Sanitize "post" request of field. |
| 175 |
if ( ! isset( $field['sanitize'] ) ) { |
| 176 |
|
| 177 |
if( is_array( $field_value ) ) { |
| 178 |
$data[$field_id] = wp_kses_post_deep( $field_value ); |
| 179 |
} else { |
| 180 |
$data[$field_id] = wp_kses_post( $field_value ); |
| 181 |
} |
| 182 |
|
| 183 |
} else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) { |
| 184 |
|
| 185 |
$data[$field_id] = call_user_func( $field['sanitize'], $field_value ); |
| 186 |
|
| 187 |
} else { |
| 188 |
|
| 189 |
$data[$field_id] = $field_value; |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
// Validate "post" request of field. |
| 194 |
if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) { |
| 195 |
|
| 196 |
$has_validated = call_user_func( $field['validate'], $field_value ); |
| 197 |
|
| 198 |
if ( ! empty( $has_validated ) ) { |
| 199 |
|
| 200 |
$errors['sections'][$count] = true; |
| 201 |
$errors['fields'][$field_id] = $has_validated; |
| 202 |
$data[$field_id] = $this->get_meta_value( $menu_item_db_id, $field ); |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
$count++; |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
$data = apply_filters( "csf_{$this->unique}_save", $data, $menu_item_db_id, $this ); |
| 221 |
|
| 222 |
do_action( "csf_{$this->unique}_save_before", $data, $menu_item_db_id, $this ); |
| 223 |
|
| 224 |
if ( empty( $data ) ) { |
| 225 |
|
| 226 |
if ( $this->args['data_type'] !== 'serialize' ) { |
| 227 |
foreach ( $this->pre_fields as $field ) { |
| 228 |
if ( ! empty( $field['id'] ) ) { |
| 229 |
delete_post_meta( $menu_item_db_id, $field['id'] ); |
| 230 |
} |
| 231 |
} |
| 232 |
} else { |
| 233 |
delete_post_meta( $menu_item_db_id, $this->unique ); |
| 234 |
} |
| 235 |
|
| 236 |
} else { |
| 237 |
|
| 238 |
if ( $this->args['data_type'] !== 'serialize' ) { |
| 239 |
foreach ( $data as $key => $value ) { |
| 240 |
update_post_meta( $menu_item_db_id, $key, $value ); |
| 241 |
} |
| 242 |
} else { |
| 243 |
update_post_meta( $menu_item_db_id, $this->unique, $data ); |
| 244 |
} |
| 245 |
|
| 246 |
if ( ! empty( $errors ) ) { |
| 247 |
update_post_meta( $menu_item_db_id, '_csf_errors_'. $this->unique, $errors ); |
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
do_action( "csf_{$this->unique}_saved", $data, $menu_item_db_id, $this ); |
| 253 |
|
| 254 |
do_action( "csf_{$this->unique}_save_after", $data, $menu_item_db_id, $this ); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
} |
| 259 |
} |
| 260 |
|