Skip to content

Commit 6150185

Browse files
fix(sync): address review feedback on scoping, sync failure handling, and docs
1 parent 22519d9 commit 6150185

5 files changed

Lines changed: 43 additions & 13 deletions

File tree

‎UPGRADING.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Check that your environment is compatible with 6.0's requirements before upgradi
1616

1717
Update your customizations, if necessary:
1818

19-
- **No changes are required for the plugin's built-in features.** Login, logout, the callback handler, user session handling, and the User Sync background jobs continue to work as they did in 5.x. The settings stored in the WordPress admin are unchanged.
19+
- **No changes are required for the plugin's built-in features.** Login, logout, the callback handler, and user session handling continue to work as they did in 5.x. The settings stored in the WordPress admin are unchanged.
20+
- **User Sync now retries a failed event instead of always discarding it.** In 5.x a sync event that failed at the Management API was dropped from the queue regardless of the reason. In 6.x, a transient failure (an HTTP 429 rate limit, a 5xx server error, or a network error) keeps the event in the queue so the next cron pass retries it. Permanent failures (such as an HTTP 400 from an invalid profile) are still dropped, since retrying them would never succeed. No configuration change is needed, but a queue that previously drained to empty on every pass may now hold a retrying event until it succeeds.
2021
- **Custom code that calls the Management API has changed.** In v9 the old `wpAuth0()->getSdk()->management()` entry point is non-functional and will throw a `TypeError`. Use the new `wpAuth0()->getManagement()` accessor instead. It builds a Management client from the Domain, Client ID, and Client Secret you already configure in the plugin settings, and fetches and caches a client credentials token for you automatically.
2122

2223
```php
@@ -41,8 +42,9 @@ Update your customizations, if necessary:
4142

4243
- If your custom code calls the Management API directly, review the [auth0-php v9 migration guide](https://github.com/auth0/auth0-php/blob/v9/v9_MIGRATION_GUIDE.md) for the full set of changes. The most common adjustments are:
4344
- Sub-resources are reached by property access, not method calls: `->users->list()` rather than `->users()->getAll()`.
45+
- `users->list()` returns a `Pager` you iterate with `foreach`. It is a lazy iterator that makes further requests as you consume it, so `count()` and array access will not behave the way a plain array would.
4446
- Responses are typed objects instead of raw PSR-7 responses. Call `$response->jsonSerialize()` to get the same snake_case array the v8 `HttpResponse::decodeContent()` returned, or use the typed getters (`$response->getEmail()`).
4547
- Errors throw exceptions. A non-2xx response raises `Auth0\SDK\API\Management\Exceptions\Auth0ApiException` (use `getCode()` for the HTTP status), and transport-level failures raise `Auth0\SDK\API\Management\Exceptions\Auth0Exception`. There is no more `HttpResponse::wasSuccessful()` check.
4648
- Request parameters are camelCase typed objects. For example, creating a user takes a `CreateUserRequestContent` whose keys are `givenName` and `familyName` (not `given_name` / `family_name`), and the `connection` is set inside that object rather than passed as a separate argument.
47-
- Listing users only returns the paginated envelope when `includeTotals` is set to `true`. Omitting it yields an empty result, so pass `'includeTotals' => true` when you page through users.
49+
- The `includeTotals` parameter only controls whether the response envelope carries the count fields (`total`, `start`, `limit`, `length`). It defaults to `true` and does not affect whether users are returned, so you get results either way.
4850
- Some ticket methods were renamed: `tickets()->createPasswordChange()` is now `tickets->changePassword()`, and `tickets()->createEmailVerification()` is now `tickets->verifyEmail()`. The user id moves inside the request object as `userId`.

‎scoper.inc.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,16 @@
8383
'/^update_/',
8484
'/^delete_/',
8585
'/^set_/',
86+
// WordPress globals not matched by the prefixes above.
87+
'checked',
88+
'maybe_create_table',
89+
'nocache_headers',
90+
'submit_button',
8691
],
8792

88-
'expose-global-constants' => true,
89-
'expose-global-classes' => true,
90-
'expose-global-functions' => true,
93+
'expose-global-constants' => false,
94+
'expose-global-classes' => false,
95+
'expose-global-functions' => false,
9196

9297
'patchers' => [],
9398
];

‎src/Actions/Sync.php‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Auth0\SDK\API\Management\Users\Requests\{CreateUserRequestContent, ListUsersByEmailRequestParameters, UpdateUserRequestContent};
1010
use Auth0\WordPress\Database;
1111
use JsonSerializable;
12+
use Psr\Http\Client\ClientExceptionInterface;
1213
use Throwable;
1314
use WP_User;
1415

@@ -198,7 +199,7 @@ public function getDatabaseName(?string $dbConnection): ?string
198199
static $dbConnectionName = [];
199200

200201
if (isset($dbConnectionName[$dbConnection])) {
201-
return $dbConnectionName[$dbConnectionName];
202+
return $dbConnectionName[$dbConnection];
202203
}
203204

204205
if (null !== $dbConnection) {
@@ -221,6 +222,11 @@ public function onBackgroundMaintenance(): void
221222

222223
public function onBackgroundSync(): void
223224
{
225+
// Leave the queue intact until configured, rather than failing per item.
226+
if (! $this->isPluginReady()) {
227+
return;
228+
}
229+
224230
$database = $this->getPlugin()->database();
225231
$table = $database->getTableName(Database::CONST_TABLE_SYNC);
226232
$network = get_current_network_id();
@@ -256,14 +262,24 @@ public function onBackgroundSync(): void
256262
$this->eventUserUpdated($dbConnection, $payload);
257263
}
258264
}
259-
} catch (Throwable $throwable) {
260-
// A single failed item must not abort draining the rest of
261-
// the queue; the row is still removed below to avoid a
262-
// poison message blocking the queue indefinitely.
263-
try {
264-
error_log($throwable->getMessage());
265-
} catch (Throwable) {
265+
} catch (Auth0ApiException $auth0ApiException) {
266+
$status = $auth0ApiException->getCode();
267+
268+
// Keep the row for the next cron pass on transient failures.
269+
if (429 === $status || $status >= 500) {
270+
error_log($auth0ApiException->getMessage());
271+
272+
continue;
266273
}
274+
275+
error_log($auth0ApiException->getMessage());
276+
} catch (ClientExceptionInterface $clientException) {
277+
// Transport failures are transient, so keep the row for retry.
278+
error_log($clientException->getMessage());
279+
280+
continue;
281+
} catch (Throwable $throwable) {
282+
error_log($throwable->getMessage());
267283
}
268284
}
269285

‎src/Filters/Base.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Auth0\WordPress\Filters;
66

7+
use Auth0\SDK\API\Management\Wrapper\ManagementClient;
78
use Auth0\SDK\Auth0;
89
use Auth0\WordPress\Plugin;
910

@@ -44,6 +45,11 @@ final public function getPriority(string $event, int $default = 10, string $pref
4445
return $default;
4546
}
4647

48+
final public function getManagement(): ManagementClient
49+
{
50+
return $this->plugin->getManagement();
51+
}
52+
4753
final public function getSdk(): Auth0
4854
{
4955
return $this->plugin->getSdk();

‎src/Plugin.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ public function getManagement(): ManagementClient
195195
: null;
196196

197197
$this->management = new ManagementClient(new ManagementClientOptions(
198+
// Management always uses the tenant domain, not a custom domain, for its audience and token endpoint.
198199
domain: (string) $configuration->getDomain(),
199200
clientId: $configuration->getClientId(),
200201
clientSecret: $configuration->getClientSecret(),

0 commit comments

Comments
 (0)