UNPKG

@ai-growth/n8n-nodes-wordpress

Version:

n8n node for WordPress integration with AI GROWTH - SEO WP plugin

305 lines 18.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MetadataService = void 0; const ErrorUtils_1 = require("../utils/ErrorUtils"); /** * Serviço para gerenciamento de metadados SEO do plugin AI GROWTH - SEO WP */ class MetadataService { /** * Construtor do serviço * @param client Cliente WordPress */ constructor(client) { this.pluginEndpoint = 'ai-growth-seo/v1'; this.pluginValidated = false; this.pluginAvailable = false; this.client = client; } /** * Valida se o endpoint do plugin AI GROWTH está disponível * @returns Promise<boolean> - true se o plugin estiver disponível */ async validatePluginEndpoint() { if (this.pluginValidated) { return this.pluginAvailable; } try { console.log('[MetadataService] Validating AI GROWTH plugin endpoint...'); // Tenta acessar o endpoint de informações do plugin const response = await this.client.get(`${this.pluginEndpoint}/info`); this.pluginAvailable = true; this.pluginValidated = true; console.log('[MetadataService] AI GROWTH plugin is available:', { version: (response === null || response === void 0 ? void 0 : response.version) || 'unknown', features: (response === null || response === void 0 ? void 0 : response.features) || [] }); return true; } catch (error) { this.pluginAvailable = false; this.pluginValidated = true; console.warn('[MetadataService] AI GROWTH plugin not available, using WordPress standard API:', { error: error instanceof Error ? error.message : 'Unknown error', endpoint: `${this.pluginEndpoint}/info` }); return false; } } /** * Atualiza os metadados SEO de um post/página usando API padrão do WordPress * @param postId ID do post/página * @param metadata Metadados a serem atualizados * @returns Metadados atualizados */ async updateMetadataViaWordPressAPI(postId, metadata) { var _a, _b, _c, _d, _e, _f, _g; console.log('[MetadataService] Using WordPress standard API for metadata update'); // Preparar meta fields no formato do WordPress const metaFields = {}; if (metadata.meta_title !== undefined) { metaFields._ai_growth_seo_meta_title = metadata.meta_title; console.log('[MetadataService] WordPress API - Including meta_title:', metadata.meta_title); } if (metadata.meta_description !== undefined) { metaFields._ai_growth_seo_meta_description = metadata.meta_description; console.log('[MetadataService] WordPress API - Including meta_description:', metadata.meta_description); } if (metadata.meta_keywords !== undefined) { metaFields._ai_growth_seo_meta_keywords = metadata.meta_keywords; console.log('[MetadataService] WordPress API - Including meta_keywords:', metadata.meta_keywords); } if (metadata.og_title !== undefined) { metaFields._ai_growth_seo_og_title = metadata.og_title; console.log('[MetadataService] WordPress API - Including og_title:', metadata.og_title); } if (metadata.og_description !== undefined) { metaFields._ai_growth_seo_og_description = metadata.og_description; console.log('[MetadataService] WordPress API - Including og_description:', metadata.og_description); } if (metadata.twitter_title !== undefined) { metaFields._ai_growth_seo_twitter_title = metadata.twitter_title; console.log('[MetadataService] WordPress API - Including twitter_title:', metadata.twitter_title); } if (metadata.twitter_description !== undefined) { metaFields._ai_growth_seo_twitter_description = metadata.twitter_description; console.log('[MetadataService] WordPress API - Including twitter_description:', metadata.twitter_description); } if (Object.keys(metaFields).length === 0) { console.log('[MetadataService] WordPress API - No metadata fields to update'); return {}; } const payload = { meta: metaFields }; console.log('[MetadataService] WordPress API - Sending update request:', { endpoint: `posts/${postId}`, payload }); // Atualizar via API padrão do WordPress const response = await this.client.post(`posts/${postId}`, payload); console.log('[MetadataService] WordPress API - Response received:', { id: response === null || response === void 0 ? void 0 : response.id, meta: response === null || response === void 0 ? void 0 : response.meta }); // Retornar metadados no formato esperado const result = { meta_title: ((_a = response === null || response === void 0 ? void 0 : response.meta) === null || _a === void 0 ? void 0 : _a._ai_growth_seo_meta_title) || metadata.meta_title || '', meta_description: ((_b = response === null || response === void 0 ? void 0 : response.meta) === null || _b === void 0 ? void 0 : _b._ai_growth_seo_meta_description) || metadata.meta_description || '', meta_keywords: ((_c = response === null || response === void 0 ? void 0 : response.meta) === null || _c === void 0 ? void 0 : _c._ai_growth_seo_meta_keywords) || metadata.meta_keywords || '', og_title: ((_d = response === null || response === void 0 ? void 0 : response.meta) === null || _d === void 0 ? void 0 : _d._ai_growth_seo_og_title) || metadata.og_title || '', og_description: ((_e = response === null || response === void 0 ? void 0 : response.meta) === null || _e === void 0 ? void 0 : _e._ai_growth_seo_og_description) || metadata.og_description || '', twitter_title: ((_f = response === null || response === void 0 ? void 0 : response.meta) === null || _f === void 0 ? void 0 : _f._ai_growth_seo_twitter_title) || metadata.twitter_title || '', twitter_description: ((_g = response === null || response === void 0 ? void 0 : response.meta) === null || _g === void 0 ? void 0 : _g._ai_growth_seo_twitter_description) || metadata.twitter_description || '' }; console.log('[MetadataService] WordPress API - Metadata update completed successfully:', result); return result; } /** * Atualiza os metadados SEO de um post/página * @param postId ID do post/página * @param metadata Metadados a serem atualizados * @returns Metadados atualizados */ async updateMetadata(postId, metadata) { try { console.log('[MetadataService] Starting metadata update for post:', { postId, metadata: { meta_title: metadata.meta_title, meta_description: metadata.meta_description, meta_keywords: metadata.meta_keywords, og_title: metadata.og_title, og_description: metadata.og_description, twitter_title: metadata.twitter_title, twitter_description: metadata.twitter_description, hasMetaTitle: metadata.meta_title !== undefined, hasMetaDescription: metadata.meta_description !== undefined, hasMetaKeywords: metadata.meta_keywords !== undefined } }); // Verificar se o post existe await this.client.get(`posts/${postId}`); console.log('[MetadataService] Post exists, proceeding with metadata update'); // Validar disponibilidade do plugin const pluginAvailable = await this.validatePluginEndpoint(); if (!pluginAvailable) { console.log('[MetadataService] Plugin not available, using WordPress standard API'); return await this.updateMetadataViaWordPressAPI(postId, metadata); } // Usar API do plugin se disponível const updateData = {}; if (metadata.meta_title !== undefined) { updateData.meta_title = metadata.meta_title; console.log('[MetadataService] Plugin API - Including meta_title:', metadata.meta_title); } if (metadata.meta_description !== undefined) { updateData.meta_description = metadata.meta_description; console.log('[MetadataService] Plugin API - Including meta_description:', metadata.meta_description); } if (metadata.meta_keywords !== undefined) { updateData.meta_keywords = metadata.meta_keywords; console.log('[MetadataService] Plugin API - Including meta_keywords:', metadata.meta_keywords); } if (metadata.og_title !== undefined) { updateData.og_title = metadata.og_title; console.log('[MetadataService] Plugin API - Including og_title:', metadata.og_title); } if (metadata.og_description !== undefined) { updateData.og_description = metadata.og_description; console.log('[MetadataService] Plugin API - Including og_description:', metadata.og_description); } if (metadata.twitter_title !== undefined) { updateData.twitter_title = metadata.twitter_title; console.log('[MetadataService] Plugin API - Including twitter_title:', metadata.twitter_title); } if (metadata.twitter_description !== undefined) { updateData.twitter_description = metadata.twitter_description; console.log('[MetadataService] Plugin API - Including twitter_description:', metadata.twitter_description); } // Se não há dados para atualizar, retornar metadados vazios if (Object.keys(updateData).length === 0) { console.log('[MetadataService] No metadata fields to update'); return {}; } console.log('[MetadataService] Sending update request to plugin:', { endpoint: `${this.pluginEndpoint}/post/${postId}/metadata`, data: updateData }); // Atualizar metadados via plugin try { const response = await this.client.post(`${this.pluginEndpoint}/post/${postId}/metadata`, updateData); console.log('[MetadataService] Plugin response received:', response); const result = { meta_title: (response === null || response === void 0 ? void 0 : response.meta_title) || '', meta_description: (response === null || response === void 0 ? void 0 : response.meta_description) || '', meta_keywords: (response === null || response === void 0 ? void 0 : response.meta_keywords) || '', og_title: (response === null || response === void 0 ? void 0 : response.og_title) || '', og_description: (response === null || response === void 0 ? void 0 : response.og_description) || '', twitter_title: (response === null || response === void 0 ? void 0 : response.twitter_title) || '', twitter_description: (response === null || response === void 0 ? void 0 : response.twitter_description) || '' }; console.log('[MetadataService] Plugin metadata update completed successfully:', result); return result; } catch (error) { console.error('[MetadataService] Plugin API call failed, falling back to WordPress API:', { error: error instanceof Error ? error.message : 'Unknown error', endpoint: `${this.pluginEndpoint}/post/${postId}/metadata`, data: updateData }); // Fallback para API padrão do WordPress return await this.updateMetadataViaWordPressAPI(postId, metadata); } } catch (error) { console.error('[MetadataService] Metadata update failed:', { postId, error: error instanceof Error ? error.message : 'Unknown error' }); if (error instanceof ErrorUtils_1.WordPressError) { throw error; } throw new ErrorUtils_1.WordPressError(`Erro ao atualizar metadados SEO para o post ${postId}: ${error.message}`, ErrorUtils_1.WordPressErrorType.UNKNOWN, undefined, error); } } /** * Obtém os metadados SEO de um post/página usando API padrão do WordPress * @param postId ID do post/página * @returns Metadados SEO */ async getMetadataViaWordPressAPI(postId) { var _a, _b, _c, _d, _e, _f, _g; console.log('[MetadataService] WordPress API - Getting metadata for post:', postId); const response = await this.client.get(`posts/${postId}`); console.log('[MetadataService] WordPress API - Post response received:', { id: response === null || response === void 0 ? void 0 : response.id, meta: response === null || response === void 0 ? void 0 : response.meta }); const result = { meta_title: ((_a = response === null || response === void 0 ? void 0 : response.meta) === null || _a === void 0 ? void 0 : _a._ai_growth_seo_meta_title) || '', meta_description: ((_b = response === null || response === void 0 ? void 0 : response.meta) === null || _b === void 0 ? void 0 : _b._ai_growth_seo_meta_description) || '', meta_keywords: ((_c = response === null || response === void 0 ? void 0 : response.meta) === null || _c === void 0 ? void 0 : _c._ai_growth_seo_meta_keywords) || '', og_title: ((_d = response === null || response === void 0 ? void 0 : response.meta) === null || _d === void 0 ? void 0 : _d._ai_growth_seo_og_title) || '', og_description: ((_e = response === null || response === void 0 ? void 0 : response.meta) === null || _e === void 0 ? void 0 : _e._ai_growth_seo_og_description) || '', twitter_title: ((_f = response === null || response === void 0 ? void 0 : response.meta) === null || _f === void 0 ? void 0 : _f._ai_growth_seo_twitter_title) || '', twitter_description: ((_g = response === null || response === void 0 ? void 0 : response.meta) === null || _g === void 0 ? void 0 : _g._ai_growth_seo_twitter_description) || '' }; console.log('[MetadataService] WordPress API - Metadata retrieval completed:', result); return result; } /** * Obtém os metadados SEO de um post/página * @param postId ID do post/página * @returns Metadados SEO */ async getMetadata(postId) { try { console.log('[MetadataService] Getting metadata for post:', postId); // Verificar se o post existe await this.client.get(`posts/${postId}`); console.log('[MetadataService] Post exists, proceeding with metadata retrieval'); // Validar disponibilidade do plugin const pluginAvailable = await this.validatePluginEndpoint(); if (!pluginAvailable) { console.log('[MetadataService] Plugin not available, using WordPress standard API'); return await this.getMetadataViaWordPressAPI(postId); } // Obter metadados via plugin try { console.log('[MetadataService] Requesting metadata from plugin:', `${this.pluginEndpoint}/post/${postId}/metadata`); const response = await this.client.get(`${this.pluginEndpoint}/post/${postId}/metadata`); console.log('[MetadataService] Plugin response received:', response); const result = { meta_title: (response === null || response === void 0 ? void 0 : response.meta_title) || '', meta_description: (response === null || response === void 0 ? void 0 : response.meta_description) || '', meta_keywords: (response === null || response === void 0 ? void 0 : response.meta_keywords) || '', og_title: (response === null || response === void 0 ? void 0 : response.og_title) || '', og_description: (response === null || response === void 0 ? void 0 : response.og_description) || '', twitter_title: (response === null || response === void 0 ? void 0 : response.twitter_title) || '', twitter_description: (response === null || response === void 0 ? void 0 : response.twitter_description) || '' }; console.log('[MetadataService] Plugin metadata retrieval completed successfully:', result); return result; } catch (error) { console.error('[MetadataService] Plugin API call failed, falling back to WordPress API:', { error: error instanceof Error ? error.message : 'Unknown error', endpoint: `${this.pluginEndpoint}/post/${postId}/metadata` }); // Fallback para API padrão do WordPress return await this.getMetadataViaWordPressAPI(postId); } } catch (error) { console.error('[MetadataService] Metadata retrieval failed:', { postId, error: error instanceof Error ? error.message : 'Unknown error' }); if (error instanceof ErrorUtils_1.WordPressError) { throw error; } throw new ErrorUtils_1.WordPressError(`Erro ao obter metadados SEO para o post ${postId}: ${error.message}`, ErrorUtils_1.WordPressErrorType.UNKNOWN, undefined, error); } } } exports.MetadataService = MetadataService; //# sourceMappingURL=MetadataService.js.map