1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
<?php
namespace hji\geofarms\widgets;
use \hji\geofarms\GeoFarms;
use \hji\common\utils\Views;
class Display extends \WP_Widget {
private $view;
function __construct()
{
parent::__construct
(
'gf_display_widget',
'HJI ' . GeoFarms::NAME,
array('description' => 'Easily display your ' . GeoFarms::NAME . ' in a sidebar anywhere on your site!',)
);
$this->view = new Views(GeoFarms::$views);
}
function widget($args, $instance)
{
foreach($instance as $k => $v)
{
$instance[$k] = esc_attr($v);
}
$ids = $instance['ids'] ? explode(',', $instance['ids']) : '';
echo $args['before_widget'];
if(!empty($instance['title']))
{
echo $args['before_title'] . apply_filters('widget_title', $instance['title']). $args['after_title'];
}
hjigf_display($ids, $instance['tags'], $instance['sort'], array('w' => $instance['img_w'], 'h' => $instance['img_h'], 'classes' => $instance['img_class']), strtolower($instance['display_type']), $instance['grid_size']);
echo $args['after_widget'];
}
function form($instance)
{
$fields = array_merge(
$this->addField('title', $instance),
$this->addField('ids', $instance),
$this->addField('tags', $instance),
$this->addField('sort', $instance),
$this->addField('display_type', $instance),
$this->addField('grid_size', $instance),
$this->addField('img_w', $instance),
$this->addField('img_h', $instance),
$this->addField('img_class', $instance)
);
$vars['field'] = $fields;
$vars['instance'] = $instance;
$vars['sortOptions'] = array(
'No order' => 'none',
'Random' => 'rand',
'Area ID' => 'ID',
'Area Title' => 'title',
'Area Name (Slug)' => 'name',
'Date' => 'date',
'Modified Date' => 'modified',
'Menu Order' => 'menu_order',
'ID List Above' => 'post__in',
);
$vars['displayOptions'] = array('List', 'Grid');
$vars['gridSize'] = array(
'1' => '12',
'2' => '6',
'3' => '4',
'4' => '3'
);
echo $this->view->render('widget-display-admin', $vars);
}
function addField($fieldName, $instance)
{
$field[$fieldName]['id'] = $this->get_field_id($fieldName);
$field[$fieldName]['name'] = $this->get_field_name($fieldName);
$field[$fieldName]['value'] = (isset($instance[$fieldName])) ? $instance[$fieldName] : false;
return $field;
}
function update($new, $old)
{
$values = array();
$values['title'] = sanitize_text_field($new['title']);
$values['ids'] = sanitize_text_field($new['ids']);
$values['tags'] = sanitize_text_field($new['tags']);
$values['sort'] = sanitize_text_field($new['sort']);
$values['display_type'] = sanitize_text_field($new['display_type']);
$values['grid_size'] = sanitize_text_field($new['grid_size']);
if(is_numeric($new['img_w']))
{
$values['img_w'] = absint(sanitize_text_field($new['img_w']));
}
else
{
$values['img_w'] = sanitize_text_field($old['img_w']);
}
if(is_numeric($new['img_h']))
{
$values['img_h'] = absint(sanitize_text_field($new['img_h']));
}
else
{
$values['img_h'] = sanitize_text_field($old['img_h']);
}
$values['img_class'] = sanitize_text_field($new['img_class']);
return $values;
}
}