first commit
This commit is contained in:
@@ -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 ) )
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
@@ -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' ] );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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' ) );
|
||||
}
|
||||
}
|
||||
@@ -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: ' ) );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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: ') );
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user