Jobs.php 11.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
<?php

namespace WPML\TM\API;

use WPML\Collect\Support\Traits\Macroable;
use WPML\Element\API\PostTranslations;
use WPML\Element\API\TranslationsRepository;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\FP\Str;
use WPML\FP\Lst;
use WPML\Settings\PostType\Automatic;
use WPML\TM\API\ATE\LanguageMappings;
use WPML\TM\API\Job\Map;
use WPML\TM\Records\UpdateTranslationReviewStatus;
use function WPML\Container\make;
use function WPML\FP\curryN;
use function WPML\FP\pipe;

/**
 * Class Jobs
 * @package WPML\TM\API
 *
 * @phpstan-type curried "__CURRIED_PLACEHOLDER__"
 *
 * @method static callable|null|\stdClass getPostJob( ...$postId, ...$postType, ...$language ) : Curried:: int->string->string->null|\stdClass
 * @method static callable|null|\stdClass getTridJob( ...$trid, ...$language ) : Curried:: int->string->null|\stdClass
 * @method static callable|false|\stdClass get( ...$jobId ) : Curried:: int->false|\stdClass
 * @method static callable|void setNotTranslatedStatus( ...$jobId )  : Curried:: int->int
 * @method static callable|void setTranslationService( ...$jobId, $translationService ) : Curried:: int->int|string->int
 * @method static callable|void clearReviewStatus( ...$jobId ) : Curried:: int->int->int
 * @method static callable|array getTranslation( ...$job ) - Curried :: \stdClass->array
 * @method static callable|int getTranslatedPostId( ...$job ) - Curried :: \stdClass->int
 * @method static callable|void incrementRetryCount( ...$jobId ) : Curried:: int->void
 * @method static callable|void setTranslated( ...$jobId, ...$status ) - Curried :: int->bool->int
 * @method static callable|void clearTranslated( ...$jobId ) - Curried :: int->int
 * @method static callable|int clearAutomatic( ...$jobId ) - Curried :: int->int
 * @method static callable|void delete( ...$jobId ) - Curried :: int->void
 * @method static callable|bool isEligibleForAutomaticTranslations( ...$jobId ) - Curried :: int->bool
 */
class Jobs {
	use Macroable;

	const SENT_MANUALLY      = 1;
	const SENT_VIA_BASKET    = 2;
	const SENT_AUTOMATICALLY = 3;
	const SENT_FROM_REVIEW   = 4;
	const SENT_RETRY         = 5;
	const SENT_VIA_DASHBOARD = 6;

	public static function init() {

		self::macro( 'getPostJob', curryN( 3, function ( $postId, $postType, $language ) {
			return self::getElementJob( $postId, 'post_' . $postType, $language );
		} ) );


		self::macro( 'getTridJob', curryN( 2, function ( $trid, $language ) {
			$result = TranslationsRepository::getByTridAndLanguage( $trid, $language );
			if ( $result ) {
				return $result;
			}
			$jobId = wpml_load_core_tm()->get_translation_job_id( $trid, $language );

			return $jobId ? wpml_tm_load_job_factory()->get_translation_job_as_stdclass( $jobId ) : null;
		} ) );

		self::macro( 'setNotTranslatedStatus', self::setStatus( Fns::__, ICL_TM_NOT_TRANSLATED ) );

		self::macro( 'setTranslationService', curryN( 2, function ( $jobId, $translationService ) {
			return self::updateTranslationStatusField( $jobId, 'translation_service', $translationService, '%s' );
		} ) );

		self::macro( 'clearReviewStatus', self::setReviewStatus( Fns::__, null ) );

		self::macro( 'incrementRetryCount', curryN( 1, function ( $jobId ) {
			$job = self::get( $jobId );

			return $job && isset( $job->ate_comm_retry_count )
				? self::updateTranslationStatusField(
					$jobId,
					'ate_comm_retry_count',
					$job->ate_comm_retry_count + 1
				 )
				: null;
		} ) );

		self::macro( 'getTranslation', curryN( 1, Fns::converge( Obj::prop(), [
			Obj::prop( 'language_code' ),
			pipe( Obj::prop( 'original_doc_id' ), Fns::memorize( PostTranslations::get() ) )
		] ) ) );

		self::macro( 'getTranslatedPostId', curryN( 1, pipe( self::getTranslation(), Obj::prop( 'element_id' ) ) ) );


		self::macro( 'setTranslated', curryN( 2, function ( $jobId, $status ) {
			return self::updateTranslateJobField( $jobId, 'translated', $status );
		} ) );

		/** @phpstan-ignore-next-line */
		self::macro( 'clearTranslated', self::setTranslated( Fns::__, false ) );

		self::macro( 'clearAutomatic', curryN( 1, function ( $jobId ) {
			return self::updateTranslateJobField( $jobId, 'automatic', 0 );
		} ) );

		self::macro( 'delete', curryN( 1, function ( $jobId ) {
			/** @var \wpdb $wpdb */
			global $wpdb;

			$rid           = Map::fromJobId( $jobId );
			$previousState = \WPML_TM_ICL_Translation_Status::makeByRid( $rid )
			                                                ->previous()
			                                                ->getOrElse( null );

			if ( is_object( $previousState ) || is_array( $previousState ) ) {
				$wpdb->update(
					$wpdb->prefix . 'icl_translation_status',
					Obj::pick( [ 'status', 'translator_id', 'needs_update', 'md5 ' ], $previousState ),
					[ 'rid' => $rid ]
				);
			} else {
				$wpdb->delete(
					$wpdb->prefix . 'icl_translation_status',
					[ 'rid' => $rid ],
					[ 'rid' => '%d' ]
				);
			}
			$wpdb->delete(
				$wpdb->prefix . 'icl_translate_job',
				[ 'job_id' => $jobId ],
				[ 'job_id' => '%d' ]
			);
		} ) );

		self::macro( 'isEligibleForAutomaticTranslations', curryN( 1, Fns::memorize( function ( $wpmlJobId ) {
			$getPostType = pipe( Obj::prop( 'original_post_type' ), Str::replace( 'post_', '' ) );

			return Maybe::of( $wpmlJobId )
			            ->map( Jobs::get() )
			            ->map( Logic::both(
				            pipe( $getPostType, [ Automatic::class, 'shouldTranslate' ] ),
				            pipe( Obj::prop( 'language_code' ), LanguageMappings::isCodeEligibleForAutomaticTranslations() )
			            ) )
			            ->getOrElse( false );
		} ) ) );
	}

	/**
	 * @return string
	 */
	public static function getCurrentUrl() {
		$protocol = ( ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] != 'off' ) || Obj::prop( 'SERVER_PORT', $_SERVER ) == 443 ) ? "https://" : "http://";

		return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
	}

	/**
	 * It checks whether the job must be synced with ATE or not
	 *
	 * @param array{status: int, editor: string}|\stdClass{status: int, editor: string} $job
	 *
	 * @return bool
	 */
	public static function shouldBeATESynced( $job ) {
		$statuses = [ ICL_TM_WAITING_FOR_TRANSLATOR, ICL_TM_IN_PROGRESS ];

		return Lst::includes( (int) Obj::prop( 'status', $job ), $statuses ) &&
		       Obj::prop( 'editor', $job ) === \WPML_TM_Editors::ATE;
	}

	/**
	 * @param int $jobId
	 * @param bool $isAutomatic
	 *
	 * @return void
	 */
	public static function setAutomaticStatus( $jobId, $isAutomatic ) {
		self::updateTranslateJobField( $jobId, 'automatic', $isAutomatic ? 1 : 0 );

		if ( $isAutomatic ) {
			self::updateTranslateJobField( $jobId, 'translator_id', 0 );
			self::updateTranslationStatusField( $jobId, 'translator_id', 0 );
			self::setStatus( $jobId, ICL_TM_IN_PROGRESS );
		}
	}

	/**
	 * @template A as int
	 * @template B as int
	 * @template R as int
	 *
	 * @param ?(int|curried) $jobId
	 * @param ?(int|curried) $status
	 *
	 * @return ($jobId is A
	 *  ? ($status is B ? R : callable(B=):R)
	 *  : ($jobId is curried
	 *    ? ($status is B ? callable(A=):R : callable(A=,B=):R)
	 *    : callable(A=,B=):R
	 *    )
	 *  )
	 */
	public static function setStatus( $jobId = null, $status = null ) {
		return call_user_func_array(
			curryN(
				2,
				function ( $jobId, $status ) {
					return self::updateTranslationStatusField(
						$jobId,
						'status',
						$status
					);
				}
			),
			func_get_args()
		);
	}


	/**
	 * @template A as int
	 * @template B as string
	 * @template R as int
	 *
	 * @param ?(int|curried)    $jobId
	 * @param ?(string|curried) $status
	 *
	 * @return ($jobId is A
	 *  ? ($status is B ? R : callable(B=):R)
	 *  : ($jobId is curried
	 *    ? ($status is B ? callable(A=):R : callable(A=,B=):R)
	 *    : callable(A=,B=):R
	 *    )
	 *  )
	 */
	public static function setReviewStatus( $jobId = null, $status = null ) {
		return call_user_func_array(
			curryN(
				2,
				function ( $jobId, $status ) {
					return self::updateTranslationStatusField(
						$jobId,
						'review_status',
						$status,
						'%s'
					);
				}
			),
			func_get_args()
		);
	}


	/**
	 * @param int $jobId
	 *
	 * @return \stdClass|false
	 *
	 * @phpstan-template V1 of int|curried
	 * @phpstan-template P1 of int
	 * @phpstan-template R of \stdClass|false
	 *
	 * @phpstan-param ?V1 $jobId
	 *
	 * @phpstan-return ($jobId is P1 ? R : callable(P1=):R)
	 */
	public static function get( $jobId = null ) {
		return call_user_func_array(
			curryN(
				1,
				function ( $jobId ) {
					return wpml_tm_load_job_factory()->get_translation_job_as_stdclass( $jobId );
				}
			),
			func_get_args()
		);
	}

	/**
	 * @param string $returnUrl
	 * @param int $jobId
	 *
	 * @return callable|string
	 *
	 * @phpstan-template A1 of string|curried
	 * @phpstan-template A2 of int|curried
	 * @phpstan-template P1 of string
	 * @phpstan-template P2 of int
	 * @phpstan-template R of string
	 *
	 * @phpstan-param ?A1 $returnUrl
	 * @phpstan-param ?A2 $jobId
	 *
	 * @phpstan-return ($returnUrl is P1
	 *  ? ($jobId is P2 ? R : callable(P2=):R)
	 *  : ($jobId is P2 ? callable(P1=):R : callable(P1=,P2=):R)
	 * )
	 */
	public static function getEditUrl( $returnUrl = null, $jobId = null ) {
		return call_user_func_array(
			curryN(
				2,
				function ( $returnUrl, $jobId ) {
					$jobEditUrl = admin_url( 'admin.php?page='
						. WPML_TM_FOLDER
						. '/menu/translations-queue.php&job_id='
						. $jobId
						. '&return_url=' . urlencode( $returnUrl ) );

					return apply_filters( 'icl_job_edit_url', $jobEditUrl, $jobId );
				}
			),
			func_get_args()
		);
	}

	/**
	 * @param int    $postId
	 * @param string $elementType
	 * @param string $language
	 *
	 * @return callable|\stdClass|null
	 *
	 * @phpstan-template A1 of int|curried
	 * @phpstan-template A2 of string|curried
	 * @phpstan-template A3 of string|curried
	 * @phpstan-template P1 of int
	 * @phpstan-template P2 of string
	 * @phpstan-template P3 of string
	 * @phpstan-template R of \stdClass|null
	 *
	 * @phpstan-param ?A1 $postId
	 * @phpstan-param ?A2 $elementType
	 * @phpstan-param ?A3 $language
	 *
	 * @phpstan-return ($postId is P1
	 *  ? ($elementType is P2
	 *    ? ($language is P3
	 *      ? R
	 *      : callable(P3=):R)
	 *    : ($language is P3
	 *      ? callable(P2=):R
	 *      : callable(P2=,P3=):R)
	 *  )
	 *  : ($elementType is P2
	 *    ? ($language is P3
	 *      ? callable(P1=):R
	 *      : callable(P1=,P3=):R)
	 *    : ($language is P3
	 *      ? callable(P1=,P2=):R
	 *      : callable(P1=,P2=,P3=):R)
	 *  )
	 * )
	 */
	public static function getElementJob( $postId = null, $elementType = null, $language = null ) {
		return call_user_func_array(
			curryN(
				3,
				function ( $postId, $elementType, $language ) {
					global $sitepress;

					$trid = $sitepress->get_element_trid( $postId, $elementType );

					return self::getTridJob( $trid, $language );
				}
			),
			func_get_args()
		);
	}

	private static function updateTranslationStatusField( $jobId, $fieldName, $newValue, $fieldType = '%d' ) {
		global $wpdb;

		$newValueSqlString = null === $newValue ? 'NULL' : $fieldType;
		$unpreparedQuery = "
					UPDATE {$wpdb->prefix}icl_translation_status
						SET `{$fieldName}` = {$newValueSqlString}
						WHERE rid = (
						    SELECT rid FROM {$wpdb->prefix}icl_translate_job
						    WHERE job_id = %d
						)
					";

		if ( null === $newValue ) {
			// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
			$query = $wpdb->prepare( $unpreparedQuery, $jobId );
		} else {
			// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
			$query = $wpdb->prepare( $unpreparedQuery, $newValue, $jobId );
		}

		// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
		$wpdb->query( $query );

		return $jobId;
	}

	private static function updateTranslateJobField( $jobId, $fieldName, $newValue ) {
		global $wpdb;

		$wpdb->update(
			$wpdb->prefix . 'icl_translate_job',
			[ $fieldName => $newValue ],
			[ 'job_id' => $jobId ]
		);

		return $jobId;
	}
}

Jobs::init();