PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 1.31.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v1.31.0
2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.10.0 All 48 releases
thinkrank / includes / seo / class-social-meta-manager.php

class-social-meta-manager.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 1.31.0, at includes/seo/class-social-meta-manager.php

2,670 lines 104.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Social Meta Manager Class
4 *
5 * Universal social media meta tag generation and optimization for all platforms.
6 * Implements 2025 social media SEO best practices with real Open Graph and Twitter Card
7 * specifications, social image optimization, and platform-specific handling.
8 *
9 * @package ThinkRank
10 * @subpackage SEO
11 * @since 1.0.0
12 */
13
14 declare(strict_types=1);
15
16 namespace ThinkRank\SEO;
17
18 // Prevent direct access
19 if (!defined('ABSPATH')) {
20 exit;
21 }
22
23 /**
24 * Social Meta Manager Class
25 *
26 * Generates and validates social media meta tags for all supported platforms.
27 * Provides context-aware social meta generation with image optimization and
28 * platform-specific meta tag handling.
29 *
30 * @since 1.0.0
31 */
32 class Social_Meta_Manager extends Abstract_SEO_Manager {
33
34 /**
35 * Supported social media platforms with their specifications
36 *
37 * @since 1.0.0
38 * @var array
39 */
40 private array $supported_platforms = [
41 'facebook' => [
42 'og_required' => ['og:title', 'og:type', 'og:image', 'og:url'],
43 'og_recommended' => ['og:description', 'og:site_name', 'og:locale'],
44 'og_optional' => ['og:updated_time', 'og:see_also', 'og:video', 'og:audio'],
45 'image_min_width' => 600,
46 'image_min_height' => 315,
47 'image_recommended_ratio' => 1.91,
48 'title_max_length' => 60,
49 'description_max_length' => 160
50 ],
51 'twitter' => [
52 'card_types' => ['summary', 'summary_large_image', 'app', 'player'],
53 'required' => ['twitter:card', 'twitter:title'],
54 'recommended' => ['twitter:description', 'twitter:image', 'twitter:site'],
55 'optional' => ['twitter:creator', 'twitter:player', 'twitter:app:name'],
56 'image_min_width' => 300,
57 'image_min_height' => 157,
58 'image_max_size' => 5242880, // 5MB
59 'title_max_length' => 70,
60 'description_max_length' => 200
61 ],
62 'linkedin' => [
63 'og_required' => ['og:title', 'og:type', 'og:image', 'og:url'],
64 'og_recommended' => ['og:description'],
65 'image_min_width' => 1200,
66 'image_min_height' => 627,
67 'image_recommended_ratio' => 1.91,
68 'title_max_length' => 70,
69 'description_max_length' => 160
70 ],
71 'pinterest' => [
72 'required' => ['og:title', 'og:type', 'og:image', 'og:url'],
73 'recommended' => ['og:description', 'og:site_name'],
74 'image_min_width' => 600,
75 'image_min_height' => 900,
76 'image_recommended_ratio' => 0.67, // 2:3 ratio
77 'title_max_length' => 100,
78 'description_max_length' => 500
79 ],
80 'whatsapp' => [
81 'og_required' => ['og:title', 'og:type', 'og:image', 'og:url'],
82 'og_recommended' => ['og:description'],
83 'image_min_width' => 300,
84 'image_min_height' => 200,
85 'title_max_length' => 65,
86 'description_max_length' => 160
87 ]
88 ];
89
90 /**
91 * Open Graph types with their specifications
92 *
93 * @since 1.0.0
94 * @var array
95 */
96 private array $og_types = [
97 'website' => ['og:title', 'og:type', 'og:image', 'og:url'],
98 'article' => ['og:title', 'og:type', 'og:image', 'og:url', 'article:author', 'article:published_time'],
99 'product' => ['og:title', 'og:type', 'og:image', 'og:url', 'product:price:amount', 'product:price:currency'],
100 'video' => ['og:title', 'og:type', 'og:image', 'og:url', 'og:video', 'video:duration'],
101 'music' => ['og:title', 'og:type', 'og:image', 'og:url', 'music:duration', 'music:album'],
102 'book' => ['og:title', 'og:type', 'og:image', 'og:url', 'book:author', 'book:isbn'],
103 'profile' => ['og:title', 'og:type', 'og:image', 'og:url', 'profile:first_name', 'profile:last_name']
104 ];
105
106 /**
107 * Constructor
108 *
109 * @since 1.0.0
110 */
111 public function __construct() {
112 parent::__construct('social_meta');
113 }
114
115 /**
116 * Generate Open Graph tags for content
117 *
118 * @since 1.0.0
119 *
120 * @param array $data Content data for OG generation
121 * @param string $context Context type ('site', 'post', 'page', etc.)
122 * @param string $platform Target platform ('facebook', 'linkedin', etc.)
123 * @return array Generated Open Graph tags
124 */
125 public function generate_og_tags(array $data, string $context = 'site', string $platform = 'facebook'): array {
126 $platform = strtolower($platform);
127 if (!isset($this->supported_platforms[$platform])) {
128 $platform = 'facebook'; // Default fallback
129 }
130
131 $platform_spec = $this->supported_platforms[$platform];
132 $og_tags = [];
133
134 // Determine OG type based on context
135 $og_type = $this->determine_og_type($context, $data);
136 $og_tags['og:type'] = $og_type;
137
138 // Required OG tags
139 // og:title must render the full resolved Open Graph title. The 60-char
140 // cap in optimize_title_for_platform() is an SEO-title recommendation for
141 // search results and does not apply to the og:title social tag, so use the
142 // resolved title verbatim (falling back to the site name when empty).
143 $og_tags['og:title'] = ($data['title'] ?? '') !== '' ? $data['title'] : get_bloginfo('name');
144 $og_tags['og:url'] = $data['url'] ?? $this->get_current_url();
145
146 // Image handling with optimization
147 if (!empty($data['image'])) {
148 $optimized_image = $this->optimize_image_for_platform($data['image'], $platform);
149 $og_tags['og:image'] = $optimized_image['url'];
150 // Emit og:image:secure_url for https images (parity with the basic
151 // emitter), so crawlers that prefer the secure URL get it.
152 if (!empty($optimized_image['url']) && strpos((string) $optimized_image['url'], 'https://') === 0) {
153 $og_tags['og:image:secure_url'] = $optimized_image['url'];
154 }
155 if (!empty($optimized_image['width'])) {
156 $og_tags['og:image:width'] = $optimized_image['width'];
157 }
158 if (!empty($optimized_image['height'])) {
159 $og_tags['og:image:height'] = $optimized_image['height'];
160 }
161 if (!empty($optimized_image['type'])) {
162 $og_tags['og:image:type'] = $optimized_image['type'];
163 }
164 if (!empty($optimized_image['alt'])) {
165 $og_tags['og:image:alt'] = $optimized_image['alt'];
166 }
167 } else {
168 // Fallback to site default image
169 $default_image = $this->get_default_social_image();
170 if ($default_image) {
171 $og_tags['og:image'] = $default_image;
172 }
173 }
174
175 // Recommended OG tags
176 if (!empty($data['description'])) {
177 $og_tags['og:description'] = $this->optimize_description_for_platform($data['description'], $platform);
178 }
179
180 $og_tags['og:site_name'] = $data['site_name'] ?? get_bloginfo('name');
181 $og_tags['og:locale'] = $data['locale'] ?? $this->get_og_locale();
182
183 // Context-specific OG tags
184 $og_tags = $this->add_context_specific_og_tags($og_tags, $context, $data, $og_type);
185
186 // Platform-specific optimizations
187 $og_tags = $this->apply_platform_specific_optimizations($og_tags, $platform, $data);
188
189 return $og_tags;
190 }
191
192 /**
193 * Generate Twitter Card tags for content
194 *
195 * @since 1.0.0
196 *
197 * @param array $data Content data for Twitter Card generation
198 * @param string $context Context type ('site', 'post', 'page', etc.)
199 * @return array Generated Twitter Card tags
200 */
201 public function generate_twitter_tags(array $data, string $context = 'site'): array {
202 $twitter_tags = [];
203
204 // Determine card type based on content
205 $card_type = $this->determine_twitter_card_type($data, $context);
206 $twitter_tags['twitter:card'] = $card_type;
207
208 // Required tags. Prefer a per-post Twitter-specific title, falling back
209 // to the resolved og:title/SEO title. Render it in full — the length cap
210 // in optimize_title_for_platform() is an SEO-title recommendation for
211 // search results, not a rule for the twitter:title social tag.
212 $twitter_title = ($data['twitter_title'] ?? '') !== '' ? $data['twitter_title'] : ($data['title'] ?? '');
213 $twitter_tags['twitter:title'] = $twitter_title !== '' ? $twitter_title : get_bloginfo('name');
214
215 // Recommended tags
216 if (!empty($data['description'])) {
217 $twitter_tags['twitter:description'] = $this->optimize_description_for_platform($data['description'], 'twitter');
218 }
219
220 // Image handling - prioritize Twitter-specific image
221 $twitter_image = !empty($data['twitter_image']) ? $data['twitter_image'] : $data['image'];
222 if (!empty($twitter_image)) {
223 $optimized_image = $this->optimize_image_for_platform($twitter_image, 'twitter');
224 $twitter_tags['twitter:image'] = $optimized_image['url'];
225 if (!empty($optimized_image['alt'])) {
226 $twitter_tags['twitter:image:alt'] = $optimized_image['alt'];
227 }
228 }
229
230 // Site and creator information
231 $twitter_site = $this->get_twitter_site_handle();
232 if ($twitter_site) {
233 $twitter_tags['twitter:site'] = $twitter_site;
234 }
235
236 $twitter_creator = $this->get_twitter_creator_handle();
237 if ($twitter_creator) {
238 $twitter_tags['twitter:creator'] = $twitter_creator;
239 } elseif (!empty($data['author']['twitter'])) {
240 // Fallback to author data if available
241 $twitter_tags['twitter:creator'] = $data['author']['twitter'];
242 }
243
244 // Card-specific tags
245 $twitter_tags = $this->add_twitter_card_specific_tags($twitter_tags, $card_type, $data, $context);
246
247 return $twitter_tags;
248 }
249
250 /**
251 * Optimize social image for platform requirements
252 *
253 * @since 1.0.0
254 *
255 * @param string $image_url Image URL to optimize
256 * @param string $platform Target platform
257 * @return array Optimized image data
258 */
259 public function optimize_social_image(string $image_url, string $platform): array {
260 return $this->optimize_image_for_platform($image_url, $platform);
261 }
262
263 /**
264 * Generate social media preview data
265 *
266 * @since 1.0.0
267 *
268 * @param array $data Content data
269 * @param string $platform Target platform
270 * @return array Preview data for the platform
271 */
272 public function preview_social_post(array $data, string $platform): array {
273 $preview = [
274 'platform' => $platform,
275 'valid' => false,
276 'preview_url' => '',
277 'title' => '',
278 'description' => '',
279 'image' => '',
280 'warnings' => [],
281 'suggestions' => []
282 ];
283
284 switch ($platform) {
285 case 'facebook':
286 case 'linkedin':
287 $og_tags = $this->generate_og_tags($data, 'post', $platform);
288 $preview = $this->generate_og_preview($og_tags, $platform, $preview);
289 break;
290 case 'twitter':
291 $twitter_tags = $this->generate_twitter_tags($data, 'post');
292 $preview = $this->generate_twitter_preview($twitter_tags, $preview);
293 break;
294 case 'pinterest':
295 $og_tags = $this->generate_og_tags($data, 'post', $platform);
296 $preview = $this->generate_pinterest_preview($og_tags, $preview);
297 break;
298 default:
299 $preview['warnings'][] = 'Unsupported platform for preview generation';
300 }
301
302 return $preview;
303 }
304
305 /**
306 * Validate SEO settings (implements interface)
307 *
308 * @since 1.0.0
309 *
310 * @param array $settings Settings array to validate
311 * @param string $context Optional context for focused validation
312 * @return array Validation results
313 */
314 public function validate_settings(array $settings, string $context = 'all'): array {
315 $validation = [
316 'valid' => true,
317 'errors' => [],
318 'warnings' => [],
319 'suggestions' => [],
320 'score' => 100
321 ];
322
323 // Add detailed field validation breakdown
324 $field_details = $this->get_detailed_field_validation($settings, $context);
325 $validation['field_details'] = $field_details;
326
327 // Convert field details to validation format
328 foreach ($field_details as $field) {
329 switch ($field['status']) {
330 case 'error':
331 $validation['errors'][] = $field['label'];
332 $validation['valid'] = false;
333 $validation['score'] -= 15;
334 break;
335 case 'warning':
336 $validation['warnings'][] = $field['label'];
337 $validation['score'] -= 8;
338 break;
339 case 'suggestion':
340 $validation['suggestions'][] = $field['label'];
341 $validation['score'] -= 3;
342 break;
343 }
344 }
345
346 // Ensure score doesn't go below 0
347 $validation['score'] = max(0, $validation['score']);
348
349
350 // Validate general enabled setting (Social Media tab format)
351 if (isset($settings['enabled'])) {
352 // Convert string booleans to actual booleans
353 if (is_string($settings['enabled'])) {
354 $settings['enabled'] = filter_var($settings['enabled'], FILTER_VALIDATE_BOOLEAN);
355 }
356 if (!is_bool($settings['enabled'])) {
357 $validation['errors'][] = 'enabled must be a boolean value';
358 $validation['valid'] = false;
359 }
360 }
361
362 // Validate Open Graph settings (Social Media tab format)
363 if (isset($settings['enable_open_graph'])) {
364 // Convert string booleans to actual booleans
365 if (is_string($settings['enable_open_graph'])) {
366 $settings['enable_open_graph'] = filter_var($settings['enable_open_graph'], FILTER_VALIDATE_BOOLEAN);
367 }
368 if (!is_bool($settings['enable_open_graph'])) {
369 $validation['errors'][] = 'enable_open_graph must be a boolean value';
370 $validation['valid'] = false;
371 }
372 }
373
374 // Validate Twitter Cards settings (Social Media tab format)
375 if (isset($settings['enable_twitter_cards'])) {
376 // Convert string booleans to actual booleans
377 if (is_string($settings['enable_twitter_cards'])) {
378 $settings['enable_twitter_cards'] = filter_var($settings['enable_twitter_cards'], FILTER_VALIDATE_BOOLEAN);
379 }
380 if (!is_bool($settings['enable_twitter_cards'])) {
381 $validation['errors'][] = 'enable_twitter_cards must be a boolean value';
382 $validation['valid'] = false;
383 }
384 }
385
386 // Validate Twitter settings (only when relevant)
387 if ($context === 'all' || $context === 'twitter' || $context === 'twitter-cards') {
388 // Validate Twitter username format
389 if (isset($settings['twitter_username']) && !empty($settings['twitter_username'])) {
390 $username = trim($settings['twitter_username']);
391
392 // Remove @ if present (we store without @, add @ in output)
393 $clean_username = ltrim($username, '@');
394
395 if (empty($clean_username)) {
396 $validation['errors'][] = 'twitter_username cannot be empty';
397 $validation['valid'] = false;
398 } elseif (strlen($clean_username) > 15) {
399 $validation['errors'][] = 'twitter_username must be 15 characters or less';
400 $validation['valid'] = false;
401 } elseif (!preg_match('/^[A-Za-z0-9_]+$/', $clean_username)) {
402 $validation['errors'][] = 'twitter_username can only contain letters, numbers, and underscores';
403 $validation['valid'] = false;
404 }
405
406 // Update the setting to store without @ for consistency
407 $settings['twitter_username'] = $clean_username;
408 }
409
410 // Validate Twitter creator format
411 if (isset($settings['twitter_creator']) && !empty($settings['twitter_creator'])) {
412 $creator = trim($settings['twitter_creator']);
413
414 // Remove @ if present (we store without @, add @ in output)
415 $clean_creator = ltrim($creator, '@');
416
417 if (empty($clean_creator)) {
418 $validation['errors'][] = 'twitter_creator cannot be empty';
419 $validation['valid'] = false;
420 } elseif (strlen($clean_creator) > 15) {
421 $validation['errors'][] = 'twitter_creator must be 15 characters or less';
422 $validation['valid'] = false;
423 } elseif (!preg_match('/^[A-Za-z0-9_]+$/', $clean_creator)) {
424 $validation['errors'][] = 'twitter_creator can only contain letters, numbers, and underscores';
425 $validation['valid'] = false;
426 }
427
428 // Update the setting to store without @ for consistency
429 $settings['twitter_creator'] = $clean_creator;
430 }
431
432 // Validate Twitter card type
433 if (isset($settings['twitter_card_type'])) {
434 $valid_types = ['summary', 'summary_large_image', 'app', 'player'];
435 if (!in_array($settings['twitter_card_type'], $valid_types, true)) {
436 $validation['errors'][] = 'twitter_card_type must be one of: ' . implode(', ', $valid_types);
437 $validation['valid'] = false;
438 }
439 }
440 }
441
442 // Validate OG type
443 if (isset($settings['og_type'])) {
444 $valid_types = ['website', 'article', 'book', 'profile', 'music.song', 'music.album', 'video.movie', 'video.episode'];
445 if (!in_array($settings['og_type'], $valid_types, true)) {
446 $validation['warnings'][] = 'og_type should be one of the standard Open Graph types for best compatibility';
447 }
448 }
449
450 // Validate image dimensions
451 if (isset($settings['og_image_width']) && (!is_numeric($settings['og_image_width']) || $settings['og_image_width'] < 200)) {
452 $validation['warnings'][] = 'og_image_width should be at least 200 pixels for optimal social sharing';
453 }
454
455 if (isset($settings['og_image_height']) && (!is_numeric($settings['og_image_height']) || $settings['og_image_height'] < 200)) {
456 $validation['warnings'][] = 'og_image_height should be at least 200 pixels for optimal social sharing';
457 }
458
459 // Validate description length
460 if (isset($settings['max_description_length']) && (!is_numeric($settings['max_description_length']) || $settings['max_description_length'] < 50 || $settings['max_description_length'] > 300)) {
461 $validation['warnings'][] = 'max_description_length should be between 50 and 300 characters';
462 }
463
464 // Legacy validation for backward compatibility (only when legacy fields are actually present)
465 if (isset($settings['og_enabled'])) {
466 // Convert string booleans to actual booleans
467 if (is_string($settings['og_enabled'])) {
468 $settings['og_enabled'] = filter_var($settings['og_enabled'], FILTER_VALIDATE_BOOLEAN);
469 }
470 if (!is_bool($settings['og_enabled'])) {
471 $validation['errors'][] = 'og_enabled must be a boolean value';
472 $validation['valid'] = false;
473 }
474 }
475
476 if (isset($settings['twitter_enabled'])) {
477 // Convert string booleans to actual booleans
478 if (is_string($settings['twitter_enabled'])) {
479 $settings['twitter_enabled'] = filter_var($settings['twitter_enabled'], FILTER_VALIDATE_BOOLEAN);
480 }
481 if (!is_bool($settings['twitter_enabled'])) {
482 $validation['errors'][] = 'twitter_enabled must be a boolean value';
483 $validation['valid'] = false;
484 }
485 }
486
487 // Validate Twitter card type
488 if (isset($settings['twitter_card_type'])) {
489 $valid_card_types = $this->supported_platforms['twitter']['card_types'];
490 if (!in_array($settings['twitter_card_type'], $valid_card_types, true)) {
491 $validation['errors'][] = 'Invalid Twitter card type. Must be one of: ' . implode(', ', $valid_card_types);
492 $validation['valid'] = false;
493 }
494 }
495
496 // Validate default image
497 if (isset($settings['default_image']) && !empty($settings['default_image'])) {
498 if (!filter_var($settings['default_image'], FILTER_VALIDATE_URL)) {
499 $validation['errors'][] = 'default_image must be a valid URL';
500 $validation['valid'] = false;
501 } else {
502 // Check image dimensions and format
503 $image_validation = $this->validate_social_image($settings['default_image']);
504 if (!$image_validation['valid']) {
505 $validation['warnings'] = array_merge($validation['warnings'], $image_validation['warnings']);
506 }
507 }
508 }
509
510 // Validate Twitter site handle
511 if (isset($settings['twitter_site']) && !empty($settings['twitter_site'])) {
512 if (!preg_match('/^@[a-zA-Z0-9_]{1,15}$/', $settings['twitter_site'])) {
513 $validation['errors'][] = 'twitter_site must be a valid Twitter handle (e.g., @username)';
514 $validation['valid'] = false;
515 }
516 }
517
518 // Validate custom OG tags
519 if (isset($settings['custom_og_tags']) && !empty($settings['custom_og_tags'])) {
520 if (!is_array($settings['custom_og_tags'])) {
521 $validation['errors'][] = 'custom_og_tags must be an array';
522 $validation['valid'] = false;
523 } else {
524 foreach ($settings['custom_og_tags'] as $property => $content) {
525 if (!is_string($property) || !is_string($content)) {
526 $validation['errors'][] = 'Custom OG tags must have string property names and content';
527 $validation['valid'] = false;
528 break;
529 }
530 }
531 }
532 }
533
534 // Validate platform verification codes / IDs (Instagram, TikTok, YouTube,
535 // WhatsApp, Facebook App ID, Pinterest) against the shared format rules.
536 // Single source of truth — also enforced on the Social Platforms REST save
537 // path via self::validate_platform_field(). See get_platform_field_validation_rules().
538 foreach (self::get_platform_field_validation_rules() as $field_key => $rule) {
539 if (isset($settings[$field_key]) && $settings[$field_key] !== '') {
540 $error = self::validate_platform_field($field_key, $settings[$field_key]);
541 if ($error !== null) {
542 $validation['errors'][] = $error;
543 $validation['valid'] = false;
544 }
545 }
546 }
547
548 return $validation;
549 }
550
551 /**
552 * Format-validation rules for social platform verification codes / IDs.
553 *
554 * Single source of truth for the per-field format constraints, shared by
555 * validate_settings() and the Social Platforms REST endpoint
556 * (ThinkRank\API\Social_Platforms_Endpoint) so a value that is accepted on
557 * one path is accepted on the other. These mirror the `pattern` entries in
558 * get_settings_schema(); sanitization strips unsafe characters but does not
559 * enforce shape, so these rules back it with real validation.
560 *
561 * @since 1.14.0
562 *
563 * @return array<string, array{pattern: string, message: string}> Map of field key => rule.
564 */
565 public static function get_platform_field_validation_rules(): array {
566 return [
567 'facebook_app_id' => [
568 'pattern' => '/^[0-9]+$/',
569 'message' => 'Facebook App ID must contain digits only.',
570 ],
571 'pinterest_site_verification' => [
572 'pattern' => '/^[a-f0-9]{32}$/',
573 'message' => 'Pinterest site verification must be a 32-character hexadecimal string.',
574 ],
575 'instagram_verification' => [
576 'pattern' => '/^[a-zA-Z0-9_-]{20,}$/',
577 'message' => 'Instagram verification must be at least 20 characters (letters, numbers, underscore, dash).',
578 ],
579 'tiktok_verification' => [
580 'pattern' => '/^[a-zA-Z0-9_-]{20,}$/',
581 'message' => 'TikTok verification must be at least 20 characters (letters, numbers, underscore, dash).',
582 ],
583 'youtube_channel_id' => [
584 'pattern' => '/^UC[a-zA-Z0-9_-]{22}$/',
585 'message' => 'YouTube channel ID must start with "UC" followed by 22 characters (letters, numbers, underscore, dash).',
586 ],
587 'whatsapp_business_id' => [
588 'pattern' => '/^[0-9]{10,15}$/',
589 'message' => 'WhatsApp Business ID must be 10-15 digits.',
590 ],
591 ];
592 }
593
594 /**
595 * Validate a single social platform field value against its shared format rule.
596 *
597 * Returns null for fields with no format rule (e.g. facebook_admins) and for
598 * empty values, so callers can validate an arbitrary settings map and only
599 * act on genuine format violations.
600 *
601 * @since 1.14.0
602 *
603 * @param string $key Field key.
604 * @param mixed $value Field value.
605 * @return string|null Error message when the value violates the format, null otherwise.
606 */
607 public static function validate_platform_field(string $key, $value): ?string {
608 $rules = self::get_platform_field_validation_rules();
609
610 if (!isset($rules[$key]) || $value === '' || $value === null) {
611 return null;
612 }
613
614 if (!preg_match($rules[$key]['pattern'], (string) $value)) {
615 return $rules[$key]['message'];
616 }
617
618 return null;
619 }
620
621 /**
622 * Get detailed field validation breakdown
623 *
624 * @since 1.0.0
625 *
626 * @param array $settings Settings array to validate
627 * @param string $context Context for specific validation
628 * @return array Detailed field validation results
629 */
630 private function get_detailed_field_validation(array $settings, string $context = 'all'): array {
631 $field_details = [];
632
633 // Return tab-specific validation based on context
634 switch ($context) {
635 case 'open-graph':
636 return $this->validate_open_graph_fields($settings);
637 case 'twitter-cards':
638 return $this->validate_twitter_fields($settings);
639 default:
640 // For 'all' or unknown context, return only Social Media fields (Open Graph + Twitter)
641 $field_details = array_merge($field_details, $this->validate_open_graph_fields($settings));
642 $field_details = array_merge($field_details, $this->validate_twitter_fields($settings));
643 return $field_details;
644 }
645 }
646
647 /**
648 * Validate Open Graph fields
649 *
650 * @since 1.0.0
651 *
652 * @param array $settings Settings array to validate
653 * @return array Open Graph field validation results
654 */
655 private function validate_open_graph_fields(array $settings): array {
656 $field_details = [];
657
658 // Open Graph Enabled
659 if (!empty($settings['enable_open_graph'])) {
660 $field_details[] = [
661 'field' => 'enable_open_graph',
662 'label' => 'Open Graph is enabled for social media sharing.',
663 'status' => 'valid',
664 'icon' => ''
665 ];
666
667 // Site Name validation
668 if (!empty($settings['og_site_name'])) {
669 if (strlen($settings['og_site_name']) <= 60) {
670 $field_details[] = [
671 'field' => 'og_site_name',
672 'label' => 'Site name is properly configured for Open Graph.',
673 'status' => 'valid',
674 'icon' => ''
675 ];
676 } else {
677 $field_details[] = [
678 'field' => 'og_site_name',
679 'label' => 'Site name is longer than 60 characters, may be truncated.',
680 'status' => 'warning',
681 'icon' => ''
682 ];
683 }
684 } else {
685 $field_details[] = [
686 'field' => 'og_site_name',
687 'label' => 'Site name is required for Open Graph.',
688 'status' => 'error',
689 'icon' => ''
690 ];
691 }
692
693 // Description validation
694 if (!empty($settings['og_description'])) {
695 $length = strlen($settings['og_description']);
696 if ($length >= 120 && $length <= 160) {
697 $field_details[] = [
698 'field' => 'og_description',
699 'label' => 'Description is properly configured for social sharing.',
700 'status' => 'valid',
701 'icon' => ''
702 ];
703 } else {
704 $field_details[] = [
705 'field' => 'og_description',
706 'label' => 'Description length could be optimized (120-160 characters recommended).',
707 'status' => 'warning',
708 'icon' => ''
709 ];
710 }
711 } else {
712 $field_details[] = [
713 'field' => 'og_description',
714 'label' => 'Description is recommended for better social sharing.',
715 'status' => 'warning',
716 'icon' => ''
717 ];
718 }
719
720 // Content Type validation
721 if (!empty($settings['og_type'])) {
722 $field_details[] = [
723 'field' => 'og_type',
724 'label' => 'Content type is configured for Open Graph.',
725 'status' => 'valid',
726 'icon' => ''
727 ];
728 } else {
729 $field_details[] = [
730 'field' => 'og_type',
731 'label' => 'Content type should be specified.',
732 'status' => 'suggestion',
733 'icon' => ''
734 ];
735 }
736
737 // Default Image validation
738 if (!empty($settings['default_og_image'])) {
739 if (filter_var($settings['default_og_image'], FILTER_VALIDATE_URL)) {
740 $field_details[] = [
741 'field' => 'default_og_image',
742 'label' => 'Default Open Graph image is configured.',
743 'status' => 'valid',
744 'icon' => ''
745 ];
746 } else {
747 $field_details[] = [
748 'field' => 'default_og_image',
749 'label' => 'Default Open Graph image URL appears invalid.',
750 'status' => 'warning',
751 'icon' => ''
752 ];
753 }
754 } else {
755 $field_details[] = [
756 'field' => 'default_og_image',
757 'label' => 'Default image is recommended for social sharing.',
758 'status' => 'suggestion',
759 'icon' => ''
760 ];
761 }
762
763 } else {
764 $field_details[] = [
765 'field' => 'enable_open_graph',
766 'label' => 'Open Graph is disabled. Enable for better social media sharing.',
767 'status' => 'suggestion',
768 'icon' => ''
769 ];
770 }
771
772 return $field_details;
773 }
774
775 /**
776 * Validate Twitter fields
777 *
778 * @since 1.0.0
779 *
780 * @param array $settings Settings array to validate
781 * @return array Twitter field validation results
782 */
783 private function validate_twitter_fields(array $settings): array {
784 $field_details = [];
785
786 // Twitter Cards Enabled
787 if (!empty($settings['enable_twitter_cards'])) {
788 $field_details[] = [
789 'field' => 'enable_twitter_cards',
790 'label' => 'Twitter Cards are enabled for Twitter sharing.',
791 'status' => 'valid',
792 'icon' => ''
793 ];
794
795 // Twitter Username validation
796 if (!empty($settings['twitter_username'])) {
797 $username = trim($settings['twitter_username']);
798 $clean_username = ltrim($username, '@');
799
800 if (strlen($clean_username) <= 15 && preg_match('/^[A-Za-z0-9_]+$/', $clean_username)) {
801 $field_details[] = [
802 'field' => 'twitter_username',
803 'label' => 'Twitter username is properly formatted.',
804 'status' => 'valid',
805 'icon' => ''
806 ];
807 } else {
808 $field_details[] = [
809 'field' => 'twitter_username',
810 'label' => 'Twitter username format is invalid (max 15 chars, letters/numbers/underscore only).',
811 'status' => 'error',
812 'icon' => ''
813 ];
814 }
815 } else {
816 $field_details[] = [
817 'field' => 'twitter_username',
818 'label' => 'Twitter username recommended for better attribution.',
819 'status' => 'suggestion',
820 'icon' => ''
821 ];
822 }
823
824 // Twitter Creator validation
825 if (!empty($settings['twitter_creator'])) {
826 $creator = trim($settings['twitter_creator']);
827 $clean_creator = ltrim($creator, '@');
828
829 if (strlen($clean_creator) <= 15 && preg_match('/^[A-Za-z0-9_]+$/', $clean_creator)) {
830 $field_details[] = [
831 'field' => 'twitter_creator',
832 'label' => 'Twitter creator is properly formatted.',
833 'status' => 'valid',
834 'icon' => ''
835 ];
836 } else {
837 $field_details[] = [
838 'field' => 'twitter_creator',
839 'label' => 'Twitter creator format is invalid (max 15 chars, letters/numbers/underscore only).',
840 'status' => 'error',
841 'icon' => ''
842 ];
843 }
844 } else {
845 $field_details[] = [
846 'field' => 'twitter_creator',
847 'label' => 'Twitter creator recommended for content attribution.',
848 'status' => 'suggestion',
849 'icon' => ''
850 ];
851 }
852
853 // Card Type validation
854 $valid_card_types = ['summary', 'summary_large_image', 'app', 'player'];
855 if (!empty($settings['twitter_card_type']) && in_array($settings['twitter_card_type'], $valid_card_types, true)) {
856 $field_details[] = [
857 'field' => 'twitter_card_type',
858 'label' => 'Twitter Card type is properly configured.',
859 'status' => 'valid',
860 'icon' => ''
861 ];
862 } else {
863 $field_details[] = [
864 'field' => 'twitter_card_type',
865 'label' => 'Valid Twitter Card type is required.',
866 'status' => 'error',
867 'icon' => ''
868 ];
869 }
870
871 // Default Twitter Image validation
872 if (!empty($settings['default_twitter_image'])) {
873 if (filter_var($settings['default_twitter_image'], FILTER_VALIDATE_URL)) {
874 $field_details[] = [
875 'field' => 'default_twitter_image',
876 'label' => 'Default Twitter Card image is configured.',
877 'status' => 'valid',
878 'icon' => ''
879 ];
880 } else {
881 $field_details[] = [
882 'field' => 'default_twitter_image',
883 'label' => 'Default Twitter Card image URL appears invalid.',
884 'status' => 'warning',
885 'icon' => ''
886 ];
887 }
888 } else {
889 $field_details[] = [
890 'field' => 'default_twitter_image',
891 'label' => 'Default image recommended for Twitter sharing.',
892 'status' => 'suggestion',
893 'icon' => ''
894 ];
895 }
896
897 } else {
898 $field_details[] = [
899 'field' => 'enable_twitter_cards',
900 'label' => 'Twitter Cards are disabled. Enable for better Twitter sharing.',
901 'status' => 'suggestion',
902 'icon' => ''
903 ];
904 }
905
906 return $field_details;
907 }
908
909
910 /**
911 * Get output data for frontend rendering (implements interface)
912 *
913 * @since 1.0.0
914 *
915 * @param string $context_type The context type
916 * @param int|null $context_id Optional. Context ID
917 * @param string|null $fallback_title Optional. Effective SEO title to
918 * use when no per-post Open Graph
919 * title override is set, so
920 * rendered output mirrors the
921 * document title and the Social
922 * metabox preview.
923 * @param string|null $fallback_description Optional. Effective meta
924 * description, used as the
925 * Open Graph description fallback
926 * for the same parity reason.
927 * @return array Output data ready for frontend rendering
928 */
929 public function get_output_data(string $context_type, ?int $context_id, ?string $fallback_title = null, ?string $fallback_description = null): array {
930 // Memoize per request: the OG, Twitter and platform wp_head callbacks
931 // each call this with the same arguments, so the extraction work +
932 // Site_Identity_Manager instantiation would otherwise run three times.
933 $cache_key = $context_type . ':' . ($context_id ?? 0) . ':' . md5((string) $fallback_title . '|' . (string) $fallback_description);
934 if (isset($this->output_data_cache[$cache_key])) {
935 return $this->output_data_cache[$cache_key];
936 }
937
938 $settings = $this->get_settings($context_type, $context_id);
939 $output = [
940 'og_tags' => [],
941 'twitter_tags' => [],
942 'meta_tags' => [],
943 'platform_tags' => [],
944 // Per-feature flags so each emitter can honor its own toggle. The
945 // aggregate `enabled` (true if either is on) is kept for callers
946 // that only care whether social output ran at all.
947 'og_enabled' => false,
948 'twitter_enabled' => false,
949 'enabled' => false,
950 ];
951
952 // Check if individual social features are enabled
953 $og_enabled = !empty($settings['enable_open_graph'] ?? $settings['og_enabled'] ?? false);
954 $twitter_enabled = !empty($settings['enable_twitter_cards'] ?? $settings['twitter_enabled'] ?? false);
955 $output['og_enabled'] = $og_enabled;
956 $output['twitter_enabled'] = $twitter_enabled;
957
958 if (!$og_enabled && !$twitter_enabled) {
959 $this->output_data_cache[$cache_key] = $output;
960 return $output;
961 }
962
963 $output['enabled'] = true;
964
965 // Extract content data
966 $content_data = $this->extract_social_content_data($context_type, $context_id, $settings, $fallback_title, $fallback_description);
967
968 // Generate Open Graph tags if enabled
969 if ($og_enabled) {
970 $output['og_tags'] = $this->generate_og_tags($content_data, $context_type);
971
972 // Add custom OG tags
973 if (!empty($settings['custom_og_tags'])) {
974 $output['og_tags'] = array_merge($output['og_tags'], $settings['custom_og_tags']);
975 }
976 }
977
978 // Generate Twitter Card tags if enabled
979 if ($twitter_enabled) {
980 $output['twitter_tags'] = $this->generate_twitter_tags($content_data, $context_type);
981 }
982
983 // Generate platform-specific meta tags
984 $output['platform_tags'] = $this->generate_platform_meta_tags($settings);
985
986 // Convert to meta tag format for HTML output
987 $output['meta_tags'] = $this->convert_to_meta_tags($output['og_tags'], $output['twitter_tags'], $output['platform_tags']);
988
989 $this->output_data_cache[$cache_key] = $output;
990 return $output;
991 }
992
993 /**
994 * Request-scoped memoization of get_output_data() keyed by context + title/desc.
995 *
996 * @var array<string, array>
997 */
998 private array $output_data_cache = [];
999
1000 /**
1001 * Site-wide default image keys inherited by every other context.
1002 *
1003 * @since 1.24.1
1004 * @var string[]
1005 */
1006 private const INHERITED_SITE_KEYS = [
1007 'default_og_image',
1008 'default_twitter_image',
1009 'default_image',
1010 ];
1011
1012 /**
1013 * Get settings for a context, inheriting the site-wide default images
1014 *
1015 * Two corrections over the generic lookup:
1016 *
1017 * 1. Site-wide settings are always stored with context_id 0, but the
1018 * frontend maps a homepage request to the `site` context while still
1019 * passing the queried object ID (non-zero on a static front page). That
1020 * looked up `site`/<page ID>, matched no row and silently fell back to
1021 * the schema defaults, so a configured default OG image never rendered
1022 * on a static front page. Normalize the ID away for `site`.
1023 * 2. `default_og_image` / `default_twitter_image` only exist in the site
1024 * context, so post/page and archive contexts had nothing to fall back to
1025 * when a post had no featured image — og:image dropped to the site logo
1026 * or vanished entirely. Inherit those keys when the context has no value
1027 * of its own.
1028 *
1029 * @since 1.24.1
1030 *
1031 * @param string $context_type The context type
1032 * @param int|null $context_id Optional. Context ID
1033 * @return array Settings with site-wide image defaults applied
1034 */
1035 public function get_settings(string $context_type, ?int $context_id = null): array {
1036 if ($context_type === 'site') {
1037 $context_id = null;
1038 }
1039
1040 $settings = parent::get_settings($context_type, $context_id);
1041
1042 if ($context_type === 'site') {
1043 return $settings;
1044 }
1045
1046 $site_settings = parent::get_settings('site', null);
1047 foreach (self::INHERITED_SITE_KEYS as $key) {
1048 if (empty($settings[$key]) && !empty($site_settings[$key])) {
1049 $settings[$key] = $site_settings[$key];
1050 }
1051 }
1052
1053 return $settings;
1054 }
1055
1056 /**
1057 * Get default settings for a context type (implements interface)
1058 *
1059 * @since 1.0.0
1060 *
1061 * @param string $context_type The context type to get defaults for
1062 * @return array Default settings array
1063 */
1064 public function get_default_settings(string $context_type): array {
1065 // Get WordPress site defaults
1066 $site_name = get_bloginfo('name');
1067 $site_description = get_bloginfo('description');
1068
1069 $defaults = [
1070 // Open Graph settings
1071 'enable_open_graph' => true,
1072 'og_site_name' => $site_name,
1073 'og_description' => $site_description,
1074 'og_type' => 'website',
1075 // Empty by design: og:locale is resolved from the site locale via
1076 // get_og_locale(), which is where the thinkrank_og_locale filter (and
1077 // therefore the WPML/Polylang/TranslatePress integration) applies. A
1078 // hardcoded default was merged into every settings read, so the
1079 // resolver was unreachable and every install advertised en_US.
1080 'og_locale' => '',
1081 'default_og_image' => '',
1082 'og_image_width' => 1200,
1083 'og_image_height' => 630,
1084
1085 // Twitter Cards settings
1086 'enable_twitter_cards' => true,
1087 'twitter_username' => '',
1088 'twitter_creator' => '',
1089 'twitter_card_type' => 'summary_large_image',
1090 'default_twitter_image' => '',
1091
1092 // Facebook settings
1093 'facebook_app_id' => '',
1094 'facebook_admins' => '',
1095
1096 // LinkedIn settings
1097 'enable_linkedin' => false,
1098
1099 // Pinterest settings
1100 'enable_pinterest' => false,
1101 'pinterest_site_verification' => '',
1102
1103 // Instagram settings
1104 'enable_instagram' => false,
1105 'instagram_verification' => '',
1106
1107 // TikTok settings
1108 'enable_tiktok' => false,
1109 'tiktok_verification' => '',
1110
1111 // YouTube settings
1112 'enable_youtube' => false,
1113 'youtube_channel_id' => '',
1114
1115 // WhatsApp Business settings
1116 'enable_whatsapp' => false,
1117 'whatsapp_business_id' => '',
1118
1119 // Advanced settings
1120 'auto_generate_descriptions' => true,
1121 'fallback_to_excerpt' => true,
1122 'strip_html_tags' => true,
1123 'max_description_length' => 160,
1124
1125 // Legacy format for backward compatibility (only when needed)
1126 'custom_og_tags' => [],
1127 'image_optimization' => true,
1128 'auto_generate' => true
1129 ];
1130
1131 // Context-specific defaults
1132 switch ($context_type) {
1133 case 'site':
1134 $defaults['og_type'] = 'website';
1135 break;
1136 case 'post':
1137 $defaults['og_type'] = 'article';
1138 break;
1139 case 'page':
1140 $defaults['og_type'] = 'website';
1141 break;
1142 case 'product':
1143 $defaults['og_type'] = 'product';
1144 break;
1145 }
1146
1147 return $defaults;
1148 }
1149
1150 /**
1151 * Get settings schema definition (implements interface)
1152 *
1153 * @since 1.0.0
1154 *
1155 * @param string $context_type The context type to get schema for
1156 * @return array Settings schema definition
1157 */
1158 public function get_settings_schema(string $context_type): array {
1159 return [
1160 'enable_open_graph' => [
1161 'type' => 'boolean',
1162 'title' => 'Enable Open Graph',
1163 'description' => 'Generate Open Graph meta tags for social media sharing',
1164 'default' => true
1165 ],
1166 'og_site_name' => [
1167 'type' => 'string',
1168 'title' => 'Site Name',
1169 'description' => 'The name of your website for Open Graph',
1170 'maxLength' => 60,
1171 'default' => get_bloginfo('name')
1172 ],
1173 'og_description' => [
1174 'type' => 'string',
1175 'title' => 'Site Description',
1176 'description' => 'Default description for Open Graph tags',
1177 'maxLength' => 160,
1178 'default' => get_bloginfo('description')
1179 ],
1180 'og_locale' => [
1181 'type' => 'string',
1182 'title' => 'Locale',
1183 'description' => 'Optional override for og:locale. Leave empty to follow the site language.',
1184 'default' => ''
1185 ],
1186 'default_og_image' => [
1187 'type' => 'string',
1188 'title' => 'Default Open Graph Image',
1189 'description' => 'Default image URL for Open Graph tags',
1190 'format' => 'uri',
1191 'default' => ''
1192 ],
1193 'og_image_width' => [
1194 'type' => 'integer',
1195 'title' => 'Image Width',
1196 'description' => 'Default width for Open Graph images',
1197 'minimum' => 200,
1198 'default' => 1200
1199 ],
1200 'og_image_height' => [
1201 'type' => 'integer',
1202 'title' => 'Image Height',
1203 'description' => 'Default height for Open Graph images',
1204 'minimum' => 200,
1205 'default' => 630
1206 ],
1207 'enable_twitter_cards' => [
1208 'type' => 'boolean',
1209 'title' => 'Enable Twitter Cards',
1210 'description' => 'Generate Twitter Card meta tags for Twitter sharing',
1211 'default' => true
1212 ],
1213 'twitter_username' => [
1214 'type' => 'string',
1215 'title' => 'Twitter Username',
1216 'description' => 'Twitter username for the site (without @, e.g., username)',
1217 'pattern' => '^[a-zA-Z0-9_]{1,15}$',
1218 'default' => ''
1219 ],
1220 'twitter_creator' => [
1221 'type' => 'string',
1222 'title' => 'Twitter Creator',
1223 'description' => 'Content creator Twitter username (without @, e.g., creator_username)',
1224 'pattern' => '^[a-zA-Z0-9_]{1,15}$',
1225 'default' => ''
1226 ],
1227 'twitter_card_type' => [
1228 'type' => 'string',
1229 'title' => 'Twitter Card Type',
1230 'description' => 'The type of Twitter Card to generate',
1231 'enum' => $this->supported_platforms['twitter']['card_types'],
1232 'default' => 'summary_large_image'
1233 ],
1234 'og_type' => [
1235 'type' => 'string',
1236 'title' => 'Open Graph Type',
1237 'description' => 'The Open Graph type for this content',
1238 'enum' => array_keys($this->og_types),
1239 'default' => $this->get_default_settings($context_type)['og_type']
1240 ],
1241 'default_image' => [
1242 'type' => 'string',
1243 'title' => 'Default Social Image',
1244 'description' => 'Default image URL for social media sharing',
1245 'format' => 'uri',
1246 'default' => ''
1247 ],
1248 'twitter_site' => [
1249 'type' => 'string',
1250 'title' => 'Twitter Site Handle',
1251 'description' => 'Twitter handle for the site (e.g., @username)',
1252 'pattern' => '^@[a-zA-Z0-9_]{1,15}$',
1253 'default' => ''
1254 ],
1255 'default_twitter_image' => [
1256 'type' => 'string',
1257 'title' => 'Default Twitter Image',
1258 'description' => 'Default image URL for Twitter Cards',
1259 'format' => 'uri',
1260 'default' => ''
1261 ],
1262 'facebook_app_id' => [
1263 'type' => 'string',
1264 'title' => 'Facebook App ID',
1265 'description' => 'Facebook App ID for analytics and insights',
1266 'pattern' => '^[0-9]+$',
1267 'default' => ''
1268 ],
1269 'facebook_admins' => [
1270 'type' => 'string',
1271 'title' => 'Facebook Admins',
1272 'description' => 'Comma-separated list of Facebook admin user IDs',
1273 'default' => ''
1274 ],
1275 'enable_linkedin' => [
1276 'type' => 'boolean',
1277 'title' => 'Enable LinkedIn',
1278 'description' => 'Enable LinkedIn-specific optimizations',
1279 'default' => false
1280 ],
1281 'enable_pinterest' => [
1282 'type' => 'boolean',
1283 'title' => 'Enable Pinterest',
1284 'description' => 'Enable Pinterest-specific optimizations',
1285 'default' => false
1286 ],
1287 'pinterest_site_verification' => [
1288 'type' => 'string',
1289 'title' => 'Pinterest Site Verification',
1290 'description' => 'Pinterest site verification meta tag content',
1291 'pattern' => '^[a-f0-9]{32}$',
1292 'default' => ''
1293 ],
1294 'enable_instagram' => [
1295 'type' => 'boolean',
1296 'title' => 'Enable Instagram',
1297 'description' => 'Enable Instagram-specific optimizations',
1298 'default' => false
1299 ],
1300 'instagram_verification' => [
1301 'type' => 'string',
1302 'title' => 'Instagram Site Verification',
1303 'description' => 'Instagram Business account verification code',
1304 'pattern' => '^[a-zA-Z0-9_-]{20,}$',
1305 'default' => ''
1306 ],
1307 'enable_tiktok' => [
1308 'type' => 'boolean',
1309 'title' => 'Enable TikTok',
1310 'description' => 'Enable TikTok-specific optimizations',
1311 'default' => false
1312 ],
1313 'tiktok_verification' => [
1314 'type' => 'string',
1315 'title' => 'TikTok Site Verification',
1316 'description' => 'TikTok for Business verification code',
1317 'pattern' => '^[a-zA-Z0-9_-]{20,}$',
1318 'default' => ''
1319 ],
1320 'enable_youtube' => [
1321 'type' => 'boolean',
1322 'title' => 'Enable YouTube',
1323 'description' => 'Enable YouTube-specific optimizations',
1324 'default' => false
1325 ],
1326 'youtube_channel_id' => [
1327 'type' => 'string',
1328 'title' => 'YouTube Channel ID',
1329 'description' => 'YouTube channel ID for content attribution',
1330 'pattern' => '^UC[a-zA-Z0-9_-]{22}$',
1331 'default' => ''
1332 ],
1333 'enable_whatsapp' => [
1334 'type' => 'boolean',
1335 'title' => 'Enable WhatsApp Business',
1336 'description' => 'Enable WhatsApp Business optimizations',
1337 'default' => false
1338 ],
1339 'whatsapp_business_id' => [
1340 'type' => 'string',
1341 'title' => 'WhatsApp Business ID',
1342 'description' => 'WhatsApp Business account ID',
1343 'pattern' => '^[0-9]{10,15}$',
1344 'default' => ''
1345 ],
1346 'auto_generate_descriptions' => [
1347 'type' => 'boolean',
1348 'title' => 'Auto-generate Descriptions',
1349 'description' => 'Automatically generate descriptions from content',
1350 'default' => true
1351 ],
1352 'fallback_to_excerpt' => [
1353 'type' => 'boolean',
1354 'title' => 'Fallback to Excerpt',
1355 'description' => 'Use post excerpt as fallback for descriptions',
1356 'default' => true
1357 ],
1358 'strip_html_tags' => [
1359 'type' => 'boolean',
1360 'title' => 'Strip HTML Tags',
1361 'description' => 'Remove HTML tags from generated descriptions',
1362 'default' => true
1363 ],
1364 'max_description_length' => [
1365 'type' => 'integer',
1366 'title' => 'Max Description Length',
1367 'description' => 'Maximum length for generated descriptions',
1368 'minimum' => 50,
1369 'maximum' => 300,
1370 'default' => 160
1371 ],
1372 'custom_og_tags' => [
1373 'type' => 'object',
1374 'title' => 'Custom Open Graph Tags',
1375 'description' => 'Additional custom Open Graph meta tags',
1376 'default' => []
1377 ],
1378 'image_optimization' => [
1379 'type' => 'boolean',
1380 'title' => 'Enable Image Optimization',
1381 'description' => 'Optimize images for social media platforms',
1382 'default' => true
1383 ],
1384 'auto_generate' => [
1385 'type' => 'boolean',
1386 'title' => 'Auto-generate Tags',
1387 'description' => 'Automatically generate social meta tags from content',
1388 'default' => true
1389 ]
1390 ];
1391 }
1392
1393 /**
1394 * Extract content data for social meta generation
1395 *
1396 * @since 1.0.0
1397 *
1398 * @param string $context_type The context type
1399 * @param int|null $context_id Optional. Context ID
1400 * @param array $settings Social meta settings
1401 * @return array Extracted content data
1402 */
1403 private function extract_social_content_data(string $context_type, ?int $context_id, array $settings, ?string $fallback_title = null, ?string $fallback_description = null): array {
1404 $data = [
1405 'title' => '',
1406 'description' => '',
1407 'url' => '',
1408 'image' => '',
1409 'twitter_title' => '', // Separate field for a Twitter-specific title
1410 'twitter_image' => '', // Separate field for Twitter-specific images
1411 'type' => 'website',
1412 'author' => [],
1413 'published_time' => '',
1414 'modified_time' => '',
1415 'site_name' => $settings['og_site_name'] ?? get_bloginfo('name')
1416 ];
1417
1418 if ($context_type === 'site') {
1419 // Site-wide data with Social Media tab settings priority.
1420 //
1421 // og:title priority: an explicitly-configured OG Site Name wins;
1422 // otherwise mirror the effective SEO <title> (what search shows),
1423 // then the blogname. og_site_name is always present because it is
1424 // merged from a default equal to the blogname, so a value matching
1425 // the blogname is treated as "not explicitly overridden" and we fall
1426 // through to the passed effective SEO title. (og:site_name itself is
1427 // set separately from og_site_name and is unaffected.)
1428 $blogname = get_bloginfo('name');
1429 $og_site_name = $settings['og_site_name'] ?? '';
1430 if ($og_site_name !== '' && $og_site_name !== $blogname) {
1431 $data['title'] = $og_site_name;
1432 } elseif ($fallback_title !== null && $fallback_title !== '') {
1433 $data['title'] = $fallback_title;
1434 } else {
1435 $data['title'] = $blogname;
1436 }
1437 $data['description'] = $settings['og_description'] ?? get_bloginfo('description');
1438 // Use home_url('/') so og:url matches the homepage canonical
1439 // (class-seo-manager.php) and the WebSite schema, which both include
1440 // the trailing slash. A bare home_url() would key a different URL in
1441 // social caches than the canonical.
1442 $data['url'] = home_url('/');
1443 $data['type'] = $settings['og_type'] ?? 'website';
1444 // Leaving this null lets generate_og_tags() fall through to
1445 // get_og_locale(), which is what every other context already does.
1446 //
1447 // A stored 'en_US' is deliberately treated as "not set". It was the
1448 // hardcoded default on every install and there has never been a UI
1449 // control for this field, so it cannot represent a deliberate choice
1450 // — it is the old default persisted by an unrelated save of the
1451 // Social Media tab. Honouring it would leave every already-saved
1452 // site broken after this fix. Any other stored value is a genuine
1453 // override and still wins; a site that really wants to force en_US
1454 // can do so through the thinkrank_og_locale filter.
1455 $stored_locale = trim((string) ($settings['og_locale'] ?? ''));
1456 $data['locale'] = ('' !== $stored_locale && 'en_US' !== $stored_locale)
1457 ? $stored_locale
1458 : null;
1459
1460 // Set Open Graph image with proper fallback
1461 $data['image'] = $this->get_og_image_for_context($settings, null);
1462
1463 // Set Twitter image with proper fallback
1464 $data['twitter_image'] = $this->get_twitter_image_for_context($settings, null);
1465 } elseif ($context_id && in_array($context_type, ['post', 'page', 'product'], true)) {
1466 // Post-specific data
1467 $post = get_post($context_id);
1468 if ($post) {
1469 // Per-post Open Graph overrides from the metabox Social tab take
1470 // precedence over the derived title/description/image. These read
1471 // the same meta keys the metabox save handler and the import
1472 // migrator write to, so manual edits and migrated data flow
1473 // through one path. Title/description may hold variable tags
1474 // (e.g. %title%), resolved here to match output_basic_og_tags().
1475 $og_title_override = \ThinkRank\SEO\Pattern_Resolver::resolve_value(
1476 (string) get_post_meta($post->ID, '_thinkrank_og_title', true),
1477 $post->ID
1478 );
1479 $og_description_override = \ThinkRank\SEO\Pattern_Resolver::resolve_value(
1480 (string) get_post_meta($post->ID, '_thinkrank_og_description', true),
1481 $post->ID
1482 );
1483 $og_image_override = get_post_meta($post->ID, '_thinkrank_og_image', true);
1484
1485 // Per-post Twitter-specific title override (metabox Social tab).
1486 // Twitter Cards fall back to the og:title when this is empty, so
1487 // only capture it here; the fallback is applied in
1488 // generate_twitter_tags().
1489 $data['twitter_title'] = \ThinkRank\SEO\Pattern_Resolver::resolve_value(
1490 (string) get_post_meta($post->ID, '_thinkrank_twitter_title', true),
1491 $post->ID
1492 );
1493
1494 // Title priority: per-post OG override > effective SEO title
1495 // (document <title> / metabox preview) > post title.
1496 if ($og_title_override !== '') {
1497 $data['title'] = $og_title_override;
1498 } elseif ($fallback_title !== null && $fallback_title !== '') {
1499 $data['title'] = $fallback_title;
1500 } else {
1501 $data['title'] = get_the_title($post);
1502 }
1503 // Description priority: per-post OG override > effective meta
1504 // description (meta tag / metabox preview) > derived excerpt.
1505 if ($og_description_override !== '') {
1506 $data['description'] = $og_description_override;
1507 } elseif ($fallback_description !== null && $fallback_description !== '') {
1508 $data['description'] = $fallback_description;
1509 } else {
1510 $data['description'] = $this->get_social_description($post);
1511 }
1512 $data['url'] = get_permalink($post);
1513 $data['type'] = $settings['og_type'] ?? $this->determine_og_type($context_type, []);
1514 $data['published_time'] = get_the_date('c', $post);
1515 $data['modified_time'] = get_the_modified_date('c', $post);
1516 $data['post_id'] = $post->ID;
1517
1518 // Author data
1519 $author_id = $post->post_author;
1520 $data['author'] = [
1521 'name' => get_the_author_meta('display_name', $author_id),
1522 'url' => get_author_posts_url($author_id),
1523 'twitter' => get_the_author_meta('twitter', $author_id)
1524 ];
1525
1526 // Set Open Graph image: per-post override first, then the
1527 // featured-image/default fallback chain.
1528 $data['image'] = $og_image_override !== ''
1529 ? $og_image_override
1530 : $this->get_og_image_for_context($settings, $post);
1531
1532 // Set Twitter image with proper fallback
1533 $data['twitter_image'] = $this->get_twitter_image_for_context($settings, $post);
1534 }
1535 } else {
1536 // Archive-style contexts (category, tag, author, search, date).
1537 // They carry no object of their own, but the site-wide default
1538 // image still applies — without this they fell through to the raw
1539 // logo/site-icon fallback in generate_og_tags() and ignored a
1540 // configured default OG image.
1541 $data['image'] = $this->get_og_image_for_context($settings, null);
1542 $data['twitter_image'] = $this->get_twitter_image_for_context($settings, null);
1543 }
1544
1545 return $data;
1546 }
1547
1548 /**
1549 * Get Open Graph image for context with proper fallback
1550 *
1551 * @since 1.0.0
1552 *
1553 * @param array $settings Social meta settings
1554 * @param \WP_Post|null $post Optional. Post object for post-specific images
1555 * @return string Image URL or empty string
1556 */
1557 private function get_og_image_for_context(array $settings, ?\WP_Post $post = null): string {
1558 // For posts, check featured image first
1559 if ($post) {
1560 $image_id = get_post_thumbnail_id($post);
1561 if ($image_id) {
1562 $image_data = wp_get_attachment_image_src($image_id, 'large');
1563 if (!empty($image_data[0])) {
1564 return $image_data[0];
1565 }
1566 }
1567 }
1568
1569 // Fallback to configured Open Graph image
1570 if (!empty($settings['default_og_image'])) {
1571 return $settings['default_og_image'];
1572 }
1573
1574 // Final fallback to generic default image
1575 if (!empty($settings['default_image'])) {
1576 return $settings['default_image'];
1577 }
1578
1579 // Last-resort fallback to the site logo / site icon. Resolving it here
1580 // (rather than late inside generate_og_tags()) writes it back to the
1581 // shared $data['image'], so generate_twitter_tags() and
1582 // determine_twitter_card_type() see the same image: twitter:image is
1583 // emitted and the card is promoted to summary_large_image, and the
1584 // image is routed through optimize_image_for_platform() so
1585 // og:image:width/height/type/alt companions are produced.
1586 return $this->get_default_social_image();
1587 }
1588
1589 /**
1590 * Get Twitter image for context with proper fallback
1591 *
1592 * @since 1.0.0
1593 *
1594 * @param array $settings Social meta settings
1595 * @param \WP_Post|null $post Optional. Post object for post-specific images
1596 * @return string Image URL or empty string
1597 */
1598 private function get_twitter_image_for_context(array $settings, ?\WP_Post $post = null): string {
1599 // For posts, check for post-specific Twitter image meta first (if implemented)
1600 if ($post) {
1601 // Check for post-specific Twitter image meta (future enhancement)
1602 $post_twitter_image = get_post_meta($post->ID, '_thinkrank_twitter_image', true);
1603 if (!empty($post_twitter_image)) {
1604 return $post_twitter_image;
1605 }
1606
1607 // Check featured image as fallback for posts
1608 $image_id = get_post_thumbnail_id($post);
1609 if ($image_id) {
1610 $image_data = wp_get_attachment_image_src($image_id, 'large');
1611 if (!empty($image_data[0])) {
1612 return $image_data[0];
1613 }
1614 }
1615 }
1616
1617 // Prioritize Twitter-specific default image
1618 if (!empty($settings['default_twitter_image'])) {
1619 return $settings['default_twitter_image'];
1620 }
1621
1622 // Fallback to Open Graph default image
1623 if (!empty($settings['default_og_image'])) {
1624 return $settings['default_og_image'];
1625 }
1626
1627 // Final fallback to generic default image
1628 if (!empty($settings['default_image'])) {
1629 return $settings['default_image'];
1630 }
1631
1632 return '';
1633 }
1634
1635 /**
1636 * Get social media description for post
1637 *
1638 * @since 1.0.0
1639 *
1640 * @param \WP_Post $post Post object
1641 * @return string Social media description
1642 */
1643 private function get_social_description(\WP_Post $post): string {
1644 // Try excerpt first
1645 $description = get_the_excerpt($post);
1646
1647 // If no excerpt, generate from content
1648 if (empty($description)) {
1649 $content = wp_strip_all_tags($post->post_content);
1650 $description = wp_trim_words($content, 30, '...');
1651 }
1652
1653 // If still empty, use site description
1654 if (empty($description)) {
1655 $description = get_bloginfo('description');
1656 }
1657
1658 return $description;
1659 }
1660
1661 /**
1662 * Determine Open Graph type based on context
1663 *
1664 * @since 1.0.0
1665 *
1666 * @param string $context Context type
1667 * @param array $data Content data
1668 * @return string OG type
1669 */
1670 private function determine_og_type(string $context, array $data): string {
1671 switch ($context) {
1672 case 'post':
1673 return 'article';
1674 case 'product':
1675 return 'product';
1676 case 'page':
1677 case 'site':
1678 default:
1679 return 'website';
1680 }
1681 }
1682
1683 /**
1684 * Determine Twitter Card type based on content
1685 *
1686 * @since 1.0.0
1687 *
1688 * @param array $data Content data
1689 * @param string $context Context type
1690 * @return string Twitter Card type
1691 */
1692 private function determine_twitter_card_type(array $data, string $context): string {
1693 // Use a large-image card when a Twitter image will actually be emitted.
1694 // twitter:image resolves to the twitter-specific image first, then the OG
1695 // image, so key the card type off the same precedence.
1696 $twitter_image = !empty($data['twitter_image']) ? $data['twitter_image'] : ($data['image'] ?? '');
1697 return !empty($twitter_image) ? 'summary_large_image' : 'summary';
1698 }
1699
1700 /**
1701 * Optimize title for platform requirements
1702 *
1703 * @since 1.0.0
1704 *
1705 * @param string $title Original title
1706 * @param string $platform Target platform
1707 * @return string Optimized title
1708 */
1709 private function optimize_title_for_platform(string $title, string $platform): string {
1710 if (empty($title)) {
1711 return get_bloginfo('name');
1712 }
1713
1714 $max_length = $this->supported_platforms[$platform]['title_max_length'] ?? 60;
1715
1716 // Multibyte-safe: byte-based substr() would split characters in
1717 // CJK/Bengali/accented titles (WP ships an mbstring fallback).
1718 if (mb_strlen($title) <= $max_length) {
1719 return $title;
1720 }
1721
1722 // Truncate at word boundary
1723 $truncated = wp_trim_words($title, 10, '');
1724 if (mb_strlen($truncated) <= $max_length) {
1725 return $truncated;
1726 }
1727
1728 // Hard truncate if necessary
1729 return mb_substr($title, 0, $max_length - 3) . '...';
1730 }
1731
1732 /**
1733 * Optimize description for platform requirements
1734 *
1735 * @since 1.0.0
1736 *
1737 * @param string $description Original description
1738 * @param string $platform Target platform
1739 * @return string Optimized description
1740 */
1741 private function optimize_description_for_platform(string $description, string $platform): string {
1742 if (empty($description)) {
1743 return get_bloginfo('description');
1744 }
1745
1746 $max_length = $this->supported_platforms[$platform]['description_max_length'] ?? 160;
1747
1748 // Multibyte-safe: byte-based substr() would split characters in
1749 // CJK/Bengali/accented descriptions (WP ships an mbstring fallback).
1750 if (mb_strlen($description) <= $max_length) {
1751 return $description;
1752 }
1753
1754 // Truncate at word boundary
1755 $truncated = wp_trim_words($description, 25, '');
1756 if (mb_strlen($truncated) <= $max_length) {
1757 return $truncated;
1758 }
1759
1760 // Hard truncate if necessary
1761 return mb_substr($description, 0, $max_length - 3) . '...';
1762 }
1763
1764 /**
1765 * Optimize image for platform requirements
1766 *
1767 * @since 1.0.0
1768 *
1769 * @param string $image_url Image URL
1770 * @param string $platform Target platform
1771 * @return array Optimized image data
1772 */
1773 private function optimize_image_for_platform(string $image_url, string $platform): array {
1774 $image_data = [
1775 'url' => $image_url,
1776 'width' => '',
1777 'height' => '',
1778 'alt' => '',
1779 'type' => '',
1780 'valid' => false,
1781 'warnings' => []
1782 ];
1783
1784 if (empty($image_url)) {
1785 return $image_data;
1786 }
1787
1788 // Get image metadata
1789 $attachment_id = attachment_url_to_postid($image_url);
1790 if ($attachment_id) {
1791 $image_meta = wp_get_attachment_metadata($attachment_id);
1792 $image_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
1793 $mime_type = get_post_mime_type($attachment_id);
1794
1795 if ($image_meta && isset($image_meta['width'], $image_meta['height'])) {
1796 $width = (int) $image_meta['width'];
1797 $height = (int) $image_meta['height'];
1798
1799 $image_data['alt'] = $image_alt ?: '';
1800 $image_data['type'] = $mime_type ?: '';
1801
1802 // SVGs and other vector uploads store 0x0 metadata. Dimension
1803 // checks are meaningless there and dividing by 0 is fatal.
1804 if ($width > 0 && $height > 0) {
1805 $image_data['width'] = $width;
1806 $image_data['height'] = $height;
1807
1808 // Validate against platform requirements
1809 $platform_spec = $this->supported_platforms[$platform] ?? [];
1810 $min_width = $platform_spec['image_min_width'] ?? 300;
1811 $min_height = $platform_spec['image_min_height'] ?? 200;
1812
1813 if ($width >= $min_width && $height >= $min_height) {
1814 $image_data['valid'] = true;
1815 } else {
1816 $image_data['warnings'][] = "Image dimensions ({$width}x{$height}) are below recommended minimum ({$min_width}x{$min_height}) for {$platform}";
1817 }
1818
1819 // Check aspect ratio if specified
1820 if (isset($platform_spec['image_recommended_ratio'])) {
1821 $actual_ratio = $width / $height;
1822 $recommended_ratio = $platform_spec['image_recommended_ratio'];
1823 $ratio_tolerance = 0.1;
1824
1825 if (abs($actual_ratio - $recommended_ratio) > $ratio_tolerance) {
1826 $image_data['warnings'][] = sprintf(
1827 "Image aspect ratio (%.2f) differs from recommended ratio (%.2f) for %s",
1828 $actual_ratio,
1829 $recommended_ratio,
1830 $platform
1831 );
1832 }
1833 }
1834 }
1835 }
1836 }
1837
1838 return $image_data;
1839 }
1840
1841 /**
1842 * Get default social image
1843 *
1844 * @since 1.0.0
1845 *
1846 * @return string Default social image URL
1847 */
1848 private function get_default_social_image(): string {
1849 // Try custom logo first
1850 $custom_logo_id = get_theme_mod('custom_logo');
1851 if ($custom_logo_id) {
1852 $logo_data = wp_get_attachment_image_src($custom_logo_id, 'large');
1853 if ($logo_data) {
1854 return $logo_data[0];
1855 }
1856 }
1857
1858 // Try site icon
1859 $site_icon_id = get_option('site_icon');
1860 if ($site_icon_id) {
1861 $icon_data = wp_get_attachment_image_src($site_icon_id, 'large');
1862 if ($icon_data) {
1863 return $icon_data[0];
1864 }
1865 }
1866
1867 return '';
1868 }
1869
1870 /**
1871 * Get Open Graph locale
1872 *
1873 * @since 1.0.0
1874 *
1875 * @return string OG locale
1876 */
1877 private function get_og_locale(): string {
1878 /**
1879 * Filter the locale used for og:locale.
1880 *
1881 * Defaults to get_locale(), which only tracks the active language once
1882 * that language's translation files are installed — on a multilingual
1883 * site without them every translated URL still reports the default
1884 * locale. The multilingual integration answers with the locale its
1885 * provider reports for the language actually being viewed.
1886 *
1887 * @since 1.23.0
1888 *
1889 * @param string $locale Locale for the current request.
1890 */
1891 $locale = (string) apply_filters('thinkrank_og_locale', get_locale());
1892
1893 // Convert WordPress locale to OG locale format for the few that differ
1894 // from the xx_YY form (e.g. bare 'ja').
1895 $og_locale_map = [
1896 'ja' => 'ja_JP',
1897 ];
1898
1899 if (isset($og_locale_map[$locale])) {
1900 return $og_locale_map[$locale];
1901 }
1902
1903 // Fall back to the actual site locale (normalized to xx_YY) rather than
1904 // mislabeling every unmapped language as en_US.
1905 if (preg_match('/^[a-z]{2,3}_[A-Z]{2}$/', $locale)) {
1906 return $locale;
1907 }
1908
1909 // Bare language code (e.g. 'nl') → best-effort xx_XX.
1910 if (preg_match('/^([a-z]{2,3})$/', $locale, $m)) {
1911 return $m[1] . '_' . strtoupper($m[1]);
1912 }
1913
1914 return 'en_US';
1915 }
1916
1917 /**
1918 * Get current URL
1919 *
1920 * @since 1.0.0
1921 *
1922 * @return string Current URL
1923 */
1924 private function get_current_url(): string {
1925 if (is_admin()) {
1926 return home_url();
1927 }
1928
1929 global $wp;
1930 return home_url(add_query_arg([], $wp->request));
1931 }
1932
1933 /**
1934 * Get Twitter site handle
1935 *
1936 * @since 1.0.0
1937 *
1938 * @return string Twitter site handle
1939 */
1940 private function get_twitter_site_handle(): string {
1941 $settings = $this->get_settings('site');
1942 $username = $settings['twitter_username'] ?? '';
1943
1944 if (empty($username)) {
1945 return '';
1946 }
1947
1948 // Ensure username starts with @ for meta tag output
1949 return '@' . ltrim($username, '@');
1950 }
1951
1952 /**
1953 * Get Twitter creator handle
1954 *
1955 * @since 1.0.0
1956 *
1957 * @return string Twitter creator handle
1958 */
1959 private function get_twitter_creator_handle(): string {
1960 $settings = $this->get_settings('site');
1961 $creator = $settings['twitter_creator'] ?? '';
1962
1963 if (empty($creator)) {
1964 return '';
1965 }
1966
1967 // Ensure creator starts with @ for meta tag output
1968 return '@' . ltrim($creator, '@');
1969 }
1970
1971 /**
1972 * Add context-specific Open Graph tags
1973 *
1974 * @since 1.0.0
1975 *
1976 * @param array $og_tags OG tags array
1977 * @param string $context Context type
1978 * @param array $data Content data
1979 * @param string $og_type OG type
1980 * @return array Updated OG tags
1981 */
1982 private function add_context_specific_og_tags(array $og_tags, string $context, array $data, string $og_type): array {
1983 switch ($og_type) {
1984 case 'article':
1985 if (!empty($data['author']['name'])) {
1986 $og_tags['article:author'] = $data['author']['name'];
1987 }
1988 if (!empty($data['published_time'])) {
1989 $og_tags['article:published_time'] = $data['published_time'];
1990 }
1991 if (!empty($data['modified_time'])) {
1992 $og_tags['article:modified_time'] = $data['modified_time'];
1993 }
1994 // article:section — the post's primary category (parity with the
1995 // basic emitter, which the active path previously omitted).
1996 if (!empty($data['post_id'])) {
1997 $categories = get_the_category((int) $data['post_id']);
1998 if (!empty($categories) && !is_wp_error($categories)) {
1999 $og_tags['article:section'] = $categories[0]->name;
2000 }
2001 }
2002 break;
2003 case 'product':
2004 // Product-specific tags would be added here
2005 // This could be extended with price, availability, etc.
2006 break;
2007 }
2008
2009 // Add local business Open Graph tags if business data is available
2010 $og_tags = $this->add_local_business_og_tags($og_tags, $data);
2011
2012 return $og_tags;
2013 }
2014
2015 /**
2016 * Add local business Open Graph tags
2017 *
2018 * @since 1.0.0
2019 *
2020 * @param array $og_tags OG tags array
2021 * @param array $data Content data
2022 * @return array Updated OG tags with local business information
2023 */
2024 private function add_local_business_og_tags(array $og_tags, array $data): array {
2025 // Get business data from Site Identity settings
2026 $business_data = $this->get_local_business_data();
2027
2028 if (empty($business_data) || empty($business_data['business_name'])) {
2029 return $og_tags;
2030 }
2031
2032 // Add business contact data Open Graph tags
2033 if (!empty($business_data['business_address'])) {
2034 $og_tags['business:contact_data:street_address'] = $business_data['business_address'];
2035 }
2036
2037 if (!empty($business_data['business_city'])) {
2038 $og_tags['business:contact_data:locality'] = $business_data['business_city'];
2039 }
2040
2041 if (!empty($business_data['business_state'])) {
2042 $og_tags['business:contact_data:region'] = $business_data['business_state'];
2043 }
2044
2045 if (!empty($business_data['business_postal_code'])) {
2046 $og_tags['business:contact_data:postal_code'] = $business_data['business_postal_code'];
2047 }
2048
2049 if (!empty($business_data['business_country'])) {
2050 $og_tags['business:contact_data:country_name'] = $business_data['business_country'];
2051 }
2052
2053 if (!empty($business_data['business_phone'])) {
2054 $og_tags['business:contact_data:phone_number'] = $business_data['business_phone'];
2055 }
2056
2057 if (!empty($business_data['business_email'])) {
2058 $og_tags['business:contact_data:email'] = $business_data['business_email'];
2059 }
2060
2061 // Add business hours if available
2062 if (!empty($business_data['business_hours']) && is_array($business_data['business_hours'])) {
2063 $formatted_hours = $this->format_business_hours_for_og($business_data['business_hours']);
2064 if (!empty($formatted_hours)) {
2065 $og_tags['business:hours'] = $formatted_hours;
2066 }
2067 }
2068
2069 // Add business website
2070 if (!empty($business_data['business_website'])) {
2071 $og_tags['business:contact_data:website'] = $business_data['business_website'];
2072 }
2073
2074 // Add coordinates if available
2075 if (!empty($business_data['business_latitude']) && !empty($business_data['business_longitude'])) {
2076 $og_tags['place:location:latitude'] = $business_data['business_latitude'];
2077 $og_tags['place:location:longitude'] = $business_data['business_longitude'];
2078 }
2079
2080 return $og_tags;
2081 }
2082
2083 /**
2084 * Get local business data from Site Identity settings
2085 *
2086 * @since 1.0.0
2087 *
2088 * @return array Business data array
2089 */
2090 private function get_local_business_data(): array {
2091 // Try to get Site Identity Manager
2092 if (!class_exists('ThinkRank\\SEO\\Site_Identity_Manager')) {
2093 require_once THINKRANK_PLUGIN_DIR . 'includes/seo/class-site-identity-manager.php';
2094 }
2095
2096 $site_identity_manager = new \ThinkRank\SEO\Site_Identity_Manager();
2097 $settings = $site_identity_manager->get_settings('site');
2098
2099 // Only return data if local SEO is enabled
2100 if (empty($settings['local_seo_enabled'])) {
2101 return [];
2102 }
2103
2104 return [
2105 'business_name' => $settings['business_name'] ?? '',
2106 'business_address' => $settings['business_address'] ?? '',
2107 'business_city' => $settings['business_city'] ?? '',
2108 'business_state' => $settings['business_state'] ?? '',
2109 'business_postal_code' => $settings['business_postal_code'] ?? '',
2110 'business_country' => $settings['business_country'] ?? '',
2111 'business_phone' => $settings['business_phone'] ?? '',
2112 'business_email' => $settings['business_email'] ?? '',
2113 'business_hours' => $settings['business_hours'] ?? [],
2114 'business_website' => $settings['business_website'] ?? home_url(),
2115 'business_latitude' => $settings['business_latitude'] ?? '',
2116 'business_longitude' => $settings['business_longitude'] ?? ''
2117 ];
2118 }
2119
2120 /**
2121 * Format business hours for Open Graph tags
2122 *
2123 * @since 1.0.0
2124 *
2125 * @param array $business_hours Business hours array
2126 * @return string Formatted hours string
2127 */
2128 private function format_business_hours_for_og(array $business_hours): string {
2129 $formatted_days = [];
2130
2131 $day_names = [
2132 'monday' => 'Monday',
2133 'tuesday' => 'Tuesday',
2134 'wednesday' => 'Wednesday',
2135 'thursday' => 'Thursday',
2136 'friday' => 'Friday',
2137 'saturday' => 'Saturday',
2138 'sunday' => 'Sunday'
2139 ];
2140
2141 foreach ($day_names as $day => $display_name) {
2142 if (isset($business_hours[$day]) && !empty($business_hours[$day])) {
2143 $day_data = $business_hours[$day];
2144
2145 if (!empty($day_data['closed']) || empty($day_data['open']) || empty($day_data['close'])) {
2146 $formatted_days[] = $display_name . ': Closed';
2147 } else {
2148 $formatted_days[] = $display_name . ': ' . $day_data['open'] . ' - ' . $day_data['close'];
2149 }
2150 }
2151 }
2152
2153 return implode('; ', $formatted_days);
2154 }
2155
2156 /**
2157 * Apply platform-specific optimizations
2158 *
2159 * @since 1.0.0
2160 *
2161 * @param array $og_tags OG tags array
2162 * @param string $platform Target platform
2163 * @param array $data Content data
2164 * @return array Optimized OG tags
2165 */
2166 private function apply_platform_specific_optimizations(array $og_tags, string $platform, array $data): array {
2167 $settings = $this->get_settings('site');
2168
2169 switch ($platform) {
2170 case 'linkedin':
2171 // LinkedIn prefers professional content
2172 if ($settings['enable_linkedin'] ?? false) {
2173 if (isset($og_tags['og:description'])) {
2174 $og_tags['og:description'] = $this->make_description_professional($og_tags['og:description']);
2175 }
2176 // Add LinkedIn-specific type if appropriate
2177 if ($og_tags['og:type'] === 'article') {
2178 $og_tags['article:author'] = $data['author'] ?? '';
2179 }
2180 }
2181 break;
2182
2183 case 'pinterest':
2184 // Pinterest prefers descriptive content
2185 if ($settings['enable_pinterest'] ?? false) {
2186 if (isset($og_tags['og:description'])) {
2187 $og_tags['og:description'] = $this->make_description_descriptive($og_tags['og:description']);
2188 }
2189 // Pinterest prefers article type for rich pins
2190 if (in_array($og_tags['og:type'], ['website', 'blog'], true)) {
2191 $og_tags['og:type'] = 'article';
2192 }
2193 }
2194 break;
2195
2196 case 'instagram':
2197 // Instagram optimizations
2198 if ($settings['enable_instagram'] ?? false) {
2199 // Instagram prefers square or vertical images
2200 if (isset($og_tags['og:description'])) {
2201 $og_tags['og:description'] = $this->make_description_engaging($og_tags['og:description']);
2202 }
2203 }
2204 break;
2205
2206 case 'tiktok':
2207 // TikTok optimizations
2208 if ($settings['enable_tiktok'] ?? false) {
2209 // TikTok prefers short, catchy descriptions
2210 if (isset($og_tags['og:description'])) {
2211 $og_tags['og:description'] = $this->make_description_catchy($og_tags['og:description']);
2212 }
2213 }
2214 break;
2215 }
2216
2217 return $og_tags;
2218 }
2219
2220 /**
2221 * Make description more professional for LinkedIn
2222 *
2223 * @since 1.0.0
2224 *
2225 * @param string $description Original description
2226 * @return string Professional description
2227 */
2228 private function make_description_professional(string $description): string {
2229 // Remove casual language and emojis, add professional tone
2230 $description = preg_replace('/[^\w\s\.,!?-]/', '', $description);
2231
2232 // Add professional keywords if not present
2233 $professional_words = ['insights', 'expertise', 'professional', 'industry', 'business'];
2234 if (!preg_match('/\b(' . implode('|', $professional_words) . ')\b/i', $description)) {
2235 $description = 'Professional insights: ' . $description;
2236 }
2237
2238 return $description;
2239 }
2240
2241 /**
2242 * Make description more descriptive for Pinterest
2243 *
2244 * @since 1.0.0
2245 *
2246 * @param string $description Original description
2247 * @return string Descriptive description
2248 */
2249 private function make_description_descriptive(string $description): string {
2250 // Pinterest users love detailed, searchable descriptions
2251 $descriptive_words = ['discover', 'explore', 'learn', 'find', 'ideas'];
2252
2253 if (!preg_match('/\b(' . implode('|', $descriptive_words) . ')\b/i', $description)) {
2254 $description = 'Discover ' . lcfirst($description);
2255 }
2256
2257 return $description;
2258 }
2259
2260 /**
2261 * Make description engaging for Instagram
2262 *
2263 * @since 1.0.0
2264 *
2265 * @param string $description Original description
2266 * @return string Engaging description
2267 */
2268 private function make_description_engaging(string $description): string {
2269 // Instagram prefers engaging, visual language
2270 $engaging_words = ['amazing', 'stunning', 'beautiful', 'incredible', 'inspiring'];
2271
2272 if (!preg_match('/\b(' . implode('|', $engaging_words) . ')\b/i', $description)) {
2273 $description = '' . $description;
2274 }
2275
2276 return $description;
2277 }
2278
2279 /**
2280 * Make description catchy for TikTok
2281 *
2282 * @since 1.0.0
2283 *
2284 * @param string $description Original description
2285 * @return string Catchy description
2286 */
2287 private function make_description_catchy(string $description): string {
2288 // TikTok prefers short, catchy descriptions
2289 $catchy_words = ['viral', 'trending', 'must-see', 'epic', 'mind-blowing'];
2290
2291 // Limit to 100 characters for TikTok
2292 if (strlen($description) > 100) {
2293 $description = substr($description, 0, 97) . '...';
2294 }
2295
2296 if (!preg_match('/\b(' . implode('|', $catchy_words) . ')\b/i', $description)) {
2297 $description = '🔥 ' . $description;
2298 }
2299
2300 return $description;
2301 }
2302
2303 /**
2304 * Add Twitter Card specific tags
2305 *
2306 * @since 1.0.0
2307 *
2308 * @param array $twitter_tags Twitter tags array
2309 * @param string $card_type Card type
2310 * @param array $data Content data
2311 * @param string $context Context type
2312 * @return array Updated Twitter tags
2313 */
2314 private function add_twitter_card_specific_tags(array $twitter_tags, string $card_type, array $data, string $context): array {
2315 // The content extractor only produces summary / summary_large_image
2316 // cards, so the player/app card variants were dead code that read keys
2317 // (video_url, app_name, …) the extractor never sets. Kept as a filterable
2318 // extension point for add-ons that do populate richer card data.
2319 return apply_filters('thinkrank_twitter_card_specific_tags', $twitter_tags, $card_type, $data, $context);
2320 }
2321
2322 /**
2323 * Generate Open Graph preview
2324 *
2325 * @since 1.0.0
2326 *
2327 * @param array $og_tags OG tags
2328 * @param string $platform Platform name
2329 * @param array $preview Preview array
2330 * @return array Updated preview
2331 */
2332 private function generate_og_preview(array $og_tags, string $platform, array $preview): array {
2333 $preview['title'] = $og_tags['og:title'] ?? '';
2334 $preview['description'] = $og_tags['og:description'] ?? '';
2335 $preview['image'] = $og_tags['og:image'] ?? '';
2336 $preview['preview_url'] = $og_tags['og:url'] ?? '';
2337
2338 // Validate required fields
2339 $required_fields = ['og:title', 'og:type', 'og:image', 'og:url'];
2340 $missing_fields = [];
2341
2342 foreach ($required_fields as $field) {
2343 if (empty($og_tags[$field])) {
2344 $missing_fields[] = $field;
2345 }
2346 }
2347
2348 if (empty($missing_fields)) {
2349 $preview['valid'] = true;
2350 } else {
2351 $preview['warnings'][] = 'Missing required fields: ' . implode(', ', $missing_fields);
2352 }
2353
2354 // Platform-specific validation
2355 $platform_spec = $this->supported_platforms[$platform] ?? [];
2356 if (isset($platform_spec['title_max_length']) && strlen($preview['title']) > $platform_spec['title_max_length']) {
2357 $preview['warnings'][] = "Title exceeds {$platform} maximum length of {$platform_spec['title_max_length']} characters";
2358 }
2359
2360 return $preview;
2361 }
2362
2363 /**
2364 * Generate Twitter preview
2365 *
2366 * @since 1.0.0
2367 *
2368 * @param array $twitter_tags Twitter tags
2369 * @param array $preview Preview array
2370 * @return array Updated preview
2371 */
2372 private function generate_twitter_preview(array $twitter_tags, array $preview): array {
2373 $preview['title'] = $twitter_tags['twitter:title'] ?? '';
2374 $preview['description'] = $twitter_tags['twitter:description'] ?? '';
2375 $preview['image'] = $twitter_tags['twitter:image'] ?? '';
2376
2377 // Validate required fields
2378 $required_fields = ['twitter:card', 'twitter:title'];
2379 $missing_fields = [];
2380
2381 foreach ($required_fields as $field) {
2382 if (empty($twitter_tags[$field])) {
2383 $missing_fields[] = $field;
2384 }
2385 }
2386
2387 if (empty($missing_fields)) {
2388 $preview['valid'] = true;
2389 } else {
2390 $preview['warnings'][] = 'Missing required fields: ' . implode(', ', $missing_fields);
2391 }
2392
2393 // Twitter-specific validation
2394 $twitter_spec = $this->supported_platforms['twitter'];
2395 if (strlen($preview['title']) > $twitter_spec['title_max_length']) {
2396 $preview['warnings'][] = "Title exceeds Twitter maximum length of {$twitter_spec['title_max_length']} characters";
2397 }
2398
2399 return $preview;
2400 }
2401
2402 /**
2403 * Generate Pinterest preview
2404 *
2405 * @since 1.0.0
2406 *
2407 * @param array $og_tags OG tags
2408 * @param array $preview Preview array
2409 * @return array Updated preview
2410 */
2411 private function generate_pinterest_preview(array $og_tags, array $preview): array {
2412 $preview['title'] = $og_tags['og:title'] ?? '';
2413 $preview['description'] = $og_tags['og:description'] ?? '';
2414 $preview['image'] = $og_tags['og:image'] ?? '';
2415 $preview['preview_url'] = $og_tags['og:url'] ?? '';
2416
2417 // Validate required fields for Pinterest
2418 $required_fields = ['og:title', 'og:image', 'og:url'];
2419 $missing_fields = [];
2420
2421 foreach ($required_fields as $field) {
2422 if (empty($og_tags[$field])) {
2423 $missing_fields[] = $field;
2424 }
2425 }
2426
2427 if (empty($missing_fields)) {
2428 $preview['valid'] = true;
2429 } else {
2430 $preview['warnings'][] = 'Missing required fields: ' . implode(', ', $missing_fields);
2431 }
2432
2433 // Pinterest-specific validation and suggestions
2434 $pinterest_spec = $this->supported_platforms['pinterest'] ?? [];
2435
2436 // Title length validation
2437 if (isset($pinterest_spec['title_max_length']) && strlen($preview['title']) > $pinterest_spec['title_max_length']) {
2438 $preview['warnings'][] = "Title exceeds Pinterest maximum length of {$pinterest_spec['title_max_length']} characters";
2439 }
2440
2441 // Description length validation
2442 if (isset($pinterest_spec['description_max_length']) && strlen($preview['description']) > $pinterest_spec['description_max_length']) {
2443 $preview['warnings'][] = "Description exceeds Pinterest maximum length of {$pinterest_spec['description_max_length']} characters";
2444 }
2445
2446 // Image dimension suggestions for Pinterest
2447 if (!empty($preview['image'])) {
2448 $image_data = $this->optimize_image_for_platform($preview['image'], 'pinterest');
2449 if (!empty($image_data['width']) && !empty($image_data['height'])) {
2450 $aspect_ratio = $image_data['width'] / $image_data['height'];
2451
2452 // Pinterest prefers vertical images (2:3 ratio is optimal)
2453 if ($aspect_ratio > 1) {
2454 $preview['suggestions'][] = 'Pinterest performs better with vertical images (2:3 aspect ratio recommended)';
2455 } elseif ($aspect_ratio < 0.6) {
2456 $preview['suggestions'][] = 'Image is very tall - consider a 2:3 aspect ratio for optimal Pinterest performance';
2457 }
2458
2459 // Minimum size recommendations
2460 if ($image_data['width'] < 600) {
2461 $preview['suggestions'][] = 'Pinterest recommends images at least 600px wide for better quality';
2462 }
2463 }
2464 }
2465
2466 return $preview;
2467 }
2468
2469 /**
2470 * Generate platform-specific meta tags
2471 *
2472 * Platform IDs and verification codes are owned by the Social Platforms tab
2473 * (ThinkRank\API\Social_Platforms_Endpoint), which stores them in core
2474 * Settings (wp_options) with the sensitive codes encrypted at rest. The
2475 * social_meta settings table this manager normally reads no longer receives
2476 * these values from the UI, so we resolve each key from core Settings first
2477 * and fall back to any legacy value still present in the passed-in table
2478 * settings — otherwise the verification tags would never render.
2479 *
2480 * @since 1.0.0
2481 *
2482 * @param array $settings Settings array (social_meta table) — legacy fallback.
2483 * @return array Platform-specific meta tags
2484 */
2485 private function generate_platform_meta_tags(array $settings): array {
2486 $platform_tags = [];
2487
2488 $core = \ThinkRank\Core\Settings::instance();
2489 // Core Settings (decrypted for sensitive keys) wins; the table value is a
2490 // backward-compat fallback for installs that saved these before the UI
2491 // moved to the Social Platforms tab.
2492 $resolve = static function (string $key) use ($core, $settings): string {
2493 $value = (string) $core->get($key, '');
2494 if ('' === $value) {
2495 $value = (string) ($settings[$key] ?? '');
2496 }
2497 return $value;
2498 };
2499
2500 // Facebook meta tags
2501 $facebook_app_id = $resolve('facebook_app_id');
2502 if ('' !== $facebook_app_id) {
2503 $platform_tags['fb:app_id'] = $facebook_app_id;
2504 }
2505
2506 $facebook_admins = $resolve('facebook_admins');
2507 if ('' !== $facebook_admins) {
2508 $platform_tags['fb:admins'] = $facebook_admins;
2509 }
2510
2511 // Pinterest site verification
2512 $pinterest = $resolve('pinterest_site_verification');
2513 if ('' !== $pinterest) {
2514 $platform_tags['pinterest-site-verification'] = $pinterest;
2515 }
2516
2517 // Instagram verification
2518 $instagram = $resolve('instagram_verification');
2519 if ('' !== $instagram) {
2520 $platform_tags['instagram-site-verification'] = $instagram;
2521 }
2522
2523 // TikTok verification
2524 $tiktok = $resolve('tiktok_verification');
2525 if ('' !== $tiktok) {
2526 $platform_tags['tiktok-site-verification'] = $tiktok;
2527 }
2528
2529 // YouTube channel verification
2530 $youtube = $resolve('youtube_channel_id');
2531 if ('' !== $youtube) {
2532 $platform_tags['youtube-channel-id'] = $youtube;
2533 }
2534
2535 // WhatsApp Business verification
2536 $whatsapp = $resolve('whatsapp_business_id');
2537 if ('' !== $whatsapp) {
2538 $platform_tags['whatsapp-business-id'] = $whatsapp;
2539 }
2540
2541 return $platform_tags;
2542 }
2543
2544 /**
2545 * Convert OG, Twitter, and Platform tags to HTML meta tags
2546 *
2547 * @since 1.0.0
2548 *
2549 * @param array $og_tags Open Graph tags
2550 * @param array $twitter_tags Twitter tags
2551 * @param array $platform_tags Platform-specific tags
2552 * @return array HTML meta tags
2553 */
2554 private function convert_to_meta_tags(array $og_tags, array $twitter_tags, array $platform_tags = []): array {
2555 $meta_tags = [];
2556
2557 // Convert OG tags
2558 foreach ($og_tags as $property => $content) {
2559 if (!empty($content)) {
2560 $meta_tags[] = [
2561 'property' => esc_attr($property),
2562 'content' => esc_attr($content)
2563 ];
2564 }
2565 }
2566
2567 // Convert Twitter tags
2568 foreach ($twitter_tags as $name => $content) {
2569 if (!empty($content)) {
2570 $meta_tags[] = [
2571 'name' => esc_attr($name),
2572 'content' => esc_attr($content)
2573 ];
2574 }
2575 }
2576
2577 // Convert Platform tags
2578 foreach ($platform_tags as $name => $content) {
2579 if (!empty($content)) {
2580 // Determine if it should be property or name attribute
2581 if (strpos($name, 'fb:') === 0) {
2582 // Facebook tags use property attribute
2583 $meta_tags[] = [
2584 'property' => esc_attr($name),
2585 'content' => esc_attr($content)
2586 ];
2587 } else {
2588 // Other platform tags use name attribute
2589 $meta_tags[] = [
2590 'name' => esc_attr($name),
2591 'content' => esc_attr($content)
2592 ];
2593 }
2594 }
2595 }
2596
2597 return $meta_tags;
2598 }
2599
2600 /**
2601 * Validate social image
2602 *
2603 * @since 1.0.0
2604 *
2605 * @param string $image_url Image URL to validate
2606 * @return array Validation results
2607 */
2608 private function validate_social_image(string $image_url): array {
2609 $validation = [
2610 'valid' => false,
2611 'warnings' => [],
2612 'suggestions' => []
2613 ];
2614
2615 if (empty($image_url)) {
2616 $validation['warnings'][] = 'No image provided';
2617 return $validation;
2618 }
2619
2620 // Check if URL is valid
2621 if (!filter_var($image_url, FILTER_VALIDATE_URL)) {
2622 $validation['warnings'][] = 'Invalid image URL';
2623 return $validation;
2624 }
2625
2626 // Get image metadata if it's a local attachment
2627 $attachment_id = attachment_url_to_postid($image_url);
2628 if ($attachment_id) {
2629 $image_meta = wp_get_attachment_metadata($attachment_id);
2630 $meta_width = isset($image_meta['width']) ? (int) $image_meta['width'] : 0;
2631 $meta_height = isset($image_meta['height']) ? (int) $image_meta['height'] : 0;
2632
2633 // SVGs and other vector uploads store 0x0 metadata — skip the
2634 // dimension/ratio checks instead of dividing by 0.
2635 if ($image_meta && $meta_width > 0 && $meta_height > 0) {
2636 // Check minimum dimensions for major platforms
2637 $min_width = 600; // Facebook minimum
2638 $min_height = 315; // Facebook minimum
2639
2640 if ($meta_width >= $min_width && $meta_height >= $min_height) {
2641 $validation['valid'] = true;
2642 } else {
2643 $validation['warnings'][] = "Image dimensions ({$meta_width}x{$meta_height}) are below recommended minimum ({$min_width}x{$min_height})";
2644 }
2645
2646 // Check file size
2647 $file_path = get_attached_file($attachment_id);
2648 if ($file_path && file_exists($file_path)) {
2649 $file_size = filesize($file_path);
2650 $max_size = 5 * 1024 * 1024; // 5MB
2651
2652 if ($file_size > $max_size) {
2653 $validation['warnings'][] = 'Image file size exceeds 5MB, may not display on some platforms';
2654 }
2655 }
2656
2657 // Check aspect ratio
2658 $aspect_ratio = $meta_width / $meta_height;
2659 if ($aspect_ratio < 1.5 || $aspect_ratio > 2.5) {
2660 $validation['suggestions'][] = 'Consider using an image with 1.91:1 aspect ratio for optimal display';
2661 }
2662 }
2663 } else {
2664 $validation['suggestions'][] = 'External images may not be optimized for social media platforms';
2665 }
2666
2667 return $validation;
2668 }
2669 }
2670