Registrar   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 11
eloc 39
c 4
b 0
f 0
dl 0
loc 107
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A pre_load_hook() 0 2 1
A register_subpage() 0 33 3
A register_primary() 0 29 6
A enqueue_scripts() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Used to register all pages and subpages.
7
 *
8
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
 *
20
 * @author Glynn Quelch <[email protected]>
21
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
22
 * @package PinkCrab\Perique_Admin_Menu
23
 */
24
25
namespace PinkCrab\Perique_Admin_Menu\Registrar;
26
27
use TypeError;
28
use PinkCrab\Perique_Admin_Menu\Hooks;
29
use PinkCrab\Perique_Admin_Menu\Page\Page;
30
use PinkCrab\Perique_Admin_Menu\Page\Menu_Page;
31
use PinkCrab\Perique_Admin_Menu\Group\Abstract_Group;
32
use PinkCrab\Perique_Admin_Menu\Registrar\Page_Load_Action;
33
34
class Registrar {
35
36
	/**
37
	 * Used to register a primary page for a group.
38
	 *
39
	 * @param \PinkCrab\Perique_Admin_Menu\Page\Page $page
40
	 * @param \PinkCrab\Perique_Admin_Menu\Group\Abstract_Group|null $group
41
	 * @return void
42
	 */
43
	public function register_primary( Page $page, ?Abstract_Group $group = null ): void {
44
45
		switch ( true ) {
46
			// For menu pages
47
			case $page instanceof Menu_Page:
0 ignored issues
show
Coding Style introduced by
CASE keyword must be indented 4 spaces from SWITCH keyword
Loading history...
48
				/** @var Menu_Page $page */
49
				$page = $page;
50
51
				$hook = add_menu_page(
52
					$page->page_title() ?? '',
53
					$group ? $group->get_group_title() : $page->menu_title(),
54
					$group ? $group->get_capability() : $page->capability(),
55
					$page->slug(),
56
					$page->render_view(),
57
					$group ? $group->get_icon() : '',
58
					(int) ( $group ? $group->get_position() : $page->position() )
59
				);
60
61
				$page->set_page_hook( $hook );
62
63
				// Register Enqueue hooks for page/group.
64
				$this->enqueue_scripts( $hook, $page, $group );
65
				// Register hook for pre-load page
66
				$this->pre_load_hook( $hook, $page, $group );
67
68
				break;
0 ignored issues
show
Coding Style introduced by
Case breaking statement must be indented 4 spaces from SWITCH keyword
Loading history...
69
70
			default:
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
DEFAULT case must have a breaking statement
Loading history...
71
				do_action( Hooks::PAGE_REGISTRAR_PRIMARY, $page, $group );
72
		}
73
	}
74
75
	/**
76
	 * Used to register a sub menu page for any group.
77
	 *
78
	 * @param \PinkCrab\Perique_Admin_Menu\Page\Page $page
79
	 * @param string $parent_slug
80
	 * @param \PinkCrab\Perique_Admin_Menu\Group\Abstract_Group|null $group
81
	 * @return void
82
	 */
83
	public function register_subpage( Page $page, string $parent_slug, ?Abstract_Group $group = null ): void {
84
		switch ( true ) {
85
			case $page instanceof Menu_Page:
0 ignored issues
show
Coding Style introduced by
CASE keyword must be indented 4 spaces from SWITCH keyword
Loading history...
86
				/** @var Menu_Page $page */
87
				$page = $page;
88
89
				$hook = add_submenu_page(
90
					$parent_slug,
91
					$page->page_title() ?? '',
92
					$page->menu_title(),
93
					$page->capability(),
94
					$page->slug(),
95
					$page->render_view(),
96
					$page->position()
97
				);
98
99
				// If the sub page cant be registered because of permissions. Then we need to register the page as a primary page.
100
				if ( ! is_string( $hook ) ) {
101
					return;
102
				}
103
104
				// Set the pages hook.
105
				$page->set_page_hook( $hook );
106
107
				// Register Enqueue hooks for page/group.
108
				$this->enqueue_scripts( $page->page_hook() ?? $hook, $page, $group );
109
110
				// Register hook for pre-load page
111
				$this->pre_load_hook( $page->page_hook() ?? $hook, $page, $group );
112
113
				break;
0 ignored issues
show
Coding Style introduced by
Case breaking statement must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
114
			default:
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
DEFAULT case must have a breaking statement
Loading history...
115
				do_action( Hooks::PAGE_REGISTRAR_SUB, $page, $parent_slug );
116
		}
117
	}
118
119
	/**
120
	 * Adds enqueue admin scripts based on the current page hook.
121
	 *
122
	 * @param string $hook
123
	 * @param \PinkCrab\Perique_Admin_Menu\Page\Page $page
124
	 * @param \PinkCrab\Perique_Admin_Menu\Group\Abstract_Group|null $group
125
	 * @return void
126
	 */
127
	protected function enqueue_scripts( string $hook, Page $page, ?Abstract_Group $group = null ): void {
128
		add_action( 'admin_enqueue_scripts', new Page_Enqueue_Action( $hook, $page, $group ) );
129
	}
130
131
	/**
132
	 * Adds the pre load actions for the current page.
133
	 *
134
	 * @param string $hook
135
	 * @param \PinkCrab\Perique_Admin_Menu\Page\Page $page
136
	 * @param \PinkCrab\Perique_Admin_Menu\Group\Abstract_Group|null $group
137
	 * @return void
138
	 */
139
	protected function pre_load_hook( string $hook, Page $page, ?Abstract_Group $group = null ): void {
140
		add_action( 'load-' . $hook, new Page_Load_Action( $page, $group ) );
141
	}
142
}
143