Jetpack_Image_Widget

The Jetpack by WordPress.com Jetpack Image Widget class.

Defined (1)

The class is defined in the following location(s).

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