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.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 1.0.1 All 51 releases
thinkrank / includes / seo / email-report-sections / class-top-losing-keywords-section.php

class-top-losing-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-losing-keywords-section.php

102 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Top Losing Keywords Section — "Queries losing ground".
4 *
5 * Queries with the largest click loss 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, not on
8 * average position — position is carried alongside as context only, and is
9 * null for a query that dropped out of the current window entirely.
10 *
11 * @package ThinkRank
12 * @subpackage SEO\Email_Report_Sections
13 * @since 1.9.0
14 */
15
16 declare(strict_types=1);
17
18 namespace ThinkRank\SEO\Email_Report_Sections;
19
20 if (!defined('ABSPATH')) {
21 exit;
22 }
23
24 final class Top_Losing_Keywords_Section implements Email_Report_Section_Interface {
25
26 private const MAX_ROWS = 5;
27
28 public function key(): string {
29 return 'top_losing_keywords';
30 }
31
32 public function label(): string {
33 return __('Queries losing ground', 'thinkrank');
34 }
35
36 public function default_enabled(): bool {
37 return true;
38 }
39
40 public function requires_capability(): ?string {
41 return null;
42 }
43
44 public function renders_own_heading(): bool {
45 return true;
46 }
47
48 public function collect(array $context): array {
49 $shared = $context['shared'] ?? [];
50 $comparison = $shared['comparison'] ?? [];
51 if (empty($comparison['available']) || empty($comparison['queries'])) {
52 return [];
53 }
54
55 // Real losers: keywords whose clicks fell vs the previous period.
56 $rows = [];
57 foreach ($comparison['queries'] as $entry) {
58 $delta = (int) $entry['cur_clicks'] - (int) $entry['prev_clicks'];
59 if ($delta >= 0) {
60 continue;
61 }
62 $rows[] = [
63 'query' => $entry['query'] ?? '',
64 // A keyword that dropped out entirely has no current position.
65 // Keep it null so the row says so instead of "0.0".
66 'position' => isset($entry['cur_pos']) ? (float) $entry['cur_pos'] : null,
67 'prev_position' => isset($entry['prev_pos']) ? (float) $entry['prev_pos'] : null,
68 'clicks' => (int) $entry['cur_clicks'],
69 'change' => $delta,
70 ];
71 }
72
73 // Biggest click loss first (most negative).
74 usort($rows, static fn($a, $b) => $a['change'] <=> $b['change']);
75
76 return [
77 'rows' => array_slice($rows, 0, self::MAX_ROWS),
78 ];
79 }
80
81 public function has_data(array $payload): bool {
82 return !empty($payload['rows']);
83 }
84
85 public function render(array $payload): string {
86 $rows = Top_Posts_Renderer::keyword_rows($payload['rows'] ?? []);
87 if ($rows === []) {
88 return '';
89 }
90 return Email_Report_Html::heading(
91 $this->label(),
92 __('Worth a look: these search terms lost the most clicks vs the previous period', 'thinkrank')
93 )
94 . Email_Report_Html::list_rows($rows, 'down')
95 . Email_Report_Html::link(__('See all queries', 'thinkrank'), Email_Report_Html::admin_link('analytics', 'keywords'));
96 }
97
98 public function fallback_html(): string {
99 return '';
100 }
101 }
102