Skip to content

Commit

Permalink
Merge pull request #19 from stellarwp/fix/18-i18n-returns-403
Browse files Browse the repository at this point in the history
Create a default PUP User-Agent as WP.org blocks Guzzle.
  • Loading branch information
bordoni authored Apr 28, 2024
2 parents f61d682 + ba732ae commit 77b0533
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ parameters:

ignoreErrors:
- '#^Constant __PUP_DIR__ not found\.$#'
- '#^Constant PUP_VERSION not found\.$#'
- '#^Constant (.*)PUP_VERSION not found\.$#'
19 changes: 17 additions & 2 deletions src/Commands/I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use StellarWP\Pup\App;
use StellarWP\Pup;
use StellarWP\Pup\Command\Command;
use StellarWP\Pup\I18nConfig;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -62,6 +63,20 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
return 0;
}

/**
* Returns default client options.
*
* @return array<string, array<string, string>>
*/
protected function get_default_client_options() {
$version = Pup\PUP_VERSION;
return [
'headers' => [
'User-Agent' => "StellarWP PUP/{$version}",
],
];
}

/**
* Downloads language files.
*
Expand All @@ -85,7 +100,7 @@ protected function download_language_files( I18nConfig $i18n_config ): int {

$io->writeln( "<fg=yellow>Fetching language files for {$options->text_domain} from {$options->url}</>" ); // @phpstan-ignore-line: Those are strings.

$client = new Client();
$client = new Client( $this->get_default_client_options() );

$project_url = $options->url . '/api/projects/' . $options->slug;
$project_res = $client->request( 'GET', $project_url );
Expand Down Expand Up @@ -150,7 +165,7 @@ protected function download_and_save_translation( $options, $translation, $forma

$tried++;

$client = new Client();
$client = new Client( $this->get_default_client_options() );
$request = new Request( 'GET', $translation_url );

$promise = $client->sendAsync( $request )->then( function ( $response ) use ( $translation_url, $options, $translation, $format, $project_url, $tried, $io ) {
Expand Down

0 comments on commit 77b0533

Please sign in to comment.