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