Plugin code design review
-
Hello!
I spent the last two days reading tons of tutorials for WordPress plugin architecture. The fact that WordPress has been heavily used for so many years results, as you will probably know, in a ton of outdated tutorials. This is not made better by the fact that PHP has evolved from PHP 4.x to now PHP 7.
Long story short: I am a bit unsure whether what I figured out works for the plugin project I have is really the best way to write a plugin. Creating small but complex bits of code is no problem, but getting the whole “architecture” set up is something I still struggle with. Anyway, I wanted to try to create my complete own system instead of copying together pieces of code from somewhere else. If you could take a look on what I figured out so far and tell me what you think could be improved (or remade), I would be more than happy!
A small side-information: The plugin will only extend the admin interface functionality.
File Structure (in plugin-folder):
- admin
- partials
- partial.header.php
- partial.footer.php
- screens
- screen.overview.php
- screen.anotherpage.php
- screen.settings.php
- templates
- template.overview.php
- template.anotherpage.pgp
- template.settings.php
- includes
- class.Demoname.php
- class.DemonamePath.php
- class.DemonameScreen.php
- languages
- demoname-de_DE.mo
- demoname-de_DE.po
- …
- demoname.php
Here is what happens:
demoname.php in the root-folder does nothing more than include the class.Demoname.php from the “includes”-folder. The
class Demoname { }
is a singleton-class (I’ve read about the pros and cons and decided to use a singleton). The demoname.php also callsDemoname::getInstance()
the first time to ‘create’ the plugin.This main classes constructor hooks many of its sibling functions to the corresponding WordPress actions. So
Demoname::internationalization
for example is hooked to ‘plugins_loaded’ and executesload_plugin_textdomain()
.The function
Demoname::createAdminScreens()
, hookd to ‘init’, automatically includes all files from admin/screens/ and automatically creates an object for the class contained in each file.Those screen.*.php files contain a class looking like this:
class ScreenOverview extends DemonameScreen { protected function setup() { $this->setTitle(__('Overview', 'demoname')); $this->setSlug('overview'); } }
The DemonameScreen class, which every screen extends, looks like this:
abstract class DemonameScreen { private $name; private $slug; final public function __construct() { $this->setup(); add_action('admin_menu', array($this, 'createMenuPage'); } abstract protected function setup(); final protected function setTitle($newTitle) { $this->title = $newTitle; } final protected function setSlug($newSlug) { $this->slug = $newSlug; } final protected function createMenuPage() { add_submenu_page( ** Use $this->name and $this->slug to create submenu-page of my plugins main menu page slug, callback is array($this, 'show') ** ); } final public function show() { ** include partial header ** ** include template/template. $this->slug .php ** ** include partial footer } }
Parts in stars are pseudo-code since I know it is working and I wanted to simplify it to be understandable without the rest of my code.
The whole code is working right now, I can add “screens” to my plugin and the templates are loaded and shown on the admin backend accordingly. But yet it doesn’t “feel good” and I can’t tell why. Do you have any suggestions for improvement?
Thanks a lot in advance for your help!
- The topic ‘Plugin code design review’ is closed to new replies.