PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 All 502 releases
jetpack / modules / widgets / facebook-likebox.php

facebook-likebox.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at modules/widgets/facebook-likebox.php

458 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit( 0 );
7 }
8
9 add_action( 'widgets_init', 'jetpack_facebook_likebox_init' );
10 /**
11 * Register the widget for use in Appearance -> Widgets
12 */
13 function jetpack_facebook_likebox_init() {
14 register_widget( 'WPCOM_Widget_Facebook_LikeBox' );
15 }
16
17 /**
18 * Facebook Page Plugin (formerly known as the Like Box)
19 * Display a Facebook Page Plugin as a widget (replaces the old like box plugin)
20 * https://developers.facebook.com/docs/plugins/page-plugin
21 */
22 class WPCOM_Widget_Facebook_LikeBox extends WP_Widget {
23 /**
24 * Default height.
25 *
26 * @var int
27 */
28 private $default_height = 580;
29
30 /**
31 * Default width.
32 *
33 * @var int
34 */
35 private $default_width = 340;
36
37 /**
38 * Max width.
39 *
40 * @var int
41 */
42 private $max_width = 500;
43
44 /**
45 * Min width.
46 *
47 * @var int
48 */
49 private $min_width = 180;
50
51 /**
52 * Max height.
53 *
54 * @var int
55 */
56 private $max_height = 9999;
57
58 /**
59 * Min height/
60 *
61 * @var int
62 */
63 private $min_height = 130;
64
65 /**
66 * WPCOM_Widget_Facebook_LikeBox constructor.
67 */
68 public function __construct() {
69 parent::__construct(
70 'facebook-likebox',
71 /**
72 * Filter the name of a widget included in the Extra Sidebar Widgets module.
73 *
74 * @module widgets
75 *
76 * @since 2.1.2
77 *
78 * @param string $widget_name Widget title.
79 */
80 apply_filters( 'jetpack_widget_name', __( 'Facebook Page Plugin', 'jetpack' ) ),
81 array(
82 'classname' => 'widget_facebook_likebox',
83 'description' => __( 'Use the Facebook Page Plugin to connect visitors to your Facebook Page', 'jetpack' ),
84 'customize_selective_refresh' => true,
85 )
86 );
87
88 if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
89 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
90 }
91 }
92
93 /**
94 * Enqueue scripts.
95 */
96 public function enqueue_scripts() {
97 wp_enqueue_script( 'jetpack-facebook-embed' );
98 wp_enqueue_style(
99 'jetpack_facebook_likebox',
100 plugins_url( 'facebook-likebox/style.css', __FILE__ ),
101 array(),
102 JETPACK__VERSION
103 );
104 // Inline styles. @see wp_maybe_inline_styles()
105 wp_style_add_data( 'jetpack_facebook_likebox', 'path', plugin_dir_path( __FILE__ ) . 'facebook-likebox/style.css' );
106 }
107
108 /**
109 * Display the widget.
110 *
111 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
112 * @param array $instance The settings for the particular instance of the widget.
113 */
114 public function widget( $args, $instance ) {
115 $before_widget = $args['before_widget'] ?? '';
116 $before_title = $args['before_title'] ?? '';
117 $after_title = $args['after_title'] ?? '';
118 $after_widget = $args['after_widget'] ?? '';
119 $like_args = $this->get_default_args();
120
121 if ( isset( $instance['like_args'] ) ) {
122 $like_args = $this->normalize_facebook_args( $instance['like_args'] );
123 }
124
125 if ( empty( $like_args['href'] ) || ! $this->is_valid_facebook_url( $like_args['href'] ) ) {
126 if ( current_user_can( 'edit_theme_options' ) ) {
127 echo $before_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
128
129 $error_link = wp_kses(
130 sprintf(
131 /* translators: %s: link to widgets administration screen. */
132 __( 'It looks like your Facebook URL is incorrectly configured. Please check it in your <a href="%1$s">widget settings</a>.', 'jetpack' ),
133 esc_url( admin_url( 'widgets.php' ) )
134 ),
135 array( 'a' => array( 'href' => array() ) )
136 );
137 printf(
138 '<p>%s</p>',
139 $error_link // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
140 );
141
142 echo $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
143 }
144 echo '<!-- Invalid Facebook Page URL -->';
145 return;
146 }
147
148 /** This filter is documented in core/src/wp-includes/default-widgets.php */
149 $title = apply_filters( 'widget_title', $instance['title'] );
150 $page_url = set_url_scheme( $like_args['href'], 'https' );
151
152 $like_args['show_faces'] = (bool) $like_args['show_faces'] ? 'true' : 'false';
153 $like_args['stream'] = (bool) $like_args['stream'] ? 'timeline' : 'false';
154 $like_args['cover'] = (bool) $like_args['cover'] ? 'false' : 'true';
155 $like_args['small_header'] = (bool) $like_args['small_header'] ? 'true' : 'false';
156
157 /**
158 * Filter Facebook Likebox's widget call to action button
159 *
160 * @module widgets
161 *
162 * @since 8.4.0
163 *
164 * @param bool True value hides the call to action button
165 */
166 $hide_cta = apply_filters( 'jetpack_facebook_likebox_hide_cta', false );
167
168 echo $before_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
169
170 if ( ! empty( $title ) ) :
171 echo $before_title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
172
173 $likebox_widget_title = '<a href="' . esc_url( $page_url ) . '">' . $title . '</a>';
174 /**
175 * Filter Facebook Likebox's widget title.
176 *
177 * @module widgets
178 *
179 * @since 3.3.0
180 *
181 * @param string $likebox_widget_title Likebox Widget title (including a link to the Page URL).
182 * @param string $title Widget title as set in the widget settings.
183 * @param string $page_url Facebook Page URL.
184 */
185 $likebox_widget_title = apply_filters( 'jetpack_facebook_likebox_title', $likebox_widget_title, $title, $page_url );
186
187 echo wp_kses(
188 $likebox_widget_title,
189 array( 'a' => array( 'href' => array() ) )
190 );
191
192 echo $after_title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
193 endif;
194
195 ?>
196 <div id="fb-root"></div>
197 <div class="fb-page" data-href="<?php echo esc_url( $page_url ); ?>" data-width="<?php echo (int) $like_args['width']; ?>" data-height="<?php echo (int) $like_args['height']; ?>" data-hide-cover="<?php echo esc_attr( $like_args['cover'] ); ?>" data-show-facepile="<?php echo esc_attr( $like_args['show_faces'] ); ?>" data-tabs="<?php echo esc_attr( $like_args['stream'] ); ?>" data-hide-cta="<?php echo esc_attr( $hide_cta ? 'true' : 'false' ); ?>" data-small-header="<?php echo esc_attr( $like_args['small_header'] ); ?>">
198 <div class="fb-xfbml-parse-ignore"><blockquote cite="<?php echo esc_url( $page_url ); ?>"><a href="<?php echo esc_url( $page_url ); ?>"><?php echo esc_html( $title ); ?></a></blockquote></div>
199 </div>
200 <?php
201 wp_enqueue_script( 'jetpack-facebook-embed' );
202
203 echo $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
204
205 /** This action is documented in modules/widgets/gravatar-profile.php */
206 do_action( 'jetpack_stats_extra', 'widget_view', 'facebook-likebox' );
207 }
208
209 /**
210 * Update widget.
211 *
212 * @see WP_Widget::update()
213 *
214 * @param array $new_instance New widget instance data.
215 * @param array $old_instance Old widget instance data.
216 */
217 public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
218 $instance = array();
219
220 $instance['title'] = trim( wp_strip_all_tags( stripslashes( $new_instance['title'] ) ) );
221
222 // Set up widget values.
223 $instance['like_args'] = array(
224 'href' => trim( wp_strip_all_tags( stripslashes( $new_instance['href'] ) ) ),
225 'width' => (int) $new_instance['width'],
226 'height' => (int) $new_instance['height'],
227 'show_faces' => isset( $new_instance['show_faces'] ),
228 'stream' => isset( $new_instance['stream'] ),
229 'cover' => isset( $new_instance['cover'] ),
230 'small_header' => isset( $new_instance['small_header'] ),
231 );
232
233 $instance['like_args'] = $this->normalize_facebook_args( $instance['like_args'] );
234
235 // Include the new instance's args in the array's top level to support updating from the Widgets page.
236 $instance = array_merge( $instance, array_intersect_key( $instance['like_args'], $new_instance ) );
237
238 return $instance;
239 }
240
241 /**
242 * Outputs the widget settings form.
243 *
244 * @param array $instance Current settings.
245 * @return string|void
246 */
247 public function form( $instance ) {
248 $instance = wp_parse_args(
249 (array) $instance,
250 array(
251 'title' => '',
252 'like_args' => $this->get_default_args(),
253 )
254 );
255 $like_args = $this->normalize_facebook_args( $instance['like_args'] );
256 ?>
257
258 <p>
259 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
260 <?php esc_html_e( 'Title', 'jetpack' ); ?>
261 <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" />
262 </label>
263 </p>
264
265 <p>
266 <label for="<?php echo esc_attr( $this->get_field_id( 'href' ) ); ?>">
267 <?php esc_html_e( 'Facebook Page URL', 'jetpack' ); ?>
268 <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'href' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'href' ) ); ?>" value="<?php echo esc_url( $like_args['href'] ); ?>" class="widefat" />
269 <br />
270 <small><?php esc_html_e( 'The widget only works with Facebook Pages.', 'jetpack' ); ?></small>
271 </label>
272 </p>
273
274 <p>
275 <label for="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>">
276 <?php esc_html_e( 'Width in pixels', 'jetpack' ); ?>
277 <input type="number" class="smalltext" min="<?php echo esc_attr( $this->min_width ); ?>" max="<?php echo esc_attr( $this->max_width ); ?>" maxlength="3" name="<?php echo esc_attr( $this->get_field_name( 'width' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>" value="<?php echo esc_attr( $like_args['width'] ); ?>" style="text-align: center;" />
278 <small>
279 <?php
280 echo esc_html(
281 sprintf(
282 /* translators: %s is the minimum pixel width */
283 __( 'Minimum: %s', 'jetpack' ),
284 $this->min_width
285 )
286 );
287 echo ' / ';
288 echo esc_html(
289 sprintf(
290 /* translators: %s is the maximum pixel width */
291 __( 'Maximum: %s', 'jetpack' ),
292 $this->max_width
293 )
294 );
295 ?>
296 </small>
297 </label>
298 </p>
299
300 <p>
301 <label for="<?php echo esc_attr( $this->get_field_id( 'height' ) ); ?>">
302 <?php esc_html_e( 'Height in pixels', 'jetpack' ); ?>
303 <input type="number" class="smalltext" min="<?php echo esc_attr( $this->min_height ); ?>" max="<?php echo esc_attr( $this->max_height ); ?>" maxlength="3" name="<?php echo esc_attr( $this->get_field_name( 'height' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'height' ) ); ?>" value="<?php echo esc_attr( $like_args['height'] ); ?>" style="text-align: center;" />
304 <small>
305 <?php
306 echo esc_html(
307 sprintf(
308 /* translators: %s is the minimum pixel height */
309 __( 'Minimum: %s', 'jetpack' ),
310 $this->min_height
311 )
312 );
313 echo ' / ';
314 echo esc_html(
315 sprintf(
316 /* translators: %s is the maximum pixel height */
317 __( 'Maximum: %s', 'jetpack' ),
318 $this->max_height
319 )
320 );
321 ?>
322 </small>
323 </label>
324 </p>
325
326 <p>
327 <label for="<?php echo esc_attr( $this->get_field_id( 'show_faces' ) ); ?>">
328 <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_faces' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'show_faces' ) ); ?>" <?php checked( $like_args['show_faces'] ); ?> />
329 <?php esc_html_e( 'Show Faces', 'jetpack' ); ?>
330 <br />
331 <small><?php esc_html_e( 'Show profile photos in the plugin.', 'jetpack' ); ?></small>
332 </label>
333 </p>
334
335 <p>
336 <label for="<?php echo esc_attr( $this->get_field_id( 'stream' ) ); ?>">
337 <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'stream' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'stream' ) ); ?>" <?php checked( $like_args['stream'] ); ?> />
338 <?php esc_html_e( 'Show Timeline', 'jetpack' ); ?>
339 <br />
340 <small><?php esc_html_e( 'Show Page Posts.', 'jetpack' ); ?></small>
341 </label>
342 </p>
343
344 <p>
345 <label for="<?php echo esc_attr( $this->get_field_id( 'cover' ) ); ?>">
346 <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'cover' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'cover' ) ); ?>" <?php checked( $like_args['cover'] ); ?> />
347 <?php esc_html_e( 'Show Cover Photo', 'jetpack' ); ?>
348 <br />
349 </label>
350 </p>
351
352 <p>
353 <label for="<?php echo esc_attr( $this->get_field_id( 'small_header' ) ); ?>">
354 <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'small_header' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'small_header' ) ); ?>" <?php checked( $like_args['small_header'] ); ?> />
355 <?php esc_html_e( 'Use Small Header', 'jetpack' ); ?>
356 <br />
357 </label>
358 </p>
359
360 <?php
361 }
362
363 /**
364 * Facebook Likebox default options.
365 */
366 public function get_default_args() {
367 $defaults = array(
368 'href' => '',
369 'width' => $this->default_width,
370 'height' => $this->default_height,
371 'show_faces' => 'true',
372 'stream' => '',
373 'cover' => 'true',
374 'small_header' => '',
375 );
376
377 /**
378 * Filter Facebook Likebox default options.
379 *
380 * @module widgets
381 *
382 * @since 1.3.1
383 *
384 * @param array $defaults Array of default options.
385 */
386 return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults );
387 }
388
389 /**
390 * Normalize the Facebook Likebox options.
391 *
392 * @param array $args Array of arguments.
393 */
394 public function normalize_facebook_args( $args ) {
395 $args = wp_parse_args( (array) $args, $this->get_default_args() );
396
397 // Validate the Facebook Page URL.
398 if ( $this->is_valid_facebook_url( $args['href'] ) ) {
399 $temp = explode( '?', $args['href'] );
400 $args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] );
401 } else {
402 $args['href'] = '';
403 }
404
405 $args['width'] = $this->normalize_int_value( (int) $args['width'], $this->max_width, $this->min_width );
406 $args['height'] = $this->normalize_int_value( (int) $args['height'], $this->max_height, $this->min_height );
407 $args['show_faces'] = (bool) $args['show_faces'];
408 $args['stream'] = (bool) $args['stream'];
409 $args['cover'] = (bool) $args['cover'];
410 $args['small_header'] = (bool) $args['small_header'];
411
412 // The height used to be dependent on other widget settings
413 // If the user changes those settings but doesn't customize the height,
414 // let's intelligently assign a new height.
415 if ( in_array( $args['height'], array( 580, 110, 432 ), true ) ) {
416 if ( $args['show_faces'] && $args['stream'] ) {
417 $args['height'] = 580;
418 } elseif ( ! $args['show_faces'] && ! $args['stream'] ) {
419 $args['height'] = 130;
420 } else {
421 $args['height'] = 432;
422 }
423 }
424
425 return $args;
426 }
427
428 /**
429 * Check if URL is a valid Facebook Page URL.
430 *
431 * @param string $url URL to check.
432 */
433 public function is_valid_facebook_url( $url ) {
434 return str_contains( $url, 'facebook.com' );
435 }
436
437 /**
438 * Normalize an integer value within a given range.
439 *
440 * @param int $value Value to normalize.
441 * @param int $max Maximum value.
442 * @param int $min Minimum value.
443 *
444 * @return int Normalized value.
445 */
446 public function normalize_int_value( $value, $max = 0, $min = 0 ) {
447 $value = (int) $value;
448
449 if ( $value > $max ) {
450 $value = $max;
451 } elseif ( $value < $min ) {
452 $value = $min;
453 }
454
455 return (int) $value;
456 }
457 }
458