/
class-connector-posts.php
389 lines (344 loc) · 9.48 KB
/
class-connector-posts.php
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
<?php
namespace WP_Stream;
class Connector_Posts extends Connector {
/**
* Connector slug
*
* @var string
*/
public $name = 'posts';
/**
* Actions registered for this connector
*
* @var array
*/
public $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 )
);
$links[ sprintf( esc_html_x( 'Restore %s', 'Post type singular name', 'stream' ), $post_type_name ) ] = $untrash;
$links[ sprintf( esc_html_x( 'Delete %s Permenantly', 'Post type singular name', 'stream' ), $post_type_name ) ] = $delete;
} else {
$links[ sprintf( esc_html_x( 'Edit %s', 'Post type singular name', 'stream' ), $post_type_name ) ] = get_edit_post_link( $post->ID );
if ( $view_link = get_permalink( $post->ID ) ) {
$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 registeration 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
* @param mixed $old
* @param \WP_Post $post
*/
public function callback_transition_post_status( $new, $old, $post ) {
if ( in_array( $post->post_type, $this->get_excluded_post_types() ) ) {
return;
}
if ( in_array( $new, array( 'auto-draft', 'inherit' ) ) ) {
return;
} elseif ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
} elseif ( 'draft' === $new && 'publish' === $old ) {
$summary = _x(
'"%1$s" %2$s unpublished',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'trash' === $old && 'trash' !== $new ) {
$summary = _x(
'"%1$s" %2$s restored from trash',
'1: Post title, 2: Post type singular name',
'stream'
);
$action = 'untrashed';
} elseif ( 'draft' === $new && 'draft' === $old ) {
$summary = _x(
'"%1$s" %2$s draft saved',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'publish' === $new && 'draft' === $old ) {
$summary = _x(
'"%1$s" %2$s published',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'draft' === $new ) {
$summary = _x(
'"%1$s" %2$s drafted',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'pending' === $new ) {
$summary = _x(
'"%1$s" %2$s pending review',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'future' === $new ) {
$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 && 'publish' === $new ) {
$summary = _x(
'"%1$s" scheduled %2$s published',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'private' === $new ) {
$summary = _x(
'"%1$s" %2$s privately published',
'1: Post title, 2: Post type singular name',
'stream'
);
} elseif ( 'trash' === $new ) {
$summary = _x(
'"%1$s" %2$s trashed',
'1: Post title, 2: Post type singular name',
'stream'
);
$action = 'trashed';
} else {
$summary = _x(
'"%1$s" %2$s updated',
'1: Post title, 2: Post type singular name',
'stream'
);
}
if ( 'auto-draft' === $old && 'auto-draft' !== $new ) {
$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,
'old_status' => $old,
'revision_id' => $revision_id,
),
$post->ID,
$post->post_type,
$action
);
}
/**
* Log post deletion
*
* @action deleted_post
*
* $param integer $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() ) ) {
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(
_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() {
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
*
* @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
* @param bool $previous
*
* @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;
$revision_id = $wpdb->get_var( // db call okay
// @codingStandardsIgnoreStart
$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
)
// @codingStandardsIgnoreEnd prepare okay
);
$revision_id = absint( $revision_id );
if ( ! wp_is_post_revision( $revision_id ) ) {
return false;
}
return $revision_id;
}
}