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