job = $job; } /** * Gets an attribute from the underlying job object. * * @since 2.0.0 * * @param string $attr_name the attribute name * @param mixed $default_value value if attribute is not found * @return mixed */ protected function get_attr( $attr_name, $default_value = null ) { return isset( $this->job->$attr_name ) ? $this->job->$attr_name : $default_value; } /** * Sets an attribute on the underlying job object. * * @since 2.0.0 * * @param string $attr_name the attribute name * @param mixed $attr_value the attribute value * @param bool $update whether to update the job object (defaults to true) */ protected function set_attr( $attr_name, $attr_value, $update = true ) { $this->job->$attr_name = $attr_value; if ( true === $update ) { wc_square()->get_background_job_handler()->update_job( $this->job ); } } /** * Checks if the job is currently locked. * * @since 2.0.0 * * @return bool */ protected function is_job_locked() { return (bool) $this->get_attr( 'locked' ); } /** * Locks the job. * * @since 2.0.0 */ protected function lock_job() { $this->set_attr( 'locked', true ); } /** * Unlocks the job. * * @since 2.0.0 */ protected function unlock_job() { $this->set_attr( 'locked', false ); } /** * Executes the job. * * Child implementation should override this method with their own job processing logic. * * @since 2.0.0 * * @return \stdClass the job object */ public function run() { wp_set_current_user( $this->get_attr( 'created_by' ) ); // phpcs:ignore Generic.PHP.ForbiddenFunctions.Discouraged -- required for background job processing if ( ! defined( 'DOING_SQUARE_SYNC' ) || false === DOING_SQUARE_SYNC ) { define( 'DOING_SQUARE_SYNC', true ); } return $this->job; } /** * Completes the job. * * @since 2.0.0 * * @return \stdClass the job object */ protected function complete() { return $this->job = wc_square()->get_background_job_handler()->complete_job( $this->job ); } /** * Fails the job. * * @since 2.0.0 * * @param string $reason failure reason message (optional) * @return \stdClass the job object */ protected function fail( $reason = '' ) { if ( ! empty( $reason ) ) { wc_square()->log( $reason ); } return $this->job = wc_square()->get_background_job_handler()->fail_job( $this->job, $reason ); } /** * Checks if this job uses WooCommerce as the SOR. * * @since 2.0.0 * * @return bool */ public function is_system_of_record_woocommerce() { return 'woocommerce' === $this->get_attr( 'system_of_record' ); } /** * Checks if this is job uses square as the SOR. * * @since 2.0.0 * * @return bool */ public function is_system_of_record_square() { return 'square' === $this->get_attr( 'system_of_record' ); } }