PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.9.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.9.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 1.0.1 1.0.2 All 50 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.9.0, at includes/seo/email-report-sections/class-top-winning-keywords-section.php

98 lines 2.9 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 — "Top growing queries".
4 *
5 * Queries with the largest click gain vs the previous period, computed from
6 * the shared current-vs-previous comparison the Data Provider builds from
7 * Search Console (query dimension). Ranking is on the click delta; average
8 * position rides along as context.
9 *
10 * @package ThinkRank
11 * @subpackage SEO\Email_Report_Sections
12 * @since 1.9.0
13 */
14
15 declare(strict_types=1);
16
17 namespace ThinkRank\SEO\Email_Report_Sections;
18
19 if (!defined('ABSPATH')) {
20 exit;
21 }
22
23 final class Top_Winning_Keywords_Section implements Email_Report_Section_Interface {
24
25 private const MAX_ROWS = 5;
26
27 public function key(): string {
28 return 'top_winning_keywords';
29 }
30
31 public function label(): string {
32 return __('Top growing queries', 'thinkrank');
33 }
34
35 public function default_enabled(): bool {
36 return true;
37 }
38
39 public function requires_capability(): ?string {
40 return null;
41 }
42
43 public function renders_own_heading(): bool {
44 return true;
45 }
46
47 public function collect(array $context): array {
48 $shared = $context['shared'] ?? [];
49 $comparison = $shared['comparison'] ?? [];
50 if (empty($comparison['available']) || empty($comparison['queries'])) {
51 return [];
52 }
53
54 // Real gainers: keywords whose clicks grew vs the previous period.
55 $rows = [];
56 foreach ($comparison['queries'] as $entry) {
57 $delta = (int) $entry['cur_clicks'] - (int) $entry['prev_clicks'];
58 if ($delta <= 0) {
59 continue;
60 }
61 $rows[] = [
62 'query' => $entry['query'] ?? '',
63 'position' => isset($entry['cur_pos']) ? (float) $entry['cur_pos'] : null,
64 'prev_position' => isset($entry['prev_pos']) ? (float) $entry['prev_pos'] : null,
65 'clicks' => (int) $entry['cur_clicks'],
66 'change' => $delta,
67 ];
68 }
69
70 usort($rows, static fn($a, $b) => $b['change'] <=> $a['change']);
71
72 return [
73 'rows' => array_slice($rows, 0, self::MAX_ROWS),
74 ];
75 }
76
77 public function has_data(array $payload): bool {
78 return !empty($payload['rows']);
79 }
80
81 public function render(array $payload): string {
82 $rows = Top_Posts_Renderer::keyword_rows($payload['rows'] ?? []);
83 if ($rows === []) {
84 return '';
85 }
86 return Email_Report_Html::heading(
87 $this->label(),
88 __('Search terms that sent more clicks than last period', 'thinkrank')
89 )
90 . Email_Report_Html::list_rows($rows, 'up')
91 . Email_Report_Html::link(__('See all queries', 'thinkrank'), Email_Report_Html::admin_link('analytics', 'keywords'));
92 }
93
94 public function fallback_html(): string {
95 return '';
96 }
97 }
98