autosaves-preview.php
1 year ago
index.php
7 months ago
menu-preview.php
1 year ago
site-data-preview.php
1 year ago
menu-preview.php
126 lines
| 1 | <?php |
| 2 | |
| 3 | use IlluminateAgnostic\Arr\Support\Arr; |
| 4 | |
| 5 | function kubio_preview_menu_item( $item, $initial_item, $menu_term_id = 0, $item_index = 0 ) { |
| 6 | |
| 7 | $order = Arr::get( $item, 'order', $item_index ); |
| 8 | |
| 9 | return (object) array_merge( |
| 10 | array( |
| 11 | 'object_id' => 0, |
| 12 | 'object' => '', |
| 13 | 'menu_item_parent' => 0, |
| 14 | 'position' => 1, |
| 15 | 'type' => '', |
| 16 | 'title' => '', |
| 17 | 'url' => '', |
| 18 | 'target' => '', |
| 19 | 'attr_title' => '', |
| 20 | 'description' => '', |
| 21 | 'classes' => '', |
| 22 | 'xfn' => '', |
| 23 | 'status' => '', |
| 24 | 'original_title' => '', |
| 25 | 'nav_menu_term_id' => 0, |
| 26 | 'type_label' => '', |
| 27 | 'current' => false, |
| 28 | 'menu_order' => 1, |
| 29 | ), |
| 30 | (array) $initial_item, |
| 31 | array( |
| 32 | 'object_id' => $item['objectId'], |
| 33 | 'object' => $item['objectId'], |
| 34 | 'menu_item_parent' => $item['parent'], |
| 35 | 'position' => $order, |
| 36 | 'menu_order' => ( $item['parent'] * 1000 ) + $order, |
| 37 | 'type' => $item['type'], |
| 38 | 'title' => $item['label'], |
| 39 | 'url' => $item['url'], |
| 40 | 'original_title' => $item['label'], |
| 41 | 'nav_menu_term_id' => $menu_term_id, |
| 42 | 'db_id' => $item['id'], |
| 43 | 'ID' => $item['id'], |
| 44 | 'post_parent' => $item['id'] ? wp_get_post_parent_id( $item['id'] ) : $item['id'], |
| 45 | 'target' => $item['target'], |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | add_action( |
| 51 | 'kubio/preview/handle_custom_entities', |
| 52 | function ( $data ) { |
| 53 | $kind = Arr::get( $data, 'kind' ); |
| 54 | $name = Arr::get( $data, 'name' ); |
| 55 | |
| 56 | if ( $kind === 'kubio' && $name === 'menu' ) { |
| 57 | $menu_data = json_decode( Arr::get( $data, 'data' ), true ); |
| 58 | $items = Arr::get( (array) $menu_data, 'items' ); |
| 59 | $menu = Arr::get( (array) $menu_data, 'menu' ); |
| 60 | |
| 61 | add_filter( |
| 62 | 'wp_get_nav_menu_items', |
| 63 | function ( $menu_items, $current_menu, $args ) use ( $items, $menu ) { |
| 64 | |
| 65 | if ( $cache = wp_cache_get( intval( $current_menu->term_id ), 'kubio/preview-menus' ) ) { |
| 66 | return $cache; |
| 67 | } |
| 68 | |
| 69 | if ( intval( $current_menu->term_id ) === intval( $menu['term_id'] ) ) { |
| 70 | $mapped_items = array(); |
| 71 | |
| 72 | foreach ( $items as $item_index => $item ) { |
| 73 | |
| 74 | $initial_item = null; |
| 75 | |
| 76 | foreach ( $menu_items as $menu_item ) { |
| 77 | if ( intval( $menu_item->db_id ) === intval( $item['id'] ) ) { |
| 78 | $initial_item = $menu_item; |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $mapped_items[] = kubio_preview_menu_item( $item, $initial_item, intval( $menu['term_id'] ), $item_index ); |
| 84 | } |
| 85 | |
| 86 | wp_cache_add( intval( $current_menu->term_id ), $mapped_items, 'kubio/preview-menus' ); |
| 87 | |
| 88 | } else { |
| 89 | $mapped_items = $menu_items; |
| 90 | } |
| 91 | |
| 92 | return $mapped_items; |
| 93 | }, |
| 94 | 10, |
| 95 | 3 |
| 96 | ); |
| 97 | } |
| 98 | } |
| 99 | ); |
| 100 | |
| 101 | |
| 102 | function kubio_nav_menu_locations_preview( $locations ) { |
| 103 | |
| 104 | if ( ! kubio_is_page_preview() ) { |
| 105 | return $locations; |
| 106 | } |
| 107 | |
| 108 | $global_data_locations = kubio_get_global_data( 'menuLocations', array() ); |
| 109 | |
| 110 | foreach ( $global_data_locations as $global_data_location ) { |
| 111 | $location = $global_data_location['name']; |
| 112 | $menu_id = $global_data_location['menu']; |
| 113 | |
| 114 | if ( $menu_id ) { |
| 115 | $locations[ $location ] = $menu_id; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return $locations; |
| 120 | } |
| 121 | |
| 122 | add_filter( |
| 123 | 'theme_mod_nav_menu_locations', |
| 124 | 'kubio_nav_menu_locations_preview' |
| 125 | ); |
| 126 |