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