$state, 'verifier' => $verifier, ), self::ATTEMPT_SECONDS ); $query = array( 'site_url' => home_url( '/' ), 'receive_url' => self::receive_url(), 'return_url' => self::return_url(), 'state' => $state, 'challenge' => ThirdDraft_Pkce::challenge( $verifier ), 'challenge_method' => 'S256', 'plugin_version' => THIRDDRAFT_VERSION, 'wp_version' => get_bloginfo( 'version' ), ); return self::app_url( '/connect/wordpress' ) . '?' . http_build_query( $query, '', '&', PHP_QUERY_RFC3986 ); } /** * Finishes connecting, once ThirdDraft sent the browser back. * * @since 1.0.0 * * @param string $code The one-time code. * @param string $state The state ThirdDraft sent back. * @return string 'connected', or why not: 'expired', 'mismatch', 'busy', 'refused', 'unreachable'. */ public static function finish( $code, $state ) { $attempt = self::take_attempt( $state ); if ( ! is_array( $attempt ) ) { return null === $attempt ? 'expired' : 'mismatch'; } $answer = self::request( '/api/wordpress/exchange', array( 'code' => (string) $code, 'verifier' => $attempt['verifier'], ), false ); if ( is_wp_error( $answer ) ) { return 'unreachable'; } $status = (int) wp_remote_retrieve_response_code( $answer ); $body = json_decode( (string) wp_remote_retrieve_body( $answer ), true ); if ( 429 === $status ) { return 'busy'; } if ( 200 !== $status || ! is_array( $body ) || empty( $body['secret'] ) || ! is_string( $body['secret'] ) || empty( $body['destination'] ) || ! is_string( $body['destination'] ) ) { return 'refused'; } $site = isset( $body['site'] ) && is_array( $body['site'] ) ? $body['site'] : array(); ThirdDraft_Store::forget_connection(); ThirdDraft_Store::save_connection( array( 'secret' => $body['secret'], 'destination' => $body['destination'], 'site' => isset( $site['id'] ) && is_string( $site['id'] ) ? $site['id'] : '', 'domain' => isset( $site['domain'] ) && is_string( $site['domain'] ) ? $site['domain'] : '', 'connected_at' => time(), 'connected_by' => get_current_user_id(), 'pending_since' => time(), ) ); return 'connected'; } /** * Says the person cancelled at ThirdDraft, and forgets the attempt. * * @since 1.0.0 * * @param string $state The state ThirdDraft sent back. * @return void */ public static function cancelled( $state ) { self::take_attempt( $state ); } /** * Asks ThirdDraft to send its test again. * * @since 1.0.0 * * @return string 'queued', 'busy', 'refused' or 'unreachable'. */ public static function retest() { $connection = ThirdDraft_Store::connection(); if ( empty( $connection ) ) { return 'refused'; } $answer = self::request( '/api/wordpress/retest', self::signed_body( (string) $connection['destination'], 'retest' ), true ); $result = is_wp_error( $answer ) ? 'unreachable' : self::retest_result( (int) wp_remote_retrieve_response_code( $answer ) ); if ( 'queued' === $result ) { $connection['pending_since'] = time(); ThirdDraft_Store::save_connection( $connection ); } return $result; } /** * What ThirdDraft's answer to Try again means. * * @since 1.0.0 * * @param int $status The HTTP status. * @return string 'queued', 'busy', 'refused' or 'unreachable'. */ public static function retest_result( $status ) { if ( $status >= 200 && $status < 300 ) { return 'queued'; } if ( 429 === $status ) { return 'busy'; } return $status >= 500 || 0 === $status ? 'unreachable' : 'refused'; } /** * The body of a signed request to ThirdDraft: the connection and what * to do with it, so a signed body is never good for another action. * * @since 1.0.0 * * @param string $destination The connection's id at ThirdDraft. * @param string $action 'retest' or 'disconnect'. * @return array{destination: string, action: string} */ public static function signed_body( $destination, $action ) { return array( 'destination' => (string) $destination, 'action' => (string) $action, ); } /** * Tells ThirdDraft to stop sending, and forgets the secret whatever it * answers. Posts already made stay. * * @since 1.0.0 * * @return void */ public static function disconnect() { $connection = ThirdDraft_Store::connection(); if ( ! empty( $connection ) ) { self::request( '/api/wordpress/disconnect', self::signed_body( (string) $connection['destination'], 'disconnect' ), true ); } ThirdDraft_Store::forget_connection(); } /** * A request to ThirdDraft: JSON, fifteen seconds, TLS verified, no * redirects; signed with the secret when asked. * * @param string $path The path at ThirdDraft. * @param array $payload The body. * @param bool $signed Whether to sign it. * @return array|WP_Error */ private static function request( $path, $payload, $signed ) { $body = (string) wp_json_encode( $payload ); $headers = array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', ); if ( $signed ) { $headers['X-ThirdDraft-Signature'] = ThirdDraft_Signature::sign( ThirdDraft_Store::secret(), time(), $body ); } return wp_remote_post( self::app_url( $path ), array( 'timeout' => 15, 'redirection' => 0, 'sslverify' => true, 'user-agent' => self::user_agent(), 'headers' => $headers, 'body' => $body, ) ); } /** * How the plugin names itself to ThirdDraft. * * @since 1.0.0 * * @return string */ public static function user_agent() { return 'ThirdDraft-WordPress/' . THIRDDRAFT_VERSION . '; ' . home_url( '/' ); } /** * The current user's connect attempt, removed as it is read — it is * good for one return only — when the state matches. * * @param string $state The state ThirdDraft sent back. * @return array{state: string, verifier: string}|false|null Null when there is none; false when the state does not match. */ private static function take_attempt( $state ) { $attempt = get_transient( self::attempt_key() ); if ( ! is_array( $attempt ) || empty( $attempt['state'] ) || empty( $attempt['verifier'] ) ) { return null; } delete_transient( self::attempt_key() ); if ( ! is_string( $state ) || ! hash_equals( (string) $attempt['state'], $state ) ) { return false; } return $attempt; } /** * The transient that holds the current user's attempt. * * @return string */ private static function attempt_key() { return 'thirddraft_connect_' . get_current_user_id(); } }