Completed
Push — add/react-videopress-settings ( 1a2dcb...a2519f )
by
unknown
275:56 queued 263:52
created

fetch_remote_community()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 1
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Disable direct access/execution to/of the widget code.
4
 */
5
if ( ! defined( 'ABSPATH' ) ) {
6
	exit;
7
}
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;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $expiration.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
22
23
	/**
24
	 * Default widget title.
25
	 *
26
	 * @var string $default_title
27
	 */
28
	var $default_title;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $default_title.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
29
30
	/**
31
	 * Registers the widget with WordPress.
32
	 */
33
	function __construct() {
34
		parent::__construct(
35
			'jetpack_my_community', // Base ID
36
			/** This filter is documented in modules/widgets/facebook-likebox.php */
37
			apply_filters( 'jetpack_widget_name', esc_html__( 'My Community', 'jetpack' ) ),
38
			array(
39
				'description' => esc_html__( 'A sampling of users from your blog.', 'jetpack' ),
40
			)
41
		);
42
43 View Code Duplication
		if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) ) {
44
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
45
		}
46
47
		$this->default_title = esc_html__( 'Community', 'jetpack' );
48
	}
49
50
	/**
51
	 * Enqueue stylesheet for grid layout.
52
	 */
53
	function enqueue_style() {
54
		wp_register_style( 'jetpack-my-community-widget', plugins_url( 'my-community/style.css', __FILE__ ), array(), '20160129' );
55
		wp_enqueue_style( 'jetpack-my-community-widget' );
56
	}
57
58
	/**
59
	 * Back-end widget form.
60
	 *
61
	 * @see WP_Widget::form()
62
	 *
63
	 * @param array $instance Previously saved values from database.
64
	 *
65
	 * @return string|void
66
	 */
67
	function form( $instance ) {
68
		$title = isset( $instance['title' ] ) ? $instance['title'] : false;
69
		if ( false === $title ) {
70
			$title = $this->default_title;
71
		}
72
73
		$number = isset( $instance['number'] ) ? $instance['number'] : 10;
74
		if ( ! in_array( $number, array( 10, 50 ) ) ) {
75
			$number = 10;
76
		}
77
78
		$include_likers     = isset( $instance['include_likers'] )     ? (bool) $instance['include_likers']     : true;
79
		$include_followers  = isset( $instance['include_followers'] )  ? (bool) $instance['include_followers']  : true;
80
		$include_commenters = isset( $instance['include_commenters'] ) ? (bool) $instance['include_commenters'] : true;
81
		?>
82
83
		<p>
84
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
85
			<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 ); ?>" />
86
		</p>
87
88
		<p>
89
			<label><?php esc_html_e( 'Show how many users?', 'jetpack' ); ?></label>
90
		</p>
91
		<ul>
92
			<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( 'A few', 'jetpack' ); ?></label></li>
93
			<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( 'Lots', 'jetpack' ); ?></label></li>
94
		</ul>
95
96
		<p>
97
			<label for="<?php echo $this->get_field_id( 'include_likers' ); ?>">
98
				<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 ); ?> />
99
				<?php esc_html_e( 'Include activity from likers', 'jetpack' ); ?>
100
			</label>
101
		</p>
102
103
		<p>
104
			<label for="<?php echo $this->get_field_id( 'include_followers' ); ?>">
105
				<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 ); ?> />
106
				<?php esc_html_e( 'Include activity from followers', 'jetpack' ); ?>
107
			</label>
108
		</p>
109
110
		<p>
111
			<label for="<?php echo $this->get_field_id( 'include_commenters' ); ?>">
112
				<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 ); ?> />
113
				<?php esc_html_e( 'Include activity from commenters', 'jetpack' ); ?>
114
			</label>
115
		</p>
116
117
		<?php
118
	}
119
120
	/**
121
	 * Sanitize widget form values as they are saved.
122
	 *
123
	 * @see WP_Widget::update()
124
	 *
125
	 * @param array $new_instance Values just sent to be saved.
126
	 * @param array $old_instance Previously saved values from database.
127
	 *
128
	 * @return array Updated safe values to be saved.
129
	 */
130
	function update( $new_instance, $old_instance ) {
131
		$instance = array();
132
		$instance['title'] = wp_kses( $new_instance['title'], array() );
133
		if ( $instance['title'] === $this->default_title ) {
134
			$instance['title'] = false; // Store as false in case of language change
135
		}
136
137
		$instance['number'] = (int) $new_instance['number'];
138
		if ( !in_array( $instance['number'], array( 10, 50 ) ) ) {
139
			$instance['number'] = 10;
140
		}
141
142
		$instance['include_likers']     = (bool) $new_instance['include_likers'];
143
		$instance['include_followers']  = (bool) $new_instance['include_followers'];
144
		$instance['include_commenters'] = (bool) $new_instance['include_commenters'];
145
146
		delete_transient( "$this->id-{$instance['number']}" . (int) $instance['include_likers'] . (int) $instance['include_followers'] . (int) $instance['include_commenters'] );
147
148
		return $instance;
149
	}
150
151
	/**
152
	 * Front-end display of widget.
153
	 *
154
	 * @see WP_Widget::widget()
155
	 *
156
	 * @param array $args     Widget arguments.
157
	 * @param array $instance Saved values from database.
158
	 */
159
	function widget( $args, $instance ) {
160
		$title = isset( $instance['title' ] ) ? $instance['title'] : false;
161
162
		if ( false === $title ) {
163
			$title = $this->default_title;
164
		}
165
166
		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
167
		$title = apply_filters( 'widget_title', $title );
168
169
		echo $args['before_widget'];
170
171
		if ( ! empty( $title ) ) {
172
			echo $args['before_title'] . $title . $args['after_title'];
173
		}
174
175
		$transient_name = "$this->id-{$instance['number']}" . (int) $instance['include_likers'] . (int) $instance['include_followers'] . (int) $instance['include_commenters'];
176
177
		$my_community = get_transient( $transient_name );
178
179
		if ( empty( $my_community ) ) {
180
			$my_community = $this->get_community( $instance );
181
182
			set_transient( $transient_name, $my_community, self::$expiration );
183
		}
184
185
		echo $my_community;
186
187
		echo $args['after_widget'];
188
189
		/** This action is documented in modules/widgets/gravatar-profile.php */
190
		do_action( 'jetpack_stats_extra', 'widget_view', 'my_community' );
191
	}
192
193
	/**
194
	 * Initiate request and render the response.
195
	 *
196
	 * @since 4.0
197
	 *
198
	 * @param array $query
199
	 *
200
	 * @return string
201
	 */
202
	function get_community( $query ) {
203
		$members = $this->fetch_remote_community( $query );
204
205
		if ( ! empty( $members ) ) {
206
207
			$my_community = '<div class="widgets-multi-column-grid"><ul>';
208
209
			foreach ( $members as $member ) {
210
				$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>',
211
					$member->profile_URL,
212
					empty( $member->name ) ? '' : 'title="' . $member->name . '"',
213
					$member->avatar_URL
214
				);
215
			}
216
217
			$my_community .= '</ul></div>';
218
219
		} else {
220
			if ( current_user_can( 'edit_theme_options' ) ) {
221
				$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' ),
222
						admin_url( 'widgets.php' ),
223
						'http://en.support.wordpress.com/getting-more-site-traffic/'
224
					), array( 'a' => array( 'href' => true ) ) ) . '</p>';
225
			} else {
226
				$my_community = '<p>' . esc_html__( "I'm just starting out; leave me a comment or a like :)", 'jetpack' ) . '</p>';
227
			}
228
		}
229
230
		return $my_community;
231
	}
232
233
	/**
234
	 * Request community members to WordPress.com endpoint.
235
	 *
236
	 * @since 4.0
237
	 *
238
	 * @param $query
239
	 *
240
	 * @return array
241
	 */
242
	function fetch_remote_community( $query ) {
243
		$jetpack_blog_id = Jetpack_Options::get_option( 'id' );
244
		$url = add_query_arg(
245
			array(
246
				'number'     => $query['number'],
247
				'likers'     => (int) $query['include_likers'],
248
				'followers'  => (int) $query['include_followers'],
249
				'commenters' => (int) $query['include_commenters'],
250
			),
251
			"https://public-api.wordpress.com/rest/v1.1/sites/$jetpack_blog_id/community"
252
		);
253
		$response = wp_remote_get( $url );
254
		$response_body = wp_remote_retrieve_body( $response );
255
256
		if ( empty( $response_body ) ) {
257
			return array();
258
		}
259
260
		$response_body = json_decode( $response_body );
261
262
		if ( isset( $response_body->users ) ) {
263
			return $response_body->users;
264
		}
265
266
		return array();
267
	}
268
}
269
270
/**
271
 * If site is connected to WordPress.com, register the widget.
272
 *
273
 * @since 4.0
274
 */
275
function jetpack_my_community_init() {
276
	if ( Jetpack::is_active() ) {
277
		register_widget( 'Jetpack_My_Community_Widget' );
278
	}
279
}
280
281
add_action( 'widgets_init', 'jetpack_my_community_init' );