Skip to content
This repository was archived by the owner on Sep 26, 2019. It is now read-only.

Commit fc60196

Browse files
author
Felix Arntz
committed
Introduce WordPress_Admin_Submenu_Page class.
1 parent 7628173 commit fc60196

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* WordPress_Admin_Submenu_Page class
4+
*
5+
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page
6+
* @since 1.0.0
7+
*/
8+
9+
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page;
10+
11+
/**
12+
* Class representing a WordPress admin page that also has a submenu entry.
13+
*
14+
* @since 1.0.0
15+
*/
16+
class WordPress_Admin_Submenu_Page extends WordPress_Admin_Page {
17+
18+
const PARENT_SLUG = 'parent_slug';
19+
const MENU_TITLE = 'menu_title';
20+
21+
/**
22+
* Gets the callback to use to register the admin page within WordPress.
23+
*
24+
* @since 1.0.0
25+
*
26+
* @return callable Callback to hook into the respective menu action.
27+
*/
28+
protected function get_register_hook_callback() {
29+
return function() {
30+
$slug = $this->getConfigKey( self::SLUG );
31+
$title = $this->getConfigKey( self::TITLE );
32+
$capability = $this->getConfigKey( self::CAPABILITY );
33+
$parent_slug = $this->getConfigKey( self::PARENT_SLUG );
34+
$menu_title = $this->hasConfigKey( self::MENU_TITLE ) ? $this->getConfigKey( self::MENU_TITLE ) : $title;
35+
36+
$hook_suffix = add_submenu_page( $parent_slug, $title, $menu_title, $capability, $slug, array( $this, 'render' ) );
37+
38+
add_action( "load-{$hook_suffix}", array( $this, 'initialize' ) );
39+
};
40+
}
41+
42+
/**
43+
* Gets the parent file for the admin page URL in WordPress.
44+
*
45+
* May contain query parameters.
46+
*
47+
* @since 1.0.0
48+
*
49+
* @return string Parent file for the URL.
50+
*/
51+
protected function get_parent_file() {
52+
$parent_slug = $this->getConfigKey( self::PARENT_SLUG );
53+
54+
if ( false !== strpos( $parent_slug, '?' ) ) {
55+
list( $base_slug, $query ) = explode( '?', $parent_slug, 2 );
56+
} else {
57+
$base_slug = $parent_slug;
58+
}
59+
60+
if ( '.php' === substr( $base_slug, -4 ) ) {
61+
return $parent_slug;
62+
}
63+
64+
return 'admin.php';
65+
}
66+
67+
/**
68+
* Gets the configuration keys that are required for an admin page.
69+
*
70+
* @since 1.0.0
71+
*
72+
* @return array Array of configuration keys.
73+
*/
74+
protected function get_required_config_keys() {
75+
$required_keys = parent::get_required_config_keys();
76+
$required_keys[] = self::PARENT_SLUG;
77+
78+
return $required_keys;
79+
}
80+
}

0 commit comments

Comments
 (0)