Nenhuma Descrição

flickr.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. 'target' => false,
  51. 'flickr_image_size' => 'thumbnail',
  52. 'flickr_rss_url' => '',
  53. );
  54. }
  55. /**
  56. * Front-end display of the widget.
  57. *
  58. * @param array $args Widget arguments.
  59. * @param array $instance Saved values from database.
  60. */
  61. public function widget( $args, $instance ) {
  62. $instance = wp_parse_args( $instance, $this->defaults() );
  63. $image_size_string = 'small' == $instance['flickr_image_size'] ? '_m.jpg' : '_t.jpg';
  64. if ( ! empty( $instance['flickr_rss_url'] ) ) {
  65. /*
  66. * Parse the URL, and rebuild a URL that's sure to display images.
  67. * Some Flickr Feeds do not display images by default.
  68. */
  69. $flickr_parameters = wp_parse_url( htmlspecialchars_decode( $instance['flickr_rss_url'] ) );
  70. // Is it a Flickr Feed.
  71. if (
  72. ! empty( $flickr_parameters['host'] )
  73. && ! empty( $flickr_parameters['query'] )
  74. && false !== strpos( $flickr_parameters['host'], 'flickr' )
  75. ) {
  76. parse_str( $flickr_parameters['query'], $vars );
  77. // Do we have an ID in the feed? Let's continue.
  78. if ( isset( $vars['id'] ) ) {
  79. // Flickr Feeds can be used for groups or for individuals.
  80. if (
  81. ! empty( $flickr_parameters['path'] )
  82. && false !== strpos( $flickr_parameters['path'], 'groups' )
  83. ) {
  84. $feed_url = 'https://api.flickr.com/services/feeds/groups_pool.gne';
  85. } else {
  86. $feed_url = 'https://api.flickr.com/services/feeds/photos_public.gne';
  87. }
  88. // Build our new RSS feed.
  89. $rss_url = sprintf(
  90. '%1$s?id=%2$s&format=rss_200_enc',
  91. esc_url( $feed_url ),
  92. esc_attr( $vars['id'] )
  93. );
  94. }
  95. }
  96. } // End if().
  97. // Still no RSS feed URL? Get a default feed from Flickr to grab interesting photos.
  98. if ( empty( $rss_url ) ) {
  99. $rss_url = 'https://api.flickr.com/services/feeds/photos_interesting.gne?format=rss_200';
  100. }
  101. $rss = fetch_feed( $rss_url );
  102. $photos = '';
  103. if ( ! is_wp_error( $rss ) ) {
  104. foreach ( $rss->get_items( 0, $instance['items'] ) as $photo ) {
  105. switch ( $instance['flickr_image_size'] ) {
  106. case 'thumbnail':
  107. $src = $photo->get_enclosure()->get_thumbnail();
  108. break;
  109. case 'small':
  110. $src = preg_match( '/src="(.*?)"/i', $photo->get_description(), $p );
  111. $src = $p[1];
  112. break;
  113. case 'large':
  114. $src = $photo->get_enclosure()->get_link();
  115. break;
  116. }
  117. $photos .= '<a href="' . esc_url( $photo->get_permalink(), array( 'http', 'https' ) ) . '" ';
  118. if ( $instance['target'] ) {
  119. $photos .= 'target="_blank" rel="noopener noreferrer" ';
  120. }
  121. $photos .= '><img src="' . esc_url( $src, array( 'http', 'https' ) ) . '" ';
  122. $photos .= 'alt="' . esc_attr( $photo->get_title() ) . '" ';
  123. $photos .= 'title="' . esc_attr( $photo->get_title() ) . '" ';
  124. $photos .= ' /></a>';
  125. }
  126. if ( ! empty( $photos ) && class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' ) ) {
  127. $photos = Jetpack_Photon::filter_the_content( $photos );
  128. }
  129. $flickr_home = $rss->get_link();
  130. }
  131. echo $args['before_widget'];
  132. if ( empty( $photos ) ) {
  133. if ( current_user_can( 'edit_theme_options' ) ) {
  134. printf(
  135. '<p>%1$s<br />%2$s</p>',
  136. esc_html__( 'There are no photos to display. Make sure your Flickr feed URL is correct, and that your pictures are publicly accessible.', 'jetpack' ),
  137. esc_html__( '(Only admins can see this message)', 'jetpack' )
  138. );
  139. }
  140. } else {
  141. echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'];
  142. require( dirname( __FILE__ ) . '/flickr/widget.php' );
  143. }
  144. echo $args['after_widget'];
  145. /** This action is already documented in modules/widgets/gravatar-profile.php */
  146. do_action( 'jetpack_stats_extra', 'widget_view', 'flickr' );
  147. }
  148. /**
  149. * Back-end widget form.
  150. *
  151. * @param array $instance Previously saved values from database.
  152. */
  153. public function form( $instance ) {
  154. $instance = wp_parse_args( $instance, $this->defaults() );
  155. require( dirname( __FILE__ ) . '/flickr/form.php' );
  156. }
  157. /**
  158. * Sanitize widget form values as they are saved.
  159. *
  160. * @param array $new_instance Values just sent to be saved.
  161. * @param array $old_instance Previously saved values from database.
  162. * @return array Updated safe values to be saved.
  163. */
  164. public function update( $new_instance, $old_instance ) {
  165. $instance = array();
  166. $defaults = $this->defaults();
  167. if ( isset( $new_instance['title'] ) ) {
  168. $instance['title'] = wp_kses( $new_instance['title'], array() );
  169. }
  170. if ( isset( $new_instance['items'] ) ) {
  171. $instance['items'] = (int) $new_instance['items'];
  172. }
  173. if ( isset( $new_instance['target'] ) ) {
  174. $instance['target'] = (bool) $new_instance['target'];
  175. }
  176. if (
  177. isset( $new_instance['flickr_image_size'] ) &&
  178. in_array( $new_instance['flickr_image_size'], array( 'thumbnail', 'small', 'large' ) )
  179. ) {
  180. $instance['flickr_image_size'] = $new_instance['flickr_image_size'];
  181. } else {
  182. $instance['flickr_image_size'] = 'thumbnail';
  183. }
  184. if ( isset( $new_instance['flickr_rss_url'] ) ) {
  185. $instance['flickr_rss_url'] = esc_url( $new_instance['flickr_rss_url'], array( 'http', 'https' ) );
  186. if ( strlen( $instance['flickr_rss_url'] ) < 10 ) {
  187. $instance['flickr_rss_url'] = '';
  188. }
  189. }
  190. return $instance;
  191. }
  192. }
  193. // Register Jetpack_Flickr_Widget widget.
  194. function jetpack_register_flickr_widget() {
  195. register_widget( 'Jetpack_Flickr_Widget' );
  196. }
  197. add_action( 'widgets_init', 'jetpack_register_flickr_widget' );
  198. }