forked from facebook/facebook-for-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacebook-commerce-messenger-chat.php
executable file
·254 lines (216 loc) · 5.14 KB
/
facebook-commerce-messenger-chat.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
<?php
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* @package FacebookCommerce
*/
if ( ! class_exists( 'WC_Facebookcommerce_MessengerChat' ) ) :
if ( ! class_exists( 'WC_Facebookcommerce_Utils' ) ) {
include_once 'includes/fbutils.php';
}
class WC_Facebookcommerce_MessengerChat {
/** @var string[] list of supported locales */
private static $supported_locales = [
'af_ZA',
'ar_AR',
'as_IN',
'az_AZ',
'be_BY',
'bg_BG',
'bn_IN',
'br_FR',
'bs_BA',
'ca_ES',
'cb_IQ',
'co_FR',
'cs_CZ',
'cx_PH',
'cy_GB',
'da_DK',
'de_DE',
'el_GR',
'en_GB',
'en_US',
'es_ES',
'es_LA',
'et_EE',
'eu_ES',
'fa_IR',
'ff_NG',
'fi_FI',
'fo_FO',
'fr_CA',
'fr_FR',
'fy_NL',
'ga_IE',
'gl_ES',
'gn_PY',
'gu_IN',
'ha_NG',
'he_IL',
'hi_IN',
'hr_HR',
'hu_HU',
'hy_AM',
'id_ID',
'is_IS',
'it_IT',
'ja_JP',
'ja_KS',
'jv_ID',
'ka_GE',
'kk_KZ',
'km_KH',
'kn_IN',
'ko_KR',
'ku_TR',
'lt_LT',
'lv_LV',
'mg_MG',
'mk_MK',
'ml_IN',
'mn_MN',
'mr_IN',
'ms_MY',
'mt_MT',
'my_MM',
'nb_NO',
'ne_NP',
'nl_BE',
'nl_NL',
'nn_NO',
'or_IN',
'pa_IN',
'pl_PL',
'ps_AF',
'pt_BR',
'pt_PT',
'qz_MM',
'ro_RO',
'ru_RU',
'rw_RW',
'sc_IT',
'si_LK',
'sk_SK',
'sl_SI',
'so_SO',
'sq_AL',
'sr_RS',
'sv_SE',
'sw_KE',
'sz_PL',
'ta_IN',
'te_IN',
'tg_TJ',
'th_TH',
'tl_PH',
'tr_TR',
'tz_MA',
'uk_UA',
'ur_PK',
'uz_UZ',
'vi_VN',
'zh_CN',
'zh_HK',
'zh_TW',
];
public function __construct( $settings ) {
$this->page_id = isset( $settings['fb_page_id'] )
? $settings['fb_page_id']
: '';
$this->jssdk_version = isset( $settings['facebook_jssdk_version'] )
? $settings['facebook_jssdk_version']
: '';
add_action( 'wp_footer', array( $this, 'inject_messenger_chat_plugin' ) );
}
/**
* Outputs the Facebook Messenger chat script.
*
* @internal
*/
public function inject_messenger_chat_plugin() {
if ( facebook_for_woocommerce()->get_integration()->is_messenger_enabled() ) :
printf( "
<div
attribution=\"fbe_woocommerce\"
class=\"fb-customerchat\"
page_id=\"%s\"
></div>
<!-- Facebook JSSDK -->
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '',
autoLogAppEvents : true,
xfbml : true,
version : '%s'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = 'https://connect.facebook.net/%s/sdk/xfbml.customerchat.js';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div></div>
",
esc_attr( $this->page_id ),
esc_js( $this->jssdk_version ?: 'v5.0' ),
esc_js( facebook_for_woocommerce()->get_integration()->get_messenger_locale() ?: 'en_US' )
);
endif;
}
/**
* Gets the locales supported by Facebook Messenger.
*
* Returns the format $locale => $name for display in options.
* @link https://developers.facebook.com/docs/messenger-platform/messenger-profile/supported-locales/
* If the Locale extension is not available, will attempt to match locales to WordPress available language names.
*
* @since 1.10.0
*
* @return array associative array of locale codes and names
*/
public static function get_supported_locales() {
global $wp_version;
$locales = [];
if ( class_exists( 'Locale' ) ) {
foreach ( self::$supported_locales as $locale ) {
if ( $name = \Locale::getDisplayName( $locale, substr( $locale, 0, 2 ) ) ) {
$locales[ $locale ] = ucfirst( $name );
}
}
} else {
include_once( ABSPATH . '/wp-admin/includes/translation-install.php' );
$translations = wp_get_available_translations();
foreach ( self::$supported_locales as $locale ) {
if ( isset( $translations[ $locale ]['native_name'] ) ) {
$locales[ $locale ] = $translations[ $locale ]['native_name'];
} else { // generic match e.g. <it>_IT, <it>_CH (any language in the the <it> group )
$matched_locale = substr( $locale, 0, 2 );
if ( isset( $translations[ $matched_locale ]['native_name'] ) ) {
$locales[ $locale ] = $translations[ $matched_locale ]['native_name'];
}
}
}
// always include US English
$locales['en_US'] = _x( 'English (United States)', 'language', 'facebook-for-woocommerce' );
}
/**
* Filters the locales supported by Facebook Messenger.
*
* @since 1.10.0
*
* @param array $locales locales supported by Facebook Messenger, in $locale => $name format
*/
$locales = (array) apply_filters( 'wc_facebook_messenger_supported_locales', array_unique( $locales ) );
natcasesort( $locales );
return $locales;
}
}
endif;