Created
August 23, 2018 15:41
-
-
Save earth3300/9d9ebb93fc709573a6b26c024ab7ad06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** Do not allow direct access */ | |
defined( 'NDA' ) || exit('Access Denied.'); | |
/** | |
* Interface | |
* | |
* This file attempts to implement formatting and documentation standards | |
* so that it can be used with a higher degree of confidence as a template. | |
* This specific file interfaces with WordPress by _decoupling_ using an interface. | |
* This is important so that the ideas and the code contained herein can be used in | |
* frameworks _other_ than WordPress. This is the goal. This is the hope. | |
* | |
* Note that we do not use WordPress specific calls _except for_ in the class(???). | |
* | |
* Do we also need an abstract class, or does the interface provide that function. | |
* | |
* It took a substantial amount of work already just to achieve this level of commonality | |
* between the examples given online, the need to follow documation standards and the | |
* need to use tabs instead of spaces. When copying code from online, it appears that spaces | |
* are used. When this is done, the editor assumes spaces. Thus the indenting may need to be | |
* removed, then the text may need to be copied to a new file and the indentation redone, using | |
* tabs instead of spaces. This is done to increase interoperability between clients. | |
* | |
* Note also that UTF-8 is used and line endings may need to be cleared up. Do not close the | |
* file with ?>. Open the file with <?php, not <?. | |
* | |
* @link https://carlalexander.ca/dependency-inversion-principle-wordpress/ | |
* @see https://en.wikipedia.org/wiki/Dependency_inversion_principle | |
*/ | |
/** | |
* EC01 Options Interface | |
*/ | |
interface EC01_OptionsInterface | |
{ | |
/** | |
* Gets the option for the given name. Returns the default value if | |
* the value does not exist. | |
* | |
* @param string $name | |
* @param mixed $default | |
* | |
* @return mixed | |
*/ | |
public function get($name, $default = null); | |
/** | |
* Sets an option. | |
* | |
* @param string $name | |
* @param mixed $value | |
*/ | |
public function set($name, $value); | |
} | |
/** | |
* EC01 Admin Page Class | |
*/ | |
class EC01_AdminPage | |
{ | |
/** | |
* EC01 options | |
* | |
* @var EC01_OptionsInterface | |
*/ | |
private $options; | |
/** | |
* Constructor. | |
*/ | |
public function __construct(EC01_OptionsInterface $options) | |
{ | |
$this->options = $options; | |
} | |
// ... | |
} | |
/** | |
* EC01 Plugin Class | |
* | |
* @uses AdminPageInterface | |
* @uses OptionsInterface | |
* @uses AdminPage | |
* @uses OptionsPage | |
*/ | |
class EC01_Plugin | |
{ | |
/** | |
* EC01 admin page | |
* | |
* @var EC01_AdminPageInterface | |
*/ | |
private $admin_page; | |
/** | |
* Constructor. | |
*/ | |
public function __construct(EC01_AdminPageInterface $admin_page) | |
{ | |
$this->admin_page = $admin_page; | |
} | |
/** | |
* Loads the plugin into WordPress. | |
*/ | |
public function load() | |
{ | |
$this->admin_page->register(); | |
} | |
//... | |
} | |
$myplugin = new EC01_Plugin(); | |
add_action('wp_loaded', array($myplugin, 'load')); | |
/** | |
* EC01 Admin Page Interface | |
*/ | |
interface EC01_AdminPageInterface | |
{ | |
/** | |
* Register the admin page with WordPress when WordPress loads. | |
*/ | |
public function register(); | |
//... | |
} | |
/** | |
* EC01 Admin Page Class | |
* | |
* Implements EC01 Admin Page Interface | |
*/ | |
class EC01_AdminPage implements EC01_AdminPageInterface | |
{ | |
/** | |
* EC01 options | |
* | |
* @var EC01_OptionsInterface | |
*/ | |
private $options; | |
/** | |
* Constructor. | |
*/ | |
public function __construct(EC01_OptionsInterface $options) | |
{ | |
$this->options = $options; | |
} | |
/** | |
* Register the admin page with WordPress when WordPress loads. | |
*/ | |
public function register() | |
{ | |
add_menu_page('EC01 Admin Page', 'EC01 Admin Page', 'edit_posts', 'ec01_admin_page', array($this, 'display_page')); | |
add_action('admin_menu', array($this, 'add_page')); | |
} | |
/** | |
* Add admin page to WordPress admin menu. | |
*/ | |
public function add_page() | |
{ | |
add_menu_page('EC01 Admin Page', 'EC01 Admin Page', 'edit_posts', 'ec01_admin_page', array($this, 'display_page')); | |
} | |
/** | |
* Display plugin admin page. | |
*/ | |
public function display_page() | |
{ | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment