-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatering-menu.php
More file actions
219 lines (180 loc) · 6.02 KB
/
Copy pathcatering-menu.php
File metadata and controls
219 lines (180 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* Plugin Name: Catering Menu System
* Version: 2.1.0
* Description: Mobile-first Catering Menu
* Author: Devang Jani
*/
if (!defined('ABSPATH')) exit;
class Catering_Menu {
public function __construct() {
add_action('init', [$this, 'cpt']);
add_action('init', [$this, 'tax']);
add_shortcode('catering_menu', [$this, 'render']);
add_action('wp_enqueue_scripts', [$this, 'assets']);
add_action('init', [$this, 'register_menu_section_hooks'], 20);
}
public function register_menu_section_hooks() {
add_action('created_menu_section', [$this, 'auto_create_menu_page'], 10, 2);
add_action('edited_menu_section', [$this, 'sync_menu_page_on_edit'], 10, 2);
add_action('delete_menu_section', [$this, 'delete_menu_page_on_term_delete'], 10, 1);
}
public function auto_create_menu_page($term_id, $tt_id = null) {
$term = get_term($term_id, 'menu_section');
if (!$term || is_wp_error($term)) return;
$parent_id = $this->get_menu_parent_page_id();
if (get_page_by_path('menu/' . $term->slug)) return;
wp_insert_post([
'post_title' => $term->name . ' Menu',
'post_name' => $term->slug,
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $parent_id,
'post_content' => '[catering_menu]',
]);
}
public function sync_menu_page_on_edit($term_id, $tt_id = null) {
$term = get_term($term_id, 'menu_section');
if (!$term || is_wp_error($term)) return;
$page = get_page_by_path('menu/' . $term->slug);
if (!$page) return;
wp_update_post([
'ID' => $page->ID,
'post_title' => $term->name . ' Menu',
]);
}
private function get_menu_parent_page_id() {
$parent = get_page_by_path('menu');
if ($parent) return $parent->ID;
return wp_insert_post([
'post_title' => 'Menu',
'post_name' => 'menu',
'post_status' => 'publish',
'post_type' => 'page',
]);
}
public function cpt() {
register_post_type('catering_menu_item', [
'label' => 'Menu Items',
'public' => false,
'show_ui' => true,
'supports' => ['title', 'page-attributes'],
]);
}
public function tax() {
register_taxonomy('menu_section', 'catering_menu_item', [
'label' => 'Menu Sections',
'show_ui' => true,
]);
register_taxonomy('menu_category', 'catering_menu_item', [
'label' => 'Menu Categories',
'hierarchical' => true,
'show_ui' => true,
]);
}
public function assets() {
wp_enqueue_style('catering-menu', plugin_dir_url(__FILE__) . 'menu.css');
wp_enqueue_script('catering-menu', plugin_dir_url(__FILE__) . 'menu.js', [], false, true);
}
public function render() {
$sections = get_terms([
'taxonomy' => 'menu_section',
'hide_empty' => false
]);
if (empty($sections)) return;
$active = '';
if (is_page()) {
global $post;
$slug = trim(str_replace('menu/', '', $post->post_name));
if ($slug && term_exists($slug, 'menu_section')) {
$active = $slug;
}
}
if (!$active) {
$active = $sections[0]->slug;
}
$items = get_posts([
'post_type' => 'catering_menu_item',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'tax_query' => [
[
'taxonomy' => 'menu_section',
'field' => 'slug',
'terms' => $active
]
]
]);
$map = [];
foreach ($items as $item) {
// 🔧 FIX: DO NOT overwrite $sections
$item_sections = wp_get_post_terms($item->ID, 'menu_section', ['fields' => 'slugs']);
if (!in_array($active, $item_sections, true)) continue;
$cats = wp_get_post_terms($item->ID, 'menu_category');
if (empty($cats)) continue;
foreach ($cats as $cat) {
if (!isset($map[$cat->slug])) {
$map[$cat->slug] = [
'name' => $cat->name,
'items' => []
];
}
$map[$cat->slug]['items'][] = $item->post_title;
}
}
$first_cat = array_key_first($map);
?>
<div class="cm-wrapper catering-menu">
<div class="cm-top">
<div class="cm-tabs">
<?php foreach ($sections as $s): ?>
<a class="cm-tab <?php echo $s->slug === $active ? 'active' : ''; ?>"
href="<?php echo esc_url(home_url('/menu/' . $s->slug)); ?>">
<?php echo esc_html($s->name); ?>
</a>
<?php endforeach; ?>
</div>
<a class="cm-download" href="?catering_menu_download=<?php echo esc_attr($active); ?>">
Download Menu Items
</a>
</div>
<div class="cm-header">
<h2><?php echo esc_html(ucfirst($active)); ?> Menu</h2>
<p>The menu shows a variety of foods we offer. Customisation available on request.</p>
</div>
<div class="cm-box">
<aside class="cm-sidebar">
<?php foreach ($map as $id => $cat): ?>
<a href="#"
class="cm-cat <?php echo $id === $first_cat ? 'active' : ''; ?>"
data-cat="<?php echo esc_attr($id); ?>">
<span class="dashicons dashicons-food"></span>
<?php echo esc_html($cat['name']); ?>
</a>
<?php endforeach; ?>
</aside>
<div class="cm-content">
<?php if (empty($map[$first_cat]['items'])): ?>
<div class="cm-empty">Items coming soon. Please check back later.</div>
<?php else: ?>
<h3 class="cm-title"><?php echo esc_html($map[$first_cat]['name']); ?></h3>
<ul class="cm-items">
<?php foreach ($map[$first_cat]['items'] as $item): ?>
<li class="cm-item">
<span class="dashicons dashicons-food"></span>
<?php echo esc_html($item); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
<script>
window.CM_DATA = <?php echo json_encode($map); ?>;
</script>
<?php
}
}
new Catering_Menu();