Jetpack_Display_Posts_Widget

Display a list of recent posts from a WordPress.com or Jetpack-enabled blog.

Defined (1)

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

/modules/widgets/wordpress-post-widget.php  
  1. class Jetpack_Display_Posts_Widget extends WP_Widget { 
  2.  
  3. public function __construct() { 
  4. parent::__construct( 
  5. // internal id 
  6. 'jetpack_display_posts_widget',  
  7. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  8. apply_filters( 'jetpack_widget_name', __( 'Display WordPress Posts', 'jetpack' ) ),  
  9. array( 
  10. 'description' => __( 'Displays a list of recent posts from another WordPress.com or Jetpack-enabled blog.', 'jetpack' ),  
  11. ); 
  12.  
  13. /** 
  14. * Expiring transients have a name length maximum of 45 characters,  
  15. * so this function returns an abbreviated MD5 hash to use instead of 
  16. * the full URI. 
  17. */ 
  18. public function get_site_hash( $site ) { 
  19. return substr( md5( $site ), 0, 21 ); 
  20.  
  21. public function get_site_info( $site ) { 
  22. $site_hash = $this->get_site_hash( $site ); 
  23. $data_from_cache = get_transient( 'display_posts_site_info_' . $site_hash ); 
  24. if ( false === $data_from_cache ) { 
  25. $response = wp_remote_get( sprintf( 'https://public-api.wordpress.com/rest/v1.1/sites/%s', urlencode( $site ) ) ); 
  26. set_transient( 'display_posts_site_info_' . $site_hash, $response, 10 * MINUTE_IN_SECONDS ); 
  27. } else { 
  28. $response = $data_from_cache; 
  29.  
  30. if ( is_wp_error( $response ) ) { 
  31. return false; 
  32.  
  33. $site_info = json_decode( $response ['body'] ); 
  34. if ( ! isset( $site_info->ID ) ) { 
  35. return false; 
  36.  
  37. return $site_info; 
  38.  
  39. /** 
  40. * Set up the widget display on the front end 
  41. */ 
  42. public function widget( $args, $instance ) { 
  43. /** This filter is documented in core/src/wp-includes/default-widgets.php */ 
  44. $title = apply_filters( 'widget_title', $instance['title'] ); 
  45.  
  46. wp_enqueue_style( 'jetpack_display_posts_widget', plugins_url( 'wordpress-post-widget/style.css', __FILE__ ) ); 
  47.  
  48. $site_info = $this->get_site_info( $instance['url'] ); 
  49.  
  50. echo $args['before_widget']; 
  51.  
  52. if ( false === $site_info ) { 
  53. echo '<p>' . __( 'We cannot load blog data at this time.', 'jetpack' ) . '</p>'; 
  54. echo $args['after_widget']; 
  55. return; 
  56.  
  57. if ( ! empty( $title ) ) { 
  58. echo $args['before_title'] . esc_html( $title . ': ' . $site_info->name ) . $args['after_title']; 
  59. } else { 
  60. echo $args['before_title'] . esc_html( $site_info->name ) . $args['after_title']; 
  61.  
  62. $site_hash = $this->get_site_hash( $instance['url'] ); 
  63. $data_from_cache = get_transient( 'display_posts_post_info_' . $site_hash ); 
  64. if ( false === $data_from_cache ) { 
  65. $response = wp_remote_get( 
  66. sprintf( 
  67. 'https://public-api.wordpress.com/rest/v1.1/sites/%1$d/posts/%2$s',  
  68. $site_info->ID,  
  69. /** 
  70. * Filters the parameters used to fetch for posts in the Display Posts Widget. 
  71. * @see https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/posts/ 
  72. * @module widgets 
  73. * @since 3.6.0 
  74. * @param string $args Extra parameters to filter posts returned from the WordPress.com REST API. 
  75. */ 
  76. apply_filters( 'jetpack_display_posts_widget_posts_params', '' ) 
  77. ); 
  78. set_transient( 'display_posts_post_info_' . $site_hash, $response, 10 * MINUTE_IN_SECONDS ); 
  79. } else { 
  80. $response = $data_from_cache; 
  81.  
  82. if ( is_wp_error( $response ) ) { 
  83. echo '<p>' . __( 'We cannot load blog data at this time.', 'jetpack' ) . '</p>'; 
  84. echo $args['after_widget']; 
  85. return; 
  86.  
  87. $posts_info = json_decode( $response['body'] ); 
  88.  
  89. echo '<div class="jetpack-display-remote-posts">'; 
  90.  
  91. if ( isset( $posts_info->error ) && 'jetpack_error' == $posts_info->error ) { 
  92. echo '<p>' . __( 'We cannot display posts for this blog.', 'jetpack' ) . '</p>'; 
  93. echo '</div><!-- .jetpack-display-remote-posts -->'; 
  94. echo $args['after_widget']; 
  95. return; 
  96.  
  97. $number_of_posts = min( $instance['number_of_posts'], count( $posts_info->posts ) ); 
  98.  
  99. for ( $i = 0; $i < $number_of_posts; $i++ ) { 
  100. $single_post = $posts_info->posts[$i]; 
  101. $post_title = ( $single_post->title ) ? $single_post->title : '( No Title )'; 
  102.  
  103. $target = ''; 
  104. if ( isset( $instance['open_in_new_window'] ) && $instance['open_in_new_window'] == true ) { 
  105. $target = ' target="_blank"'; 
  106. echo '<h4><a href="' . esc_url( $single_post->URL ) . '"' . $target . '>' . esc_html( $post_title ) . '</a></h4>' . "\n"; 
  107. if ( ( $instance['featured_image'] == true ) && ( ! empty ( $single_post->featured_image) ) ) { 
  108. $featured_image = ( $single_post->featured_image ) ? $single_post->featured_image : ''; 
  109. /** 
  110. * Allows setting up custom Photon parameters to manipulate the image output in the Display Posts widget. 
  111. * @see https://developer.wordpress.com/docs/photon/ 
  112. * @module widgets 
  113. * @since 3.6.0 
  114. * @param array $args Array of Photon Parameters. 
  115. */ 
  116. $image_params = apply_filters( 'jetpack_display_posts_widget_image_params', array() ); 
  117. echo '<a title="' . esc_attr( $post_title ) . '" href="' . esc_url( $single_post->URL ) . '"><img src="' . jetpack_photon_url( $featured_image, $image_params ) . '" alt="' . esc_attr( $post_title ) . '"/></a>'; 
  118.  
  119. if ( $instance['show_excerpts'] == true ) { 
  120. $post_excerpt = ( $single_post->excerpt ) ? $single_post->excerpt : ''; 
  121. echo $post_excerpt; 
  122.  
  123. echo '</div><!-- .jetpack-display-remote-posts -->'; 
  124. echo $args['after_widget']; 
  125.  
  126. public function form( $instance ) { 
  127. if ( isset( $instance['title'] ) ) { 
  128. $title = $instance['title']; 
  129. } else { 
  130. $title = __( 'Recent Posts', 'jetpack' ); 
  131.  
  132. if ( isset( $instance['url'] ) ) { 
  133. $url = $instance['url']; 
  134. } else { 
  135. $url = ''; 
  136.  
  137. if ( isset( $instance['number_of_posts'] ) ) { 
  138. $number_of_posts = $instance['number_of_posts']; 
  139. } else { 
  140. $number_of_posts = 5; 
  141.  
  142. $open_in_new_window = false; 
  143. if ( isset( $instance['open_in_new_window'] ) ) { 
  144. $open_in_new_window = $instance['open_in_new_window']; 
  145.  
  146. if ( isset( $instance['featured_image'] ) ) { 
  147. $featured_image = $instance['featured_image']; 
  148. } else { 
  149. $featured_image = false; 
  150.  
  151. if ( isset( $instance['show_excerpts'] ) ) { 
  152. $show_excerpts = $instance['show_excerpts']; 
  153. } else { 
  154. $show_excerpts = false; 
  155.  
  156. ?> 
  157. <p> 
  158. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'jetpack' ); ?></label> 
  159. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> 
  160. </p> 
  161.  
  162. <p> 
  163. <label for="<?php echo $this->get_field_id( 'url' ); ?>"><?php _e( 'Blog URL:', 'jetpack' ); ?></label> 
  164. <input class="widefat" id="<?php echo $this->get_field_id( 'url' ); ?>" name="<?php echo $this->get_field_name( 'url' ); ?>" type="text" value="<?php echo esc_attr( $url ); ?>" /> 
  165. <p> 
  166. <?php _e( "Enter a WordPress.com or Jetpack WordPress site URL.", 'jetpack' ); ?> 
  167. </p> 
  168. </p> 
  169. <p> 
  170. <label for="<?php echo $this->get_field_id( 'number_of_posts' ); ?>"><?php _e( 'Number of Posts to Display:', 'jetpack' ); ?></label> 
  171. <select name="<?php echo $this->get_field_name( 'number_of_posts' ); ?>"> 
  172. <?php 
  173. for ($i = 1; $i <= 10; $i++) { 
  174. echo '<option value="' . $i . '" '.selected( $number_of_posts, $i ).'>' . $i . '</option>'; 
  175. ?> 
  176. </select> 
  177. </p> 
  178. <label for="<?php echo $this->get_field_id( 'open_in_new_window' ); ?>"><?php _e( 'Open links in new window/tab:', 'jetpack' ); ?></label> 
  179. <input type="checkbox" name="<?php echo $this->get_field_name( 'open_in_new_window' ); ?>" <?php checked( $open_in_new_window, 1 ); ?> /> 
  180. </p> 
  181. <p> 
  182. <label for="<?php echo $this->get_field_id( 'featured_image' ); ?>"><?php _e( 'Show Featured Image:', 'jetpack' ); ?></label> 
  183. <input type="checkbox" name="<?php echo $this->get_field_name( 'featured_image' ); ?>" <?php checked( $featured_image, 1 ); ?> /> 
  184. </p> 
  185. <p> 
  186. <label for="<?php echo $this->get_field_id( 'show_excerpts' ); ?>"><?php _e( 'Show Excerpts:', 'jetpack' ); ?></label> 
  187. <input type="checkbox" name="<?php echo $this->get_field_name( 'show_excerpts' ); ?>" <?php checked( $show_excerpts, 1 ); ?> /> 
  188. </p> 
  189.  
  190. <?php 
  191.  
  192. public function update( $new_instance, $old_instance ) { 
  193. $instance = array(); 
  194. $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; 
  195. $instance['url'] = ( ! empty( $new_instance['url'] ) ) ? strip_tags( $new_instance['url'] ) : ''; 
  196. $instance['url'] = str_replace( "http://", "", $instance['url'] ); 
  197. $instance['url'] = untrailingslashit( $instance['url'] ); 
  198.  
  199. // Normalize www. 
  200. $site_info = $this->get_site_info( $instance['url'] ); 
  201. if ( ! $site_info && 'www.' === substr( $instance['url'], 0, 4 ) ) { 
  202. $site_info = $this->get_site_info( substr( $instance['url'], 4 ) ); 
  203. if ( $site_info ) { 
  204. $instance['url'] = substr( $instance['url'], 4 ); 
  205.  
  206. $instance['number_of_posts'] = ( ! empty( $new_instance['number_of_posts'] ) ) ? intval( $new_instance['number_of_posts'] ) : ''; 
  207. $instance['open_in_new_window'] = ( ! empty( $new_instance['open_in_new_window'] ) ) ? true : ''; 
  208. $instance['featured_image'] = ( ! empty( $new_instance['featured_image'] ) ) ? true : ''; 
  209. $instance['show_excerpts'] = ( ! empty( $new_instance['show_excerpts'] ) ) ? true : ''; 
  210. return $instance;