isset( $saved['id'] ) ? (string) $saved['id'] : '', 'secret' => '', 'url' => isset( $saved['url'] ) ? (string) $saved['url'] : '', 'registered_at' => isset( $saved['registered_at'] ) ? (int) $saved['registered_at'] : 0, 'error' => isset( $saved['error'] ) ? (string) $saved['error'] : '', ); if ( ! empty( $saved['secret'] ) ) { $plain = SecretStore::for_site()->decrypt( $saved['secret'] ); $out['secret'] = null === $plain ? '' : $plain; } return $out; } /** * Whether a working registration for the current URL exists. * * @return bool */ public static function is_registered() { $saved = self::saved(); return '' !== $saved['id'] && '' !== $saved['secret'] && self::url() === $saved['url']; } /** * Registers (or re-registers) the webhook. Old subscriptions for this URL are removed * first, so ABS never delivers twice or with a secret this store no longer has. * * @return bool Success. On failure the reason is saved for the Overview tab. */ public static function register() { $url = self::url(); if ( 0 !== stripos( $url, 'https://' ) ) { return self::fail( __( 'Live status updates need the store to use HTTPS. Shipment statuses are still checked every 30 minutes.', 'abs-courier' ) ); } $client = new AbsClient( Credentials::get() ); try { self::remove_existing( $client, $url ); $created = $client->post( 'webhooks/me', array( 'url' => $url, 'events' => self::EVENTS, /* translators: %s: store address. */ 'description' => sprintf( __( 'WooCommerce store %s', 'abs-courier' ), wp_parse_url( home_url(), PHP_URL_HOST ) ), ) ); } catch ( AbsError $e ) { /* translators: %s: reason from ABS. */ return self::fail( sprintf( __( 'ABS Courier could not register live status updates: %s Shipment statuses are still checked every 30 minutes.', 'abs-courier' ), $e->getMessage() ) ); } $id = is_array( $created ) && isset( $created['id'] ) ? (string) $created['id'] : ''; $secret = is_array( $created ) && isset( $created['signingSecret'] ) ? (string) $created['signingSecret'] : ''; if ( '' === $id || '' === $secret ) { return self::fail( __( 'ABS Courier did not return the webhook details. Shipment statuses are still checked every 30 minutes.', 'abs-courier' ) ); } update_option( self::OPTION, array( 'id' => $id, 'secret' => SecretStore::for_site()->encrypt( $secret ), 'url' => $url, 'registered_at' => time(), 'error' => '', ), false ); return true; } /** * Removes the subscription at ABS and forgets it locally. * * @return void */ public static function unregister() { $saved = self::saved(); if ( '' !== $saved['id'] && '' !== Credentials::get() ) { try { ( new AbsClient( Credentials::get() ) )->delete( 'webhooks/me/' . rawurlencode( $saved['id'] ) ); } catch ( AbsError $e ) { // Already gone or unreachable: nothing more the store can do. unset( $e ); } } delete_option( self::OPTION ); } /** * Deletes this store's earlier subscriptions at ABS (same URL). * * @param AbsClient $client Client. * @param string $url Webhook URL. * @return void * @throws AbsError When ABS cannot be reached. */ private static function remove_existing( AbsClient $client, $url ) { $list = $client->get( 'webhooks/me', array( 'page' => 1, 'limit' => 100, ) ); $rows = array(); foreach ( array( 'content', 'items', 'data' ) as $key ) { if ( isset( $list[ $key ] ) && is_array( $list[ $key ] ) ) { $rows = $list[ $key ]; break; } } foreach ( $rows as $row ) { if ( isset( $row['id'], $row['url'] ) && $url === $row['url'] ) { $client->delete( 'webhooks/me/' . rawurlencode( (string) $row['id'] ) ); } } } /** * Saves a failure reason and returns false. * * @param string $reason Reason. * @return bool */ private static function fail( $reason ) { update_option( self::OPTION, array( 'id' => '', 'url' => self::url(), 'error' => $reason, ), false ); return false; } }