Skip to content
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

Cleanup, add PHP file support #270

Merged
merged 21 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
32 changes: 20 additions & 12 deletions .github/workflows/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,40 @@ jobs:
matrix:
os: [ ubuntu-latest ]
php: [ '7.4' ]
wordpress: [ 'latest', 'nightly' ]
glotpress: [ 'develop' ]
wordpress: [ 'latest' ]
glotpress: [ 'latest' ]
experimental: [ false ]
include:
# - php: '8.0'
# os: ubuntu-latest
# experimental: true
- os: ubuntu-latest
php: '7.2'
php: '8.0'
wordpress: 'latest'
glotpress: 'latest'
experimental: false
- os: ubuntu-latest
php: '7.3'
php: '8.1'
wordpress: 'latest'
glotpress: 'latest'
experimental: false
- os: ubuntu-latest
php: '7.4'
php: '8.2'
wordpress: 'latest'
glotpress: 'develop'
glotpress: 'latest'
experimental: false
coverage: true
- os: ubuntu-latest
php: '8.2'
wordpress: 'nightly'
glotpress: 'develop'
experimental: true
- os: ubuntu-latest
php: '8.3'
wordpress: 'nightly'
glotpress: 'develop'
experimental: true

steps:
- name: Check out Git repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -79,7 +86,7 @@ jobs:
done

- name: Install PHP dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3

- name: Make Composer packages available globally
run: |
Expand Down Expand Up @@ -134,8 +141,9 @@ jobs:

- name: Upload code coverage report
if: ${{ matrix.coverage }}
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ${{ steps.coverage_files.outputs.COVERAGE_FILES }}
flags: feature
fail_ci_if_error: true
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

steps:
- name: Check out Git repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up PHP
uses: shivammathur/setup-php@v2
Expand All @@ -28,7 +28,7 @@ jobs:
echo "::set-output name=dir::$(composer config home)"

- name: Install PHP dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3

- name: Install composer-normalize
run: composer global require ergebnis/composer-normalize
Expand Down
32 changes: 20 additions & 12 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,40 @@ jobs:
matrix:
os: [ ubuntu-latest ]
php: [ '7.4' ]
wordpress: [ 'latest', 'nightly' ]
glotpress: [ 'develop' ]
wordpress: [ 'latest' ]
glotpress: [ 'latest' ]
experimental: [ false ]
include:
# - php: '8.0'
# os: ubuntu-latest
# experimental: true
- os: ubuntu-latest
php: '7.2'
php: '8.0'
wordpress: 'latest'
glotpress: 'latest'
experimental: false
- os: ubuntu-latest
php: '7.3'
php: '8.1'
wordpress: 'latest'
glotpress: 'latest'
experimental: false
- os: ubuntu-latest
php: '7.4'
php: '8.2'
wordpress: 'latest'
glotpress: 'develop'
glotpress: 'latest'
experimental: false
coverage: true
- os: ubuntu-latest
php: '8.2'
wordpress: 'nightly'
glotpress: 'develop'
experimental: true
- os: ubuntu-latest
php: '8.3'
wordpress: 'nightly'
glotpress: 'develop'
experimental: true

steps:
- name: Check out Git repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -65,7 +72,7 @@ jobs:
tools: composer

- name: Install PHP dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3

- name: Make Composer packages available globally
run: |
Expand Down Expand Up @@ -99,7 +106,8 @@ jobs:

- name: Upload coverage to Codecov
if: ${{ matrix.coverage }}
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: coverage-clover-${{ github.sha }}.xml
flags: php
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# PHPUnit
/coverage*.xml
.phpunit.result.cache

# Behat
*.log
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Changed
* Require at least PHP 7.2. [#236]
* Require GlotPress 4.0. [#270]
* Require at least PHP 7.4. [#270]

### Fixed
* Pass correct parameter for translation set in `traduttore.generate_zip_delay` filter. [#236]
Expand Down
10 changes: 7 additions & 3 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ SKIP_DB_CREATE=${7-false}
TMPDIR=${TMPDIR-/tmp}
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
WP_TESTS_FILE="$WP_TESTS_DIR"/includes/functions.php
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/}
WP_CORE_FILE="$WP_CORE_DIR"/wp-includs.php
GP_TESTS_DIR="$WP_CORE_DIR/build/wp-content/plugins/glotpress/tests/phpunit"

download() {
Expand Down Expand Up @@ -56,10 +58,11 @@ set -ex

install_wp() {

if [ -d $WP_CORE_DIR ]; then
if [ -f $WP_CORE_FILE ]; then
return;
fi

rm -rf $WP_CORE_DIR
mkdir -p $WP_CORE_DIR

if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
Expand Down Expand Up @@ -104,9 +107,10 @@ install_test_suite() {
local ioption='-i'
fi

# set up testing suite if it doesn't yet exist
if [ ! -d $WP_TESTS_DIR ]; then
# set up testing suite if it doesn't yet exist or only partially exists
if [ ! -f $WP_TESTS_FILE ]; then
# set up testing suite
rm -rf $WP_TESTS_DIR
mkdir -p $WP_TESTS_DIR
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
Expand Down
17 changes: 8 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,21 @@
"issues": "https://github.com/wearerequired/traduttore/issues"
},
"require": {
"php": ">=7.2",
"php": ">=7.4",
"ext-json": "*",
"ext-zip": "*",
"wearerequired/traduttore-registry": "^2.0"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
"php-stubs/wp-cli-stubs": "^2.4",
"phpunit/phpunit": "^7.5.20",
"szepeviktor/phpstan-wordpress": "^1.1",
"wearerequired/coding-standards": "^3.0",
"dealerdirect/phpcodesniffer-composer-installer": "^v1.0.0",
"php-stubs/wp-cli-stubs": "^v2.10",
"szepeviktor/phpstan-wordpress": "^1.3",
"wearerequired/coding-standards": "^6.0",
"wp-cli/extension-command": "^2.0",
"wp-cli/rewrite-command": "^2.0",
"wp-cli/wp-cli-tests": "^3.0.11",
"wpackagist-plugin/glotpress": "^3.0.0",
"yoast/phpunit-polyfills": "^1.0"
"wp-cli/wp-cli-tests": "^v4.2.9",
"wpackagist-plugin/glotpress": "^4.0.0",
"yoast/phpunit-polyfills": "^1.1"
},
"suggest": {
"wpackagist-plugin/slack": "Send Slack notifications for various events"
Expand Down
1 change: 0 additions & 1 deletion inc/CLI/ProjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,4 @@ public function update( array $args, array $assoc_args ): void {

WP_CLI::warning( sprintf( 'Could not update translations for project (ID: %d)!', $project->get_id() ) );
}

}
23 changes: 23 additions & 0 deletions inc/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function export_strings(): ?array {
$this->build_json_files( $mapping );
$this->build_po_file( $php_entries );
$this->build_mo_file( $php_entries );
$this->build_php_file( $php_entries );

return $this->files;
}
Expand Down Expand Up @@ -271,4 +272,26 @@ protected function build_mo_file( array $entries ): void {
$this->files[ $file_name ] = $temp_file;
}
}

/**
* Builds a PHP file for translations.
*
* @since 3.3.0
*
* @param \Translation_Entry[] $entries The translation entries.
*/
protected function build_php_file( array $entries ): void {
/** @var \GP_Format $format */
$format = gp_array_get( GP::$formats, 'php' );

$base_file_name = $this->get_base_file_name();
$file_name = "{$base_file_name}.l10n.php";
$temp_file = wp_tempnam( $file_name );

$contents = $format->print_exported_file( $this->project->get_project(), $this->locale, $this->translation_set, $entries );

if ( $this->write_to_file( $temp_file, $contents ) ) {
$this->files[ $file_name ] = $temp_file;
}
}
}
1 change: 0 additions & 1 deletion inc/Loader/Subversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ protected function download_new_repository(): ?string {
);

return 0 === $status ? $target : null;

}

/**
Expand Down
10 changes: 5 additions & 5 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static function ( $project_id, $originals_added, $originals_existing, $originals

add_action(
'traduttore.generate_zip',
static function( $translation_set_id ): void {
static function ( $translation_set_id ): void {
/** @var \GP_Translation_Set $translation_set */
$translation_set = GP::$translation_set->get( $translation_set_id );

Expand All @@ -117,7 +117,7 @@ static function( $translation_set_id ): void {

add_filter(
'gp_update_meta',
static function( $meta_tuple ) {
static function ( $meta_tuple ) {
$allowed_keys = [
Project::VERSION_KEY, // '_traduttore_version'.
Project::TEXT_DOMAIN_KEY, // '_traduttore_text_domain'.
Expand Down Expand Up @@ -183,11 +183,11 @@ function ( $project_id ): void {

add_filter(
'slack_get_events',
static function( $events ) {
static function ( $events ) {
$events['traduttore.zip_generated'] = [
'action' => 'traduttore.zip_generated',
'description' => __( 'When a new translation ZIP file is built', 'traduttore' ),
'message' => function( $zip_path, $zip_url, GP_Translation_Set $translation_set ) {
'message' => function ( $zip_path, $zip_url, GP_Translation_Set $translation_set ) {
/** @var \GP_Locale $locale */
$locale = GP_Locales::by_slug( $translation_set->locale );
$project = new Project( GP::$project->get( $translation_set->project_id ) );
Expand Down Expand Up @@ -231,7 +231,7 @@ static function( $events ) {
$events['traduttore.updated'] = [
'action' => 'traduttore.updated',
'description' => __( 'When new translations are updated for a project', 'traduttore' ),
'message' => function( Project $project, array $stats ) {
'message' => function ( Project $project, array $stats ) {
[
$originals_added,
$originals_existing, // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
Expand Down
2 changes: 1 addition & 1 deletion inc/WebhookHandler/Bitbucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function permission_callback(): ?bool {
return false;
}

$payload_signature = 'sha256=' . hash_hmac( 'sha256', $this->request->get_body(), $secret );
$payload_signature = 'sha256=' . hash_hmac( 'sha256', wp_json_encode( $params ), $secret );

return hash_equals( $token, $payload_signature );
}
Expand Down
2 changes: 1 addition & 1 deletion inc/WebhookHandler/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function permission_callback(): ?bool {

$secret = $this->get_secret( $project );

$payload_signature = 'sha1=' . hash_hmac( 'sha1', $this->request->get_body(), $secret );
$payload_signature = 'sha1=' . hash_hmac( 'sha1', wp_json_encode( $params ), $secret );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

return hash_equals( $token, $payload_signature );
}
Expand Down
6 changes: 5 additions & 1 deletion tests/phpunit/tests/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function test_creates_only_po_and_mo_files(): void {
[
'foo-project-de_DE.po',
'foo-project-de_DE.mo',
'foo-project-de_DE.l10n.php',
],
array_keys( $actual )
);
Expand Down Expand Up @@ -133,6 +134,7 @@ public function test_creates_multiple_json_files(): void {
[
'foo-project-de_DE.po',
'foo-project-de_DE.mo',
'foo-project-de_DE.l10n.php',
$json_filename_1,
$json_filename_2,
],
Expand Down Expand Up @@ -216,6 +218,7 @@ public function test_map_entries_to_source_filter(): void {
[
'foo-project-de_DE.po',
'foo-project-de_DE.mo',
'foo-project-de_DE.l10n.php',
$json_filename_target,
],
array_keys( $actual )
Expand Down Expand Up @@ -356,7 +359,7 @@ public function test_js_source_entries_are_not_exported_as_json_files(): void {

array_map( 'unlink', $actual );

$this->assertInternalType( 'array', $json );
$this->assertIsArray( $json );
$this->assertCount( 4, $json['locale_data']['messages'] );
$this->assertArrayHasKey( $original_1->singular, $json['locale_data']['messages'] );
$this->assertArrayHasKey( $original_2->singular, $json['locale_data']['messages'] );
Expand All @@ -365,6 +368,7 @@ public function test_js_source_entries_are_not_exported_as_json_files(): void {
[
'foo-project-de_DE.po',
'foo-project-de_DE.mo',
'foo-project-de_DE.l10n.php',
$json_filename,
],
array_keys( $actual )
Expand Down
Loading
Loading