Skip to content

Commit

Permalink
Added wordpress menu tree in editor config
Browse files Browse the repository at this point in the history
Added api action to update the menu items
  • Loading branch information
alecszaharia committed Jul 4, 2018
1 parent 6a023b5 commit 6a74a54
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
38 changes: 38 additions & 0 deletions editor/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Brizy_Editor_API {
const AJAX_DELETE_FORM = 'brizy_delete_form';
const AJAX_FORM_INTEGRATION_STATUS = 'brizy_form_integration_status';
const AJAX_SUBMIT_FORM = 'brizy_submit_form';
const AJAX_UPDATE_MENU_DATA = 'brizy_update_menu_data';
const AJAX_UPDATE_MENU_ITEM_DATA = 'brizy_update_menu_item_data';


/**
Expand Down Expand Up @@ -97,13 +99,49 @@ private function initialize() {
'update_form_integrations_status'
) );
add_action( 'wp_ajax_' . self::AJAX_DELETE_FORM, array( $this, 'delete_form' ) );
add_action( 'wp_ajax_' . self::AJAX_UPDATE_MENU_ITEM_DATA, array( $this, 'update_menu_item_data' ) );
add_action( 'wp_ajax_' . self::AJAX_UPDATE_MENU_DATA, array( $this, 'update_menu_data' ) );

}

add_action( 'wp_ajax_' . self::AJAX_SUBMIT_FORM, array( $this, 'submit_form' ) );
add_action( 'wp_ajax_nopriv_' . self::AJAX_SUBMIT_FORM, array( $this, 'submit_form' ) );
}

public function update_menu_item_data() {
if ( ! isset( $_POST['menuItemId'] ) || get_post_type( $_POST['menuItemId'] ) != 'nav_menu_item' ) {
$this->error( 400, 'Unknown menu item' );
}

$json_decode = json_decode( stripslashes( $_POST['menuItemData'] ) );

if ( ! isset( $_POST['menuItemData'] ) || is_null( $json_decode ) ) {
$this->error( 400, 'Bad request' );
}

update_post_meta( (int) $_POST['menuItemId'], 'brizy_data', $json_decode );

$this->success( array() );
}

public function update_menu_data() {
if ( ! isset( $_POST['menuId'] )) {
$this->error( 400, 'Unknown menu' );
}

$json_decode = json_decode( stripslashes( $_POST['menuData'] ) );

if ( ! isset( $_POST['menuData'] ) || is_null( $json_decode ) ) {
$this->error( 400, 'Bad request' );
}

update_term_meta( (int) $_POST['menuId'], 'brizy_data', $json_decode );

$this->success( array() );
}



public function default_form() {
try {

Expand Down
84 changes: 82 additions & 2 deletions editor/editor/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function config() {
'woocommerce' => $this->get_woocomerce_plugin_info(),
),
'hasSidebars' => count( $wp_registered_sidebars ) > 0,
'l10n' => Brizy_Languages_Texts::get_editor_texts()
'l10n' => Brizy_Languages_Texts::get_editor_texts(),
),
'applications' => array(
'form' => array(
Expand All @@ -146,7 +146,8 @@ public function config() {
'wpApiUrl' => admin_url( 'admin-ajax.php' ),
'submitUrl' => admin_url( 'admin-ajax.php' ) . "?action=brizy_submit_form"
)
)
),
'menuData' => $this->get_menu_data()
);

return apply_filters( 'brizy_editor_config', $config );
Expand All @@ -171,4 +172,83 @@ private function get_woocomerce_plugin_info() {

return null;
}

private function get_menu_data() {
$menus = wp_get_nav_menus();
$menu_data = array();

foreach ( $menus as $menu ) {

$custom_menu_data = get_term_meta($menu->term_id,'brizy_data', true);

$amenu = array(
'id' => $menu->term_id,
'name' => $menu->name,
);

$amenu = (object) array_merge( $amenu, get_object_vars( is_object( $custom_menu_data ) ? $custom_menu_data : (object) array() ) );

$menu_items = wp_get_nav_menu_items( $menu->term_id );

$menu_items = $this->get_menu_tree( $menu_items );

if ( count( $menu_items ) > 0 ) {
$amenu->items = $menu_items;
}

$menu_data[] = $amenu;
}

return $menu_data;
}

private function get_menu_tree( $items, $parent = 0 ) {
$result_items = array();

foreach ( $items as $item ) {
if ( $item->menu_item_parent != $parent ) {
continue;
}

$megaMenuItems = $this->getMegaMenuItems();

$menu_data = get_post_meta( $item->ID, 'brizy_data', true );

$item_value = array(
'id' => $item->ID,
'title' => $item->title,
'url' => $item->url,
'megaMenuItems' => $megaMenuItems
);

$an_item = (object) array(
'type' => 'MenuItem',
);

$an_item->value = (object) array_merge( $item_value, get_object_vars( is_object( $menu_data ) ? $menu_data : (object) array() ) );

$child_items = $this->get_menu_tree( $items, $item->ID );

if ( count( $child_items ) > 0 ) {
$an_item->value->items = $child_items;
}

$result_items[] = $an_item;
}

return $result_items;
}

/**
* @return array
*/
private function getMegaMenuItems() {

return [
(object) ( array(
'type' => "SectionMegaMenu",
'value' => (object) array( 'items' => array() )
) )
];
}
}

0 comments on commit 6a74a54

Please sign in to comment.