flickr.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Disable direct access/execution to/of the widget code.
  4. */
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) {
  9. /**
  10. * Flickr Widget
  11. *
  12. * Display your recent Flickr photos.
  13. */
  14. class Jetpack_Flickr_Widget extends WP_Widget {
  15. /**
  16. * Constructor.
  17. */
  18. function __construct() {
  19. parent::__construct(
  20. 'flickr',
  21. /** This filter is documented in modules/widgets/facebook-likebox.php */
  22. apply_filters( 'jetpack_widget_name', esc_html__( 'Flickr', 'jetpack' ) ),
  23. array(
  24. 'description' => esc_html__( 'Display your recent Flickr photos.', 'jetpack' ),
  25. 'customize_selective_refresh' => true,
  26. ),
  27. array()
  28. );
  29. if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
  30. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
  31. }
  32. }
  33. /**
  34. * Enqueue style.
  35. */
  36. function enqueue_style() {
  37. wp_enqueue_style( 'flickr-widget-style', plugins_url( 'flickr/style.css', __FILE__ ), array(), '20170405' );
  38. }
  39. /**
  40. * Return an associative array of default values.
  41. *
  42. * These values are used in new widgets.
  43. *
  44. * @return array Default values for the widget options.
  45. */
  46. public function defaults() {
  47. return array(
  48. 'title' => esc_html__( 'Flickr Photos', 'jetpack' ),
  49. 'items' => 4,
  50. 'flickr_image_size' => 'thumbnail',
  51. 'flickr_rss_url' => ''
  52. );
  53. }
  54. /**
  55. * Front-end display of the widget.
  56. *
  57. * @param array $args Widget arguments.
  58. * @param array $instance Saved values from database.
  59. */
  60. public function widget( $args, $instance ) {
  61. $instance = wp_parse_args( $instance, $this->defaults() );
  62. $image_size_string = 'small' == $instance['flickr_image_size'] ? '_m.jpg' : '_t.jpg';
  63. if ( ! empty( $instance['flickr_rss_url'] ) ) {
  64. /*
  65. * Parse the URL, and rebuild a URL that's sure to display images.
  66. * Some Flickr Feeds do not display images by default.
  67. */
  68. $flickr_parameters = parse_url( htmlspecialchars_decode( $instance['flickr_rss_url'] ) );
  69. // Is it a Flickr Feed.
  70. if (
  71. ! empty( $flickr_parameters['host'] )
  72. && ! empty( $flickr_parameters['query'] )
  73. && false !== strpos( $flickr_parameters['host'], 'flickr' )
  74. ) {
  75. parse_str( $flickr_parameters['query'], $vars );
  76. // Do we have an ID in the feed? Let's continue.
  77. if ( isset( $vars['id'] ) ) {
  78. // Flickr Feeds can be used for groups or for individuals.
  79. if (
  80. ! empty( $flickr_parameters['path'] )
  81. && false !== strpos( $flickr_parameters['path'], 'groups' )
  82. ) {
  83. $feed_url = 'https://api.flickr.com/services/feeds/groups_pool.gne';
  84. } else {
  85. $feed_url = 'https://api.flickr.com/services/feeds/photos_public.gne';
  86. }
  87. // Build our new RSS feed.
  88. $rss_url = sprintf(
  89. '%1$s?id=%2$s&format=rss_200_enc',
  90. esc_url( $feed_url ),
  91. esc_attr( $vars['id'] )
  92. );
  93. }
  94. }
  95. } // End if().
  96. // Still no RSS feed URL? Get a default feed from Flickr to grab interesting photos.
  97. if ( empty( $rss_url ) ) {
  98. $rss_url = 'https://api.flickr.com/services/feeds/photos_interesting.gne?format=rss_200';
  99. }
  100. $rss = fetch_feed( $rss_url );
  101. $photos = '';
  102. if ( ! is_wp_error( $rss ) ) {
  103. foreach ( $rss->get_items( 0, $instance['items'] ) as $photo ) {
  104. switch ( $instance['flickr_image_size'] ) {
  105. case 'thumbnail':
  106. $src = $photo->get_enclosure()->get_thumbnail();
  107. break;
  108. case 'small':
  109. $src = preg_match( '/src="(.*?)"/i', $photo->get_description(), $p );
  110. $src = $p[1];
  111. break;
  112. case 'large':
  113. $src = $photo->get_enclosure()->get_link();
  114. break;
  115. }
  116. $photos .= '<a href="' . esc_url( $photo->get_permalink(), array( 'http', 'https' ) ) . '">';
  117. $photos .= '<img src="' . esc_url( $src, array( 'http', 'https' ) ) . '" ';
  118. $photos .= 'alt="' . esc_attr( $photo->get_title() ) . '" ';
  119. $photos .= 'title="' . esc_attr( $photo->get_title() ) . '" ';
  120. $photos .= ' /></a>';
  121. }
  122. if ( ! empty( $photos ) && class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' ) ) {
  123. $photos = Jetpack_Photon::filter_the_content( $photos );
  124. }
  125. $flickr_home = $rss->get_link();
  126. }
  127. echo $args['before_widget'];
  128. if ( empty( $photos ) ) {
  129. if ( current_user_can( 'edit_theme_options' ) ) {
  130. printf(
  131. '<p>%1$s<br />%2$s</p>',
  132. esc_html__( 'There are no photos to display. Make sure your Flickr feed URL is correct, and that your pictures are publicly accessible.', 'jetpack' ),
  133. esc_html__( '(Only admins can see this message)', 'jetpack' )
  134. );
  135. }
  136. } else {
  137. echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'];
  138. require( dirname( __FILE__ ) . '/flickr/widget.php' );
  139. }
  140. echo $args['after_widget'];
  141. /** This action is already documented in modules/widgets/gravatar-profile.php */
  142. do_action( 'jetpack_stats_extra', 'widget_view', 'flickr' );
  143. }
  144. /**
  145. * Back-end widget form.
  146. *
  147. * @param array $instance Previously saved values from database.
  148. */
  149. public function form( $instance ) {
  150. $instance = wp_parse_args( $instance, $this->defaults() );
  151. require( dirname( __FILE__ ) . '/flickr/form.php' );
  152. }
  153. /**
  154. * Sanitize widget form values as they are saved.
  155. *
  156. * @param array $new_instance Values just sent to be saved.
  157. * @param array $old_instance Previously saved values from database.
  158. * @return array Updated safe values to be saved.
  159. */
  160. public function update( $new_instance, $old_instance ) {
  161. $instance = array();
  162. $defaults = $this->defaults();
  163. if ( isset( $new_instance['title'] ) ) {
  164. $instance['title'] = wp_kses( $new_instance['title'], array() );
  165. }
  166. if ( isset( $new_instance['items'] ) ) {
  167. $instance['items'] = intval( $new_instance['items'] );
  168. }
  169. if (
  170. isset( $new_instance['flickr_image_size'] ) &&
  171. in_array( $new_instance['flickr_image_size'], array( 'thumbnail', 'small', 'large' ) )
  172. ) {
  173. $instance['flickr_image_size'] = $new_instance['flickr_image_size'];
  174. } else {
  175. $instance['flickr_image_size'] = 'thumbnail';
  176. }
  177. if ( isset( $new_instance['flickr_rss_url'] ) ) {
  178. $instance['flickr_rss_url'] = esc_url( $new_instance['flickr_rss_url'], array( 'http', 'https' ) );
  179. if ( strlen( $instance['flickr_rss_url'] ) < 10 ) {
  180. $instance['flickr_rss_url'] = '';
  181. }
  182. }
  183. return $instance;
  184. }
  185. }
  186. // Register Jetpack_Flickr_Widget widget.
  187. function jetpack_register_flickr_widget() {
  188. register_widget( 'Jetpack_Flickr_Widget' );
  189. }
  190. add_action( 'widgets_init', 'jetpack_register_flickr_widget' );
  191. }