first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
<?php
namespace WPML\Setup;
use WPML\FP\Lst;
use WPML\FP\Obj;
class DisableNotices implements \IWPML_DIC_Action, \IWPML_Backend_Action {
public function add_hooks() {
add_action( 'wp_before_admin_bar_render', function () {
$iSetupPage = Lst::includes( Obj::propOr( '', 'page', $_GET ), [
'sitepress-multilingual-cms/menu/setup.php',
'sitepress-multilingual-cms/menu/languages.php'
] );
if ( $iSetupPage ) {
remove_all_actions( 'admin_notices' );
remove_all_actions( 'all_admin_notices' );
}
} );
}
}

View File

@@ -0,0 +1,216 @@
<?php
namespace WPML\Setup;
use WPML\Ajax\Endpoint\Upload;
use WPML\Core\LanguageNegotiation;
use WPML\Core\WP\App\Resources;
use WPML\Element\API\Languages;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\FP\Wrapper;
use WPML\LIB\WP\Option as WPOption;
use WPML\LIB\WP\User;
use WPML\Setup\Endpoint\CheckTMAllowed;
use WPML\Setup\Endpoint\TranslationServices;
use WPML\TM\Menu\TranslationServices\Endpoints\Activate;
use WPML\TM\Menu\TranslationServices\Endpoints\Deactivate;
use WPML\TranslationMode\Endpoint\SetTranslateEverything;
use WPML\TranslationRoles\UI\Initializer as TranslationRolesInitializer;
use WPML\UIPage;
use function WPML\Container\make;
class Initializer {
public static function loadJS() {
Wrapper::of( self::getData() )->map( Resources::enqueueApp( 'setup' ) );
}
public static function getData() {
$currentStep = Option::getCurrentStep();
$siteUrl = self::getSiteUrl();
$defaultServiceName = self::getDefaultTranslationServiceName();
Option::setDefaultTranslationMode( ! empty( $defaultServiceName ) );
$defaultLang = self::getDefaultLang();
$originalLang = Option::getOriginalLang();
if ( ! $originalLang ) {
$originalLang = $defaultLang;
Option::setOriginalLang( $originalLang );
}
$userLang = Languages::getUserLanguageCode()->getOrElse( $defaultLang );
if ( defined( 'OTGS_INSTALLER_SITE_KEY_WPML' ) ) {
self::savePredefinedSiteKey( OTGS_INSTALLER_SITE_KEY_WPML );
}
$translationMethod = Option::shouldTranslateEverything( Option::getTranslateEverythingDefaultInSetup( ! empty( $defaultServiceName ) ) )
? 'automatic'
: 'manual';
return [
'name' => 'wpml_wizard',
'data' => [
'currentStep' => $currentStep,
'endpoints' => Lst::concat( [
'setOriginalLanguage' => Endpoint\SetOriginalLanguage::class,
'setSupport' => Endpoint\SetSupport::class,
'setSecondaryLanguages' => Endpoint\SetSecondaryLanguages::class,
'currentStep' => Endpoint\CurrentStep::class,
'addressStep' => Endpoint\AddressStep::class,
'licenseStep' => Endpoint\LicenseStep::class,
'translationStep' => Endpoint\TranslationStep::class,
'setTranslateEverything' => SetTranslateEverything::class,
'recommendedPlugins' => Endpoint\RecommendedPlugins::class,
'finishStep' => Endpoint\FinishStep::class,
'addLanguages' => Endpoint\AddLanguages::class,
'upload' => Upload::class,
'getTranslationServices' => TranslationServices::class,
'activateService' => Activate::class,
'deactivateService' => Deactivate::class,
'checkTMAllowed' => CheckTMAllowed::class,
], TranslationRolesInitializer::getEndPoints() ),
'languages' => [
'list' => Obj::values( Languages::withFlags( Languages::getAll( $userLang ) ) ),
'secondaries' => Fns::map( Languages::getLanguageDetails(), Option::getTranslationLangs() ),
'original' => Languages::getLanguageDetails( $originalLang ),
'customFlagsDir' => self::getCustomFlagsDir(),
'predefinedFlagsDir' => \WPML_Flags::get_wpml_flags_url(),
],
'siteAddUrl' => 'https://wpml.org/account/sites/?add=' . urlencode( $siteUrl ) . '&wpml_version=' . self::getWPMLVersion(),
'siteKey' => self::getSiteKey(),
'usePredefinedSiteKey' => self::isPredefinedSiteKeySaved(),
'supportValue' => \OTGS_Installer_WP_Share_Local_Components_Setting::get_setting( 'wpml' ),
'address' => [
'siteUrl' => $siteUrl,
'mode' => self::getLanguageNegotiationMode(),
'domains' => LanguageNegotiation::getDomains() ?: [],
'gotUrlRewrite' => got_url_rewrite(),
],
'isTMAllowed' => Option::isTMAllowed() === true,
'isTMDisabled' => Option::isTMAllowed() === false,
'ateBaseUrl' => self::getATEBaseUrl(),
'whenFinishedUrlLanguages' => admin_url( UIPage::getLanguages() ),
'whenFinishedUrlTM' => admin_url( UIPage::getTM() ),
'ateSignUpUrl' => admin_url( UIPage::getTMATE() ),
'languagesMenuUrl' => admin_url( UIPage::getLanguages() ),
'adminUserName' => User::getCurrent()->display_name,
'translation' => Lst::concat(
[
'whoModes' => Option::getTranslationMode(),
'defaultServiceName' => $defaultServiceName,
'method' => $translationMethod,
'reviewMode' => Option::getReviewMode(),
],
TranslationRolesInitializer::getTranslationData()
),
],
];
}
/**
* Get the actual service name, or empty string if there's no default service.
*
* @return string
*/
private static function getDefaultTranslationServiceName() {
return Maybe::fromNullable( \TranslationProxy::get_tp_default_suid() )
->map( [ \TranslationProxy_Service::class, 'get_service_by_suid'] )
->map( Obj::prop('name') )
->getOrElse('');
}
/**
* @return bool
*/
private static function isPredefinedSiteKeySaved() {
return function_exists( 'OTGS_Installer' )
&& defined( 'OTGS_INSTALLER_SITE_KEY_WPML' )
&& OTGS_INSTALLER_SITE_KEY_WPML
&& OTGS_Installer()->get_site_key( 'wpml' ) === OTGS_INSTALLER_SITE_KEY_WPML;
}
/**
* @param string $siteKey
*/
private static function savePredefinedSiteKey( $siteKey ) {
if ( function_exists( 'OTGS_Installer' ) ) {
$args = [
'repository_id' => 'wpml',
'nonce' => wp_create_nonce( 'save_site_key_wpml' ),
'site_key' => $siteKey,
'return' => 1,
];
$result = OTGS_Installer()->save_site_key( $args );
if ( empty( $result['error'] ) ) {
icl_set_setting( 'site_key', $siteKey, true );
}
}
}
/**
* @return string
*/
private static function getLanguageNegotiationMode() {
if (
Option::getCurrentStep() === 'address'
&& ! got_url_rewrite()
&& LanguageNegotiation::getMode() === WPML_LANGUAGE_NEGOTIATION_TYPE_DIRECTORY
) {
return LanguageNegotiation::getModeAsString( WPML_LANGUAGE_NEGOTIATION_TYPE_PARAMETER );
}
return LanguageNegotiation::getModeAsString();
}
/**
* @return string
*/
private static function getDefaultLang() {
$getLangFromConstant = function () {
global $sitepress;
return Maybe::fromNullable( $sitepress->get_wp_api()->constant( 'WP_LANG' ) )
->map( Languages::localeToCode() )
->getOrElse( 'en' );
};
return Maybe::fromNullable( WPOption::getOr( 'WPLANG', null ) )
->map( Languages::localeToCode() )
->getOrElse( $getLangFromConstant );
}
private static function getCustomFlagsDir() {
return sprintf( '%s/flags/', Obj::propOr( '', 'baseurl', wp_upload_dir() ) );
}
private static function getATEBaseUrl() {
return make( \WPML_TM_ATE_AMS_Endpoints::class )->get_base_url( \WPML_TM_ATE_AMS_Endpoints::SERVICE_ATE );
}
/**
* @return string
*/
private static function getWPMLVersion() {
return Obj::prop( 'Version', get_plugin_data( WPML_PLUGIN_PATH . '/' . WPML_PLUGIN_FILE ) );
}
/**
* @return string
*/
private static function getSiteKey() {
$siteKey = wpml_get_setting( 'site_key', (string) OTGS_Installer()->get_site_key( 'wpml' ) );
return is_string( $siteKey ) && strlen( $siteKey ) === 10 ? $siteKey : '';
}
private static function getSiteUrl() {
return OTGS_Installer()->get_installer_site_url('wpml');
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace WPML\Setup\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\Element\API\Languages;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Obj;
use WPML\FP\Str;
use WPML\Element\API\Entity\LanguageMapping;
use WPML\Setup\Option;
class AddLanguages implements IHandler {
public function run( Collection $data ) {
$languages = $data->get( 'languages' );
$create = function ( $language ) {
$id = Languages::add(
$language['code'],
$language['name'],
$language['locale'],
0,
0,
(int) $language['encode_url'],
$language['hreflang'],
Obj::prop('country', $language)
);
if ( $id ) {
$flag = Obj::prop( 'flag', $language );
if ( $flag ) {
Languages::setFlag(
$language['code'],
Obj::propOr( '', 'name', $flag ),
(bool) Obj::propOr( false, 'fromTemplate', $flag )
);
}
}
$this->saveMapping( $language, $id );
return [ $language['code'], $id ];
};
$result = Either::right( Fns::map( $create, $languages ) );
icl_cache_clear( false );
return $result;
}
/**
* @param string $language
* @param int $id
*/
private function saveMapping( $language, $id ) {
$languageMapping = Obj::prop( 'mapping', $language );
if ( $id && $languageMapping ) {
$languageMapping = Str::split( '_', $languageMapping );
Option::addLanguageMapping( new LanguageMapping(
$language['code'],
$language['name'],
$languageMapping[0],
Obj::prop( 1, $languageMapping ) )
);
}
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace WPML\Setup\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\Core\LanguageNegotiation;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\FP\Right;
use WPML\FP\Str;
use function WPML\Container\make;
use function WPML\FP\chain;
use function WPML\FP\pipe;
use function WPML\FP\System\sanitizeString;
class AddressStep implements IHandler {
public function run( Collection $data ) {
$isDomainMode = Relation::propEq( 'mode', LanguageNegotiation::DOMAIN_STRING );
$isDirectoryMode = Relation::propEq( 'mode', LanguageNegotiation::DIRECTORY_STRING );
$otherWise = Fns::always( true );
$handleModes = Logic::cond( [
[ $isDomainMode, $this->handleDomains() ],
[ $isDirectoryMode, $this->validateSubdirectoryUrls() ],
[ $otherWise, Fns::identity() ]
] );
return Right::of( $data )
->chain( $handleModes )
->map( Obj::prop( 'mode' ) )
->map( sanitizeString() )
->map( LanguageNegotiation::saveMode() )
->map( Fns::always( 'ok' ) );
}
/**
* @return callable(Collection) : Either Left(unavailable) | Right(data)
*/
private function validateDomains() {
return function ( $data ) {
return $this->validate( wpml_collect( $data->get( 'domains' ) ), $data );
};
}
/**
* @return callable(Collection) : Either Left(unavailable) | Right(data)
*/
private function handleDomains() {
$saveDomains = Fns::tap( pipe( Obj::prop( 'domains' ), LanguageNegotiation::saveDomains() ) );
return pipe( $this->validateDomains(), chain( $saveDomains ) );
}
/**
* @return callable(Collection) : Either Left(unavailable) | Right(data)
*/
private function validateSubdirectoryUrls() {
return function ( Collection $data ) {
return $this->validate( \wpml_collect( $data->get( 'domains' ) )->map( Fns::nthArg( 1 ) ), $data );
};
}
/**
* @param Collection $domains
* @param Collection $data
*
* @return callable|\WPML\FP\Left|Right
*/
private function validate( Collection $domains, Collection $data ) {
$unavailableUrls = $domains->reject( $this->getValidator( $data ) )->keys()->toArray();
return $unavailableUrls
? Either::left( $unavailableUrls )
: Either::of( $data );
}
/**
* @param Collection $data
*
* @return callable(string) : bool
*/
private function getValidator( Collection $data ) {
$validator = Relation::propEq( 'mode', LanguageNegotiation::DIRECTORY_STRING, $data )
? [ make( \WPML_Lang_URL_Validator::class ), 'validate_langs_in_dirs' ]
: [ make( \WPML_Language_Domain_Validation::class ), 'is_valid' ];
return Obj::propOr( false, 'ignoreInvalidUrls', $data )
? Fns::always( true )
: Fns::unary( $validator );
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace WPML\Setup\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\Plugins;
use WPML\Setup\Option;
class CheckTMAllowed implements IHandler {
public function run( Collection $data ) {
$isTMAllowed = Option::isTMAllowed();
if ( $isTMAllowed === null ) {
$isTMAllowed = Plugins::isTMAllowed();
Option::setTMAllowed( $isTMAllowed );
}
return Either::right( $isTMAllowed );
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace WPML\Setup\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\Setup\Option;
class CurrentStep implements IHandler {
const STEPS = [ 'languages', 'address', 'license', 'translation', 'support', 'plugins', 'finished' ];
public function run( Collection $data ) {
$isValid = Logic::allPass( [
Lst::includes( Fns::__, self::STEPS ),
Logic::ifElse(
Relation::equals( 'languages' ),
Fns::identity(),
Fns::always( ! empty( Option::getTranslationLangs() ) )
),
] );
return Either::fromNullable( Obj::prop( 'currentStep', $data ) )
->filter( $isValid )
->map( [ Option::class, 'saveCurrentStep' ] );
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace WPML\Setup\Endpoint;
use WPML\AdminLanguageSwitcher\AdminLanguageSwitcher;
use WPML\Ajax\IHandler;
use WPML\API\Settings;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\LIB\WP\User;
use WPML\TM\ATE\AutoTranslate\Endpoint\EnableATE;
use WPML\UrlHandling\WPLoginUrlConverter;
use function WPML\Container\make;
use WPML\FP\Lst;
use WPML\FP\Right;
use WPML\Setup\Option;
use WPML\TM\Menu\TranslationServices\Endpoints\Deactivate;
class FinishStep implements IHandler {
public function run( Collection $data ) {
$wpmlInstallation = wpml_get_setup_instance();
$originalLanguage = Option::getOriginalLang();
$wpmlInstallation->finish_step1( $originalLanguage );
$wpmlInstallation->finish_step2( Lst::append( $originalLanguage, Option::getTranslationLangs() ) );
$wpmlInstallation->finish_installation();
self::enableFooterLanguageSwitcher();
$translationMode = Option::getTranslationMode();
if ( ! Lst::includes( 'users', $translationMode ) ) {
make( \WPML_Translator_Records::class )->delete_all();
}
if ( ! Lst::includes( 'manager', $translationMode ) ) {
make( \WPML_Translation_Manager_Records::class )->delete_all();
}
if ( Lst::includes( 'myself', $translationMode ) ) {
self::setCurrentUserToTranslateAllLangs();
}
if ( Option::isTMAllowed( ) ) {
Option::setTranslateEverythingDefault();
if ( ! Lst::includes( 'service', $translationMode ) ) {
make( Deactivate::class )->run( wpml_collect( [] ) );
}
make( EnableATE::class )->run( wpml_collect( [] ) );
} else {
Option::setTranslateEverything( false );
}
WPLoginUrlConverter::enable( true );
AdminLanguageSwitcher::enable();
return Right::of( true );
}
private static function enableFooterLanguageSwitcher() {
\WPML_Config::load_config_run();
/** @var \WPML_LS_Settings $lsSettings */
$lsSettings = make( \WPML_LS_Dependencies_Factory::class )->settings();
$settings = $lsSettings->get_settings();
$settings['statics']['footer']->set( 'show', true );
$lsSettings->save_settings( $settings );
}
private static function setCurrentUserToTranslateAllLangs() {
$currentUser = User::getCurrent();
$currentUser->add_cap( \WPML_Translator_Role::CAPABILITY );
User::updateMeta( $currentUser->ID, \WPML_TM_Wizard_Options::ONLY_I_USER_META, true );
make( \WPML_Language_Pair_Records::class )->store(
$currentUser->ID,
\WPML_All_Language_Pairs::get()
);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace WPML\Setup\Endpoint;
use WPML\Ajax\IHandler;
use WPML\API\Sanitize;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Right;
use WPML\Plugins;
class LicenseStep implements IHandler {
public function run( Collection $data ) {
$site_key = Sanitize::string( $data->get( 'siteKey' ) );
icl_set_setting( 'site_key', null, true );
if ( function_exists( 'OTGS_Installer' ) ) {
$args = [
'repository_id' => 'wpml',
'nonce' => wp_create_nonce( 'save_site_key_wpml' ),
'site_key' => $site_key,
'return' => 1,
];
$r = OTGS_Installer()->save_site_key( $args );
if ( ! empty( $r['error'] ) ) {
return Either::left( strip_tags( $r['error'] ) );
} else {
icl_set_setting( 'site_key', $site_key, true );
Plugins::updateTMAllowedOption();
return Right::of( __( 'Thank you for registering WPML on this site. You will receive automatic updates when new versions are available.', 'sitepress' ) );
}
}
return Either::left( false );
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace WPML\Setup\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Obj;
class RecommendedPlugins implements IHandler {
public function run( Collection $data ) {
return Either::of( OTGS_Installer()->get_recommendations( 'wpml' ) );
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace WPML\Setup\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Obj;
use WPML\FP\Str;
use WPML\Setup\Option;
class SetOriginalLanguage implements IHandler {
public function run( Collection $data ) {
return Either::of( $data )
->map( Obj::prop( 'languageCode' ) )
->map( Fns::tap( [ Option::class, 'setOriginalLang' ] ) )
->map( Str::concat( 'success: ' ) );
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace WPML\Setup\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\FP\Str;
use WPML\Setup\Option;
class SetSecondaryLanguages implements IHandler {
public function run( Collection $data ) {
return Either::of( $data )
->map( Obj::prop( 'languages' ) )
->map( Fns::tap( [ Option::class, 'setTranslationLangs' ] ) )
->map( Lst::join( ', ' ) )
->map( Str::concat('success: ') );
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace WPML\Setup\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Right;
class SetSupport implements IHandler {
public function run( Collection $data ) {
$settingStorage = new \OTGS_Installer_WP_Share_Local_Components_Setting();
$settingStorage->save( [ $data->get( 'repo' ) => $data->get( 'agree' ) ] );
return Right::of( true );
}
}

View File

@@ -0,0 +1,80 @@
<?php
namespace WPML\Setup\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Relation;
use WPML\LIB\WP\Http;
use WPML\TM\Geolocalization;
use WPML\TM\Menu\TranslationServices\ActiveServiceRepository;
use WPML\TM\Menu\TranslationServices\ServiceMapper;
use WPML\TM\Menu\TranslationServices\ServicesRetriever;
use function WPML\Container\make;
use function WPML\FP\partialRight;
class TranslationServices implements IHandler {
public function run( Collection $data ) {
$tpApi = make( \WPML_TP_Client_Factory::class )->create()->services();
$serviceMapperFunction = partialRight(
[ ServiceMapper::class, 'map' ],
[ ActiveServiceRepository::class, 'getId' ]
);
$services = ServicesRetriever::get(
$tpApi,
Geolocalization::getCountryByIp( Http::post() ),
$serviceMapperFunction
);
$preferredServiceSUID = \TranslationProxy::get_tp_default_suid();
$preferredService = false;
if ( $preferredServiceSUID ) {
$services = self::filterByPreferred( $services, $preferredServiceSUID );
$preferredServiceData = \TranslationProxy_Service::get_service_by_suid( $preferredServiceSUID );
$preferredService = new \WPML_TP_Service( $preferredServiceData );
$preferredService->set_custom_fields_data();
$preferredService = $serviceMapperFunction( $preferredService );
}
return Either::of( [
'services' => $services,
'preferredService' => $preferredService,
'logoPlaceholder' => WPML_TM_URL . '/res/img/lsp-logo-placeholder.png',
] );
}
/**
* @param array $services
* @param string $preferredServiceSUID
* @return array
*/
private static function filterByPreferred( $services, $preferredServiceSUID ) {
$preferredService = \TranslationProxy_Service::get_service_by_suid( $preferredServiceSUID );
if ( $preferredService ) {
foreach ( $services as $key => $serviceGroup ) {
$services[ $key ] = self::filterServices( $serviceGroup, $preferredService->id );
if ( empty( $services[ $key ]['services'] ) ) {
unset( $services[ $key ] );
}
}
}
return array_values( $services );
}
/**
* @param array $serviceGroup
* @param int $serviceId
* @return array
*/
public static function filterServices( $serviceGroup, $serviceId ) {
$serviceGroup['services'] = Fns::filter( Relation::propEq( 'id', $serviceId ), $serviceGroup['services'] );
return $serviceGroup;
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace WPML\Setup\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Right;
use WPML\Setup\Option;
class TranslationStep implements IHandler {
public function run( Collection $data ) {
Option::setTranslationMode( $data->get('whoMode') );
return Right::of( true );
}
}