Completed
Push — update/single-product-cards ( 24aeb3...5ff579 )
by
unknown
73:22 queued 66:41
created

WPCOM_Widget_Facebook_LikeBox::get_default_args()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 21
rs 9.584
c 0
b 0
f 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 (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
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-tabs="<?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(
144
			(array) $instance, array(
145
				'title'     => '',
146
				'like_args' => $this->get_default_args(),
147
			)
148
		);
149
		$like_args = $this->normalize_facebook_args( $instance['like_args'] );
150
		?>
151
152
		<p>
153
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
154
				<?php _e( 'Title', 'jetpack' ); ?>
155
				<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" />
156
			</label>
157
		</p>
158
159
		<p>
160
			<label for="<?php echo esc_attr( $this->get_field_id( 'href' ) ); ?>">
161
				<?php _e( 'Facebook Page URL', 'jetpack' ); ?>
162
				<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" />
163
				<br />
164
				<small><?php _e( 'The widget only works with Facebook Pages.', 'jetpack' ); ?></small>
165
			</label>
166
		</p>
167
168
		<p>
169
			<label for="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>">
170
				<?php _e( 'Width in pixels', 'jetpack' ); ?>
171
				<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;" />
172
				<small><?php echo sprintf( __( 'Minimum: %s', 'jetpack' ), $this->min_width ); ?> / <?php echo sprintf( __( 'Maximum: %s', 'jetpack' ), $this->max_width ); ?></small>
173
			</label>
174
		</p>
175
176
		<p>
177
			<label for="<?php echo esc_attr( $this->get_field_id( 'height' ) ); ?>">
178
				<?php _e( 'Height in pixels', 'jetpack' ); ?>
179
				<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;" />
180
				<small><?php echo sprintf( __( 'Minimum: %s', 'jetpack' ), $this->min_height ); ?> / <?php echo sprintf( __( 'Maximum: %s', 'jetpack' ), $this->max_height ); ?></small>
181
			</label>
182
		</p>
183
184
		<p>
185
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_faces' ) ); ?>">
186
				<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'] ); ?> />
187
				<?php _e( 'Show Faces', 'jetpack' ); ?>
188
				<br />
189
				<small><?php _e( 'Show profile photos in the plugin.', 'jetpack' ); ?></small>
190
			</label>
191
		</p>
192
193
		<p>
194
			<label for="<?php echo esc_attr( $this->get_field_id( 'stream' ) ); ?>">
195
				<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'] ); ?> />
196
				<?php _e( 'Show Timeline', 'jetpack' ); ?>
197
				<br />
198
				<small><?php _e( 'Show Page Posts.', 'jetpack' ); ?></small>
199
			</label>
200
		</p>
201
202
		<p>
203
			<label for="<?php echo esc_attr( $this->get_field_id( 'cover' ) ); ?>">
204
				<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'] ); ?> />
205
				<?php _e( 'Show Cover Photo', 'jetpack' ); ?>
206
				<br />
207
			</label>
208
		</p>
209
210
		<?php
211
	}
212
213
	function get_default_args() {
214
		$defaults = array(
215
			'href'       => '',
216
			'width'      => $this->default_width,
217
			'height'     => $this->default_height,
218
			'show_faces' => 'true',
219
			'stream'     => '',
220
			'cover'      => 'true',
221
		);
222
223
		/**
224
		 * Filter Facebook Likebox default options.
225
		 *
226
		 * @module widgets
227
		 *
228
		 * @since 1.3.1
229
		 *
230
		 * @param array $defaults Array of default options.
231
		 */
232
		return apply_filters( 'jetpack_facebook_likebox_defaults', $defaults );
233
	}
234
235
	function normalize_facebook_args( $args ) {
236
		$args = wp_parse_args( (array) $args, $this->get_default_args() );
237
238
		// Validate the Facebook Page URL
239
		if ( $this->is_valid_facebook_url( $args['href'] ) ) {
240
			$temp         = explode( '?', $args['href'] );
241
			$args['href'] = str_replace( array( 'http://facebook.com', 'https://facebook.com' ), array( 'http://www.facebook.com', 'https://www.facebook.com' ), $temp[0] );
242
		} else {
243
			$args['href'] = '';
244
		}
245
246
		$args['width']      = $this->normalize_int_value( (int) $args['width'], $this->default_width, $this->max_width, $this->min_width );
247
		$args['height']     = $this->normalize_int_value( (int) $args['height'], $this->default_height, $this->max_height, $this->min_height );
248
		$args['show_faces'] = (bool) $args['show_faces'];
249
		$args['stream']     = (bool) $args['stream'];
250
		$args['cover']      = (bool) $args['cover'];
251
252
		// The height used to be dependent on other widget settings
253
		// If the user changes those settings but doesn't customize the height,
254
		// let's intelligently assign a new height.
255
		if ( in_array( $args['height'], array( 580, 110, 432 ) ) ) {
256
			if ( $args['show_faces'] && $args['stream'] ) {
257
				$args['height'] = 580;
258
			} elseif ( ! $args['show_faces'] && ! $args['stream'] ) {
259
				$args['height'] = 130;
260
			} else {
261
				$args['height'] = 432;
262
			}
263
		}
264
265
		return $args;
266
	}
267
268
	function is_valid_facebook_url( $url ) {
269
		return ( false !== strpos( $url, 'facebook.com' ) ) ? true : false;
270
	}
271
272
	function normalize_int_value( $value, $default = 0, $max = 0, $min = 0 ) {
273
		$value = (int) $value;
274
275
		if ( $value > $max ) {
276
			$value = $max;
277
		} elseif ( $value < $min ) {
278
			$value = $min;
279
		}
280
281
		return (int) $value;
282
	}
283
284 View Code Duplication
	function normalize_text_value( $value, $default = '', $allowed = array() ) {
285
		$allowed = (array) $allowed;
286
287
		if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) {
288
			$value = $default;
289
		}
290
291
		return $value;
292
	}
293
294
	/**
295
	 * @deprecated
296
	 */
297
	function guess_locale_from_lang( $lang ) {
298
		_deprecated_function( __METHOD__, '4.0.0', 'Jetpack::guess_locale_from_lang()' );
299
		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...
300
	}
301
302
	/**
303
	 * @deprecated
304
	 */
305
	function get_locale() {
306
		_deprecated_function( __METHOD__, '4.0.0', 'Jetpack::get_locale()' );
307
		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...
308
	}
309
}
310