statuses = new StatusRegistry( null, $reconciler ); } /** * Registering the status route. */ protected function routes(): array { return array( '/statuses' => array( 'methods' => 'POST', 'callback' => array( $this, 'receive' ), 'permission_callback' => array( $this, 'require_token' ), ), ); } /** * Storing a pushed status list. * * @param WP_REST_Request>|WP_REST_Request $request Incoming request. * * @return WP_REST_Response Acknowledgement. */ public function receive( $request ): WP_REST_Response { $payload = $request->get_json_params(); if ( ! is_array( $payload ) ) { return Response::text( 'Error: Empty JSON request data', 400 ); } $this->remember_platform( $payload ); $active = $this->status_list( $payload, 'activeStatuses' ); $deactivated = $this->status_list( $payload, 'deactivatedStatuses' ); list( $active, $deactivated ) = $this->statuses->prepare( $active, $deactivated ); if ( array() !== $active ) { Options::set( StatusRegistry::ACTIVE_OPTION, $active ); // Seed the published <-> 'publish' pair so a fresh site syncs published stories. Options::set( 'status_export_publish', (string) Config::PUBLISHED_STATUS_ID ); Options::set( StatusMapper::option_key( Config::PUBLISHED_STATUS_ID ), 'publish' ); } if ( array() !== $deactivated ) { Options::set( StatusRegistry::DEACTIVATED_OPTION, $deactivated ); } return Response::accepted_no_action(); } /** * Recording which Kordiam platform this site is. * * @param array $payload Decoded request body. */ private function remember_platform( array $payload ): void { $platform = $payload['platform'] ?? null; if ( is_numeric( $platform ) ) { Options::set( PublicationResolver::PLATFORM_OPTION, (int) $platform ); } } /** * Reading one of the status lists off the payload. * * @param array $payload Decoded request body. * @param string $key List key. * * @return array> The statuses. */ private function status_list( array $payload, string $key ): array { $list = $payload[ $key ] ?? null; if ( ! is_array( $list ) ) { return array(); } $statuses = array(); foreach ( $list as $status ) { if ( is_array( $status ) && isset( $status['id'] ) ) { $statuses[] = $status; } } return $statuses; } }