refactor(admin): split 1089-line admin class into 7 focused files

Addresses excessive line count in Peptide_News_Admin (1089 lines)
by extracting responsibilities into single-purpose classes:

- class-pn-admin-assets.php (88 lines): CSS/JS enqueuing
- class-pn-admin-menu.php (98 lines): Menu & submenu registration
- class-pn-admin-field-renderers.php (325 lines): 25+ field renderers (static)
- class-pn-admin-settings.php (289 lines): Settings registration & sanitization
- class-pn-admin-log-viewer.php (155 lines): Plugin log viewer UI & AJAX
- class-pn-admin-settings-page.php (214 lines): Settings page + manual buttons
- class-pn-admin-dashboard-pages.php (109 lines): Dashboard/articles/cost pages
- class-peptide-news-admin.php (143 lines): Thin orchestrator (delegator)

All files under 300 lines. Original public interface preserved exactly
so no consumer code or tests break. Updated ARCHITECTURE.md.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
peptiderepo
2026-04-16 16:46:27 +07:00
parent e921fd4af2
commit 5ece95f236
10 changed files with 1374 additions and 1030 deletions

View File

@@ -0,0 +1,109 @@
<?php
declare( strict_types=1 );
/**
* Dashboard, articles, and cost dashboard page rendering.
*
* Handles the main analytics dashboard, article list management,
* and LLM cost tracking dashboard. Includes CSV export for analytics.
*
* @since 2.5.0
* @see Peptide_News_Admin — Main admin orchestrator
* @see admin/partials/dashboard.php — Analytics dashboard partial
* @see admin/partials/articles-list.php — Article list partial
* @see admin/partials/cost-dashboard.php — Cost dashboard partial
*/
class Peptide_News_Admin_Dashboard_Pages {
/**
* Render the analytics dashboard page.
*
* Includes date filtering and calls the dashboard partial.
* Handles CSV export before rendering.
*
* @return void
*/
public function render_dashboard_page(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Handle CSV export before any HTML output.
if ( isset( $_GET['export'] ) && 'csv' === $_GET['export'] ) {
check_admin_referer( 'peptide_news_export_csv' );
$this->export_csv();
return;
}
include PEPTIDE_NEWS_PLUGIN_DIR . 'admin/partials/dashboard.php';
}
/**
* Stream a CSV export of click analytics data.
*
* Respects date range filters from GET parameters.
* Defaults to last 30 days if not specified.
*
* @return void
*/
private function export_csv(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Unauthorized.', 'peptide-news' ) );
}
$raw_start = isset( $_GET['start_date'] ) ? sanitize_text_field( wp_unslash( $_GET['start_date'] ) ) : '';
$raw_end = isset( $_GET['end_date'] ) ? sanitize_text_field( wp_unslash( $_GET['end_date'] ) ) : '';
$start_date = preg_match( '/^\d{4}-\d{2}-\d{2}$/', $raw_start ) ? $raw_start : gmdate( 'Y-m-d', strtotime( '-30 days' ) );
$end_date = preg_match( '/^\d{4}-\d{2}-\d{2}$/', $raw_end ) ? $raw_end : gmdate( 'Y-m-d' );
$data = Peptide_News_Analytics::export_clicks_csv( $start_date, $end_date );
$filename = 'peptide-news-clicks-' . $start_date . '-to-' . $end_date . '.csv';
header( 'Content-Type: text/csv; charset=utf-8' );
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );
$output = fopen( 'php://output', 'w' );
if ( ! empty( $data ) ) {
fputcsv( $output, array_keys( $data[0] ) );
foreach ( $data as $row ) {
fputcsv( $output, $row );
}
} else {
fputcsv( $output, array( 'No data for selected period' ) );
}
fclose( $output );
exit;
}
/**
* Render the articles management page.
*
* @return void
*/
public function render_articles_page(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
include PEPTIDE_NEWS_PLUGIN_DIR . 'admin/partials/articles-list.php';
}
/**
* Render the LLM cost tracking dashboard page.
*
* Shows monthly budget status, daily cost chart, per-model breakdown,
* and recent API call log. Data is loaded via AJAX for responsiveness.
*
* @since 2.4.0
* @return void
*/
public function render_cost_dashboard_page(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
include PEPTIDE_NEWS_PLUGIN_DIR . 'admin/partials/cost-dashboard.php';
}
}