class-page.php
327 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The admin-page functionality. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package MyThemeShop |
| 7 | * @subpackage MyThemeShop\Admin |
| 8 | * @author MyThemeShop <admin@mythemeshop.com> |
| 9 | */ |
| 10 | |
| 11 | namespace MyThemeShop\Admin; |
| 12 | |
| 13 | use MyThemeShop\Helpers\Param; |
| 14 | |
| 15 | /** |
| 16 | * Page class. |
| 17 | */ |
| 18 | class Page { |
| 19 | |
| 20 | /** |
| 21 | * Unique ID used for menu_slug. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | public $id = null; |
| 26 | |
| 27 | /** |
| 28 | * The text to be displayed in the title tags of the page. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | public $title = null; |
| 33 | |
| 34 | /** |
| 35 | * The slug name for the parent menu. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | public $parent = null; |
| 40 | |
| 41 | /** |
| 42 | * The The on-screen name text for the menu. |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | public $menu_title = null; |
| 47 | |
| 48 | /** |
| 49 | * The capability required for this menu to be displayed to the user. |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | public $capability = 'manage_options'; |
| 54 | |
| 55 | /** |
| 56 | * The icon for this menu. |
| 57 | * |
| 58 | * @var string |
| 59 | */ |
| 60 | public $icon = 'dashicons-art'; |
| 61 | |
| 62 | /** |
| 63 | * The position in the menu order this menu should appear. |
| 64 | * |
| 65 | * @var int |
| 66 | */ |
| 67 | public $position = -1; |
| 68 | |
| 69 | /** |
| 70 | * The function/file that displays the page content for the menu page. |
| 71 | * |
| 72 | * @var string|callable |
| 73 | */ |
| 74 | public $render = null; |
| 75 | |
| 76 | /** |
| 77 | * The function that run on page POST to save data. |
| 78 | * |
| 79 | * @var callable |
| 80 | */ |
| 81 | public $onsave = null; |
| 82 | |
| 83 | /** |
| 84 | * Hold contextual help tabs. |
| 85 | * |
| 86 | * @var array |
| 87 | */ |
| 88 | public $help = null; |
| 89 | |
| 90 | /** |
| 91 | * Hold scripts and styles. |
| 92 | * |
| 93 | * @var array |
| 94 | */ |
| 95 | public $assets = null; |
| 96 | |
| 97 | /** |
| 98 | * Check if plugin is network active. |
| 99 | * |
| 100 | * @var array |
| 101 | */ |
| 102 | public $is_network = false; |
| 103 | |
| 104 | /** |
| 105 | * Hold classes for body tag. |
| 106 | * |
| 107 | * @var array |
| 108 | */ |
| 109 | public $classes = null; |
| 110 | |
| 111 | /** |
| 112 | * The Constructor. |
| 113 | * |
| 114 | * @param string $id Admin page unique id. |
| 115 | * @param string $title Title of the admin page. |
| 116 | * @param array $config Optional. Override page settings. |
| 117 | */ |
| 118 | public function __construct( $id, $title, $config = [] ) { |
| 119 | |
| 120 | // Early bail! |
| 121 | if ( ! $id ) { |
| 122 | wp_die( esc_html__( '$id variable required' ), esc_html__( 'Variable Required' ) ); |
| 123 | } |
| 124 | |
| 125 | if ( ! $title ) { |
| 126 | wp_die( esc_html__( '$title variable required' ), esc_html__( 'Variable Required' ) ); |
| 127 | } |
| 128 | |
| 129 | $this->id = $id; |
| 130 | $this->title = $title; |
| 131 | foreach ( $config as $key => $value ) { |
| 132 | $this->$key = $value; |
| 133 | } |
| 134 | |
| 135 | if ( ! $this->menu_title ) { |
| 136 | $this->menu_title = $title; |
| 137 | } |
| 138 | |
| 139 | add_action( 'init', [ $this, 'init' ], 25 ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Init admin page when WordPress Initialises. |
| 144 | * |
| 145 | * @codeCoverageIgnore |
| 146 | */ |
| 147 | public function init() { |
| 148 | $priority = $this->parent ? intval( $this->position ) : -1; |
| 149 | add_action( $this->is_network ? 'network_admin_menu' : 'admin_menu', [ $this, 'register_menu' ], $priority ); |
| 150 | |
| 151 | // If not the page is not this page stop here. |
| 152 | if ( ! $this->is_current_page() ) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | $hooks = [ |
| 157 | 'admin_init' => [ |
| 158 | 'callback' => 'save', |
| 159 | 'condition' => ! is_null( $this->onsave ) && is_callable( $this->onsave ), |
| 160 | ], |
| 161 | 'admin_enqueue_scripts' => [ |
| 162 | 'callback' => 'enqueue', |
| 163 | 'condition' => ! empty( $this->assets ), |
| 164 | ], |
| 165 | 'admin_head' => [ |
| 166 | 'callback' => 'contextual_help', |
| 167 | 'condition' => ! empty( $this->help ), |
| 168 | ], |
| 169 | 'admin_body_class' => [ |
| 170 | 'callback' => 'body_class', |
| 171 | 'condition' => ! empty( $this->classes ), |
| 172 | ], |
| 173 | ]; |
| 174 | |
| 175 | foreach ( $hooks as $hook => $data ) { |
| 176 | if ( true === $data['condition'] ) { |
| 177 | add_action( $hook, [ $this, $data['callback'] ] ); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Register Admin Menu. |
| 184 | * |
| 185 | * @codeCoverageIgnore |
| 186 | */ |
| 187 | public function register_menu() { |
| 188 | if ( ! $this->parent ) { |
| 189 | add_menu_page( $this->title, $this->menu_title, $this->capability, $this->id, [ $this, 'display' ], $this->icon, $this->position ); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | add_submenu_page( $this->parent, $this->title, $this->menu_title, $this->capability, $this->id, [ $this, 'display' ] ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Enqueue styles and scripts. |
| 198 | * |
| 199 | * @codeCoverageIgnore |
| 200 | */ |
| 201 | public function enqueue() { |
| 202 | $this->enqueue_styles(); |
| 203 | $this->enqueue_scripts(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Add classes to <body> of WordPress admin. |
| 208 | * |
| 209 | * @codeCoverageIgnore |
| 210 | * |
| 211 | * @param string $classes Space-separated list of CSS classes. |
| 212 | * |
| 213 | * @return string |
| 214 | */ |
| 215 | public function body_class( $classes = '' ) { |
| 216 | return $classes . ' ' . join( ' ', $this->classes ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Save anything you want using onsave function. |
| 221 | * |
| 222 | * @codeCoverageIgnore |
| 223 | */ |
| 224 | public function save() { |
| 225 | call_user_func( $this->onsave, $this ); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Contextual Help. |
| 230 | * |
| 231 | * @codeCoverageIgnore |
| 232 | */ |
| 233 | public function contextual_help() { |
| 234 | $screen = get_current_screen(); |
| 235 | |
| 236 | foreach ( $this->help as $tab_id => $tab ) { |
| 237 | $tab['id'] = $tab_id; |
| 238 | $tab['content'] = $this->get_help_content( $tab ); |
| 239 | $screen->add_help_tab( $tab ); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Render admin page content using render function you passed in config. |
| 245 | * |
| 246 | * @codeCoverageIgnore |
| 247 | */ |
| 248 | public function display() { |
| 249 | if ( is_null( $this->render ) ) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | if ( is_callable( $this->render ) ) { |
| 254 | call_user_func( $this->render, $this ); |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | if ( is_string( $this->render ) ) { |
| 259 | include_once $this->render; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Is the page is currrent page. |
| 265 | * |
| 266 | * @return bool |
| 267 | */ |
| 268 | public function is_current_page() { |
| 269 | return Param::get( 'page' ) === $this->id; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Enqueue styles |
| 274 | * |
| 275 | * @codeCoverageIgnore |
| 276 | */ |
| 277 | private function enqueue_styles() { |
| 278 | if ( ! isset( $this->assets['styles'] ) || empty( $this->assets['styles'] ) ) { |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | foreach ( $this->assets['styles'] as $handle => $src ) { |
| 283 | wp_enqueue_style( $handle, $src, null ); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Enqueue scripts. |
| 289 | * |
| 290 | * @codeCoverageIgnore |
| 291 | */ |
| 292 | private function enqueue_scripts() { |
| 293 | if ( ! isset( $this->assets['scripts'] ) || empty( $this->assets['scripts'] ) ) { |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | foreach ( $this->assets['scripts'] as $handle => $src ) { |
| 298 | wp_enqueue_script( $handle, $src, null, null, true ); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Get tab content |
| 304 | * |
| 305 | * @codeCoverageIgnore |
| 306 | * |
| 307 | * @param array $tab Tab to get content for. |
| 308 | * |
| 309 | * @return string |
| 310 | */ |
| 311 | private function get_help_content( $tab ) { |
| 312 | ob_start(); |
| 313 | |
| 314 | // If it is a function. |
| 315 | if ( isset( $tab['content'] ) && is_callable( $tab['content'] ) ) { |
| 316 | call_user_func( $tab['content'] ); |
| 317 | } |
| 318 | |
| 319 | // If it is a file. |
| 320 | if ( isset( $tab['view'] ) && $tab['view'] ) { |
| 321 | require $tab['view']; |
| 322 | } |
| 323 | |
| 324 | return ob_get_clean(); |
| 325 | } |
| 326 | } |
| 327 |