| @@ -218,56 +218,8 @@ | ||
| 218 | 218 | return gmdate( $format, $scheduled ); |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | /** |
| 222 | - * Schedules a single event in the WordPress CRON to refresh the access token(s) | |
| 223 | - * shortly before the earliest one expires. | |
| 224 | - * | |
| 225 | - * Access tokens are short lived and refresh tokens are single use, so refreshing | |
| 226 | - * proactively avoids requests failing with an authentication error, and avoids | |
| 227 | - * several requests attempting to refresh using the same refresh token at once. | |
| 228 | - * | |
| 229 | - * @since 6.2.0 | |
| 230 | - */ | |
| 231 | - public function schedule_refresh_token_event() { | |
| 232 | - | |
| 233 | - // Clear any existing scheduled event. | |
| 234 | - $this->unschedule_refresh_token_event(); | |
| 235 | - | |
| 236 | - // Get the earliest expiry timestamp across all connected accounts. | |
| 237 | - $token_expires = $this->get_earliest_token_expiry(); | |
| 238 | - | |
| 239 | - // Bail only if no account has a refresh token. | |
| 240 | - if ( $token_expires === false ) { | |
| 241 | - return; | |
| 242 | - } | |
| 243 | - | |
| 244 | - // The default number of seconds before an access token expires to refresh it. | |
| 245 | - $seconds = 5 * MINUTE_IN_SECONDS; | |
| 246 | - | |
| 247 | - /** | |
| 248 | - * The number of seconds before an access token expires to refresh it. | |
| 249 | - * | |
| 250 | - * @since 6.2.0 | |
| 251 | - * | |
| 252 | - * @param int $seconds Seconds before expiry. | |
| 253 | - */ | |
| 254 | - $seconds = apply_filters( $this->base->plugin->filter_name . '_cron_refresh_token_seconds_before_expiry', $seconds ); | |
| 255 | - | |
| 256 | - // Determine when to refresh. | |
| 257 | - $refresh_at = $token_expires - $seconds; | |
| 258 | - | |
| 259 | - // If the refresh time is in the past, schedule the event for 1 minute from now. | |
| 260 | - if ( $refresh_at <= time() ) { | |
| 261 | - $refresh_at = time() + MINUTE_IN_SECONDS; | |
| 262 | - } | |
| 263 | - | |
| 264 | - // Schedule event. | |
| 265 | - wp_schedule_single_event( $refresh_at, $this->base->plugin->filter_name . '_refresh_token_cron' ); | |
| 266 | - | |
| 267 | - } | |
| 268 | - | |
| 269 | - /** | |
| 270 | 222 | * Unschedules the refresh token event in the WordPress CRON. |
| 271 | 223 | * |
| 272 | 224 | * @since 6.2.0 |
| 273 | 225 | */ |
| @@ -273,149 +225,7 @@ | ||
| 273 | 225 | */ |
| 274 | 226 | public function unschedule_refresh_token_event() { |
| 275 | 227 | |
| 276 | 228 | wp_clear_scheduled_hook( $this->base->plugin->filter_name . '_refresh_token_cron' ); |
| 277 | - | |
| 278 | - } | |
| 279 | - | |
| 280 | - /** | |
| 281 | - * Reschedules the refresh token event in the WordPress CRON, based on the expiry | |
| 282 | - * of the stored access token(s). | |
| 283 | - * | |
| 284 | - * Called on upgrade, so that sites connected before this event existed get one | |
| 285 | - * scheduled without having to reconnect, and whenever a token is issued or | |
| 286 | - * refreshed, so that each token schedules the refresh of its successor. | |
| 287 | - * | |
| 288 | - * @since 6.2.0 | |
| 289 | - */ | |
| 290 | - public function reschedule_refresh_token_event() { | |
| 291 | - | |
| 292 | - $this->schedule_refresh_token_event(); | |
| 293 | - | |
| 294 | - } | |
| 295 | - | |
| 296 | - /** | |
| 297 | - * Returns the scheduled refresh token event, if it exists | |
| 298 | - * | |
| 299 | - * @since 6.2.0 | |
| 300 | - * | |
| 301 | - * @return mixed bool | string | |
| 302 | - */ | |
| 303 | - public function get_refresh_token_event() { | |
| 304 | - | |
| 305 | - return wp_get_schedule( $this->base->plugin->filter_name . '_refresh_token_cron' ); | |
| 306 | - | |
| 307 | - } | |
| 308 | - | |
| 309 | - /** | |
| 310 | - * Returns the scheduled refresh token event's next date and time to run, if it exists | |
| 311 | - * | |
| 312 | - * @since 6.2.0 | |
| 313 | - * | |
| 314 | - * @param mixed $format Format Timestamp (false | php date() compat. string). | |
| 315 | - * @return mixed bool | int | string | |
| 316 | - */ | |
| 317 | - public function get_refresh_token_event_next_scheduled( $format = false ) { | |
| 318 | - | |
| 319 | - // Get timestamp for when the event will next run. | |
| 320 | - $scheduled = wp_next_scheduled( $this->base->plugin->filter_name . '_refresh_token_cron' ); | |
| 321 | - | |
| 322 | - // If no timestamp or we're not formatting the result, return it now. | |
| 323 | - if ( ! $scheduled || ! $format ) { | |
| 324 | - return $scheduled; | |
| 325 | - } | |
| 326 | - | |
| 327 | - // Return formatted date/time. | |
| 328 | - return gmdate( $format, $scheduled ); | |
| 329 | - | |
| 330 | - } | |
| 331 | - | |
| 332 | - /** | |
| 333 | - * Returns the earliest access token expiry timestamp across all connected accounts. | |
| 334 | - * | |
| 335 | - * Accounts holding a refresh token but no expiry timestamp are treated as due now, | |
| 336 | - * returning zero, so that an event is still scheduled for them. | |
| 337 | - * | |
| 338 | - * @since 6.2.0 | |
| 339 | - * | |
| 340 | - * @return mixed false | int false if no account has a refresh token. | |
| 341 | - */ | |
| 342 | - private function get_earliest_token_expiry() { | |
| 343 | - | |
| 344 | - $earliest = false; | |
| 345 | - | |
| 346 | - foreach ( $this->base->get_class( 'settings' )->get_accounts() as $account ) { | |
| 347 | - // Skip accounts with no refresh token, as there's nothing to refresh with. | |
| 348 | - if ( empty( $account['refresh_token'] ) ) { | |
| 349 | - continue; | |
| 350 | - } | |
| 351 | - | |
| 352 | - // An account with no stored expiry is treated as due now, so that an | |
| 353 | - // event is still scheduled for it. | |
| 354 | - $token_expires = ( empty( $account['token_expires'] ) ? 0 : (int) $account['token_expires'] ); | |
| 355 | - | |
| 356 | - // Compare against false explicitly, as a zero timestamp is a valid value | |
| 357 | - // here and would otherwise be treated as "not yet set". | |
| 358 | - if ( $earliest === false || $token_expires < $earliest ) { | |
| 359 | - $earliest = $token_expires; | |
| 360 | - } | |
| 361 | - } | |
| 362 | - | |
| 363 | - return $earliest; | |
| 364 | - | |
| 365 | - } | |
| 366 | - | |
| 367 | - /** | |
| 368 | - * Runs the refresh token CRON event, exchanging each stored refresh token that is | |
| 369 | - * due to expire for a new access token. | |
| 370 | - * | |
| 371 | - * The API class fires an action on a successful refresh, which stores the new | |
| 372 | - * access token, refresh token and expiry against the account. | |
| 373 | - * | |
| 374 | - * @since 6.2.0 | |
| 375 | - */ | |
| 376 | - public function refresh_token() { | |
| 377 | - | |
| 378 | - // Bail if the API class cannot refresh tokens, for example where the service | |
| 379 | - // issues a token that does not expire. | |
| 380 | - if ( ! method_exists( $this->base->get_class( 'api' ), 'refresh_token' ) ) { | |
| 381 | - return; | |
| 382 | - } | |
| 383 | - | |
| 384 | - // The default number of seconds before an access token expires to refresh it. | |
| 385 | - $seconds = 5 * MINUTE_IN_SECONDS; | |
| 386 | - | |
| 387 | - /** | |
| 388 | - * The number of seconds before an access token expires to refresh it. | |
| 389 | - * | |
| 390 | - * @since 6.2.0 | |
| 391 | - * | |
| 392 | - * @param int $seconds Seconds before expiry. | |
| 393 | - */ | |
| 394 | - $seconds = apply_filters( $this->base->plugin->filter_name . '_cron_refresh_token_seconds_before_expiry', $seconds ); | |
| 395 | - | |
| 396 | - // Iterate through accounts, refreshing any access token that is due to expire. | |
| 397 | - foreach ( $this->base->get_class( 'settings' )->get_accounts() as $account ) { | |
| 398 | - // Skip accounts with no refresh token, as there's nothing to refresh with. | |
| 399 | - if ( empty( $account['refresh_token'] ) ) { | |
| 400 | - continue; | |
| 401 | - } | |
| 402 | - | |
| 403 | - // Skip accounts whose access token isn't due to expire yet. | |
| 404 | - if ( ! empty( $account['token_expires'] ) && (int) $account['token_expires'] > ( time() + $seconds ) ) { | |
| 405 | - continue; | |
| 406 | - } | |
| 407 | - | |
| 408 | - // Refresh this account's access token. | |
| 409 | - $this->base->get_class( 'api' )->set_tokens( | |
| 410 | - $account['access_token'], | |
| 411 | - $account['refresh_token'], | |
| 412 | - $account['token_expires'] | |
| 413 | - ); | |
| 414 | - $this->base->get_class( 'api' )->refresh_token(); | |
| 415 | - } | |
| 416 | - | |
| 417 | - // Schedule the next event based on what is now stored. | |
| 418 | - $this->schedule_refresh_token_event(); | |
| 419 | 229 | |
| 420 | 230 | } |
| 421 | 231 | } |