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,123 @@
<?php
namespace WPML\ST\MO\Scan\UI;
use WPML\Collect\Support\Collection;
use WPML\ST\MO\Generate\DomainsAndLanguagesRepository;
use WPML\ST\MO\Generate\Process\ProcessFactory;
use WPML_ST_Translations_File_Dictionary_Storage_Table;
use WPML_ST_Translations_File_Dictionary;
use WPML\WP\OptionManager;
use function WPML\Container\make;
use function WPML\FP\partial;
class Factory implements \IWPML_Backend_Action_Loader, \IWPML_Deferred_Action_Loader {
const WPML_VERSION_INTRODUCING_ST_MO_FLOW = '4.3.0';
const OPTION_GROUP = 'ST-MO';
const IGNORE_WPML_VERSION = 'ignore-wpml-version';
/**
* @return callable|null
* @throws \WPML\Auryn\InjectionException
*/
public function create() {
if (
current_user_can( 'manage_options' ) &&
function_exists( 'wpml_is_rest_enabled' ) && wpml_is_rest_enabled()
) {
global $sitepress;
$wp_api = $sitepress->get_wp_api();
$pre_gen_dissmissed = self::isDismissed();
$st_page = $wp_api->is_core_page( 'theme-localization.php' ) || $wp_api->is_string_translation_page();
if (
( $st_page || ( ! $st_page && ! $pre_gen_dissmissed ) )
&& ( DomainsAndLanguagesRepository::hasTranslationFilesTable() || is_network_admin() )
) {
$files_to_import = $st_page ? $this->getFilesToImport() : wpml_collect( [] );
$domains_to_pre_generate_count = self::getDomainsToPreGenerateCount();
if ( $files_to_import->count() || $domains_to_pre_generate_count || isset( $_GET['search'] ) ) {
return partial(
[ UI::class, 'add_hooks' ],
Model::provider( $files_to_import, $domains_to_pre_generate_count, $st_page, is_network_admin() ),
$st_page
);
}
}
}
return null;
}
public function get_load_action() {
return 'init';
}
/**
* @return bool
* @throws \WPML\Auryn\InjectionException
*/
public static function isDismissed() {
return make( OptionManager::class )->get( self::OPTION_GROUP, 'pregen-dismissed', false );
}
/**
* @return Collection
* @throws \WPML\Auryn\InjectionException
*/
private function getFilesToImport() {
/** @var WPML_ST_Translations_File_Dictionary $file_dictionary */
$file_dictionary = make(
WPML_ST_Translations_File_Dictionary::class,
[ 'storage' => WPML_ST_Translations_File_Dictionary_Storage_Table::class ]
);
$file_dictionary->clear_skipped();
return InstalledComponents::filter( wpml_collect( $file_dictionary->get_not_imported_files() ) );
}
/**
* @return bool
*/
private static function isPreGenerationRequired() {
return self::shouldIgnoreWpmlVersion() || self::wpmlStartVersionBeforeMOFlow();
}
/**
* @return bool
*/
private static function wpmlStartVersionBeforeMOFlow() {
return version_compare(
get_option( \WPML_Installation::WPML_START_VERSION_KEY, '0.0.0' ),
self::WPML_VERSION_INTRODUCING_ST_MO_FLOW,
'<'
);
}
/**
* @return int
* @throws \WPML\Auryn\InjectionException
*/
public static function getDomainsToPreGenerateCount() {
return self::isPreGenerationRequired() ? make( ProcessFactory::class )->create()->getPagesCount() : 0;
}
/**
* @return bool
*/
public static function shouldIgnoreWpmlVersion() {
return make( OptionManager::class )->get( self::OPTION_GROUP, self::IGNORE_WPML_VERSION, false );
}
public static function ignoreWpmlVersion() {
make( OptionManager::class )->set( self::OPTION_GROUP, self::IGNORE_WPML_VERSION, true );
}
public static function clearIgnoreWpmlVersion() {
make( OptionManager::class )->set( self::OPTION_GROUP, self::IGNORE_WPML_VERSION, false );
}
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* @author OnTheGo Systems
*/
namespace WPML\ST\MO\Scan\UI;
use WPML\Collect\Support\Collection;
use WPML_ST_Translations_File_Entry;
class InstalledComponents {
/**
* @param Collection $components Collection of WPML_ST_Translations_File_Entry objects.
*
* @return Collection
*/
public static function filter( Collection $components ) {
return $components
->reject( self::isPluginMissing() )
->reject( self::isThemeMissing() );
}
/**
* WPML_ST_Translations_File_Entry -> bool
*
* @return \Closure
*/
public static function isPluginMissing() {
return function( WPML_ST_Translations_File_Entry $entry ) {
return 'plugin' === $entry->get_component_type()
&& ! is_readable( WPML_PLUGINS_DIR . '/' . $entry->get_component_id() );
};
}
/**
* WPML_ST_Translations_File_Entry -> bool
*
* @return \Closure
*/
public static function isThemeMissing() {
return function( WPML_ST_Translations_File_Entry $entry ) {
return 'theme' === $entry->get_component_type()
&& ! wp_get_theme( $entry->get_component_id() )->exists();
};
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace WPML\ST\MO\Scan\UI;
use WPML\Collect\Support\Collection;
use WPML\ST\MO\Generate\MultiSite\Condition;
use WPML\ST\TranslationFile\EntryQueries;
class Model {
/**
* @param Collection $files_to_scan
* @param int $domains_to_pre_generate_count
* @param bool $is_st_page
* @param bool $is_network_admin
*
* @return \Closure
*/
public static function provider(
Collection $files_to_scan,
$domains_to_pre_generate_count,
$is_st_page,
$is_network_admin
) {
return function () use ( $files_to_scan, $domains_to_pre_generate_count, $is_st_page ) {
return [
'files_to_scan' => [
'count' => $files_to_scan->count(),
'plugins' => self::filterFilesByType( $files_to_scan, 'plugin' ),
'themes' => self::filterFilesByType( $files_to_scan, 'theme' ),
'other' => self::filterFilesByType( $files_to_scan, 'other' ),
],
'domains_to_pre_generate_count' => $domains_to_pre_generate_count,
'file_path' => WP_LANG_DIR . '/wpml',
'is_st_page' => $is_st_page,
'admin_texts_url' => 'admin.php?page=' . WPML_ST_FOLDER . '/menu/string-translation.php&trop=1',
'is_search' => isset( $_GET['search'] ),
'run_ror_all_sites' => ( new Condition() )->shouldRunWithAllSites(),
];
};
}
/**
* @param Collection $files_to_scan
* @param string $type
*
* @return array
*/
private static function filterFilesByType( Collection $files_to_scan, $type ) {
return $files_to_scan->filter( EntryQueries::isType( $type ) )
->map( EntryQueries::getResourceName() )
->unique()
->values()
->toArray();
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace WPML\ST\MO\Scan\UI;
use \WPML\ST\WP\App\Resources;
use WPML\LIB\WP\Hooks as WPHooks;
class UI {
public static function add_hooks( callable $getModel, $isSTPage ) {
WPHooks::onAction( 'admin_enqueue_scripts' )
->then( $getModel )
->then( [ self::class, 'localize' ] )
->then( Resources::enqueueApp( 'mo-scan' ) );
if ( ! $isSTPage ) {
WPHooks::onAction( [ 'admin_notices', 'network_admin_notices' ] )
->then( [ self::class, 'add_admin_notice' ] );
}
}
public static function add_admin_notice() {
?>
<div id="wpml-mo-scan-st-page"></div>
<?php
}
public static function localize( $model ) {
return [
'name' => 'wpml_mo_scan_ui_files',
'data' => $model,
];
}
}