wp_create_nonce( 'quadmenu' ),
'add_name' => QUADMENU_PLUGIN_NAME,
'add_background' => esc_html( 'Add Background', 'quadmenu' ),
'add_to_column' => esc_html( 'Add to Column', 'quadmenu' ),
)
);
}
public function nav_items() {
global $_nav_menu_placeholder, $nav_menu_selected_id;
$items = Configuration::custom_nav_menu_items();
?>
true ), 'object' );
if ( $post_types ) :
$items = array();
$loop_index = 999999;
foreach ( $post_types as $post_type ) {
$item = new \stdClass();
$loop_index++;
$item->object_id = $loop_index;
$item->db_id = 0;
$item->object = $post_type->name;
$item->menu_item_parent = 0;
$item->type = 'post_type_archive';
$item->title = sprintf( esc_html__( 'All %s', 'quadmenu' ), str_replace( 'All ', '', $post_type->labels->name ) );
$item->url = get_post_type_archive_link( $post_type->query_var );
$item->target = '';
$item->attr_title = '';
$item->classes = array();
$item->xfn = '';
$items[] = $item;
}
$walker = new \Walker_Nav_Menu_Checklist( array() );
?>
0 && is_nav_menu($menu_id)) {
$saved_themes = get_term_meta($menu_id, QUADMENU_DB_THEME);
Plugin::send_json_error(json_encode($saved_themes));
if (add_term_meta($menu_id, QUADMENU_DB_THEME, array_merge((array) $saved_themes, (array) $_REQUEST['quadmenu_themes']), true)) {
Plugin::send_json_success();
} else {
Plugin::send_json_error(sprintf(esc_html__('Failed to add theme', 'quadmenu')));
}
}
wp_die();
}
*/
public function ajax_add_nav_menu_item() {
if ( ! check_ajax_referer( 'quadmenu', 'nonce', false ) ) {
Plugin::send_json_error( esc_html__( 'Please reload the page.', 'quadmenu' ) );
}
if ( empty( $_REQUEST['menu-item'] ) ) {
Plugin::send_json_error( esc_html__( 'Undefined menu-item var.', 'quadmenu' ) );
wp_die();
}
require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
// For performance reasons, we omit some object properties from the checklist.
// The following is a hacky way to restore them when adding non-custom items.
$menu_items_data = array();
foreach ( (array) $_REQUEST['menu-item'] as $menu_item_data ) {
$menu_items_data[] = $menu_item_data;
}
$menu_id = absint( $_REQUEST['menu_id'] );
$item_ids = $this->ajax_create_nav_menu_items( $menu_id, $menu_items_data );
if ( is_wp_error( $item_ids ) ) {
wp_die();
}
$menu_items = array();
foreach ( (array) $item_ids as $menu_item_id ) {
$menu_obj = Plugin::wp_setup_nav_menu_item( $menu_item_id );
$menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items
$menu_items[] = $menu_obj;
}
$walker_class_name = apply_filters( 'quadmenu_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id, $menu_obj, $menu_items );
if ( ! class_exists( $walker_class_name ) ) {
Plugin::send_json_error( sprintf( esc_html__( 'Class %s don\'t exists.', 'quadmenu' ), $walker_class_name ) );
wp_die();
}
if ( ! empty( $menu_items ) ) {
$args = array(
'after' => '',
'before' => '',
'link_after' => '',
'link_before' => '',
'walker' => new $walker_class_name(),
'doing_ajax' => true,
);
$html = walk_nav_menu_tree( $menu_items, 0, (object) $args );
if ( $html ) {
Plugin::send_json_success( $html );
} else {
Plugin::send_json_error( sprintf( esc_html__( 'Failed to add %s', 'quadmenu' ), $items[ $menu_obj->quadmenu ]['label'] ) );
}
}
wp_die();
}
public function ajax_create_nav_menu_items( $menu_id = 0, $menu_data = array() ) {
$items_saved = array();
if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) {
wp_cache_delete( "wp_get_nav_menu_items_{$menu_id}", 'quadmenu' );
// Loop through all the menu items' POST values.
foreach ( (array) $menu_data as $_possible_db_id => $_item_object_data ) {
// If this possible menu item doesn't actually have a menu database ID yet.
if ( empty( $_item_object_data['menu-item-db-id'] ) || ( 0 > $_possible_db_id ) || $_possible_db_id != $_item_object_data['menu-item-db-id'] ) {
$_actual_db_id = 0;
} else {
$_actual_db_id = (int) $_item_object_data['menu-item-db-id'];
}
$args = array(
'menu-item-status' => ( isset( $_item_object_data['menu-item-status'] ) ? $_item_object_data['menu-item-status'] : '' ),
'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),
'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),
'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),
'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),
'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),
'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),
'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),
'menu-item-columns' => ( isset( $_item_object_data['menu-item-columns'] ) ? $_item_object_data['menu-item-columns'] : $_item_object_data['menu-item-type'] ),
'menu-item-quadmenu' => ( isset( $_item_object_data['menu-item-quadmenu'] ) ? $_item_object_data['menu-item-quadmenu'] : $_item_object_data['menu-item-type'] ),
'menu-item-quadmenu-widget' => ( isset( $_item_object_data['menu-item-quadmenu-widget'] ) ? $_item_object_data['menu-item-quadmenu-widget'] : '' ),
);
if ( $this->ajax_check_nav_menu_item_parent( $args ) ) {
$items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );
}
}
}
return $items_saved;
}
public function ajax_check_nav_menu_item_parent( $args ) {
$items = Configuration::custom_nav_menu_items();
if ( ! isset( $items->{$args['menu-item-quadmenu']} ) ) {
Plugin::send_json_error( sprintf( esc_html__( 'Failed to add %s. Is not registered in QuadMenu Items.', 'quadmenu' ), $items->{$args['menu-item-quadmenu']} ) );
}
if ( isset( $items->{$args['menu-item-quadmenu']}->parent ) ) {
if ( $parent_obj = Plugin::wp_setup_nav_menu_item( $args['menu-item-parent-id'] ) ) {
if ( $items->{$args['menu-item-quadmenu']}->parent === 'main' ) {
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 ) );
}
if ( ! in_array( sanitize_key( $parent_obj->quadmenu ), (array) $items->{$args['menu-item-quadmenu']}->parent ) ) {
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 ) );
}
} elseif ( $items->{$args['menu-item-quadmenu']}->parent !== 'main' ) {
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 ) ) );
}
}
return true;
}
public function ajax_delete_nav_menu_item() {
if ( ! check_ajax_referer( 'quadmenu', 'nonce', false ) ) {
Plugin::send_json_error( esc_html__( 'Please reload the page.', 'quadmenu' ) );
}
$menu_id = absint( $_REQUEST['menu_id'] );
$menu_item_id = absint( $_REQUEST['menu_item_id'] );
$deleted = $this->delete_children_nav_menu_items( $menu_id, $menu_item_id );
Plugin::send_json_success( sprintf( esc_html__( 'Removed items %s.', 'quadmenu' ), join( ' ', (array) $deleted ) ) );
wp_die();
}
public function delete_children_nav_menu_items( $menu_id = 0, $menu_item_id = 0 ) {
$deleted = array();
if ( $menu_item_id > 0 && is_nav_menu_item( $menu_item_id ) ) {
wp_cache_delete( "wp_get_nav_menu_items_{$menu_id}", 'quadmenu' );
$delete_menu_items_id = $this->get_children_nav_menu_items( $menu_id, $menu_item_id );
$delete_menu_items_id[] = array( 'id' => $menu_item_id );
if ( is_array( $delete_menu_items_id ) && count( $delete_menu_items_id ) ) {
foreach ( $delete_menu_items_id as $item ) {
$id = absint( $item['id'] );
do_action( 'quadmenu_delete_nav_menu_item', $id, $menu_id );
if ( wp_delete_post( $id, true ) ) {
$deleted[] = $id;
}
}
}
}
return $deleted;
}
public function ajax_update_nav_menu_item_order() {
if ( ! check_ajax_referer( 'quadmenu', 'nonce', false ) ) {
Plugin::send_json_error( esc_html__( 'Please reload the page.', 'quadmenu' ) );
}
$menu_id = absint( $_REQUEST['menu_id'] );
if ( isset( $_REQUEST['menu-item'] ) && is_array( $_REQUEST['menu-item'] ) ) {
$updated = array();
foreach ( $_REQUEST['menu-item'] as $menu_item_id => $menu_item_data ) {
$menu_item_parent_id = absint( $menu_item_data['menu-item-parent-id'] );
$menu_item_position = absint( $menu_item_data['menu-item-position'] );
update_post_meta( $menu_item_id, '_menu_item_menu_item_parent', $menu_item_parent_id );
if ( $id = wp_update_post(
array(
'ID' => $menu_item_id,
'menu_order' => $menu_item_position,
)
) ) {
$updated[] = $menu_item_id;
}
}
wp_cache_delete( "wp_get_nav_menu_items_{$menu_id}", 'quadmenu' );
Plugin::send_json_success( sprintf( esc_html__( 'Updated items %s.', 'quadmenu' ), join( ' ', (array) $updated ) ) );
} else {
Plugin::send_json_error( esc_html__( 'Undefined menu-item var.', 'quadmenu' ) );
}
wp_die();
}
public function ajax_add_nav_menu_item_settings() {
if ( ! check_ajax_referer( 'quadmenu', 'nonce', false ) ) {
Plugin::send_json_error( esc_html__( 'Please reload the page.', 'quadmenu' ) );
}
$menu_id = absint( $_REQUEST['menu_id'] );
$menu_item_id = absint( $_REQUEST['menu_item_id'] );
$menu_item_depth = absint( $_REQUEST['menu_item_depth'] );
$menu_obj = Plugin::wp_setup_nav_menu_item( $menu_item_id );
ob_start();
?>
ID ) );
}
wp_die();
}
public function ajax_save_nav_menu_item_settings() {
if ( ! check_ajax_referer( 'quadmenu', 'nonce', false ) ) {
Plugin::send_json_error( esc_html__( 'Please reload the page.', 'quadmenu' ) );
}
if ( $menu_item_id = absint( $_REQUEST['menu_item_id'] ) ) {
wp_cache_delete( "wp_setup_nav_menu_item_{$menu_item_id}", 'quadmenu' );
}
$menu_id = absint( $_REQUEST['menu_id'] );
if ( $menu_item_id > 0 ) {
if ( isset( $_REQUEST['menu-item-quadmenu-settings'] ) && is_array( $_REQUEST['menu-item-quadmenu-settings'] ) ) {
$saved_settings = array_filter( (array) get_post_meta( $menu_item_id, QUADMENU_DB_ITEM, true ) );
if ( is_array( $saved_settings ) && $updated_settings = array_merge( $saved_settings, $_REQUEST['menu-item-quadmenu-settings'] ) ) {
update_post_meta( $menu_item_id, QUADMENU_DB_ITEM, $updated_settings );
}
}
if ( isset( $_REQUEST['menu-item-url'] ) ) {
update_post_meta( $menu_item_id, '_menu_item_url', esc_url_raw( $_REQUEST['menu-item-url'] ) );
}
if ( isset( $_REQUEST['menu-item-parent-id'] ) ) {
update_post_meta( $menu_item_id, '_menu_item_menu_item_parent', strval( (int) $_REQUEST['menu-item-parent-id'] ) );
}
if ( isset( $_REQUEST['menu-item-target'] ) ) {
update_post_meta( $menu_item_id, '_menu_item_target', sanitize_key( $_REQUEST['menu-item-target'] ) );
}
if ( isset( $_REQUEST['menu-item-classes'] ) ) {
$_REQUEST['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $_REQUEST['menu-item-classes'] ) );
update_post_meta( $menu_item_id, '_menu_item_classes', $_REQUEST['menu-item-classes'] );
}
if ( isset( $_REQUEST['menu-item-xfn'] ) ) {
$_REQUEST['menu-item-xfn'] = join( ' ', array_map( 'sanitize_html_class', explode( ' ', $_REQUEST['menu-item-xfn'] ) ) );
update_post_meta( $menu_item_id, '_menu_item_xfn', $_REQUEST['menu-item-xfn'] );
}
if ( isset( $_REQUEST['menu-item-title'] ) || isset( $_REQUEST['menu-item-attr-title'] ) || isset( $_REQUEST['menu-item-description'] ) ) {
$post = array(
'ID' => $menu_item_id,
);
if ( isset( $_REQUEST['menu-item-title'] ) ) {
$post['post_title'] = apply_filters( 'the_title', $_REQUEST['menu-item-title'], $menu_item_id );
}
if ( isset( $_REQUEST['menu-item-attr-title'] ) ) {
$post['post_excerpt'] = wp_kses_post( $_REQUEST['menu-item-attr-title'] );
}
if ( isset( $_REQUEST['menu-item-description'] ) ) {
$post['post_content'] = wp_kses_post( $_REQUEST['menu-item-description'] );
}
// 'post_type' => 'nav_menu_item',
// 'post_parent' => $original_parent,
// 'menu_order' => $args['menu-item-position'],
if ( ! wp_update_post( $post ) ) {
Plugin::send_json_error( sprintf( esc_html__( 'Can\'t save settings of %s.', 'quadmenu' ), $menu_item_id ) );
wp_die();
}
}
Plugin::send_json_success( sprintf( esc_html__( 'Saved settings of %s.', 'quadmenu' ), $menu_item_id ) );
wp_die();
}
Plugin::send_json_error( sprintf( esc_html__( 'Can\'t save settings of %s.', 'quadmenu' ), $menu_item_id ) );
wp_die();
}
public function update_nav_menu_item( $menu_id, $menu_item_db_id, $args ) {
if ( ! empty( $args['menu-item-quadmenu'] ) ) {
$saved_settings = array_filter( (array) get_post_meta( $menu_item_db_id, QUADMENU_DB_ITEM, true ) );
$saved_settings['quadmenu'] = $args['menu-item-quadmenu'];
if ( ! empty( $args['menu-item-columns'] ) ) {
$saved_settings['columns'] = $args['menu-item-columns'];
}
update_post_meta( $menu_item_db_id, QUADMENU_DB_ITEM, $saved_settings );
}
}
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
}