@newcms/core 0.1.0 → 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.
@@ -0,0 +1,569 @@
1
+ /**
2
+ * Generic hook callback. Used for both actions and filters.
3
+ * Actions ignore the return value; filters chain it.
4
+ */
5
+ type HookCallback = (...args: any[]) => any;
6
+ /**
7
+ * A registered hook handler with its metadata.
8
+ */
9
+ interface HookHandler {
10
+ /** The callback function */
11
+ callback: HookCallback;
12
+ /** Execution priority (lower = earlier). Default: 10 */
13
+ priority: number;
14
+ /** Number of arguments the callback accepts */
15
+ acceptedArgs: number;
16
+ /** Unique identifier for this handler */
17
+ id: string;
18
+ }
19
+ /**
20
+ * Represents a hook currently being executed in the stack.
21
+ */
22
+ interface HookStackEntry {
23
+ /** The hook name being executed */
24
+ name: string;
25
+ /** Current iteration index within the priority groups */
26
+ currentIndex: number;
27
+ }
28
+ /**
29
+ * Options for adding a hook.
30
+ */
31
+ interface AddHookOptions {
32
+ /** Execution priority. Default: 10 */
33
+ priority?: number;
34
+ /** Number of arguments the callback accepts. Default: 1 */
35
+ acceptedArgs?: number;
36
+ }
37
+ /**
38
+ * Result of checking if a hook has handlers.
39
+ * Returns false if no handlers, or the priority of the first matching handler.
40
+ */
41
+ type HasHookResult = false | number;
42
+ /**
43
+ * Registered post type definition.
44
+ */
45
+ interface PostTypeDefinition {
46
+ name: string;
47
+ label: string;
48
+ labels?: Record<string, string>;
49
+ public?: boolean;
50
+ hierarchical?: boolean;
51
+ showInRest?: boolean;
52
+ restBase?: string;
53
+ supports?: string[];
54
+ taxonomies?: string[];
55
+ hasArchive?: boolean;
56
+ rewrite?: {
57
+ slug: string;
58
+ withFront?: boolean;
59
+ } | false;
60
+ menuPosition?: number;
61
+ menuIcon?: string;
62
+ capability_type?: string;
63
+ capabilities?: Record<string, string>;
64
+ }
65
+ /**
66
+ * Registered taxonomy definition.
67
+ */
68
+ interface TaxonomyDefinition {
69
+ name: string;
70
+ objectTypes: string[];
71
+ label: string;
72
+ labels?: Record<string, string>;
73
+ public?: boolean;
74
+ hierarchical?: boolean;
75
+ showInRest?: boolean;
76
+ restBase?: string;
77
+ rewrite?: {
78
+ slug: string;
79
+ withFront?: boolean;
80
+ hierarchical?: boolean;
81
+ } | false;
82
+ }
83
+ /**
84
+ * User capability check context.
85
+ */
86
+ interface CapabilityContext {
87
+ userId: number;
88
+ capability: string;
89
+ objectId?: number;
90
+ }
91
+ /**
92
+ * Standard post statuses.
93
+ */
94
+ declare const POST_STATUS: {
95
+ readonly PUBLISH: "publish";
96
+ readonly DRAFT: "draft";
97
+ readonly PENDING: "pending";
98
+ readonly PRIVATE: "private";
99
+ readonly TRASH: "trash";
100
+ readonly AUTO_DRAFT: "auto-draft";
101
+ readonly INHERIT: "inherit";
102
+ readonly FUTURE: "future";
103
+ };
104
+ /**
105
+ * Default user roles.
106
+ */
107
+ declare const USER_ROLES: {
108
+ readonly ADMINISTRATOR: "administrator";
109
+ readonly EDITOR: "editor";
110
+ readonly AUTHOR: "author";
111
+ readonly CONTRIBUTOR: "contributor";
112
+ readonly SUBSCRIBER: "subscriber";
113
+ };
114
+ /**
115
+ * Default hook priorities.
116
+ */
117
+ declare const HOOK_PRIORITY: {
118
+ readonly EARLIEST: 1;
119
+ readonly EARLY: 5;
120
+ readonly DEFAULT: 10;
121
+ readonly LATE: 15;
122
+ readonly LATEST: 20;
123
+ };
124
+
125
+ /**
126
+ * HookEngine — the backbone of the CMS extensibility system.
127
+ *
128
+ * Implements a WordPress-compatible hook system with actions and filters.
129
+ * Actions are hooks that perform side effects. Filters are hooks that
130
+ * transform a value through a pipeline of callbacks.
131
+ *
132
+ * Features:
133
+ * - Priority-based execution (lower number = earlier execution)
134
+ * - Recursive hook execution support
135
+ * - Universal "all" hook that fires for every hook
136
+ * - Execution stack tracking
137
+ * - Fire counters per hook
138
+ * - Precise removal by callback + priority
139
+ */
140
+ declare class HookEngine {
141
+ /**
142
+ * Map of hook name → array of handlers, kept sorted by priority.
143
+ */
144
+ private hooks;
145
+ /**
146
+ * Stack of hooks currently being executed (supports recursion).
147
+ */
148
+ private currentStack;
149
+ /**
150
+ * Counter of how many times each hook has been fired.
151
+ */
152
+ private fireCount;
153
+ /**
154
+ * Register a callback for a hook.
155
+ *
156
+ * @param hookName - The hook identifier
157
+ * @param callback - The function to execute
158
+ * @param options - Priority and accepted args configuration
159
+ * @returns The generated handler ID
160
+ */
161
+ addHook(hookName: string, callback: HookCallback, options?: AddHookOptions): string;
162
+ /**
163
+ * Remove a specific callback from a hook.
164
+ *
165
+ * Both the callback reference AND priority must match for removal.
166
+ *
167
+ * @param hookName - The hook identifier
168
+ * @param callback - The callback to remove
169
+ * @param priority - The priority it was registered with (default: 10)
170
+ * @returns true if a handler was removed
171
+ */
172
+ removeHook(hookName: string, callback: HookCallback, priority?: number): boolean;
173
+ /**
174
+ * Remove all callbacks from a hook, optionally only for a specific priority.
175
+ *
176
+ * @param hookName - The hook identifier
177
+ * @param priority - If provided, only remove handlers at this priority
178
+ * @returns true if any handlers were removed
179
+ */
180
+ removeAllHooks(hookName: string, priority?: number): boolean;
181
+ /**
182
+ * Check if a hook has registered handlers.
183
+ *
184
+ * @param hookName - The hook identifier
185
+ * @param callback - If provided, check for this specific callback
186
+ * @returns false if no handlers, or the priority of the matching handler
187
+ */
188
+ hasHook(hookName: string, callback?: HookCallback): HasHookResult;
189
+ /**
190
+ * Execute an action hook. All registered callbacks are called in priority order.
191
+ * The "all" universal hook fires before the specific hook.
192
+ *
193
+ * @param hookName - The hook identifier
194
+ * @param args - Arguments to pass to callbacks
195
+ */
196
+ doAction(hookName: string, ...args: unknown[]): Promise<void>;
197
+ /**
198
+ * Execute an action hook synchronously.
199
+ */
200
+ doActionSync(hookName: string, ...args: unknown[]): void;
201
+ /**
202
+ * Execute a filter hook. The first argument is the value being filtered.
203
+ * Each callback receives the (possibly modified) value and returns a new value.
204
+ * The "all" universal hook fires before the specific hook.
205
+ *
206
+ * @param hookName - The hook identifier
207
+ * @param value - The initial value to filter
208
+ * @param args - Additional arguments passed to each callback
209
+ * @returns The filtered value after all callbacks have processed it
210
+ */
211
+ applyFilters(hookName: string, value: unknown, ...args: unknown[]): Promise<unknown>;
212
+ /**
213
+ * Execute a filter hook synchronously.
214
+ */
215
+ applyFiltersSync(hookName: string, value: unknown, ...args: unknown[]): unknown;
216
+ /**
217
+ * Get how many times a hook has been fired.
218
+ */
219
+ getFireCount(hookName: string): number;
220
+ /**
221
+ * Check if a specific hook is currently being executed.
222
+ */
223
+ isDoingHook(hookName?: string): boolean;
224
+ /**
225
+ * Get the name of the hook currently being executed (top of stack).
226
+ * Returns undefined if no hook is executing.
227
+ */
228
+ currentHook(): string | undefined;
229
+ /**
230
+ * Get a snapshot of the current execution stack.
231
+ */
232
+ getExecutionStack(): readonly HookStackEntry[];
233
+ /**
234
+ * Check if a hook has ever been fired (fire count > 0).
235
+ */
236
+ didHook(hookName: string): boolean;
237
+ /**
238
+ * Get the number of handlers registered for a hook.
239
+ */
240
+ getHandlerCount(hookName: string): number;
241
+ /**
242
+ * Reset the engine — useful for testing.
243
+ */
244
+ reset(): void;
245
+ addAction(hookName: string, callback: HookCallback, options?: AddHookOptions): string;
246
+ addFilter(hookName: string, callback: HookCallback, options?: AddHookOptions): string;
247
+ removeAction(hookName: string, callback: HookCallback, priority?: number): boolean;
248
+ removeFilter(hookName: string, callback: HookCallback, priority?: number): boolean;
249
+ hasAction(hookName: string, callback?: HookCallback): HasHookResult;
250
+ hasFilter(hookName: string, callback?: HookCallback): HasHookResult;
251
+ didAction(hookName: string): boolean;
252
+ didFilter(hookName: string): boolean;
253
+ private incrementFireCount;
254
+ private fireUniversalHook;
255
+ private fireUniversalHookSync;
256
+ }
257
+
258
+ /**
259
+ * Registry for content types (post types).
260
+ *
261
+ * Manages registration and lookup of all content types in the system.
262
+ * Built-in types (post, page, attachment, revision, nav_menu_item) are
263
+ * registered during bootstrap; custom types are registered by extensions.
264
+ */
265
+ declare class PostTypeRegistry {
266
+ private types;
267
+ /**
268
+ * Register a new post type.
269
+ *
270
+ * @throws If a type with the same name is already registered
271
+ */
272
+ register(definition: PostTypeDefinition): void;
273
+ /**
274
+ * Get a post type definition by name.
275
+ */
276
+ get(name: string): PostTypeDefinition | undefined;
277
+ /**
278
+ * Check if a post type is registered.
279
+ */
280
+ has(name: string): boolean;
281
+ /**
282
+ * Get all registered post types.
283
+ */
284
+ getAll(): PostTypeDefinition[];
285
+ /**
286
+ * Get only public post types (for REST API, search, etc).
287
+ */
288
+ getPublic(): PostTypeDefinition[];
289
+ /**
290
+ * Get post types that are exposed via REST API.
291
+ */
292
+ getRestVisible(): PostTypeDefinition[];
293
+ /**
294
+ * Unregister a post type. Only custom types can be unregistered.
295
+ */
296
+ unregister(name: string): boolean;
297
+ /**
298
+ * Reset registry — for testing only.
299
+ */
300
+ reset(): void;
301
+ }
302
+ /**
303
+ * Built-in post type definitions, registered during bootstrap.
304
+ */
305
+ declare const BUILTIN_POST_TYPES: PostTypeDefinition[];
306
+
307
+ /**
308
+ * Registry for taxonomies (categories, tags, custom taxonomies).
309
+ */
310
+ declare class TaxonomyRegistry {
311
+ private taxonomies;
312
+ register(definition: TaxonomyDefinition): void;
313
+ get(name: string): TaxonomyDefinition | undefined;
314
+ has(name: string): boolean;
315
+ getAll(): TaxonomyDefinition[];
316
+ /**
317
+ * Get taxonomies assigned to a specific object type (e.g., 'post').
318
+ */
319
+ getForObjectType(objectType: string): TaxonomyDefinition[];
320
+ getRestVisible(): TaxonomyDefinition[];
321
+ unregister(name: string): boolean;
322
+ reset(): void;
323
+ }
324
+ declare const BUILTIN_TAXONOMIES: TaxonomyDefinition[];
325
+
326
+ /**
327
+ * Extension manifest — equivalent to plugin headers in the spec.
328
+ * Found in the extension's manifest.json file.
329
+ */
330
+ interface ExtensionManifest {
331
+ /** Unique slug identifier */
332
+ slug: string;
333
+ /** Display name */
334
+ name: string;
335
+ /** Semantic version */
336
+ version: string;
337
+ /** Short description */
338
+ description?: string;
339
+ /** Author name */
340
+ author?: string;
341
+ /** Author URL */
342
+ authorUri?: string;
343
+ /** Minimum CMS version required */
344
+ minCmsVersion?: string;
345
+ /** Minimum Node.js version required */
346
+ minNodeVersion?: string;
347
+ /** Dependencies (slugs of other extensions) */
348
+ dependencies?: string[];
349
+ /** Text domain for i18n */
350
+ textDomain?: string;
351
+ /** Whether this is a network-wide extension (multisite) */
352
+ network?: boolean;
353
+ /** Entry point file (relative to extension dir). Default: index.ts */
354
+ main?: string;
355
+ }
356
+ type ExtensionStatus = 'active' | 'inactive' | 'paused' | 'must-use';
357
+ interface ExtensionEntry {
358
+ manifest: ExtensionManifest;
359
+ status: ExtensionStatus;
360
+ /** Absolute path to the extension directory */
361
+ path: string;
362
+ /** Error that caused the extension to be paused */
363
+ pauseReason?: string;
364
+ /** When the extension was activated */
365
+ activatedAt?: Date;
366
+ }
367
+ /**
368
+ * Registry that tracks all discovered and active extensions.
369
+ *
370
+ * Extensions go through these states:
371
+ * discovered → activated → loaded → running
372
+ * activated → paused (on fatal error, via recovery mode)
373
+ * running → deactivated → inactive
374
+ */
375
+ declare class ExtensionRegistry {
376
+ private extensions;
377
+ /**
378
+ * Register an extension manifest (discovery phase).
379
+ */
380
+ register(manifest: ExtensionManifest, path: string, status: ExtensionStatus): void;
381
+ get(slug: string): ExtensionEntry | undefined;
382
+ has(slug: string): boolean;
383
+ /**
384
+ * Get all extensions by status.
385
+ */
386
+ getByStatus(status: ExtensionStatus): ExtensionEntry[];
387
+ getAll(): ExtensionEntry[];
388
+ getActive(): ExtensionEntry[];
389
+ getMustUse(): ExtensionEntry[];
390
+ getPaused(): ExtensionEntry[];
391
+ /**
392
+ * Activate an extension. Checks dependencies first.
393
+ *
394
+ * @throws If dependencies are not met
395
+ */
396
+ activate(slug: string): void;
397
+ /**
398
+ * Deactivate an extension. Checks if other extensions depend on it.
399
+ *
400
+ * @throws If other active extensions depend on this one
401
+ */
402
+ deactivate(slug: string): void;
403
+ /**
404
+ * Pause an extension due to a fatal error (recovery mode).
405
+ */
406
+ pause(slug: string, reason: string): void;
407
+ /**
408
+ * Unpause an extension (resume from recovery mode).
409
+ */
410
+ unpause(slug: string): void;
411
+ /**
412
+ * Remove an extension from the registry entirely.
413
+ */
414
+ unregister(slug: string): boolean;
415
+ /**
416
+ * Get dependencies that are not active.
417
+ */
418
+ getUnmetDependencies(slug: string): string[];
419
+ /**
420
+ * Get active extensions that depend on the given extension.
421
+ */
422
+ getDependents(slug: string): ExtensionEntry[];
423
+ /**
424
+ * Check for circular dependencies starting from a slug.
425
+ */
426
+ hasCircularDependency(slug: string, visited?: Set<string>): boolean;
427
+ reset(): void;
428
+ }
429
+
430
+ /**
431
+ * Theme manifest — found in the theme's theme.json file.
432
+ */
433
+ interface ThemeManifest {
434
+ slug: string;
435
+ name: string;
436
+ version: string;
437
+ description?: string;
438
+ author?: string;
439
+ authorUri?: string;
440
+ /** Parent theme slug (for child themes) */
441
+ parent?: string;
442
+ /** Supported features */
443
+ supports?: ThemeSupports;
444
+ /** Menu locations */
445
+ menuLocations?: Record<string, string>;
446
+ /** Design tokens / settings */
447
+ settings?: Record<string, unknown>;
448
+ }
449
+ interface ThemeSupports {
450
+ thumbnails?: boolean;
451
+ postFormats?: string[];
452
+ html5?: string[];
453
+ customLogo?: {
454
+ width?: number;
455
+ height?: number;
456
+ flexWidth?: boolean;
457
+ flexHeight?: boolean;
458
+ };
459
+ customHeader?: {
460
+ width?: number;
461
+ height?: number;
462
+ flexWidth?: boolean;
463
+ flexHeight?: boolean;
464
+ };
465
+ customBackground?: boolean;
466
+ menus?: boolean;
467
+ feedLinks?: boolean;
468
+ responsiveEmbeds?: boolean;
469
+ blockTemplates?: boolean;
470
+ }
471
+ interface ThemeEntry {
472
+ manifest: ThemeManifest;
473
+ path: string;
474
+ active: boolean;
475
+ parentTheme?: ThemeEntry;
476
+ }
477
+ /**
478
+ * Registry for installed themes.
479
+ */
480
+ declare class ThemeRegistry {
481
+ private themes;
482
+ private activeSlug;
483
+ register(manifest: ThemeManifest, path: string): void;
484
+ get(slug: string): ThemeEntry | undefined;
485
+ getAll(): ThemeEntry[];
486
+ getActive(): ThemeEntry | undefined;
487
+ /**
488
+ * Activate a theme. Resolves parent theme for child themes.
489
+ */
490
+ activate(slug: string): void;
491
+ /**
492
+ * Check if the active theme supports a feature.
493
+ */
494
+ supports(feature: keyof ThemeSupports): boolean;
495
+ reset(): void;
496
+ }
497
+ /**
498
+ * Template hierarchy resolver.
499
+ *
500
+ * Given query flags and context, returns an ordered list of template
501
+ * names to search for (most specific first).
502
+ */
503
+ interface TemplateContext {
504
+ type: 'single' | 'page' | 'category' | 'tag' | 'taxonomy' | 'author' | 'date' | 'search' | '404' | 'home' | 'archive' | 'attachment';
505
+ slug?: string;
506
+ id?: number;
507
+ postType?: string;
508
+ taxonomy?: string;
509
+ term?: string;
510
+ mimeType?: string;
511
+ mimeSubtype?: string;
512
+ nicename?: string;
513
+ customTemplate?: string;
514
+ }
515
+ declare function resolveTemplateHierarchy(ctx: TemplateContext): string[];
516
+
517
+ /**
518
+ * The 17 bootstrap phases as defined in the spec.
519
+ * Each phase is a named step executed in strict order during startup.
520
+ */
521
+ declare const BOOTSTRAP_PHASES: readonly ["entry_point", "configuration", "initial_constants", "environment_check", "error_handling", "core_functions", "database_connect", "object_cache", "default_filters", "must_use_extensions", "regular_extensions", "overridable_functions", "extensions_loaded", "global_objects", "theme", "init", "system_loaded"];
522
+ type BootstrapPhase = (typeof BOOTSTRAP_PHASES)[number];
523
+ /**
524
+ * Callback for a bootstrap phase.
525
+ */
526
+ type PhaseHandler = () => void | Promise<void>;
527
+ /**
528
+ * Orchestrates the CMS bootstrap process through 17 ordered phases.
529
+ *
530
+ * Each phase can have multiple handlers registered. Handlers within a phase
531
+ * run in registration order. Phases are always executed in the order defined
532
+ * by BOOTSTRAP_PHASES.
533
+ *
534
+ * The HookEngine fires a hook for each phase, allowing extensions to tap in.
535
+ */
536
+ declare class BootstrapManager {
537
+ private hooks;
538
+ private phaseHandlers;
539
+ private completedPhases;
540
+ private currentPhase;
541
+ constructor(hooks: HookEngine);
542
+ /**
543
+ * Register a handler for a bootstrap phase.
544
+ */
545
+ on(phase: BootstrapPhase, handler: PhaseHandler): void;
546
+ /**
547
+ * Execute all 17 bootstrap phases in order.
548
+ * For short-init mode, pass a phase to stop at.
549
+ */
550
+ run(stopAfter?: BootstrapPhase): Promise<void>;
551
+ /**
552
+ * Check if a phase has been completed.
553
+ */
554
+ isPhaseComplete(phase: BootstrapPhase): boolean;
555
+ /**
556
+ * Get the phase currently being executed.
557
+ */
558
+ getCurrentPhase(): BootstrapPhase | null;
559
+ /**
560
+ * Get all completed phases.
561
+ */
562
+ getCompletedPhases(): BootstrapPhase[];
563
+ /**
564
+ * Reset — for testing.
565
+ */
566
+ reset(): void;
567
+ }
568
+
569
+ export { type AddHookOptions, BOOTSTRAP_PHASES, BUILTIN_POST_TYPES, BUILTIN_TAXONOMIES, BootstrapManager, type BootstrapPhase, type CapabilityContext, type ExtensionEntry, type ExtensionManifest, ExtensionRegistry, type ExtensionStatus, HOOK_PRIORITY, type HasHookResult, type HookCallback, HookEngine, type HookHandler, type HookStackEntry, POST_STATUS, type PhaseHandler, type PostTypeDefinition, PostTypeRegistry, type TaxonomyDefinition, TaxonomyRegistry, type TemplateContext, type ThemeEntry, type ThemeManifest, ThemeRegistry, type ThemeSupports, USER_ROLES, resolveTemplateHierarchy };