Skip to content

Commit

Permalink
Load separate scripts per template
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-mendonca committed Dec 14, 2023
1 parent 0cf5745 commit 0761578
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 8 deletions.
55 changes: 55 additions & 0 deletions assets/js/project.js
Original file line number Diff line number Diff line change
@@ -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.' );
} );
}
} );
68 changes: 60 additions & 8 deletions includes/class-toolbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static function init() {
add_action( 'wp_enqueue_scripts', array( self::class, 'register_plugin_styles' ) );

// Register and enqueue plugin scripts.

Check warning on line 50 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

This comment is 42% valid code; is this commented out code?
add_action( 'wp_enqueue_scripts', array( self::class, 'register_plugin_scripts' ) );
//add_action( 'wp_enqueue_scripts', array( self::class, 'register_plugin_scripts' ) );

Check failure on line 51 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

No space found before comment text; expected "// add_action( 'wp_enqueue_scripts', array( self::class, 'register_plugin_scripts' ) );" but found "//add_action( 'wp_enqueue_scripts', array( self::class, 'register_plugin_scripts' ) );"

Check failure on line 51 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Inline comments must end in full-stops, exclamation marks, or question marks

// Load things before templates.
add_action( 'gp_pre_tmpl_load', array( self::class, 'pre_template_load' ), 10, 2 );
Expand Down Expand Up @@ -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.

Check warning on line 119 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

This comment is 41% valid code; is this commented out code?
// add_action( 'wp_enqueue_scripts', array( self::class, 'register_plugin_scripts' ) );

Check failure on line 120 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Inline comments must end in full-stops, exclamation marks, or question marks

add_action(
'wp_enqueue_scripts',
function() use ( $template, $args ) {

Check failure on line 124 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space after FUNCTION keyword; 0 found
self::register_plugin_scripts( $template, $args );
}
);

}

Check failure on line 130 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Functions must not contain multiple empty lines in a row; found 2 empty lines

// Unset unused variables.
unset( $template, $args );
}
Expand Down Expand Up @@ -321,39 +336,76 @@ public static function register_plugin_styles() {
*
* @since 1.0.0
*
* @param string $template GlotPress template name.
* @param array<string> $args GlotPress template arguments.
* @param array<int, string> $dependencies Array of script dependencies.
*
* @return void
*/
public static function register_plugin_scripts() {
public static function register_plugin_scripts( $template, &$args, $dependencies = array() ) {

Check warning on line 345 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

The method parameter $dependencies is never used

Check warning on line 345 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Unused function parameter $dependencies.

// 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 =

Check warning on line 371 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Unused variable $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 ),

Check failure on line 383 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Concat operator must be surrounded by a single space
)
);
}

/**
* Project template arguments.
*
* @since 1.0.0
*
* @param array<string> $args GlotPress template arguments.

Check failure on line 393 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after parameter type; 6 found
*
* @return array Array of template arguments.
*/
public static function template_args_project( $args ) {

Check failure on line 397 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Method GP_Toolbox\Toolbox::template_args_project() return type has no value type specified in iterable type array.

Check failure on line 397 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Method GP_Toolbox\Toolbox::template_args_project() return type has no value type specified in iterable type array.

Check failure on line 397 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Method GP_Toolbox\Toolbox::template_args_project() return type has no value type specified in iterable type array.

Check failure on line 397 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Method GP_Toolbox\Toolbox::template_args_project() return type has no value type specified in iterable type array.

$result = array();

$result['project'] = $args['project'];

foreach ( $args['translation_sets'] as $translation_set ) {

Check failure on line 403 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Argument of an invalid type string supplied for foreach, only iterables are supported.

Check failure on line 403 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Argument of an invalid type string supplied for foreach, only iterables are supported.

Check failure on line 403 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Argument of an invalid type string supplied for foreach, only iterables are supported.

Check failure on line 403 in includes/class-toolbox.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Argument of an invalid type string supplied for foreach, only iterables are supported.
$result['translation_sets'][ $translation_set->locale ] = $translation_set;
}

// Return Project and Translation Sets.
return $result;
}
}
}

0 comments on commit 0761578

Please sign in to comment.