image-widget
Last commit date
lang
3 years ago
lib
3 years ago
resources
3 years ago
views
3 years ago
.gitignore
3 years ago
image-widget.php
3 years ago
index.php
3 years ago
readme.txt
1 year ago
image-widget.php
486 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Image Widget |
| 4 | Plugin URI: https://wordpress.org/plugins/image-widget/ |
| 5 | Description: A simple image widget that uses the native WordPress media manager to add image widgets to your site. |
| 6 | Author: The Events Calendar |
| 7 | Version: 4.4.9 |
| 8 | Author URI: https://evnt.is/1aor |
| 9 | Text Domain: image-widget |
| 10 | Domain Path: /lang |
| 11 | */ |
| 12 | |
| 13 | // Block direct requests |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | die( '-1' ); |
| 16 | } |
| 17 | |
| 18 | // Load the widget on widgets_init |
| 19 | function tribe_load_image_widget() { |
| 20 | register_widget( 'Tribe_Image_Widget' ); |
| 21 | } |
| 22 | add_action( 'widgets_init', 'tribe_load_image_widget' ); |
| 23 | |
| 24 | class Tribe_Image_Widget extends WP_Widget { |
| 25 | |
| 26 | const VERSION = '4.4.9'; |
| 27 | |
| 28 | const CUSTOM_IMAGE_SIZE_SLUG = 'tribe_image_widget_custom'; |
| 29 | |
| 30 | const VERSION_KEY = '_image_widget_version'; |
| 31 | |
| 32 | /** |
| 33 | * Tribe Image Widget constructor |
| 34 | */ |
| 35 | public function __construct() { |
| 36 | load_plugin_textdomain( 'image-widget', false, trailingslashit( basename( dirname( __FILE__ ) ) ) . 'lang/' ); |
| 37 | |
| 38 | $widget_ops = array( 'classname' => 'widget_sp_image', 'description' => __( 'Showcase a single image with a Title, URL, and a Description', 'image-widget' ) ); |
| 39 | $control_ops = array( 'id_base' => 'widget_sp_image' ); |
| 40 | |
| 41 | parent::__construct( 'widget_sp_image', __( 'Image Widget', 'image-widget' ), $widget_ops, $control_ops ); |
| 42 | |
| 43 | if ( $this->use_old_uploader() ) { |
| 44 | require_once( 'lib/ImageWidgetDeprecated.php' ); |
| 45 | new ImageWidgetDeprecated( $this ); |
| 46 | } else { |
| 47 | add_action( 'sidebar_admin_setup', array( $this, 'admin_setup' ) ); |
| 48 | } |
| 49 | |
| 50 | // fire admin_setup if we are in the customizer |
| 51 | add_action( 'admin_enqueue_scripts', array( $this, 'maybe_admin_setup' ) ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Test to see if this version of WordPress supports the new image manager. |
| 56 | * |
| 57 | * @return bool True if the current version of WordPress does NOT support the current image management tech. |
| 58 | */ |
| 59 | private function use_old_uploader() { |
| 60 | if ( defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | return ! function_exists( 'wp_enqueue_media' ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Enqueue all the javascript and CSS. |
| 69 | */ |
| 70 | public function admin_setup() { |
| 71 | wp_enqueue_media(); |
| 72 | |
| 73 | wp_enqueue_style( 'tribe-image-widget', plugins_url( 'resources/css/admin.css', __FILE__ ), array(), self::VERSION ); |
| 74 | wp_enqueue_script( 'tribe-image-widget', plugins_url( 'resources/js/image-widget.js', __FILE__ ), array( 'jquery', 'media-upload', 'media-views' ), self::VERSION ); |
| 75 | |
| 76 | wp_localize_script( 'tribe-image-widget', 'TribeImageWidget', array( |
| 77 | 'frame_title' => __( 'Select an Image', 'image-widget' ), |
| 78 | 'button_title' => __( 'Insert Into Widget', 'image-widget' ), |
| 79 | ) ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Trigger the enqueueing of admin scripts and styles on widget admin page and in the "Customizer" view. |
| 84 | */ |
| 85 | public function maybe_admin_setup() { |
| 86 | $screen = get_current_screen(); |
| 87 | |
| 88 | if ( 'customize' !== $screen->base ) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | $this->admin_setup(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Widget frontend output |
| 97 | * |
| 98 | * @param array $args |
| 99 | * @param array $instance |
| 100 | */ |
| 101 | public function widget( $args, $instance ) { |
| 102 | extract( $args ); |
| 103 | |
| 104 | $instance = wp_parse_args( (array) $instance, self::get_defaults() ); |
| 105 | |
| 106 | if ( ! empty( $instance['imageurl'] ) || ! empty( $instance['attachment_id'] ) ) { |
| 107 | |
| 108 | $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'] ); |
| 109 | $instance['description'] = apply_filters( 'widget_text', $instance['description'], $args, $instance ); |
| 110 | $instance['link'] = apply_filters( 'image_widget_image_link', esc_url( $instance['link'] ), $args, $instance ); |
| 111 | $instance['linkid'] = apply_filters( 'image_widget_image_link_id', esc_attr( $instance['linkid'] ), $args, $instance ); |
| 112 | $instance['linktitle'] = apply_filters( 'image_widget_image_link_title', esc_attr( $instance['linktitle'] ), $args, $instance ); |
| 113 | $instance['linktarget'] = apply_filters( 'image_widget_image_link_target', esc_attr( $instance['linktarget'] ), $args, $instance ); |
| 114 | $instance['width'] = apply_filters( 'image_widget_image_width', abs( $instance['width'] ), $args, $instance ); |
| 115 | $instance['height'] = apply_filters( 'image_widget_image_height', abs( $instance['height'] ), $args, $instance ); |
| 116 | $instance['maxwidth'] = apply_filters( 'image_widget_image_maxwidth', esc_attr( $instance['maxwidth'] ), $args, $instance ); |
| 117 | $instance['maxheight'] = apply_filters( 'image_widget_image_maxheight', esc_attr( $instance['maxheight'] ), $args, $instance ); |
| 118 | $instance['align'] = apply_filters( 'image_widget_image_align', esc_attr( $instance['align'] ), $args, $instance ); |
| 119 | $instance['alt'] = apply_filters( 'image_widget_image_alt', esc_attr( $instance['alt'] ), $args, $instance ); |
| 120 | $instance['rel'] = apply_filters( 'image_widget_image_rel', esc_attr( $instance['rel'] ), $args, $instance ); |
| 121 | |
| 122 | if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) { |
| 123 | $instance['attachment_id'] = ( $instance['attachment_id'] > 0 ) ? $instance['attachment_id'] : $instance['image']; |
| 124 | $instance['attachment_id'] = apply_filters( 'image_widget_image_attachment_id', abs( $instance['attachment_id'] ), $args, $instance ); |
| 125 | $instance['size'] = apply_filters( 'image_widget_image_size', esc_attr( $instance['size'] ), $args, $instance ); |
| 126 | } |
| 127 | |
| 128 | $instance['imageurl'] = apply_filters( 'image_widget_image_url', esc_url( $instance['imageurl'] ), $args, $instance ); |
| 129 | |
| 130 | // No longer using extracted vars. This is here for backwards compatibility. |
| 131 | extract( $instance ); |
| 132 | |
| 133 | include( $this->getTemplateHierarchy( 'widget' ) ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Update widget options |
| 139 | * |
| 140 | * @param object $new_instance Widget Instance |
| 141 | * @param object $old_instance Widget Instance |
| 142 | * @return object |
| 143 | */ |
| 144 | public function update( $new_instance, $old_instance ) { |
| 145 | |
| 146 | $instance = $old_instance; |
| 147 | $new_instance = wp_parse_args( (array) $new_instance, self::get_defaults() ); |
| 148 | |
| 149 | $instance['title'] = strip_tags( $new_instance['title'] ); |
| 150 | |
| 151 | if ( current_user_can( 'unfiltered_html' ) ) { |
| 152 | $instance['description'] = $new_instance['description']; |
| 153 | } else { |
| 154 | $instance['description'] = wp_kses_post( $new_instance['description'] ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Make the description filterable; especially useful for working with allowing/disallowing HTML and other content. |
| 159 | * |
| 160 | * @since 4.4.6 |
| 161 | * |
| 162 | * @param $description The current value of $instance['description']. |
| 163 | * @param $new_instance The new instance of the widget, i.e. the new values being saved for it. |
| 164 | * @param $old_instance The pre-existing instance of the widget, i.e. the values that are potentially being updated. |
| 165 | */ |
| 166 | $instance['description'] = apply_filters( 'tribe_image_widget_instance_description', $instance['description'], $new_instance, $old_instance ); |
| 167 | |
| 168 | $instance['link'] = $new_instance['link']; |
| 169 | $instance['linktitle'] = $new_instance['linktitle']; |
| 170 | $instance['linkid'] = $new_instance['linkid']; |
| 171 | $instance['linktarget'] = $new_instance['linktarget']; |
| 172 | $instance['width'] = abs( $new_instance['width'] ); |
| 173 | $instance['height'] = abs( $new_instance['height'] ); |
| 174 | |
| 175 | if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) { |
| 176 | $instance['size'] = $new_instance['size']; |
| 177 | } |
| 178 | |
| 179 | $instance['align'] = $new_instance['align']; |
| 180 | $instance['alt'] = $new_instance['alt']; |
| 181 | $instance['rel'] = $new_instance['rel']; |
| 182 | |
| 183 | // Reverse compatibility with $image, now called $attachement_id |
| 184 | if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) && $new_instance['attachment_id'] > 0 ) { |
| 185 | $instance['attachment_id'] = abs( $new_instance['attachment_id'] ); |
| 186 | } elseif ( $new_instance['image'] > 0 ) { |
| 187 | $instance['attachment_id'] = $instance['image'] = abs( $new_instance['image'] ); |
| 188 | if ( class_exists( 'ImageWidgetDeprecated' ) ) { |
| 189 | $instance['imageurl'] = ImageWidgetDeprecated::get_image_url( $instance['image'], $instance['width'], $instance['height'] ); // image resizing not working right now |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | $instance['imageurl'] = $new_instance['imageurl']; // deprecated |
| 194 | $instance['aspect_ratio'] = $this->get_image_aspect_ratio( $instance ); |
| 195 | |
| 196 | return $instance; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Form UI |
| 201 | * |
| 202 | * @param object $instance Widget Instance |
| 203 | */ |
| 204 | public function form( $instance ) { |
| 205 | $instance = wp_parse_args( (array) $instance, self::get_defaults() ); |
| 206 | if ( $this->use_old_uploader() ) { |
| 207 | include( $this->getTemplateHierarchy( 'widget-admin.deprecated' ) ); |
| 208 | } else { |
| 209 | include( $this->getTemplateHierarchy( 'widget-admin' ) ); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Render an array of default values. |
| 215 | * |
| 216 | * @return array default values |
| 217 | */ |
| 218 | private static function get_defaults() { |
| 219 | |
| 220 | $defaults = array( |
| 221 | 'title' => '', |
| 222 | 'description' => '', |
| 223 | 'link' => '', |
| 224 | 'linkid' => '', |
| 225 | 'linktitle' => '', |
| 226 | 'linktarget' => '', |
| 227 | 'width' => 0, |
| 228 | 'height' => 0, |
| 229 | 'maxwidth' => '100%', |
| 230 | 'maxheight' => '', |
| 231 | 'image' => 0, // reverse compatible - now attachement_id |
| 232 | 'imageurl' => '', // reverse compatible. |
| 233 | 'align' => 'none', |
| 234 | 'alt' => '', |
| 235 | 'rel' => '', |
| 236 | ); |
| 237 | |
| 238 | if ( ! defined( 'IMAGE_WIDGET_COMPATIBILITY_TEST' ) ) { |
| 239 | $defaults['size'] = self::CUSTOM_IMAGE_SIZE_SLUG; |
| 240 | $defaults['attachment_id'] = 0; |
| 241 | } |
| 242 | /** |
| 243 | * Allow users to customize the default values of various Image Widget options. |
| 244 | * |
| 245 | * @since 4.4.7 |
| 246 | * |
| 247 | * @param array $defaults The array of default option values. |
| 248 | */ |
| 249 | return apply_filters( 'image_widget_option_defaults', $defaults ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Render the image html output. |
| 254 | * |
| 255 | * @param array $instance |
| 256 | * @param bool $include_link will only render the link if this is set to true. Otherwise link is ignored. |
| 257 | * @return string image html |
| 258 | */ |
| 259 | private function get_image_html( $instance, $include_link = true ) { |
| 260 | |
| 261 | // Backwards compatible image display. |
| 262 | if ( $instance['attachment_id'] == 0 && $instance['image'] > 0 ) { |
| 263 | $instance['attachment_id'] = $instance['image']; |
| 264 | } |
| 265 | |
| 266 | $output = ''; |
| 267 | |
| 268 | if ( $include_link && ! empty( $instance['link'] ) ) { |
| 269 | |
| 270 | $attr = array( |
| 271 | 'href' => $instance['link'], |
| 272 | 'id' => $instance['linkid'], |
| 273 | 'target' => $instance['linktarget'], |
| 274 | 'class' => $this->widget_options['classname'] . '-image-link', |
| 275 | 'title' => ( ! empty( $instance['linktitle'] ) ) ? $instance['linktitle'] : $instance['title'], |
| 276 | 'rel' => $instance['rel'], |
| 277 | ); |
| 278 | |
| 279 | $attr = apply_filters( 'image_widget_link_attributes', $attr, $instance ); |
| 280 | $attr = array_map( 'esc_attr', $attr ); |
| 281 | |
| 282 | $output = '<a'; |
| 283 | |
| 284 | foreach ( $attr as $name => $value ) { |
| 285 | if ( ! empty( $value ) ) { |
| 286 | $output .= sprintf( ' %s="%s"', $name, $value ); |
| 287 | } |
| 288 | } |
| 289 | $output .= '>'; |
| 290 | } |
| 291 | |
| 292 | $size = $this->get_image_size( $instance ); |
| 293 | |
| 294 | if ( is_array( $size ) ) { |
| 295 | $instance['width'] = $size[0]; |
| 296 | $instance['height'] = $size[1]; |
| 297 | } elseif ( ! empty( $instance['attachment_id'] ) ) { |
| 298 | //$instance['width'] = $instance['height'] = 0; |
| 299 | $image_details = wp_get_attachment_image_src( $instance['attachment_id'], $size ); |
| 300 | if ( $image_details ) { |
| 301 | $instance['imageurl'] = $image_details[0]; |
| 302 | $instance['width'] = $image_details[1]; |
| 303 | $instance['height'] = $image_details[2]; |
| 304 | } |
| 305 | |
| 306 | $image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) |
| 307 | ? wp_get_attachment_image_srcset( $instance['attachment_id'], $size ) |
| 308 | : false; |
| 309 | if ( $image_srcset ) { |
| 310 | $instance['srcset'] = $image_srcset; |
| 311 | |
| 312 | $image_sizes = function_exists( 'wp_get_attachment_image_sizes' ) |
| 313 | ? wp_get_attachment_image_sizes( $instance['attachment_id'], $size ) |
| 314 | : false; |
| 315 | if ( $image_sizes ) { |
| 316 | $instance['sizes'] = $image_sizes; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | $instance['width'] = abs( $instance['width'] ); |
| 322 | $instance['height'] = abs( $instance['height'] ); |
| 323 | |
| 324 | $attr = array(); |
| 325 | |
| 326 | if ( ! empty( $instance['alt'] ) ) { |
| 327 | $attr['alt'] = $instance['alt']; |
| 328 | } elseif ( ! empty( $instance['title'] ) ) { |
| 329 | $attr['alt'] = $instance['title']; |
| 330 | } |
| 331 | |
| 332 | if ( is_array( $size ) ) { |
| 333 | $attr['class'] = 'attachment-' . join( 'x', $size ); |
| 334 | } else { |
| 335 | $attr['class'] = 'attachment-' . $size; |
| 336 | } |
| 337 | |
| 338 | $attr['style'] = ''; |
| 339 | if ( ! empty( $instance['maxwidth'] ) ) { |
| 340 | $attr['style'] .= "max-width: {$instance['maxwidth']};"; |
| 341 | } |
| 342 | |
| 343 | if ( ! empty( $instance['maxheight'] ) ) { |
| 344 | $attr['style'] .= "max-height: {$instance['maxheight']};"; |
| 345 | } |
| 346 | |
| 347 | if ( ! empty( $instance['align'] ) && $instance['align'] != 'none' ) { |
| 348 | $attr['class'] .= " align{$instance['align']}"; |
| 349 | } |
| 350 | |
| 351 | if ( ! empty( $instance['srcset'] ) ) { |
| 352 | $attr['srcset'] = $instance['srcset']; |
| 353 | } |
| 354 | |
| 355 | if ( ! empty( $instance['sizes'] ) ) { |
| 356 | $attr['sizes'] = $instance['sizes']; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Allow filtering of the image attributes used on the front-end. |
| 361 | * |
| 362 | * @param array $attr Image attributes to be filtered. |
| 363 | * @param array $instance The current widget instance. |
| 364 | */ |
| 365 | $attr = apply_filters( 'image_widget_image_attributes', $attr, $instance ); |
| 366 | |
| 367 | // If there is an imageurl, use it to render the image. Eventually we should kill this and simply rely on attachment_ids. |
| 368 | if ( ! empty( $instance['imageurl'] ) ) { |
| 369 | // If all we have is an image src url we can still render an image. |
| 370 | $attr['src'] = $instance['imageurl']; |
| 371 | $attr = array_map( 'esc_attr', $attr ); |
| 372 | $hwstring = image_hwstring( $instance['width'], $instance['height'] ); |
| 373 | |
| 374 | $output .= rtrim( "<img $hwstring" ); |
| 375 | |
| 376 | foreach ( $attr as $name => $value ) { |
| 377 | $output .= sprintf( ' %s="%s"', $name, $value ); |
| 378 | } |
| 379 | |
| 380 | $output .= ' />'; |
| 381 | |
| 382 | } elseif ( abs( $instance['attachment_id'] ) > 0 ) { |
| 383 | $output .= wp_get_attachment_image( $instance['attachment_id'], $size, false, $attr ); |
| 384 | } |
| 385 | |
| 386 | if ( $include_link && ! empty( $instance['link'] ) ) { |
| 387 | $output .= '</a>'; |
| 388 | } |
| 389 | |
| 390 | return $output; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Get all possible image sizes to choose from |
| 395 | * |
| 396 | * @return array |
| 397 | */ |
| 398 | private function possible_image_sizes() { |
| 399 | $registered = get_intermediate_image_sizes(); |
| 400 | // label other sizes with their image size "ID" |
| 401 | $registered = array_combine( $registered, $registered ); |
| 402 | |
| 403 | $possible_sizes = array_merge( $registered, array( |
| 404 | 'full' => __( 'Full Size', 'image-widget' ), |
| 405 | 'thumbnail' => __( 'Thumbnail', 'image-widget' ), |
| 406 | 'medium' => __( 'Medium', 'image-widget' ), |
| 407 | 'large' => __( 'Large', 'image-widget' ), |
| 408 | self::CUSTOM_IMAGE_SIZE_SLUG => __( 'Custom', 'image-widget' ), |
| 409 | ) ); |
| 410 | |
| 411 | /** |
| 412 | * Allow filtering the image sizes available for use in the Image Widget dropdown |
| 413 | * |
| 414 | * @param array $possible_sizes The array of available sizes. |
| 415 | */ |
| 416 | return (array) apply_filters( 'image_size_names_choose', $possible_sizes ); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Assesses the image size in case it has not been set or in case there is a mismatch. |
| 421 | * |
| 422 | * @param $instance |
| 423 | * @return array|string |
| 424 | */ |
| 425 | private function get_image_size( $instance ) { |
| 426 | if ( ! empty( $instance['size'] ) && $instance['size'] != self::CUSTOM_IMAGE_SIZE_SLUG ) { |
| 427 | $size = $instance['size']; |
| 428 | } elseif ( isset( $instance['width'] ) && is_numeric( $instance['width'] ) && isset( $instance['height'] ) && is_numeric( $instance['height'] ) ) { |
| 429 | //$size = array(abs($instance['width']),abs($instance['height'])); |
| 430 | $size = array( $instance['width'], $instance['height'] ); |
| 431 | } else { |
| 432 | $size = 'full'; |
| 433 | } |
| 434 | |
| 435 | return $size; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Establish the aspect ratio of the image. |
| 440 | * |
| 441 | * @param $instance |
| 442 | * @return float|number |
| 443 | */ |
| 444 | private function get_image_aspect_ratio( $instance ) { |
| 445 | if ( ! empty( $instance['aspect_ratio'] ) ) { |
| 446 | return abs( $instance['aspect_ratio'] ); |
| 447 | } else { |
| 448 | $attachment_id = ( ! empty( $instance['attachment_id'] ) ) ? $instance['attachment_id'] : $instance['image']; |
| 449 | if ( ! empty( $attachment_id ) ) { |
| 450 | $image_details = wp_get_attachment_image_src( $attachment_id, 'full' ); |
| 451 | if ( $image_details ) { |
| 452 | return ( $image_details[1] / $image_details[2] ); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Loads theme files in appropriate hierarchy: 1) child theme, |
| 460 | * 2) parent template, 3) plugin resources. will look in the image-widget/ |
| 461 | * directory in a theme and the views/ directory in the plugin |
| 462 | * |
| 463 | * @param string $template template file to search for |
| 464 | * @return template path |
| 465 | */ |
| 466 | |
| 467 | public function getTemplateHierarchy( $template ) { |
| 468 | // whether or not .php was added |
| 469 | $template_slug = rtrim( $template, '.php' ); |
| 470 | $template = $template_slug . '.php'; |
| 471 | |
| 472 | if ( $theme_file = locate_template( array( 'image-widget/' . $template ) ) ) { |
| 473 | $file = $theme_file; |
| 474 | } else { |
| 475 | $file = 'views/' . $template; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Allow filtering of currently-retrieved Image Widget template file's path. |
| 480 | * |
| 481 | * @param string $file The retrieved template file's path. |
| 482 | */ |
| 483 | return apply_filters( 'sp_template_image-widget_' . $template, $file ); |
| 484 | } |
| 485 | } |
| 486 |