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