@newcms/core 0.0.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,1003 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ BOOTSTRAP_PHASES: () => BOOTSTRAP_PHASES,
24
+ BUILTIN_POST_TYPES: () => BUILTIN_POST_TYPES,
25
+ BUILTIN_TAXONOMIES: () => BUILTIN_TAXONOMIES,
26
+ BootstrapManager: () => BootstrapManager,
27
+ ExtensionRegistry: () => ExtensionRegistry,
28
+ HOOK_PRIORITY: () => HOOK_PRIORITY,
29
+ HookEngine: () => HookEngine,
30
+ POST_STATUS: () => POST_STATUS,
31
+ PostTypeRegistry: () => PostTypeRegistry,
32
+ TaxonomyRegistry: () => TaxonomyRegistry,
33
+ ThemeRegistry: () => ThemeRegistry,
34
+ USER_ROLES: () => USER_ROLES,
35
+ resolveTemplateHierarchy: () => resolveTemplateHierarchy
36
+ });
37
+ module.exports = __toCommonJS(index_exports);
38
+
39
+ // src/hook-engine.ts
40
+ var anonymousCounter = 0;
41
+ function generateCallbackId(callback, priority) {
42
+ if (callback.name && callback.name !== "") {
43
+ return `${callback.name}::${priority}`;
44
+ }
45
+ anonymousCounter++;
46
+ return `__anonymous_${anonymousCounter}::${priority}`;
47
+ }
48
+ var HookEngine = class {
49
+ /**
50
+ * Map of hook name → array of handlers, kept sorted by priority.
51
+ */
52
+ hooks = /* @__PURE__ */ new Map();
53
+ /**
54
+ * Stack of hooks currently being executed (supports recursion).
55
+ */
56
+ currentStack = [];
57
+ /**
58
+ * Counter of how many times each hook has been fired.
59
+ */
60
+ fireCount = /* @__PURE__ */ new Map();
61
+ /**
62
+ * Register a callback for a hook.
63
+ *
64
+ * @param hookName - The hook identifier
65
+ * @param callback - The function to execute
66
+ * @param options - Priority and accepted args configuration
67
+ * @returns The generated handler ID
68
+ */
69
+ addHook(hookName, callback, options = {}) {
70
+ const priority = options.priority ?? 10;
71
+ const acceptedArgs = options.acceptedArgs ?? 1;
72
+ const id = generateCallbackId(callback, priority);
73
+ const handler = {
74
+ callback,
75
+ priority,
76
+ acceptedArgs,
77
+ id
78
+ };
79
+ const existing = this.hooks.get(hookName) ?? [];
80
+ existing.push(handler);
81
+ existing.sort((a, b) => a.priority - b.priority);
82
+ this.hooks.set(hookName, existing);
83
+ return id;
84
+ }
85
+ /**
86
+ * Remove a specific callback from a hook.
87
+ *
88
+ * Both the callback reference AND priority must match for removal.
89
+ *
90
+ * @param hookName - The hook identifier
91
+ * @param callback - The callback to remove
92
+ * @param priority - The priority it was registered with (default: 10)
93
+ * @returns true if a handler was removed
94
+ */
95
+ removeHook(hookName, callback, priority = 10) {
96
+ const handlers = this.hooks.get(hookName);
97
+ if (!handlers) return false;
98
+ const initialLength = handlers.length;
99
+ const filtered = handlers.filter(
100
+ (h) => !(h.callback === callback && h.priority === priority)
101
+ );
102
+ if (filtered.length === initialLength) return false;
103
+ if (filtered.length === 0) {
104
+ this.hooks.delete(hookName);
105
+ } else {
106
+ this.hooks.set(hookName, filtered);
107
+ }
108
+ return true;
109
+ }
110
+ /**
111
+ * Remove all callbacks from a hook, optionally only for a specific priority.
112
+ *
113
+ * @param hookName - The hook identifier
114
+ * @param priority - If provided, only remove handlers at this priority
115
+ * @returns true if any handlers were removed
116
+ */
117
+ removeAllHooks(hookName, priority) {
118
+ if (priority === void 0) {
119
+ return this.hooks.delete(hookName);
120
+ }
121
+ const handlers = this.hooks.get(hookName);
122
+ if (!handlers) return false;
123
+ const filtered = handlers.filter((h) => h.priority !== priority);
124
+ if (filtered.length === handlers.length) return false;
125
+ if (filtered.length === 0) {
126
+ this.hooks.delete(hookName);
127
+ } else {
128
+ this.hooks.set(hookName, filtered);
129
+ }
130
+ return true;
131
+ }
132
+ /**
133
+ * Check if a hook has registered handlers.
134
+ *
135
+ * @param hookName - The hook identifier
136
+ * @param callback - If provided, check for this specific callback
137
+ * @returns false if no handlers, or the priority of the matching handler
138
+ */
139
+ hasHook(hookName, callback) {
140
+ const handlers = this.hooks.get(hookName);
141
+ if (!handlers || handlers.length === 0) return false;
142
+ if (callback === void 0) {
143
+ return handlers[0].priority;
144
+ }
145
+ const found = handlers.find((h) => h.callback === callback);
146
+ return found ? found.priority : false;
147
+ }
148
+ /**
149
+ * Execute an action hook. All registered callbacks are called in priority order.
150
+ * The "all" universal hook fires before the specific hook.
151
+ *
152
+ * @param hookName - The hook identifier
153
+ * @param args - Arguments to pass to callbacks
154
+ */
155
+ async doAction(hookName, ...args) {
156
+ if (hookName !== "all") {
157
+ await this.fireUniversalHook(hookName, args);
158
+ }
159
+ const handlers = this.hooks.get(hookName);
160
+ this.incrementFireCount(hookName);
161
+ if (!handlers || handlers.length === 0) return;
162
+ const stackEntry = { name: hookName, currentIndex: 0 };
163
+ this.currentStack.push(stackEntry);
164
+ try {
165
+ for (let i = 0; i < handlers.length; i++) {
166
+ stackEntry.currentIndex = i;
167
+ const handler = handlers[i];
168
+ const slicedArgs = args.slice(0, handler.acceptedArgs);
169
+ await handler.callback(...slicedArgs);
170
+ }
171
+ } finally {
172
+ this.currentStack.pop();
173
+ }
174
+ }
175
+ /**
176
+ * Execute an action hook synchronously.
177
+ */
178
+ doActionSync(hookName, ...args) {
179
+ if (hookName !== "all") {
180
+ this.fireUniversalHookSync(hookName, args);
181
+ }
182
+ const handlers = this.hooks.get(hookName);
183
+ this.incrementFireCount(hookName);
184
+ if (!handlers || handlers.length === 0) return;
185
+ const stackEntry = { name: hookName, currentIndex: 0 };
186
+ this.currentStack.push(stackEntry);
187
+ try {
188
+ for (let i = 0; i < handlers.length; i++) {
189
+ stackEntry.currentIndex = i;
190
+ const handler = handlers[i];
191
+ const slicedArgs = args.slice(0, handler.acceptedArgs);
192
+ handler.callback(...slicedArgs);
193
+ }
194
+ } finally {
195
+ this.currentStack.pop();
196
+ }
197
+ }
198
+ /**
199
+ * Execute a filter hook. The first argument is the value being filtered.
200
+ * Each callback receives the (possibly modified) value and returns a new value.
201
+ * The "all" universal hook fires before the specific hook.
202
+ *
203
+ * @param hookName - The hook identifier
204
+ * @param value - The initial value to filter
205
+ * @param args - Additional arguments passed to each callback
206
+ * @returns The filtered value after all callbacks have processed it
207
+ */
208
+ async applyFilters(hookName, value, ...args) {
209
+ if (hookName !== "all") {
210
+ await this.fireUniversalHook(hookName, [value, ...args]);
211
+ }
212
+ const handlers = this.hooks.get(hookName);
213
+ this.incrementFireCount(hookName);
214
+ if (!handlers || handlers.length === 0) return value;
215
+ const stackEntry = { name: hookName, currentIndex: 0 };
216
+ this.currentStack.push(stackEntry);
217
+ let filteredValue = value;
218
+ try {
219
+ for (let i = 0; i < handlers.length; i++) {
220
+ stackEntry.currentIndex = i;
221
+ const handler = handlers[i];
222
+ const callArgs = [filteredValue, ...args].slice(0, handler.acceptedArgs);
223
+ filteredValue = await handler.callback(filteredValue, ...callArgs.slice(1));
224
+ }
225
+ } finally {
226
+ this.currentStack.pop();
227
+ }
228
+ return filteredValue;
229
+ }
230
+ /**
231
+ * Execute a filter hook synchronously.
232
+ */
233
+ applyFiltersSync(hookName, value, ...args) {
234
+ if (hookName !== "all") {
235
+ this.fireUniversalHookSync(hookName, [value, ...args]);
236
+ }
237
+ const handlers = this.hooks.get(hookName);
238
+ this.incrementFireCount(hookName);
239
+ if (!handlers || handlers.length === 0) return value;
240
+ const stackEntry = { name: hookName, currentIndex: 0 };
241
+ this.currentStack.push(stackEntry);
242
+ let filteredValue = value;
243
+ try {
244
+ for (let i = 0; i < handlers.length; i++) {
245
+ stackEntry.currentIndex = i;
246
+ const handler = handlers[i];
247
+ const callArgs = [filteredValue, ...args].slice(0, handler.acceptedArgs);
248
+ filteredValue = handler.callback(filteredValue, ...callArgs.slice(1));
249
+ }
250
+ } finally {
251
+ this.currentStack.pop();
252
+ }
253
+ return filteredValue;
254
+ }
255
+ /**
256
+ * Get how many times a hook has been fired.
257
+ */
258
+ getFireCount(hookName) {
259
+ return this.fireCount.get(hookName) ?? 0;
260
+ }
261
+ /**
262
+ * Check if a specific hook is currently being executed.
263
+ */
264
+ isDoingHook(hookName) {
265
+ if (hookName === void 0) {
266
+ return this.currentStack.length > 0;
267
+ }
268
+ return this.currentStack.some((entry) => entry.name === hookName);
269
+ }
270
+ /**
271
+ * Get the name of the hook currently being executed (top of stack).
272
+ * Returns undefined if no hook is executing.
273
+ */
274
+ currentHook() {
275
+ if (this.currentStack.length === 0) return void 0;
276
+ return this.currentStack[this.currentStack.length - 1].name;
277
+ }
278
+ /**
279
+ * Get a snapshot of the current execution stack.
280
+ */
281
+ getExecutionStack() {
282
+ return [...this.currentStack];
283
+ }
284
+ /**
285
+ * Check if a hook has ever been fired (fire count > 0).
286
+ */
287
+ didHook(hookName) {
288
+ return this.getFireCount(hookName) > 0;
289
+ }
290
+ /**
291
+ * Get the number of handlers registered for a hook.
292
+ */
293
+ getHandlerCount(hookName) {
294
+ return this.hooks.get(hookName)?.length ?? 0;
295
+ }
296
+ /**
297
+ * Reset the engine — useful for testing.
298
+ */
299
+ reset() {
300
+ this.hooks.clear();
301
+ this.currentStack = [];
302
+ this.fireCount.clear();
303
+ anonymousCounter = 0;
304
+ }
305
+ // --- Convenience aliases matching WordPress API names ---
306
+ addAction(hookName, callback, options) {
307
+ return this.addHook(hookName, callback, options);
308
+ }
309
+ addFilter(hookName, callback, options) {
310
+ return this.addHook(hookName, callback, options);
311
+ }
312
+ removeAction(hookName, callback, priority) {
313
+ return this.removeHook(hookName, callback, priority);
314
+ }
315
+ removeFilter(hookName, callback, priority) {
316
+ return this.removeHook(hookName, callback, priority);
317
+ }
318
+ hasAction(hookName, callback) {
319
+ return this.hasHook(hookName, callback);
320
+ }
321
+ hasFilter(hookName, callback) {
322
+ return this.hasHook(hookName, callback);
323
+ }
324
+ didAction(hookName) {
325
+ return this.didHook(hookName);
326
+ }
327
+ didFilter(hookName) {
328
+ return this.didHook(hookName);
329
+ }
330
+ // --- Private helpers ---
331
+ incrementFireCount(hookName) {
332
+ this.fireCount.set(hookName, (this.fireCount.get(hookName) ?? 0) + 1);
333
+ }
334
+ async fireUniversalHook(hookName, args) {
335
+ const allHandlers = this.hooks.get("all");
336
+ if (!allHandlers || allHandlers.length === 0) return;
337
+ const stackEntry = { name: "all", currentIndex: 0 };
338
+ this.currentStack.push(stackEntry);
339
+ try {
340
+ for (let i = 0; i < allHandlers.length; i++) {
341
+ stackEntry.currentIndex = i;
342
+ const handler = allHandlers[i];
343
+ await handler.callback(hookName, ...args);
344
+ }
345
+ } finally {
346
+ this.currentStack.pop();
347
+ }
348
+ }
349
+ fireUniversalHookSync(hookName, args) {
350
+ const allHandlers = this.hooks.get("all");
351
+ if (!allHandlers || allHandlers.length === 0) return;
352
+ const stackEntry = { name: "all", currentIndex: 0 };
353
+ this.currentStack.push(stackEntry);
354
+ try {
355
+ for (let i = 0; i < allHandlers.length; i++) {
356
+ stackEntry.currentIndex = i;
357
+ const handler = allHandlers[i];
358
+ handler.callback(hookName, ...args);
359
+ }
360
+ } finally {
361
+ this.currentStack.pop();
362
+ }
363
+ }
364
+ };
365
+
366
+ // src/post-type-registry.ts
367
+ var PostTypeRegistry = class {
368
+ types = /* @__PURE__ */ new Map();
369
+ /**
370
+ * Register a new post type.
371
+ *
372
+ * @throws If a type with the same name is already registered
373
+ */
374
+ register(definition) {
375
+ if (this.types.has(definition.name)) {
376
+ throw new Error(`Post type "${definition.name}" is already registered.`);
377
+ }
378
+ this.types.set(definition.name, definition);
379
+ }
380
+ /**
381
+ * Get a post type definition by name.
382
+ */
383
+ get(name) {
384
+ return this.types.get(name);
385
+ }
386
+ /**
387
+ * Check if a post type is registered.
388
+ */
389
+ has(name) {
390
+ return this.types.has(name);
391
+ }
392
+ /**
393
+ * Get all registered post types.
394
+ */
395
+ getAll() {
396
+ return [...this.types.values()];
397
+ }
398
+ /**
399
+ * Get only public post types (for REST API, search, etc).
400
+ */
401
+ getPublic() {
402
+ return this.getAll().filter((t) => t.public !== false);
403
+ }
404
+ /**
405
+ * Get post types that are exposed via REST API.
406
+ */
407
+ getRestVisible() {
408
+ return this.getAll().filter((t) => t.showInRest === true);
409
+ }
410
+ /**
411
+ * Unregister a post type. Only custom types can be unregistered.
412
+ */
413
+ unregister(name) {
414
+ return this.types.delete(name);
415
+ }
416
+ /**
417
+ * Reset registry — for testing only.
418
+ */
419
+ reset() {
420
+ this.types.clear();
421
+ }
422
+ };
423
+ var BUILTIN_POST_TYPES = [
424
+ {
425
+ name: "post",
426
+ label: "Posts",
427
+ labels: { singular: "Post", plural: "Posts" },
428
+ public: true,
429
+ hierarchical: false,
430
+ showInRest: true,
431
+ restBase: "posts",
432
+ supports: [
433
+ "title",
434
+ "editor",
435
+ "author",
436
+ "thumbnail",
437
+ "excerpt",
438
+ "trackbacks",
439
+ "custom-fields",
440
+ "comments",
441
+ "revisions",
442
+ "post-formats"
443
+ ],
444
+ taxonomies: ["category", "post_tag"],
445
+ hasArchive: true,
446
+ rewrite: { slug: "", withFront: true },
447
+ menuPosition: 5,
448
+ capability_type: "post"
449
+ },
450
+ {
451
+ name: "page",
452
+ label: "Pages",
453
+ labels: { singular: "Page", plural: "Pages" },
454
+ public: true,
455
+ hierarchical: true,
456
+ showInRest: true,
457
+ restBase: "pages",
458
+ supports: [
459
+ "title",
460
+ "editor",
461
+ "author",
462
+ "thumbnail",
463
+ "page-attributes",
464
+ "custom-fields",
465
+ "comments",
466
+ "revisions"
467
+ ],
468
+ taxonomies: [],
469
+ hasArchive: false,
470
+ rewrite: { slug: "", withFront: false },
471
+ menuPosition: 20,
472
+ capability_type: "page"
473
+ },
474
+ {
475
+ name: "attachment",
476
+ label: "Media",
477
+ labels: { singular: "Media", plural: "Media" },
478
+ public: true,
479
+ hierarchical: false,
480
+ showInRest: true,
481
+ restBase: "media",
482
+ supports: ["title", "author", "comments"],
483
+ taxonomies: [],
484
+ hasArchive: false,
485
+ rewrite: false,
486
+ capability_type: "post"
487
+ },
488
+ {
489
+ name: "revision",
490
+ label: "Revisions",
491
+ labels: { singular: "Revision", plural: "Revisions" },
492
+ public: false,
493
+ hierarchical: false,
494
+ showInRest: false,
495
+ supports: ["author"],
496
+ taxonomies: [],
497
+ hasArchive: false,
498
+ rewrite: false,
499
+ capability_type: "post"
500
+ },
501
+ {
502
+ name: "nav_menu_item",
503
+ label: "Navigation Menu Items",
504
+ labels: { singular: "Navigation Menu Item", plural: "Navigation Menu Items" },
505
+ public: false,
506
+ hierarchical: false,
507
+ showInRest: false,
508
+ supports: [],
509
+ taxonomies: ["nav_menu"],
510
+ hasArchive: false,
511
+ rewrite: false,
512
+ capability_type: "post"
513
+ }
514
+ ];
515
+
516
+ // src/taxonomy-registry.ts
517
+ var TaxonomyRegistry = class {
518
+ taxonomies = /* @__PURE__ */ new Map();
519
+ register(definition) {
520
+ if (this.taxonomies.has(definition.name)) {
521
+ throw new Error(`Taxonomy "${definition.name}" is already registered.`);
522
+ }
523
+ this.taxonomies.set(definition.name, definition);
524
+ }
525
+ get(name) {
526
+ return this.taxonomies.get(name);
527
+ }
528
+ has(name) {
529
+ return this.taxonomies.has(name);
530
+ }
531
+ getAll() {
532
+ return [...this.taxonomies.values()];
533
+ }
534
+ /**
535
+ * Get taxonomies assigned to a specific object type (e.g., 'post').
536
+ */
537
+ getForObjectType(objectType) {
538
+ return this.getAll().filter((t) => t.objectTypes.includes(objectType));
539
+ }
540
+ getRestVisible() {
541
+ return this.getAll().filter((t) => t.showInRest === true);
542
+ }
543
+ unregister(name) {
544
+ return this.taxonomies.delete(name);
545
+ }
546
+ reset() {
547
+ this.taxonomies.clear();
548
+ }
549
+ };
550
+ var BUILTIN_TAXONOMIES = [
551
+ {
552
+ name: "category",
553
+ objectTypes: ["post"],
554
+ label: "Categories",
555
+ labels: { singular: "Category", plural: "Categories" },
556
+ public: true,
557
+ hierarchical: true,
558
+ showInRest: true,
559
+ restBase: "categories",
560
+ rewrite: { slug: "category", withFront: true, hierarchical: true }
561
+ },
562
+ {
563
+ name: "post_tag",
564
+ objectTypes: ["post"],
565
+ label: "Tags",
566
+ labels: { singular: "Tag", plural: "Tags" },
567
+ public: true,
568
+ hierarchical: false,
569
+ showInRest: true,
570
+ restBase: "tags",
571
+ rewrite: { slug: "tag", withFront: true }
572
+ },
573
+ {
574
+ name: "nav_menu",
575
+ objectTypes: ["nav_menu_item"],
576
+ label: "Navigation Menus",
577
+ labels: { singular: "Navigation Menu", plural: "Navigation Menus" },
578
+ public: false,
579
+ hierarchical: false,
580
+ showInRest: false,
581
+ rewrite: false
582
+ }
583
+ ];
584
+
585
+ // src/extension-registry.ts
586
+ var ExtensionRegistry = class {
587
+ extensions = /* @__PURE__ */ new Map();
588
+ /**
589
+ * Register an extension manifest (discovery phase).
590
+ */
591
+ register(manifest, path, status) {
592
+ this.extensions.set(manifest.slug, {
593
+ manifest,
594
+ status,
595
+ path,
596
+ activatedAt: status === "active" ? /* @__PURE__ */ new Date() : void 0
597
+ });
598
+ }
599
+ get(slug) {
600
+ return this.extensions.get(slug);
601
+ }
602
+ has(slug) {
603
+ return this.extensions.has(slug);
604
+ }
605
+ /**
606
+ * Get all extensions by status.
607
+ */
608
+ getByStatus(status) {
609
+ return [...this.extensions.values()].filter((e) => e.status === status);
610
+ }
611
+ getAll() {
612
+ return [...this.extensions.values()];
613
+ }
614
+ getActive() {
615
+ return this.getByStatus("active");
616
+ }
617
+ getMustUse() {
618
+ return this.getByStatus("must-use");
619
+ }
620
+ getPaused() {
621
+ return this.getByStatus("paused");
622
+ }
623
+ /**
624
+ * Activate an extension. Checks dependencies first.
625
+ *
626
+ * @throws If dependencies are not met
627
+ */
628
+ activate(slug) {
629
+ const entry = this.extensions.get(slug);
630
+ if (!entry) throw new Error(`Extension "${slug}" not found`);
631
+ if (entry.status === "active") return;
632
+ const unmet = this.getUnmetDependencies(slug);
633
+ if (unmet.length > 0) {
634
+ throw new Error(
635
+ `Cannot activate "${slug}": missing dependencies: ${unmet.join(", ")}`
636
+ );
637
+ }
638
+ entry.status = "active";
639
+ entry.activatedAt = /* @__PURE__ */ new Date();
640
+ entry.pauseReason = void 0;
641
+ }
642
+ /**
643
+ * Deactivate an extension. Checks if other extensions depend on it.
644
+ *
645
+ * @throws If other active extensions depend on this one
646
+ */
647
+ deactivate(slug) {
648
+ const entry = this.extensions.get(slug);
649
+ if (!entry) throw new Error(`Extension "${slug}" not found`);
650
+ if (entry.status === "inactive") return;
651
+ if (entry.status === "must-use") {
652
+ throw new Error(`Cannot deactivate must-use extension "${slug}"`);
653
+ }
654
+ const dependents = this.getDependents(slug);
655
+ if (dependents.length > 0) {
656
+ throw new Error(
657
+ `Cannot deactivate "${slug}": required by: ${dependents.map((d) => d.manifest.slug).join(", ")}`
658
+ );
659
+ }
660
+ entry.status = "inactive";
661
+ entry.activatedAt = void 0;
662
+ }
663
+ /**
664
+ * Pause an extension due to a fatal error (recovery mode).
665
+ */
666
+ pause(slug, reason) {
667
+ const entry = this.extensions.get(slug);
668
+ if (!entry) return;
669
+ entry.status = "paused";
670
+ entry.pauseReason = reason;
671
+ }
672
+ /**
673
+ * Unpause an extension (resume from recovery mode).
674
+ */
675
+ unpause(slug) {
676
+ const entry = this.extensions.get(slug);
677
+ if (!entry || entry.status !== "paused") return;
678
+ entry.status = "active";
679
+ entry.pauseReason = void 0;
680
+ }
681
+ /**
682
+ * Remove an extension from the registry entirely.
683
+ */
684
+ unregister(slug) {
685
+ return this.extensions.delete(slug);
686
+ }
687
+ /**
688
+ * Get dependencies that are not active.
689
+ */
690
+ getUnmetDependencies(slug) {
691
+ const entry = this.extensions.get(slug);
692
+ if (!entry) return [];
693
+ const deps = entry.manifest.dependencies ?? [];
694
+ return deps.filter((dep) => {
695
+ const depEntry = this.extensions.get(dep);
696
+ return !depEntry || depEntry.status !== "active" && depEntry.status !== "must-use";
697
+ });
698
+ }
699
+ /**
700
+ * Get active extensions that depend on the given extension.
701
+ */
702
+ getDependents(slug) {
703
+ return this.getActive().filter(
704
+ (entry) => entry.manifest.dependencies?.includes(slug)
705
+ );
706
+ }
707
+ /**
708
+ * Check for circular dependencies starting from a slug.
709
+ */
710
+ hasCircularDependency(slug, visited = /* @__PURE__ */ new Set()) {
711
+ if (visited.has(slug)) return true;
712
+ visited.add(slug);
713
+ const entry = this.extensions.get(slug);
714
+ if (!entry) return false;
715
+ for (const dep of entry.manifest.dependencies ?? []) {
716
+ if (this.hasCircularDependency(dep, new Set(visited))) return true;
717
+ }
718
+ return false;
719
+ }
720
+ reset() {
721
+ this.extensions.clear();
722
+ }
723
+ };
724
+
725
+ // src/theme-registry.ts
726
+ var ThemeRegistry = class {
727
+ themes = /* @__PURE__ */ new Map();
728
+ activeSlug = null;
729
+ register(manifest, path) {
730
+ this.themes.set(manifest.slug, { manifest, path, active: false });
731
+ }
732
+ get(slug) {
733
+ return this.themes.get(slug);
734
+ }
735
+ getAll() {
736
+ return [...this.themes.values()];
737
+ }
738
+ getActive() {
739
+ if (!this.activeSlug) return void 0;
740
+ return this.themes.get(this.activeSlug);
741
+ }
742
+ /**
743
+ * Activate a theme. Resolves parent theme for child themes.
744
+ */
745
+ activate(slug) {
746
+ const theme = this.themes.get(slug);
747
+ if (!theme) throw new Error(`Theme "${slug}" not found`);
748
+ if (this.activeSlug) {
749
+ const current = this.themes.get(this.activeSlug);
750
+ if (current) current.active = false;
751
+ }
752
+ if (theme.manifest.parent) {
753
+ const parent = this.themes.get(theme.manifest.parent);
754
+ if (!parent) {
755
+ throw new Error(
756
+ `Parent theme "${theme.manifest.parent}" not found for child theme "${slug}"`
757
+ );
758
+ }
759
+ theme.parentTheme = parent;
760
+ }
761
+ theme.active = true;
762
+ this.activeSlug = slug;
763
+ }
764
+ /**
765
+ * Check if the active theme supports a feature.
766
+ */
767
+ supports(feature) {
768
+ const active = this.getActive();
769
+ if (!active) return false;
770
+ const supports = active.manifest.supports;
771
+ if (!supports) return false;
772
+ const value = supports[feature];
773
+ if (value === void 0 || value === false) return false;
774
+ return true;
775
+ }
776
+ reset() {
777
+ this.themes.clear();
778
+ this.activeSlug = null;
779
+ }
780
+ };
781
+ function resolveTemplateHierarchy(ctx) {
782
+ switch (ctx.type) {
783
+ case "single":
784
+ return [
785
+ ctx.postType && ctx.slug ? `${ctx.postType}-${ctx.slug}` : null,
786
+ ctx.postType ?? null,
787
+ "singular",
788
+ "index"
789
+ ].filter(Boolean);
790
+ case "page":
791
+ return [
792
+ ctx.customTemplate ?? null,
793
+ ctx.slug ? `page-${ctx.slug}` : null,
794
+ ctx.id ? `page-${ctx.id}` : null,
795
+ "page",
796
+ "singular",
797
+ "index"
798
+ ].filter(Boolean);
799
+ case "category":
800
+ return [
801
+ ctx.slug ? `category-${ctx.slug}` : null,
802
+ ctx.id ? `category-${ctx.id}` : null,
803
+ "category",
804
+ "archive",
805
+ "index"
806
+ ].filter(Boolean);
807
+ case "tag":
808
+ return [
809
+ ctx.slug ? `tag-${ctx.slug}` : null,
810
+ ctx.id ? `tag-${ctx.id}` : null,
811
+ "tag",
812
+ "archive",
813
+ "index"
814
+ ].filter(Boolean);
815
+ case "taxonomy":
816
+ return [
817
+ ctx.taxonomy && ctx.term ? `taxonomy-${ctx.taxonomy}-${ctx.term}` : null,
818
+ ctx.taxonomy && ctx.id ? `taxonomy-${ctx.taxonomy}-${ctx.id}` : null,
819
+ ctx.taxonomy ? `taxonomy-${ctx.taxonomy}` : null,
820
+ "taxonomy",
821
+ "archive",
822
+ "index"
823
+ ].filter(Boolean);
824
+ case "author":
825
+ return [
826
+ ctx.nicename ? `author-${ctx.nicename}` : null,
827
+ ctx.id ? `author-${ctx.id}` : null,
828
+ "author",
829
+ "archive",
830
+ "index"
831
+ ].filter(Boolean);
832
+ case "date":
833
+ return ["date", "archive", "index"];
834
+ case "search":
835
+ return ["search", "index"];
836
+ case "404":
837
+ return ["404", "index"];
838
+ case "home":
839
+ return ["front-page", "home", "index"];
840
+ case "archive":
841
+ return ["archive", "index"];
842
+ case "attachment":
843
+ return [
844
+ ctx.mimeType && ctx.mimeSubtype ? `${ctx.mimeType}-${ctx.mimeSubtype}` : null,
845
+ ctx.mimeSubtype ?? null,
846
+ ctx.mimeType ?? null,
847
+ "attachment",
848
+ "single",
849
+ "index"
850
+ ].filter(Boolean);
851
+ default:
852
+ return ["index"];
853
+ }
854
+ }
855
+
856
+ // src/bootstrap.ts
857
+ var BOOTSTRAP_PHASES = [
858
+ "entry_point",
859
+ // 1. HTTP request received
860
+ "configuration",
861
+ // 2. Load config (DB, keys, flags)
862
+ "initial_constants",
863
+ // 3. Define constants, memory limits
864
+ "environment_check",
865
+ // 4. Validate runtime versions
866
+ "error_handling",
867
+ // 5. Register fatal error handler + recovery mode
868
+ "core_functions",
869
+ // 6. Load core utility modules
870
+ "database_connect",
871
+ // 7. Connect to database (+ drop-in check)
872
+ "object_cache",
873
+ // 8. Initialize cache system (+ drop-in check)
874
+ "default_filters",
875
+ // 9. Register all core hooks/filters
876
+ "must_use_extensions",
877
+ // 10. Load must-use extensions (alphabetical)
878
+ "regular_extensions",
879
+ // 11. Load active extensions (skip paused)
880
+ "overridable_functions",
881
+ // 12. Load auth/hash/nonce functions (skip if overridden)
882
+ "extensions_loaded",
883
+ // 13. Fire "extensions_loaded" hook
884
+ "global_objects",
885
+ // 14. Create query engine, rewrite, widgets, roles
886
+ "theme",
887
+ // 15. Load active theme (child → parent)
888
+ "init",
889
+ // 16. Fire "init" hook (register types, taxonomies, etc.)
890
+ "system_loaded"
891
+ // 17. Fire "cms_loaded" hook
892
+ ];
893
+ var BootstrapManager = class {
894
+ constructor(hooks) {
895
+ this.hooks = hooks;
896
+ for (const phase of BOOTSTRAP_PHASES) {
897
+ this.phaseHandlers.set(phase, []);
898
+ }
899
+ }
900
+ hooks;
901
+ phaseHandlers = /* @__PURE__ */ new Map();
902
+ completedPhases = /* @__PURE__ */ new Set();
903
+ currentPhase = null;
904
+ /**
905
+ * Register a handler for a bootstrap phase.
906
+ */
907
+ on(phase, handler) {
908
+ const handlers = this.phaseHandlers.get(phase);
909
+ if (!handlers) throw new Error(`Unknown bootstrap phase: "${phase}"`);
910
+ handlers.push(handler);
911
+ }
912
+ /**
913
+ * Execute all 17 bootstrap phases in order.
914
+ * For short-init mode, pass a phase to stop at.
915
+ */
916
+ async run(stopAfter) {
917
+ for (const phase of BOOTSTRAP_PHASES) {
918
+ this.currentPhase = phase;
919
+ const handlers = this.phaseHandlers.get(phase) ?? [];
920
+ for (const handler of handlers) {
921
+ await handler();
922
+ }
923
+ await this.hooks.doAction(`bootstrap:${phase}`);
924
+ this.completedPhases.add(phase);
925
+ this.currentPhase = null;
926
+ if (stopAfter === phase) break;
927
+ }
928
+ if (!stopAfter || stopAfter === "system_loaded") {
929
+ await this.hooks.doAction("cms_loaded");
930
+ }
931
+ }
932
+ /**
933
+ * Check if a phase has been completed.
934
+ */
935
+ isPhaseComplete(phase) {
936
+ return this.completedPhases.has(phase);
937
+ }
938
+ /**
939
+ * Get the phase currently being executed.
940
+ */
941
+ getCurrentPhase() {
942
+ return this.currentPhase;
943
+ }
944
+ /**
945
+ * Get all completed phases.
946
+ */
947
+ getCompletedPhases() {
948
+ return [...this.completedPhases];
949
+ }
950
+ /**
951
+ * Reset — for testing.
952
+ */
953
+ reset() {
954
+ this.completedPhases.clear();
955
+ this.currentPhase = null;
956
+ for (const phase of BOOTSTRAP_PHASES) {
957
+ this.phaseHandlers.set(phase, []);
958
+ }
959
+ }
960
+ };
961
+
962
+ // src/types.ts
963
+ var POST_STATUS = {
964
+ PUBLISH: "publish",
965
+ DRAFT: "draft",
966
+ PENDING: "pending",
967
+ PRIVATE: "private",
968
+ TRASH: "trash",
969
+ AUTO_DRAFT: "auto-draft",
970
+ INHERIT: "inherit",
971
+ FUTURE: "future"
972
+ };
973
+ var USER_ROLES = {
974
+ ADMINISTRATOR: "administrator",
975
+ EDITOR: "editor",
976
+ AUTHOR: "author",
977
+ CONTRIBUTOR: "contributor",
978
+ SUBSCRIBER: "subscriber"
979
+ };
980
+ var HOOK_PRIORITY = {
981
+ EARLIEST: 1,
982
+ EARLY: 5,
983
+ DEFAULT: 10,
984
+ LATE: 15,
985
+ LATEST: 20
986
+ };
987
+ // Annotate the CommonJS export names for ESM import in node:
988
+ 0 && (module.exports = {
989
+ BOOTSTRAP_PHASES,
990
+ BUILTIN_POST_TYPES,
991
+ BUILTIN_TAXONOMIES,
992
+ BootstrapManager,
993
+ ExtensionRegistry,
994
+ HOOK_PRIORITY,
995
+ HookEngine,
996
+ POST_STATUS,
997
+ PostTypeRegistry,
998
+ TaxonomyRegistry,
999
+ ThemeRegistry,
1000
+ USER_ROLES,
1001
+ resolveTemplateHierarchy
1002
+ });
1003
+ //# sourceMappingURL=index.cjs.map