Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active Mar 13, 2019
Embed
What would you like to do?
[WordPress] WordPress Widgets: Refactoring, Part 13
<?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']; ?>-->
<?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']; ?>-->
<?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']; ?>-->
<?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');
}
<?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 [];
}
<?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