[WordPress] WordPress Widgets: Refactoring, Part 8
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 | |
/* | |
* This file is part of WordPress Widget Boilerplate | |
* (c) Tom McFarlin <tom@tommcfarlin.com> | |
* | |
* This source file is subject to the GPL license that is bundled | |
* with this source code in the file LICENSE. | |
*/ | |
namespace WordPressWidgetBoilerplate\Subscriber; | |
/** | |
* An abstract implementation of a subscriber that requires a hook and the ability to | |
* start the class. | |
*/ | |
abstract class AbstractSubscriber | |
{ | |
/** | |
* @var string a reference to the hook to which the subscriber should be registered | |
*/ | |
protected $hook; | |
/** | |
* @param string $hook the hook to which the subscriber is registered | |
*/ | |
public function __construct(string $hook) | |
{ | |
$this->hook = $hook; | |
} | |
/** | |
* @return string the hook to which the subscriber is registered | |
*/ | |
public function getHook(): string | |
{ | |
return $this->hook; | |
} | |
/** | |
* Implements the domain logic for the concrete class implementating this subcriber. | |
*/ | |
abstract public function load(); | |
} |
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 | |
/* | |
* This file is part of WordPress Widget Boilerplate | |
* (c) Tom McFarlin <tom@tommcfarlin.com> | |
* | |
* This source file is subject to the GPL license that is bundled | |
* with this source code in the file LICENSE. | |
*/ | |
namespace WordPressWidgetBoilerplate\WordPress; | |
use WP_Widget; | |
class Widget extends WP_Widget | |
{ | |
/** | |
* @var string unique identifier for your widget | |
*/ | |
protected $widgetSlug; | |
/** | |
* Initializes the plugin by setting its properties and calling the parent class with the description. | |
* | |
* @param mixed $widgetSlug | |
*/ | |
public function __construct($widgetSlug) | |
{ | |
$this->widgetSlug = $widgetSlug; | |
parent::__construct( | |
$this->getWidgetSlug(), | |
__('Widget Name', $this->getWidgetSlug()), | |
[ | |
'classname' => $this->getWidgetSlug().'-class', | |
'description' => __('Short description of the widget goes here.', $this->getWidgetSlug()), | |
] | |
); | |
} | |
/** | |
* Return the widget slug. | |
* | |
* @return string slug variable | |
*/ | |
public function getWidgetSlug() | |
{ | |
return $this->widgetSlug; | |
} | |
} |
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
"autoload": { | |
"psr-4": { | |
"WordPressWidgetBoilerplate\\": "src/", | |
"WordPressWidgetBoilerplate\\Utilities\\": "src/Utilities/", | |
"WordPressWidgetBoilerplate\\Subscriber\\": "src/Subscriber/", | |
"WordPressWidgetBoilerplate\\WordPress\\": "src/WordPress/" | |
} | |
}, |
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 | |
<?php | |
namespace WordPressWidgetBoilerplate\Subscriber; | |
use WordPressWidgetBoilerplate\WordPress\Widget; | |
/** | |
* Initializes the core Widget class that's used by WordPress to instantiate the widget, | |
* renders the administrative area, provide serialization functionality, and displays the | |
* public-facing view. | |
*/ | |
class WidgetSubscriber extends AbstractSubscriber | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __construct(string $hook) | |
{ | |
parent::__construct($hook); | |
} | |
/** | |
* Registers the core Widget class using the WordPress APIs. | |
*/ | |
public function load() | |
{ | |
register_widget(new Widget('widget-name')); | |
} | |
} |
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 | |
namespace WordPressWidgetBoilerplate; | |
use WordPressWidgetBoilerplate\Utilities\Registry; | |
use WordPressWidgetBoilerplate\Plugin; | |
use WordPressWidgetBoilerplate\Subscriber\WidgetSubscriber; | |
// Prevent this file from being called directly. | |
defined('WPINC') || die; | |
// Include the autoloader. | |
require_once __DIR__ . '/vendor/autoload.php'; | |
// Setup a filter so we can retrieve the registry throughout the plugin. | |
$registry = new Registry(); | |
add_filter('wpwBoilerplateRegistry', function () use ($registry) { | |
return $registry; | |
}); | |
// Add the Widget base class to the Registry. | |
$registry->add('widgetSubscriber', new WidgetSubscriber('widgets_init')); | |
// Start the machine. | |
(new Plugin($registry))->start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment