| 1 |
/** |
| 2 |
* ThinkRank Metabox Entry Point |
| 3 |
* |
| 4 |
* React-based metabox implementation for SEO optimization |
| 5 |
* |
| 6 |
* @package ThinkRank |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
import { createRoot } from '@wordpress/element'; |
| 11 |
import { __ } from '@wordpress/i18n'; |
| 12 |
import './styles.scss'; |
| 13 |
import MetaboxApp from './components/MetaboxApp'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Initialize React metabox |
| 17 |
*/ |
| 18 |
document.addEventListener('DOMContentLoaded', () => { |
| 19 |
// Find metabox container |
| 20 |
const container = document.getElementById('thinkrank-metabox-container'); |
| 21 |
|
| 22 |
if (container) { |
| 23 |
// Get data from global thinkrankMetabox object (localized by PHP) |
| 24 |
const metaboxData = window.thinkrankMetabox || {}; |
| 25 |
|
| 26 |
// Get existing metadata from form fields |
| 27 |
const existingMetadata = { |
| 28 |
title: getFieldValue('thinkrank_seo_title'), |
| 29 |
description: getFieldValue('thinkrank_meta_description'), |
| 30 |
focus_keyword: getFieldValue('thinkrank_focus_keyword'), |
| 31 |
seo_score: getFieldValue('thinkrank_seo_score'), |
| 32 |
generated_at: getFieldValue('thinkrank_generated_at') |
| 33 |
}; |
| 34 |
|
| 35 |
// Get content preview |
| 36 |
const contentPreview = getFieldValue('thinkrank_content_preview'); |
| 37 |
|
| 38 |
// Get post title and home URL |
| 39 |
const postTitle = document.getElementById('title')?.value || ''; |
| 40 |
const homeUrl = metaboxData.homeUrl || window.location.origin; |
| 41 |
|
| 42 |
try { |
| 43 |
// Clear the container and render React component using createRoot |
| 44 |
container.innerHTML = ''; |
| 45 |
|
| 46 |
// Create root and render component |
| 47 |
const root = createRoot(container); |
| 48 |
root.render( |
| 49 |
<MetaboxApp |
| 50 |
postId={metaboxData.postId} |
| 51 |
existingMetadata={existingMetadata} |
| 52 |
contentPreview={contentPreview} |
| 53 |
strings={metaboxData.strings || {}} |
| 54 |
postTitle={postTitle} |
| 55 |
homeUrl={homeUrl} |
| 56 |
/> |
| 57 |
); |
| 58 |
} catch (error) { |
| 59 |
// Fallback error display for metabox render failure |
| 60 |
container.innerHTML = ` |
| 61 |
<div class="notice notice-error"> |
| 62 |
<p>${__('Failed to load ThinkRank metabox. Please refresh the page.', 'thinkrank')}</p> |
| 63 |
</div> |
| 64 |
`; |
| 65 |
} |
| 66 |
} |
| 67 |
}); |
| 68 |
|
| 69 |
/** |
| 70 |
* Helper function to get field value |
| 71 |
*/ |
| 72 |
function getFieldValue(fieldId) { |
| 73 |
const field = document.getElementById(fieldId); |
| 74 |
return field ? field.value : ''; |
| 75 |
} |
| 76 |
|