Przejdź do treści

Logika warunkowa

Pola można pokazywać lub ukrywać w zależności od warunków.

Operatory

Dostępnych jest 8 różnych operatorów:

Operator Opis
== Wartości są równe
!= Wartości nie są równe
includes Wartość zawiera się w wartości referencyjnej
!includes Wartość nie zawiera się w wartości referencyjnej
empty Wartość jest pusta
!empty Wartość nie jest pusta
< Wartość jest mniejsza od wartości referencyjnej
> Wartość jest większa od wartości referencyjnej
<= Wartość jest mniejsza lub równa wartości referencyjnej
>= Wartość jest większa lub równa wartości referencyjnej

Warunki globalne

Domyślnie Blockstudio zawiera 4 warunki globalne: typ wpisu, ID wpisu, rola użytkownika i ID użytkownika.

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

W powyższym przykładzie atrybut tekstowy pojawi się w edytorze tylko wtedy, gdy ID wpisu to 1386 ORAZ typ wpisu to post.

Uwaga: w konwencji camelCase zapisuje się klucze typu: postType, postId, userRole, userId.

Warunki OR

Aby utworzyć warunki or, umieść każdą grupę warunków we własnej tablicy:

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

Atrybut tekstowy pojawi się, jeśli (ID wpisu to 1386 ORAZ typ wpisu to post) LUB (typ wpisu to jobs).

Warunki niestandardowe

Własne warunki można dodawać za pomocą filtra blockstudio/blocks/conditions. Filtr działa w PHP podczas ładowania edytora i zwraca jedną wartość dla każdego typu warunku. Warunki są oceniane w edytorze, który porównuje tę wartość z value warunku za pomocą operatora.

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

Zwróć niepusty ciąg dla każdego stanu. Typ warunku, którego wartość jest pusta lub nie istnieje, zostaje pominięty, więc pole pozostanie widoczne.

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

Warunki bloków

Możesz ustawić warunki zależne od innych atrybutów. Zamiast klucza type użyj id atrybutu, który chcesz sprawdzić:

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

W razie potrzeby możesz łączyć warunki globalne z warunkami bloków.

Kontekst powtarzacza

Domyślnie warunki dla atrybutów wewnątrz powtarzaczy zależą od atrybutów aktualnie powtarzanego elementu.

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

Ponieważ identyfikatory atrybutów są przypisane do aktualnego elementu powtarzacza, atrybut tekstowy wewnątrz powtarzacza pojawi się tylko wtedy, gdy przełącznik wewnątrz powtarzacza jest ustawiony na true.

Aby sprawdzić przełącznik poza powtarzaczem, dodaj "context": "main" do warunku:

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

Przepisy na wzorce pól