forked from facebookarchive/wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook.php
501 lines (430 loc) · 17.9 KB
/
facebook.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
/**
* @package Facebook
* @version 1.1.11
*/
/*
Plugin Name: Facebook
Plugin URI: http://wordpress.org/extend/plugins/facebook/
Description: Facebook for WordPress. Make your site deeply social in just a couple of clicks.
Author: Facebook
Author URI: https://developers.facebook.com/wordpress/
Version: 1.1.11
License: GPL2
License URI: license.txt
Domain Path: /languages/
*/
/**
* Load the Facebook plugin
*
* @since 1.1
*/
class Facebook_Loader {
/**
* Uniquely identify plugin version
* Bust caches based on this value
*
* @since 1.1
* @var string
*/
const VERSION = '1.1.11';
/**
* Default Facebook locale
*
* @since 1.1.11
* @var string
*/
const DEFAULT_LOCALE = 'en_US';
/**
* Locale of the site expressed as a Facebook locale
*
* @since 1.1
* @var string
*/
public $locale = 'en_US';
/**
* Store Facebook application information (id, secret, namespace) if available.
*
* @since 1.1
* @var array
*/
public $credentials = array();
/**
* List of locales supported by Facebook.
* Two-letter languages codes stored in WordPress are translated to full locales; if a language has multiple country localizations place the first choice earlier in the array to make it the language default
* @link https://www.facebook.com/translations/FacebookLocales.xml Facebook locales
*
* @since 1.1
*/
public static $locales = array( 'af_ZA' => true, 'ar_AR' => true, 'az_AZ' => true, 'be_BY' => true, 'bg_BG' => true, 'bn_IN' => true, 'bs_BA' => true, 'ca_ES' => true, 'cs_CZ' => true, 'cy_GB' => true, 'da_DK' => true, 'de_DE' => true, 'el_GR' => true, 'en_US' => true, 'en_GB' => true, 'eo_EO' => true, 'es_ES' => true, 'es_LA' => true, 'et_EE' => true, 'eu_ES' => true, 'fa_IR' => true, 'fb_LT' => true, 'fi_FI' => true, 'fo_FO' => true, 'fr_FR' => true, 'fr_CA' => true, 'fy_NL' => true, 'ga_IE' => true, 'gl_ES' => true, 'he_IL' => true, 'hi_IN' => true, 'hr_HR' => true, 'hu_HU' => true, 'hy_AM' => true, 'id_ID' => true, 'is_IS' => true, 'it_IT' => true, 'ja_JP' => true, 'ka_GE' => true, 'km_KH' => true, 'ko_KR' => true, 'ku_TR' => true, 'la_VA' => true, 'lt_LT' => true, 'lv_LV' => true, 'mk_MK' => true, 'ml_IN' => true, 'ms_MY' => true, 'nb_NO' => true, 'ne_NP' => true, 'nl_NL' => true, 'nn_NO' => true, 'pa_IN' => true, 'pl_PL' => true, 'ps_AF' => true, 'pt_PT' => true, 'pt_BR' => true, 'ro_RO' => true, 'ru_RU' => true, 'sk_SK' => true, 'sl_SI' => true, 'sq_AL' => true, 'sr_RS' => true, 'sv_SE' => true, 'sw_KE' => true, 'ta_IN' => true, 'te_IN' => true, 'th_TH' => true, 'tl_PH' => true, 'tr_TR' => true, 'uk_UA' => true, 'vi_VN' => true, 'zh_CN' => true, 'zh_HK' => true, 'zh_TW' => true );
/**
* Let's get it started
*
* @since 1.1
*/
public function __construct() {
// load plugin files relative to this directory
$this->plugin_directory = dirname(__FILE__) . '/';
$this->set_locale();
// Load the textdomain for translations
load_plugin_textdomain( 'facebook', false, $this->plugin_directory . 'languages/' );
$credentials = get_option( 'facebook_application' );
if ( ! is_array( $credentials ) )
$credentials = array();
$this->credentials = $credentials;
unset( $credentials );
add_action( 'widgets_init', array( &$this, 'widgets_init' ) );
// load shortcodes
if ( ! class_exists( 'Facebook_Shortcodes' ) )
require_once( $this->plugin_directory . 'social-plugins/shortcodes.php' );
Facebook_Shortcodes::init();
if ( is_user_logged_in() ) {
// admin bar may show on public-facing site as well as administrative section
add_action( 'add_admin_bar_menus', array( &$this, 'admin_bar' ) );
}
if ( is_admin() ) {
add_action( 'admin_enqueue_scripts', array( &$this, 'register_js_sdk' ), 1 );
$this->admin_init();
} else {
add_action( 'wp_enqueue_scripts', array( &$this, 'register_js_sdk' ), 1 );
add_action( 'wp', array( &$this, 'public_init' ) );
}
}
/**
* Add Facebook functionality to the WordPress admin bar
*
* @since 1.1
* @param WP_Admin_Bar $wp_admin_bar existing WordPress admin bar object
*/
public function admin_bar() {
if ( isset( $this->credentials ) && isset( $this->credentials['app_id'] ) ) {
if ( get_option( 'facebook_comments_enabled' ) ) {
if ( ! class_exists( 'Facebook_Comments' ) )
require_once( $this->plugin_directory . 'social-plugins/class-facebook-comments.php' );
Facebook_Comments::admin_bar_menu();
}
}
}
/**
* Register the Facebook JavaScript SDK for later enqueueing
*
* @since 1.1
* @uses wp_register_script
*/
public function register_js_sdk() {
global $wp_scripts;
$handle = 'facebook-jssdk'; // match the Facebook async snippet ID to avoid double load
wp_register_script( $handle, ( is_ssl() ? 'https' : 'http' ) . '://connect.facebook.net/' . $this->locale . '/' . ( defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? 'all/debug.js' : 'all.js' ), array(), null, true );
// register the script but take it back with an async load
add_filter( 'script_loader_src', array( 'Facebook_Loader', 'async_script_loader_src' ), 1, 2 );
$args = array(
'channelUrl' => plugins_url( 'channel.php', __FILE__ ),
'status' => true,
'cookie' => true,
'xfbml' => true
);
// appId optional
if ( ! empty( $this->credentials['app_id'] ) )
$args['appId'] = $this->credentials['app_id'];
$args = apply_filters( 'facebook_jssdk_init_options', $args );
// allow the publisher to short circuit the init through the filter
if ( ! empty( $args ) && isset( $wp_scripts ) ) {
$wp_scripts->add_data( $handle, 'data', 'var FB_WP=FB_WP||{};FB_WP.queue={_methods:[],flushed:false,add:function(fn){FB_WP.queue.flushed?fn():FB_WP.queue._methods.push(fn)},flush:function(){for(var fn;fn=FB_WP.queue._methods.shift();){fn()}FB_WP.queue.flushed=true}};window.fbAsyncInit=function(){FB.init(' . json_encode( $args ) . ');if(FB_WP && FB_WP.queue && FB_WP.queue.flush){FB_WP.queue.flush()}' . apply_filters( 'facebook_jssdk_init_extras', '', isset( $this->credentials['app_id'] ) ? $this->credentials['app_id'] : '' ) . '}' );
}
}
/**
* Proactively resolve Facebook JavaScript SDK domain name asynchronously before later use
*
* @since 1.1.9
* @link http://dev.chromium.org/developers/design-documents/dns-prefetching Chromium prefetch behavior
* @link https://developer.mozilla.org/en-US/docs/Controlling_DNS_prefetching Firefox prefetch behavior
*/
public static function dns_prefetch_js_sdk(){
echo '<link rel="dns-prefetch" href="//connect.facebook.net" />' . "\n";
}
/**
* Enqueue the JavaScript SDK
*
* @since 1.1
* @uses wp_enqueue_script()
*/
public static function enqueue_js_sdk() {
wp_enqueue_script( 'facebook-jssdk', false, array(), false, true );
}
/**
* Load Facebook JS SDK async
* Called from script_loader_src filter
*
* @since 1.1
* @param string $src script URL
* @param string $handle WordPress registered script handle
* @return string empty string if Facebook JavaScript SDK, else give back the src variable
*/
public static function async_script_loader_src( $src, $handle ) {
global $wp_scripts;
if ( $handle !== 'facebook-jssdk' )
return $src;
// @link https://developers.facebook.com/docs/reference/javascript/#loading
$html = '<div id="fb-root"></div><script type="text/javascript">(function(d){var id="facebook-jssdk";if(!d.getElementById(id)){var js=d.createElement("script"),ref=d.getElementsByTagName("script")[0];js.id=id,js.async=true,js.src=' . json_encode( $src ) . ',ref.parentNode.insertBefore(js,ref)}})(document)</script>' . "\n";
if ( isset( $wp_scripts ) && $wp_scripts->do_concat )
$wp_scripts->print_html .= $html;
else
echo $html;
// empty out the src response
// results in extra DOM but nothing to load
return '';
}
/**
* Styles applied to public-facing pages
*
* @since 1.1
* @uses enqueue_styles()
*/
public static function enqueue_styles() {
wp_enqueue_style( 'facebook', plugins_url( 'static/css/style' . ( defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min' ) . '.css', __FILE__ ), array(), self::VERSION );
}
/**
* Initialize a global $facebook variable if one does not already exist and credentials stored for this site
*
* @since 1.1
* @return true if $facebook global exists, else false
*/
public function load_php_sdk() {
global $facebook;
if ( isset( $facebook ) )
return true;
if ( ! empty( $this->credentials['app_id'] ) && ! empty( $this->credentials['app_secret'] ) ) {
if ( ! class_exists( 'Facebook_WP_Extend' ) )
require_once( $this->plugin_directory . 'includes/facebook-php-sdk/class-facebook-wp.php' );
$facebook = new Facebook_WP_Extend( array(
'appId' => $this->credentials['app_id'],
'secret' => $this->credentials['app_secret']
) );
if ( $facebook )
return true;
}
return false;
}
/**
* Intialize the public, front end views
*
* @since 1.1
*/
public function public_init() {
// no feed filters yet
if ( is_feed() || is_404() )
return;
// always include Open Graph protocol markup
if ( ! class_exists( 'Facebook_Open_Graph_Protocol' ) )
require_once( $this->plugin_directory . 'open-graph-protocol.php' );
add_action( 'wp_head', array( 'Facebook_Open_Graph_Protocol', 'add_og_protocol' ) );
add_action( 'wp_head', array( 'Facebook_Loader', 'dns_prefetch_js_sdk' ), 1, 0 );
add_action( 'wp_enqueue_scripts', array( 'Facebook_Loader', 'enqueue_js_sdk' ) );
self::plugin_extras();
// include comment count filters on all pages
if ( get_option( 'facebook_comments_enabled' ) ) {
if ( ! class_exists( 'Facebook_Comments' ) )
require_once( $this->plugin_directory . 'social-plugins/class-facebook-comments.php' );
add_filter( 'comments_array', array( 'Facebook_Comments', 'comments_array_filter' ), 10, 2 );
add_filter( 'comments_open', array( 'Facebook_Comments', 'comments_open_filter' ), 10, 2 );
// override comment count to a garbage number
add_filter( 'get_comments_number', array( 'Facebook_Comments', 'get_comments_number_filter' ), 10, 2 );
// display comments number if used in template
add_filter( 'comments_number', array( 'Facebook_Comments', 'comments_number_filter' ), 10, 2 );
}
// check for enabled features by page type
$enabled_features = array();
$option_name = 'facebook_%s_features';
if ( is_home() || is_front_page() ) {
$enabled_features = get_option( sprintf( $option_name, 'home' ) );
} else if ( is_archive() ) {
// all archives wrapped in one option
// is_post_type_archive || is_date || is_author || is_category || is_tag || is_tax
$enabled_features = get_option( sprintf( $option_name, 'archive' ) );
} else {
$post_type = get_post_type();
if ( $post_type )
$enabled_features = get_option( sprintf( $option_name, $post_type ) );
}
if ( ! is_array( $enabled_features ) || empty( $enabled_features ) )
return;
require_once( $this->plugin_directory . 'social-plugins/social-plugins.php' );
$priority = apply_filters( 'facebook_content_filter_priority', 30 );
// features available for archives and singular
if ( isset( $enabled_features['like'] ) )
add_filter( 'the_content', 'facebook_the_content_like_button', $priority );
if ( isset( $enabled_features['send'] ) )
add_filter( 'the_content', 'facebook_the_content_send_button', $priority );
if ( isset( $enabled_features['follow'] ) )
add_filter( 'the_content', 'facebook_the_content_follow_button', $priority );
if ( isset( $enabled_features['mentions'] ) ) {
if ( ! function_exists( 'facebook_social_publisher_mentioning_output' ) )
require_once( dirname(__FILE__) . '/social-publisher/mentions.php' );
add_filter( 'the_content', 'facebook_social_publisher_mentioning_output', $priority );
}
// individual posts, pages, and custom post types features
if ( isset( $post_type ) ) {
if ( isset( $enabled_features['recommendations_bar'] ) )
add_filter( 'the_content', 'facebook_the_content_recommendations_bar', $priority );
// only load comments class and features if enabled and post type supports
if ( isset( $enabled_features['comments'] ) && post_type_supports( $post_type, 'comments' ) ) {
if ( ! class_exists( 'Facebook_Comments' ) )
require_once( $this->plugin_directory . 'social-plugins/class-facebook-comments.php' );
add_filter( 'the_content', array( 'Facebook_Comments', 'the_content_comments_box' ), $priority );
add_action( 'wp_enqueue_scripts', array( 'Facebook_Comments', 'css_hide_comments' ), 0 );
}
}
add_action( 'wp_enqueue_scripts', array( 'Facebook_Loader', 'enqueue_styles' ) );
}
/**
* Initialize the backend, administrative views
*
* @since 1.1
*/
public function admin_init() {
$admin_dir = $this->plugin_directory . 'admin/';
$sdk = $this->load_php_sdk();
if ( $sdk ) {
if ( ! class_exists( 'Facebook_User' ) )
require_once( dirname(__FILE__) . '/facebook-user.php' );
Facebook_User::extend_access_token();
}
if ( ! class_exists( 'Facebook_Settings' ) )
require_once( $admin_dir . 'settings.php' );
Facebook_Settings::init();
if ( ! class_exists( 'Facebook_Social_Publisher' ) )
require_once( $admin_dir . 'social-publisher/social-publisher.php' );
new Facebook_Social_Publisher();
if ( ! class_exists( 'Facebook_Mentions_Search' ) )
require_once( $admin_dir . 'social-publisher/mentions/mentions-search.php' );
Facebook_Mentions_Search::wp_ajax_handlers();
}
/**
* Register available widgets
*
* @since 1.1
* @uses register_widget()
*/
public function widgets_init() {
$widget_directory = $this->plugin_directory . 'social-plugins/widgets/';
foreach ( array(
'like-box' => 'Facebook_Like_Box_Widget',
'like-button' => 'Facebook_Like_Button_Widget',
'send-button' => 'Facebook_Send_Button_Widget',
'follow-button' => 'Facebook_Follow_Button_Widget',
'recommendations-box' => 'Facebook_Recommendations_Widget',
'activity-feed' => 'Facebook_Activity_Feed_Widget'
) as $filename => $classname ) {
if ( class_exists( $classname ) ) {
register_widget( $classname );
} else {
$file = $widget_directory . $filename . '.php';
if ( file_exists( $file ) ) {
include_once( $file );
if ( class_exists( $classname ) )
register_widget( $classname );
}
unset( $file );
}
}
}
/**
* Test if a given locale is a valid Facebook locale
*
* @since 1.1.11
* @see Facebook_Loader::$locales
* @param @param string $locale language and localization combined in a single string. ISO 639-1 (alpha-2) language + underscore character (_) + ISO 3166-1 (alpha-2) country code. example: en_US, es_ES
* @return bool true if locals in list of valid locales. else false
*/
public static function is_valid_locale( $locale ) {
if ( is_string( $locale ) && isset( self::$locales[$locale] ) )
return true;
return false;
}
/**
* Sanitize a locale input against a list of Facebook-specific locales
*
* @since 1.1.11
* @param string $locale language and localization combined in a single string. The function will attempt to convert an ISO 639-1 (alpha-2) language or a language combined with a ISO 3166-1 (alpha-2) country code separated by a dash or underscore. examples: en, en-US, en_US
* @return string a Facebook-friendly locale
*/
public static function sanitize_locale( $locale ) {
if ( ! is_string( $locale ) )
return self::DEFAULT_LOCALE;
$locale_length = strlen( $locale );
if ( ! ( $locale_length === 2 || $locale_length === 5 ) )
return self::DEFAULT_LOCALE;
// convert locales like "es" to "es_ES"
if ( $locale_length === 2 ) {
if ( ! ctype_alpha( $locale ) )
return self::DEFAULT_LOCALE;
$locale = strtolower( $locale );
foreach( self::$locales as $facebook_locale => $exists ) {
if ( substr_compare( $facebook_locale, $locale, 0, 2 ) === 0 )
return $facebook_locale;
}
// no ISO 639-1 match found
return self::DEFAULT_LOCALE;
}
unset( $locale_length );
$lang = substr( $locale, 0, 2 );
if ( ! ctype_alpha( $lang ) )
return self::DEFAULT_LOCALE;
$localization = substr( $locale, 3, 2 );
if ( ! ctype_alpha( $localization ) )
return self::DEFAULT_LOCALE;
// rebuild based on expectations
return strtolower( $lang ) . '_' . strtoupper( $localization );
}
/**
* Map the site locale to a supported Facebook locale
* Affects OGP and SDK outputs
*
* @since 1.1
*/
public function set_locale() {
$transient_key = 'facebook_locale';
$locale = get_transient( $transient_key );
if ( $locale ) {
$this->locale = $locale;
return;
}
// sanitize the locale. e.g. en-US to en_US
// filter the result in case a site would like to override
$locale = apply_filters( 'fb_locale', self::sanitize_locale( get_locale() ) );
// validate our sanitized value and a possible filter override
if ( ! self::is_valid_locale( $locale ) )
$locale = self::DEFAULT_LOCALE;
set_transient( $transient_key, $locale, 60*60*24 );
$this->locale = $locale;
}
/**
* Tie-in to popular site features handled by popular WordPress plugins
*
* @since 1.1.9
*/
public static function plugin_extras() {
// add Google Analytics social trackers
if ( defined( 'GOOGLE_ANALYTICATOR_VERSION' ) && function_exists( 'add_google_analytics' ) && has_action( 'wp_head', 'add_google_analytics' ) !== false ) {
if ( ! class_exists( 'Facebook_Google_Analytics' ) )
require_once( dirname(__FILE__) . '/extras/google-analytics.php' );
add_action( 'google_analyticator_extra_js_after', array( 'Facebook_Google_Analytics', 'enqueue' ) );
}
if ( ( defined( 'GAWP_VERSION' ) && class_exists( 'GA_Filter' ) && has_action( 'wp_head', array( 'GA_Filter', 'spool_analytics' ) ) !== false ) ) {
if ( ! class_exists( 'Facebook_Google_Analytics' ) )
require_once( dirname(__FILE__) . '/extras/google-analytics.php' );
add_filter( 'yoast-ga-push-after-pageview', array( 'Facebook_Google_Analytics', 'gaq_filter' ) );
}
}
}
/**
* Load plugin function during the WordPress init action
*
* @since 1.1
*/
function facebook_loader_init() {
global $facebook_loader;
$facebook_loader = new Facebook_Loader();
}
add_action( 'init', 'facebook_loader_init', 0 ); // load before widgets_init at 1
?>