Commit 09ea881d authored by srdjan's avatar srdjan Committed by Dimitris Mitsis

Added translation package cleanup for unused strings.

On form save, if there are some unused strings - unregister them.

https://onthegosystems.myjetbrains.com/youtrack/issue/wpmlforms-31
parent b802abd9
......@@ -42,6 +42,13 @@ class Package {
*/
private $package;
/**
* Registered package strings.
*
* @var array $registeredPackageStrings Array of string names.
*/
private $registeredPackageStrings = [];
/**
* Package constructor.
*
......@@ -139,9 +146,13 @@ class Package {
* @param string $type Type of string.
*/
public function registerString( $value, $propertyId, $propertyName, $type = 'LINE' ) {
$string_name = $this->getStringName( $propertyId, $propertyName );
if ( ! in_array( $string_name, $this->registeredPackageStrings, true ) ) {
$this->registeredPackageStrings[] = $this->sanitizeStringNameAsStringTranslation( $string_name );
}
do_action( 'wpml_register_string',
$value,
$this->getStringName( $propertyId, $propertyName ),
$string_name,
$this->getPackage(),
$this->getStringTitle( $propertyId, $propertyName ),
$type
......@@ -332,4 +343,35 @@ class Package {
return $options;
}
/** Cleans translation packages. */
public function cleanup() {
$package = $this->getPackage();
$stringPackage = apply_filters( 'wpml_st_get_string_package', null, $package );
if ( ! $stringPackage ) {
return;
}
$context = $stringPackage->get_string_context_from_package();
$allStrings = $stringPackage->get_package_strings();
foreach ( $allStrings as $string ) {
if ( ! in_array( $string->name, $this->registeredPackageStrings, true ) ) {
icl_unregister_string( $context, $string->name );
}
}
}
/**
* Sanitizes string name.
*
* @param string $string_name String name.
*
* @return string
* @see \WPML_Package::sanitize_string_name
*/
private function sanitizeStringNameAsStringTranslation( $string_name ) {
return preg_replace( '/[ \[\]]+/', '-', $string_name );
}
}
......@@ -22,7 +22,7 @@
/**
* WPML Forms version - increase after every major update.
*/
$wpml_forms_version = '1.0.1';
$wpml_forms_version = '1.0.2';
/**
* =================
......
......@@ -5,6 +5,7 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
beStrictAboutTestsThatDoNotTestAnything="false"
>
<testsuites>
<testsuite name="wpml-forms">
......
......@@ -176,4 +176,105 @@ class PackageTest extends \OTGS_TestCase {
return $translatable_properties;
}
}
\ No newline at end of file
/**
* Cleans up translation package without any fields.
*
* @dataProvider dp_packages_fields_deleted
*
* @test
*/
public function it_cleans_up_translation_package( $package, $updated_fields, $expected_deleted ) {
$translatable_properties = $this->getMockBuilder( '\WPML\Forms\Translation\Properties' )->disableOriginalConstructor()->setMethods( [
'forTranslation',
'getValue',
] )->getMock();
$translatable_properties->expects( $this->exactly( count( $updated_fields ) ) )
->method( 'forTranslation' )
->willReturn( [ 'label' => [ 'type' => 'LINE' ] ] );
$translatable_properties->expects( $this->exactly( count( $updated_fields ) ) )
->method( 'getValue' )
->willReturn( str_shuffle( 'This is field label' ) );
$subject = new Package(
$this->data['id'],
self::TITLE,
$this->get_translatable_properties(),
$translatable_properties
);
$wpml_package = $this->getMockBuilder( '\WPML_Translation_Package' )->disableOriginalConstructor()->setMethods( [
'get_string_context_from_package',
'get_package_strings',
] )->getMock();
$wpml_package->expects( $this->exactly( 1 ) )
->method( 'get_string_context_from_package' )
->willReturn( 'context' );
$sanitizeName = function ( $name ) {
return preg_replace( '/[ \[\]]+/', '-', $name );
};
if ( $package ) {
$wpml_package_object = array_map( function ( $value ) use ( $sanitizeName ) {
return (object) [ 'name' => $sanitizeName( $value ) ];
}, $package );
} else {
$wpml_package_object = null;
}
$wpml_package->expects( $this->exactly( 1 ) )
->method( 'get_package_strings' )
->willReturn( $wpml_package_object );
WP_Mock::onFilter( 'wpml_st_get_string_package' )->with( null )->reply( $wpml_package );
if ( $expected_deleted ) {
foreach ( $expected_deleted as $string_name ) {
WP_Mock::userFunction( 'icl_unregister_string', [
'times' => 1,
'args' => [ 'context', $sanitizeName( $string_name ) ],
] );
}
} else {
WP_Mock::userFunction( 'icl_unregister_string', [ 'times' => 0 ] );
}
foreach ( $updated_fields as $k => $label ) {
$field_id = substr( $label, 6 );
$field['label'] = $label;
$subject->registerField( $field_id, [ $field ] );
}
$subject->cleanup();
}
public function dp_packages_fields_deleted() {
return [
[
[ 'label-1', 'label-2 option 1' ],
[ 'label-2 option 1' ],
[ 'label-1' ],
],
[
[ 'label-1', 'label-2 option 1' ],
[ 'label-1', 'label-2 option 1' ],
[],
],
[
[ 'label-1', 'label-2 option 1' ],
[],
[ 'label-1', 'label-2 option 1' ],
],
[
[ 'label-1' ],
[ 'label-1', 'label-2 option 1' ],
[],
],
];
}
}
......@@ -16,7 +16,7 @@ class WelcomeTest extends \OTGS_TestCase {
] );
WP_Mock::userFunction( '\plugin_dir_url', [
'return' => WPML_FORMS_PLUGIN_URL,
'return' => '/',
] );
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment