Last active
January 16, 2019 10:01
-
-
Save markhowellsmead/6c6f0d8e63333fc86d661746a08f2b24 to your computer and use it in GitHub Desktop.
Widget class for use with the Say Hello Roots Theme for 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
{% if widget.text is not empty or widget.text is not empty or widget.image is not empty %} | |
<div class="c-widget c-widget--br-book"> | |
{% if widget.title is not empty %} | |
<div class="c-widget__header"> | |
<h1 class="c-widget__title">{{ widget.title }}</h1> | |
</div> | |
{% endif %} | |
{% if widget.image is not empty or widget.text is not empty %} | |
<div class="c-widget__content"> | |
{% if widget.image is not empty %} | |
{{ widget.image }} | |
{% endif %} | |
{% if widget.text is not empty %} | |
<div class="c-widget__text"> | |
{{ widget.text }} | |
</div> | |
{% endif %} | |
</div> | |
{% endif %} | |
</div> | |
{% endif %} |
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 SayHello\Theme\Widget; | |
use SayHello\Theme\Package\ThemeOptions; | |
use SayHello\Theme\Vendor\LazyImage; | |
use Timber\Timber; | |
/** | |
* Adds widget for user navigation. | |
*/ | |
class BRBookWidget extends \WP_Widget | |
{ | |
/** | |
* Register widget with WordPress. | |
*/ | |
public function __construct() | |
{ | |
parent::__construct( | |
'sht_br_book_widget', | |
_x('Book widget', 'Widget title text', 'sht'), | |
[ | |
'description' => _x('A widget to promote a book on Amazon', 'Widget description text', 'sht'), | |
] | |
); | |
} | |
public function run() | |
{ | |
add_action('widgets_init', [$this, 'register_widget']); | |
} | |
//This widget can then be registered in the 'widgets_init' hook: | |
// register Widget widget | |
public function register_widget() | |
{ | |
register_widget($this); | |
} | |
/** | |
* Front-end display of widget. | |
* | |
* @see WP_Widget::widget() | |
* | |
* @param array $args Widget arguments. | |
* @param array $instance Saved values from database. | |
*/ | |
public function widget($args, $instance) | |
{ | |
$context = Timber::get_context(); | |
$context = ThemeOptions::addToTimberContext($context); | |
$context['widget'] = [ | |
'title' => get_field('title', 'widget_' . $args['widget_id']), | |
'text' => get_field('text', 'widget_' . $args['widget_id']), | |
'link' => get_field('link', 'widget_' . $args['widget_id']), | |
]; | |
if ($acf_image = get_field('image', 'widget_' . $args['widget_id'])) { | |
$image_object = new LazyImage($acf_image, 'widget_br_book'); | |
$context['widget']['image'] = $image_object->get_image(); | |
} | |
Timber::render('partials/widgets/br_book.twig', $context); | |
} | |
/** | |
* Back-end widget form. | |
* | |
* @see WP_Widget::form() | |
* | |
* @param array $instance Previously saved values from database. | |
*/ | |
public function form($instance) | |
{ | |
} | |
/** | |
* Sanitize widget form values as they are saved. | |
* | |
* @see WP_Widget::update() | |
* | |
* @param array $new_instance Values just sent to be saved. | |
* @param array $old_instance Previously saved values from database. | |
* | |
* @return array Updated safe values to be saved. | |
*/ | |
public function update($new_instance, $old_instance) | |
{ | |
return $instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment