Records
6 years ago
Catalog_Item.php
6 years ago
Interval_Polling.php
6 years ago
Job.php
6 years ago
Manual_Synchronization.php
6 years ago
Product_Import.php
5 years ago
Records.php
6 years ago
Stepped_Job.php
6 years ago
Job.php
224 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 |
| 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 | * @author WooCommerce |
| 20 | * @copyright Copyright: (c) 2019, Automattic, Inc. |
| 21 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square\Sync; |
| 25 | |
| 26 | use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework; |
| 27 | |
| 28 | defined( 'ABSPATH' ) || exit; |
| 29 | |
| 30 | /** |
| 31 | * Synchronization Job abstract. |
| 32 | * |
| 33 | * Synchronization jobs should extend this parent method with their own synchronization logic. |
| 34 | * @see \WooCommerce\Square\Handlers\Background_Job main handler should be responsible to set and create all Square background jobs |
| 35 | * |
| 36 | * @since 2.0.0 |
| 37 | */ |
| 38 | class Job { |
| 39 | |
| 40 | |
| 41 | /** @var \stdClass background job object */ |
| 42 | protected $job; |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Synchronization job constructor. |
| 47 | * |
| 48 | * @since 2.0.0 |
| 49 | * |
| 50 | * @param null|\stdClass $job background synchronization job object |
| 51 | */ |
| 52 | public function __construct( $job = null ) { |
| 53 | |
| 54 | if ( null === $job ) { |
| 55 | $job = new \stdClass(); |
| 56 | } |
| 57 | |
| 58 | $this->job = $job; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Gets an attribute from the underlying job object. |
| 64 | * |
| 65 | * @since 2.0.0 |
| 66 | * |
| 67 | * @param string $attr_name the attribute name |
| 68 | * @param mixed $default_value value if attribute is not found |
| 69 | * @return mixed |
| 70 | */ |
| 71 | protected function get_attr( $attr_name, $default_value = null ) { |
| 72 | |
| 73 | return isset( $this->job->$attr_name ) ? $this->job->$attr_name : $default_value; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * Sets an attribute on the underlying job object. |
| 79 | * |
| 80 | * @since 2.0.0 |
| 81 | * |
| 82 | * @param string $attr_name the attribute name |
| 83 | * @param mixed $attr_value the attribute value |
| 84 | * @param bool $update whether to update the job object (defaults to true) |
| 85 | */ |
| 86 | protected function set_attr( $attr_name, $attr_value, $update = true ) { |
| 87 | |
| 88 | $this->job->$attr_name = $attr_value; |
| 89 | |
| 90 | if ( true === $update ) { |
| 91 | wc_square()->get_background_job_handler()->update_job( $this->job ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Checks if the job is currently locked. |
| 98 | * |
| 99 | * @since 2.0.0 |
| 100 | * |
| 101 | * @return bool |
| 102 | */ |
| 103 | protected function is_job_locked() { |
| 104 | |
| 105 | return (bool) $this->get_attr( 'locked' ); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * Locks the job. |
| 111 | * |
| 112 | * @since 2.0.0 |
| 113 | */ |
| 114 | protected function lock_job() { |
| 115 | |
| 116 | $this->set_attr( 'locked', true ); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * Unlocks the job. |
| 122 | * |
| 123 | * @since 2.0.0 |
| 124 | */ |
| 125 | protected function unlock_job() { |
| 126 | |
| 127 | $this->set_attr( 'locked', false ); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Executes the job. |
| 133 | * |
| 134 | * Child implementation should override this method with their own job processing logic. |
| 135 | * |
| 136 | * @since 2.0.0 |
| 137 | * |
| 138 | * @return \stdClass the job object |
| 139 | */ |
| 140 | public function run() { |
| 141 | wp_set_current_user( $this->get_attr( 'created_by' ) ); |
| 142 | |
| 143 | if ( ! defined( 'DOING_SQUARE_SYNC' ) || false === DOING_SQUARE_SYNC ) { |
| 144 | define( 'DOING_SQUARE_SYNC', true ); |
| 145 | } |
| 146 | |
| 147 | return $this->job; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * Completes the job. |
| 153 | * |
| 154 | * @since 2.0.0 |
| 155 | * |
| 156 | * @return \stdClass the job object |
| 157 | */ |
| 158 | protected function complete() { |
| 159 | |
| 160 | return $this->job = wc_square()->get_background_job_handler()->complete_job( $this->job ); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * Fails the job. |
| 166 | * |
| 167 | * @since 2.0.0 |
| 168 | * |
| 169 | * @param string $reason failure reason message (optional) |
| 170 | * @return \stdClass the job object |
| 171 | */ |
| 172 | protected function fail( $reason = '' ) { |
| 173 | |
| 174 | if ( ! empty( $reason ) ) { |
| 175 | wc_square()->log( $reason ); |
| 176 | } |
| 177 | |
| 178 | return $this->job = wc_square()->get_background_job_handler()->fail_job( $this->job, $reason ); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /** |
| 183 | * Checks if this job uses WooCommerce as the SOR. |
| 184 | * |
| 185 | * @since 2.0.0 |
| 186 | * |
| 187 | * @return bool |
| 188 | */ |
| 189 | public function is_system_of_record_woocommerce() { |
| 190 | |
| 191 | return 'woocommerce' === $this->get_attr( 'system_of_record' ); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /** |
| 196 | * Checks if this is job uses square as the SOR. |
| 197 | * |
| 198 | * @since 2.0.0 |
| 199 | * |
| 200 | * @return bool |
| 201 | */ |
| 202 | public function is_system_of_record_square() { |
| 203 | |
| 204 | return 'square' === $this->get_attr( 'system_of_record' ); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * Checks if a sensible time has been exceeded for this request. |
| 210 | * |
| 211 | * Convenience method for accessing the background job handler. |
| 212 | * |
| 213 | * @since 2.0.0 |
| 214 | * |
| 215 | * @return bool |
| 216 | */ |
| 217 | protected function is_time_exceeded() { |
| 218 | |
| 219 | return wc_square()->get_background_job_handler()->is_time_exceeded(); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | } |
| 224 |