| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — Living Tree: REST snapshot endpoint. | |
| 3 | + * OpenStation — Living Tree: REST snapshot endpoint. | |
| 4 | 4 | * |
| 5 | 5 | * One route: `GET desktop-mode/v1/living-tree/snapshot`. Returns the |
| 6 | 6 | * compact site DNA (`TreeSnapshot` in the JS types) — aggregate counts |
| 7 | 7 | * and branch hints — never the full post list. The client turns this |
| @@ -14,19 +14,27 @@ | ||
| 14 | 14 | * |
| 15 | 15 | * The response is cached in a transient (TTL 6h) and invalidated whenever |
| 16 | 16 | * content changes (`save_post` / `deleted_post` / `comment_post`). |
| 17 | 17 | * |
| 18 | - * @package WPDesktopMode | |
| 19 | - * @since 0.9.4 | |
| 18 | + * @package OpenStation | |
| 20 | 19 | */ |
| 21 | 20 | |
| 22 | 21 | defined( 'ABSPATH' ) || exit; |
| 23 | 22 | |
| 24 | -/** Transient key the built snapshot is cached under. */ | |
| 25 | -const DESKTOP_MODE_LIVING_TREE_CACHE_KEY = 'desktop_mode_living_tree_snapshot'; | |
| 23 | +/** | |
| 24 | + * Transient key the built snapshot is cached under. | |
| 25 | + * | |
| 26 | + * The VALUE keeps its pre-rebrand spelling on purpose, so live caches | |
| 27 | + * stay addressable. The mismatch between this constant's name and its | |
| 28 | + * value is deliberate — it is NOT a half-finished rename. | |
| 29 | + * | |
| 30 | + * Not to be confused with the `openstation_living_tree_snapshot` filter, | |
| 31 | + * which once shared this string and is now deliberately decoupled. | |
| 32 | + */ | |
| 33 | +const OPENSTATION_LIVING_TREE_CACHE_KEY = 'desktop_mode_living_tree_snapshot'; | |
| 26 | 34 | |
| 27 | 35 | /** Cache lifetime for the snapshot. */ |
| 28 | -const DESKTOP_MODE_LIVING_TREE_CACHE_TTL = 6 * HOUR_IN_SECONDS; | |
| 36 | +const OPENSTATION_LIVING_TREE_CACHE_TTL = 6 * HOUR_IN_SECONDS; | |
| 29 | 37 | |
| 30 | 38 | /** |
| 31 | 39 | * Whether the current user may read the Living Tree snapshot. |
| 32 | 40 | * |
| @@ -32,61 +40,53 @@ | ||
| 32 | 40 | * |
| 33 | 41 | * Defaults to `read` — anyone who can see the admin can see the wallpaper |
| 34 | 42 | * of their own site. Filterable so a site can widen or restrict it. |
| 35 | 43 | * |
| 36 | - * @since 0.9.4 | |
| 37 | - * | |
| 38 | 44 | * @return bool |
| 39 | 45 | */ |
| 40 | -function desktop_mode_living_tree_user_can_use() { | |
| 46 | +function openstation_living_tree_user_can_use() { | |
| 41 | 47 | $can = current_user_can( 'read' ); |
| 42 | 48 | |
| 43 | 49 | /** |
| 44 | 50 | * Filter whether the current user can read the Living Tree snapshot. |
| 45 | 51 | * |
| 46 | - * @since 0.9.4 | |
| 47 | - * | |
| 48 | 52 | * @param bool $can Default: the `read` capability. |
| 49 | 53 | */ |
| 50 | - return (bool) apply_filters( 'desktop_mode_living_tree_user_can_use', $can ); | |
| 54 | + return (bool) apply_filters( 'openstation_living_tree_user_can_use', $can ); | |
| 51 | 55 | } |
| 52 | 56 | |
| 53 | 57 | /** |
| 54 | 58 | * Register the snapshot route. |
| 55 | - * | |
| 56 | - * @since 0.9.4 | |
| 57 | 59 | */ |
| 58 | -function desktop_mode_living_tree_register_routes() { | |
| 60 | +function openstation_living_tree_register_routes() { | |
| 59 | 61 | register_rest_route( |
| 60 | 62 | 'desktop-mode/v1', |
| 61 | 63 | '/living-tree/snapshot', |
| 62 | 64 | array( |
| 63 | 65 | 'methods' => WP_REST_Server::READABLE, |
| 64 | - 'callback' => 'desktop_mode_living_tree_rest_snapshot', | |
| 65 | - 'permission_callback' => 'desktop_mode_living_tree_user_can_use', | |
| 66 | + 'callback' => 'openstation_living_tree_rest_snapshot', | |
| 67 | + 'permission_callback' => 'openstation_living_tree_user_can_use', | |
| 66 | 68 | ) |
| 67 | 69 | ); |
| 68 | 70 | } |
| 69 | -add_action( 'rest_api_init', 'desktop_mode_living_tree_register_routes' ); | |
| 71 | +add_action( 'rest_api_init', 'openstation_living_tree_register_routes' ); | |
| 70 | 72 | |
| 71 | 73 | /** |
| 72 | 74 | * GET /living-tree/snapshot — cached snapshot response. |
| 73 | 75 | * |
| 74 | - * @since 0.9.4 | |
| 75 | - * | |
| 76 | 76 | * @return WP_REST_Response |
| 77 | 77 | */ |
| 78 | -function desktop_mode_living_tree_rest_snapshot() { | |
| 79 | - $cached = get_transient( DESKTOP_MODE_LIVING_TREE_CACHE_KEY ); | |
| 78 | +function openstation_living_tree_rest_snapshot() { | |
| 79 | + $cached = get_transient( OPENSTATION_LIVING_TREE_CACHE_KEY ); | |
| 80 | 80 | if ( is_array( $cached ) ) { |
| 81 | 81 | return rest_ensure_response( $cached ); |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - $snapshot = desktop_mode_living_tree_build_snapshot(); | |
| 84 | + $snapshot = openstation_living_tree_build_snapshot(); | |
| 85 | 85 | set_transient( |
| 86 | - DESKTOP_MODE_LIVING_TREE_CACHE_KEY, | |
| 86 | + OPENSTATION_LIVING_TREE_CACHE_KEY, | |
| 87 | 87 | $snapshot, |
| 88 | - DESKTOP_MODE_LIVING_TREE_CACHE_TTL | |
| 88 | + OPENSTATION_LIVING_TREE_CACHE_TTL | |
| 89 | 89 | ); |
| 90 | 90 | |
| 91 | 91 | return rest_ensure_response( $snapshot ); |
| 92 | 92 | } |
| @@ -93,17 +93,15 @@ | ||
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * Invalidate the cached snapshot. Wired to the content-mutation hooks so |
| 96 | 96 | * the tree re-DNAs on the next load after the site changes. |
| 97 | - * | |
| 98 | - * @since 0.9.4 | |
| 99 | 97 | */ |
| 100 | -function desktop_mode_living_tree_flush_cache() { | |
| 101 | - delete_transient( DESKTOP_MODE_LIVING_TREE_CACHE_KEY ); | |
| 98 | +function openstation_living_tree_flush_cache() { | |
| 99 | + delete_transient( OPENSTATION_LIVING_TREE_CACHE_KEY ); | |
| 102 | 100 | } |
| 103 | -add_action( 'save_post', 'desktop_mode_living_tree_flush_cache' ); | |
| 104 | -add_action( 'deleted_post', 'desktop_mode_living_tree_flush_cache' ); | |
| 105 | -add_action( 'comment_post', 'desktop_mode_living_tree_flush_cache' ); | |
| 101 | +add_action( 'save_post', 'openstation_living_tree_flush_cache' ); | |
| 102 | +add_action( 'deleted_post', 'openstation_living_tree_flush_cache' ); | |
| 103 | +add_action( 'comment_post', 'openstation_living_tree_flush_cache' ); | |
| 106 | 104 | |
| 107 | 105 | /** |
| 108 | 106 | * Build the compact site DNA snapshot. |
| 109 | 107 | * |
| @@ -111,13 +109,11 @@ | ||
| 111 | 109 | * yields a well-formed snapshot — the golden rule (WordPress emits |
| 112 | 110 | * hormones, never geometry) lives here: this function must never leak a |
| 113 | 111 | * per-post coordinate or identity into the payload. |
| 114 | 112 | * |
| 115 | - * @since 0.9.4 | |
| 116 | - * | |
| 117 | 113 | * @return array The snapshot, matching the JS `TreeSnapshot` shape. |
| 118 | 114 | */ |
| 119 | -function desktop_mode_living_tree_build_snapshot() { | |
| 115 | +function openstation_living_tree_build_snapshot() { | |
| 120 | 116 | $posts = wp_count_posts( 'post' ); |
| 121 | 117 | $pages = wp_count_posts( 'page' ); |
| 122 | 118 | $comments = wp_count_comments(); |
| 123 | 119 | |
| @@ -126,20 +122,20 @@ | ||
| 126 | 122 | |
| 127 | 123 | $snapshot = array( |
| 128 | 124 | 'siteUrl' => (string) home_url(), |
| 129 | 125 | 'siteName' => (string) get_bloginfo( 'name' ), |
| 130 | - 'installEpoch' => desktop_mode_living_tree_install_epoch(), | |
| 131 | - 'siteAgeDays' => desktop_mode_living_tree_site_age_days(), | |
| 126 | + 'installEpoch' => openstation_living_tree_install_epoch(), | |
| 127 | + 'siteAgeDays' => openstation_living_tree_site_age_days(), | |
| 132 | 128 | 'totalPosts' => isset( $posts->publish ) ? (int) $posts->publish : 0, |
| 133 | 129 | 'totalPages' => isset( $pages->publish ) ? (int) $pages->publish : 0, |
| 134 | 130 | 'totalCategories' => is_wp_error( $categories ) ? 0 : (int) $categories, |
| 135 | 131 | 'totalTags' => is_wp_error( $tags ) ? 0 : (int) $tags, |
| 136 | 132 | 'totalComments' => isset( $comments->approved ) ? (int) $comments->approved : 0, |
| 137 | - 'activeUsers' => desktop_mode_living_tree_active_users(), | |
| 138 | - 'traffic' => desktop_mode_living_tree_traffic(), | |
| 139 | - 'seoHealth' => desktop_mode_living_tree_seo_health(), | |
| 140 | - 'performance' => desktop_mode_living_tree_performance(), | |
| 141 | - 'branches' => desktop_mode_living_tree_branch_dna(), | |
| 133 | + 'activeUsers' => openstation_living_tree_active_users(), | |
| 134 | + 'traffic' => openstation_living_tree_traffic(), | |
| 135 | + 'seoHealth' => openstation_living_tree_seo_health(), | |
| 136 | + 'performance' => openstation_living_tree_performance(), | |
| 137 | + 'branches' => openstation_living_tree_branch_dna(), | |
| 142 | 138 | ); |
| 143 | 139 | |
| 144 | 140 | /** |
| 145 | 141 | * Filter the Living Tree snapshot before it is cached and served. |
| @@ -146,10 +142,8 @@ | ||
| 146 | 142 | * Keep the shape intact — the JS client validates nothing; it trusts |
| 147 | 143 | * this contract. Aggregates only: never add per-post identities or |
| 148 | 144 | * coordinates (the golden rule). |
| 149 | 145 | * |
| 150 | - * @since 0.9.4 | |
| 151 | - * | |
| 152 | 146 | * @param array $snapshot The compact site DNA. |
| 153 | 147 | */ |
| 154 | - return apply_filters( 'desktop_mode_living_tree_snapshot', $snapshot ); | |
| 148 | + return apply_filters( 'openstation_living_tree_snapshot', $snapshot ); | |
| 155 | 149 | } |