Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import uniffi.wp_api.PostType
import uniffi.wp_api.PostTypeCapabilities
import uniffi.wp_api.PostTypeSupports
import uniffi.wp_api.WpErrorCode
import uniffi.wp_api.postTypeSupports
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
Expand All @@ -26,7 +27,7 @@ class PostTypesEndpointTest {
val postTypesPost = client.request { requestBuilder ->
requestBuilder.postTypes().retrieveWithEditContext(PostType.Post)
}.assertSuccessAndRetrieveData().data
assert(postTypesPost.supports.map[PostTypeSupports.Title]!!)
assert(postTypeSupports(postTypesPost.supports, PostTypeSupports.Title))
assertFalse(postTypesPost.capabilities[PostTypeCapabilities.EditPosts]!!.isEmpty())
}

Expand Down
10 changes: 10 additions & 0 deletions native/swift/Sources/wordpress-api/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,13 @@ extension PostType: ExpressibleByStringLiteral {
self.init(stringLiteral)
}
}

public extension PostTypeSupportsMap {
func supports(_ feature: PostTypeSupports) -> Bool {
postTypeSupports(supportsMap: self, feature: feature)
}

func supports(_ feature: String) -> Bool {
postTypeSupports(supportsMap: self, feature: .custom(feature))
}
}
26 changes: 24 additions & 2 deletions wp_api/src/post_types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::impl_as_query_value_from_to_string;
use crate::{JsonValue, impl_as_query_value_from_to_string};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::str::FromStr;
Expand Down Expand Up @@ -97,7 +97,29 @@ pub struct PostTypeSupportsMap {
#[serde(deserialize_with = "deserialize_empty_array_or_hashmap")]
#[serde(flatten)]
#[serde(rename = "supports")]
pub map: HashMap<PostTypeSupports, bool>,
pub map: HashMap<PostTypeSupports, JsonValue>,
}

impl PostTypeSupportsMap {
/// Check if the post type supports a specific feature by checking if the key is present.
///
/// Note: This only checks for key presence, not the associated value. WordPress typically
/// includes a feature in the map with a `true` value when supported, and omits it entirely
/// when not supported. The value can also be an object with additional configuration (e.g.,
/// `editor` may have nested settings). We assume that if a key is present, the feature is
/// supported, regardless of the actual value.
pub fn supports(&self, feature: &PostTypeSupports) -> bool {
self.map.contains_key(feature)
}
}

/// Check if a post type supports a specific feature by checking if the key is present.
///
/// Note: This only checks for key presence, not the associated value. See
/// `PostTypeSupportsMap::supports` for details on this assumption.
#[uniffi::export]
fn post_type_supports(supports_map: &PostTypeSupportsMap, feature: PostTypeSupports) -> bool {
supports_map.supports(&feature)
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, uniffi::Record)]
Expand Down
5 changes: 1 addition & 4 deletions wp_api_integration_tests/tests/test_post_types_immut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ async fn retrieve_post_types_with_edit_context(
// It's possible that we might have more test sites in the future and some of their
// post types might not support `Title` in which case it's perfectly fine to completely
// remove this assertion.
assert_eq!(
post_type.supports.map.get(&PostTypeSupports::Title),
Some(true).as_ref()
);
assert!(post_type.supports.supports(&PostTypeSupports::Title));
// All post types in our current testing sites have `EditPost` capability, so we use this
// assertion to verify that we are able to parse `capabilities` field properly.
//
Expand Down