| 1 |
<?php |
| 2 |
namespace EverCompare\Admin; |
| 3 |
/** |
| 4 |
* Dashboard handlers class |
| 5 |
*/ |
| 6 |
class Dashboard { |
| 7 |
|
| 8 |
/** |
| 9 |
* Menu capability |
| 10 |
*/ |
| 11 |
const MENU_CAPABILITY = 'manage_options'; |
| 12 |
|
| 13 |
/** |
| 14 |
* Parent Menu Page Slug |
| 15 |
*/ |
| 16 |
const MENU_PAGE_SLUG = 'evercompare'; |
| 17 |
|
| 18 |
/** |
| 19 |
* [$parent_menu_hook] Parent Menu Hook |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
static $parent_menu_hook = ''; |
| 23 |
|
| 24 |
/** |
| 25 |
* [$_instance] |
| 26 |
* @var null |
| 27 |
*/ |
| 28 |
private static $_instance = null; |
| 29 |
|
| 30 |
/** |
| 31 |
* [instance] Initializes a singleton instance |
| 32 |
* @return [Admin] |
| 33 |
*/ |
| 34 |
public static function instance() { |
| 35 |
if ( is_null( self::$_instance ) ) { |
| 36 |
self::$_instance = new self(); |
| 37 |
} |
| 38 |
return self::$_instance; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Initialize the class |
| 43 |
*/ |
| 44 |
private function __construct() { |
| 45 |
|
| 46 |
Admin_Fields::instance(); |
| 47 |
|
| 48 |
add_action( 'admin_menu', [ $this, 'add_menu' ], 20 ); |
| 49 |
|
| 50 |
add_filter('plugin_action_links_'.EVERCOMPARE_BASE, [ $this, 'action_links' ] ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* [action_links] add plugin action link |
| 55 |
* @param [array] $links default plugin action link |
| 56 |
* @return [array] plugin action link |
| 57 |
*/ |
| 58 |
public function action_links( $links ) { |
| 59 |
|
| 60 |
if ( ! current_user_can( self::MENU_CAPABILITY ) ) { |
| 61 |
return $links; |
| 62 |
} |
| 63 |
|
| 64 |
$settings_link = '<a href="'.admin_url( 'admin.php?page='.self::MENU_PAGE_SLUG ).'">'.esc_html__( 'Settings', 'ever-compare' ).'</a>'; |
| 65 |
|
| 66 |
array_unshift( $links, $settings_link ); |
| 67 |
|
| 68 |
return $links; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* [add_menu] Admin Menu |
| 73 |
*/ |
| 74 |
public function add_menu(){ |
| 75 |
|
| 76 |
global $submenu; |
| 77 |
|
| 78 |
self::$parent_menu_hook = add_menu_page( |
| 79 |
esc_html__( 'Ever Compare', 'ever-compare' ), |
| 80 |
esc_html__( 'Ever Compare', 'ever-compare' ), |
| 81 |
self::MENU_CAPABILITY, |
| 82 |
self::MENU_PAGE_SLUG, |
| 83 |
[ $this,'dashboard' ], |
| 84 |
'dashicons-update-alt', |
| 85 |
59 |
| 86 |
); |
| 87 |
|
| 88 |
if ( current_user_can( self::MENU_CAPABILITY ) ) { |
| 89 |
|
| 90 |
foreach ( $this->sub_menu_nav() as $menukey => $menu ) { |
| 91 |
$submenu[ self::MENU_PAGE_SLUG ][] = array( |
| 92 |
esc_html__( $menu['title'], 'ever-compare' ), |
| 93 |
self::MENU_CAPABILITY, |
| 94 |
'admin.php?page='.self::MENU_PAGE_SLUG.'#'.$menukey, |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
add_action( 'load-' . self::$parent_menu_hook, [ $this, 'init_hooks'] ); |
| 101 |
|
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Initialize our hooks for the admin page |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
public function init_hooks() { |
| 111 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* [enqueue_scripts] Add Scripts Base Menu Slug |
| 116 |
* @param [string] $hook |
| 117 |
* @return [void] |
| 118 |
*/ |
| 119 |
public function enqueue_scripts() { |
| 120 |
wp_enqueue_style( 'evercompare-admin' ); |
| 121 |
wp_enqueue_script( 'evercompare-admin' ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* [dashboard] Dashboard plugin page |
| 126 |
* @return [HTML] |
| 127 |
*/ |
| 128 |
public function dashboard(){ |
| 129 |
Admin_Fields::instance()->plugin_page(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* [sub_menu_nav] |
| 134 |
* @return [array] |
| 135 |
*/ |
| 136 |
public function sub_menu_nav() { |
| 137 |
|
| 138 |
$submenu = [ |
| 139 |
'settings' => [ |
| 140 |
'title' => esc_html__( 'Settings', 'ever-compare' ), |
| 141 |
'subtitle' => esc_html__( 'Settings', 'ever-compare' ), |
| 142 |
'icon' => '', |
| 143 |
'class' => '', |
| 144 |
], |
| 145 |
]; |
| 146 |
|
| 147 |
return apply_filters( 'ever_compare_dashboard_submenu', $submenu ); |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
} |