Jetpack_My_Community_Widget

Jetpack_My_Community_Widget displays community members of this site.

Defined (1)

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

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