facebook-likebox.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Register the widget for use in Appearance -> Widgets |
| 5 | */ |
| 6 | add_action( 'widgets_init', 'jetpack_facebook_likebox_init' ); |
| 7 | |
| 8 | function jetpack_facebook_likebox_init() { |
| 9 | register_widget( 'WPCOM_Widget_Facebook_LikeBox' ); |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Facebook Page Plugin (formerly known as the Like Box) |
| 14 | * Display a Facebook Page Plugin as a widget (replaces the old like box plugin) |
| 15 | * https://developers.facebook.com/docs/plugins/page-plugin |
| 16 | */ |
| 17 | class WPCOM_Widget_Facebook_LikeBox extends WP_Widget { |
| 18 | |
| 19 | private $default_height = 580; |
| 20 | private $default_width = 340; |
| 21 | private $max_width = 500; |
| 22 | private $min_width = 180; |
| 23 | private $max_height = 9999; |
| 24 | private $min_height = 130; |
| 25 | |
| 26 | function __construct() { |
| 27 | parent::__construct( |
| 28 | 'facebook-likebox', |
| 29 | /** |
| 30 | * Filter the name of a widget included in the Extra Sidebar Widgets module. |
| 31 | * |
| 32 | * @module widgets |
| 33 | * |
| 34 | * @since 2.1.2 |
| 35 | * |
| 36 | * @param string $widget_name Widget title. |
| 37 | */ |
| 38 | apply_filters( 'jetpack_widget_name', __( 'Facebook Page Plugin', 'jetpack' ) ), |
| 39 | array( |
| 40 | 'classname' => 'widget_facebook_likebox', |
| 41 | 'description' => __( 'Use the Facebook Page Plugin to connect visitors to your Facebook Page', 'jetpack' ), |
| 42 | 'customize_selective_refresh' => true, |
| 43 | ) |
| 44 | ); |
| 45 | |
| 46 | if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) { |
| 47 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Enqueue scripts. |
| 53 | */ |
| 54 | public function enqueue_scripts() { |
| 55 | wp_enqueue_script( 'jetpack-facebook-embed' ); |
| 56 | wp_enqueue_style( 'jetpack_facebook_likebox', plugins_url( 'facebook-likebox/style.css', __FILE__ ) ); |
| 57 | wp_style_add_data( 'jetpack_facebook_likebox', 'jetpack-inline', true ); |
| 58 | } |
| 59 | |
| 60 | function widget( $args, $instance ) { |
| 61 | extract( $args ); |
| 62 | |
| 63 | $like_args = $this->normalize_facebook_args( $instance['like_args'] ); |
| 64 | |
| 65 | if ( empty( $like_args['href'] ) || ! $this->is_valid_facebook_url( $like_args['href'] ) ) { |
| 66 | if ( current_user_can('edit_theme_options') ) { |
| 67 | echo $before_widget; |
| 68 | echo '<p>' . sprintf( __( 'It looks like your Facebook URL is incorrectly configured. Please check it in your <a href="%s">widget settings</a>.', 'jetpack' ), admin_url( 'widgets.php' ) ) . '</p>'; |
| 69 | echo $after_widget; |
| 70 | } |
| 71 | echo '<!-- Invalid Facebook Page URL -->'; |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
| 76 | $title = apply_filters( 'widget_title', $instance['title'] ); |
| 77 | $page_url = set_url_scheme( $like_args['href'], 'https' ); |
| 78 | |
| 79 | $like_args['show_faces'] = (bool) $like_args['show_faces'] ? 'true' : 'false'; |
| 80 | $like_args['stream'] = (bool) $like_args['stream'] ? 'true' : 'false'; |
| 81 | $like_args['cover'] = (bool) $like_args['cover'] ? 'false' : 'true'; |
| 82 | |
| 83 | echo $before_widget; |
| 84 | |
| 85 | if ( ! empty( $title ) ) : |
| 86 | echo $before_title; |
| 87 | |
| 88 | $likebox_widget_title = '<a href="' . esc_url( $page_url ) . '">' . esc_html( $title ) . '</a>'; |
| 89 | |
| 90 | /** |
| 91 | * Filter Facebook Likebox's widget title. |
| 92 | * |
| 93 | * @module widgets |
| 94 | * |
| 95 | * @since 3.3.0 |
| 96 | * |
| 97 | * @param string $likebox_widget_title Likebox Widget title (including a link to the Page URL). |
| 98 | * @param string $title Widget title as set in the widget settings. |
| 99 | * @param string $page_url Facebook Page URL. |
| 100 | */ |
| 101 | echo apply_filters( 'jetpack_facebook_likebox_title', $likebox_widget_title, $title, $page_url ); |
| 102 | |
| 103 | echo $after_title; |
| 104 | endif; |
| 105 | |
| 106 | ?> |
| 107 | <div id="fb-root"></div> |
| 108 | <div class="fb-page" data-href="<?php echo esc_url( $page_url ); ?>" data-width="<?php echo intval( $like_args['width'] ); ?>" data-height="<?php echo intval( $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-show-posts="<?php echo esc_attr( $like_args['stream'] ); ?>"> |
| 109 | <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> |
| 110 | </div> |
| 111 | <?php |
| 112 | wp_enqueue_script( 'jetpack-facebook-embed' ); |
| 113 | echo $after_widget; |
| 114 | |
| 115 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
| 116 | do_action( 'jetpack_stats_extra', 'widget_view', 'facebook-likebox' ); |
| 117 | } |
| 118 | |
| 119 | function update( $new_instance, $old_instance ) { |
| 120 | $instance = array( |
| 121 | 'title' => '', |
| 122 | 'like_args' => $this->get_default_args(), |
| 123 | ); |
| 124 | |
| 125 | $instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) ); |
| 126 | |
| 127 | // Set up widget values |
| 128 | $instance['like_args'] = array( |
| 129 | 'href' => trim( strip_tags( stripslashes( $new_instance['href'] ) ) ), |
| 130 | 'width' => (int) $new_instance['width'], |
| 131 | 'height' => (int) $new_instance['height'], |
| 132 | 'show_faces' => isset( $new_instance['show_faces'] ), |
| 133 | 'stream' => isset( $new_instance['stream'] ), |
| 134 | 'cover' => isset( $new_instance['cover'] ), |
| 135 | ); |
| 136 | |
| 137 | $instance['like_args'] = $this->normalize_facebook_args( $instance['like_args'] ); |
| 138 | |
| 139 | return $instance; |
| 140 | } |
| 141 | |
| 142 | function form( $instance ) { |
| 143 | $instance = wp_parse_args( (array) $instance, array( |
| 144 | 'title' => '', |
| 145 | 'like_args' => $this->get_default_args() |
| 146 | ) ); |
| 147 | $like_args = $this->normalize_facebook_args( $instance['like_args'] ); |
| 148 | ?> |
| 149 | |
| 150 | <p> |
| 151 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> |
| 152 | <?php _e( 'Title', 'jetpack' ); ?> |
| 153 | <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" /> |
| 154 | </label> |
| 155 | </p> |
| 156 | |
| 157 | <p> |
| 158 | <label for="<?php echo esc_attr( $this->get_field_id( 'href' ) ); ?>"> |
| 159 | <?php _e( 'Facebook Page URL', 'jetpack' ); ?> |
| 160 | <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" /> |
| 161 | <br /> |
| 162 | <small><?php _e( 'The widget only works with Facebook Pages.', 'jetpack' ); ?></small> |
| 163 | </label> |
| 164 | </p> |
| 165 | |
| 166 | <p> |
| 167 | <label for="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>"> |
| 168 | <?php _e( 'Width in pixels', 'jetpack' ); ?> |
| 169 | <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;" /> |
| 170 | <small><?php echo sprintf( __( 'Minimum: %s', 'jetpack' ), $this->min_width ); ?> / <?php echo sprintf( __( 'Maximum: %s', 'jetpack' ), $this->max_width ); ?></small> |
| 171 | </label> |
| 172 | </p> |
| 173 | |
| 174 | <p> |
| 175 | <label for="<?php echo esc_attr( $this->get_field_id( 'height' ) ); ?>"> |
| 176 | <?php _e( 'Height in pixels', 'jetpack' ); ?> |
| 177 | <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;" /> |
| 178 | <small><?php echo sprintf( __( 'Minimum: %s', 'jetpack' ), $this->min_height ); ?> / <?php echo sprintf( __( 'Maximum: %s', 'jetpack' ), $this->max_height ); ?></small> |
| 179 | </label> |
| 180 | </p> |
| 181 | |
| 182 | <p> |
| 183 | <label for="<?php echo esc_attr( $this->get_field_id( 'show_faces' ) ); ?>"> |
| 184 | <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'] ); ?> /> |
| 185 | <?php _e( 'Show Faces', 'jetpack' ); ?> |
| 186 | <br /> |
| 187 | <small><?php _e( 'Show profile photos in the plugin.', 'jetpack' ); ?></small> |
| 188 | </label> |
| 189 | </p> |
| 190 | |
| 191 | <p> |
| 192 | <label for="<?php echo esc_attr( $this->get_field_id( 'stream' ) ); ?>"> |
| 193 | <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'] ); ?> /> |
| 194 | <?php _e( 'Show Stream', 'jetpack' ); ?> |
| 195 | <br /> |
| 196 | <small><?php _e( 'Show Page Posts.', 'jetpack' ); ?></small> |
| 197 | </label> |
| 198 | </p> |
| 199 | |
| 200 | <p> |
| 201 | <label for="<?php echo esc_attr( $this->get_field_id( 'cover' ) ); ?>"> |
| 202 | <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'] ); ?> /> |
| 203 | <?php _e( 'Show Cover Photo', 'jetpack' ); ?> |
| 204 | <br /> |
| 205 | </label> |
| 206 | </p> |
| 207 | |
| 208 | <?php |
| 209 | } |
| 210 | |
| 211 | function get_default_args() { |
| 212 | $defaults = array( |
| 213 | 'href' => '', |
| 214 | 'width' => $this->default_width, |
| 215 | 'height' => $this->default_height, |
| 216 | 'show_faces' => 'true', |
| 217 | 'stream' => '', |
| 218 | 'cover' => 'true', |
| 219 | ); |
| 220 | |
| 221 | /** |
| 222 | * Filter Facebook Likebox default options. |
| 223 | * |
| 224 | * @module widgets |
| 225 | * |
| 226 | * @since 1.3.1 |
| 227 | * |
| 228 | * @param array $defaults Array of default options. |
| 229 | */ |
| 230 | return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults ); |
| 231 | } |
| 232 | |
| 233 | function normalize_facebook_args( $args ) { |
| 234 | $args = wp_parse_args( (array) $args, $this->get_default_args() ); |
| 235 | |
| 236 | // Validate the Facebook Page URL |
| 237 | if ( $this->is_valid_facebook_url( $args['href'] ) ) { |
| 238 | $temp = explode( '?', $args['href'] ); |
| 239 | $args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] ); |
| 240 | } else { |
| 241 | $args['href'] = ''; |
| 242 | } |
| 243 | |
| 244 | $args['width'] = $this->normalize_int_value( (int) $args['width'], $this->default_width, $this->max_width, $this->min_width ); |
| 245 | $args['height'] = $this->normalize_int_value( (int) $args['height'], $this->default_height, $this->max_height, $this->min_height ); |
| 246 | $args['show_faces'] = (bool) $args['show_faces']; |
| 247 | $args['stream'] = (bool) $args['stream']; |
| 248 | $args['cover'] = (bool) $args['cover']; |
| 249 | |
| 250 | // The height used to be dependent on other widget settings |
| 251 | // If the user changes those settings but doesn't customize the height, |
| 252 | // let's intelligently assign a new height. |
| 253 | if ( in_array( $args['height'], array( 580, 110, 432 ) ) ) { |
| 254 | if ( $args['show_faces'] && $args['stream'] ) { |
| 255 | $args['height'] = 580; |
| 256 | } else if ( ! $args['show_faces'] && ! $args['stream'] ) { |
| 257 | $args['height'] = 130; |
| 258 | } else { |
| 259 | $args['height'] = 432; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return $args; |
| 264 | } |
| 265 | |
| 266 | function is_valid_facebook_url( $url ) { |
| 267 | return ( false !== strpos( $url, 'facebook.com' ) ) ? true : false; |
| 268 | } |
| 269 | |
| 270 | function normalize_int_value( $value, $default = 0, $max = 0, $min = 0 ) { |
| 271 | $value = (int) $value; |
| 272 | |
| 273 | if ( $value > $max ) { |
| 274 | $value = $max; |
| 275 | } else if ( $value < $min ) { |
| 276 | $value = $min; |
| 277 | } |
| 278 | |
| 279 | return (int) $value; |
| 280 | } |
| 281 | |
| 282 | function normalize_text_value( $value, $default = '', $allowed = array() ) { |
| 283 | $allowed = (array) $allowed; |
| 284 | |
| 285 | if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) |
| 286 | $value = $default; |
| 287 | |
| 288 | return $value; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * @deprecated |
| 293 | */ |
| 294 | function guess_locale_from_lang( $lang ) { |
| 295 | _deprecated_function( __METHOD__, '4.0.0', 'Jetpack::guess_locale_from_lang()' ); |
| 296 | Jetpack::$instance->guess_locale_from_lang( $lang ); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * @deprecated |
| 301 | */ |
| 302 | function get_locale() { |
| 303 | _deprecated_function( __METHOD__, '4.0.0', 'Jetpack::get_locale()' ); |
| 304 | Jetpack::$instance->get_locale(); |
| 305 | } |
| 306 | } |
| 307 |