|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Abstract_Admin_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 | +use Leaves_And_Love\OOP_Admin_Pages\Admin_Page; |
| 12 | +use Leaves_And_Love\OOP_Admin_Pages\Exception\Config_Invalid_Exception; |
| 13 | +use BrightNucleus\Config\ConfigInterface as Config; |
| 14 | +use BrightNucleus\Config\ConfigTrait; |
| 15 | + |
| 16 | +/** |
| 17 | + * Abstract class representing an admin page. |
| 18 | + * |
| 19 | + * @since 1.0.0 |
| 20 | + */ |
| 21 | +abstract class Abstract_Admin_Page implements Admin_Page { |
| 22 | + |
| 23 | + use ConfigTrait; |
| 24 | + |
| 25 | + const SLUG = 'slug'; |
| 26 | + const TITLE = 'title'; |
| 27 | + const RENDER_CALLBACK = 'render_callback'; |
| 28 | + const INITIALIZE_CALLBACK = 'initialize_callback'; |
| 29 | + |
| 30 | + /** |
| 31 | + * Constructor. |
| 32 | + * |
| 33 | + * Sets the configuration. |
| 34 | + * |
| 35 | + * @since 1.0.0 |
| 36 | + * |
| 37 | + * @param Config $config Admin page configuration. Must contain keys 'slug' (admin page identifier), 'title' |
| 38 | + * and 'render_callback' (callable that receives the configuration as sole parameter). |
| 39 | + * May also contain an 'initialize_callback' key. |
| 40 | + * |
| 41 | + * @throws Config_Invalid_Exception Thrown when the configuration is invalid. |
| 42 | + */ |
| 43 | + public function __construct( Config $config ) { |
| 44 | + $this->processConfig( $config ); |
| 45 | + |
| 46 | + $required_keys = $this->get_required_config_keys(); |
| 47 | + |
| 48 | + foreach ( $required_keys as $required_key ) { |
| 49 | + if ( ! $this->hasConfigKey( $required_key ) ) { |
| 50 | + throw new Config_Invalid_Exception( sprintf( 'The required configuration key %s is missing.', $required_key ) ); |
| 51 | + } |
| 52 | + |
| 53 | + $value = $this->getConfigKey( $required_key ); |
| 54 | + if ( empty( $value ) ) { |
| 55 | + throw new Config_Invalid_Exception( sprintf( 'The required configuration key %s is empty.', $required_key ) ); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Gets the slug of the admin page. |
| 62 | + * |
| 63 | + * @since 1.0.0 |
| 64 | + * |
| 65 | + * @return string Admin page slug. |
| 66 | + */ |
| 67 | + public function get_slug() : string { |
| 68 | + return $this->getConfigKey( self::SLUG ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Gets the title of the admin page. |
| 73 | + * |
| 74 | + * @since 1.0.0 |
| 75 | + * |
| 76 | + * @return string Admin page title. |
| 77 | + */ |
| 78 | + public function get_title() : string { |
| 79 | + return $this->getConfigKey( self::TITLE ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Initializes the admin page on pageload. |
| 84 | + * |
| 85 | + * This method must be called before any output is printed. |
| 86 | + * |
| 87 | + * @since 1.0.0 |
| 88 | + */ |
| 89 | + public function initialize() { |
| 90 | + if ( ! $this->hasConfigKey( self::INITIALIZE_CALLBACK ) ) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + call_user_func( $this->getConfigKey( self::INITIALIZE_CALLBACK ), $this->config ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Renders the admin page content. |
| 99 | + * |
| 100 | + * @since 1.0.0 |
| 101 | + */ |
| 102 | + public function render() { |
| 103 | + call_user_func( $this->getConfigKey( self::RENDER_CALLBACK ), $this->config ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Gets the configuration keys that are required for an admin page. |
| 108 | + * |
| 109 | + * @since 1.0.0 |
| 110 | + * |
| 111 | + * @return array Array of configuration keys. |
| 112 | + */ |
| 113 | + protected function get_required_config_keys() { |
| 114 | + return array( self::SLUG, self::TITLE, self::RENDER_CALLBACK ); |
| 115 | + } |
| 116 | +} |
0 commit comments