Repository navigation
Fixed script translation support for the builder #529
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
281ff4f
fix: add script translation support for the builder
girishpanchal30 8fc6172
fix: add Beaver Builder stubs
girishpanchal30 ee3df6c
test: drop redundant pre-test cleanup
girishpanchal30 2249dac
fix: make error message translatable
girishpanchal30 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] ); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' ); | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.