my-community.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * Disable direct access/execution to/of the widget code.
  4. */
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Jetpack_My_Community_Widget displays community members of this site.
  10. *
  11. * A community member is a WordPress.com user that liked or commented on an entry or subscribed to the site.
  12. * Requires WordPress.com connection to work. Otherwise it won't be visible in Widgets screen in admin.
  13. */
  14. class Jetpack_My_Community_Widget extends WP_Widget {
  15. /**
  16. * Transient expiration time.
  17. *
  18. * @var int $expiration
  19. */
  20. static $expiration = 600;
  21. /**
  22. * Default widget title.
  23. *
  24. * @var string $default_title
  25. */
  26. var $default_title;
  27. /**
  28. * Registers the widget with WordPress.
  29. */
  30. function __construct() {
  31. parent::__construct(
  32. 'jetpack_my_community', // Base ID
  33. /** This filter is documented in modules/widgets/facebook-likebox.php */
  34. apply_filters( 'jetpack_widget_name', esc_html__( 'My Community', 'jetpack' ) ),
  35. array(
  36. 'description' => esc_html__( "Display members of your site's community.", 'jetpack' ),
  37. 'customize_selective_refresh' => true,
  38. )
  39. );
  40. if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
  41. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
  42. }
  43. $this->default_title = esc_html__( 'Community', 'jetpack' );
  44. }
  45. /**
  46. * Enqueue stylesheet for grid layout.
  47. */
  48. function enqueue_style() {
  49. wp_register_style( 'jetpack-my-community-widget', plugins_url( 'my-community/style.css', __FILE__ ), array(), '20160129' );
  50. wp_enqueue_style( 'jetpack-my-community-widget' );
  51. }
  52. /**
  53. * Back end widget form.
  54. *
  55. * @see WP_Widget::form()
  56. *
  57. * @param array $instance Previously saved values from database.
  58. *
  59. * @return string|void
  60. */
  61. function form( $instance ) {
  62. $title = isset( $instance['title' ] ) ? $instance['title'] : false;
  63. if ( false === $title ) {
  64. $title = $this->default_title;
  65. }
  66. $number = isset( $instance['number'] ) ? $instance['number'] : 10;
  67. if ( ! in_array( $number, array( 10, 50 ) ) ) {
  68. $number = 10;
  69. }
  70. $include_likers = isset( $instance['include_likers'] ) ? (bool) $instance['include_likers'] : true;
  71. $include_followers = isset( $instance['include_followers'] ) ? (bool) $instance['include_followers'] : true;
  72. $include_commenters = isset( $instance['include_commenters'] ) ? (bool) $instance['include_commenters'] : true;
  73. ?>
  74. <p>
  75. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
  76. <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 ); ?>" />
  77. </p>
  78. <p>
  79. <label><?php esc_html_e( 'Show a maximum of', 'jetpack' ); ?></label>
  80. </p>
  81. <ul>
  82. <li><label><input id="<?php echo $this->get_field_id( 'number' ); ?>-few" name="<?php echo $this->get_field_name( 'number' ); ?>" type="radio" value="10" <?php checked( '10', $number ); ?> /> <?php esc_html_e( '10 community members', 'jetpack' ); ?></label></li>
  83. <li><label><input id="<?php echo $this->get_field_id( 'number' ); ?>-lots" name="<?php echo $this->get_field_name( 'number' ); ?>" type="radio" value="50" <?php checked( '50', $number ); ?> /> <?php esc_html_e( '50 community members', 'jetpack' ); ?></label></li>
  84. </ul>
  85. <p>
  86. <label for="<?php echo $this->get_field_id( 'include_likers' ); ?>">
  87. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'include_likers' ); ?>" name="<?php echo $this->get_field_name( 'include_likers' ); ?>" value="1" <?php checked( $include_likers, 1 ); ?> />
  88. <?php esc_html_e( 'Include activity from likers', 'jetpack' ); ?>
  89. </label>
  90. </p>
  91. <p>
  92. <label for="<?php echo $this->get_field_id( 'include_followers' ); ?>">
  93. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'include_followers' ); ?>" name="<?php echo $this->get_field_name( 'include_followers' ); ?>" value="1" <?php checked( $include_followers, 1 ); ?> />
  94. <?php esc_html_e( 'Include activity from followers', 'jetpack' ); ?>
  95. </label>
  96. </p>
  97. <p>
  98. <label for="<?php echo $this->get_field_id( 'include_commenters' ); ?>">
  99. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'include_commenters' ); ?>" name="<?php echo $this->get_field_name( 'include_commenters' ); ?>" value="1" <?php checked( $include_commenters, 1 ); ?> />
  100. <?php esc_html_e( 'Include activity from commenters', 'jetpack' ); ?>
  101. </label>
  102. </p>
  103. <?php
  104. }
  105. /**
  106. * Sanitize widget form values as they are saved.
  107. *
  108. * @see WP_Widget::update()
  109. *
  110. * @param array $new_instance Values just sent to be saved.
  111. * @param array $old_instance Previously saved values from database.
  112. *
  113. * @return array Updated safe values to be saved.
  114. */
  115. function update( $new_instance, $old_instance ) {
  116. $instance = array();
  117. $instance['title'] = wp_kses( $new_instance['title'], array() );
  118. if ( $instance['title'] === $this->default_title ) {
  119. $instance['title'] = false; // Store as false in case of language change
  120. }
  121. $instance['number'] = (int) $new_instance['number'];
  122. if ( !in_array( $instance['number'], array( 10, 50 ) ) ) {
  123. $instance['number'] = 10;
  124. }
  125. $instance['include_likers'] = (bool) $new_instance['include_likers'];
  126. $instance['include_followers'] = (bool) $new_instance['include_followers'];
  127. $instance['include_commenters'] = (bool) $new_instance['include_commenters'];
  128. delete_transient( "$this->id-{$instance['number']}" . (int) $instance['include_likers'] . (int) $instance['include_followers'] . (int) $instance['include_commenters'] );
  129. return $instance;
  130. }
  131. /**
  132. * Front-end display of widget.
  133. *
  134. * @see WP_Widget::widget()
  135. *
  136. * @param array $args Widget arguments.
  137. * @param array $instance Saved values from database.
  138. */
  139. function widget( $args, $instance ) {
  140. $instance = wp_parse_args( $instance, array(
  141. 'title' => false,
  142. 'number' => true,
  143. 'include_likers' => true,
  144. 'include_followers' => true,
  145. 'include_commenters' => true,
  146. ) );
  147. $title = $instance['title'];
  148. if ( false === $title ) {
  149. $title = $this->default_title;
  150. }
  151. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  152. $title = apply_filters( 'widget_title', $title );
  153. echo $args['before_widget'];
  154. if ( ! empty( $title ) ) {
  155. echo $args['before_title'] . $title . $args['after_title'];
  156. }
  157. $transient_name = "$this->id-{$instance['number']}" . (int) $instance['include_likers'] . (int) $instance['include_followers'] . (int) $instance['include_commenters'];
  158. $my_community = get_transient( $transient_name );
  159. if ( empty( $my_community ) ) {
  160. $my_community = $this->get_community( $instance );
  161. set_transient( $transient_name, $my_community, self::$expiration );
  162. }
  163. echo $my_community;
  164. echo $args['after_widget'];
  165. /** This action is documented in modules/widgets/gravatar-profile.php */
  166. do_action( 'jetpack_stats_extra', 'widget_view', 'my_community' );
  167. }
  168. /**
  169. * Initiate request and render the response.
  170. *
  171. * @since 4.0
  172. *
  173. * @param array $query
  174. *
  175. * @return string
  176. */
  177. function get_community( $query ) {
  178. $members = $this->fetch_remote_community( $query );
  179. if ( ! empty( $members ) ) {
  180. $my_community = '<div class="widgets-multi-column-grid"><ul>';
  181. foreach ( $members as $member ) {
  182. $my_community .= sprintf( '<li><a href="%s" %s><img alt="" src="%s" class="avatar avatar-240" height="48" width="48" originals="240" scale="1" /></a></li>',
  183. $member->profile_URL,
  184. empty( $member->name ) ? '' : 'title="' . $member->name . '"',
  185. $member->avatar_URL
  186. );
  187. }
  188. $my_community .= '</ul></div>';
  189. } else {
  190. if ( current_user_can( 'edit_theme_options' ) ) {
  191. $my_community = '<p>' . wp_kses( sprintf( __( 'There are no users to display in this <a href="%1$s">My Community widget</a>. <a href="%2$s">Want more traffic?</a>', 'jetpack' ),
  192. admin_url( 'widgets.php' ),
  193. 'https://jetpack.com/support/getting-more-views-and-traffic/'
  194. ), array( 'a' => array( 'href' => true ) ) ) . '</p>';
  195. } else {
  196. $my_community = '<p>' . esc_html__( "I'm just starting out; leave me a comment or a like :)", 'jetpack' ) . '</p>';
  197. }
  198. }
  199. return $my_community;
  200. }
  201. /**
  202. * Request community members to WordPress.com endpoint.
  203. *
  204. * @since 4.0
  205. *
  206. * @param $query
  207. *
  208. * @return array
  209. */
  210. function fetch_remote_community( $query ) {
  211. $jetpack_blog_id = Jetpack_Options::get_option( 'id' );
  212. $url = add_query_arg(
  213. array(
  214. 'number' => $query['number'],
  215. 'likers' => (int) $query['include_likers'],
  216. 'followers' => (int) $query['include_followers'],
  217. 'commenters' => (int) $query['include_commenters'],
  218. ),
  219. "https://public-api.wordpress.com/rest/v1.1/sites/$jetpack_blog_id/community"
  220. );
  221. $response = wp_remote_get( $url );
  222. $response_body = wp_remote_retrieve_body( $response );
  223. if ( empty( $response_body ) ) {
  224. return array();
  225. }
  226. $response_body = json_decode( $response_body );
  227. if ( isset( $response_body->users ) ) {
  228. return $response_body->users;
  229. }
  230. return array();
  231. }
  232. }
  233. /**
  234. * If site is connected to WordPress.com, register the widget.
  235. *
  236. * @since 4.0
  237. */
  238. function jetpack_my_community_init() {
  239. if ( Jetpack::is_active() ) {
  240. register_widget( 'Jetpack_My_Community_Widget' );
  241. }
  242. }
  243. add_action( 'widgets_init', 'jetpack_my_community_init' );