| 1 |
<?php |
| 2 |
/** |
| 3 |
* GamiPress Widget Class |
| 4 |
* |
| 5 |
* @package GamiPress\Widgets\Widget |
| 6 |
* @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class GamiPress_Widget extends WP_Widget { |
| 13 |
|
| 14 |
/** |
| 15 |
* Unique identifier for this widget. |
| 16 |
* |
| 17 |
* Will also serve as the widget class. |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $widget_slug; |
| 24 |
|
| 25 |
/** |
| 26 |
* Array of default values for widget settings. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
protected $defaults = array(); |
| 33 |
|
| 34 |
/** |
| 35 |
* Store the instance properties as property. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
protected $_instance = array(); |
| 42 |
|
| 43 |
/** |
| 44 |
* Array of CMB2 fields args. |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
* |
| 48 |
* @var array |
| 49 |
*/ |
| 50 |
protected $fields = array(); |
| 51 |
|
| 52 |
public function __construct( $widget_slug, $name, $description ) { |
| 53 |
$this->widget_slug = $widget_slug; |
| 54 |
|
| 55 |
parent::__construct( |
| 56 |
$this->widget_slug, |
| 57 |
esc_html( $name ), |
| 58 |
array( |
| 59 |
'classname' => $this->widget_slug, |
| 60 |
'customize_selective_refresh' => true, |
| 61 |
'description' => esc_html( $description ), |
| 62 |
) |
| 63 |
); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Front-end display of widget. |
| 69 |
* |
| 70 |
* @since 1.0.0 |
| 71 |
* |
| 72 |
* @param array $args The widget arguments set up when a sidebar is registered. |
| 73 |
* @param array $instance The widget settings as set by user. |
| 74 |
*/ |
| 75 |
public function widget( $args, $instance ) { |
| 76 |
|
| 77 |
global $gamipress_renderer, $gamipress_current_widget; |
| 78 |
|
| 79 |
// Setup renderer |
| 80 |
$gamipress_renderer = 'widget'; |
| 81 |
$gamipress_current_widget = $this; |
| 82 |
|
| 83 |
$atts = array( |
| 84 |
'args' => $args, |
| 85 |
'instance' => $instance, |
| 86 |
); |
| 87 |
|
| 88 |
if( ! isset( $atts['args'] ) ) { |
| 89 |
$atts['args'] = array(); |
| 90 |
} |
| 91 |
|
| 92 |
// Set up default values for attributes |
| 93 |
$atts = shortcode_atts( |
| 94 |
array( |
| 95 |
// Ensure variables |
| 96 |
'instance' => array(), |
| 97 |
'before_widget' => '', |
| 98 |
'after_widget' => '', |
| 99 |
'before_title' => '', |
| 100 |
'after_title' => '', |
| 101 |
), |
| 102 |
$args, |
| 103 |
$this->widget_slug |
| 104 |
); |
| 105 |
|
| 106 |
$this->setup_fields(); |
| 107 |
|
| 108 |
$instance = shortcode_atts( |
| 109 |
$this->defaults, |
| 110 |
$instance, |
| 111 |
$this->widget_slug |
| 112 |
); |
| 113 |
|
| 114 |
// Let's render the widget |
| 115 |
$widget = ''; |
| 116 |
|
| 117 |
// Before widget hook |
| 118 |
$widget .= $atts['before_widget']; |
| 119 |
|
| 120 |
// Title |
| 121 |
$widget .= ( $instance['title'] ) ? $atts['before_title'] . esc_html( $instance['title'] ) . $atts['after_title'] : ''; |
| 122 |
|
| 123 |
ob_start(); |
| 124 |
|
| 125 |
// {$widget_slug}_before |
| 126 |
do_action( $this->widget_slug . '_before' ); |
| 127 |
|
| 128 |
// Widget content |
| 129 |
$this->get_widget( $args, $instance ); |
| 130 |
|
| 131 |
// {$widget_slug}_after |
| 132 |
do_action( $this->widget_slug . '_after' ); |
| 133 |
|
| 134 |
$widget .= ob_get_clean(); |
| 135 |
|
| 136 |
// After widget hook |
| 137 |
$widget .= $atts['after_widget']; |
| 138 |
|
| 139 |
// Reset renderer |
| 140 |
$gamipress_renderer = false; |
| 141 |
$gamipress_current_widget = null; |
| 142 |
|
| 143 |
echo $widget; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Return the widget/shortcode output |
| 148 |
* |
| 149 |
* @since 1.0.0 |
| 150 |
* |
| 151 |
* @param array $args The widget arguments set up when a sidebar is registered. |
| 152 |
* @param array $instance The widget settings as set by user. |
| 153 |
* @return string The widget output. |
| 154 |
*/ |
| 155 |
public function get_widget( $args, $instance ) { |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* The widget tabs. |
| 161 |
* |
| 162 |
* @since 1.0.0 |
| 163 |
* |
| 164 |
* @return array |
| 165 |
*/ |
| 166 |
public function get_tabs() { |
| 167 |
return array(); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* The widget form fields. |
| 172 |
* |
| 173 |
* @since 1.0.0 |
| 174 |
* |
| 175 |
* @return array |
| 176 |
*/ |
| 177 |
public function get_fields() { |
| 178 |
return array(); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Setup the widget form fields. |
| 183 |
* |
| 184 |
* @since 1.0.0 |
| 185 |
* |
| 186 |
* @return array |
| 187 |
*/ |
| 188 |
public function setup_fields() { |
| 189 |
// Set up fields |
| 190 |
$this->fields = $this->get_fields(); |
| 191 |
|
| 192 |
if( ! isset( $this->fields['title'] ) ) { |
| 193 |
$this->fields = array( |
| 194 |
'title' => array( |
| 195 |
'name' => __( 'Title:', 'gamipress' ), |
| 196 |
'type' => 'text', |
| 197 |
'default' => '', |
| 198 |
), |
| 199 |
) + $this->fields; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Available filter to extend the widget form fields |
| 204 |
* |
| 205 |
* @since 1.9.9.2 |
| 206 |
* |
| 207 |
* @param array $fields |
| 208 |
* @param array $widget_slug |
| 209 |
* |
| 210 |
* @return array |
| 211 |
*/ |
| 212 |
$this->fields = apply_filters( 'gamipress_setup_widget_fields', $this->fields, $this->widget_slug ); |
| 213 |
|
| 214 |
$this->defaults = array(); |
| 215 |
|
| 216 |
// Set up fields defaults |
| 217 |
foreach( $this->fields as $field_id => $field ) { |
| 218 |
$this->defaults[$field_id] = ( isset( $field['default'] ) ? $field['default'] : '' ); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Back-end widget form with defaults. |
| 224 |
* |
| 225 |
* @param array $instance Current settings. |
| 226 |
* @return string Default return is 'noform'. |
| 227 |
*/ |
| 228 |
public function form( $instance ) { |
| 229 |
|
| 230 |
$this->setup_fields(); |
| 231 |
|
| 232 |
if( count( $this->fields ) > 0 ) { |
| 233 |
// If there are no settings, set up defaults |
| 234 |
$this->_instance = wp_parse_args( (array) $instance, $this->defaults); |
| 235 |
|
| 236 |
$cmb2 = $this->cmb2(); |
| 237 |
|
| 238 |
$cmb2->object_id( $this->option_name ); |
| 239 |
CMB2_Hookup::enqueue_cmb_css(); |
| 240 |
CMB2_Hookup::enqueue_cmb_js(); |
| 241 |
$cmb2->show_form(); |
| 242 |
|
| 243 |
return ''; |
| 244 |
} |
| 245 |
|
| 246 |
return 'noform'; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Update form values as they are saved. |
| 251 |
* |
| 252 |
* @param array $new_instance New settings for this instance as input by the user. |
| 253 |
* @param array $old_instance Old settings for this instance. |
| 254 |
* @return array Settings to save or bool false to cancel saving. |
| 255 |
*/ |
| 256 |
public function update( $new_instance, $old_instance ) { |
| 257 |
$this->setup_fields(); |
| 258 |
|
| 259 |
$sanitized = $this->cmb2( true )->get_sanitized_values( $new_instance ); |
| 260 |
|
| 261 |
return $sanitized; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Creates a new instance of CMB2 and adds the fields |
| 266 |
* |
| 267 |
* @since 1.0.0 |
| 268 |
* |
| 269 |
* @return CMB2 |
| 270 |
*/ |
| 271 |
public function cmb2( $saving = false ) { |
| 272 |
|
| 273 |
$tabs = $this->get_tabs(); |
| 274 |
|
| 275 |
foreach ( $tabs as $tab_id => $tab ) { |
| 276 |
// Generate the id of the tab based on shortcode slug |
| 277 |
$tab['id'] = $this->get_field_name( $tab_id ); |
| 278 |
|
| 279 |
foreach( $tab['fields'] as $tab_field_index => $tab_field ) { |
| 280 |
// Update the id of the tab field based on shortcode slug |
| 281 |
$tab['fields'][$tab_field_index] = $this->get_field_name( $tab_field ); |
| 282 |
} |
| 283 |
|
| 284 |
$tabs[$tab_id] = $tab; |
| 285 |
} |
| 286 |
|
| 287 |
// Create a new box in the class |
| 288 |
$cmb2 = new CMB2( array( |
| 289 |
'id' => $this->option_name .'_box', // Option name is taken from the WP_Widget class. |
| 290 |
'classes' => 'gamipress-form gamipress-widget-form', |
| 291 |
'tabs' => $tabs, |
| 292 |
'hookup' => false, |
| 293 |
'show_on' => array( |
| 294 |
'key' => 'options-page', |
| 295 |
'value' => array( $this->option_name ) |
| 296 |
), |
| 297 |
), $this->option_name ); |
| 298 |
|
| 299 |
foreach ( $this->fields as $field_id => $field ) { |
| 300 |
$field['id'] = $field_id; |
| 301 |
$field['id_key'] = $field_id; // Saves the given id into a custom parameter to use it as the real field id |
| 302 |
|
| 303 |
if ( ! $saving ) { |
| 304 |
$field['id'] = $this->get_field_name( $field_id ); |
| 305 |
} |
| 306 |
|
| 307 |
$field['default_cb'] = array( $this, 'default_cb' ); |
| 308 |
|
| 309 |
// Remove default to make default_cb work |
| 310 |
if( isset( $field['default'] ) ) { |
| 311 |
unset( $field['default'] ); |
| 312 |
} |
| 313 |
|
| 314 |
$cmb2->add_field( $field ); |
| 315 |
} |
| 316 |
|
| 317 |
return $cmb2; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Sets the field default, or the field value. |
| 322 |
* |
| 323 |
* @param array $field_args CMB2 field args array |
| 324 |
* @param CMB2_Field $field CMB2 Field object. |
| 325 |
* |
| 326 |
* @return mixed Field value. |
| 327 |
*/ |
| 328 |
public function default_cb( $field_args, $field ) { |
| 329 |
|
| 330 |
if( $field->args( 'type' ) === 'checkbox' ) { |
| 331 |
return isset( $this->_instance[ $field->args( 'id_key' ) ] ) |
| 332 |
&& $this->_instance[ $field->args( 'id_key' ) ] === 'on' |
| 333 |
? 'on' |
| 334 |
: ''; |
| 335 |
} |
| 336 |
|
| 337 |
return isset( $this->_instance[ $field->args( 'id_key' ) ] ) |
| 338 |
? $this->_instance[ $field->args( 'id_key' ) ] |
| 339 |
: null; |
| 340 |
} |
| 341 |
|
| 342 |
} |