-
Notifications
You must be signed in to change notification settings - Fork 1
/
Manager.php
93 lines (83 loc) · 2.46 KB
/
Manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace Amarkal\Settings;
class Manager
{
/**
* @var Singleton The reference to *Singleton* instance of this class
*/
private static $instance;
/**
* @var array The list of registered settings pages
*/
private $settings_pages = array();
/**
* Returns the *Singleton* instance of this class.
*
* @return Singleton The *Singleton* instance.
*/
public static function get_instance()
{
if( null === static::$instance )
{
static::$instance = new static();
}
return static::$instance;
}
/**
* Add a page to the admin menu.
*
* @param array $args
* @throws \RuntimeException
*/
public function add_settings_page( $args )
{
$slug = $args['slug'];
if(array_key_exists($slug,$this->settings_pages))
{
throw new \RuntimeException("A settings page with slug '$slug' has already been registered");
}
$page = new SettingsPage($args);
$this->settings_pages[$slug] = $page;
return $page;
}
/**
* Get a settings page from the list of registered settings pages.
*
* @param string $slug
* @param string $parent_slug
* @return SettingsPage
* @throws \RuntimeException If no settings page was found for the given slug/parent_slug
*/
public function get_settings_page( $slug )
{
if(!array_key_exists($slug, $this->settings_pages))
{
throw new \RuntimeException("The settings page '$slug' does not exist");
}
return $this->settings_pages[$slug];
}
/**
* Register styles & scripts to be enqueued by settings pages
*/
public function register_scripts()
{
\wp_register_style('amarkal-settings', \Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/css/dist/amarkal-settings.min.css'));
\wp_register_script('amarkal-settings',\Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/js/dist/amarkal-settings.min.js'),array('amarkal-ui'));
}
/**
* Private constructor to prevent instantiation
*/
private function __construct()
{
$this->init();
}
/**
* Register scripts and initiate the request handler.
*/
private function init()
{
\add_action('admin_init',array($this,'register_scripts'));
$rh = RequestHandler::get_instance();
$rh->init();
}
}