-
-
Save tommcfarlin/39038338730b7141a19c3d2f48801dac to your computer and use it in GitHub Desktop.
[WordPress] WordPress Widgets: Refactoring, Part 11
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
<script type="text/javascript">This is the Title</script> |
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 | |
/* | |
* 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; | |
/** | |
* Deletes the cached contents of the widget. | |
*/ | |
class DeleteWidgetCacheSubscriber extends AbstractSubscriber | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __construct(string $hook) | |
{ | |
parent::__construct($hook); | |
} | |
/** | |
* Flushes the widget's cache based on the key that's specified in the function arguments. | |
*/ | |
public function load() | |
{ | |
/* Because we're implementing an abstract class, we'll parse arguments from the | |
* func_get_args(). | |
*/ | |
$args = \func_get_args(); | |
if (!$this->hasValidArguments($args)) { | |
return; | |
} | |
// TODO: More to come... | |
} | |
/** | |
* Verifies that we have valid arguments with which to work. | |
* | |
* @param array $args the array of arguments we are validating | |
* | |
* @return bool true if the arguments are valid; otherwise, false | |
*/ | |
private function hasValidArguments(array $args): bool | |
{ | |
// First, check the initial index of the arguments. | |
if (!isset($args[0])) { | |
return false; | |
} | |
// Next, check the values of the arguments for the widget key and group. | |
$args = $args[0]; | |
if (!isset($args[0]) && !isset($args[1])) { | |
return false; | |
} | |
return true; | |
} | |
} |
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 | |
/** | |
* WordPress Widget Boilerplate | |
* | |
* The WordPress Widget Boilerplate is an organized, maintainable boilerplate for building | |
* widgets using WordPress best practices. | |
* | |
* @package WordPressWidgetBoilerplate | |
* @author Your Name <email@example.com> | |
* @license GPL-3.0+ | |
* @link http://example.com | |
* @copyright 2018 - 2019 Your Name or Company Name | |
* | |
* @wordpress-plugin | |
* Plugin Name: WordPress Widget Boilerplate | |
* Plugin URI: https://github.com/tommcfarlin/wordpress-widget-boilerplate | |
* Description: An object-oriented foundation for building WordPress Widgets. | |
* Version: 1.0.0 | |
* Author: Tom McFarlin | |
* Author URI: https://tommcfarlin.com | |
* Text Domain: widget-name | |
* License: GPL-3.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt | |
* Domain Path: /lang | |
*/ | |
namespace WordPressWidgetBoilerplate; | |
use WordPressWidgetBoilerplate\Utilities\Registry; | |
use WordPressWidgetBoilerplate\Plugin; | |
use WordPressWidgetBoilerplate\Subscriber\WidgetSubscriber; | |
use WordPressWidgetBoilerplate\Subscriber\DeleteWidgetCacheSubscriber; | |
// 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 subscribers. | |
$registry->add('deleteWidgetCacheSubscriber', new DeleteWidgetCacheSubscriber('flush_widget_cache')); | |
// Add the Widget base class to the Registry. | |
$registry->add('widgetSubscriber', new WidgetSubscriber('widgets_init')); | |
// Start the machine. | |
(new Plugin($registry))->start(); |
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; | |
/** | |
* Santiizes and saves the data for the widget. | |
*/ | |
class WidgetSerializer | |
{ | |
/** | |
* @var string a reference to the slug of the widget to which the serialier is associated | |
*/ | |
private $widgetSlug; | |
/** | |
* Initializes the class. | |
* | |
* @param string a reference to the slug of the widget to which the serialier is associated | |
*/ | |
public function __construct(string $widgetSlug) | |
{ | |
$this->widgetSlug = $widgetSlug; | |
} | |
/** | |
* Updates the values of the widget. Sanitizes the information before saving it. | |
* | |
* @param array $newInstance the array of new options to save | |
*/ | |
public function update($newInstance) | |
{ | |
$instance = []; | |
foreach ($newInstance as $key => $value) { | |
$instance[$key] = strip_tags( | |
stripslashes($value) | |
); | |
} | |
do_action('flush_widget_cache', [$this->widgetSlug, 'widget']); | |
return $instance; | |
} | |
} |
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 | |
/* | |
* 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; | |
/** | |
* Deletes the cached contents of the widget. | |
*/ | |
class DeleteWidgetCacheSubscriber extends AbstractSubscriber | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __construct(string $hook) | |
{ | |
parent::__construct($hook); | |
} | |
/** | |
* Flushes the widget's cache based on the key that's specified in the function arguments. | |
*/ | |
public function load() | |
{ | |
/* Because we're implementing an abstract class, we'll parse arguments from the | |
* func_get_args(). | |
*/ | |
$args = \func_get_args(); | |
if (!$this->hasValidArguments($args)) { | |
return; | |
} | |
$args = $args[0]; | |
wp_cache_delete($args[0], $args[1]); | |
} | |
/** | |
* Verifies that we have valid arguments with which to work. | |
* | |
* @param array $args the array of arguments we are validating | |
* | |
* @return bool true if the arguments are valid; otherwise, false | |
*/ | |
private function hasValidArguments(array $args): bool | |
{ | |
// First, check the initial index of the arguments. | |
if (!isset($args[0])) { | |
return false; | |
} | |
// Next, check the values of the arguments for the widget key and group. | |
$args = $args[0]; | |
if (!isset($args[0]) && !isset($args[1])) { | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment