Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Replace curl-based HTTP client with WordPress\AsyncHttp\Client #2

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

adamziel
Copy link
Owner

@adamziel adamziel commented Jul 15, 2024

Make the URL rewriter download WordPress assets without curl dependency.

Benefits:

  • No dependencies = this can eventually become a part of WordPress core
  • The code is much simpler
  • We'll get more testing for WordPress\AsyncHttp\Client and will end up building less curl-specific code

The implementation is pretty much this:

$client = new Client( [
	'concurrency' => 10,
] );
$client->enqueue( $requests );

$results = [];
while ( $client->await_next_event() ) {
	$request = $client->get_request();
	
	switch ( $client->get_event() ) {
		case Client::EVENT_BODY_CHUNK_AVAILABLE:
			file_put_contents(
				$local_paths[$request->original_request()->id],
				$client->get_response_body_chunk(),
				FILE_APPEND
			);
			break;
		case Client::EVENT_FAILED:
			$results[$request->original_request()->url] = [
				'success' => false,
				'error' => $request->error,
			];
			break;
		case Client::EVENT_FINISHED:
			$results[$request->original_request()->url] = [
				'success' => true
			];
			break;
	}
}

To test, run

$ php bin/rewrite-urls.php migrate_urls \
    --file ./adamadam-blog.html \
    --current-site-url https://adamadam.blog \
    --new-site-url https://adamziel.com

And you should see a new assets directory with bunch of static assets fetched from adamadam.blog in it.

See WordPress/blueprints-library#113 for more details about the HTTP client library.

Follow-up work

cc @dmsnell @akirk @brandonpayton @bgrgicak @griffbrad

The code got much simpler plus we can easily expand to stream-rewriting remote
pages or even zip archives (with the ZIP Processor).

See WordPress/blueprints-library#113
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant