-
-
Save tommcfarlin/392a2eda9ebc0b0a59cd4b364e427445 to your computer and use it in GitHub Desktop.
[WordPress] WordPress Widgets: Refactoring, Part 10
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 | |
/** | |
* If the value for the key exists in the current instance of the widget, then it will | |
* retrieve it. Otherwise, it will return an empty value. | |
* | |
* @param string $key the used to identify the value of the widget. | |
* @param array $instance the options for the instance of this widget | |
*/ | |
protected function get($key, $instance) | |
{ | |
return empty($instance[$key]) ? '' : $instance[$key]; | |
} |
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 class="widget-content"> | |
<p> | |
<input | |
type="text" | |
id="<?php echo esc_attr($this->get_field_id('title')); ?>" | |
name="<?php echo esc_attr($this->get_field_name('title')); ?>" | |
value="<?php echo $this->get('title', $instance) ?>" | |
placeholder="Widget Title" | |
class="widefat" | |
/> | |
</p> | |
<p> | |
<textarea | |
id="<?php echo esc_attr($this->get_field_id('content')); ?>" | |
name="<?php echo esc_attr($this->get_field_name('content')); ?>" | |
placeholder="Widget Content" | |
style="width:100%;"><?php echo $this->get('content', $instance) ?></textarea> | |
</p> | |
<p> | |
<input | |
type="checkbox" | |
value="on" | |
name="<?php echo esc_attr($this->get_field_name('display-title')); ?>" | |
id="<?php echo esc_attr($this->get_field_id('display-title')); ?>" | |
<?php checked('on', $this->get('display-title', $instance), true); ?> | |
class="checkbox" | |
/> | |
<label for="<?php echo esc_attr($this->get_field_id('display-title')); ?>">Display Title?</label> | |
</p> | |
</div><!-- .widget-content --> |
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 | |
{ | |
/** | |
* 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) | |
); | |
} | |
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 | |
/* | |
* 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 string $widgetSlug unique identifier for your widget | |
* @param WidgetSerializer $widgetSerializer the class responsible for saving widget options | |
*/ | |
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; | |
} | |
/** | |
* Displays the administrative view of the form and includes the options | |
* for the instance of the widget as arguments passed into the function. | |
* | |
* @param array $instance the options for the instance of this widget | |
*/ | |
public function form($instance) | |
{ | |
include plugin_dir_path(__FILE__).'Views/Admin.php'; | |
} | |
/** | |
* Updates the values of the widget. Uses the serialization class to sanitize the | |
* information before saving it. | |
* | |
* @param array $newInstance the values to be sanitized and saved | |
* @param array $oldInstance the values that were originally saved | |
*/ | |
public function update($newInstance, $oldInstance) | |
{ | |
return $this->widgetSerializer->update($newInstance, $oldInstance); | |
} | |
/** | |
* If the value for the key exists in the current instance of the widget, then it will | |
* retrieve it. Otherwise, it will return an empty value. | |
* | |
* @param string $key the used to identify the value of the widget | |
* @param array $instance the options for the instance of this widget | |
*/ | |
protected function get($key, $instance) | |
{ | |
return empty($instance[$key]) ? '' : $instance[$key]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment