Ei kuvausta

image-widget.php 11KB

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