Concerns
5 months ago
Jobs
1 year ago
Tasks
1 year ago
WooCommerce
5 days ago
BatchCheckService.php
6 months ago
ContentSyncService.php
6 months ago
CustomerSyncService.php
1 year ago
ImportState.php
1 month ago
PostSyncService.php
2 months ago
ProductSyncService.php
1 year ago
ProductsSyncService.php
1 year ago
StoreSyncService.php
1 year ago
SyncService.php
1 month ago
SyncServiceProvider.php
1 month ago
StoreSyncService.php
87 lines
| 1 | <?php |
| 2 | namespace SureCart\Sync; |
| 3 | |
| 4 | /** |
| 5 | * The store sync service. |
| 6 | * |
| 7 | * This service is responsible for syncing the store |
| 8 | * data when the account id is first set or changed. |
| 9 | * |
| 10 | * @since 3.0.0 |
| 11 | * @package SureCart\Sync |
| 12 | */ |
| 13 | class StoreSyncService { |
| 14 | /** |
| 15 | * The option name for the stored account. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | protected $option_name = 'surecart_current_account_id'; |
| 20 | |
| 21 | /** |
| 22 | * Bootstrap the service. |
| 23 | * |
| 24 | * @return mixed |
| 25 | */ |
| 26 | public function bootstrap() { |
| 27 | add_action( 'admin_init', array( $this, 'maybeStartSync' ) ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Retrieves the stored account from the WordPress options. |
| 32 | * |
| 33 | * @return string The stored account. |
| 34 | */ |
| 35 | public function getStoredAccount() { |
| 36 | return get_option( $this->option_name, null ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Retrieves the current plugin account. |
| 41 | * |
| 42 | * @return string The current plugin account. |
| 43 | */ |
| 44 | public function currentAccountId() { |
| 45 | return \SureCart::account()->id ?? ''; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Flush the rewrite rules on account change. |
| 50 | * |
| 51 | * @return boolean |
| 52 | */ |
| 53 | public function maybeStartSync() { |
| 54 | $current_account_id = $this->currentAccountId(); |
| 55 | $stored_account_id = $this->getStoredAccount(); |
| 56 | |
| 57 | // there is no current account id, or the stored account id is the same as the current account id. |
| 58 | if ( empty( $current_account_id ) || $current_account_id === $stored_account_id ) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | return $this->startSync(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Flushes the rewrite rules and updates the stored account. |
| 67 | * |
| 68 | * @return boolean |
| 69 | */ |
| 70 | public function startSync() { |
| 71 | $this->sync(); |
| 72 | return update_option( $this->option_name, $this->currentAccountId(), false ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Sync the store data. |
| 77 | * |
| 78 | * @return mixed |
| 79 | */ |
| 80 | public function sync() { |
| 81 | if ( \SureCart::sync()->products()->isActive() ) { |
| 82 | return false; |
| 83 | } |
| 84 | return \SureCart::sync()->products()->dispatch(); |
| 85 | } |
| 86 | } |
| 87 |