-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-map-co-authors.php
More file actions
151 lines (135 loc) · 3.95 KB
/
class-map-co-authors.php
File metadata and controls
151 lines (135 loc) · 3.95 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
<?php
/**
* Co-authors storage and retrieval.
*
* @package MultiAuthorPosts
*/
namespace MultiAuthorPosts;
/**
* Manages the list of co-authors stored against each post.
*/
class Co_Authors {
const META_KEY = '_map_co_authors';
/**
* Register post meta on init.
*/
public static function init(): void {
add_action( 'init', array( __CLASS__, 'register_meta' ) );
add_action( 'post_updated', array( __CLASS__, 'preserve_previous_author' ), 10, 3 );
}
/**
* Register the co-authors post meta.
*/
public static function register_meta(): void {
register_post_meta(
'',
self::META_KEY,
array(
'type' => 'array',
'description' => 'Co-author user IDs for this post.',
'single' => true,
'default' => array(),
'show_in_rest' => false,
'auth_callback' => function ( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
},
)
);
}
/**
* Return the list of co-author user IDs for a post.
*
* @param int $post_id Post ID.
* @return int[]
*/
public static function get_co_authors( int $post_id ): array {
$value = get_post_meta( $post_id, self::META_KEY, true );
if ( ! is_array( $value ) ) {
return array();
}
return array_map( 'intval', $value );
}
/**
* Add a user as a co-author of a post.
*
* No-ops silently if the user is already the post author or already a co-author.
*
* @param int $post_id Post ID.
* @param int $user_id User ID.
* @return bool Whether the operation succeeded.
*/
public static function add_co_author( int $post_id, int $user_id ): bool {
$post = get_post( $post_id );
if ( ! $post ) {
return false;
}
if ( ! get_userdata( $user_id ) ) {
return false;
}
// The post's original author never needs to be stored as a co-author.
if ( (int) $post->post_author === $user_id ) {
return true;
}
$co_authors = self::get_co_authors( $post_id );
if ( in_array( $user_id, $co_authors, true ) ) {
return true;
}
$co_authors[] = $user_id;
return (bool) update_post_meta( $post_id, self::META_KEY, $co_authors );
}
/**
* Remove a user from the co-author list of a post.
*
* @param int $post_id Post ID.
* @param int $user_id User ID.
* @return bool Whether the operation succeeded.
*/
public static function remove_co_author( int $post_id, int $user_id ): bool {
$co_authors = self::get_co_authors( $post_id );
$filtered = array_values( array_filter( $co_authors, fn( $id ) => $id !== $user_id ) );
return (bool) update_post_meta( $post_id, self::META_KEY, $filtered );
}
/**
* Check whether a user is a co-author of a post.
*
* @param int $post_id Post ID.
* @param int $user_id User ID.
* @return bool
*/
public static function is_co_author( int $post_id, int $user_id ): bool {
return in_array( $user_id, self::get_co_authors( $post_id ), true );
}
/**
* Return enriched co-author data (id, name, avatar) for a post.
*
* @param int $post_id Post ID.
* @return array<int, array{id:int,name:string,avatar:string}>
*/
public static function get_co_author_data( int $post_id ): array {
$result = array();
foreach ( self::get_co_authors( $post_id ) as $user_id ) {
$user = get_userdata( $user_id );
if ( ! $user ) {
continue;
}
$result[] = array(
'id' => $user->ID,
'name' => $user->display_name,
'avatar' => get_avatar_url( $user->ID, array( 'size' => 48 ) ),
);
}
return $result;
}
/**
* When post authorship changes, preserve the previous author as a co-author.
*
* @param int $post_id Post ID.
* @param \WP_Post $post_after Post object after the update.
* @param \WP_Post $post_before Post object before the update.
*/
public static function preserve_previous_author( int $post_id, \WP_Post $post_after, \WP_Post $post_before ): void {
if ( (int) $post_before->post_author !== (int) $post_after->post_author ) {
self::add_co_author( $post_id, (int) $post_before->post_author );
}
}
}