PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/widgets/wordpress-post-widget.php

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