OS : Linux
PHP Version : 7.4.33
Software : Apache/2.4.6 (CentOS) PHP/7.4.33 esc_html__( "Display members of your site's community.", 'jetpack' ),
'customize_selective_refresh' => true,
)
);
if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
}
$this->default_title = esc_html__( 'Community', 'jetpack' );
}
/**
* Enqueue stylesheet for grid layout.
*/
function enqueue_style() {
wp_register_style( 'jetpack-my-community-widget', plugins_url( 'my-community/style.css', __FILE__ ), array(), '20160129' );
wp_enqueue_style( 'jetpack-my-community-widget' );
}
/**
* Back end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*
* @return string|void
*/
function form( $instance ) {
$title = isset( $instance['title'] ) ? $instance['title'] : false;
if ( false === $title ) {
$title = $this->default_title;
}
$number = isset( $instance['number'] ) ? $instance['number'] : 10;
if ( ! in_array( $number, array( 10, 50 ) ) ) {
$number = 10;
}
$include_likers = isset( $instance['include_likers'] ) ? (bool) $instance['include_likers'] : true;
$include_followers = isset( $instance['include_followers'] ) ? (bool) $instance['include_followers'] : true;
$include_commenters = isset( $instance['include_commenters'] ) ? (bool) $instance['include_commenters'] : true;
?>
/>
/>
/>
default_title ) {
$instance['title'] = false; // Store as false in case of language change
}
$instance['number'] = (int) $new_instance['number'];
if ( ! in_array( $instance['number'], array( 10, 50 ) ) ) {
$instance['number'] = 10;
}
$instance['include_likers'] = (bool) $new_instance['include_likers'];
$instance['include_followers'] = (bool) $new_instance['include_followers'];
$instance['include_commenters'] = (bool) $new_instance['include_commenters'];
delete_transient( "$this->id-v2-{$instance['number']}" . (int) $instance['include_likers'] . (int) $instance['include_followers'] . (int) $instance['include_commenters'] );
return $instance;
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
function widget( $args, $instance ) {
$instance = wp_parse_args(
$instance, array(
'title' => false,
'number' => true,
'include_likers' => true,
'include_followers' => true,
'include_commenters' => true,
)
);
$title = $instance['title'];
if ( false === $title ) {
$title = $this->default_title;
}
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
$transient_name = "$this->id-v2-{$instance['number']}" . (int) $instance['include_likers'] . (int) $instance['include_followers'] . (int) $instance['include_commenters'];
$my_community = get_transient( $transient_name );
if ( empty( $my_community ) ) {
$my_community = $this->get_community( $instance );
set_transient( $transient_name, $my_community, self::$expiration );
}
echo $my_community;
echo $args['after_widget'];
/** This action is documented in modules/widgets/gravatar-profile.php */
do_action( 'jetpack_stats_extra', 'widget_view', 'my_community' );
}
/**
* Initiate request and render the response.
*
* @since 4.0
*
* @param array $query
*
* @return string
*/
function get_community( $query ) {
$members = $this->fetch_remote_community( $query );
if ( ! empty( $members ) ) {
$my_community = '';
} else {
if ( current_user_can( 'edit_theme_options' ) ) {
$my_community = '' . wp_kses(
sprintf(
__( 'There are no users to display in this My Community widget . Want more traffic? ', 'jetpack' ),
admin_url( 'widgets.php' ),
'https://jetpack.com/support/getting-more-views-and-traffic/'
), array( 'a' => array( 'href' => true ) )
) . '
';
} else {
$my_community = '' . esc_html__( "I'm just starting out; leave me a comment or a like :)", 'jetpack' ) . '
';
}
}
return $my_community;
}
/**
* Request community members to WordPress.com endpoint.
*
* @since 4.0
*
* @param $query
*
* @return array
*/
function fetch_remote_community( $query ) {
$jetpack_blog_id = Jetpack_Options::get_option( 'id' );
$url = add_query_arg(
array(
'number' => $query['number'],
'likers' => (int) $query['include_likers'],
'followers' => (int) $query['include_followers'],
'commenters' => (int) $query['include_commenters'],
),
"https://public-api.wordpress.com/rest/v1.1/sites/$jetpack_blog_id/community"
);
$response = wp_remote_get( $url );
$response_body = wp_remote_retrieve_body( $response );
if ( empty( $response_body ) ) {
return array();
}
$response_body = json_decode( $response_body );
if ( isset( $response_body->users ) ) {
return $response_body->users;
}
return array();
}
}
/**
* If site is connected to WordPress.com, register the widget.
*
* @since 4.0
*/
function jetpack_my_community_init() {
if ( Jetpack::is_active() ) {
register_widget( 'Jetpack_My_Community_Widget' );
}
}
add_action( 'widgets_init', 'jetpack_my_community_init' );