| 1 |
<?php |
| 2 |
/** |
| 3 |
* Standard PerfOps One admin bar handling. |
| 4 |
* |
| 5 |
* @package PerfOpsOne |
| 6 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 7 |
* @since 2.1.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace PerfOpsOne; |
| 11 |
|
| 12 |
use PerfOpsOne\Resources; |
| 13 |
|
| 14 |
/** |
| 15 |
* Standard PerfOps One admin bar handling. |
| 16 |
* |
| 17 |
* This class defines all code necessary to initialize and handle PerfOps One admin bar. |
| 18 |
* |
| 19 |
* @package Plugin |
| 20 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 21 |
* @since 2.1.0 |
| 22 |
*/ |
| 23 |
|
| 24 |
if ( ! class_exists( 'PerfOpsOne\AdminBar' ) ) { |
| 25 |
|
| 26 |
class AdminBar { |
| 27 |
|
| 28 |
/** |
| 29 |
* The PerfOps One admin bar items. |
| 30 |
* |
| 31 |
* @since 2.1.0 |
| 32 |
* @var array $items Maintains the PerfOps One admin bar items. |
| 33 |
*/ |
| 34 |
private static $items = []; |
| 35 |
|
| 36 |
/** |
| 37 |
* Are the menus already initialized. |
| 38 |
* |
| 39 |
* @since 2.1.0 |
| 40 |
* @var boolean $initialized Maintains the menus initialization status. |
| 41 |
*/ |
| 42 |
private static $initialized = false; |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Initialize the admin bar items. |
| 47 |
* |
| 48 |
* @since 2.1.0 |
| 49 |
*/ |
| 50 |
public static function initialize() { |
| 51 |
if ( ! self::$initialized ) { |
| 52 |
wp_register_style( PERFOO_ASSETS_ID, PERFOO_ASSETS_CSS, [], true ); |
| 53 |
wp_enqueue_style( PERFOO_ASSETS_ID ); |
| 54 |
add_action( 'admin_bar_menu', [ self::class, 'finalize' ], PHP_INT_MAX, 1 ); |
| 55 |
self::$initialized = true; |
| 56 |
} |
| 57 |
self::$items = apply_filters( 'init_perfopsone_admin_bar', [] ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Dispatch the admin menus. |
| 62 |
* |
| 63 |
* @since 2.1.0 |
| 64 |
*/ |
| 65 |
public static function finalize( $admin_bar ) { |
| 66 |
if ( apply_filters( 'poo_hide_adminbar', false ) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
if ( 0 < count( self::$items ) ) { |
| 70 |
usort( |
| 71 |
self::$items, |
| 72 |
function( $a, $b ) { |
| 73 |
return strcmp( strtolower( $a['title'] ), strtolower( $b['title'] ) ); |
| 74 |
} |
| 75 |
); |
| 76 |
$id = 'perfopsone-dashboard'; |
| 77 |
$admin_bar->add_node( |
| 78 |
[ |
| 79 |
'id' => $id, |
| 80 |
'href' => esc_url( admin_url( 'admin.php?page=' . $id ) ), |
| 81 |
'title' => '<span class="ab-icon poo-ico-logo" style="padding-top: 6px;"></span><span class="ab-label">' . PERFOO_PRODUCT_NAME . '</span>', |
| 82 |
] |
| 83 |
); |
| 84 |
foreach ( self::$items as $item ) { |
| 85 |
$admin_bar->add_node( array_merge( $item, [ 'parent' => $id ] ) ); |
| 86 |
} |
| 87 |
} |
| 88 |
self::$initialized = true; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|