Completed
Push — develop ( 765c38...eb44bf )
by David
03:19
created

Wordlift_Admin_Page::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Pages: Abstract Admin Page.
4
 *
5
 * An abstract class meant to be extended by admin pages.
6
 *
7
 * @since      3.11.0
8
 * @package    Wordlift
9
 * @subpackage Wordlift/admin
10
 */
11
12
/**
13
 * Define the {@link Wordlift_Admin_Page} class.
14
 *
15
 * @since      3.11.0
16
 * @package    Wordlift
17
 * @subpackage Wordlift/admin
18
 */
19
abstract class Wordlift_Admin_Page {
20
21
	/**
22
	 * Define the {@link Wordlift_Admin_Page} constructor.
23
	 *
24
	 * @since 3.20.0
25
	 */
26
	public function __construct() {
27
28
		add_action( 'admin_menu', array( $this, 'admin_menu' ), 10, 0 );
29
30
	}
31
32
	/**
33
	 * Get the parent slug.
34
	 *
35
	 * @since 3.11.0
36
	 *
37
	 * @return string The parent slug (default 'wl_admin_menu').
38
	 */
39
	protected function get_parent_slug() {
40
41
		return 'wl_admin_menu';
42
	}
43
44
	/**
45
	 * Get the required capability.
46
	 *
47
	 * @since 3.11.0
48
	 *
49
	 * @return string The capability (default 'manage_options').
50
	 */
51
	protected function get_capability() {
52
53
		return 'manage_options';
54
	}
55
56
	/**
57
	 * Get the page title. Will be translated.
58
	 *
59
	 * @since 3.11.0
60
	 *
61
	 * @return string The page title.
62
	 */
63
	abstract function get_page_title();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
64
65
	/**
66
	 * Get the menu title. Will be translated.
67
	 *
68
	 * @since 3.11.0
69
	 *
70
	 * @return string The menu title.
71
	 */
72
	abstract function get_menu_title();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
73
74
	/**
75
	 * Get the menu slug.
76
	 *
77
	 * @since 3.11.0
78
	 *
79
	 * @return string The menu slug.
80
	 */
81
	abstract function get_menu_slug();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
82
83
	/**
84
	 * Get the page url.
85
	 *
86
	 * @since 3.14.0
87
	 *
88
	 * @return string The escaped url of the admin page
89
	 */
90
	function get_url() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
91
92
		// ideally should have used menu_page_url, but it is loaded later than some usages.
93
		$url = admin_url( 'admin.php?page=' . $this->get_menu_slug() );
94
95
		return esc_url( $url );
96
	}
97
98
	/**
99
	 * Get the partial file name, used in the {@link render} function.
100
	 *
101
	 * @since 3.11.0
102
	 *
103
	 * @return string The partial file name.
104
	 */
105
	abstract function get_partial_name();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
106
107
	/**
108
	 * The `admin_menu` callback. Will call {@link add_submenu_page} to add the
109
	 * page to the admin menu.
110
	 *
111
	 * @since 3.11.0
112
	 *
113
	 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
114
	 */
115
	public function admin_menu() {
116
117
		// Add the sub-menu page.
118
		//
119
		// See http://codex.wordpress.org/Function_Reference/add_submenu_page
120
		$page = add_submenu_page(
121
			$this->get_parent_slug(),
122
			$this->get_page_title(),
123
			$this->get_menu_title(),
124
			$this->get_capability(),                   // The required capability, provided by the calling hook.
125
			$this->get_menu_slug(),
126
			array( $this, 'render' )
127
		);
128
129
		// Set a hook to enqueue scripts only when the settings page is displayed.
130
		add_action( 'admin_print_scripts-' . $page, array(
131
			$this,
132
			'enqueue_scripts',
133
		) );
134
135
		// Finally return the page hook_suffix.
136
		return $page;
137
	}
138
139
	/**
140
	 * Enqueue scripts for the specific page. Subclasses can override this function
141
	 * to provide their own styles/scripts.
142
	 *
143
	 * @since 3.11.0
144
	 */
145
	public function enqueue_scripts() {
146
	}
147
148
	/**
149
	 * Render the page.
150
	 */
151
	public function render() {
152
153
		// Include the partial.
154
		include( plugin_dir_path( __FILE__ ) . 'partials/' . $this->get_partial_name() );
155
156
	}
157
158
}
159