Completed
Push — fix/shortcode-youtube-obsolete... ( ca84d5...80091a )
by
unknown
33:45 queued 23:26
created

WPCOM_Widget_Facebook_LikeBox::__construct()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 24
Code Lines 10

Duplication

Lines 24
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 24
loc 24
rs 8.6845
cc 4
eloc 10
nc 2
nop 0
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 (formely 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 View Code Duplication
	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
62
		extract( $args );
63
64
		$like_args = $this->normalize_facebook_args( $instance['like_args'] );
65
66
		if ( empty( $like_args['href'] ) || ! $this->is_valid_facebook_url( $like_args['href'] ) ) {
67
			if ( current_user_can('edit_theme_options') ) {
68
				echo $before_widget;
69
				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>';
70
				echo $after_widget;
71
			}
72
			echo '<!-- Invalid Facebook Page URL -->';
73
			return;
74
		}
75
76
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
77
		$title    = apply_filters( 'widget_title', $instance['title'] );
78
		$page_url = set_url_scheme( $like_args['href'], 'https' );
79
80
		$like_args['show_faces'] = (bool) $like_args['show_faces'] ? 'true'  : 'false';
81
		$like_args['stream']     = (bool) $like_args['stream']     ? 'true'  : 'false';
82
		$like_args['cover']      = (bool) $like_args['cover']      ? 'false' : 'true';
83
84
		echo $before_widget;
85
86
		if ( ! empty( $title ) ) :
87
			echo $before_title;
88
89
			$likebox_widget_title = '<a href="' . esc_url( $page_url ) . '">' . esc_html( $title ) . '</a>';
90
91
			/**
92
			 * Filter Facebook Likebox's widget title.
93
			 *
94
			 * @module widgets
95
			 *
96
			 * @since 3.3.0
97
			 *
98
			 * @param string $likebox_widget_title Likebox Widget title (including a link to the Page URL).
99
			 * @param string $title Widget title as set in the widget settings.
100
			 * @param string $page_url Facebook Page URL.
101
			 */
102
			echo apply_filters( 'jetpack_facebook_likebox_title', $likebox_widget_title, $title, $page_url );
103
104
			echo $after_title;
105
		endif;
106
107
		?>
108
		<div id="fb-root"></div>
109
		<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'] ); ?>">
110
		<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>
111
		</div>
112
		<?php
113
		wp_enqueue_script( 'jetpack-facebook-embed' );
114
		echo $after_widget;
115
116
		/** This action is already documented in modules/widgets/gravatar-profile.php */
117
		do_action( 'jetpack_stats_extra', 'widget', 'facebook-likebox' );
118
	}
119
120
	function update( $new_instance, $old_instance ) {
121
		$instance = array(
122
			'title' => '',
123
			'like_args' => $this->get_default_args(),
124
		);
125
126
		$instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) );
127
128
		// Set up widget values
129
		$instance['like_args'] = array(
130
			'href'        => trim( strip_tags( stripslashes( $new_instance['href'] ) ) ),
131
			'width'       => (int) $new_instance['width'],
132
			'height'      => (int) $new_instance['height'],
133
			'show_faces'  => isset( $new_instance['show_faces'] ),
134
			'stream'      => isset( $new_instance['stream'] ),
135
			'cover'       => isset( $new_instance['cover'] ),
136
		);
137
138
		$instance['like_args'] = $this->normalize_facebook_args( $instance['like_args'] );
139
140
		return $instance;
141
	}
142
143
	function form( $instance ) {
144
		$instance = wp_parse_args( (array) $instance, array(
145
			'title'     => '',
146
			'like_args' => $this->get_default_args()
147
		) );
148
		$like_args = $this->normalize_facebook_args( $instance['like_args'] );
149
		?>
150
151
		<p>
152
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">
153
				<?php _e( 'Title', 'jetpack' ); ?>
154
				<input type="text" name="<?php echo $this->get_field_name( 'title' ); ?>" id="<?php echo $this->get_field_id( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" />
155
			</label>
156
		</p>
157
158
		<p>
159
			<label for="<?php echo $this->get_field_id( 'href' ); ?>">
160
				<?php _e( 'Facebook Page URL', 'jetpack' ); ?>
161
				<input type="text" name="<?php echo $this->get_field_name( 'href' ); ?>" id="<?php echo $this->get_field_id( 'href' ); ?>" value="<?php echo esc_url( $like_args['href'] ); ?>" class="widefat" />
162
				<br />
163
				<small><?php _e( 'The widget only works with Facebook Pages.', 'jetpack' ); ?></small>
164
			</label>
165
		</p>
166
167
		<p>
168
			<label for="<?php echo $this->get_field_id( 'width' ); ?>">
169
				<?php _e( 'Width', 'jetpack' ); ?>
170
				<input type="number" class="smalltext" min="1" max="999" maxlength="3" name="<?php echo $this->get_field_name( 'width' ); ?>" id="<?php echo $this->get_field_id( 'width' ); ?>" value="<?php echo esc_attr( $like_args['width'] ); ?>" style="text-align: center;" />px
171
			</label>
172
		</p>
173
174
		<p>
175
			<label for="<?php echo $this->get_field_id( 'height' ); ?>">
176
				<?php _e( 'Height', 'jetpack' ); ?>
177
				<input type="number" class="smalltext" min="1" max="999" maxlength="3" name="<?php echo $this->get_field_name( 'height' ); ?>" id="<?php echo $this->get_field_id( 'height' ); ?>" value="<?php echo esc_attr( $like_args['height'] ); ?>" style="text-align: center;" />px
178
			</label>
179
		</p>
180
181
		<p>
182
			<label for="<?php echo $this->get_field_id( 'show_faces' ); ?>">
183
				<input type="checkbox" name="<?php echo $this->get_field_name( 'show_faces' ); ?>" id="<?php echo $this->get_field_id( 'show_faces' ); ?>" <?php checked( $like_args['show_faces'] ); ?> />
184
				<?php _e( 'Show Faces', 'jetpack' ); ?>
185
				<br />
186
				<small><?php _e( 'Show profile photos in the plugin.', 'jetpack' ); ?></small>
187
			</label>
188
		</p>
189
190
		<p>
191
			<label for="<?php echo $this->get_field_id( 'stream' ); ?>">
192
				<input type="checkbox" name="<?php echo $this->get_field_name( 'stream' ); ?>" id="<?php echo $this->get_field_id( 'stream' ); ?>" <?php checked( $like_args['stream'] ); ?> />
193
				<?php _e( 'Show Stream', 'jetpack' ); ?>
194
				<br />
195
				<small><?php _e( 'Show Page Posts.', 'jetpack' ); ?></small>
196
			</label>
197
		</p>
198
199
		<p>
200
			<label for="<?php echo $this->get_field_id( 'cover' ); ?>">
201
				<input type="checkbox" name="<?php echo $this->get_field_name( 'cover' ); ?>" id="<?php echo $this->get_field_id( 'cover' ); ?>" <?php checked( $like_args['cover'] ); ?> />
202
				<?php _e( 'Show Cover Photo', 'jetpack' ); ?>
203
				<br />
204
			</label>
205
		</p>
206
207
		<?php
208
	}
209
210
	function get_default_args() {
211
		$defaults = array(
212
			'href'        => '',
213
			'width'       => $this->default_width,
214
			'height'      => $this->default_height,
215
			'show_faces'  => 'true',
216
			'stream'      => '',
217
			'cover'       => 'true',
218
		);
219
220
		/**
221
		 * Filter Facebook Likebox default options.
222
		 *
223
		 * @module widgets
224
		 *
225
		 * @since 1.3.1
226
		 *
227
		 * @param array $defaults Array of default options.
228
		 */
229
		return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults );
230
	}
231
232
	function normalize_facebook_args( $args ) {
233
		$args = wp_parse_args( (array) $args, $this->get_default_args() );
234
235
		// Validate the Facebook Page URL
236 View Code Duplication
		if ( $this->is_valid_facebook_url( $args['href'] ) ) {
237
			$temp = explode( '?', $args['href'] );
238
			$args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] );
239
		} else {
240
			$args['href'] = '';
241
		}
242
243
		$args['width']      = $this->normalize_int_value(  (int) $args['width'], $this->default_width,   $this->max_width,  $this->min_width );
244
		$args['height']     = $this->normalize_int_value(  (int) $args['height'], $this->default_height, $this->max_height, $this->min_height );
245
		$args['show_faces'] = (bool) $args['show_faces'];
246
		$args['stream']     = (bool) $args['stream'];
247
		$args['cover']      = (bool) $args['cover'];
248
249
		// The height used to be dependent on other widget settings
250
		// If the user changes those settings but doesn't customize the height,
251
		// let's intelligently assign a new height.
252
		if ( in_array( $args['height'], array( 580, 110, 432 ) ) ) {
253
			if ( $args['show_faces'] && $args['stream'] ) {
254
				$args['height'] = 580;
255
			} else if ( ! $args['show_faces'] && ! $args['stream'] ) {
256
				$args['height'] = 130;
257
			} else {
258
				$args['height'] = 432;
259
			}
260
		}
261
262
		return $args;
263
	}
264
265
	function is_valid_facebook_url( $url ) {
266
		return ( FALSE !== strpos( $url, 'facebook.com' ) ) ? TRUE : FALSE;
267
	}
268
269
	function normalize_int_value( $value, $default = 0, $max = 0, $min = 0 ) {
270
		$value = (int) $value;
271
272
		if ( $max < $value || $min > $value )
273
			$value = $default;
274
275
		return (int) $value;
276
	}
277
278 View Code Duplication
	function normalize_text_value( $value, $default = '', $allowed = array() ) {
279
		$allowed = (array) $allowed;
280
281
		if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) )
282
			$value = $default;
283
284
		return $value;
285
	}
286
287
	/**
288
	 * @deprecated
289
	 */
290
	function guess_locale_from_lang( $lang ) {
291
		_deprecated_function( __METHOD__, '3.10', 'Jetpack::guess_locale_from_lang()' );
292
		Jetpack::$instance->get_locale_from_lang( $lang );
0 ignored issues
show
Bug introduced by
The property instance cannot be accessed from this context as it is declared private in class Jetpack.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Bug introduced by
The method get_locale_from_lang() does not exist on Jetpack. Did you maybe mean get_locale()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
293
	}
294
295
	/**
296
	 * @deprecated
297
	 */
298
	function get_locale() {
299
		_deprecated_function( __METHOD__, '3.10', 'Jetpack::get_locale()' );
300
		Jetpack::$instance->get_locale();
0 ignored issues
show
Bug introduced by
The property instance cannot be accessed from this context as it is declared private in class Jetpack.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
301
	}
302
}
303
304
// END
305