Skip to content

Commit

Permalink
Revert "Only skip translations if loaded early."
Browse files Browse the repository at this point in the history
This reverts commit 2108ee2.
  • Loading branch information
Biont committed Nov 27, 2024
1 parent 2108ee2 commit 03af2d4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 59 deletions.
5 changes: 2 additions & 3 deletions docs/Properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ Additionally, PluginProperties will have the following public API:
- `PluginProperties::isNetworkActive(): bool` - returns if the current Plugin is network-wide active.
- `PluginProperties::isMuPlugin(): bool` - returns if the current Plugin is a must-use Plugin.

Please note that to avoid triggering notices, our usage of `get_plugin_data` opts out of translations if called very early.
Additionally, we disable HTML-safe text processing (via `wptexturize`) offered by default.
These functions should not be used before the `'init'` hook which may be too late for some applications.
Please note that our usage of `get_plugin_data` opts out of translations and HTML-safe text processing (via `wptexturize`) offered by default.
These functions should not be used before the 'init' hook which may be too late for some applications.

## ThemeProperties

Expand Down
7 changes: 3 additions & 4 deletions src/Properties/PluginProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ protected function __construct(string $pluginMainFile)
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

// Avoid implicitly loading translations too early
// @see https://core.trac.wordpress.org/changeset/59127
$translate = did_action('after_setup_theme') || doing_action('after_setup_theme');
// $markup = false, to avoid an incorrect early wptexturize call.
// $translate = false, to avoid loading translations too early
// @see https://core.trac.wordpress.org/ticket/49965
$pluginData = (array) get_plugin_data($pluginMainFile, false, $translate);
// @see https://core.trac.wordpress.org/ticket/34114
$pluginData = (array) get_plugin_data($pluginMainFile, false, false);
$properties = Properties::DEFAULT_PROPERTIES;

// Map pluginData to internal structure.
Expand Down
52 changes: 0 additions & 52 deletions tests/unit/Properties/PluginPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public function testRequiresPlugins(string $requiresPlugins, array $expected): v
$pluginMainFile = '/app/wp-content/plugins/plugin-dir/plugin-name.php';
$expectedBaseName = 'plugin-dir/plugin-name.php';

Functions\expect('current_action')->andReturn('init');
Functions\expect('get_plugin_data')->andReturn([
'RequiresPlugins' => $requiresPlugins,
]);
Expand Down Expand Up @@ -295,55 +294,4 @@ public static function provideIsMuPluginData(): \Generator
],
];
}

/**
* @test
* @dataProvider provideTranslationLoadingData
*
* @param bool $didAfterSetupTheme
* @param bool $shouldTranslate
*
* @return void
*/
public function testTranslationLoading(bool $didAfterSetupTheme, bool $shouldTranslate): void
{
$pluginMainFile = '/app/wp-content/plugins/plugin-dir/plugin-name.php';
$expectedBaseName = 'plugin-dir/plugin-name.php';
$expectedBasePath = '/app/wp-content/plugins/plugin-dir/';

$actualTranslate = false;

Functions\expect('did_action')->andReturn($didAfterSetupTheme);
Functions\expect('doing_action')->andReturn($didAfterSetupTheme);
Functions\expect('get_plugin_data')->andReturnUsing(
static function (string $file, bool $markup, bool $translate) use (&$actualTranslate): array {
$actualTranslate = $translate;
return [];
}
);
Functions\when('plugins_url')->returnArg(1);
Functions\when('wp_normalize_path')->returnArg(1);
Functions\expect('plugin_basename')->andReturn($expectedBaseName);
Functions\expect('plugin_dir_path')->andReturn($expectedBasePath);

PluginProperties::new($pluginMainFile);
static::assertSame($shouldTranslate, $actualTranslate);
}

/**
* @return \Generator
*/
public static function provideTranslationLoadingData(): \Generator
{
yield from [
'is late hook' => [
true,
true,
],
'is early hook' => [
false,
false,
],
];
}
}

0 comments on commit 03af2d4

Please sign in to comment.