PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 3.2.5
Jetpack – WP Security, Backup, Speed, & Growth v3.2.5
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 3.2.5, at modules/custom-post-types/nova.php

1,146 lines 35.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * Plugin Name: Nova - Restaurant Websites Shouldn't Suck
5 * Plugin URI: http://wordpress.org/extend/plugins/nova/
6 * Author: Automattic
7 * Version: 0.1
8 * License: GPL2+
9 * Text Domain: nova
10 * Domain Path: /languages/
11 */
12
13 /*
14 * Put the following code in your theme's Food Menu Page Template to customize the markup of the menu.
15
16 if ( class_exists( 'Nova_Restaurant' ) ) {
17 Nova_Restaurant::init( array(
18 'menu_tag' => 'section',
19 'menu_class' => 'menu-items',
20 'menu_header_tag' => 'header',
21 'menu_header_class' => 'menu-group-header',
22 'menu_title_tag' => 'h1',
23 'menu_title_class' => 'menu-group-title',
24 'menu_description_tag' => 'div',
25 'menu_description_class' => 'menu-group-description',
26 ) );
27 }
28
29 */
30
31 /* @todo
32
33 Bulk/Quick edit response of Menu Item rows is broken.
34
35 Drag and Drop reordering.
36 */
37
38 class Nova_Restaurant {
39 const MENU_ITEM_POST_TYPE = 'nova_menu_item';
40 const MENU_ITEM_LABEL_TAX = 'nova_menu_item_label';
41 const MENU_TAX = 'nova_menu';
42
43 var $version = '0.1';
44
45 protected $default_menu_item_loop_markup = array(
46 'menu_tag' => 'section',
47 'menu_class' => 'menu-items',
48 'menu_header_tag' => 'header',
49 'menu_header_class' => 'menu-group-header',
50 'menu_title_tag' => 'h1',
51 'menu_title_class' => 'menu-group-title',
52 'menu_description_tag' => 'div',
53 'menu_description_class' => 'menu-group-description',
54 );
55
56 protected $menu_item_loop_markup = array();
57 protected $menu_item_loop_last_term_id = false;
58 protected $menu_item_loop_current_term = false;
59
60 static function init( $menu_item_loop_markup = array() ) {
61 static $instance = false;
62
63 if ( !$instance ) {
64 $instance = new Nova_Restaurant;
65 }
66
67 if ( $menu_item_loop_markup ) {
68 $instance->menu_item_loop_markup = wp_parse_args( $menu_item_loop_markup, $this->default_menu_item_loop_markup );
69 }
70
71 return $instance;
72 }
73
74 function __construct() {
75 if ( ! $this->site_supports_nova() )
76 return;
77
78 $this->register_taxonomies();
79 $this->register_post_types();
80 add_action( 'admin_menu', array( $this, 'add_admin_menus' ) );
81 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_nova_styles' ) );
82 add_action( 'admin_head', array( $this, 'set_custom_font_icon' ) );
83
84 // Always sort menu items correctly
85 add_action( 'parse_query', array( $this, 'sort_menu_item_queries_by_menu_order' ) );
86 add_filter( 'posts_results', array( $this, 'sort_menu_item_queries_by_menu_taxonomy' ), 10, 2 );
87
88 add_action( 'wp_insert_post', array( $this, 'add_post_meta' ) );
89
90 $this->menu_item_loop_markup = $this->default_menu_item_loop_markup;
91
92 // Only output our Menu Item Loop Markup on a real blog view. Not feeds, XML-RPC, admin, etc.
93 add_filter( 'template_include', array( $this, 'setup_menu_item_loop_markup__in_filter' ) );
94
95 add_filter( 'enter_title_here', array( $this, 'change_default_title' ) );
96 add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) );
97 add_filter( 'dashboard_glance_items', array( $this, 'add_to_dashboard' ) );
98 }
99
100 /**
101 * Should this Custom Post Type be made available?
102 */
103 function site_supports_nova() {
104 // If we're on WordPress.com, and it has the menu site vertical.
105 if ( function_exists( 'site_vertical' ) && 'nova_menu' == site_vertical() )
106 return true;
107
108 // Else, if the current theme requests it.
109 if ( current_theme_supports( self::MENU_ITEM_POST_TYPE ) )
110 return true;
111
112 // Otherwise, say no unless something wants to filter us to say yes.
113 return (bool) apply_filters( 'jetpack_enable_cpt', false, self::MENU_ITEM_POST_TYPE );
114 }
115
116 /* Setup */
117
118 /**
119 * Register Taxonomies and Post Type
120 */
121 function register_taxonomies() {
122 register_taxonomy( self::MENU_ITEM_LABEL_TAX, self::MENU_ITEM_POST_TYPE, array(
123 'labels' => array(
124 'name' => __( 'Menu Item Labels', 'jetpack' ),
125 'singular_name' => __( 'Menu Item Label', 'jetpack' ),
126 'search_items' => __( 'Search Menu Item Labels', 'jetpack' ),
127 'popular_items' => __( 'Popular Labels', 'jetpack' ),
128 'all_items' => __( 'All Menu Item Labels', 'jetpack' ),
129 'edit_item' => __( 'Edit Menu Item Label', 'jetpack' ),
130 'view_item' => __( 'View Menu Item Label', 'jetpack' ),
131 'update_item' => __( 'Update Menu Item Label', 'jetpack' ),
132 'add_new_item' => __( 'Add New Menu Item Label', 'jetpack' ),
133 'new_item_name' => __( 'New Menu Item Label Name', 'jetpack' ),
134 'separate_items_with_commas' => __( 'For example, spicy, favorite, etc. <br /> Separate Labels with commas', 'jetpack' ),
135 'add_or_remove_items' => __( 'Add or remove Labels', 'jetpack' ),
136 'choose_from_most_used' => __( 'Choose from the most used Labels', 'jetpack' ),
137 ),
138 'no_tagcloud' => __( 'No Labels found', 'jetpack' ),
139
140 'hierarchical' => false,
141 ) );
142
143 register_taxonomy( self::MENU_TAX, self::MENU_ITEM_POST_TYPE, array(
144 'labels' => array(
145 'name' => __( 'Menu Sections', 'jetpack' ),
146 'singular_name' => __( 'Menu Section', 'jetpack' ),
147 'search_items' => __( 'Search Menu Sections', 'jetpack' ),
148 'all_items' => __( 'All Menu Sections', 'jetpack' ),
149 'parent_item' => __( 'Parent Menu Section', 'jetpack' ),
150 'parent_item_colon' => __( 'Parent Menu Section:', 'jetpack' ),
151 'edit_item' => __( 'Edit Menu Section', 'jetpack' ),
152 'view_item' => __( 'View Menu Section', 'jetpack' ),
153 'update_item' => __( 'Update Menu Section', 'jetpack' ),
154 'add_new_item' => __( 'Add New Menu Section', 'jetpack' ),
155 'new_item_name' => __( 'New Menu Sections Name', 'jetpack' ),
156 ),
157 'rewrite' => array(
158 'slug' => 'menu',
159 'with_front' => false,
160 'hierarchical' => true,
161 ),
162
163 'hierarchical' => true,
164 'show_tagcloud' => false,
165 'query_var' => 'menu',
166 ) );
167 }
168
169 function register_post_types() {
170 register_post_type( self::MENU_ITEM_POST_TYPE, array(
171 'description' => __( "Items on your restaurant's menu", 'jetpack' ),
172
173 'labels' => array(
174 'name' => __( 'Menu Items', 'jetpack' ),
175 'singular_name' => __( 'Menu Item', 'jetpack' ),
176 'menu_name' => __( 'Food Menus', 'jetpack' ),
177 'all_items' => __( 'Menu Items', 'jetpack' ),
178 'add_new' => __( 'Add One Item', 'jetpack' ),
179 'add_new_item' => __( 'Add Menu Item', 'jetpack' ),
180 'edit_item' => __( 'Edit Menu Item', 'jetpack' ),
181 'new_item' => __( 'New Menu Item', 'jetpack' ),
182 'view_item' => __( 'View Menu Item', 'jetpack' ),
183 'search_items' => __( 'Search Menu Items', 'jetpack' ),
184 'not_found' => __( 'No Menu Items found', 'jetpack' ),
185 'not_found_in_trash' => __( 'No Menu Items found in Trash', 'jetpack' ),
186 ),
187 'supports' => array(
188 'title',
189 'editor',
190 'thumbnail',
191 'excerpt',
192 ),
193 'rewrite' => array(
194 'slug' => 'item',
195 'with_front' => false,
196 'feeds' => false,
197 'pages' => false,
198 ),
199 'register_meta_box_cb' => array( $this, 'register_menu_item_meta_boxes' ),
200
201 'public' => true,
202 'show_ui' => true, // set to false to replace with custom UI
203 'menu_position' => 20, // below Pages
204 'capability_type' => 'page',
205 'map_meta_cap' => true,
206 'has_archive' => false,
207 'query_var' => 'item',
208 ) );
209 }
210
211
212 /**
213 * Update messages for the Menu Item admin.
214 */
215 function updated_messages( $messages ) {
216 global $post;
217
218 $messages[self::MENU_ITEM_POST_TYPE] = array(
219 0 => '', // Unused. Messages start at index 1.
220 1 => sprintf( __( 'Menu item updated. <a href="%s">View item</a>', 'jetpack' ), esc_url( get_permalink( $post->ID ) ) ),
221 2 => esc_html__( 'Custom field updated.', 'jetpack' ),
222 3 => esc_html__( 'Custom field deleted.', 'jetpack' ),
223 4 => esc_html__( 'Menu item updated.', 'jetpack' ),
224 /* translators: %s: date and time of the revision */
225 5 => isset( $_GET['revision'] ) ? sprintf( esc_html__( 'Menu item restored to revision from %s', 'jetpack' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
226 6 => sprintf( __( 'Menu item published. <a href="%s">View item</a>', 'jetpack' ), esc_url( get_permalink( $post->ID ) ) ),
227 7 => esc_html__( 'Menu item saved.', 'jetpack' ),
228 8 => sprintf( __( 'Menu item submitted. <a target="_blank" href="%s">Preview item</a>', 'jetpack' ), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) ),
229 9 => sprintf( __( 'Menu item scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview item</a>', 'jetpack' ),
230 // translators: Publish box date format, see http://php.net/date
231 date_i18n( __( 'M j, Y @ G:i', 'jetpack' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post->ID) ) ),
232 10 => sprintf( __( 'Menu item draft updated. <a target="_blank" href="%s">Preview item</a>', 'jetpack' ), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) ),
233 );
234
235 return $messages;
236 }
237
238
239 /**
240 * Nova Styles and Scripts
241 */
242 function enqueue_nova_styles( $hook ) {
243 global $post_type;
244 $pages = array( 'edit.php', 'post.php', 'post-new.php' );
245
246 if ( in_array( $hook, $pages ) && $post_type == self::MENU_ITEM_POST_TYPE ) {
247 wp_enqueue_style( 'nova-style', plugins_url( 'css/nova.css', __FILE__ ), array(), $this->version );
248 }
249
250 wp_enqueue_style( 'nova-font', plugins_url( 'css/nova-font.css', __FILE__ ), array(), $this->version );
251 }
252
253
254 /**
255 * Change ‘Enter Title Here’ text for the Menu Item.
256 */
257 function change_default_title( $title ) {
258 $screen = get_current_screen();
259
260 if ( self::MENU_ITEM_POST_TYPE == $screen->post_type )
261 $title = esc_html__( "Enter the menu item's name here", 'jetpack' );
262
263 return $title;
264 }
265
266
267 /**
268 * Add to Dashboard At A Glance
269 */
270 function add_to_dashboard() {
271 $number_menu_items = wp_count_posts( self::MENU_ITEM_POST_TYPE );
272 $number_menu_items_published = sprintf( '%1s %2s',
273 number_format_i18n( $number_menu_items->publish ),
274 _n( 'Food Menu Item', 'Food Menu Items', intval( $number_menu_items->publish ), 'jetpack' )
275 );
276
277 if ( current_user_can( 'administrator' ) ) {
278 $number_menu_items_published = sprintf( '<a href="%1s">%2s %3s</a>',
279 esc_url( get_admin_url( get_current_blog_id(), 'edit.php?post_type=' . self::MENU_ITEM_POST_TYPE ) ),
280 number_format_i18n( $number_menu_items->publish ),
281 _n( 'Food Menu Item', 'Food Menu Items', intval( $number_menu_items->publish ), 'jetpack' )
282 );
283 }
284
285 echo '<li class="nova-menu-count">' . $number_menu_items_published . '</li>';
286 }
287
288
289 /**
290 * Query
291 */
292 function is_menu_item_query( $query ) {
293 if (
294 ( isset( $query->query_vars['taxonomy'] ) && self::MENU_TAX == $query->query_vars['taxonomy'] )
295 ||
296 ( isset( $query->query_vars['post_type'] ) && self::MENU_ITEM_POST_TYPE == $query->query_vars['post_type'] )
297 ) {
298 return true;
299 }
300
301 return false;
302 }
303
304 function sort_menu_item_queries_by_menu_order( $query ) {
305 if ( ! $this->is_menu_item_query( $query ) ) {
306 return;
307 }
308
309 $query->query_vars['orderby'] = 'menu_order';
310 $query->query_vars['order'] = 'ASC';
311
312 // For now, just turn off paging so we can sort by taxonmy later
313 // 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)
314 $query->query_vars['posts_per_page'] = -1;
315 }
316
317 function sort_menu_item_queries_by_menu_taxonomy( $posts, $query ) {
318 if ( !$posts ) {
319 return $posts;
320 }
321
322 if ( !$this->is_menu_item_query( $query ) ) {
323 return $posts;
324 }
325
326 $grouped_by_term = array();
327
328 foreach ( $posts as $post ) {
329 $term = $this->get_menu_item_menu_leaf( $post->ID );
330 if ( !$term || is_wp_error( $term ) ) {
331 $term_id = 0;
332 } else {
333 $term_id = $term->term_id;
334 }
335
336 if ( !isset( $grouped_by_term["$term_id"] ) ) {
337 $grouped_by_term["$term_id"] = array();
338 }
339
340 $grouped_by_term["$term_id"][] = $post;
341 }
342
343 $term_order = get_option( 'nova_menu_order', array() );
344
345 $return = array();
346 foreach ( $term_order as $term_id ) {
347 if ( isset( $grouped_by_term["$term_id"] ) ) {
348 $return = array_merge( $return, $grouped_by_term["$term_id"] );
349 unset( $grouped_by_term["$term_id"] );
350 }
351 }
352
353 foreach ( $grouped_by_term as $term_id => $posts ) {
354 $return = array_merge( $return, $posts );
355 }
356
357 return $return;
358 }
359
360
361 /**
362 * Add Many Items
363 */
364 function add_admin_menus() {
365 $hook = add_submenu_page(
366 'edit.php?post_type=' . self::MENU_ITEM_POST_TYPE,
367 __( 'Add Many Items', 'jetpack' ),
368 __( 'Add Many Items', 'jetpack' ),
369 'edit_pages',
370 'add_many_nova_items',
371 array( $this, 'add_many_new_items_page' )
372 );
373
374 add_action( "load-$hook", array( $this, 'add_many_new_items_page_load' ) );
375
376 add_action( 'current_screen', array( $this, 'current_screen_load' ) );
377
378 //Adjust 'Add Many Items' submenu position
379 $submenu_item = array_pop( $GLOBALS['submenu']['edit.php?post_type=' . self::MENU_ITEM_POST_TYPE] );
380 $GLOBALS['submenu']['edit.php?post_type=' . self::MENU_ITEM_POST_TYPE][11] = $submenu_item;
381 ksort( $GLOBALS['submenu']['edit.php?post_type=' . self::MENU_ITEM_POST_TYPE] );
382
383 $this->setup_menu_item_columns();
384
385 wp_register_script( 'nova-menu-checkboxes', plugins_url( 'js/menu-checkboxes.js', __FILE__ ), array( 'jquery' ), $this->version, true );
386 }
387
388
389 /**
390 * Custom Nova Icon CSS
391 */
392 function set_custom_font_icon() {
393 ?>
394 <style type="text/css">
395 #menu-posts-nova_menu_item .wp-menu-image:before {
396 font-family: 'nova-font' !important;
397 content: '\e603' !important;
398 }
399 </style>
400 <?php
401 }
402
403 function current_screen_load() {
404 $screen = get_current_screen();
405 if ( 'edit-nova_menu_item' !== $screen->id ) {
406 return;
407 }
408
409 $this->edit_menu_items_page_load();
410 add_filter( 'admin_notices', array( $this, 'admin_notices' ) );
411 }
412
413 /* Edit Items List */
414
415 function admin_notices() {
416 if ( isset( $_GET['nova_reordered'] ) )
417 printf( '<div class="updated"><p>%s</p></div>', __( 'Menu Items re-ordered.', 'jetpack' ) );
418 }
419
420 function no_title_sorting( $columns ) {
421 if ( isset( $columns['title'] ) )
422 unset( $columns['title'] );
423 return $columns;
424 }
425
426 function setup_menu_item_columns() {
427 add_filter( sprintf( 'manage_edit-%s_sortable_columns', self::MENU_ITEM_POST_TYPE ), array( $this, 'no_title_sorting' ) );
428 add_filter( sprintf( 'manage_%s_posts_columns', self::MENU_ITEM_POST_TYPE ), array( $this, 'menu_item_columns' ) );
429
430 add_action( sprintf( 'manage_%s_posts_custom_column', self::MENU_ITEM_POST_TYPE ), array( $this, 'menu_item_column_callback' ), 10, 2 );
431 }
432
433 function menu_item_columns( $columns ) {
434 unset( $columns['date'], $columns['likes'] );
435
436 $columns['thumbnail'] = __( 'Thumbnail', 'jetpack' );
437 $columns['labels'] = __( 'Labels', 'jetpack' );
438 $columns['price'] = __( 'Price', 'jetpack' );
439 $columns['order'] = __( 'Order', 'jetpack' );
440
441 return $columns;
442 }
443
444 function menu_item_column_callback( $column, $post_id ) {
445 $screen = get_current_screen();
446
447 switch ( $column ) {
448 case 'thumbnail':
449 echo get_the_post_thumbnail( $post_id, array( 50, 50 ) );
450 break;
451 case 'labels' :
452 $this->list_admin_labels( $post_id );
453 break;
454 case 'price' :
455 $this->display_price( $post_id );
456 break;
457 case 'order' :
458 $url = admin_url( $screen->parent_file );
459
460 $up_url = add_query_arg( array(
461 'action' => 'move-item-up',
462 'post_id' => (int) $post_id,
463 ), wp_nonce_url( $url, 'nova_move_item_up_' . $post_id ) );
464
465 $down_url = add_query_arg( array(
466 'action' => 'move-item-down',
467 'post_id' => (int) $post_id,
468 ), wp_nonce_url( $url, 'nova_move_item_down_' . $post_id ) );
469 $menu_item = get_post($post_id);
470 $this->get_menu_by_post_id( $post_id );
471 ?>
472 <input type="hidden" class="menu-order-value" name="nova_order[<?php echo (int) $post_id ?>]" value="<?php echo esc_attr( $menu_item->menu_order ) ?>" />
473 <input type="hidden" class='nova-menu-term' name="nova_menu_term[<?php echo (int) $post_id ?>]" value="<?php echo esc_attr( $this->get_menu_by_post_id( $post_id )->term_id ); ?>">
474
475 <span class="hide-if-js">
476 &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>
477 <br />
478 &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>
479 </span>
480 <?php
481 break;
482 }
483 }
484
485 function get_menu_by_post_id( $post_id = null ) {
486 if ( ! $post_id )
487 return false;
488
489 $terms = get_the_terms( $post_id, self::MENU_TAX );
490
491 if ( ! is_array( $terms ) )
492 return false;
493
494 return array_pop( $terms );
495 }
496
497 /**
498 * Fires on a menu edit page. We might have drag-n-drop reordered
499 */
500 function maybe_reorder_menu_items() {
501 // make sure we clicked our button
502 if ( ! ( isset( $_REQUEST['menu_reorder_submit'] ) && $_REQUEST['menu_reorder_submit'] === __( 'Save New Order', 'jetpack' ) ) )
503 return;
504 ;
505
506 // make sure we have the nonce
507 if ( ! ( isset( $_REQUEST['drag-drop-reorder'] ) && wp_verify_nonce( $_REQUEST['drag-drop-reorder'], 'drag-drop-reorder' ) ) )
508 return;
509
510 $term_pairs = array_map( 'absint', $_REQUEST['nova_menu_term'] );
511 $order_pairs = array_map( 'absint', $_REQUEST['nova_order'] );
512
513 foreach( $order_pairs as $ID => $menu_order ) {
514 $ID = absint( $ID );
515 unset( $order_pairs[$ID] );
516 if ( $ID < 0 )
517 continue;
518
519 $post = get_post( $ID );
520 if ( ! $post )
521 continue;
522
523 // save a write if the order hasn't changed
524 if ( $menu_order != $post->menu_order )
525 wp_update_post( compact( 'ID', 'menu_order' ) );
526
527 // save a write if the term hasn't changed
528 if ( $term_pairs[$ID] != $this->get_menu_by_post_id( $ID )->term_id )
529 wp_set_object_terms( $ID, $term_pairs[$ID], self::MENU_TAX );
530
531 }
532
533 $redirect = add_query_arg( array(
534 'post_type' => self::MENU_ITEM_POST_TYPE,
535 'nova_reordered' => '1'
536 ), admin_url( 'edit.php' ) );
537 wp_safe_redirect( $redirect );
538 exit;
539
540 }
541
542 function edit_menu_items_page_load() {
543 if ( isset( $_GET['action'] ) ) {
544 $this->handle_menu_item_actions();
545 }
546
547 $this->maybe_reorder_menu_items();
548
549 wp_enqueue_script( 'nova-drag-drop', plugins_url( 'js/nova-drag-drop.js', __FILE__ ), array( 'jquery-ui-sortable' ), $this->version, true );
550 wp_localize_script( 'nova-drag-drop', '_novaDragDrop', array(
551 'nonce' => wp_create_nonce( 'drag-drop-reorder' ),
552 'nonceName' => 'drag-drop-reorder',
553 'reorder' => __( 'Save New Order', 'jetpack' ),
554 'reorderName' => 'menu_reorder_submit'
555 ) );
556 add_action( 'the_post', array( $this, 'show_menu_titles_in_menu_item_list' ) );
557 }
558
559 function handle_menu_item_actions() {
560 $action = (string) $_GET['action'];
561
562 switch ( $action ) {
563 case 'move-item-up' :
564 case 'move-item-down' :
565 $reorder = false;
566
567 $post_id = (int) $_GET['post_id'];
568
569 $term = $this->get_menu_item_menu_leaf( $post_id );
570
571 // Get all posts in that term
572 $query = new WP_Query( array(
573 'taxonomy' => self::MENU_TAX,
574 'term' => $term->slug,
575 ) );
576
577 $order = array();
578 foreach ( $query->posts as $post ) {
579 $order[] = $post->ID;
580 }
581
582 if ( 'move-item-up' == $action ) {
583 check_admin_referer( 'nova_move_item_up_' . $post_id );
584
585 $first_post_id = $order[0];
586 if ( $post_id == $first_post_id ) {
587 break;
588 }
589
590 foreach ( $order as $menu_order => $order_post_id ) {
591 if ( $post_id != $order_post_id ) {
592 continue;
593 }
594
595 $swap_post_id = $order[$menu_order - 1];
596 $order[$menu_order - 1] = $post_id;
597 $order[$menu_order] = $swap_post_id;
598
599 $reorder = true;
600 break;
601 }
602 } else {
603 check_admin_referer( 'nova_move_item_down_' . $post_id );
604
605 $last_post_id = end( $order );
606 if ( $post_id == $last_post_id ) {
607 break;
608 }
609
610 foreach ( $order as $menu_order => $order_post_id ) {
611 if ( $post_id != $order_post_id ) {
612 continue;
613 }
614
615 $swap_post_id = $order[$menu_order + 1];
616 $order[$menu_order + 1] = $post_id;
617 $order[$menu_order] = $swap_post_id;
618
619 $reorder = true;
620 }
621 }
622
623 if ( $reorder ) {
624 foreach ( $order as $menu_order => $ID ) {
625 wp_update_post( compact( 'ID', 'menu_order' ) );
626 }
627 }
628
629 break;
630 case 'move-menu-up' :
631 case 'move-menu-down' :
632 $reorder = false;
633
634 $term_id = (int) $_GET['term_id'];
635
636 $terms = $this->get_menus();
637
638 $order = array();
639 foreach ( $terms as $term ) {
640 $order[] = $term->term_id;
641 }
642
643 if ( 'move-menu-up' == $action ) {
644 check_admin_referer( 'nova_move_menu_up_' . $term_id );
645
646 $first_term_id = $order[0];
647 if ( $term_id == $first_term_id ) {
648 break;
649 }
650
651 foreach ( $order as $menu_order => $order_term_id ) {
652 if ( $term_id != $order_term_id ) {
653 continue;
654 }
655
656 $swap_term_id = $order[$menu_order - 1];
657 $order[$menu_order - 1] = $term_id;
658 $order[$menu_order] = $swap_term_id;
659
660 $reorder = true;
661 break;
662 }
663 } else {
664 check_admin_referer( 'nova_move_menu_down_' . $term_id );
665
666 $last_term_id = end( $order );
667 if ( $term_id == $last_term_id ) {
668 break;
669 }
670
671 foreach ( $order as $menu_order => $order_term_id ) {
672 if ( $term_id != $order_term_id ) {
673 continue;
674 }
675
676 $swap_term_id = $order[$menu_order + 1];
677 $order[$menu_order + 1] = $term_id;
678 $order[$menu_order] = $swap_term_id;
679
680 $reorder = true;
681 }
682 }
683
684 if ( $reorder ) {
685 update_option( 'nova_menu_order', $order );
686 }
687
688 break;
689 default :
690 return;
691 }
692
693 $redirect = add_query_arg( array(
694 'post_type' => self::MENU_ITEM_POST_TYPE,
695 'nova_reordered' => '1'
696 ), admin_url( 'edit.php' ) );
697 wp_safe_redirect( $redirect );
698 exit;
699 }
700
701 /*
702 * Add menu title rows to the list table
703 */
704 function show_menu_titles_in_menu_item_list( $post ) {
705 global $wp_list_table;
706
707 static $last_term_id = false;
708
709 $term = $this->get_menu_item_menu_leaf( $post->ID );
710
711 if ( false !== $last_term_id && $last_term_id === $term->term_id )
712 return;
713
714 $last_term_id = $term->term_id;
715
716 $parent_count = 0;
717 $current_term = $term;
718 while ( $current_term->parent ) {
719 $parent_count++;
720 $current_term = get_term( $current_term->parent, self::MENU_TAX );
721 }
722
723 $non_order_column_count = $wp_list_table->get_column_count() - 1;
724
725 $screen = get_current_screen();
726
727 $url = admin_url( $screen->parent_file );
728
729 $up_url = add_query_arg( array(
730 'action' => 'move-menu-up',
731 'term_id' => (int) $term->term_id,
732 ), wp_nonce_url( $url, 'nova_move_menu_up_' . $term->term_id ) );
733
734 $down_url = add_query_arg( array(
735 'action' => 'move-menu-down',
736 'term_id' => (int) $term->term_id,
737 ), wp_nonce_url( $url, 'nova_move_menu_down_' . $term->term_id ) );
738
739 ?>
740 <tr class="no-items menu-label-row" data-term_id="<?php echo esc_attr( $term->term_id ) ?>">
741 <td class="colspanchange" colspan="<?php echo (int) $non_order_column_count; ?>">
742 <h3><?php
743 echo str_repeat( ' &mdash; ', (int) $parent_count );
744
745 if ( ! is_wp_error( $term ) ) {
746 echo esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, self::MENU_TAX, 'display' ) );
747 edit_term_link( __( 'edit', 'jetpack' ), '<span class="edit-nova-section"><span class="dashicon dashicon-edit"></span>', '</span>', $term );
748
749 } else {
750 _e( 'Uncategorized' , 'jetpack' );
751 }
752 ?></h3>
753 </td>
754 <td>
755 <?php if ( ! is_wp_error( $term ) ) { ?>
756 <a class="nova-move-menu-up" title="<?php esc_attr_e( 'Move menu section up', 'jetpack' ); ?>" href="<?php echo esc_url( $up_url ); ?>"><?php esc_html_e( 'UP', 'jetpack' ); ?></a>
757 <br />
758 <a class="nova-move-menu-down" title="<?php esc_attr_e( 'Move menu section down', 'jetpack' ); ?>" href="<?php echo esc_url( $down_url ); ?>"><?php esc_html_e( 'DOWN', 'jetpack' ); ?></a>
759 <?php } ?>
760 </td>
761 </tr>
762 <?php
763 }
764
765 /* Edit Many Items */
766
767 function add_many_new_items_page_load() {
768 if ( 'POST' === strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
769 $this->process_form_request();
770 exit;
771 }
772
773 $this->enqueue_many_items_scripts();
774 }
775
776 function enqueue_many_items_scripts() {
777 wp_enqueue_script( 'nova-many-items', plugins_url( 'js/many-items.js', __FILE__ ), array( 'jquery' ), $this->version, true );
778 }
779
780 function process_form_request() {
781 if ( !isset( $_POST['nova_title'] ) || !is_array( $_POST['nova_title'] ) ) {
782 return;
783 }
784
785 $is_ajax = !empty( $_POST['ajax'] );
786
787 if ( $is_ajax ) {
788 check_ajax_referer( 'nova_many_items' );
789 } else {
790 check_admin_referer( 'nova_many_items' );
791 }
792
793 foreach ( array_keys( $_POST['nova_title'] ) as $key ) :
794 // $_POST is already slashed
795 $post_details = array(
796 'post_status' => 'publish',
797 'post_type' => self::MENU_ITEM_POST_TYPE,
798 'post_content' => $_POST['nova_content'][$key],
799 'post_title' => $_POST['nova_title'][$key],
800 'tax_input' => array(
801 self::MENU_ITEM_LABEL_TAX => $_POST['nova_labels'][$key],
802 self::MENU_TAX => $_POST['nova_menu_tax'],
803 ),
804 );
805
806 $post_id = wp_insert_post( $post_details );
807 if ( !$post_id || is_wp_error( $post_id ) ) {
808 continue;
809 }
810
811 $this->set_price( $post_id, isset( $_POST['nova_price'][$key] ) ? stripslashes( $_POST['nova_price'][$key] ) : '' );
812
813 if ( $is_ajax ) :
814 $post = get_post( $post_id );
815 $GLOBALS['post'] = $post;
816 setup_postdata( $post );
817
818 ?>
819 <td><?php the_title(); ?></td>
820 <td class="nova-price"><?php $this->display_price(); ?></td>
821 <td><?php $this->list_labels( $post_id ); ?></td>
822 <td><?php the_content(); ?></td>
823 <?php
824 endif;
825
826 endforeach;
827
828 if ( $is_ajax ) {
829 exit;
830 }
831
832 wp_safe_redirect( admin_url( 'edit.php?post_type=' . self::MENU_ITEM_POST_TYPE ) );
833 exit;
834 }
835
836 function add_many_new_items_page() {
837 ?>
838 <div class="wrap">
839 <h2><?php esc_html_e( 'Add Many Items', 'jetpack' ); ?></h2>
840
841 <p><?php _e( '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' ); ?></p>
842
843 <form method="post" action="" enctype="multipart/form-data">
844 <p><h3><?php esc_html_e( 'Add to section:', 'jetpack' ); ?> <?php wp_dropdown_categories( array(
845 'id' => 'nova-menu-tax',
846 'name' => 'nova_menu_tax',
847 'taxonomy' => self::MENU_TAX,
848 'hide_empty' => false,
849 'hierarchical' => true,
850 ) ); ?></h3></p>
851
852 <table class="many-items-table wp-list-table widefat">
853 <thead>
854 <tr>
855 <th scope="col"><?php esc_html_e( 'Name', 'jetpack' ); ?></th>
856 <th scope="col" class="nova-price"><?php esc_html_e( 'Price', 'jetpack' ); ?></th>
857 <th scope="col"><?php _e( 'Labels: <small>spicy, favorite, etc. <em>Separate Labels with commas</em></small>', 'jetpack' ); ?></th>
858 <th scope="col"><?php esc_html_e( 'Description', 'jetpack' ); ?></th>
859 </tr>
860 </thead>
861 <tbody>
862 <tr>
863 <td><input type="text" name="nova_title[]" aria-required="true" /></td>
864 <td class="nova-price"><input type="text" name="nova_price[]" /></td>
865 <td><input type="text" name="nova_labels[]" /></td>
866 <td><textarea name="nova_content[]" cols="20" rows="1"></textarea>
867 </tr>
868 </tbody>
869 <tbody>
870 <tr>
871 <td><input type="text" name="nova_title[]" aria-required="true" /></td>
872 <td class="nova-price"><input type="text" name="nova_price[]" /></td>
873 <td><input type="text" name="nova_labels[]" /></td>
874 <td><textarea name="nova_content[]" cols="20" rows="1"></textarea>
875 </tr>
876 </tbody>
877 <tfoot>
878 <tr>
879 <th><a class="button button-secondary nova-new-row"><span class="dashicon dashicon-plus"></span> <?php esc_html_e( 'New Row' , 'jetpack' ); ?></a></th>
880 <th class="nova-price"></th>
881 <th></th>
882 <th></th>
883 </tr>
884 </tfoot>
885 </table>
886
887 <p class="submit">
888 <input type="submit" class="button-primary" value="<?php esc_attr_e( 'Add These New Menu Items', 'jetpack' ); ?>" />
889 <?php wp_nonce_field( 'nova_many_items' ); ?>
890 </p>
891 </form>
892 </div>
893 <?php
894 }
895
896 /* Edit One Item */
897
898 function register_menu_item_meta_boxes() {
899 wp_enqueue_script( 'nova-menu-checkboxes' );
900
901 add_meta_box( 'menu_item_price', __( 'Price', 'jetpack' ), array( $this, 'menu_item_price_meta_box' ), null, 'side', 'high' );
902 }
903
904 function menu_item_price_meta_box( $post, $meta_box ) {
905 $price = $this->get_price( $post->ID );
906 ?>
907 <label for="nova-price-<?php echo (int) $post->ID; ?>" class="screen-reader-text"><?php esc_html_e( 'Price', 'jetpack' ); ?></label>
908 <input type="text" id="nova-price-<?php echo (int) $post->ID; ?>" class="widefat" name="nova_price[<?php echo (int) $post->ID; ?>]" value="<?php echo esc_attr( $price ); ?>" />
909 <?php
910 }
911
912 function add_post_meta( $post_id ) {
913 if ( !isset( $_POST['nova_price'][$post_id] ) ) {
914 return;
915 }
916
917 $this->set_price( $post_id, stripslashes( $_POST['nova_price'][$post_id] ) );
918 }
919
920 /* Data */
921
922 function get_menus( $args = array() ) {
923 $args = wp_parse_args( $args, array(
924 'hide_empty' => false,
925 ) );
926
927 $terms = get_terms( self::MENU_TAX, $args );
928 if ( !$terms || is_wp_error( $terms ) ) {
929 return array();
930 }
931
932 $terms_by_id = array();
933 foreach ( $terms as $term ) {
934 $terms_by_id["{$term->term_id}"] = $term;
935 }
936
937 $term_order = get_option( 'nova_menu_order', array() );
938
939 $return = array();
940 foreach ( $term_order as $term_id ) {
941 if ( isset( $terms_by_id["$term_id"] ) ) {
942 $return[] = $terms_by_id["$term_id"];
943 unset( $terms_by_id["$term_id"] );
944 }
945 }
946
947 foreach ( $terms_by_id as $term_id => $term ) {
948 $return[] = $term;
949 }
950
951 return $return;
952 }
953
954 function get_menu_item_menu_leaf( $post_id ) {
955 // Get first menu taxonomy "leaf"
956 $term_ids = wp_get_object_terms( $post_id, self::MENU_TAX, array( 'fields' => 'ids' ) );
957
958 foreach ( $term_ids as $term_id ) {
959 $children = get_term_children( $term_id, self::MENU_TAX );
960 if ( ! $children ) {
961 break;
962 }
963 }
964
965 return get_term( $term_id, self::MENU_TAX );
966 }
967
968 function list_labels( $post_id = 0 ) {
969 $post = get_post( $post_id );
970 echo get_the_term_list( $post->ID, self::MENU_ITEM_LABEL_TAX, '', _x( ', ', 'Nova label separator', 'jetpack' ), '' );
971 }
972
973 function list_admin_labels( $post_id = 0 ) {
974 $post = get_post( $post_id );
975 $labels = get_the_terms( $post->ID, self::MENU_ITEM_LABEL_TAX );
976 if ( !empty( $labels ) ) {
977 $out = array();
978 foreach ( $labels as $label ) {
979 $out[] = sprintf( '<a href="%s">%s</a>',
980 esc_url( add_query_arg( array(
981 'post_type' => self::MENU_ITEM_POST_TYPE,
982 'taxonomy' => self::MENU_ITEM_LABEL_TAX,
983 'term' => $label->slug
984 ), 'edit.php' ) ),
985 esc_html( sanitize_term_field( 'name', $label->name, $label->term_id, self::MENU_ITEM_LABEL_TAX, 'display' ) )
986 );
987 }
988
989 echo join( _x( ', ', 'Nova label separator', 'jetpack' ), $out );
990 } else {
991 esc_html_e( 'No Labels', 'jetpack' );
992 }
993 }
994
995 function set_price( $post_id = 0, $price = '' ) {
996 $post = get_post( $post_id );
997
998 return update_post_meta( $post->ID, 'nova_price', $price );
999 }
1000
1001 function get_price( $post_id = 0 ) {
1002 $post = get_post( $post_id );
1003
1004 return get_post_meta( $post->ID, 'nova_price', true );
1005 }
1006
1007 function display_price( $post_id = 0 ) {
1008 echo esc_html( $this->get_price( $post_id ) );
1009 }
1010
1011 /* Menu Item Loop Markup */
1012
1013 /* Does not support nested loops */
1014
1015 function get_menu_item_loop_markup( $field = null ) {
1016 return $this->menu_item_loop_markup;
1017 }
1018
1019 /**
1020 * Sets up the loop markup.
1021 * Attached to the 'template_include' *filter*,
1022 * which fires only during a real blog view (not in admin, feeds, etc.)
1023 *
1024 * @param string Template File
1025 * @return string Template File. VERY Important.
1026 */
1027 function setup_menu_item_loop_markup__in_filter( $template ) {
1028 add_action( 'loop_start', array( $this, 'start_menu_item_loop' ) );
1029
1030 return $template;
1031 }
1032
1033 /**
1034 * If the Query is a Menu Item Query, start outputing the Menu Item Loop Marku
1035 * Attached to the 'loop_start' action.
1036 *
1037 * @param WP_Query
1038 */
1039 function start_menu_item_loop( $query ) {
1040 if ( !$this->is_menu_item_query( $query ) ) {
1041 return;
1042 }
1043
1044 $this->menu_item_loop_last_term_id = false;
1045 $this->menu_item_loop_current_term = false;
1046
1047 add_action( 'the_post', array( $this, 'menu_item_loop_each_post' ) );
1048 add_action( 'loop_end', array( $this, 'stop_menu_item_loop' ) );
1049 }
1050
1051 /**
1052 * Outputs the Menu Item Loop Marku
1053 * Attached to the 'the_post' action.
1054 *
1055 * @param WP_Post
1056 */
1057 function menu_item_loop_each_post( $post ) {
1058 $this->menu_item_loop_current_term = $this->get_menu_item_menu_leaf( $post->ID );
1059
1060 if ( false === $this->menu_item_loop_last_term_id ) {
1061 // We're at the very beginning of the loop
1062
1063 $this->menu_item_loop_open_element( 'menu' ); // Start a new menu section
1064 $this->menu_item_loop_header(); // Output the menu's header
1065 } elseif ( $this->menu_item_loop_last_term_id != $this->menu_item_loop_current_term->term_id ) {
1066 // We're not at the very beginning but still need to start a new menu section. End the previous menu section first.
1067
1068 $this->menu_item_loop_close_element( 'menu' ); // End the previous menu section
1069 $this->menu_item_loop_open_element( 'menu' ); // Start a new menu section
1070 $this->menu_item_loop_header(); // Output the menu's header
1071 }
1072
1073 $this->menu_item_loop_last_term_id = $this->menu_item_loop_current_term->term_id;
1074 }
1075
1076 /**
1077 * If the Query is a Menu Item Query, stop outputing the Menu Item Loop Marku
1078 * Attached to the 'loop_end' action.
1079 *
1080 * @param WP_Query
1081 */
1082 function stop_menu_item_loop( $query ) {
1083 if ( !$this->is_menu_item_query( $query ) ) {
1084 return;
1085 }
1086
1087 remove_action( 'the_post', array( $this, 'menu_item_loop_each_post' ) );
1088 remove_action( 'loop_start', array( $this, 'start_menu_item_loop' ) );
1089 remove_action( 'loop_end', array( $this, 'stop_menu_item_loop' ) );
1090
1091 $this->menu_item_loop_close_element( 'menu' ); // End the last menu section
1092 }
1093
1094 /**
1095 * Outputs the Menu Group Header
1096 */
1097 function menu_item_loop_header() {
1098 $this->menu_item_loop_open_element( 'menu_header' );
1099 $this->menu_item_loop_open_element( 'menu_title' );
1100 echo esc_html( $this->menu_item_loop_current_term->name ); // @todo tax filter
1101 $this->menu_item_loop_close_element( 'menu_title' );
1102 if ( $this->menu_item_loop_current_term->description ) :
1103 $this->menu_item_loop_open_element( 'menu_description' );
1104 echo esc_html( $this->menu_item_loop_current_term->description ); // @todo kses, tax filter
1105 $this->menu_item_loop_close_element( 'menu_description' );
1106 endif;
1107 $this->menu_item_loop_close_element( 'menu_header' );
1108 }
1109
1110 /**
1111 * Outputs a Menu Item Markup element opening tag
1112 *
1113 * @param string $field - Menu Item Markup settings field
1114 */
1115 function menu_item_loop_open_element( $field ) {
1116 $markup = $this->get_menu_item_loop_markup();
1117 echo '<' . tag_escape( $markup["{$field}_tag"] ) . $this->menu_item_loop_class( $markup["{$field}_class"] ) . ">\n";
1118 }
1119
1120 /**
1121 * Outputs a Menu Item Markup element closing tag
1122 *
1123 * @param string $field - Menu Item Markup settings field
1124 */
1125 function menu_item_loop_close_element( $field ) {
1126 $markup = $this->get_menu_item_loop_markup();
1127 echo '</' . tag_escape( $markup["{$field}_tag"] ) . ">\n";
1128 }
1129
1130 /**
1131 * Returns a Menu Item Markup element's class attribute
1132 *
1133 * @param string $class
1134 * @return string HTML class attribute with leading whitespace
1135 */
1136 function menu_item_loop_class( $class ) {
1137 if ( !$class ) {
1138 return '';
1139 }
1140
1141 return ' class="' . esc_attr( $class ) . '"';
1142 }
1143 }
1144
1145 add_action( 'init', array( 'Nova_Restaurant', 'init' ) );
1146