PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.7
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.7
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 0.9.7, at includes/notes/cpt.php

200 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Desktop Mode — 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 desktop-mode 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 WPDesktopMode
25 * @since 0.9.6
26 */
27
28 defined( 'ABSPATH' ) || exit;
29
30 const DESKTOP_MODE_NOTES_POST_TYPE = 'wpd_note';
31
32 /**
33 * The pastel paper color slugs a note may use.
34 *
35 * Mirrored client-side in `src/notes/colors.ts` — keep both lists in
36 * sync (the CSS custom properties in `assets/css/notes.css` are keyed
37 * by these slugs).
38 *
39 * @since 0.9.6
40 *
41 * @return string[] Color slugs.
42 */
43 function desktop_mode_notes_colors() {
44 $colors = array( 'butter', 'blush', 'sky', 'mint', 'lilac', 'peach' );
45
46 /**
47 * Filters the allowed pinned-note paper colors.
48 *
49 * Slugs added here must also ship CSS custom properties
50 * (`--dm-note-paper` / `--dm-note-paper-deep` / `--dm-note-ink`)
51 * for a `[data-note-color="<slug>"]` selector, otherwise notes
52 * using them render with the fallback (butter) paper.
53 *
54 * @since 0.9.6
55 *
56 * @param string[] $colors Allowed color slugs.
57 */
58 $colors = apply_filters( 'desktop_mode_notes_colors', $colors );
59
60 return array_values( array_filter( array_map( 'sanitize_key', (array) $colors ) ) );
61 }
62
63 /**
64 * Register the `wpd_note` post type + its meta.
65 *
66 * @since 0.9.6
67 */
68 function desktop_mode_notes_register_cpt() {
69 register_post_type(
70 DESKTOP_MODE_NOTES_POST_TYPE,
71 array(
72 'label' => __( 'Desktop Notes', 'desktop-mode' ),
73 'public' => false,
74 'publicly_queryable' => false,
75 'exclude_from_search' => true,
76 'show_ui' => false,
77 'show_in_menu' => false,
78 'show_in_rest' => false, // Custom controller in includes/notes/rest.php.
79 'supports' => array( 'title', 'editor', 'author', 'custom-fields' ),
80 'capability_type' => 'post',
81 'map_meta_cap' => true,
82 'delete_with_user' => true,
83 'rewrite' => false,
84 'query_var' => false,
85 )
86 );
87
88 register_post_meta(
89 DESKTOP_MODE_NOTES_POST_TYPE,
90 '_wpd_note_color',
91 array(
92 'type' => 'string',
93 'single' => true,
94 'default' => 'butter',
95 'sanitize_callback' => 'desktop_mode_notes_sanitize_color',
96 )
97 );
98
99 register_post_meta(
100 DESKTOP_MODE_NOTES_POST_TYPE,
101 '_wpd_note_x',
102 array(
103 'type' => 'number',
104 'single' => true,
105 'default' => 0.1,
106 'sanitize_callback' => 'desktop_mode_notes_sanitize_fraction',
107 )
108 );
109
110 register_post_meta(
111 DESKTOP_MODE_NOTES_POST_TYPE,
112 '_wpd_note_y',
113 array(
114 'type' => 'number',
115 'single' => true,
116 'default' => 0.1,
117 'sanitize_callback' => 'desktop_mode_notes_sanitize_fraction',
118 )
119 );
120
121 register_post_meta(
122 DESKTOP_MODE_NOTES_POST_TYPE,
123 '_wpd_note_z',
124 array(
125 'type' => 'integer',
126 'single' => true,
127 'default' => 1,
128 'sanitize_callback' => 'absint',
129 )
130 );
131
132 // Jitter seed — hashed from the note's text once at CREATION and
133 // never rewritten, so the paper's subtle tilt survives edits.
134 register_post_meta(
135 DESKTOP_MODE_NOTES_POST_TYPE,
136 '_wpd_note_seed',
137 array(
138 'type' => 'integer',
139 'single' => true,
140 'default' => 0,
141 'sanitize_callback' => 'absint',
142 )
143 );
144 }
145 add_action( 'init', 'desktop_mode_notes_register_cpt' );
146
147 /**
148 * Clamp a note color to the allowed pastel whitelist.
149 *
150 * @since 0.9.6
151 *
152 * @param mixed $color Raw value.
153 * @return string Whitelisted slug (falls back to 'butter').
154 */
155 function desktop_mode_notes_sanitize_color( $color ) {
156 $color = is_scalar( $color ) ? sanitize_key( (string) $color ) : '';
157 $colors = desktop_mode_notes_colors();
158 if ( in_array( $color, $colors, true ) ) {
159 return $color;
160 }
161 return isset( $colors[0] ) ? $colors[0] : 'butter';
162 }
163
164 /**
165 * Clamp a note position coordinate to the normalized 0–1 range.
166 *
167 * @since 0.9.6
168 *
169 * @param mixed $value Raw value.
170 * @return float
171 */
172 function desktop_mode_notes_sanitize_fraction( $value ) {
173 return (float) min( 1, max( 0, (float) $value ) );
174 }
175
176 /**
177 * Restore a note's pre-trash status on untrash.
178 *
179 * Core's default untrash status is 'draft', which would silently flip
180 * a public note private (and make it invisible to its own list query,
181 * which only looks at private + publish). Restore what the note was.
182 *
183 * @since 0.9.6
184 *
185 * @param string $new_status Status the post is about to get.
186 * @param int $post_id Post ID.
187 * @param string $previous_status Status the post had before trashing.
188 * @return string
189 */
190 function desktop_mode_notes_untrash_status( $new_status, $post_id, $previous_status ) {
191 if ( DESKTOP_MODE_NOTES_POST_TYPE !== get_post_type( $post_id ) ) {
192 return $new_status;
193 }
194 if ( in_array( $previous_status, array( 'private', 'publish' ), true ) ) {
195 return $previous_status;
196 }
197 return 'private';
198 }
199 add_filter( 'wp_untrash_post_status', 'desktop_mode_notes_untrash_status', 10, 3 );
200