| 1 |
<?php |
| 2 |
namespace QuadLayers\QuadMenu\Backend; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
die( '-1' ); |
| 6 |
} |
| 7 |
|
| 8 |
use QuadLayers\QuadMenu\Plugin; |
| 9 |
use QuadLayers\QuadMenu\Backend\Settings; |
| 10 |
use QuadLayers\QuadMenu\Configuration; |
| 11 |
|
| 12 |
/** |
| 13 |
* Ajax Class ex QuadMenu_Ajax |
| 14 |
*/ |
| 15 |
class Ajax extends Settings { |
| 16 |
|
| 17 |
public static $instance; |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
|
| 21 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 10 ); |
| 22 |
|
| 23 |
// Ajax |
| 24 |
// ----------------------------------------------------------------- |
| 25 |
|
| 26 |
add_action( 'wp_ajax_quadmenu_add_nav_menu_item', array( $this, 'ajax_add_nav_menu_item' ) ); |
| 27 |
|
| 28 |
add_action( 'wp_ajax_quadmenu_delete_nav_menu_item', array( $this, 'ajax_delete_nav_menu_item' ) ); |
| 29 |
|
| 30 |
add_action( 'wp_ajax_quadmenu_update_nav_menu_item', array( $this, 'ajax_update_nav_menu_item_order' ) ); |
| 31 |
|
| 32 |
add_action( 'wp_ajax_quadmenu_add_nav_menu_item_settings', array( $this, 'ajax_add_nav_menu_item_settings' ) ); |
| 33 |
|
| 34 |
add_action( 'wp_ajax_quadmenu_save_nav_menu_item_settings', array( $this, 'ajax_save_nav_menu_item_settings' ) ); |
| 35 |
|
| 36 |
// WP |
| 37 |
// --------------------------------------------------------------------- |
| 38 |
|
| 39 |
add_action( 'wp_update_nav_menu_item', array( $this, 'update_nav_menu_item' ), 10, 3 ); |
| 40 |
|
| 41 |
// Nav Menu |
| 42 |
// ----------------------------------------------------------------- |
| 43 |
|
| 44 |
add_meta_box( 'quadmenu_nav_menu_items', esc_html__( 'QuadMenu Items', 'quadmenu' ), array( $this, 'nav_items' ), 'nav-menus', 'side', 'high' ); |
| 45 |
|
| 46 |
add_meta_box( 'quadmenu_nav_menu_archives', esc_html__( 'QuadMenu Archives', 'quadmenu' ), array( $this, 'nav_archives' ), 'nav-menus', 'side', 'high' ); |
| 47 |
} |
| 48 |
|
| 49 |
public function enqueue() { |
| 50 |
|
| 51 |
global $pagenow; |
| 52 |
|
| 53 |
if ( $pagenow != 'nav-menus.php' ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
wp_enqueue_media(); |
| 58 |
|
| 59 |
wp_enqueue_style( 'quadmenu-admin' ); |
| 60 |
|
| 61 |
wp_enqueue_script( 'quadmenu-admin' ); |
| 62 |
|
| 63 |
wp_localize_script( |
| 64 |
'quadmenu-admin', |
| 65 |
'quadmenu', |
| 66 |
array( |
| 67 |
'nonce' => wp_create_nonce( 'quadmenu' ), |
| 68 |
'add_name' => QUADMENU_PLUGIN_NAME, |
| 69 |
'add_background' => esc_html( 'Add Background', 'quadmenu' ), |
| 70 |
'add_to_column' => esc_html( 'Add to Column', 'quadmenu' ), |
| 71 |
) |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
public function nav_items() { |
| 76 |
global $_nav_menu_placeholder, $nav_menu_selected_id; |
| 77 |
|
| 78 |
$items = Configuration::custom_nav_menu_items(); |
| 79 |
?> |
| 80 |
<div id="posttype-quadmenu-custom" class="posttypediv"> |
| 81 |
<div id="tabs-panel-quadmenu-custom" class="tabs-panel tabs-panel-active"> |
| 82 |
<ul id ="quadmenu-custom-checklist" class="categorychecklist form-no-clear"> |
| 83 |
<?php |
| 84 |
if ( is_object( $items ) ) : |
| 85 |
|
| 86 |
foreach ( $items as $id => $item ) : |
| 87 |
|
| 88 |
if ( ! isset( $item->depth ) || $item->depth > 0 ) { |
| 89 |
continue; |
| 90 |
} |
| 91 |
|
| 92 |
$classes = array(); |
| 93 |
|
| 94 |
$classes[] = 'quadmenu-item'; |
| 95 |
|
| 96 |
$classes[] = 'quadmenu-item-' . $id; |
| 97 |
|
| 98 |
if ( isset( $item->depth ) ) { |
| 99 |
$classes[] = 'quadmenu-item-depth-' . $item->depth; |
| 100 |
} |
| 101 |
|
| 102 |
$title = $id; |
| 103 |
|
| 104 |
if ( isset( $item->title ) ) { |
| 105 |
$title = $item->title; |
| 106 |
} |
| 107 |
|
| 108 |
$_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval( $_nav_menu_placeholder ) - 1 : -1; |
| 109 |
?> |
| 110 |
|
| 111 |
<li class="<?php echo join( ' ', array_map( 'sanitize_html_class', $classes ) ); ?>"> |
| 112 |
<label class="menu-item-title"> |
| 113 |
<input type="checkbox" class="menu-item-checkbox" name="menu-item[<?php echo esc_attr( $_nav_menu_placeholder ); ?>][menu-item-label]" value="0"> |
| 114 |
<?php echo esc_html( $title ); ?> |
| 115 |
<?php if ( ! empty( $item->desc ) ) : ?> |
| 116 |
<small><?php echo $item->desc; ?></small> |
| 117 |
<?php endif; ?> |
| 118 |
</label> |
| 119 |
<input type="hidden" class="menu-item-type" name="menu-item[<?php echo esc_attr( $_nav_menu_placeholder ); ?>][menu-item-type]" value="custom"> |
| 120 |
<input type="hidden" class="menu-item-title" name="menu-item[<?php echo esc_attr( $_nav_menu_placeholder ); ?>][menu-item-title]" value="<?php echo esc_html( $title ); ?>"> |
| 121 |
<input type="hidden" class="menu-item-quadmenu" name="menu-item[<?php echo esc_attr( $_nav_menu_placeholder ); ?>][menu-item-quadmenu]" value="<?php echo esc_attr( $id ); ?>"> |
| 122 |
</li> |
| 123 |
|
| 124 |
<?php endforeach; ?> |
| 125 |
|
| 126 |
<?php endif; ?> |
| 127 |
</ul> |
| 128 |
</div> |
| 129 |
<p class="button-controls"> |
| 130 |
<span class="add-to-menu"> |
| 131 |
<input type="submit" class="button-secondary submit-add-to-quadmenu right" value="<?php esc_html_e( 'Add to Menu' ); ?>" name="submit-posttype-quadmenu-custom" id="submit-posttype-quadmenu-custom"> |
| 132 |
<span class="spinner"></span> |
| 133 |
</span> |
| 134 |
</p> |
| 135 |
</div> |
| 136 |
<?php |
| 137 |
} |
| 138 |
|
| 139 |
public function nav_archives() { |
| 140 |
|
| 141 |
$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' ); |
| 142 |
|
| 143 |
if ( $post_types ) : |
| 144 |
$items = array(); |
| 145 |
$loop_index = 999999; |
| 146 |
|
| 147 |
foreach ( $post_types as $post_type ) { |
| 148 |
$item = new \stdClass(); |
| 149 |
++$loop_index; |
| 150 |
|
| 151 |
$item->object_id = $loop_index; |
| 152 |
$item->db_id = 0; |
| 153 |
$item->object = $post_type->name; |
| 154 |
$item->menu_item_parent = 0; |
| 155 |
$item->type = 'post_type_archive'; |
| 156 |
$item->title = sprintf( esc_html__( 'All %s', 'quadmenu' ), str_replace( 'All ', '', $post_type->labels->name ) ); |
| 157 |
$item->url = get_post_type_archive_link( $post_type->query_var ); |
| 158 |
$item->target = ''; |
| 159 |
$item->attr_title = ''; |
| 160 |
$item->classes = array(); |
| 161 |
$item->xfn = ''; |
| 162 |
|
| 163 |
$items[] = $item; |
| 164 |
} |
| 165 |
|
| 166 |
$walker = new \Walker_Nav_Menu_Checklist( array() ); |
| 167 |
?> |
| 168 |
|
| 169 |
<div id="posttype-archive" class="posttypediv"> |
| 170 |
<div id="tabs-panel-posttype-archive" class="tabs-panel tabs-panel-active"> |
| 171 |
<ul id="posttype-archive-checklist" class="categorychecklist form-no-clear"> |
| 172 |
<?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $items ), 0, (object) array( 'walker' => $walker ) ); ?> |
| 173 |
</ul> |
| 174 |
</div> |
| 175 |
</div> |
| 176 |
|
| 177 |
<p class="button-controls"> |
| 178 |
<span class="add-to-menu"> |
| 179 |
<input type="submit"<?php disabled( 1, 0 ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_html_e( 'Add to Menu' ); ?>" name="add-posttype-archive-menu-item" id="submit-posttype-archive" /> |
| 180 |
<span class="spinner"></span> |
| 181 |
</span> |
| 182 |
</p> |
| 183 |
|
| 184 |
<?php |
| 185 |
endif; |
| 186 |
} |
| 187 |
|
| 188 |
public function ajax_add_nav_menu_item() { |
| 189 |
|
| 190 |
if ( ! check_ajax_referer( 'quadmenu', 'nonce', false ) ) { |
| 191 |
Plugin::send_json_error( esc_html__( 'Please reload the page.', 'quadmenu' ) ); |
| 192 |
} |
| 193 |
|
| 194 |
if ( empty( $_REQUEST['menu-item'] ) ) { |
| 195 |
Plugin::send_json_error( esc_html__( 'Undefined menu-item var.', 'quadmenu' ) ); |
| 196 |
wp_die(); |
| 197 |
} |
| 198 |
|
| 199 |
require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; |
| 200 |
|
| 201 |
// For performance reasons, we omit some object properties from the checklist. |
| 202 |
// The following is a hacky way to restore them when adding non-custom items. |
| 203 |
|
| 204 |
$menu_items_data = array(); |
| 205 |
|
| 206 |
foreach ( (array) $_REQUEST['menu-item'] as $menu_item_data ) { |
| 207 |
$menu_items_data[] = $menu_item_data; |
| 208 |
} |
| 209 |
|
| 210 |
$menu_id = absint( $_REQUEST['menu_id'] ); |
| 211 |
|
| 212 |
$item_ids = $this->ajax_create_nav_menu_items( $menu_id, $menu_items_data ); |
| 213 |
|
| 214 |
if ( is_wp_error( $item_ids ) ) { |
| 215 |
wp_die(); |
| 216 |
} |
| 217 |
|
| 218 |
$menu_items = array(); |
| 219 |
|
| 220 |
foreach ( (array) $item_ids as $menu_item_id ) { |
| 221 |
|
| 222 |
$menu_obj = Plugin::wp_setup_nav_menu_item( $menu_item_id ); |
| 223 |
|
| 224 |
$menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items |
| 225 |
|
| 226 |
$menu_items[] = $menu_obj; |
| 227 |
} |
| 228 |
|
| 229 |
$walker_class_name = apply_filters( 'quadmenu_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id, $menu_obj, $menu_items ); |
| 230 |
|
| 231 |
if ( ! class_exists( $walker_class_name ) ) { |
| 232 |
|
| 233 |
Plugin::send_json_error( sprintf( esc_html__( 'Class %s don\'t exists.', 'quadmenu' ), $walker_class_name ) ); |
| 234 |
|
| 235 |
wp_die(); |
| 236 |
} |
| 237 |
|
| 238 |
if ( ! empty( $menu_items ) ) { |
| 239 |
$args = array( |
| 240 |
'after' => '', |
| 241 |
'before' => '', |
| 242 |
'link_after' => '', |
| 243 |
'link_before' => '', |
| 244 |
'walker' => new $walker_class_name(), |
| 245 |
'doing_ajax' => true, |
| 246 |
); |
| 247 |
|
| 248 |
$html = walk_nav_menu_tree( $menu_items, 0, (object) $args ); |
| 249 |
|
| 250 |
if ( $html ) { |
| 251 |
Plugin::send_json_success( $html ); |
| 252 |
} else { |
| 253 |
Plugin::send_json_error( sprintf( esc_html__( 'Failed to add %s', 'quadmenu' ), $items[ $menu_obj->quadmenu ]['label'] ) ); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
wp_die(); |
| 258 |
} |
| 259 |
|
| 260 |
public function ajax_create_nav_menu_items( $menu_id = 0, $menu_data = array() ) { |
| 261 |
|
| 262 |
$items_saved = array(); |
| 263 |
|
| 264 |
if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) { |
| 265 |
|
| 266 |
wp_cache_delete( "wp_get_nav_menu_items_{$menu_id}", 'quadmenu' ); |
| 267 |
|
| 268 |
// Loop through all the menu items' POST values. |
| 269 |
foreach ( (array) $menu_data as $_possible_db_id => $_item_object_data ) { |
| 270 |
|
| 271 |
// If this possible menu item doesn't actually have a menu database ID yet. |
| 272 |
if ( empty( $_item_object_data['menu-item-db-id'] ) || ( 0 > $_possible_db_id ) || $_possible_db_id != $_item_object_data['menu-item-db-id'] ) { |
| 273 |
$_actual_db_id = 0; |
| 274 |
} else { |
| 275 |
$_actual_db_id = (int) $_item_object_data['menu-item-db-id']; |
| 276 |
} |
| 277 |
|
| 278 |
$args = array( |
| 279 |
'menu-item-status' => ( isset( $_item_object_data['menu-item-status'] ) ? $_item_object_data['menu-item-status'] : '' ), |
| 280 |
'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ), |
| 281 |
'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ), |
| 282 |
'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ), |
| 283 |
'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ), |
| 284 |
'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ), |
| 285 |
'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ), |
| 286 |
'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ), |
| 287 |
'menu-item-columns' => ( isset( $_item_object_data['menu-item-columns'] ) ? $_item_object_data['menu-item-columns'] : $_item_object_data['menu-item-type'] ), |
| 288 |
'menu-item-quadmenu' => ( isset( $_item_object_data['menu-item-quadmenu'] ) ? $_item_object_data['menu-item-quadmenu'] : $_item_object_data['menu-item-type'] ), |
| 289 |
'menu-item-quadmenu-widget' => ( isset( $_item_object_data['menu-item-quadmenu-widget'] ) ? $_item_object_data['menu-item-quadmenu-widget'] : '' ), |
| 290 |
); |
| 291 |
|
| 292 |
if ( $this->ajax_check_nav_menu_item_parent( $args ) ) { |
| 293 |
$items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args ); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
return $items_saved; |
| 299 |
} |
| 300 |
|
| 301 |
public function ajax_check_nav_menu_item_parent( $args ) { |
| 302 |
|
| 303 |
$items = Configuration::custom_nav_menu_items(); |
| 304 |
|
| 305 |
if ( ! isset( $items->{$args['menu-item-quadmenu']} ) ) { |
| 306 |
Plugin::send_json_error( sprintf( esc_html__( 'Failed to add %s. Is not registered in QuadMenu Items.', 'quadmenu' ), $items->{$args['menu-item-quadmenu']} ) ); |
| 307 |
} |
| 308 |
|
| 309 |
if ( isset( $items->{$args['menu-item-quadmenu']}->parent ) ) { |
| 310 |
|
| 311 |
if ( $parent_obj = Plugin::wp_setup_nav_menu_item( $args['menu-item-parent-id'] ) ) { |
| 312 |
|
| 313 |
if ( $items->{$args['menu-item-quadmenu']}->parent === 'main' ) { |
| 314 |
Plugin::send_json_error( sprintf( esc_html__( 'Failed to add %s. Only can be placed on the main menu.', 'quadmenu' ), $items->{$args['menu-item-quadmenu']}->label ) ); |
| 315 |
} |
| 316 |
|
| 317 |
if ( ! in_array( sanitize_key( $parent_obj->quadmenu ), (array) $items->{$args['menu-item-quadmenu']}->parent ) ) { |
| 318 |
Plugin::send_json_error( sprintf( esc_html__( 'Failed to add %1$s as child of %2$s', 'quadmenu' ), $items->{$args['menu-item-quadmenu']}->label, $items->{$parent_obj->quadmenu}->label ) ); |
| 319 |
} |
| 320 |
} elseif ( $items->{$args['menu-item-quadmenu']}->parent !== 'main' ) { |
| 321 |
Plugin::send_json_error( sprintf( esc_html__( 'Failed to add %1$s. Has to be child of %2$s', 'quadmenu' ), join( ' ', (array) $items->{$args['menu-item-quadmenu']}->label ), join( ' ', (array) $items->{$args['menu-item-quadmenu']}->parent ) ) ); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
return true; |
| 326 |
} |
| 327 |
|
| 328 |
public function ajax_delete_nav_menu_item() { |
| 329 |
|
| 330 |
if ( ! check_ajax_referer( 'quadmenu', 'nonce', false ) ) { |
| 331 |
Plugin::send_json_error( esc_html__( 'Please reload the page.', 'quadmenu' ) ); |
| 332 |
} |
| 333 |
|
| 334 |
$menu_id = absint( $_REQUEST['menu_id'] ); |
| 335 |
|
| 336 |
$menu_item_id = absint( $_REQUEST['menu_item_id'] ); |
| 337 |
|
| 338 |
$deleted = $this->delete_children_nav_menu_items( $menu_id, $menu_item_id ); |
| 339 |
|
| 340 |
Plugin::send_json_success( sprintf( esc_html__( 'Removed items %s.', 'quadmenu' ), join( ' ', (array) $deleted ) ) ); |
| 341 |
|
| 342 |
wp_die(); |
| 343 |
} |
| 344 |
|
| 345 |
public function delete_children_nav_menu_items( $menu_id = 0, $menu_item_id = 0 ) { |
| 346 |
|
| 347 |
$deleted = array(); |
| 348 |
|
| 349 |
if ( $menu_item_id > 0 && is_nav_menu_item( $menu_item_id ) ) { |
| 350 |
|
| 351 |
wp_cache_delete( "wp_get_nav_menu_items_{$menu_id}", 'quadmenu' ); |
| 352 |
|
| 353 |
$delete_menu_items_id = $this->get_children_nav_menu_items( $menu_id, $menu_item_id ); |
| 354 |
|
| 355 |
$delete_menu_items_id[] = array( 'id' => $menu_item_id ); |
| 356 |
|
| 357 |
if ( is_array( $delete_menu_items_id ) && count( $delete_menu_items_id ) ) { |
| 358 |
foreach ( $delete_menu_items_id as $item ) { |
| 359 |
|
| 360 |
$id = absint( $item['id'] ); |
| 361 |
|
| 362 |
do_action( 'quadmenu_delete_nav_menu_item', $id, $menu_id ); |
| 363 |
|
| 364 |
if ( wp_delete_post( $id, true ) ) { |
| 365 |
|
| 366 |
$deleted[] = $id; |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
return $deleted; |
| 373 |
} |
| 374 |
|
| 375 |
public function ajax_update_nav_menu_item_order() { |
| 376 |
|
| 377 |
if ( ! check_ajax_referer( 'quadmenu', 'nonce', false ) ) { |
| 378 |
Plugin::send_json_error( esc_html__( 'Please reload the page.', 'quadmenu' ) ); |
| 379 |
} |
| 380 |
|
| 381 |
$menu_id = absint( $_REQUEST['menu_id'] ); |
| 382 |
|
| 383 |
if ( isset( $_REQUEST['menu-item'] ) && is_array( $_REQUEST['menu-item'] ) ) { |
| 384 |
|
| 385 |
$updated = array(); |
| 386 |
|
| 387 |
foreach ( $_REQUEST['menu-item'] as $menu_item_id => $menu_item_data ) { |
| 388 |
|
| 389 |
$menu_item_parent_id = absint( $menu_item_data['menu-item-parent-id'] ); |
| 390 |
|
| 391 |
$menu_item_position = absint( $menu_item_data['menu-item-position'] ); |
| 392 |
|
| 393 |
update_post_meta( $menu_item_id, '_menu_item_menu_item_parent', $menu_item_parent_id ); |
| 394 |
|
| 395 |
if ( $id = wp_update_post( |
| 396 |
array( |
| 397 |
'ID' => $menu_item_id, |
| 398 |
'menu_order' => $menu_item_position, |
| 399 |
) |
| 400 |
) ) { |
| 401 |
$updated[] = $menu_item_id; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
wp_cache_delete( "wp_get_nav_menu_items_{$menu_id}", 'quadmenu' ); |
| 406 |
|
| 407 |
Plugin::send_json_success( sprintf( esc_html__( 'Updated items %s.', 'quadmenu' ), join( ' ', (array) $updated ) ) ); |
| 408 |
} else { |
| 409 |
|
| 410 |
Plugin::send_json_error( esc_html__( 'Undefined menu-item var.', 'quadmenu' ) ); |
| 411 |
} |
| 412 |
|
| 413 |
wp_die(); |
| 414 |
} |
| 415 |
|
| 416 |
public function ajax_add_nav_menu_item_settings() { |
| 417 |
|
| 418 |
if ( ! check_ajax_referer( 'quadmenu', 'nonce', false ) ) { |
| 419 |
Plugin::send_json_error( esc_html__( 'Please reload the page.', 'quadmenu' ) ); |
| 420 |
} |
| 421 |
|
| 422 |
$menu_id = absint( $_REQUEST['menu_id'] ); |
| 423 |
|
| 424 |
$menu_item_id = absint( $_REQUEST['menu_item_id'] ); |
| 425 |
|
| 426 |
$menu_item_depth = absint( $_REQUEST['menu_item_depth'] ); |
| 427 |
|
| 428 |
$menu_obj = Plugin::wp_setup_nav_menu_item( $menu_item_id ); |
| 429 |
|
| 430 |
ob_start(); |
| 431 |
?> |
| 432 |
|
| 433 |
<div id="quadmenu-settings" class="quadmenu-item-depth-<?php echo esc_attr( $menu_item_depth ); ?> quadmenu-item-<?php echo esc_attr( $menu_obj->quadmenu ); ?>"> |
| 434 |
<div class="quadmenu-settings-header"> |
| 435 |
<div class="quadmenu_saving"><?php esc_html_e( 'Saving', 'quadmenu' ); ?></div> |
| 436 |
<button type="button" class="quadmenu_close button-secondary" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> |
| 437 |
<h3 id="quadmenu-title-<?php echo esc_attr( $menu_obj->ID ); ?>" class="quadmenu-title"><i class="<?php echo esc_attr( $menu_obj->icon ); ?>"></i><span><?php esc_html_e( $menu_obj->title ); ?></span></h3> |
| 438 |
</div> |
| 439 |
<div class="quadmenu-settings-body clearfix"> |
| 440 |
<?php do_action( 'quadmenu_modal_panels', $menu_item_depth, $menu_obj, $menu_id ); ?> |
| 441 |
</div> |
| 442 |
<div class="clearfix"></div> |
| 443 |
<div class="quadmenu-settings-modal-footer"> |
| 444 |
</div> |
| 445 |
<div class="clearfix"></div> |
| 446 |
</div> |
| 447 |
<?php |
| 448 |
if ( $settings = ob_get_clean() ) { |
| 449 |
Plugin::send_json_success( $settings ); |
| 450 |
} else { |
| 451 |
Plugin::send_json_error( sprintf( esc_html__( 'Can\'t load settings of %s.', 'quadmenu' ), $menu_obj->ID ) ); |
| 452 |
} |
| 453 |
|
| 454 |
wp_die(); |
| 455 |
} |
| 456 |
|
| 457 |
public function ajax_save_nav_menu_item_settings() { |
| 458 |
|
| 459 |
if ( ! check_ajax_referer( 'quadmenu', 'nonce', false ) ) { |
| 460 |
Plugin::send_json_error( esc_html__( 'Please reload the page.', 'quadmenu' ) ); |
| 461 |
} |
| 462 |
|
| 463 |
if ( $menu_item_id = absint( $_REQUEST['menu_item_id'] ) ) { |
| 464 |
wp_cache_delete( "wp_setup_nav_menu_item_{$menu_item_id}", 'quadmenu' ); |
| 465 |
} |
| 466 |
|
| 467 |
$menu_id = absint( $_REQUEST['menu_id'] ); |
| 468 |
|
| 469 |
if ( $menu_item_id > 0 ) { |
| 470 |
|
| 471 |
if ( isset( $_REQUEST['menu-item-quadmenu-settings'] ) && is_array( $_REQUEST['menu-item-quadmenu-settings'] ) ) { |
| 472 |
|
| 473 |
$saved_settings = array_filter( (array) get_post_meta( $menu_item_id, QUADMENU_DB_ITEM, true ) ); |
| 474 |
|
| 475 |
if ( is_array( $saved_settings ) && $updated_settings = array_merge( $saved_settings, $_REQUEST['menu-item-quadmenu-settings'] ) ) { |
| 476 |
update_post_meta( $menu_item_id, QUADMENU_DB_ITEM, $updated_settings ); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
if ( isset( $_REQUEST['menu-item-url'] ) ) { |
| 481 |
update_post_meta( $menu_item_id, '_menu_item_url', esc_url_raw( $_REQUEST['menu-item-url'] ) ); |
| 482 |
} |
| 483 |
if ( isset( $_REQUEST['menu-item-parent-id'] ) ) { |
| 484 |
update_post_meta( $menu_item_id, '_menu_item_menu_item_parent', strval( (int) $_REQUEST['menu-item-parent-id'] ) ); |
| 485 |
} |
| 486 |
if ( isset( $_REQUEST['menu-item-target'] ) ) { |
| 487 |
update_post_meta( $menu_item_id, '_menu_item_target', sanitize_key( $_REQUEST['menu-item-target'] ) ); |
| 488 |
} |
| 489 |
if ( isset( $_REQUEST['menu-item-classes'] ) ) { |
| 490 |
$_REQUEST['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $_REQUEST['menu-item-classes'] ) ); |
| 491 |
update_post_meta( $menu_item_id, '_menu_item_classes', $_REQUEST['menu-item-classes'] ); |
| 492 |
} |
| 493 |
if ( isset( $_REQUEST['menu-item-xfn'] ) ) { |
| 494 |
$_REQUEST['menu-item-xfn'] = join( ' ', array_map( 'sanitize_html_class', explode( ' ', $_REQUEST['menu-item-xfn'] ) ) ); |
| 495 |
update_post_meta( $menu_item_id, '_menu_item_xfn', $_REQUEST['menu-item-xfn'] ); |
| 496 |
} |
| 497 |
|
| 498 |
if ( isset( $_REQUEST['menu-item-title'] ) || isset( $_REQUEST['menu-item-attr-title'] ) || isset( $_REQUEST['menu-item-description'] ) ) { |
| 499 |
|
| 500 |
$post = array( |
| 501 |
'ID' => $menu_item_id, |
| 502 |
); |
| 503 |
|
| 504 |
if ( isset( $_REQUEST['menu-item-title'] ) ) { |
| 505 |
$post['post_title'] = apply_filters( 'the_title', $_REQUEST['menu-item-title'], $menu_item_id ); |
| 506 |
} |
| 507 |
if ( isset( $_REQUEST['menu-item-attr-title'] ) ) { |
| 508 |
$post['post_excerpt'] = wp_kses_post( $_REQUEST['menu-item-attr-title'] ); |
| 509 |
} |
| 510 |
if ( isset( $_REQUEST['menu-item-description'] ) ) { |
| 511 |
$post['post_content'] = wp_kses_post( $_REQUEST['menu-item-description'] ); |
| 512 |
} |
| 513 |
|
| 514 |
// 'post_type' => 'nav_menu_item', |
| 515 |
// 'post_parent' => $original_parent, |
| 516 |
// 'menu_order' => $args['menu-item-position'], |
| 517 |
|
| 518 |
if ( ! wp_update_post( $post ) ) { |
| 519 |
|
| 520 |
Plugin::send_json_error( sprintf( esc_html__( 'Can\'t save settings of %s.', 'quadmenu' ), $menu_item_id ) ); |
| 521 |
|
| 522 |
wp_die(); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
Plugin::send_json_success( sprintf( esc_html__( 'Saved settings of %s.', 'quadmenu' ), $menu_item_id ) ); |
| 527 |
|
| 528 |
wp_die(); |
| 529 |
} |
| 530 |
|
| 531 |
Plugin::send_json_error( sprintf( esc_html__( 'Can\'t save settings of %s.', 'quadmenu' ), $menu_item_id ) ); |
| 532 |
|
| 533 |
wp_die(); |
| 534 |
} |
| 535 |
|
| 536 |
public function update_nav_menu_item( $menu_id, $menu_item_db_id, $args ) { |
| 537 |
|
| 538 |
if ( ! empty( $args['menu-item-quadmenu'] ) ) { |
| 539 |
|
| 540 |
$saved_settings = array_filter( (array) get_post_meta( $menu_item_db_id, QUADMENU_DB_ITEM, true ) ); |
| 541 |
|
| 542 |
$saved_settings['quadmenu'] = $args['menu-item-quadmenu']; |
| 543 |
|
| 544 |
if ( ! empty( $args['menu-item-columns'] ) ) { |
| 545 |
$saved_settings['columns'] = $args['menu-item-columns']; |
| 546 |
} |
| 547 |
|
| 548 |
update_post_meta( $menu_item_db_id, QUADMENU_DB_ITEM, $saved_settings ); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
public static function instance() { |
| 553 |
if ( ! isset( self::$instance ) ) { |
| 554 |
self::$instance = new self(); |
| 555 |
} |
| 556 |
return self::$instance; |
| 557 |
} |
| 558 |
} |
| 559 |
|