'section',
'menu_class' => 'menu-items',
'menu_header_tag' => 'header',
'menu_header_class' => 'menu-group-header',
'menu_title_tag' => 'h1',
'menu_title_class' => 'menu-group-title',
'menu_description_tag' => 'div',
'menu_description_class' => 'menu-group-description',
) );
}
*/
/* @todo
Bulk/Quick edit response of Menu Item rows is broken.
Drag and Drop reordering.
*/
class Nova_Restaurant {
const MENU_ITEM_POST_TYPE = 'nova_menu_item';
const MENU_ITEM_LABEL_TAX = 'nova_menu_item_label';
const MENU_TAX = 'nova_menu';
public $version = '0.1';
protected $default_menu_item_loop_markup = array(
'menu_tag' => 'section',
'menu_class' => 'menu-items',
'menu_header_tag' => 'header',
'menu_header_class' => 'menu-group-header',
'menu_title_tag' => 'h1',
'menu_title_class' => 'menu-group-title',
'menu_description_tag' => 'div',
'menu_description_class' => 'menu-group-description',
);
protected $menu_item_loop_markup = array();
protected $menu_item_loop_last_term_id = false;
protected $menu_item_loop_current_term = false;
static function init( $menu_item_loop_markup = array() ) {
static $instance = false;
if ( !$instance ) {
$instance = new Nova_Restaurant;
}
if ( $menu_item_loop_markup ) {
$instance->menu_item_loop_markup = wp_parse_args( $menu_item_loop_markup, $instance->default_menu_item_loop_markup );
}
return $instance;
}
function __construct() {
if ( ! $this->site_supports_nova() )
return;
$this->register_taxonomies();
$this->register_post_types();
add_action( 'admin_menu', array( $this, 'add_admin_menus' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_nova_styles' ) );
add_action( 'admin_head', array( $this, 'set_custom_font_icon' ) );
// Enable Omnisearch for Menu Items.
if ( class_exists( 'Jetpack_Omnisearch_Posts' ) )
new Jetpack_Omnisearch_Posts( self::MENU_ITEM_POST_TYPE );
// Always sort menu items correctly
add_action( 'parse_query', array( $this, 'sort_menu_item_queries_by_menu_order' ) );
add_filter( 'posts_results', array( $this, 'sort_menu_item_queries_by_menu_taxonomy' ), 10, 2 );
add_action( 'wp_insert_post', array( $this, 'add_post_meta' ) );
$this->menu_item_loop_markup = $this->default_menu_item_loop_markup;
// Only output our Menu Item Loop Markup on a real blog view. Not feeds, XML-RPC, admin, etc.
add_filter( 'template_include', array( $this, 'setup_menu_item_loop_markup__in_filter' ) );
add_filter( 'enter_title_here', array( $this, 'change_default_title' ) );
add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) );
add_filter( 'dashboard_glance_items', array( $this, 'add_to_dashboard' ) );
}
/**
* Should this Custom Post Type be made available?
*/
function site_supports_nova() {
// If we're on WordPress.com, and it has the menu site vertical.
if ( function_exists( 'site_vertical' ) && 'nova_menu' == site_vertical() )
return true;
// Else, if the current theme requests it.
if ( current_theme_supports( self::MENU_ITEM_POST_TYPE ) )
return true;
// Otherwise, say no unless something wants to filter us to say yes.
/**
* Allow something else to hook in and enable this CPT.
*
* @module custom-content-types
*
* @since 2.6.0
*
* @param bool false Whether or not to enable this CPT.
* @param string $var The slug for this CPT.
*/
return (bool) apply_filters( 'jetpack_enable_cpt', false, self::MENU_ITEM_POST_TYPE );
}
/* Setup */
/**
* Register Taxonomies and Post Type
*/
function register_taxonomies() {
if ( ! taxonomy_exists( self::MENU_ITEM_LABEL_TAX ) ) {
register_taxonomy( self::MENU_ITEM_LABEL_TAX, self::MENU_ITEM_POST_TYPE, array(
'labels' => array(
/* translators: this is about a food menu */
'name' => __( 'Menu Item Labels', 'jetpack' ),
/* translators: this is about a food menu */
'singular_name' => __( 'Menu Item Label', 'jetpack' ),
/* translators: this is about a food menu */
'search_items' => __( 'Search Menu Item Labels', 'jetpack' ),
'popular_items' => __( 'Popular Labels', 'jetpack' ),
/* translators: this is about a food menu */
'all_items' => __( 'All Menu Item Labels', 'jetpack' ),
/* translators: this is about a food menu */
'edit_item' => __( 'Edit Menu Item Label', 'jetpack' ),
/* translators: this is about a food menu */
'view_item' => __( 'View Menu Item Label', 'jetpack' ),
/* translators: this is about a food menu */
'update_item' => __( 'Update Menu Item Label', 'jetpack' ),
/* translators: this is about a food menu */
'add_new_item' => __( 'Add New Menu Item Label', 'jetpack' ),
/* translators: this is about a food menu */
'new_item_name' => __( 'New Menu Item Label Name', 'jetpack' ),
'separate_items_with_commas' => __( 'For example, spicy, favorite, etc. Separate Labels with commas', 'jetpack' ),
'add_or_remove_items' => __( 'Add or remove Labels', 'jetpack' ),
'choose_from_most_used' => __( 'Choose from the most used Labels', 'jetpack' ),
'items_list_navigation' => __( 'Menu item label list navigation', 'jetpack' ),
'items_list' => __( 'Menu item labels list', 'jetpack' ),
),
'no_tagcloud' => __( 'No Labels found', 'jetpack' ),
'hierarchical' => false,
) );
}
if ( ! taxonomy_exists( self::MENU_TAX ) ) {
register_taxonomy( self::MENU_TAX, self::MENU_ITEM_POST_TYPE, array(
'labels' => array(
/* translators: this is about a food menu */
'name' => __( 'Menu Sections', 'jetpack' ),
/* translators: this is about a food menu */
'singular_name' => __( 'Menu Section', 'jetpack' ),
/* translators: this is about a food menu */
'search_items' => __( 'Search Menu Sections', 'jetpack' ),
/* translators: this is about a food menu */
'all_items' => __( 'All Menu Sections', 'jetpack' ),
/* translators: this is about a food menu */
'parent_item' => __( 'Parent Menu Section', 'jetpack' ),
/* translators: this is about a food menu */
'parent_item_colon' => __( 'Parent Menu Section:', 'jetpack' ),
/* translators: this is about a food menu */
'edit_item' => __( 'Edit Menu Section', 'jetpack' ),
/* translators: this is about a food menu */
'view_item' => __( 'View Menu Section', 'jetpack' ),
/* translators: this is about a food menu */
'update_item' => __( 'Update Menu Section', 'jetpack' ),
/* translators: this is about a food menu */
'add_new_item' => __( 'Add New Menu Section', 'jetpack' ),
/* translators: this is about a food menu */
'new_item_name' => __( 'New Menu Sections Name', 'jetpack' ),
'items_list_navigation' => __( 'Menu section list navigation', 'jetpack' ),
'items_list' => __( 'Menu section list', 'jetpack' ),
),
'rewrite' => array(
'slug' => 'menu',
'with_front' => false,
'hierarchical' => true,
),
'hierarchical' => true,
'show_tagcloud' => false,
'query_var' => 'menu',
) );
}
}
function register_post_types() {
if ( post_type_exists( self::MENU_ITEM_POST_TYPE ) ) {
return;
}
register_post_type( self::MENU_ITEM_POST_TYPE, array(
'description' => __( "Items on your restaurant's menu", 'jetpack' ),
'labels' => array(
/* translators: this is about a food menu */
'name' => __( 'Menu Items', 'jetpack' ),
/* translators: this is about a food menu */
'singular_name' => __( 'Menu Item', 'jetpack' ),
/* translators: this is about a food menu */
'menu_name' => __( 'Food Menus', 'jetpack' ),
/* translators: this is about a food menu */
'all_items' => __( 'Menu Items', 'jetpack' ),
/* translators: this is about a food menu */
'add_new' => __( 'Add One Item', 'jetpack' ),
/* translators: this is about a food menu */
'add_new_item' => __( 'Add Menu Item', 'jetpack' ),
/* translators: this is about a food menu */
'edit_item' => __( 'Edit Menu Item', 'jetpack' ),
/* translators: this is about a food menu */
'new_item' => __( 'New Menu Item', 'jetpack' ),
/* translators: this is about a food menu */
'view_item' => __( 'View Menu Item', 'jetpack' ),
/* translators: this is about a food menu */
'search_items' => __( 'Search Menu Items', 'jetpack' ),
/* translators: this is about a food menu */
'not_found' => __( 'No Menu Items found', 'jetpack' ),
/* translators: this is about a food menu */
'not_found_in_trash' => __( 'No Menu Items found in Trash', 'jetpack' ),
'filter_items_list' => __( 'Filter menu items list', 'jetpack' ),
'items_list_navigation' => __( 'Menu item list navigation', 'jetpack' ),
'items_list' => __( 'Menu items list', 'jetpack' ),
),
'supports' => array(
'title',
'editor',
'thumbnail',
'excerpt',
),
'rewrite' => array(
'slug' => 'item',
'with_front' => false,
'feeds' => false,
'pages' => false,
),
'register_meta_box_cb' => array( $this, 'register_menu_item_meta_boxes' ),
'public' => true,
'show_ui' => true, // set to false to replace with custom UI
'menu_position' => 20, // below Pages
'capability_type' => 'page',
'map_meta_cap' => true,
'has_archive' => false,
'query_var' => 'item',
) );
}
/**
* Update messages for the Menu Item admin.
*/
function updated_messages( $messages ) {
global $post;
$messages[self::MENU_ITEM_POST_TYPE] = array(
0 => '', // Unused. Messages start at index 1.
/* translators: this is about a food menu */
1 => sprintf( __( 'Menu item updated. View item', 'jetpack' ), esc_url( get_permalink( $post->ID ) ) ),
2 => esc_html__( 'Custom field updated.', 'jetpack' ),
3 => esc_html__( 'Custom field deleted.', 'jetpack' ),
/* translators: this is about a food menu */
4 => esc_html__( 'Menu item updated.', 'jetpack' ),
/* translators: %s: date and time of the revision */
5 => isset( $_GET['revision'] ) ? sprintf( esc_html__( 'Menu item restored to revision from %s', 'jetpack' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
/* translators: this is about a food menu */
6 => sprintf( __( 'Menu item published. View item', 'jetpack' ), esc_url( get_permalink( $post->ID ) ) ),
/* translators: this is about a food menu */
7 => esc_html__( 'Menu item saved.', 'jetpack' ),
/* translators: this is about a food menu */
8 => sprintf( __( 'Menu item submitted. Preview item', 'jetpack' ), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) ),
/* translators: this is about a food menu */
9 => sprintf( __( 'Menu item scheduled for: %1$s. Preview item', 'jetpack' ),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i', 'jetpack' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post->ID) ) ),
/* translators: this is about a food menu */
10 => sprintf( __( 'Menu item draft updated. Preview item', 'jetpack' ), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) ),
);
return $messages;
}
/**
* Nova Styles and Scripts
*/
function enqueue_nova_styles( $hook ) {
global $post_type;
$pages = array( 'edit.php', 'post.php', 'post-new.php' );
if ( in_array( $hook, $pages ) && $post_type == self::MENU_ITEM_POST_TYPE ) {
wp_enqueue_style( 'nova-style', plugins_url( 'css/nova.css', __FILE__ ), array(), $this->version );
}
wp_enqueue_style( 'nova-font', plugins_url( 'css/nova-font.css', __FILE__ ), array(), $this->version );
}
/**
* Change ‘Enter Title Here’ text for the Menu Item.
*/
function change_default_title( $title ) {
$screen = get_current_screen();
if ( self::MENU_ITEM_POST_TYPE == $screen->post_type )
/* translators: this is about a food menu */
$title = esc_html__( "Enter the menu item's name here", 'jetpack' );
return $title;
}
/**
* Add to Dashboard At A Glance
*/
function add_to_dashboard() {
$number_menu_items = wp_count_posts( self::MENU_ITEM_POST_TYPE );
if ( current_user_can( 'administrator' ) ) {
$number_menu_items_published = sprintf( '%2$s',
esc_url( get_admin_url( get_current_blog_id(), 'edit.php?post_type=' . self::MENU_ITEM_POST_TYPE ) ),
sprintf( _n( '%1$d Food Menu Item', '%1$d Food Menu Items', intval( $number_menu_items->publish ), 'jetpack' ), number_format_i18n( $number_menu_items->publish ) )
);
}
else {
$number_menu_items_published = sprintf( '%1$s',
sprintf( _n( '%1$d Food Menu Item', '%1$d Food Menu Items', intval( $number_menu_items->publish ), 'jetpack' ), number_format_i18n( $number_menu_items->publish ) )
);
}
echo '
' . $number_menu_items_published . '
';
}
/**
* Query
*/
function is_menu_item_query( $query ) {
if (
( isset( $query->query_vars['taxonomy'] ) && self::MENU_TAX == $query->query_vars['taxonomy'] )
||
( isset( $query->query_vars['post_type'] ) && self::MENU_ITEM_POST_TYPE == $query->query_vars['post_type'] )
) {
return true;
}
return false;
}
function sort_menu_item_queries_by_menu_order( $query ) {
if ( ! $this->is_menu_item_query( $query ) ) {
return;
}
$query->query_vars['orderby'] = 'menu_order';
$query->query_vars['order'] = 'ASC';
// For now, just turn off paging so we can sort by taxonmy later
// 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)
$query->query_vars['posts_per_page'] = -1;
}
function sort_menu_item_queries_by_menu_taxonomy( $posts, $query ) {
if ( !$posts ) {
return $posts;
}
if ( !$this->is_menu_item_query( $query ) ) {
return $posts;
}
$grouped_by_term = array();
foreach ( $posts as $post ) {
$term = $this->get_menu_item_menu_leaf( $post->ID );
if ( !$term || is_wp_error( $term ) ) {
$term_id = 0;
} else {
$term_id = $term->term_id;
}
if ( !isset( $grouped_by_term["$term_id"] ) ) {
$grouped_by_term["$term_id"] = array();
}
$grouped_by_term["$term_id"][] = $post;
}
$term_order = get_option( 'nova_menu_order', array() );
$return = array();
foreach ( $term_order as $term_id ) {
if ( isset( $grouped_by_term["$term_id"] ) ) {
$return = array_merge( $return, $grouped_by_term["$term_id"] );
unset( $grouped_by_term["$term_id"] );
}
}
foreach ( $grouped_by_term as $term_id => $posts ) {
$return = array_merge( $return, $posts );
}
return $return;
}
/**
* Add Many Items
*/
function add_admin_menus() {
$hook = add_submenu_page(
'edit.php?post_type=' . self::MENU_ITEM_POST_TYPE,
__( 'Add Many Items', 'jetpack' ),
__( 'Add Many Items', 'jetpack' ),
'edit_pages',
'add_many_nova_items',
array( $this, 'add_many_new_items_page' )
);
add_action( "load-$hook", array( $this, 'add_many_new_items_page_load' ) );
add_action( 'current_screen', array( $this, 'current_screen_load' ) );
//Adjust 'Add Many Items' submenu position
if ( isset( $GLOBALS['submenu']['edit.php?post_type=' . self::MENU_ITEM_POST_TYPE] ) ) {
$submenu_item = array_pop( $GLOBALS['submenu']['edit.php?post_type=' . self::MENU_ITEM_POST_TYPE] );
$GLOBALS['submenu']['edit.php?post_type=' . self::MENU_ITEM_POST_TYPE][11] = $submenu_item;
ksort( $GLOBALS['submenu']['edit.php?post_type=' . self::MENU_ITEM_POST_TYPE] );
}
$this->setup_menu_item_columns();
wp_register_script( 'nova-menu-checkboxes', plugins_url( 'js/menu-checkboxes.js', __FILE__ ), array( 'jquery' ), $this->version, true );
}
/**
* Custom Nova Icon CSS
*/
function set_custom_font_icon() {
?>
id ) {
return;
}
$this->edit_menu_items_page_load();
add_filter( 'admin_notices', array( $this, 'admin_notices' ) );
}
/* Edit Items List */
function admin_notices() {
if ( isset( $_GET['nova_reordered'] ) )
/* translators: this is about a food menu */
printf( '