-
-
Save tommcfarlin/2f6bbe19253188596f804a7c211d9cd5 to your computer and use it in GitHub Desktop.
[WordPress] WordPress Widgets: Refactoring, Part 13
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. | |
*/ | |
?> | |
<div id="<?php echo $args['id']; ?>"> | |
<h3 class="widget-title"><?php echo $instance['title']; ?></h3> | |
<p><?php echo $instance['content']; ?></p> | |
<pre><?php echo $instance['display-title']; ?></pre> | |
</div><!-- #<?php echo $args['id']; ?>--> |
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. | |
*/ | |
?> | |
<div id="<?php echo $args['id']; ?>"> | |
<?php if (isset($instance['display-title']) && 'on' === $instance['display-title']) : ?> | |
<h3 class="widget-title"><?php echo $instance['title']; ?></h3> | |
<?php endif; ?> | |
<p><?php echo $instance['content']; ?></p> | |
</div><!-- #<?php echo $args['id']; ?>--> |
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. | |
*/ | |
?> | |
<?php if (empty($instance['title']) && empty($instance['content'])) : | |
return; | |
endif; ?> | |
<div id="<?php echo $args['id']; ?>"> | |
<?php if (isset($instance['display-title']) && 'on' === $instance['display-title']) : ?> | |
<h3 class="widget-title"><?php echo $instance['title']; ?></h3> | |
<?php endif; ?> | |
<p><?php echo $instance['content']; ?></p> | |
</div><!-- #<?php echo $args['id']; ?>--> |
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 | |
/** | |
* Caches the values for the instance of this widget. | |
* | |
* @param array $args argument provided by WordPress that may be useful in rendering the widget | |
* @param array $instance the values of the widget | |
*/ | |
private function cacheWidget($args, $instance) | |
{ | |
$cache = []; | |
$cache['widget_id'] = $args['widget_id']; | |
$cache['title'] = empty($instance['title']) ? '' : $instance['title']; | |
$cache['content'] = empty($instance['content']) ? '' : $instance['content']; | |
$instance['display-title'] = isset($instance['display-title']) ? $instance['display-title'] : ''; | |
$cache['display-title'] = $instance['display-title']; | |
wp_cache_set($this->getWidgetSlug(), $cache, 'widget'); | |
} |
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 | |
/** | |
* @return array the cached instance of this widget if it's not empty | |
*/ | |
private function getCachedWidget() | |
{ | |
$cache = wp_cache_get($this->getWidgetSlug(), 'widget'); | |
if (!empty($cache)) { | |
return $cache; | |
} | |
return []; | |
} |
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 | |
/** | |
* Displays the widget based on the contents of the included template. | |
* | |
* @param array $args argument provided by WordPress that may be useful in rendering the widget | |
* @param array $instance the values of the widget | |
*/ | |
public function widget($args, $instance) | |
{ | |
// Get a cached version of the widget. If it's empty, cache what we have. | |
$cache = $this->getCachedWidget(); | |
if (empty($cache)) { | |
$this->cacheWidget($args, $instance); | |
} | |
return $this->widgetDisplay->show($args, $instance); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment