facebook-for-woocommerce
Last commit date
assets
2 years ago
data
3 years ago
i18n
2 years ago
includes
2 years ago
vendor
2 years ago
LICENSE
7 years ago
changelog.txt
2 years ago
class-wc-facebookcommerce.php
2 years ago
facebook-commerce-events-tracker.php
2 years ago
facebook-commerce-messenger-chat.php
2 years ago
facebook-commerce-pixel-event.php
2 years ago
facebook-commerce.php
2 years ago
facebook-config-warmer.php
3 years ago
facebook-for-woocommerce.php
2 years ago
readme.txt
2 years ago
facebook-commerce-messenger-chat.php
115 lines
| 1 | <?php |
| 2 | // phpcs:ignoreFile |
| 3 | /** |
| 4 | * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved |
| 5 | * |
| 6 | * This source code is licensed under the license found in the |
| 7 | * LICENSE file in the root directory of this source tree. |
| 8 | * |
| 9 | * @package FacebookCommerce |
| 10 | */ |
| 11 | |
| 12 | use WooCommerce\Facebook\Locale; |
| 13 | |
| 14 | if ( ! class_exists( 'WC_Facebookcommerce_MessengerChat' ) ) : |
| 15 | |
| 16 | if ( ! class_exists( 'WC_Facebookcommerce_Utils' ) ) { |
| 17 | include_once 'includes/fbutils.php'; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Messenger chat handler class. |
| 22 | */ |
| 23 | class WC_Facebookcommerce_MessengerChat { |
| 24 | |
| 25 | /** @var string Facebook Page ID. */ |
| 26 | private $page_id; |
| 27 | |
| 28 | /** @var string|null JS SDK Version. */ |
| 29 | private $jssdk_version; |
| 30 | |
| 31 | /** |
| 32 | * Class constructor. |
| 33 | * |
| 34 | * @param array $settings FB page settings array. |
| 35 | */ |
| 36 | public function __construct( $settings ) { |
| 37 | |
| 38 | $this->page_id = isset( $settings['fb_page_id'] ) |
| 39 | ? $settings['fb_page_id'] |
| 40 | : ''; |
| 41 | |
| 42 | $this->jssdk_version = isset( $settings['facebook_jssdk_version'] ) |
| 43 | ? $settings['facebook_jssdk_version'] |
| 44 | : ''; |
| 45 | |
| 46 | add_action( 'wp_footer', array( $this, 'inject_messenger_chat_plugin' ) ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * __get method for backward compatibility. |
| 51 | * |
| 52 | * @param string $key property name |
| 53 | * @return mixed |
| 54 | * @since 3.0.32 |
| 55 | */ |
| 56 | public function __get( $key ) { |
| 57 | // Add warning for private properties. |
| 58 | if ( in_array( $key, array( 'page_id', 'jssdk_version' ), true ) ) { |
| 59 | /* translators: %s property name. */ |
| 60 | _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'The %s property is private and should not be accessed outside its class.', 'facebook-for-woocommerce' ), esc_html( $key ) ), '3.0.32' ); |
| 61 | return $this->$key; |
| 62 | } |
| 63 | |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Outputs the Facebook Messenger chat script. |
| 69 | * |
| 70 | * @internal |
| 71 | */ |
| 72 | public function inject_messenger_chat_plugin() { |
| 73 | |
| 74 | if ( facebook_for_woocommerce()->get_integration()->is_messenger_enabled() ) : |
| 75 | |
| 76 | printf( |
| 77 | " |
| 78 | <div |
| 79 | attribution=\"fbe_woocommerce\" |
| 80 | class=\"fb-customerchat\" |
| 81 | page_id=\"%s\" |
| 82 | ></div> |
| 83 | <!-- Facebook JSSDK --> |
| 84 | <script> |
| 85 | window.fbAsyncInit = function() { |
| 86 | FB.init({ |
| 87 | appId : '', |
| 88 | autoLogAppEvents : true, |
| 89 | xfbml : true, |
| 90 | version : '%s' |
| 91 | }); |
| 92 | }; |
| 93 | |
| 94 | (function(d, s, id){ |
| 95 | var js, fjs = d.getElementsByTagName(s)[0]; |
| 96 | if (d.getElementById(id)) {return;} |
| 97 | js = d.createElement(s); js.id = id; |
| 98 | js.src = 'https://connect.facebook.net/%s/sdk/xfbml.customerchat.js'; |
| 99 | fjs.parentNode.insertBefore(js, fjs); |
| 100 | }(document, 'script', 'facebook-jssdk')); |
| 101 | </script> |
| 102 | <div></div> |
| 103 | ", |
| 104 | esc_attr( $this->page_id ), |
| 105 | esc_js( $this->jssdk_version ?: 'v5.0' ), |
| 106 | esc_js( facebook_for_woocommerce()->get_integration()->get_messenger_locale() ?: 'en_US' ) |
| 107 | ); |
| 108 | |
| 109 | endif; |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | endif; |
| 115 |