-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomment.php
More file actions
226 lines (198 loc) · 5.1 KB
/
Copy pathcomment.php
File metadata and controls
226 lines (198 loc) · 5.1 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
<?php
/**
* Prompt behavior specific to a comment.
*
* Encapsulates a WordPress comment, since WordPress doesn't allow extension.
*
* @since 2.0.0
*/
class Prompt_Comment {
/** @var string */
protected static $recipient_ids_meta_key = 'prompt_recipient_ids';
/** @var string */
protected static $sent_meta_key = 'prompt_sent_ids';
/** @var string */
protected static $failed_meta_key = 'prompt_failed_ids';
/** @var string */
protected static $outbound_message_batch_ids_meta_key = 'prompt_outbound_message_batch_ids';
/** @var string */
protected static $subscription_requested_meta_key = 'prompt_comment_subscribe';
/** @var int comment ID */
protected $id;
/** @var object|WP_Comment comment object */
protected $wp_comment;
/** @var WP_User */
protected $author_user;
/**
* Create a Prompt comment.
*
* @since 2.0.0
*
* @param int|object|WP_Comment $comment_id_or_object
*/
public function __construct( $comment_id_or_object ) {
if ( is_object( $comment_id_or_object ) ) {
$this->wp_comment = $comment_id_or_object;
$this->id = $this->wp_comment->comment_ID;
} else {
$this->id = intval( $comment_id_or_object );
}
}
/**
* Get the WordPress comment ID.
*
* @since 2.0.0
* @return int
*/
public function id() {
return $this->id;
}
/**
* Get the underlying comment.
*
* @since 1.0.0
* @return null|object|WP_Comment
*/
public function get_wp_comment() {
if ( !isset( $this->wp_comment ) ) {
$this->wp_comment = get_comment( $this->id );
}
return $this->wp_comment;
}
/**
* @since 2.0.0
* @return array
*/
public function get_recipient_ids() {
$ids = get_comment_meta( $this->id, self::$recipient_ids_meta_key, true );
return $ids ? $ids : array();
}
/**
* @since 2.0.0
* @param array $ids
* @return $this
*/
public function set_recipient_ids( array $ids ) {
update_comment_meta( $this->id, self::$recipient_ids_meta_key, $ids );
return $this;
}
/**
* @since 2.0.0
* @return array
*/
public function get_sent_subscriber_ids() {
$ids = get_comment_meta( $this->id, self::$sent_meta_key, true );
return $ids ? $ids : array();
}
/**
* @since 2.0.0
* @param array $ids
* @return $this
*/
public function set_sent_subscriber_ids( array $ids ) {
update_comment_meta( $this->id, self::$sent_meta_key, $ids );
return $this;
}
/**
* @since 2.0.14
* @return array
*/
public function get_failed_subscriber_ids() {
$ids = get_comment_meta( $this->id, self::$failed_meta_key, true );
return $ids ? $ids : array();
}
/**
* @since 2.0.14
* @param array $ids
* @return $this
*/
public function set_failed_subscriber_ids( array $ids ) {
update_comment_meta( $this->id, self::$failed_meta_key, $ids );
return $this;
}
/**
* @since 2.0.14
* @param array $ids
* @return $this
*/
public function add_failed_subscriber_ids( array $ids ) {
return $this->set_failed_subscriber_ids( array_unique( array_merge( $this->get_failed_subscriber_ids(), $ids ) ) );
}
/**
* @since 2.0.14
* @param array $ids
* @return $this
*/
public function remove_failed_subscriber_ids( array $ids ) {
return $this->set_failed_subscriber_ids( array_diff( $this->get_failed_subscriber_ids(), $ids ) );
}
/**
* @since 2.0.0
* @return array
*/
public function get_sent_batch_ids() {
$ids = get_comment_meta( $this->id, self::$outbound_message_batch_ids_meta_key, true );
return $ids ? $ids : array();
}
/**
* @since 2.0.0
* @param int $id
* @return $this
*/
public function add_sent_batch_id( $id ) {
$ids = $this->get_sent_batch_ids();
$ids[] = intval( $id );
update_comment_meta( $this->id, self::$outbound_message_batch_ids_meta_key, $ids );
return $this;
}
/**
* @since 2.0.0
* @return $this
*/
public function set_subscription_requested() {
update_comment_meta( $this->id, self::$subscription_requested_meta_key, true );
return $this;
}
/**
* @since 2.0.0
* @return bool
*/
public function get_subscription_requested() {
return (bool) get_comment_meta( $this->id, self::$subscription_requested_meta_key, true );
}
/**
* Get the comment author user if there is one.
*
* @since 2.0.0
*
* @return bool|WP_User Author user or false if none is found.
*/
public function get_author_user() {
if ( $this->author_user ) {
return $this->author_user;
}
$user = get_user_by( 'id', $this->get_wp_comment()->user_id );
$this->author_user = $user ? $user : get_user_by( 'email', $this->get_wp_comment()->comment_author_email );
return $this->author_user;
}
/**
* Get the author user display name, comment form author name, or Anonymous.
* @since 2.0.0
* @return string
*/
public function get_author_name() {
$author = $this->get_author_user();
if ( $author ) {
return $author->display_name;
}
$name_from_form =$this->get_wp_comment()->comment_author;
return $name_from_form ? $name_from_form : __( 'Anonymous', 'Postmatic' );
}
/**
* @since 2.0.0
* @return bool
*/
public function author_can_subscribe() {
return (bool) ( $this->get_wp_comment()->user_id or is_email( $this->get_wp_comment()->comment_author_email ) );
}
}