PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/widgets/image-widget.php

https://gitlab.com/thisishayat/itv-2016
PHP | 244 lines | 170 code | 27 blank | 47 comment | 33 complexity | 95f5705535ef41fba6a4640292e3934e MD5 | raw file
  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' );
  12. function jetpack_image_widget_init() {
  13. register_widget( 'Jetpack_Image_Widget' );
  14. }
  15. class Jetpack_Image_Widget extends WP_Widget {
  16. /**
  17. * Register widget with WordPress.
  18. */
  19. public function __construct() {
  20. parent::__construct(
  21. 'image',
  22. /** This filter is documented in modules/widgets/facebook-likebox.php */
  23. apply_filters( 'jetpack_widget_name', esc_html__( 'Image', 'jetpack' ) ),
  24. array(
  25. 'classname' => 'widget_image',
  26. 'description' => __( 'Display an image in your sidebar', 'jetpack' )
  27. )
  28. );
  29. if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) ) {
  30. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
  31. }
  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. echo $args['before_widget'];
  51. $instance = wp_parse_args( $instance, array(
  52. 'title' => '',
  53. 'img_url' => ''
  54. ) );
  55. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  56. $title = apply_filters( 'widget_title', $instance['title'] );
  57. if ( $title ) {
  58. echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
  59. }
  60. if ( '' != $instance['img_url'] ) {
  61. $output = '<img src="' . esc_attr( $instance['img_url'] ) .'" ';
  62. if ( '' != $instance['alt_text'] ) {
  63. $output .= 'alt="' . esc_attr( $instance['alt_text'] ) .'" ';
  64. }
  65. if ( '' != $instance['img_title'] ) {
  66. $output .= 'title="' . esc_attr( $instance['img_title'] ) .'" ';
  67. }
  68. if ( '' == $instance['caption'] ) {
  69. $output .= 'class="align' . esc_attr( $instance['align'] ) . '" ';
  70. }
  71. if ( '' != $instance['img_width'] ) {
  72. $output .= 'width="' . esc_attr( $instance['img_width'] ) .'" ';
  73. }
  74. if ( '' != $instance['img_height'] ) {
  75. $output .= 'height="' . esc_attr( $instance['img_height'] ) .'" ';
  76. }
  77. $output .= '/>';
  78. if ( '' != $instance['link'] && ! empty( $instance['link_target_blank'] ) ) {
  79. $output = '<a target="_blank" href="' . esc_attr( $instance['link'] ) . '">' . $output . '</a>';
  80. }
  81. if ( '' != $instance['link'] && empty( $instance['link_target_blank'] ) ) {
  82. $output = '<a href="' . esc_attr( $instance['link'] ) . '">' . $output . '</a>';
  83. }
  84. if ( '' != $instance['caption'] ) {
  85. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  86. $caption = apply_filters( 'widget_text', $instance['caption'] );
  87. $img_width = ( ! empty( $instance['img_width'] ) ? 'style="width: ' . esc_attr( $instance['img_width'] ) .'px"' : '' );
  88. $output = '<figure ' . $img_width .' class="wp-caption align' . esc_attr( $instance['align'] ) . '">
  89. ' . $output . '
  90. <figcaption class="wp-caption-text">' . $caption . '</figcaption>
  91. </figure>'; // wp_kses_post caption on update
  92. }
  93. echo '<div class="jetpack-image-container">' . do_shortcode( $output ) . '</div>';
  94. }
  95. echo "\n" . $args['after_widget'];
  96. }
  97. /**
  98. * Sanitize widget form values as they are saved.
  99. *
  100. * @see WP_Widget::update()
  101. *
  102. * @param array $new_instance Values just sent to be saved.
  103. * @param array $old_instance Previously saved values from database.
  104. *
  105. * @return array Updated safe values to be saved.
  106. */
  107. public function update( $new_instance, $old_instance ) {
  108. $allowed_caption_html = array(
  109. 'a' => array(
  110. 'href' => array(),
  111. 'title' => array(),
  112. ),
  113. 'b' => array(),
  114. 'em' => array(),
  115. 'i' => array(),
  116. 'p' => array(),
  117. 'strong' => array()
  118. );
  119. $instance = $old_instance;
  120. $instance['title'] = strip_tags( $new_instance['title'] );
  121. $instance['img_url'] = esc_url( $new_instance['img_url'], null, 'display' );
  122. $instance['alt_text'] = strip_tags( $new_instance['alt_text'] );
  123. $instance['img_title'] = strip_tags( $new_instance['img_title'] );
  124. $instance['caption'] = wp_kses( stripslashes($new_instance['caption'] ), $allowed_caption_html );
  125. $instance['align'] = $new_instance['align'];
  126. $instance['img_width'] = absint( $new_instance['img_width'] );
  127. $instance['img_height'] = absint( $new_instance['img_height'] );
  128. $instance['link'] = esc_url( $new_instance['link'], null, 'display' );
  129. $instance['link_target_blank'] = isset( $new_instance['link_target_blank'] ) ? (bool) $new_instance['link_target_blank'] : false;
  130. return $instance;
  131. }
  132. /**
  133. * Back-end widget form.
  134. *
  135. * @see WP_Widget::form()
  136. *
  137. * @param array $instance Previously saved values from database.
  138. */
  139. public function form( $instance ) {
  140. // Defaults
  141. $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 ) );
  142. $title = esc_attr( $instance['title'] );
  143. $img_url = esc_url( $instance['img_url'], null, 'display' );
  144. $alt_text = esc_attr( $instance['alt_text'] );
  145. $img_title = esc_attr( $instance['img_title'] );
  146. $caption = esc_textarea( $instance['caption'] );
  147. $align = esc_attr( $instance['align'] );
  148. $img_width = esc_attr( $instance['img_width'] );
  149. $img_height = esc_attr( $instance['img_height'] );
  150. $link_target_blank = checked( $instance['link_target_blank'], true, false );
  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. if ( '' == $instance['img_width'] ) {
  157. $width = $size[0];
  158. $img_width = $width;
  159. } else {
  160. $img_width = absint( $instance['img_width'] );
  161. }
  162. if ( '' == $instance['img_height'] ) {
  163. $height = $size[1];
  164. $img_height = $height;
  165. } else {
  166. $img_height = absint( $instance['img_height'] );
  167. }
  168. unlink( $tmp_file );
  169. }
  170. }
  171. $link = esc_url( $instance['link'], null, 'display' );
  172. echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html__( 'Widget title:', 'jetpack' ) . '
  173. <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . $title . '" />
  174. </label></p>
  175. <p><label for="' . $this->get_field_id( 'img_url' ) . '">' . esc_html__( 'Image URL:', 'jetpack' ) . '
  176. <input class="widefat" id="' . $this->get_field_id( 'img_url' ) . '" name="' . $this->get_field_name( 'img_url' ) . '" type="text" value="' . $img_url . '" />
  177. </label></p>
  178. <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>
  179. <input class="widefat" id="' . $this->get_field_id( 'alt_text' ) . '" name="' . $this->get_field_name( 'alt_text' ) . '" type="text" value="' . $alt_text . '" />
  180. </label></p>
  181. <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>
  182. <input class="widefat" id="' . $this->get_field_id( 'img_title' ) . '" name="' . $this->get_field_name( 'img_title' ) . '" type="text" value="' . $img_title . '" />
  183. </label></p>
  184. <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>
  185. <textarea class="widefat" id="' . $this->get_field_id( 'caption' ) . '" name="' . $this->get_field_name( 'caption' ) . '" rows="2" cols="20">' . $caption . '</textarea>
  186. </label></p>';
  187. $alignments = array(
  188. 'none' => __( 'None', 'jetpack' ),
  189. 'left' => __( 'Left', 'jetpack' ),
  190. 'center' => __( 'Center', 'jetpack' ),
  191. 'right' => __( 'Right', 'jetpack' ),
  192. );
  193. echo '<p><label for="' . $this->get_field_id( 'align' ) . '">' . esc_html__( 'Image Alignment:', 'jetpack' ) . '
  194. <select id="' . $this->get_field_id( 'align' ) . '" name="' . $this->get_field_name( 'align' ) . '">';
  195. foreach ( $alignments as $alignment => $alignment_name ) {
  196. echo '<option value="' . esc_attr( $alignment ) . '" ';
  197. if ( $alignment == $align )
  198. echo 'selected="selected" ';
  199. echo '>' . esc_html($alignment_name) . "</option>\n";
  200. }
  201. echo '</select></label></p>';
  202. echo '<p><label for="' . $this->get_field_id( 'img_width' ) . '">' . esc_html__( 'Width:', 'jetpack' ) . '
  203. <input size="3" id="' . $this->get_field_id( 'img_width' ) . '" name="' . $this->get_field_name( 'img_width' ) . '" type="text" value="' . $img_width . '" />
  204. </label>
  205. <label for="' . $this->get_field_id( 'img_height' ) . '">' . esc_html__( 'Height:', 'jetpack' ) . '
  206. <input size="3" id="' . $this->get_field_id( 'img_height' ) . '" name="' . $this->get_field_name( 'img_height' ) . '" type="text" value="' . $img_height . '" />
  207. </label><br />
  208. <small>' . esc_html__( "If empty, we will attempt to determine the image size.", 'jetpack' ) . '</small></p>
  209. <p><label for="' . $this->get_field_id( 'link' ) . '">' . esc_html__( 'Link URL (when the image is clicked):', 'jetpack' ) . '
  210. <input class="widefat" id="' . $this->get_field_id( 'link' ) . '" name="' . $this->get_field_name( 'link' ) . '" type="text" value="' . $link . '" />
  211. </label>
  212. <label for="' . $this->get_field_id( 'link_target_blank' ) . '">
  213. <input type="checkbox" name="' . $this->get_field_name( 'link_target_blank' ) . '" id="' . $this->get_field_id( 'link_target_blank' ) . '" value="1"' . $link_target_blank . '/>
  214. ' . esc_html__( 'Open link in a new window/tab', 'jetpack' ) . '
  215. </label></p>';
  216. }
  217. } // Class Jetpack_Image_Widget