PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.10
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.10
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 / apps / comments / parts / fields.php

fields.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 1.1.10, at apps/comments/parts/fields.php

226 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Comments app — the `wp/v2/comments` projection.
4 *
5 * The default query args the app's `data()` sends to the comments
6 * collection (filterable through `openstation_comments_window_query_args`,
7 * so a plugin can widen `_fields` or scope the default view), and the
8 * computed `openstation_*` REST fields on the `comment` resource the
9 * conversation view reads. Fields are computed lazily — none of them
10 * runs unless a request asks for it via `_fields`.
11 *
12 * @package OpenStation
13 */
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Default REST query args for the Comments app.
19 *
20 * @return array
21 */
22 function openstation_comments_window_default_query_args() {
23 // `context=edit` on `wp/v2/comments` requires `moderate_comments`
24 // — sending it as an author-without-moderate-cap 401s the entire
25 // list. Stick to `view` (every authenticated user can read it) and
26 // fall back to the REST `edit` context per-row only when the user
27 // opens the inline-edit affordance on a row they're allowed to
28 // edit.
29 $context = current_user_can( 'moderate_comments' ) ? 'edit' : 'view';
30 $args = array(
31 // Exactly the fields the conversation view renders — no more.
32 // Every `openstation_*` field is computed per row, so an
33 // over-broad `_fields` is a per-row query multiplier. The
34 // viewer-wide facts (`openstation_can_moderate`) and the reply
35 // counts (`openstation_replies_count`, one COUNT per row) are
36 // not requested: the app ships the first with its config and
37 // computes the second in one grouped query. The scoring fields
38 // (`spam_score`, `link_count`, `akismet`, `ai_verdict`) cost a
39 // meta read or worse per row and nothing in the view reads them.
40 // Widen the projection through the filter below when a plugin
41 // surfaces one of them.
42 '_fields' =>
43 'id,post,parent,author,author_name,author_avatar_urls,'
44 . 'date_gmt,content,status,'
45 . 'openstation_post_title,openstation_post_link,'
46 . 'openstation_can_edit',
47 'context' => $context,
48 'per_page' => 20,
49 // 'hold' = pending. Use the wp/v2 status names where they differ
50 // from core's: 'hold' / 'approve' / 'spam' / 'trash'.
51 'status' => 'hold',
52 );
53
54 /**
55 * Filter the default outbound REST query args for the Comments window.
56 *
57 * @param array $args Default args.
58 */
59 return (array) apply_filters( 'openstation_comments_window_query_args', $args );
60 }
61
62 /**
63 * Register the app's REST fields on the `comment` resource.
64 */
65 function openstation_comments_window_register_rest_fields() {
66 $readonly = static function ( $description, $type, array $extra = array() ) {
67 return array_merge(
68 array(
69 'description' => $description,
70 'type' => $type,
71 'context' => array( 'view', 'edit' ),
72 'readonly' => true,
73 ),
74 $extra
75 );
76 };
77
78 register_rest_field(
79 'comment',
80 'openstation_post_title',
81 array(
82 'get_callback' => static function ( $row ) {
83 $post_id = isset( $row['post'] ) ? (int) $row['post'] : 0;
84 if ( $post_id <= 0 ) {
85 return '';
86 }
87 $post = get_post( $post_id );
88 return $post ? (string) get_the_title( $post ) : '';
89 },
90 'schema' => $readonly( __( 'Title of the post the comment is attached to.', 'desktop-mode' ), 'string' ),
91 )
92 );
93
94 register_rest_field(
95 'comment',
96 'openstation_post_link',
97 array(
98 'get_callback' => static function ( $row ) {
99 $post_id = isset( $row['post'] ) ? (int) $row['post'] : 0;
100 return $post_id > 0 ? (string) get_permalink( $post_id ) : '';
101 },
102 'schema' => $readonly( __( 'Permalink of the post the comment is attached to.', 'desktop-mode' ), 'string', array( 'format' => 'uri' ) ),
103 )
104 );
105
106 register_rest_field(
107 'comment',
108 'openstation_spam_score',
109 array(
110 'get_callback' => static function ( $row ) {
111 $id = isset( $row['id'] ) ? (int) $row['id'] : 0;
112 return $id > 0 ? (int) openstation_comments_window_spam_score( $id ) : 0;
113 },
114 'schema' => $readonly(
115 __( 'Spam confidence score, 0–100.', 'desktop-mode' ),
116 'integer',
117 array(
118 'minimum' => 0,
119 'maximum' => 100,
120 )
121 ),
122 )
123 );
124
125 register_rest_field(
126 'comment',
127 'openstation_link_count',
128 array(
129 'get_callback' => static function ( $row ) {
130 $content = isset( $row['content']['raw'] )
131 ? (string) $row['content']['raw']
132 : ( isset( $row['content']['rendered'] ) ? (string) $row['content']['rendered'] : '' );
133 return (int) preg_match_all( '#https?://#i', $content );
134 },
135 'schema' => $readonly( __( 'Number of links in the comment.', 'desktop-mode' ), 'integer', array( 'minimum' => 0 ) ),
136 )
137 );
138
139 register_rest_field(
140 'comment',
141 'openstation_can_edit',
142 array(
143 'get_callback' => static function ( $row ) {
144 $id = isset( $row['id'] ) ? (int) $row['id'] : 0;
145 return $id > 0 && current_user_can( 'edit_comment', $id );
146 },
147 'schema' => $readonly( __( 'Whether the requester can edit this comment.', 'desktop-mode' ), 'boolean' ),
148 )
149 );
150
151 register_rest_field(
152 'comment',
153 'openstation_can_moderate',
154 array(
155 'get_callback' => static function () {
156 return current_user_can( 'moderate_comments' );
157 },
158 'schema' => $readonly( __( 'Whether the requester can moderate comments globally.', 'desktop-mode' ), 'boolean' ),
159 )
160 );
161
162 register_rest_field(
163 'comment',
164 'openstation_replies_count',
165 array(
166 'get_callback' => static function ( $row ) {
167 $id = isset( $row['id'] ) ? (int) $row['id'] : 0;
168 if ( $id <= 0 ) {
169 return 0;
170 }
171 return (int) get_comments(
172 array(
173 'parent' => $id,
174 'count' => true,
175 'status' => 'all',
176 )
177 );
178 },
179 'schema' => $readonly( __( 'Direct-reply count for this comment.', 'desktop-mode' ), 'integer', array( 'minimum' => 0 ) ),
180 )
181 );
182
183 register_rest_field(
184 'comment',
185 'openstation_ai_verdict',
186 array(
187 'get_callback' => static function ( $row ) {
188 $id = isset( $row['id'] ) ? (int) $row['id'] : 0;
189 if ( $id <= 0 || ! function_exists( 'openstation_ai_get_meta' ) ) {
190 return null;
191 }
192 $meta = openstation_ai_get_meta( 'comment', $id );
193 if ( ! is_array( $meta ) ) {
194 return null;
195 }
196 return array(
197 'spam' => ! empty( $meta['spam'] ),
198 'harmful' => ! empty( $meta['harmful'] ),
199 'topic' => isset( $meta['topic'] ) ? (string) $meta['topic'] : '',
200 'summary' => isset( $meta['ai_summary'] ) ? (string) $meta['ai_summary'] : '',
201 'analyzedAt' => isset( $meta['analyzed_at'] ) ? (int) $meta['analyzed_at'] : 0,
202 );
203 },
204 'schema' => $readonly( __( 'AI moderation verdict for this comment, when analyzed (null otherwise).', 'desktop-mode' ), array( 'object', 'null' ) ),
205 )
206 );
207
208 register_rest_field(
209 'comment',
210 'openstation_akismet',
211 array(
212 'get_callback' => static function ( $row ) {
213 $id = isset( $row['id'] ) ? (int) $row['id'] : 0;
214 if ( $id <= 0 ) {
215 return null;
216 }
217 // Akismet stores one of `true`, `false` or `pending`.
218 $result = (string) get_comment_meta( $id, 'akismet_result', true );
219 return '' === $result ? null : $result;
220 },
221 'schema' => $readonly( __( 'Akismet verdict if the plugin is installed (null otherwise).', 'desktop-mode' ), array( 'string', 'null' ) ),
222 )
223 );
224 }
225 add_action( 'rest_api_init', 'openstation_comments_window_register_rest_fields' );
226