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

Commit

Permalink
Introduce WordPress_Admin_Page class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Arntz committed Jan 14, 2018
1 parent e7e6a21 commit 3f73ca4
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/Admin_Page/WordPress_Admin_Page.php
@@ -0,0 +1,93 @@
<?php
/**
* WordPress_Admin_Page class
*
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page
* @since 1.0.0
*/

namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page;

/**
* Class representing a basic WordPress admin page.
*
* @since 1.0.0
*/
class WordPress_Admin_Page extends Abstract_Admin_Page {

const CAPABILITY = 'capability';

/**
* Gets the URL of the admin page.
*
* @since 1.0.0
*
* @return string Admin page URL.
*/
public function get_url() : string {
$parent_file = $this->get_parent_file();

return add_query_arg( 'page', $this->getConfigKey( self::SLUG ), admin_url( $parent_file ) );
}

/**
* Registers the admin page within the environment.
*
* @since 1.0.0
*/
public function register() {
$callback = $this->get_register_hook_callback();

if ( doing_action( 'admin_menu' ) ) {
call_user_func( $callback );
} else {
add_action( 'admin_menu', $callback );
}
}

/**
* Gets the callback to use to register the admin page within WordPress.
*
* @since 1.0.0
*
* @return callable Callback to hook into the respective menu action.
*/
protected function get_register_hook_callback() {
return function() {

This comment has been minimized.

Copy link
@felixarntz

felixarntz Jan 20, 2018

Owner

Anonymous functions, also known as closures, are available since PHP 5.3.

$slug = $this->getConfigKey( self::SLUG );
$title = $this->getConfigKey( self::TITLE );
$capability = $this->getConfigKey( self::CAPABILITY );

$hook_suffix = add_submenu_page( null, $title, $title, $capability, $slug, array( $this, 'render' ) );

add_action( "load-{$hook_suffix}", array( $this, 'initialize' ) );

This comment has been minimized.

Copy link
@felixarntz

felixarntz Jan 20, 2018

Owner

This hook is triggered before that very admin page is displayed.

};
}

/**
* Gets the parent file for the admin page URL in WordPress.
*
* May contain query parameters.
*
* @since 1.0.0
*
* @return string Parent file for the URL.
*/
protected function get_parent_file() {
return 'admin.php';

This comment has been minimized.

Copy link
@felixarntz

felixarntz Jan 20, 2018

Owner

The parent file of a page that is not a submenu page is always admin.php. This is still a separate method so that it can be easily overridden in a subclass.

}

/**
* Gets the configuration keys that are required for an admin page.
*
* @since 1.0.0
*
* @return array Array of configuration keys.
*/
protected function get_required_config_keys() {
$required_keys = parent::get_required_config_keys();
$required_keys[] = self::CAPABILITY;

return $required_keys;
}
}

1 comment on commit 3f73ca4

@felixarntz
Copy link
Owner

@felixarntz felixarntz commented on 3f73ca4 Jan 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are the other WordPress-specific parameters, such as menu_title, parent_slug etc?

Keep in mind that you should abstract out the different variants. While in WordPress you typically use menu pages and submenu pages, the most basic admin page in WordPress is one that doesn't have any menu entry (this is possible by calling add_submenu_page() with null as parent slug). Therefore the other parameters are not necessary here. Further specifications can be made in subclasses.

Please sign in to comment.