providers
1 year ago
class-fast-image.php
2 years ago
class-folders.php
1 year ago
class-frontend.php
1 year ago
class-galleries.php
1 year ago
class-multilang.php
2 years ago
class-remote-library-api.php
1 year ago
class-remote-library.php
1 year ago
class-settings.php
1 year ago
class-tour.php
2 years ago
class-welcome.php
2 years ago
class-widgets.php
2 years ago
functions.php
3 years ago
class-widgets.php
788 lines
| 1 | <?php |
| 2 | // exit if accessed directly |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | new Responsive_Lightbox_Widgets(); |
| 7 | |
| 8 | /** |
| 9 | * Responsive Lightbox Widgets class. |
| 10 | * |
| 11 | * @class Responsive_Lightbox_Widgets |
| 12 | */ |
| 13 | class Responsive_Lightbox_Widgets { |
| 14 | |
| 15 | /** |
| 16 | * Class constructor. |
| 17 | * |
| 18 | * @return void |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | // actions |
| 22 | add_action( 'widgets_init', [ $this, 'register_widgets' ] ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Register widgets. |
| 27 | * |
| 28 | * @return void |
| 29 | */ |
| 30 | public function register_widgets() { |
| 31 | register_widget( 'Responsive_Lightbox_Gallery_Widget' ); |
| 32 | register_widget( 'Responsive_Lightbox_Image_Widget' ); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Responsive Lightbox Gallery Widget class. |
| 38 | * |
| 39 | * @class Responsive_Lightbox_Gallery_Widget |
| 40 | */ |
| 41 | class Responsive_Lightbox_Gallery_Widget extends WP_Widget { |
| 42 | |
| 43 | private $rlg_defaults = []; |
| 44 | private $rlg_orders = []; |
| 45 | private $rlg_order_types = []; |
| 46 | private $rlg_image_sizes = []; |
| 47 | private $rlg_gallery_types = []; |
| 48 | private $rli_allowed_html = [ |
| 49 | 'div' => [ |
| 50 | 'class' => true, |
| 51 | 'id' => true |
| 52 | ], |
| 53 | 'p' => true, |
| 54 | 'br' => true, |
| 55 | 'textarea' => [ |
| 56 | 'id' => true, |
| 57 | 'class' => true, |
| 58 | 'name' => true |
| 59 | ], |
| 60 | 'label' => [ |
| 61 | 'for' => true |
| 62 | ], |
| 63 | 'input' => [ |
| 64 | 'type' => true, |
| 65 | 'class' => true, |
| 66 | 'id' => true, |
| 67 | 'name' => true, |
| 68 | 'value' => true, |
| 69 | 'min' => true |
| 70 | ], |
| 71 | 'a' => [ |
| 72 | 'href' => true, |
| 73 | 'class' => true, |
| 74 | 'title' => true |
| 75 | ], |
| 76 | 'ul' => [ |
| 77 | 'id' => true, |
| 78 | 'class' => true |
| 79 | ], |
| 80 | 'li' => [ |
| 81 | 'class' => true, |
| 82 | 'data-attachment_id' => true |
| 83 | ], |
| 84 | 'select' => [ |
| 85 | 'name' => true, |
| 86 | 'id' => true, |
| 87 | 'class' => true |
| 88 | ], |
| 89 | 'option' => [ |
| 90 | 'value' => true, |
| 91 | 'selected' => true |
| 92 | ], |
| 93 | 'img' => [ |
| 94 | 'id' => true, |
| 95 | 'width' => true, |
| 96 | 'height' => true, |
| 97 | 'src' => true, |
| 98 | 'class' => true, |
| 99 | 'alt' => true, |
| 100 | 'decoding' => true, |
| 101 | 'loading' => true, |
| 102 | 'srcset' => true, |
| 103 | 'sizes' => true, |
| 104 | 'style' => true, |
| 105 | 'title' => true, |
| 106 | 'data-*' => true, |
| 107 | 'aria-describedby' => true, |
| 108 | 'aria-details' => true, |
| 109 | 'aria-label' => true, |
| 110 | 'aria-labelledby' => true, |
| 111 | 'aria-hidden' => true, |
| 112 | 'align' => true |
| 113 | ] |
| 114 | ]; |
| 115 | |
| 116 | /** |
| 117 | * Class constructor. |
| 118 | * |
| 119 | * @return void |
| 120 | */ |
| 121 | public function __construct() { |
| 122 | parent::__construct( |
| 123 | 'Responsive_Lightbox_Gallery_Widget', |
| 124 | __( 'Gallery', 'responsive-lightbox' ), |
| 125 | [ |
| 126 | 'description' => __( 'Displays an image gallery.', 'responsive-lightbox' ), |
| 127 | 'classname' => 'rl-gallery-widget' |
| 128 | ] |
| 129 | ); |
| 130 | |
| 131 | $this->rlg_defaults = [ |
| 132 | 'title' => __( 'Gallery', 'responsive-lightbox' ), |
| 133 | 'orderby' => 'menu_order', |
| 134 | 'order' => 'asc', |
| 135 | 'columns' => 3, |
| 136 | 'size' => 'thumbnail', |
| 137 | 'type' => 'none', |
| 138 | 'atts' => '', |
| 139 | 'ids' => '' |
| 140 | ]; |
| 141 | |
| 142 | $this->rlg_orders = [ |
| 143 | 'menu_order' => __( 'Menu order', 'responsive-lightbox' ), |
| 144 | 'title' => __( 'Title', 'responsive-lightbox' ), |
| 145 | 'post_date' => __( 'Image date', 'responsive-lightbox' ), |
| 146 | 'ID' => __( 'ID', 'responsive-lightbox' ), |
| 147 | 'rand' => __( 'Random', 'responsive-lightbox' ) |
| 148 | ]; |
| 149 | |
| 150 | $this->rlg_order_types = [ |
| 151 | 'asc' => __( 'Ascending', 'responsive-lightbox' ), |
| 152 | 'desc' => __( 'Descending', 'responsive-lightbox' ) |
| 153 | ]; |
| 154 | |
| 155 | $gallery_types = apply_filters( 'rl_gallery_types', Responsive_Lightbox()->get_data( 'gallery_types' ) ); |
| 156 | |
| 157 | if ( ! empty( $gallery_types ) ) { |
| 158 | $this->rlg_gallery_types = array_merge( |
| 159 | [ |
| 160 | 'none' => __( 'None', 'responsive-lightbox' ), |
| 161 | 'default' => __( 'Default', 'responsive-lightbox' ) |
| 162 | ], |
| 163 | $gallery_types |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | $this->rlg_image_sizes = array_merge( [ 'full' ], get_intermediate_image_sizes() ); |
| 168 | |
| 169 | sort( $this->rlg_image_sizes, SORT_STRING ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Display widget. |
| 174 | * |
| 175 | * @param array $args |
| 176 | * @param object $instance |
| 177 | * @return void |
| 178 | */ |
| 179 | public function widget( $args, $instance ) { |
| 180 | if ( empty( $instance ) ) |
| 181 | $instance = $this->rlg_defaults; |
| 182 | |
| 183 | $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); |
| 184 | |
| 185 | $html = $args['before_widget'] . $args['before_title'] . ( $instance['title'] !== '' ? esc_html( $instance['title'] ) : '' ) . $args['after_title']; |
| 186 | |
| 187 | $atts = []; |
| 188 | |
| 189 | // escape atts |
| 190 | if ( $instance['atts'] !== '' ) { |
| 191 | $atts_exp = explode( '" ', $instance['atts'] ); |
| 192 | |
| 193 | if ( ! empty( $atts_exp ) ) { |
| 194 | end( $atts_exp ); |
| 195 | |
| 196 | $last = key( $atts_exp ); |
| 197 | |
| 198 | reset( $atts_exp ); |
| 199 | |
| 200 | foreach ( $atts_exp as $id => $attribute ) { |
| 201 | $check = $attribute . ( $last === $id ? '' : '"' ); |
| 202 | |
| 203 | if ( preg_match( '/^([a-z0-9_-]+)=\"(.+?)\"$/', $check, $matches ) === 1 ) |
| 204 | $atts[] = $matches[1] . '="' . esc_attr( $matches[2] ) . '"'; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if ( ! empty( $atts ) ) |
| 210 | $instance['atts'] = implode( ' ', $atts ); |
| 211 | else |
| 212 | $instance['atts'] = ''; |
| 213 | |
| 214 | $html .= do_shortcode( '[gallery link="file" columns="' . (int) $instance['columns'] . '" size="' . esc_attr( $instance['size'] ) . '" ' . ( $instance['type'] !== 'none' ? 'type="' . esc_attr( $instance['type'] ) . '"' : '' ) . ' ids="' . ( ! empty( $instance['ids'] ) ? esc_attr( $instance['ids'] ) : 'none' ) . '" orderby="' . esc_attr( $instance['orderby'] ) . '" order="' . esc_attr( $instance['order'] ) . '"' . ( $instance['atts'] !== '' ? ' ' . $instance['atts'] : '' ) . ']' ); |
| 215 | $html .= $args['after_widget']; |
| 216 | |
| 217 | echo wp_kses_post( apply_filters( 'rl_gallery_widget_html', $html, $instance ) ); |
| 218 | } |
| 219 | |
| 220 | /** Render widget form. |
| 221 | * |
| 222 | * @param object $instance |
| 223 | * @return void |
| 224 | */ |
| 225 | public function form( $instance ) { |
| 226 | $attachments = ! empty( $instance['ids'] ) ? array_filter( explode( ',', $instance['ids'] ) ) : []; |
| 227 | |
| 228 | $html = ' |
| 229 | <div class="rl-gallery-widget-container"> |
| 230 | <p> |
| 231 | <label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Title', 'responsive-lightbox' ) . ':</label> |
| 232 | <input id="' . $this->get_field_id( 'title' ) . '" class="widefat" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . esc_attr( isset( $instance['title'] ) ? $instance['title'] : $this->rlg_defaults['title'] ) . '" /> |
| 233 | </p> |
| 234 | <div id="' . $this->get_field_id( 'gallery' ) . '" class="rl-gallery-widget' . ( ! empty( $attachments ) ? ' has-image' : '' ) . '"> |
| 235 | <input type="hidden" class="rl-gallery-ids" id="' . $this->get_field_id( 'ids' ) . '" name="' . $this->get_field_name( 'ids' ) . '" value="' . ( ! empty( $instance['ids'] ) ? esc_attr( $instance['ids'] ) : '' ) . '">'; |
| 236 | |
| 237 | $html .= ' |
| 238 | <a href="#" class="rl-gallery-widget-select button button-secondary">' . esc_html__( 'Select images', 'responsive-lightbox' ) . '</a> |
| 239 | <div class="rl-gallery-widget-content"> |
| 240 | <ul id="' . $this->get_field_id( 'gallery-images' ) . '" class="rl-gallery-images">'; |
| 241 | |
| 242 | if ( $attachments ) { |
| 243 | foreach ( $attachments as $attachment_id ) { |
| 244 | if ( ! $attachment_id || ! wp_attachment_is_image( $attachment_id ) ) |
| 245 | continue; |
| 246 | |
| 247 | $html .= ' |
| 248 | <li class="rl-gallery-image" data-attachment_id="' . (int) $attachment_id . '"> |
| 249 | <div class="rl-gallery-inner"> |
| 250 | <div class="centered">' . wp_get_attachment_image( $attachment_id, 'thumbnail' ) . '</div> |
| 251 | </div> |
| 252 | <div class="rl-gallery-actions"><a href="#" class="rl-gallery-image-remove dashicons-before dashicons-no" title="' . esc_attr__( 'Delete image', 'responsive-lightbox' ) . '"></a></div> |
| 253 | </li>'; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | $html .= ' |
| 258 | </ul> |
| 259 | </div> |
| 260 | </div> |
| 261 | <p>'; |
| 262 | |
| 263 | if ( ! empty( $this->rlg_gallery_types ) ) { |
| 264 | $html .= ' |
| 265 | <label for="' . $this->get_field_id( 'type' ) . '">' . esc_html__( 'Gallery type', 'responsive-lightbox' ) . ':</label> |
| 266 | <select id="' . $this->get_field_id( 'type' ) . '" class="widefat" name="' . $this->get_field_name( 'type' ) . '">'; |
| 267 | |
| 268 | foreach ( $this->rlg_gallery_types as $id => $type ) { |
| 269 | $html .= ' |
| 270 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['type'] ) ? $instance['type'] : $this->rlg_defaults['type'] ), false ) . '>' . esc_html( $type ) . '</option>'; |
| 271 | } |
| 272 | |
| 273 | $html .= ' |
| 274 | </select> |
| 275 | </p> |
| 276 | <p>'; |
| 277 | } |
| 278 | |
| 279 | $html .= ' |
| 280 | <label for="' . $this->get_field_id( 'orderby' ) . '">' . esc_html__( 'Order by', 'responsive-lightbox' ) . ':</label> |
| 281 | <select id="' . $this->get_field_id( 'orderby' ) . '" class="widefat" name="' . $this->get_field_name( 'orderby' ) . '">'; |
| 282 | |
| 283 | foreach ( $this->rlg_orders as $id => $orderby ) { |
| 284 | $html .= ' |
| 285 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['orderby'] ) ? $instance['orderby'] : $this->rlg_defaults['orderby'] ), false ) . '>' . esc_html( $orderby ) . '</option>'; |
| 286 | } |
| 287 | |
| 288 | $html .= ' |
| 289 | </select> |
| 290 | </p> |
| 291 | <p> |
| 292 | <label for="' . $this->get_field_id( 'order' ) . '">' . esc_html__( 'Order', 'responsive-lightbox' ) . ':</label> |
| 293 | <select id="' . $this->get_field_id( 'order' ) . '" class="widefat" name="' . $this->get_field_name( 'order' ) . '">'; |
| 294 | |
| 295 | foreach ( $this->rlg_order_types as $id => $order ) { |
| 296 | $html .= ' |
| 297 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['order'] ) ? $instance['order'] : $this->rlg_defaults['order'] ), false ) . '>' . esc_html( $order ) . '</option>'; |
| 298 | } |
| 299 | |
| 300 | $html .= ' |
| 301 | </select> |
| 302 | </p> |
| 303 | <p> |
| 304 | <label for="' . $this->get_field_id( 'size' ) . '">' . esc_html__( 'Image size', 'responsive-lightbox' ) . ':</label> |
| 305 | <select id="' . $this->get_field_id( 'size' ) . '" class="widefat" name="' . $this->get_field_name( 'size' ) . '">'; |
| 306 | |
| 307 | foreach ( $this->rlg_image_sizes as $size ) { |
| 308 | $html .= ' |
| 309 | <option value="' . esc_attr( $size ) . '" ' . selected( $size, ( isset( $instance['size'] ) ? $instance['size'] : $this->rlg_defaults['size'] ), false ) . '>' . esc_html( $size ) . '</option>'; |
| 310 | } |
| 311 | |
| 312 | $html .= ' |
| 313 | </select> |
| 314 | </p> |
| 315 | <p> |
| 316 | <label for="' . $this->get_field_id( 'columns' ) . '">' . esc_html__( 'Number of columns', 'responsive-lightbox' ) . ':</label> |
| 317 | <input id="' . $this->get_field_id( 'columns' ) . '" class="small-text" name="' . $this->get_field_name( 'columns' ) . '" type="number" min="0" value="' . (int) ( isset( $instance['columns'] ) ? $instance['columns'] : $this->rlg_defaults['columns'] ) . '" /> |
| 318 | </p> |
| 319 | <p> |
| 320 | <label for="' . $this->get_field_id( 'atts' ) . '">' . esc_html__( 'Custom gallery shortcode attributes', 'responsive-lightbox' ) . ':</label> |
| 321 | <br /> |
| 322 | <textarea id="' . $this->get_field_id( 'atts' ) . '" class="widefat" name="' . $this->get_field_name( 'atts' ) . '">' . esc_textarea( isset( $instance['atts'] ) ? $instance['atts'] : $this->rlg_defaults['atts'] ) . '</textarea> |
| 323 | </p> |
| 324 | </div>'; |
| 325 | |
| 326 | echo wp_kses( $html, $this->rli_allowed_html ); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Save widget form. |
| 331 | * |
| 332 | * @param array $new_instance |
| 333 | * @param array $old_instance |
| 334 | * @return array |
| 335 | */ |
| 336 | public function update( $new_instance, $old_instance ) { |
| 337 | // title |
| 338 | $old_instance['title'] = sanitize_text_field( isset( $new_instance['title'] ) ? trim( $new_instance['title'] ) : $this->rlg_defaults['title'] ); |
| 339 | |
| 340 | // order by |
| 341 | $old_instance['orderby'] = isset( $new_instance['orderby'] ) && array_key_exists( $new_instance['orderby'], $this->rlg_orders ) ? $new_instance['orderby'] : $this->rlg_defaults['orderby']; |
| 342 | |
| 343 | // order |
| 344 | $old_instance['order'] = isset( $new_instance['order'] ) && array_key_exists( $new_instance['order'], $this->rlg_order_types ) ? $new_instance['order'] : $this->rlg_defaults['order']; |
| 345 | |
| 346 | // image size |
| 347 | $old_instance['size'] = isset( $new_instance['size'] ) && in_array( $new_instance['size'], $this->rlg_image_sizes, true ) ? $new_instance['size'] : $this->rlg_defaults['size']; |
| 348 | |
| 349 | // gallery type |
| 350 | $old_instance['type'] = isset( $new_instance['type'] ) && array_key_exists( $new_instance['type'], $this->rlg_gallery_types ) ? $new_instance['type'] : $this->rlg_defaults['type']; |
| 351 | |
| 352 | // number of columns |
| 353 | $old_instance['columns'] = isset( $new_instance['columns'] ) ? ( ( $columns = (int) $new_instance['columns'] ) > 0 ? $columns : $this->rlg_defaults['columns'] ) : $this->rlg_defaults['columns']; |
| 354 | |
| 355 | // image ids |
| 356 | if ( ! empty( $new_instance['ids'] ) && is_string( $new_instance['ids'] ) ) { |
| 357 | // get unique and non empty attachment ids only |
| 358 | $old_instance['ids'] = implode( ',', array_unique( array_filter( array_map( 'intval', explode( ',', $new_instance['ids'] ) ) ) ) ); |
| 359 | } else |
| 360 | $old_instance['ids'] = $this->rlg_defaults['ids']; |
| 361 | |
| 362 | // custom attributes |
| 363 | $atts = sanitize_textarea_field( preg_replace( '/\s+/', ' ', trim( str_replace( [ "\r\n", "\n\r", "\n", "\r" ], ' ', isset( $new_instance['atts'] ) ? $new_instance['atts'] : $this->rlg_defaults['atts'] ) ) ) ); |
| 364 | |
| 365 | $new_atts = []; |
| 366 | |
| 367 | if ( $atts !== '' ) { |
| 368 | $atts_exp = explode( '" ', $atts ); |
| 369 | |
| 370 | if ( ! empty( $atts_exp ) ) { |
| 371 | end( $atts_exp ); |
| 372 | |
| 373 | $last = key( $atts_exp ); |
| 374 | |
| 375 | reset( $atts_exp ); |
| 376 | |
| 377 | foreach ( $atts_exp as $id => $attribute ) { |
| 378 | $check = $attribute . ( $last === $id ? '' : '"' ); |
| 379 | |
| 380 | if ( preg_match( '/^[a-z0-9_-]+=\"(.+?)\"$/', $check ) === 1 ) |
| 381 | $new_atts[] = $check; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | if ( ! empty( $new_atts ) ) |
| 387 | $old_instance['atts'] = implode( ' ', $new_atts ); |
| 388 | else |
| 389 | $old_instance['atts'] = ''; |
| 390 | |
| 391 | return $old_instance; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Responsive Lightbox Gallery Widget class. |
| 397 | * |
| 398 | * @class Responsive_Lightbox_Gallery_Widget |
| 399 | */ |
| 400 | class Responsive_Lightbox_Image_Widget extends WP_Widget { |
| 401 | |
| 402 | private $rli_defaults = []; |
| 403 | private $rli_text_positions = []; |
| 404 | private $rli_link_to = []; |
| 405 | private $rli_aligns = []; |
| 406 | private $rli_image_sizes = []; |
| 407 | private $rli_allowed_html = [ |
| 408 | 'div' => [ |
| 409 | 'class' => true, |
| 410 | 'id' => true |
| 411 | ], |
| 412 | 'p' => [ |
| 413 | 'style' => true, |
| 414 | 'class' => true |
| 415 | ], |
| 416 | 'textarea' => [ |
| 417 | 'id' => true, |
| 418 | 'class' => true, |
| 419 | 'name' => true, |
| 420 | 'rows' => true |
| 421 | ], |
| 422 | 'label' => [ |
| 423 | 'for' => true |
| 424 | ], |
| 425 | 'input' => [ |
| 426 | 'type' => true, |
| 427 | 'class' => true, |
| 428 | 'id' => true, |
| 429 | 'name' => true, |
| 430 | 'value' => true, |
| 431 | 'min' => true, |
| 432 | 'checked' => true |
| 433 | ], |
| 434 | 'a' => [ |
| 435 | 'href' => true, |
| 436 | 'class' => true, |
| 437 | 'title' => true |
| 438 | ], |
| 439 | 'select' => [ |
| 440 | 'name' => true, |
| 441 | 'id' => true, |
| 442 | 'class' => true |
| 443 | ], |
| 444 | 'option' => [ |
| 445 | 'value' => true, |
| 446 | 'selected' => true |
| 447 | ], |
| 448 | 'img' => [ |
| 449 | 'id' => true, |
| 450 | 'width' => true, |
| 451 | 'height' => true, |
| 452 | 'src' => true, |
| 453 | 'class' => true, |
| 454 | 'alt' => true, |
| 455 | 'decoding' => true, |
| 456 | 'loading' => true, |
| 457 | 'srcset' => true, |
| 458 | 'sizes' => true, |
| 459 | 'style' => true, |
| 460 | 'title' => true, |
| 461 | 'data-*' => true, |
| 462 | 'aria-describedby' => true, |
| 463 | 'aria-details' => true, |
| 464 | 'aria-label' => true, |
| 465 | 'aria-labelledby' => true, |
| 466 | 'aria-hidden' => true, |
| 467 | 'align' => true |
| 468 | ] |
| 469 | ]; |
| 470 | |
| 471 | /** |
| 472 | * Class constructor. |
| 473 | * |
| 474 | * @return void |
| 475 | */ |
| 476 | public function __construct() { |
| 477 | parent::__construct( |
| 478 | 'Responsive_Lightbox_Image_Widget', |
| 479 | __( 'Image', 'responsive-lightbox' ), |
| 480 | [ |
| 481 | 'description' => __( 'Displays a single image.', 'responsive-lightbox' ), |
| 482 | 'classname' => 'rl-image-widget' |
| 483 | ] |
| 484 | ); |
| 485 | |
| 486 | $this->rli_defaults = [ |
| 487 | 'title' => __( 'Image', 'responsive-lightbox' ), |
| 488 | 'image_id' => 0, |
| 489 | 'responsive' => true, |
| 490 | 'size' => 'thumbnail', |
| 491 | 'link_to' => 'file', |
| 492 | 'link_custom_url' => '', |
| 493 | 'image_align' => 'none', |
| 494 | 'text' => '', |
| 495 | 'autobr' => false, |
| 496 | 'text_position' => 'below_image', |
| 497 | 'text_align' => 'none' |
| 498 | ]; |
| 499 | |
| 500 | $this->rli_text_positions = [ |
| 501 | 'below_image' => __( 'Below the image', 'responsive-lightbox' ), |
| 502 | 'above_image' => __( 'Above the image', 'responsive-lightbox' ) |
| 503 | ]; |
| 504 | |
| 505 | $this->rli_link_to = [ |
| 506 | 'none' => __( 'None', 'responsive-lightbox' ), |
| 507 | 'file' => __( 'Media File', 'responsive-lightbox' ), |
| 508 | 'post' => __( 'Attachment Page', 'responsive-lightbox' ), |
| 509 | 'custom' => __( 'Custom URL', 'responsive-lightbox' ) |
| 510 | ]; |
| 511 | |
| 512 | $this->rli_aligns = [ |
| 513 | 'none' => __( 'None', 'responsive-lightbox' ), |
| 514 | 'left' => __( 'Left', 'responsive-lightbox' ), |
| 515 | 'center' => __( 'Center', 'responsive-lightbox' ), |
| 516 | 'right' => __( 'Right', 'responsive-lightbox' ), |
| 517 | 'justify' => __( 'Justify', 'responsive-lightbox' ) |
| 518 | ]; |
| 519 | |
| 520 | $this->rli_image_sizes = array_merge( [ 'full' ], get_intermediate_image_sizes() ); |
| 521 | |
| 522 | sort( $this->rli_image_sizes, SORT_STRING ); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Display widget. |
| 527 | * |
| 528 | * @param array $args |
| 529 | * @param array $instance |
| 530 | * @return void |
| 531 | */ |
| 532 | public function widget( $args, $instance ) { |
| 533 | if ( empty( $instance ) ) |
| 534 | $instance = $this->rli_defaults; |
| 535 | |
| 536 | $href = ''; |
| 537 | |
| 538 | switch ( $instance['link_to'] ) { |
| 539 | case 'file': |
| 540 | $file = wp_get_attachment_image_src( $instance['image_id'], 'full', false ); |
| 541 | |
| 542 | if ( $file !== false ) |
| 543 | $href = $file[0]; |
| 544 | break; |
| 545 | |
| 546 | case 'post': |
| 547 | $href = get_permalink( $instance['image_id'] ); |
| 548 | |
| 549 | if ( $href === false ) |
| 550 | $href = ''; |
| 551 | break; |
| 552 | |
| 553 | case 'custom': |
| 554 | $href = $instance['link_custom_url']; |
| 555 | } |
| 556 | |
| 557 | // image align |
| 558 | if ( $instance['image_align'] === 'left' ) |
| 559 | $image_align = 'float: left;'; |
| 560 | elseif ( $instance['image_align'] === 'center' ) |
| 561 | $image_align = 'margin-left: auto; margin-right: auto; display: block;'; |
| 562 | elseif ( $instance['image_align'] === 'right' ) |
| 563 | $image_align = 'float: right;'; |
| 564 | else |
| 565 | $image_align = ''; |
| 566 | |
| 567 | // text align |
| 568 | if ( $instance['text_align'] === 'left' ) |
| 569 | $text_align = 'text-align: left; display: block;'; |
| 570 | elseif ( $instance['text_align'] === 'center' ) |
| 571 | $text_align = 'text-align: center; display: block;'; |
| 572 | elseif ( $instance['text_align'] === 'right' ) |
| 573 | $text_align = 'text-align: right; display: block;'; |
| 574 | elseif ( $instance['text_align'] === 'justify' ) |
| 575 | $text_align = 'text-align: justify; display: block;'; |
| 576 | else |
| 577 | $text_align = ''; |
| 578 | |
| 579 | // get image data |
| 580 | $image = wp_get_attachment_image_src( $instance['image_id'], $instance['size'], false ); |
| 581 | |
| 582 | if ( $image !== false ) { |
| 583 | $image_url = $image[0]; |
| 584 | $width = $instance['responsive'] === false ? $image[1] : '100%'; |
| 585 | $height = $instance['responsive'] === false ? $image[2] : 'auto'; |
| 586 | $post = get_post( $instance['image_id'] ); |
| 587 | $image_title = isset( $post->post_title ) ? $post->post_title : ''; |
| 588 | $alt = (string) get_post_meta( $instance['image_id'], '_wp_attachment_image_alt', true ); |
| 589 | } else { |
| 590 | $image_url = ''; |
| 591 | $width = ''; |
| 592 | $height = ''; |
| 593 | $image_title = ''; |
| 594 | $alt = ''; |
| 595 | } |
| 596 | |
| 597 | $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); |
| 598 | |
| 599 | // start output |
| 600 | $html = $args['before_widget'] . $args['before_title'] . ( $instance['title'] !== '' ? esc_html( $instance['title'] ) : '' ) . $args['after_title']; |
| 601 | |
| 602 | if ( $instance['autobr'] === true ) |
| 603 | $escaped_text = wpautop( esc_html( $instance['text'] ) ); |
| 604 | else |
| 605 | $escaped_text = esc_html( $instance['text'] ); |
| 606 | |
| 607 | $container_html = '<div class="rl-image-widget-text" style="' . esc_attr( $text_align ) . '">' . $escaped_text . '</div>'; |
| 608 | $image_html = ( $href !== '' ? '<a href="' . esc_url( $href ) . '" class="rl-image-widget-link">' : '' ) . '<img class="rl-image-widget-image" src="' . esc_url( $image_url ) . '" width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" title="' . esc_attr( $image_title ) . '" alt="' . esc_attr( $alt ) . '" style="' . esc_attr( $image_align ) . '" />' . ( $href !== '' ? '</a>' : '' ); |
| 609 | |
| 610 | if ( $instance['text_position'] === 'below_image' ) |
| 611 | $html .= $image_html . $container_html; |
| 612 | else |
| 613 | $html .= $container_html . $image_html; |
| 614 | |
| 615 | $html .= $args['after_widget']; |
| 616 | |
| 617 | echo wp_kses_post( apply_filters( 'rl_image_widget_html', $html, $instance ) ); |
| 618 | } |
| 619 | |
| 620 | /** Render widget form. |
| 621 | * |
| 622 | * @param array $instance |
| 623 | * @return void |
| 624 | */ |
| 625 | public function form( $instance ) { |
| 626 | $image_id = (int) ( isset( $instance['image_id'] ) ? $instance['image_id'] : $this->rli_defaults['image_id'] ); |
| 627 | $image = ''; |
| 628 | |
| 629 | if ( ! empty( $image_id ) ) |
| 630 | $image = wp_get_attachment_image( $image_id, 'medium', false ); |
| 631 | |
| 632 | if ( ! $image ) |
| 633 | $image = wp_get_attachment_image( $image_id, 'full', false ); |
| 634 | |
| 635 | $html = ' |
| 636 | <div class="rl-image-widget-container"> |
| 637 | <p> |
| 638 | <label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Title', 'responsive-lightbox' ) . '</label> |
| 639 | <input id="' . $this->get_field_id( 'title' ) . '" class="widefat" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . esc_attr( isset( $instance['title'] ) ? $instance['title'] : $this->rli_defaults['title'] ) . '" /> |
| 640 | </p> |
| 641 | <div class="rl-image-widget' . ( ! empty( $image_id ) ? ' has-image' : '' ) . '"> |
| 642 | <input class="rl-image-widget-image-id" type="hidden" name="' . $this->get_field_name( 'image_id' ) . '" value="' . (int) $image_id . '" /> |
| 643 | <a href="#" class="rl-image-widget-select button button-secondary">' . esc_html__( 'Select image', 'responsive-lightbox' ) . '</a> |
| 644 | <div class="rl-image-widget-content">'; |
| 645 | |
| 646 | if ( ! empty( $image ) ) |
| 647 | $html .= $image; |
| 648 | |
| 649 | $html .= ' |
| 650 | </div> |
| 651 | </div> |
| 652 | <p> |
| 653 | <input id="' . $this->get_field_id( 'responsive' ) . '" type="checkbox" name="' . $this->get_field_name( 'responsive' ) . '" value="responsive" ' . checked( true, ( isset( $instance['responsive'] ) ? $instance['responsive'] : $this->rli_defaults['responsive'] ), false ) . ' /> <label for="' . $this->get_field_id( 'responsive' ) . '">' . esc_html__( 'Force responsive', 'responsive-lightbox' ) . '</label> |
| 654 | </p>'; |
| 655 | |
| 656 | $html .= ' |
| 657 | <p> |
| 658 | <label for="' . $this->get_field_id( 'size' ) . '">' . esc_html__( 'Size', 'responsive-lightbox' ) . '</label> |
| 659 | <select class="rl-image-size-select widefat" id="' . $this->get_field_id( 'size' ) . '" name="' . $this->get_field_name( 'size' ) . '">'; |
| 660 | |
| 661 | $size_type = ( isset( $instance['size'] ) ? $instance['size'] : $this->rli_defaults['size'] ); |
| 662 | |
| 663 | foreach ( $this->rli_image_sizes as $size ) { |
| 664 | $html .= ' |
| 665 | <option value="' . esc_attr( $size ) . '" ' . selected( $size, $size_type, false ) . '>' . esc_html( $size ) . '</option>'; |
| 666 | } |
| 667 | |
| 668 | $html .= ' |
| 669 | </select> |
| 670 | </p> |
| 671 | <p> |
| 672 | <label for="' . $this->get_field_id( 'link_to' ) . '">' . esc_html__( 'Link to', 'responsive-lightbox' ) . '</label> |
| 673 | <select class="rl-image-link-to widefat" id="' . $this->get_field_id( 'link_to' ) . '" name="' . $this->get_field_name( 'link_to' ) . '">'; |
| 674 | |
| 675 | $link_type = ( isset( $instance['link_to'] ) ? $instance['link_to'] : $this->rli_defaults['link_to'] ); |
| 676 | |
| 677 | foreach ( $this->rli_link_to as $id => $type ) { |
| 678 | $html .= ' |
| 679 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, $link_type, false ) . '>' . esc_html( $type ) . '</option>'; |
| 680 | } |
| 681 | |
| 682 | $html .= ' |
| 683 | </select> |
| 684 | </p> |
| 685 | <p class="rl-image-link-url"' . ( $link_type === 'custom' ? '' : ' style="display: none;"' ) . '> |
| 686 | <label for="' . $this->get_field_id( 'link_custom_url' ) . '">' . esc_html__( 'URL', 'responsive-lightbox' ) . '</label> |
| 687 | <input id="' . $this->get_field_id( 'link_custom_url' ) . '" class="widefat" name="' . $this->get_field_name( 'link_custom_url' ) . '" type="text" value="' . esc_attr( isset( $instance['link_custom_url'] ) ? $instance['link_custom_url'] : $this->rli_defaults['link_custom_url'] ) . '" /> |
| 688 | </p>'; |
| 689 | |
| 690 | $html .= ' |
| 691 | <p> |
| 692 | <label for="' . $this->get_field_id( 'image_align' ) . '">' . esc_html__( 'Image align', 'responsive-lightbox' ) . '</label> |
| 693 | <select id="' . $this->get_field_id( 'image_align' ) . '" class="widefat" name="' . $this->get_field_name( 'image_align' ) . '">'; |
| 694 | |
| 695 | foreach ( $this->rli_aligns as $id => $image_align ) { |
| 696 | if ( $id !== 'justify' ) |
| 697 | $html .= ' |
| 698 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['image_align'] ) ? $instance['image_align'] : $this->rli_defaults['image_align'] ), false ) . '>' . esc_html( $image_align ) . '</option>'; |
| 699 | } |
| 700 | |
| 701 | $html .= ' |
| 702 | </select> |
| 703 | </p> |
| 704 | <p> |
| 705 | <label for="' . $this->get_field_id( 'text' ) . '">' . esc_html__( 'Text', 'responsive-lightbox' ) . '</label> |
| 706 | <textarea id="' . $this->get_field_id( 'text' ) . '" class="widefat" name="' . $this->get_field_name( 'text' ) . '" rows="4">' . esc_html( isset( $instance['text'] ) ? $instance['text'] : $this->rli_defaults['text'] ) . '</textarea> |
| 707 | </p> |
| 708 | <p> |
| 709 | <input id="' . $this->get_field_id( 'autobr' ) . '" type="checkbox" name="' . $this->get_field_name( 'autobr' ) . '" value="autobr" ' . checked( true, ( isset( $instance['autobr'] ) ? $instance['autobr'] : $this->rli_defaults['autobr'] ), false ) . ' /> <label for="' . $this->get_field_id( 'autobr' ) . '">' . esc_html__( 'Automatically add paragraphs', 'responsive-lightbox' ) . '</label> |
| 710 | </p>'; |
| 711 | |
| 712 | $html .= ' |
| 713 | <p> |
| 714 | <label for="' . $this->get_field_id( 'text_position' ) . '">' . esc_html__( 'Text position', 'responsive-lightbox' ) . '</label> |
| 715 | <select id="' . $this->get_field_id( 'text_position' ) . '" class="widefat" name="' . $this->get_field_name( 'text_position' ) . '">'; |
| 716 | |
| 717 | foreach ( $this->rli_text_positions as $id => $text_position ) { |
| 718 | $html .= ' |
| 719 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['text_position'] ) ? $instance['text_position'] : $this->rli_defaults['text_position'] ), false ) . '>' . esc_html( $text_position ) . '</option>'; |
| 720 | } |
| 721 | |
| 722 | $html .= ' |
| 723 | </select> |
| 724 | </p> |
| 725 | <label for="' . $this->get_field_id( 'text_align' ) . '">' . esc_html__( 'Text align', 'responsive-lightbox' ) . '</label> |
| 726 | <select id="' . $this->get_field_id( 'text_align' ) . '" class="widefat" name="' . $this->get_field_name( 'text_align' ) . '">'; |
| 727 | |
| 728 | foreach ( $this->rli_aligns as $id => $text_align ) { |
| 729 | $html .= ' |
| 730 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['text_align'] ) ? $instance['text_align'] : $this->rli_defaults['text_align'] ), false ) . '>' . esc_html( $text_align ) . '</option>'; |
| 731 | } |
| 732 | |
| 733 | $html .= ' |
| 734 | </select> |
| 735 | </div>'; |
| 736 | |
| 737 | add_filter( 'safe_style_css', [ $this, 'rli_allow_display_attr' ] ); |
| 738 | |
| 739 | echo wp_kses( $html, $this->rli_allowed_html ); |
| 740 | |
| 741 | remove_filter( 'safe_style_css', [ $this, 'rli_allow_display_attr' ] ); |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * Save widget form. |
| 746 | * |
| 747 | * @param array $new_instance |
| 748 | * @param array $old_instance |
| 749 | * @return array |
| 750 | */ |
| 751 | public function update( $new_instance, $old_instance ) { |
| 752 | // whitelists |
| 753 | $old_instance['size'] = isset( $new_instance['size'] ) && in_array( $new_instance['size'], $this->rli_image_sizes, true ) ? $new_instance['size'] : $this->rli_defaults['size']; |
| 754 | $old_instance['link_to'] = isset( $new_instance['link_to'] ) && in_array( $new_instance['link_to'], array_keys( $this->rli_link_to ), true ) ? $new_instance['link_to'] : $this->rli_defaults['link_to']; |
| 755 | $old_instance['image_align'] = isset( $new_instance['image_align'] ) && in_array( $new_instance['image_align'], array_keys( $this->rli_aligns ), true ) ? $new_instance['image_align'] : $this->rli_defaults['image_align']; |
| 756 | $old_instance['text_position'] = isset( $new_instance['text_position'] ) && in_array( $new_instance['text_position'], array_keys( $this->rli_text_positions ), true ) ? $new_instance['text_position'] : $this->rli_defaults['text_position']; |
| 757 | $old_instance['text_align'] = isset( $new_instance['text_align'] ) && in_array( $new_instance['text_align'], array_keys( $this->rli_aligns ), true ) ? $new_instance['text_align'] : $this->rli_defaults['text_align']; |
| 758 | |
| 759 | // booleands |
| 760 | $old_instance['responsive'] = ! empty( $new_instance['responsive'] ); |
| 761 | $old_instance['autobr'] = ! empty( $new_instance['autobr'] ); |
| 762 | |
| 763 | // texts |
| 764 | $old_instance['title'] = sanitize_text_field( isset( $new_instance['title'] ) ? $new_instance['title'] : $this->rli_defaults['title'] ); |
| 765 | $old_instance['text'] = isset( $new_instance['text'] ) ? wp_kses_post( $new_instance['text'] ) : $this->rli_defaults['text']; |
| 766 | |
| 767 | // integers |
| 768 | $old_instance['image_id'] = isset( $new_instance['image_id'] ) ? (int) $new_instance['image_id'] : $this->rli_defaults['image_id']; |
| 769 | |
| 770 | // urls |
| 771 | $old_instance['link_custom_url'] = isset( $new_instance['link_custom_url'] ) ? esc_url( $new_instance['link_custom_url'] ) : $this->rli_defaults['link_custom_url']; |
| 772 | |
| 773 | return $old_instance; |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * Add display property to style safe list. |
| 778 | * |
| 779 | * @param array $styles |
| 780 | * @return array |
| 781 | */ |
| 782 | public function rli_allow_display_attr( $styles ) { |
| 783 | $styles[] = 'display'; |
| 784 | |
| 785 | return $styles; |
| 786 | } |
| 787 | } |
| 788 |