-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommand-handling.php
More file actions
172 lines (140 loc) · 4 KB
/
Copy pathcommand-handling.php
File metadata and controls
172 lines (140 loc) · 4 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
<?php
/**
* Manage encoding and decoding of commands as message metadata.
* @since 1.0.0
*/
class Prompt_Command_Handling {
/**
* @var array Default metadata IDs to command class mapping
*/
protected static $class_map = array(
1 => 'Prompt_Comment_Command',
2 => 'Prompt_Register_Subscribe_Command',
3 => 'Prompt_New_Post_Comment_Command',
4 => 'Postmatic\Commentium\Commands\Comment_Moderation',
5 => 'Prompt_Comment_Flood_Command',
6 => 'Prompt_Confirmation_Command',
7 => 'Postmatic\Premium\Commands\Forward',
8 => 'Prompt_Post_Reply_Command',
9 => 'Postmatic\Premium\Commands\Post_Request',
);
/**
* Get the class associated with an encoded ID.
* @since 2.0.0
* @param int $id
* @return null|string class name or null if not found
*/
public static function get_class( $id ) {
if ( ! isset( self::$class_map[$id] ) ) {
return null;
}
return apply_filters( 'prompt/command_handling/get_class', self::$class_map[$id], $id );
}
/**
* Get the encoded ID for a class.
* @since 2.0.0
* @param string $class
* @return null|int class ID or null if not found
*/
public static function get_class_id( $class ) {
$class_to_ids = array_flip( self::$class_map );
if ( isset( $class_to_ids[$class] ) ) {
return $class_to_ids[$class];
}
// Map subclasses to their parent class ID
foreach( $class_to_ids as $our_class => $class_id ) {
if ( is_subclass_of( $class, $our_class ) ) {
return $class_id;
}
}
return null;
}
/**
* Create a command from message data.
* @param object $update Message data in prompt format.
* @return Prompt_Interface_Command
*/
public static function make_command( $update ) {
$metadata = $update->metadata;
if ( !isset( $metadata->ids ) )
return null;
$data = $metadata->ids;
$class_id = array_shift( $data );
$class = self::get_class( $class_id );
if ( ! $class ) {
Prompt_Logging::add_error(
'invalid_command_id',
__( 'Received a reply with invalid command data.', 'Postmatic' ),
$update
);
return null;
}
/** @var Prompt_Interface_Command $command */
$command = new $class;
$command->set_keys( $data );
$command->set_message( $update );
return $command;
}
/**
* Get the metadata required to reproduce a command instance.
*
* @since 2.0.0
*
* @param Prompt_Interface_Command $command
* @return stdClass
*/
public static function get_command_metadata( Prompt_Interface_Command $command ) {
$metadata = new stdClass();
$class = get_class( $command );
$class_id = self::get_class_id( $class );
if ( is_null( $class_id ) ) {
Prompt_Logging::add_error(
'invalid_command_id',
__( 'Tried to create an email with an unrecognized reply command.', 'Postmatic' ),
compact( 'command', 'email' )
);
return $metadata;
}
$data = $command->get_keys();
array_unshift( $data, $class_id );
$metadata->ids = $data;
return $metadata;
}
/**
* Shorthand method to get metadata for a comment command.
*
* @since 2.0.0
*
* @param int $user_id
* @param int $post_id
* @param int $parent_comment_id
* @return stdClass
*/
public static function get_comment_command_metadata( $user_id, $post_id, $parent_comment_id = null ) {
$command = new Prompt_Comment_Command();
$command->set_user_id( $user_id );
$command->set_post_id( $post_id );
if ( $parent_comment_id ) {
$command->set_parent_comment_id( $parent_comment_id );
}
return self::get_command_metadata( $command );
}
/**
* Generate comment reply address macros from comments and the replying user ID.
*
* @since 2.0.0
*
* @param array $comments
* @param int $replier_id
* @return array
*/
public static function get_comment_reply_macros( array $comments, $replier_id ) {
$macros = array();
foreach ( $comments as $comment ) {
$macros['reply_to_comment_'. $comment->comment_ID] = Prompt_Email_Batch::trackable_address(
self::get_comment_command_metadata( $replier_id, $comment->comment_post_ID, $comment->comment_ID )
);
}
return $macros;
}
}