PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.2
Jetpack – WP Security, Backup, Speed, & Growth v11.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / custom-post-types / nova.php

nova.php in Jetpack – WP Security, Backup, Speed, & Growth 11.2, at modules/custom-post-types/nova.php

1,736 lines 49.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Manage restaurant menus from your WordPress site,
4 * via a new "nova" CPT.
5 *
6 * Put the following code in your theme's Food Menu Page Template to customize the markup of the menu.
7 *
8 * if ( class_exists( 'Nova_Restaurant' ) ) {
9 * Nova_Restaurant::init( array(
10 * 'menu_tag' => 'section',
11 * 'menu_class' => 'menu-items',
12 * 'menu_header_tag' => 'header',
13 * 'menu_header_class' => 'menu-group-header',
14 * 'menu_title_tag' => 'h1',
15 * 'menu_title_class' => 'menu-group-title',
16 * 'menu_description_tag' => 'div',
17 * 'menu_description_class' => 'menu-group-description',
18 * ) );
19 * }
20 *
21 * @todo
22 * - Bulk/Quick edit response of Menu Item rows is broken.
23 * - Drag and Drop reordering.
24 *
25 * @package automattic/jetpack
26 */
27
28 use Automattic\Jetpack\Assets;
29
30 /**
31 * Create the new Nova CPT.
32 */
33 class Nova_Restaurant {
34 const MENU_ITEM_POST_TYPE = 'nova_menu_item';
35 const MENU_ITEM_LABEL_TAX = 'nova_menu_item_label';
36 const MENU_TAX = 'nova_menu';
37
38 /**
39 * Version number used when enqueuing all resources (css and js).
40 *
41 * @var string
42 */
43 public $version = '20210303';
44
45 /**
46 * Default markup for the menu items.
47 *
48 * @var array
49 */
50 protected $default_menu_item_loop_markup = array(
51 'menu_tag' => 'section',
52 'menu_class' => 'menu-items',
53 'menu_header_tag' => 'header',
54 'menu_header_class' => 'menu-group-header',
55 'menu_title_tag' => 'h1',
56 'menu_title_class' => 'menu-group-title',
57 'menu_description_tag' => 'div',
58 'menu_description_class' => 'menu-group-description',
59 );
60
61 /**
62 * Array of markup for the menu items.
63 *
64 * @var array
65 */
66 protected $menu_item_loop_markup = array();
67
68 /**
69 * Last term ID of a loop of menu items.
70 *
71 * @var bool|int
72 */
73 protected $menu_item_loop_last_term_id = false;
74
75 /**
76 * Current term ID of a loop of menu items.
77 *
78 * @var bool|int
79 */
80 protected $menu_item_loop_current_term = false;
81
82 /**
83 * Initialize class.
84 *
85 * @param array $menu_item_loop_markup Array of markup for the menu items.
86 *
87 * @return self
88 */
89 public static function init( $menu_item_loop_markup = array() ) {
90 static $instance = false;
91
92 if ( ! $instance ) {
93 $instance = new Nova_Restaurant();
94 }
95
96 if ( $menu_item_loop_markup ) {
97 $instance->menu_item_loop_markup = wp_parse_args( $menu_item_loop_markup, $instance->default_menu_item_loop_markup );
98 }
99
100 return $instance;
101 }
102
103 /**
104 * Constructor.
105 * Hook into WordPress to create CPT and utilities if needed.
106 */
107 public function __construct() {
108 if ( ! $this->site_supports_nova() ) {
109 return;
110 }
111
112 $this->register_taxonomies();
113 $this->register_post_types();
114 add_action( 'admin_menu', array( $this, 'add_admin_menus' ) );
115 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_nova_styles' ) );
116 add_action( 'admin_head', array( $this, 'set_custom_font_icon' ) );
117
118 // Always sort menu items correctly
119 add_action( 'parse_query', array( $this, 'sort_menu_item_queries_by_menu_order' ) );
120 add_filter( 'posts_results', array( $this, 'sort_menu_item_queries_by_menu_taxonomy' ), 10, 2 );
121
122 add_action( 'wp_insert_post', array( $this, 'add_post_meta' ) );
123
124 $this->menu_item_loop_markup = $this->default_menu_item_loop_markup;
125
126 // Only output our Menu Item Loop Markup on a real blog view. Not feeds, XML-RPC, admin, etc.
127 add_filter( 'template_include', array( $this, 'setup_menu_item_loop_markup__in_filter' ) );
128
129 add_filter( 'enter_title_here', array( $this, 'change_default_title' ) );
130 add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) );
131 add_filter( 'dashboard_glance_items', array( $this, 'add_to_dashboard' ) );
132 }
133
134 /**
135 * Should this Custom Post Type be made available?
136 *
137 * @return bool
138 */
139 public function site_supports_nova() {
140 // If we're on WordPress.com, and it has the menu site vertical.
141 if ( function_exists( 'site_vertical' ) && 'nova_menu' === site_vertical() ) {
142 return true;
143 }
144
145 // Else, if the current theme requests it.
146 if ( current_theme_supports( self::MENU_ITEM_POST_TYPE ) ) {
147 return true;
148 }
149
150 // Otherwise, say no unless something wants to filter us to say yes.
151 /**
152 * Allow something else to hook in and enable this CPT.
153 *
154 * @module custom-content-types
155 *
156 * @since 2.6.0
157 *
158 * @param bool false Whether or not to enable this CPT.
159 * @param string $var The slug for this CPT.
160 */
161 return (bool) apply_filters( 'jetpack_enable_cpt', false, self::MENU_ITEM_POST_TYPE );
162 }
163
164 /* Setup */
165
166 /**
167 * Register Taxonomies and Post Type
168 */
169 public function register_taxonomies() {
170 if ( ! taxonomy_exists( self::MENU_ITEM_LABEL_TAX ) ) {
171 register_taxonomy(
172 self::MENU_ITEM_LABEL_TAX,
173 self::MENU_ITEM_POST_TYPE,
174 array(
175 'labels' => array(
176 /* translators: this is about a food menu */
177 'name' => __( 'Menu Item Labels', 'jetpack' ),
178 /* translators: this is about a food menu */
179 'singular_name' => __( 'Menu Item Label', 'jetpack' ),
180 /* translators: this is about a food menu */
181 'search_items' => __( 'Search Menu Item Labels', 'jetpack' ),
182 'popular_items' => __( 'Popular Labels', 'jetpack' ),
183 /* translators: this is about a food menu */
184 'all_items' => __( 'All Menu Item Labels', 'jetpack' ),
185 /* translators: this is about a food menu */
186 'edit_item' => __( 'Edit Menu Item Label', 'jetpack' ),
187 /* translators: this is about a food menu */
188 'view_item' => __( 'View Menu Item Label', 'jetpack' ),
189 /* translators: this is about a food menu */
190 'update_item' => __( 'Update Menu Item Label', 'jetpack' ),
191 /* translators: this is about a food menu */
192 'add_new_item' => __( 'Add New Menu Item Label', 'jetpack' ),
193 /* translators: this is about a food menu */
194 'new_item_name' => __( 'New Menu Item Label Name', 'jetpack' ),
195 'separate_items_with_commas' => __( 'For example, spicy, favorite, etc. <br /> Separate Labels with commas', 'jetpack' ),
196 'add_or_remove_items' => __( 'Add or remove Labels', 'jetpack' ),
197 'choose_from_most_used' => __( 'Choose from the most used Labels', 'jetpack' ),
198 'items_list_navigation' => __( 'Menu item label list navigation', 'jetpack' ),
199 'items_list' => __( 'Menu item labels list', 'jetpack' ),
200 ),
201 'no_tagcloud' => __( 'No Labels found', 'jetpack' ),
202 'hierarchical' => false,
203 )
204 );
205 }
206
207 if ( ! taxonomy_exists( self::MENU_TAX ) ) {
208 register_taxonomy(
209 self::MENU_TAX,
210 self::MENU_ITEM_POST_TYPE,
211 array(
212 'labels' => array(
213 /* translators: this is about a food menu */
214 'name' => __( 'Menu Sections', 'jetpack' ),
215 /* translators: this is about a food menu */
216 'singular_name' => __( 'Menu Section', 'jetpack' ),
217 /* translators: this is about a food menu */
218 'search_items' => __( 'Search Menu Sections', 'jetpack' ),
219 /* translators: this is about a food menu */
220 'all_items' => __( 'All Menu Sections', 'jetpack' ),
221 /* translators: this is about a food menu */
222 'parent_item' => __( 'Parent Menu Section', 'jetpack' ),
223 /* translators: this is about a food menu */
224 'parent_item_colon' => __( 'Parent Menu Section:', 'jetpack' ),
225 /* translators: this is about a food menu */
226 'edit_item' => __( 'Edit Menu Section', 'jetpack' ),
227 /* translators: this is about a food menu */
228 'view_item' => __( 'View Menu Section', 'jetpack' ),
229 /* translators: this is about a food menu */
230 'update_item' => __( 'Update Menu Section', 'jetpack' ),
231 /* translators: this is about a food menu */
232 'add_new_item' => __( 'Add New Menu Section', 'jetpack' ),
233 /* translators: this is about a food menu */
234 'new_item_name' => __( 'New Menu Sections Name', 'jetpack' ),
235 'items_list_navigation' => __( 'Menu section list navigation', 'jetpack' ),
236 'items_list' => __( 'Menu section list', 'jetpack' ),
237 ),
238 'rewrite' => array(
239 'slug' => 'menu',
240 'with_front' => false,
241 'hierarchical' => true,
242 ),
243 'hierarchical' => true,
244 'show_tagcloud' => false,
245 'query_var' => 'menu',
246 )
247 );
248 }
249 }
250
251 /**
252 * Register our Post Type.
253 */
254 public function register_post_types() {
255 if ( post_type_exists( self::MENU_ITEM_POST_TYPE ) ) {
256 return;
257 }
258
259 register_post_type(
260 self::MENU_ITEM_POST_TYPE,
261 array(
262 'description' => __( "Items on your restaurant's menu", 'jetpack' ),
263
264 'labels' => array(
265 /* translators: this is about a food menu */
266 'name' => __( 'Menu Items', 'jetpack' ),
267 /* translators: this is about a food menu */
268 'singular_name' => __( 'Menu Item', 'jetpack' ),
269 /* translators: this is about a food menu */
270 'menu_name' => __( 'Food Menus', 'jetpack' ),
271 /* translators: this is about a food menu */
272 'all_items' => __( 'Menu Items', 'jetpack' ),
273 /* translators: this is about a food menu */
274 'add_new' => __( 'Add One Item', 'jetpack' ),
275 /* translators: this is about a food menu */
276 'add_new_item' => __( 'Add Menu Item', 'jetpack' ),
277 /* translators: this is about a food menu */
278 'edit_item' => __( 'Edit Menu Item', 'jetpack' ),
279 /* translators: this is about a food menu */
280 'new_item' => __( 'New Menu Item', 'jetpack' ),
281 /* translators: this is about a food menu */
282 'view_item' => __( 'View Menu Item', 'jetpack' ),
283 /* translators: this is about a food menu */
284 'search_items' => __( 'Search Menu Items', 'jetpack' ),
285 /* translators: this is about a food menu */
286 'not_found' => __( 'No Menu Items found', 'jetpack' ),
287 /* translators: this is about a food menu */
288 'not_found_in_trash' => __( 'No Menu Items found in Trash', 'jetpack' ),
289 'filter_items_list' => __( 'Filter menu items list', 'jetpack' ),
290 'items_list_navigation' => __( 'Menu item list navigation', 'jetpack' ),
291 'items_list' => __( 'Menu items list', 'jetpack' ),
292 ),
293 'supports' => array(
294 'title',
295 'editor',
296 'thumbnail',
297 'excerpt',
298 ),
299 'rewrite' => array(
300 'slug' => 'item',
301 'with_front' => false,
302 'feeds' => false,
303 'pages' => false,
304 ),
305 'register_meta_box_cb' => array( $this, 'register_menu_item_meta_boxes' ),
306
307 'public' => true,
308 'show_ui' => true, // set to false to replace with custom UI
309 'menu_position' => 20, // below Pages
310 'capability_type' => 'page',
311 'map_meta_cap' => true,
312 'has_archive' => false,
313 'query_var' => 'item',
314 )
315 );
316 }
317
318 /**
319 * Update messages for the Menu Item admin.
320 *
321 * @param array $messages Existing post update messages.
322 *
323 * @return array $messages Updated post update messages.
324 */
325 public function updated_messages( $messages ) {
326 global $post;
327
328 $messages[ self::MENU_ITEM_POST_TYPE ] = array(
329 0 => '', // Unused. Messages start at index 1.
330 1 => sprintf(
331 /* translators: this is about a food menu. Placeholder is a link to the food menu. */
332 __( 'Menu item updated. <a href="%s">View item</a>', 'jetpack' ),
333 esc_url( get_permalink( $post->ID ) )
334 ),
335 2 => esc_html__( 'Custom field updated.', 'jetpack' ),
336 3 => esc_html__( 'Custom field deleted.', 'jetpack' ),
337 /* translators: this is about a food menu */
338 4 => esc_html__( 'Menu item updated.', 'jetpack' ),
339 5 => isset( $_GET['revision'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Copying core message handling.
340 ? sprintf(
341 /* translators: %s: date and time of the revision */
342 esc_html__( 'Menu item restored to revision from %s', 'jetpack' ),
343 wp_post_revision_title( (int) $_GET['revision'], false ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Copying core message handling.
344 )
345 : false,
346 6 => sprintf(
347 /* translators: this is about a food menu. Placeholder is a link to the food menu. */
348 __( 'Menu item published. <a href="%s">View item</a>', 'jetpack' ),
349 esc_url( get_permalink( $post->ID ) )
350 ),
351 /* translators: this is about a food menu */
352 7 => esc_html__( 'Menu item saved.', 'jetpack' ),
353 8 => sprintf(
354 /* translators: this is about a food menu */
355 __( 'Menu item submitted. <a target="_blank" href="%s">Preview item</a>', 'jetpack' ),
356 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) )
357 ),
358 9 => sprintf(
359 /* translators: this is about a food menu. 1. Publish box date format, see https://php.net/date 2. link to the food menu. */
360 __( 'Menu item scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview item</a>', 'jetpack' ),
361 /* translators: Publish box date format, see https://php.net/date */
362 date_i18n( __( 'M j, Y @ G:i', 'jetpack' ), strtotime( $post->post_date ) ),
363 esc_url( get_permalink( $post->ID ) )
364 ),
365 10 => sprintf(
366 /* translators: this is about a food menu. Placeholder is a link to the food menu. */
367 __( 'Menu item draft updated. <a target="_blank" href="%s">Preview item</a>', 'jetpack' ),
368 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) )
369 ),
370 );
371
372 return $messages;
373 }
374
375 /**
376 * Nova styles and scripts.
377 *
378 * @param string $hook Page hook.
379 *
380 * @return void
381 */
382 public function enqueue_nova_styles( $hook ) {
383 global $post_type;
384 $pages = array( 'edit.php', 'post.php', 'post-new.php' );
385
386 if ( in_array( $hook, $pages, true ) && $post_type === self::MENU_ITEM_POST_TYPE ) {
387 wp_enqueue_style( 'nova-style', plugins_url( 'css/nova.css', __FILE__ ), array(), $this->version );
388 }
389
390 wp_enqueue_style( 'nova-font', plugins_url( 'css/nova-font.css', __FILE__ ), array(), $this->version );
391 }
392
393 /**
394 * Change ‘Enter Title Here’ text for the Menu Item.
395 *
396 * @param string $title Default title placeholder text.
397 *
398 * @return string
399 */
400 public function change_default_title( $title ) {
401 if ( self::MENU_ITEM_POST_TYPE === get_post_type() ) {
402 /* translators: this is about a food menu */
403 $title = esc_html__( "Enter the menu item's name here", 'jetpack' );
404 }
405
406 return $title;
407 }
408
409 /**
410 * Add to Dashboard At A Glance
411 *
412 * @return void
413 */
414 public function add_to_dashboard() {
415 $number_menu_items = wp_count_posts( self::MENU_ITEM_POST_TYPE );
416
417 if ( current_user_can( 'administrator' ) ) {
418 $number_menu_items_published = sprintf(
419 '<a href="%1$s">%2$s</a>',
420 esc_url(
421 get_admin_url(
422 get_current_blog_id(),
423 'edit.php?post_type=' . self::MENU_ITEM_POST_TYPE
424 )
425 ),
426 sprintf(
427 /* translators: Placehoder is a number of items. */
428 _n(
429 '%1$d Food Menu Item',
430 '%1$d Food Menu Items',
431 (int) $number_menu_items->publish,
432 'jetpack'
433 ),
434 number_format_i18n( $number_menu_items->publish )
435 )
436 );
437 } else {
438 $number_menu_items_published = sprintf(
439 '<span>%1$s</span>',
440 sprintf(
441 /* translators: Placehoder is a number of items. */
442 _n(
443 '%1$d Food Menu Item',
444 '%1$d Food Menu Items',
445 (int) $number_menu_items->publish,
446 'jetpack'
447 ),
448 number_format_i18n( $number_menu_items->publish )
449 )
450 );
451 }
452
453 echo '<li class="nova-menu-count">' . $number_menu_items_published . '</li>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- we escape things above.
454 }
455
456 /**
457 * If the WP query for our menu items.
458 *
459 * @param WP_Query $query WP Query.
460 *
461 * @return bool
462 */
463 public function is_menu_item_query( $query ) {
464 if (
465 ( isset( $query->query_vars['taxonomy'] ) && self::MENU_TAX === $query->query_vars['taxonomy'] )
466 ||
467 ( isset( $query->query_vars['post_type'] ) && self::MENU_ITEM_POST_TYPE === $query->query_vars['post_type'] )
468 ) {
469 return true;
470 }
471
472 return false;
473 }
474
475 /**
476 * Custom sort the menu item queries by menu order.
477 *
478 * @param WP_Query $query WP Query.
479 *
480 * @return void
481 */
482 public function sort_menu_item_queries_by_menu_order( $query ) {
483 if ( ! $this->is_menu_item_query( $query ) ) {
484 return;
485 }
486
487 $query->query_vars['orderby'] = 'menu_order';
488 $query->query_vars['order'] = 'ASC';
489
490 // For now, just turn off paging so we can sort by taxonmy later
491 // If we want paging in the future, we'll need to add the taxonomy sort here (or at least before the DB query is made)
492 $query->query_vars['posts_per_page'] = -1;
493 }
494
495 /**
496 * Custom sort the menu item queries by menu taxonomies.
497 *
498 * @param WP_Post[] $posts Array of post objects.
499 * @param WP_Query $query The WP_Query instance.
500 *
501 * @return WP_Post[]
502 */
503 public function sort_menu_item_queries_by_menu_taxonomy( $posts, $query ) {
504 if ( ! $posts ) {
505 return $posts;
506 }
507
508 if ( ! $this->is_menu_item_query( $query ) ) {
509 return $posts;
510 }
511
512 $grouped_by_term = array();
513
514 foreach ( $posts as $post ) {
515 $term = $this->get_menu_item_menu_leaf( $post->ID );
516 if ( ! $term || is_wp_error( $term ) ) {
517 $term_id = 0;
518 } else {
519 $term_id = $term->term_id;
520 }
521
522 if ( ! isset( $grouped_by_term[ "$term_id" ] ) ) {
523 $grouped_by_term[ "$term_id" ] = array();
524 }
525
526 $grouped_by_term[ "$term_id" ][] = $post;
527 }
528
529 $term_order = get_option( 'nova_menu_order', array() );
530
531 $return = array();
532 foreach ( $term_order as $term_id ) {
533 if ( isset( $grouped_by_term[ "$term_id" ] ) ) {
534 $return = array_merge( $return, $grouped_by_term[ "$term_id" ] );
535 unset( $grouped_by_term[ "$term_id" ] );
536 }
537 }
538
539 foreach ( $grouped_by_term as $term_id => $posts ) {
540 $return = array_merge( $return, $posts );
541 }
542
543 return $return;
544 }
545
546 /**
547 * Add new "Add many items" submenu, custom colunmns, and custom bulk actions.
548 *
549 * @return void
550 */
551 public function add_admin_menus() {
552 $hook = add_submenu_page(
553 'edit.php?post_type=' . self::MENU_ITEM_POST_TYPE,
554 __( 'Add Many Items', 'jetpack' ),
555 __( 'Add Many Items', 'jetpack' ),
556 'edit_pages',
557 'add_many_nova_items',
558 array( $this, 'add_many_new_items_page' )
559 );
560
561 add_action( "load-$hook", array( $this, 'add_many_new_items_page_load' ) );
562
563 add_action( 'current_screen', array( $this, 'current_screen_load' ) );
564
565 /*
566 * Adjust 'Add Many Items' submenu position
567 * We're making changes to the menu global, but no other choice unfortunately.
568 * phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited
569 */
570 if ( isset( $GLOBALS['submenu'][ 'edit.php?post_type=' . self::MENU_ITEM_POST_TYPE ] ) ) {
571 $submenu_item = array_pop( $GLOBALS['submenu'][ 'edit.php?post_type=' . self::MENU_ITEM_POST_TYPE ] );
572 $GLOBALS['submenu'][ 'edit.php?post_type=' . self::MENU_ITEM_POST_TYPE ][11] = $submenu_item;
573 ksort( $GLOBALS['submenu'][ 'edit.php?post_type=' . self::MENU_ITEM_POST_TYPE ] );
574 }
575 // phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited
576
577 $this->setup_menu_item_columns();
578
579 wp_register_script(
580 'nova-menu-checkboxes',
581 Assets::get_file_url_for_environment(
582 '_inc/build/custom-post-types/js/menu-checkboxes.min.js',
583 'modules/custom-post-types/js/menu-checkboxes.js'
584 ),
585 array( 'jquery' ),
586 $this->version,
587 true
588 );
589 }
590
591 /**
592 * Custom Nova Icon CSS
593 *
594 * @return void
595 */
596 public function set_custom_font_icon() {
597 ?>
598 <style type="text/css">
599 #menu-posts-nova_menu_item .wp-menu-image:before {
600 font-family: 'nova-font' !important;
601 content: '\e603' !important;
602 }
603 </style>
604 <?php
605 }
606
607 /**
608 * Load Nova menu management tools on the CPT admin screen.
609 *
610 * @return void
611 */
612 public function current_screen_load() {
613 $screen = get_current_screen();
614 if ( 'edit-nova_menu_item' !== $screen->id ) {
615 return;
616 }
617
618 $this->edit_menu_items_page_load();
619 add_filter( 'admin_notices', array( $this, 'admin_notices' ) );
620 }
621
622 /* Edit Items List */
623
624 /**
625 * Display a notice in wp-admin after items have been changed.
626 *
627 * @return void
628 */
629 public function admin_notices() {
630 if ( isset( $_GET['nova_reordered'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- this is only displaying a message with no dynamic values.
631 printf(
632 '<div class="updated"><p>%s</p></div>',
633 /* translators: this is about a food menu */
634 esc_html__( 'Menu Items re-ordered.', 'jetpack' )
635 );
636 }
637 }
638
639 /**
640 * Do not allow sorting by title.
641 *
642 * @param array $columns An array of sortable columns.
643 *
644 * @return array $columns.
645 */
646 public function no_title_sorting( $columns ) {
647 if ( isset( $columns['title'] ) ) {
648 unset( $columns['title'] );
649 }
650 return $columns;
651 }
652
653 /**
654 * Set up custom columns for our Nova menu.
655 *
656 * @return void
657 */
658 public function setup_menu_item_columns() {
659 add_filter( sprintf( 'manage_edit-%s_sortable_columns', self::MENU_ITEM_POST_TYPE ), array( $this, 'no_title_sorting' ) );
660 add_filter( sprintf( 'manage_%s_posts_columns', self::MENU_ITEM_POST_TYPE ), array( $this, 'menu_item_columns' ) );
661
662 add_action( sprintf( 'manage_%s_posts_custom_column', self::MENU_ITEM_POST_TYPE ), array( $this, 'menu_item_column_callback' ), 10, 2 );
663 }
664
665 /**
666 * Add custom columns to the Nova menu item list.
667 *
668 * @param array $columns An array of columns.
669 *
670 * @return array $columns.
671 */
672 public function menu_item_columns( $columns ) {
673 unset( $columns['date'], $columns['likes'] );
674
675 $columns['thumbnail'] = __( 'Thumbnail', 'jetpack' );
676 $columns['labels'] = __( 'Labels', 'jetpack' );
677 $columns['price'] = __( 'Price', 'jetpack' );
678 $columns['order'] = __( 'Order', 'jetpack' );
679
680 return $columns;
681 }
682
683 /**
684 * Display custom data in each new custom column we created.
685 *
686 * @param string $column The name of the column to display.
687 * @param int $post_id The current post ID.
688 *
689 * @return void
690 */
691 public function menu_item_column_callback( $column, $post_id ) {
692 $screen = get_current_screen();
693
694 switch ( $column ) {
695 case 'thumbnail':
696 echo get_the_post_thumbnail( $post_id, array( 50, 50 ) );
697 break;
698 case 'labels':
699 $this->list_admin_labels( $post_id );
700 break;
701 case 'price':
702 $this->display_price( $post_id );
703 break;
704 case 'order':
705 $url = admin_url( $screen->parent_file );
706
707 $up_url = add_query_arg(
708 array(
709 'action' => 'move-item-up',
710 'post_id' => (int) $post_id,
711 ),
712 wp_nonce_url( $url, 'nova_move_item_up_' . $post_id )
713 );
714
715 $down_url = add_query_arg(
716 array(
717 'action' => 'move-item-down',
718 'post_id' => (int) $post_id,
719 ),
720 wp_nonce_url( $url, 'nova_move_item_down_' . $post_id )
721 );
722 $menu_item = get_post( $post_id );
723 $this->get_menu_by_post_id( $post_id );
724 $term_id = $this->get_menu_by_post_id( $post_id );
725 if ( $term_id ) {
726 $term_id = $term_id->term_id;
727 }
728 ?>
729 <input type="hidden" class="menu-order-value" name="nova_order[<?php echo (int) $post_id; ?>]" value="<?php echo esc_attr( $menu_item->menu_order ); ?>" />
730 <input type="hidden" class='nova-menu-term' name="nova_menu_term[<?php echo (int) $post_id; ?>]" value="<?php echo esc_attr( $term_id ); ?>">
731
732 <span class="hide-if-js">
733 &nbsp; &nbsp; &mdash; <a class="nova-move-item-up" data-post-id="<?php echo (int) $post_id; ?>" href="<?php echo esc_url( $up_url ); ?>">up</a>
734 <br />
735 &nbsp; &nbsp; &mdash; <a class="nova-move-item-down" data-post-id="<?php echo (int) $post_id; ?>" href="<?php echo esc_url( $down_url ); ?>">down</a>
736 </span>
737 <?php
738 break;
739 }
740 }
741
742 /**
743 * Get menu item by post ID.
744 *
745 * @param int $post_id Post ID.
746 *
747 * @return bool|WP_Term
748 */
749 public function get_menu_by_post_id( $post_id = null ) {
750 if ( ! $post_id ) {
751 return false;
752 }
753
754 $terms = get_the_terms( $post_id, self::MENU_TAX );
755
756 if ( ! is_array( $terms ) ) {
757 return false;
758 }
759
760 return array_pop( $terms );
761 }
762
763 /**
764 * Fires on a menu edit page. We might have drag-n-drop reordered
765 */
766 public function maybe_reorder_menu_items() {
767 // make sure we clicked our button.
768 if (
769 empty( $_REQUEST['menu_reorder_submit'] )
770 || __( 'Save New Order', 'jetpack' ) !== $_REQUEST['menu_reorder_submit']
771 ) {
772 return;
773 }
774
775 // make sure we have the nonce.
776 if (
777 empty( $_REQUEST['drag-drop-reorder'] )
778 || ! wp_verify_nonce( sanitize_key( $_REQUEST['drag-drop-reorder'] ), 'drag-drop-reorder' )
779 ) {
780 return;
781 }
782
783 // make sure we have data to work with.
784 if ( empty( $_REQUEST['nova_menu_term'] ) || empty( $_REQUEST['nova_order'] ) ) {
785 return;
786 }
787
788 $term_pairs = array_map( 'absint', $_REQUEST['nova_menu_term'] );
789 $order_pairs = array_map( 'absint', $_REQUEST['nova_order'] );
790
791 foreach ( $order_pairs as $id => $menu_order ) {
792 $id = absint( $id );
793 unset( $order_pairs[ $id ] );
794 if ( $id < 0 ) {
795 continue;
796 }
797
798 $post = get_post( $id );
799 if ( ! $post ) {
800 continue;
801 }
802
803 // save a write if the order hasn't changed
804 if ( (int) $menu_order !== $post->menu_order ) {
805 $args = array(
806 'ID' => $id,
807 'menu_order' => $menu_order,
808 );
809 wp_update_post( $args );
810 }
811
812 // save a write if the term hasn't changed
813 if ( (int) $term_pairs[ $id ] !== $this->get_menu_by_post_id( $id )->term_id ) {
814 wp_set_object_terms( $id, $term_pairs[ $id ], self::MENU_TAX );
815 }
816 }
817
818 $redirect = add_query_arg(
819 array(
820 'post_type' => self::MENU_ITEM_POST_TYPE,
821 'nova_reordered' => '1',
822 ),
823 admin_url( 'edit.php' )
824 );
825 wp_safe_redirect( $redirect );
826 exit;
827
828 }
829
830 /**
831 * Handle changes to menu items.
832 * (process actions, update data, enqueue necessary scripts).
833 *
834 * @return void
835 */
836 public function edit_menu_items_page_load() {
837 if ( isset( $_GET['action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- we process the form and check nonces in handle_menu_item_actions.
838 $this->handle_menu_item_actions();
839 }
840
841 $this->maybe_reorder_menu_items();
842
843 wp_enqueue_script(
844 'nova-drag-drop',
845 Assets::get_file_url_for_environment(
846 '_inc/build/custom-post-types/js/nova-drag-drop.min.js',
847 'modules/custom-post-types/js/nova-drag-drop.js'
848 ),
849 array( 'jquery-ui-sortable' ),
850 $this->version,
851 true
852 );
853
854 wp_localize_script(
855 'nova-drag-drop',
856 '_novaDragDrop',
857 array(
858 'nonce' => wp_create_nonce( 'drag-drop-reorder' ),
859 'nonceName' => 'drag-drop-reorder',
860 'reorder' => __( 'Save New Order', 'jetpack' ),
861 'reorderName' => 'menu_reorder_submit',
862 )
863 );
864 add_action( 'the_post', array( $this, 'show_menu_titles_in_menu_item_list' ) );
865 }
866
867 /**
868 * Process actions to move menu items around.
869 *
870 * @return void
871 */
872 public function handle_menu_item_actions() {
873 if ( isset( $_GET['action'] ) ) {
874 $action = (string) wp_unslash( $_GET['action'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- we check for nonces below, and check against specific strings in switch statement.
875 } else {
876 return;
877 }
878
879 switch ( $action ) {
880 case 'move-item-up':
881 case 'move-item-down':
882 $reorder = false;
883
884 if ( empty( $_GET['post_id'] ) ) {
885 break;
886 }
887
888 $post_id = (int) $_GET['post_id'];
889
890 $term = $this->get_menu_item_menu_leaf( $post_id );
891
892 // Get all posts in that term.
893 $query = new WP_Query(
894 array(
895 'taxonomy' => self::MENU_TAX,
896 'term' => $term->slug,
897 )
898 );
899
900 $order = array();
901 foreach ( $query->posts as $post ) {
902 $order[] = $post->ID;
903 }
904
905 if ( 'move-item-up' === $action ) {
906 check_admin_referer( 'nova_move_item_up_' . $post_id );
907
908 $first_post_id = $order[0];
909 if ( $post_id === $first_post_id ) {
910 break;
911 }
912
913 foreach ( $order as $menu_order => $order_post_id ) {
914 if ( $post_id !== $order_post_id ) {
915 continue;
916 }
917
918 $swap_post_id = $order[ $menu_order - 1 ];
919 $order[ $menu_order - 1 ] = $post_id;
920 $order[ $menu_order ] = $swap_post_id;
921
922 $reorder = true;
923 break;
924 }
925 } else {
926 check_admin_referer( 'nova_move_item_down_' . $post_id );
927
928 $last_post_id = end( $order );
929 if ( $post_id === $last_post_id ) {
930 break;
931 }
932
933 foreach ( $order as $menu_order => $order_post_id ) {
934 if ( $post_id !== $order_post_id ) {
935 continue;
936 }
937
938 $swap_post_id = $order[ $menu_order + 1 ];
939 $order[ $menu_order + 1 ] = $post_id;
940 $order[ $menu_order ] = $swap_post_id;
941
942 $reorder = true;
943 }
944 }
945
946 if ( $reorder ) {
947 foreach ( $order as $menu_order => $id ) {
948 wp_update_post( compact( 'id', 'menu_order' ) );
949 }
950 }
951
952 break;
953 case 'move-menu-up':
954 case 'move-menu-down':
955 $reorder = false;
956
957 if ( empty( $_GET['term_id'] ) ) {
958 break;
959 }
960
961 $term_id = (int) $_GET['term_id'];
962
963 $terms = $this->get_menus();
964
965 $order = array();
966 foreach ( $terms as $term ) {
967 $order[] = $term->term_id;
968 }
969
970 if ( 'move-menu-up' === $action ) {
971 check_admin_referer( 'nova_move_menu_up_' . $term_id );
972
973 $first_term_id = $order[0];
974 if ( $term_id === $first_term_id ) {
975 break;
976 }
977
978 foreach ( $order as $menu_order => $order_term_id ) {
979 if ( $term_id !== $order_term_id ) {
980 continue;
981 }
982
983 $swap_term_id = $order[ $menu_order - 1 ];
984 $order[ $menu_order - 1 ] = $term_id;
985 $order[ $menu_order ] = $swap_term_id;
986
987 $reorder = true;
988 break;
989 }
990 } else {
991 check_admin_referer( 'nova_move_menu_down_' . $term_id );
992
993 $last_term_id = end( $order );
994 if ( $term_id === $last_term_id ) {
995 break;
996 }
997
998 foreach ( $order as $menu_order => $order_term_id ) {
999 if ( $term_id !== $order_term_id ) {
1000 continue;
1001 }
1002
1003 $swap_term_id = $order[ $menu_order + 1 ];
1004 $order[ $menu_order + 1 ] = $term_id;
1005 $order[ $menu_order ] = $swap_term_id;
1006
1007 $reorder = true;
1008 }
1009 }
1010
1011 if ( $reorder ) {
1012 update_option( 'nova_menu_order', $order );
1013 }
1014
1015 break;
1016 default:
1017 return;
1018 }
1019
1020 $redirect = add_query_arg(
1021 array(
1022 'post_type' => self::MENU_ITEM_POST_TYPE,
1023 'nova_reordered' => '1',
1024 ),
1025 admin_url( 'edit.php' )
1026 );
1027 wp_safe_redirect( $redirect );
1028 exit;
1029 }
1030
1031 /**
1032 * Add menu title rows to the list table
1033 *
1034 * @param WP_Post $post The Post object.
1035 *
1036 * @return void
1037 */
1038 public function show_menu_titles_in_menu_item_list( $post ) {
1039 global $wp_list_table;
1040
1041 static $last_term_id = false;
1042
1043 $term = $this->get_menu_item_menu_leaf( $post->ID );
1044
1045 $term_id = $term instanceof WP_Term ? $term->term_id : null;
1046
1047 if ( false !== $last_term_id && $last_term_id === $term_id ) {
1048 return;
1049 }
1050
1051 if ( $term_id === null ) {
1052 $last_term_id = null;
1053 $term_name = '';
1054 $parent_count = 0;
1055 } else {
1056 $last_term_id = $term->term_id;
1057 $term_name = $term->name;
1058 $parent_count = 0;
1059 $current_term = $term;
1060 while ( $current_term->parent ) {
1061 $parent_count++;
1062 $current_term = get_term( $current_term->parent, self::MENU_TAX );
1063 }
1064 }
1065
1066 $non_order_column_count = $wp_list_table->get_column_count() - 1;
1067
1068 $screen = get_current_screen();
1069
1070 $url = admin_url( $screen->parent_file );
1071
1072 $up_url = add_query_arg(
1073 array(
1074 'action' => 'move-menu-up',
1075 'term_id' => (int) $term_id,
1076 ),
1077 wp_nonce_url( $url, 'nova_move_menu_up_' . $term_id )
1078 );
1079
1080 $down_url = add_query_arg(
1081 array(
1082 'action' => 'move-menu-down',
1083 'term_id' => (int) $term_id,
1084 ),
1085 wp_nonce_url( $url, 'nova_move_menu_down_' . $term_id )
1086 );
1087
1088 ?>
1089 <tr class="no-items menu-label-row" data-term_id="<?php echo esc_attr( $term_id ); ?>">
1090 <td class="colspanchange" colspan="<?php echo (int) $non_order_column_count; ?>">
1091 <h3>
1092 <?php
1093 echo str_repeat( ' &mdash; ', (int) $parent_count ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- nothing to escape here.
1094
1095 if ( $term instanceof WP_Term ) {
1096 echo esc_html( sanitize_term_field( 'name', $term_name, $term_id, self::MENU_TAX, 'display' ) );
1097 edit_term_link( __( 'edit', 'jetpack' ), '<span class="edit-nova-section"><span class="dashicon dashicon-edit"></span>', '</span>', $term );
1098
1099 } else {
1100 esc_html_e( 'Uncategorized', 'jetpack' );
1101 }
1102 ?>
1103 </h3>
1104 </td>
1105 <td>
1106 <?php if ( $term instanceof WP_Term ) { ?>
1107 <a class="nova-move-menu-up" title="<?php esc_attr_e( 'Move menu section up', 'jetpack' ); ?>" href="<?php echo esc_url( $up_url ); ?>"><?php echo esc_html_x( 'UP', 'indicates movement (up or down)', 'jetpack' ); ?></a>
1108 <br />
1109 <a class="nova-move-menu-down" title="<?php esc_attr_e( 'Move menu section down', 'jetpack' ); ?>" href="<?php echo esc_url( $down_url ); ?>"><?php echo esc_html_x( 'DOWN', 'indicates movement (up or down)', 'jetpack' ); ?></a>
1110 <?php } ?>
1111 </td>
1112 </tr>
1113 <?php
1114 }
1115
1116 /* Edit Many Items */
1117
1118 /**
1119 * Handle form submissions that aim to add many menu items at once.
1120 * (process posted data and enqueue necessary script).
1121 *
1122 * @return void
1123 */
1124 public function add_many_new_items_page_load() {
1125 if (
1126 isset( $_SERVER['REQUEST_METHOD'] )
1127 && 'POST' === strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) )
1128 ) {
1129 $this->process_form_request();
1130 exit;
1131 }
1132
1133 $this->enqueue_many_items_scripts();
1134 }
1135
1136 /**
1137 * Enqueue script to create many items at once.
1138 *
1139 * @return void
1140 */
1141 public function enqueue_many_items_scripts() {
1142 wp_enqueue_script(
1143 'nova-many-items',
1144 Assets::get_file_url_for_environment(
1145 '_inc/build/custom-post-types/js/many-items.min.js',
1146 'modules/custom-post-types/js/many-items.js'
1147 ),
1148 array( 'jquery' ),
1149 $this->version,
1150 true
1151 );
1152 }
1153
1154 /**
1155 * Process form request to create many items at once.
1156 *
1157 * @return void
1158 */
1159 public function process_form_request() {
1160 if ( ! isset( $_POST['nova_title'] ) || ! is_array( $_POST['nova_title'] ) ) {
1161 return;
1162 }
1163
1164 $is_ajax = ! empty( $_POST['ajax'] );
1165
1166 if ( $is_ajax ) {
1167 check_ajax_referer( 'nova_many_items' );
1168 } else {
1169 check_admin_referer( 'nova_many_items' );
1170 }
1171
1172 /*
1173 * $_POST is already slashed
1174 * phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash
1175 */
1176 foreach ( array_keys( $_POST['nova_title'] ) as $key ) : // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- we sanitize below.
1177 $post_details = array(
1178 'post_status' => 'publish',
1179 'post_type' => self::MENU_ITEM_POST_TYPE,
1180 'post_content' => ! empty( $_POST['nova_content'] ) && ! empty( $_POST['nova_content'][ $key ] )
1181 ? sanitize_text_field( $_POST['nova_content'][ $key ] )
1182 : '',
1183 'post_title' => isset( $_POST['nova_title'][ $key ] )
1184 ? sanitize_title( $_POST['nova_title'][ $key ] )
1185 : '',
1186 'tax_input' => array(
1187 self::MENU_ITEM_LABEL_TAX => isset( $_POST['nova_labels'][ $key ] )
1188 ? sanitize_meta( self::MENU_ITEM_LABEL_TAX, $_POST['nova_labels'][ $key ], 'term' )
1189 : null,
1190 self::MENU_TAX => isset( $_POST['nova_menu_tax'] )
1191 ? sanitize_meta( self::MENU_TAX, $_POST['nova_menu_tax'], 'term' )
1192 : null,
1193 ),
1194 );
1195
1196 $post_id = wp_insert_post( $post_details );
1197 if ( ! $post_id || is_wp_error( $post_id ) ) {
1198 continue;
1199 }
1200
1201 $this->set_price(
1202 $post_id,
1203 isset( $_POST['nova_price'][ $key ] )
1204 ? sanitize_meta( 'nova_price', $_POST['nova_price'][ $key ], 'post' )
1205 : ''
1206 );
1207 // phpcs:enable WordPress.Security.ValidatedSanitizedInput.MissingUnslash
1208
1209 if ( $is_ajax ) :
1210 $post = get_post( $post_id );
1211 $GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
1212 setup_postdata( $post );
1213
1214 ?>
1215 <td><?php the_title(); ?></td>
1216 <td class="nova-price"><?php $this->display_price(); ?></td>
1217 <td><?php $this->list_labels( $post_id ); ?></td>
1218 <td><?php the_content(); ?></td>
1219 <?php
1220 endif;
1221
1222 endforeach;
1223
1224 if ( $is_ajax ) {
1225 exit;
1226 }
1227
1228 wp_safe_redirect( admin_url( 'edit.php?post_type=' . self::MENU_ITEM_POST_TYPE ) );
1229 exit;
1230 }
1231
1232 /**
1233 * Admin page contents for adding many menu items at once.
1234 *
1235 * @return void
1236 */
1237 public function add_many_new_items_page() {
1238 ?>
1239 <div class="wrap">
1240 <h2><?php esc_html_e( 'Add Many Items', 'jetpack' ); ?></h2>
1241
1242 <p>
1243 <?php
1244 echo wp_kses(
1245 __( 'Use the <kbd>TAB</kbd> key on your keyboard to move between colums and the <kbd>ENTER</kbd> or <kbd>RETURN</kbd> key to save each row and move on to the next.', 'jetpack' ),
1246 array(
1247 'kbd' => array(),
1248 )
1249 );
1250 ?>
1251 </p>
1252
1253 <form method="post" action="" enctype="multipart/form-data">
1254 <p>
1255 <h3><?php esc_html_e( 'Add to section:', 'jetpack' ); ?>
1256 <?php
1257 wp_dropdown_categories(
1258 array(
1259 'id' => 'nova-menu-tax',
1260 'name' => 'nova_menu_tax',
1261 'taxonomy' => self::MENU_TAX,
1262 'hide_empty' => false,
1263 'hierarchical' => true,
1264 )
1265 );
1266 ?>
1267 </h3></p>
1268
1269 <table class="many-items-table wp-list-table widefat">
1270 <thead>
1271 <tr>
1272 <th scope="col"><?php esc_html_e( 'Name', 'jetpack' ); ?></th>
1273 <th scope="col" class="nova-price"><?php esc_html_e( 'Price', 'jetpack' ); ?></th>
1274 <th scope="col">
1275 <?php
1276 echo wp_kses(
1277 __( 'Labels: <small>spicy, favorite, etc. <em>Separate Labels with commas</em></small>', 'jetpack' ),
1278 array(
1279 'small' => array(),
1280 'em' => array(),
1281 )
1282 );
1283 ?>
1284 </th>
1285 <th scope="col"><?php esc_html_e( 'Description', 'jetpack' ); ?></th>
1286 </tr>
1287 </thead>
1288 <tbody>
1289 <tr>
1290 <td><input type="text" name="nova_title[]" aria-required="true" /></td>
1291 <td class="nova-price"><input type="text" name="nova_price[]" /></td>
1292 <td><input type="text" name="nova_labels[]" /></td>
1293 <td><textarea name="nova_content[]" cols="20" rows="1"></textarea>
1294 </tr>
1295 </tbody>
1296 <tbody>
1297 <tr>
1298 <td><input type="text" name="nova_title[]" aria-required="true" /></td>
1299 <td class="nova-price"><input type="text" name="nova_price[]" /></td>
1300 <td><input type="text" name="nova_labels[]" /></td>
1301 <td><textarea name="nova_content[]" cols="20" rows="1"></textarea>
1302 </tr>
1303 </tbody>
1304 <tfoot>
1305 <tr>
1306 <th><a class="button button-secondary nova-new-row"><span class="dashicon dashicon-plus"></span> <?php esc_html_e( 'New Row', 'jetpack' ); ?></a></th>
1307 <th class="nova-price"></th>
1308 <th></th>
1309 <th></th>
1310 </tr>
1311 </tfoot>
1312 </table>
1313
1314 <p class="submit">
1315 <input type="submit" class="button-primary" value="<?php esc_attr_e( 'Add These New Menu Items', 'jetpack' ); ?>" />
1316 <?php wp_nonce_field( 'nova_many_items' ); ?>
1317 </p>
1318 </form>
1319 </div>
1320 <?php
1321 }
1322
1323 /* Edit One Item */
1324
1325 /**
1326 * Create admin meta box to save price for a menu item,
1327 * and add script to add extra checkboxes to the UI.
1328 *
1329 * @return void
1330 */
1331 public function register_menu_item_meta_boxes() {
1332 wp_enqueue_script( 'nova-menu-checkboxes' );
1333
1334 add_meta_box(
1335 'menu_item_price',
1336 __( 'Price', 'jetpack' ),
1337 array( $this, 'menu_item_price_meta_box' ),
1338 null,
1339 'side',
1340 'high'
1341 );
1342 }
1343
1344 /**
1345 * Meta box to edit the price of a menu item.
1346 *
1347 * @param WP_Post $post The post object.
1348 *
1349 * @return void
1350 */
1351 public function menu_item_price_meta_box( $post ) {
1352 printf(
1353 '<label for="nova-price-%1$s" class="screen-reader-text">%2$s</label><input type="text" id="nova-price-%1$s" class="widefat" name="nova_price[%1$s]" value="%3$s" />',
1354 (int) $post->ID,
1355 esc_html__( 'Price', 'jetpack' ),
1356 esc_attr( $this->get_price( (int) $post->ID ) )
1357 );
1358 }
1359
1360 /**
1361 * Save the price of a menu item.
1362 *
1363 * @param int $post_id Post ID.
1364 */
1365 public function add_post_meta( $post_id ) {
1366 if ( ! isset( $_POST['nova_price'][ $post_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce handling happens via core, since we hook into wp_insert_post.
1367 return;
1368 }
1369
1370 $this->set_price(
1371 $post_id,
1372 sanitize_meta( 'nova_price', wp_unslash( $_POST['nova_price'][ $post_id ] ), 'post' ) // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce handling happens via core, since we hook into wp_insert_post.
1373 );
1374 }
1375
1376 /* Data */
1377
1378 /**
1379 * Get ordered array of menu items.
1380 *
1381 * @param array $args Optional argumments.
1382 *
1383 * @return array
1384 */
1385 public function get_menus( $args = array() ) {
1386 $args = wp_parse_args(
1387 $args,
1388 array(
1389 'hide_empty' => false,
1390 )
1391 );
1392
1393 $terms = get_terms( self::MENU_TAX, $args );
1394 if ( ! $terms || is_wp_error( $terms ) ) {
1395 return array();
1396 }
1397
1398 $terms_by_id = array();
1399 foreach ( $terms as $term ) {
1400 $terms_by_id[ "{$term->term_id}" ] = $term;
1401 }
1402
1403 $term_order = get_option( 'nova_menu_order', array() );
1404
1405 $return = array();
1406 foreach ( $term_order as $term_id ) {
1407 if ( isset( $terms_by_id[ "$term_id" ] ) ) {
1408 $return[] = $terms_by_id[ "$term_id" ];
1409 unset( $terms_by_id[ "$term_id" ] );
1410 }
1411 }
1412
1413 foreach ( $terms_by_id as $term_id => $term ) {
1414 $return[] = $term;
1415 }
1416
1417 return $return;
1418 }
1419
1420 /**
1421 * Get first menu taxonomy "leaf".
1422 *
1423 * @param int $post_id Post ID.
1424 *
1425 * @return bool|WP_Term|WP_Error|null
1426 */
1427 public function get_menu_item_menu_leaf( $post_id ) {
1428 // Get first menu taxonomy "leaf".
1429 $term_ids = wp_get_object_terms( $post_id, self::MENU_TAX, array( 'fields' => 'ids' ) );
1430
1431 foreach ( $term_ids as $term_id ) {
1432 $children = get_term_children( $term_id, self::MENU_TAX );
1433 if ( ! $children ) {
1434 break;
1435 }
1436 }
1437
1438 if ( ! isset( $term_id ) ) {
1439 return false;
1440 }
1441
1442 return get_term( $term_id, self::MENU_TAX );
1443
1444 }
1445
1446 /**
1447 * Get a list of the labels linked to a menu item.
1448 *
1449 * @param int $post_id Post ID.
1450 *
1451 * @return void
1452 */
1453 public function list_labels( $post_id = 0 ) {
1454 $post = get_post( $post_id );
1455 echo get_the_term_list( $post->ID, self::MENU_ITEM_LABEL_TAX, '', _x( ', ', 'Nova label separator', 'jetpack' ), '' );
1456 }
1457
1458 /**
1459 * Get a list of the labels linked to a menu item, with links to manage them.
1460 *
1461 * @param int $post_id Post ID.
1462 *
1463 * @return void
1464 */
1465 public function list_admin_labels( $post_id = 0 ) {
1466 $post = get_post( $post_id );
1467 $labels = get_the_terms( $post->ID, self::MENU_ITEM_LABEL_TAX );
1468 if ( ! empty( $labels ) ) {
1469 $out = array();
1470 foreach ( $labels as $label ) {
1471 $out[] = sprintf(
1472 '<a href="%s">%s</a>',
1473 esc_url(
1474 add_query_arg(
1475 array(
1476 'post_type' => self::MENU_ITEM_POST_TYPE,
1477 'taxonomy' => self::MENU_ITEM_LABEL_TAX,
1478 'term' => $label->slug,
1479 ),
1480 'edit.php'
1481 )
1482 ),
1483 esc_html(
1484 sanitize_term_field( 'name', $label->name, $label->term_id, self::MENU_ITEM_LABEL_TAX, 'display' )
1485 )
1486 );
1487 }
1488
1489 echo join( _x( ', ', 'Nova label separator', 'jetpack' ), $out ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- we build $out ourselves and escape things there.
1490 } else {
1491 esc_html_e( 'No Labels', 'jetpack' );
1492 }
1493 }
1494
1495 /**
1496 * Update post meta with the price defined in meta box.
1497 *
1498 * @param int $post_id Post ID.
1499 * @param string $price Price.
1500 *
1501 * @return int|bool
1502 */
1503 public function set_price( $post_id = 0, $price = '' ) {
1504 return update_post_meta( $post_id, 'nova_price', $price );
1505 }
1506
1507 /**
1508 * Get the price of a menu item.
1509 *
1510 * @param int $post_id Post ID.
1511 *
1512 * @return bool|string
1513 */
1514 public function get_price( $post_id = 0 ) {
1515 return get_post_meta( $post_id, 'nova_price', true );
1516 }
1517
1518 /**
1519 * Echo the price of a menu item.
1520 *
1521 * @param int $post_id Post ID.
1522 *
1523 * @return void
1524 */
1525 public function display_price( $post_id = 0 ) {
1526 echo esc_html( $this->get_price( $post_id ) );
1527 }
1528
1529 /* Menu Item Loop Markup */
1530
1531 /**
1532 * Get markup for a menu item.
1533 * Note: Does not support nested loops.
1534 *
1535 * @param null|string $field The field to get the value for.
1536 *
1537 * @return array
1538 */
1539 public function get_menu_item_loop_markup( $field = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1540 return $this->menu_item_loop_markup;
1541 }
1542
1543 /**
1544 * Sets up the loop markup.
1545 * Attached to the 'template_include' *filter*,
1546 * which fires only during a real blog view (not in admin, feeds, etc.)
1547 *
1548 * @param string $template Template File.
1549 *
1550 * @return string Template File. VERY Important.
1551 */
1552 public function setup_menu_item_loop_markup__in_filter( $template ) {
1553 add_action( 'loop_start', array( $this, 'start_menu_item_loop' ) );
1554
1555 return $template;
1556 }
1557
1558 /**
1559 * If the Query is a Menu Item Query, start outputing the Menu Item Loop Marku
1560 * Attached to the 'loop_start' action.
1561 *
1562 * @param WP_Query $query Post query.
1563 *
1564 * @return void
1565 */
1566 public function start_menu_item_loop( $query ) {
1567 if ( ! $this->is_menu_item_query( $query ) ) {
1568 return;
1569 }
1570
1571 $this->menu_item_loop_last_term_id = false;
1572 $this->menu_item_loop_current_term = false;
1573
1574 add_action( 'the_post', array( $this, 'menu_item_loop_each_post' ) );
1575 add_action( 'loop_end', array( $this, 'stop_menu_item_loop' ) );
1576 }
1577
1578 /**
1579 * Outputs the Menu Item Loop Marku
1580 * Attached to the 'the_post' action.
1581 *
1582 * @param WP_Post $post Post object.
1583 *
1584 * @return void
1585 */
1586 public function menu_item_loop_each_post( $post ) {
1587 $this->menu_item_loop_current_term = $this->get_menu_item_menu_leaf( $post->ID );
1588
1589 if ( false === $this->menu_item_loop_last_term_id ) {
1590 // We're at the very beginning of the loop
1591
1592 $this->menu_item_loop_open_element( 'menu' ); // Start a new menu section
1593 $this->menu_item_loop_header(); // Output the menu's header
1594 } elseif ( $this->menu_item_loop_last_term_id !== $this->menu_item_loop_current_term->term_id ) {
1595 // We're not at the very beginning but still need to start a new menu section. End the previous menu section first.
1596
1597 $this->menu_item_loop_close_element( 'menu' ); // End the previous menu section
1598 $this->menu_item_loop_open_element( 'menu' ); // Start a new menu section
1599 $this->menu_item_loop_header(); // Output the menu's header
1600 }
1601
1602 $this->menu_item_loop_last_term_id = $this->menu_item_loop_current_term->term_id;
1603 }
1604
1605 /**
1606 * If the Query is a Menu Item Query, stop outputing the Menu Item Loop Marku
1607 * Attached to the 'loop_end' action.
1608 *
1609 * @param WP_Query $query Post query.
1610 *
1611 * @return void
1612 */
1613 public function stop_menu_item_loop( $query ) {
1614 if ( ! $this->is_menu_item_query( $query ) ) {
1615 return;
1616 }
1617
1618 remove_action( 'the_post', array( $this, 'menu_item_loop_each_post' ) );
1619 remove_action( 'loop_start', array( $this, 'start_menu_item_loop' ) );
1620 remove_action( 'loop_end', array( $this, 'stop_menu_item_loop' ) );
1621
1622 $this->menu_item_loop_close_element( 'menu' ); // End the last menu section
1623 }
1624
1625 /**
1626 * Outputs the Menu Group Header
1627 *
1628 * @return void
1629 */
1630 public function menu_item_loop_header() {
1631 $this->menu_item_loop_open_element( 'menu_header' );
1632 $this->menu_item_loop_open_element( 'menu_title' );
1633 echo esc_html( $this->menu_item_loop_current_term->name ); // @todo tax filter
1634 $this->menu_item_loop_close_element( 'menu_title' );
1635 if ( $this->menu_item_loop_current_term->description ) :
1636 $this->menu_item_loop_open_element( 'menu_description' );
1637 echo esc_html( $this->menu_item_loop_current_term->description ); // @todo kses, tax filter
1638 $this->menu_item_loop_close_element( 'menu_description' );
1639 endif;
1640 $this->menu_item_loop_close_element( 'menu_header' );
1641 }
1642
1643 /**
1644 * Outputs a Menu Item Markup element opening tag
1645 *
1646 * @param string $field - Menu Item Markup settings field.
1647 *
1648 * @return void
1649 */
1650 public function menu_item_loop_open_element( $field ) {
1651 $markup = $this->get_menu_item_loop_markup();
1652 /**
1653 * Filter a menu item's element opening tag.
1654 *
1655 * @module custom-content-types
1656 *
1657 * @since 4.4.0
1658 *
1659 * @param string $tag Menu item's element opening tag.
1660 * @param string $field Menu Item Markup settings field.
1661 * @param array $markup Array of markup elements for the menu item.
1662 * @param false|object $term Taxonomy term for current menu item.
1663 */
1664 echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- it's escaped in menu_item_loop_class.
1665 'jetpack_nova_menu_item_loop_open_element',
1666 '<' . tag_escape( $markup[ "{$field}_tag" ] ) . $this->menu_item_loop_class( $markup[ "{$field}_class" ] ) . ">\n",
1667 $field,
1668 $markup,
1669 $this->menu_item_loop_current_term
1670 );
1671 }
1672
1673 /**
1674 * Outputs a Menu Item Markup element closing tag
1675 *
1676 * @param string $field - Menu Item Markup settings field.
1677 *
1678 * @return void
1679 */
1680 public function menu_item_loop_close_element( $field ) {
1681 $markup = $this->get_menu_item_loop_markup();
1682 /**
1683 * Filter a menu item's element closing tag.
1684 *
1685 * @module custom-content-types
1686 *
1687 * @since 4.4.0
1688 *
1689 * @param string $tag Menu item's element closing tag.
1690 * @param string $field Menu Item Markup settings field.
1691 * @param array $markup Array of markup elements for the menu item.
1692 * @param false|object $term Taxonomy term for current menu item.
1693 */
1694 echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- tag_escape is used.
1695 'jetpack_nova_menu_item_loop_close_element',
1696 '</' . tag_escape( $markup[ "{$field}_tag" ] ) . ">\n",
1697 $field,
1698 $markup,
1699 $this->menu_item_loop_current_term
1700 );
1701 }
1702
1703 /**
1704 * Returns a Menu Item Markup element's class attribute.
1705 *
1706 * @param string $class Class name.
1707 *
1708 * @return string HTML class attribute with leading whitespace.
1709 */
1710 public function menu_item_loop_class( $class ) {
1711 if ( ! $class ) {
1712 return '';
1713 }
1714
1715 /**
1716 * Filter a menu Item Markup element's class attribute.
1717 *
1718 * @module custom-content-types
1719 *
1720 * @since 4.4.0
1721 *
1722 * @param string $tag Menu Item Markup element's class attribute.
1723 * @param string $class Menu Item Class name.
1724 * @param false|object $term Taxonomy term for current menu item.
1725 */
1726 return apply_filters(
1727 'jetpack_nova_menu_item_loop_class',
1728 ' class="' . esc_attr( $class ) . '"',
1729 $class,
1730 $this->menu_item_loop_current_term
1731 );
1732 }
1733 }
1734
1735 add_action( 'init', array( 'Nova_Restaurant', 'init' ) );
1736