Bez popisu

my-community.php 9.5KB

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