Use new code style

This commit is contained in:
David Stone
2025-02-07 19:02:33 -07:00
parent 0181024ae1
commit 8433379d90
672 changed files with 37107 additions and 45249 deletions

View File

@ -9,14 +9,14 @@
namespace WP_Ultimo;
use \Amp\Iterator;
use \Amp\Sync\LocalSemaphore;
use \Amp\Sync\ConcurrentIterator;
use \Amp\Http\Client\Request;
use \Amp\Http\Client\Connection\DefaultConnectionPool;
use \Amp\Socket\ClientTlsContext;
use \Amp\Socket\ConnectContext;
use \Amp\Http\Client\HttpClientBuilder;
use Amp\Iterator;
use Amp\Sync\LocalSemaphore;
use Amp\Sync\ConcurrentIterator;
use Amp\Http\Client\Request;
use Amp\Http\Client\Connection\DefaultConnectionPool;
use Amp\Socket\ClientTlsContext;
use Amp\Socket\ConnectContext;
use Amp\Http\Client\HttpClientBuilder;
// Exit if accessed directly
defined('ABSPATH') || exit;
@ -47,12 +47,11 @@ class Async_Calls {
*/
public static function register_listener($id, $callable, ...$args) {
self::$registry[$id] = array(
self::$registry[ $id ] = array(
'callable' => $callable,
'args' => $args,
);
} // end register_listener;
}
/**
* Install the registered listeners.
@ -63,28 +62,23 @@ class Async_Calls {
public static function install_listeners() {
foreach (self::$registry as $id => $listener) {
add_action(
"wp_ajax_wu_async_call_listener_{$id}",
function () use ($listener) {
add_action("wp_ajax_wu_async_call_listener_{$id}", function() use ($listener) {
try {
$results = call_user_func_array($listener['callable'], $listener['args']);
} catch (\Throwable $th) {
wp_send_json_error($th);
}
try {
wp_send_json_success($results);
$results = call_user_func_array($listener['callable'], $listener['args']);
} catch (\Throwable $th) {
wp_send_json_error($th);
} // end try;
wp_send_json_success($results);
exit;
});
} // end foreach;
} // end install_listeners;
exit;
}
);
}
}
/**
* Build the base URL for the listener calls.
@ -98,8 +92,7 @@ class Async_Calls {
public static function build_base_url($id, $args) {
return add_query_arg($args, admin_url('admin-ajax.php'));
} // end build_base_url;
}
/**
* Build the final URL to be called.
@ -119,19 +112,22 @@ class Async_Calls {
$urls = array();
for ($i = 1; $i <= $pages; $i++) {
$urls[] = self::build_base_url($id, array_merge($args, array(
'action' => "wu_async_call_listener_$id",
'parallel' => 1,
'page' => $i,
'per_page' => $chunk_size,
)));
} // end for;
$urls[] = self::build_base_url(
$id,
array_merge(
$args,
array(
'action' => "wu_async_call_listener_$id",
'parallel' => 1,
'page' => $i,
'per_page' => $chunk_size,
)
)
);
}
return $urls;
} // end build_url_list;
}
/**
* Builds and returns the client that will handle the calls.
@ -143,17 +139,16 @@ class Async_Calls {
$client_tls_context = new ClientTlsContext('');
$connect_base_context = new ConnectContext;
$connect_base_context = new ConnectContext();
$tls_context = $client_tls_context->withoutPeerVerification();
$connect_context = $connect_base_context->withTlsContext($tls_context);
$builder = new HttpClientBuilder;
$builder = new HttpClientBuilder();
return $builder->usingPool(new DefaultConnectionPool(null, $connect_context))->build();
} // end get_client;
}
/**
* Run the parallel queue after everything is correctly enqueued.
@ -173,49 +168,48 @@ class Async_Calls {
$urls = self::build_url_list($id, $total, $chunk_size, $args);
$coroutine = \Amp\call(static function() use ($id, $total, $chunk_size, $parallel_threads, $client, $urls) {
$coroutine = \Amp\call(
static function () use ($id, $total, $chunk_size, $parallel_threads, $client, $urls) {
$results = array();
$results = array();
$chunker = new LocalSemaphore($parallel_threads);
$chunker = new LocalSemaphore($parallel_threads);
yield ConcurrentIterator\each(Iterator\fromIterable($urls), $chunker, function($url) use (&$results, $client) {
yield ConcurrentIterator\each(
Iterator\fromIterable($urls),
$chunker,
function ($url) use (&$results, $client) {
try {
try {
$request = new Request($url);
$request = new Request($url);
$request->setTcpConnectTimeout(1000 * 1000);
$request->setTcpConnectTimeout(1000 * 1000);
$request->setTlsHandshakeTimeout(1000 * 1000);
$request->setTlsHandshakeTimeout(1000 * 1000);
$request->setTransferTimeout(1000 * 1000);
$request->setTransferTimeout(1000 * 1000);
$request->setHeader('cookie', wu_get_isset($_SERVER, 'HTTP_COOKIE', ''));
$request->setHeader('cookie', wu_get_isset($_SERVER, 'HTTP_COOKIE', ''));
$response = yield $client->request($request);
$response = yield $client->request($request);
$body = yield $response->getBody()->buffer();
$body = yield $response->getBody()->buffer();
$results[$url] = json_decode($body);
} catch (\Throwable $e) {
throw $e;
} // end try;
});
$results[ $url ] = json_decode($body);
} catch (\Throwable $e) {
throw $e;
}
}
);
return self::condense_results($results); // phpcs:ignore
});
}
);
$responses = \Amp\Promise\wait($coroutine);
return $responses;
} // end run;
}
/**
* Condense multiple results into one single result.
@ -228,19 +222,13 @@ class Async_Calls {
public static function condense_results($results) {
foreach ($results as $result) {
$status = wu_get_isset($result, 'success', false);
if ($status === false) {
return $result;
} // end if;
} // end foreach;
}
}
return true;
} // end condense_results;
} // end class Async_Calls;
}
}