Jetpack_Flickr_Widget

Flickr Widget.

Defined (1)

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

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