Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 208
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
jetpack_facebook_likebox_init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
WPCOM_Widget_Facebook_LikeBox
0.00% covered (danger)
0.00%
0 / 204
0.00% covered (danger)
0.00%
0 / 9
1190
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 enqueue_scripts
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 widget
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
182
 update
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 form
0.00% covered (danger)
0.00%
0 / 87
0.00% covered (danger)
0.00%
0 / 1
12
 get_default_args
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 normalize_facebook_args
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
56
 is_valid_facebook_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 normalize_int_value
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
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
5if ( ! defined( 'ABSPATH' ) ) {
6    exit( 0 );
7}
8
9add_action( 'widgets_init', 'jetpack_facebook_likebox_init' );
10/**
11 * Register the widget for use in Appearance -> Widgets
12 */
13function 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 */
22class 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            // Widgets saved before the setting existed keep their linked title.
174            $link_title           = ! isset( $instance['link_title'] ) || $instance['link_title'];
175            $likebox_widget_title = $link_title ? '<a href="' . esc_url( $page_url ) . '">' . $title . '</a>' : $title;
176            /**
177             * Filter Facebook Likebox's widget title.
178             *
179             * @module widgets
180             *
181             * @since 3.3.0
182             * @since $$next-version$$ Added the `$link_title` parameter. The title is only linked when that setting is enabled.
183             *
184             * @param string $likebox_widget_title Likebox Widget title, linked to the Page URL when `$link_title` is true.
185             * @param string $title Widget title as set in the widget settings.
186             * @param string $page_url Facebook Page URL.
187             * @param bool   $link_title Whether the widget settings link the title to the Page URL.
188             */
189            $likebox_widget_title = apply_filters( 'jetpack_facebook_likebox_title', $likebox_widget_title, $title, $page_url, $link_title );
190
191            echo wp_kses(
192                $likebox_widget_title,
193                array( 'a' => array( 'href' => array() ) )
194            );
195
196            echo $after_title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
197        endif;
198
199        ?>
200        <div id="fb-root"></div>
201        <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'] ); ?>">
202        <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>
203        </div>
204        <?php
205        wp_enqueue_script( 'jetpack-facebook-embed' );
206
207        echo $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
208
209        /** This action is documented in modules/widgets/gravatar-profile.php */
210        do_action( 'jetpack_stats_extra', 'widget_view', 'facebook-likebox' );
211    }
212
213    /**
214     * Update widget.
215     *
216     * @see WP_Widget::update()
217     *
218     * @param array $new_instance New widget instance data.
219     * @param array $old_instance Old widget instance data.
220     */
221    public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
222        $instance = array();
223
224        $instance['title']      = trim( wp_strip_all_tags( stripslashes( $new_instance['title'] ?? '' ) ) );
225        $instance['link_title'] = isset( $new_instance['link_title'] );
226
227        // Set up widget values.
228        $instance['like_args'] = array(
229            'href'         => trim( wp_strip_all_tags( stripslashes( $new_instance['href'] ?? '' ) ) ),
230            'width'        => (int) ( $new_instance['width'] ?? $this->default_width ),
231            'height'       => (int) ( $new_instance['height'] ?? $this->default_height ),
232            'show_faces'   => isset( $new_instance['show_faces'] ),
233            'stream'       => isset( $new_instance['stream'] ),
234            'cover'        => isset( $new_instance['cover'] ),
235            'small_header' => isset( $new_instance['small_header'] ),
236        );
237
238        $instance['like_args'] = $this->normalize_facebook_args( $instance['like_args'] );
239
240        // Include the new instance's args in the array's top level to support updating from the Widgets page.
241        $instance = array_merge( $instance, array_intersect_key( $instance['like_args'], $new_instance ) );
242
243        return $instance;
244    }
245
246    /**
247     * Outputs the widget settings form.
248     *
249     * @param array $instance Current settings.
250     * @return string|void
251     */
252    public function form( $instance ) {
253        // New widgets start unlinked; saved widgets without the setting were always linked.
254        $link_title = empty( $instance ) ? false : ( ! isset( $instance['link_title'] ) || $instance['link_title'] );
255        $instance   = wp_parse_args(
256            (array) $instance,
257            array(
258                'title'     => '',
259                'like_args' => $this->get_default_args(),
260            )
261        );
262        $like_args  = $this->normalize_facebook_args( $instance['like_args'] );
263        ?>
264
265        <p>
266            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
267                <?php esc_html_e( 'Title', 'jetpack' ); ?>
268                <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" />
269            </label>
270        </p>
271
272        <p>
273            <label for="<?php echo esc_attr( $this->get_field_id( 'link_title' ) ); ?>">
274                <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'link_title' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'link_title' ) ); ?>" <?php checked( $link_title ); ?> />
275                <?php esc_html_e( 'Link title to Facebook Page', 'jetpack' ); ?>
276            </label>
277        </p>
278
279        <p>
280            <label for="<?php echo esc_attr( $this->get_field_id( 'href' ) ); ?>">
281                <?php esc_html_e( 'Facebook Page URL', 'jetpack' ); ?>
282                <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" />
283                <br />
284                <small><?php esc_html_e( 'The widget only works with Facebook Pages.', 'jetpack' ); ?></small>
285            </label>
286        </p>
287
288        <p>
289            <label for="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>">
290                <?php esc_html_e( 'Width in pixels', 'jetpack' ); ?>
291                <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;" />
292                <small>
293                    <?php
294                    echo esc_html(
295                        sprintf(
296                            /* translators: %s is the minimum pixel width */
297                            __( 'Minimum: %s', 'jetpack' ),
298                            $this->min_width
299                        )
300                    );
301                    echo ' / ';
302                    echo esc_html(
303                        sprintf(
304                            /* translators: %s is the maximum pixel width */
305                            __( 'Maximum: %s', 'jetpack' ),
306                            $this->max_width
307                        )
308                    );
309                    ?>
310                </small>
311            </label>
312        </p>
313
314        <p>
315            <label for="<?php echo esc_attr( $this->get_field_id( 'height' ) ); ?>">
316                <?php esc_html_e( 'Height in pixels', 'jetpack' ); ?>
317                <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;" />
318                <small>
319                    <?php
320                    echo esc_html(
321                        sprintf(
322                            /* translators: %s is the minimum pixel height */
323                            __( 'Minimum: %s', 'jetpack' ),
324                            $this->min_height
325                        )
326                    );
327                    echo ' / ';
328                    echo esc_html(
329                        sprintf(
330                            /* translators: %s is the maximum pixel height */
331                            __( 'Maximum: %s', 'jetpack' ),
332                            $this->max_height
333                        )
334                    );
335                    ?>
336                </small>
337            </label>
338        </p>
339
340        <p>
341            <label for="<?php echo esc_attr( $this->get_field_id( 'show_faces' ) ); ?>">
342                <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'] ); ?> />
343                <?php esc_html_e( 'Show Faces', 'jetpack' ); ?>
344                <br />
345                <small><?php esc_html_e( 'Show profile photos in the plugin.', 'jetpack' ); ?></small>
346            </label>
347        </p>
348
349        <p>
350            <label for="<?php echo esc_attr( $this->get_field_id( 'stream' ) ); ?>">
351                <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'] ); ?> />
352                <?php esc_html_e( 'Show Timeline', 'jetpack' ); ?>
353                <br />
354                <small><?php esc_html_e( 'Show Page Posts.', 'jetpack' ); ?></small>
355            </label>
356        </p>
357
358        <p>
359            <label for="<?php echo esc_attr( $this->get_field_id( 'cover' ) ); ?>">
360                <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'] ); ?> />
361                <?php esc_html_e( 'Show Cover Photo', 'jetpack' ); ?>
362                <br />
363            </label>
364        </p>
365
366        <p>
367            <label for="<?php echo esc_attr( $this->get_field_id( 'small_header' ) ); ?>">
368                <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'] ); ?> />
369                <?php esc_html_e( 'Use Small Header', 'jetpack' ); ?>
370                <br />
371            </label>
372        </p>
373
374        <?php
375    }
376
377    /**
378     * Facebook Likebox default options.
379     */
380    public function get_default_args() {
381        $defaults = array(
382            'href'         => '',
383            'width'        => $this->default_width,
384            'height'       => $this->default_height,
385            'show_faces'   => 'true',
386            'stream'       => '',
387            'cover'        => 'true',
388            'small_header' => '',
389        );
390
391        /**
392         * Filter Facebook Likebox default options.
393         *
394         * @module widgets
395         *
396         * @since 1.3.1
397         *
398         * @param array $defaults Array of default options.
399         */
400        return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults );
401    }
402
403    /**
404     * Normalize the Facebook Likebox options.
405     *
406     * @param array $args Array of arguments.
407     */
408    public function normalize_facebook_args( $args ) {
409        $args = wp_parse_args( (array) $args, $this->get_default_args() );
410
411        // Validate the Facebook Page URL.
412        if ( $this->is_valid_facebook_url( $args['href'] ) ) {
413            $temp         = explode( '?', $args['href'] );
414            $args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] );
415        } else {
416            $args['href'] = '';
417        }
418
419        $args['width']        = $this->normalize_int_value( (int) $args['width'], $this->max_width, $this->min_width );
420        $args['height']       = $this->normalize_int_value( (int) $args['height'], $this->max_height, $this->min_height );
421        $args['show_faces']   = (bool) $args['show_faces'];
422        $args['stream']       = (bool) $args['stream'];
423        $args['cover']        = (bool) $args['cover'];
424        $args['small_header'] = (bool) $args['small_header'];
425
426        // The height used to be dependent on other widget settings
427        // If the user changes those settings but doesn't customize the height,
428        // let's intelligently assign a new height.
429        if ( in_array( $args['height'], array( 580, 110, 432 ), true ) ) {
430            if ( $args['show_faces'] && $args['stream'] ) {
431                $args['height'] = 580;
432            } elseif ( ! $args['show_faces'] && ! $args['stream'] ) {
433                $args['height'] = 130;
434            } else {
435                $args['height'] = 432;
436            }
437        }
438
439        return $args;
440    }
441
442    /**
443     * Check if URL is a valid Facebook Page URL.
444     *
445     * @param string $url URL to check.
446     */
447    public function is_valid_facebook_url( $url ) {
448        return str_contains( $url, 'facebook.com' );
449    }
450
451    /**
452     * Normalize an integer value within a given range.
453     *
454     * @param int $value Value to normalize.
455     * @param int $max Maximum value.
456     * @param int $min Minimum value.
457     *
458     * @return int Normalized value.
459     */
460    public function normalize_int_value( $value, $max = 0, $min = 0 ) {
461        $value = (int) $value;
462
463        if ( $value > $max ) {
464            $value = $max;
465        } elseif ( $value < $min ) {
466            $value = $min;
467        }
468
469        return (int) $value;
470    }
471}