HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: /var/www/html/ielts-store/wp-content/plugins/automatewoo/includes/Jobs/WishlistItemOnSale.php
<?php

namespace AutomateWoo\Jobs;

use AutomateWoo\Clean;
use AutomateWoo\Customer_Factory;
use AutomateWoo\Exceptions\InvalidArgument;
use AutomateWoo\Jobs\Traits\ValidateItemAsIntegerId;
use AutomateWoo\Traits\ArrayValidator;
use AutomateWoo\Triggers;
use AutomateWoo\Wishlists;
use Exception;
use RuntimeException;

defined( 'ABSPATH' ) || exit;

/**
 * WishlistItemOnSale job class.
 *
 * Requires a 'products' arg which contains an array of product IDs that are recently on sale.
 *
 * @since 5.1.0
 */
class WishlistItemOnSale extends AbstractBatchedActionSchedulerJob {

	use ValidateItemAsIntegerId, ArrayValidator;

	/**
	 * Get the name of the job.
	 *
	 * @return string
	 */
	public function get_name() {
		return 'wishlist_item_on_sale';
	}

	/**
	 * Get a new batch of items.
	 *
	 * If no items are returned the job will stop.
	 *
	 * @param int   $batch_number The batch number increments for each new batch in the job cycle.
	 * @param array $args         The args for this instance of the job. Args are already validated.
	 *
	 * @return int[]
	 * @throws Exception If an error occurs. The exception will be logged by ActionScheduler.
	 */
	protected function get_batch( int $batch_number, array $args ) {
		return Wishlists::get_wishlist_ids(
			$this->get_batch_size(),
			$this->get_query_offset( $batch_number )
		);
	}

	/**
	 * Process a single item.
	 *
	 * @param int   $item A single item from the get_batch() method. Expects a validated item.
	 * @param array $args The args for this instance of the job. Args are already validated.
	 *
	 * @throws Exception If an error occurs. The exception will be logged by ActionScheduler.
	 */
	protected function process_item( $item, array $args ) {
		$wishlist = Wishlists::get_wishlist( $item );
		if ( ! $wishlist ) {
			throw JobException::item_not_found();
		}

		$sale_product_ids = Clean::ids( $args['products'] );

		$trigger = Triggers::get( 'wishlist_item_goes_on_sale' );
		if ( ! $trigger ) {
			throw new RuntimeException( 'Wishlist on sale trigger not found.' );
		}

		$wishlist_items_on_sale = array_intersect( $wishlist->get_items(), $sale_product_ids );

		foreach ( $wishlist_items_on_sale as $product_id ) {
			$customer = Customer_Factory::get_by_user_id( $wishlist->get_user_id() );

			$trigger->maybe_run(
				[
					'customer' => $customer,
					'product'  => wc_get_product( $product_id ),
					'wishlist' => $wishlist,
				]
			);
		}
	}

	/**
	 * Validate the job args.
	 *
	 * @param array $args The args for this instance of the job.
	 *
	 * @throws InvalidArgument If args are invalid.
	 */
	protected function validate_args( array $args ) {
		if ( ! isset( $args['products'] ) ) {
			throw InvalidArgument::missing_required( 'products' );
		}

		$this->validate_is_non_empty_array( $args['products'] );
	}

}