| 1 |
<?php |
| 2 |
/** |
| 3 |
* Content Optimization Manager Class |
| 4 |
* |
| 5 |
* Advanced content optimization engine with real-time scoring, SEO templates, |
| 6 |
* performance tracking, and integration with AI Content Analyzer. Implements |
| 7 |
* 2025 SEO best practices with industry-standard optimization algorithms. |
| 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 |
* Content Optimization Manager Class |
| 25 |
* |
| 26 |
* Provides comprehensive content optimization with real-time scoring, |
| 27 |
* SEO templates, performance tracking, and actionable recommendations |
| 28 |
* integrated with AI Content Analyzer for complete optimization workflow. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
*/ |
| 32 |
class Content_Optimization_Manager extends Abstract_SEO_Manager { |
| 33 |
|
| 34 |
/** |
| 35 |
* Content optimization scoring weights (2025 SEO standards) |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
private array $optimization_weights = [ |
| 41 |
'content_quality' => 25, // Content depth, uniqueness, value |
| 42 |
'keyword_optimization' => 20, // Keyword usage and distribution |
| 43 |
'readability' => 15, // Reading ease and comprehension |
| 44 |
'structure' => 15, // Headings, paragraphs, formatting |
| 45 |
'technical_seo' => 10, // Meta tags, URLs, schema |
| 46 |
'user_experience' => 10, // Engagement signals, CTR optimization |
| 47 |
'semantic_relevance' => 5 // Topic relevance and entity coverage |
| 48 |
]; |
| 49 |
|
| 50 |
/** |
| 51 |
* SEO content templates for different content types |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* @var array |
| 55 |
*/ |
| 56 |
private array $content_templates = [ |
| 57 |
'blog_post' => [ |
| 58 |
'min_word_count' => 300, |
| 59 |
'optimal_word_count' => 1500, |
| 60 |
'max_word_count' => 3000, |
| 61 |
'heading_structure' => [ |
| 62 |
'h1_count' => 1, |
| 63 |
'h2_min' => 2, |
| 64 |
'h2_max' => 8, |
| 65 |
'h3_max' => 15 |
| 66 |
], |
| 67 |
'keyword_density' => [ |
| 68 |
'primary' => [1.0, 3.0], |
| 69 |
'secondary' => [0.5, 2.0], |
| 70 |
'long_tail' => [0.1, 1.0] |
| 71 |
], |
| 72 |
'content_elements' => [ |
| 73 |
'introduction' => ['min_words' => 50, 'max_words' => 150], |
| 74 |
'conclusion' => ['min_words' => 50, 'max_words' => 200], |
| 75 |
'images' => ['min' => 1, 'optimal' => 3], |
| 76 |
'internal_links' => ['min' => 2, 'optimal' => 5], |
| 77 |
'external_links' => ['min' => 1, 'optimal' => 3] |
| 78 |
] |
| 79 |
], |
| 80 |
'product_page' => [ |
| 81 |
'min_word_count' => 150, |
| 82 |
'optimal_word_count' => 500, |
| 83 |
'max_word_count' => 1000, |
| 84 |
'heading_structure' => [ |
| 85 |
'h1_count' => 1, |
| 86 |
'h2_min' => 1, |
| 87 |
'h2_max' => 5, |
| 88 |
'h3_max' => 10 |
| 89 |
], |
| 90 |
'keyword_density' => [ |
| 91 |
'primary' => [1.5, 4.0], |
| 92 |
'secondary' => [0.5, 2.5], |
| 93 |
'long_tail' => [0.2, 1.5] |
| 94 |
], |
| 95 |
'content_elements' => [ |
| 96 |
'product_description' => ['min_words' => 100, 'max_words' => 300], |
| 97 |
'features' => ['min_items' => 3, 'max_items' => 10], |
| 98 |
'images' => ['min' => 3, 'optimal' => 8], |
| 99 |
'reviews_section' => ['required' => true], |
| 100 |
'related_products' => ['min' => 3, 'optimal' => 6] |
| 101 |
] |
| 102 |
], |
| 103 |
'landing_page' => [ |
| 104 |
'min_word_count' => 400, |
| 105 |
'optimal_word_count' => 1200, |
| 106 |
'max_word_count' => 2500, |
| 107 |
'heading_structure' => [ |
| 108 |
'h1_count' => 1, |
| 109 |
'h2_min' => 3, |
| 110 |
'h2_max' => 6, |
| 111 |
'h3_max' => 12 |
| 112 |
], |
| 113 |
'keyword_density' => [ |
| 114 |
'primary' => [1.5, 3.5], |
| 115 |
'secondary' => [0.8, 2.5], |
| 116 |
'long_tail' => [0.3, 1.5] |
| 117 |
], |
| 118 |
'content_elements' => [ |
| 119 |
'hero_section' => ['min_words' => 30, 'max_words' => 80], |
| 120 |
'value_proposition' => ['min_words' => 50, 'max_words' => 150], |
| 121 |
'benefits' => ['min_items' => 3, 'max_items' => 6], |
| 122 |
'social_proof' => ['required' => true], |
| 123 |
'call_to_action' => ['min' => 2, 'optimal' => 4] |
| 124 |
] |
| 125 |
], |
| 126 |
'category_page' => [ |
| 127 |
'min_word_count' => 200, |
| 128 |
'optimal_word_count' => 800, |
| 129 |
'max_word_count' => 1500, |
| 130 |
'heading_structure' => [ |
| 131 |
'h1_count' => 1, |
| 132 |
'h2_min' => 2, |
| 133 |
'h2_max' => 6, |
| 134 |
'h3_max' => 10 |
| 135 |
], |
| 136 |
'keyword_density' => [ |
| 137 |
'primary' => [1.0, 3.0], |
| 138 |
'secondary' => [0.5, 2.0], |
| 139 |
'long_tail' => [0.2, 1.0] |
| 140 |
], |
| 141 |
'content_elements' => [ |
| 142 |
'category_description' => ['min_words' => 100, 'max_words' => 300], |
| 143 |
'subcategories' => ['min' => 2, 'optimal' => 8], |
| 144 |
'featured_products' => ['min' => 6, 'optimal' => 12], |
| 145 |
'filters' => ['required' => true], |
| 146 |
'breadcrumbs' => ['required' => true] |
| 147 |
] |
| 148 |
] |
| 149 |
]; |
| 150 |
|
| 151 |
/** |
| 152 |
* Performance tracking metrics |
| 153 |
* |
| 154 |
* @since 1.0.0 |
| 155 |
* @var array |
| 156 |
*/ |
| 157 |
private array $performance_metrics = [ |
| 158 |
'optimization_score' => [ |
| 159 |
'weight' => 30, |
| 160 |
'target' => 85, |
| 161 |
'calculation' => 'weighted_average' |
| 162 |
], |
| 163 |
'content_quality_score' => [ |
| 164 |
'weight' => 25, |
| 165 |
'target' => 80, |
| 166 |
'calculation' => 'ai_analysis' |
| 167 |
], |
| 168 |
'keyword_performance' => [ |
| 169 |
'weight' => 20, |
| 170 |
'target' => 75, |
| 171 |
'calculation' => 'density_distribution' |
| 172 |
], |
| 173 |
'readability_score' => [ |
| 174 |
'weight' => 15, |
| 175 |
'target' => 70, |
| 176 |
'calculation' => 'flesch_kincaid' |
| 177 |
], |
| 178 |
'technical_seo_score' => [ |
| 179 |
'weight' => 10, |
| 180 |
'target' => 90, |
| 181 |
'calculation' => 'compliance_check' |
| 182 |
] |
| 183 |
]; |
| 184 |
|
| 185 |
/** |
| 186 |
* Optimization recommendation priorities |
| 187 |
* |
| 188 |
* @since 1.0.0 |
| 189 |
* @var array |
| 190 |
*/ |
| 191 |
private array $recommendation_priorities = [ |
| 192 |
'critical' => [ |
| 193 |
'score_threshold' => 40, |
| 194 |
'impact' => 'high', |
| 195 |
'urgency' => 'immediate', |
| 196 |
'color' => '#dc3545' |
| 197 |
], |
| 198 |
'high' => [ |
| 199 |
'score_threshold' => 60, |
| 200 |
'impact' => 'high', |
| 201 |
'urgency' => 'soon', |
| 202 |
'color' => '#fd7e14' |
| 203 |
], |
| 204 |
'medium' => [ |
| 205 |
'score_threshold' => 75, |
| 206 |
'impact' => 'medium', |
| 207 |
'urgency' => 'moderate', |
| 208 |
'color' => '#ffc107' |
| 209 |
], |
| 210 |
'low' => [ |
| 211 |
'score_threshold' => 85, |
| 212 |
'impact' => 'low', |
| 213 |
'urgency' => 'when_possible', |
| 214 |
'color' => '#28a745' |
| 215 |
], |
| 216 |
'optimal' => [ |
| 217 |
'score_threshold' => 100, |
| 218 |
'impact' => 'maintenance', |
| 219 |
'urgency' => 'none', |
| 220 |
'color' => '#20c997' |
| 221 |
] |
| 222 |
]; |
| 223 |
|
| 224 |
/** |
| 225 |
* Constructor |
| 226 |
* |
| 227 |
* @since 1.0.0 |
| 228 |
*/ |
| 229 |
public function __construct() { |
| 230 |
parent::__construct('content_optimization_manager'); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Optimize content with comprehensive analysis and recommendations |
| 235 |
* |
| 236 |
* @since 1.0.0 |
| 237 |
* |
| 238 |
* @param string $content Content to optimize |
| 239 |
* @param array $keywords Target keywords |
| 240 |
* @param string $content_type Content type (blog_post, product_page, etc.) |
| 241 |
* @param array $options Optimization options |
| 242 |
* @return array Comprehensive optimization results |
| 243 |
*/ |
| 244 |
public function optimize_content(string $content, array $keywords = [], string $content_type = 'blog_post', array $options = []): array { |
| 245 |
$optimization = [ |
| 246 |
'content_analysis' => [], |
| 247 |
'optimization_score' => 0, |
| 248 |
'template_compliance' => [], |
| 249 |
'recommendations' => [], |
| 250 |
'performance_metrics' => [], |
| 251 |
'optimization_opportunities' => [], |
| 252 |
'content_suggestions' => [], |
| 253 |
'optimization_timestamp' => current_time('mysql') |
| 254 |
]; |
| 255 |
|
| 256 |
// Get AI Content Analyzer results |
| 257 |
$ai_analyzer = new AI_Content_Analyzer(); |
| 258 |
$optimization['content_analysis'] = $ai_analyzer->analyze_content($content, $keywords, $options); |
| 259 |
|
| 260 |
// Analyze template compliance |
| 261 |
$optimization['template_compliance'] = $this->analyze_template_compliance($content, $content_type, $keywords); |
| 262 |
|
| 263 |
// Calculate comprehensive optimization score |
| 264 |
$optimization['optimization_score'] = $this->calculate_comprehensive_optimization_score( |
| 265 |
$optimization['content_analysis'], |
| 266 |
$optimization['template_compliance'] |
| 267 |
); |
| 268 |
|
| 269 |
// Generate performance metrics |
| 270 |
$optimization['performance_metrics'] = $this->calculate_performance_metrics( |
| 271 |
$optimization['content_analysis'], |
| 272 |
$optimization['template_compliance'] |
| 273 |
); |
| 274 |
|
| 275 |
// Generate optimization recommendations |
| 276 |
$optimization['recommendations'] = $this->generate_optimization_recommendations( |
| 277 |
$optimization['content_analysis'], |
| 278 |
$optimization['template_compliance'], |
| 279 |
$content_type |
| 280 |
); |
| 281 |
|
| 282 |
// Identify optimization opportunities |
| 283 |
$optimization['optimization_opportunities'] = $this->identify_optimization_opportunities( |
| 284 |
$optimization['content_analysis'], |
| 285 |
$optimization['template_compliance'], |
| 286 |
$content_type |
| 287 |
); |
| 288 |
|
| 289 |
// Generate content suggestions |
| 290 |
$optimization['content_suggestions'] = $this->generate_content_suggestions( |
| 291 |
$optimization['content_analysis'], |
| 292 |
$content_type, |
| 293 |
$keywords |
| 294 |
); |
| 295 |
|
| 296 |
return $optimization; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Get real-time optimization score for content |
| 301 |
* |
| 302 |
* @since 1.0.0 |
| 303 |
* |
| 304 |
* @param string $content Content to score |
| 305 |
* @param array $keywords Target keywords |
| 306 |
* @param string $content_type Content type |
| 307 |
* @return array Real-time scoring results |
| 308 |
*/ |
| 309 |
public function get_realtime_score(string $content, array $keywords = [], string $content_type = 'blog_post'): array { |
| 310 |
$scoring = [ |
| 311 |
'overall_score' => 0, |
| 312 |
'component_scores' => [], |
| 313 |
'quick_wins' => [], |
| 314 |
'critical_issues' => [], |
| 315 |
'score_breakdown' => [], |
| 316 |
'improvement_potential' => 0 |
| 317 |
]; |
| 318 |
|
| 319 |
// Quick content analysis for real-time feedback |
| 320 |
$quick_analysis = $this->perform_quick_analysis($content, $keywords, $content_type); |
| 321 |
|
| 322 |
// Calculate component scores |
| 323 |
$scoring['component_scores'] = $this->calculate_component_scores($quick_analysis, $content_type); |
| 324 |
|
| 325 |
// Calculate overall score |
| 326 |
$scoring['overall_score'] = $this->calculate_weighted_score($scoring['component_scores']); |
| 327 |
|
| 328 |
// Identify quick wins |
| 329 |
$scoring['quick_wins'] = $this->identify_quick_wins($quick_analysis, $content_type); |
| 330 |
|
| 331 |
// Identify critical issues |
| 332 |
$scoring['critical_issues'] = $this->identify_critical_issues($quick_analysis, $content_type); |
| 333 |
|
| 334 |
// Generate score breakdown |
| 335 |
$scoring['score_breakdown'] = $this->generate_score_breakdown($scoring['component_scores']); |
| 336 |
|
| 337 |
// Calculate improvement potential |
| 338 |
$scoring['improvement_potential'] = $this->calculate_improvement_potential($scoring['component_scores']); |
| 339 |
|
| 340 |
return $scoring; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Analyze template compliance for content type |
| 345 |
* |
| 346 |
* @since 1.0.0 |
| 347 |
* |
| 348 |
* @param string $content Content to analyze |
| 349 |
* @param string $content_type Content type |
| 350 |
* @param array $keywords Target keywords |
| 351 |
* @return array Template compliance analysis |
| 352 |
*/ |
| 353 |
public function analyze_template_compliance(string $content, string $content_type, array $keywords = []): array { |
| 354 |
$template = $this->content_templates[$content_type] ?? $this->content_templates['blog_post']; |
| 355 |
|
| 356 |
$compliance = [ |
| 357 |
'content_type' => $content_type, |
| 358 |
'template_score' => 0, |
| 359 |
'word_count_compliance' => [], |
| 360 |
'heading_compliance' => [], |
| 361 |
'keyword_compliance' => [], |
| 362 |
'element_compliance' => [], |
| 363 |
'compliance_percentage' => 0, |
| 364 |
'missing_elements' => [], |
| 365 |
'recommendations' => [] |
| 366 |
]; |
| 367 |
|
| 368 |
// Analyze word count compliance |
| 369 |
$compliance['word_count_compliance'] = $this->analyze_word_count_compliance($content, $template); |
| 370 |
|
| 371 |
// Analyze heading structure compliance |
| 372 |
$compliance['heading_compliance'] = $this->analyze_heading_compliance($content, $template); |
| 373 |
|
| 374 |
// Analyze keyword density compliance |
| 375 |
if (!empty($keywords)) { |
| 376 |
$compliance['keyword_compliance'] = $this->analyze_keyword_compliance($content, $keywords, $template); |
| 377 |
} |
| 378 |
|
| 379 |
// Analyze content elements compliance |
| 380 |
$compliance['element_compliance'] = $this->analyze_element_compliance($content, $template); |
| 381 |
|
| 382 |
// Calculate template score |
| 383 |
$compliance['template_score'] = $this->calculate_template_score($compliance); |
| 384 |
|
| 385 |
// Calculate compliance percentage |
| 386 |
$compliance['compliance_percentage'] = $this->calculate_compliance_percentage($compliance); |
| 387 |
|
| 388 |
// Identify missing elements |
| 389 |
$compliance['missing_elements'] = $this->identify_missing_elements($compliance, $template); |
| 390 |
|
| 391 |
// Generate template recommendations |
| 392 |
$compliance['recommendations'] = $this->generate_template_recommendations($compliance, $template); |
| 393 |
|
| 394 |
return $compliance; |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Track content performance over time |
| 399 |
* |
| 400 |
* @since 1.0.0 |
| 401 |
* |
| 402 |
* @param string $context_type Context type |
| 403 |
* @param int|null $context_id Context ID |
| 404 |
* @param array $metrics Performance metrics |
| 405 |
* @return array Performance tracking results |
| 406 |
*/ |
| 407 |
public function track_performance(string $context_type, ?int $context_id, array $metrics): array { |
| 408 |
$tracking = [ |
| 409 |
'current_metrics' => $metrics, |
| 410 |
'historical_data' => [], |
| 411 |
'performance_trends' => [], |
| 412 |
'improvement_rate' => 0, |
| 413 |
'performance_score' => 0, |
| 414 |
'benchmark_comparison' => [], |
| 415 |
'tracking_timestamp' => current_time('mysql') |
| 416 |
]; |
| 417 |
|
| 418 |
// Get historical performance data |
| 419 |
$tracking['historical_data'] = $this->get_historical_performance($context_type, $context_id); |
| 420 |
|
| 421 |
// Calculate performance trends |
| 422 |
$tracking['performance_trends'] = $this->calculate_performance_trends($tracking['historical_data'], $metrics); |
| 423 |
|
| 424 |
// Calculate improvement rate |
| 425 |
$tracking['improvement_rate'] = $this->calculate_improvement_rate($tracking['performance_trends']); |
| 426 |
|
| 427 |
// Calculate overall performance score |
| 428 |
$tracking['performance_score'] = $this->calculate_performance_score($metrics); |
| 429 |
|
| 430 |
// Compare with benchmarks |
| 431 |
$tracking['benchmark_comparison'] = $this->compare_with_benchmarks($metrics, $context_type); |
| 432 |
|
| 433 |
// Store performance data |
| 434 |
$this->store_performance_data($context_type, $context_id, $tracking); |
| 435 |
|
| 436 |
return $tracking; |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Validate SEO settings (implements interface) |
| 441 |
* |
| 442 |
* @since 1.0.0 |
| 443 |
* |
| 444 |
* @param array $settings Settings array to validate |
| 445 |
* @return array Validation results |
| 446 |
*/ |
| 447 |
public function validate_settings(array $settings): array { |
| 448 |
$validation = [ |
| 449 |
'valid' => true, |
| 450 |
'errors' => [], |
| 451 |
'warnings' => [], |
| 452 |
'suggestions' => [], |
| 453 |
'score' => 100 |
| 454 |
]; |
| 455 |
|
| 456 |
// Validate optimization weights |
| 457 |
if (isset($settings['optimization_weights']) && is_array($settings['optimization_weights'])) { |
| 458 |
$total_weight = array_sum($settings['optimization_weights']); |
| 459 |
if ($total_weight !== 100) { |
| 460 |
$validation['errors'][] = 'Optimization weights must total 100%'; |
| 461 |
$validation['valid'] = false; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
// Validate content templates |
| 466 |
if (isset($settings['content_templates']) && is_array($settings['content_templates'])) { |
| 467 |
foreach ($settings['content_templates'] as $type => $template) { |
| 468 |
if (!isset($template['min_word_count']) || !is_numeric($template['min_word_count'])) { |
| 469 |
$validation['errors'][] = "Invalid min_word_count for content type: {$type}"; |
| 470 |
$validation['valid'] = false; |
| 471 |
} |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
// Validate performance targets |
| 476 |
if (isset($settings['performance_targets']) && is_array($settings['performance_targets'])) { |
| 477 |
foreach ($settings['performance_targets'] as $metric => $target) { |
| 478 |
if (!is_numeric($target) || $target < 0 || $target > 100) { |
| 479 |
$validation['errors'][] = "Invalid performance target for {$metric}: must be 0-100"; |
| 480 |
$validation['valid'] = false; |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
// Validate optimization features |
| 486 |
if (isset($settings['realtime_optimization']) && !is_bool($settings['realtime_optimization'])) { |
| 487 |
$validation['errors'][] = 'Real-time optimization setting must be boolean'; |
| 488 |
$validation['valid'] = false; |
| 489 |
} |
| 490 |
|
| 491 |
if (isset($settings['auto_suggestions']) && !is_bool($settings['auto_suggestions'])) { |
| 492 |
$validation['errors'][] = 'Auto suggestions setting must be boolean'; |
| 493 |
$validation['valid'] = false; |
| 494 |
} |
| 495 |
|
| 496 |
// Calculate validation score |
| 497 |
$validation['score'] = $this->calculate_validation_score($validation); |
| 498 |
|
| 499 |
return $validation; |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* Get output data for frontend rendering (implements interface) |
| 504 |
* |
| 505 |
* @since 1.0.0 |
| 506 |
* |
| 507 |
* @param string $context_type The context type |
| 508 |
* @param int|null $context_id Optional. Context ID |
| 509 |
* @return array Output data ready for frontend rendering |
| 510 |
*/ |
| 511 |
public function get_output_data(string $context_type, ?int $context_id): array { |
| 512 |
$settings = $this->get_settings($context_type, $context_id); |
| 513 |
|
| 514 |
$output = [ |
| 515 |
'optimization_dashboard' => [], |
| 516 |
'realtime_scoring' => [], |
| 517 |
'content_templates' => [], |
| 518 |
'performance_metrics' => [], |
| 519 |
'recommendations' => [], |
| 520 |
'enabled' => $settings['enabled'] ?? true |
| 521 |
]; |
| 522 |
|
| 523 |
if (!$output['enabled']) { |
| 524 |
return $output; |
| 525 |
} |
| 526 |
|
| 527 |
// Get content for optimization |
| 528 |
$content = $this->extract_content_for_optimization($context_type, $context_id); |
| 529 |
$keywords = $this->extract_target_keywords($context_type, $context_id); |
| 530 |
$content_type = $this->determine_content_type($context_type, $context_id); |
| 531 |
|
| 532 |
if (!empty($content)) { |
| 533 |
// Generate optimization dashboard |
| 534 |
$output['optimization_dashboard'] = $this->generate_optimization_dashboard($content, $keywords, $content_type); |
| 535 |
|
| 536 |
// Get real-time scoring |
| 537 |
$output['realtime_scoring'] = $this->get_realtime_score($content, $keywords, $content_type); |
| 538 |
|
| 539 |
// Get content templates |
| 540 |
$output['content_templates'] = $this->get_content_templates_for_type($content_type); |
| 541 |
|
| 542 |
// Get performance metrics |
| 543 |
$output['performance_metrics'] = $this->get_performance_metrics($context_type, $context_id); |
| 544 |
|
| 545 |
// Get optimization recommendations |
| 546 |
$optimization_results = $this->optimize_content($content, $keywords, $content_type); |
| 547 |
$output['recommendations'] = $optimization_results['recommendations'] ?? []; |
| 548 |
|
| 549 |
// Store optimization results |
| 550 |
$this->store_optimization_results($context_type, $context_id, $optimization_results); |
| 551 |
} |
| 552 |
|
| 553 |
return $output; |
| 554 |
} |
| 555 |
|
| 556 |
/** |
| 557 |
* Get default settings for a context type (implements interface) |
| 558 |
* |
| 559 |
* @since 1.0.0 |
| 560 |
* |
| 561 |
* @param string $context_type The context type to get defaults for |
| 562 |
* @return array Default settings array |
| 563 |
*/ |
| 564 |
public function get_default_settings(string $context_type): array { |
| 565 |
$defaults = [ |
| 566 |
'enabled' => true, |
| 567 |
'realtime_optimization' => true, |
| 568 |
'auto_suggestions' => true, |
| 569 |
'performance_tracking' => true, |
| 570 |
'template_compliance_check' => true, |
| 571 |
'optimization_weights' => $this->optimization_weights, |
| 572 |
'performance_targets' => [ |
| 573 |
'optimization_score' => 85, |
| 574 |
'content_quality' => 80, |
| 575 |
'keyword_performance' => 75, |
| 576 |
'readability' => 70, |
| 577 |
'technical_seo' => 90 |
| 578 |
], |
| 579 |
'notification_thresholds' => [ |
| 580 |
'critical' => 40, |
| 581 |
'warning' => 60, |
| 582 |
'success' => 85 |
| 583 |
] |
| 584 |
]; |
| 585 |
|
| 586 |
// Context-specific defaults |
| 587 |
switch ($context_type) { |
| 588 |
case 'post': |
| 589 |
$defaults['default_content_type'] = 'blog_post'; |
| 590 |
$defaults['performance_targets']['optimization_score'] = 85; |
| 591 |
break; |
| 592 |
case 'page': |
| 593 |
$defaults['default_content_type'] = 'landing_page'; |
| 594 |
$defaults['performance_targets']['optimization_score'] = 90; |
| 595 |
break; |
| 596 |
case 'product': |
| 597 |
$defaults['default_content_type'] = 'product_page'; |
| 598 |
$defaults['performance_targets']['optimization_score'] = 80; |
| 599 |
break; |
| 600 |
case 'category': |
| 601 |
$defaults['default_content_type'] = 'category_page'; |
| 602 |
$defaults['performance_targets']['optimization_score'] = 75; |
| 603 |
break; |
| 604 |
} |
| 605 |
|
| 606 |
return $defaults; |
| 607 |
} |
| 608 |
|
| 609 |
/** |
| 610 |
* Get settings schema definition (implements interface) |
| 611 |
* |
| 612 |
* @since 1.0.0 |
| 613 |
* |
| 614 |
* @param string $context_type The context type to get schema for |
| 615 |
* @return array Settings schema definition |
| 616 |
*/ |
| 617 |
public function get_settings_schema(string $context_type): array { |
| 618 |
return [ |
| 619 |
'enabled' => [ |
| 620 |
'type' => 'boolean', |
| 621 |
'title' => 'Enable Content Optimization', |
| 622 |
'description' => 'Enable content optimization engine and recommendations', |
| 623 |
'default' => true |
| 624 |
], |
| 625 |
'realtime_optimization' => [ |
| 626 |
'type' => 'boolean', |
| 627 |
'title' => 'Real-time Optimization', |
| 628 |
'description' => 'Enable real-time content scoring and feedback', |
| 629 |
'default' => true |
| 630 |
], |
| 631 |
'auto_suggestions' => [ |
| 632 |
'type' => 'boolean', |
| 633 |
'title' => 'Auto Suggestions', |
| 634 |
'description' => 'Automatically generate content improvement suggestions', |
| 635 |
'default' => true |
| 636 |
], |
| 637 |
'performance_tracking' => [ |
| 638 |
'type' => 'boolean', |
| 639 |
'title' => 'Performance Tracking', |
| 640 |
'description' => 'Track content performance metrics over time', |
| 641 |
'default' => true |
| 642 |
], |
| 643 |
'template_compliance_check' => [ |
| 644 |
'type' => 'boolean', |
| 645 |
'title' => 'Template Compliance Check', |
| 646 |
'description' => 'Check content against SEO templates and best practices', |
| 647 |
'default' => true |
| 648 |
], |
| 649 |
'optimization_score_target' => [ |
| 650 |
'type' => 'integer', |
| 651 |
'title' => 'Optimization Score Target', |
| 652 |
'description' => 'Target optimization score (0-100)', |
| 653 |
'minimum' => 0, |
| 654 |
'maximum' => 100, |
| 655 |
'default' => 85 |
| 656 |
], |
| 657 |
'content_quality_target' => [ |
| 658 |
'type' => 'integer', |
| 659 |
'title' => 'Content Quality Target', |
| 660 |
'description' => 'Target content quality score (0-100)', |
| 661 |
'minimum' => 0, |
| 662 |
'maximum' => 100, |
| 663 |
'default' => 80 |
| 664 |
], |
| 665 |
'default_content_type' => [ |
| 666 |
'type' => 'string', |
| 667 |
'title' => 'Default Content Type', |
| 668 |
'description' => 'Default content type for optimization templates', |
| 669 |
'enum' => ['blog_post', 'product_page', 'landing_page', 'category_page'], |
| 670 |
'default' => 'blog_post' |
| 671 |
] |
| 672 |
]; |
| 673 |
} |
| 674 |
|
| 675 |
/** |
| 676 |
* Calculate comprehensive optimization score |
| 677 |
* |
| 678 |
* @since 1.0.0 |
| 679 |
* |
| 680 |
* @param array $content_analysis AI content analysis results |
| 681 |
* @param array $template_compliance Template compliance results |
| 682 |
* @return int Comprehensive optimization score (0-100) |
| 683 |
*/ |
| 684 |
private function calculate_comprehensive_optimization_score(array $content_analysis, array $template_compliance): int { |
| 685 |
$scores = []; |
| 686 |
|
| 687 |
// Content quality score (25% weight) |
| 688 |
if (!empty($content_analysis['optimization_score'])) { |
| 689 |
$scores['content_quality'] = $content_analysis['optimization_score'] * ($this->optimization_weights['content_quality'] / 100); |
| 690 |
} |
| 691 |
|
| 692 |
// Keyword optimization score (20% weight) |
| 693 |
if (!empty($content_analysis['keyword_analysis']['optimization_score'])) { |
| 694 |
$scores['keyword_optimization'] = $content_analysis['keyword_analysis']['optimization_score'] * ($this->optimization_weights['keyword_optimization'] / 100); |
| 695 |
} |
| 696 |
|
| 697 |
// Readability score (15% weight) |
| 698 |
if (!empty($content_analysis['readability']['readability_score'])) { |
| 699 |
$scores['readability'] = $content_analysis['readability']['readability_score'] * ($this->optimization_weights['readability'] / 100); |
| 700 |
} |
| 701 |
|
| 702 |
// Structure score (15% weight) |
| 703 |
if (!empty($content_analysis['structure_analysis']['structure_score'])) { |
| 704 |
$scores['structure'] = $content_analysis['structure_analysis']['structure_score'] * ($this->optimization_weights['structure'] / 100); |
| 705 |
} |
| 706 |
|
| 707 |
// Template compliance score (10% weight) |
| 708 |
if (!empty($template_compliance['template_score'])) { |
| 709 |
$scores['technical_seo'] = $template_compliance['template_score'] * ($this->optimization_weights['technical_seo'] / 100); |
| 710 |
} |
| 711 |
|
| 712 |
// User experience score (10% weight) |
| 713 |
$ux_score = $this->calculate_user_experience_score($content_analysis, $template_compliance); |
| 714 |
$scores['user_experience'] = $ux_score * ($this->optimization_weights['user_experience'] / 100); |
| 715 |
|
| 716 |
// Semantic relevance score (5% weight) |
| 717 |
if (!empty($content_analysis['semantic_analysis']['relevance_score'])) { |
| 718 |
$scores['semantic_relevance'] = $content_analysis['semantic_analysis']['relevance_score'] * ($this->optimization_weights['semantic_relevance'] / 100); |
| 719 |
} |
| 720 |
|
| 721 |
return (int) round(array_sum($scores)); |
| 722 |
} |
| 723 |
|
| 724 |
/** |
| 725 |
* Perform quick analysis for real-time feedback |
| 726 |
* |
| 727 |
* @since 1.0.0 |
| 728 |
* |
| 729 |
* @param string $content Content to analyze |
| 730 |
* @param array $keywords Target keywords |
| 731 |
* @param string $content_type Content type |
| 732 |
* @return array Quick analysis results |
| 733 |
*/ |
| 734 |
private function perform_quick_analysis(string $content, array $keywords, string $content_type): array { |
| 735 |
$analysis = [ |
| 736 |
'word_count' => str_word_count(wp_strip_all_tags($content)), |
| 737 |
'character_count' => strlen(wp_strip_all_tags($content)), |
| 738 |
'paragraph_count' => count(preg_split('/\n\s*\n/', trim($content), -1, PREG_SPLIT_NO_EMPTY)), |
| 739 |
'heading_count' => preg_match_all('/<h[1-6][^>]*>/i', $content), |
| 740 |
'image_count' => preg_match_all('/<img[^>]*>/i', $content), |
| 741 |
'link_count' => preg_match_all('/<a[^>]*href/i', $content), |
| 742 |
'keyword_density' => [], |
| 743 |
'readability_estimate' => 0 |
| 744 |
]; |
| 745 |
|
| 746 |
// Quick keyword density calculation |
| 747 |
if (!empty($keywords)) { |
| 748 |
$content_lower = strtolower(wp_strip_all_tags($content)); |
| 749 |
foreach ($keywords as $keyword) { |
| 750 |
$keyword_lower = strtolower($keyword); |
| 751 |
$occurrences = substr_count($content_lower, $keyword_lower); |
| 752 |
$analysis['keyword_density'][$keyword] = $analysis['word_count'] > 0 ? |
| 753 |
($occurrences / $analysis['word_count']) * 100 : 0; |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
// Quick readability estimate |
| 758 |
if ($analysis['word_count'] > 0 && $analysis['paragraph_count'] > 0) { |
| 759 |
$avg_words_per_paragraph = $analysis['word_count'] / $analysis['paragraph_count']; |
| 760 |
$analysis['readability_estimate'] = max(0, min(100, 100 - ($avg_words_per_paragraph * 2))); |
| 761 |
} |
| 762 |
|
| 763 |
return $analysis; |
| 764 |
} |
| 765 |
|
| 766 |
/** |
| 767 |
* Calculate component scores for real-time feedback |
| 768 |
* |
| 769 |
* @since 1.0.0 |
| 770 |
* |
| 771 |
* @param array $quick_analysis Quick analysis results |
| 772 |
* @param string $content_type Content type |
| 773 |
* @return array Component scores |
| 774 |
*/ |
| 775 |
private function calculate_component_scores(array $quick_analysis, string $content_type): array { |
| 776 |
$template = $this->content_templates[$content_type] ?? $this->content_templates['blog_post']; |
| 777 |
$scores = []; |
| 778 |
|
| 779 |
// Word count score |
| 780 |
$word_count = $quick_analysis['word_count']; |
| 781 |
if ($word_count >= $template['optimal_word_count']) { |
| 782 |
$scores['word_count'] = 100; |
| 783 |
} elseif ($word_count >= $template['min_word_count']) { |
| 784 |
$scores['word_count'] = 50 + (($word_count - $template['min_word_count']) / |
| 785 |
($template['optimal_word_count'] - $template['min_word_count'])) * 50; |
| 786 |
} else { |
| 787 |
$scores['word_count'] = ($word_count / $template['min_word_count']) * 50; |
| 788 |
} |
| 789 |
|
| 790 |
// Heading structure score |
| 791 |
$heading_count = $quick_analysis['heading_count']; |
| 792 |
$min_headings = $template['heading_structure']['h2_min'] ?? 2; |
| 793 |
$scores['headings'] = min(100, ($heading_count / $min_headings) * 100); |
| 794 |
|
| 795 |
// Keyword density score |
| 796 |
$keyword_scores = []; |
| 797 |
foreach ($quick_analysis['keyword_density'] as $keyword => $density) { |
| 798 |
if ($density >= 1.0 && $density <= 3.0) { |
| 799 |
$keyword_scores[] = 100; |
| 800 |
} elseif ($density >= 0.5 && $density <= 5.0) { |
| 801 |
$keyword_scores[] = 70; |
| 802 |
} else { |
| 803 |
$keyword_scores[] = 30; |
| 804 |
} |
| 805 |
} |
| 806 |
$scores['keywords'] = !empty($keyword_scores) ? array_sum($keyword_scores) / count($keyword_scores) : 50; |
| 807 |
|
| 808 |
// Readability score |
| 809 |
$scores['readability'] = $quick_analysis['readability_estimate']; |
| 810 |
|
| 811 |
// Content elements score |
| 812 |
$element_score = 0; |
| 813 |
if ($quick_analysis['image_count'] > 0) { |
| 814 |
$element_score += 25; |
| 815 |
} |
| 816 |
if ($quick_analysis['link_count'] > 0) { |
| 817 |
$element_score += 25; |
| 818 |
} |
| 819 |
if ($quick_analysis['paragraph_count'] >= 3) { |
| 820 |
$element_score += 25; |
| 821 |
} |
| 822 |
if ($quick_analysis['heading_count'] >= 2) { |
| 823 |
$element_score += 25; |
| 824 |
} |
| 825 |
$scores['elements'] = $element_score; |
| 826 |
|
| 827 |
return $scores; |
| 828 |
} |
| 829 |
|
| 830 |
/** |
| 831 |
* Calculate weighted score from component scores |
| 832 |
* |
| 833 |
* @since 1.0.0 |
| 834 |
* |
| 835 |
* @param array $component_scores Component scores |
| 836 |
* @return int Weighted overall score |
| 837 |
*/ |
| 838 |
private function calculate_weighted_score(array $component_scores): int { |
| 839 |
$weights = [ |
| 840 |
'word_count' => 20, |
| 841 |
'headings' => 15, |
| 842 |
'keywords' => 25, |
| 843 |
'readability' => 20, |
| 844 |
'elements' => 20 |
| 845 |
]; |
| 846 |
|
| 847 |
$weighted_sum = 0; |
| 848 |
$total_weight = 0; |
| 849 |
|
| 850 |
foreach ($component_scores as $component => $score) { |
| 851 |
$weight = $weights[$component] ?? 0; |
| 852 |
$weighted_sum += $score * $weight; |
| 853 |
$total_weight += $weight; |
| 854 |
} |
| 855 |
|
| 856 |
return $total_weight > 0 ? (int) round($weighted_sum / $total_weight) : 0; |
| 857 |
} |
| 858 |
|
| 859 |
/** |
| 860 |
* Identify quick wins for optimization |
| 861 |
* |
| 862 |
* @since 1.0.0 |
| 863 |
* |
| 864 |
* @param array $quick_analysis Quick analysis results |
| 865 |
* @param string $content_type Content type |
| 866 |
* @return array Quick win recommendations |
| 867 |
*/ |
| 868 |
private function identify_quick_wins(array $quick_analysis, string $content_type): array { |
| 869 |
$quick_wins = []; |
| 870 |
$template = $this->content_templates[$content_type] ?? $this->content_templates['blog_post']; |
| 871 |
|
| 872 |
// Word count quick wins |
| 873 |
if ($quick_analysis['word_count'] < $template['min_word_count']) { |
| 874 |
$needed_words = $template['min_word_count'] - $quick_analysis['word_count']; |
| 875 |
$quick_wins[] = [ |
| 876 |
'type' => 'word_count', |
| 877 |
'priority' => 'high', |
| 878 |
'effort' => 'medium', |
| 879 |
'impact' => 'high', |
| 880 |
'message' => "Add {$needed_words} more words to reach minimum length", |
| 881 |
'action' => 'Expand content with relevant information' |
| 882 |
]; |
| 883 |
} |
| 884 |
|
| 885 |
// Heading quick wins |
| 886 |
if ($quick_analysis['heading_count'] < 2) { |
| 887 |
$quick_wins[] = [ |
| 888 |
'type' => 'headings', |
| 889 |
'priority' => 'medium', |
| 890 |
'effort' => 'low', |
| 891 |
'impact' => 'medium', |
| 892 |
'message' => 'Add more heading tags to improve structure', |
| 893 |
'action' => 'Break content into sections with H2 and H3 tags' |
| 894 |
]; |
| 895 |
} |
| 896 |
|
| 897 |
// Image quick wins |
| 898 |
if ($quick_analysis['image_count'] === 0) { |
| 899 |
$quick_wins[] = [ |
| 900 |
'type' => 'images', |
| 901 |
'priority' => 'medium', |
| 902 |
'effort' => 'low', |
| 903 |
'impact' => 'medium', |
| 904 |
'message' => 'Add images to improve engagement', |
| 905 |
'action' => 'Include relevant images with alt text' |
| 906 |
]; |
| 907 |
} |
| 908 |
|
| 909 |
// Keyword density quick wins |
| 910 |
foreach ($quick_analysis['keyword_density'] as $keyword => $density) { |
| 911 |
if ($density < 0.5) { |
| 912 |
$quick_wins[] = [ |
| 913 |
'type' => 'keyword_density', |
| 914 |
'priority' => 'high', |
| 915 |
'effort' => 'low', |
| 916 |
'impact' => 'high', |
| 917 |
'message' => "Increase usage of keyword '{$keyword}'", |
| 918 |
'action' => 'Add keyword naturally throughout content' |
| 919 |
]; |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
return $quick_wins; |
| 924 |
} |
| 925 |
|
| 926 |
/** |
| 927 |
* Identify critical issues requiring immediate attention |
| 928 |
* |
| 929 |
* @since 1.0.0 |
| 930 |
* |
| 931 |
* @param array $quick_analysis Quick analysis results |
| 932 |
* @param string $content_type Content type |
| 933 |
* @return array Critical issues |
| 934 |
*/ |
| 935 |
private function identify_critical_issues(array $quick_analysis, string $content_type): array { |
| 936 |
$critical_issues = []; |
| 937 |
$template = $this->content_templates[$content_type] ?? $this->content_templates['blog_post']; |
| 938 |
|
| 939 |
// Critical word count issues |
| 940 |
if ($quick_analysis['word_count'] < ($template['min_word_count'] * 0.5)) { |
| 941 |
$critical_issues[] = [ |
| 942 |
'type' => 'word_count', |
| 943 |
'severity' => 'critical', |
| 944 |
'message' => 'Content is severely under the minimum word count', |
| 945 |
'impact' => 'SEO performance will be significantly impacted', |
| 946 |
'action' => 'Substantially expand content before publishing' |
| 947 |
]; |
| 948 |
} |
| 949 |
|
| 950 |
// Critical keyword issues |
| 951 |
foreach ($quick_analysis['keyword_density'] as $keyword => $density) { |
| 952 |
if ($density > 5.0) { |
| 953 |
$critical_issues[] = [ |
| 954 |
'type' => 'keyword_stuffing', |
| 955 |
'severity' => 'critical', |
| 956 |
'message' => "Keyword '{$keyword}' may be over-optimized (keyword stuffing)", |
| 957 |
'impact' => 'Search engines may penalize this content', |
| 958 |
'action' => 'Reduce keyword density to 1-3%' |
| 959 |
]; |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
// Critical structure issues |
| 964 |
if ($quick_analysis['heading_count'] === 0) { |
| 965 |
$critical_issues[] = [ |
| 966 |
'type' => 'no_headings', |
| 967 |
'severity' => 'critical', |
| 968 |
'message' => 'Content has no heading structure', |
| 969 |
'impact' => 'Poor readability and SEO performance', |
| 970 |
'action' => 'Add proper heading hierarchy (H1, H2, H3)' |
| 971 |
]; |
| 972 |
} |
| 973 |
|
| 974 |
return $critical_issues; |
| 975 |
} |
| 976 |
|
| 977 |
/** |
| 978 |
* Generate score breakdown for visualization |
| 979 |
* |
| 980 |
* @since 1.0.0 |
| 981 |
* |
| 982 |
* @param array $component_scores Component scores |
| 983 |
* @return array Score breakdown data |
| 984 |
*/ |
| 985 |
private function generate_score_breakdown(array $component_scores): array { |
| 986 |
$breakdown = []; |
| 987 |
|
| 988 |
foreach ($component_scores as $component => $score) { |
| 989 |
$breakdown[] = [ |
| 990 |
'component' => $component, |
| 991 |
'score' => (int) round($score), |
| 992 |
'status' => $this->get_score_status($score), |
| 993 |
'color' => $this->get_score_color($score), |
| 994 |
'description' => $this->get_component_description($component) |
| 995 |
]; |
| 996 |
} |
| 997 |
|
| 998 |
return $breakdown; |
| 999 |
} |
| 1000 |
|
| 1001 |
/** |
| 1002 |
* Calculate improvement potential |
| 1003 |
* |
| 1004 |
* @since 1.0.0 |
| 1005 |
* |
| 1006 |
* @param array $component_scores Component scores |
| 1007 |
* @return int Improvement potential percentage |
| 1008 |
*/ |
| 1009 |
private function calculate_improvement_potential(array $component_scores): int { |
| 1010 |
$total_possible = count($component_scores) * 100; |
| 1011 |
$current_total = array_sum($component_scores); |
| 1012 |
$potential = $total_possible - $current_total; |
| 1013 |
|
| 1014 |
return (int) round(($potential / $total_possible) * 100); |
| 1015 |
} |
| 1016 |
|
| 1017 |
/** |
| 1018 |
* Extract content for optimization |
| 1019 |
* |
| 1020 |
* @since 1.0.0 |
| 1021 |
* |
| 1022 |
* @param string $context_type Context type |
| 1023 |
* @param int|null $context_id Context ID |
| 1024 |
* @return string Content to optimize |
| 1025 |
*/ |
| 1026 |
private function extract_content_for_optimization(string $context_type, ?int $context_id): string { |
| 1027 |
$content = ''; |
| 1028 |
|
| 1029 |
switch ($context_type) { |
| 1030 |
case 'post': |
| 1031 |
case 'page': |
| 1032 |
case 'product': |
| 1033 |
if ($context_id) { |
| 1034 |
$post = get_post($context_id); |
| 1035 |
if ($post) { |
| 1036 |
$content = $post->post_title . ' ' . $post->post_content; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
break; |
| 1040 |
case 'site': |
| 1041 |
// Get homepage content |
| 1042 |
$homepage_id = get_option('page_on_front'); |
| 1043 |
if ($homepage_id && $homepage_id !== '0') { |
| 1044 |
$homepage = get_post($homepage_id); |
| 1045 |
if ($homepage) { |
| 1046 |
$content = $homepage->post_title . ' ' . $homepage->post_content; |
| 1047 |
} |
| 1048 |
} else { |
| 1049 |
// Get recent posts for blog homepage |
| 1050 |
$recent_posts = get_posts(['numberposts' => 3]); |
| 1051 |
$content_parts = []; |
| 1052 |
foreach ($recent_posts as $post) { |
| 1053 |
$content_parts[] = $post->post_title . ' ' . wp_trim_words($post->post_content, 100); |
| 1054 |
} |
| 1055 |
$content = implode(' ', $content_parts); |
| 1056 |
} |
| 1057 |
break; |
| 1058 |
} |
| 1059 |
|
| 1060 |
return $content; |
| 1061 |
} |
| 1062 |
|
| 1063 |
/** |
| 1064 |
* Extract target keywords for optimization |
| 1065 |
* |
| 1066 |
* @since 1.0.0 |
| 1067 |
* |
| 1068 |
* @param string $context_type Context type |
| 1069 |
* @param int|null $context_id Context ID |
| 1070 |
* @return array Target keywords |
| 1071 |
*/ |
| 1072 |
private function extract_target_keywords(string $context_type, ?int $context_id): array { |
| 1073 |
// This would typically get keywords from the database |
| 1074 |
// For now, return some default keywords based on content |
| 1075 |
$keywords = []; |
| 1076 |
|
| 1077 |
$content = $this->extract_content_for_optimization($context_type, $context_id); |
| 1078 |
if (!empty($content)) { |
| 1079 |
// Simple keyword extraction from title and content |
| 1080 |
$words = str_word_count(strtolower(wp_strip_all_tags($content)), 1); |
| 1081 |
$word_counts = array_count_values($words); |
| 1082 |
|
| 1083 |
// Remove common stop words |
| 1084 |
$stop_words = ['the', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by']; |
| 1085 |
foreach ($stop_words as $stop_word) { |
| 1086 |
unset($word_counts[$stop_word]); |
| 1087 |
} |
| 1088 |
|
| 1089 |
arsort($word_counts); |
| 1090 |
$keywords = array_slice(array_keys($word_counts), 0, 5); |
| 1091 |
} |
| 1092 |
|
| 1093 |
return $keywords; |
| 1094 |
} |
| 1095 |
|
| 1096 |
/** |
| 1097 |
* Determine content type from context |
| 1098 |
* |
| 1099 |
* @since 1.0.0 |
| 1100 |
* |
| 1101 |
* @param string $context_type Context type |
| 1102 |
* @param int|null $context_id Context ID |
| 1103 |
* @return string Content type |
| 1104 |
*/ |
| 1105 |
private function determine_content_type(string $context_type, ?int $context_id): string { |
| 1106 |
switch ($context_type) { |
| 1107 |
case 'post': |
| 1108 |
return 'blog_post'; |
| 1109 |
case 'page': |
| 1110 |
return 'landing_page'; |
| 1111 |
case 'product': |
| 1112 |
return 'product_page'; |
| 1113 |
case 'category': |
| 1114 |
return 'category_page'; |
| 1115 |
default: |
| 1116 |
return 'blog_post'; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
/** |
| 1121 |
* Store optimization results in database |
| 1122 |
* |
| 1123 |
* @since 1.0.0 |
| 1124 |
* |
| 1125 |
* @param string $context_type Context type |
| 1126 |
* @param int|null $context_id Context ID |
| 1127 |
* @param array $results Optimization results |
| 1128 |
* @return bool Success status |
| 1129 |
*/ |
| 1130 |
private function store_optimization_results(string $context_type, ?int $context_id, array $results): bool { |
| 1131 |
global $wpdb; |
| 1132 |
|
| 1133 |
$table_name = $wpdb->prefix . 'thinkrank_seo_analysis'; |
| 1134 |
|
| 1135 |
$data = [ |
| 1136 |
'context_type' => $context_type, |
| 1137 |
'context_id' => $context_id, |
| 1138 |
'analysis_type' => 'content_optimization', |
| 1139 |
'analysis_data' => wp_json_encode($results), |
| 1140 |
'score' => $results['optimization_score'] ?? 0, |
| 1141 |
'status' => 'completed', |
| 1142 |
'recommendations' => wp_json_encode($results['recommendations'] ?? []), |
| 1143 |
'analyzed_by' => get_current_user_id() |
| 1144 |
]; |
| 1145 |
|
| 1146 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Optimization results storage requires direct database access |
| 1147 |
$result = $wpdb->insert($table_name, $data); |
| 1148 |
|
| 1149 |
return $result !== false; |
| 1150 |
} |
| 1151 |
|
| 1152 |
/** |
| 1153 |
* Calculate validation score |
| 1154 |
* |
| 1155 |
* @since 1.0.0 |
| 1156 |
* |
| 1157 |
* @param array $validation Validation results |
| 1158 |
* @return int Score (0-100) |
| 1159 |
*/ |
| 1160 |
private function calculate_validation_score(array $validation): int { |
| 1161 |
$score = 100; |
| 1162 |
$score -= count($validation['errors']) * 20; |
| 1163 |
$score -= count($validation['warnings']) * 10; |
| 1164 |
$score -= count($validation['suggestions']) * 5; |
| 1165 |
|
| 1166 |
return max(0, $score); |
| 1167 |
} |
| 1168 |
|
| 1169 |
/** |
| 1170 |
* Get score status based on score value |
| 1171 |
* |
| 1172 |
* @since 1.0.0 |
| 1173 |
* |
| 1174 |
* @param float $score Score value |
| 1175 |
* @return string Status |
| 1176 |
*/ |
| 1177 |
private function get_score_status(float $score): string { |
| 1178 |
if ($score >= 85) { |
| 1179 |
return 'excellent'; |
| 1180 |
} elseif ($score >= 70) { |
| 1181 |
return 'good'; |
| 1182 |
} elseif ($score >= 50) { |
| 1183 |
return 'fair'; |
| 1184 |
} else { |
| 1185 |
return 'poor'; |
| 1186 |
} |
| 1187 |
} |
| 1188 |
|
| 1189 |
/** |
| 1190 |
* Get score color based on score value |
| 1191 |
* |
| 1192 |
* @since 1.0.0 |
| 1193 |
* |
| 1194 |
* @param float $score Score value |
| 1195 |
* @return string Color code |
| 1196 |
*/ |
| 1197 |
private function get_score_color(float $score): string { |
| 1198 |
if ($score >= 85) { |
| 1199 |
return '#28a745'; // Green |
| 1200 |
} elseif ($score >= 70) { |
| 1201 |
return '#20c997'; // Teal |
| 1202 |
} elseif ($score >= 50) { |
| 1203 |
return '#ffc107'; // Yellow |
| 1204 |
} else { |
| 1205 |
return '#dc3545'; // Red |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
/** |
| 1210 |
* Get component description |
| 1211 |
* |
| 1212 |
* @since 1.0.0 |
| 1213 |
* |
| 1214 |
* @param string $component Component name |
| 1215 |
* @return string Description |
| 1216 |
*/ |
| 1217 |
private function get_component_description(string $component): string { |
| 1218 |
$descriptions = [ |
| 1219 |
'word_count' => 'Content length and depth', |
| 1220 |
'headings' => 'Heading structure and organization', |
| 1221 |
'keywords' => 'Keyword optimization and density', |
| 1222 |
'readability' => 'Reading ease and comprehension', |
| 1223 |
'elements' => 'Content elements (images, links, etc.)' |
| 1224 |
]; |
| 1225 |
|
| 1226 |
return $descriptions[$component] ?? 'Content optimization component'; |
| 1227 |
} |
| 1228 |
|
| 1229 |
/** |
| 1230 |
* Simple implementations for helper methods |
| 1231 |
* These would be enhanced with more sophisticated algorithms in production |
| 1232 |
*/ |
| 1233 |
|
| 1234 |
private function analyze_word_count_compliance(string $content, array $template): array { |
| 1235 |
$word_count = str_word_count(wp_strip_all_tags($content)); |
| 1236 |
$min_words = $template['min_word_count']; |
| 1237 |
$optimal_words = $template['optimal_word_count']; |
| 1238 |
|
| 1239 |
$compliance_score = 0; |
| 1240 |
if ($word_count >= $optimal_words) { |
| 1241 |
$compliance_score = 100; |
| 1242 |
} elseif ($word_count >= $min_words) { |
| 1243 |
$compliance_score = 50 + (($word_count - $min_words) / ($optimal_words - $min_words)) * 50; |
| 1244 |
} else { |
| 1245 |
$compliance_score = ($word_count / $min_words) * 50; |
| 1246 |
} |
| 1247 |
|
| 1248 |
return [ |
| 1249 |
'current_count' => $word_count, |
| 1250 |
'min_required' => $min_words, |
| 1251 |
'optimal_count' => $optimal_words, |
| 1252 |
'compliance_score' => (int) round($compliance_score), |
| 1253 |
'status' => $word_count >= $min_words ? 'compliant' : 'non_compliant' |
| 1254 |
]; |
| 1255 |
} |
| 1256 |
|
| 1257 |
private function analyze_heading_compliance(string $content, array $template): array { |
| 1258 |
$heading_counts = []; |
| 1259 |
for ($i = 1; $i <= 6; $i++) { |
| 1260 |
$heading_counts["h{$i}"] = preg_match_all("/<h{$i}[^>]*>/i", $content); |
| 1261 |
} |
| 1262 |
|
| 1263 |
$compliance_score = 100; |
| 1264 |
$issues = []; |
| 1265 |
|
| 1266 |
// Check H1 count |
| 1267 |
if ($heading_counts['h1'] !== 1) { |
| 1268 |
$compliance_score -= 30; |
| 1269 |
$issues[] = 'Should have exactly one H1 tag'; |
| 1270 |
} |
| 1271 |
|
| 1272 |
// Check H2 minimum |
| 1273 |
$h2_min = $template['heading_structure']['h2_min'] ?? 2; |
| 1274 |
if ($heading_counts['h2'] < $h2_min) { |
| 1275 |
$compliance_score -= 20; |
| 1276 |
$issues[] = "Should have at least {$h2_min} H2 tags"; |
| 1277 |
} |
| 1278 |
|
| 1279 |
return [ |
| 1280 |
'heading_counts' => $heading_counts, |
| 1281 |
'compliance_score' => max(0, $compliance_score), |
| 1282 |
'issues' => $issues, |
| 1283 |
'status' => empty($issues) ? 'compliant' : 'non_compliant' |
| 1284 |
]; |
| 1285 |
} |
| 1286 |
|
| 1287 |
private function analyze_keyword_compliance(string $content, array $keywords, array $template): array { |
| 1288 |
$content_lower = strtolower(wp_strip_all_tags($content)); |
| 1289 |
$word_count = str_word_count($content_lower); |
| 1290 |
$compliance_data = []; |
| 1291 |
|
| 1292 |
foreach ($keywords as $index => $keyword) { |
| 1293 |
$keyword_lower = strtolower($keyword); |
| 1294 |
$occurrences = substr_count($content_lower, $keyword_lower); |
| 1295 |
$density = $word_count > 0 ? ($occurrences / $word_count) * 100 : 0; |
| 1296 |
|
| 1297 |
$keyword_type = $index === 0 ? 'primary' : 'secondary'; |
| 1298 |
$target_range = $template['keyword_density'][$keyword_type] ?? [1.0, 3.0]; |
| 1299 |
|
| 1300 |
$compliance_data[] = [ |
| 1301 |
'keyword' => $keyword, |
| 1302 |
'type' => $keyword_type, |
| 1303 |
'density' => round($density, 2), |
| 1304 |
'target_range' => $target_range, |
| 1305 |
'compliant' => $density >= $target_range[0] && $density <= $target_range[1] |
| 1306 |
]; |
| 1307 |
} |
| 1308 |
|
| 1309 |
return $compliance_data; |
| 1310 |
} |
| 1311 |
|
| 1312 |
private function analyze_element_compliance(string $content, array $template): array { |
| 1313 |
$elements = $template['content_elements'] ?? []; |
| 1314 |
$compliance_data = []; |
| 1315 |
|
| 1316 |
// Check images |
| 1317 |
if (isset($elements['images'])) { |
| 1318 |
$image_count = preg_match_all('/<img[^>]*>/i', $content); |
| 1319 |
$min_images = $elements['images']['min'] ?? 1; |
| 1320 |
$compliance_data['images'] = [ |
| 1321 |
'current' => $image_count, |
| 1322 |
'required' => $min_images, |
| 1323 |
'compliant' => $image_count >= $min_images |
| 1324 |
]; |
| 1325 |
} |
| 1326 |
|
| 1327 |
// Check internal links |
| 1328 |
if (isset($elements['internal_links'])) { |
| 1329 |
$link_count = preg_match_all('/<a[^>]*href/i', $content); |
| 1330 |
$min_links = $elements['internal_links']['min'] ?? 2; |
| 1331 |
$compliance_data['internal_links'] = [ |
| 1332 |
'current' => $link_count, |
| 1333 |
'required' => $min_links, |
| 1334 |
'compliant' => $link_count >= $min_links |
| 1335 |
]; |
| 1336 |
} |
| 1337 |
|
| 1338 |
return $compliance_data; |
| 1339 |
} |
| 1340 |
|
| 1341 |
private function calculate_template_score(array $compliance): int { |
| 1342 |
$scores = []; |
| 1343 |
|
| 1344 |
if (!empty($compliance['word_count_compliance']['compliance_score'])) { |
| 1345 |
$scores[] = $compliance['word_count_compliance']['compliance_score']; |
| 1346 |
} |
| 1347 |
|
| 1348 |
if (!empty($compliance['heading_compliance']['compliance_score'])) { |
| 1349 |
$scores[] = $compliance['heading_compliance']['compliance_score']; |
| 1350 |
} |
| 1351 |
|
| 1352 |
// Add other compliance scores |
| 1353 |
|
| 1354 |
return !empty($scores) ? (int) round(array_sum($scores) / count($scores)) : 0; |
| 1355 |
} |
| 1356 |
|
| 1357 |
private function calculate_compliance_percentage(array $compliance): int { |
| 1358 |
$total_checks = 0; |
| 1359 |
$passed_checks = 0; |
| 1360 |
|
| 1361 |
// Count word count compliance |
| 1362 |
if (!empty($compliance['word_count_compliance'])) { |
| 1363 |
$total_checks++; |
| 1364 |
if ($compliance['word_count_compliance']['status'] === 'compliant') { |
| 1365 |
$passed_checks++; |
| 1366 |
} |
| 1367 |
} |
| 1368 |
|
| 1369 |
// Count heading compliance |
| 1370 |
if (!empty($compliance['heading_compliance'])) { |
| 1371 |
$total_checks++; |
| 1372 |
if ($compliance['heading_compliance']['status'] === 'compliant') { |
| 1373 |
$passed_checks++; |
| 1374 |
} |
| 1375 |
} |
| 1376 |
|
| 1377 |
return $total_checks > 0 ? (int) round(($passed_checks / $total_checks) * 100) : 0; |
| 1378 |
} |
| 1379 |
|
| 1380 |
private function identify_missing_elements(array $compliance, array $template): array { |
| 1381 |
$missing = []; |
| 1382 |
|
| 1383 |
// Check for missing content elements |
| 1384 |
if (!empty($compliance['element_compliance'])) { |
| 1385 |
foreach ($compliance['element_compliance'] as $element => $data) { |
| 1386 |
if (!$data['compliant']) { |
| 1387 |
$missing[] = [ |
| 1388 |
'element' => $element, |
| 1389 |
'current' => $data['current'], |
| 1390 |
'required' => $data['required'] |
| 1391 |
]; |
| 1392 |
} |
| 1393 |
} |
| 1394 |
} |
| 1395 |
|
| 1396 |
return $missing; |
| 1397 |
} |
| 1398 |
|
| 1399 |
private function generate_template_recommendations(array $compliance, array $template): array { |
| 1400 |
$recommendations = []; |
| 1401 |
|
| 1402 |
// Word count recommendations |
| 1403 |
if (!empty($compliance['word_count_compliance']) && |
| 1404 |
$compliance['word_count_compliance']['status'] === 'non_compliant') { |
| 1405 |
$needed = $compliance['word_count_compliance']['min_required'] - |
| 1406 |
$compliance['word_count_compliance']['current_count']; |
| 1407 |
$recommendations[] = [ |
| 1408 |
'type' => 'word_count', |
| 1409 |
'priority' => 'high', |
| 1410 |
'message' => "Add {$needed} more words to meet minimum requirements", |
| 1411 |
'action' => 'Expand content with relevant information' |
| 1412 |
]; |
| 1413 |
} |
| 1414 |
|
| 1415 |
// Heading recommendations |
| 1416 |
if (!empty($compliance['heading_compliance']['issues'])) { |
| 1417 |
foreach ($compliance['heading_compliance']['issues'] as $issue) { |
| 1418 |
$recommendations[] = [ |
| 1419 |
'type' => 'headings', |
| 1420 |
'priority' => 'medium', |
| 1421 |
'message' => $issue, |
| 1422 |
'action' => 'Improve heading structure' |
| 1423 |
]; |
| 1424 |
} |
| 1425 |
} |
| 1426 |
|
| 1427 |
return $recommendations; |
| 1428 |
} |
| 1429 |
|
| 1430 |
private function calculate_user_experience_score(array $content_analysis, array $template_compliance): int { |
| 1431 |
$score = 100; |
| 1432 |
|
| 1433 |
// Deduct for poor readability |
| 1434 |
if (!empty($content_analysis['readability']['readability_score'])) { |
| 1435 |
if ($content_analysis['readability']['readability_score'] < 60) { |
| 1436 |
$score -= 30; |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
// Deduct for poor structure |
| 1441 |
if (!empty($template_compliance['heading_compliance']['compliance_score'])) { |
| 1442 |
if ($template_compliance['heading_compliance']['compliance_score'] < 70) { |
| 1443 |
$score -= 20; |
| 1444 |
} |
| 1445 |
} |
| 1446 |
|
| 1447 |
return max(0, $score); |
| 1448 |
} |
| 1449 |
|
| 1450 |
// Placeholder implementations for methods referenced but not yet implemented |
| 1451 |
private function calculate_performance_metrics(array $content_analysis, array $template_compliance): array { |
| 1452 |
return ['performance_score' => 80, 'metrics' => []]; |
| 1453 |
} |
| 1454 |
|
| 1455 |
private function generate_optimization_recommendations(array $content_analysis, array $template_compliance, string $content_type): array { |
| 1456 |
$recommendations = []; |
| 1457 |
|
| 1458 |
// Merge recommendations from different sources |
| 1459 |
if (!empty($content_analysis['recommendations'])) { |
| 1460 |
$recommendations = array_merge($recommendations, $content_analysis['recommendations']); |
| 1461 |
} |
| 1462 |
|
| 1463 |
if (!empty($template_compliance['recommendations'])) { |
| 1464 |
$recommendations = array_merge($recommendations, $template_compliance['recommendations']); |
| 1465 |
} |
| 1466 |
|
| 1467 |
return $recommendations; |
| 1468 |
} |
| 1469 |
|
| 1470 |
private function identify_optimization_opportunities(array $content_analysis, array $template_compliance, string $content_type): array { |
| 1471 |
return ['opportunities' => [], 'potential_impact' => 'medium']; |
| 1472 |
} |
| 1473 |
|
| 1474 |
private function generate_content_suggestions(array $content_analysis, string $content_type, array $keywords): array { |
| 1475 |
return ['suggestions' => [], 'content_ideas' => []]; |
| 1476 |
} |
| 1477 |
|
| 1478 |
private function generate_optimization_dashboard(string $content, array $keywords, string $content_type): array { |
| 1479 |
return ['dashboard_data' => [], 'widgets' => []]; |
| 1480 |
} |
| 1481 |
|
| 1482 |
private function get_content_templates_for_type(string $content_type): array { |
| 1483 |
return $this->content_templates[$content_type] ?? $this->content_templates['blog_post']; |
| 1484 |
} |
| 1485 |
|
| 1486 |
private function get_performance_metrics(string $context_type, ?int $context_id): array { |
| 1487 |
return ['metrics' => [], 'trends' => []]; |
| 1488 |
} |
| 1489 |
|
| 1490 |
private function get_historical_performance(string $context_type, ?int $context_id): array { |
| 1491 |
return ['historical_data' => []]; |
| 1492 |
} |
| 1493 |
|
| 1494 |
private function calculate_performance_trends(array $historical_data, array $current_metrics): array { |
| 1495 |
return ['trends' => []]; |
| 1496 |
} |
| 1497 |
|
| 1498 |
private function calculate_improvement_rate(array $performance_trends): float { |
| 1499 |
return 0.0; |
| 1500 |
} |
| 1501 |
|
| 1502 |
private function calculate_performance_score(array $metrics): int { |
| 1503 |
return 80; |
| 1504 |
} |
| 1505 |
|
| 1506 |
private function compare_with_benchmarks(array $metrics, string $context_type): array { |
| 1507 |
return ['benchmark_comparison' => []]; |
| 1508 |
} |
| 1509 |
|
| 1510 |
private function store_performance_data(string $context_type, ?int $context_id, array $tracking): bool { |
| 1511 |
return true; |
| 1512 |
} |
| 1513 |
} |
| 1514 |
|