@marvalt/wadapter 2.3.44 → 2.3.46

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.js CHANGED
@@ -289,11 +289,107 @@ class WordPressClient {
289
289
  }
290
290
  /**
291
291
  * Fetch a specific navigation menu with its items
292
- * @param menuId Menu ID
292
+ * Handles both menu term IDs and wp_navigation post IDs
293
+ * @param menuId Menu ID (can be menu term ID or wp_navigation post ID)
293
294
  * @returns Menu data with hierarchical items
294
295
  */
295
296
  async getMenu(menuId) {
296
- return this.makeRequest(`/wp-custom/v1/menus/${menuId}`);
297
+ console.log(`🌐 [WordPressClient] Fetching menu ID ${menuId}...`);
298
+ try {
299
+ // First try the custom endpoint (handles both menu terms and wp_navigation posts)
300
+ console.log(` → Trying endpoint: /wp-custom/v1/menus/${menuId}`);
301
+ const result = await this.makeRequest(`/wp-custom/v1/menus/${menuId}`);
302
+ console.log(` ✅ Successfully fetched menu ID ${menuId} as menu term: ${result.name}`);
303
+ return result;
304
+ }
305
+ catch (error) {
306
+ const errorStatus = error.status || (error.response?.status) || 'unknown';
307
+ const errorMessage = error.message || 'Unknown error';
308
+ console.log(` ❌ Menu term fetch failed (status: ${errorStatus}): ${errorMessage}`);
309
+ // If 404, try fetching as wp_navigation post
310
+ if (errorStatus === 404 || errorMessage?.includes('404')) {
311
+ console.log(` → Menu ID ${menuId} not found as menu term, trying as wp_navigation post...`);
312
+ // Try custom endpoint first
313
+ try {
314
+ console.log(` → Trying navigation post endpoint: /wp-custom/v1/navigation/${menuId}`);
315
+ const navPost = await this.makeRequest(`/wp-custom/v1/navigation/${menuId}`);
316
+ console.log(` ✅ Navigation post fetched:`, {
317
+ id: navPost.id,
318
+ hasMenu: !!navPost.menu,
319
+ menuTermId: navPost.menu_term_id,
320
+ });
321
+ if (navPost.menu) {
322
+ console.log(` ✅ Returning menu from navigation post: ${navPost.menu.name}`);
323
+ return navPost.menu;
324
+ }
325
+ // If navigation post has a menu_term_id, fetch that menu
326
+ if (navPost.menu_term_id) {
327
+ console.log(` → Navigation post has menu_term_id ${navPost.menu_term_id}, fetching menu...`);
328
+ const menu = await this.makeRequest(`/wp-custom/v1/menus/${navPost.menu_term_id}`);
329
+ console.log(` ✅ Fetched menu from menu_term_id ${navPost.menu_term_id}: ${menu.name}`);
330
+ return menu;
331
+ }
332
+ else {
333
+ console.warn(` âš ī¸ Navigation post ${menuId} has no menu_term_id and no menu data`);
334
+ throw new Error(`Navigation post ${menuId} has no menu_term_id and no menu data`);
335
+ }
336
+ }
337
+ catch (navError) {
338
+ const navErrorStatus = navError.status || (navError.response?.status) || 'unknown';
339
+ const navErrorMessage = navError.message || 'Unknown error';
340
+ console.log(` ❌ Navigation post fetch failed (status: ${navErrorStatus}): ${navErrorMessage}`);
341
+ // If custom endpoint fails, try WordPress native REST API (WordPress 6.3+)
342
+ if (navErrorStatus === 404 || navErrorMessage?.includes('404')) {
343
+ console.log(` → Custom navigation endpoint not found, trying WordPress native REST API...`);
344
+ try {
345
+ console.log(` → Trying WordPress native REST API: /wp/v2/navigation/${menuId}`);
346
+ // WordPress 6.3+ has native REST API for wp_navigation posts
347
+ const navPost = await this.makeRequest(`/wp/v2/navigation/${menuId}`);
348
+ console.log(` ✅ WordPress native navigation post fetched:`, {
349
+ id: navPost.id,
350
+ title: navPost.title?.rendered,
351
+ hasContent: !!navPost.content?.raw,
352
+ });
353
+ // Parse blocks from content to find menu reference
354
+ // The content.raw contains the block JSON
355
+ if (navPost.content?.raw) {
356
+ try {
357
+ const blocks = JSON.parse(navPost.content.raw);
358
+ console.log(` → Parsed ${Array.isArray(blocks) ? blocks.length : 'unknown'} blocks from navigation post`);
359
+ // Look for menu reference in blocks (this would need to be done server-side)
360
+ // For now, throw error to indicate menu extraction requires server-side processing
361
+ console.log(`â„šī¸ Found wp_navigation post ${menuId}, but menu extraction requires server-side processing`);
362
+ throw new Error(`Menu extraction from wp_navigation post ${menuId} requires server-side processing`);
363
+ }
364
+ catch (parseError) {
365
+ console.log(`âš ī¸ Could not parse navigation post content`);
366
+ throw new Error(`Could not parse navigation post content for menu ID ${menuId}`);
367
+ }
368
+ }
369
+ else {
370
+ throw new Error(`wp_navigation post ${menuId} has no content to parse`);
371
+ }
372
+ }
373
+ catch (wpError) {
374
+ const wpErrorStatus = wpError.status || (wpError.response?.status) || 'unknown';
375
+ const wpErrorMessage = wpError.message || 'Unknown error';
376
+ console.error(` ❌ WordPress native REST API also failed (status: ${wpErrorStatus}): ${wpErrorMessage}`);
377
+ console.error(` Full WordPress API error:`, wpError);
378
+ // Re-throw to ensure error is propagated
379
+ throw new Error(`Could not fetch menu ID ${menuId} via any method: ${wpErrorMessage}`);
380
+ }
381
+ }
382
+ else {
383
+ console.error(` ❌ Navigation post fetch failed with non-404 error, re-throwing...`);
384
+ throw navError; // Re-throw navigation error
385
+ }
386
+ }
387
+ }
388
+ else {
389
+ console.error(` ❌ Menu term fetch failed with non-404 error (status: ${errorStatus}), re-throwing...`);
390
+ throw error; // Re-throw original error
391
+ }
392
+ }
297
393
  }
298
394
  async getFooter(slug = 'footer', area = 'footer') {
299
395
  return this.makeRequest(`/wp-custom/v1/footer`, { slug, area });