This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 235
/
class-facebook-like-button.php
387 lines (335 loc) · 8.83 KB
/
class-facebook-like-button.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
if ( ! class_exists( 'Facebook_Social_Plugin' ) )
require_once( dirname(__FILE__) . '/class-facebook-social-plugin.php' );
/**
* Allow page visitors to easily share content on Facebook with a Like button.
*
* @since 1.1
*
* @link https://developers.facebook.com/docs/plugins/like-button/ Like button social plugin documentation
*/
class Facebook_Like_Button extends Facebook_Social_Plugin {
/**
* Element and class name used in markup builders.
*
* @since 1.1
*
* @var string
*/
const ID = 'like';
/**
* Override the URL used for the like action.
*
* Default is og:url or link[rel=canonical] or document.URL
*
* @since 1.1
*
* @var string
*/
protected $href;
/**
* Display a share button alongside the like button?
*
* @since 1.5.4
*
* @var bool
*/
protected $share;
/**
* Which like button you would like displayed.
*
* @since 1.1
*
* @var string
*/
protected $layout;
/**
* Choose your like button
*
* @since 1.1
*
* @var array
*/
public static $layout_choices = array( 'standard' => true, 'button_count' => true, 'box_count' => true );
/**
* Show faces of the viewer's friends?
*
* Only applies to standard layout. Needs the extra width.
*
* @since 1.1
*
* @var bool
*/
protected $show_faces;
/**
* Define a custom width in whole pixels
*
* @since 1.1
*
* @var int
*/
protected $width;
/**
* Like or recommend
*
* @since 1.1
*
* @var string
*/
protected $action;
/**
* The verb to display on the button
*
* @since 1.1
*
* @var array
*/
public static $action_choices = array( 'like' => true, 'recommend' => true );
/**
* Option to bypass validation.
*
* You might validate when changing settings but choose not to validate on future generators
*
* @since 1.1
*
* @param bool $validate false if object should not be validated
*/
public function __construct( $validate = true ) {
if ( $validate === false )
$this->validate = false;
else
$this->validate = true;
}
/**
* I am a like button
*
* @since 1.1
*
* @return string Facebook social plugin name
*/
public function __toString() {
return 'Facebook Like Button';
}
/**
* Setter for href attribute.
*
* @since 1.1
*
* @param string $url absolute URL
* @return Facebook_Like_Button support chaining
*/
public function setURL( $url ) {
$url = esc_url_raw( $url, array( 'http', 'https' ) );
if ( $url )
$this->href = $url;
return $this;
}
/**
* Should a share button appear next to the like button?
*
* @since 1.5.4
*
* @return Facebook_Like_Button support chaining
*/
public function includeShareButton() {
$this->share = true;
return $this;
}
/**
* Choose a layout option.
*
* @since 1.1
*
* @see self::$layout_choices
* @param string $layout a supported layout option
* @return Facebook_Like_Button support chaining
*/
public function setLayout( $layout ) {
if ( is_string( $layout ) && isset( self::$layout_choices[$layout] ) )
$this->layout = $layout;
return $this;
}
/**
* Show the faces of a logged-on Facebook user's friends
*
* @since 1.1
*
* @return Facebook_Like_Button support chaining
*/
public function showFaces() {
$this->show_faces = true;
return $this;
}
/**
* Do not display the faces of a logged-on Facebook user's friends
*
* Reverts to default state
*
* @since 1.1.11
*
* @return Facebook_Like_Button support chaining
*/
public function hideFaces() {
$this->show_faces = false;
return $this;
}
/**
* Width of the like button
*
* Should be greater than the minimum width of layout + share button (if enabled) + recommend text (if chosen).
*
* @since 1.1
*
* @param int $width width in whole pixels
* @return Facebook_Like_Button support chaining
*/
public function setWidth( $width ) {
// narrowest like button is box_count at 55
if ( is_int( $width ) && $width > 55 )
$this->width = $width;
return $this;
}
/**
* Override the default "like" text with "recommend"
*
* @since 1.1
*
* @param string $action like|recommend
* @return Facebook_Like_Button support chaining
*/
public function setAction( $action ) {
if ( is_string( $action ) && isset( self::$action_choices[$action] ) )
$this->action = $action;
return $this;
}
/**
* Compute a minimum width of a like button based on configured options.
*
* Note: Minimum widths vary based on the language-specific text used for "like" and "recommend" action. Language variances are not factored into this calculation.
*
* @since 1.1
*
* @return int minimum width of the current configuration in whole pixels
*/
private function compute_minimum_width() {
$min_width = 225; // standard
if ( isset( $this->layout ) ) {
if ( $this->layout === 'button_count' )
$min_width = 90;
else if ( $this->layout === 'box_count' )
$min_width = 55;
}
if ( $this->action === 'recommend' )
$min_width += 40;
if ( $this->share === true )
$min_width += 60;
return $min_width;
}
/**
* Some options may be in conflict with other options or not available for main choices.
*
* Reset customizations if we can detect non-compliance to avoid later confusion and/or layout issues.
*
* @since 1.1
* @return void
*/
public function validate() {
// allow overrides
if ( isset( $this->validate ) && $this->validate === false )
return;
// show faces supported in standard layout only
if ( isset( $this->show_faces ) && $this->show_faces === true && $this->layout !== 'standard' )
unset( $this->show_faces );
// is the specified width less than minimum for config?
if ( isset( $this->width ) ) {
$min_width = $this->compute_minimum_width();
if ( $this->width < $min_width )
$this->width = $min_width;
unset( $min_width );
}
$this->validate = false;
}
/**
* convert an options array into an object.
*
* @since 1.1
*
* @param array $values associative array
* @return Facebook_Like_Button like object
*/
public static function fromArray( $values ) {
if ( ! is_array( $values ) || empty( $values ) )
return;
$like_button = new Facebook_Like_Button();
if ( isset( $values['href'] ) && is_string( $values['href'] ) )
$like_button->setURL( $values['href'] );
if ( isset( $values['layout'] ) && is_string( $values['layout'] ) )
$like_button->setLayout( $values['layout'] );
if ( isset( $values['show_faces'] ) && ( $values['show_faces'] === true || $values['show_faces'] === 'true' || $values['show_faces'] == 1 ) )
$like_button->showFaces();
if ( isset( $values['share'] ) && ( $values['share'] === true || $values['share'] === 'true' || $values['share'] == 1 ) )
$like_button->includeShareButton();
if ( isset( $values['width'] ) )
$like_button->setWidth( absint( $values['width'] ) );
if ( isset( $values['action'] ) )
$like_button->setAction( $values['action'] );
if ( isset( $values['font'] ) )
$like_button->setFont( $values['font'] );
if ( isset( $values['colorscheme'] ) )
$like_button->setColorScheme( $values['colorscheme'] );
if ( isset( $values['ref'] ) )
$like_button->setReference( $values['ref'] );
if ( isset( $values['kid_directed_site'] ) && ( $values['kid_directed_site'] === true || $values['kid_directed_site'] === 'true' || $values['kid_directed_site'] == 1 ) )
$like_button->isKidDirectedSite();
return $like_button;
}
/**
* Convert the class to data-* attribute friendly associative array
*
* will become data-key="value". Exclude values if default.
*
* @since 1.1
*
* @return array associative array
*/
public function toHTMLDataArray() {
$data = parent::toHTMLDataArray();
if ( isset( $this->href ) )
$data['href'] = $this->href;
if ( isset( $this->layout ) && $this->layout !== 'standard' )
$data['layout'] = $this->layout;
if ( isset( $this->share ) && $this->share === true )
$data['share'] = 'true';
if ( isset( $this->show_faces ) && $this->show_faces === true )
$data['show-faces'] = 'true';
if ( isset( $this->width ) && is_int( $this->width ) && $this->width > 0 )
$data['width'] = strval( $this->width );
if ( isset( $this->action ) && $this->action !== 'like' )
$data['action'] = $this->action;
return $data;
}
/**
* Output Like button with data-* attributes
*
* @since 1.1
*
* @param array $div_attributes associative array. customize the returned div with id, class, or style attributes
* @return HTML div or empty string
*/
public function asHTML( $div_attributes=array() ) {
$div_attributes = self::add_required_class( 'fb-' . self::ID, $div_attributes );
$div_attributes['data'] = $this->toHTMLDataArray();
return self::div_builder( $div_attributes );
}
/**
* Output Like button as XFBML
*
* @since 1.1
*
* @return string XFBML markup
*/
public function asXFBML() {
return self::xfbml_builder( self::ID, $this->toHTMLDataArray() );
}
}
?>