CollectionSyncService.php
1 year ago
CustomerSyncService.php
1 year ago
PostSyncService.php
1 year ago
ProductSyncService.php
1 year ago
ProductsSyncProcess.php
1 year ago
ProductsSyncService.php
1 year ago
StoreSyncService.php
1 year ago
SyncService.php
1 year ago
SyncServiceProvider.php
1 year ago
ProductsSyncService.php
142 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Sync; |
| 4 | |
| 5 | /** |
| 6 | * Syncs customer records to WordPress users. |
| 7 | */ |
| 8 | class ProductsSyncService { |
| 9 | /** |
| 10 | * Application instance. |
| 11 | * |
| 12 | * @var \SureCart\Application |
| 13 | */ |
| 14 | protected $app = null; |
| 15 | |
| 16 | /** |
| 17 | * The notice id. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $notice_id = 'surecart_products_sync'; |
| 22 | |
| 23 | /** |
| 24 | * Constructor. |
| 25 | * |
| 26 | * @param \SureCart\Application $app The application. |
| 27 | */ |
| 28 | public function __construct( $app ) { |
| 29 | $this->app = $app; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Bootstrap the service. |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public function bootstrap() { |
| 38 | add_action( 'admin_notices', [ $this, 'showMigrationNotice' ] ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get the queue process. |
| 43 | * |
| 44 | * @return ProductsQueueProcess |
| 45 | */ |
| 46 | public function queue() { |
| 47 | return $this->app->resolve( 'surecart.process.products.queue' ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get the sync process. |
| 52 | * |
| 53 | * @return ProductsSyncProcess |
| 54 | */ |
| 55 | public function sync() { |
| 56 | return $this->app->resolve( 'surecart.process.products.sync' ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Cancel the process. |
| 61 | * |
| 62 | * This cancels the queue and sync processes |
| 63 | * and also deletes all the queue and sync items. |
| 64 | * |
| 65 | * @return void |
| 66 | */ |
| 67 | public function cancel() { |
| 68 | $this->queue()->cancel(); |
| 69 | $this->queue()->delete_all(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Is this process active? |
| 74 | * |
| 75 | * @return boolean |
| 76 | */ |
| 77 | public function isActive() { |
| 78 | if ( $this->queue()->is_active() ) { |
| 79 | return 'queue'; |
| 80 | } |
| 81 | |
| 82 | if ( \SureCart::queue()->showNotice( 'surecart/sync/product' ) ) { |
| 83 | return 'sync'; |
| 84 | } |
| 85 | |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Show the migration notice. |
| 91 | * |
| 92 | * @return void |
| 93 | */ |
| 94 | public function showMigrationNotice() { |
| 95 | if ( ! $this->isActive() ) { |
| 96 | return; |
| 97 | } |
| 98 | echo wp_kses_post( |
| 99 | \SureCart::notices()->render( |
| 100 | [ |
| 101 | 'name' => $this->notice_id, |
| 102 | 'type' => 'info', |
| 103 | 'title' => esc_html__( 'SureCart: Getting things ready...', 'surecart' ), |
| 104 | 'text' => wp_sprintf( |
| 105 | '<p>%s</p>', |
| 106 | esc_html__( 'We are getting things ready and optimized in the background. This may take a few minutes.', 'surecart' ), |
| 107 | ), |
| 108 | ] |
| 109 | ) |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Start the sync process. |
| 115 | * |
| 116 | * @param boolean $args The arguments. |
| 117 | * |
| 118 | * @return array|WP_Error The response or WP_Error on failure. |
| 119 | */ |
| 120 | public function dispatch( $args = [] ) { |
| 121 | $args = wp_parse_args( |
| 122 | $args, |
| 123 | [ |
| 124 | 'page' => 1, |
| 125 | 'per_page' => 25, |
| 126 | ] |
| 127 | ); |
| 128 | |
| 129 | // cancel previous processes. |
| 130 | $this->cancel(); |
| 131 | |
| 132 | // reset the notice. |
| 133 | \SureCart::notices()->reset( $this->notice_id ); |
| 134 | |
| 135 | // clear account cache. |
| 136 | \SureCart::account()->clearCache(); |
| 137 | |
| 138 | // save and dispatch the process. |
| 139 | return $this->queue()->push_to_queue( $args )->save()->dispatch(); |
| 140 | } |
| 141 | } |
| 142 |