Completed
Push — instant-search-master ( a786a9...893b7c )
by
unknown
85:32 queued 79:17
created

WPCOM_Widget_Facebook_LikeBox::widget()   C

Complexity

Conditions 10
Paths 34

Size

Total Lines 70

Duplication

Lines 9
Ratio 12.86 %

Importance

Changes 0
Metric Value
cc 10
nc 34
nop 2
dl 9
loc 70
rs 6.7878
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 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
		extract( $args );
62
63
		$like_args = $this->normalize_facebook_args( $instance['like_args'] );
64
65 View Code Duplication
		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'] ? 'timeline' : 'false';
81
		$like_args['cover']        = (bool) $like_args['cover'] ? 'false' : 'true';
82
		$like_args['small_header'] = (bool) $like_args['small_header'] ? 'true' : 'false';
83
84
		/**
85
		 * Filter Facebook Likebox's widget call to action button
86
		 *
87
		 * @module widgets
88
		 *
89
		 * @since 8.4.0
90
		 *
91
		 * @param bool True value hides the call to action button
92
		 */
93
		$hide_cta = apply_filters( 'jetpack_facebook_likebox_hide_cta', false );
94
95
		echo $before_widget;
96
97
		if ( ! empty( $title ) ) :
98
			echo $before_title;
99
100
			$likebox_widget_title = '<a href="' . esc_url( $page_url ) . '">' . esc_html( $title ) . '</a>';
101
102
			/**
103
			 * Filter Facebook Likebox's widget title.
104
			 *
105
			 * @module widgets
106
			 *
107
			 * @since 3.3.0
108
			 *
109
			 * @param string $likebox_widget_title Likebox Widget title (including a link to the Page URL).
110
			 * @param string $title Widget title as set in the widget settings.
111
			 * @param string $page_url Facebook Page URL.
112
			 */
113
			echo apply_filters( 'jetpack_facebook_likebox_title', $likebox_widget_title, $title, $page_url );
114
115
			echo $after_title;
116
		endif;
117
118
		?>
119
		<div id="fb-root"></div>
120
		<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-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'] ); ?>">
121
		<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>
122
		</div>
123
		<?php
124
		wp_enqueue_script( 'jetpack-facebook-embed' );
125
		echo $after_widget;
126
127
		/** This action is documented in modules/widgets/gravatar-profile.php */
128
		do_action( 'jetpack_stats_extra', 'widget_view', 'facebook-likebox' );
129
	}
130
131
	function update( $new_instance, $old_instance ) {
132
		$instance = array(
133
			'title'     => '',
134
			'like_args' => $this->get_default_args(),
135
		);
136
137
		$instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) );
138
139
		// Set up widget values
140
		$instance['like_args'] = array(
141
			'href'         => trim( strip_tags( stripslashes( $new_instance['href'] ) ) ),
142
			'width'        => (int) $new_instance['width'],
143
			'height'       => (int) $new_instance['height'],
144
			'show_faces'   => isset( $new_instance['show_faces'] ),
145
			'stream'       => isset( $new_instance['stream'] ),
146
			'cover'        => isset( $new_instance['cover'] ),
147
			'small_header' => isset( $new_instance['small_header'] ),
148
		);
149
150
		$instance['like_args'] = $this->normalize_facebook_args( $instance['like_args'] );
151
152
		return $instance;
153
	}
154
155
	function form( $instance ) {
156
		$instance  = wp_parse_args(
157
			(array) $instance, array(
158
				'title'     => '',
159
				'like_args' => $this->get_default_args(),
160
			)
161
		);
162
		$like_args = $this->normalize_facebook_args( $instance['like_args'] );
163
		?>
164
165
		<p>
166
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
167
				<?php esc_html_e( 'Title', 'jetpack' ); ?>
168
				<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" />
169
			</label>
170
		</p>
171
172
		<p>
173
			<label for="<?php echo esc_attr( $this->get_field_id( 'href' ) ); ?>">
174
				<?php esc_html_e( 'Facebook Page URL', 'jetpack' ); ?>
175
				<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" />
176
				<br />
177
				<small><?php esc_html_e( 'The widget only works with Facebook Pages.', 'jetpack' ); ?></small>
178
			</label>
179
		</p>
180
181
		<p>
182
			<label for="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>">
183
				<?php esc_html_e( 'Width in pixels', 'jetpack' ); ?>
184
				<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;" />
185
				<small><?php echo sprintf( __( 'Minimum: %s', 'jetpack' ), $this->min_width ); ?> / <?php echo sprintf( __( 'Maximum: %s', 'jetpack' ), $this->max_width ); ?></small>
186
			</label>
187
		</p>
188
189
		<p>
190
			<label for="<?php echo esc_attr( $this->get_field_id( 'height' ) ); ?>">
191
				<?php esc_html_e( 'Height in pixels', 'jetpack' ); ?>
192
				<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;" />
193
				<small><?php echo sprintf( __( 'Minimum: %s', 'jetpack' ), $this->min_height ); ?> / <?php echo sprintf( __( 'Maximum: %s', 'jetpack' ), $this->max_height ); ?></small>
194
			</label>
195
		</p>
196
197
		<p>
198
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_faces' ) ); ?>">
199
				<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'] ); ?> />
200
				<?php esc_html_e( 'Show Faces', 'jetpack' ); ?>
201
				<br />
202
				<small><?php esc_html_e( 'Show profile photos in the plugin.', 'jetpack' ); ?></small>
203
			</label>
204
		</p>
205
206
		<p>
207
			<label for="<?php echo esc_attr( $this->get_field_id( 'stream' ) ); ?>">
208
				<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'] ); ?> />
209
				<?php esc_html_e( 'Show Timeline', 'jetpack' ); ?>
210
				<br />
211
				<small><?php esc_html_e( 'Show Page Posts.', 'jetpack' ); ?></small>
212
			</label>
213
		</p>
214
215
		<p>
216
			<label for="<?php echo esc_attr( $this->get_field_id( 'cover' ) ); ?>">
217
				<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'] ); ?> />
218
				<?php esc_html_e( 'Show Cover Photo', 'jetpack' ); ?>
219
				<br />
220
			</label>
221
		</p>
222
223
		<p>
224
			<label for="<?php echo esc_attr( $this->get_field_id( 'small_header' ) ); ?>">
225
				<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'] ); ?> />
226
				<?php esc_html_e( 'Use Small Header', 'jetpack' ); ?>
227
				<br />
228
			</label>
229
		</p>
230
231
		<?php
232
	}
233
234
	function get_default_args() {
235
		$defaults = array(
236
			'href'         => '',
237
			'width'        => $this->default_width,
238
			'height'       => $this->default_height,
239
			'show_faces'   => 'true',
240
			'stream'       => '',
241
			'cover'        => 'true',
242
			'small_header' => '',
243
		);
244
245
		/**
246
		 * Filter Facebook Likebox default options.
247
		 *
248
		 * @module widgets
249
		 *
250
		 * @since 1.3.1
251
		 *
252
		 * @param array $defaults Array of default options.
253
		 */
254
		return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults );
255
	}
256
257
	function normalize_facebook_args( $args ) {
258
		$args = wp_parse_args( (array) $args, $this->get_default_args() );
259
260
		// Validate the Facebook Page URL
261
		if ( $this->is_valid_facebook_url( $args['href'] ) ) {
262
			$temp         = explode( '?', $args['href'] );
263
			$args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] );
264
		} else {
265
			$args['href'] = '';
266
		}
267
268
		$args['width']        = $this->normalize_int_value( (int) $args['width'], $this->default_width, $this->max_width, $this->min_width );
269
		$args['height']       = $this->normalize_int_value( (int) $args['height'], $this->default_height, $this->max_height, $this->min_height );
270
		$args['show_faces']   = (bool) $args['show_faces'];
271
		$args['stream']       = (bool) $args['stream'];
272
		$args['cover']        = (bool) $args['cover'];
273
		$args['small_header'] = (bool) $args['small_header'];
274
275
		// The height used to be dependent on other widget settings
276
		// If the user changes those settings but doesn't customize the height,
277
		// let's intelligently assign a new height.
278
		if ( in_array( $args['height'], array( 580, 110, 432 ) ) ) {
279
			if ( $args['show_faces'] && $args['stream'] ) {
280
				$args['height'] = 580;
281
			} elseif ( ! $args['show_faces'] && ! $args['stream'] ) {
282
				$args['height'] = 130;
283
			} else {
284
				$args['height'] = 432;
285
			}
286
		}
287
288
		return $args;
289
	}
290
291
	function is_valid_facebook_url( $url ) {
292
		return ( false !== strpos( $url, 'facebook.com' ) ) ? true : false;
293
	}
294
295
	function normalize_int_value( $value, $default = 0, $max = 0, $min = 0 ) {
296
		$value = (int) $value;
297
298
		if ( $value > $max ) {
299
			$value = $max;
300
		} elseif ( $value < $min ) {
301
			$value = $min;
302
		}
303
304
		return (int) $value;
305
	}
306
307 View Code Duplication
	function normalize_text_value( $value, $default = '', $allowed = array() ) {
308
		$allowed = (array) $allowed;
309
310
		if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) {
311
			$value = $default;
312
		}
313
314
		return $value;
315
	}
316
317
	/**
318
	 * @deprecated
319
	 */
320
	function guess_locale_from_lang( $lang ) {
321
		_deprecated_function( __METHOD__, '4.0.0', 'Jetpack::guess_locale_from_lang()' );
322
		Jetpack::$instance->guess_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...
323
	}
324
325
	/**
326
	 * @deprecated
327
	 */
328
	function get_locale() {
329
		_deprecated_function( __METHOD__, '4.0.0', 'Jetpack::get_locale()' );
330
		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...
331
	}
332
}
333