Product
3 years ago
Background_Job.php
3 years ago
Category.php
3 years ago
Connection.php
3 years ago
Email.php
3 years ago
Order.php
3 years ago
Product.php
3 years ago
Products.php
3 years ago
Sync.php
3 years ago
Background_Job.php
293 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Square |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@woocommerce.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * DISCLAIMER |
| 14 | * |
| 15 | * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer |
| 16 | * versions in the future. If you wish to customize WooCommerce Square for your |
| 17 | * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/ |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | namespace WooCommerce\Square\Handlers; |
| 22 | |
| 23 | use WooCommerce\Square\Framework\Utilities\Background_Job_Handler; |
| 24 | use WooCommerce\Square\Sync\Job; |
| 25 | use WooCommerce\Square\Sync\Records; |
| 26 | use WooCommerce\Square\Sync\Interval_Polling; |
| 27 | use WooCommerce\Square\Sync\Manual_Synchronization; |
| 28 | use WooCommerce\Square\Sync\Product_Import; |
| 29 | |
| 30 | defined( 'ABSPATH' ) || exit; |
| 31 | |
| 32 | /** |
| 33 | * Product and Inventory Synchronization handler class. |
| 34 | * |
| 35 | * This class handles manual and interval synchronization jobs. |
| 36 | * It is a wrapper for the framework background handler and as such it only handles loopback business to keep the queue processing. |
| 37 | * See the individual job implementations: |
| 38 | * |
| 39 | * @see Manual_Synchronization manual jobs re-process ALL synced products |
| 40 | * @see Interval_Polling interval (polling) jobs perform API requests for ONLY the latest changes and update the associated products |
| 41 | * |
| 42 | * @since 2.0.0 |
| 43 | */ |
| 44 | class Background_Job extends Background_Job_Handler { |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * Initializes the background sync handler. |
| 49 | * |
| 50 | * @since 2.0.0 |
| 51 | */ |
| 52 | public function __construct() { |
| 53 | |
| 54 | $this->prefix = 'wc_square'; |
| 55 | $this->action = 'background_sync'; |
| 56 | $this->data_key = 'product_ids'; |
| 57 | |
| 58 | parent::__construct(); |
| 59 | |
| 60 | add_action( "{$this->identifier}_job_complete", array( $this, 'job_complete' ) ); |
| 61 | add_action( "{$this->identifier}_job_failed", array( $this, 'job_failed' ) ); |
| 62 | add_filter( 'woocommerce_debug_tools', array( $this, 'add_debug_tool' ) ); |
| 63 | add_action( 'wc_square_job_runner', array( $this, 'handle' ) ); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Creates a new job. |
| 69 | * |
| 70 | * @since 2.0.0 |
| 71 | * |
| 72 | * @param array $attrs array of job attributes |
| 73 | * @return \stdClass|null |
| 74 | */ |
| 75 | public function create_job( $attrs ) { |
| 76 | |
| 77 | $sor = wc_square()->get_settings_handler()->get_system_of_record(); |
| 78 | |
| 79 | return parent::create_job( |
| 80 | wp_parse_args( |
| 81 | $attrs, |
| 82 | array( |
| 83 | 'action' => '', // job action |
| 84 | 'catalog_processed' => false, // whether the Square catalog has been processed |
| 85 | 'cursor' => '', // job advancement position |
| 86 | 'manual' => false, // whether it's a sync job triggered manually |
| 87 | 'percentage' => 0, // percentage completed |
| 88 | 'product_ids' => array(), // products to process |
| 89 | 'processed_product_ids' => array(), // newly imported products processed |
| 90 | 'updated_product_ids' => array(), // updated products processed |
| 91 | 'skipped_products' => array(), // remote product IDs that were skipped |
| 92 | 'system_of_record' => $sor, // Sync setting used |
| 93 | ) |
| 94 | ) |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * Handles job execution. |
| 101 | * |
| 102 | * Overridden to support our multi-step job structure. There are steps that can take a long time to process, so this |
| 103 | * ensures only one step is performed for each background request. |
| 104 | * |
| 105 | * @since 2.0.0 |
| 106 | */ |
| 107 | public function handle() { |
| 108 | |
| 109 | $this->lock_process(); |
| 110 | |
| 111 | // Get next job in the queue |
| 112 | $job = $this->get_job(); |
| 113 | |
| 114 | // handle PHP errors from here on out |
| 115 | register_shutdown_function( array( $this, 'handle_shutdown' ), $job ); |
| 116 | |
| 117 | // Start processing |
| 118 | $this->process_job( $job ); |
| 119 | |
| 120 | $this->unlock_process(); |
| 121 | |
| 122 | // Start next job or complete process |
| 123 | if ( ! $this->is_queue_empty() ) { |
| 124 | as_enqueue_async_action( 'wc_square_job_runner' ); |
| 125 | } else { |
| 126 | $this->complete(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Processes a background job. |
| 133 | * |
| 134 | * @since 2.0.0 |
| 135 | * |
| 136 | * @param object|\stdClass $job |
| 137 | * @param null $items_per_batch |
| 138 | * @return false|object|\stdClass |
| 139 | */ |
| 140 | public function process_job( $job, $items_per_batch = null ) { |
| 141 | |
| 142 | if ( ! $job ) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | // indicate that the job has started processing |
| 147 | if ( 'processing' !== $job->status ) { |
| 148 | |
| 149 | $job->status = 'processing'; |
| 150 | $job->started_processing_at = current_time( 'mysql' ); |
| 151 | $job = $this->update_job( $job ); |
| 152 | } |
| 153 | |
| 154 | if ( 'poll' === $job->action ) { |
| 155 | |
| 156 | $job = new Interval_Polling( $job ); |
| 157 | |
| 158 | } elseif ( 'product_import' === $job->action ) { |
| 159 | |
| 160 | $job = new Product_Import( $job ); |
| 161 | |
| 162 | } elseif ( ! empty( $job->manual ) ) { |
| 163 | |
| 164 | $job = new Manual_Synchronization( $job ); |
| 165 | } |
| 166 | |
| 167 | if ( $job instanceof Job ) { |
| 168 | $current_user_id = get_current_user_id(); |
| 169 | $job = $job->run(); |
| 170 | wp_set_current_user( $current_user_id ); |
| 171 | } |
| 172 | |
| 173 | return $job; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * Handles actions after a sync job is complete. |
| 179 | * |
| 180 | * @since 2.0.0 |
| 181 | * |
| 182 | * @param $job |
| 183 | */ |
| 184 | public function job_complete( $job ) { |
| 185 | |
| 186 | wc_square()->get_sync_handler()->set_last_synced_at(); |
| 187 | |
| 188 | wc_square()->get_sync_handler()->record_sync( $job->processed_product_ids, $job ); |
| 189 | |
| 190 | wc_square()->get_email_handler()->get_sync_completed_email()->trigger( $job ); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | /** |
| 195 | * Handles actions after a sync job has failed. |
| 196 | * |
| 197 | * @since 2.0.0 |
| 198 | * |
| 199 | * @param $job |
| 200 | */ |
| 201 | public function job_failed( $job ) { |
| 202 | |
| 203 | Records::set_record( |
| 204 | array( |
| 205 | 'type' => 'alert', |
| 206 | 'message' => 'Sync failed. Please try again', |
| 207 | ) |
| 208 | ); |
| 209 | |
| 210 | wc_square()->get_email_handler()->get_sync_completed_email()->trigger( $job ); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /** |
| 215 | * No-op: implements framework parent abstract method. |
| 216 | * |
| 217 | * @since 2.0.0 |
| 218 | * |
| 219 | * @param null $item |
| 220 | * @param \stdClass $job |
| 221 | */ |
| 222 | protected function process_item( $item, $job ) {} |
| 223 | |
| 224 | /** |
| 225 | * Adds some helpful debug tools. |
| 226 | * |
| 227 | * @since 2.0.0 |
| 228 | * |
| 229 | * @param array $tools existing debug tools |
| 230 | * @return array |
| 231 | */ |
| 232 | public function add_debug_tool( $tools ) { |
| 233 | |
| 234 | // this key is not unique to the plugin to avoid duplicate tools |
| 235 | $tools['wc_square_clear_background_jobs'] = array( |
| 236 | 'name' => __( 'Clear Square Sync', 'woocommerce-square' ), |
| 237 | 'button' => __( 'Clear', 'woocommerce-square' ), |
| 238 | 'desc' => __( 'This tool will clear any ongoing Square product syncs.', 'woocommerce-square' ), |
| 239 | 'callback' => array( $this, 'run_clear_background_jobs' ), |
| 240 | ); |
| 241 | |
| 242 | return $tools; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /** |
| 247 | * Clear all background jobs of any status. |
| 248 | * |
| 249 | * @since 2.0.0 |
| 250 | */ |
| 251 | public function clear_all_jobs() { |
| 252 | |
| 253 | $jobs = $this->get_jobs(); |
| 254 | |
| 255 | if ( is_array( $jobs ) ) { |
| 256 | $this->delete_jobs( $jobs ); |
| 257 | } |
| 258 | |
| 259 | delete_transient( 'wc_square_background_sync_process_lock' ); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /** |
| 264 | * Deletes a set of background jobs. |
| 265 | * |
| 266 | * @since 2.0.0 |
| 267 | * |
| 268 | * @param object[] $jobs jobs to delete |
| 269 | */ |
| 270 | public function delete_jobs( $jobs ) { |
| 271 | |
| 272 | foreach ( $jobs as $job ) { |
| 273 | $this->delete_job( $job ); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Runs the "Clear Square Sync" tool. |
| 279 | * |
| 280 | * Provides a way for merchants to clear any ongoing or stuck product syncs. |
| 281 | * |
| 282 | * @since 2.0.0 |
| 283 | */ |
| 284 | public function run_clear_background_jobs() { |
| 285 | |
| 286 | $this->clear_all_jobs(); |
| 287 | |
| 288 | $this->debug_message = esc_html__( 'Success! You can now sync your products.', 'woocommerce-square' ); |
| 289 | |
| 290 | return true; |
| 291 | } |
| 292 | } |
| 293 |