Passed
Pull Request — master (#16)
by Glynn
02:35
created

Registrar::pre_load_hook()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 3
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\Exception\Page_Exception;
33
use PinkCrab\Perique_Admin_Menu\Registrar\Page_Load_Action;
34
35
class Registrar {
36
37
	/**
38
	 * Used to register a primary page for a group.
39
	 *
40
	 * @param \PinkCrab\Perique_Admin_Menu\Page\Page $page
41
	 * @param \PinkCrab\Perique_Admin_Menu\Group\Abstract_Group|null $group
42
	 * @return void
43
	 */
44
	public function register_primary( Page $page, ?Abstract_Group $group = null ): void {
45
46
		switch ( get_parent_class( $page ) ) {
47
			// For menu pages
48
			case Menu_Page::class:
0 ignored issues
show
Coding Style introduced by
CASE keyword must be indented 4 spaces from SWITCH keyword
Loading history...
49
				$hook = add_menu_page(
50
					$page->page_title() ?? '',
51
					$group ? $group->get_group_title() : $page->menu_title(),
52
					$group ? $group->get_capability() : $page->capability(),
53
					$page->slug(),
54
					$page->render_view(),
55
					$group ? $group->get_icon() : '',
56
					(int) ( $group ? $group->get_position() : $page->position() )
57
				);
58
59
				// Register Enqueue hooks for page/group.
60
				$this->enqueue_scripts( $hook, $page, $group );
61
				// Register hook for pre-load page
62
				$this->pre_load_hook( $hook, $page, $group );
63
64
				break;
1 ignored issue
show
Coding Style introduced by
Case breaking statement must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
Blank lines are not allowed before case breaking statements
Loading history...
65
66
			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...
67
				do_action( Hooks::PAGE_REGISTRAR_PRIMARY, $page, $group );
68
		}
69
	}
70
71
	/**
72
	 * Used to register a sub menu page for any group.
73
	 *
74
	 * @param \PinkCrab\Perique_Admin_Menu\Page\Page $page
75
	 * @param string $parent_slug
76
	 * @param \PinkCrab\Perique_Admin_Menu\Group\Abstract_Group|null $group
77
	 * @return void
78
	 */
79
	public function register_subpage( Page $page, string $parent_slug, ?Abstract_Group $group = null ): void {
80
		switch ( get_parent_class( $page ) ) {
81
			case Menu_Page::class:
0 ignored issues
show
Coding Style introduced by
CASE keyword must be indented 4 spaces from SWITCH keyword
Loading history...
82
				$hook = add_submenu_page(
83
					$parent_slug,
84
					$page->page_title() ?? '',
85
					$page->menu_title(),
86
					$page->capability(),
87
					$page->slug(),
88
					$page->render_view(),
89
					$page->position()
90
				);
91
92
				// If the sub page cant be registered because of permissions. Then we need to register the page as a primary page.
93
				if ( ! is_string( $hook ) ) {
94
					return;
95
				}
96
97
				// Register Enqueue hooks for page/group.
98
				$this->enqueue_scripts( $hook, $page, $group );
99
100
				// Register hook for pre-load page
101
				$this->pre_load_hook( $hook, $page, $group );
102
103
				break;
1 ignored issue
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...
Coding Style introduced by
Blank lines are not allowed before case breaking statements
Loading history...
104
			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...
105
				do_action( Hooks::PAGE_REGISTRAR_SUB, $page, $parent_slug );
106
		}
107
	}
108
109
	/**
110
	 * Adds enqueue admin scripts based on the current page hook.
111
	 *
112
	 * @param string $hook
113
	 * @param \PinkCrab\Perique_Admin_Menu\Page\Page $page
114
	 * @param \PinkCrab\Perique_Admin_Menu\Group\Abstract_Group|null $group
115
	 * @return void
116
	 */
117
	protected function enqueue_scripts( string $hook, Page $page, ?Abstract_Group $group = null ): void {
118
		add_action( 'admin_enqueue_scripts', new Page_Enqueue_Action( $hook, $page, $group ) );
119
	}
120
121
	/**
122
	 * Adds the pre load actions for the current page.
123
	 *
124
	 * @param string $hook
125
	 * @param \PinkCrab\Perique_Admin_Menu\Page\Page $page
126
	 * @param \PinkCrab\Perique_Admin_Menu\Group\Abstract_Group|null $group
127
	 * @return void
128
	 */
129
	protected function pre_load_hook( string $hook, Page $page, ?Abstract_Group $group = null ): void {
130
		add_action( 'load-' . $hook, new Page_Load_Action( $page, $group ) );
131
	}
132
}
133