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

Prepare version 1.5.0 #229

Merged
merged 20 commits into from
Nov 6, 2024
Merged
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ Sure! You are welcome to report any issues or add feature suggestions on the [Gi

## Changelog

### Unreleased

* Use REST API to convert the entire Translation Set on demand.
* Fix original_id error when changing string status.
* Fix i18n of updated count numbers.
* Stricten code.
* Analyse GP Template with PHPStan.

### 1.4.10

* Tested up to WP 6.7.
Expand Down
188 changes: 91 additions & 97 deletions assets/js/scripts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* global document, gpConvertPTAO90, setTimeout, wp */
/* global document, gpConvertPTAO90, Intl, setTimeout, wp */

jQuery( document ).ready( function( $ ) {
// Set array of Translation Sets.
var translationSets = [];

// Get User Locale.
var userLocale = gpConvertPTAO90.user_locale;

// Check if user is has GlotPress Admin previleges.
var glotpressAdmin = gpConvertPTAO90.admin;

Expand Down Expand Up @@ -96,111 +99,102 @@ jQuery( document ).ready( function( $ ) {
var button = $( 'table.gp-table.translation-sets tr[data-locale="' + locale + '"][data-slug="' + slug + '"] td:first-child button.gp-convert-pt-ao90-update-button' );
console.log( 'Clicked to convert project "' + projectPath + '" locale "' + locale + '/' + slug + '"' );

$.ajax( {
button.attr( 'disabled', true ).removeClass( 'success fail' ).addClass( 'updating' ).children( 'span.label' ).text( wp.i18n.__( 'Syncing...', 'gp-convert-pt-ao90' ) );

url: gpConvertPTAO90.ajaxurl,
type: 'POST',
data: {
action: 'convert_project',
projectPath: projectPath,
locale: locale,
slug: slug,
nonce: gpConvertPTAO90.nonce,
},
beforeSend: function() {
console.log( 'Ajax request is starting...' );
button.attr( 'disabled', true ).removeClass( 'success fail' ).addClass( 'updating' ).children( 'span.label' ).text( wp.i18n.__( 'Syncing...', 'gp-convert-pt-ao90' ) );
},
wp.apiFetch( {
path: '/gp-convert-pt-ao90/v1/translation-set/' + projectPath + '/' + locale + '/' + slug + '/-convert',
method: 'POST',

} )
.then( function( response ) {
// Set translation set data.
var percent = response.percent;
var current = response.current;
var fuzzy = response.fuzzy;
var untranslated = response.untranslated;
var waiting = response.waiting;

// For compatibility with GP Toolbox where these columns might be shown.
var old = response.old;
var rejected = response.rejected;
var warnings = response.warnings;

// Check if GP Toolbox is available.
var gpToolboxUpdateHighlight = wp.hooks.hasAction( 'gpToolboxUpdateHighlight', 'update_highlight_action' );

// Check if bubble of more than 90% exist.
var bubbleMoreThan90 = button.closest( 'td' ).children( 'span.bubble.morethan90' ).length;

} ).done( function( response, textStatus, jqXHR ) {
// Set translation set data.
var percent = response.data.percent;
var current = response.data.current;
var fuzzy = response.data.fuzzy;
var untranslated = response.data.untranslated;
var waiting = response.data.waiting;

// For compatibility with GP Toolbox where these columns might be shown.
var old = response.data.old;
var rejected = response.data.rejected;
var warnings = response.data.warnings;

// Check if GP Toolbox is available.
var gpToolboxUpdateHighlight = wp.hooks.hasAction( 'gpToolboxUpdateHighlight', 'update_highlight_action' );

// Check if bubble of more than 90% exist.
var bubbleMoreThan90 = button.closest( 'td' ).children( 'span.bubble.morethan90' ).length;

// Set translation set row data.
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.percent' ).text( percent + '%' );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.translated a' ).text( current );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.fuzzy a' ).text( fuzzy );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.untranslated a' ).text( untranslated );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.waiting a' ).text( waiting );

// Do if GP Toolbox update highlight hook is available.
if ( gpToolboxUpdateHighlight ) {
console.log( 'GP Toolbox gpToolboxUpdateHighlight action is available:', gpToolboxUpdateHighlight );
// Set translation set row data.
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.old a' ).text( old );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.rejected a' ).text( rejected );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.warnings a' ).text( warnings );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.percent' ).text( percent + '%' );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.translated a' ).text( Intl.NumberFormat( userLocale.slug ).format( current ) );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.fuzzy a' ).text( Intl.NumberFormat( userLocale.slug ).format( fuzzy ) );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.untranslated a' ).text( Intl.NumberFormat( userLocale.slug ).format( untranslated ) );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.waiting a' ).text( Intl.NumberFormat( userLocale.slug ).format( waiting ) );

// Do if GP Toolbox update highlight hook is available.
if ( gpToolboxUpdateHighlight ) {
console.log( 'GP Toolbox gpToolboxUpdateHighlight action is available:', gpToolboxUpdateHighlight );
// Set translation set row data.
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.old a' ).text( Intl.NumberFormat( userLocale.slug ).format( old ) );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.rejected a' ).text( Intl.NumberFormat( userLocale.slug ).format( rejected ) );
$( 'table.gp-table.translation-sets tr[data-locale="pt-ao90"][data-slug="default"] td.stats.warnings a' ).text( Intl.NumberFormat( userLocale.slug ).format( warnings ) );

// Trigger the Update Highlight function from GP Toolbox.
wp.hooks.doAction( 'gpToolboxUpdateHighlight' );
}

// Trigger the Update Highlight function from GP Toolbox.
wp.hooks.doAction( 'gpToolboxUpdateHighlight' );
}
// Add Bubble of more than 90% if currently doesn't exist.
if ( percent >= 90 ) {
console.log( 'Setting percentage in the morethan90 Bubble: ' + percent + '%' );

// Add Bubble of more than 90% if currently doesn't exist.
if ( percent >= 90 ) {
console.log( 'Setting percentage in the morethan90 Bubble: ' + percent + '%' );
// Update current Bubble.
if ( bubbleMoreThan90 ) {
console.log( 'Change Bubble from ' + button.closest( 'td' ).children( 'span.bubble.morethan90' ).text() + ' to ' + percent + '%' );
$( button ).closest( 'td' ).children( 'span.bubble.morethan90' ).text( percent + '%' );

// Update current Bubble.
if ( bubbleMoreThan90 ) {
console.log( 'Change Bubble from ' + button.closest( 'td' ).children( 'span.bubble.morethan90' ).text() + ' to ' + percent + '%' );
$( button ).closest( 'td' ).children( 'span.bubble.morethan90' ).text( percent + '%' );
// Add new Bubble.
} else {
console.log( 'Add Bubble ' + percent + '%' );

// Add new Bubble.
} else {
console.log( 'Add Bubble ' + percent + '%' );
$( '<span class="bubble morethan90" style="margin-left: 0.25em;">' + percent + '%' + '</span>' ).insertAfter( button.closest( 'td' ).find( 'strong' ) );
}

$( '<span class="bubble morethan90" style="margin-left: 0.25em;">' + percent + '%' + '</span>' ).insertAfter( button.closest( 'td' ).find( 'strong' ) );
// Check if there is a bubble to remove.
} else if ( bubbleMoreThan90 ) {
// Remove Bubble.
$( button ).closest( 'td' ).children( 'span.bubble.morethan90' ).remove();
}

// Check if there is a bubble to remove.
} else if ( bubbleMoreThan90 ) {
// Remove Bubble.
$( button ).closest( 'td' ).children( 'span.bubble.morethan90' ).remove();
}

// Change button status to 'Synced'.
button.removeClass( 'updating' ).addClass( 'success' );
button.children( 'span.icon.dashicons' ).hide().removeClass( 'dashicons-update' ).addClass( 'dashicons-yes' ).show();
button.children( 'span.label' ).text( wp.i18n.__( 'Synced!', 'gp-convert-pt-ao90' ) );

console.log( 'Ajax request has been completed (' + textStatus + '). Status: ' + jqXHR.status + ' ' + jqXHR.statusText );
console.log( response );
console.log( textStatus );
console.log( jqXHR );
} ).fail( function( jqXHR, textStatus ) {
// Change button status to 'Failed'.
button.removeClass( 'updating' ).addClass( 'fail' );
button.children( 'span.icon.dashicons' ).hide().removeClass( 'dashicons-update' ).addClass( 'dashicons-warning' ).show();
button.children( 'span.label' ).text( wp.i18n.__( 'Failed!', 'gp-convert-pt-ao90' ) );

// Show the Error notice.
console.log( 'Ajax request has failed (' + textStatus + '). Status: ' + jqXHR.status + ' ' + jqXHR.statusText );
} ).always( function() {
// Change button status back to default.
setTimeout(
function() {
button.attr( 'disabled', false ).removeClass( 'success fail' );
button.children( 'span.icon.dashicons' ).hide().removeClass( 'dashicons-yes dashicons-warning' ).addClass( 'dashicons-update' ).show();
button.children( 'span.label' ).text( wp.i18n.__( 'Sync', 'gp-convert-pt-ao90' ) );
},
3000 // Wait 3 Seconds.
);

console.log( 'Ajax end.' );
} );
// Change button status to 'Synced'.
button.removeClass( 'updating' ).addClass( 'success' );
button.children( 'span.icon.dashicons' ).hide().removeClass( 'dashicons-update' ).addClass( 'dashicons-yes' ).show();
button.children( 'span.label' ).text( wp.i18n.__( 'Synced!', 'gp-convert-pt-ao90' ) );

console.log( response );
} )
.catch( function( error ) {
// Change button status to 'Failed'.
button.removeClass( 'updating' ).addClass( 'fail' );
button.children( 'span.icon.dashicons' ).hide().removeClass( 'dashicons-update' ).addClass( 'dashicons-warning' ).show();
button.children( 'span.label' ).text( wp.i18n.__( 'Failed!', 'gp-convert-pt-ao90' ) );

// Show the Error notice.
console.log( 'Failed to convert Translation Set.' );
console.log( 'Error:', error.message );
} )
.finally( function() {
// Change button status back to default.
setTimeout(
function() {
button.attr( 'disabled', false ).removeClass( 'success fail' );
button.children( 'span.icon.dashicons' ).hide().removeClass( 'dashicons-yes dashicons-warning' ).addClass( 'dashicons-update' ).show();
button.children( 'span.label' ).text( wp.i18n.__( 'Sync', 'gp-convert-pt-ao90' ) );
},
3000 // Wait 3 Seconds.
);

console.log( 'Request ended.' );
} );
}
} );
Loading