| 1 |
/** |
| 2 |
* Schema Settings Configuration |
| 3 |
* |
| 4 |
* Shared configuration for schema settings between frontend components. |
| 5 |
* This eliminates duplication and ensures consistency across the application. |
| 6 |
* |
| 7 |
* @package ThinkRank |
| 8 |
* @subpackage Config |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
import { __ } from '@wordpress/i18n'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Get default schema settings |
| 16 |
* |
| 17 |
* @param {string} contextType Context type ('site', 'post', 'page', 'product') |
| 18 |
* @returns {Object} Default settings object |
| 19 |
*/ |
| 20 |
export const getDefaultSettings = (contextType = 'site') => { |
| 21 |
const defaults = { |
| 22 |
// Core settings |
| 23 |
auto_generate_schema: true, |
| 24 |
enabled_schema_types: [], |
| 25 |
validation_level: 'moderate', |
| 26 |
rich_snippets_optimization: true, |
| 27 |
cache_duration: 3600, // 1 hour |
| 28 |
auto_deploy: true, |
| 29 |
|
| 30 |
// Basic schema settings (moved from Site Identity) |
| 31 |
organization_schema: true, |
| 32 |
knowledge_graph: true, |
| 33 |
|
| 34 |
// Organization schema settings |
| 35 |
organization_name: '', |
| 36 |
organization_type: 'Organization', |
| 37 |
organization_logo: '', |
| 38 |
organization_url: '', |
| 39 |
organization_description: '', |
| 40 |
organization_social_facebook: '', |
| 41 |
organization_social_twitter: '', |
| 42 |
organization_social_linkedin: '', |
| 43 |
organization_social_instagram: '', |
| 44 |
organization_social_youtube: '', |
| 45 |
organization_contact_type: 'customer service', |
| 46 |
organization_contact_phone: '', |
| 47 |
organization_contact_email: '', |
| 48 |
organization_contact_hours: '', |
| 49 |
|
| 50 |
// Website schema settings |
| 51 |
website_name: '', |
| 52 |
website_url: '', |
| 53 |
website_description: '', |
| 54 |
website_author: '', |
| 55 |
|
| 56 |
// LocalBusiness schema settings (now managed in Business Info tab) |
| 57 |
// business_price_range: moved to Site Identity Business Info |
| 58 |
// business_geo_latitude: moved to Site Identity Business Info |
| 59 |
// business_geo_longitude: moved to Site Identity Business Info |
| 60 |
// business_opening_hours: moved to Site Identity Business Info |
| 61 |
|
| 62 |
// Content schema settings (site-wide only) |
| 63 |
enable_breadcrumbs_schema: true, |
| 64 |
enable_local_business: false, |
| 65 |
|
| 66 |
// Removed post/page-specific schema settings (Product, Event, Article, Software Application, HowTo, FAQ) |
| 67 |
// These are now handled only at the post/page level via metabox |
| 68 |
|
| 69 |
// Person schema settings (site-wide) |
| 70 |
person_name: '', |
| 71 |
person_job_title: '', |
| 72 |
person_description: '', |
| 73 |
person_image: '', |
| 74 |
person_url: '', |
| 75 |
person_email: '', |
| 76 |
person_telephone: '', |
| 77 |
person_address: '', |
| 78 |
person_birth_date: '', |
| 79 |
person_nationality: '', |
| 80 |
person_works_for: '', |
| 81 |
person_same_as: [], |
| 82 |
|
| 83 |
// Removed FAQPage schema settings (now handled at post/page level) |
| 84 |
}; |
| 85 |
|
| 86 |
// Context-specific defaults |
| 87 |
switch (contextType) { |
| 88 |
case 'site': |
| 89 |
defaults.enabled_schema_types = ['Organization', 'LocalBusiness']; |
| 90 |
defaults.auto_deploy = true; |
| 91 |
break; |
| 92 |
case 'post': |
| 93 |
defaults.enabled_schema_types = ['Article', 'Person']; |
| 94 |
defaults.validation_level = 'strict'; |
| 95 |
break; |
| 96 |
case 'page': |
| 97 |
defaults.enabled_schema_types = ['Article', 'FAQ']; |
| 98 |
defaults.rich_snippets_optimization = true; |
| 99 |
break; |
| 100 |
case 'product': |
| 101 |
defaults.enabled_schema_types = ['Product']; |
| 102 |
defaults.validation_level = 'strict'; |
| 103 |
defaults.rich_snippets_optimization = true; |
| 104 |
break; |
| 105 |
} |
| 106 |
|
| 107 |
return defaults; |
| 108 |
}; |
| 109 |
|
| 110 |
/** |
| 111 |
* Get supported schema types for context |
| 112 |
* |
| 113 |
* @param {string} contextType Context type |
| 114 |
* @returns {Array} Supported schema types |
| 115 |
*/ |
| 116 |
export const getSupportedSchemaTypes = (contextType = 'site') => { |
| 117 |
// Site-level schema types only (post/page schemas handled by metabox) |
| 118 |
const siteTypes = [ |
| 119 |
'Organization', 'LocalBusiness', 'Person', 'WebSite' |
| 120 |
]; |
| 121 |
|
| 122 |
// All schema types for metabox context |
| 123 |
const allTypes = [ |
| 124 |
'Article', 'BlogPosting', 'TechnicalArticle', 'NewsArticle', |
| 125 |
'ScholarlyArticle', 'Report', 'Product', 'Organization', |
| 126 |
'LocalBusiness', 'Person', 'WebSite', 'FAQPage', |
| 127 |
'Event', 'HowTo' |
| 128 |
]; |
| 129 |
|
| 130 |
// Return appropriate types based on context |
| 131 |
return contextType === 'metabox' ? allTypes : siteTypes; |
| 132 |
}; |
| 133 |
|
| 134 |
/** |
| 135 |
* Get validation level options |
| 136 |
* |
| 137 |
* @returns {Array} Validation level options |
| 138 |
*/ |
| 139 |
export const getValidationLevelOptions = () => [ |
| 140 |
{ value: 'strict', label: __('Strict', 'thinkrank') }, |
| 141 |
{ value: 'moderate', label: __('Moderate', 'thinkrank') }, |
| 142 |
{ value: 'lenient', label: __('Lenient', 'thinkrank') } |
| 143 |
]; |
| 144 |
|
| 145 |
/** |
| 146 |
* Get boolean settings list |
| 147 |
* |
| 148 |
* @returns {Array} List of boolean setting keys |
| 149 |
*/ |
| 150 |
export const getBooleanSettings = () => [ |
| 151 |
'enabled', |
| 152 |
'auto_generate_schema', |
| 153 |
'rich_snippets_optimization', |
| 154 |
'auto_deploy', |
| 155 |
'organization_schema', |
| 156 |
'knowledge_graph', |
| 157 |
'enable_breadcrumbs_schema', |
| 158 |
'enable_article_schema', |
| 159 |
'enable_product_schema', |
| 160 |
'enable_local_business', |
| 161 |
'enable_faq_schema', |
| 162 |
'enable_howto_schema' |
| 163 |
]; |
| 164 |
|
| 165 |
/** |
| 166 |
* Get array settings list |
| 167 |
* |
| 168 |
* @returns {Array} List of array setting keys |
| 169 |
*/ |
| 170 |
export const getArraySettings = () => [ |
| 171 |
'enabled_schema_types', |
| 172 |
'software_operating_systems', |
| 173 |
'software_features', |
| 174 |
'howto_supply' |
| 175 |
]; |
| 176 |
|
| 177 |
/** |
| 178 |
* Get integer settings list |
| 179 |
* |
| 180 |
* @returns {Array} List of integer setting keys |
| 181 |
*/ |
| 182 |
export const getIntegerSettings = () => [ |
| 183 |
'cache_duration' |
| 184 |
]; |
| 185 |
|
| 186 |
/** |
| 187 |
* Get organization type options |
| 188 |
* |
| 189 |
* @returns {Array} Organization type options |
| 190 |
*/ |
| 191 |
export const getOrganizationTypeOptions = () => [ |
| 192 |
{ value: 'Organization', label: __('Organization', 'thinkrank') }, |
| 193 |
{ value: 'Corporation', label: __('Corporation', 'thinkrank') }, |
| 194 |
{ value: 'EducationalOrganization', label: __('Educational Organization', 'thinkrank') }, |
| 195 |
{ value: 'GovernmentOrganization', label: __('Government Organization', 'thinkrank') }, |
| 196 |
{ value: 'LocalBusiness', label: __('Local Business', 'thinkrank') }, |
| 197 |
{ value: 'NGO', label: __('Non-Governmental Organization', 'thinkrank') }, |
| 198 |
{ value: 'PerformingGroup', label: __('Performing Group', 'thinkrank') }, |
| 199 |
{ value: 'SportsOrganization', label: __('Sports Organization', 'thinkrank') } |
| 200 |
]; |
| 201 |
|
| 202 |
/** |
| 203 |
* Get contact type options |
| 204 |
* |
| 205 |
* @returns {Array} Contact type options |
| 206 |
*/ |
| 207 |
export const getContactTypeOptions = () => [ |
| 208 |
{ value: 'customer service', label: __('Customer Service', 'thinkrank') }, |
| 209 |
{ value: 'technical support', label: __('Technical Support', 'thinkrank') }, |
| 210 |
{ value: 'billing support', label: __('Billing Support', 'thinkrank') }, |
| 211 |
{ value: 'bill payment', label: __('Bill Payment', 'thinkrank') }, |
| 212 |
{ value: 'sales', label: __('Sales', 'thinkrank') }, |
| 213 |
{ value: 'reservations', label: __('Reservations', 'thinkrank') }, |
| 214 |
{ value: 'credit card support', label: __('Credit Card Support', 'thinkrank') }, |
| 215 |
{ value: 'emergency', label: __('Emergency', 'thinkrank') }, |
| 216 |
{ value: 'baggage tracking', label: __('Baggage Tracking', 'thinkrank') }, |
| 217 |
{ value: 'roadside assistance', label: __('Roadside Assistance', 'thinkrank') }, |
| 218 |
{ value: 'package tracking', label: __('Package Tracking', 'thinkrank') } |
| 219 |
]; |
| 220 |
|
| 221 |
/** |
| 222 |
* Normalize settings data types |
| 223 |
* |
| 224 |
* @param {Object} settings Settings object |
| 225 |
* @returns {Object} Normalized settings |
| 226 |
*/ |
| 227 |
export const normalizeSettingsDataTypes = (settings) => { |
| 228 |
const normalized = { ...settings }; |
| 229 |
|
| 230 |
// Convert boolean settings |
| 231 |
getBooleanSettings().forEach(key => { |
| 232 |
if (key in normalized) { |
| 233 |
normalized[key] = Boolean(normalized[key]); |
| 234 |
} |
| 235 |
}); |
| 236 |
|
| 237 |
// Convert array settings |
| 238 |
getArraySettings().forEach(key => { |
| 239 |
if (key in normalized && !Array.isArray(normalized[key])) { |
| 240 |
normalized[key] = []; |
| 241 |
} |
| 242 |
}); |
| 243 |
|
| 244 |
// Convert integer settings |
| 245 |
getIntegerSettings().forEach(key => { |
| 246 |
if (key in normalized) { |
| 247 |
normalized[key] = parseInt(normalized[key], 10) || 0; |
| 248 |
} |
| 249 |
}); |
| 250 |
|
| 251 |
return normalized; |
| 252 |
}; |
| 253 |
|
| 254 |
/** |
| 255 |
* Validate URL format |
| 256 |
* |
| 257 |
* @param {string} url URL to validate |
| 258 |
* @returns {boolean} Whether URL is valid |
| 259 |
*/ |
| 260 |
export const isValidUrl = (url) => { |
| 261 |
try { |
| 262 |
new URL(url); |
| 263 |
return true; |
| 264 |
} catch { |
| 265 |
return false; |
| 266 |
} |
| 267 |
}; |
| 268 |
|