providers
3 years ago
class-fast-image.php
3 years ago
class-folders-walker.php
7 years ago
class-folders.php
3 years ago
class-frontend.php
3 years ago
class-galleries.php
3 years ago
class-multilang.php
3 years ago
class-remote-library-api.php
3 years ago
class-remote-library.php
3 years ago
class-settings.php
3 years ago
class-tour.php
3 years ago
class-welcome.php
3 years ago
class-widgets.php
3 years ago
functions.php
3 years ago
class-widgets.php
623 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 | |
| 49 | /** |
| 50 | * Class constructor. |
| 51 | * |
| 52 | * @return void |
| 53 | */ |
| 54 | public function __construct() { |
| 55 | parent::__construct( |
| 56 | 'Responsive_Lightbox_Gallery_Widget', |
| 57 | __( 'Gallery', 'responsive-lightbox' ), |
| 58 | [ |
| 59 | 'description' => __( 'Displays an image gallery.', 'responsive-lightbox' ), |
| 60 | 'classname' => 'rl-gallery-widget' |
| 61 | ] |
| 62 | ); |
| 63 | |
| 64 | $this->rlg_defaults = [ |
| 65 | 'title' => __( 'Gallery', 'responsive-lightbox' ), |
| 66 | 'orderby' => 'menu_order', |
| 67 | 'order' => 'asc', |
| 68 | 'columns' => 3, |
| 69 | 'size' => 'thumbnail', |
| 70 | 'type' => 'none', |
| 71 | 'atts' => '', |
| 72 | 'ids' => '' |
| 73 | ]; |
| 74 | |
| 75 | $this->rlg_orders = [ |
| 76 | 'menu_order' => __( 'Menu order', 'responsive-lightbox' ), |
| 77 | 'title' => __( 'Title', 'responsive-lightbox' ), |
| 78 | 'post_date' => __( 'Image date', 'responsive-lightbox' ), |
| 79 | 'ID' => __( 'ID', 'responsive-lightbox' ), |
| 80 | 'rand' => __( 'Random', 'responsive-lightbox' ) |
| 81 | ]; |
| 82 | |
| 83 | $this->rlg_order_types = [ |
| 84 | 'asc' => __( 'Ascending', 'responsive-lightbox' ), |
| 85 | 'desc' => __( 'Descending', 'responsive-lightbox' ) |
| 86 | ]; |
| 87 | |
| 88 | $gallery_types = apply_filters( 'rl_gallery_types', Responsive_Lightbox()->gallery_types ); |
| 89 | |
| 90 | if ( ! empty( $gallery_types ) ) { |
| 91 | $this->rlg_gallery_types = array_merge( |
| 92 | [ |
| 93 | 'none' => __( 'None', 'responsive-lightbox' ), |
| 94 | 'default' => __( 'Default', 'responsive-lightbox' ) |
| 95 | ], |
| 96 | $gallery_types |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | $this->rlg_image_sizes = array_merge( [ 'full' ], get_intermediate_image_sizes() ); |
| 101 | |
| 102 | sort( $this->rlg_image_sizes, SORT_STRING ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Display widget. |
| 107 | * |
| 108 | * @param array $args |
| 109 | * @param object $instance |
| 110 | * @return void |
| 111 | */ |
| 112 | public function widget( $args, $instance ) { |
| 113 | $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); |
| 114 | |
| 115 | $html = $args['before_widget'] . $args['before_title'] . ( $instance['title'] !== '' ? esc_html( $instance['title'] ) : '' ) . $args['after_title']; |
| 116 | |
| 117 | $atts = []; |
| 118 | |
| 119 | // escape atts |
| 120 | if ( $instance['atts'] !== '' ) { |
| 121 | $atts_exp = explode( '" ', $instance['atts'] ); |
| 122 | |
| 123 | if ( ! empty( $atts_exp ) ) { |
| 124 | end( $atts_exp ); |
| 125 | |
| 126 | $last = key( $atts_exp ); |
| 127 | |
| 128 | reset( $atts_exp ); |
| 129 | |
| 130 | foreach ( $atts_exp as $id => $attribute ) { |
| 131 | $check = $attribute . ( $last === $id ? '' : '"' ); |
| 132 | |
| 133 | if ( preg_match( '/^([a-z0-9_-]+)=\"(.+?)\"$/', $check, $matches ) === 1 ) |
| 134 | $atts[] = $matches[1] . '="' . esc_attr( $matches[2] ) . '"'; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if ( ! empty( $atts ) ) |
| 140 | $instance['atts'] = implode( ' ', $atts ); |
| 141 | else |
| 142 | $instance['atts'] = ''; |
| 143 | |
| 144 | $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'] ) : '' ) . '" orderby="' . esc_attr( $instance['orderby'] ) . '" order="' . esc_attr( $instance['order'] ) . '"' . ( $instance['atts'] !== '' ? ' ' . $instance['atts'] : '' ) . ']' ); |
| 145 | $html .= $args['after_widget']; |
| 146 | |
| 147 | echo apply_filters( 'rl_gallery_widget_html', $html, $instance ); |
| 148 | } |
| 149 | |
| 150 | /** Render widget form. |
| 151 | * |
| 152 | * @param object $instance |
| 153 | * @return void |
| 154 | */ |
| 155 | public function form( $instance ) { |
| 156 | $attachments = ! empty( $instance['ids'] ) ? array_filter( explode( ',', $instance['ids'] ) ) : []; |
| 157 | |
| 158 | $html = ' |
| 159 | <div class="rl-gallery-widget-container"> |
| 160 | <p> |
| 161 | <label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Title', 'responsive-lightbox' ) . ':</label> |
| 162 | <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'] ) . '" /> |
| 163 | </p> |
| 164 | <div id="' . $this->get_field_id( 'gallery' ) . '" class="rl-gallery-widget' . ( ! empty( $attachments ) ? ' has-image' : '' ) . '"> |
| 165 | <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'] ) : '' ) . '">'; |
| 166 | |
| 167 | $html .= ' |
| 168 | <a href="#" class="rl-gallery-widget-select button button-secondary">' . esc_html__( 'Select images', 'responsive-lightbox' ) . '</a> |
| 169 | <div class="rl-gallery-widget-content"> |
| 170 | <ul id="' . $this->get_field_id( 'gallery-images' ) . '" class="rl-gallery-images">'; |
| 171 | |
| 172 | if ( $attachments ) { |
| 173 | foreach ( $attachments as $attachment_id ) { |
| 174 | if ( ! $attachment_id || ! wp_attachment_is_image( $attachment_id ) ) |
| 175 | continue; |
| 176 | |
| 177 | $html .= ' |
| 178 | <li class="rl-gallery-image" data-attachment_id="' . (int) $attachment_id . '"> |
| 179 | <div class="rl-gallery-inner">' . wp_get_attachment_image( $attachment_id, 'thumbnail' ) . '</div> |
| 180 | <div class="rl-gallery-actions"><a href="#" class="rl-gallery-image-remove dashicons dashicons-no" title="' . esc_attr__( 'Delete image', 'responsive-lightbox' ) . '"></a></div> |
| 181 | </li>'; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | $html .= ' |
| 186 | </ul> |
| 187 | </div> |
| 188 | </div> |
| 189 | <p>'; |
| 190 | |
| 191 | if ( ! empty( $this->rlg_gallery_types ) ) { |
| 192 | $html .= ' |
| 193 | <label for="' . $this->get_field_id( 'type' ) . '">' . esc_html__( 'Gallery type', 'responsive-lightbox' ) . ':</label> |
| 194 | <select id="' . $this->get_field_id( 'type' ) . '" class="widefat" name="' . $this->get_field_name( 'type' ) . '">'; |
| 195 | |
| 196 | foreach ( $this->rlg_gallery_types as $id => $type ) { |
| 197 | $html .= ' |
| 198 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['type'] ) ? $instance['type'] : $this->rlg_defaults['type'] ), false ) . '>' . esc_html( $type ) . '</option>'; |
| 199 | } |
| 200 | |
| 201 | $html .= ' |
| 202 | </select> |
| 203 | </p> |
| 204 | <p>'; |
| 205 | } |
| 206 | |
| 207 | $html .= ' |
| 208 | <label for="' . $this->get_field_id( 'orderby' ) . '">' . esc_html__( 'Order by', 'responsive-lightbox' ) . ':</label> |
| 209 | <select id="' . $this->get_field_id( 'orderby' ) . '" class="widefat" name="' . $this->get_field_name( 'orderby' ) . '">'; |
| 210 | |
| 211 | foreach ( $this->rlg_orders as $id => $orderby ) { |
| 212 | $html .= ' |
| 213 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['orderby'] ) ? $instance['orderby'] : $this->rlg_defaults['orderby'] ), false ) . '>' . esc_html( $orderby ) . '</option>'; |
| 214 | } |
| 215 | |
| 216 | $html .= ' |
| 217 | </select> |
| 218 | </p> |
| 219 | <p> |
| 220 | <label for="' . $this->get_field_id( 'order' ) . '">' . esc_html__( 'Order', 'responsive-lightbox' ) . ':</label> |
| 221 | <select id="' . $this->get_field_id( 'order' ) . '" class="widefat" name="' . $this->get_field_name( 'order' ) . '">'; |
| 222 | |
| 223 | foreach ( $this->rlg_order_types as $id => $order ) { |
| 224 | $html .= ' |
| 225 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, ( isset( $instance['order'] ) ? $instance['order'] : $this->rlg_defaults['order'] ), false ) . '>' . esc_html( $order ) . '</option>'; |
| 226 | } |
| 227 | |
| 228 | $html .= ' |
| 229 | </select> |
| 230 | </p> |
| 231 | <p> |
| 232 | <label for="' . $this->get_field_id( 'size' ) . '">' . esc_html__( 'Image size', 'responsive-lightbox' ) . ':</label> |
| 233 | <select id="' . $this->get_field_id( 'size' ) . '" class="widefat" name="' . $this->get_field_name( 'size' ) . '">'; |
| 234 | |
| 235 | foreach ( $this->rlg_image_sizes as $size ) { |
| 236 | $html .= ' |
| 237 | <option value="' . esc_attr( $size ) . '" ' . selected( $size, ( isset( $instance['size'] ) ? $instance['size'] : $this->rlg_defaults['size'] ), false ) . '>' . esc_html( $size ) . '</option>'; |
| 238 | } |
| 239 | |
| 240 | $html .= ' |
| 241 | </select> |
| 242 | </p> |
| 243 | <p> |
| 244 | <label for="' . $this->get_field_id( 'columns' ) . '">' . esc_html__( 'Number of columns', 'responsive-lightbox' ) . ':</label> |
| 245 | <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'] ) . '" /> |
| 246 | </p> |
| 247 | <p> |
| 248 | <label for="' . $this->get_field_id( 'atts' ) . '">' . esc_html__( 'Custom gallery shortcode attributes', 'responsive-lightbox' ) . ':</label> |
| 249 | <br /> |
| 250 | <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> |
| 251 | </p> |
| 252 | </div>'; |
| 253 | |
| 254 | echo $html; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Save widget form. |
| 259 | * |
| 260 | * @param array $new_instance |
| 261 | * @param array $old_instance |
| 262 | * @return array |
| 263 | */ |
| 264 | public function update( $new_instance, $old_instance ) { |
| 265 | // title |
| 266 | $old_instance['title'] = sanitize_text_field( isset( $new_instance['title'] ) ? trim( $new_instance['title'] ) : $this->rlg_defaults['title'] ); |
| 267 | |
| 268 | // order by |
| 269 | $old_instance['orderby'] = isset( $new_instance['orderby'] ) && array_key_exists( $new_instance['orderby'], $this->rlg_orders ) ? $new_instance['orderby'] : $this->rlg_defaults['orderby']; |
| 270 | |
| 271 | // order |
| 272 | $old_instance['order'] = isset( $new_instance['order'] ) && array_key_exists( $new_instance['order'], $this->rlg_order_types ) ? $new_instance['order'] : $this->rlg_defaults['order']; |
| 273 | |
| 274 | // image size |
| 275 | $old_instance['size'] = isset( $new_instance['size'] ) && in_array( $new_instance['size'], $this->rlg_image_sizes, true ) ? $new_instance['size'] : $this->rlg_defaults['size']; |
| 276 | |
| 277 | // gallery type |
| 278 | $old_instance['type'] = isset( $new_instance['type'] ) && array_key_exists( $new_instance['type'], $this->rlg_gallery_types ) ? $new_instance['type'] : $this->rlg_defaults['type']; |
| 279 | |
| 280 | // number of columns |
| 281 | $old_instance['columns'] = isset( $new_instance['columns'] ) ? ( ( $columns = (int) $new_instance['columns'] ) > 0 ? $columns : $this->rlg_defaults['columns'] ) : $this->rlg_defaults['columns']; |
| 282 | |
| 283 | // image ids |
| 284 | if ( ! empty( $new_instance['ids'] ) && is_string( $new_instance['ids'] ) ) { |
| 285 | // get unique and non empty attachment ids only |
| 286 | $old_instance['ids'] = implode( ',', array_unique( array_filter( array_map( 'intval', explode( ',', $new_instance['ids'] ) ) ) ) ); |
| 287 | } else |
| 288 | $old_instance['ids'] = $this->rlg_defaults['ids']; |
| 289 | |
| 290 | // custom attributes |
| 291 | $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'] ) ) ) ); |
| 292 | |
| 293 | $new_atts = []; |
| 294 | |
| 295 | if ( $atts !== '' ) { |
| 296 | $atts_exp = explode( '" ', $atts ); |
| 297 | |
| 298 | if ( ! empty( $atts_exp ) ) { |
| 299 | end( $atts_exp ); |
| 300 | |
| 301 | $last = key( $atts_exp ); |
| 302 | |
| 303 | reset( $atts_exp ); |
| 304 | |
| 305 | foreach ( $atts_exp as $id => $attribute ) { |
| 306 | $check = $attribute . ( $last === $id ? '' : '"' ); |
| 307 | |
| 308 | if ( preg_match( '/^[a-z0-9_-]+=\"(.+?)\"$/', $check ) === 1 ) |
| 309 | $new_atts[] = $check; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if ( ! empty( $new_atts ) ) |
| 315 | $old_instance['atts'] = implode( ' ', $new_atts ); |
| 316 | else |
| 317 | $old_instance['atts'] = ''; |
| 318 | |
| 319 | return $old_instance; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Responsive Lightbox Gallery Widget class. |
| 325 | * |
| 326 | * @class Responsive_Lightbox_Gallery_Widget |
| 327 | */ |
| 328 | class Responsive_Lightbox_Image_Widget extends WP_Widget { |
| 329 | |
| 330 | private $rli_defaults = []; |
| 331 | private $rli_text_positions = []; |
| 332 | private $rli_link_to = []; |
| 333 | private $rli_aligns = []; |
| 334 | private $rli_image_sizes = []; |
| 335 | |
| 336 | /** |
| 337 | * Class constructor. |
| 338 | * |
| 339 | * @return void |
| 340 | */ |
| 341 | public function __construct() { |
| 342 | parent::__construct( |
| 343 | 'Responsive_Lightbox_Image_Widget', |
| 344 | __( 'Image', 'responsive-lightbox' ), |
| 345 | [ |
| 346 | 'description' => __( 'Displays a single image.', 'responsive-lightbox' ), |
| 347 | 'classname' => 'rl-image-widget' |
| 348 | ] |
| 349 | ); |
| 350 | |
| 351 | $this->rli_defaults = [ |
| 352 | 'title' => __( 'Image', 'responsive-lightbox' ), |
| 353 | 'image_id' => 0, |
| 354 | 'responsive' => true, |
| 355 | 'size' => 'thumbnail', |
| 356 | 'link_to' => 'file', |
| 357 | 'link_custom_url' => '', |
| 358 | 'image_align' => 'none', |
| 359 | 'text' => '', |
| 360 | 'autobr' => false, |
| 361 | 'text_position' => 'below_image', |
| 362 | 'text_align' => 'none' |
| 363 | ]; |
| 364 | |
| 365 | $this->rli_text_positions = [ |
| 366 | 'below_image' => __( 'Below the image', 'responsive-lightbox' ), |
| 367 | 'above_image' => __( 'Above the image', 'responsive-lightbox' ) |
| 368 | ]; |
| 369 | |
| 370 | $this->rli_link_to = [ |
| 371 | 'none' => __( 'None', 'responsive-lightbox' ), |
| 372 | 'file' => __( 'Media File', 'responsive-lightbox' ), |
| 373 | 'post' => __( 'Attachment Page', 'responsive-lightbox' ), |
| 374 | 'custom' => __( 'Custom URL', 'responsive-lightbox' ) |
| 375 | ]; |
| 376 | |
| 377 | $this->rli_aligns = [ |
| 378 | 'none' => __( 'None', 'responsive-lightbox' ), |
| 379 | 'left' => __( 'Left', 'responsive-lightbox' ), |
| 380 | 'center' => __( 'Center', 'responsive-lightbox' ), |
| 381 | 'right' => __( 'Right', 'responsive-lightbox' ), |
| 382 | 'justify' => __( 'Justify', 'responsive-lightbox' ) |
| 383 | ]; |
| 384 | |
| 385 | $this->rli_image_sizes = array_merge( [ 'full' ], get_intermediate_image_sizes() ); |
| 386 | |
| 387 | sort( $this->rli_image_sizes, SORT_STRING ); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Display widget. |
| 392 | * |
| 393 | * @param array $args |
| 394 | * @param array $instance |
| 395 | * @return void |
| 396 | */ |
| 397 | public function widget( $args, $instance ) { |
| 398 | switch ( $instance['link_to'] ) { |
| 399 | case 'file': |
| 400 | $file = wp_get_attachment_image_src( $instance['image_id'], 'full', false ); |
| 401 | $href = $file[0]; |
| 402 | break; |
| 403 | |
| 404 | case 'post': |
| 405 | $href = get_permalink( $instance['image_id'] ); |
| 406 | break; |
| 407 | |
| 408 | case 'custom': |
| 409 | $href = $instance['link_custom_url']; |
| 410 | break; |
| 411 | |
| 412 | case 'none': |
| 413 | default: |
| 414 | $href = ''; |
| 415 | } |
| 416 | |
| 417 | // image align |
| 418 | if ( $instance['image_align'] === 'left' ) |
| 419 | $image_align = 'float: left;'; |
| 420 | elseif ( $instance['image_align'] === 'center' ) |
| 421 | $image_align = 'margin-left: auto; margin-right: auto; display: block;'; |
| 422 | elseif ( $instance['image_align'] === 'right' ) |
| 423 | $image_align = 'float: right;'; |
| 424 | else |
| 425 | $image_align = ''; |
| 426 | |
| 427 | // text align |
| 428 | if ( $instance['text_align'] === 'left' ) |
| 429 | $text_align = 'text-align: left; display: block;'; |
| 430 | elseif ( $instance['text_align'] === 'center' ) |
| 431 | $text_align = 'text-align: center; display: block;'; |
| 432 | elseif ( $instance['text_align'] === 'right' ) |
| 433 | $text_align = 'text-align: right; display: block;'; |
| 434 | elseif ( $instance['text_align'] === 'justify' ) |
| 435 | $text_align = 'text-align: justify; display: block;'; |
| 436 | else |
| 437 | $text_align = ''; |
| 438 | |
| 439 | // get image data |
| 440 | $image = wp_get_attachment_image_src( $instance['image_id'], $instance['size'], false ); |
| 441 | |
| 442 | $width = $instance['responsive'] === false ? $image[1] : '100%'; |
| 443 | $height = $instance['responsive'] === false ? $image[2] : 'auto'; |
| 444 | $post = get_post( $instance['image_id'] ); |
| 445 | $image_title = isset( $post->post_title ) ? $post->post_title : ''; |
| 446 | $alt = (string) get_post_meta( $instance['image_id'], '_wp_attachment_image_alt', true ); |
| 447 | $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); |
| 448 | |
| 449 | // start output |
| 450 | $html = $args['before_widget'] . $args['before_title'] . ( $instance['title'] !== '' ? esc_html( $instance['title'] ) : '' ) . $args['after_title']; |
| 451 | |
| 452 | if ( $instance['autobr'] === true ) |
| 453 | $escaped_text = wpautop( esc_html( $instance['text'] ) ); |
| 454 | else |
| 455 | $escaped_text = esc_html( $instance['text'] ); |
| 456 | |
| 457 | $container_html = '<div class="rl-image-widget-text" style="' . esc_attr( $text_align ) . '">' . $escaped_text . '</div>'; |
| 458 | $image_html = ( $href !== '' ? '<a href="' . esc_url( $href ) . '" class="rl-image-widget-link">' : '' ) . '<img class="rl-image-widget-image" src="' . esc_url( $image[0] ) . '" width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" title="' . esc_attr( $image_title ) . '" alt="' . esc_attr( $alt ) . '" style="' . esc_attr( $image_align ) . '" />' . ( $href !== '' ? '</a>' : '' ); |
| 459 | |
| 460 | if ( $instance['text_position'] === 'below_image' ) |
| 461 | $html .= $image_html . $container_html; |
| 462 | else |
| 463 | $html .= $container_html . $image_html; |
| 464 | |
| 465 | $html .= $args['after_widget']; |
| 466 | |
| 467 | echo apply_filters( 'rl_image_widget_html', $html, $instance ); |
| 468 | } |
| 469 | |
| 470 | /** Render widget form. |
| 471 | * |
| 472 | * @param array $instance |
| 473 | * @return void |
| 474 | */ |
| 475 | public function form( $instance ) { |
| 476 | $image_id = (int) ( isset( $instance['image_id'] ) ? $instance['image_id'] : $this->rli_defaults['image_id'] ); |
| 477 | $image = ''; |
| 478 | |
| 479 | if ( ! empty( $image_id ) ) |
| 480 | $image = wp_get_attachment_image( $image_id, 'medium', false ); |
| 481 | |
| 482 | if ( ! $image ) |
| 483 | $image = wp_get_attachment_image( $image_id, 'full', false ); |
| 484 | |
| 485 | $html = ' |
| 486 | <div class="rl-image-widget-container"> |
| 487 | <p> |
| 488 | <label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Title', 'responsive-lightbox' ) . '</label> |
| 489 | <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'] ) . '" /> |
| 490 | </p> |
| 491 | <div class="rl-image-widget' . ( ! empty( $image_id ) ? ' has-image' : '' ) . '"> |
| 492 | <input class="rl-image-widget-image-id" type="hidden" name="' . $this->get_field_name( 'image_id' ) . '" value="' . (int) $image_id . '" /> |
| 493 | <a href="#" class="rl-image-widget-select button button-secondary">' . esc_html__( 'Select image', 'responsive-lightbox' ) . '</a> |
| 494 | <div class="rl-image-widget-content">'; |
| 495 | |
| 496 | if ( ! empty( $image ) ) |
| 497 | $html .= $image; |
| 498 | |
| 499 | $html .= ' |
| 500 | </div> |
| 501 | </div> |
| 502 | <p> |
| 503 | <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> |
| 504 | </p>'; |
| 505 | |
| 506 | $html .= ' |
| 507 | <p> |
| 508 | <label for="' . $this->get_field_id( 'size' ) . '">' . esc_html__( 'Size', 'responsive-lightbox' ) . '</label> |
| 509 | <select class="rl-image-size-select widefat" id="' . $this->get_field_id( 'size' ) . '" name="' . $this->get_field_name( 'size' ) . '">'; |
| 510 | |
| 511 | $size_type = ( isset( $instance['size'] ) ? $instance['size'] : $this->rli_defaults['size'] ); |
| 512 | |
| 513 | foreach ( $this->rli_image_sizes as $size ) { |
| 514 | $html .= ' |
| 515 | <option value="' . esc_attr( $size ) . '" ' . selected( $size, $size_type, false ) . '>' . esc_html( $size ) . '</option>'; |
| 516 | } |
| 517 | |
| 518 | $html .= ' |
| 519 | </select> |
| 520 | </p> |
| 521 | <p> |
| 522 | <label for="' . $this->get_field_id( 'link_to' ) . '">' . esc_html__( 'Link to', 'responsive-lightbox' ) . '</label> |
| 523 | <select class="rl-image-link-to widefat" id="' . $this->get_field_id( 'link_to' ) . '" name="' . $this->get_field_name( 'link_to' ) . '">'; |
| 524 | |
| 525 | $link_type = ( isset( $instance['link_to'] ) ? $instance['link_to'] : $this->rli_defaults['link_to'] ); |
| 526 | |
| 527 | foreach ( $this->rli_link_to as $id => $type ) { |
| 528 | $html .= ' |
| 529 | <option value="' . esc_attr( $id ) . '" ' . selected( $id, $link_type, false ) . '>' . esc_html( $type ) . '</option>'; |
| 530 | } |
| 531 | |
| 532 | $html .= ' |
| 533 | </select> |
| 534 | </p> |
| 535 | <p class="rl-image-link-url"' . ( $link_type === 'custom' ? '' : ' style="display: none;"' ) . '> |
| 536 | <label for="' . $this->get_field_id( 'link_custom_url' ) . '">' . esc_html__( 'URL', 'responsive-lightbox' ) . '</label> |
| 537 | <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'] ) . '" /> |
| 538 | </p>'; |
| 539 | |
| 540 | $html .= ' |
| 541 | <p> |
| 542 | <label for="' . $this->get_field_id( 'image_align' ) . '">' . esc_html__( 'Image align', 'responsive-lightbox' ) . '</label> |
| 543 | <select id="' . $this->get_field_id( 'image_align' ) . '" class="widefat" name="' . $this->get_field_name( 'image_align' ) . '">'; |
| 544 | |
| 545 | foreach ( $this->rli_aligns as $id => $image_align ) { |
| 546 | if ( $id !== 'justify' ) |
| 547 | $html .= ' |
| 548 | <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>'; |
| 549 | } |
| 550 | |
| 551 | $html .= ' |
| 552 | </select> |
| 553 | </p> |
| 554 | <p> |
| 555 | <label for="' . $this->get_field_id( 'text' ) . '">' . esc_html__( 'Text', 'responsive-lightbox' ) . '</label> |
| 556 | <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> |
| 557 | </p> |
| 558 | <p> |
| 559 | <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> |
| 560 | </p>'; |
| 561 | |
| 562 | $html .= ' |
| 563 | <p> |
| 564 | <label for="' . $this->get_field_id( 'text_position' ) . '">' . esc_html__( 'Text position', 'responsive-lightbox' ) . '</label> |
| 565 | <select id="' . $this->get_field_id( 'text_position' ) . '" class="widefat" name="' . $this->get_field_name( 'text_position' ) . '">'; |
| 566 | |
| 567 | foreach ( $this->rli_text_positions as $id => $text_position ) { |
| 568 | $html .= ' |
| 569 | <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>'; |
| 570 | } |
| 571 | |
| 572 | $html .= ' |
| 573 | </select> |
| 574 | </p> |
| 575 | <label for="' . $this->get_field_id( 'text_align' ) . '">' . esc_html__( 'Text align', 'responsive-lightbox' ) . '</label> |
| 576 | <select id="' . $this->get_field_id( 'text_align' ) . '" class="widefat" name="' . $this->get_field_name( 'text_align' ) . '">'; |
| 577 | |
| 578 | foreach ( $this->rli_aligns as $id => $text_align ) { |
| 579 | $html .= ' |
| 580 | <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>'; |
| 581 | } |
| 582 | |
| 583 | $html .= ' |
| 584 | </select> |
| 585 | |
| 586 | </div>'; |
| 587 | |
| 588 | echo $html; |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Save widget form. |
| 593 | * |
| 594 | * @param array $new_instance |
| 595 | * @param array $old_instance |
| 596 | * @return array |
| 597 | */ |
| 598 | public function update( $new_instance, $old_instance ) { |
| 599 | // whitelists |
| 600 | $old_instance['size'] = isset( $new_instance['size'] ) && in_array( $new_instance['size'], $this->rli_image_sizes, true ) ? $new_instance['size'] : $this->rli_defaults['size']; |
| 601 | $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']; |
| 602 | $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']; |
| 603 | $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']; |
| 604 | $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']; |
| 605 | |
| 606 | // booleands |
| 607 | $old_instance['responsive'] = ! empty( $new_instance['responsive'] ); |
| 608 | $old_instance['autobr'] = ! empty( $new_instance['autobr'] ); |
| 609 | |
| 610 | // texts |
| 611 | $old_instance['title'] = sanitize_text_field( isset( $new_instance['title'] ) ? $new_instance['title'] : $this->rli_defaults['title'] ); |
| 612 | $old_instance['text'] = isset( $new_instance['text'] ) ? wp_kses_post( $new_instance['text'] ) : $this->rli_defaults['text']; |
| 613 | |
| 614 | // integers |
| 615 | $old_instance['image_id'] = isset( $new_instance['image_id'] ) ? (int) $new_instance['image_id'] : $this->rli_defaults['image_id']; |
| 616 | |
| 617 | // urls |
| 618 | $old_instance['link_custom_url'] = isset( $new_instance['link_custom_url'] ) ? esc_url( $new_instance['link_custom_url'] ) : $this->rli_defaults['link_custom_url']; |
| 619 | |
| 620 | return $old_instance; |
| 621 | } |
| 622 | |
| 623 | } |