PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / trunk
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin vtrunk
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 0.8.6 All 33 releases
desktop-mode / includes / notes / cpt.php

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

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