Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
9 | class EEM_Message extends EEM_Base implements EEI_Query_Filter { |
||
10 | |||
11 | // private instance of the Message object |
||
12 | protected static $_instance = null; |
||
13 | |||
14 | |||
15 | /** |
||
16 | * This priority indicates a message should be generated and sent ASAP |
||
17 | * @type int |
||
18 | */ |
||
19 | const priority_high = 10; |
||
20 | |||
21 | |||
22 | |||
23 | |||
24 | /** |
||
25 | * This priority indicates a message should be generated ASAP and queued for sending. |
||
26 | * @type |
||
27 | */ |
||
28 | const priority_medium = 20; |
||
29 | |||
30 | |||
31 | |||
32 | |||
33 | /** |
||
34 | * This priority indicates a message should be queued for generating. |
||
35 | * @type int |
||
36 | */ |
||
37 | const priority_low = 30; |
||
38 | |||
39 | |||
40 | |||
41 | /** |
||
42 | * indicates this message was sent at the time modified |
||
43 | */ |
||
44 | const status_sent = 'MSN'; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * indicates this message is waiting to be sent |
||
49 | */ |
||
50 | const status_idle = 'MID'; |
||
51 | |||
52 | |||
53 | /** |
||
54 | * indicates an attempt was a made to send this message |
||
55 | * at the scheduled time, but it failed at the time modified. This differs from MDO status in that it will ALWAYs |
||
56 | * appear to the end user. |
||
57 | */ |
||
58 | const status_failed = 'MFL'; |
||
59 | |||
60 | |||
61 | /** |
||
62 | * indicates the message has been flagged for resending (at the time modified). |
||
63 | */ |
||
64 | const status_resend = 'MRS'; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * indicates the message has been flagged for generation but has not been generated yet. Messages always start as this |
||
69 | * status when added to the queue. |
||
70 | */ |
||
71 | const status_incomplete = 'MIC'; |
||
72 | |||
73 | |||
74 | |||
75 | |||
76 | /** |
||
77 | * Indicates everything was generated fine for the message, however, the messenger was unable to send. |
||
78 | * This status means that its possible to retry sending the message. |
||
79 | */ |
||
80 | const status_retry = 'MRT'; |
||
81 | |||
82 | |||
83 | |||
84 | /** |
||
85 | * This is for failed messages that are only displayed when `WP_DEBUG` is true. Usually this is used for more informational |
||
86 | * messages that may not indicate anything is broken. |
||
87 | */ |
||
88 | const status_debug_only = 'MDO'; |
||
89 | |||
90 | |||
91 | |||
92 | |||
93 | |||
94 | /** |
||
95 | * Private constructor to prevent direct creation. |
||
96 | * |
||
97 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and |
||
98 | * any incoming timezone data that gets saved). Note this just sends the timezone info to the |
||
99 | * date time model field objects. Default is null (and will be assumed using the set timezone |
||
100 | * in the 'timezone_string' wp option) |
||
101 | * |
||
102 | * @return EEM_Message |
||
|
|||
103 | */ |
||
104 | protected function __construct( $timezone = null ) { |
||
105 | $this->singular_item = __('Message','event_espresso'); |
||
106 | $this->plural_item = __('Messages','event_espresso'); |
||
107 | |||
108 | //used for token generator |
||
109 | EE_Registry::instance()->load_helper( 'URL' ); |
||
110 | |||
111 | $this->_tables = array( |
||
112 | 'Message'=>new EE_Primary_Table('esp_message','MSG_ID') |
||
113 | ); |
||
114 | |||
115 | $allowed_priority = array( |
||
116 | self::priority_high => __( 'high', 'event_espresso' ), |
||
117 | self::priority_medium => __( 'medium', 'event_espresso' ), |
||
118 | self::priority_low => __( 'low', 'event_espresso' ) |
||
119 | ); |
||
120 | |||
121 | $this->_fields = array( |
||
122 | 'Message'=>array( |
||
123 | 'MSG_ID'=>new EE_Primary_Key_Int_Field('MSG_ID', __('Message ID','event_espresso')), |
||
124 | 'MSG_token' => new EE_Plain_Text_Field( 'MSG_token', __('Unique Token used to represent this row in publicly viewable contexts (eg. a url).', 'event_espresso' ), false, EEH_URL::generate_unique_token() ), |
||
125 | 'GRP_ID'=>new EE_Foreign_Key_Int_Field( 'GRP_ID', __('Foreign key to the EEM_Message_Template_Group table.', 'event_espresso' ), true, 0, 'Message_Template_Group' ), |
||
126 | 'TXN_ID' => new EE_Foreign_Key_Int_Field( 'TXN_ID', __( 'Foreign key to the related EE_Transaction. This is required to give context for regenerating the specific message', 'event_espresso' ), true, 0, 'Transaction' ), |
||
127 | 'MSG_messenger' => new EE_Plain_Text_Field('MSG_messenger', __( 'Corresponds to the EE_messenger::name used to send this message. This will also be used to attempt any resending of the message.', 'event_espresso' ), false, 'email' ), |
||
128 | 'MSG_message_type' => new EE_Plain_Text_Field( 'MSG_message_type', __( 'Corresponds to the EE_message_type::name used to generate this message.', 'event_espresso' ), false, 'receipt' ), |
||
129 | 'MSG_context' => new EE_Plain_Text_Field( 'MSG_context', __( 'Context', 'event_espresso' ), false ), |
||
130 | 'MSG_recipient_ID' => new EE_Foreign_Key_Int_Field( 'MSG_recipient_ID', __( 'Recipient ID', 'event_espresso' ), true, null, array( 'Registration', 'Attendee', 'WP_User' ) ), |
||
131 | 'MSG_recipient_type' => new EE_Any_Foreign_Model_Name_Field( 'MSG_recipient_type', __( 'Recipient Type', 'event_espresso' ), true, null, array( 'Registration', 'Attendee', 'WP_User' ) ), |
||
132 | 'MSG_content' => new EE_Maybe_Serialized_Text_Field( 'MSG_content', __( 'Content', 'event_espresso' ), true, '' ), |
||
133 | 'MSG_to' => new EE_Maybe_Serialized_Text_Field( 'MSG_to', __( 'Address To', 'event_espresso' ), true ), |
||
134 | 'MSG_from' => new EE_Maybe_Serialized_Text_Field( 'MSG_from', __( 'Address From', 'event_espresso' ), true ), |
||
135 | 'MSG_subject' => new EE_Maybe_Serialized_Text_Field( 'MSG_subject', __( 'Subject', 'event_espresso' ), true, '' ), |
||
136 | 'MSG_priority' => new EE_Enum_Integer_Field( 'MSG_priority', __( 'Priority', 'event_espresso' ), false, self::priority_low, $allowed_priority ), |
||
137 | 'STS_ID' => new EE_Foreign_Key_String_Field( 'STS_ID', __( 'Status', 'event_espresso' ), false, self::status_incomplete, 'Status' ), |
||
138 | 'MSG_created' => new EE_Datetime_Field( 'MSG_created', __( 'Created', 'event_espresso' ), false, time() ), |
||
139 | 'MSG_modified' => new EE_Datetime_Field( 'MSG_modified', __( 'Modified', 'event_espresso' ), true, time() ) |
||
140 | ) |
||
141 | ); |
||
142 | $this->_model_relations = array( |
||
143 | 'Attendee' => new EE_Belongs_To_Any_Relation(), |
||
144 | 'Registration' => new EE_Belongs_To_Any_Relation(), |
||
145 | 'WP_User' => new EE_Belongs_To_Any_Relation(), |
||
146 | 'Message_Template_Group' => new EE_Belongs_To_Relation(), |
||
147 | 'Transaction' => new EE_Belongs_To_Relation() |
||
148 | ); |
||
149 | parent::__construct( $timezone ); |
||
150 | } |
||
151 | |||
152 | |||
153 | |||
154 | /** |
||
155 | * @return \EE_Message |
||
156 | */ |
||
157 | public function create_default_object() { |
||
158 | /** @type EE_Message $message */ |
||
159 | $message = parent::create_default_object(); |
||
160 | if ( $message instanceof EE_Message ) { |
||
161 | return EE_Message_Factory::set_messenger_and_message_type( $message ); |
||
162 | } |
||
163 | return null; |
||
164 | } |
||
165 | |||
166 | |||
167 | |||
168 | /** |
||
169 | * @param mixed $cols_n_values |
||
170 | * @return \EE_Message |
||
171 | */ |
||
172 | public function instantiate_class_from_array_or_object( $cols_n_values ) { |
||
173 | /** @type EE_Message $message */ |
||
174 | $message = parent::instantiate_class_from_array_or_object( $cols_n_values ); |
||
175 | if ( $message instanceof EE_Message ) { |
||
176 | return EE_Message_Factory::set_messenger_and_message_type( $message ); |
||
177 | } |
||
178 | return null; |
||
179 | } |
||
180 | |||
181 | |||
182 | |||
183 | /** |
||
184 | * Returns whether or not a message of that type was sent for a given attendee. |
||
185 | * @param EE_Attendee|int $attendee |
||
186 | * @param string $message_type the message type slug |
||
187 | * @return boolean |
||
188 | */ |
||
189 | View Code Duplication | public function message_sent_for_attendee( $attendee, $message_type ) { |
|
190 | $attendee_ID = EEM_Attendee::instance()->ensure_is_ID( $attendee ); |
||
191 | return $this->exists( array( array( |
||
192 | 'Attendee.ATT_ID' => $attendee_ID, |
||
193 | 'MSG_message_type' => $message_type, |
||
194 | 'STS_ID' => array( 'IN', $this->stati_indicating_sent() ) |
||
195 | ) ) ); |
||
196 | } |
||
197 | |||
198 | |||
199 | |||
200 | |||
201 | /** |
||
202 | * Returns whether or not a message of that type was sent for a given registration |
||
203 | * @param EE_Registration|int $registration |
||
204 | * @param string $message_type the message type slug |
||
205 | * @return boolean |
||
206 | */ |
||
207 | View Code Duplication | public function message_sent_for_registration( $registration, $message_type ) { |
|
208 | $registrationID = EEM_Registration::instance()->ensure_is_ID( $registration ); |
||
209 | return $this->exists( array( array( |
||
210 | 'Registration.REG_ID' => $registrationID, |
||
211 | 'MSG_message_type' => $message_type, |
||
212 | 'STS_ID' => array( 'IN', $this->stati_indicating_sent() ) |
||
213 | ) ) ); |
||
214 | } |
||
215 | |||
216 | |||
217 | |||
218 | |||
219 | /** |
||
220 | * This retrieves an EE_Message object from the db matching the given token string. |
||
221 | * @param string $token |
||
222 | * @return EE_Message |
||
223 | */ |
||
224 | public function get_one_by_token( $token ) { |
||
225 | return $this->get_one( array( array( |
||
226 | 'MSG_token' => $token |
||
227 | ) ) ); |
||
228 | } |
||
229 | |||
230 | |||
231 | /** |
||
232 | * Returns stati that indicate the message HAS been sent |
||
233 | * @return array of strings for possible stati |
||
234 | */ |
||
235 | public function stati_indicating_sent(){ |
||
236 | return apply_filters( 'FHEE__EEM_Message__stati_indicating_sent', array( self::status_sent ) ); |
||
237 | } |
||
238 | |||
239 | |||
240 | |||
241 | |||
242 | /** |
||
243 | * Returns stati that indicate the message is waiting to be sent. |
||
244 | * @return array of strings for possible stati. |
||
245 | */ |
||
246 | public function stati_indicating_to_send() { |
||
247 | return apply_filters( 'FHEE__EEM_Message__stati_indicating_to_send', array( self::status_idle, self::status_resend ) ); |
||
248 | } |
||
249 | |||
250 | |||
251 | /** |
||
252 | * Returns stati that indicate the message has failed sending |
||
253 | * @return array array of strings for possible stati. |
||
254 | */ |
||
255 | public function stati_indicating_failed_sending() { |
||
266 | |||
267 | |||
268 | |||
269 | |||
270 | /** |
||
271 | * Returns filterable array of all EEM_Message statuses. |
||
272 | * @return array |
||
273 | */ |
||
274 | public function all_statuses() { |
||
288 | |||
289 | /** |
||
290 | * Detects any specific query variables in the request and uses those to setup appropriate |
||
291 | * filter for any queries. |
||
292 | * @return array |
||
293 | */ |
||
294 | public function filter_by_query_params() { |
||
322 | |||
323 | |||
324 | |||
325 | |||
326 | |||
327 | public function get_pretty_label_for_results() { |
||
391 | |||
392 | |||
393 | |||
394 | |||
395 | /** |
||
396 | * This returns the array of expected variables for the EEI_Query_Filter methods being implemented |
||
397 | * The array is in the format: |
||
398 | * |
||
399 | * array( |
||
400 | * {$field_name} => {$model_name} |
||
401 | * ); |
||
402 | * |
||
403 | * @since 4.9.0 |
||
404 | * @return array |
||
405 | */ |
||
406 | protected function _expected_vars_for_query_inject() { |
||
415 | |||
416 | |||
417 | } |
||
418 | // End of file EEM_Message.model.php |
||
420 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.