Integrations
6 years ago
Products
6 years ago
Utilities
6 years ago
test
6 years ago
AJAX.php
6 years ago
Admin.php
6 years ago
Lifecycle.php
6 years ago
Products.php
6 years ago
fbasync.php
6 years ago
fbbackground.php
6 years ago
fbgraph.php
6 years ago
fbinfobanner.php
6 years ago
fbproduct.php
6 years ago
fbproductfeed.php
6 years ago
fbutils.php
6 years ago
fbwpml.php
6 years ago
Lifecycle.php
238 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved |
| 4 | * |
| 5 | * This source code is licensed under the license found in the |
| 6 | * LICENSE file in the root directory of this source tree. |
| 7 | * |
| 8 | * @package FacebookCommerce |
| 9 | */ |
| 10 | |
| 11 | namespace SkyVerge\WooCommerce\Facebook; |
| 12 | |
| 13 | use SkyVerge\WooCommerce\PluginFramework\v5_5_4 as Framework; |
| 14 | |
| 15 | defined( 'ABSPATH' ) or exit; |
| 16 | |
| 17 | /** |
| 18 | * The Facebook for WooCommerce plugin lifecycle handler. |
| 19 | * |
| 20 | * @since 1.10.0 |
| 21 | * |
| 22 | * @method \WC_Facebookcommerce get_plugin() |
| 23 | */ |
| 24 | class Lifecycle extends Framework\Plugin\Lifecycle { |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * Lifecycle constructor. |
| 29 | * |
| 30 | * @since 1.10.0 |
| 31 | * |
| 32 | * @param Framework\SV_WC_Plugin $plugin |
| 33 | */ |
| 34 | public function __construct( $plugin ) { |
| 35 | |
| 36 | parent::__construct( $plugin ); |
| 37 | |
| 38 | $this->upgrade_versions = [ |
| 39 | '1.10.0', |
| 40 | '1.10.1', |
| 41 | '1.11.0', |
| 42 | '1.11.3', |
| 43 | ]; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * Migrates options from previous versions of the plugin, which did not use the Framework. |
| 49 | * |
| 50 | * @since 1.10.0 |
| 51 | */ |
| 52 | protected function install() { |
| 53 | |
| 54 | /** |
| 55 | * Versions prior to 1.10.0 did not set a version option, so the upgrade method needs to be called manually. |
| 56 | * We do this by checking first if an old option exists, but a new one doesn't. |
| 57 | */ |
| 58 | if ( get_option( 'woocommerce_facebookcommerce_settings' ) && ! get_option( 'wc_facebook_page_access_token' ) ) { |
| 59 | |
| 60 | $this->upgrade( '1.9.15' ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Upgrades to version 1.10.0. |
| 67 | * |
| 68 | * @since 1.10.0 |
| 69 | */ |
| 70 | protected function upgrade_to_1_10_0() { |
| 71 | |
| 72 | $this->migrate_1_9_settings(); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Migrates Facebook for WooCommerce options used in version 1.9.x to the options and settings used in 1.10.x. |
| 78 | * |
| 79 | * Some users who upgraded from 1.9.x to 1.10.0 ended up with an incomplete upgrade and could have configured the plugin from scratch after that. |
| 80 | * This routine will update the options and settings only if a previous value does not exists. |
| 81 | * |
| 82 | * @since 1.10.1 |
| 83 | */ |
| 84 | private function migrate_1_9_settings() { |
| 85 | |
| 86 | $values = get_option( 'woocommerce_facebookcommerce_settings', [] ); |
| 87 | |
| 88 | // preserve legacy values |
| 89 | if ( false === get_option( 'woocommerce_facebookcommerce_legacy_settings' ) ) { |
| 90 | update_option( 'woocommerce_facebookcommerce_legacy_settings', $values ); |
| 91 | } |
| 92 | |
| 93 | // migrate options from woocommerce_facebookcommerce_settings |
| 94 | $options = [ |
| 95 | 'fb_api_key' => \WC_Facebookcommerce_Integration::OPTION_PAGE_ACCESS_TOKEN, |
| 96 | 'fb_product_catalog_id' => \WC_Facebookcommerce_Integration::OPTION_PRODUCT_CATALOG_ID, |
| 97 | 'fb_external_merchant_settings_id' => \WC_Facebookcommerce_Integration::OPTION_EXTERNAL_MERCHANT_SETTINGS_ID, |
| 98 | 'fb_feed_id' => \WC_Facebookcommerce_Integration::OPTION_FEED_ID, |
| 99 | 'facebook_jssdk_version' => \WC_Facebookcommerce_Integration::OPTION_JS_SDK_VERSION, |
| 100 | 'pixel_install_time' => \WC_Facebookcommerce_Integration::OPTION_PIXEL_INSTALL_TIME, |
| 101 | ]; |
| 102 | |
| 103 | foreach ( $options as $old_index => $new_option_name ) { |
| 104 | |
| 105 | if ( isset( $values[ $old_index ] ) && false === get_option( $new_option_name ) ) { |
| 106 | |
| 107 | $new_value = $values[ $old_index ]; |
| 108 | |
| 109 | if ( 'pixel_install_time' === $old_index ) { |
| 110 | |
| 111 | // convert to UTC timestamp |
| 112 | try { |
| 113 | $pixel_install_time = \DateTime::createFromFormat( 'Y-m-d G:i:s', $new_value, new \DateTimeZone( wc_timezone_string() ) ); |
| 114 | } catch ( \Exception $e ) { |
| 115 | $pixel_install_time = false; |
| 116 | } |
| 117 | |
| 118 | $new_value = $pixel_install_time instanceof \DateTime ? $pixel_install_time->getTimestamp() : null; |
| 119 | } |
| 120 | |
| 121 | update_option( $new_option_name, $new_value ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | $new_settings = get_option( 'woocommerce_' . \WC_Facebookcommerce::INTEGRATION_ID . '_settings', [] ); |
| 126 | |
| 127 | // migrate settings from woocommerce_facebookcommerce_settings |
| 128 | $settings = [ |
| 129 | 'fb_page_id' => \WC_Facebookcommerce_Integration::SETTING_FACEBOOK_PAGE_ID, |
| 130 | 'fb_pixel_id' => \WC_Facebookcommerce_Integration::SETTING_FACEBOOK_PIXEL_ID, |
| 131 | 'fb_pixel_use_pii' => \WC_Facebookcommerce_Integration::SETTING_ENABLE_ADVANCED_MATCHING, |
| 132 | 'is_messenger_chat_plugin_enabled' => \WC_Facebookcommerce_Integration::SETTING_ENABLE_MESSENGER, |
| 133 | 'msger_chat_customization_locale' => \WC_Facebookcommerce_Integration::SETTING_MESSENGER_LOCALE, |
| 134 | 'msger_chat_customization_greeting_text_code' => \WC_Facebookcommerce_Integration::SETTING_MESSENGER_GREETING, |
| 135 | 'msger_chat_customization_theme_color_code' => \WC_Facebookcommerce_Integration::SETTING_MESSENGER_COLOR_HEX, |
| 136 | ]; |
| 137 | |
| 138 | foreach ( $settings as $old_index => $new_index ) { |
| 139 | |
| 140 | if ( isset( $values[ $old_index ] ) && ! isset( $new_settings[ $new_index ] ) ) { |
| 141 | $new_settings[ $new_index ] = $values[ $old_index ]; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // migrate settings from standalone options |
| 146 | if ( ! isset( $new_settings[ \WC_Facebookcommerce_Integration::SETTING_ENABLE_PRODUCT_SYNC ] ) ) { |
| 147 | |
| 148 | $product_sync_enabled = empty( get_option( 'fb_disable_sync_on_dev_environment', 0 ) ); |
| 149 | |
| 150 | $new_settings[ \WC_Facebookcommerce_Integration::SETTING_ENABLE_PRODUCT_SYNC ] = $product_sync_enabled ? 'yes' : 'no'; |
| 151 | } |
| 152 | |
| 153 | if ( ! isset( $new_settings[ \WC_Facebookcommerce_Integration::SETTING_PRODUCT_DESCRIPTION_MODE ] ) ) { |
| 154 | $new_settings[ \WC_Facebookcommerce_Integration::SETTING_PRODUCT_DESCRIPTION_MODE ] = ! empty( get_option( 'fb_sync_short_description', 0 ) ) ? \WC_Facebookcommerce_Integration::PRODUCT_DESCRIPTION_MODE_SHORT : \WC_Facebookcommerce_Integration::PRODUCT_DESCRIPTION_MODE_STANDARD; |
| 155 | } |
| 156 | |
| 157 | if ( ! isset( $new_settings[ \WC_Facebookcommerce_Integration::SETTING_SCHEDULED_RESYNC_OFFSET ] ) ) { |
| 158 | |
| 159 | $autosync_time = get_option( 'woocommerce_fb_autosync_time' ); |
| 160 | $parsed_time = ! empty( $autosync_time ) ? strtotime( $autosync_time ) : false; |
| 161 | $resync_offset = null; |
| 162 | |
| 163 | if ( false !== $parsed_time ) { |
| 164 | |
| 165 | $midnight = ( new \DateTime() )->setTimestamp( $parsed_time )->setTime( 0, 0, 0 ); |
| 166 | |
| 167 | $resync_offset = $parsed_time - $midnight->getTimestamp(); |
| 168 | } |
| 169 | |
| 170 | $new_settings[ \WC_Facebookcommerce_Integration::SETTING_SCHEDULED_RESYNC_OFFSET ] = $resync_offset; |
| 171 | } |
| 172 | |
| 173 | // maybe remove old settings entries |
| 174 | $old_indexes = array_merge( array_keys( $options ), array_keys( $settings ), [ 'fb_settings_heading', 'fb_upload_id', 'upload_end_time' ] ); |
| 175 | |
| 176 | foreach ( $old_indexes as $old_index ) { |
| 177 | unset( $new_settings[ $old_index ] ); |
| 178 | } |
| 179 | |
| 180 | update_option( 'woocommerce_' . \WC_Facebookcommerce::INTEGRATION_ID . '_settings', $new_settings ); |
| 181 | |
| 182 | // schedule the next product resync action |
| 183 | if ( $new_settings[ \WC_Facebookcommerce_Integration::SETTING_SCHEDULED_RESYNC_OFFSET ] && 'yes' === $new_settings[ \WC_Facebookcommerce_Integration::SETTING_ENABLE_PRODUCT_SYNC ] ) { |
| 184 | |
| 185 | $integration = $this->get_plugin()->get_integration(); |
| 186 | |
| 187 | if ( ! $integration->is_resync_scheduled() ) { |
| 188 | $integration->schedule_resync( $new_settings[ \WC_Facebookcommerce_Integration::SETTING_SCHEDULED_RESYNC_OFFSET ] ); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | |
| 194 | /** |
| 195 | * Upgrades to version 1.10.1. |
| 196 | * |
| 197 | * @since 1.10.1 |
| 198 | */ |
| 199 | protected function upgrade_to_1_10_1() { |
| 200 | |
| 201 | $this->migrate_1_9_settings(); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | /** |
| 206 | * Upgrades to version 1.11.0. |
| 207 | * |
| 208 | * @since 1.11.0 |
| 209 | */ |
| 210 | protected function upgrade_to_1_11_0() { |
| 211 | |
| 212 | $settings = get_option( 'woocommerce_' . \WC_Facebookcommerce::INTEGRATION_ID . '_settings', [] ); |
| 213 | |
| 214 | // moves the upload ID to a standalone option |
| 215 | if ( ! empty( $settings['fb_upload_id'] ) ) { |
| 216 | $this->get_plugin()->get_integration()->update_upload_id( $settings['fb_upload_id'] ); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /** |
| 222 | * Upgrades to version 1.11.3. |
| 223 | * |
| 224 | * @since 1.11.3-dev.2 |
| 225 | */ |
| 226 | protected function upgrade_to_1_11_3() { |
| 227 | |
| 228 | if ( $handler = $this->get_plugin()->get_background_disable_virtual_products_sync_instance() ) { |
| 229 | |
| 230 | // create_job() expects an non-empty array of attributes |
| 231 | $handler->create_job( [ 'created_at' => current_time( 'mysql' ) ] ); |
| 232 | $handler->dispatch(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | } |
| 238 |