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
class-wc-facebookcommerce.php
861 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 | require_once __DIR__ . '/vendor/autoload.php'; |
| 13 | require_once __DIR__ . '/includes/fbutils.php'; |
| 14 | |
| 15 | use Automattic\WooCommerce\Admin\Features\Features as WooAdminFeatures; |
| 16 | use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists; |
| 17 | use WooCommerce\Facebook\Admin\Tasks\Setup; |
| 18 | use WooCommerce\Facebook\Admin\Notes\SettingsMoved; |
| 19 | use WooCommerce\Facebook\Framework\Api\Exception as ApiException; |
| 20 | use WooCommerce\Facebook\Framework\Helper; |
| 21 | use WooCommerce\Facebook\Framework\Plugin\Compatibility; |
| 22 | use WooCommerce\Facebook\Integrations\Bookings as BookingsIntegration; |
| 23 | use WooCommerce\Facebook\Lifecycle; |
| 24 | use WooCommerce\Facebook\ProductSync\ProductValidator as ProductSyncValidator; |
| 25 | use WooCommerce\Facebook\Utilities\Background_Handle_Virtual_Products_Variations; |
| 26 | use WooCommerce\Facebook\Utilities\Background_Remove_Duplicate_Visibility_Meta; |
| 27 | use WooCommerce\Facebook\Utilities\DebugTools; |
| 28 | use WooCommerce\Facebook\Utilities\Heartbeat; |
| 29 | |
| 30 | class WC_Facebookcommerce extends WooCommerce\Facebook\Framework\Plugin { |
| 31 | /** @var string the plugin version */ |
| 32 | const VERSION = WC_Facebook_Loader::PLUGIN_VERSION; |
| 33 | |
| 34 | /** @var string for backwards compatibility TODO: remove this in v2.0.0 {CW 2020-02-06} */ |
| 35 | const PLUGIN_VERSION = self::VERSION; |
| 36 | |
| 37 | /** @var string the plugin ID */ |
| 38 | const PLUGIN_ID = 'facebook_for_woocommerce'; |
| 39 | |
| 40 | /** @var string the integration ID */ |
| 41 | const INTEGRATION_ID = 'facebookcommerce'; |
| 42 | |
| 43 | /** @var string the product set categories meta name */ |
| 44 | const PRODUCT_SET_META = '_wc_facebook_product_cats'; |
| 45 | |
| 46 | /** @var string the plugin user agent name to use for HTTP calls within User-Agent header */ |
| 47 | const PLUGIN_USER_AGENT_NAME = 'Facebook-for-WooCommerce'; |
| 48 | |
| 49 | /** @var WC_Facebookcommerce singleton instance */ |
| 50 | protected static $instance; |
| 51 | |
| 52 | /** @var WooCommerce\Facebook\API instance */ |
| 53 | private $api; |
| 54 | |
| 55 | /** @var \WC_Facebookcommerce_Integration instance */ |
| 56 | private $integration; |
| 57 | |
| 58 | /** @var WooCommerce\Facebook\Admin admin handler instance */ |
| 59 | private $admin; |
| 60 | |
| 61 | /** @var WooCommerce\Facebook\Admin\Settings */ |
| 62 | private $admin_settings; |
| 63 | |
| 64 | /** @var WooCommerce\Facebook\AJAX Ajax handler instance */ |
| 65 | private $ajax; |
| 66 | |
| 67 | /** @var WooCommerce\Facebook\Products\Feed product feed handler */ |
| 68 | private $product_feed; |
| 69 | |
| 70 | /** @var Background_Handle_Virtual_Products_Variations instance */ |
| 71 | protected $background_handle_virtual_products_variations; |
| 72 | |
| 73 | /** @var Background_Remove_Duplicate_Visibility_Meta job handler instance */ |
| 74 | protected $background_remove_duplicate_visibility_meta; |
| 75 | |
| 76 | /** @var WooCommerce\Facebook\Products\Stock products stock handler */ |
| 77 | private $products_stock_handler; |
| 78 | |
| 79 | /** @var WooCommerce\Facebook\Products\Sync products sync handler */ |
| 80 | private $products_sync_handler; |
| 81 | |
| 82 | /** @var WooCommerce\Facebook\Products\Sync\Background background sync handler */ |
| 83 | private $sync_background_handler; |
| 84 | |
| 85 | /** @var WooCommerce\Facebook\ProductSets\Sync product sets sync handler */ |
| 86 | private $product_sets_sync_handler; |
| 87 | |
| 88 | /** @var WooCommerce\Facebook\Handlers\Connection connection handler */ |
| 89 | private $connection_handler; |
| 90 | |
| 91 | /** @var WooCommerce\Facebook\Handlers\WebHook webhook handler */ |
| 92 | private $webhook_handler; |
| 93 | |
| 94 | /** @var WooCommerce\Facebook\Commerce commerce handler */ |
| 95 | private $commerce_handler; |
| 96 | |
| 97 | /** @var WooCommerce\Facebook\Utilities\Tracker */ |
| 98 | private $tracker; |
| 99 | |
| 100 | /** @var WooCommerce\Facebook\Jobs\JobManager */ |
| 101 | public $job_manager; |
| 102 | |
| 103 | /** @var WooCommerce\Facebook\Utilities\Heartbeat */ |
| 104 | public $heartbeat; |
| 105 | |
| 106 | /** @var WooCommerce\Facebook\ExternalVersionUpdate */ |
| 107 | private $external_version_update; |
| 108 | |
| 109 | /** @var WooCommerce\Facebook\Feed\FeedConfigurationDetection instance. */ |
| 110 | private $configuration_detection; |
| 111 | |
| 112 | /** @var WooCommerce\Facebook\Products\FBCategories instance. */ |
| 113 | private $fb_categories; |
| 114 | |
| 115 | /** |
| 116 | * The Debug tools instance. |
| 117 | * |
| 118 | * @var WooCommerce\Facebook\Utilities\DebugTools |
| 119 | */ |
| 120 | private $debug_tools; |
| 121 | |
| 122 | /** |
| 123 | * Constructs the plugin. |
| 124 | * |
| 125 | * @since 1.0.0 |
| 126 | */ |
| 127 | public function __construct() { |
| 128 | parent::__construct( |
| 129 | self::PLUGIN_ID, |
| 130 | self::VERSION, |
| 131 | [ 'text_domain' => 'facebook-for-woocommerce' ] |
| 132 | ); |
| 133 | $this->init(); |
| 134 | $this->init_admin(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * __get method for backward compatibility. |
| 139 | * |
| 140 | * @param string $key property name |
| 141 | * @return mixed |
| 142 | * @since 3.0.32 |
| 143 | */ |
| 144 | public function __get( $key ) { |
| 145 | // Add warning for private properties. |
| 146 | if ( in_array( $key, array( 'configuration_detection', 'fb_categories' ), true ) ) { |
| 147 | /* translators: %s property name. */ |
| 148 | _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' ); |
| 149 | return $this->$key; |
| 150 | } |
| 151 | |
| 152 | return null; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Initializes the plugin. |
| 157 | * |
| 158 | * @internal |
| 159 | */ |
| 160 | public function init() { |
| 161 | add_action( 'init', array( $this, 'get_integration' ) ); |
| 162 | add_action( 'init', array( $this, 'register_custom_taxonomy' ) ); |
| 163 | add_action( 'add_meta_boxes_product', array( $this, 'remove_product_fb_product_set_metabox' ), 50 ); |
| 164 | add_filter( 'fb_product_set_row_actions', array( $this, 'product_set_links' ) ); |
| 165 | add_filter( 'manage_edit-fb_product_set_columns', array( $this, 'manage_fb_product_set_columns' ) ); |
| 166 | |
| 167 | // Hook the setup task. The hook admin_init is not triggered when the WC fetches the tasks using the endpoint: wp-json/wc-admin/onboarding/tasks and hence hooking into init. |
| 168 | add_action( 'init', array( $this, 'add_setup_task' ), 20 ); |
| 169 | add_action( 'admin_notices', array( $this, 'add_inbox_notes' ) ); |
| 170 | |
| 171 | // Product Set breadcrumb filters |
| 172 | add_filter( 'woocommerce_navigation_is_connected_page', array( $this, 'is_current_page_conected_filter' ), 99, 2 ); |
| 173 | add_filter( 'woocommerce_navigation_get_breadcrumbs', array( $this, 'wc_page_breadcrumbs_filter' ), 99 ); |
| 174 | |
| 175 | add_filter( |
| 176 | 'wc_' . WC_Facebookcommerce::PLUGIN_ID . '_http_request_args', |
| 177 | array( $this, 'force_user_agent_in_latin' ) |
| 178 | ); |
| 179 | |
| 180 | if ( \WC_Facebookcommerce_Utils::isWoocommerceIntegration() ) { |
| 181 | include_once 'facebook-commerce.php'; |
| 182 | |
| 183 | require_once __DIR__ . '/includes/fbproductfeed.php'; |
| 184 | require_once __DIR__ . '/facebook-commerce-messenger-chat.php'; |
| 185 | |
| 186 | $this->heartbeat = new Heartbeat( WC()->queue() ); |
| 187 | $this->heartbeat->init(); |
| 188 | |
| 189 | $this->product_feed = new WooCommerce\Facebook\Products\Feed(); |
| 190 | $this->products_stock_handler = new WooCommerce\Facebook\Products\Stock(); |
| 191 | $this->products_sync_handler = new WooCommerce\Facebook\Products\Sync(); |
| 192 | $this->sync_background_handler = new WooCommerce\Facebook\Products\Sync\Background(); |
| 193 | $this->configuration_detection = new WooCommerce\Facebook\Feed\FeedConfigurationDetection(); |
| 194 | $this->product_sets_sync_handler = new WooCommerce\Facebook\ProductSets\Sync(); |
| 195 | $this->commerce_handler = new WooCommerce\Facebook\Commerce(); |
| 196 | $this->fb_categories = new WooCommerce\Facebook\Products\FBCategories(); |
| 197 | $this->external_version_update = new WooCommerce\Facebook\ExternalVersionUpdate\Update(); |
| 198 | |
| 199 | if ( wp_doing_ajax() ) { |
| 200 | $this->ajax = new WooCommerce\Facebook\AJAX(); |
| 201 | } |
| 202 | |
| 203 | // Load integrations. |
| 204 | require_once __DIR__ . '/includes/fbwpml.php'; |
| 205 | new WC_Facebook_WPML_Injector(); |
| 206 | new BookingsIntegration(); |
| 207 | |
| 208 | if ( 'yes' !== get_option( 'wc_facebook_background_handle_virtual_products_variations_complete', 'no' ) ) { |
| 209 | $this->background_handle_virtual_products_variations = new Background_Handle_Virtual_Products_Variations(); |
| 210 | } |
| 211 | |
| 212 | if ( 'yes' !== get_option( 'wc_facebook_background_remove_duplicate_visibility_meta_complete', 'no' ) ) { |
| 213 | $this->background_remove_duplicate_visibility_meta = new Background_Remove_Duplicate_Visibility_Meta(); |
| 214 | } |
| 215 | |
| 216 | $this->connection_handler = new WooCommerce\Facebook\Handlers\Connection( $this ); |
| 217 | $this->webhook_handler = new WooCommerce\Facebook\Handlers\WebHook( $this ); |
| 218 | $this->tracker = new WooCommerce\Facebook\Utilities\Tracker(); |
| 219 | |
| 220 | // Init jobs |
| 221 | $this->job_manager = new WooCommerce\Facebook\Jobs\JobManager(); |
| 222 | add_action( 'init', [ $this->job_manager, 'init' ] ); |
| 223 | |
| 224 | // Instantiate the debug tools. |
| 225 | $this->debug_tools = new DebugTools(); |
| 226 | |
| 227 | // load admin handlers, before admin_init |
| 228 | if ( is_admin() ) { |
| 229 | $this->admin_settings = new WooCommerce\Facebook\Admin\Settings( $this->connection_handler->is_connected() ); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * Initializes the admin handling. |
| 237 | * |
| 238 | * @internal |
| 239 | * |
| 240 | * @since 1.10.0 |
| 241 | */ |
| 242 | public function init_admin() { |
| 243 | add_action( |
| 244 | 'admin_init', |
| 245 | function () { |
| 246 | $this->admin = new WooCommerce\Facebook\Admin(); |
| 247 | }, |
| 248 | 0 |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Add Inbox notes. |
| 254 | */ |
| 255 | public function add_inbox_notes() { |
| 256 | if ( Compatibility::is_enhanced_admin_available() ) { |
| 257 | if ( class_exists( WooAdminFeatures::class ) ) { |
| 258 | $is_marketing_enabled = WooAdminFeatures::is_enabled( 'marketing' ); |
| 259 | } else { |
| 260 | $is_marketing_enabled = is_callable( '\Automattic\WooCommerce\Admin\Loader::is_feature_enabled' ) |
| 261 | && \Automattic\WooCommerce\Admin\Loader::is_feature_enabled( 'marketing' ); |
| 262 | } |
| 263 | |
| 264 | if ( $is_marketing_enabled && class_exists( '\Automattic\WooCommerce\Admin\Notes\Note' ) ) { // Checking for Note class is for backward compatibility. |
| 265 | SettingsMoved::possibly_add_or_delete_note(); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Gets deprecated and removed hooks. |
| 272 | * |
| 273 | * @since 2.1.0 |
| 274 | * |
| 275 | * @return array |
| 276 | */ |
| 277 | protected function get_deprecated_hooks() { |
| 278 | return array( |
| 279 | 'wc_facebook_page_access_token' => array( |
| 280 | 'version' => '2.1.0', |
| 281 | 'replacement' => false, |
| 282 | ), |
| 283 | ); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Adds the setup task to the Tasklists. |
| 288 | * |
| 289 | * @since 2.6.29 |
| 290 | */ |
| 291 | public function add_setup_task() { |
| 292 | if ( class_exists( TaskLists::class ) ) { // This is added for backward compatibility. |
| 293 | TaskLists::add_task( |
| 294 | 'extended', |
| 295 | new Setup( |
| 296 | TaskLists::get_list( 'extended' ) |
| 297 | ) |
| 298 | ); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Get the last event from the plugin lifecycle. |
| 304 | * |
| 305 | * @since 2.6.29 |
| 306 | * @return array |
| 307 | */ |
| 308 | public function get_last_event_from_history() { |
| 309 | $last_event = array(); |
| 310 | $history_events = $this->lifecycle_handler->get_event_history(); |
| 311 | |
| 312 | if ( isset( $history_events[0] ) ) { |
| 313 | $last_event = $history_events[0]; |
| 314 | } |
| 315 | return $last_event; |
| 316 | } |
| 317 | |
| 318 | public function add_wordpress_integration() { |
| 319 | new WP_Facebook_Integration(); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Saves errors or messages to WooCommerce Log (woocommerce/logs/plugin-id-xxx.txt) |
| 324 | * |
| 325 | * @since 2.3.3 |
| 326 | * @param string $message error or message to save to log |
| 327 | * @param string $log_id optional log id to segment the files by, defaults to plugin id |
| 328 | */ |
| 329 | public function log( $message, $log_id = null ) { |
| 330 | // Bail if site is connected and user has disabled logging. |
| 331 | // If site is disconnected, force-enable logging so merchant can diagnose connection issues. |
| 332 | if ( ( ! $this->get_integration() || ! $this->get_integration()->is_debug_mode_enabled() ) && $this->get_connection_handler()->is_connected() ) { |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | parent::log( $message, $log_id ); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Logs an API request. |
| 341 | * |
| 342 | * @since 2.0.0 |
| 343 | * |
| 344 | * @param array $request request data |
| 345 | * @param array $response response data |
| 346 | * @param null $log_id log ID |
| 347 | */ |
| 348 | public function log_api_request( $request, $response, $log_id = null ) { |
| 349 | // bail if logging isn't enabled |
| 350 | if ( ! $this->get_integration() || ! $this->get_integration()->is_debug_mode_enabled() ) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | // Maybe remove headers from the debug log. |
| 355 | if( ! $this->get_integration()->are_headers_requested_for_debug() ) { |
| 356 | unset( $request['headers'] ); |
| 357 | unset( $response['headers'] ); |
| 358 | } |
| 359 | |
| 360 | $this->log( $this->get_api_log_message( $request ), $log_id ); |
| 361 | |
| 362 | if ( ! empty( $response ) ) { |
| 363 | $this->log( $this->get_api_log_message( $response ), $log_id ); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Remove Product Set metabox from Product edit page |
| 369 | * |
| 370 | * @since 2.3.0 |
| 371 | */ |
| 372 | public function remove_product_fb_product_set_metabox() { |
| 373 | remove_meta_box( 'fb_product_setdiv', 'product', 'side' ); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Register Facebook Product Set Taxonomy |
| 378 | * |
| 379 | * @since 2.3.0 |
| 380 | */ |
| 381 | public function register_custom_taxonomy() { |
| 382 | $plural = esc_html__( 'Facebook Product Sets', 'facebook-for-woocommerce' ); |
| 383 | $singular = esc_html__( 'Facebook Product Set', 'facebook-for-woocommerce' ); |
| 384 | |
| 385 | $args = array( |
| 386 | 'labels' => array( |
| 387 | 'name' => $plural, |
| 388 | 'singular_name' => $singular, |
| 389 | 'menu_name' => $plural, |
| 390 | // translators: Edit item label |
| 391 | 'edit_item' => sprintf( esc_html__( 'Edit %s', 'facebook-for-woocommerce' ), $singular ), |
| 392 | // translators: Add new label |
| 393 | 'add_new_item' => sprintf( esc_html__( 'Add new %s', 'facebook-for-woocommerce' ), $singular ), |
| 394 | // translators: No items found text |
| 395 | 'not_found' => sprintf( esc_html__( 'No %s found.', 'facebook-for-woocommerce' ), $plural ), |
| 396 | // translators: Search label |
| 397 | 'search_items' => sprintf( esc_html__( 'Search %s.', 'facebook-for-woocommerce' ), $plural ), |
| 398 | // translators: Text label |
| 399 | 'separate_items_with_commas' => sprintf( esc_html__( 'Separate %s with commas', 'facebook-for-woocommerce' ), $plural ), |
| 400 | // translators: Text label |
| 401 | 'choose_from_most_used' => sprintf( esc_html__( 'Choose from the most used %s', 'facebook-for-woocommerce' ), $plural ), |
| 402 | // translators: Backlink item label |
| 403 | 'back_to_items' => sprintf( esc_html__( '← Go to %s', 'facebook-for-woocommerce' ), $plural ), |
| 404 | ), |
| 405 | 'hierarchical' => true, |
| 406 | 'public' => true, |
| 407 | 'show_in_nav_menus' => false, |
| 408 | 'show_tagcloud' => false, |
| 409 | 'show_in_menu' => false, |
| 410 | ); |
| 411 | |
| 412 | register_taxonomy( 'fb_product_set', array( 'product' ), $args ); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /** |
| 417 | * Filter Facebook Product Set Taxonomy table links |
| 418 | * |
| 419 | * @since 2.3.0 |
| 420 | * |
| 421 | * @param array $actions Item Actions. |
| 422 | * |
| 423 | * @return array |
| 424 | */ |
| 425 | public function product_set_links( $actions ) { |
| 426 | unset( $actions['inline hide-if-no-js'] ); |
| 427 | unset( $actions['view'] ); |
| 428 | return $actions; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | /** |
| 433 | * Remove posts count column from Facebook Product Set custom taxonomy |
| 434 | * |
| 435 | * @since 2.3.0 |
| 436 | * |
| 437 | * @param array $columns Taxonomy columns. |
| 438 | * |
| 439 | * @return array |
| 440 | */ |
| 441 | public function manage_fb_product_set_columns( $columns ) { |
| 442 | unset( $columns['posts'] ); |
| 443 | return $columns; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | /** |
| 448 | * Filter WC Breadcrumbs when the page is Facebook Product Sets |
| 449 | * |
| 450 | * @since 2.3.0 |
| 451 | * |
| 452 | * @param array $breadcrumbs Page breadcrumbs. |
| 453 | * |
| 454 | * @return array |
| 455 | */ |
| 456 | public function wc_page_breadcrumbs_filter( $breadcrumbs ) { |
| 457 | |
| 458 | if ( 'edit-fb_product_set' !== $this->get_current_page_id() ) { |
| 459 | return $breadcrumbs; |
| 460 | } |
| 461 | |
| 462 | $breadcrumbs = array( |
| 463 | array( 'admin.php?page=wc-admin', 'WooCommerce' ), |
| 464 | array( 'edit.php?post_type=product', 'Products' ), |
| 465 | ); |
| 466 | |
| 467 | $term_id = empty( $_GET['tag_ID'] ) ? '' : wc_clean( wp_unslash( $_GET['tag_ID'] ) ); //phpcs:ignore WordPress.Security |
| 468 | if ( ! empty( $term_id ) ) { |
| 469 | $breadcrumbs[] = array( 'edit-tags.php?taxonomy=fb_product_set&post_type=product', 'Products Sets' ); |
| 470 | } |
| 471 | |
| 472 | $breadcrumbs[] = ( empty( $term_id ) ? 'Product Sets' : 'Edit Product Set' ); |
| 473 | return $breadcrumbs; |
| 474 | } |
| 475 | |
| 476 | |
| 477 | /** |
| 478 | * Return that Facebook Product Set page is a WC Conected Page |
| 479 | * |
| 480 | * @since 2.3.0 |
| 481 | * |
| 482 | * @param boolean $is_conected If it's connected or not. |
| 483 | * |
| 484 | * @return boolean |
| 485 | */ |
| 486 | public function is_current_page_conected_filter( $is_conected ) { |
| 487 | if ( 'edit-fb_product_set' === $this->get_current_page_id() ) { |
| 488 | return true; |
| 489 | } |
| 490 | |
| 491 | return $is_conected; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Filter is responsible to always set latin user agent header value, because translated plugin names |
| 496 | * may contain characters which Facebook does not accept and return 400 response for requests with such |
| 497 | * header values. |
| 498 | * Applying either sanitize_title() nor remove_accents() on header value will not work for all the languages |
| 499 | * we support translations to e.g. Hebrew is going to convert into something %d7%90%d7%a8%d7%99%d7%92 which is |
| 500 | * not acceptable neither. |
| 501 | * |
| 502 | * @param array $http_request_headers - http request headers |
| 503 | * @return array |
| 504 | */ |
| 505 | public function force_user_agent_in_latin( array $http_request_headers ) { |
| 506 | if ( isset( $http_request_headers['user-agent'] ) ) { |
| 507 | $http_request_headers['user-agent'] = sprintf( |
| 508 | '%s/%s (WooCommerce/%s; WordPress/%s)', |
| 509 | WC_Facebookcommerce::PLUGIN_USER_AGENT_NAME, |
| 510 | WC_Facebookcommerce::PLUGIN_VERSION, |
| 511 | defined( 'WC_VERSION' ) ? WC_VERSION : WC_Facebook_Loader::MINIMUM_WC_VERSION, |
| 512 | $GLOBALS['wp_version'] |
| 513 | ); |
| 514 | } |
| 515 | return $http_request_headers; |
| 516 | } |
| 517 | |
| 518 | |
| 519 | /** Getter methods ********************************************************************************************/ |
| 520 | |
| 521 | |
| 522 | /** |
| 523 | * Gets the API instance. |
| 524 | * |
| 525 | * @since 2.0.0 |
| 526 | * |
| 527 | * @param string $access_token access token to use for this API request |
| 528 | * @return WooCommerce\Facebook\API |
| 529 | * @throws ApiException |
| 530 | */ |
| 531 | public function get_api( string $access_token = '' ): WooCommerce\Facebook\API { |
| 532 | // if none provided, use the general access token |
| 533 | if ( ! $access_token ) { |
| 534 | $access_token = $this->get_connection_handler()->get_access_token(); |
| 535 | } |
| 536 | if ( ! is_object( $this->api ) ) { |
| 537 | if ( ! $access_token ) { |
| 538 | throw new ApiException( __( 'Cannot create the API instance because the access token is missing.', 'facebook-for-woocommerce' ) ); |
| 539 | } |
| 540 | $this->api = new WooCommerce\Facebook\API( $access_token ); |
| 541 | } else { |
| 542 | $this->api->set_access_token( $access_token ); |
| 543 | } |
| 544 | return $this->api; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Gets the category handler. |
| 549 | * |
| 550 | * @since 1.11.0 |
| 551 | * |
| 552 | * @return WooCommerce\Facebook\Products\FBCategories |
| 553 | */ |
| 554 | public function get_facebook_category_handler() { |
| 555 | return $this->fb_categories; |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * Gets the background handle virtual products and variations handler instance. |
| 560 | * |
| 561 | * @since 2.0.0 |
| 562 | * |
| 563 | * @return Background_Handle_Virtual_Products_Variations |
| 564 | */ |
| 565 | public function get_background_handle_virtual_products_variations_instance() { |
| 566 | return $this->background_handle_virtual_products_variations; |
| 567 | } |
| 568 | |
| 569 | |
| 570 | /** |
| 571 | * Gets the background remove duplicate visibility meta data handler instance. |
| 572 | * |
| 573 | * @since 2.0.3 |
| 574 | * |
| 575 | * @return Background_Remove_Duplicate_Visibility_Meta |
| 576 | */ |
| 577 | public function get_background_remove_duplicate_visibility_meta_instance() { |
| 578 | return $this->background_remove_duplicate_visibility_meta; |
| 579 | } |
| 580 | |
| 581 | |
| 582 | /** |
| 583 | * Gets the products sync handler. |
| 584 | * |
| 585 | * @since 2.0.0 |
| 586 | * |
| 587 | * @return WooCommerce\Facebook\Products\Sync |
| 588 | */ |
| 589 | public function get_products_sync_handler() { |
| 590 | return $this->products_sync_handler; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | /** |
| 595 | * Gets the products sync background handler. |
| 596 | * |
| 597 | * @since 2.0.0 |
| 598 | * |
| 599 | * @return WooCommerce\Facebook\Products\Sync\Background |
| 600 | */ |
| 601 | public function get_products_sync_background_handler() { |
| 602 | return $this->sync_background_handler; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | /** |
| 607 | * Gets the connection handler. |
| 608 | * |
| 609 | * @since 2.0.0 |
| 610 | * |
| 611 | * @return WooCommerce\Facebook\Handlers\Connection |
| 612 | */ |
| 613 | public function get_connection_handler() { |
| 614 | return $this->connection_handler; |
| 615 | } |
| 616 | |
| 617 | |
| 618 | /** |
| 619 | * Gets the integration instance. |
| 620 | * |
| 621 | * @since 1.10.0 |
| 622 | * |
| 623 | * @return WC_Facebookcommerce_Integration instance |
| 624 | */ |
| 625 | public function get_integration() { |
| 626 | if ( null === $this->integration ) { |
| 627 | $this->integration = new WC_Facebookcommerce_Integration( $this ); |
| 628 | } |
| 629 | |
| 630 | return $this->integration; |
| 631 | } |
| 632 | |
| 633 | |
| 634 | /** |
| 635 | * Gets the commerce handler instance. |
| 636 | * |
| 637 | * @since 2.1.0 |
| 638 | * |
| 639 | * @return WooCommerce\Facebook\Commerce commerce handler instance |
| 640 | */ |
| 641 | public function get_commerce_handler() { |
| 642 | return $this->commerce_handler; |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Gets tracker instance. |
| 647 | * |
| 648 | * @since 2.6.0 |
| 649 | * |
| 650 | * @return WooCommerce\Facebook\Utilities\Tracker |
| 651 | */ |
| 652 | public function get_tracker() { |
| 653 | return $this->tracker; |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Gets the debug profiling logger instance. |
| 658 | * |
| 659 | * @return WooCommerce\Facebook\Debug\ProfilingLogger |
| 660 | */ |
| 661 | public function get_profiling_logger() { |
| 662 | static $instance = null; |
| 663 | if ( null === $instance ) { |
| 664 | $is_enabled = defined( 'FACEBOOK_FOR_WOOCOMMERCE_PROFILING_LOG_ENABLED' ) && FACEBOOK_FOR_WOOCOMMERCE_PROFILING_LOG_ENABLED; |
| 665 | $instance = new WooCommerce\Facebook\Debug\ProfilingLogger( $is_enabled ); |
| 666 | } |
| 667 | |
| 668 | return $instance; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Get the product sync validator class. |
| 673 | * |
| 674 | * @param WC_Product $product A product object to be validated. |
| 675 | * |
| 676 | * @return ProductSyncValidator |
| 677 | */ |
| 678 | public function get_product_sync_validator( WC_Product $product ) { |
| 679 | return new ProductSyncValidator( $this->get_integration(), $product ); |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * Gets the advertise tab page URL. |
| 684 | * |
| 685 | * @since 2.6.29 |
| 686 | * |
| 687 | * @return string |
| 688 | */ |
| 689 | public function get_advertise_tab_url() { |
| 690 | return admin_url( 'admin.php?page=wc-facebook&tab=advertise' ); |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Gets the settings page URL. |
| 695 | * |
| 696 | * @since 1.10.0 |
| 697 | * |
| 698 | * @param null $plugin_id unused |
| 699 | * @return string |
| 700 | */ |
| 701 | public function get_settings_url( $plugin_id = null ) { |
| 702 | return admin_url( 'admin.php?page=wc-facebook' ); |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Gets the plugin's documentation URL. |
| 707 | * |
| 708 | * @since 1.10.0 |
| 709 | * |
| 710 | * @return string |
| 711 | */ |
| 712 | public function get_documentation_url() { |
| 713 | return 'https://docs.woocommerce.com/document/facebook-for-woocommerce/'; |
| 714 | } |
| 715 | |
| 716 | |
| 717 | /** |
| 718 | * Gets the plugin's support URL. |
| 719 | * |
| 720 | * @since 1.10.0 |
| 721 | * |
| 722 | * @return string |
| 723 | */ |
| 724 | public function get_support_url() { |
| 725 | return 'https://wordpress.org/support/plugin/facebook-for-woocommerce/'; |
| 726 | } |
| 727 | |
| 728 | |
| 729 | /** |
| 730 | * Gets the plugin's sales page URL. |
| 731 | * |
| 732 | * @since 1.10.0 |
| 733 | * |
| 734 | * @return string |
| 735 | */ |
| 736 | public function get_sales_page_url() { |
| 737 | return 'https://woocommerce.com/products/facebook/'; |
| 738 | } |
| 739 | |
| 740 | |
| 741 | /** |
| 742 | * Gets the plugin's reviews URL. |
| 743 | * |
| 744 | * @since 1.10.0 |
| 745 | * |
| 746 | * @return string |
| 747 | */ |
| 748 | public function get_reviews_url() { |
| 749 | return 'https://wordpress.org/support/plugin/facebook-for-woocommerce/reviews/'; |
| 750 | } |
| 751 | |
| 752 | |
| 753 | /** |
| 754 | * Gets the plugin name. |
| 755 | * |
| 756 | * @since 1.10.0 |
| 757 | * |
| 758 | * @return string |
| 759 | */ |
| 760 | public function get_plugin_name() { |
| 761 | return __( 'Facebook for WooCommerce', 'facebook-for-woocommerce' ); |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Gets the url for the assets build directory. |
| 766 | * |
| 767 | * @since 2.3.4 |
| 768 | * |
| 769 | * @return string |
| 770 | */ |
| 771 | public function get_asset_build_dir_url() { |
| 772 | return $this->get_plugin_url() . '/assets/build'; |
| 773 | } |
| 774 | |
| 775 | |
| 776 | /** Conditional methods ***************************************************************************************/ |
| 777 | |
| 778 | |
| 779 | /** |
| 780 | * Determines if viewing the plugin settings in the admin. |
| 781 | * |
| 782 | * @since 1.10.0 |
| 783 | * |
| 784 | * @return bool |
| 785 | */ |
| 786 | public function is_plugin_settings() { |
| 787 | return is_admin() && WooCommerce\Facebook\Admin\Settings::PAGE_ID === Helper::get_requested_value( 'page' ); |
| 788 | } |
| 789 | |
| 790 | |
| 791 | /** Utility methods *******************************************************************************************/ |
| 792 | |
| 793 | |
| 794 | /** |
| 795 | * Initializes the lifecycle handler. |
| 796 | * |
| 797 | * @since 1.10.0 |
| 798 | */ |
| 799 | protected function init_lifecycle_handler() { |
| 800 | $this->lifecycle_handler = new Lifecycle( $this ); |
| 801 | } |
| 802 | |
| 803 | |
| 804 | /** |
| 805 | * Gets the plugin singleton instance. |
| 806 | * |
| 807 | * @see \facebook_for_woocommerce() |
| 808 | * |
| 809 | * @since 1.10.0 |
| 810 | * |
| 811 | * @return \WC_Facebookcommerce the plugin singleton instance |
| 812 | */ |
| 813 | public static function instance() { |
| 814 | if ( null === self::$instance ) { |
| 815 | self::$instance = new self(); |
| 816 | } |
| 817 | return self::$instance; |
| 818 | } |
| 819 | |
| 820 | |
| 821 | /** |
| 822 | * Gets the plugin file. |
| 823 | * |
| 824 | * @since 1.10.0 |
| 825 | * |
| 826 | * @return string |
| 827 | */ |
| 828 | protected function get_file() { |
| 829 | return __FILE__; |
| 830 | } |
| 831 | |
| 832 | |
| 833 | /** |
| 834 | * Return current page ID |
| 835 | * |
| 836 | * @since 2.3.0 |
| 837 | * |
| 838 | * @return string |
| 839 | */ |
| 840 | protected function get_current_page_id() { |
| 841 | $current_screen_id = ''; |
| 842 | $current_screen = get_current_screen(); |
| 843 | if ( ! empty( $current_screen ) ) { |
| 844 | $current_screen_id = $current_screen->id; |
| 845 | } |
| 846 | return $current_screen_id; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | |
| 851 | /** |
| 852 | * Gets the Facebook for WooCommerce plugin instance. |
| 853 | * |
| 854 | * @since 1.10.0 |
| 855 | * |
| 856 | * @return \WC_Facebookcommerce instance of the plugin |
| 857 | */ |
| 858 | function facebook_for_woocommerce() { |
| 859 | return \WC_Facebookcommerce::instance(); |
| 860 | } |
| 861 |