본문으로 건너뛰기

조건부 로직

조건에 따라 필드를 표시하거나 숨길 수 있습니다.

연산자

사용할 수 있는 연산자는 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인 경우에만 편집기에 text 속성이 표시됩니다.

유형 키에는 camelCase 규칙을 사용합니다. postType, postId, userRole, userId를 참고하세요.

OR 조건

또는 조건을 만들려면 각 조건 그룹을 별도의 배열에 넣으세요.

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인 경우 text 속성이 표시됩니다.

사용자 지정 조건

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
            }
          ]
        ]
      }
    ]
  }
}

필요에 따라 전역 조건과 블록 조건을 조합할 수 있습니다.

반복기 컨텍스트

기본적으로 반복기 내부 속성의 조건은 현재 반복 중인 요소의 속성에 따라 달라집니다.

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는 현재 반복기 요소의 범위에서 지정되므로, 반복기 내부의 text 속성은 반복기 내부의 토글이 true로 설정된 경우에만 표시됩니다.

반복기 바깥의 토글을 확인하려면 조건에 "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"
                }
              ]
            ]
          }
        ]
      }
    ]
  }
}

필드 패턴 요리책