| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpinupWp; |
| 4 |
|
| 5 |
class AdminBar { |
| 6 |
|
| 7 |
/** |
| 8 |
* @var array |
| 9 |
*/ |
| 10 |
private $items = array(); |
| 11 |
|
| 12 |
/** |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public $url; |
| 16 |
|
| 17 |
/** |
| 18 |
* AdminBar constructor. |
| 19 |
* |
| 20 |
* @param string $url |
| 21 |
*/ |
| 22 |
public function __construct( $url ) { |
| 23 |
$this->url = $url; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Init |
| 28 |
*/ |
| 29 |
public function init() { |
| 30 |
add_action( 'admin_bar_menu', array( $this, 'render' ), 100 ); |
| 31 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Enqueue admin scripts. |
| 36 |
*/ |
| 37 |
public function enqueue_scripts() { |
| 38 |
wp_enqueue_style( 'spinupwp-admin', $this->url . 'assets/css/admin.css', array(), '1.0' ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Render the admin bar menu. |
| 43 |
* |
| 44 |
* @param $wp_admin_bar |
| 45 |
*/ |
| 46 |
public function render( $wp_admin_bar ) { |
| 47 |
if ( empty( $this->items ) ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! current_user_can( apply_filters( 'spinupwp_purge_cache_capability', 'manage_options' ) ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$wp_admin_bar->add_node( array( |
| 56 |
'id' => 'spinupwp', |
| 57 |
'title' => apply_filters( 'spinupwp_admin_bar_title', __( 'Cache', 'spinupwp' ) ), |
| 58 |
) ); |
| 59 |
|
| 60 |
foreach ( $this->items as $item ) { |
| 61 |
$wp_admin_bar->add_node( array( |
| 62 |
'parent' => 'spinupwp', |
| 63 |
'id' => strtolower( str_replace( ' ', '-', $item['title'] ) ), |
| 64 |
'title' => $item['title'], |
| 65 |
'href' => wp_nonce_url( add_query_arg( 'spinupwp_action', $item['action'], admin_url() ), $item['action'] ), |
| 66 |
) ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Add an item to the admin bar. |
| 72 |
* |
| 73 |
* @param string $title |
| 74 |
* @param string $action |
| 75 |
*/ |
| 76 |
public function add_item( $title, $action ) { |
| 77 |
$this->items[] = array( |
| 78 |
'title' => $title, |
| 79 |
'action' => $action, |
| 80 |
); |
| 81 |
} |
| 82 |
} |
| 83 |
|