@@ -15,571 +15,571 @@ |
||
15 | 15 | abstract class EEM_CPT_Base extends EEM_Soft_Delete_Base |
16 | 16 | { |
17 | 17 | |
18 | - const EVENT_CATEGORY_TAXONOMY = 'espresso_event_categories'; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var string post_status_publish - the wp post status for published CPTs |
|
22 | - */ |
|
23 | - const post_status_publish = 'publish'; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var string post_status_future - the wp post status for scheduled CPTs |
|
27 | - */ |
|
28 | - const post_status_future = 'future'; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var string post_status_draft - the wp post status for draft CPTs |
|
32 | - */ |
|
33 | - const post_status_draft = 'draft'; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var string post_status_pending - the wp post status for pending CPTs |
|
37 | - */ |
|
38 | - const post_status_pending = 'pending'; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var string post_status_private - the wp post status for private CPTs |
|
42 | - */ |
|
43 | - const post_status_private = 'private'; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var string post_status_trashed - the wp post status for trashed CPTs |
|
47 | - */ |
|
48 | - const post_status_trashed = 'trash'; |
|
49 | - |
|
50 | - /** |
|
51 | - * This is an array of custom statuses for the given CPT model (modified by children) |
|
52 | - * format: |
|
53 | - * array( |
|
54 | - * 'status_name' => array( |
|
55 | - * 'label' => esc_html__('Status Name', 'event_espresso'), |
|
56 | - * 'public' => TRUE //whether a public status or not. |
|
57 | - * ) |
|
58 | - * ) |
|
59 | - * |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - protected $_custom_stati = []; |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * Adds a relationship to Term_Taxonomy for each CPT_Base |
|
67 | - * |
|
68 | - * @param string|null $timezone |
|
69 | - * @throws EE_Error |
|
70 | - */ |
|
71 | - protected function __construct(string $timezone = '') |
|
72 | - { |
|
73 | - // adds a relationship to Term_Taxonomy for all these models. For this to work |
|
74 | - // Term_Relationship must have a relation to each model subclassing EE_CPT_Base explicitly |
|
75 | - // eg, in EEM_Term_Relationship, inside the _model_relations array, there must be an entry |
|
76 | - // with key equalling the subclassing model's model name (eg 'Event' or 'Venue'), and the value |
|
77 | - // must also be new EE_HABTM_Relation('Term_Relationship'); |
|
78 | - $this->_model_relations['Term_Taxonomy'] = new EE_HABTM_Relation('Term_Relationship'); |
|
79 | - $primary_table_name = null; |
|
80 | - // add the common _status field to all CPT primary tables. |
|
81 | - foreach ($this->_tables as $alias => $table_obj) { |
|
82 | - if ($table_obj instanceof EE_Primary_Table) { |
|
83 | - $primary_table_name = $alias; |
|
84 | - } |
|
85 | - } |
|
86 | - // set default wp post statuses if child has not already set. |
|
87 | - if (! isset($this->_fields[ $primary_table_name ]['status'])) { |
|
88 | - $this->_fields[ $primary_table_name ]['status'] = new EE_WP_Post_Status_Field( |
|
89 | - 'post_status', |
|
90 | - esc_html__("Event Status", "event_espresso"), |
|
91 | - false, |
|
92 | - 'draft' |
|
93 | - ); |
|
94 | - } |
|
95 | - if (! isset($this->_fields[ $primary_table_name ]['to_ping'])) { |
|
96 | - $this->_fields[ $primary_table_name ]['to_ping'] = new EE_DB_Only_Text_Field( |
|
97 | - 'to_ping', |
|
98 | - esc_html__('To Ping', 'event_espresso'), |
|
99 | - false, |
|
100 | - '' |
|
101 | - ); |
|
102 | - } |
|
103 | - if (! isset($this->_fields[ $primary_table_name ]['pinged'])) { |
|
104 | - $this->_fields[ $primary_table_name ]['pinged'] = new EE_DB_Only_Text_Field( |
|
105 | - 'pinged', |
|
106 | - esc_html__('Pinged', 'event_espresso'), |
|
107 | - false, |
|
108 | - '' |
|
109 | - ); |
|
110 | - } |
|
111 | - if (! isset($this->_fields[ $primary_table_name ]['comment_status'])) { |
|
112 | - $this->_fields[ $primary_table_name ]['comment_status'] = new EE_Plain_Text_Field( |
|
113 | - 'comment_status', |
|
114 | - esc_html__('Comment Status', 'event_espresso'), |
|
115 | - false, |
|
116 | - 'open' |
|
117 | - ); |
|
118 | - } |
|
119 | - if (! isset($this->_fields[ $primary_table_name ]['ping_status'])) { |
|
120 | - $this->_fields[ $primary_table_name ]['ping_status'] = new EE_Plain_Text_Field( |
|
121 | - 'ping_status', |
|
122 | - esc_html__('Ping Status', 'event_espresso'), |
|
123 | - false, |
|
124 | - 'open' |
|
125 | - ); |
|
126 | - } |
|
127 | - if (! isset($this->_fields[ $primary_table_name ]['post_content_filtered'])) { |
|
128 | - $this->_fields[ $primary_table_name ]['post_content_filtered'] = new EE_DB_Only_Text_Field( |
|
129 | - 'post_content_filtered', |
|
130 | - esc_html__('Post Content Filtered', 'event_espresso'), |
|
131 | - false, |
|
132 | - '' |
|
133 | - ); |
|
134 | - } |
|
135 | - if (! isset($this->_model_relations['Post_Meta'])) { |
|
136 | - // don't block deletes though because we want to maintain the current behaviour |
|
137 | - $this->_model_relations['Post_Meta'] = new EE_Has_Many_Relation(false); |
|
138 | - } |
|
139 | - if (! $this->_minimum_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
140 | - // nothing was set during child constructor, so set default |
|
141 | - $this->_minimum_where_conditions_strategy = new EE_CPT_Minimum_Where_Conditions($this->post_type()); |
|
142 | - } |
|
143 | - if (! $this->_default_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
144 | - // nothing was set during child constructor, so set default |
|
145 | - // it's ok for child classes to specify this, but generally this is more DRY |
|
146 | - $this->_default_where_conditions_strategy = new EE_CPT_Where_Conditions($this->post_type()); |
|
147 | - } |
|
148 | - parent::__construct($timezone); |
|
149 | - } |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * @return array |
|
154 | - */ |
|
155 | - public function public_event_stati(): array |
|
156 | - { |
|
157 | - // @see wp-includes/post.php |
|
158 | - return get_post_stati(['public' => true]); |
|
159 | - } |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * Searches for field on this model of type 'deleted_flag'. if it is found, |
|
164 | - * returns it's name. BUT That doesn't apply to CPTs. We should instead use post_status_field_name |
|
165 | - * |
|
166 | - * @return void |
|
167 | - * @throws EE_Error |
|
168 | - */ |
|
169 | - public function deleted_field_name() |
|
170 | - { |
|
171 | - throw new EE_Error( |
|
172 | - esc_html__( |
|
173 | - 'EEM_CPT_Base should not call deleted_field_name! It should instead use post_status_field_name', |
|
174 | - 'event_espresso' |
|
175 | - ) |
|
176 | - ); |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * Gets the field's name that sets the post status |
|
182 | - * |
|
183 | - * @return string |
|
184 | - * @throws EE_Error |
|
185 | - */ |
|
186 | - public function post_status_field_name(): string |
|
187 | - { |
|
188 | - $field = $this->get_a_field_of_type('EE_WP_Post_Status_Field'); |
|
189 | - if ($field) { |
|
190 | - return $field->get_name(); |
|
191 | - } else { |
|
192 | - throw new EE_Error( |
|
193 | - sprintf( |
|
194 | - esc_html__( |
|
195 | - 'We are trying to find the post status flag field on %s, but none was found. Are you sure there is a field of type EE_Trashed_Flag_Field in %s constructor?', |
|
196 | - 'event_espresso' |
|
197 | - ), |
|
198 | - get_class($this), |
|
199 | - get_class($this) |
|
200 | - ) |
|
201 | - ); |
|
202 | - } |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - /** |
|
207 | - * Alters the query params so that only trashed/soft-deleted items are considered |
|
208 | - * |
|
209 | - * @param array $query_params |
|
210 | - * @return array |
|
211 | - * @throws EE_Error |
|
212 | - * @throws EE_Error |
|
213 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
214 | - */ |
|
215 | - protected function _alter_query_params_so_only_trashed_items_included(array $query_params): array |
|
216 | - { |
|
217 | - $post_status_field_name = $this->post_status_field_name(); |
|
218 | - $query_params[0][ $post_status_field_name ] = self::post_status_trashed; |
|
219 | - return $query_params; |
|
220 | - } |
|
221 | - |
|
222 | - |
|
223 | - /** |
|
224 | - * Alters the query params so each item's deleted status is ignored. |
|
225 | - * |
|
226 | - * @param array $query_params |
|
227 | - * @return array |
|
228 | - */ |
|
229 | - protected function _alter_query_params_so_deleted_and_undeleted_items_included(array $query_params): array |
|
230 | - { |
|
231 | - $query_params['default_where_conditions'] = 'minimum'; |
|
232 | - return $query_params; |
|
233 | - } |
|
234 | - |
|
235 | - |
|
236 | - /** |
|
237 | - * Performs deletes or restores on items. Both soft-deleted and non-soft-deleted items considered. |
|
238 | - * |
|
239 | - * @param boolean $delete true to indicate deletion, false to indicate restoration |
|
240 | - * @param array $query_params |
|
241 | - * @return boolean success |
|
242 | - * @throws EE_Error |
|
243 | - * @throws ReflectionException |
|
244 | - * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
245 | - */ |
|
246 | - public function delete_or_restore(bool $delete = true, array $query_params = []): bool |
|
247 | - { |
|
248 | - $post_status_field_name = $this->post_status_field_name(); |
|
249 | - $query_params = $this->_alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
250 | - $new_status = $delete ? self::post_status_trashed : 'draft'; |
|
251 | - if ($this->update([$post_status_field_name => $new_status], $query_params)) { |
|
252 | - return true; |
|
253 | - } |
|
254 | - return false; |
|
255 | - } |
|
256 | - |
|
257 | - |
|
258 | - /** |
|
259 | - * meta_table |
|
260 | - * returns first EE_Secondary_Table table name |
|
261 | - * |
|
262 | - * @access public |
|
263 | - * @return string |
|
264 | - */ |
|
265 | - public function meta_table(): ?string |
|
266 | - { |
|
267 | - $meta_table = $this->_get_other_tables(); |
|
268 | - $meta_table = reset($meta_table); |
|
269 | - return $meta_table instanceof EE_Secondary_Table ? $meta_table->get_table_name() : null; |
|
270 | - } |
|
271 | - |
|
272 | - |
|
273 | - /** |
|
274 | - * This simply returns an array of the meta table fields (useful for when we just need to update those fields) |
|
275 | - * |
|
276 | - * @param bool $all triggers whether we include DB_Only fields or JUST non DB_Only fields. Defaults to false (no |
|
277 | - * db only fields) |
|
278 | - * @return array |
|
279 | - */ |
|
280 | - public function get_meta_table_fields(bool $all = false): array |
|
281 | - { |
|
282 | - $all_fields = $fields_to_return = []; |
|
283 | - foreach ($this->_tables as $alias => $table_obj) { |
|
284 | - if ($table_obj instanceof EE_Secondary_Table) { |
|
285 | - $all_fields = array_merge($this->_get_fields_for_table($alias), $all_fields); |
|
286 | - } |
|
287 | - } |
|
288 | - if (! $all) { |
|
289 | - foreach ($all_fields as $name => $obj) { |
|
290 | - if ($obj instanceof EE_DB_Only_Field_Base) { |
|
291 | - continue; |
|
292 | - } |
|
293 | - $fields_to_return[] = $name; |
|
294 | - } |
|
295 | - } else { |
|
296 | - $fields_to_return = array_keys($all_fields); |
|
297 | - } |
|
298 | - return $fields_to_return; |
|
299 | - } |
|
300 | - |
|
301 | - |
|
302 | - /** |
|
303 | - * Adds an event category with the specified name and description to the specified |
|
304 | - * $cpt_model_object. Intelligently adds a term if necessary, and adds a term_taxonomy if necessary, |
|
305 | - * and adds an entry in the term_relationship if necessary. |
|
306 | - * |
|
307 | - * @param EE_CPT_Base $cpt_model_object |
|
308 | - * @param string $category_name (used to derive the term slug too) |
|
309 | - * @param string $category_description |
|
310 | - * @param int|null $parent_term_taxonomy_id |
|
311 | - * @return EE_Term_Taxonomy |
|
312 | - * @throws EE_Error |
|
313 | - * @throws ReflectionException |
|
314 | - */ |
|
315 | - public function add_event_category( |
|
316 | - EE_CPT_Base $cpt_model_object, |
|
317 | - string $category_name, |
|
318 | - string $category_description = '', |
|
319 | - int $parent_term_taxonomy_id = null |
|
320 | - ): EE_Term_Taxonomy { |
|
321 | - // create term |
|
322 | - require_once(EE_MODELS . 'EEM_Term.model.php'); |
|
323 | - // first, check for a term by the same name or slug |
|
324 | - $category_slug = sanitize_title($category_name); |
|
325 | - $term = EEM_Term::instance()->get_one( |
|
326 | - [ |
|
327 | - [ |
|
328 | - 'OR' => [ |
|
329 | - 'name' => $category_name, |
|
330 | - 'slug' => $category_slug, |
|
331 | - ], |
|
332 | - 'Term_Taxonomy.taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
333 | - ], |
|
334 | - ] |
|
335 | - ); |
|
336 | - if (! $term) { |
|
337 | - $term = EE_Term::new_instance( |
|
338 | - [ |
|
339 | - 'name' => $category_name, |
|
340 | - 'slug' => $category_slug, |
|
341 | - ] |
|
342 | - ); |
|
343 | - $term->save(); |
|
344 | - } |
|
345 | - // make sure there's a term-taxonomy entry too |
|
346 | - require_once(EE_MODELS . 'EEM_Term_Taxonomy.model.php'); |
|
347 | - $term_taxonomy = EEM_Term_Taxonomy::instance()->get_one( |
|
348 | - [ |
|
349 | - [ |
|
350 | - 'term_id' => $term->ID(), |
|
351 | - 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
352 | - ], |
|
353 | - ] |
|
354 | - ); |
|
355 | - /** @var $term_taxonomy EE_Term_Taxonomy */ |
|
356 | - if (! $term_taxonomy) { |
|
357 | - $term_taxonomy = EE_Term_Taxonomy::new_instance( |
|
358 | - [ |
|
359 | - 'term_id' => $term->ID(), |
|
360 | - 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
361 | - 'description' => $category_description, |
|
362 | - 'term_count' => 1, |
|
363 | - 'parent' => $parent_term_taxonomy_id, |
|
364 | - ] |
|
365 | - ); |
|
366 | - } else { |
|
367 | - $term_taxonomy->set_count($term_taxonomy->count() + 1); |
|
368 | - } |
|
369 | - $term_taxonomy->save(); |
|
370 | - return $this->add_relationship_to($cpt_model_object, $term_taxonomy, 'Term_Taxonomy'); |
|
371 | - } |
|
372 | - |
|
373 | - |
|
374 | - /** |
|
375 | - * Removed the category specified by name as having a relation to this event. |
|
376 | - * Does not remove the term or term_taxonomy. |
|
377 | - * |
|
378 | - * @param EE_CPT_Base $cpt_model_object_event |
|
379 | - * @param string $category_name name of the event category (term) |
|
380 | - * @return bool |
|
381 | - * @throws EE_Error |
|
382 | - * @throws ReflectionException |
|
383 | - */ |
|
384 | - public function remove_event_category(EE_CPT_Base $cpt_model_object_event, string $category_name): bool |
|
385 | - { |
|
386 | - // find the term_taxonomy by that name |
|
387 | - $term_taxonomy = $this->get_first_related( |
|
388 | - $cpt_model_object_event, |
|
389 | - 'Term_Taxonomy', |
|
390 | - [['Term.name' => $category_name, 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY]] |
|
391 | - ); |
|
392 | - /** @var $term_taxonomy EE_Term_Taxonomy */ |
|
393 | - if ($term_taxonomy) { |
|
394 | - $term_taxonomy->set_count($term_taxonomy->count() - 1); |
|
395 | - $term_taxonomy->save(); |
|
396 | - } |
|
397 | - return $this->remove_relationship_to($cpt_model_object_event, $term_taxonomy, 'Term_Taxonomy'); |
|
398 | - } |
|
399 | - |
|
400 | - |
|
401 | - /** |
|
402 | - * This is a wrapper for the WordPress get_the_post_thumbnail() function that returns the feature image for the |
|
403 | - * given CPT ID. It accepts the same params as what get_the_post_thumbnail() accepts. |
|
404 | - * |
|
405 | - * @link http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail |
|
406 | - * @access public |
|
407 | - * @param int $id the ID for the cpt we want the feature image for |
|
408 | - * @param string|array $size (optional) Image size. Defaults to 'post-thumbnail' but can also be a 2-item array |
|
409 | - * representing width and height in pixels (i.e. array(32,32) ). |
|
410 | - * @param string|array $attr Optional. Query string or array of attributes. |
|
411 | - * @return string HTML image element |
|
412 | - */ |
|
413 | - public function get_feature_image(int $id, $size = 'thumbnail', $attr = ''): string |
|
414 | - { |
|
415 | - return get_the_post_thumbnail($id, $size, $attr); |
|
416 | - } |
|
417 | - |
|
418 | - |
|
419 | - /** |
|
420 | - * Just a handy way to get the list of post statuses currently registered with WP. |
|
421 | - * |
|
422 | - * @return array |
|
423 | - * @global array $wp_post_statuses set in wp core for storing all the post statuses |
|
424 | - */ |
|
425 | - public function get_post_statuses(): array |
|
426 | - { |
|
427 | - global $wp_post_statuses; |
|
428 | - $statuses = []; |
|
429 | - foreach ($wp_post_statuses as $post_status => $args_object) { |
|
430 | - $statuses[ $post_status ] = $args_object->label; |
|
431 | - } |
|
432 | - return $statuses; |
|
433 | - } |
|
434 | - |
|
435 | - |
|
436 | - /** |
|
437 | - * public method that can be used to retrieve the protected status array on the instantiated cpt model |
|
438 | - * |
|
439 | - * @return array array of statuses. |
|
440 | - */ |
|
441 | - public function get_status_array(): array |
|
442 | - { |
|
443 | - $statuses = $this->get_post_statuses(); |
|
444 | - // first the global filter |
|
445 | - $statuses = apply_filters('FHEE_EEM_CPT_Base__get_status_array', $statuses); |
|
446 | - // now the class specific filter |
|
447 | - return apply_filters('FHEE_EEM_' . get_class($this) . '__get_status_array', $statuses); |
|
448 | - } |
|
449 | - |
|
450 | - |
|
451 | - /** |
|
452 | - * this returns the post statuses that are NOT the default wordpress status |
|
453 | - * |
|
454 | - * @return array |
|
455 | - */ |
|
456 | - public function get_custom_post_statuses(): array |
|
457 | - { |
|
458 | - $new_statuses = []; |
|
459 | - foreach ($this->_custom_stati as $status => $props) { |
|
460 | - $new_statuses[ $status ] = $props['label']; |
|
461 | - } |
|
462 | - return $new_statuses; |
|
463 | - } |
|
464 | - |
|
465 | - |
|
466 | - /** |
|
467 | - * Creates a child of EE_CPT_Base given a WP_Post or array of wpdb results which |
|
468 | - * are a row from the posts table. If we're missing any fields required for the model, |
|
469 | - * we just fetch the entire entry from the DB (ie, if you want to use this to save DB queries, |
|
470 | - * make sure you are attaching all the model's fields onto the post) |
|
471 | - * |
|
472 | - * @param WP_Post|array $post |
|
473 | - * @return EE_Base_Class|EE_Soft_Delete_Base_Class |
|
474 | - * @throws EE_Error |
|
475 | - * @throws ReflectionException |
|
476 | - */ |
|
477 | - public function instantiate_class_from_post_object_orig($post) |
|
478 | - { |
|
479 | - $post = (array) $post; |
|
480 | - $has_all_necessary_fields_for_table = true; |
|
481 | - // check if the post has fields on the meta table already |
|
482 | - foreach ($this->_get_other_tables() as $table_obj) { |
|
483 | - $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
|
484 | - foreach ($fields_for_that_table as $field_obj) { |
|
485 | - if ( |
|
486 | - ! isset($post[ $field_obj->get_table_column() ]) |
|
487 | - && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
488 | - ) { |
|
489 | - $has_all_necessary_fields_for_table = false; |
|
490 | - } |
|
491 | - } |
|
492 | - } |
|
493 | - // if we don't have all the fields we need, then just fetch the proper model from the DB |
|
494 | - if (! $has_all_necessary_fields_for_table) { |
|
495 | - return $this->get_one_by_ID($post['ID']); |
|
496 | - } |
|
497 | - return $this->instantiate_class_from_array_or_object($post); |
|
498 | - } |
|
499 | - |
|
500 | - |
|
501 | - /** |
|
502 | - * @param null $post |
|
503 | - * @return EE_Base_Class|EE_Soft_Delete_Base_Class |
|
504 | - * @throws EE_Error |
|
505 | - * @throws ReflectionException |
|
506 | - */ |
|
507 | - public function instantiate_class_from_post_object($post = null) |
|
508 | - { |
|
509 | - if (empty($post)) { |
|
510 | - global $post; |
|
511 | - } |
|
512 | - $post = (array) $post; |
|
513 | - $tables_needing_to_be_queried = []; |
|
514 | - // check if the post has fields on the meta table already |
|
515 | - foreach ($this->get_tables() as $table_obj) { |
|
516 | - $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
|
517 | - foreach ($fields_for_that_table as $field_obj) { |
|
518 | - if ( |
|
519 | - ! isset($post[ $field_obj->get_table_column() ]) |
|
520 | - && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
521 | - ) { |
|
522 | - $tables_needing_to_be_queried[ $table_obj->get_table_alias() ] = $table_obj; |
|
523 | - } |
|
524 | - } |
|
525 | - } |
|
526 | - // if we don't have all the fields we need, then just fetch the proper model from the DB |
|
527 | - if ($tables_needing_to_be_queried) { |
|
528 | - if ( |
|
529 | - count($tables_needing_to_be_queried) == 1 |
|
530 | - && reset($tables_needing_to_be_queried) instanceof EE_Secondary_Table |
|
531 | - ) { |
|
532 | - // so we're only missing data from a secondary table. Well that's not too hard to query for |
|
533 | - $table_to_query = reset($tables_needing_to_be_queried); |
|
534 | - $missing_data = $this->_do_wpdb_query( |
|
535 | - 'get_row', |
|
536 | - [ |
|
537 | - 'SELECT * FROM ' |
|
538 | - . $table_to_query->get_table_name() |
|
539 | - . ' WHERE ' |
|
540 | - . $table_to_query->get_fk_on_table() |
|
541 | - . ' = ' |
|
542 | - . $post['ID'], |
|
543 | - ARRAY_A, |
|
544 | - ] |
|
545 | - ); |
|
546 | - if (! empty($missing_data)) { |
|
547 | - $post = array_merge($post, $missing_data); |
|
548 | - } |
|
549 | - } else { |
|
550 | - return $this->get_one_by_ID($post['ID']); |
|
551 | - } |
|
552 | - } |
|
553 | - return $this->instantiate_class_from_array_or_object($post); |
|
554 | - } |
|
555 | - |
|
556 | - |
|
557 | - /** |
|
558 | - * Gets the post type associated with this |
|
559 | - * |
|
560 | - * @return string |
|
561 | - * @throws EE_Error |
|
562 | - */ |
|
563 | - public function post_type(): string |
|
564 | - { |
|
565 | - $post_type_field = null; |
|
566 | - foreach ($this->field_settings(true) as $field_obj) { |
|
567 | - if ($field_obj instanceof EE_WP_Post_Type_Field) { |
|
568 | - $post_type_field = $field_obj; |
|
569 | - break; |
|
570 | - } |
|
571 | - } |
|
572 | - if ($post_type_field == null) { |
|
573 | - throw new EE_Error( |
|
574 | - sprintf( |
|
575 | - esc_html__( |
|
576 | - "CPT Model %s should have a field of type EE_WP_Post_Type, but doesnt", |
|
577 | - "event_espresso" |
|
578 | - ), |
|
579 | - get_class($this) |
|
580 | - ) |
|
581 | - ); |
|
582 | - } |
|
583 | - return $post_type_field->get_default_value(); |
|
584 | - } |
|
18 | + const EVENT_CATEGORY_TAXONOMY = 'espresso_event_categories'; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var string post_status_publish - the wp post status for published CPTs |
|
22 | + */ |
|
23 | + const post_status_publish = 'publish'; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var string post_status_future - the wp post status for scheduled CPTs |
|
27 | + */ |
|
28 | + const post_status_future = 'future'; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var string post_status_draft - the wp post status for draft CPTs |
|
32 | + */ |
|
33 | + const post_status_draft = 'draft'; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var string post_status_pending - the wp post status for pending CPTs |
|
37 | + */ |
|
38 | + const post_status_pending = 'pending'; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var string post_status_private - the wp post status for private CPTs |
|
42 | + */ |
|
43 | + const post_status_private = 'private'; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var string post_status_trashed - the wp post status for trashed CPTs |
|
47 | + */ |
|
48 | + const post_status_trashed = 'trash'; |
|
49 | + |
|
50 | + /** |
|
51 | + * This is an array of custom statuses for the given CPT model (modified by children) |
|
52 | + * format: |
|
53 | + * array( |
|
54 | + * 'status_name' => array( |
|
55 | + * 'label' => esc_html__('Status Name', 'event_espresso'), |
|
56 | + * 'public' => TRUE //whether a public status or not. |
|
57 | + * ) |
|
58 | + * ) |
|
59 | + * |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + protected $_custom_stati = []; |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * Adds a relationship to Term_Taxonomy for each CPT_Base |
|
67 | + * |
|
68 | + * @param string|null $timezone |
|
69 | + * @throws EE_Error |
|
70 | + */ |
|
71 | + protected function __construct(string $timezone = '') |
|
72 | + { |
|
73 | + // adds a relationship to Term_Taxonomy for all these models. For this to work |
|
74 | + // Term_Relationship must have a relation to each model subclassing EE_CPT_Base explicitly |
|
75 | + // eg, in EEM_Term_Relationship, inside the _model_relations array, there must be an entry |
|
76 | + // with key equalling the subclassing model's model name (eg 'Event' or 'Venue'), and the value |
|
77 | + // must also be new EE_HABTM_Relation('Term_Relationship'); |
|
78 | + $this->_model_relations['Term_Taxonomy'] = new EE_HABTM_Relation('Term_Relationship'); |
|
79 | + $primary_table_name = null; |
|
80 | + // add the common _status field to all CPT primary tables. |
|
81 | + foreach ($this->_tables as $alias => $table_obj) { |
|
82 | + if ($table_obj instanceof EE_Primary_Table) { |
|
83 | + $primary_table_name = $alias; |
|
84 | + } |
|
85 | + } |
|
86 | + // set default wp post statuses if child has not already set. |
|
87 | + if (! isset($this->_fields[ $primary_table_name ]['status'])) { |
|
88 | + $this->_fields[ $primary_table_name ]['status'] = new EE_WP_Post_Status_Field( |
|
89 | + 'post_status', |
|
90 | + esc_html__("Event Status", "event_espresso"), |
|
91 | + false, |
|
92 | + 'draft' |
|
93 | + ); |
|
94 | + } |
|
95 | + if (! isset($this->_fields[ $primary_table_name ]['to_ping'])) { |
|
96 | + $this->_fields[ $primary_table_name ]['to_ping'] = new EE_DB_Only_Text_Field( |
|
97 | + 'to_ping', |
|
98 | + esc_html__('To Ping', 'event_espresso'), |
|
99 | + false, |
|
100 | + '' |
|
101 | + ); |
|
102 | + } |
|
103 | + if (! isset($this->_fields[ $primary_table_name ]['pinged'])) { |
|
104 | + $this->_fields[ $primary_table_name ]['pinged'] = new EE_DB_Only_Text_Field( |
|
105 | + 'pinged', |
|
106 | + esc_html__('Pinged', 'event_espresso'), |
|
107 | + false, |
|
108 | + '' |
|
109 | + ); |
|
110 | + } |
|
111 | + if (! isset($this->_fields[ $primary_table_name ]['comment_status'])) { |
|
112 | + $this->_fields[ $primary_table_name ]['comment_status'] = new EE_Plain_Text_Field( |
|
113 | + 'comment_status', |
|
114 | + esc_html__('Comment Status', 'event_espresso'), |
|
115 | + false, |
|
116 | + 'open' |
|
117 | + ); |
|
118 | + } |
|
119 | + if (! isset($this->_fields[ $primary_table_name ]['ping_status'])) { |
|
120 | + $this->_fields[ $primary_table_name ]['ping_status'] = new EE_Plain_Text_Field( |
|
121 | + 'ping_status', |
|
122 | + esc_html__('Ping Status', 'event_espresso'), |
|
123 | + false, |
|
124 | + 'open' |
|
125 | + ); |
|
126 | + } |
|
127 | + if (! isset($this->_fields[ $primary_table_name ]['post_content_filtered'])) { |
|
128 | + $this->_fields[ $primary_table_name ]['post_content_filtered'] = new EE_DB_Only_Text_Field( |
|
129 | + 'post_content_filtered', |
|
130 | + esc_html__('Post Content Filtered', 'event_espresso'), |
|
131 | + false, |
|
132 | + '' |
|
133 | + ); |
|
134 | + } |
|
135 | + if (! isset($this->_model_relations['Post_Meta'])) { |
|
136 | + // don't block deletes though because we want to maintain the current behaviour |
|
137 | + $this->_model_relations['Post_Meta'] = new EE_Has_Many_Relation(false); |
|
138 | + } |
|
139 | + if (! $this->_minimum_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
140 | + // nothing was set during child constructor, so set default |
|
141 | + $this->_minimum_where_conditions_strategy = new EE_CPT_Minimum_Where_Conditions($this->post_type()); |
|
142 | + } |
|
143 | + if (! $this->_default_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
144 | + // nothing was set during child constructor, so set default |
|
145 | + // it's ok for child classes to specify this, but generally this is more DRY |
|
146 | + $this->_default_where_conditions_strategy = new EE_CPT_Where_Conditions($this->post_type()); |
|
147 | + } |
|
148 | + parent::__construct($timezone); |
|
149 | + } |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * @return array |
|
154 | + */ |
|
155 | + public function public_event_stati(): array |
|
156 | + { |
|
157 | + // @see wp-includes/post.php |
|
158 | + return get_post_stati(['public' => true]); |
|
159 | + } |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * Searches for field on this model of type 'deleted_flag'. if it is found, |
|
164 | + * returns it's name. BUT That doesn't apply to CPTs. We should instead use post_status_field_name |
|
165 | + * |
|
166 | + * @return void |
|
167 | + * @throws EE_Error |
|
168 | + */ |
|
169 | + public function deleted_field_name() |
|
170 | + { |
|
171 | + throw new EE_Error( |
|
172 | + esc_html__( |
|
173 | + 'EEM_CPT_Base should not call deleted_field_name! It should instead use post_status_field_name', |
|
174 | + 'event_espresso' |
|
175 | + ) |
|
176 | + ); |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * Gets the field's name that sets the post status |
|
182 | + * |
|
183 | + * @return string |
|
184 | + * @throws EE_Error |
|
185 | + */ |
|
186 | + public function post_status_field_name(): string |
|
187 | + { |
|
188 | + $field = $this->get_a_field_of_type('EE_WP_Post_Status_Field'); |
|
189 | + if ($field) { |
|
190 | + return $field->get_name(); |
|
191 | + } else { |
|
192 | + throw new EE_Error( |
|
193 | + sprintf( |
|
194 | + esc_html__( |
|
195 | + 'We are trying to find the post status flag field on %s, but none was found. Are you sure there is a field of type EE_Trashed_Flag_Field in %s constructor?', |
|
196 | + 'event_espresso' |
|
197 | + ), |
|
198 | + get_class($this), |
|
199 | + get_class($this) |
|
200 | + ) |
|
201 | + ); |
|
202 | + } |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + /** |
|
207 | + * Alters the query params so that only trashed/soft-deleted items are considered |
|
208 | + * |
|
209 | + * @param array $query_params |
|
210 | + * @return array |
|
211 | + * @throws EE_Error |
|
212 | + * @throws EE_Error |
|
213 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
214 | + */ |
|
215 | + protected function _alter_query_params_so_only_trashed_items_included(array $query_params): array |
|
216 | + { |
|
217 | + $post_status_field_name = $this->post_status_field_name(); |
|
218 | + $query_params[0][ $post_status_field_name ] = self::post_status_trashed; |
|
219 | + return $query_params; |
|
220 | + } |
|
221 | + |
|
222 | + |
|
223 | + /** |
|
224 | + * Alters the query params so each item's deleted status is ignored. |
|
225 | + * |
|
226 | + * @param array $query_params |
|
227 | + * @return array |
|
228 | + */ |
|
229 | + protected function _alter_query_params_so_deleted_and_undeleted_items_included(array $query_params): array |
|
230 | + { |
|
231 | + $query_params['default_where_conditions'] = 'minimum'; |
|
232 | + return $query_params; |
|
233 | + } |
|
234 | + |
|
235 | + |
|
236 | + /** |
|
237 | + * Performs deletes or restores on items. Both soft-deleted and non-soft-deleted items considered. |
|
238 | + * |
|
239 | + * @param boolean $delete true to indicate deletion, false to indicate restoration |
|
240 | + * @param array $query_params |
|
241 | + * @return boolean success |
|
242 | + * @throws EE_Error |
|
243 | + * @throws ReflectionException |
|
244 | + * @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
245 | + */ |
|
246 | + public function delete_or_restore(bool $delete = true, array $query_params = []): bool |
|
247 | + { |
|
248 | + $post_status_field_name = $this->post_status_field_name(); |
|
249 | + $query_params = $this->_alter_query_params_so_deleted_and_undeleted_items_included($query_params); |
|
250 | + $new_status = $delete ? self::post_status_trashed : 'draft'; |
|
251 | + if ($this->update([$post_status_field_name => $new_status], $query_params)) { |
|
252 | + return true; |
|
253 | + } |
|
254 | + return false; |
|
255 | + } |
|
256 | + |
|
257 | + |
|
258 | + /** |
|
259 | + * meta_table |
|
260 | + * returns first EE_Secondary_Table table name |
|
261 | + * |
|
262 | + * @access public |
|
263 | + * @return string |
|
264 | + */ |
|
265 | + public function meta_table(): ?string |
|
266 | + { |
|
267 | + $meta_table = $this->_get_other_tables(); |
|
268 | + $meta_table = reset($meta_table); |
|
269 | + return $meta_table instanceof EE_Secondary_Table ? $meta_table->get_table_name() : null; |
|
270 | + } |
|
271 | + |
|
272 | + |
|
273 | + /** |
|
274 | + * This simply returns an array of the meta table fields (useful for when we just need to update those fields) |
|
275 | + * |
|
276 | + * @param bool $all triggers whether we include DB_Only fields or JUST non DB_Only fields. Defaults to false (no |
|
277 | + * db only fields) |
|
278 | + * @return array |
|
279 | + */ |
|
280 | + public function get_meta_table_fields(bool $all = false): array |
|
281 | + { |
|
282 | + $all_fields = $fields_to_return = []; |
|
283 | + foreach ($this->_tables as $alias => $table_obj) { |
|
284 | + if ($table_obj instanceof EE_Secondary_Table) { |
|
285 | + $all_fields = array_merge($this->_get_fields_for_table($alias), $all_fields); |
|
286 | + } |
|
287 | + } |
|
288 | + if (! $all) { |
|
289 | + foreach ($all_fields as $name => $obj) { |
|
290 | + if ($obj instanceof EE_DB_Only_Field_Base) { |
|
291 | + continue; |
|
292 | + } |
|
293 | + $fields_to_return[] = $name; |
|
294 | + } |
|
295 | + } else { |
|
296 | + $fields_to_return = array_keys($all_fields); |
|
297 | + } |
|
298 | + return $fields_to_return; |
|
299 | + } |
|
300 | + |
|
301 | + |
|
302 | + /** |
|
303 | + * Adds an event category with the specified name and description to the specified |
|
304 | + * $cpt_model_object. Intelligently adds a term if necessary, and adds a term_taxonomy if necessary, |
|
305 | + * and adds an entry in the term_relationship if necessary. |
|
306 | + * |
|
307 | + * @param EE_CPT_Base $cpt_model_object |
|
308 | + * @param string $category_name (used to derive the term slug too) |
|
309 | + * @param string $category_description |
|
310 | + * @param int|null $parent_term_taxonomy_id |
|
311 | + * @return EE_Term_Taxonomy |
|
312 | + * @throws EE_Error |
|
313 | + * @throws ReflectionException |
|
314 | + */ |
|
315 | + public function add_event_category( |
|
316 | + EE_CPT_Base $cpt_model_object, |
|
317 | + string $category_name, |
|
318 | + string $category_description = '', |
|
319 | + int $parent_term_taxonomy_id = null |
|
320 | + ): EE_Term_Taxonomy { |
|
321 | + // create term |
|
322 | + require_once(EE_MODELS . 'EEM_Term.model.php'); |
|
323 | + // first, check for a term by the same name or slug |
|
324 | + $category_slug = sanitize_title($category_name); |
|
325 | + $term = EEM_Term::instance()->get_one( |
|
326 | + [ |
|
327 | + [ |
|
328 | + 'OR' => [ |
|
329 | + 'name' => $category_name, |
|
330 | + 'slug' => $category_slug, |
|
331 | + ], |
|
332 | + 'Term_Taxonomy.taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
333 | + ], |
|
334 | + ] |
|
335 | + ); |
|
336 | + if (! $term) { |
|
337 | + $term = EE_Term::new_instance( |
|
338 | + [ |
|
339 | + 'name' => $category_name, |
|
340 | + 'slug' => $category_slug, |
|
341 | + ] |
|
342 | + ); |
|
343 | + $term->save(); |
|
344 | + } |
|
345 | + // make sure there's a term-taxonomy entry too |
|
346 | + require_once(EE_MODELS . 'EEM_Term_Taxonomy.model.php'); |
|
347 | + $term_taxonomy = EEM_Term_Taxonomy::instance()->get_one( |
|
348 | + [ |
|
349 | + [ |
|
350 | + 'term_id' => $term->ID(), |
|
351 | + 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
352 | + ], |
|
353 | + ] |
|
354 | + ); |
|
355 | + /** @var $term_taxonomy EE_Term_Taxonomy */ |
|
356 | + if (! $term_taxonomy) { |
|
357 | + $term_taxonomy = EE_Term_Taxonomy::new_instance( |
|
358 | + [ |
|
359 | + 'term_id' => $term->ID(), |
|
360 | + 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY, |
|
361 | + 'description' => $category_description, |
|
362 | + 'term_count' => 1, |
|
363 | + 'parent' => $parent_term_taxonomy_id, |
|
364 | + ] |
|
365 | + ); |
|
366 | + } else { |
|
367 | + $term_taxonomy->set_count($term_taxonomy->count() + 1); |
|
368 | + } |
|
369 | + $term_taxonomy->save(); |
|
370 | + return $this->add_relationship_to($cpt_model_object, $term_taxonomy, 'Term_Taxonomy'); |
|
371 | + } |
|
372 | + |
|
373 | + |
|
374 | + /** |
|
375 | + * Removed the category specified by name as having a relation to this event. |
|
376 | + * Does not remove the term or term_taxonomy. |
|
377 | + * |
|
378 | + * @param EE_CPT_Base $cpt_model_object_event |
|
379 | + * @param string $category_name name of the event category (term) |
|
380 | + * @return bool |
|
381 | + * @throws EE_Error |
|
382 | + * @throws ReflectionException |
|
383 | + */ |
|
384 | + public function remove_event_category(EE_CPT_Base $cpt_model_object_event, string $category_name): bool |
|
385 | + { |
|
386 | + // find the term_taxonomy by that name |
|
387 | + $term_taxonomy = $this->get_first_related( |
|
388 | + $cpt_model_object_event, |
|
389 | + 'Term_Taxonomy', |
|
390 | + [['Term.name' => $category_name, 'taxonomy' => self::EVENT_CATEGORY_TAXONOMY]] |
|
391 | + ); |
|
392 | + /** @var $term_taxonomy EE_Term_Taxonomy */ |
|
393 | + if ($term_taxonomy) { |
|
394 | + $term_taxonomy->set_count($term_taxonomy->count() - 1); |
|
395 | + $term_taxonomy->save(); |
|
396 | + } |
|
397 | + return $this->remove_relationship_to($cpt_model_object_event, $term_taxonomy, 'Term_Taxonomy'); |
|
398 | + } |
|
399 | + |
|
400 | + |
|
401 | + /** |
|
402 | + * This is a wrapper for the WordPress get_the_post_thumbnail() function that returns the feature image for the |
|
403 | + * given CPT ID. It accepts the same params as what get_the_post_thumbnail() accepts. |
|
404 | + * |
|
405 | + * @link http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail |
|
406 | + * @access public |
|
407 | + * @param int $id the ID for the cpt we want the feature image for |
|
408 | + * @param string|array $size (optional) Image size. Defaults to 'post-thumbnail' but can also be a 2-item array |
|
409 | + * representing width and height in pixels (i.e. array(32,32) ). |
|
410 | + * @param string|array $attr Optional. Query string or array of attributes. |
|
411 | + * @return string HTML image element |
|
412 | + */ |
|
413 | + public function get_feature_image(int $id, $size = 'thumbnail', $attr = ''): string |
|
414 | + { |
|
415 | + return get_the_post_thumbnail($id, $size, $attr); |
|
416 | + } |
|
417 | + |
|
418 | + |
|
419 | + /** |
|
420 | + * Just a handy way to get the list of post statuses currently registered with WP. |
|
421 | + * |
|
422 | + * @return array |
|
423 | + * @global array $wp_post_statuses set in wp core for storing all the post statuses |
|
424 | + */ |
|
425 | + public function get_post_statuses(): array |
|
426 | + { |
|
427 | + global $wp_post_statuses; |
|
428 | + $statuses = []; |
|
429 | + foreach ($wp_post_statuses as $post_status => $args_object) { |
|
430 | + $statuses[ $post_status ] = $args_object->label; |
|
431 | + } |
|
432 | + return $statuses; |
|
433 | + } |
|
434 | + |
|
435 | + |
|
436 | + /** |
|
437 | + * public method that can be used to retrieve the protected status array on the instantiated cpt model |
|
438 | + * |
|
439 | + * @return array array of statuses. |
|
440 | + */ |
|
441 | + public function get_status_array(): array |
|
442 | + { |
|
443 | + $statuses = $this->get_post_statuses(); |
|
444 | + // first the global filter |
|
445 | + $statuses = apply_filters('FHEE_EEM_CPT_Base__get_status_array', $statuses); |
|
446 | + // now the class specific filter |
|
447 | + return apply_filters('FHEE_EEM_' . get_class($this) . '__get_status_array', $statuses); |
|
448 | + } |
|
449 | + |
|
450 | + |
|
451 | + /** |
|
452 | + * this returns the post statuses that are NOT the default wordpress status |
|
453 | + * |
|
454 | + * @return array |
|
455 | + */ |
|
456 | + public function get_custom_post_statuses(): array |
|
457 | + { |
|
458 | + $new_statuses = []; |
|
459 | + foreach ($this->_custom_stati as $status => $props) { |
|
460 | + $new_statuses[ $status ] = $props['label']; |
|
461 | + } |
|
462 | + return $new_statuses; |
|
463 | + } |
|
464 | + |
|
465 | + |
|
466 | + /** |
|
467 | + * Creates a child of EE_CPT_Base given a WP_Post or array of wpdb results which |
|
468 | + * are a row from the posts table. If we're missing any fields required for the model, |
|
469 | + * we just fetch the entire entry from the DB (ie, if you want to use this to save DB queries, |
|
470 | + * make sure you are attaching all the model's fields onto the post) |
|
471 | + * |
|
472 | + * @param WP_Post|array $post |
|
473 | + * @return EE_Base_Class|EE_Soft_Delete_Base_Class |
|
474 | + * @throws EE_Error |
|
475 | + * @throws ReflectionException |
|
476 | + */ |
|
477 | + public function instantiate_class_from_post_object_orig($post) |
|
478 | + { |
|
479 | + $post = (array) $post; |
|
480 | + $has_all_necessary_fields_for_table = true; |
|
481 | + // check if the post has fields on the meta table already |
|
482 | + foreach ($this->_get_other_tables() as $table_obj) { |
|
483 | + $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
|
484 | + foreach ($fields_for_that_table as $field_obj) { |
|
485 | + if ( |
|
486 | + ! isset($post[ $field_obj->get_table_column() ]) |
|
487 | + && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
488 | + ) { |
|
489 | + $has_all_necessary_fields_for_table = false; |
|
490 | + } |
|
491 | + } |
|
492 | + } |
|
493 | + // if we don't have all the fields we need, then just fetch the proper model from the DB |
|
494 | + if (! $has_all_necessary_fields_for_table) { |
|
495 | + return $this->get_one_by_ID($post['ID']); |
|
496 | + } |
|
497 | + return $this->instantiate_class_from_array_or_object($post); |
|
498 | + } |
|
499 | + |
|
500 | + |
|
501 | + /** |
|
502 | + * @param null $post |
|
503 | + * @return EE_Base_Class|EE_Soft_Delete_Base_Class |
|
504 | + * @throws EE_Error |
|
505 | + * @throws ReflectionException |
|
506 | + */ |
|
507 | + public function instantiate_class_from_post_object($post = null) |
|
508 | + { |
|
509 | + if (empty($post)) { |
|
510 | + global $post; |
|
511 | + } |
|
512 | + $post = (array) $post; |
|
513 | + $tables_needing_to_be_queried = []; |
|
514 | + // check if the post has fields on the meta table already |
|
515 | + foreach ($this->get_tables() as $table_obj) { |
|
516 | + $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
|
517 | + foreach ($fields_for_that_table as $field_obj) { |
|
518 | + if ( |
|
519 | + ! isset($post[ $field_obj->get_table_column() ]) |
|
520 | + && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
521 | + ) { |
|
522 | + $tables_needing_to_be_queried[ $table_obj->get_table_alias() ] = $table_obj; |
|
523 | + } |
|
524 | + } |
|
525 | + } |
|
526 | + // if we don't have all the fields we need, then just fetch the proper model from the DB |
|
527 | + if ($tables_needing_to_be_queried) { |
|
528 | + if ( |
|
529 | + count($tables_needing_to_be_queried) == 1 |
|
530 | + && reset($tables_needing_to_be_queried) instanceof EE_Secondary_Table |
|
531 | + ) { |
|
532 | + // so we're only missing data from a secondary table. Well that's not too hard to query for |
|
533 | + $table_to_query = reset($tables_needing_to_be_queried); |
|
534 | + $missing_data = $this->_do_wpdb_query( |
|
535 | + 'get_row', |
|
536 | + [ |
|
537 | + 'SELECT * FROM ' |
|
538 | + . $table_to_query->get_table_name() |
|
539 | + . ' WHERE ' |
|
540 | + . $table_to_query->get_fk_on_table() |
|
541 | + . ' = ' |
|
542 | + . $post['ID'], |
|
543 | + ARRAY_A, |
|
544 | + ] |
|
545 | + ); |
|
546 | + if (! empty($missing_data)) { |
|
547 | + $post = array_merge($post, $missing_data); |
|
548 | + } |
|
549 | + } else { |
|
550 | + return $this->get_one_by_ID($post['ID']); |
|
551 | + } |
|
552 | + } |
|
553 | + return $this->instantiate_class_from_array_or_object($post); |
|
554 | + } |
|
555 | + |
|
556 | + |
|
557 | + /** |
|
558 | + * Gets the post type associated with this |
|
559 | + * |
|
560 | + * @return string |
|
561 | + * @throws EE_Error |
|
562 | + */ |
|
563 | + public function post_type(): string |
|
564 | + { |
|
565 | + $post_type_field = null; |
|
566 | + foreach ($this->field_settings(true) as $field_obj) { |
|
567 | + if ($field_obj instanceof EE_WP_Post_Type_Field) { |
|
568 | + $post_type_field = $field_obj; |
|
569 | + break; |
|
570 | + } |
|
571 | + } |
|
572 | + if ($post_type_field == null) { |
|
573 | + throw new EE_Error( |
|
574 | + sprintf( |
|
575 | + esc_html__( |
|
576 | + "CPT Model %s should have a field of type EE_WP_Post_Type, but doesnt", |
|
577 | + "event_espresso" |
|
578 | + ), |
|
579 | + get_class($this) |
|
580 | + ) |
|
581 | + ); |
|
582 | + } |
|
583 | + return $post_type_field->get_default_value(); |
|
584 | + } |
|
585 | 585 | } |
@@ -84,63 +84,63 @@ discard block |
||
84 | 84 | } |
85 | 85 | } |
86 | 86 | // set default wp post statuses if child has not already set. |
87 | - if (! isset($this->_fields[ $primary_table_name ]['status'])) { |
|
88 | - $this->_fields[ $primary_table_name ]['status'] = new EE_WP_Post_Status_Field( |
|
87 | + if ( ! isset($this->_fields[$primary_table_name]['status'])) { |
|
88 | + $this->_fields[$primary_table_name]['status'] = new EE_WP_Post_Status_Field( |
|
89 | 89 | 'post_status', |
90 | 90 | esc_html__("Event Status", "event_espresso"), |
91 | 91 | false, |
92 | 92 | 'draft' |
93 | 93 | ); |
94 | 94 | } |
95 | - if (! isset($this->_fields[ $primary_table_name ]['to_ping'])) { |
|
96 | - $this->_fields[ $primary_table_name ]['to_ping'] = new EE_DB_Only_Text_Field( |
|
95 | + if ( ! isset($this->_fields[$primary_table_name]['to_ping'])) { |
|
96 | + $this->_fields[$primary_table_name]['to_ping'] = new EE_DB_Only_Text_Field( |
|
97 | 97 | 'to_ping', |
98 | 98 | esc_html__('To Ping', 'event_espresso'), |
99 | 99 | false, |
100 | 100 | '' |
101 | 101 | ); |
102 | 102 | } |
103 | - if (! isset($this->_fields[ $primary_table_name ]['pinged'])) { |
|
104 | - $this->_fields[ $primary_table_name ]['pinged'] = new EE_DB_Only_Text_Field( |
|
103 | + if ( ! isset($this->_fields[$primary_table_name]['pinged'])) { |
|
104 | + $this->_fields[$primary_table_name]['pinged'] = new EE_DB_Only_Text_Field( |
|
105 | 105 | 'pinged', |
106 | 106 | esc_html__('Pinged', 'event_espresso'), |
107 | 107 | false, |
108 | 108 | '' |
109 | 109 | ); |
110 | 110 | } |
111 | - if (! isset($this->_fields[ $primary_table_name ]['comment_status'])) { |
|
112 | - $this->_fields[ $primary_table_name ]['comment_status'] = new EE_Plain_Text_Field( |
|
111 | + if ( ! isset($this->_fields[$primary_table_name]['comment_status'])) { |
|
112 | + $this->_fields[$primary_table_name]['comment_status'] = new EE_Plain_Text_Field( |
|
113 | 113 | 'comment_status', |
114 | 114 | esc_html__('Comment Status', 'event_espresso'), |
115 | 115 | false, |
116 | 116 | 'open' |
117 | 117 | ); |
118 | 118 | } |
119 | - if (! isset($this->_fields[ $primary_table_name ]['ping_status'])) { |
|
120 | - $this->_fields[ $primary_table_name ]['ping_status'] = new EE_Plain_Text_Field( |
|
119 | + if ( ! isset($this->_fields[$primary_table_name]['ping_status'])) { |
|
120 | + $this->_fields[$primary_table_name]['ping_status'] = new EE_Plain_Text_Field( |
|
121 | 121 | 'ping_status', |
122 | 122 | esc_html__('Ping Status', 'event_espresso'), |
123 | 123 | false, |
124 | 124 | 'open' |
125 | 125 | ); |
126 | 126 | } |
127 | - if (! isset($this->_fields[ $primary_table_name ]['post_content_filtered'])) { |
|
128 | - $this->_fields[ $primary_table_name ]['post_content_filtered'] = new EE_DB_Only_Text_Field( |
|
127 | + if ( ! isset($this->_fields[$primary_table_name]['post_content_filtered'])) { |
|
128 | + $this->_fields[$primary_table_name]['post_content_filtered'] = new EE_DB_Only_Text_Field( |
|
129 | 129 | 'post_content_filtered', |
130 | 130 | esc_html__('Post Content Filtered', 'event_espresso'), |
131 | 131 | false, |
132 | 132 | '' |
133 | 133 | ); |
134 | 134 | } |
135 | - if (! isset($this->_model_relations['Post_Meta'])) { |
|
135 | + if ( ! isset($this->_model_relations['Post_Meta'])) { |
|
136 | 136 | // don't block deletes though because we want to maintain the current behaviour |
137 | 137 | $this->_model_relations['Post_Meta'] = new EE_Has_Many_Relation(false); |
138 | 138 | } |
139 | - if (! $this->_minimum_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
139 | + if ( ! $this->_minimum_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
140 | 140 | // nothing was set during child constructor, so set default |
141 | 141 | $this->_minimum_where_conditions_strategy = new EE_CPT_Minimum_Where_Conditions($this->post_type()); |
142 | 142 | } |
143 | - if (! $this->_default_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
143 | + if ( ! $this->_default_where_conditions_strategy instanceof EE_Default_Where_Conditions) { |
|
144 | 144 | // nothing was set during child constructor, so set default |
145 | 145 | // it's ok for child classes to specify this, but generally this is more DRY |
146 | 146 | $this->_default_where_conditions_strategy = new EE_CPT_Where_Conditions($this->post_type()); |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | protected function _alter_query_params_so_only_trashed_items_included(array $query_params): array |
216 | 216 | { |
217 | 217 | $post_status_field_name = $this->post_status_field_name(); |
218 | - $query_params[0][ $post_status_field_name ] = self::post_status_trashed; |
|
218 | + $query_params[0][$post_status_field_name] = self::post_status_trashed; |
|
219 | 219 | return $query_params; |
220 | 220 | } |
221 | 221 | |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | $all_fields = array_merge($this->_get_fields_for_table($alias), $all_fields); |
286 | 286 | } |
287 | 287 | } |
288 | - if (! $all) { |
|
288 | + if ( ! $all) { |
|
289 | 289 | foreach ($all_fields as $name => $obj) { |
290 | 290 | if ($obj instanceof EE_DB_Only_Field_Base) { |
291 | 291 | continue; |
@@ -319,7 +319,7 @@ discard block |
||
319 | 319 | int $parent_term_taxonomy_id = null |
320 | 320 | ): EE_Term_Taxonomy { |
321 | 321 | // create term |
322 | - require_once(EE_MODELS . 'EEM_Term.model.php'); |
|
322 | + require_once(EE_MODELS.'EEM_Term.model.php'); |
|
323 | 323 | // first, check for a term by the same name or slug |
324 | 324 | $category_slug = sanitize_title($category_name); |
325 | 325 | $term = EEM_Term::instance()->get_one( |
@@ -333,7 +333,7 @@ discard block |
||
333 | 333 | ], |
334 | 334 | ] |
335 | 335 | ); |
336 | - if (! $term) { |
|
336 | + if ( ! $term) { |
|
337 | 337 | $term = EE_Term::new_instance( |
338 | 338 | [ |
339 | 339 | 'name' => $category_name, |
@@ -343,7 +343,7 @@ discard block |
||
343 | 343 | $term->save(); |
344 | 344 | } |
345 | 345 | // make sure there's a term-taxonomy entry too |
346 | - require_once(EE_MODELS . 'EEM_Term_Taxonomy.model.php'); |
|
346 | + require_once(EE_MODELS.'EEM_Term_Taxonomy.model.php'); |
|
347 | 347 | $term_taxonomy = EEM_Term_Taxonomy::instance()->get_one( |
348 | 348 | [ |
349 | 349 | [ |
@@ -353,7 +353,7 @@ discard block |
||
353 | 353 | ] |
354 | 354 | ); |
355 | 355 | /** @var $term_taxonomy EE_Term_Taxonomy */ |
356 | - if (! $term_taxonomy) { |
|
356 | + if ( ! $term_taxonomy) { |
|
357 | 357 | $term_taxonomy = EE_Term_Taxonomy::new_instance( |
358 | 358 | [ |
359 | 359 | 'term_id' => $term->ID(), |
@@ -427,7 +427,7 @@ discard block |
||
427 | 427 | global $wp_post_statuses; |
428 | 428 | $statuses = []; |
429 | 429 | foreach ($wp_post_statuses as $post_status => $args_object) { |
430 | - $statuses[ $post_status ] = $args_object->label; |
|
430 | + $statuses[$post_status] = $args_object->label; |
|
431 | 431 | } |
432 | 432 | return $statuses; |
433 | 433 | } |
@@ -444,7 +444,7 @@ discard block |
||
444 | 444 | // first the global filter |
445 | 445 | $statuses = apply_filters('FHEE_EEM_CPT_Base__get_status_array', $statuses); |
446 | 446 | // now the class specific filter |
447 | - return apply_filters('FHEE_EEM_' . get_class($this) . '__get_status_array', $statuses); |
|
447 | + return apply_filters('FHEE_EEM_'.get_class($this).'__get_status_array', $statuses); |
|
448 | 448 | } |
449 | 449 | |
450 | 450 | |
@@ -457,7 +457,7 @@ discard block |
||
457 | 457 | { |
458 | 458 | $new_statuses = []; |
459 | 459 | foreach ($this->_custom_stati as $status => $props) { |
460 | - $new_statuses[ $status ] = $props['label']; |
|
460 | + $new_statuses[$status] = $props['label']; |
|
461 | 461 | } |
462 | 462 | return $new_statuses; |
463 | 463 | } |
@@ -483,15 +483,15 @@ discard block |
||
483 | 483 | $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
484 | 484 | foreach ($fields_for_that_table as $field_obj) { |
485 | 485 | if ( |
486 | - ! isset($post[ $field_obj->get_table_column() ]) |
|
487 | - && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
486 | + ! isset($post[$field_obj->get_table_column()]) |
|
487 | + && ! isset($post[$field_obj->get_qualified_column()]) |
|
488 | 488 | ) { |
489 | 489 | $has_all_necessary_fields_for_table = false; |
490 | 490 | } |
491 | 491 | } |
492 | 492 | } |
493 | 493 | // if we don't have all the fields we need, then just fetch the proper model from the DB |
494 | - if (! $has_all_necessary_fields_for_table) { |
|
494 | + if ( ! $has_all_necessary_fields_for_table) { |
|
495 | 495 | return $this->get_one_by_ID($post['ID']); |
496 | 496 | } |
497 | 497 | return $this->instantiate_class_from_array_or_object($post); |
@@ -516,10 +516,10 @@ discard block |
||
516 | 516 | $fields_for_that_table = $this->_get_fields_for_table($table_obj->get_table_alias()); |
517 | 517 | foreach ($fields_for_that_table as $field_obj) { |
518 | 518 | if ( |
519 | - ! isset($post[ $field_obj->get_table_column() ]) |
|
520 | - && ! isset($post[ $field_obj->get_qualified_column() ]) |
|
519 | + ! isset($post[$field_obj->get_table_column()]) |
|
520 | + && ! isset($post[$field_obj->get_qualified_column()]) |
|
521 | 521 | ) { |
522 | - $tables_needing_to_be_queried[ $table_obj->get_table_alias() ] = $table_obj; |
|
522 | + $tables_needing_to_be_queried[$table_obj->get_table_alias()] = $table_obj; |
|
523 | 523 | } |
524 | 524 | } |
525 | 525 | } |
@@ -543,7 +543,7 @@ discard block |
||
543 | 543 | ARRAY_A, |
544 | 544 | ] |
545 | 545 | ); |
546 | - if (! empty($missing_data)) { |
|
546 | + if ( ! empty($missing_data)) { |
|
547 | 547 | $post = array_merge($post, $missing_data); |
548 | 548 | } |
549 | 549 | } else { |
@@ -10,498 +10,498 @@ |
||
10 | 10 | class EEM_Ticket extends EEM_Soft_Delete_Base |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * the following constants define where tickets can be viewed throughout the UI |
|
15 | - * |
|
16 | - * TICKET_VISIBILITY_NONE - will not be displayed anywhere |
|
17 | - * TICKET_VISIBILITY_PUBLIC - displayed basically anywhere |
|
18 | - * TICKET_VISIBILITY_MEMBERS_ONLY - displayed to any logged in user |
|
19 | - * TICKET_VISIBILITY_ADMINS_ONLY - displayed to any logged in user that is an admin |
|
20 | - * TICKET_VISIBILITY_ADMIN_UI_ONLY - only displayed in the admin, never publicly |
|
21 | - */ |
|
22 | - public const TICKET_VISIBILITY_NONE_KEY = 'NONE'; |
|
23 | - |
|
24 | - public const TICKET_VISIBILITY_NONE_VALUE = 0; |
|
25 | - |
|
26 | - public const TICKET_VISIBILITY_PUBLIC_KEY = 'PUBLIC'; |
|
27 | - |
|
28 | - public const TICKET_VISIBILITY_PUBLIC_VALUE = 100; |
|
29 | - |
|
30 | - public const TICKET_VISIBILITY_MEMBERS_ONLY_KEY = 'MEMBERS_ONLY'; |
|
31 | - |
|
32 | - public const TICKET_VISIBILITY_MEMBERS_ONLY_VALUE = 200; |
|
33 | - |
|
34 | - public const TICKET_VISIBILITY_ADMINS_ONLY_KEY = 'ADMINS_ONLY'; |
|
35 | - |
|
36 | - public const TICKET_VISIBILITY_ADMINS_ONLY_VALUE = 300; |
|
37 | - |
|
38 | - public const TICKET_VISIBILITY_ADMIN_UI_ONLY_KEY = 'ADMIN_UI_ONLY'; |
|
39 | - |
|
40 | - public const TICKET_VISIBILITY_ADMIN_UI_ONLY_VALUE = 400; |
|
41 | - |
|
42 | - |
|
43 | - /** |
|
44 | - * defines where tickets can be viewed throughout the UI |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - private $ticket_visibility; |
|
49 | - |
|
50 | - /** |
|
51 | - * private instance of the EEM_Ticket object |
|
52 | - * |
|
53 | - * @var EEM_Ticket $_instance |
|
54 | - */ |
|
55 | - protected static $_instance; |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * private constructor to prevent direct creation |
|
60 | - * |
|
61 | - * @Constructor |
|
62 | - * @access private |
|
63 | - * @param string $timezone string representing the timezone we want to set for returned Date Time Strings |
|
64 | - * (and any incoming timezone data that gets saved). |
|
65 | - * Note this just sends the timezone info to the date time model field objects. |
|
66 | - * Default is NULL |
|
67 | - * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
68 | - * @throws EE_Error |
|
69 | - */ |
|
70 | - protected function __construct(string $timezone = '') |
|
71 | - { |
|
72 | - $this->singular_item = esc_html__('Ticket', 'event_espresso'); |
|
73 | - $this->plural_item = esc_html__('Tickets', 'event_espresso'); |
|
74 | - $this->_tables = [ |
|
75 | - 'Ticket' => new EE_Primary_Table('esp_ticket', 'TKT_ID'), |
|
76 | - ]; |
|
77 | - $this->parseTicketVisibilityOptions(); |
|
78 | - $this->_fields = [ |
|
79 | - 'Ticket' => [ |
|
80 | - 'TKT_ID' => new EE_Primary_Key_Int_Field( |
|
81 | - 'TKT_ID', |
|
82 | - esc_html__('Ticket ID', 'event_espresso') |
|
83 | - ), |
|
84 | - 'TTM_ID' => new EE_Foreign_Key_Int_Field( |
|
85 | - 'TTM_ID', |
|
86 | - esc_html__('Ticket Template ID', 'event_espresso'), |
|
87 | - false, |
|
88 | - 0, |
|
89 | - 'Ticket_Template' |
|
90 | - ), |
|
91 | - 'TKT_name' => new EE_Plain_Text_Field( |
|
92 | - 'TKT_name', |
|
93 | - esc_html__('Ticket Name', 'event_espresso'), |
|
94 | - false, |
|
95 | - '' |
|
96 | - ), |
|
97 | - 'TKT_description' => new EE_Post_Content_Field( |
|
98 | - 'TKT_description', |
|
99 | - esc_html__('Description of Ticket', 'event_espresso'), |
|
100 | - false, |
|
101 | - '' |
|
102 | - ), |
|
103 | - 'TKT_start_date' => new EE_Datetime_Field( |
|
104 | - 'TKT_start_date', |
|
105 | - esc_html__('Start time/date of Ticket', 'event_espresso'), |
|
106 | - false, |
|
107 | - EE_Datetime_Field::now, |
|
108 | - $timezone |
|
109 | - ), |
|
110 | - 'TKT_end_date' => new EE_Datetime_Field( |
|
111 | - 'TKT_end_date', |
|
112 | - esc_html__('End time/date of Ticket', 'event_espresso'), |
|
113 | - false, |
|
114 | - EE_Datetime_Field::now, |
|
115 | - $timezone |
|
116 | - ), |
|
117 | - 'TKT_min' => new EE_Integer_Field( |
|
118 | - 'TKT_min', |
|
119 | - esc_html__('Minimum quantity of this ticket that must be purchased', 'event_espresso'), |
|
120 | - false, |
|
121 | - 0 |
|
122 | - ), |
|
123 | - 'TKT_max' => new EE_Infinite_Integer_Field( |
|
124 | - 'TKT_max', |
|
125 | - esc_html__( |
|
126 | - 'Maximum quantity of this ticket that can be purchased in one transaction', |
|
127 | - 'event_espresso' |
|
128 | - ), |
|
129 | - false, |
|
130 | - EE_INF |
|
131 | - ), |
|
132 | - 'TKT_price' => new EE_Money_Field( |
|
133 | - 'TKT_price', |
|
134 | - esc_html__('Final calculated price for ticket', 'event_espresso'), |
|
135 | - false, |
|
136 | - 0 |
|
137 | - ), |
|
138 | - 'TKT_sold' => new EE_Integer_Field( |
|
139 | - 'TKT_sold', |
|
140 | - esc_html__('Number of this ticket sold', 'event_espresso'), |
|
141 | - false, |
|
142 | - 0 |
|
143 | - ), |
|
144 | - 'TKT_qty' => new EE_Infinite_Integer_Field( |
|
145 | - 'TKT_qty', |
|
146 | - esc_html__('Quantity of this ticket that is available', 'event_espresso'), |
|
147 | - false, |
|
148 | - EE_INF |
|
149 | - ), |
|
150 | - 'TKT_reserved' => new EE_Integer_Field( |
|
151 | - 'TKT_reserved', |
|
152 | - esc_html__( |
|
153 | - 'Quantity of this ticket that is reserved, but not yet fully purchased', |
|
154 | - 'event_espresso' |
|
155 | - ), |
|
156 | - false, |
|
157 | - 0 |
|
158 | - ), |
|
159 | - 'TKT_uses' => new EE_Infinite_Integer_Field( |
|
160 | - 'TKT_uses', |
|
161 | - esc_html__('Number of datetimes this ticket can be used at', 'event_espresso'), |
|
162 | - false, |
|
163 | - EE_INF |
|
164 | - ), |
|
165 | - 'TKT_required' => new EE_Boolean_Field( |
|
166 | - 'TKT_required', |
|
167 | - esc_html__( |
|
168 | - 'Flag indicating whether this ticket must be purchased with a transaction', |
|
169 | - 'event_espresso' |
|
170 | - ), |
|
171 | - false, |
|
172 | - false |
|
173 | - ), |
|
174 | - 'TKT_taxable' => new EE_Boolean_Field( |
|
175 | - 'TKT_taxable', |
|
176 | - esc_html__( |
|
177 | - 'Flag indicating whether there is tax applied on this ticket', |
|
178 | - 'event_espresso' |
|
179 | - ), |
|
180 | - false, |
|
181 | - false |
|
182 | - ), |
|
183 | - 'TKT_is_default' => new EE_Boolean_Field( |
|
184 | - 'TKT_is_default', |
|
185 | - esc_html__('Flag indicating that this ticket is a default ticket', 'event_espresso'), |
|
186 | - false, |
|
187 | - false |
|
188 | - ), |
|
189 | - 'TKT_order' => new EE_Integer_Field( |
|
190 | - 'TKT_order', |
|
191 | - esc_html__( |
|
192 | - 'The order in which the Ticket is displayed in the editor (used for autosaves when the form doesn\'t have the ticket ID yet)', |
|
193 | - 'event_espresso' |
|
194 | - ), |
|
195 | - false, |
|
196 | - 0 |
|
197 | - ), |
|
198 | - 'TKT_row' => new EE_Integer_Field( |
|
199 | - 'TKT_row', |
|
200 | - esc_html__('How tickets are displayed in the ui', 'event_espresso'), |
|
201 | - false, |
|
202 | - 0 |
|
203 | - ), |
|
204 | - 'TKT_deleted' => new EE_Trashed_Flag_Field( |
|
205 | - 'TKT_deleted', |
|
206 | - esc_html__('Flag indicating if this has been archived or not', 'event_espresso'), |
|
207 | - false, |
|
208 | - false |
|
209 | - ), |
|
210 | - 'TKT_wp_user' => new EE_WP_User_Field( |
|
211 | - 'TKT_wp_user', |
|
212 | - esc_html__('Ticket Creator ID', 'event_espresso'), |
|
213 | - false |
|
214 | - ), |
|
215 | - 'TKT_parent' => new EE_Integer_Field( |
|
216 | - 'TKT_parent', |
|
217 | - esc_html__( |
|
218 | - 'Indicates what TKT_ID is the parent of this TKT_ID (used in autosaves/revisions)', |
|
219 | - 'event_espresso' |
|
220 | - ), |
|
221 | - true, |
|
222 | - 0 |
|
223 | - ), |
|
224 | - 'TKT_reverse_calculate' => new EE_Boolean_Field( |
|
225 | - 'TKT_reverse_calculate', |
|
226 | - esc_html__( |
|
227 | - 'Flag indicating whether ticket calculations should run in reverse and calculate the base ticket price from the provided ticket total.', |
|
228 | - 'event_espresso' |
|
229 | - ), |
|
230 | - false, |
|
231 | - false |
|
232 | - ), |
|
233 | - 'TKT_visibility' => new EE_Enum_Integer_Field( |
|
234 | - 'TKT_visibility', |
|
235 | - esc_html__('Defines where the ticket can be viewed throughout the UI.', 'event_espresso'), |
|
236 | - false, |
|
237 | - EEM_Ticket::TICKET_VISIBILITY_PUBLIC_VALUE, |
|
238 | - $this->getTicketVisibilityEnumOptions() |
|
239 | - ), |
|
240 | - ], |
|
241 | - ]; |
|
242 | - $this->_model_relations = [ |
|
243 | - 'Datetime' => new EE_HABTM_Relation('Datetime_Ticket'), |
|
244 | - 'Datetime_Ticket' => new EE_Has_Many_Relation(), |
|
245 | - 'Price' => new EE_HABTM_Relation('Ticket_Price'), |
|
246 | - 'Ticket_Template' => new EE_Belongs_To_Relation(), |
|
247 | - 'Registration' => new EE_Has_Many_Relation(), |
|
248 | - 'WP_User' => new EE_Belongs_To_Relation(), |
|
249 | - ]; |
|
250 | - // this model is generally available for reading |
|
251 | - $path_to_event = 'Datetime.Event'; |
|
252 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Default_Public( |
|
253 | - 'TKT_is_default', |
|
254 | - $path_to_event |
|
255 | - ); |
|
256 | - // account for default tickets in the caps |
|
257 | - $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] |
|
258 | - = new EE_Restriction_Generator_Default_Protected( |
|
259 | - 'TKT_is_default', |
|
260 | - $path_to_event |
|
261 | - ); |
|
262 | - $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Default_Protected( |
|
263 | - 'TKT_is_default', |
|
264 | - $path_to_event |
|
265 | - ); |
|
266 | - $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Default_Protected( |
|
267 | - 'TKT_is_default', |
|
268 | - $path_to_event |
|
269 | - ); |
|
270 | - $this->model_chain_to_password = $path_to_event; |
|
271 | - parent::__construct($timezone); |
|
272 | - } |
|
273 | - |
|
274 | - |
|
275 | - /** |
|
276 | - * override parent class get_all() just to fix return types in IDE |
|
277 | - * |
|
278 | - * @param array $query_params |
|
279 | - * @return EE_Ticket[] |
|
280 | - * @throws EE_Error |
|
281 | - * @throws ReflectionException |
|
282 | - * @since $VID:$ |
|
283 | - */ |
|
284 | - public function get_all($query_params = []): array |
|
285 | - { |
|
286 | - return parent::get_all($query_params); |
|
287 | - } |
|
288 | - |
|
289 | - |
|
290 | - /** |
|
291 | - * This returns all tickets that are defaults from the db |
|
292 | - * |
|
293 | - * @return EE_Ticket[] |
|
294 | - * @throws EE_Error |
|
295 | - * @throws ReflectionException |
|
296 | - */ |
|
297 | - public function get_all_default_tickets(): array |
|
298 | - { |
|
299 | - $tickets = $this->get_all( |
|
300 | - [ |
|
301 | - [ |
|
302 | - 'TKT_is_default' => 1, |
|
303 | - 'TKT_visibility' => ['>', EEM_Ticket::TICKET_VISIBILITY_NONE_VALUE], |
|
304 | - ], |
|
305 | - 'order_by' => ['TKT_ID' => 'ASC'], |
|
306 | - ] |
|
307 | - ); |
|
308 | - // we need to set the start date and end date to today's date and the start of the default dtt |
|
309 | - return $this->_set_default_dates($tickets); |
|
310 | - } |
|
311 | - |
|
312 | - |
|
313 | - /** |
|
314 | - * sets up relevant start and end date for EE_Ticket (s) |
|
315 | - * |
|
316 | - * @param EE_Ticket[] $tickets |
|
317 | - * @return EE_Ticket[] |
|
318 | - * @throws EE_Error |
|
319 | - * @throws ReflectionException |
|
320 | - */ |
|
321 | - private function _set_default_dates(array $tickets): array |
|
322 | - { |
|
323 | - foreach ($tickets as $ticket) { |
|
324 | - $ticket->set( |
|
325 | - 'TKT_start_date', |
|
326 | - (int) $this->current_time_for_query('TKT_start_date', true) |
|
327 | - ); |
|
328 | - $ticket->set( |
|
329 | - 'TKT_end_date', |
|
330 | - (int) $this->current_time_for_query('TKT_end_date', true) + MONTH_IN_SECONDS |
|
331 | - ); |
|
332 | - $ticket->set_end_time( |
|
333 | - $this->convert_datetime_for_query( |
|
334 | - 'TKT_end_date', |
|
335 | - '11:59 pm', |
|
336 | - 'g:i a', |
|
337 | - $this->_timezone |
|
338 | - ) |
|
339 | - ); |
|
340 | - } |
|
341 | - return $tickets; |
|
342 | - } |
|
343 | - |
|
344 | - |
|
345 | - /** |
|
346 | - * Gets the total number of tickets available at a particular datetime (does |
|
347 | - * NOT take int account the datetime's spaces available) |
|
348 | - * |
|
349 | - * @param int $DTT_ID |
|
350 | - * @param array $query_params |
|
351 | - * @return int |
|
352 | - * @throws EE_Error |
|
353 | - * @throws ReflectionException |
|
354 | - */ |
|
355 | - public function sum_tickets_currently_available_at_datetime(int $DTT_ID, array $query_params = []): int |
|
356 | - { |
|
357 | - $query_params += [['TKT_visibility' => ['>', EEM_Ticket::TICKET_VISIBILITY_NONE_VALUE]]]; |
|
358 | - return EEM_Datetime::instance()->sum_tickets_currently_available_at_datetime($DTT_ID, $query_params); |
|
359 | - } |
|
360 | - |
|
361 | - |
|
362 | - /** |
|
363 | - * Updates the TKT_sold quantity on all the tickets matching $query_params |
|
364 | - * |
|
365 | - * @param EE_Ticket[] $tickets |
|
366 | - * @return void |
|
367 | - * @throws EE_Error |
|
368 | - * @throws ReflectionException |
|
369 | - */ |
|
370 | - public function update_tickets_sold(array $tickets) |
|
371 | - { |
|
372 | - foreach ($tickets as $ticket) { |
|
373 | - $ticket->update_tickets_sold(); |
|
374 | - } |
|
375 | - } |
|
376 | - |
|
377 | - |
|
378 | - /** |
|
379 | - * returns an array of EE_Ticket objects with a non-zero value for TKT_reserved |
|
380 | - * |
|
381 | - * @return EE_Ticket[] |
|
382 | - * @throws EE_Error |
|
383 | - * @throws ReflectionException |
|
384 | - */ |
|
385 | - public function get_tickets_with_reservations(): array |
|
386 | - { |
|
387 | - return $this->get_all( |
|
388 | - [ |
|
389 | - [ |
|
390 | - 'TKT_reserved' => ['>', 0], |
|
391 | - 'TKT_visibility' => ['>', EEM_Ticket::TICKET_VISIBILITY_NONE_VALUE], |
|
392 | - ], |
|
393 | - ] |
|
394 | - ); |
|
395 | - } |
|
396 | - |
|
397 | - |
|
398 | - /** |
|
399 | - * returns an array of EE_Ticket objects matching the supplied list of IDs |
|
400 | - * |
|
401 | - * @param array $ticket_IDs |
|
402 | - * @return EE_Ticket[] |
|
403 | - * @throws EE_Error |
|
404 | - * @throws ReflectionException |
|
405 | - */ |
|
406 | - public function get_tickets_with_IDs(array $ticket_IDs): array |
|
407 | - { |
|
408 | - return $this->get_all( |
|
409 | - [ |
|
410 | - [ |
|
411 | - 'TKT_ID' => ['IN', $ticket_IDs], |
|
412 | - ], |
|
413 | - ] |
|
414 | - ); |
|
415 | - } |
|
416 | - |
|
417 | - |
|
418 | - /** |
|
419 | - * @return void |
|
420 | - */ |
|
421 | - private function parseTicketVisibilityOptions() |
|
422 | - { |
|
423 | - $this->ticket_visibility = (array) apply_filters( |
|
424 | - 'FHEE__EEM_Ticket__construct__ticket_visibility', |
|
425 | - [ |
|
426 | - EEM_Ticket::TICKET_VISIBILITY_PUBLIC_KEY => [ |
|
427 | - 'label' => esc_html__('Public', 'event_espresso'), |
|
428 | - 'value' => EEM_Ticket::TICKET_VISIBILITY_PUBLIC_VALUE, |
|
429 | - ], |
|
430 | - EEM_Ticket::TICKET_VISIBILITY_MEMBERS_ONLY_KEY => [ |
|
431 | - 'label' => esc_html__('Members only', 'event_espresso'), |
|
432 | - 'value' => EEM_Ticket::TICKET_VISIBILITY_MEMBERS_ONLY_VALUE, |
|
433 | - ], |
|
434 | - EEM_Ticket::TICKET_VISIBILITY_ADMINS_ONLY_KEY => [ |
|
435 | - 'label' => esc_html__('Admins only', 'event_espresso'), |
|
436 | - 'value' => EEM_Ticket::TICKET_VISIBILITY_ADMINS_ONLY_VALUE, |
|
437 | - ], |
|
438 | - EEM_Ticket::TICKET_VISIBILITY_ADMIN_UI_ONLY_KEY => [ |
|
439 | - 'label' => esc_html__('Admin UI only', 'event_espresso'), |
|
440 | - 'value' => EEM_Ticket::TICKET_VISIBILITY_ADMIN_UI_ONLY_VALUE, |
|
441 | - ], |
|
442 | - EEM_Ticket::TICKET_VISIBILITY_NONE_KEY => [ |
|
443 | - 'label' => esc_html__('None', 'event_espresso'), |
|
444 | - 'value' => EEM_Ticket::TICKET_VISIBILITY_NONE_VALUE, |
|
445 | - ], |
|
446 | - ] |
|
447 | - ); |
|
448 | - } |
|
449 | - |
|
450 | - |
|
451 | - /** |
|
452 | - * @return array |
|
453 | - */ |
|
454 | - public function getTicketVisibilityEnumOptions(): array |
|
455 | - { |
|
456 | - $ticket_visibility = []; |
|
457 | - foreach ($this->ticket_visibility as $visibility) { |
|
458 | - if (isset($visibility['value'], $visibility['label'])) { |
|
459 | - $ticket_visibility[ $visibility['value'] ] = $visibility['label']; |
|
460 | - } |
|
461 | - } |
|
462 | - return $ticket_visibility; |
|
463 | - } |
|
464 | - |
|
465 | - |
|
466 | - /** |
|
467 | - * @return array |
|
468 | - */ |
|
469 | - public function getTicketVisibilityValues(): array |
|
470 | - { |
|
471 | - // copy ticket_visibility array |
|
472 | - $ticket_visibility_options = $this->ticket_visibility; |
|
473 | - foreach ($ticket_visibility_options as $ticket_visibility_option) { |
|
474 | - // remove labels because we only want the values |
|
475 | - unset($ticket_visibility_option['label']); |
|
476 | - } |
|
477 | - return $ticket_visibility_options; |
|
478 | - } |
|
479 | - |
|
480 | - |
|
481 | - /** |
|
482 | - * @return array |
|
483 | - */ |
|
484 | - public function getTicketVisibilityLabels(): array |
|
485 | - { |
|
486 | - $ticket_visibility_options = []; |
|
487 | - foreach ($this->ticket_visibility as $key => $ticket_visibility_option) { |
|
488 | - if (isset($ticket_visibility_option['label'])) { |
|
489 | - // change because we only want the labels tied to the keys |
|
490 | - $ticket_visibility_options[] = [ |
|
491 | - 'value' => $key, |
|
492 | - 'label' => $ticket_visibility_option['label'] |
|
493 | - ]; |
|
494 | - } |
|
495 | - } |
|
496 | - return $ticket_visibility_options; |
|
497 | - } |
|
498 | - |
|
499 | - |
|
500 | - /** |
|
501 | - * @return array |
|
502 | - */ |
|
503 | - public function ticketVisibilityOptions(): array |
|
504 | - { |
|
505 | - return $this->ticket_visibility; |
|
506 | - } |
|
13 | + /** |
|
14 | + * the following constants define where tickets can be viewed throughout the UI |
|
15 | + * |
|
16 | + * TICKET_VISIBILITY_NONE - will not be displayed anywhere |
|
17 | + * TICKET_VISIBILITY_PUBLIC - displayed basically anywhere |
|
18 | + * TICKET_VISIBILITY_MEMBERS_ONLY - displayed to any logged in user |
|
19 | + * TICKET_VISIBILITY_ADMINS_ONLY - displayed to any logged in user that is an admin |
|
20 | + * TICKET_VISIBILITY_ADMIN_UI_ONLY - only displayed in the admin, never publicly |
|
21 | + */ |
|
22 | + public const TICKET_VISIBILITY_NONE_KEY = 'NONE'; |
|
23 | + |
|
24 | + public const TICKET_VISIBILITY_NONE_VALUE = 0; |
|
25 | + |
|
26 | + public const TICKET_VISIBILITY_PUBLIC_KEY = 'PUBLIC'; |
|
27 | + |
|
28 | + public const TICKET_VISIBILITY_PUBLIC_VALUE = 100; |
|
29 | + |
|
30 | + public const TICKET_VISIBILITY_MEMBERS_ONLY_KEY = 'MEMBERS_ONLY'; |
|
31 | + |
|
32 | + public const TICKET_VISIBILITY_MEMBERS_ONLY_VALUE = 200; |
|
33 | + |
|
34 | + public const TICKET_VISIBILITY_ADMINS_ONLY_KEY = 'ADMINS_ONLY'; |
|
35 | + |
|
36 | + public const TICKET_VISIBILITY_ADMINS_ONLY_VALUE = 300; |
|
37 | + |
|
38 | + public const TICKET_VISIBILITY_ADMIN_UI_ONLY_KEY = 'ADMIN_UI_ONLY'; |
|
39 | + |
|
40 | + public const TICKET_VISIBILITY_ADMIN_UI_ONLY_VALUE = 400; |
|
41 | + |
|
42 | + |
|
43 | + /** |
|
44 | + * defines where tickets can be viewed throughout the UI |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + private $ticket_visibility; |
|
49 | + |
|
50 | + /** |
|
51 | + * private instance of the EEM_Ticket object |
|
52 | + * |
|
53 | + * @var EEM_Ticket $_instance |
|
54 | + */ |
|
55 | + protected static $_instance; |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * private constructor to prevent direct creation |
|
60 | + * |
|
61 | + * @Constructor |
|
62 | + * @access private |
|
63 | + * @param string $timezone string representing the timezone we want to set for returned Date Time Strings |
|
64 | + * (and any incoming timezone data that gets saved). |
|
65 | + * Note this just sends the timezone info to the date time model field objects. |
|
66 | + * Default is NULL |
|
67 | + * (and will be assumed using the set timezone in the 'timezone_string' wp option) |
|
68 | + * @throws EE_Error |
|
69 | + */ |
|
70 | + protected function __construct(string $timezone = '') |
|
71 | + { |
|
72 | + $this->singular_item = esc_html__('Ticket', 'event_espresso'); |
|
73 | + $this->plural_item = esc_html__('Tickets', 'event_espresso'); |
|
74 | + $this->_tables = [ |
|
75 | + 'Ticket' => new EE_Primary_Table('esp_ticket', 'TKT_ID'), |
|
76 | + ]; |
|
77 | + $this->parseTicketVisibilityOptions(); |
|
78 | + $this->_fields = [ |
|
79 | + 'Ticket' => [ |
|
80 | + 'TKT_ID' => new EE_Primary_Key_Int_Field( |
|
81 | + 'TKT_ID', |
|
82 | + esc_html__('Ticket ID', 'event_espresso') |
|
83 | + ), |
|
84 | + 'TTM_ID' => new EE_Foreign_Key_Int_Field( |
|
85 | + 'TTM_ID', |
|
86 | + esc_html__('Ticket Template ID', 'event_espresso'), |
|
87 | + false, |
|
88 | + 0, |
|
89 | + 'Ticket_Template' |
|
90 | + ), |
|
91 | + 'TKT_name' => new EE_Plain_Text_Field( |
|
92 | + 'TKT_name', |
|
93 | + esc_html__('Ticket Name', 'event_espresso'), |
|
94 | + false, |
|
95 | + '' |
|
96 | + ), |
|
97 | + 'TKT_description' => new EE_Post_Content_Field( |
|
98 | + 'TKT_description', |
|
99 | + esc_html__('Description of Ticket', 'event_espresso'), |
|
100 | + false, |
|
101 | + '' |
|
102 | + ), |
|
103 | + 'TKT_start_date' => new EE_Datetime_Field( |
|
104 | + 'TKT_start_date', |
|
105 | + esc_html__('Start time/date of Ticket', 'event_espresso'), |
|
106 | + false, |
|
107 | + EE_Datetime_Field::now, |
|
108 | + $timezone |
|
109 | + ), |
|
110 | + 'TKT_end_date' => new EE_Datetime_Field( |
|
111 | + 'TKT_end_date', |
|
112 | + esc_html__('End time/date of Ticket', 'event_espresso'), |
|
113 | + false, |
|
114 | + EE_Datetime_Field::now, |
|
115 | + $timezone |
|
116 | + ), |
|
117 | + 'TKT_min' => new EE_Integer_Field( |
|
118 | + 'TKT_min', |
|
119 | + esc_html__('Minimum quantity of this ticket that must be purchased', 'event_espresso'), |
|
120 | + false, |
|
121 | + 0 |
|
122 | + ), |
|
123 | + 'TKT_max' => new EE_Infinite_Integer_Field( |
|
124 | + 'TKT_max', |
|
125 | + esc_html__( |
|
126 | + 'Maximum quantity of this ticket that can be purchased in one transaction', |
|
127 | + 'event_espresso' |
|
128 | + ), |
|
129 | + false, |
|
130 | + EE_INF |
|
131 | + ), |
|
132 | + 'TKT_price' => new EE_Money_Field( |
|
133 | + 'TKT_price', |
|
134 | + esc_html__('Final calculated price for ticket', 'event_espresso'), |
|
135 | + false, |
|
136 | + 0 |
|
137 | + ), |
|
138 | + 'TKT_sold' => new EE_Integer_Field( |
|
139 | + 'TKT_sold', |
|
140 | + esc_html__('Number of this ticket sold', 'event_espresso'), |
|
141 | + false, |
|
142 | + 0 |
|
143 | + ), |
|
144 | + 'TKT_qty' => new EE_Infinite_Integer_Field( |
|
145 | + 'TKT_qty', |
|
146 | + esc_html__('Quantity of this ticket that is available', 'event_espresso'), |
|
147 | + false, |
|
148 | + EE_INF |
|
149 | + ), |
|
150 | + 'TKT_reserved' => new EE_Integer_Field( |
|
151 | + 'TKT_reserved', |
|
152 | + esc_html__( |
|
153 | + 'Quantity of this ticket that is reserved, but not yet fully purchased', |
|
154 | + 'event_espresso' |
|
155 | + ), |
|
156 | + false, |
|
157 | + 0 |
|
158 | + ), |
|
159 | + 'TKT_uses' => new EE_Infinite_Integer_Field( |
|
160 | + 'TKT_uses', |
|
161 | + esc_html__('Number of datetimes this ticket can be used at', 'event_espresso'), |
|
162 | + false, |
|
163 | + EE_INF |
|
164 | + ), |
|
165 | + 'TKT_required' => new EE_Boolean_Field( |
|
166 | + 'TKT_required', |
|
167 | + esc_html__( |
|
168 | + 'Flag indicating whether this ticket must be purchased with a transaction', |
|
169 | + 'event_espresso' |
|
170 | + ), |
|
171 | + false, |
|
172 | + false |
|
173 | + ), |
|
174 | + 'TKT_taxable' => new EE_Boolean_Field( |
|
175 | + 'TKT_taxable', |
|
176 | + esc_html__( |
|
177 | + 'Flag indicating whether there is tax applied on this ticket', |
|
178 | + 'event_espresso' |
|
179 | + ), |
|
180 | + false, |
|
181 | + false |
|
182 | + ), |
|
183 | + 'TKT_is_default' => new EE_Boolean_Field( |
|
184 | + 'TKT_is_default', |
|
185 | + esc_html__('Flag indicating that this ticket is a default ticket', 'event_espresso'), |
|
186 | + false, |
|
187 | + false |
|
188 | + ), |
|
189 | + 'TKT_order' => new EE_Integer_Field( |
|
190 | + 'TKT_order', |
|
191 | + esc_html__( |
|
192 | + 'The order in which the Ticket is displayed in the editor (used for autosaves when the form doesn\'t have the ticket ID yet)', |
|
193 | + 'event_espresso' |
|
194 | + ), |
|
195 | + false, |
|
196 | + 0 |
|
197 | + ), |
|
198 | + 'TKT_row' => new EE_Integer_Field( |
|
199 | + 'TKT_row', |
|
200 | + esc_html__('How tickets are displayed in the ui', 'event_espresso'), |
|
201 | + false, |
|
202 | + 0 |
|
203 | + ), |
|
204 | + 'TKT_deleted' => new EE_Trashed_Flag_Field( |
|
205 | + 'TKT_deleted', |
|
206 | + esc_html__('Flag indicating if this has been archived or not', 'event_espresso'), |
|
207 | + false, |
|
208 | + false |
|
209 | + ), |
|
210 | + 'TKT_wp_user' => new EE_WP_User_Field( |
|
211 | + 'TKT_wp_user', |
|
212 | + esc_html__('Ticket Creator ID', 'event_espresso'), |
|
213 | + false |
|
214 | + ), |
|
215 | + 'TKT_parent' => new EE_Integer_Field( |
|
216 | + 'TKT_parent', |
|
217 | + esc_html__( |
|
218 | + 'Indicates what TKT_ID is the parent of this TKT_ID (used in autosaves/revisions)', |
|
219 | + 'event_espresso' |
|
220 | + ), |
|
221 | + true, |
|
222 | + 0 |
|
223 | + ), |
|
224 | + 'TKT_reverse_calculate' => new EE_Boolean_Field( |
|
225 | + 'TKT_reverse_calculate', |
|
226 | + esc_html__( |
|
227 | + 'Flag indicating whether ticket calculations should run in reverse and calculate the base ticket price from the provided ticket total.', |
|
228 | + 'event_espresso' |
|
229 | + ), |
|
230 | + false, |
|
231 | + false |
|
232 | + ), |
|
233 | + 'TKT_visibility' => new EE_Enum_Integer_Field( |
|
234 | + 'TKT_visibility', |
|
235 | + esc_html__('Defines where the ticket can be viewed throughout the UI.', 'event_espresso'), |
|
236 | + false, |
|
237 | + EEM_Ticket::TICKET_VISIBILITY_PUBLIC_VALUE, |
|
238 | + $this->getTicketVisibilityEnumOptions() |
|
239 | + ), |
|
240 | + ], |
|
241 | + ]; |
|
242 | + $this->_model_relations = [ |
|
243 | + 'Datetime' => new EE_HABTM_Relation('Datetime_Ticket'), |
|
244 | + 'Datetime_Ticket' => new EE_Has_Many_Relation(), |
|
245 | + 'Price' => new EE_HABTM_Relation('Ticket_Price'), |
|
246 | + 'Ticket_Template' => new EE_Belongs_To_Relation(), |
|
247 | + 'Registration' => new EE_Has_Many_Relation(), |
|
248 | + 'WP_User' => new EE_Belongs_To_Relation(), |
|
249 | + ]; |
|
250 | + // this model is generally available for reading |
|
251 | + $path_to_event = 'Datetime.Event'; |
|
252 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Default_Public( |
|
253 | + 'TKT_is_default', |
|
254 | + $path_to_event |
|
255 | + ); |
|
256 | + // account for default tickets in the caps |
|
257 | + $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] |
|
258 | + = new EE_Restriction_Generator_Default_Protected( |
|
259 | + 'TKT_is_default', |
|
260 | + $path_to_event |
|
261 | + ); |
|
262 | + $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Default_Protected( |
|
263 | + 'TKT_is_default', |
|
264 | + $path_to_event |
|
265 | + ); |
|
266 | + $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Default_Protected( |
|
267 | + 'TKT_is_default', |
|
268 | + $path_to_event |
|
269 | + ); |
|
270 | + $this->model_chain_to_password = $path_to_event; |
|
271 | + parent::__construct($timezone); |
|
272 | + } |
|
273 | + |
|
274 | + |
|
275 | + /** |
|
276 | + * override parent class get_all() just to fix return types in IDE |
|
277 | + * |
|
278 | + * @param array $query_params |
|
279 | + * @return EE_Ticket[] |
|
280 | + * @throws EE_Error |
|
281 | + * @throws ReflectionException |
|
282 | + * @since $VID:$ |
|
283 | + */ |
|
284 | + public function get_all($query_params = []): array |
|
285 | + { |
|
286 | + return parent::get_all($query_params); |
|
287 | + } |
|
288 | + |
|
289 | + |
|
290 | + /** |
|
291 | + * This returns all tickets that are defaults from the db |
|
292 | + * |
|
293 | + * @return EE_Ticket[] |
|
294 | + * @throws EE_Error |
|
295 | + * @throws ReflectionException |
|
296 | + */ |
|
297 | + public function get_all_default_tickets(): array |
|
298 | + { |
|
299 | + $tickets = $this->get_all( |
|
300 | + [ |
|
301 | + [ |
|
302 | + 'TKT_is_default' => 1, |
|
303 | + 'TKT_visibility' => ['>', EEM_Ticket::TICKET_VISIBILITY_NONE_VALUE], |
|
304 | + ], |
|
305 | + 'order_by' => ['TKT_ID' => 'ASC'], |
|
306 | + ] |
|
307 | + ); |
|
308 | + // we need to set the start date and end date to today's date and the start of the default dtt |
|
309 | + return $this->_set_default_dates($tickets); |
|
310 | + } |
|
311 | + |
|
312 | + |
|
313 | + /** |
|
314 | + * sets up relevant start and end date for EE_Ticket (s) |
|
315 | + * |
|
316 | + * @param EE_Ticket[] $tickets |
|
317 | + * @return EE_Ticket[] |
|
318 | + * @throws EE_Error |
|
319 | + * @throws ReflectionException |
|
320 | + */ |
|
321 | + private function _set_default_dates(array $tickets): array |
|
322 | + { |
|
323 | + foreach ($tickets as $ticket) { |
|
324 | + $ticket->set( |
|
325 | + 'TKT_start_date', |
|
326 | + (int) $this->current_time_for_query('TKT_start_date', true) |
|
327 | + ); |
|
328 | + $ticket->set( |
|
329 | + 'TKT_end_date', |
|
330 | + (int) $this->current_time_for_query('TKT_end_date', true) + MONTH_IN_SECONDS |
|
331 | + ); |
|
332 | + $ticket->set_end_time( |
|
333 | + $this->convert_datetime_for_query( |
|
334 | + 'TKT_end_date', |
|
335 | + '11:59 pm', |
|
336 | + 'g:i a', |
|
337 | + $this->_timezone |
|
338 | + ) |
|
339 | + ); |
|
340 | + } |
|
341 | + return $tickets; |
|
342 | + } |
|
343 | + |
|
344 | + |
|
345 | + /** |
|
346 | + * Gets the total number of tickets available at a particular datetime (does |
|
347 | + * NOT take int account the datetime's spaces available) |
|
348 | + * |
|
349 | + * @param int $DTT_ID |
|
350 | + * @param array $query_params |
|
351 | + * @return int |
|
352 | + * @throws EE_Error |
|
353 | + * @throws ReflectionException |
|
354 | + */ |
|
355 | + public function sum_tickets_currently_available_at_datetime(int $DTT_ID, array $query_params = []): int |
|
356 | + { |
|
357 | + $query_params += [['TKT_visibility' => ['>', EEM_Ticket::TICKET_VISIBILITY_NONE_VALUE]]]; |
|
358 | + return EEM_Datetime::instance()->sum_tickets_currently_available_at_datetime($DTT_ID, $query_params); |
|
359 | + } |
|
360 | + |
|
361 | + |
|
362 | + /** |
|
363 | + * Updates the TKT_sold quantity on all the tickets matching $query_params |
|
364 | + * |
|
365 | + * @param EE_Ticket[] $tickets |
|
366 | + * @return void |
|
367 | + * @throws EE_Error |
|
368 | + * @throws ReflectionException |
|
369 | + */ |
|
370 | + public function update_tickets_sold(array $tickets) |
|
371 | + { |
|
372 | + foreach ($tickets as $ticket) { |
|
373 | + $ticket->update_tickets_sold(); |
|
374 | + } |
|
375 | + } |
|
376 | + |
|
377 | + |
|
378 | + /** |
|
379 | + * returns an array of EE_Ticket objects with a non-zero value for TKT_reserved |
|
380 | + * |
|
381 | + * @return EE_Ticket[] |
|
382 | + * @throws EE_Error |
|
383 | + * @throws ReflectionException |
|
384 | + */ |
|
385 | + public function get_tickets_with_reservations(): array |
|
386 | + { |
|
387 | + return $this->get_all( |
|
388 | + [ |
|
389 | + [ |
|
390 | + 'TKT_reserved' => ['>', 0], |
|
391 | + 'TKT_visibility' => ['>', EEM_Ticket::TICKET_VISIBILITY_NONE_VALUE], |
|
392 | + ], |
|
393 | + ] |
|
394 | + ); |
|
395 | + } |
|
396 | + |
|
397 | + |
|
398 | + /** |
|
399 | + * returns an array of EE_Ticket objects matching the supplied list of IDs |
|
400 | + * |
|
401 | + * @param array $ticket_IDs |
|
402 | + * @return EE_Ticket[] |
|
403 | + * @throws EE_Error |
|
404 | + * @throws ReflectionException |
|
405 | + */ |
|
406 | + public function get_tickets_with_IDs(array $ticket_IDs): array |
|
407 | + { |
|
408 | + return $this->get_all( |
|
409 | + [ |
|
410 | + [ |
|
411 | + 'TKT_ID' => ['IN', $ticket_IDs], |
|
412 | + ], |
|
413 | + ] |
|
414 | + ); |
|
415 | + } |
|
416 | + |
|
417 | + |
|
418 | + /** |
|
419 | + * @return void |
|
420 | + */ |
|
421 | + private function parseTicketVisibilityOptions() |
|
422 | + { |
|
423 | + $this->ticket_visibility = (array) apply_filters( |
|
424 | + 'FHEE__EEM_Ticket__construct__ticket_visibility', |
|
425 | + [ |
|
426 | + EEM_Ticket::TICKET_VISIBILITY_PUBLIC_KEY => [ |
|
427 | + 'label' => esc_html__('Public', 'event_espresso'), |
|
428 | + 'value' => EEM_Ticket::TICKET_VISIBILITY_PUBLIC_VALUE, |
|
429 | + ], |
|
430 | + EEM_Ticket::TICKET_VISIBILITY_MEMBERS_ONLY_KEY => [ |
|
431 | + 'label' => esc_html__('Members only', 'event_espresso'), |
|
432 | + 'value' => EEM_Ticket::TICKET_VISIBILITY_MEMBERS_ONLY_VALUE, |
|
433 | + ], |
|
434 | + EEM_Ticket::TICKET_VISIBILITY_ADMINS_ONLY_KEY => [ |
|
435 | + 'label' => esc_html__('Admins only', 'event_espresso'), |
|
436 | + 'value' => EEM_Ticket::TICKET_VISIBILITY_ADMINS_ONLY_VALUE, |
|
437 | + ], |
|
438 | + EEM_Ticket::TICKET_VISIBILITY_ADMIN_UI_ONLY_KEY => [ |
|
439 | + 'label' => esc_html__('Admin UI only', 'event_espresso'), |
|
440 | + 'value' => EEM_Ticket::TICKET_VISIBILITY_ADMIN_UI_ONLY_VALUE, |
|
441 | + ], |
|
442 | + EEM_Ticket::TICKET_VISIBILITY_NONE_KEY => [ |
|
443 | + 'label' => esc_html__('None', 'event_espresso'), |
|
444 | + 'value' => EEM_Ticket::TICKET_VISIBILITY_NONE_VALUE, |
|
445 | + ], |
|
446 | + ] |
|
447 | + ); |
|
448 | + } |
|
449 | + |
|
450 | + |
|
451 | + /** |
|
452 | + * @return array |
|
453 | + */ |
|
454 | + public function getTicketVisibilityEnumOptions(): array |
|
455 | + { |
|
456 | + $ticket_visibility = []; |
|
457 | + foreach ($this->ticket_visibility as $visibility) { |
|
458 | + if (isset($visibility['value'], $visibility['label'])) { |
|
459 | + $ticket_visibility[ $visibility['value'] ] = $visibility['label']; |
|
460 | + } |
|
461 | + } |
|
462 | + return $ticket_visibility; |
|
463 | + } |
|
464 | + |
|
465 | + |
|
466 | + /** |
|
467 | + * @return array |
|
468 | + */ |
|
469 | + public function getTicketVisibilityValues(): array |
|
470 | + { |
|
471 | + // copy ticket_visibility array |
|
472 | + $ticket_visibility_options = $this->ticket_visibility; |
|
473 | + foreach ($ticket_visibility_options as $ticket_visibility_option) { |
|
474 | + // remove labels because we only want the values |
|
475 | + unset($ticket_visibility_option['label']); |
|
476 | + } |
|
477 | + return $ticket_visibility_options; |
|
478 | + } |
|
479 | + |
|
480 | + |
|
481 | + /** |
|
482 | + * @return array |
|
483 | + */ |
|
484 | + public function getTicketVisibilityLabels(): array |
|
485 | + { |
|
486 | + $ticket_visibility_options = []; |
|
487 | + foreach ($this->ticket_visibility as $key => $ticket_visibility_option) { |
|
488 | + if (isset($ticket_visibility_option['label'])) { |
|
489 | + // change because we only want the labels tied to the keys |
|
490 | + $ticket_visibility_options[] = [ |
|
491 | + 'value' => $key, |
|
492 | + 'label' => $ticket_visibility_option['label'] |
|
493 | + ]; |
|
494 | + } |
|
495 | + } |
|
496 | + return $ticket_visibility_options; |
|
497 | + } |
|
498 | + |
|
499 | + |
|
500 | + /** |
|
501 | + * @return array |
|
502 | + */ |
|
503 | + public function ticketVisibilityOptions(): array |
|
504 | + { |
|
505 | + return $this->ticket_visibility; |
|
506 | + } |
|
507 | 507 | } |
@@ -10,643 +10,643 @@ |
||
10 | 10 | class EEM_Message extends EEM_Base implements EEI_Query_Filter |
11 | 11 | { |
12 | 12 | |
13 | - // private instance of the Message object |
|
14 | - protected static $_instance; |
|
13 | + // private instance of the Message object |
|
14 | + protected static $_instance; |
|
15 | 15 | |
16 | 16 | |
17 | - /** |
|
18 | - * This priority indicates a message should be generated and sent ASAP |
|
19 | - * |
|
20 | - * @type int |
|
21 | - */ |
|
22 | - const priority_high = 10; |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * This priority indicates a message should be generated ASAP and queued for sending. |
|
27 | - * |
|
28 | - * @type |
|
29 | - */ |
|
30 | - const priority_medium = 20; |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * This priority indicates a message should be queued for generating. |
|
35 | - * |
|
36 | - * @type int |
|
37 | - */ |
|
38 | - const priority_low = 30; |
|
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 |
|
69 | - * this status when added to the queue. |
|
70 | - */ |
|
71 | - const status_incomplete = 'MIC'; |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * Indicates everything was generated fine for the message, however, the messenger was unable to send. |
|
76 | - * This status means that its possible to retry sending the message. |
|
77 | - */ |
|
78 | - const status_retry = 'MRT'; |
|
79 | - |
|
80 | - |
|
81 | - /** |
|
82 | - * This is used for more informational messages that may not indicate anything is broken but still cannot be |
|
83 | - * generated or sent correctly. An example of a message that would get flagged this way would be when a not |
|
84 | - * approved message was queued for generation, but at time of generation, the attached registration(s) are |
|
85 | - * approved. So the message queued for generation is no longer valid. Messages for this status will only persist |
|
86 | - * in the db and be viewable in the message activity list table when the messages system is in debug mode. |
|
87 | - * |
|
88 | - * @see EEM_Message::debug() |
|
89 | - */ |
|
90 | - const status_debug_only = 'MDO'; |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * This status is given to messages it is processed by the messenger send method. |
|
95 | - * Messages with this status should rarely be seen in the Message List table, but if they are, that's usually |
|
96 | - * indicative of a PHP timeout or memory limit issue. |
|
97 | - */ |
|
98 | - const status_messenger_executing = 'MEX'; |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * Private constructor to prevent direct creation. |
|
103 | - * |
|
104 | - * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and |
|
105 | - * any incoming timezone data that gets saved). Note this just sends the timezone info to |
|
106 | - * the date time model field objects. Default is null (and will be assumed using the set |
|
107 | - * timezone in the 'timezone_string' wp option) |
|
108 | - * @throws EE_Error |
|
109 | - * @throws ReflectionException |
|
110 | - */ |
|
111 | - protected function __construct(string $timezone = '') |
|
112 | - { |
|
113 | - $this->singular_item = esc_html__('Message', 'event_espresso'); |
|
114 | - $this->plural_item = esc_html__('Messages', 'event_espresso'); |
|
115 | - |
|
116 | - // used for token generator |
|
117 | - EE_Registry::instance()->load_helper('URL'); |
|
118 | - |
|
119 | - $this->_tables = array( |
|
120 | - 'Message' => new EE_Primary_Table('esp_message', 'MSG_ID'), |
|
121 | - ); |
|
122 | - |
|
123 | - $allowed_priority = array( |
|
124 | - self::priority_high => esc_html__('high', 'event_espresso'), |
|
125 | - self::priority_medium => esc_html__('medium', 'event_espresso'), |
|
126 | - self::priority_low => esc_html__('low', 'event_espresso'), |
|
127 | - ); |
|
128 | - |
|
129 | - $this->_fields = array( |
|
130 | - 'Message' => array( |
|
131 | - 'MSG_ID' => new EE_Primary_Key_Int_Field('MSG_ID', esc_html__('Message ID', 'event_espresso')), |
|
132 | - 'MSG_token' => new EE_Plain_Text_Field( |
|
133 | - 'MSG_token', |
|
134 | - esc_html__( |
|
135 | - 'Unique Token used to represent this row in publicly viewable contexts (eg. a url).', |
|
136 | - 'event_espresso' |
|
137 | - ), |
|
138 | - false, |
|
139 | - EEH_URL::generate_unique_token() |
|
140 | - ), |
|
141 | - 'GRP_ID' => new EE_Foreign_Key_Int_Field( |
|
142 | - 'GRP_ID', |
|
143 | - esc_html__('Foreign key to the EEM_Message_Template_Group table.', 'event_espresso'), |
|
144 | - true, |
|
145 | - 0, |
|
146 | - 'Message_Template_Group' |
|
147 | - ), |
|
148 | - 'TXN_ID' => new EE_Foreign_Key_Int_Field( |
|
149 | - 'TXN_ID', |
|
150 | - esc_html__( |
|
151 | - 'Foreign key to the related EE_Transaction. This is required to give context for regenerating the specific message', |
|
152 | - 'event_espresso' |
|
153 | - ), |
|
154 | - true, |
|
155 | - 0, |
|
156 | - 'Transaction' |
|
157 | - ), |
|
158 | - 'MSG_messenger' => new EE_Plain_Text_Field( |
|
159 | - 'MSG_messenger', |
|
160 | - esc_html__( |
|
161 | - 'Corresponds to the EE_messenger::name used to send this message. This will also be used to attempt any resending of the message.', |
|
162 | - 'event_espresso' |
|
163 | - ), |
|
164 | - false, |
|
165 | - 'email' |
|
166 | - ), |
|
167 | - 'MSG_message_type' => new EE_Plain_Text_Field( |
|
168 | - 'MSG_message_type', |
|
169 | - esc_html__('Corresponds to the EE_message_type::name used to generate this message.', 'event_espresso'), |
|
170 | - false, |
|
171 | - 'receipt' |
|
172 | - ), |
|
173 | - 'MSG_context' => new EE_Plain_Text_Field('MSG_context', esc_html__('Context', 'event_espresso'), false), |
|
174 | - 'MSG_recipient_ID' => new EE_Foreign_Key_Int_Field( |
|
175 | - 'MSG_recipient_ID', |
|
176 | - esc_html__('Recipient ID', 'event_espresso'), |
|
177 | - true, |
|
178 | - null, |
|
179 | - array('Registration', 'Attendee', 'WP_User') |
|
180 | - ), |
|
181 | - 'MSG_recipient_type' => new EE_Any_Foreign_Model_Name_Field( |
|
182 | - 'MSG_recipient_type', |
|
183 | - esc_html__('Recipient Type', 'event_espresso'), |
|
184 | - true, |
|
185 | - null, |
|
186 | - array('Registration', 'Attendee', 'WP_User') |
|
187 | - ), |
|
188 | - 'MSG_content' => new EE_Maybe_Serialized_Text_Field( |
|
189 | - 'MSG_content', |
|
190 | - esc_html__('Content', 'event_espresso'), |
|
191 | - true, |
|
192 | - '' |
|
193 | - ), |
|
194 | - 'MSG_to' => new EE_Maybe_Serialized_Text_Field( |
|
195 | - 'MSG_to', |
|
196 | - esc_html__('Address To', 'event_espresso'), |
|
197 | - true |
|
198 | - ), |
|
199 | - 'MSG_from' => new EE_Maybe_Serialized_Text_Field( |
|
200 | - 'MSG_from', |
|
201 | - esc_html__('Address From', 'event_espresso'), |
|
202 | - true |
|
203 | - ), |
|
204 | - 'MSG_subject' => new EE_Maybe_Serialized_Text_Field( |
|
205 | - 'MSG_subject', |
|
206 | - esc_html__('Subject', 'event_espresso'), |
|
207 | - true, |
|
208 | - '' |
|
209 | - ), |
|
210 | - 'MSG_priority' => new EE_Enum_Integer_Field( |
|
211 | - 'MSG_priority', |
|
212 | - esc_html__('Priority', 'event_espresso'), |
|
213 | - false, |
|
214 | - self::priority_low, |
|
215 | - $allowed_priority |
|
216 | - ), |
|
217 | - 'STS_ID' => new EE_Foreign_Key_String_Field( |
|
218 | - 'STS_ID', |
|
219 | - esc_html__('Status', 'event_espresso'), |
|
220 | - false, |
|
221 | - self::status_incomplete, |
|
222 | - 'Status' |
|
223 | - ), |
|
224 | - 'MSG_created' => new EE_Datetime_Field( |
|
225 | - 'MSG_created', |
|
226 | - esc_html__('Created', 'event_espresso'), |
|
227 | - false, |
|
228 | - EE_Datetime_Field::now |
|
229 | - ), |
|
230 | - 'MSG_modified' => new EE_Datetime_Field( |
|
231 | - 'MSG_modified', |
|
232 | - esc_html__('Modified', 'event_espresso'), |
|
233 | - true, |
|
234 | - EE_Datetime_Field::now |
|
235 | - ), |
|
236 | - ), |
|
237 | - ); |
|
238 | - $this->_model_relations = array( |
|
239 | - 'Attendee' => new EE_Belongs_To_Any_Relation(), |
|
240 | - 'Registration' => new EE_Belongs_To_Any_Relation(), |
|
241 | - 'WP_User' => new EE_Belongs_To_Any_Relation(), |
|
242 | - 'Message_Template_Group' => new EE_Belongs_To_Relation(), |
|
243 | - 'Transaction' => new EE_Belongs_To_Relation(), |
|
244 | - ); |
|
245 | - parent::__construct($timezone); |
|
246 | - } |
|
247 | - |
|
248 | - |
|
249 | - /** |
|
250 | - * @return EE_Message |
|
251 | - * @throws EE_Error |
|
252 | - * @throws ReflectionException |
|
253 | - */ |
|
254 | - public function create_default_object(): ?EE_Message |
|
255 | - { |
|
256 | - /** @type EE_Message $message */ |
|
257 | - $message = parent::create_default_object(); |
|
258 | - if ($message instanceof EE_Message) { |
|
259 | - return EE_Message_Factory::set_messenger_and_message_type($message); |
|
260 | - } |
|
261 | - return null; |
|
262 | - } |
|
263 | - |
|
264 | - |
|
265 | - /** |
|
266 | - * @param mixed $cols_n_values |
|
267 | - * @return EE_Message |
|
268 | - * @throws EE_Error |
|
269 | - * @throws ReflectionException |
|
270 | - */ |
|
271 | - public function instantiate_class_from_array_or_object($cols_n_values): ?EE_Message |
|
272 | - { |
|
273 | - /** @type EE_Message $message */ |
|
274 | - $message = parent::instantiate_class_from_array_or_object($cols_n_values); |
|
275 | - if ($message instanceof EE_Message) { |
|
276 | - return EE_Message_Factory::set_messenger_and_message_type($message); |
|
277 | - } |
|
278 | - return null; |
|
279 | - } |
|
280 | - |
|
281 | - |
|
282 | - /** |
|
283 | - * Returns whether or not a message of that type was sent for a given attendee. |
|
284 | - * |
|
285 | - * @param EE_Attendee|int $attendee |
|
286 | - * @param string $message_type the message type slug |
|
287 | - * @return boolean |
|
288 | - * @throws EE_Error |
|
289 | - * @throws ReflectionException |
|
290 | - */ |
|
291 | - public function message_sent_for_attendee($attendee, string $message_type): bool |
|
292 | - { |
|
293 | - $attendee_ID = EEM_Attendee::instance()->ensure_is_ID($attendee); |
|
294 | - return $this->exists(array( |
|
295 | - array( |
|
296 | - 'Attendee.ATT_ID' => $attendee_ID, |
|
297 | - 'MSG_message_type' => $message_type, |
|
298 | - 'STS_ID' => array('IN', $this->stati_indicating_sent()), |
|
299 | - ), |
|
300 | - )); |
|
301 | - } |
|
302 | - |
|
303 | - |
|
304 | - /** |
|
305 | - * Returns whether or not a message of that type was sent for a given registration |
|
306 | - * |
|
307 | - * @param EE_Registration|int $registration |
|
308 | - * @param string $message_type the message type slug |
|
309 | - * @return boolean |
|
310 | - * @throws EE_Error |
|
311 | - * @throws ReflectionException |
|
312 | - */ |
|
313 | - public function message_sent_for_registration($registration, string $message_type): bool |
|
314 | - { |
|
315 | - $registrationID = EEM_Registration::instance()->ensure_is_ID($registration); |
|
316 | - return $this->exists(array( |
|
317 | - array( |
|
318 | - 'Registration.REG_ID' => $registrationID, |
|
319 | - 'MSG_message_type' => $message_type, |
|
320 | - 'STS_ID' => array('IN', $this->stati_indicating_sent()), |
|
321 | - ), |
|
322 | - )); |
|
323 | - } |
|
324 | - |
|
325 | - |
|
326 | - /** |
|
327 | - * This retrieves an EE_Message object from the db matching the given token string. |
|
328 | - * |
|
329 | - * @param string $token |
|
330 | - * @return EE_Message |
|
331 | - * @throws EE_Error |
|
332 | - * @throws ReflectionException |
|
333 | - */ |
|
334 | - public function get_one_by_token(string $token): EE_Message |
|
335 | - { |
|
336 | - return $this->get_one(array( |
|
337 | - array( |
|
338 | - 'MSG_token' => $token, |
|
339 | - ), |
|
340 | - )); |
|
341 | - } |
|
342 | - |
|
343 | - |
|
344 | - /** |
|
345 | - * Returns stati that indicate the message HAS been sent |
|
346 | - * |
|
347 | - * @return array of strings for possible stati |
|
348 | - */ |
|
349 | - public function stati_indicating_sent() |
|
350 | - { |
|
351 | - return apply_filters('FHEE__EEM_Message__stati_indicating_sent', array(self::status_sent)); |
|
352 | - } |
|
353 | - |
|
354 | - |
|
355 | - /** |
|
356 | - * Returns stati that indicate the message is waiting to be sent. |
|
357 | - * |
|
358 | - * @return array of strings for possible stati. |
|
359 | - */ |
|
360 | - public function stati_indicating_to_send() |
|
361 | - { |
|
362 | - return apply_filters( |
|
363 | - 'FHEE__EEM_Message__stati_indicating_to_send', |
|
364 | - array(self::status_idle, self::status_resend) |
|
365 | - ); |
|
366 | - } |
|
367 | - |
|
368 | - |
|
369 | - /** |
|
370 | - * Returns stati that indicate the message has failed sending |
|
371 | - * |
|
372 | - * @return array array of strings for possible stati. |
|
373 | - */ |
|
374 | - public function stati_indicating_failed_sending() |
|
375 | - { |
|
376 | - $failed_stati = array( |
|
377 | - self::status_failed, |
|
378 | - self::status_retry, |
|
379 | - self::status_messenger_executing, |
|
380 | - ); |
|
381 | - // if WP_DEBUG is set, then let's include debug_only fails |
|
382 | - if (WP_DEBUG) { |
|
383 | - $failed_stati[] = self::status_debug_only; |
|
384 | - } |
|
385 | - return apply_filters('FHEE__EEM_Message__stati_indicating_failed_sending', $failed_stati); |
|
386 | - } |
|
387 | - |
|
388 | - |
|
389 | - /** |
|
390 | - * Returns filterable array of all EEM_Message statuses. |
|
391 | - * |
|
392 | - * @return array |
|
393 | - */ |
|
394 | - public function all_statuses() |
|
395 | - { |
|
396 | - return apply_filters( |
|
397 | - 'FHEE__EEM_Message__all_statuses', |
|
398 | - array( |
|
399 | - EEM_Message::status_sent, |
|
400 | - EEM_Message::status_incomplete, |
|
401 | - EEM_Message::status_idle, |
|
402 | - EEM_Message::status_resend, |
|
403 | - EEM_Message::status_retry, |
|
404 | - EEM_Message::status_failed, |
|
405 | - EEM_Message::status_messenger_executing, |
|
406 | - EEM_Message::status_debug_only, |
|
407 | - ) |
|
408 | - ); |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Detects any specific query variables in the request and uses those to setup appropriate |
|
413 | - * filter for any queries. |
|
414 | - * |
|
415 | - * @return array |
|
416 | - */ |
|
417 | - public function filter_by_query_params() |
|
418 | - { |
|
419 | - // expected possible query_vars, the key in this array matches an expected key in the request, |
|
420 | - // the value, matches the corresponding EEM_Base child reference. |
|
421 | - $expected_vars = $this->_expected_vars_for_query_inject(); |
|
422 | - $query_params[0] = array(); |
|
423 | - foreach ($expected_vars as $request_key => $model_name) { |
|
424 | - $request_value = EE_Registry::instance()->REQ->get($request_key); |
|
425 | - if ($request_value) { |
|
426 | - // special case |
|
427 | - switch ($request_key) { |
|
428 | - case '_REG_ID': |
|
429 | - $query_params[0]['AND**filter_by']['OR**filter_by_REG_ID'] = array( |
|
430 | - 'Transaction.Registration.REG_ID' => $request_value, |
|
431 | - ); |
|
432 | - break; |
|
433 | - case 'EVT_ID': |
|
434 | - $query_params[0]['AND**filter_by']['OR**filter_by_EVT_ID'] = array( |
|
435 | - 'Transaction.Registration.EVT_ID' => $request_value, |
|
436 | - ); |
|
437 | - break; |
|
438 | - default: |
|
439 | - $query_params[0]['AND**filter_by'][ 'OR**filter_by_' . $request_key ][ $model_name . '.' . $request_key ] = $request_value; |
|
440 | - break; |
|
441 | - } |
|
442 | - } |
|
443 | - } |
|
444 | - return $query_params; |
|
445 | - } |
|
446 | - |
|
447 | - |
|
448 | - /** |
|
449 | - * @return string |
|
450 | - * @throws EE_Error |
|
451 | - * @throws ReflectionException |
|
452 | - */ |
|
453 | - public function get_pretty_label_for_results() |
|
454 | - { |
|
455 | - $expected_vars = $this->_expected_vars_for_query_inject(); |
|
456 | - $pretty_label = ''; |
|
457 | - $label_parts = array(); |
|
458 | - foreach ($expected_vars as $request_key => $model_name) { |
|
459 | - $model = EE_Registry::instance()->load_model($model_name); |
|
460 | - if ($model_field_value = EE_Registry::instance()->REQ->get($request_key)) { |
|
461 | - switch ($request_key) { |
|
462 | - case '_REG_ID': |
|
463 | - $label_parts[] = sprintf( |
|
464 | - esc_html__('Registration with the ID: %s', 'event_espresso'), |
|
465 | - $model_field_value |
|
466 | - ); |
|
467 | - break; |
|
468 | - case 'ATT_ID': |
|
469 | - /** @var EE_Attendee $attendee */ |
|
470 | - $attendee = $model->get_one_by_ID($model_field_value); |
|
471 | - $label_parts[] = $attendee instanceof EE_Attendee |
|
472 | - ? sprintf(esc_html__('Attendee %s', 'event_espresso'), $attendee->full_name()) |
|
473 | - : sprintf(esc_html__('Attendee ID: %s', 'event_espresso'), $model_field_value); |
|
474 | - break; |
|
475 | - case 'ID': |
|
476 | - /** @var EE_WP_User $wpUser */ |
|
477 | - $wpUser = $model->get_one_by_ID($model_field_value); |
|
478 | - $label_parts[] = $wpUser instanceof EE_WP_User |
|
479 | - ? sprintf(esc_html__('WP User: %s', 'event_espresso'), $wpUser->name()) |
|
480 | - : sprintf(esc_html__('WP User ID: %s', 'event_espresso'), $model_field_value); |
|
481 | - break; |
|
482 | - case 'TXN_ID': |
|
483 | - $label_parts[] = sprintf( |
|
484 | - esc_html__('Transaction with the ID: %s', 'event_espresso'), |
|
485 | - $model_field_value |
|
486 | - ); |
|
487 | - break; |
|
488 | - case 'EVT_ID': |
|
489 | - /** @var EE_Event $Event */ |
|
490 | - $Event = $model->get_one_by_ID($model_field_value); |
|
491 | - $label_parts[] = $Event instanceof EE_Event |
|
492 | - ? sprintf(esc_html__('for the Event: %s', 'event_espresso'), $Event->name()) |
|
493 | - : sprintf(esc_html__('for the Event with ID: %s', 'event_espresso'), $model_field_value); |
|
494 | - break; |
|
495 | - } |
|
496 | - } |
|
497 | - } |
|
498 | - |
|
499 | - if ($label_parts) { |
|
500 | - // prepend to the last element of $label_parts an "and". |
|
501 | - if (count($label_parts) > 1) { |
|
502 | - $label_parts_index_to_prepend = count($label_parts) - 1; |
|
503 | - $label_parts[ $label_parts_index_to_prepend ] = 'and' . $label_parts[ $label_parts_index_to_prepend ]; |
|
504 | - } |
|
505 | - |
|
506 | - $pretty_label .= sprintf( |
|
507 | - esc_html_x( |
|
508 | - 'Showing messages for %s', |
|
509 | - 'A label for the messages returned in a query that are filtered by items in the query. This could be Transaction, Event, Attendee, Registration, or WP_User.', |
|
510 | - 'event_espresso' |
|
511 | - ), |
|
512 | - implode(', ', $label_parts) |
|
513 | - ); |
|
514 | - } |
|
515 | - return $pretty_label; |
|
516 | - } |
|
517 | - |
|
518 | - |
|
519 | - /** |
|
520 | - * This returns the array of expected variables for the EEI_Query_Filter methods being implemented |
|
521 | - * The array is in the format: |
|
522 | - * array( |
|
523 | - * {$field_name} => {$model_name} |
|
524 | - * ); |
|
525 | - * |
|
526 | - * @since 4.9.0 |
|
527 | - * @return array |
|
528 | - */ |
|
529 | - protected function _expected_vars_for_query_inject() |
|
530 | - { |
|
531 | - return array( |
|
532 | - '_REG_ID' => 'Registration', |
|
533 | - 'ATT_ID' => 'Attendee', |
|
534 | - 'ID' => 'WP_User', |
|
535 | - 'TXN_ID' => 'Transaction', |
|
536 | - 'EVT_ID' => 'Event', |
|
537 | - ); |
|
538 | - } |
|
539 | - |
|
540 | - |
|
541 | - /** |
|
542 | - * This returns whether EEM_Message is in debug mode or not. |
|
543 | - * Currently "debug mode" is used to control the handling of the EEM_Message::debug_only status when |
|
544 | - * generating/sending messages. Debug mode can be set by either: |
|
545 | - * 1. Sending in a value for the $set_debug argument |
|
546 | - * 2. Defining `EE_DEBUG_MESSAGES` constant in wp-config.php |
|
547 | - * 3. Overriding the above via the provided filter. |
|
548 | - * |
|
549 | - * @param bool|null $set_debug If provided, then the debug mode will be set internally until reset via the |
|
550 | - * provided boolean. When no argument is provided (default null) then the debug |
|
551 | - * mode will be returned. |
|
552 | - * @return bool true means Messages is in debug mode. false means messages system is not in debug mode. |
|
553 | - */ |
|
554 | - public static function debug($set_debug = null) |
|
555 | - { |
|
556 | - static $is_debugging = null; |
|
557 | - |
|
558 | - // initialize (use constant if set). |
|
559 | - if (is_null($set_debug) && is_null($is_debugging)) { |
|
560 | - $is_debugging = defined('EE_DEBUG_MESSAGES') && EE_DEBUG_MESSAGES; |
|
561 | - } |
|
562 | - |
|
563 | - if (! is_null($set_debug)) { |
|
564 | - $is_debugging = filter_var($set_debug, FILTER_VALIDATE_BOOLEAN); |
|
565 | - } |
|
566 | - |
|
567 | - // return filtered value |
|
568 | - return apply_filters('FHEE__EEM_Message__debug', $is_debugging); |
|
569 | - } |
|
570 | - |
|
571 | - |
|
572 | - /** |
|
573 | - * Deletes old messages meeting certain criteria for removal from the database. |
|
574 | - * By default, this will delete messages that: |
|
575 | - * - are older than the value of the delete_threshold in months. |
|
576 | - * - have a STS_ID other than EEM_Message::status_idle |
|
577 | - * |
|
578 | - * @param int $delete_threshold This integer will be used to set the boundary for what messages are deleted in months. |
|
579 | - * @return bool|false|int Either the number of records affected or false if there was an error (you can call |
|
580 | - * $wpdb->last_error to find out what the error was. |
|
581 | - * @throws EE_Error |
|
582 | - * @throws ReflectionException |
|
583 | - */ |
|
584 | - public function delete_old_messages($delete_threshold = 6) |
|
585 | - { |
|
586 | - $number_deleted = 0; |
|
587 | - /** |
|
588 | - * Allows code to change the boundary for what messages are kept. |
|
589 | - * Uses the value of the `delete_threshold` variable by default. |
|
590 | - * |
|
591 | - * @param int $seconds seconds that will be subtracted from the timestamp for now. |
|
592 | - * @return int |
|
593 | - */ |
|
594 | - $time_to_leave_alone = absint( |
|
595 | - apply_filters( |
|
596 | - 'FHEE__EEM_Message__delete_old_messages__time_to_leave_alone', |
|
597 | - ((int) $delete_threshold) * MONTH_IN_SECONDS |
|
598 | - ) |
|
599 | - ); |
|
600 | - |
|
601 | - |
|
602 | - /** |
|
603 | - * Allows code to change what message stati are ignored when deleting. |
|
604 | - * Defaults to only ignore EEM_Message::status_idle messages. |
|
605 | - * |
|
606 | - * @param string $message_stati_to_keep An array of message statuses that will be ignored when deleting. |
|
607 | - */ |
|
608 | - $message_stati_to_keep = (array) apply_filters( |
|
609 | - 'FHEE__EEM_Message__delete_old_messages__message_stati_to_keep', |
|
610 | - array( |
|
611 | - EEM_Message::status_idle |
|
612 | - ) |
|
613 | - ); |
|
614 | - |
|
615 | - // first get all the ids of messages being deleted |
|
616 | - $message_ids_to_delete = EEM_Message::instance()->get_col( |
|
617 | - array( |
|
618 | - 0 => array( |
|
619 | - 'STS_ID' => array('NOT_IN', $message_stati_to_keep), |
|
620 | - 'MSG_modified' => array('<', time() - $time_to_leave_alone) |
|
621 | - ), |
|
622 | - 'limit' => apply_filters( |
|
623 | - 'EEM_Message__delete_old_messages__limit', |
|
624 | - 2000, |
|
625 | - $delete_threshold |
|
626 | - ) |
|
627 | - ) |
|
628 | - ); |
|
629 | - |
|
630 | - if (! empty($message_ids_to_delete) && is_array($message_ids_to_delete)) { |
|
631 | - global $wpdb; |
|
632 | - $number_deleted = $wpdb->query(' |
|
17 | + /** |
|
18 | + * This priority indicates a message should be generated and sent ASAP |
|
19 | + * |
|
20 | + * @type int |
|
21 | + */ |
|
22 | + const priority_high = 10; |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * This priority indicates a message should be generated ASAP and queued for sending. |
|
27 | + * |
|
28 | + * @type |
|
29 | + */ |
|
30 | + const priority_medium = 20; |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * This priority indicates a message should be queued for generating. |
|
35 | + * |
|
36 | + * @type int |
|
37 | + */ |
|
38 | + const priority_low = 30; |
|
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 |
|
69 | + * this status when added to the queue. |
|
70 | + */ |
|
71 | + const status_incomplete = 'MIC'; |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * Indicates everything was generated fine for the message, however, the messenger was unable to send. |
|
76 | + * This status means that its possible to retry sending the message. |
|
77 | + */ |
|
78 | + const status_retry = 'MRT'; |
|
79 | + |
|
80 | + |
|
81 | + /** |
|
82 | + * This is used for more informational messages that may not indicate anything is broken but still cannot be |
|
83 | + * generated or sent correctly. An example of a message that would get flagged this way would be when a not |
|
84 | + * approved message was queued for generation, but at time of generation, the attached registration(s) are |
|
85 | + * approved. So the message queued for generation is no longer valid. Messages for this status will only persist |
|
86 | + * in the db and be viewable in the message activity list table when the messages system is in debug mode. |
|
87 | + * |
|
88 | + * @see EEM_Message::debug() |
|
89 | + */ |
|
90 | + const status_debug_only = 'MDO'; |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * This status is given to messages it is processed by the messenger send method. |
|
95 | + * Messages with this status should rarely be seen in the Message List table, but if they are, that's usually |
|
96 | + * indicative of a PHP timeout or memory limit issue. |
|
97 | + */ |
|
98 | + const status_messenger_executing = 'MEX'; |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * Private constructor to prevent direct creation. |
|
103 | + * |
|
104 | + * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and |
|
105 | + * any incoming timezone data that gets saved). Note this just sends the timezone info to |
|
106 | + * the date time model field objects. Default is null (and will be assumed using the set |
|
107 | + * timezone in the 'timezone_string' wp option) |
|
108 | + * @throws EE_Error |
|
109 | + * @throws ReflectionException |
|
110 | + */ |
|
111 | + protected function __construct(string $timezone = '') |
|
112 | + { |
|
113 | + $this->singular_item = esc_html__('Message', 'event_espresso'); |
|
114 | + $this->plural_item = esc_html__('Messages', 'event_espresso'); |
|
115 | + |
|
116 | + // used for token generator |
|
117 | + EE_Registry::instance()->load_helper('URL'); |
|
118 | + |
|
119 | + $this->_tables = array( |
|
120 | + 'Message' => new EE_Primary_Table('esp_message', 'MSG_ID'), |
|
121 | + ); |
|
122 | + |
|
123 | + $allowed_priority = array( |
|
124 | + self::priority_high => esc_html__('high', 'event_espresso'), |
|
125 | + self::priority_medium => esc_html__('medium', 'event_espresso'), |
|
126 | + self::priority_low => esc_html__('low', 'event_espresso'), |
|
127 | + ); |
|
128 | + |
|
129 | + $this->_fields = array( |
|
130 | + 'Message' => array( |
|
131 | + 'MSG_ID' => new EE_Primary_Key_Int_Field('MSG_ID', esc_html__('Message ID', 'event_espresso')), |
|
132 | + 'MSG_token' => new EE_Plain_Text_Field( |
|
133 | + 'MSG_token', |
|
134 | + esc_html__( |
|
135 | + 'Unique Token used to represent this row in publicly viewable contexts (eg. a url).', |
|
136 | + 'event_espresso' |
|
137 | + ), |
|
138 | + false, |
|
139 | + EEH_URL::generate_unique_token() |
|
140 | + ), |
|
141 | + 'GRP_ID' => new EE_Foreign_Key_Int_Field( |
|
142 | + 'GRP_ID', |
|
143 | + esc_html__('Foreign key to the EEM_Message_Template_Group table.', 'event_espresso'), |
|
144 | + true, |
|
145 | + 0, |
|
146 | + 'Message_Template_Group' |
|
147 | + ), |
|
148 | + 'TXN_ID' => new EE_Foreign_Key_Int_Field( |
|
149 | + 'TXN_ID', |
|
150 | + esc_html__( |
|
151 | + 'Foreign key to the related EE_Transaction. This is required to give context for regenerating the specific message', |
|
152 | + 'event_espresso' |
|
153 | + ), |
|
154 | + true, |
|
155 | + 0, |
|
156 | + 'Transaction' |
|
157 | + ), |
|
158 | + 'MSG_messenger' => new EE_Plain_Text_Field( |
|
159 | + 'MSG_messenger', |
|
160 | + esc_html__( |
|
161 | + 'Corresponds to the EE_messenger::name used to send this message. This will also be used to attempt any resending of the message.', |
|
162 | + 'event_espresso' |
|
163 | + ), |
|
164 | + false, |
|
165 | + 'email' |
|
166 | + ), |
|
167 | + 'MSG_message_type' => new EE_Plain_Text_Field( |
|
168 | + 'MSG_message_type', |
|
169 | + esc_html__('Corresponds to the EE_message_type::name used to generate this message.', 'event_espresso'), |
|
170 | + false, |
|
171 | + 'receipt' |
|
172 | + ), |
|
173 | + 'MSG_context' => new EE_Plain_Text_Field('MSG_context', esc_html__('Context', 'event_espresso'), false), |
|
174 | + 'MSG_recipient_ID' => new EE_Foreign_Key_Int_Field( |
|
175 | + 'MSG_recipient_ID', |
|
176 | + esc_html__('Recipient ID', 'event_espresso'), |
|
177 | + true, |
|
178 | + null, |
|
179 | + array('Registration', 'Attendee', 'WP_User') |
|
180 | + ), |
|
181 | + 'MSG_recipient_type' => new EE_Any_Foreign_Model_Name_Field( |
|
182 | + 'MSG_recipient_type', |
|
183 | + esc_html__('Recipient Type', 'event_espresso'), |
|
184 | + true, |
|
185 | + null, |
|
186 | + array('Registration', 'Attendee', 'WP_User') |
|
187 | + ), |
|
188 | + 'MSG_content' => new EE_Maybe_Serialized_Text_Field( |
|
189 | + 'MSG_content', |
|
190 | + esc_html__('Content', 'event_espresso'), |
|
191 | + true, |
|
192 | + '' |
|
193 | + ), |
|
194 | + 'MSG_to' => new EE_Maybe_Serialized_Text_Field( |
|
195 | + 'MSG_to', |
|
196 | + esc_html__('Address To', 'event_espresso'), |
|
197 | + true |
|
198 | + ), |
|
199 | + 'MSG_from' => new EE_Maybe_Serialized_Text_Field( |
|
200 | + 'MSG_from', |
|
201 | + esc_html__('Address From', 'event_espresso'), |
|
202 | + true |
|
203 | + ), |
|
204 | + 'MSG_subject' => new EE_Maybe_Serialized_Text_Field( |
|
205 | + 'MSG_subject', |
|
206 | + esc_html__('Subject', 'event_espresso'), |
|
207 | + true, |
|
208 | + '' |
|
209 | + ), |
|
210 | + 'MSG_priority' => new EE_Enum_Integer_Field( |
|
211 | + 'MSG_priority', |
|
212 | + esc_html__('Priority', 'event_espresso'), |
|
213 | + false, |
|
214 | + self::priority_low, |
|
215 | + $allowed_priority |
|
216 | + ), |
|
217 | + 'STS_ID' => new EE_Foreign_Key_String_Field( |
|
218 | + 'STS_ID', |
|
219 | + esc_html__('Status', 'event_espresso'), |
|
220 | + false, |
|
221 | + self::status_incomplete, |
|
222 | + 'Status' |
|
223 | + ), |
|
224 | + 'MSG_created' => new EE_Datetime_Field( |
|
225 | + 'MSG_created', |
|
226 | + esc_html__('Created', 'event_espresso'), |
|
227 | + false, |
|
228 | + EE_Datetime_Field::now |
|
229 | + ), |
|
230 | + 'MSG_modified' => new EE_Datetime_Field( |
|
231 | + 'MSG_modified', |
|
232 | + esc_html__('Modified', 'event_espresso'), |
|
233 | + true, |
|
234 | + EE_Datetime_Field::now |
|
235 | + ), |
|
236 | + ), |
|
237 | + ); |
|
238 | + $this->_model_relations = array( |
|
239 | + 'Attendee' => new EE_Belongs_To_Any_Relation(), |
|
240 | + 'Registration' => new EE_Belongs_To_Any_Relation(), |
|
241 | + 'WP_User' => new EE_Belongs_To_Any_Relation(), |
|
242 | + 'Message_Template_Group' => new EE_Belongs_To_Relation(), |
|
243 | + 'Transaction' => new EE_Belongs_To_Relation(), |
|
244 | + ); |
|
245 | + parent::__construct($timezone); |
|
246 | + } |
|
247 | + |
|
248 | + |
|
249 | + /** |
|
250 | + * @return EE_Message |
|
251 | + * @throws EE_Error |
|
252 | + * @throws ReflectionException |
|
253 | + */ |
|
254 | + public function create_default_object(): ?EE_Message |
|
255 | + { |
|
256 | + /** @type EE_Message $message */ |
|
257 | + $message = parent::create_default_object(); |
|
258 | + if ($message instanceof EE_Message) { |
|
259 | + return EE_Message_Factory::set_messenger_and_message_type($message); |
|
260 | + } |
|
261 | + return null; |
|
262 | + } |
|
263 | + |
|
264 | + |
|
265 | + /** |
|
266 | + * @param mixed $cols_n_values |
|
267 | + * @return EE_Message |
|
268 | + * @throws EE_Error |
|
269 | + * @throws ReflectionException |
|
270 | + */ |
|
271 | + public function instantiate_class_from_array_or_object($cols_n_values): ?EE_Message |
|
272 | + { |
|
273 | + /** @type EE_Message $message */ |
|
274 | + $message = parent::instantiate_class_from_array_or_object($cols_n_values); |
|
275 | + if ($message instanceof EE_Message) { |
|
276 | + return EE_Message_Factory::set_messenger_and_message_type($message); |
|
277 | + } |
|
278 | + return null; |
|
279 | + } |
|
280 | + |
|
281 | + |
|
282 | + /** |
|
283 | + * Returns whether or not a message of that type was sent for a given attendee. |
|
284 | + * |
|
285 | + * @param EE_Attendee|int $attendee |
|
286 | + * @param string $message_type the message type slug |
|
287 | + * @return boolean |
|
288 | + * @throws EE_Error |
|
289 | + * @throws ReflectionException |
|
290 | + */ |
|
291 | + public function message_sent_for_attendee($attendee, string $message_type): bool |
|
292 | + { |
|
293 | + $attendee_ID = EEM_Attendee::instance()->ensure_is_ID($attendee); |
|
294 | + return $this->exists(array( |
|
295 | + array( |
|
296 | + 'Attendee.ATT_ID' => $attendee_ID, |
|
297 | + 'MSG_message_type' => $message_type, |
|
298 | + 'STS_ID' => array('IN', $this->stati_indicating_sent()), |
|
299 | + ), |
|
300 | + )); |
|
301 | + } |
|
302 | + |
|
303 | + |
|
304 | + /** |
|
305 | + * Returns whether or not a message of that type was sent for a given registration |
|
306 | + * |
|
307 | + * @param EE_Registration|int $registration |
|
308 | + * @param string $message_type the message type slug |
|
309 | + * @return boolean |
|
310 | + * @throws EE_Error |
|
311 | + * @throws ReflectionException |
|
312 | + */ |
|
313 | + public function message_sent_for_registration($registration, string $message_type): bool |
|
314 | + { |
|
315 | + $registrationID = EEM_Registration::instance()->ensure_is_ID($registration); |
|
316 | + return $this->exists(array( |
|
317 | + array( |
|
318 | + 'Registration.REG_ID' => $registrationID, |
|
319 | + 'MSG_message_type' => $message_type, |
|
320 | + 'STS_ID' => array('IN', $this->stati_indicating_sent()), |
|
321 | + ), |
|
322 | + )); |
|
323 | + } |
|
324 | + |
|
325 | + |
|
326 | + /** |
|
327 | + * This retrieves an EE_Message object from the db matching the given token string. |
|
328 | + * |
|
329 | + * @param string $token |
|
330 | + * @return EE_Message |
|
331 | + * @throws EE_Error |
|
332 | + * @throws ReflectionException |
|
333 | + */ |
|
334 | + public function get_one_by_token(string $token): EE_Message |
|
335 | + { |
|
336 | + return $this->get_one(array( |
|
337 | + array( |
|
338 | + 'MSG_token' => $token, |
|
339 | + ), |
|
340 | + )); |
|
341 | + } |
|
342 | + |
|
343 | + |
|
344 | + /** |
|
345 | + * Returns stati that indicate the message HAS been sent |
|
346 | + * |
|
347 | + * @return array of strings for possible stati |
|
348 | + */ |
|
349 | + public function stati_indicating_sent() |
|
350 | + { |
|
351 | + return apply_filters('FHEE__EEM_Message__stati_indicating_sent', array(self::status_sent)); |
|
352 | + } |
|
353 | + |
|
354 | + |
|
355 | + /** |
|
356 | + * Returns stati that indicate the message is waiting to be sent. |
|
357 | + * |
|
358 | + * @return array of strings for possible stati. |
|
359 | + */ |
|
360 | + public function stati_indicating_to_send() |
|
361 | + { |
|
362 | + return apply_filters( |
|
363 | + 'FHEE__EEM_Message__stati_indicating_to_send', |
|
364 | + array(self::status_idle, self::status_resend) |
|
365 | + ); |
|
366 | + } |
|
367 | + |
|
368 | + |
|
369 | + /** |
|
370 | + * Returns stati that indicate the message has failed sending |
|
371 | + * |
|
372 | + * @return array array of strings for possible stati. |
|
373 | + */ |
|
374 | + public function stati_indicating_failed_sending() |
|
375 | + { |
|
376 | + $failed_stati = array( |
|
377 | + self::status_failed, |
|
378 | + self::status_retry, |
|
379 | + self::status_messenger_executing, |
|
380 | + ); |
|
381 | + // if WP_DEBUG is set, then let's include debug_only fails |
|
382 | + if (WP_DEBUG) { |
|
383 | + $failed_stati[] = self::status_debug_only; |
|
384 | + } |
|
385 | + return apply_filters('FHEE__EEM_Message__stati_indicating_failed_sending', $failed_stati); |
|
386 | + } |
|
387 | + |
|
388 | + |
|
389 | + /** |
|
390 | + * Returns filterable array of all EEM_Message statuses. |
|
391 | + * |
|
392 | + * @return array |
|
393 | + */ |
|
394 | + public function all_statuses() |
|
395 | + { |
|
396 | + return apply_filters( |
|
397 | + 'FHEE__EEM_Message__all_statuses', |
|
398 | + array( |
|
399 | + EEM_Message::status_sent, |
|
400 | + EEM_Message::status_incomplete, |
|
401 | + EEM_Message::status_idle, |
|
402 | + EEM_Message::status_resend, |
|
403 | + EEM_Message::status_retry, |
|
404 | + EEM_Message::status_failed, |
|
405 | + EEM_Message::status_messenger_executing, |
|
406 | + EEM_Message::status_debug_only, |
|
407 | + ) |
|
408 | + ); |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Detects any specific query variables in the request and uses those to setup appropriate |
|
413 | + * filter for any queries. |
|
414 | + * |
|
415 | + * @return array |
|
416 | + */ |
|
417 | + public function filter_by_query_params() |
|
418 | + { |
|
419 | + // expected possible query_vars, the key in this array matches an expected key in the request, |
|
420 | + // the value, matches the corresponding EEM_Base child reference. |
|
421 | + $expected_vars = $this->_expected_vars_for_query_inject(); |
|
422 | + $query_params[0] = array(); |
|
423 | + foreach ($expected_vars as $request_key => $model_name) { |
|
424 | + $request_value = EE_Registry::instance()->REQ->get($request_key); |
|
425 | + if ($request_value) { |
|
426 | + // special case |
|
427 | + switch ($request_key) { |
|
428 | + case '_REG_ID': |
|
429 | + $query_params[0]['AND**filter_by']['OR**filter_by_REG_ID'] = array( |
|
430 | + 'Transaction.Registration.REG_ID' => $request_value, |
|
431 | + ); |
|
432 | + break; |
|
433 | + case 'EVT_ID': |
|
434 | + $query_params[0]['AND**filter_by']['OR**filter_by_EVT_ID'] = array( |
|
435 | + 'Transaction.Registration.EVT_ID' => $request_value, |
|
436 | + ); |
|
437 | + break; |
|
438 | + default: |
|
439 | + $query_params[0]['AND**filter_by'][ 'OR**filter_by_' . $request_key ][ $model_name . '.' . $request_key ] = $request_value; |
|
440 | + break; |
|
441 | + } |
|
442 | + } |
|
443 | + } |
|
444 | + return $query_params; |
|
445 | + } |
|
446 | + |
|
447 | + |
|
448 | + /** |
|
449 | + * @return string |
|
450 | + * @throws EE_Error |
|
451 | + * @throws ReflectionException |
|
452 | + */ |
|
453 | + public function get_pretty_label_for_results() |
|
454 | + { |
|
455 | + $expected_vars = $this->_expected_vars_for_query_inject(); |
|
456 | + $pretty_label = ''; |
|
457 | + $label_parts = array(); |
|
458 | + foreach ($expected_vars as $request_key => $model_name) { |
|
459 | + $model = EE_Registry::instance()->load_model($model_name); |
|
460 | + if ($model_field_value = EE_Registry::instance()->REQ->get($request_key)) { |
|
461 | + switch ($request_key) { |
|
462 | + case '_REG_ID': |
|
463 | + $label_parts[] = sprintf( |
|
464 | + esc_html__('Registration with the ID: %s', 'event_espresso'), |
|
465 | + $model_field_value |
|
466 | + ); |
|
467 | + break; |
|
468 | + case 'ATT_ID': |
|
469 | + /** @var EE_Attendee $attendee */ |
|
470 | + $attendee = $model->get_one_by_ID($model_field_value); |
|
471 | + $label_parts[] = $attendee instanceof EE_Attendee |
|
472 | + ? sprintf(esc_html__('Attendee %s', 'event_espresso'), $attendee->full_name()) |
|
473 | + : sprintf(esc_html__('Attendee ID: %s', 'event_espresso'), $model_field_value); |
|
474 | + break; |
|
475 | + case 'ID': |
|
476 | + /** @var EE_WP_User $wpUser */ |
|
477 | + $wpUser = $model->get_one_by_ID($model_field_value); |
|
478 | + $label_parts[] = $wpUser instanceof EE_WP_User |
|
479 | + ? sprintf(esc_html__('WP User: %s', 'event_espresso'), $wpUser->name()) |
|
480 | + : sprintf(esc_html__('WP User ID: %s', 'event_espresso'), $model_field_value); |
|
481 | + break; |
|
482 | + case 'TXN_ID': |
|
483 | + $label_parts[] = sprintf( |
|
484 | + esc_html__('Transaction with the ID: %s', 'event_espresso'), |
|
485 | + $model_field_value |
|
486 | + ); |
|
487 | + break; |
|
488 | + case 'EVT_ID': |
|
489 | + /** @var EE_Event $Event */ |
|
490 | + $Event = $model->get_one_by_ID($model_field_value); |
|
491 | + $label_parts[] = $Event instanceof EE_Event |
|
492 | + ? sprintf(esc_html__('for the Event: %s', 'event_espresso'), $Event->name()) |
|
493 | + : sprintf(esc_html__('for the Event with ID: %s', 'event_espresso'), $model_field_value); |
|
494 | + break; |
|
495 | + } |
|
496 | + } |
|
497 | + } |
|
498 | + |
|
499 | + if ($label_parts) { |
|
500 | + // prepend to the last element of $label_parts an "and". |
|
501 | + if (count($label_parts) > 1) { |
|
502 | + $label_parts_index_to_prepend = count($label_parts) - 1; |
|
503 | + $label_parts[ $label_parts_index_to_prepend ] = 'and' . $label_parts[ $label_parts_index_to_prepend ]; |
|
504 | + } |
|
505 | + |
|
506 | + $pretty_label .= sprintf( |
|
507 | + esc_html_x( |
|
508 | + 'Showing messages for %s', |
|
509 | + 'A label for the messages returned in a query that are filtered by items in the query. This could be Transaction, Event, Attendee, Registration, or WP_User.', |
|
510 | + 'event_espresso' |
|
511 | + ), |
|
512 | + implode(', ', $label_parts) |
|
513 | + ); |
|
514 | + } |
|
515 | + return $pretty_label; |
|
516 | + } |
|
517 | + |
|
518 | + |
|
519 | + /** |
|
520 | + * This returns the array of expected variables for the EEI_Query_Filter methods being implemented |
|
521 | + * The array is in the format: |
|
522 | + * array( |
|
523 | + * {$field_name} => {$model_name} |
|
524 | + * ); |
|
525 | + * |
|
526 | + * @since 4.9.0 |
|
527 | + * @return array |
|
528 | + */ |
|
529 | + protected function _expected_vars_for_query_inject() |
|
530 | + { |
|
531 | + return array( |
|
532 | + '_REG_ID' => 'Registration', |
|
533 | + 'ATT_ID' => 'Attendee', |
|
534 | + 'ID' => 'WP_User', |
|
535 | + 'TXN_ID' => 'Transaction', |
|
536 | + 'EVT_ID' => 'Event', |
|
537 | + ); |
|
538 | + } |
|
539 | + |
|
540 | + |
|
541 | + /** |
|
542 | + * This returns whether EEM_Message is in debug mode or not. |
|
543 | + * Currently "debug mode" is used to control the handling of the EEM_Message::debug_only status when |
|
544 | + * generating/sending messages. Debug mode can be set by either: |
|
545 | + * 1. Sending in a value for the $set_debug argument |
|
546 | + * 2. Defining `EE_DEBUG_MESSAGES` constant in wp-config.php |
|
547 | + * 3. Overriding the above via the provided filter. |
|
548 | + * |
|
549 | + * @param bool|null $set_debug If provided, then the debug mode will be set internally until reset via the |
|
550 | + * provided boolean. When no argument is provided (default null) then the debug |
|
551 | + * mode will be returned. |
|
552 | + * @return bool true means Messages is in debug mode. false means messages system is not in debug mode. |
|
553 | + */ |
|
554 | + public static function debug($set_debug = null) |
|
555 | + { |
|
556 | + static $is_debugging = null; |
|
557 | + |
|
558 | + // initialize (use constant if set). |
|
559 | + if (is_null($set_debug) && is_null($is_debugging)) { |
|
560 | + $is_debugging = defined('EE_DEBUG_MESSAGES') && EE_DEBUG_MESSAGES; |
|
561 | + } |
|
562 | + |
|
563 | + if (! is_null($set_debug)) { |
|
564 | + $is_debugging = filter_var($set_debug, FILTER_VALIDATE_BOOLEAN); |
|
565 | + } |
|
566 | + |
|
567 | + // return filtered value |
|
568 | + return apply_filters('FHEE__EEM_Message__debug', $is_debugging); |
|
569 | + } |
|
570 | + |
|
571 | + |
|
572 | + /** |
|
573 | + * Deletes old messages meeting certain criteria for removal from the database. |
|
574 | + * By default, this will delete messages that: |
|
575 | + * - are older than the value of the delete_threshold in months. |
|
576 | + * - have a STS_ID other than EEM_Message::status_idle |
|
577 | + * |
|
578 | + * @param int $delete_threshold This integer will be used to set the boundary for what messages are deleted in months. |
|
579 | + * @return bool|false|int Either the number of records affected or false if there was an error (you can call |
|
580 | + * $wpdb->last_error to find out what the error was. |
|
581 | + * @throws EE_Error |
|
582 | + * @throws ReflectionException |
|
583 | + */ |
|
584 | + public function delete_old_messages($delete_threshold = 6) |
|
585 | + { |
|
586 | + $number_deleted = 0; |
|
587 | + /** |
|
588 | + * Allows code to change the boundary for what messages are kept. |
|
589 | + * Uses the value of the `delete_threshold` variable by default. |
|
590 | + * |
|
591 | + * @param int $seconds seconds that will be subtracted from the timestamp for now. |
|
592 | + * @return int |
|
593 | + */ |
|
594 | + $time_to_leave_alone = absint( |
|
595 | + apply_filters( |
|
596 | + 'FHEE__EEM_Message__delete_old_messages__time_to_leave_alone', |
|
597 | + ((int) $delete_threshold) * MONTH_IN_SECONDS |
|
598 | + ) |
|
599 | + ); |
|
600 | + |
|
601 | + |
|
602 | + /** |
|
603 | + * Allows code to change what message stati are ignored when deleting. |
|
604 | + * Defaults to only ignore EEM_Message::status_idle messages. |
|
605 | + * |
|
606 | + * @param string $message_stati_to_keep An array of message statuses that will be ignored when deleting. |
|
607 | + */ |
|
608 | + $message_stati_to_keep = (array) apply_filters( |
|
609 | + 'FHEE__EEM_Message__delete_old_messages__message_stati_to_keep', |
|
610 | + array( |
|
611 | + EEM_Message::status_idle |
|
612 | + ) |
|
613 | + ); |
|
614 | + |
|
615 | + // first get all the ids of messages being deleted |
|
616 | + $message_ids_to_delete = EEM_Message::instance()->get_col( |
|
617 | + array( |
|
618 | + 0 => array( |
|
619 | + 'STS_ID' => array('NOT_IN', $message_stati_to_keep), |
|
620 | + 'MSG_modified' => array('<', time() - $time_to_leave_alone) |
|
621 | + ), |
|
622 | + 'limit' => apply_filters( |
|
623 | + 'EEM_Message__delete_old_messages__limit', |
|
624 | + 2000, |
|
625 | + $delete_threshold |
|
626 | + ) |
|
627 | + ) |
|
628 | + ); |
|
629 | + |
|
630 | + if (! empty($message_ids_to_delete) && is_array($message_ids_to_delete)) { |
|
631 | + global $wpdb; |
|
632 | + $number_deleted = $wpdb->query(' |
|
633 | 633 | DELETE |
634 | 634 | FROM ' . $this->table() . ' |
635 | 635 | WHERE |
636 | 636 | MSG_ID IN (' . implode(",", $message_ids_to_delete) . ') |
637 | 637 | '); |
638 | - } |
|
639 | - |
|
640 | - /** |
|
641 | - * This will get called if the number of records deleted 0 or greater. So a successful deletion is one where |
|
642 | - * there were no errors. An unsuccessful deletion is where there were errors. Keep that in mind for the actions |
|
643 | - * below. |
|
644 | - */ |
|
645 | - if ($number_deleted !== false) { |
|
646 | - do_action('AHEE__EEM_Message__delete_old_messages__after_successful_deletion', $message_ids_to_delete, $number_deleted); |
|
647 | - } else { |
|
648 | - do_action('AHEE__EEM_Message__delete_old_messages__after_deletion_fail', $message_ids_to_delete, $number_deleted); |
|
649 | - } |
|
650 | - return $number_deleted; |
|
651 | - } |
|
638 | + } |
|
639 | + |
|
640 | + /** |
|
641 | + * This will get called if the number of records deleted 0 or greater. So a successful deletion is one where |
|
642 | + * there were no errors. An unsuccessful deletion is where there were errors. Keep that in mind for the actions |
|
643 | + * below. |
|
644 | + */ |
|
645 | + if ($number_deleted !== false) { |
|
646 | + do_action('AHEE__EEM_Message__delete_old_messages__after_successful_deletion', $message_ids_to_delete, $number_deleted); |
|
647 | + } else { |
|
648 | + do_action('AHEE__EEM_Message__delete_old_messages__after_deletion_fail', $message_ids_to_delete, $number_deleted); |
|
649 | + } |
|
650 | + return $number_deleted; |
|
651 | + } |
|
652 | 652 | } |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | self::priority_low => esc_html__('low', 'event_espresso'), |
127 | 127 | ); |
128 | 128 | |
129 | - $this->_fields = array( |
|
129 | + $this->_fields = array( |
|
130 | 130 | 'Message' => array( |
131 | 131 | 'MSG_ID' => new EE_Primary_Key_Int_Field('MSG_ID', esc_html__('Message ID', 'event_espresso')), |
132 | 132 | 'MSG_token' => new EE_Plain_Text_Field( |
@@ -436,7 +436,7 @@ discard block |
||
436 | 436 | ); |
437 | 437 | break; |
438 | 438 | default: |
439 | - $query_params[0]['AND**filter_by'][ 'OR**filter_by_' . $request_key ][ $model_name . '.' . $request_key ] = $request_value; |
|
439 | + $query_params[0]['AND**filter_by']['OR**filter_by_'.$request_key][$model_name.'.'.$request_key] = $request_value; |
|
440 | 440 | break; |
441 | 441 | } |
442 | 442 | } |
@@ -499,8 +499,8 @@ discard block |
||
499 | 499 | if ($label_parts) { |
500 | 500 | // prepend to the last element of $label_parts an "and". |
501 | 501 | if (count($label_parts) > 1) { |
502 | - $label_parts_index_to_prepend = count($label_parts) - 1; |
|
503 | - $label_parts[ $label_parts_index_to_prepend ] = 'and' . $label_parts[ $label_parts_index_to_prepend ]; |
|
502 | + $label_parts_index_to_prepend = count($label_parts) - 1; |
|
503 | + $label_parts[$label_parts_index_to_prepend] = 'and'.$label_parts[$label_parts_index_to_prepend]; |
|
504 | 504 | } |
505 | 505 | |
506 | 506 | $pretty_label .= sprintf( |
@@ -560,7 +560,7 @@ discard block |
||
560 | 560 | $is_debugging = defined('EE_DEBUG_MESSAGES') && EE_DEBUG_MESSAGES; |
561 | 561 | } |
562 | 562 | |
563 | - if (! is_null($set_debug)) { |
|
563 | + if ( ! is_null($set_debug)) { |
|
564 | 564 | $is_debugging = filter_var($set_debug, FILTER_VALIDATE_BOOLEAN); |
565 | 565 | } |
566 | 566 | |
@@ -627,13 +627,13 @@ discard block |
||
627 | 627 | ) |
628 | 628 | ); |
629 | 629 | |
630 | - if (! empty($message_ids_to_delete) && is_array($message_ids_to_delete)) { |
|
630 | + if ( ! empty($message_ids_to_delete) && is_array($message_ids_to_delete)) { |
|
631 | 631 | global $wpdb; |
632 | 632 | $number_deleted = $wpdb->query(' |
633 | 633 | DELETE |
634 | - FROM ' . $this->table() . ' |
|
634 | + FROM ' . $this->table().' |
|
635 | 635 | WHERE |
636 | - MSG_ID IN (' . implode(",", $message_ids_to_delete) . ') |
|
636 | + MSG_ID IN (' . implode(",", $message_ids_to_delete).') |
|
637 | 637 | '); |
638 | 638 | } |
639 | 639 |
@@ -8,97 +8,97 @@ |
||
8 | 8 | */ |
9 | 9 | abstract class EE_Field_With_Model_Name extends EE_Model_Field_Base |
10 | 10 | { |
11 | - /** |
|
12 | - * Usually the name of a single model. However, as in the case for custom post types, |
|
13 | - * it can actually be an array of models |
|
14 | - * |
|
15 | - * @var string|string[] |
|
16 | - */ |
|
17 | - protected $_model_name_pointed_to; |
|
11 | + /** |
|
12 | + * Usually the name of a single model. However, as in the case for custom post types, |
|
13 | + * it can actually be an array of models |
|
14 | + * |
|
15 | + * @var string|string[] |
|
16 | + */ |
|
17 | + protected $_model_name_pointed_to; |
|
18 | 18 | |
19 | 19 | |
20 | - /** |
|
21 | - * @param string $table_column name fo column for field |
|
22 | - * @param string $nice_name should be internationalized with esc_html__('blah','event_espresso') |
|
23 | - * @param boolean $nullable |
|
24 | - * @param mixed $default_value if this is a integer field, it should be an int. |
|
25 | - * if it's a string field, it should be a string |
|
26 | - * @param string|array $model_name eg 'Event', 'Answer', 'Term', etc. |
|
27 | - * Basically its the model class's name without the "EEM_" |
|
28 | - */ |
|
29 | - public function __construct($table_column, $nice_name, $nullable, $default_value, $model_name) |
|
30 | - { |
|
31 | - $this->_model_name_pointed_to = $model_name; |
|
32 | - parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
33 | - } |
|
20 | + /** |
|
21 | + * @param string $table_column name fo column for field |
|
22 | + * @param string $nice_name should be internationalized with esc_html__('blah','event_espresso') |
|
23 | + * @param boolean $nullable |
|
24 | + * @param mixed $default_value if this is a integer field, it should be an int. |
|
25 | + * if it's a string field, it should be a string |
|
26 | + * @param string|array $model_name eg 'Event', 'Answer', 'Term', etc. |
|
27 | + * Basically its the model class's name without the "EEM_" |
|
28 | + */ |
|
29 | + public function __construct($table_column, $nice_name, $nullable, $default_value, $model_name) |
|
30 | + { |
|
31 | + $this->_model_name_pointed_to = $model_name; |
|
32 | + parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
33 | + } |
|
34 | 34 | |
35 | 35 | |
36 | - /** |
|
37 | - * Returns the name of the model(s) pointed to |
|
38 | - * |
|
39 | - * @return string|string[] |
|
40 | - * @deprecated since version 4.6.7 |
|
41 | - */ |
|
42 | - public function get_model_name_pointed_to() |
|
43 | - { |
|
44 | - EE_Error::doing_it_wrong( |
|
45 | - 'get_model_name_pointed_to', |
|
46 | - esc_html__( |
|
47 | - 'This method has been deprecated in favour of instead using get_model_names_pointed_to, which consistently returns an array', |
|
48 | - 'event_espresso' |
|
49 | - ), |
|
50 | - '4.6.7' |
|
51 | - ); |
|
52 | - return $this->_model_name_pointed_to; |
|
53 | - } |
|
36 | + /** |
|
37 | + * Returns the name of the model(s) pointed to |
|
38 | + * |
|
39 | + * @return string|string[] |
|
40 | + * @deprecated since version 4.6.7 |
|
41 | + */ |
|
42 | + public function get_model_name_pointed_to() |
|
43 | + { |
|
44 | + EE_Error::doing_it_wrong( |
|
45 | + 'get_model_name_pointed_to', |
|
46 | + esc_html__( |
|
47 | + 'This method has been deprecated in favour of instead using get_model_names_pointed_to, which consistently returns an array', |
|
48 | + 'event_espresso' |
|
49 | + ), |
|
50 | + '4.6.7' |
|
51 | + ); |
|
52 | + return $this->_model_name_pointed_to; |
|
53 | + } |
|
54 | 54 | |
55 | 55 | |
56 | - /** |
|
57 | - * Gets the model names pointed to by this field, always as an array |
|
58 | - * (even if there's only one) |
|
59 | - * |
|
60 | - * @return string|string[] of model names pointed to by this field |
|
61 | - */ |
|
62 | - public function get_model_names_pointed_to() |
|
63 | - { |
|
64 | - if (is_array($this->_model_name_pointed_to)) { |
|
65 | - return $this->_model_name_pointed_to; |
|
66 | - } else { |
|
67 | - return [$this->_model_name_pointed_to]; |
|
68 | - } |
|
69 | - } |
|
56 | + /** |
|
57 | + * Gets the model names pointed to by this field, always as an array |
|
58 | + * (even if there's only one) |
|
59 | + * |
|
60 | + * @return string|string[] of model names pointed to by this field |
|
61 | + */ |
|
62 | + public function get_model_names_pointed_to() |
|
63 | + { |
|
64 | + if (is_array($this->_model_name_pointed_to)) { |
|
65 | + return $this->_model_name_pointed_to; |
|
66 | + } else { |
|
67 | + return [$this->_model_name_pointed_to]; |
|
68 | + } |
|
69 | + } |
|
70 | 70 | |
71 | 71 | |
72 | - /** |
|
73 | - * Returns the model's classname (eg EE_Event instead of just Event) |
|
74 | - * |
|
75 | - * @return string[] |
|
76 | - */ |
|
77 | - public function get_model_class_names_pointed_to(): array |
|
78 | - { |
|
79 | - $model_names = []; |
|
80 | - if (is_array($this->_model_name_pointed_to)) { |
|
81 | - foreach ($this->_model_name_pointed_to as $model_name) { |
|
82 | - $model_names[] = "EE_" . $model_name; |
|
83 | - } |
|
84 | - } else { |
|
85 | - $model_names = ["EE_" . $this->_model_name_pointed_to]; |
|
86 | - } |
|
87 | - return $model_names; |
|
88 | - } |
|
72 | + /** |
|
73 | + * Returns the model's classname (eg EE_Event instead of just Event) |
|
74 | + * |
|
75 | + * @return string[] |
|
76 | + */ |
|
77 | + public function get_model_class_names_pointed_to(): array |
|
78 | + { |
|
79 | + $model_names = []; |
|
80 | + if (is_array($this->_model_name_pointed_to)) { |
|
81 | + foreach ($this->_model_name_pointed_to as $model_name) { |
|
82 | + $model_names[] = "EE_" . $model_name; |
|
83 | + } |
|
84 | + } else { |
|
85 | + $model_names = ["EE_" . $this->_model_name_pointed_to]; |
|
86 | + } |
|
87 | + return $model_names; |
|
88 | + } |
|
89 | 89 | |
90 | 90 | |
91 | - /** |
|
92 | - * @param $model_obj_or_ID |
|
93 | - * @return bool |
|
94 | - */ |
|
95 | - public function is_model_obj_of_type_pointed_to($model_obj_or_ID): bool |
|
96 | - { |
|
97 | - foreach ($this->get_model_class_names_pointed_to() as $model_obj_classname) { |
|
98 | - if ($model_obj_or_ID instanceof $model_obj_classname) { |
|
99 | - return true; |
|
100 | - } |
|
101 | - } |
|
102 | - return false; |
|
103 | - } |
|
91 | + /** |
|
92 | + * @param $model_obj_or_ID |
|
93 | + * @return bool |
|
94 | + */ |
|
95 | + public function is_model_obj_of_type_pointed_to($model_obj_or_ID): bool |
|
96 | + { |
|
97 | + foreach ($this->get_model_class_names_pointed_to() as $model_obj_classname) { |
|
98 | + if ($model_obj_or_ID instanceof $model_obj_classname) { |
|
99 | + return true; |
|
100 | + } |
|
101 | + } |
|
102 | + return false; |
|
103 | + } |
|
104 | 104 | } |
@@ -79,10 +79,10 @@ |
||
79 | 79 | $model_names = []; |
80 | 80 | if (is_array($this->_model_name_pointed_to)) { |
81 | 81 | foreach ($this->_model_name_pointed_to as $model_name) { |
82 | - $model_names[] = "EE_" . $model_name; |
|
82 | + $model_names[] = "EE_".$model_name; |
|
83 | 83 | } |
84 | 84 | } else { |
85 | - $model_names = ["EE_" . $this->_model_name_pointed_to]; |
|
85 | + $model_names = ["EE_".$this->_model_name_pointed_to]; |
|
86 | 86 | } |
87 | 87 | return $model_names; |
88 | 88 | } |
@@ -5,12 +5,12 @@ |
||
5 | 5 | */ |
6 | 6 | class EE_WP_Post_Type_Field extends EE_DB_Only_Text_Field |
7 | 7 | { |
8 | - /** |
|
9 | - * @param string $post_type the exact string to be used for the post type |
|
10 | - * of all these post type model objects/rows |
|
11 | - */ |
|
12 | - public function __construct($post_type) |
|
13 | - { |
|
14 | - parent::__construct('post_type', esc_html__("Post Type", 'event_espresso'), false, $post_type); |
|
15 | - } |
|
8 | + /** |
|
9 | + * @param string $post_type the exact string to be used for the post type |
|
10 | + * of all these post type model objects/rows |
|
11 | + */ |
|
12 | + public function __construct($post_type) |
|
13 | + { |
|
14 | + parent::__construct('post_type', esc_html__("Post Type", 'event_espresso'), false, $post_type); |
|
15 | + } |
|
16 | 16 | } |
@@ -11,131 +11,131 @@ |
||
11 | 11 | class EE_Enum_Text_Field extends EE_Text_Field_Base |
12 | 12 | { |
13 | 13 | |
14 | - /** |
|
15 | - * @var array $_allowed_enum_values |
|
16 | - */ |
|
17 | - public $_allowed_enum_values; |
|
18 | - |
|
19 | - /** |
|
20 | - * @param string $table_column |
|
21 | - * @param string $nice_name |
|
22 | - * @param boolean $nullable |
|
23 | - * @param mixed $default_value |
|
24 | - * @param array $allowed_enum_values keys are values to be used in the DB, values are how they should be displayed |
|
25 | - */ |
|
26 | - public function __construct($table_column, $nice_name, $nullable, $default_value, $allowed_enum_values) |
|
27 | - { |
|
28 | - $this->_allowed_enum_values = $allowed_enum_values; |
|
29 | - parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
30 | - $this->setSchemaType('object'); |
|
31 | - } |
|
32 | - |
|
33 | - |
|
34 | - |
|
35 | - /** |
|
36 | - * Returns the list of allowed enum options, but filterable. |
|
37 | - * This is used internally |
|
38 | - * |
|
39 | - * @return array |
|
40 | - */ |
|
41 | - protected function _allowed_enum_values() |
|
42 | - { |
|
43 | - return apply_filters( |
|
44 | - 'FHEE__EE_Enum_Text_Field___allowed_enum_options', |
|
45 | - $this->_allowed_enum_values, |
|
46 | - $this |
|
47 | - ); |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * When setting, just verify that the value being used matches what we've defined as allowable enum values. |
|
54 | - * If not, throw an error (but if WP_DEBUG is false, just set the value to default). |
|
55 | - * |
|
56 | - * @param string $value_inputted_for_field_on_model_object |
|
57 | - * @return string |
|
58 | - * @throws EE_Error |
|
59 | - */ |
|
60 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
61 | - { |
|
62 | - if ( |
|
63 | - $value_inputted_for_field_on_model_object !== null |
|
64 | - && ! array_key_exists($value_inputted_for_field_on_model_object, $this->_allowed_enum_values()) |
|
65 | - ) { |
|
66 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
67 | - $msg = sprintf( |
|
68 | - esc_html__('System is assigning incompatible value "%1$s" to field "%2$s"', 'event_espresso'), |
|
69 | - $value_inputted_for_field_on_model_object, |
|
70 | - $this->_name |
|
71 | - ); |
|
72 | - $msg2 = sprintf( |
|
73 | - esc_html__('Allowed values for "%1$s" are "%2$s". You provided: "%3$s"', 'event_espresso'), |
|
74 | - $this->_name, |
|
75 | - implode(', ', array_keys($this->_allowed_enum_values())), |
|
76 | - $value_inputted_for_field_on_model_object |
|
77 | - ); |
|
78 | - EE_Error::add_error("{$msg}||{$msg2}", __FILE__, __FUNCTION__, __LINE__); |
|
79 | - } |
|
80 | - return $this->get_default_value(); |
|
81 | - } |
|
82 | - return $value_inputted_for_field_on_model_object; |
|
83 | - } |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * Gets the pretty version of the enum's value. |
|
88 | - * |
|
89 | - * @param int |string $value_on_field_to_be_outputted |
|
90 | - * @param null $schema |
|
91 | - * @return string |
|
92 | - */ |
|
93 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
94 | - { |
|
95 | - $options = $this->_allowed_enum_values(); |
|
96 | - if (isset($options[ $value_on_field_to_be_outputted ])) { |
|
97 | - return $options[ $value_on_field_to_be_outputted ]; |
|
98 | - } else { |
|
99 | - return $value_on_field_to_be_outputted; |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * When retrieving something from the DB, don't enforce the enum's options. If it's in the DB, we just have to live |
|
107 | - * with that. Note also: when we're saving to the DB again, we also don't enforce the enum options. It's ONLY |
|
108 | - * when we're receiving USER input from prepare_for_set() that we enforce the enum options. |
|
109 | - * |
|
110 | - * @param mixed $value_in_db |
|
111 | - * @return mixed |
|
112 | - */ |
|
113 | - public function prepare_for_set_from_db($value_in_db) |
|
114 | - { |
|
115 | - return $value_in_db; |
|
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - public function getSchemaProperties() |
|
120 | - { |
|
121 | - return array( |
|
122 | - 'raw' => array( |
|
123 | - 'description' => sprintf( |
|
124 | - esc_html__('%s - the value in the database.', 'event_espresso'), |
|
125 | - $this->get_nicename() |
|
126 | - ), |
|
127 | - 'type' => 'string', |
|
128 | - 'enum' => array_keys($this->_allowed_enum_values) |
|
129 | - ), |
|
130 | - 'pretty' => array( |
|
131 | - 'description' => sprintf( |
|
132 | - esc_html__('%s - the value for display.', 'event_espresso'), |
|
133 | - $this->get_nicename() |
|
134 | - ), |
|
135 | - 'type' => 'string', |
|
136 | - 'enum' => array_values($this->_allowed_enum_values), |
|
137 | - 'read_only' => true |
|
138 | - ) |
|
139 | - ); |
|
140 | - } |
|
14 | + /** |
|
15 | + * @var array $_allowed_enum_values |
|
16 | + */ |
|
17 | + public $_allowed_enum_values; |
|
18 | + |
|
19 | + /** |
|
20 | + * @param string $table_column |
|
21 | + * @param string $nice_name |
|
22 | + * @param boolean $nullable |
|
23 | + * @param mixed $default_value |
|
24 | + * @param array $allowed_enum_values keys are values to be used in the DB, values are how they should be displayed |
|
25 | + */ |
|
26 | + public function __construct($table_column, $nice_name, $nullable, $default_value, $allowed_enum_values) |
|
27 | + { |
|
28 | + $this->_allowed_enum_values = $allowed_enum_values; |
|
29 | + parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
30 | + $this->setSchemaType('object'); |
|
31 | + } |
|
32 | + |
|
33 | + |
|
34 | + |
|
35 | + /** |
|
36 | + * Returns the list of allowed enum options, but filterable. |
|
37 | + * This is used internally |
|
38 | + * |
|
39 | + * @return array |
|
40 | + */ |
|
41 | + protected function _allowed_enum_values() |
|
42 | + { |
|
43 | + return apply_filters( |
|
44 | + 'FHEE__EE_Enum_Text_Field___allowed_enum_options', |
|
45 | + $this->_allowed_enum_values, |
|
46 | + $this |
|
47 | + ); |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * When setting, just verify that the value being used matches what we've defined as allowable enum values. |
|
54 | + * If not, throw an error (but if WP_DEBUG is false, just set the value to default). |
|
55 | + * |
|
56 | + * @param string $value_inputted_for_field_on_model_object |
|
57 | + * @return string |
|
58 | + * @throws EE_Error |
|
59 | + */ |
|
60 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
61 | + { |
|
62 | + if ( |
|
63 | + $value_inputted_for_field_on_model_object !== null |
|
64 | + && ! array_key_exists($value_inputted_for_field_on_model_object, $this->_allowed_enum_values()) |
|
65 | + ) { |
|
66 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
67 | + $msg = sprintf( |
|
68 | + esc_html__('System is assigning incompatible value "%1$s" to field "%2$s"', 'event_espresso'), |
|
69 | + $value_inputted_for_field_on_model_object, |
|
70 | + $this->_name |
|
71 | + ); |
|
72 | + $msg2 = sprintf( |
|
73 | + esc_html__('Allowed values for "%1$s" are "%2$s". You provided: "%3$s"', 'event_espresso'), |
|
74 | + $this->_name, |
|
75 | + implode(', ', array_keys($this->_allowed_enum_values())), |
|
76 | + $value_inputted_for_field_on_model_object |
|
77 | + ); |
|
78 | + EE_Error::add_error("{$msg}||{$msg2}", __FILE__, __FUNCTION__, __LINE__); |
|
79 | + } |
|
80 | + return $this->get_default_value(); |
|
81 | + } |
|
82 | + return $value_inputted_for_field_on_model_object; |
|
83 | + } |
|
84 | + |
|
85 | + |
|
86 | + /** |
|
87 | + * Gets the pretty version of the enum's value. |
|
88 | + * |
|
89 | + * @param int |string $value_on_field_to_be_outputted |
|
90 | + * @param null $schema |
|
91 | + * @return string |
|
92 | + */ |
|
93 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
94 | + { |
|
95 | + $options = $this->_allowed_enum_values(); |
|
96 | + if (isset($options[ $value_on_field_to_be_outputted ])) { |
|
97 | + return $options[ $value_on_field_to_be_outputted ]; |
|
98 | + } else { |
|
99 | + return $value_on_field_to_be_outputted; |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * When retrieving something from the DB, don't enforce the enum's options. If it's in the DB, we just have to live |
|
107 | + * with that. Note also: when we're saving to the DB again, we also don't enforce the enum options. It's ONLY |
|
108 | + * when we're receiving USER input from prepare_for_set() that we enforce the enum options. |
|
109 | + * |
|
110 | + * @param mixed $value_in_db |
|
111 | + * @return mixed |
|
112 | + */ |
|
113 | + public function prepare_for_set_from_db($value_in_db) |
|
114 | + { |
|
115 | + return $value_in_db; |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + public function getSchemaProperties() |
|
120 | + { |
|
121 | + return array( |
|
122 | + 'raw' => array( |
|
123 | + 'description' => sprintf( |
|
124 | + esc_html__('%s - the value in the database.', 'event_espresso'), |
|
125 | + $this->get_nicename() |
|
126 | + ), |
|
127 | + 'type' => 'string', |
|
128 | + 'enum' => array_keys($this->_allowed_enum_values) |
|
129 | + ), |
|
130 | + 'pretty' => array( |
|
131 | + 'description' => sprintf( |
|
132 | + esc_html__('%s - the value for display.', 'event_espresso'), |
|
133 | + $this->get_nicename() |
|
134 | + ), |
|
135 | + 'type' => 'string', |
|
136 | + 'enum' => array_values($this->_allowed_enum_values), |
|
137 | + 'read_only' => true |
|
138 | + ) |
|
139 | + ); |
|
140 | + } |
|
141 | 141 | } |
@@ -2,69 +2,69 @@ |
||
2 | 2 | |
3 | 3 | class EE_Boolean_Field extends EE_Integer_Field |
4 | 4 | { |
5 | - /** |
|
6 | - * @param string $table_column |
|
7 | - * @param string $nicename |
|
8 | - * @param bool $nullable |
|
9 | - * @param null $default_value |
|
10 | - */ |
|
11 | - public function __construct(string $table_column, string $nicename, bool $nullable, $default_value = null) |
|
12 | - { |
|
13 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
14 | - $this->setSchemaType('boolean'); |
|
15 | - } |
|
5 | + /** |
|
6 | + * @param string $table_column |
|
7 | + * @param string $nicename |
|
8 | + * @param bool $nullable |
|
9 | + * @param null $default_value |
|
10 | + */ |
|
11 | + public function __construct(string $table_column, string $nicename, bool $nullable, $default_value = null) |
|
12 | + { |
|
13 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
14 | + $this->setSchemaType('boolean'); |
|
15 | + } |
|
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * Double-checks the value being returned is an boolean. |
|
20 | - * |
|
21 | - * @param mixed $value_of_field_on_model_object |
|
22 | - * @return boolean |
|
23 | - * @since 4.9.74.p |
|
24 | - */ |
|
25 | - public function prepare_for_get($value_of_field_on_model_object): bool |
|
26 | - { |
|
27 | - return (bool) parent::prepare_for_get($value_of_field_on_model_object); |
|
28 | - } |
|
18 | + /** |
|
19 | + * Double-checks the value being returned is an boolean. |
|
20 | + * |
|
21 | + * @param mixed $value_of_field_on_model_object |
|
22 | + * @return boolean |
|
23 | + * @since 4.9.74.p |
|
24 | + */ |
|
25 | + public function prepare_for_get($value_of_field_on_model_object): bool |
|
26 | + { |
|
27 | + return (bool) parent::prepare_for_get($value_of_field_on_model_object); |
|
28 | + } |
|
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * @param $value_inputted_for_field_on_model_object |
|
33 | - * @return boolean |
|
34 | - * @since 4.9.74.p |
|
35 | - */ |
|
36 | - public function prepare_for_set($value_inputted_for_field_on_model_object): bool |
|
37 | - { |
|
38 | - return $value_inputted_for_field_on_model_object; |
|
39 | - } |
|
31 | + /** |
|
32 | + * @param $value_inputted_for_field_on_model_object |
|
33 | + * @return boolean |
|
34 | + * @since 4.9.74.p |
|
35 | + */ |
|
36 | + public function prepare_for_set($value_inputted_for_field_on_model_object): bool |
|
37 | + { |
|
38 | + return $value_inputted_for_field_on_model_object; |
|
39 | + } |
|
40 | 40 | |
41 | 41 | |
42 | - /** |
|
43 | - * Make sure we're returning booleans |
|
44 | - * |
|
45 | - * @param string $value_inputted_for_field_on_model_object |
|
46 | - * @return boolean |
|
47 | - */ |
|
48 | - public function prepare_for_set_from_db($value_inputted_for_field_on_model_object): bool |
|
49 | - { |
|
50 | - return (bool) intval($value_inputted_for_field_on_model_object); |
|
51 | - } |
|
42 | + /** |
|
43 | + * Make sure we're returning booleans |
|
44 | + * |
|
45 | + * @param string $value_inputted_for_field_on_model_object |
|
46 | + * @return boolean |
|
47 | + */ |
|
48 | + public function prepare_for_set_from_db($value_inputted_for_field_on_model_object): bool |
|
49 | + { |
|
50 | + return (bool) intval($value_inputted_for_field_on_model_object); |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | - /** |
|
55 | - * Gets a nice Yes/No value for this field |
|
56 | - * |
|
57 | - * @param boolean $value_on_field_to_be_outputted |
|
58 | - * @return string Yes or No |
|
59 | - */ |
|
60 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted): string |
|
61 | - { |
|
62 | - return apply_filters( |
|
63 | - 'FHEE__EE_Boolean_Field__prepare_for_pretty_echoing__return', |
|
64 | - $value_on_field_to_be_outputted |
|
65 | - ? esc_html__('Yes', 'event_espresso') |
|
66 | - : esc_html__('No', 'event_espresso'), |
|
67 | - $value_on_field_to_be_outputted |
|
68 | - ); |
|
69 | - } |
|
54 | + /** |
|
55 | + * Gets a nice Yes/No value for this field |
|
56 | + * |
|
57 | + * @param boolean $value_on_field_to_be_outputted |
|
58 | + * @return string Yes or No |
|
59 | + */ |
|
60 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted): string |
|
61 | + { |
|
62 | + return apply_filters( |
|
63 | + 'FHEE__EE_Boolean_Field__prepare_for_pretty_echoing__return', |
|
64 | + $value_on_field_to_be_outputted |
|
65 | + ? esc_html__('Yes', 'event_espresso') |
|
66 | + : esc_html__('No', 'event_espresso'), |
|
67 | + $value_on_field_to_be_outputted |
|
68 | + ); |
|
69 | + } |
|
70 | 70 | } |
@@ -16,762 +16,762 @@ |
||
16 | 16 | class EE_Datetime_Field extends EE_Model_Field_Base |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
21 | - * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
22 | - * |
|
23 | - * @type string unix_timestamp_regex |
|
24 | - */ |
|
25 | - const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
26 | - |
|
27 | - /** |
|
28 | - * @type string mysql_timestamp_format |
|
29 | - */ |
|
30 | - const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
31 | - |
|
32 | - /** |
|
33 | - * @type string mysql_date_format |
|
34 | - */ |
|
35 | - const mysql_date_format = 'Y-m-d'; |
|
36 | - |
|
37 | - /** |
|
38 | - * @type string mysql_time_format |
|
39 | - */ |
|
40 | - const mysql_time_format = 'H:i:s'; |
|
41 | - |
|
42 | - /** |
|
43 | - * Const for using in the default value. If the field's default is set to this, |
|
44 | - * then we will return the time of calling `get_default_value()`, not |
|
45 | - * just the current time at construction |
|
46 | - */ |
|
47 | - const now = 'now'; |
|
48 | - |
|
49 | - /** |
|
50 | - * The following properties hold the default formats for date and time. |
|
51 | - * Defaults are set via the constructor and can be overridden on class instantiation. |
|
52 | - * However they can also be overridden later by the set_format() method |
|
53 | - * (and corresponding set_date_format, set_time_format methods); |
|
54 | - */ |
|
55 | - /** |
|
56 | - * @type string $_date_format |
|
57 | - */ |
|
58 | - protected $_date_format = ''; |
|
59 | - |
|
60 | - /** |
|
61 | - * @type string $_time_format |
|
62 | - */ |
|
63 | - protected $_time_format = ''; |
|
64 | - |
|
65 | - /** |
|
66 | - * @type string $_pretty_date_format |
|
67 | - */ |
|
68 | - protected $_pretty_date_format = ''; |
|
69 | - |
|
70 | - /** |
|
71 | - * @type string $_pretty_time_format |
|
72 | - */ |
|
73 | - protected $_pretty_time_format = ''; |
|
74 | - |
|
75 | - /** |
|
76 | - * @type DateTimeZone $_DateTimeZone |
|
77 | - */ |
|
78 | - protected $_DateTimeZone; |
|
79 | - |
|
80 | - /** |
|
81 | - * @type DateTimeZone $_UTC_DateTimeZone |
|
82 | - */ |
|
83 | - protected $_UTC_DateTimeZone; |
|
84 | - |
|
85 | - /** |
|
86 | - * @type DateTimeZone $_blog_DateTimeZone |
|
87 | - */ |
|
88 | - protected $_blog_DateTimeZone; |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
93 | - * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
94 | - * and time returned via getters. |
|
95 | - * |
|
96 | - * @var string |
|
97 | - */ |
|
98 | - protected $_date_time_output = ''; |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * timezone string |
|
103 | - * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
104 | - * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
105 | - * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
106 | - * |
|
107 | - * @var string |
|
108 | - */ |
|
109 | - protected $_timezone_string; |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
114 | - * offsets for comparison purposes). |
|
115 | - * |
|
116 | - * @var int |
|
117 | - */ |
|
118 | - protected $_blog_offset; |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * @param string $table_column |
|
123 | - * @param string $nice_name |
|
124 | - * @param bool $nullable |
|
125 | - * @param string $default_value |
|
126 | - * @param string $timezone_string |
|
127 | - * @param string $date_format |
|
128 | - * @param string $time_format |
|
129 | - * @param string $pretty_date_format |
|
130 | - * @param string $pretty_time_format |
|
131 | - */ |
|
132 | - public function __construct( |
|
133 | - string $table_column, |
|
134 | - string $nice_name, |
|
135 | - bool $nullable, |
|
136 | - string $default_value, |
|
137 | - string $timezone_string = '', |
|
138 | - string $date_format = '', |
|
139 | - string $time_format = '', |
|
140 | - string $pretty_date_format = '', |
|
141 | - string $pretty_time_format = '' |
|
142 | - ) { |
|
143 | - |
|
144 | - $this->_date_format = ! empty($date_format) |
|
145 | - ? $date_format |
|
146 | - : get_option('date_format'); |
|
147 | - $this->_time_format = ! empty($time_format) |
|
148 | - ? $time_format |
|
149 | - : get_option('time_format'); |
|
150 | - $this->_pretty_date_format = ! empty($pretty_date_format) |
|
151 | - ? $pretty_date_format |
|
152 | - : get_option('date_format'); |
|
153 | - $this->_pretty_time_format = ! empty($pretty_time_format) |
|
154 | - ? $pretty_time_format |
|
155 | - : get_option('time_format'); |
|
156 | - |
|
157 | - parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
158 | - $this->set_timezone($timezone_string); |
|
159 | - $this->setSchemaFormat('date-time'); |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - /** |
|
164 | - * @return DateTimeZone |
|
165 | - */ |
|
166 | - public function get_UTC_DateTimeZone(): DateTimeZone |
|
167 | - { |
|
168 | - return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
169 | - ? $this->_UTC_DateTimeZone |
|
170 | - : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
171 | - } |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * @return DateTimeZone |
|
176 | - */ |
|
177 | - public function get_blog_DateTimeZone(): DateTimeZone |
|
178 | - { |
|
179 | - return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
180 | - ? $this->_blog_DateTimeZone |
|
181 | - : $this->_create_timezone_object_from_timezone_string(); |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - /** |
|
186 | - * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
|
187 | - * |
|
188 | - * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
|
189 | - * timestamp |
|
190 | - * @return DateTime |
|
191 | - */ |
|
192 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
193 | - { |
|
194 | - return $this->_get_date_object($value_inputted_for_field_on_model_object); |
|
195 | - } |
|
196 | - |
|
197 | - |
|
198 | - /** |
|
199 | - * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
200 | - * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
201 | - * |
|
202 | - * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
203 | - * @return string The final assembled format string. |
|
204 | - */ |
|
205 | - protected function _get_date_time_output(bool $pretty = false): string |
|
206 | - { |
|
207 | - $pretty = filter_var($pretty, FILTER_VALIDATE_BOOLEAN); |
|
208 | - switch ($this->_date_time_output) { |
|
209 | - case 'time': |
|
210 | - return $pretty |
|
211 | - ? $this->_pretty_time_format |
|
212 | - : $this->_time_format; |
|
213 | - |
|
214 | - case 'date': |
|
215 | - return $pretty |
|
216 | - ? $this->_pretty_date_format |
|
217 | - : $this->_date_format; |
|
218 | - |
|
219 | - default: |
|
220 | - return $pretty |
|
221 | - ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
222 | - : $this->_date_format . ' ' . $this->_time_format; |
|
223 | - } |
|
224 | - } |
|
225 | - |
|
226 | - |
|
227 | - /** |
|
228 | - * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
229 | - * returned (using the format properties) |
|
230 | - * |
|
231 | - * @param string $what acceptable values are 'time' or 'date'. |
|
232 | - * Any other value will be set but will always result |
|
233 | - * in both 'date' and 'time' being returned. |
|
234 | - * @return void |
|
235 | - */ |
|
236 | - public function set_date_time_output(string $what = '') |
|
237 | - { |
|
238 | - $this->_date_time_output = $what; |
|
239 | - } |
|
240 | - |
|
241 | - |
|
242 | - /** |
|
243 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
244 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
245 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
246 | - * We also set some other properties in this method. |
|
247 | - * |
|
248 | - * @param string $timezone_string A valid timezone string as described by @link |
|
249 | - * http://www.php.net/manual/en/timezones.php |
|
250 | - * @return void |
|
251 | - * @throws InvalidArgumentException |
|
252 | - * @throws InvalidDataTypeException |
|
253 | - * @throws InvalidInterfaceException |
|
254 | - */ |
|
255 | - public function set_timezone(string $timezone_string) |
|
256 | - { |
|
257 | - if (empty($timezone_string) && ! empty($this->_timezone_string)) { |
|
258 | - // leave the timezone AS-IS if we already have one and |
|
259 | - // the function arg didn't provide one |
|
260 | - return; |
|
261 | - } |
|
262 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
263 | - $this->_timezone_string = ! empty($timezone_string) |
|
264 | - ? $timezone_string |
|
265 | - : 'UTC'; |
|
266 | - $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
267 | - } |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * _create_timezone_object_from_timezone_name |
|
272 | - * |
|
273 | - * @param string $timezone_string |
|
274 | - * @return DateTimeZone |
|
275 | - * @throws InvalidArgumentException |
|
276 | - * @throws InvalidDataTypeException |
|
277 | - * @throws InvalidInterfaceException |
|
278 | - */ |
|
279 | - protected function _create_timezone_object_from_timezone_string(string $timezone_string = ''): DateTimeZone |
|
280 | - { |
|
281 | - return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
282 | - } |
|
283 | - |
|
284 | - |
|
285 | - /** |
|
286 | - * This just returns whatever is set for the current timezone. |
|
287 | - * |
|
288 | - * @return string timezone string |
|
289 | - */ |
|
290 | - public function get_timezone(): string |
|
291 | - { |
|
292 | - return $this->_timezone_string; |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * set the $_date_format property |
|
298 | - * |
|
299 | - * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
300 | - * @param bool $pretty Whether to set pretty format or not. |
|
301 | - * @return void |
|
302 | - */ |
|
303 | - public function set_date_format(string $format, bool $pretty = false) |
|
304 | - { |
|
305 | - if ($pretty) { |
|
306 | - $this->_pretty_date_format = $format; |
|
307 | - } else { |
|
308 | - $this->_date_format = $format; |
|
309 | - } |
|
310 | - } |
|
311 | - |
|
312 | - |
|
313 | - /** |
|
314 | - * return the $_date_format property value. |
|
315 | - * |
|
316 | - * @param bool $pretty Whether to get pretty format or not. |
|
317 | - * @return string |
|
318 | - */ |
|
319 | - public function get_date_format(bool $pretty = false): string |
|
320 | - { |
|
321 | - return $pretty |
|
322 | - ? $this->_pretty_date_format |
|
323 | - : $this->_date_format; |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * set the $_time_format property |
|
329 | - * |
|
330 | - * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
331 | - * @param bool $pretty Whether to set pretty format or not. |
|
332 | - * @return void |
|
333 | - */ |
|
334 | - public function set_time_format(string $format, bool $pretty = false) |
|
335 | - { |
|
336 | - if ($pretty) { |
|
337 | - $this->_pretty_time_format = $format; |
|
338 | - } else { |
|
339 | - $this->_time_format = $format; |
|
340 | - } |
|
341 | - } |
|
342 | - |
|
343 | - |
|
344 | - /** |
|
345 | - * return the $_time_format property value. |
|
346 | - * |
|
347 | - * @param bool $pretty Whether to get pretty format or not. |
|
348 | - * @return string |
|
349 | - */ |
|
350 | - public function get_time_format(bool $pretty = false): string |
|
351 | - { |
|
352 | - return $pretty |
|
353 | - ? $this->_pretty_time_format |
|
354 | - : $this->_time_format; |
|
355 | - } |
|
356 | - |
|
357 | - |
|
358 | - /** |
|
359 | - * set the $_pretty_date_format property |
|
360 | - * |
|
361 | - * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
362 | - * @return void |
|
363 | - */ |
|
364 | - public function set_pretty_date_format(string $format) |
|
365 | - { |
|
366 | - $this->_pretty_date_format = $format; |
|
367 | - } |
|
368 | - |
|
369 | - |
|
370 | - /** |
|
371 | - * set the $_pretty_time_format property |
|
372 | - * |
|
373 | - * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
374 | - * @return void |
|
375 | - */ |
|
376 | - public function set_pretty_time_format(string $format) |
|
377 | - { |
|
378 | - $this->_pretty_time_format = $format; |
|
379 | - } |
|
380 | - |
|
381 | - |
|
382 | - /** |
|
383 | - * Only sets the time portion of the datetime. |
|
384 | - * |
|
385 | - * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
386 | - * @param DateTime $current current DateTime object for the datetime field |
|
387 | - * @return DateTime |
|
388 | - */ |
|
389 | - public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current): DateTime |
|
390 | - { |
|
391 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
392 | - // Otherwise parse the string. |
|
393 | - if ($time_to_set_string instanceof DateTime) { |
|
394 | - $parsed = [ |
|
395 | - 'hour' => $time_to_set_string->format('H'), |
|
396 | - 'minute' => $time_to_set_string->format('i'), |
|
397 | - 'second' => $time_to_set_string->format('s'), |
|
398 | - ]; |
|
399 | - } else { |
|
400 | - // parse incoming string |
|
401 | - $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
402 | - } |
|
403 | - EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
404 | - return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
405 | - } |
|
406 | - |
|
407 | - |
|
408 | - /** |
|
409 | - * Only sets the date portion of the datetime. |
|
410 | - * |
|
411 | - * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
|
412 | - * @param DateTime $current current DateTime object for the datetime field |
|
413 | - * @return DateTime |
|
414 | - */ |
|
415 | - public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current): DateTime |
|
416 | - { |
|
417 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
418 | - // Otherwise parse the string. |
|
419 | - if ($date_to_set_string instanceof DateTime) { |
|
420 | - $parsed = [ |
|
421 | - 'year' => $date_to_set_string->format('Y'), |
|
422 | - 'month' => $date_to_set_string->format('m'), |
|
423 | - 'day' => $date_to_set_string->format('d'), |
|
424 | - ]; |
|
425 | - } else { |
|
426 | - // parse incoming string |
|
427 | - $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
428 | - } |
|
429 | - EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
430 | - return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
431 | - } |
|
432 | - |
|
433 | - |
|
434 | - /** |
|
435 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
|
436 | - * datetime gets to this stage it should ALREADY be in UTC time |
|
437 | - * |
|
438 | - * @param DateTime $DateTime |
|
439 | - * @return string formatted date time for given timezone |
|
440 | - * @throws EE_Error |
|
441 | - */ |
|
442 | - public function prepare_for_get($DateTime): string |
|
443 | - { |
|
444 | - return $this->_prepare_for_display($DateTime); |
|
445 | - } |
|
446 | - |
|
447 | - |
|
448 | - /** |
|
449 | - * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
450 | - * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
451 | - * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
452 | - * abbreviation to the date_string. |
|
453 | - * |
|
454 | - * @param DateTime $DateTime |
|
455 | - * @param string $schema |
|
456 | - * @return string |
|
457 | - * @throws EE_Error |
|
458 | - */ |
|
459 | - public function prepare_for_pretty_echoing($DateTime, string $schema = 'pretty'): string |
|
460 | - { |
|
461 | - return $this->_prepare_for_display($DateTime, $schema); |
|
462 | - } |
|
463 | - |
|
464 | - |
|
465 | - /** |
|
466 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
467 | - * timezone). |
|
468 | - * |
|
469 | - * @param DateTime $DateTime |
|
470 | - * @param string $schema |
|
471 | - * @return string |
|
472 | - * @throws EE_Error |
|
473 | - */ |
|
474 | - protected function _prepare_for_display(DateTime $DateTime, string $schema = ''): string |
|
475 | - { |
|
476 | - if (! $DateTime instanceof DateTime) { |
|
477 | - if ($this->_nullable) { |
|
478 | - return ''; |
|
479 | - } else { |
|
480 | - if (WP_DEBUG) { |
|
481 | - throw new EE_Error( |
|
482 | - sprintf( |
|
483 | - esc_html__( |
|
484 | - 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable.', |
|
485 | - 'event_espresso' |
|
486 | - ), |
|
487 | - $this->_nicename |
|
488 | - ) |
|
489 | - ); |
|
490 | - } else { |
|
491 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now); |
|
492 | - EE_Error::add_error( |
|
493 | - sprintf( |
|
494 | - esc_html__( |
|
495 | - 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable. When WP_DEBUG is false, the value is set to "now" instead of throwing an exception.', |
|
496 | - 'event_espresso' |
|
497 | - ), |
|
498 | - $this->_nicename |
|
499 | - ) |
|
500 | - ); |
|
501 | - } |
|
502 | - } |
|
503 | - } |
|
504 | - $pretty = $schema === 'pretty'; |
|
505 | - $format_string = $this->_get_date_time_output($pretty); |
|
506 | - EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
507 | - |
|
508 | - $timezone_string = ''; |
|
509 | - if ($this->_display_timezone()) { |
|
510 | - // must be explicit because schema could equal true. |
|
511 | - $timezone_string = $schema === 'no_html' |
|
512 | - ? ' (' . $DateTime->format('T') . ')' |
|
513 | - : ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
514 | - } |
|
515 | - return $DateTime->format($format_string) . $timezone_string; |
|
516 | - } |
|
517 | - |
|
518 | - |
|
519 | - /** |
|
520 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
521 | - * timezone). |
|
522 | - * |
|
523 | - * @param DateTime|null $datetime_value |
|
524 | - * @return string mysql timestamp in UTC |
|
525 | - * @throws EE_Error |
|
526 | - */ |
|
527 | - public function prepare_for_use_in_db($datetime_value) |
|
528 | - { |
|
529 | - // we allow an empty value or DateTime object, but nothing else. |
|
530 | - if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
531 | - throw new EE_Error( |
|
532 | - sprintf( |
|
533 | - esc_html__( |
|
534 | - 'The incoming value being prepared for setting in the database must either be empty or a php |
|
19 | + /** |
|
20 | + * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
21 | + * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
22 | + * |
|
23 | + * @type string unix_timestamp_regex |
|
24 | + */ |
|
25 | + const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
26 | + |
|
27 | + /** |
|
28 | + * @type string mysql_timestamp_format |
|
29 | + */ |
|
30 | + const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
31 | + |
|
32 | + /** |
|
33 | + * @type string mysql_date_format |
|
34 | + */ |
|
35 | + const mysql_date_format = 'Y-m-d'; |
|
36 | + |
|
37 | + /** |
|
38 | + * @type string mysql_time_format |
|
39 | + */ |
|
40 | + const mysql_time_format = 'H:i:s'; |
|
41 | + |
|
42 | + /** |
|
43 | + * Const for using in the default value. If the field's default is set to this, |
|
44 | + * then we will return the time of calling `get_default_value()`, not |
|
45 | + * just the current time at construction |
|
46 | + */ |
|
47 | + const now = 'now'; |
|
48 | + |
|
49 | + /** |
|
50 | + * The following properties hold the default formats for date and time. |
|
51 | + * Defaults are set via the constructor and can be overridden on class instantiation. |
|
52 | + * However they can also be overridden later by the set_format() method |
|
53 | + * (and corresponding set_date_format, set_time_format methods); |
|
54 | + */ |
|
55 | + /** |
|
56 | + * @type string $_date_format |
|
57 | + */ |
|
58 | + protected $_date_format = ''; |
|
59 | + |
|
60 | + /** |
|
61 | + * @type string $_time_format |
|
62 | + */ |
|
63 | + protected $_time_format = ''; |
|
64 | + |
|
65 | + /** |
|
66 | + * @type string $_pretty_date_format |
|
67 | + */ |
|
68 | + protected $_pretty_date_format = ''; |
|
69 | + |
|
70 | + /** |
|
71 | + * @type string $_pretty_time_format |
|
72 | + */ |
|
73 | + protected $_pretty_time_format = ''; |
|
74 | + |
|
75 | + /** |
|
76 | + * @type DateTimeZone $_DateTimeZone |
|
77 | + */ |
|
78 | + protected $_DateTimeZone; |
|
79 | + |
|
80 | + /** |
|
81 | + * @type DateTimeZone $_UTC_DateTimeZone |
|
82 | + */ |
|
83 | + protected $_UTC_DateTimeZone; |
|
84 | + |
|
85 | + /** |
|
86 | + * @type DateTimeZone $_blog_DateTimeZone |
|
87 | + */ |
|
88 | + protected $_blog_DateTimeZone; |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
93 | + * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
94 | + * and time returned via getters. |
|
95 | + * |
|
96 | + * @var string |
|
97 | + */ |
|
98 | + protected $_date_time_output = ''; |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * timezone string |
|
103 | + * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
104 | + * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
105 | + * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
106 | + * |
|
107 | + * @var string |
|
108 | + */ |
|
109 | + protected $_timezone_string; |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
114 | + * offsets for comparison purposes). |
|
115 | + * |
|
116 | + * @var int |
|
117 | + */ |
|
118 | + protected $_blog_offset; |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * @param string $table_column |
|
123 | + * @param string $nice_name |
|
124 | + * @param bool $nullable |
|
125 | + * @param string $default_value |
|
126 | + * @param string $timezone_string |
|
127 | + * @param string $date_format |
|
128 | + * @param string $time_format |
|
129 | + * @param string $pretty_date_format |
|
130 | + * @param string $pretty_time_format |
|
131 | + */ |
|
132 | + public function __construct( |
|
133 | + string $table_column, |
|
134 | + string $nice_name, |
|
135 | + bool $nullable, |
|
136 | + string $default_value, |
|
137 | + string $timezone_string = '', |
|
138 | + string $date_format = '', |
|
139 | + string $time_format = '', |
|
140 | + string $pretty_date_format = '', |
|
141 | + string $pretty_time_format = '' |
|
142 | + ) { |
|
143 | + |
|
144 | + $this->_date_format = ! empty($date_format) |
|
145 | + ? $date_format |
|
146 | + : get_option('date_format'); |
|
147 | + $this->_time_format = ! empty($time_format) |
|
148 | + ? $time_format |
|
149 | + : get_option('time_format'); |
|
150 | + $this->_pretty_date_format = ! empty($pretty_date_format) |
|
151 | + ? $pretty_date_format |
|
152 | + : get_option('date_format'); |
|
153 | + $this->_pretty_time_format = ! empty($pretty_time_format) |
|
154 | + ? $pretty_time_format |
|
155 | + : get_option('time_format'); |
|
156 | + |
|
157 | + parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
158 | + $this->set_timezone($timezone_string); |
|
159 | + $this->setSchemaFormat('date-time'); |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + /** |
|
164 | + * @return DateTimeZone |
|
165 | + */ |
|
166 | + public function get_UTC_DateTimeZone(): DateTimeZone |
|
167 | + { |
|
168 | + return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
169 | + ? $this->_UTC_DateTimeZone |
|
170 | + : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
171 | + } |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * @return DateTimeZone |
|
176 | + */ |
|
177 | + public function get_blog_DateTimeZone(): DateTimeZone |
|
178 | + { |
|
179 | + return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
180 | + ? $this->_blog_DateTimeZone |
|
181 | + : $this->_create_timezone_object_from_timezone_string(); |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + /** |
|
186 | + * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
|
187 | + * |
|
188 | + * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
|
189 | + * timestamp |
|
190 | + * @return DateTime |
|
191 | + */ |
|
192 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
193 | + { |
|
194 | + return $this->_get_date_object($value_inputted_for_field_on_model_object); |
|
195 | + } |
|
196 | + |
|
197 | + |
|
198 | + /** |
|
199 | + * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
200 | + * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
201 | + * |
|
202 | + * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
203 | + * @return string The final assembled format string. |
|
204 | + */ |
|
205 | + protected function _get_date_time_output(bool $pretty = false): string |
|
206 | + { |
|
207 | + $pretty = filter_var($pretty, FILTER_VALIDATE_BOOLEAN); |
|
208 | + switch ($this->_date_time_output) { |
|
209 | + case 'time': |
|
210 | + return $pretty |
|
211 | + ? $this->_pretty_time_format |
|
212 | + : $this->_time_format; |
|
213 | + |
|
214 | + case 'date': |
|
215 | + return $pretty |
|
216 | + ? $this->_pretty_date_format |
|
217 | + : $this->_date_format; |
|
218 | + |
|
219 | + default: |
|
220 | + return $pretty |
|
221 | + ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
222 | + : $this->_date_format . ' ' . $this->_time_format; |
|
223 | + } |
|
224 | + } |
|
225 | + |
|
226 | + |
|
227 | + /** |
|
228 | + * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
229 | + * returned (using the format properties) |
|
230 | + * |
|
231 | + * @param string $what acceptable values are 'time' or 'date'. |
|
232 | + * Any other value will be set but will always result |
|
233 | + * in both 'date' and 'time' being returned. |
|
234 | + * @return void |
|
235 | + */ |
|
236 | + public function set_date_time_output(string $what = '') |
|
237 | + { |
|
238 | + $this->_date_time_output = $what; |
|
239 | + } |
|
240 | + |
|
241 | + |
|
242 | + /** |
|
243 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
244 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
245 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
246 | + * We also set some other properties in this method. |
|
247 | + * |
|
248 | + * @param string $timezone_string A valid timezone string as described by @link |
|
249 | + * http://www.php.net/manual/en/timezones.php |
|
250 | + * @return void |
|
251 | + * @throws InvalidArgumentException |
|
252 | + * @throws InvalidDataTypeException |
|
253 | + * @throws InvalidInterfaceException |
|
254 | + */ |
|
255 | + public function set_timezone(string $timezone_string) |
|
256 | + { |
|
257 | + if (empty($timezone_string) && ! empty($this->_timezone_string)) { |
|
258 | + // leave the timezone AS-IS if we already have one and |
|
259 | + // the function arg didn't provide one |
|
260 | + return; |
|
261 | + } |
|
262 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
263 | + $this->_timezone_string = ! empty($timezone_string) |
|
264 | + ? $timezone_string |
|
265 | + : 'UTC'; |
|
266 | + $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
267 | + } |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * _create_timezone_object_from_timezone_name |
|
272 | + * |
|
273 | + * @param string $timezone_string |
|
274 | + * @return DateTimeZone |
|
275 | + * @throws InvalidArgumentException |
|
276 | + * @throws InvalidDataTypeException |
|
277 | + * @throws InvalidInterfaceException |
|
278 | + */ |
|
279 | + protected function _create_timezone_object_from_timezone_string(string $timezone_string = ''): DateTimeZone |
|
280 | + { |
|
281 | + return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
282 | + } |
|
283 | + |
|
284 | + |
|
285 | + /** |
|
286 | + * This just returns whatever is set for the current timezone. |
|
287 | + * |
|
288 | + * @return string timezone string |
|
289 | + */ |
|
290 | + public function get_timezone(): string |
|
291 | + { |
|
292 | + return $this->_timezone_string; |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * set the $_date_format property |
|
298 | + * |
|
299 | + * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
300 | + * @param bool $pretty Whether to set pretty format or not. |
|
301 | + * @return void |
|
302 | + */ |
|
303 | + public function set_date_format(string $format, bool $pretty = false) |
|
304 | + { |
|
305 | + if ($pretty) { |
|
306 | + $this->_pretty_date_format = $format; |
|
307 | + } else { |
|
308 | + $this->_date_format = $format; |
|
309 | + } |
|
310 | + } |
|
311 | + |
|
312 | + |
|
313 | + /** |
|
314 | + * return the $_date_format property value. |
|
315 | + * |
|
316 | + * @param bool $pretty Whether to get pretty format or not. |
|
317 | + * @return string |
|
318 | + */ |
|
319 | + public function get_date_format(bool $pretty = false): string |
|
320 | + { |
|
321 | + return $pretty |
|
322 | + ? $this->_pretty_date_format |
|
323 | + : $this->_date_format; |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * set the $_time_format property |
|
329 | + * |
|
330 | + * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
331 | + * @param bool $pretty Whether to set pretty format or not. |
|
332 | + * @return void |
|
333 | + */ |
|
334 | + public function set_time_format(string $format, bool $pretty = false) |
|
335 | + { |
|
336 | + if ($pretty) { |
|
337 | + $this->_pretty_time_format = $format; |
|
338 | + } else { |
|
339 | + $this->_time_format = $format; |
|
340 | + } |
|
341 | + } |
|
342 | + |
|
343 | + |
|
344 | + /** |
|
345 | + * return the $_time_format property value. |
|
346 | + * |
|
347 | + * @param bool $pretty Whether to get pretty format or not. |
|
348 | + * @return string |
|
349 | + */ |
|
350 | + public function get_time_format(bool $pretty = false): string |
|
351 | + { |
|
352 | + return $pretty |
|
353 | + ? $this->_pretty_time_format |
|
354 | + : $this->_time_format; |
|
355 | + } |
|
356 | + |
|
357 | + |
|
358 | + /** |
|
359 | + * set the $_pretty_date_format property |
|
360 | + * |
|
361 | + * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
362 | + * @return void |
|
363 | + */ |
|
364 | + public function set_pretty_date_format(string $format) |
|
365 | + { |
|
366 | + $this->_pretty_date_format = $format; |
|
367 | + } |
|
368 | + |
|
369 | + |
|
370 | + /** |
|
371 | + * set the $_pretty_time_format property |
|
372 | + * |
|
373 | + * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
374 | + * @return void |
|
375 | + */ |
|
376 | + public function set_pretty_time_format(string $format) |
|
377 | + { |
|
378 | + $this->_pretty_time_format = $format; |
|
379 | + } |
|
380 | + |
|
381 | + |
|
382 | + /** |
|
383 | + * Only sets the time portion of the datetime. |
|
384 | + * |
|
385 | + * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
386 | + * @param DateTime $current current DateTime object for the datetime field |
|
387 | + * @return DateTime |
|
388 | + */ |
|
389 | + public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current): DateTime |
|
390 | + { |
|
391 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
392 | + // Otherwise parse the string. |
|
393 | + if ($time_to_set_string instanceof DateTime) { |
|
394 | + $parsed = [ |
|
395 | + 'hour' => $time_to_set_string->format('H'), |
|
396 | + 'minute' => $time_to_set_string->format('i'), |
|
397 | + 'second' => $time_to_set_string->format('s'), |
|
398 | + ]; |
|
399 | + } else { |
|
400 | + // parse incoming string |
|
401 | + $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
402 | + } |
|
403 | + EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
404 | + return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
405 | + } |
|
406 | + |
|
407 | + |
|
408 | + /** |
|
409 | + * Only sets the date portion of the datetime. |
|
410 | + * |
|
411 | + * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
|
412 | + * @param DateTime $current current DateTime object for the datetime field |
|
413 | + * @return DateTime |
|
414 | + */ |
|
415 | + public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current): DateTime |
|
416 | + { |
|
417 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
418 | + // Otherwise parse the string. |
|
419 | + if ($date_to_set_string instanceof DateTime) { |
|
420 | + $parsed = [ |
|
421 | + 'year' => $date_to_set_string->format('Y'), |
|
422 | + 'month' => $date_to_set_string->format('m'), |
|
423 | + 'day' => $date_to_set_string->format('d'), |
|
424 | + ]; |
|
425 | + } else { |
|
426 | + // parse incoming string |
|
427 | + $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
428 | + } |
|
429 | + EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
430 | + return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
431 | + } |
|
432 | + |
|
433 | + |
|
434 | + /** |
|
435 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
|
436 | + * datetime gets to this stage it should ALREADY be in UTC time |
|
437 | + * |
|
438 | + * @param DateTime $DateTime |
|
439 | + * @return string formatted date time for given timezone |
|
440 | + * @throws EE_Error |
|
441 | + */ |
|
442 | + public function prepare_for_get($DateTime): string |
|
443 | + { |
|
444 | + return $this->_prepare_for_display($DateTime); |
|
445 | + } |
|
446 | + |
|
447 | + |
|
448 | + /** |
|
449 | + * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
450 | + * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
451 | + * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
452 | + * abbreviation to the date_string. |
|
453 | + * |
|
454 | + * @param DateTime $DateTime |
|
455 | + * @param string $schema |
|
456 | + * @return string |
|
457 | + * @throws EE_Error |
|
458 | + */ |
|
459 | + public function prepare_for_pretty_echoing($DateTime, string $schema = 'pretty'): string |
|
460 | + { |
|
461 | + return $this->_prepare_for_display($DateTime, $schema); |
|
462 | + } |
|
463 | + |
|
464 | + |
|
465 | + /** |
|
466 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
467 | + * timezone). |
|
468 | + * |
|
469 | + * @param DateTime $DateTime |
|
470 | + * @param string $schema |
|
471 | + * @return string |
|
472 | + * @throws EE_Error |
|
473 | + */ |
|
474 | + protected function _prepare_for_display(DateTime $DateTime, string $schema = ''): string |
|
475 | + { |
|
476 | + if (! $DateTime instanceof DateTime) { |
|
477 | + if ($this->_nullable) { |
|
478 | + return ''; |
|
479 | + } else { |
|
480 | + if (WP_DEBUG) { |
|
481 | + throw new EE_Error( |
|
482 | + sprintf( |
|
483 | + esc_html__( |
|
484 | + 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable.', |
|
485 | + 'event_espresso' |
|
486 | + ), |
|
487 | + $this->_nicename |
|
488 | + ) |
|
489 | + ); |
|
490 | + } else { |
|
491 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now); |
|
492 | + EE_Error::add_error( |
|
493 | + sprintf( |
|
494 | + esc_html__( |
|
495 | + 'EE_Datetime_Field::_prepare_for_display requires a DateTime class to be the value for the $DateTime argument because the %s field is not nullable. When WP_DEBUG is false, the value is set to "now" instead of throwing an exception.', |
|
496 | + 'event_espresso' |
|
497 | + ), |
|
498 | + $this->_nicename |
|
499 | + ) |
|
500 | + ); |
|
501 | + } |
|
502 | + } |
|
503 | + } |
|
504 | + $pretty = $schema === 'pretty'; |
|
505 | + $format_string = $this->_get_date_time_output($pretty); |
|
506 | + EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
507 | + |
|
508 | + $timezone_string = ''; |
|
509 | + if ($this->_display_timezone()) { |
|
510 | + // must be explicit because schema could equal true. |
|
511 | + $timezone_string = $schema === 'no_html' |
|
512 | + ? ' (' . $DateTime->format('T') . ')' |
|
513 | + : ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
514 | + } |
|
515 | + return $DateTime->format($format_string) . $timezone_string; |
|
516 | + } |
|
517 | + |
|
518 | + |
|
519 | + /** |
|
520 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
521 | + * timezone). |
|
522 | + * |
|
523 | + * @param DateTime|null $datetime_value |
|
524 | + * @return string mysql timestamp in UTC |
|
525 | + * @throws EE_Error |
|
526 | + */ |
|
527 | + public function prepare_for_use_in_db($datetime_value) |
|
528 | + { |
|
529 | + // we allow an empty value or DateTime object, but nothing else. |
|
530 | + if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
531 | + throw new EE_Error( |
|
532 | + sprintf( |
|
533 | + esc_html__( |
|
534 | + 'The incoming value being prepared for setting in the database must either be empty or a php |
|
535 | 535 | DateTime object, instead of: %1$s %2$s', |
536 | - 'event_espresso' |
|
537 | - ), |
|
538 | - '<br />', |
|
539 | - print_r($datetime_value, true) |
|
540 | - ) |
|
541 | - ); |
|
542 | - } |
|
543 | - |
|
544 | - if ($datetime_value instanceof DateTime) { |
|
545 | - if (! $datetime_value instanceof DbSafeDateTime) { |
|
546 | - $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
547 | - } |
|
548 | - EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
|
549 | - return $datetime_value->format( |
|
550 | - EE_Datetime_Field::mysql_timestamp_format |
|
551 | - ); |
|
552 | - } |
|
553 | - |
|
554 | - // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
555 | - return ! $this->_nullable && empty($datetime_value) |
|
556 | - ? current_time('mysql', true) |
|
557 | - : null; |
|
558 | - } |
|
559 | - |
|
560 | - |
|
561 | - /** |
|
562 | - * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
563 | - * allowed) |
|
564 | - * |
|
565 | - * @param string $datetime_string mysql timestamp in UTC |
|
566 | - * @return mixed null | DateTime |
|
567 | - * @throws EE_Error |
|
568 | - */ |
|
569 | - public function prepare_for_set_from_db($datetime_string) |
|
570 | - { |
|
571 | - // if $datetime_value is empty, and ! $this->_nullable, just use time() |
|
572 | - if (empty($datetime_string) && $this->_nullable) { |
|
573 | - return null; |
|
574 | - } |
|
575 | - // datetime strings from the db should ALWAYS be in UTC+0, so use UTC_DateTimeZone when creating |
|
576 | - if (empty($datetime_string)) { |
|
577 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
578 | - } else { |
|
579 | - $DateTime = DbSafeDateTime::createFromFormat( |
|
580 | - EE_Datetime_Field::mysql_timestamp_format, |
|
581 | - $datetime_string, |
|
582 | - $this->get_UTC_DateTimeZone() |
|
583 | - ); |
|
584 | - } |
|
585 | - |
|
586 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
587 | - // if still no datetime object, then let's just use now |
|
588 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
589 | - } |
|
590 | - // THEN apply the field's set DateTimeZone |
|
591 | - EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
592 | - return $DateTime; |
|
593 | - } |
|
594 | - |
|
595 | - |
|
596 | - /** |
|
597 | - * All this method does is determine if we're going to display the timezone string or not on any output. |
|
598 | - * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
599 | - * If so, then true. |
|
600 | - * |
|
601 | - * @return bool true for yes false for no |
|
602 | - * @throws EE_Error |
|
603 | - */ |
|
604 | - protected function _display_timezone() |
|
605 | - { |
|
606 | - |
|
607 | - // first let's do a comparison of timezone strings. |
|
608 | - // If they match then we can get out without any further calculations |
|
609 | - $blog_string = get_option('timezone_string'); |
|
610 | - if ($blog_string === $this->_timezone_string) { |
|
611 | - return false; |
|
612 | - } |
|
613 | - // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
614 | - $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
615 | - $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
616 | - // now compare |
|
617 | - return $blog_offset !== $this_offset; |
|
618 | - } |
|
619 | - |
|
620 | - |
|
621 | - /** |
|
622 | - * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
623 | - * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
624 | - * with. |
|
625 | - * |
|
626 | - * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
|
627 | - * in the format that is set on the date_field (or DateTime |
|
628 | - * object)! |
|
629 | - * @return DateTime |
|
630 | - */ |
|
631 | - protected function _get_date_object($date_string) |
|
632 | - { |
|
633 | - // first if this is an empty date_string and nullable is allowed, just return null. |
|
634 | - if ($this->_nullable && empty($date_string)) { |
|
635 | - return null; |
|
636 | - } |
|
637 | - |
|
638 | - // if incoming date |
|
639 | - if ($date_string instanceof DateTime) { |
|
640 | - EEH_DTT_Helper::setTimezone($date_string, $this->_DateTimeZone); |
|
641 | - return $date_string; |
|
642 | - } |
|
643 | - // if empty date_string and made it here. |
|
644 | - // Return a datetime object for now in the given timezone. |
|
645 | - if (empty($date_string)) { |
|
646 | - return new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
647 | - } |
|
648 | - // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
649 | - if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
650 | - try { |
|
651 | - // This is operating under the assumption that the incoming Unix timestamp |
|
652 | - // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
653 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
654 | - $DateTime->setTimestamp($date_string); |
|
655 | - |
|
656 | - return $DateTime; |
|
657 | - } catch (Exception $e) { |
|
658 | - // should be rare, but if things got fooled then let's just continue |
|
659 | - } |
|
660 | - } |
|
661 | - // not a unix timestamp. So we will use the set format on this object and set timezone to |
|
662 | - // create the DateTime object. |
|
663 | - $format = $this->_date_format . ' ' . $this->_time_format; |
|
664 | - try { |
|
665 | - $DateTime = DbSafeDateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
666 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
667 | - throw new EE_Error( |
|
668 | - sprintf( |
|
669 | - esc_html__('"%1$s" does not represent a valid Date Time in the format "%2$s".', |
|
670 | - 'event_espresso'), |
|
671 | - $date_string, |
|
672 | - $format |
|
673 | - ) |
|
674 | - ); |
|
675 | - } |
|
676 | - } catch (Exception $e) { |
|
677 | - // if we made it here then likely then something went really wrong. |
|
678 | - // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
679 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
680 | - } |
|
681 | - |
|
682 | - return $DateTime; |
|
683 | - } |
|
684 | - |
|
685 | - |
|
686 | - /** |
|
687 | - * get_timezone_transitions |
|
688 | - * |
|
689 | - * @param DateTimeZone $DateTimeZone |
|
690 | - * @param int $time |
|
691 | - * @param bool $first_only |
|
692 | - * @return mixed |
|
693 | - */ |
|
694 | - public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
|
695 | - { |
|
696 | - return EEH_DTT_Helper::get_timezone_transitions($DateTimeZone, $time, $first_only); |
|
697 | - } |
|
698 | - |
|
699 | - |
|
700 | - /** |
|
701 | - * get_timezone_offset |
|
702 | - * |
|
703 | - * @param DateTimeZone $DateTimeZone |
|
704 | - * @param int $time |
|
705 | - * @return mixed |
|
706 | - * @throws DomainException |
|
707 | - */ |
|
708 | - public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
709 | - { |
|
710 | - return EEH_DTT_Helper::get_timezone_offset($DateTimeZone, $time); |
|
711 | - } |
|
712 | - |
|
713 | - |
|
714 | - /** |
|
715 | - * This will take an incoming timezone string and return the abbreviation for that timezone |
|
716 | - * |
|
717 | - * @param string $timezone_string |
|
718 | - * @return string abbreviation |
|
719 | - * @throws Exception |
|
720 | - */ |
|
721 | - public function get_timezone_abbrev($timezone_string) |
|
722 | - { |
|
723 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
724 | - $dateTime = new DateTime(EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
725 | - |
|
726 | - return $dateTime->format('T'); |
|
727 | - } |
|
728 | - |
|
729 | - |
|
730 | - /** |
|
731 | - * Overrides the parent to allow for having a dynamic "now" value |
|
732 | - * |
|
733 | - * @return mixed |
|
734 | - */ |
|
735 | - public function get_default_value() |
|
736 | - { |
|
737 | - if ($this->_default_value === EE_Datetime_Field::now) { |
|
738 | - return time(); |
|
739 | - } else { |
|
740 | - return parent::get_default_value(); |
|
741 | - } |
|
742 | - } |
|
743 | - |
|
744 | - |
|
745 | - /** |
|
746 | - * Gets the default datetime object from the field's default time |
|
747 | - * |
|
748 | - * @return DbSafeDateTime|null |
|
749 | - * @throws InvalidArgumentException |
|
750 | - * @throws InvalidDataTypeException |
|
751 | - * @throws InvalidInterfaceException |
|
752 | - * @since 4.9.66.p |
|
753 | - */ |
|
754 | - public function getDefaultDateTimeObj() |
|
755 | - { |
|
756 | - $default_raw = $this->get_default_value(); |
|
757 | - if ($default_raw instanceof DateTime) { |
|
758 | - return $default_raw; |
|
759 | - } elseif (is_null($default_raw)) { |
|
760 | - return $default_raw; |
|
761 | - } else { |
|
762 | - return new DbSafeDateTime( |
|
763 | - $this->get_default_value(), |
|
764 | - EEH_DTT_Helper::get_valid_timezone_string($this->get_timezone()) |
|
765 | - ); |
|
766 | - } |
|
767 | - } |
|
768 | - |
|
769 | - |
|
770 | - public function getSchemaDescription() |
|
771 | - { |
|
772 | - return sprintf( |
|
773 | - esc_html__('%s - the value for this field is in the timezone of the site.', 'event_espresso'), |
|
774 | - $this->get_nicename() |
|
775 | - ); |
|
776 | - } |
|
536 | + 'event_espresso' |
|
537 | + ), |
|
538 | + '<br />', |
|
539 | + print_r($datetime_value, true) |
|
540 | + ) |
|
541 | + ); |
|
542 | + } |
|
543 | + |
|
544 | + if ($datetime_value instanceof DateTime) { |
|
545 | + if (! $datetime_value instanceof DbSafeDateTime) { |
|
546 | + $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
547 | + } |
|
548 | + EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
|
549 | + return $datetime_value->format( |
|
550 | + EE_Datetime_Field::mysql_timestamp_format |
|
551 | + ); |
|
552 | + } |
|
553 | + |
|
554 | + // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
555 | + return ! $this->_nullable && empty($datetime_value) |
|
556 | + ? current_time('mysql', true) |
|
557 | + : null; |
|
558 | + } |
|
559 | + |
|
560 | + |
|
561 | + /** |
|
562 | + * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
563 | + * allowed) |
|
564 | + * |
|
565 | + * @param string $datetime_string mysql timestamp in UTC |
|
566 | + * @return mixed null | DateTime |
|
567 | + * @throws EE_Error |
|
568 | + */ |
|
569 | + public function prepare_for_set_from_db($datetime_string) |
|
570 | + { |
|
571 | + // if $datetime_value is empty, and ! $this->_nullable, just use time() |
|
572 | + if (empty($datetime_string) && $this->_nullable) { |
|
573 | + return null; |
|
574 | + } |
|
575 | + // datetime strings from the db should ALWAYS be in UTC+0, so use UTC_DateTimeZone when creating |
|
576 | + if (empty($datetime_string)) { |
|
577 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
578 | + } else { |
|
579 | + $DateTime = DbSafeDateTime::createFromFormat( |
|
580 | + EE_Datetime_Field::mysql_timestamp_format, |
|
581 | + $datetime_string, |
|
582 | + $this->get_UTC_DateTimeZone() |
|
583 | + ); |
|
584 | + } |
|
585 | + |
|
586 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
587 | + // if still no datetime object, then let's just use now |
|
588 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
589 | + } |
|
590 | + // THEN apply the field's set DateTimeZone |
|
591 | + EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
592 | + return $DateTime; |
|
593 | + } |
|
594 | + |
|
595 | + |
|
596 | + /** |
|
597 | + * All this method does is determine if we're going to display the timezone string or not on any output. |
|
598 | + * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
599 | + * If so, then true. |
|
600 | + * |
|
601 | + * @return bool true for yes false for no |
|
602 | + * @throws EE_Error |
|
603 | + */ |
|
604 | + protected function _display_timezone() |
|
605 | + { |
|
606 | + |
|
607 | + // first let's do a comparison of timezone strings. |
|
608 | + // If they match then we can get out without any further calculations |
|
609 | + $blog_string = get_option('timezone_string'); |
|
610 | + if ($blog_string === $this->_timezone_string) { |
|
611 | + return false; |
|
612 | + } |
|
613 | + // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
614 | + $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
615 | + $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
616 | + // now compare |
|
617 | + return $blog_offset !== $this_offset; |
|
618 | + } |
|
619 | + |
|
620 | + |
|
621 | + /** |
|
622 | + * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
623 | + * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
624 | + * with. |
|
625 | + * |
|
626 | + * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
|
627 | + * in the format that is set on the date_field (or DateTime |
|
628 | + * object)! |
|
629 | + * @return DateTime |
|
630 | + */ |
|
631 | + protected function _get_date_object($date_string) |
|
632 | + { |
|
633 | + // first if this is an empty date_string and nullable is allowed, just return null. |
|
634 | + if ($this->_nullable && empty($date_string)) { |
|
635 | + return null; |
|
636 | + } |
|
637 | + |
|
638 | + // if incoming date |
|
639 | + if ($date_string instanceof DateTime) { |
|
640 | + EEH_DTT_Helper::setTimezone($date_string, $this->_DateTimeZone); |
|
641 | + return $date_string; |
|
642 | + } |
|
643 | + // if empty date_string and made it here. |
|
644 | + // Return a datetime object for now in the given timezone. |
|
645 | + if (empty($date_string)) { |
|
646 | + return new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
647 | + } |
|
648 | + // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
649 | + if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
650 | + try { |
|
651 | + // This is operating under the assumption that the incoming Unix timestamp |
|
652 | + // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
653 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
654 | + $DateTime->setTimestamp($date_string); |
|
655 | + |
|
656 | + return $DateTime; |
|
657 | + } catch (Exception $e) { |
|
658 | + // should be rare, but if things got fooled then let's just continue |
|
659 | + } |
|
660 | + } |
|
661 | + // not a unix timestamp. So we will use the set format on this object and set timezone to |
|
662 | + // create the DateTime object. |
|
663 | + $format = $this->_date_format . ' ' . $this->_time_format; |
|
664 | + try { |
|
665 | + $DateTime = DbSafeDateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
666 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
667 | + throw new EE_Error( |
|
668 | + sprintf( |
|
669 | + esc_html__('"%1$s" does not represent a valid Date Time in the format "%2$s".', |
|
670 | + 'event_espresso'), |
|
671 | + $date_string, |
|
672 | + $format |
|
673 | + ) |
|
674 | + ); |
|
675 | + } |
|
676 | + } catch (Exception $e) { |
|
677 | + // if we made it here then likely then something went really wrong. |
|
678 | + // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
679 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
680 | + } |
|
681 | + |
|
682 | + return $DateTime; |
|
683 | + } |
|
684 | + |
|
685 | + |
|
686 | + /** |
|
687 | + * get_timezone_transitions |
|
688 | + * |
|
689 | + * @param DateTimeZone $DateTimeZone |
|
690 | + * @param int $time |
|
691 | + * @param bool $first_only |
|
692 | + * @return mixed |
|
693 | + */ |
|
694 | + public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
|
695 | + { |
|
696 | + return EEH_DTT_Helper::get_timezone_transitions($DateTimeZone, $time, $first_only); |
|
697 | + } |
|
698 | + |
|
699 | + |
|
700 | + /** |
|
701 | + * get_timezone_offset |
|
702 | + * |
|
703 | + * @param DateTimeZone $DateTimeZone |
|
704 | + * @param int $time |
|
705 | + * @return mixed |
|
706 | + * @throws DomainException |
|
707 | + */ |
|
708 | + public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
709 | + { |
|
710 | + return EEH_DTT_Helper::get_timezone_offset($DateTimeZone, $time); |
|
711 | + } |
|
712 | + |
|
713 | + |
|
714 | + /** |
|
715 | + * This will take an incoming timezone string and return the abbreviation for that timezone |
|
716 | + * |
|
717 | + * @param string $timezone_string |
|
718 | + * @return string abbreviation |
|
719 | + * @throws Exception |
|
720 | + */ |
|
721 | + public function get_timezone_abbrev($timezone_string) |
|
722 | + { |
|
723 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
724 | + $dateTime = new DateTime(EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
725 | + |
|
726 | + return $dateTime->format('T'); |
|
727 | + } |
|
728 | + |
|
729 | + |
|
730 | + /** |
|
731 | + * Overrides the parent to allow for having a dynamic "now" value |
|
732 | + * |
|
733 | + * @return mixed |
|
734 | + */ |
|
735 | + public function get_default_value() |
|
736 | + { |
|
737 | + if ($this->_default_value === EE_Datetime_Field::now) { |
|
738 | + return time(); |
|
739 | + } else { |
|
740 | + return parent::get_default_value(); |
|
741 | + } |
|
742 | + } |
|
743 | + |
|
744 | + |
|
745 | + /** |
|
746 | + * Gets the default datetime object from the field's default time |
|
747 | + * |
|
748 | + * @return DbSafeDateTime|null |
|
749 | + * @throws InvalidArgumentException |
|
750 | + * @throws InvalidDataTypeException |
|
751 | + * @throws InvalidInterfaceException |
|
752 | + * @since 4.9.66.p |
|
753 | + */ |
|
754 | + public function getDefaultDateTimeObj() |
|
755 | + { |
|
756 | + $default_raw = $this->get_default_value(); |
|
757 | + if ($default_raw instanceof DateTime) { |
|
758 | + return $default_raw; |
|
759 | + } elseif (is_null($default_raw)) { |
|
760 | + return $default_raw; |
|
761 | + } else { |
|
762 | + return new DbSafeDateTime( |
|
763 | + $this->get_default_value(), |
|
764 | + EEH_DTT_Helper::get_valid_timezone_string($this->get_timezone()) |
|
765 | + ); |
|
766 | + } |
|
767 | + } |
|
768 | + |
|
769 | + |
|
770 | + public function getSchemaDescription() |
|
771 | + { |
|
772 | + return sprintf( |
|
773 | + esc_html__('%s - the value for this field is in the timezone of the site.', 'event_espresso'), |
|
774 | + $this->get_nicename() |
|
775 | + ); |
|
776 | + } |
|
777 | 777 | } |
@@ -218,8 +218,8 @@ discard block |
||
218 | 218 | |
219 | 219 | default: |
220 | 220 | return $pretty |
221 | - ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
222 | - : $this->_date_format . ' ' . $this->_time_format; |
|
221 | + ? $this->_pretty_date_format.' '.$this->_pretty_time_format |
|
222 | + : $this->_date_format.' '.$this->_time_format; |
|
223 | 223 | } |
224 | 224 | } |
225 | 225 | |
@@ -473,7 +473,7 @@ discard block |
||
473 | 473 | */ |
474 | 474 | protected function _prepare_for_display(DateTime $DateTime, string $schema = ''): string |
475 | 475 | { |
476 | - if (! $DateTime instanceof DateTime) { |
|
476 | + if ( ! $DateTime instanceof DateTime) { |
|
477 | 477 | if ($this->_nullable) { |
478 | 478 | return ''; |
479 | 479 | } else { |
@@ -509,10 +509,10 @@ discard block |
||
509 | 509 | if ($this->_display_timezone()) { |
510 | 510 | // must be explicit because schema could equal true. |
511 | 511 | $timezone_string = $schema === 'no_html' |
512 | - ? ' (' . $DateTime->format('T') . ')' |
|
513 | - : ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
512 | + ? ' ('.$DateTime->format('T').')' |
|
513 | + : ' <span class="ee_dtt_timezone_string">('.$DateTime->format('T').')</span>'; |
|
514 | 514 | } |
515 | - return $DateTime->format($format_string) . $timezone_string; |
|
515 | + return $DateTime->format($format_string).$timezone_string; |
|
516 | 516 | } |
517 | 517 | |
518 | 518 | |
@@ -527,7 +527,7 @@ discard block |
||
527 | 527 | public function prepare_for_use_in_db($datetime_value) |
528 | 528 | { |
529 | 529 | // we allow an empty value or DateTime object, but nothing else. |
530 | - if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
530 | + if ( ! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
531 | 531 | throw new EE_Error( |
532 | 532 | sprintf( |
533 | 533 | esc_html__( |
@@ -542,7 +542,7 @@ discard block |
||
542 | 542 | } |
543 | 543 | |
544 | 544 | if ($datetime_value instanceof DateTime) { |
545 | - if (! $datetime_value instanceof DbSafeDateTime) { |
|
545 | + if ( ! $datetime_value instanceof DbSafeDateTime) { |
|
546 | 546 | $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
547 | 547 | } |
548 | 548 | EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
@@ -583,7 +583,7 @@ discard block |
||
583 | 583 | ); |
584 | 584 | } |
585 | 585 | |
586 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
586 | + if ( ! $DateTime instanceof DbSafeDateTime) { |
|
587 | 587 | // if still no datetime object, then let's just use now |
588 | 588 | $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
589 | 589 | } |
@@ -660,10 +660,10 @@ discard block |
||
660 | 660 | } |
661 | 661 | // not a unix timestamp. So we will use the set format on this object and set timezone to |
662 | 662 | // create the DateTime object. |
663 | - $format = $this->_date_format . ' ' . $this->_time_format; |
|
663 | + $format = $this->_date_format.' '.$this->_time_format; |
|
664 | 664 | try { |
665 | 665 | $DateTime = DbSafeDateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
666 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
666 | + if ( ! $DateTime instanceof DbSafeDateTime) { |
|
667 | 667 | throw new EE_Error( |
668 | 668 | sprintf( |
669 | 669 | esc_html__('"%1$s" does not represent a valid Date Time in the format "%2$s".', |
@@ -6,14 +6,14 @@ |
||
6 | 6 | */ |
7 | 7 | class EE_All_Caps_Text_Field extends EE_Text_Field_Base |
8 | 8 | { |
9 | - /** |
|
10 | - * makes it all upper case, and key-like |
|
11 | - * |
|
12 | - * @param string $value_inputted_for_field_on_model_object |
|
13 | - * @return string |
|
14 | - */ |
|
15 | - public function prepare_for_set($value_inputted_for_field_on_model_object): string |
|
16 | - { |
|
17 | - return strtoupper(sanitize_key($value_inputted_for_field_on_model_object)); |
|
18 | - } |
|
9 | + /** |
|
10 | + * makes it all upper case, and key-like |
|
11 | + * |
|
12 | + * @param string $value_inputted_for_field_on_model_object |
|
13 | + * @return string |
|
14 | + */ |
|
15 | + public function prepare_for_set($value_inputted_for_field_on_model_object): string |
|
16 | + { |
|
17 | + return strtoupper(sanitize_key($value_inputted_for_field_on_model_object)); |
|
18 | + } |
|
19 | 19 | } |