Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete a suggestion from TM #357

Draft
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3223,6 +3223,10 @@ ul.sidebar-tabs {
margin-left: auto;
}

button.is-small.delete-suggestion {
margin-right: 0.5rem;
}

.meta.other-locales span.locale.unique {
margin-right: 14px;
}
Expand Down Expand Up @@ -3348,4 +3352,4 @@ ul.other-locales li {
max-width: 100%;
float: none;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function register_routes() {
GP::$router->prepend( "/$set/-get-other-language-suggestions", array( __NAMESPACE__ . '\Routes\Other_Languages', 'get_suggestions' ) );
GP::$router->prepend( "/$set/-get-tm-openai-suggestions", array( __NAMESPACE__ . '\Routes\Translation_Memory', 'get_openai_suggestions' ) );
GP::$router->prepend( "/$set/-get-tm-deepl-suggestions", array( __NAMESPACE__ . '\Routes\Translation_Memory', 'get_deepl_suggestions' ) );
GP::$router->prepend( "/$set/-delete-tm-suggestion", array( __NAMESPACE__ . '\Routes\Translation_Memory', 'delete_suggestion' ), 'post' );
}

/**
Expand Down Expand Up @@ -148,11 +149,12 @@ public function pre_tmpl_load( $template, $args ) {
wp_add_inline_script(
'gp-translation-suggestions',
sprintf(
"window.WPORG_TRANSLATION_MEMORY_API_URL = %s;\nwindow.WPORG_TRANSLATION_MEMORY_OPENAI_API_URL = %s;\nwindow.WPORG_TRANSLATION_MEMORY_DEEPL_API_URL = %s;\nwindow.WPORG_OTHER_LANGUAGES_API_URL = %s;",
"window.WPORG_TRANSLATION_MEMORY_API_URL = %s;\nwindow.WPORG_TRANSLATION_MEMORY_OPENAI_API_URL = %s;\nwindow.WPORG_TRANSLATION_MEMORY_DEEPL_API_URL = %s;\nwindow.WPORG_OTHER_LANGUAGES_API_URL = %s;\nwindow.WPORG_TRANSLATION_MEMORY_API_DELETE_URL = %s;",
wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-tm-suggestions' ) ) ),
wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-tm-openai-suggestions' ) ) ),
wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-tm-deepl-suggestions' ) ) ),
wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-other-language-suggestions' ) ) )
wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-get-other-language-suggestions' ) ) ),
wp_json_encode( gp_url_project( $args['project'], gp_url_join( $args['locale_slug'], $args['translation_set_slug'], '-delete-tm-suggestion' ) ) )
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WordPressdotorg\GlotPress\TranslationSuggestions;

use GP;
use GP_Locale;
use Text_Diff;
use WP_Error;
use WP_Http;
Expand All @@ -12,7 +13,7 @@

class Translation_Memory_Client {

const API_ENDPOINT = 'https://translate.wordpress.com/api/tm/';
const API_ENDPOINT = 'https://translate.wordpress.com/api/tm/';
const API_BULK_ENDPOINT = 'https://translate.wordpress.com/api/tm/-bulk';

/**
Expand All @@ -22,7 +23,7 @@ class Translation_Memory_Client {
* @return true|\WP_Error True on success, WP_Error on failure.
*/
public static function update( array $translations ) {
$requests = [];
$requests = array();

foreach ( $translations as $original_id => $translation_id ) {
$translation = GP::$translation->get( $translation_id );
Expand All @@ -40,34 +41,36 @@ public static function update( array $translations ) {
$locale .= '_' . $translation_set->slug;
}

$requests[] = [
$requests[] = array(
'source' => $original->fields(),
'translations' => [
[
'translations' => array(
array(
'singular' => $translation->translation_0,
'plural' => $translation->translation_1,
'locale' => $locale,
],
],
];
),
),
);
}

if ( ! $requests ) {
return new WP_Error( 'no_translations' );
}

$body = wp_json_encode( [
'token' => WPCOM_TM_TOKEN,
'requests' => $requests,
] );
$body = wp_json_encode(
array(
'token' => WPCOM_TM_TOKEN,
'requests' => $requests,
)
);

$request = wp_remote_post(
self::API_BULK_ENDPOINT,
[
array(
'timeout' => 10,
'user-agent' => 'WordPress.org Translate',
'body' => $body,
]
)
);

if ( is_wp_error( $request ) ) {
Expand All @@ -91,29 +94,35 @@ public static function update( array $translations ) {
/**
* Queries translation memory for a string.
*
* @param string $text Text to search translations for.
* @param string $text Singular text to search translations for.
* @param string $text_plural Plural text to search translations for.
* @param string $target_locale Locale to search in.
* @return array|\WP_Error List of suggestions on success, WP_Error on failure.
*/
public static function query( string $text, string $target_locale ) {
public static function query( string $text, string $text_plural, string $target_locale ) {
if ( ! defined( 'WPCOM_TM_TOKEN' ) ) {
return new WP_Error( 'no_token' );
}

$url = add_query_arg( urlencode_deep( [
'text' => $text,
'target' => $target_locale,
'token' => WPCOM_TM_TOKEN,
'ts' => time(),
] ), self::API_ENDPOINT );

$url = add_query_arg(
urlencode_deep(
array(
'text' => $text,
'text_plural' => $text_plural,
'target' => $target_locale,
'token' => WPCOM_TM_TOKEN,
'ts' => time(),
)
),
self::API_ENDPOINT
);

$request = wp_remote_get(
$url,
[
array(
'timeout' => 4,
'user-agent' => 'WordPress.org Translate',
]
)
);

if ( is_wp_error( $request ) ) {
Expand All @@ -132,22 +141,69 @@ public static function query( string $text, string $target_locale ) {
}

if ( empty( $result['matches'] ) ) {
return [];
return array();
}

$suggestions = [];
$suggestions = array();
foreach ( $result['matches'] as $match ) {
$suggestions[] = [
'similarity_score' => $match['score'],
'source' => $match['source'],
'translation' => $match['text'],
'diff' => ( 1 === $match['score'] ) ? null : self::diff( $text, $match['source'] ),
];
$suggestions[] = array(
'similarity_score' => $match['score'],
'source' => $match['source'],
'source_plural' => $match['source_plural'],
'source_context' => $match['source_context'],
'translation' => $match['text'],
'translation_plural' => $match['text_plural'],
'diff' => ( 1 === $match['score'] ) ? null : self::diff( $text, $match['source'] ),
);
}

return $suggestions;
}

/**
* Deletes a translation from translation memory.
*
* @param array $source Array with the original string (singular and plural) and the context.
* @param array $translation Array with the translation (singular and plural).
* @param string $locale_slug Locale slug.
* @param string $set_slug Translation set slug.
*
* @return bool
*/
public static function delete( array $source, array $translation, string $locale_slug, string $set_slug ):bool {
$locale = $locale_slug;
if ( 'default' !== $set_slug ) {
$locale .= '_' . $set_slug;
}
$body = wp_json_encode(
array(
'token' => WPCOM_TM_TOKEN,
'source' => $source,
'translation' => array(
'singular' => $translation['translation'],
'plural' => $translation['translation_plural'],
'locale' => $locale,
),
)
);
$request = wp_remote_post(
self::API_BULK_ENDPOINT,
array(
'method' => 'DELETE',
'timeout' => 10,
'user-agent' => 'WordPress.org Translate',
'body' => $body,
)
);
if ( is_wp_error( $request ) ) {
return false;
}
if ( WP_Http::OK !== wp_remote_retrieve_response_code( $request ) ) {
return false;
}
return true;
}

/**
* Generates the differences between two sequences of strings.
*
Expand All @@ -156,7 +212,7 @@ public static function query( string $text, string $target_locale ) {
* @return string HTML markup for the differences between the two texts.
*/
protected static function diff( $previous_text, $text ) {
$diff = new Text_Diff( 'auto', [ [ $text ], [ $previous_text ] ] );
$diff = new Text_Diff( 'auto', array( array( $text ), array( $previous_text ) ) );
$renderer = new WP_Text_Diff_Renderer_inline();

return $renderer->render( $diff );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use GP;
use GP_Locales;
use GP_Route;
use GP_Translation;
use WordPressdotorg\GlotPress\TranslationSuggestions\Translation_Memory_Client;
use WP_User;
use function cli\err;
use const WordPressdotorg\GlotPress\TranslationSuggestions\PLUGIN_DIR;

class Translation_Memory extends GP_Route {
Expand Down Expand Up @@ -42,16 +45,17 @@ public function get_suggestions( $project_path, $locale_slug, $set_slug ) {
$locale .= '_' . $set_slug;
}

$suggestions = Translation_Memory_Client::query( $original->singular, $locale );
$suggestions = Translation_Memory_Client::query( $original->singular, $original->plural, $locale );

if ( is_wp_error( $suggestions ) ) {
wp_send_json_error( $suggestions->get_error_code() );
}

$is_user_a_gte_for_locale = $this->is_user_a_gte_for_locale( wp_get_current_user(), $locale );
wp_send_json_success(
gp_tmpl_get_output(
'translation-memory-suggestions',
compact( 'suggestions', 'type' ),
compact( 'suggestions', 'type', 'is_user_a_gte_for_locale' ),
PLUGIN_DIR . '/templates/'
)
);
Expand Down Expand Up @@ -170,6 +174,40 @@ public function get_deepl_suggestions( $project_path, $locale_slug, $set_slug )
);
}

/**
* Deletes a suggestion from the translation memory.
*
* @param string $project_path Project path.
* @param string $locale_slug Locale slug.
* @param string $set_slug Set slug.
*
* @return void
*/
public function delete_suggestion( string $project_path, string $locale_slug, string $set_slug ): bool {
$source = gp_post( 'source' );
$translation = gp_post( 'translation' );
$original_id = gp_post( 'originalId' );
$nonce = gp_post( 'nonce' );
if ( ! wp_verify_nonce( $nonce, 'translation-memory-suggestions-' . $original_id ) ) {
wp_send_json_error( 'invalid_nonce' );
}

if ( empty( $source ) || empty( $translation ) ) {
wp_send_json_error( 'missing_data' );
}

if ( ! $this->is_user_a_gte_for_locale( wp_get_current_user(), $locale_slug ) ) {
wp_send_json_error( 'user_cannot_delete' );
}

$result = Translation_Memory_Client::delete( $source, $translation, $locale_slug, $set_slug );
if ( ! $result ) {
wp_send_json_error( 'delete_failed' );
}

wp_send_json_success();
}

/**
* Get suggestions from OpenAI (ChatGPT).
*
Expand Down Expand Up @@ -340,7 +378,7 @@ private function is_TM_translation_100_accurate( $project_path, $locale, $set_sl
return false;
}

$suggestions = Translation_Memory_Client::query( $original->singular, $locale );
$suggestions = Translation_Memory_Client::query( $original->singular, $original->plural, $locale );
if ( is_wp_error( $suggestions ) ) {
return false;
}
Expand Down Expand Up @@ -533,5 +571,51 @@ private static function update_one_external_translation( string $translation, st
}
update_user_option( get_current_user_id(), 'gp_external_translations', $gp_external_translations );
}

/**
* Determine if the user is a GTE for the given locale.
*
* @param WP_User $user User.
* @param string $locale Locale set.
*
* @return bool
*/
private function is_user_a_gte_for_locale( WP_User $user, string $locale ): bool {
$locale_slug = explode( '_', $locale )[0];
$locale = GP_Locales::by_slug( $locale_slug );
if ( ! $locale ) {
return false;
}

$result = get_sites(
array(
'locale' => $locale->wp_locale,
'network_id' => WPORG_GLOBAL_NETWORK_ID,
'path' => '/',
'fields' => 'ids',
'number' => '1',
)
);
$site_id = array_shift( $result );
if ( ! $site_id ) {
return false;
}

$users = get_users(
array(
'blog_id' => $site_id,
'role' => 'general_translation_editor',
'count_total' => false,
)
);

foreach ( $users as $gte_user ) {
if ( $gte_user->id === $user->id ) {
return true;
}
}

return false;
}
}

Loading