Repository navigation
Expand file tree
/
Copy pathclass-connector-posts.php
More file actions
421 lines (374 loc) · 12 KB
/
Copy pathclass-connector-posts.php
File metadata and controls
421 lines (374 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/**
* Connector for Posts
*
* @package WP_Stream
*/
namespace WP_Stream;
/**
* Class - Connector_Posts
*/
class Connector_Posts extends Connector {
/**
* Connector slug
*/
public string $name = 'posts';
/**
* Actions registered for this connector
*/
public array $actions = array(
'transition_post_status',
'deleted_post',
);
/**
* Return translated connector label
*
* @return string Translated connector label
*/
public function get_label() {
return esc_html__( 'Posts', 'stream' );
}
/**
* Return translated action labels
*
* @return array Action label translations
*/
public function get_action_labels() {
return array(
'updated' => esc_html__( 'Updated', 'stream' ),
'created' => esc_html__( 'Created', 'stream' ),
'trashed' => esc_html__( 'Trashed', 'stream' ),
'untrashed' => esc_html__( 'Restored', 'stream' ),
'deleted' => esc_html__( 'Deleted', 'stream' ),
);
}
/**
* Return translated context labels
*
* @return array Context label translations
*/
public function get_context_labels() {
global $wp_post_types;
$post_types = wp_filter_object_list( $wp_post_types, array(), null, 'label' );
$post_types = array_diff_key( $post_types, array_flip( $this->get_excluded_post_types() ) );
add_action( 'registered_post_type', array( $this, 'registered_post_type' ), 10, 2 );
return $post_types;
}
/**
* Add action links to Stream drop row in admin list screen
*
* @filter wp_stream_action_links_{connector}
*
* @param array $links Previous links registered.
* @param Record $record Stream record.
*
* @return array Action links
*/
public function action_links( $links, $record ) {
$post = get_post( $record->object_id );
if ( $post && $post->post_status === $record->get_meta( 'new_status', true ) ) {
$post_type_name = $this->get_post_type_name( get_post_type( $post->ID ) );
if ( 'trash' === $post->post_status ) {
$untrash = wp_nonce_url(
add_query_arg(
array(
'action' => 'untrash',
'post' => $post->ID,
),
admin_url( 'post.php' )
),
sprintf( 'untrash-post_%d', $post->ID )
);
$delete = wp_nonce_url(
add_query_arg(
array(
'action' => 'delete',
'post' => $post->ID,
),
admin_url( 'post.php' )
),
sprintf( 'delete-post_%d', $post->ID )
);
/* translators: %s: a post type singular name (e.g. "Post") */
$links[ sprintf( esc_html_x( 'Restore %s', 'Post type singular name', 'stream' ), $post_type_name ) ] = $untrash;
/* translators: %s: a post type singular name (e.g. "Post") */
$links[ sprintf( esc_html_x( 'Delete %s Permanently', 'Post type singular name', 'stream' ), $post_type_name ) ] = $delete;
} else {
/* translators: %s a post type singular name (e.g. "Post") */
$links[ sprintf( esc_html_x( 'Edit %s', 'Post type singular name', 'stream' ), $post_type_name ) ] = get_edit_post_link( $post->ID );
$view_link = get_permalink( $post->ID );
if ( $view_link ) {
$links[ esc_html__( 'View', 'stream' ) ] = $view_link;
}
$revision_id = absint( $record->get_meta( 'revision_id', true ) );
$revision_id = $this->get_adjacent_post_revision( $revision_id, false );
if ( $revision_id ) {
$links[ esc_html__( 'Revision', 'stream' ) ] = get_edit_post_link( $revision_id );
}
}
}
return $links;
}
/**
* Catch registration of post_types after initial loading, to cache its labels
*
* @action registered_post_type
*
* @param string $post_type Post type slug.
* @param array $args Arguments used to register the post type.
*/
public function registered_post_type( $post_type, $args ) {
unset( $args );
$post_type_obj = get_post_type_object( $post_type );
$label = $post_type_obj->label;
wp_stream_get_instance()->connectors->term_labels['stream_context'][ $post_type ] = $label;
}
/**
* Log all post status changes ( creating / updating / trashing )
*
* @action transition_post_status
*
* @param mixed $new_status New status.
* @param mixed $old_status Old status.
* @param \WP_Post $post Post object.
*/
public function callback_transition_post_status( $new_status, $old_status, $post ) {
if ( in_array( $post->post_type, $this->get_excluded_post_types(), true ) ) {
return;
}
// We don't want the meta box update request, just the post update.
if ( ! empty( wp_stream_filter_input( INPUT_GET, 'meta-box-loader' ) ) ) {
return;
}
$start_statuses = array( 'auto-draft', 'inherit', 'new' );
if ( in_array( $new_status, $start_statuses, true ) ) {
return;
} elseif ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
} elseif ( 'draft' === $new_status && 'publish' === $old_status ) {
/* translators: %1$s: a post title, %2$s: a post type singular name (e.g. "Hello World", "Post") */
$summary = _x(
'"%1$s" %2$s unpublished',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'trash' === $old_status && 'trash' !== $new_status ) {
/* translators: %1$s: a post title, %2$s: a post type singular name (e.g. "Hello World", "Post") */
$summary = _x(
'"%1$s" %2$s restored from trash',
'1: Post title, 2: Post type singular name',
'stream'
);
$action = 'untrashed';
} elseif ( 'draft' === $new_status && 'draft' === $old_status ) {
/* translators: %1$s: a post title, %2$s: a post type singular name (e.g. "Hello World", "Post") */
$summary = _x(
'"%1$s" %2$s draft saved',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'publish' === $new_status && ! in_array( $old_status, array( 'future', 'publish' ), true ) ) {
/* translators: %1$s: a post title, %2$s: a post type singular name (e.g. "Hello World", "Post") */
$summary = _x(
'"%1$s" %2$s published',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'draft' === $new_status ) {
/* translators: %1$s: a post title, %2$s a post type singular name (e.g. "Hello World", "Post") */
$summary = _x(
'"%1$s" %2$s drafted',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'pending' === $new_status ) {
/* translators: %1$s: a post title, %2$s: a post type singular name (e.g. "Hello World", "Post") */
$summary = _x(
'"%1$s" %2$s pending review',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'future' === $new_status ) {
/* translators: %1$s: a post title, %2$s: a post type singular name (e.g. "Hello World", "Post") */
$summary = _x(
'"%1$s" %2$s scheduled for %3$s',
'1: Post title, 2: Post type singular name, 3: Scheduled post date',
'stream'
);
} elseif ( 'future' === $old_status && 'publish' === $new_status ) {
/* translators: %1$s: a post title, %2$s: a post type singular name (e.g. "Hello World", "Post") */
$summary = _x(
'"%1$s" scheduled %2$s published',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'private' === $new_status ) {
/* translators: %1$s: a post title, %2$s: a post type singular name (e.g. "Hello World", "Post") */
$summary = _x(
'"%1$s" %2$s privately published',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'trash' === $new_status ) {
/* translators: %1$s: a post title, %2$s: a post type singular name (e.g. "Hello World", "Post") */
$summary = _x(
'"%1$s" %2$s trashed',
'1: Post title, 2: Post type singular name',
'stream'
);
$action = 'trashed';
} else {
/* translators: %1$s: a post title, %2$s: a post type singular name (e.g. "Hello World", "Post") */
$summary = _x(
'"%1$s" %2$s updated',
'1: Post title, 2: Post type singular name',
'stream'
);
}
if ( in_array( $old_status, $start_statuses, true ) && ! in_array( $new_status, $start_statuses, true ) ) {
$action = 'created';
}
if ( empty( $action ) ) {
$action = 'updated';
}
$revision_id = null;
if ( wp_revisions_enabled( $post ) ) {
$revision = get_children(
array(
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => $post->ID,
'posts_per_page' => 1, // VIP safe.
'orderby' => 'post_date',
'order' => 'DESC',
)
);
if ( $revision ) {
$revision = array_values( $revision );
$revision_id = $revision[0]->ID;
}
}
$post_type_name = strtolower( $this->get_post_type_name( $post->post_type ) );
$this->log(
$summary,
array(
'post_title' => $post->post_title,
'singular_name' => $post_type_name,
'post_date' => $post->post_date,
'post_date_gmt' => $post->post_date_gmt,
'new_status' => $new_status,
'old_status' => $old_status,
'revision_id' => $revision_id,
),
$post->ID,
$post->post_type,
$action
);
}
/**
* Log post deletion
*
* @action deleted_post
*
* @param integer $post_id Post ID.
*/
public function callback_deleted_post( $post_id ) {
$post = get_post( $post_id );
// We check if post is an instance of WP_Post as it doesn't always resolve in unit testing.
if ( ! ( $post instanceof \WP_Post ) || in_array( $post->post_type, $this->get_excluded_post_types(), true ) ) {
return;
}
// Ignore auto-drafts that are deleted by the system, see issue-293.
if ( 'auto-draft' === $post->post_status ) {
return;
}
$post_type_name = strtolower( $this->get_post_type_name( $post->post_type ) );
$this->log(
/* translators: %1$s: a post title, %2$s: a post type singular name (e.g. "Hello World", "Post") */
_x(
'"%1$s" %2$s deleted from trash',
'1: Post title, 2: Post type singular name',
'stream'
),
array(
'post_title' => $post->post_title,
'singular_name' => $post_type_name,
),
$post->ID,
$post->post_type,
'deleted'
);
}
/**
* Constructs list of excluded post types for the Posts connector
*
* @return array List of excluded post types
*/
public function get_excluded_post_types() {
/**
* Filter excluded post types for the Posts connector.
*
* @param array $excluded_post_types
*/
return apply_filters(
'wp_stream_posts_exclude_post_types',
array(
'nav_menu_item',
'attachment',
'revision',
)
);
}
/**
* Gets the singular post type label
*
* @param string $post_type_slug Post type slug.
*
* @return string Post type label
*/
public function get_post_type_name( $post_type_slug ) {
$name = esc_html__( 'Post', 'stream' ); // Default.
if ( post_type_exists( $post_type_slug ) ) {
$post_type = get_post_type_object( $post_type_slug );
$name = $post_type->labels->singular_name;
}
return $name;
}
/**
* Get an adjacent post revision ID
*
* @param int $revision_id Revision ID.
* @param bool $previous Has there been any revision before this one?.
*
* @return int $revision_id
*/
public function get_adjacent_post_revision( $revision_id, $previous = true ) {
if ( empty( $revision_id ) || ! wp_is_post_revision( $revision_id ) ) {
return false;
}
$revision = wp_get_post_revision( $revision_id );
$operator = ( $previous ) ? '<' : '>';
$order = ( $previous ) ? 'DESC' : 'ASC';
global $wpdb;
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- reason: $operator/$order are fixed comparators from a boolean; values are prepared. Adjacent-revision lookup cannot use WP_Query.
$revision_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT p.ID
FROM $wpdb->posts AS p
WHERE p.post_date {$operator} %s
AND p.post_type = 'revision'
AND p.post_parent = %d
ORDER BY p.post_date {$order}
LIMIT 1",
$revision->post_date,
$revision->post_parent
)
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$revision_id = absint( $revision_id );
if ( ! wp_is_post_revision( $revision_id ) ) {
return false;
}
return $revision_id;
}
}