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