跳转到正文

条件逻辑

字段可以根据条件显示或隐藏。

运算符

共有 8 种可用的运算符:

运算符 描述
== 值相等
!= 值不相等
includes 值包含在参照值中
!includes 值不包含在参照值中
empty 值为空
!empty 值不为空
< 值小于参照值
> 值大于参照值
<= 值小于或等于参照值
>= 值大于或等于参照值

全局条件

默认情况下,Blockstudio自带 4 个全局条件:文章类型、文章 ID、用户角色和用户 ID。

JSON
{
  "$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时,文本属性才会在编辑器中显示。

请注意,类型键采用 camelCase 命名约定:postType、postId、userRole、userId。

或条件

要创建或条件,请将每个条件组移入各自的数组:

JSON
{
  "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)时,文本属性会显示。

自定义条件

可以使用blockstudio/blocks/conditions筛选器添加自定义条件。 编辑器加载时,筛选器会在PHP中运行,并为每种条件类型返回一个值。条件在编辑器中求值,编辑器会使用运算符将该值与条件的value进行比较。

PHP
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;
});

每种状态都返回非空字符串。如果某种条件类型的值为空或缺失,该条件会被跳过,因此字段会保持可见。

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "id": "message",
        "type": "text",
        "label": "My message",
        "conditions": [
          [
            {
              "type": "purchasedProduct",
              "operator": "==",
              "value": "yes"
            }
          ]
        ]
      }
    ]
  }
}

区块条件

你可以设置依赖于其他属性的条件。无需设置 type 键,只需使用要检查的属性的 id:

JSON
{
  "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
            }
          ]
        ]
      }
    ]
  }
}

你可以根据需要将全局条件与区块条件组合使用。

Repeater 上下文

默认情况下,Repeater 内属性的条件取决于当前重复元素的属性。

JSON
{
  "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 的作用域限定在当前 Repeater 元素内,只有当 Repeater 内部的切换开关设为 true 时,Repeater 内的文本属性才会显示。

要检查 Repeater 外部的切换开关,请在条件中添加 "context": "main":

JSON
{
  "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"
                }
              ]
            ]
          }
        ]
      }
    ]
  }
}

字段模式手册