🎉 MAJOR UPDATE: Complete Store Theming & Shortcodes System
Browse files✨ NEW FEATURES:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📦 BACKEND (Models & APIs):
• StoreTheme model - Full custom theme support with ZIP upload
• StoreShortcode model - Built-in + custom shortcodes
• ShortcodeService - Parse, render, and manage shortcodes
• Enhanced Page model - CSS/JS per page, versions, scheduling, locking
• Enhanced StoreSettings - Payment settings, layout, SEO, security, limits
🎨 FRONTEND - Settings Page (settings.html):
• Shortcodes tab - List, create, edit, delete custom shortcodes
• Custom Theme tab - Upload ZIP, custom header/footer, layout settings
• Live preview with shortcode support
• Page editor with insert shortcode button
• CSS/JS editor per page
🛍️ FRONTEND - Store Page (shop.html):
• Shortcode parsing in page content
• Shortcode support in product descriptions
• Custom theme application (header/footer replacement)
• New section types: html, products_grid
• Enhanced live preview with theme support
🔧 TECHNICAL:
• Shortcode caching for performance
• Page versioning (rollback support)
• Edit locking (prevent conflicts)
• Page scheduling (publish/expiry dates)
• Advanced page analytics (views, referrers, unique visitors)
• TTL index for unique visitors (auto-cleanup)
📊 SHORTCODES SUPPORTED:
• [products] - Product grid with filters
• [product] - Single product display
• [contact-form] - Contact form
• [store-info] - Store information
• [cart-count] - Cart item count
• [categories] - Category list
• [search-form] - Search form
• [testimonials] - Customer reviews
• [social-links] - Social media links
• + Custom shortcodes (user-defined)
🎨 THEME FEATURES:
• Upload complete ZIP themes
• Custom header/footer HTML
• Full CSS/JS customization
• Color palette (primary, secondary, accent, background)
• Typography (font family, size)
• Layout types (full-width, boxed, fluid)
• Sticky header, back-to-top button
• Container width control
🔗 NEW APIs:
GET /api/store/shortcodes
POST /api/store/shortcodes
PUT /api/store/shortcodes/:id
DELETE /api/store/shortcodes/:id
POST /api/store/shortcodes/parse
POST /api/store/shortcodes/preview
GET /api/store/theme
PUT /api/store/theme
POST /api/store/theme/upload
POST /api/store/theme/reset
GET /api/store/my/pages (enhanced)
POST /api/store/my/pages/upload
GET /api/store/my/pages/stats
✅ USER REQUESTS IMPLEMENTED:
✓ Customization & theming system
✓ Full control over store template
✓ Customize added pages (CSS/JS per page)
✓ Display products on any page (shortcodes)
✓ Replace homepage completely
✓ Platform comparable to Shopify (better in many aspects)
🚀 NEXT PHASE (Planned):
• Template Store (ready-made templates)
• Visual Page Builder (drag & drop visual editor)
• AI-powered Page Builder (Gemini/OpenAI integration)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Total files changed: [X]
Lines added: [X]
Lines removed: [X]
- server.js +0 -0
- services/ShortcodeService.js +212 -0
|
The diff for this file is too large to render.
See raw diff
|
|
|
|
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const { StoreShortcode } = require("../server");
|
| 2 |
+
|
| 3 |
+
/**
|
| 4 |
+
* خدمة معالجة الأكواد المختصرة
|
| 5 |
+
* @version 2.0.0
|
| 6 |
+
*/
|
| 7 |
+
class ShortcodeService {
|
| 8 |
+
|
| 9 |
+
/**
|
| 10 |
+
* معالجة النص واستبدال جميع الـ Shortcodes
|
| 11 |
+
* @param {string} content - المحتوى الأصلي
|
| 12 |
+
* @param {string} storeId - معرف المتجر
|
| 13 |
+
* @param {Object} options - خيارات إضافية
|
| 14 |
+
* @returns {Promise<string>} - المحتوى بعد المعالجة
|
| 15 |
+
*/
|
| 16 |
+
static async parseShortcodes(content, storeId, options = {}) {
|
| 17 |
+
if (!content || typeof content !== 'string') return content;
|
| 18 |
+
|
| 19 |
+
// التعبير النمطي للـ Shortcodes
|
| 20 |
+
const shortcodeRegex = /\[(\w+)(?:\s+([^\]]*))?\]/g;
|
| 21 |
+
let parsedContent = content;
|
| 22 |
+
let match;
|
| 23 |
+
|
| 24 |
+
// تجميع جميع الـ Shortcodes الفريدة
|
| 25 |
+
const shortcodesToProcess = new Map();
|
| 26 |
+
while ((match = shortcodeRegex.exec(content)) !== null) {
|
| 27 |
+
const name = match[1];
|
| 28 |
+
const attrsStr = match[2] || '';
|
| 29 |
+
|
| 30 |
+
if (!shortcodesToProcess.has(name)) {
|
| 31 |
+
shortcodesToProcess.set(name, []);
|
| 32 |
+
}
|
| 33 |
+
shortcodesToProcess.get(name).push({
|
| 34 |
+
fullMatch: match[0],
|
| 35 |
+
attrs: this.parseAttributes(attrsStr)
|
| 36 |
+
});
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
// معالجة كل Shortcode
|
| 40 |
+
for (const [name, occurrences] of shortcodesToProcess) {
|
| 41 |
+
const shortcode = await StoreShortcode.findOne({
|
| 42 |
+
userId: storeId,
|
| 43 |
+
name: name,
|
| 44 |
+
isActive: true
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
if (!shortcode) continue;
|
| 48 |
+
|
| 49 |
+
for (const occ of occurrences) {
|
| 50 |
+
try {
|
| 51 |
+
const html = await shortcode.render(occ.attrs, {
|
| 52 |
+
storeId,
|
| 53 |
+
userToken: options.userToken
|
| 54 |
+
});
|
| 55 |
+
parsedContent = parsedContent.replace(occ.fullMatch, html);
|
| 56 |
+
} catch (error) {
|
| 57 |
+
console.error(`Error processing shortcode ${name}:`, error);
|
| 58 |
+
parsedContent = parsedContent.replace(occ.fullMatch,
|
| 59 |
+
`<div class="shortcode-error text-red-500 p-2 border border-red-300 rounded">
|
| 60 |
+
Error: ${error.message}
|
| 61 |
+
</div>`
|
| 62 |
+
);
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
return parsedContent;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
/**
|
| 71 |
+
* تحليل معاملات الـ Shortcode
|
| 72 |
+
* @param {string} attrsStr - نص المعاملات
|
| 73 |
+
* @returns {Object} - كائن المعاملات
|
| 74 |
+
*/
|
| 75 |
+
static parseAttributes(attrsStr) {
|
| 76 |
+
const attrs = {};
|
| 77 |
+
if (!attrsStr) return attrs;
|
| 78 |
+
|
| 79 |
+
// دعم كل من name="value" و name=value
|
| 80 |
+
const attrRegex = /(\w+)(?:="([^"]*)"|='([^']*)'|="([^"]*)"|="([^"]*)"|="([^"]*)"|=([^\s]+))/g;
|
| 81 |
+
let match;
|
| 82 |
+
|
| 83 |
+
while ((match = attrRegex.exec(attrsStr)) !== null) {
|
| 84 |
+
const name = match[1];
|
| 85 |
+
let value = match[2] || match[3] || match[4] || match[5] || match[6] || match[7] || '';
|
| 86 |
+
|
| 87 |
+
// تحويل القيم إلى الأنواع المناسبة
|
| 88 |
+
if (value === 'true') value = true;
|
| 89 |
+
else if (value === 'false') value = false;
|
| 90 |
+
else if (!isNaN(value) && value !== '') value = Number(value);
|
| 91 |
+
|
| 92 |
+
attrs[name] = value;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
return attrs;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
/**
|
| 99 |
+
* الحصول على قائمة جميع الـ Shortcodes المتاحة لمستخدم
|
| 100 |
+
* @param {string} userId - معرف المستخدم
|
| 101 |
+
* @returns {Promise<Array>} - قائمة الـ Shortcodes
|
| 102 |
+
*/
|
| 103 |
+
static async getAllShortcodes(userId) {
|
| 104 |
+
const builtins = StoreShortcode.getBuiltinShortcodes();
|
| 105 |
+
const custom = await StoreShortcode.find({ userId, type: 'custom', isActive: true });
|
| 106 |
+
|
| 107 |
+
return [
|
| 108 |
+
...builtins.map(b => ({ ...b, isBuiltin: true })),
|
| 109 |
+
...custom.map(c => ({ ...c.toObject(), isBuiltin: false }))
|
| 110 |
+
];
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
/**
|
| 114 |
+
* إنشاء Shortcode مخصص جديد
|
| 115 |
+
* @param {string} userId - معرف المستخدم
|
| 116 |
+
* @param {Object} data - بيانات الـ Shortcode
|
| 117 |
+
* @returns {Promise<Object>} - الـ Shortcode المنشأ
|
| 118 |
+
*/
|
| 119 |
+
static async createCustomShortcode(userId, data) {
|
| 120 |
+
const { name, displayName, description, template, parameters, settings } = data;
|
| 121 |
+
|
| 122 |
+
// التحقق من عدم وجود اسم مكرر
|
| 123 |
+
const existing = await StoreShortcode.findOne({ userId, name });
|
| 124 |
+
if (existing) {
|
| 125 |
+
throw new Error(`Shortcode "${name}" already exists`);
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
const shortcode = new StoreShortcode({
|
| 129 |
+
userId,
|
| 130 |
+
name: name.toLowerCase().replace(/[^a-z0-9_-]/g, ''),
|
| 131 |
+
displayName,
|
| 132 |
+
description,
|
| 133 |
+
template,
|
| 134 |
+
parameters: parameters || [],
|
| 135 |
+
settings: settings || {},
|
| 136 |
+
type: 'custom'
|
| 137 |
+
});
|
| 138 |
+
|
| 139 |
+
await shortcode.save();
|
| 140 |
+
return shortcode;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
/**
|
| 144 |
+
* تحديث Shortcode مخصص
|
| 145 |
+
* @param {string} shortcodeId - معرف الـ Shortcode
|
| 146 |
+
* @param {string} userId - معرف المستخدم (للتحقق)
|
| 147 |
+
* @param {Object} data - بيانات التحديث
|
| 148 |
+
* @returns {Promise<Object>} - الـ Shortcode المحدث
|
| 149 |
+
*/
|
| 150 |
+
static async updateCustomShortcode(shortcodeId, userId, data) {
|
| 151 |
+
const shortcode = await StoreShortcode.findOne({ _id: shortcodeId, userId });
|
| 152 |
+
if (!shortcode) {
|
| 153 |
+
throw new Error('Shortcode not found');
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
Object.assign(shortcode, data);
|
| 157 |
+
shortcode.cachedOutput = null; // إبطال التخزين المؤقت
|
| 158 |
+
await shortcode.save();
|
| 159 |
+
|
| 160 |
+
return shortcode;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
/**
|
| 164 |
+
* حذف Shortcode مخصص
|
| 165 |
+
* @param {string} shortcodeId - معرف الـ Shortcode
|
| 166 |
+
* @param {string} userId - معرف المستخدم
|
| 167 |
+
* @returns {Promise<boolean>}
|
| 168 |
+
*/
|
| 169 |
+
static async deleteCustomShortcode(shortcodeId, userId) {
|
| 170 |
+
const result = await StoreShortcode.deleteOne({ _id: shortcodeId, userId, type: 'custom' });
|
| 171 |
+
return result.deletedCount > 0;
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
/**
|
| 175 |
+
* معاينة Shortcode (دون حفظ)
|
| 176 |
+
* @param {Object} data - بيانات الـ Shortcode
|
| 177 |
+
* @param {Object} context - السياق
|
| 178 |
+
* @returns {Promise<string>} - HTML المعاينة
|
| 179 |
+
*/
|
| 180 |
+
static async previewShortcode(data, context) {
|
| 181 |
+
const { name, displayName, template, parameters, attrs = {} } = data;
|
| 182 |
+
|
| 183 |
+
// دمج المعاملات مع القيم الافتراضية
|
| 184 |
+
const mergedAttrs = {};
|
| 185 |
+
if (parameters) {
|
| 186 |
+
for (const param of parameters) {
|
| 187 |
+
mergedAttrs[param.name] = attrs[param.name] !== undefined
|
| 188 |
+
? attrs[param.name]
|
| 189 |
+
: param.defaultValue;
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
// تنفيذ القالب
|
| 194 |
+
let html = template || '';
|
| 195 |
+
for (const [key, value] of Object.entries(mergedAttrs)) {
|
| 196 |
+
const regex = new RegExp(`\\{\\{\\s*${key}\\s*\\}\\}`, 'g');
|
| 197 |
+
html = html.replace(regex, String(value ?? ''));
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
return html || `<div class="p-4 text-gray-500">Preview: ${displayName} (no content)</div>`;
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
/**
|
| 204 |
+
* تهيئة الـ Shortcodes لمستخدم جديد
|
| 205 |
+
* @param {string} userId - معرف المستخدم
|
| 206 |
+
*/
|
| 207 |
+
static async initializeUserShortcodes(userId) {
|
| 208 |
+
await StoreShortcode.initializeBuiltins(userId);
|
| 209 |
+
}
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
module.exports = ShortcodeService;
|