| 1 |
<?php |
| 2 |
/** |
| 3 |
* The widget class of the plugin. |
| 4 |
* |
| 5 |
* @package Smart_Post_Show |
| 6 |
* @subpackage Smart_Post_Show/views/sp-framework/classes |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'SP_PC_Widget' ) ) { |
| 14 |
/** |
| 15 |
* |
| 16 |
* Widgets Class |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
* @version 1.0.0 |
| 20 |
*/ |
| 21 |
class SP_PC_Widget extends WP_Widget { |
| 22 |
|
| 23 |
/** |
| 24 |
* Unique ID/Name |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
public $unique = ''; |
| 29 |
|
| 30 |
/** |
| 31 |
* Arguments. |
| 32 |
* |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
public $args = array( |
| 36 |
'title' => '', |
| 37 |
'classname' => '', |
| 38 |
'description' => '', |
| 39 |
'width' => '', |
| 40 |
'defaults' => array(), |
| 41 |
'fields' => array(), |
| 42 |
'class' => '', |
| 43 |
); |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructor. |
| 47 |
* |
| 48 |
* @param string $key the key. |
| 49 |
* @param array $params The parameters. |
| 50 |
*/ |
| 51 |
public function __construct( $key, $params ) { |
| 52 |
|
| 53 |
$widget_ops = array(); |
| 54 |
$control_ops = array(); |
| 55 |
|
| 56 |
$this->unique = $key; |
| 57 |
$this->args = apply_filters( "spf_{$this->unique}_args", wp_parse_args( $params, $this->args ), $this ); |
| 58 |
|
| 59 |
// Set control options. |
| 60 |
if ( ! empty( $this->args['width'] ) ) { |
| 61 |
$control_ops['width'] = $this->args['width']; |
| 62 |
} |
| 63 |
|
| 64 |
// Set widget options. |
| 65 |
if ( ! empty( $this->args['description'] ) ) { |
| 66 |
$widget_ops['description'] = $this->args['description']; |
| 67 |
} |
| 68 |
|
| 69 |
if ( ! empty( $this->args['classname'] ) ) { |
| 70 |
$widget_ops['classname'] = $this->args['classname']; |
| 71 |
} |
| 72 |
|
| 73 |
// Set filters. |
| 74 |
$widget_ops = apply_filters( "spf_{$this->unique}_widget_ops", $widget_ops, $this ); |
| 75 |
$control_ops = apply_filters( "spf_{$this->unique}_control_ops", $control_ops, $this ); |
| 76 |
|
| 77 |
parent::__construct( $this->unique, $this->args['title'], $widget_ops, $control_ops ); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Register widget with WordPress. |
| 83 |
* |
| 84 |
* @param string $key The key. |
| 85 |
* @param array $params The parameters. |
| 86 |
* @return object |
| 87 |
*/ |
| 88 |
public static function instance( $key, $params = array() ) { |
| 89 |
return new self( $key, $params ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Front-end display of widget. |
| 94 |
* |
| 95 |
* @param array $args The arguments. |
| 96 |
* @param mixed $instance The widget instance. |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
public function widget( $args, $instance ) { |
| 100 |
call_user_func( $this->unique, $args, $instance ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get default value |
| 105 |
* |
| 106 |
* @param mixed $field The fields. |
| 107 |
* @param array $options The options. |
| 108 |
* @return statement |
| 109 |
*/ |
| 110 |
public function get_default( $field, $options = array() ) { |
| 111 |
|
| 112 |
$default = ( isset( $this->args['defaults'][ $field['id'] ] ) ) ? $this->args['defaults'][ $field['id'] ] : ''; |
| 113 |
$default = ( isset( $field['default'] ) ) ? $field['default'] : $default; |
| 114 |
$default = ( isset( $options[ $field['id'] ] ) ) ? $options[ $field['id'] ] : $default; |
| 115 |
|
| 116 |
return $default; |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Back-end widget form. |
| 122 |
* |
| 123 |
* @param array $instance The form instance. |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function form( $instance ) { |
| 127 |
|
| 128 |
if ( ! empty( $this->args['fields'] ) ) { |
| 129 |
|
| 130 |
$class = ( $this->args['class'] ) ? ' ' . $this->args['class'] : ''; |
| 131 |
echo '<div class="spf spf-widgets spf-fields' . esc_attr( $class ) . '">'; |
| 132 |
|
| 133 |
foreach ( $this->args['fields'] as $field ) { |
| 134 |
|
| 135 |
$field_value = ''; |
| 136 |
$field_unique = ''; |
| 137 |
|
| 138 |
if ( ! empty( $field['id'] ) ) { |
| 139 |
|
| 140 |
$field_value = $this->get_default( $field, $instance ); |
| 141 |
$field_unique = 'widget-' . $this->unique . '[' . $this->number . ']'; |
| 142 |
|
| 143 |
if ( 'title' === $field['id'] ) { |
| 144 |
$field['attributes']['id'] = 'widget-' . $this->unique . '-' . $this->number . '-title'; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
SP_PC::field( $field, $field_value, $field_unique ); |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
echo '</div>'; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Sanitize widget form values as they are saved |
| 160 |
* |
| 161 |
* @param mixed $new_instance The new instance. |
| 162 |
* @param mixed $old_instance The old instance. |
| 163 |
* @return statement |
| 164 |
*/ |
| 165 |
public function update( $new_instance, $old_instance ) { |
| 166 |
|
| 167 |
$new_instance = apply_filters( "spf_{$this->unique}_save", $new_instance, $this->args, $this ); |
| 168 |
|
| 169 |
do_action( "spf_{$this->unique}_save_before", $new_instance, $this->args, $this ); |
| 170 |
|
| 171 |
return $new_instance; |
| 172 |
|
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|