Completed
Push — develop ( 9c9e32...c47bcf )
by David
02:45
created

Wordlift_Admin_Page::get_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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
	 * Get the parent slug.
23
	 *
24
	 * @since 3.11.0
25
	 *
26
	 * @return string The parent slug (default 'wl_admin_menu').
27
	 */
28
	protected function get_parent_slug() {
29
30
		return 'wl_admin_menu';
31
	}
32
33
	/**
34
	 * Get the required capability.
35
	 *
36
	 * @since 3.11.0
37
	 *
38
	 * @return string The capability (default 'manage_options').
39
	 */
40
	protected function get_capability() {
41
42
		return 'manage_options';
43
	}
44
45
	/**
46
	 * Get the page title. Will be translated.
47
	 *
48
	 * @since 3.11.0
49
	 *
50
	 * @return string The page title.
51
	 */
52
	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...
53
54
	/**
55
	 * Get the menu title. Will be translated.
56
	 *
57
	 * @since 3.11.0
58
	 *
59
	 * @return string The menu title.
60
	 */
61
	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...
62
63
	/**
64
	 * Get the menu slug.
65
	 *
66
	 * @since 3.11.0
67
	 *
68
	 * @return string The menu slug.
69
	 */
70
	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...
71
72
	/**
73
	 * Get the page url.
74
	 *
75
	 * @since 3.14.0
76
	 *
77
	 * @return string The escaped url of the admin page
78
	 */
79
	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...
80
81
		// ideally should have used menu_page_url, but it is loaded later than some usages.
82
		$url = admin_url( 'admin.php?page=' . $this->get_menu_slug() );
83
84
		return esc_url( $url );
85
	}
86
87
	/**
88
	 * Get the partial file name, used in the {@link render} function.
89
	 *
90
	 * @since 3.11.0
91
	 *
92
	 * @return string The partial file name.
93
	 */
94
	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...
95
96
	/**
97
	 * The `admin_menu` callback. Will call {@link add_submenu_page} to add the
98
	 * page to the admin menu.
99
	 *
100
	 * @since 3.11.0
101
	 *
102
	 * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required.
103
	 */
104
	public function admin_menu() {
105
106
		// Add the sub-menu page.
107
		//
108
		// See http://codex.wordpress.org/Function_Reference/add_submenu_page
109
		$page = add_submenu_page(
110
			$this->get_parent_slug(),
111
			$this->get_page_title(),
112
			$this->get_menu_title(),
113
			$this->get_capability(),                   // The required capability, provided by the calling hook.
114
			$this->get_menu_slug(),
115
			array( $this, 'render' )
116
		);
117
118
		// Set a hook to enqueue scripts only when the settings page is displayed.
119
		add_action( 'admin_print_scripts-' . $page, array(
120
			$this,
121
			'enqueue_scripts',
122
		) );
123
124
		// Finally return the page hook_suffix.
125
		return $page;
126
	}
127
128
	/**
129
	 * Enqueue scripts for the specific page. Subclasses can override this function
130
	 * to provide their own styles/scripts.
131
	 *
132
	 * @since 3.11.0
133
	 */
134
	public function enqueue_scripts() {
135
	}
136
137
	/**
138
	 * Render the page.
139
	 */
140
	public function render() {
141
142
		// Include the partial.
143
		include( plugin_dir_path( __FILE__ ) . 'partials/' . $this->get_partial_name() );
144
145
	}
146
147
}
148