| 1 |
<?php |
| 2 |
/** |
| 3 |
* Widget base |
| 4 |
* |
| 5 |
* @package wpstream-theme |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'Wpstream_Widget_Base' ) ) { |
| 14 |
/** |
| 15 |
* Base class for custom widgets. |
| 16 |
*/ |
| 17 |
class Wpstream_Widget_Base extends WP_Widget { |
| 18 |
/** |
| 19 |
* Settings. |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
public $settings; |
| 24 |
|
| 25 |
/** |
| 26 |
* The method return an array of class names to be deleted. |
| 27 |
* |
| 28 |
* @return array |
| 29 |
*/ |
| 30 |
public static function get_widgets_for_unregister(): array { |
| 31 |
return array(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Updates a particular instance of a widget. |
| 36 |
* |
| 37 |
* @param array $new_instance New instance. |
| 38 |
* @param array $old_instance Old instance. |
| 39 |
* |
| 40 |
* @return array |
| 41 |
* @see WP_Widget->update |
| 42 |
*/ |
| 43 |
public function update( $new_instance, $old_instance ) { |
| 44 |
$instance = $old_instance; |
| 45 |
|
| 46 |
if ( empty( $this->settings ) ) { |
| 47 |
return $instance; |
| 48 |
} |
| 49 |
|
| 50 |
// Loop settings and get values to save. |
| 51 |
foreach ( $this->settings as $key => $setting ) { |
| 52 |
if ( ! isset( $setting['type'] ) ) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
// Format the value based on settings type. |
| 57 |
switch ( $setting['type'] ) { |
| 58 |
case 'number': |
| 59 |
$instance[ $key ] = absint( $new_instance[ $key ] ); |
| 60 |
|
| 61 |
if ( isset( $setting['min'] ) && '' !== $setting['min'] ) { |
| 62 |
$instance[ $key ] = max( $instance[ $key ], $setting['min'] ); |
| 63 |
} |
| 64 |
|
| 65 |
if ( isset( $setting['max'] ) && '' !== $setting['max'] ) { |
| 66 |
$instance[ $key ] = min( $instance[ $key ], $setting['max'] ); |
| 67 |
} |
| 68 |
break; |
| 69 |
case 'textarea': |
| 70 |
$instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) ); |
| 71 |
break; |
| 72 |
case 'checkbox': |
| 73 |
$instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1; |
| 74 |
break; |
| 75 |
default: |
| 76 |
$instance[ $key ] = isset( $new_instance[ $key ] ) ? sanitize_text_field( $new_instance[ $key ] ) : $setting['std']; |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return $instance; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Outputs the settings update form. |
| 86 |
* |
| 87 |
* @param array $instance Instance. |
| 88 |
* |
| 89 |
* @see WP_Widget->form |
| 90 |
*/ |
| 91 |
public function form( $instance ) { |
| 92 |
if ( empty( $this->settings ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
foreach ( $this->settings as $key => $setting ) { |
| 97 |
if ( ! isset( $setting['type'] ) ) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
|
| 101 |
$class = $setting['class'] ?? ''; |
| 102 |
$value = $instance[ $key ] ?? ( $setting['std'] ?? '' ); |
| 103 |
|
| 104 |
switch ( $setting['type'] ) { |
| 105 |
case 'text': |
| 106 |
?> |
| 107 |
<p> |
| 108 |
<label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label> |
| 109 |
<?php |
| 110 |
// phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped |
| 111 |
?> |
| 112 |
<input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="text" value="<?php echo esc_attr( $value ); ?>"/> |
| 113 |
</p> |
| 114 |
<?php |
| 115 |
break; |
| 116 |
|
| 117 |
case 'number': |
| 118 |
?> |
| 119 |
<p> |
| 120 |
<label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo esc_html( $setting['label'] ); ?></label> |
| 121 |
<input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="number" step="<?php echo esc_attr( $setting['step'] ); ?>" min="<?php echo esc_attr( $setting['min'] ); ?>" max="<?php echo esc_attr( $setting['max'] ); ?>" value="<?php echo esc_attr( $value ); ?>"/> |
| 122 |
</p> |
| 123 |
<?php |
| 124 |
break; |
| 125 |
|
| 126 |
case 'select': |
| 127 |
?> |
| 128 |
<p> |
| 129 |
<label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo esc_html( $setting['label'] ); ?></label> |
| 130 |
<select class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>"> |
| 131 |
<?php foreach ( $setting['options'] as $option_key => $option_value ) : ?> |
| 132 |
<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option> |
| 133 |
<?php endforeach; ?> |
| 134 |
</select> |
| 135 |
</p> |
| 136 |
<?php |
| 137 |
break; |
| 138 |
|
| 139 |
case 'textarea': |
| 140 |
?> |
| 141 |
<p> |
| 142 |
<label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo esc_html( $setting['label'] ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></label> |
| 143 |
<textarea class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" cols="20" rows="3"><?php echo esc_textarea( $value ); ?></textarea> |
| 144 |
<?php if ( isset( $setting['desc'] ) ) : ?> |
| 145 |
<small><?php echo esc_html( $setting['desc'] ); ?></small> |
| 146 |
<?php endif; ?> |
| 147 |
</p> |
| 148 |
<?php |
| 149 |
break; |
| 150 |
|
| 151 |
case 'checkbox': |
| 152 |
?> |
| 153 |
<p> |
| 154 |
<input class="checkbox <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="checkbox" value="1" <?php checked( $value, 1 ); ?> /> |
| 155 |
<label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo esc_html( $setting['label'] ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></label> |
| 156 |
</p> |
| 157 |
<?php |
| 158 |
break; |
| 159 |
|
| 160 |
// Default: run an action. |
| 161 |
default: |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Output the html at the start of a widget. |
| 169 |
* |
| 170 |
* @param array $args Arguments. |
| 171 |
* @param array $instance Instance. |
| 172 |
*/ |
| 173 |
public function widget_start( $args, $instance ) { |
| 174 |
echo wp_kses_post( $args['before_widget']); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 175 |
|
| 176 |
$title = ''; |
| 177 |
|
| 178 |
if ( isset( $this->settings, $this->settings['title'], $this->settings['title']['std'] ) ) { |
| 179 |
$title = $this->settings['title']['std']; |
| 180 |
} |
| 181 |
|
| 182 |
if ( isset( $instance['title'] ) ) { |
| 183 |
$title = $instance['title']; |
| 184 |
} |
| 185 |
|
| 186 |
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
| 187 |
|
| 188 |
if ( $title ) { |
| 189 |
//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 190 |
echo wp_kses_post($args['before_title']) . esc_html( $title ) . wp_kses_post($args['after_title']); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Output the html at the end of a widget. |
| 196 |
* |
| 197 |
* @param array $args Arguments. |
| 198 |
*/ |
| 199 |
public function widget_end( $args ) { |
| 200 |
echo wp_kses_post($args['after_widget']); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|