/modules/widgets/image-widget.php

  1. <?php 
  2. /** 
  3. * Module Name: Image Widget 
  4. * Module Description: Easily add images to your theme's sidebar. 
  5. * Sort Order: 20 
  6. * First Introduced: 1.2 
  7. */ 
  8.  
  9. /** 
  10. * Register the widget for use in Appearance -> Widgets 
  11. */ 
  12. add_action( 'widgets_init', 'jetpack_image_widget_init' ); 
  13. function jetpack_image_widget_init() { 
  14. register_widget( 'Jetpack_Image_Widget' ); 
  15.  
  16. class Jetpack_Image_Widget extends WP_Widget { 
  17. /** 
  18. * Register widget with WordPress. 
  19. */ 
  20. public function __construct() { 
  21. parent::__construct( 
  22. 'image',  
  23. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  24. apply_filters( 'jetpack_widget_name', esc_html__( 'Image', 'jetpack' ) ),  
  25. array( 
  26. 'classname' => 'widget_image',  
  27. 'description' => __( 'Display an image in your sidebar', 'jetpack' ) 
  28. ); 
  29.  
  30. if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) ) { 
  31. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); 
  32.  
  33. /** 
  34. * Loads file for front-end widget style. 
  35. * 
  36. * @uses wp_enqueue_style(), plugins_url() 
  37. */ 
  38. public function enqueue_style() { 
  39. wp_enqueue_style( 'jetpack_image_widget', plugins_url( 'image-widget/style.css', __FILE__ ), array(), '20140808' ); 
  40.  
  41. /** 
  42. * Front-end display of widget. 
  43. * 
  44. * @see WP_Widget::widget() 
  45. * 
  46. * @param array $args Widget arguments. 
  47. * @param array $instance Saved values from database. 
  48. */ 
  49. public function widget( $args, $instance ) { 
  50. extract( $args ); 
  51.  
  52. echo $before_widget; 
  53.  
  54. $instance = wp_parse_args( $instance, array( 
  55. 'title' => '',  
  56. 'img_url' => '' 
  57. ) ); 
  58.  
  59. /** This filter is documented in core/src/wp-includes/default-widgets.php */ 
  60. $title = apply_filters( 'widget_title', $instance['title'] ); 
  61.  
  62. if ( $title ) 
  63. echo $before_title . esc_html( $title ) . $after_title; 
  64.  
  65. if ( '' != $instance['img_url'] ) { 
  66.  
  67. $output = '<img src="' . esc_attr( $instance['img_url'] ) .'" '; 
  68. if ( '' != $instance['alt_text'] ) 
  69. $output .= 'alt="' . esc_attr( $instance['alt_text'] ) .'" '; 
  70. if ( '' != $instance['img_title'] ) 
  71. $output .= 'title="' . esc_attr( $instance['img_title'] ) .'" '; 
  72. if ( '' == $instance['caption'] ) 
  73. $output .= 'class="align' . esc_attr( $instance['align'] ) . '" '; 
  74. if ( '' != $instance['img_width'] ) 
  75. $output .= 'width="' . esc_attr( $instance['img_width'] ) .'" '; 
  76. if ( '' != $instance['img_height'] ) 
  77. $output .= 'height="' . esc_attr( $instance['img_height'] ) .'" '; 
  78. $output .= '/>'; 
  79. if ( '' != $instance['link'] && ! empty( $instance['link_target_blank'] ) ) 
  80. $output = '<a target="_blank" href="' . esc_attr( $instance['link'] ) . '">' . $output . '</a>'; 
  81. if ( '' != $instance['link'] && empty( $instance['link_target_blank'] ) ) 
  82. $output = '<a href="' . esc_attr( $instance['link'] ) . '">' . $output . '</a>'; 
  83. if ( '' != $instance['caption'] ) { 
  84. /** This filter is documented in core/src/wp-includes/default-widgets.php */ 
  85. $caption = apply_filters( 'widget_text', $instance['caption'] ); 
  86. $output = ' ) . '" width="' . esc_attr( $instance['img_width'] ) .'"]' . $output . ' ' . $caption . ''; // wp_kses_post caption on update 
  87.  
  88. echo '<div class="jetpack-image-container">' . do_shortcode( $output ) . '</div>'; 
  89.  
  90. echo "\n" . $after_widget; 
  91.  
  92. /** 
  93. * Sanitize widget form values as they are saved. 
  94. * 
  95. * @see WP_Widget::update() 
  96. * 
  97. * @param array $new_instance Values just sent to be saved. 
  98. * @param array $old_instance Previously saved values from database. 
  99. * 
  100. * @return array Updated safe values to be saved. 
  101. */ 
  102. public function update( $new_instance, $old_instance ) { 
  103. $allowed_caption_html = array( 
  104. 'a' => array( 
  105. 'href' => array(),  
  106. 'title' => array(),  
  107. ),  
  108. 'b' => array(),  
  109. 'em' => array(),  
  110. 'i' => array(),  
  111. 'p' => array(),  
  112. 'strong' => array() 
  113. ); 
  114.  
  115. $instance = $old_instance; 
  116.  
  117. $instance['title'] = strip_tags( $new_instance['title'] ); 
  118. $instance['img_url'] = esc_url( $new_instance['img_url'], null, 'display' ); 
  119. $instance['alt_text'] = strip_tags( $new_instance['alt_text'] ); 
  120. $instance['img_title'] = strip_tags( $new_instance['img_title'] ); 
  121. $instance['caption'] = wp_kses( stripslashes($new_instance['caption'] ), $allowed_caption_html ); 
  122. $instance['align'] = $new_instance['align']; 
  123. $instance['img_width'] = absint( $new_instance['img_width'] ); 
  124. $instance['img_height'] = absint( $new_instance['img_height'] ); 
  125. $instance['link'] = esc_url( $new_instance['link'], null, 'display' ); 
  126. $instance['link_target_blank'] = isset( $new_instance['link_target_blank'] ) ? (bool) $new_instance['link_target_blank'] : false; 
  127.  
  128. return $instance; 
  129.  
  130. /** 
  131. * Back-end widget form. 
  132. * 
  133. * @see WP_Widget::form() 
  134. * 
  135. * @param array $instance Previously saved values from database. 
  136. */ 
  137. public function form( $instance ) { 
  138. // Defaults 
  139. $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'img_url' => '', 'alt_text' => '', 'img_title' => '', 'caption' => '', 'align' => 'none', 'img_width' => '', 'img_height' => '', 'link' => '', 'link_target_blank' => false ) ); 
  140.  
  141. $title = esc_attr( $instance['title'] ); 
  142. $img_url = esc_url( $instance['img_url'], null, 'display' ); 
  143. $alt_text = esc_attr( $instance['alt_text'] ); 
  144. $img_title = esc_attr( $instance['img_title'] ); 
  145. $caption = esc_textarea( $instance['caption'] ); 
  146. $align = esc_attr( $instance['align'] ); 
  147. $img_width = esc_attr( $instance['img_width'] ); 
  148. $img_height = esc_attr( $instance['img_height'] ); 
  149. $link_target_blank = checked( $instance['link_target_blank'], true, false ); 
  150.  
  151. if ( !empty( $instance['img_url'] ) ) { 
  152. // Download the url to a local temp file and then process it with getimagesize so we can optimize browser layout 
  153. $tmp_file = download_url( $instance['img_url'], 10 ); 
  154. if ( ! is_wp_error( $tmp_file ) ) { 
  155. $size = getimagesize( $tmp_file ); 
  156.  
  157. if ( '' == $instance['img_width'] ) { 
  158. $width = $size[0]; 
  159. $img_width = $width; 
  160. } else { 
  161. $img_width = absint( $instance['img_width'] ); 
  162.  
  163. if ( '' == $instance['img_height'] ) { 
  164. $height = $size[1]; 
  165. $img_height = $height; 
  166. } else { 
  167. $img_height = absint( $instance['img_height'] ); 
  168.  
  169. unlink( $tmp_file ); 
  170.  
  171. $link = esc_url( $instance['link'], null, 'display' ); 
  172.  
  173. echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Widget title:', 'jetpack' ) . ' 
  174. <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . $title . '" /> 
  175. </label></p> 
  176. <p><label for="' . $this->get_field_id( 'img_url' ) . '">' . esc_html__( 'Image URL:', 'jetpack' ) . ' 
  177. <input class="widefat" id="' . $this->get_field_id( 'img_url' ) . '" name="' . $this->get_field_name( 'img_url' ) . '" type="text" value="' . $img_url . '" /> 
  178. </label></p> 
  179. <p><label for="' . $this->get_field_id( 'alt_text' ) . '">' . esc_html__( 'Alternate text:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/image-widget/#image-widget-alt-text" target="_blank">( ? )</a> 
  180. <input class="widefat" id="' . $this->get_field_id( 'alt_text' ) . '" name="' . $this->get_field_name( 'alt_text' ) . '" type="text" value="' . $alt_text . '" /> 
  181. </label></p> 
  182. <p><label for="' . $this->get_field_id( 'img_title' ) . '">' . esc_html__( 'Image title:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/image-widget/#image-widget-title" target="_blank">( ? )</a> 
  183. <input class="widefat" id="' . $this->get_field_id( 'img_title' ) . '" name="' . $this->get_field_name( 'img_title' ) . '" type="text" value="' . $img_title . '" /> 
  184. </label></p> 
  185. <p><label for="' . $this->get_field_id( 'caption' ) . '">' . esc_html__( 'Caption:', 'jetpack' ) . ' <a href="http://support.wordpress.com/widgets/image-widget/#image-widget-caption" target="_blank">( ? )</a> 
  186. <textarea class="widefat" id="' . $this->get_field_id( 'caption' ) . '" name="' . $this->get_field_name( 'caption' ) . '" rows="2" cols="20">' . $caption . '</textarea> 
  187. </label></p>'; 
  188.  
  189. $alignments = array( 
  190. 'none' => __( 'None', 'jetpack' ),  
  191. 'left' => __( 'Left', 'jetpack' ),  
  192. 'center' => __( 'Center', 'jetpack' ),  
  193. 'right' => __( 'Right', 'jetpack' ),  
  194. ); 
  195. echo '<p><label for="' . $this->get_field_id( 'align' ) . '">' . esc_html__( 'Image Alignment:', 'jetpack' ) . ' 
  196. <select id="' . $this->get_field_id( 'align' ) . '" name="' . $this->get_field_name( 'align' ) . '">'; 
  197. foreach ( $alignments as $alignment => $alignment_name ) { 
  198. echo '<option value="' . esc_attr( $alignment ) . '" '; 
  199. if ( $alignment == $align ) 
  200. echo 'selected="selected" '; 
  201. echo '>' . esc_html($alignment_name) . "</option>\n"; 
  202. echo '</select></label></p>'; 
  203.  
  204. echo '<p><label for="' . $this->get_field_id( 'img_width' ) . '">' . esc_html__( 'Width:', 'jetpack' ) . ' 
  205. <input size="3" id="' . $this->get_field_id( 'img_width' ) . '" name="' . $this->get_field_name( 'img_width' ) . '" type="text" value="' . $img_width . '" /> 
  206. </label> 
  207. <label for="' . $this->get_field_id( 'img_height' ) . '">' . esc_html__( 'Height:', 'jetpack' ) . ' 
  208. <input size="3" id="' . $this->get_field_id( 'img_height' ) . '" name="' . $this->get_field_name( 'img_height' ) . '" type="text" value="' . $img_height . '" /> 
  209. </label><br /> 
  210. <small>' . esc_html__( "If empty, we will attempt to determine the image size.", 'jetpack' ) . '</small></p> 
  211. <p><label for="' . $this->get_field_id( 'link' ) . '">' . esc_html__( 'Link URL (when the image is clicked):', 'jetpack' ) . ' 
  212. <input class="widefat" id="' . $this->get_field_id( 'link' ) . '" name="' . $this->get_field_name( 'link' ) . '" type="text" value="' . $link . '" /> 
  213. </label> 
  214. <label for="' . $this->get_field_id( 'link_target_blank' ) . '"> 
  215. <input type="checkbox" name="' . $this->get_field_name( 'link_target_blank' ) . '" id="' . $this->get_field_id( 'link_target_blank' ) . '" value="1"' . $link_target_blank . '/> 
  216. ' . esc_html__( 'Open link in a new window/tab', 'jetpack' ) . ' 
  217. </label></p>'; 
  218. } // Class Jetpack_Image_Widget 
.