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