From 0761578ba83e210e40534c67743799844288eb33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Mendon=C3=A7a?= Date: Thu, 14 Dec 2023 07:54:08 +0000 Subject: [PATCH] Load separate scripts per template --- assets/js/project.js | 55 ++++++++++++++++++++++++++++++ includes/class-toolbox.php | 68 +++++++++++++++++++++++++++++++++----- 2 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 assets/js/project.js diff --git a/assets/js/project.js b/assets/js/project.js new file mode 100644 index 0000000..a3b0df1 --- /dev/null +++ b/assets/js/project.js @@ -0,0 +1,55 @@ +/* global document, gpToolbox, setTimeout, wp */ + +jQuery( document ).ready( function( $ ) { + // Set array of Translation Sets. + var translationSets = []; + + // Check if user is has GlotPress Admin previleges. + var glotpressAdmin = gpToolbox.admin; + + var cp-project-id = 1; + + // Check if user is has GlotPress Admin previleges. + var gpUrlProject = gpToolbox.gp_url_project; + + /** + * Delete Translations from a Translation Set with a specific status. + * + * @param {string} projectPath : Path ot the GP_Project. + * @param {string} locale : Locale of the GP_Translation_Set. + * @param {string} slug : Slug of the GP_Translation_Set. + * @param {string} status : Status of the GP_Translation. + */ + function deleteTranslations( projectPath, locale, slug, status ) { + 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 delete translations on project "' + projectPath + '" locale "' + locale + '/' + slug + '"' + ' and status "' + status + '"' ); + + $.ajax( { + + url: gpToolbox.ajaxurl, + type: 'POST', + data: { + action: 'delete_translations', + projectPath: projectPath, + locale: locale, + slug: slug, + status: status, + nonce: gpToolbox.nonce, + }, + beforeSend: function() { + console.log( 'Ajax request is starting...' ); + }, + + } ).done( function( response, textStatus, jqXHR ) { + 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 ) { + // Show the Error notice. + console.log( 'Ajax request has failed (' + textStatus + '). Status: ' + jqXHR.status + ' ' + jqXHR.statusText ); + } ).always( function() { + console.log( 'Ajax end.' ); + } ); + } +} ); diff --git a/includes/class-toolbox.php b/includes/class-toolbox.php index f871a06..86c8788 100644 --- a/includes/class-toolbox.php +++ b/includes/class-toolbox.php @@ -48,7 +48,7 @@ public static function init() { add_action( 'wp_enqueue_scripts', array( self::class, 'register_plugin_styles' ) ); // Register and enqueue plugin scripts. - add_action( 'wp_enqueue_scripts', array( self::class, 'register_plugin_scripts' ) ); + //add_action( 'wp_enqueue_scripts', array( self::class, 'register_plugin_scripts' ) ); // Load things before templates. add_action( 'gp_pre_tmpl_load', array( self::class, 'pre_template_load' ), 10, 2 ); @@ -114,6 +114,21 @@ public static function notice_gp_not_found() { */ public static function pre_template_load( $template, &$args ) { + if ( $template === 'project' ) { + + // Register and enqueue plugin scripts. + // add_action( 'wp_enqueue_scripts', array( self::class, 'register_plugin_scripts' ) ); + + add_action( + 'wp_enqueue_scripts', + function() use ( $template, $args ) { + self::register_plugin_scripts( $template, $args ); + } + ); + + } + + // Unset unused variables. unset( $template, $args ); } @@ -321,39 +336,76 @@ public static function register_plugin_styles() { * * @since 1.0.0 * + * @param string $template GlotPress template name. + * @param array $args GlotPress template arguments. + * @param array $dependencies Array of script dependencies. + * * @return void */ - public static function register_plugin_scripts() { + public static function register_plugin_scripts( $template, &$args, $dependencies = array() ) { // Check if SCRIPT_DEBUG is true. $suffix = SCRIPT_DEBUG ? '' : '.min'; + // Set custom script ID. + $script_id = sprintf( + 'gp-toolbox-%s', + $template + ); + wp_register_script( - 'gp-toolbox', - GP_TOOLBOX_DIR_URL . 'assets/js/scripts' . $suffix . '.js', + $script_id, + GP_TOOLBOX_DIR_URL . 'assets/js/' . $template . $suffix . '.js', array(), GP_TOOLBOX_VERSION, false ); - gp_enqueue_scripts( 'gp-toolbox' ); + gp_enqueue_scripts( $script_id ); wp_set_script_translations( - 'gp-toolbox', + $script_id, 'gp-toolbox' ); + $template_args_callback = + wp_localize_script( - 'gp-toolbox', - 'gpToolbox', + $script_id, + 'gpToolbox_' . $template, array( + 'scriptID' => 'gpToolbox_' . $template, 'admin' => GP::$permission->current_user_can( 'admin' ), 'gp_url' => gp_url(), // /glotpress/. 'gp_url_project' => gp_url_project(), // /glotpress/projects/. 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'gp-toolbox-nonce' ), + 'args' => self::{'template_args_'.$template}( $args ), ) ); } + + /** + * Project template arguments. + * + * @since 1.0.0 + * + * @param array $args GlotPress template arguments. + * + * @return array Array of template arguments. + */ + public static function template_args_project( $args ) { + + $result = array(); + + $result['project'] = $args['project']; + + foreach ( $args['translation_sets'] as $translation_set ) { + $result['translation_sets'][ $translation_set->locale ] = $translation_set; + } + + // Return Project and Translation Sets. + return $result; + } } }