| 1 |
<?php |
| 2 |
/** |
| 3 |
* Top Winning Posts Section |
| 4 |
* |
| 5 |
* Pages 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 (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_Winning_Posts_Section implements Email_Report_Section_Interface { |
| 23 |
|
| 24 |
private const MAX_ROWS = 5; |
| 25 |
|
| 26 |
public function key(): string { |
| 27 |
return 'top_winning_posts'; |
| 28 |
} |
| 29 |
|
| 30 |
public function label(): string { |
| 31 |
return __('Top Winning 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 gainers: pages whose clicks grew 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) => $b['change'] <=> $a['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'] ?? [], 'gain'); |
| 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 winning posts.', 'thinkrank') |
| 77 |
. '</p>'; |
| 78 |
} |
| 79 |
} |
| 80 |
|