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 3 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
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 @@ -12,7 +12,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 +22,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 +40,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 Down Expand Up @@ -100,20 +102,24 @@ public static function query( string $text, string $target_locale ) {
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,
'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 +138,35 @@ 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[] = [
$suggestions[] = array(
'similarity_score' => $match['score'],
'source' => $match['source'],
akirk marked this conversation as resolved.
Show resolved Hide resolved
'translation' => $match['text'],
'diff' => ( 1 === $match['score'] ) ? null : self::diff( $text, $match['source'] ),
];
);
}

return $suggestions;
}

/**
* Deletes a translation from translation memory.
*
* @param string $original_string Original string.
* @param string $translation_string Translation string.
* @param string $locale_slug Locale slug.
* @param string $set_slug Translation set slug.
*/
public static function delete( string $original_string, string $translation_string, string $locale_slug, string $set_slug ):bool {
// @todo Implement.
akirk marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

/**
* Generates the differences between two sequences of strings.
*
Expand All @@ -156,7 +175,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 @@ -48,10 +48,13 @@ public function get_suggestions( $project_path, $locale_slug, $set_slug ) {
wp_send_json_error( $suggestions->get_error_code() );
}

$project = GP::$project->by_path( $project_path );
$translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $set_slug, $locale_slug );
$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 +173,41 @@ public function get_deepl_suggestions( $project_path, $locale_slug, $set_slug )
);
}

/**
* Delete 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 {
$original_id = gp_post( 'originalId' );
$original_string = gp_post( 'originalString' );
$translation_string = gp_post( 'translationString' );
$nonce = gp_post( 'nonce' );

if ( ! wp_verify_nonce( $nonce, 'translation-memory-suggestions-' . $original_id ) ) {
wp_send_json_error( 'invalid_nonce' );
}

if ( empty( $original_string ) || empty( $translation_string ) ) {
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( $original_string, $translation_string, $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 @@ -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 null|WP_User $user User.
* @param string $set Locale set.
*
* @return bool
*/
private function is_user_a_gte_for_locale( $user, $locale ) {
$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