PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.0.0
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.0.0
1.1.10 1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 All 34 releases
desktop-mode / includes / notes / cpt.php

cpt.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 1.0.0, at includes/notes/cpt.php

187 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * OpenStation — Pinned notes CPT.
4 *
5 * Registers the `wpd_note` post type backing the pinned-notes feature:
6 * paper notes the user writes in the Note Pad widget and pins to the
7 * desktop wallpaper. Distinct from the Guidelines-backed sticky-notes
8 * layer (`includes/sticky-notes/heartbeat.php`) — that one rides
9 * Gutenberg's `wp_guideline` CPT; this one is plugin-owned.
10 *
11 * Visibility model: the "public" checkbox maps to `post_status`.
12 *
13 * 'private' — default; only the owner sees the note.
14 * 'publish' — public; every openstation user sees it (read-only
15 * for non-owners). Nothing leaks outside the plugin's
16 * own REST controller because the CPT is not publicly
17 * queryable, excluded from search, and absent from the
18 * core REST API.
19 *
20 * Position (normalized 0–1 of the desktop area), paper color, and
21 * z-order live in postmeta — per-owner placement is the canonical
22 * placement every viewer sees.
23 *
24 * @package OpenStation
25 */
26
27 defined( 'ABSPATH' ) || exit;
28
29 const OPENSTATION_NOTES_POST_TYPE = 'wpd_note';
30
31 /**
32 * The pastel paper color slugs a note may use.
33 *
34 * Mirrored client-side in `src/notes/colors.ts` — keep both lists in
35 * sync (the CSS custom properties in `assets/css/notes.css` are keyed
36 * by these slugs).
37 *
38 * @return string[] Color slugs.
39 */
40 function openstation_notes_colors() {
41 $colors = array( 'butter', 'blush', 'sky', 'mint', 'lilac', 'peach' );
42
43 /**
44 * Filters the allowed pinned-note paper colors.
45 *
46 * Slugs added here must also ship CSS custom properties
47 * (`--dm-note-paper` / `--dm-note-paper-deep` / `--dm-note-ink`)
48 * for a `[data-note-color="<slug>"]` selector, otherwise notes
49 * using them render with the fallback (butter) paper.
50 *
51 * @param string[] $colors Allowed color slugs.
52 */
53 $colors = apply_filters( 'openstation_notes_colors', $colors );
54
55 return array_values( array_filter( array_map( 'sanitize_key', (array) $colors ) ) );
56 }
57
58 /**
59 * Register the `wpd_note` post type + its meta.
60 */
61 function openstation_notes_register_cpt() {
62 register_post_type(
63 OPENSTATION_NOTES_POST_TYPE,
64 array(
65 'label' => __( 'Desktop Notes', 'desktop-mode' ),
66 'public' => false,
67 'publicly_queryable' => false,
68 'exclude_from_search' => true,
69 'show_ui' => false,
70 'show_in_menu' => false,
71 'show_in_rest' => false, // Custom controller in includes/notes/rest.php.
72 'supports' => array( 'title', 'editor', 'author', 'custom-fields' ),
73 'capability_type' => 'post',
74 'map_meta_cap' => true,
75 'delete_with_user' => true,
76 'rewrite' => false,
77 'query_var' => false,
78 )
79 );
80
81 register_post_meta(
82 OPENSTATION_NOTES_POST_TYPE,
83 '_wpd_note_color',
84 array(
85 'type' => 'string',
86 'single' => true,
87 'default' => 'butter',
88 'sanitize_callback' => 'openstation_notes_sanitize_color',
89 )
90 );
91
92 register_post_meta(
93 OPENSTATION_NOTES_POST_TYPE,
94 '_wpd_note_x',
95 array(
96 'type' => 'number',
97 'single' => true,
98 'default' => 0.1,
99 'sanitize_callback' => 'openstation_notes_sanitize_fraction',
100 )
101 );
102
103 register_post_meta(
104 OPENSTATION_NOTES_POST_TYPE,
105 '_wpd_note_y',
106 array(
107 'type' => 'number',
108 'single' => true,
109 'default' => 0.1,
110 'sanitize_callback' => 'openstation_notes_sanitize_fraction',
111 )
112 );
113
114 register_post_meta(
115 OPENSTATION_NOTES_POST_TYPE,
116 '_wpd_note_z',
117 array(
118 'type' => 'integer',
119 'single' => true,
120 'default' => 1,
121 'sanitize_callback' => 'absint',
122 )
123 );
124
125 // Jitter seed — hashed from the note's text once at CREATION and
126 // never rewritten, so the paper's subtle tilt survives edits.
127 register_post_meta(
128 OPENSTATION_NOTES_POST_TYPE,
129 '_wpd_note_seed',
130 array(
131 'type' => 'integer',
132 'single' => true,
133 'default' => 0,
134 'sanitize_callback' => 'absint',
135 )
136 );
137 }
138 add_action( 'init', 'openstation_notes_register_cpt' );
139
140 /**
141 * Clamp a note color to the allowed pastel whitelist.
142 *
143 * @param mixed $color Raw value.
144 * @return string Whitelisted slug (falls back to 'butter').
145 */
146 function openstation_notes_sanitize_color( $color ) {
147 $color = is_scalar( $color ) ? sanitize_key( (string) $color ) : '';
148 $colors = openstation_notes_colors();
149 if ( in_array( $color, $colors, true ) ) {
150 return $color;
151 }
152 return isset( $colors[0] ) ? $colors[0] : 'butter';
153 }
154
155 /**
156 * Clamp a note position coordinate to the normalized 0–1 range.
157 *
158 * @param mixed $value Raw value.
159 * @return float
160 */
161 function openstation_notes_sanitize_fraction( $value ) {
162 return (float) min( 1, max( 0, (float) $value ) );
163 }
164
165 /**
166 * Restore a note's pre-trash status on untrash.
167 *
168 * Core's default untrash status is 'draft', which would silently flip
169 * a public note private (and make it invisible to its own list query,
170 * which only looks at private + publish). Restore what the note was.
171 *
172 * @param string $new_status Status the post is about to get.
173 * @param int $post_id Post ID.
174 * @param string $previous_status Status the post had before trashing.
175 * @return string
176 */
177 function openstation_notes_untrash_status( $new_status, $post_id, $previous_status ) {
178 if ( OPENSTATION_NOTES_POST_TYPE !== get_post_type( $post_id ) ) {
179 return $new_status;
180 }
181 if ( in_array( $previous_status, array( 'private', 'publish' ), true ) ) {
182 return $previous_status;
183 }
184 return 'private';
185 }
186 add_filter( 'wp_untrash_post_status', 'openstation_notes_untrash_status', 10, 3 );
187