first commit
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\StringsCleanup;
|
||||
|
||||
use WPML\FP\Relation;
|
||||
use WPML\ST\Gettext\AutoRegisterSettings;
|
||||
use WPML\ST\StringsCleanup\Ajax\InitStringsRemoving;
|
||||
use WPML\ST\StringsCleanup\Ajax\RemoveStringsFromDomains;
|
||||
use WPML\ST\WP\App\Resources;
|
||||
use WPML\LIB\WP\Hooks as WPHooks;
|
||||
|
||||
class UI implements \IWPML_Backend_Action_Loader {
|
||||
|
||||
/**
|
||||
* @return callable|null
|
||||
*/
|
||||
public function create() {
|
||||
if ( Relation::propEq( 'page', WPML_ST_FOLDER . '/menu/string-translation.php', $_GET ) ) {
|
||||
|
||||
return function () {
|
||||
WPHooks::onAction( 'admin_enqueue_scripts' )
|
||||
->then( [ self::class, 'localize' ] )
|
||||
->then( Resources::enqueueApp( 'strings-cleanup' ) );
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static function localize() {
|
||||
$settings = \WPML\Container\make( AutoRegisterSettings::class );
|
||||
$domains_data = $settings->getDomainsWithStringsTranslationData();
|
||||
|
||||
return [
|
||||
'name' => 'wpml_strings_cleanup_ui',
|
||||
'data' => [
|
||||
'domains' => $domains_data,
|
||||
'endpoints' => [
|
||||
'removeStringsFromDomains' => RemoveStringsFromDomains::class,
|
||||
'initStringsRemoving' => InitStringsRemoving::class,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\StringsCleanup;
|
||||
|
||||
class UntranslatedStrings {
|
||||
|
||||
/** @var \wpdb */
|
||||
private $wpdb;
|
||||
|
||||
public function __construct( \wpdb $wpdb ) {
|
||||
$this->wpdb = $wpdb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $domains
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getCountInDomains( $domains ) {
|
||||
if ( ! $domains ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) $this->wpdb->get_var(
|
||||
$this->wpdb->prepare(
|
||||
"SELECT count(id) FROM {$this->wpdb->prefix}icl_strings WHERE status = %d AND context IN ("
|
||||
. wpml_prepare_in( $domains, '%s' ) . ')',
|
||||
ICL_TM_NOT_TRANSLATED
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $domains
|
||||
* @param int $batchSize
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFromDomains( $domains, $batchSize ) {
|
||||
if ( ! $domains ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->wpdb->get_results(
|
||||
$this->wpdb->prepare(
|
||||
"SELECT id FROM {$this->wpdb->prefix}icl_strings WHERE status = %d AND context IN ("
|
||||
. wpml_prepare_in( $domains, '%s' ) . ') LIMIT 0, %d',
|
||||
ICL_TM_NOT_TRANSLATED,
|
||||
$batchSize
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $stringIds
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function remove( $stringIds ) {
|
||||
if ( $stringIds ) {
|
||||
wpml_unregister_string_multi( $stringIds );
|
||||
}
|
||||
|
||||
return count( $stringIds );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\StringsCleanup\Ajax;
|
||||
|
||||
use WPML\Ajax\IHandler;
|
||||
use WPML\Collect\Support\Collection;
|
||||
use function WPML\Container\make;
|
||||
use WPML\FP\Either;
|
||||
use WPML\ST\Gettext\AutoRegisterSettings;
|
||||
use WPML\ST\StringsCleanup\UntranslatedStrings;
|
||||
|
||||
class InitStringsRemoving implements IHandler {
|
||||
|
||||
public function run( Collection $data ) {
|
||||
$domains = $data->get( 'domains', false );
|
||||
|
||||
if ( $domains !== false ) {
|
||||
|
||||
/** @var UntranslatedStrings $untranslatedStrings */
|
||||
$untranslatedStrings = make( UntranslatedStrings::class );
|
||||
|
||||
/** @var AutoRegisterSettings $autoRegsiterSettings */
|
||||
$autoRegsiterSettings = make( AutoRegisterSettings::class );
|
||||
|
||||
if ( $autoRegsiterSettings->isEnabled() ) {
|
||||
$autoRegsiterSettings->setEnabled( false );
|
||||
}
|
||||
|
||||
return Either::of( [
|
||||
'total_strings' => $untranslatedStrings->getCountInDomains( $domains )
|
||||
] );
|
||||
} else {
|
||||
return Either::left( __( 'Error: please try again', 'wpml-string-translation' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\StringsCleanup\Ajax;
|
||||
|
||||
use WPML\Ajax\IHandler;
|
||||
use WPML\Collect\Support\Collection;
|
||||
use function WPML\Container\make;
|
||||
use WPML\FP\Either;
|
||||
use WPML\FP\Fns;
|
||||
use WPML\FP\Obj;
|
||||
use WPML\ST\StringsCleanup\UntranslatedStrings;
|
||||
|
||||
class RemoveStringsFromDomains implements IHandler {
|
||||
const REMOVE_STRINGS_BATCH_SIZE = 500;
|
||||
|
||||
public function run( Collection $data ) {
|
||||
$domains = $data->get( 'domains', false );
|
||||
|
||||
if ( $domains !== false ) {
|
||||
|
||||
/** @var UntranslatedStrings $untranslated_strings */
|
||||
$untranslated_strings = make( UntranslatedStrings::class );
|
||||
|
||||
return Either::of(
|
||||
[
|
||||
'total_strings' => $untranslated_strings->getCountInDomains( $domains ),
|
||||
'removed_strings' => $untranslated_strings->remove( Fns::map(
|
||||
Obj::prop( 'id' ),
|
||||
$untranslated_strings->getFromDomains( $domains, self::REMOVE_STRINGS_BATCH_SIZE )
|
||||
) )
|
||||
]
|
||||
);
|
||||
} else {
|
||||
return Either::left( __( 'Error: please try again', 'wpml-string-translation' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user