Skip to content
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
4 changes: 2 additions & 2 deletions editor/src/plugins/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ const Exporter = () => {
if ( res.message ) {
let message = res.message;
if (
message === 'Sorry, you are not allowed to do that.'
message === __( 'Sorry, you are not allowed to do that.', 'templates-patterns-collection' )
) {
message = __(
'Could not save template, check that the template is not empty.',
Expand Down Expand Up @@ -313,7 +313,7 @@ const Exporter = () => {
if ( res.message ) {
let message = res.message;
if (
message === 'Sorry, you are not allowed to do that.'
message === __( 'Sorry, you are not allowed to do that.', 'templates-patterns-collection' )
) {
message = __(
'Could not save template, check that the template is not empty.',
Expand Down
2 changes: 2 additions & 0 deletions includes/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function register_block() {
$deps['version']
);

wp_set_script_translations( $this->handle, 'templates-patterns-collection' );

wp_localize_script(
$this->handle,
'tiTpc',
Expand Down
2 changes: 2 additions & 0 deletions includes/Elementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function register_script() {
true
);

wp_set_script_translations( 'ti-tpc-elementor', 'templates-patterns-collection' );

wp_localize_script(
'ti-tpc-elementor',
'tiTpc',
Expand Down
14 changes: 14 additions & 0 deletions includes/TI_Beaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public function __construct() {
$this->add_css( 'ti-tpc-beaver', $this->url . 'build/index.css', array( 'wp-components' ), $deps['version'] );
}

/**
* Register the module script with its translations.
*
* Beaver calls this right before enqueueing the scripts added with add_js().
*
* @return void
*/
public function enqueue_scripts() {
list( $src, $deps, $ver, $in_footer ) = $this->js['ti-tpc-beaver'];

wp_register_script( 'ti-tpc-beaver', $src, $deps, $ver, $in_footer );
wp_set_script_translations( 'ti-tpc-beaver', 'templates-patterns-collection' );
}

/**
* Get position of node.
*/
Expand Down
113 changes: 113 additions & 0 deletions tests/fixtures/beaver-builder-stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Minimal Beaver Builder stubs for loading TI_Beaver without the plugin.
*
* Declares global FLBuilder* classes, so it must only ever be loaded from an
* isolated process.
*
* @package templates-patterns-collection
*/

/**
* Class FLBuilderModule, reduced to the script storage TI_Beaver relies on.
*/
class FLBuilderModule {
/**
* Module directory url.
*
* @var string
*/
public $url = '';

/**
* Scripts added with add_js(), keyed by handle.
*
* @var array<string, array{0: string|null, 1: list<string>|null, 2: string|bool|null, 3: bool|null}>
*/
public $js = array();

/**
* Styles added with add_css(), keyed by handle.
*
* @var array<string, array{0: string|null, 1: list<string>|null, 2: string|bool|null, 3: string|null}>
*/
public $css = array();

/**
* Module constructor.
*
* @param array{name: string, description: string, category: string, dir: string, url: string, icon: string} $params Module params.
*/
public function __construct( array $params ) {
$this->url = trailingslashit( $params['url'] );
}

/**
* Store a script for later enqueueing.
*
* @param string $handle Script handle.
* @param string|null $src Script url.
* @param list<string>|null $deps Dependencies.
* @param string|bool|null $ver Version.
* @param bool|null $in_footer Load in footer.
*
* @return void
*/
public function add_js( string $handle, ?string $src = null, ?array $deps = null, $ver = null, ?bool $in_footer = null ): void {
$this->js[ $handle ] = array( $src, $deps, $ver, $in_footer );
}

/**
* Store a style for later enqueueing.
*
* @param string $handle Style handle.
* @param string|null $src Style url.
* @param list<string>|null $deps Dependencies.
* @param string|bool|null $ver Version.
* @param string|null $media Media.
*
* @return void
*/
public function add_css( string $handle, ?string $src = null, ?array $deps = null, $ver = null, ?string $media = null ): void {
$this->css[ $handle ] = array( $src, $deps, $ver, $media );
}

/**
* Hook for subclasses; untyped so TI_Beaver's override stays compatible.
*
* @return void
*/
public function enqueue_scripts() {
}
}

/**
* Class FLBuilder.
*/
class FLBuilder {
/**
* Register a module class.
*
* @param string $class Module class.
* @param array<string, mixed> $form Settings form.
*
* @return void
*/
public static function register_module( string $class, array $form ): void {
}

/**
* Same order as Beaver: module hook first, then the stored scripts.
*
* @param FLBuilderModule $module Module instance.
*
* @return void
*/
public static function enqueue_module_layout_styles_scripts( FLBuilderModule $module ): void {
$module->enqueue_scripts();

foreach ( $module->js as $handle => $props ) {
wp_enqueue_script( $handle, $props[0], $props[1], $props[2], $props[3] );
}
}
}
7 changes: 7 additions & 0 deletions tests/php/static-analysis/beaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class FLBuilderModule {
*/
public $url;

/**
* Additional JS to enqueue.
*
* @var array<string, array{0: string|null, 1: list<string>|null, 2: string|bool|null, 3: bool|null}> $js
*/
public $js = array();

/**
* Module constructor.
*
Expand Down
123 changes: 123 additions & 0 deletions tests/script-translations-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Test builder script translations.
*
* @package templates-patterns-collection
*/

use TIOB\Admin;
use TIOB\Editor;
use TIOB\Elementor;
use TIOB\TI_Beaver;

/**
* Builder bundles must be linked to the plugin text domain.
*/
class Script_Translations_Test extends \WP_UnitTestCase {
/**
* Asset stubs created by this test, removed in teardown.
*
* @var list<string>
*/
private array $created_files = array();

/**
* Build dirs created by this test, removed in teardown.
*
* @var list<string>
*/
private array $created_dirs = array();

public function set_up(): void {
parent::set_up();
// CI does not build the JS bundles.
$this->maybe_stub_asset( 'editor' );
$this->maybe_stub_asset( 'elementor' );
$this->maybe_stub_asset( 'beaver' );
}
Comment thread
Copilot marked this conversation as resolved.

public function tear_down(): void {
wp_deregister_script( 'ti-tpc-block' );
wp_deregister_script( 'ti-tpc-elementor' );
wp_deregister_script( 'ti-tpc-beaver' );
wp_deregister_style( 'ti-tpc-block' );
if ( WP_Block_Type_Registry::get_instance()->is_registered( 'ti-tpc/templates-cloud' ) ) {
unregister_block_type( 'ti-tpc/templates-cloud' );
}
set_current_screen( 'front' );

foreach ( $this->created_files as $file ) {
unlink( $file );
}
foreach ( array_reverse( $this->created_dirs ) as $dir ) {
rmdir( $dir );
}
parent::tear_down();
}

/**
* Gutenberg bundle loads its translations.
*
* @return void
*/
public function test_editor_script_has_text_domain(): void {
if ( ! defined( 'TPC_TEMPLATES_CLOUD_ENDPOINT' ) ) {
define( 'TPC_TEMPLATES_CLOUD_ENDPOINT', Admin::get_templates_cloud_endpoint() );
}
set_current_screen( 'post' );

( new Editor() )->register_block();

$this->assertSame( 'templates-patterns-collection', wp_scripts()->registered['ti-tpc-block']->textdomain );
}

/**
* Elementor bundle loads its translations.
*
* @return void
*/
public function test_elementor_script_has_text_domain(): void {
( new Elementor() )->register_script();

$this->assertSame( 'templates-patterns-collection', wp_scripts()->registered['ti-tpc-elementor']->textdomain );
}

/**
* Beaver bundle loads its translations once Beaver enqueues the module.
*
* The FLBuilder* stubs are global, so they get a process of their own.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @return void
*/
public function test_beaver_script_has_text_domain(): void {
require_once __DIR__ . '/fixtures/beaver-builder-stub.php';

FLBuilder::enqueue_module_layout_styles_scripts( new TI_Beaver() );

$this->assertSame( 'templates-patterns-collection', wp_scripts()->registered['ti-tpc-beaver']->textdomain );
}

/**
* Create a minimal build asset file when the bundle is not built.
*
* @param string $bundle Bundle directory.
*
* @return void
*/
private function maybe_stub_asset( string $bundle ): void {
$dir = TIOB_PATH . $bundle . '/build';
$file = $dir . '/index.asset.php';
if ( file_exists( $file ) ) {
return;
}
if ( ! is_dir( $dir ) ) {
mkdir( $dir );
$this->created_dirs[] = $dir;
}
file_put_contents( $file, "<?php return array( 'dependencies' => array(), 'version' => 'test' );" );
$this->created_files[] = $file;
}
}
Loading