条件付きロジック
条件に応じてフィールドを表示または非表示にできます。
演算子
使用できる演算子は8種類あります:
| 演算子 | 説明 |
|---|---|
== |
値が等しい |
!= |
値が等しくない |
includes |
値が参照値に含まれる |
!includes |
値が参照値に含まれない |
empty |
値が空 |
!empty |
値が空ではない |
< |
値が参照値より小さい |
> |
値が参照値より大きい |
<= |
値が参照値以下 |
>= |
値が参照値以上 |
グローバル条件
デフォルトでは、Blockstudioには投稿タイプ、投稿ID、ユーザーロール、ユーザーIDの4つのグローバル条件があります。
{
"$schema": "https://blockstudio.dev/schema/block",
"name": "blockstudio/native",
"title": "Native Block",
"blockstudio": {
"attributes": [
{
"id": "message",
"type": "text",
"label": "My message",
"conditions": [
[
{
"type": "postId",
"operator": "==",
"value": "1386"
},
{
"type": "postType",
"operator": "==",
"value": "post"
}
]
]
}
]
}
}
上記の例では、投稿IDが1386かつ投稿タイプがpostの場合にのみ、text属性がエディターに表示されます。
typeキーにはcamelCase規則が使用されることに注意してください: postType、postId、userRole、userId。
OR条件
OR条件を作成するには、各条件グループをそれぞれ独自の配列に移動します:
{
"blockstudio": {
"attributes": [
{
"id": "message",
"type": "text",
"label": "My message",
"conditions": [
[
{
"type": "postId",
"operator": "==",
"value": "1386"
},
{
"type": "postType",
"operator": "==",
"value": "post"
}
],
[
{
"type": "postType",
"operator": "==",
"value": "jobs"
}
]
]
}
]
}
}
投稿IDが1386かつ投稿タイプがpost、または投稿タイプがjobsの場合に、text属性が表示されます。
カスタム条件
blockstudio/blocks/conditionsフィルターを使ってカスタム条件を追加できます。
エディターの読み込み時にPHPでフィルターが実行され、条件タイプごとに1つの値を返します。条件はエディターで評価され、返された値と条件のvalueを演算子で比較します。
add_filter('blockstudio/blocks/conditions', function($conditions) {
// Check if the current user purchased the product.
$conditions['purchasedProduct'] = user_has_purchased(123) ? 'yes' : 'no';
return $conditions;
});
すべての状態について空でない文字列を返してください。値が空または未設定の条件タイプはスキップされるため、フィールドは表示されたままになります。
{
"blockstudio": {
"attributes": [
{
"id": "message",
"type": "text",
"label": "My message",
"conditions": [
[
{
"type": "purchasedProduct",
"operator": "==",
"value": "yes"
}
]
]
}
]
}
}
ブロック条件
他の属性に依存する条件を設定できます。typeキーを設定する代わりに、照合する属性のidを使用します:
{
"blockstudio": {
"attributes": [
{
"type": "toggle",
"id": "copyButton",
"label": "Copy Button"
},
{
"type": "text",
"id": "copyButtonText",
"label": "Copy Button Text",
"default": "Copy",
"conditions": [
[
{
"id": "copyButton",
"operator": "==",
"value": true
}
]
]
}
]
}
}
必要に応じて、グローバル条件とブロック条件を組み合わせられます。
リピーターのコンテキスト
デフォルトでは、リピーター内の属性の条件は、現在繰り返されている要素の属性に依存します。
{
"blockstudio": {
"attributes": [
{
"type": "toggle",
"id": "toggle",
"label": "Toggle"
},
{
"type": "repeater",
"id": "repeater",
"label": "Repeater",
"attributes": [
{
"type": "toggle",
"id": "toggle",
"label": "Toggle"
},
{
"type": "text",
"id": "text",
"label": "Text",
"conditions": [
[
{
"id": "toggle",
"operator": "==",
"value": true
}
]
]
}
]
}
]
}
}
属性IDは現在のリピーター要素内でスコープされるため、リピーター内のtext属性は、リピーター内のトグルがtrueに設定されている場合にのみ表示されます。
リピーターの外側にあるトグルと照合するには、条件に"context": "main"を追加します:
{
"blockstudio": {
"attributes": [
{
"type": "toggle",
"id": "toggle",
"label": "Toggle"
},
{
"type": "repeater",
"id": "repeater",
"label": "Repeater",
"attributes": [
{
"type": "text",
"id": "text",
"label": "Text",
"conditions": [
[
{
"id": "toggle",
"operator": "==",
"value": true,
"context": "main"
}
]
]
}
]
}
]
}
}