HEX
Server: LiteSpeed
System: Linux eko108.isimtescil.net 4.18.0-477.21.1.lve.1.el8.x86_64 #1 SMP Tue Sep 5 23:08:35 UTC 2023 x86_64
User: uyarreklamcomtr (11202)
PHP: 7.4.33
Disabled: opcache_get_status
Upload Files
File: /var/www/vhosts/uyarreklam.com.tr/httpdocs/JobInitializer.php.tar
uyarreklam.com.tr/httpdocs/wp-content/plugins/google-listings-and-ads/src/Jobs/JobInitializer.php000064400000004360151550247210032146 0ustar00var/www/vhosts<?php
declare( strict_types=1 );

namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs;

use Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler\ActionSchedulerInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Conditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
use DateTime;

defined( 'ABSPATH' ) || exit;

/**
 * Class JobInitializer
 *
 * Initializes all jobs when certain conditions are met (e.g. the request is async or initiated by CRON, CLI, etc.).
 *
 * @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs
 */
class JobInitializer implements Registerable, Conditional {

	/**
	 * @var JobRepository
	 */
	protected $job_repository;

	/**
	 * @var ActionSchedulerInterface
	 */
	protected $action_scheduler;

	/**
	 * JobInitializer constructor.
	 *
	 * @param JobRepository            $job_repository
	 * @param ActionSchedulerInterface $action_scheduler
	 */
	public function __construct( JobRepository $job_repository, ActionSchedulerInterface $action_scheduler ) {
		$this->job_repository   = $job_repository;
		$this->action_scheduler = $action_scheduler;
	}

	/**
	 * Initialize all jobs.
	 */
	public function register(): void {
		foreach ( $this->job_repository->list() as $job ) {
			$job->init();

			if ( $job instanceof StartOnHookInterface ) {
				add_action(
					$job->get_start_hook()->get_hook(),
					function ( ...$args ) use ( $job ) {
						$job->schedule( $args );
					},
					10,
					$job->get_start_hook()->get_argument_count()
				);
			}

			if (
				$job instanceof RecurringJobInterface &&
				! $this->action_scheduler->has_scheduled_action( $job->get_start_hook()->get_hook() ) &&
				$job->can_schedule()
			) {

				$recurring_date_time = new DateTime( 'tomorrow 3am', wp_timezone() );
				$schedule            = '0 3 * * *'; // 3 am every day
				$this->action_scheduler->schedule_cron( $recurring_date_time->getTimestamp(), $schedule, $job->get_start_hook()->get_hook() );
			}
		}
	}

	/**
	 * Check whether this object is currently needed.
	 *
	 * @return bool Whether the object is needed.
	 */
	public static function is_needed(): bool {
		return ( defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) || ( defined( 'WP_CLI' ) && WP_CLI ) || is_admin() );
	}
}