$issues Issues from BlockSerializer. * @return WP_Error|null WP_Error when at least one error is present, else null. * * @since 1.2.0 */ public static function to_wp_error( array $issues ): ?WP_Error { $errors = self::messages_by_severity( $issues, BlockSerializer::SEVERITY_ERROR ); if ( $errors === [] ) { return null; } $summary = sprintf( /* translators: %d: number of block validation errors. */ _n( 'The content could not be saved because of %d block validation error. Fix it and try again:', 'The content could not be saved because of %d block validation errors. Fix them and try again:', count( $errors ), 'albert-ai-butler' ), count( $errors ) ); $message = $summary . ' ' . implode( ' ', $errors ); return new WP_Error( 'block_validation_failed', $message, [ 'status' => 400, 'errors' => $errors, ] ); } /** * Extract the warning-severity messages as a plain list of strings. * * @param array $issues Issues from BlockSerializer. * @return array Warning messages (the post/page was still saved). * * @since 1.2.0 */ public static function warning_messages( array $issues ): array { return self::messages_by_severity( $issues, BlockSerializer::SEVERITY_WARNING ); } /** * Collect the messages for issues of a given severity. * * @param array $issues Issues from BlockSerializer. * @param string $severity Severity to keep. * @return array Matching messages, re-indexed. * * @since 1.2.0 */ private static function messages_by_severity( array $issues, string $severity ): array { $messages = []; foreach ( $issues as $issue ) { if ( ( $issue['severity'] ?? '' ) === $severity ) { $messages[] = (string) ( $issue['message'] ?? '' ); } } return $messages; } }