| 1 |
<?php |
| 2 |
|
| 3 |
namespace Boxzilla\Admin; |
| 4 |
|
| 5 |
if (! defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Menu |
| 10 |
{ |
| 11 |
public function init(): void |
| 12 |
{ |
| 13 |
add_action('admin_head-nav-menus.php', [ $this, 'add_nav_menu_meta_boxes' ]); |
| 14 |
|
| 15 |
// Include custom items to customizer nav menu settings. |
| 16 |
add_filter('customize_nav_menu_available_item_types', [ $this, 'register_customize_nav_menu_item_types' ]); |
| 17 |
add_filter('customize_nav_menu_available_items', [ $this, 'register_customize_nav_menu_items' ], 10, 4); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Add custom nav meta box. |
| 22 |
* |
| 23 |
* Adapted from http://www.johnmorrisonline.com/how-to-add-a-fully-functional-custom-meta-box-to-wordpress-navigation-menus/. |
| 24 |
*/ |
| 25 |
public function add_nav_menu_meta_boxes() |
| 26 |
{ |
| 27 |
add_meta_box('boxzilla_nav_link', esc_html__('Boxzilla Pop-ups', 'boxzilla'), [ $this, 'nav_menu_links' ], 'nav-menus', 'side', 'low'); |
| 28 |
} |
| 29 |
|
| 30 |
private function get_boxes() |
| 31 |
{ |
| 32 |
$q = new \WP_Query(); |
| 33 |
$posts = $q->query( |
| 34 |
[ |
| 35 |
'post_type' => 'boxzilla-box', |
| 36 |
'post_status' => 'publish', |
| 37 |
'posts_per_page' => -1, |
| 38 |
'ignore_sticky_posts' => true, |
| 39 |
'no_found_rows' => true, |
| 40 |
] |
| 41 |
); |
| 42 |
return $posts; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Output menu links. |
| 47 |
*/ |
| 48 |
public function nav_menu_links() |
| 49 |
{ |
| 50 |
$posts = $this->get_boxes(); |
| 51 |
if (count($posts) === 0) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
?> |
| 56 |
<div id="posttype-boxzilla-boxes" class="posttypediv"> |
| 57 |
<div id="tabs-panel-boxzilla-boxes" class="tabs-panel tabs-panel-active"> |
| 58 |
<ul id="boxzilla-boxes-checklist" class="categorychecklist form-no-clear"> |
| 59 |
<?php |
| 60 |
$i = -1; |
| 61 |
foreach ($posts as $key => $post) : |
| 62 |
?> |
| 63 |
<li> |
| 64 |
<label class="menu-item-title"> |
| 65 |
<input type="checkbox" class="menu-item-checkbox" name="menu-item[<?php echo (int) $i; ?>][menu-item-object-id]" value="<?php echo (int) $i; ?>" /> <?php echo esc_html($post->post_title); ?> |
| 66 |
</label> |
| 67 |
<input type="hidden" class="menu-item-type" name="menu-item[<?php echo (int) $i; ?>][menu-item-type]" value="custom" /> |
| 68 |
<input type="hidden" class="menu-item-title" name="menu-item[<?php echo (int) $i; ?>][menu-item-title]" value="<?php echo esc_attr($post->post_title); ?>" /> |
| 69 |
<input type="hidden" class="menu-item-url" name="menu-item[<?php echo (int) $i; ?>][menu-item-url]" value="<?php echo esc_attr('#boxzilla-' . $post->ID); ?>" /> |
| 70 |
<input type="hidden" class="menu-item-classes" name="menu-item[<?php echo (int) $i; ?>][menu-item-classes]" /> |
| 71 |
</li> |
| 72 |
<?php |
| 73 |
$i--; |
| 74 |
endforeach; |
| 75 |
?> |
| 76 |
</ul> |
| 77 |
</div> |
| 78 |
<p class="button-controls"> |
| 79 |
<span class="list-controls"> |
| 80 |
</span> |
| 81 |
<span class="add-to-menu"> |
| 82 |
<button type="submit" class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e('Add to menu', 'boxzilla'); ?>" name="add-post-type-menu-item" id="submit-posttype-boxzilla-boxes"><?php esc_html_e('Add to menu', 'boxzilla'); ?></button> |
| 83 |
<span class="spinner"></span> |
| 84 |
</span> |
| 85 |
</p> |
| 86 |
</div> |
| 87 |
<?php |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Register customize new nav menu item types. |
| 92 |
* |
| 93 |
* @since 3.1.0 |
| 94 |
* @param array $item_types Menu item types. |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
public function register_customize_nav_menu_item_types($item_types) |
| 98 |
{ |
| 99 |
$item_types[] = [ |
| 100 |
'title' => esc_html__('Boxzilla Pop-ups', 'boxzilla'), |
| 101 |
'type_label' => esc_html__('Boxzilla Pop-ups', 'boxzilla'), |
| 102 |
'type' => 'boxzilla_nav', |
| 103 |
'object' => 'boxzilla_box', |
| 104 |
]; |
| 105 |
|
| 106 |
return $item_types; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Register account endpoints to customize nav menu items. |
| 111 |
* |
| 112 |
* @since 3.1.0 |
| 113 |
* @param array $items List of nav menu items. |
| 114 |
* @param string $type Nav menu type. |
| 115 |
* @param string $object Nav menu object. |
| 116 |
* @param integer $page Page number. |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
public function register_customize_nav_menu_items($items = [], $type = '', $object = '', $page = 0) |
| 120 |
{ |
| 121 |
if ('boxzilla_box' !== $object) { |
| 122 |
return $items; |
| 123 |
} |
| 124 |
|
| 125 |
// Don't allow pagination since all items are loaded at once. |
| 126 |
if (0 < $page) { |
| 127 |
return $items; |
| 128 |
} |
| 129 |
|
| 130 |
$boxes = $this->get_boxes(); |
| 131 |
foreach ($boxes as $i => $post) { |
| 132 |
$items[] = [ |
| 133 |
'id' => $i, |
| 134 |
'title' => $post->post_title, |
| 135 |
'type_label' => esc_html__('Custom Link', 'boxzilla'), |
| 136 |
'url' => "#boxzilla-{$post->ID}", |
| 137 |
]; |
| 138 |
} |
| 139 |
|
| 140 |
return $items; |
| 141 |
} |
| 142 |
} |
| 143 |
|