المنطق الشرطي
يمكن إظهار الحقول أو إخفاؤها حسب الشروط.
العوامل
يمكن استخدام 8 عوامل مختلفة:
| العامل | الوصف |
|---|---|
== |
القيم متساوية |
!= |
القيم غير متساوية |
includes |
القيمة مضمنة في القيمة المرجعية |
!includes |
القيمة غير مضمنة في القيمة المرجعية |
empty |
القيمة فارغة |
!empty |
القيمة غير فارغة |
< |
القيمة أصغر من القيمة المرجعية |
> |
القيمة أكبر من القيمة المرجعية |
<= |
القيمة أصغر من القيمة المرجعية أو تساويها |
>= |
القيمة أكبر من القيمة المرجعية أو تساويها |
الشروط العامة
بشكل افتراضي، Blockstudio مزود بأربعة شروط عامة: نوع المنشور ومعرّف المنشور ودور المستخدم ومعرّف المستخدم.
{
"$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"
}
]
]
}
]
}
}
في المثال أعلاه، لن تظهر سمة النص في المحرر إلا إذا كان معرّف المنشور 1386 ونوع المنشور post.
لاحظ أن اصطلاح camelCase يُستخدم في مفاتيح النوع: postType وpostId وuserRole وuserId.
شروط 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"
}
]
]
}
]
}
}
ستظهر سمة النص إذا كان (معرّف المنشور 1386 ونوع المنشور post) أو (نوع المنشور jobs).
الشروط المخصصة
يمكن إضافة شروط مخصصة باستخدام المرشح blockstudio/blocks/conditions.
يُشغّل المرشح في PHP عند تحميل المحرر، ويُرجع قيمة واحدة لكل نوع شرط. تُقيَّم الشروط في المحرر، الذي يقارن تلك القيمة بـ 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
}
]
]
}
]
}
]
}
}
نظرًا إلى أن معرّفات السمات مرتبطة بنطاق عنصر المكرر الحالي، فلن تظهر سمة النص داخل المكرر إلا إذا كان مفتاح التبديل داخل المكرر مضبوطًا على 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"
}
]
]
}
]
}
]
}
}