Skip to content

Instantly share code, notes, and snippets.

@sjoonk
Created October 6, 2026 05:59
Show Gist options
  • Select an option

  • Save sjoonk/245c96014b56af555da02a105d65851f to your computer and use it in GitHub Desktop.

Select an option

Save sjoonk/245c96014b56af555da02a105d65851f to your computer and use it in GitHub Desktop.
UP Status Endpoint
<?php
/**
* Plugin Name: UP Status Endpoint
* Description: 사내 대시보드용 읽기 전용 사이트 상태 요약 REST 엔드포인트
* Version: 0.1.0
* Author: Usefulparadigm
*
* 설치: wp-content/mu-plugins/up-status.php 로 업로드 (mu-plugin은 활성화 불필요)
* 설정: wp-config.php 에 define( 'UP_STATUS_TOKEN', '32자 이상의 임의 문자열' );
* 호출: GET /wp-json/up-status/v1/summary 헤더: X-UP-Status-Token: <토큰>
* 캐시를 건너뛰려면 ?fresh=1
*/
defined( 'ABSPATH' ) || exit;
const UP_STATUS_NAMESPACE = 'up-status/v1';
const UP_STATUS_CACHE_KEY = 'up_status_summary';
const UP_STATUS_CACHE_TTL = 60; // 초. 대시보드 새로고침이 몰려도 사이트 부하를 줄이기 위함
add_action( 'rest_api_init', 'up_status_register_route' );
function up_status_register_route() {
register_rest_route(
UP_STATUS_NAMESPACE,
'/summary',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'up_status_get_summary',
'permission_callback' => 'up_status_check_token',
)
);
}
/**
* 토큰 검증. 토큰이 설정되지 않았거나 너무 짧으면 엔드포인트 자체를 닫는다.
*/
function up_status_check_token( WP_REST_Request $request ) {
if ( ! defined( 'UP_STATUS_TOKEN' ) || strlen( (string) UP_STATUS_TOKEN ) < 32 ) {
return new WP_Error( 'up_status_not_configured', 'Status endpoint is not configured.', array( 'status' => 503 ) );
}
$token = (string) $request->get_header( 'x-up-status-token' );
// 타이밍 공격을 피하기 위해 hash_equals로 비교
if ( '' === $token || ! hash_equals( (string) UP_STATUS_TOKEN, $token ) ) {
return new WP_Error( 'up_status_forbidden', 'Invalid token.', array( 'status' => 403 ) );
}
return true;
}
function up_status_get_summary( WP_REST_Request $request ) {
$summary = $request->get_param( 'fresh' ) ? false : get_transient( UP_STATUS_CACHE_KEY );
if ( ! is_array( $summary ) ) {
$summary = up_status_build_summary();
set_transient( UP_STATUS_CACHE_KEY, $summary, UP_STATUS_CACHE_TTL );
}
$response = rest_ensure_response( $summary );
// CDN이나 캐시 플러그인이 응답을 저장하지 않도록
$response->header( 'Cache-Control', 'no-store, private' );
return $response;
}
/**
* 상태 요약 생성. 업데이트 정보는 WordPress가 이미 저장해 둔 값(transient)만 읽고,
* 외부 업데이트 서버에 새로 요청하지 않는다.
*/
function up_status_build_summary() {
global $wpdb, $wp_version;
require_once ABSPATH . 'wp-admin/includes/plugin.php';
require_once ABSPATH . 'wp-admin/includes/update.php';
// 코어 업데이트
$core_latest = null;
$core_needs = false;
$core_offers = get_core_updates( array( 'dismissed' => false ) );
if ( is_array( $core_offers ) && ! empty( $core_offers ) ) {
$first = reset( $core_offers );
$core_latest = isset( $first->current ) ? $first->current : null;
$core_needs = isset( $first->response ) && 'upgrade' === $first->response;
}
// 플러그인 업데이트
$plugin_updates = array();
foreach ( get_plugin_updates() as $file => $plugin ) {
$plugin_updates[] = array(
'name' => $plugin->Name,
'current' => $plugin->Version,
'new' => isset( $plugin->update->new_version ) ? $plugin->update->new_version : null,
'active' => is_plugin_active( $file ),
);
}
// 테마 업데이트
$theme_updates = array();
foreach ( get_theme_updates() as $theme ) {
$theme_updates[] = array(
'name' => $theme->get( 'Name' ),
'current' => $theme->get( 'Version' ),
'new' => isset( $theme->update['new_version'] ) ? $theme->update['new_version'] : null,
);
}
// Site Health 요약. 관리자가 사이트 상태 화면을 열거나 주간 예약 점검이 돌아야 값이 생긴다
$health = null;
$health_raw = get_transient( 'health-check-site-status-result' );
if ( $health_raw ) {
$decoded = json_decode( $health_raw, true );
if ( is_array( $decoded ) ) {
$health = array(
'good' => (int) ( $decoded['good'] ?? 0 ),
'recommended' => (int) ( $decoded['recommended'] ?? 0 ),
'critical' => (int) ( $decoded['critical'] ?? 0 ),
);
}
}
$active_theme = wp_get_theme();
$core_checked = get_site_transient( 'update_core' );
$plugin_checked = get_site_transient( 'update_plugins' );
return array(
'schema' => 1,
'generated_at' => gmdate( 'c' ),
'site' => array(
'name' => get_bloginfo( 'name' ),
'url' => home_url(),
'environment' => wp_get_environment_type(),
'multisite' => is_multisite(),
'https' => function_exists( 'wp_is_using_https' ) ? wp_is_using_https() : null,
),
'core' => array(
'version' => $wp_version,
'latest' => $core_latest,
'needs_update' => $core_needs,
'last_checked' => isset( $core_checked->last_checked ) ? gmdate( 'c', $core_checked->last_checked ) : null,
),
'plugins' => array(
'total' => count( get_plugins() ),
'active' => count( (array) get_option( 'active_plugins', array() ) ),
'updates' => $plugin_updates,
'last_checked' => isset( $plugin_checked->last_checked ) ? gmdate( 'c', $plugin_checked->last_checked ) : null,
),
'themes' => array(
'active' => array(
'name' => $active_theme->get( 'Name' ),
'version' => $active_theme->get( 'Version' ),
),
'updates' => $theme_updates,
),
'server' => array(
'php' => PHP_VERSION,
'db' => $wpdb->db_version(),
'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
),
'health' => $health,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment