HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux wordpress-ubuntu-s-2vcpu-4gb-fra1-01 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
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,pcntl_unshare,
Upload Files
File: /var/www/delta/wp-content/plugins/sitepress-multilingual-cms/classes/ATE/Retry/Process.php
<?php

namespace WPML\TM\ATE\Retry;

use WPML\Collect\Support\Collection;
use WPML\FP\Fns;
use WPML\FP\Relation;
use WPML\TM\API\Jobs;
use WPML_TM_ATE_Job_Repository;
use function WPML\FP\pipe;

class Process {

	const JOBS_PROCESSED_PER_REQUEST = 10;

	/** @var WPML_TM_ATE_Job_Repository $ateRepository */
	private $ateRepository;

	/** @var Trigger $trigger */
	private $trigger;

	public function __construct(
		WPML_TM_ATE_Job_Repository $ateRepository,
		Trigger $trigger
	) {
		$this->ateRepository = $ateRepository;
		$this->trigger       = $trigger;
	}

	/**
	 * @param array $jobsToProcess
	 *
	 * @return Result
	 */
	public function run( $jobsToProcess ) {
		$result = new Result();

		if ( $jobsToProcess ) {
			$result = $this->retry( $result, wpml_collect( $jobsToProcess ) );
		} else {
			$result = $this->runRetryInit( $result );
		}

		if ( $result->jobsToProcess->isEmpty() && $this->trigger->isRetryRequired() ) {
			$this->trigger->setLastRetry( time() );
		}

		return $result;
	}

	/**
	 * @param Result $result
	 *
	 * @return Result
	 */
	private function runRetryInit( Result $result ) {
		$wpmlJobIds = $this->getWpmlJobIdsToRetry();

		if ( $this->trigger->isRetryRequired() && ! $wpmlJobIds->isEmpty() ) {
			$result = $this->retry( $result, $wpmlJobIds );
		}

		return $result;
	}

	/**
	 * @param Result $result
	 * @param Collection $jobs
	 *
	 * @return Result
	 */
	private function retry( Result $result, Collection $jobs ) {
		$jobsChunks            = $jobs->chunk( self::JOBS_PROCESSED_PER_REQUEST );
		$result->processed     = $this->handleJobs( $jobsChunks->shift() );
		$result->jobsToProcess = $jobsChunks->flatten( 1 );

		return $result;
	}

	/**
	 * @return Collection
	 */
	private function getWpmlJobIdsToRetry() {
		return wpml_collect( $this->ateRepository->get_jobs_to_retry()->map_to_property( 'translate_job_id' ) );
	}

	/**
	 * @param Collection $items
	 *
	 * @return array $items [[wpmlJobId, status, ateJobId], ...]
	 */
	private function handleJobs( Collection $items ) {
		do_action( 'wpml_added_translation_jobs', [ 'local' => $items->toArray() ], Jobs::SENT_RETRY );

		return $items->toArray();
	}
}