| 1 |
<?php |
| 2 |
/** |
| 3 |
* Top Losing Posts Section — "Pages losing ground". |
| 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). A page that dropped out of the current |
| 8 |
* window entirely still appears — the comparison keys off both windows. |
| 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_Losing_Posts_Section implements Email_Report_Section_Interface { |
| 24 |
|
| 25 |
private const MAX_ROWS = 5; |
| 26 |
|
| 27 |
public function key(): string { |
| 28 |
return 'top_losing_posts'; |
| 29 |
} |
| 30 |
|
| 31 |
public function label(): string { |
| 32 |
return __('Pages losing ground', '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['pages'])) { |
| 51 |
return []; |
| 52 |
} |
| 53 |
|
| 54 |
// Real losers: pages whose clicks fell vs the previous period. |
| 55 |
$rows = []; |
| 56 |
foreach ($comparison['pages'] as $entry) { |
| 57 |
$delta = (int) $entry['cur_clicks'] - (int) $entry['prev_clicks']; |
| 58 |
if ($delta >= 0) { |
| 59 |
continue; |
| 60 |
} |
| 61 |
$rows[] = [ |
| 62 |
'url' => $entry['url'] ?? '', |
| 63 |
'clicks' => (int) $entry['cur_clicks'], |
| 64 |
'change' => $delta, |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
// Biggest click loss first (most negative). |
| 69 |
usort($rows, static fn($a, $b) => $a['change'] <=> $b['change']); |
| 70 |
|
| 71 |
return [ |
| 72 |
'rows' => array_slice($rows, 0, self::MAX_ROWS), |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
public function has_data(array $payload): bool { |
| 77 |
return !empty($payload['rows']); |
| 78 |
} |
| 79 |
|
| 80 |
public function render(array $payload): string { |
| 81 |
$rows = Top_Posts_Renderer::page_rows($payload['rows'] ?? []); |
| 82 |
if ($rows === []) { |
| 83 |
return ''; |
| 84 |
} |
| 85 |
return Email_Report_Html::heading( |
| 86 |
$this->label(), |
| 87 |
__('Worth a look: these pages lost the most clicks vs the previous period', 'thinkrank') |
| 88 |
) |
| 89 |
. Email_Report_Html::list_rows($rows, 'down') |
| 90 |
. Email_Report_Html::link(__('See all pages', 'thinkrank'), Email_Report_Html::admin_link('analytics', 'dashboard')); |
| 91 |
} |
| 92 |
|
| 93 |
public function fallback_html(): string { |
| 94 |
return ''; |
| 95 |
} |
| 96 |
} |
| 97 |
|