PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.2.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.2.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 / email-report-sections / class-top-losing-posts-section.php

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

80 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Top Losing Posts Section
4 *
5 * Pages 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 (page dimension).
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_Losing_Posts_Section implements Email_Report_Section_Interface {
23
24 private const MAX_ROWS = 5;
25
26 public function key(): string {
27 return 'top_losing_posts';
28 }
29
30 public function label(): string {
31 return __('Top Losing Posts', '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['pages'])) {
46 return [];
47 }
48
49 // Real losers: pages whose clicks fell vs the previous period.
50 $rows = [];
51 foreach ($comparison['pages'] as $entry) {
52 $delta = (int) $entry['cur_clicks'] - (int) $entry['prev_clicks'];
53 if ($delta >= 0) {
54 continue;
55 }
56 $rows[] = [
57 'url' => $entry['url'] ?? '',
58 'clicks' => (int) $entry['cur_clicks'],
59 'change' => $delta,
60 ];
61 }
62
63 usort($rows, static fn($a, $b) => $a['change'] <=> $b['change']);
64
65 return [
66 'rows' => array_slice($rows, 0, self::MAX_ROWS),
67 ];
68 }
69
70 public function render(array $payload): string {
71 return Top_Posts_Renderer::render($payload['rows'] ?? [], 'loss');
72 }
73
74 public function fallback_html(): string {
75 return '<p style="color:#6b7280;font-style:italic;">'
76 . esc_html__('Search Console data unavailable. Connect Search Console to see your losing posts.', 'thinkrank')
77 . '</p>';
78 }
79 }
80