backgroundTaskRepository = $backgroundTaskRepository;
$this->translateEverything = $translateEverything;
$this->markPreviouslyUnsupportedContentAsCompletedInTEA = $markDisplayedAsTranslatedPostTypesAsCompletedInTEA;
}
public function add_hooks() {
if ( wpml_is_ajax() ) {
// Prevent loading this for ajax calls.
// All tasks of this class are not relevant for ajax requests. Currently it's loaded by the root plugin.php
// which do not separate between ajax and non-ajax calls and loads this whenever is_admin() is true.
// Problem: ALL ajax calls return true for is_admin() - also on the frontend and for non logged-in users.
// TODO: Remove once wpmltm-4351 is done.
return;
}
if ( UIPage::isTMJobs( $_GET ) ) {
return;
}
$maybeLoadStatusBarAndATEConsole = Fns::tap( function () {
StatusBar::add_hooks();
Hooks::onAction( 'in_admin_header' )
->then( [ self::class, 'showAteConsoleContainer' ] );
} );
/**
* The `MarkPreviouslyUnsupportedContentAsCompletedInTEA::run` migration must be run here, instead
* of inside the regular Upgrade logic due to various reasons described in: wpmldev-3809.
*
* In short, the regular upgrade logic is run too early in WP lifecycle, and custom post types may not
* be registered yet. Moreover, we need to aldo handle a case when a user temporarily disables ST, upgrades to WPML 4.7
* and AFTER that, he re-enables ST. In this case, we need to run ST migration logic later, when ST is enabled.
*/
Hooks::onAction( 'wp_loaded' )
->then( [ $this->markPreviouslyUnsupportedContentAsCompletedInTEA, 'run' ] )
->then( [ $this, 'getData' ] )
->then( $maybeLoadStatusBarAndATEConsole )
->then( Resources::enqueueApp( 'ate-jobs-sync' ) )
->then( Fns::always( make( \WPML_TM_Scripts_Factory::class ) ) )
->then( invoke( 'localize_script' )->with( 'wpml-ate-jobs-sync-ui' ) );
Hooks::onFilter( 'wpml_tm_get_wpml_auto_translate_container' )
->then( [ self::class, 'getWpmlAutoTranslateContainer' ] );
}
public function getData() {
$ateTab = admin_url( UIPage::getTMATE() );
$isAteActive = \WPML_TM_ATE_Status::is_enabled_and_activated();
$defaultLanguage = Languages::getDefaultCode();
$getLanguages = pipe(
Languages::class . '::getActive',
CachedLanguageMappings::withCanBeTranslatedAutomatically(),
CachedLanguageMappings::withMapping(),
Fns::map( Obj::over( Obj::lensProp( 'mapping' ), Obj::prop( 'targetCode' ) ) ),
Fns::map(
Obj::addProp(
'is_default',
Relation::propEq( 'code', $defaultLanguage )
)
),
Obj::values()
);
/** @var Jobs $jobs */
$jobs = make( Jobs::class );
/** @var Scheduler */
$scheduler = make( Scheduler::class );
$data = [
'name' => 'ate_jobs_sync',
'data' => [
'endpoints' => self::getEndpoints(),
'urls' => self::getUrls( $ateTab ),
'jobIdPlaceHolder' => self::JOB_ID_PLACEHOLDER,
'languages' => $isAteActive ? $getLanguages() : [],
'isTranslationManager' => User::canManageTranslations(),
'isTranslatorOrHigher' => User::isTranslator() || User::canManageTranslations(),
'shouldTranslateEverything' =>
Option::shouldTranslateEverything()
&& ! $this->translateEverything->isEverythingProcessed( true ),
'isAutomaticTranslations' => Option::shouldTranslateEverything(),
'notEnoughCreditPopup' => self::getNotEnoughCreditPopup(),
'ateConsole' => self::getAteData(),
'isAteActive' => $isAteActive,
'editorMode' => Settings::pathOr( false, [
'translation-management',
'doc_translation_method'
] ),
'shouldCheckForRetranslation' => $scheduler->shouldRun(),
'ateCallbacks' => [], // Should be used to add any needed ATE callbacks in JS side, refer to 'src/js/ate/retranslation/index.js' for example
'settings' => [
'numberOfParallelDownloads' => defined('WPML_ATE_MAX_PARALLEL_DOWNLOADS') ? WPML_ATE_MAX_PARALLEL_DOWNLOADS : 2,
],
],
];
if ( UIPage::isTMDashboard( $_GET ) ) {
$data['data']['anyJobsExist'] = $jobs->hasAny(); // any jobs, even including CTE jobs
}
return $data;
}
/**
* @return string
*/
public static function getNotEnoughCreditPopup() {
$isTranslationManager = User::canManageTranslations();
$primaryButton = $isTranslationManager
? ''
: '';
return '
' .
'
MESSAGE_TEXT
' .
$primaryButton .
'
';
}
public static function showAteConsoleContainer() {
echo '';
}
public static function getWpmlAutoTranslateContainer() {
return '