تخصيصات محرر الموقع مقابل كود القالب: حالة قاعدة البيانات مقابل المصدر الموثوق للحقيقة

عند العمل مع قوالب المكونات البرمجية الحديثة لمحرر الموقع الكامل (FSE) في WordPress، فإن أحد أهم المفاهيم المعمارية التي يجب فهمها هو الفارق بين تخصيصات القوالب المحفوظة في قاعدة البيانات وملفات القوالب الفعلية المخزنة داخل قاعدة كود القالب.

1. الإجابة المباشرة: تجاوزات قاعدة البيانات مقابل نظام الملفات كمصدر وحيد للحقيقة

Yes, your intuition is completely correct. When you log in as an administrator, navigate to Appearance > Editor (the Site Editor), and modify a template, such as inserting a Featured Image block into single blog posts or pages, those changes are stored exclusively inside that specific WordPress instance’s database.

The underlying HTML template files on disk (for instance, inside wp-content/themes/baseline/templates/single.html) لا يمسها محرر موقع WordPress أبداً. يفرض هذا تمييزاً معمارياً جوهرياً بين حالة قاعدة البيانات أثناء التشغيل وشفرة القالب الخاضعة للتحكم بالإصدارات.

2. تحت الغطاء: التسلسل الهرمي للحجب (Shadow Hierarchy)

لفهم سبب حدوث ذلك، انظر كيف يعالج WordPress قوالب الكتل في وقت التشغيل:

  • خط الأساس لنظام الملفات: يوفر قالب الكتل قوالب افتراضية كملفات HTML مسطحة تحتوي على ترميز الكتل داخل مجلد القالب: templates/single.html, templates/page.html, and templates/index.html.
  • تخصيصات قاعدة البيانات: عند تعديل قالب وحفظه في محرر الموقع، يُنشئ WordPress سجلاً لنوع المقالات المخصص (CPT) في wp_posts الجدول حيث post_type = 'wp_template' (أو 'wp_template_part' for headers and footers).
  • أسبقية المعالجة والحل: عند وصول طلب، يستعلم WordPress من قاعدة البيانات أولاً. وإذا كان wp_template يوجد مقال باسم لطيف مطابق (على سبيل المثال، single)، يقوم WordPress بتحميل نسخة قاعدة البيانات ويتجاهل تماماً الملف الموجود على القرص. يُحجب سجل قاعدة البيانات ملف الشفرة فعلياً.

3. الفروق الهندسية

إن التعامل مع القوالب كسجلات في قاعدة البيانات يترتب عليه عواقب تشغيلية مهمة لمهندسي البرمجيات:

  • عزل النسخ: Changes made via the Site Editor reside purely in that local or staging database. They will not automatically replicate to production or to another developer’s environment unless the database snapshot is exported and synchronised.
  • Git والتحكم في الإصدارات: تم حفظ التعديلات في wp-admin leave zero git commits or branch history. There is no diff, no pull request review, and no audit trail in your version control system.
  • تحديثات القوالب وحجب الملفات (Shadowing): If an engineer updates the theme file on disk, instances that hold customised templates in their database will not see those theme updates, because the database post continues to override the filesystem.
  • التراجع عن التخصيصات: Inside the Site Editor, modified templates display a blue badge indicating customisation. Clicking “Clear customisations” simply deletes the wp_template row from the database, instantly returning template resolution back to the filesystem HTML file.

4. مثال ملموس: إضافة الصورة البارزة

إذا كنت تريد عرض الصورة البارزة في جميع مقالات المدونة، فلديك نهجان أساسيان:

النهج (أ): محرر الموقع (قاعدة البيانات)

Log in to wp-admin, open the Site Editor, choose the Single Posts القالب، أدرج core/post-featured-image block above the post content, and save. This takes effect immediately on that installation, perfect for rapid visual prototyping.

النهج (ب): ملف مصدر القالب (النهج القائم على الشفرة أولاً)

Open wp-content/themes/baseline/templates/single.html in your editor and add the block markup directly into the template structure:

<!-- wp:post-featured-image {"aspectRatio":"16/9","scale":"cover","align":"wide"} /-->

نظراً لإيداع هذا التغيير في git، فإنه يصبح جزءاً من قاعدة الشفرة البرمجية للقالب الأساسي، مما يجعله ينتقل بسلاسة عبر البيئات المحلية والتجريبية وبيئات الإنتاج المباشرة من خلال مسارات النشر القياسية دون اضطراب في قاعدة البيانات أو شوائب السلاسل النصية المسلسلة.

5. سير عمل الإنتاج الموصى به: نمط الترقية (Promotion Pattern)

الهندسة عالية الضمان لا تتطلب التخلي عن محرر الموقع المرئي. بدلاً من ذلك، اتبع نمط الترقية:

  • النموذج الأولي المرئي: Use the local Site Editor on your development instance to assemble and test block layouts visually.
  • استخراج الترميز: فحص ترميز الكتل الناتج أو استخراجه باستخدام WP-CLI: wp post get <TEMPLATE_ID> --field=post_content.
  • حفظ في القالب (Commit to Theme): Copy the refined block HTML directly into the theme’s templates/*.html file.
  • مسح تخصيصات قاعدة البيانات: Revert the template in the local Site Editor so your local instance renders directly from the filesystem.
  • نشر الشفرة البرمجية: Deploy the theme repository via CI/CD. All environments now receive the verified template consistently without database dependencies.