'POST', 'callback' => array( __CLASS__, 'receive' ), 'permission_callback' => array( __CLASS__, 'authorize' ), ) ); } /** * The permission check: the signature, then the clock. * * @since 1.0.0 * * @param WP_REST_Request $request The request. * @return true|WP_Error */ public static function authorize( $request ) { $verdict = self::verdict( $request ); if ( ThirdDraft_Signature::OK === $verdict ) { return true; } return self::error( ThirdDraft_Answers::refused( $verdict ) ); } /** * WordPress refuses a body that is not JSON before any permission check, * with its own 400. For this route the answer is the plugin's own instead: * the signature first, then `thirddraft_invalid`. * * @since 1.0.0 * * @param mixed $response The response so far. * @param array $handler The route's handler. * @param WP_REST_Request $request The request. * @return mixed */ public static function before_callbacks( $response, $handler, $request ) { if ( ! is_wp_error( $response ) || ! isset( $handler['callback'] ) || array( __CLASS__, 'receive' ) !== $handler['callback'] ) { return $response; } $verdict = self::verdict( $request ); if ( ThirdDraft_Signature::OK !== $verdict ) { return self::error( ThirdDraft_Answers::refused( $verdict ) ); } return self::error( ThirdDraft_Answers::invalid( 'body' ) ); } /** * Takes a test or an article; answers a read of how an article's post * stands; takes an update in place only as ThirdDraft_Post_Writer allows. * * @since 1.0.0 * @since 1.2.0 `article.standing` and `article.update`. * * @param WP_REST_Request $request The request, already verified. * @return WP_REST_Response|WP_Error */ public static function receive( $request ) { $read = ThirdDraft_Event::parse( $request->get_body(), $request->get_header( 'x_thirddraft_event' ), $request->get_header( 'x_thirddraft_delivery' ) ); if ( ! $read['ok'] ) { ThirdDraft_Store::note_arrival( 'other' ); return self::error( ThirdDraft_Answers::invalid( $read['reason'] ) ); } $event = $read['event']; if ( ThirdDraft_Event::TEST === $event['type'] ) { ThirdDraft_Store::note_arrival( 'test' ); return self::respond( ThirdDraft_Answers::test() ); } // A read writes nothing — no lock, no event kept, not even the status line — so it never stands in an article's way. if ( ThirdDraft_Event::STANDING === $event['type'] ) { return self::respond( ThirdDraft_Post_Writer::standing( $event ) ); } self::keep_working(); $lock = 'article_' . (int) $event['article']['id']; $token = ThirdDraft_Lock::acquire( $lock, self::LOCK_SECONDS ); if ( false === $token ) { return self::error( ThirdDraft_Answers::busy() ); } // Released even if WordPress dies part-way; a no-op once released below. register_shutdown_function( array( 'ThirdDraft_Lock', 'release' ), $lock, $token ); try { $answer = ThirdDraft_Post_Writer::take( $event ); } catch ( Exception $e ) { $answer = ThirdDraft_Answers::failed(); } catch ( Error $e ) { $answer = ThirdDraft_Answers::failed(); } ThirdDraft_Lock::release( $lock, $token ); if ( 200 === $answer['status'] ) { ThirdDraft_Store::note_arrival( 'article' ); return self::respond( $answer ); } ThirdDraft_Store::note_arrival( 'other' ); return self::error( $answer ); } /** * What the signature says, noting a refusal of what looked like ThirdDraft. * * @param WP_REST_Request $request The request. * @return string */ private static function verdict( $request ) { $header = $request->get_header( 'x_thirddraft_signature' ); $verdict = ThirdDraft_Signature::verify( $header, $request->get_body(), ThirdDraft_Store::secret(), time() ); if ( ThirdDraft_Signature::OK !== $verdict && 0 === strpos( (string) $request->get_header( 'user_agent' ), 'ThirdDraft-Webhook/' ) ) { ThirdDraft_Store::note_refusal( $verdict, (int) ThirdDraft_Signature::skew( $header, time() ) ); } return $verdict; } /** * An article with many pictures can take longer than ThirdDraft waits; * the work goes on, and the retry finds it done. * * @return void */ private static function keep_working() { ignore_user_abort( true ); if ( function_exists( 'set_time_limit' ) && false === strpos( (string) ini_get( 'disable_functions' ), 'set_time_limit' ) ) { // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged, WordPress.PHP.NoSilencedErrors.Discouraged -- copying pictures can take minutes; where the host forbids a longer limit, its own applies. @set_time_limit( self::LOCK_SECONDS ); } } /** * A success. * * @param array{status: int, body: array} $answer The answer. * @return WP_REST_Response */ private static function respond( $answer ) { return new WP_REST_Response( $answer['body'], $answer['status'] ); } /** * An error answer, with its `thirddraft_` code. * * @param array{status: int, body: array} $answer The answer. * @return WP_Error */ private static function error( $answer ) { $code = (string) $answer['body']['code']; $messages = array( 'thirddraft_signature' => __( 'The request is not signed with this site’s connection.', 'thirddraft' ), 'thirddraft_clock' => __( 'The request was signed more than five minutes away from this site’s clock.', 'thirddraft' ), 'thirddraft_invalid' => __( 'This is not an article the plugin can take.', 'thirddraft' ), 'thirddraft_busy' => __( 'This article is already being taken. Send it again in a moment.', 'thirddraft' ), 'thirddraft_pictures' => __( 'A picture could not be copied into the Media Library. Send it again later.', 'thirddraft' ), 'thirddraft_failed' => __( 'WordPress could not save the post. Send it again later.', 'thirddraft' ), 'thirddraft_gone' => __( 'The post for this article is not here, so it was not updated.', 'thirddraft' ), 'thirddraft_not_live' => __( 'The post for this article is not published, so it was not updated.', 'thirddraft' ), 'thirddraft_edited' => __( 'The post for this article was edited since ThirdDraft read it, so it was not updated.', 'thirddraft' ), ); $message = isset( $messages[ $code ] ) ? $messages[ $code ] : (string) $answer['body']['message']; return new WP_Error( $code, $message, array( 'status' => (int) $answer['status'] ) ); } }