PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.6.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.6.0
2.11.0 2.10.0 2.9.0 2.8.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 All 52 releases
thinkrank / includes / seo / email-report-sections / class-top-winning-keywords-section.php

class-top-winning-keywords-section.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 2.6.0, at includes/seo/email-report-sections/class-top-winning-keywords-section.php

82 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Top Winning Keywords Section
4 *
5 * Keywords with the largest click gain vs the previous period, computed
6 * from the shared current-vs-previous comparison the Data Provider builds
7 * from Search Console.
8 *
9 * @package ThinkRank
10 * @subpackage SEO\Email_Report_Sections
11 * @since 1.9.0
12 */
13
14 declare(strict_types=1);
15
16 namespace ThinkRank\SEO\Email_Report_Sections;
17
18 if (!defined('ABSPATH')) {
19 exit;
20 }
21
22 final class Top_Winning_Keywords_Section implements Email_Report_Section_Interface {
23
24 private const MAX_ROWS = 5;
25
26 public function key(): string {
27 return 'top_winning_keywords';
28 }
29
30 public function label(): string {
31 return __('Top Winning Keywords', 'thinkrank');
32 }
33
34 public function default_enabled(): bool {
35 return true;
36 }
37
38 public function requires_capability(): ?string {
39 return null;
40 }
41
42 public function collect(array $context): array {
43 $shared = $context['shared'] ?? [];
44 $comparison = $shared['comparison'] ?? [];
45 if (empty($comparison['available']) || empty($comparison['queries'])) {
46 return [];
47 }
48
49 // Real gainers: keywords whose clicks grew vs the previous period.
50 $rows = [];
51 foreach ($comparison['queries'] as $entry) {
52 $delta = (int) $entry['cur_clicks'] - (int) $entry['prev_clicks'];
53 if ($delta <= 0) {
54 continue;
55 }
56 $rows[] = [
57 'query' => $entry['query'] ?? '',
58 'position' => isset($entry['cur_pos']) ? (float) $entry['cur_pos'] : null,
59 'clicks' => (int) $entry['cur_clicks'],
60 'change' => $delta,
61 ];
62 }
63
64 // Biggest click gain first.
65 usort($rows, static fn($a, $b) => $b['change'] <=> $a['change']);
66
67 return [
68 'rows' => array_slice($rows, 0, self::MAX_ROWS),
69 ];
70 }
71
72 public function render(array $payload): string {
73 return Top_Posts_Renderer::render_keywords($payload['rows'] ?? [], 'gain');
74 }
75
76 public function fallback_html(): string {
77 return '<p style="color:#6b7280;font-style:italic;">'
78 . esc_html__('Search Console data unavailable. Connect Search Console to see top winning keywords.', 'thinkrank')
79 . '</p>';
80 }
81 }
82