|
<?php |
|
|
|
/** |
|
* Generic sidebar widget that contains a title and text field. |
|
* |
|
* See "Text widget class" in `/wp-includes/default-widgets.php`. |
|
* |
|
* @todo Option to make this a single-instance widget. |
|
* @todo Need constant that allows user to pick template base location. |
|
* @todo Make more like WPAlchemy. |
|
* |
|
* @see http://wordpress.stackexchange.com/questions/32103 |
|
* @see http://wordpress.stackexchange.com/a/1834/32387 |
|
* @see http://wordpress.stackexchange.com/questions/104077 |
|
* @see https://github.com/farinspace/wpalchemy |
|
*/ |
|
|
|
class Foo_Widget extends WP_Widget |
|
{ |
|
|
|
private $template = ''; |
|
private $style_admin = array(); |
|
private $script_admin = array(); |
|
private $style = array(); |
|
private $script = array(); |
|
|
|
public function __construct($arr = array()) { |
|
|
|
if (is_array($arr)) { |
|
|
|
# http://stackoverflow.com/a/8329005/922323 |
|
$arr = array_filter($arr); |
|
|
|
if ( ! empty($arr)) { |
|
|
|
$args = wp_parse_args( |
|
$arr, |
|
array( |
|
'id_base' => '', |
|
'name' => '', |
|
'template' => '', |
|
'description' => '', |
|
'width' => '', |
|
'height' => '', |
|
'style_admin' => array(), |
|
'script_admin' => array(), |
|
'style' => array(), |
|
'script' => array(), |
|
) |
|
); |
|
|
|
if (( ! empty($args['id_base'])) && ( ! is_numeric($args['id_base']))) { |
|
|
|
if ( ! empty($args['template'])) { |
|
|
|
parent::__construct( |
|
|
|
$args['id_base'], |
|
$args['name'], |
|
array( |
|
'classname' => __CLASS__, |
|
'description' => $args['description'], |
|
), |
|
array( |
|
'width' => $args['width'], |
|
'height' => $args['height'], |
|
) |
|
|
|
); |
|
|
|
$this->template = $args['template']; |
|
|
|
if ( ! empty($args['style_admin'])) { |
|
|
|
$this->style_admin = $args['style_admin']; |
|
|
|
add_action('admin_enqueue_scripts', array($this, 'add_styles')); |
|
|
|
} |
|
|
|
if ( ! empty($args['script_admin'])) { |
|
|
|
$this->script_admin = $args['script_admin']; |
|
|
|
add_action('admin_enqueue_scripts', array($this, 'add_scripts')); |
|
|
|
} |
|
|
|
if ( ! empty($args['style'])) { |
|
|
|
$this->style = $args['style']; |
|
|
|
add_action('wp_enqueue_scripts', array($this, 'add_styles')); |
|
|
|
} |
|
|
|
if ( ! empty($args['script'])) { |
|
|
|
$this->script = $args['script']; |
|
|
|
add_action('wp_enqueue_scripts', array($this, 'add_styles')); |
|
|
|
} |
|
|
|
add_action( |
|
'widgets_init', |
|
function() { |
|
register_widget(__CLASS__); |
|
} |
|
); |
|
|
|
} else { |
|
|
|
//die('Widget template file required.'); |
|
|
|
} |
|
|
|
} else { |
|
|
|
//die('Widget ID required and must be a string.'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
} |
|
|
|
public function update($new_instance, $old_instance) { |
|
|
|
$instance = $old_instance; |
|
|
|
$instance['title'] = strip_tags($new_instance['title']); |
|
$instance['text'] = $new_instance['text']; // Unfiltered HTML. |
|
|
|
return $instance; |
|
|
|
} |
|
|
|
public function form($instance) { |
|
|
|
$instance = wp_parse_args((array) $instance, array('title' => '', 'text' => '',)); |
|
|
|
$title = strip_tags($instance['title']); |
|
$text = esc_textarea($instance['text']); |
|
|
|
?> |
|
|
|
<p> |
|
<label for="<?=$this->get_field_id('title')?>">Title:</label> |
|
<input type="text" name="<?=$this->get_field_name('title')?>" id="<?=$this->get_field_id('title')?>" class="widefat" value="<?=esc_attr($title)?>"> |
|
</p> |
|
|
|
<textarea rows="16" cols="20" name="<?=$this->get_field_name('text')?>" id="<?=$this->get_field_id('text')?>" class="widefat"><?=$text?></textarea> |
|
|
|
<?php |
|
|
|
} |
|
|
|
/** |
|
* @see http://wordpress.stackexchange.com/a/4471/32387 |
|
*/ |
|
|
|
public function widget($args, $instance) { |
|
|
|
extract($args); |
|
|
|
$title = apply_filters( |
|
'widget_title', // The name (`$tag`) of the filter hook. |
|
(empty($instance['title']) ? '' : $instance['title']), // The value which the filters hooked to `$tag` may modify. |
|
$instance, // Additional variable passed to the filter functions. |
|
$this->id_base // IBID. |
|
); |
|
|
|
$text = apply_filters( |
|
'widget_text', |
|
(empty($instance['text']) ? '' : $instance['text']), |
|
$instance |
|
); |
|
|
|
if ($text != '') { |
|
|
|
echo $text; |
|
|
|
} else { |
|
|
|
# Remember: Arguments will be available to included template. |
|
include($this->template); |
|
|
|
} |
|
|
|
} |
|
|
|
public function add_styles() { |
|
|
|
$defaults = array( |
|
'handle' => '', |
|
'src' => '', |
|
'deps' => array(), |
|
'version' => false, |
|
'media' => 'all', |
|
); |
|
|
|
return $this->add_files('style', $defaults); |
|
|
|
} |
|
|
|
public function add_scripts() { |
|
|
|
$defaults = array( |
|
'handle' => '', |
|
'src' => '', |
|
'deps' => array(), |
|
'version' => false, |
|
'in_footer' => false, |
|
); |
|
|
|
return $this->add_files('script', $defaults); |
|
|
|
} |
|
|
|
private function add_files($kind = '', $defaults = array()) { |
|
|
|
$return = FALSE; |
|
|
|
if ($kind) { |
|
|
|
$files = (is_admin()) ? $this->{ $kind . '_admin' } : $this->{ $kind }; |
|
|
|
if ( ! empty($files)) { |
|
|
|
foreach($files as $file) { |
|
|
|
$args = wp_parse_args($file, $defaults); |
|
|
|
call_user_func_array( |
|
|
|
('wp_enqueue_' . $kind), |
|
array( |
|
$args['handle'], |
|
$args['src'], |
|
$args['deps'], |
|
$args['version'], |
|
((array_key_exists('media', $args)) ? $args['media'] : $args['in_footer']), |
|
) |
|
|
|
); |
|
|
|
} |
|
|
|
$return = TRUE; |
|
|
|
} |
|
|
|
} |
|
|
|
return $return; |
|
|
|
} |
|
|
|
} |
Well, the undefined property notice is pretty straightforward. You need to define it in
foo_Widget {}
. Hint: Look at line 89 in wp-includes/widgets.php.