@@ -133,9 +133,9 @@ discard block |
||
133 | 133 | |
134 | 134 | /** |
135 | 135 | * @param $other_table |
136 | - * @param $other_table_alias |
|
136 | + * @param string $other_table_alias |
|
137 | 137 | * @param $other_table_column |
138 | - * @param $this_table_alias |
|
138 | + * @param string $this_table_alias |
|
139 | 139 | * @param $this_table_join_column |
140 | 140 | * @param string $extra_join_sql |
141 | 141 | * @return string |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | * Alters the $query_params to disable default where conditions, unless otherwise specified |
190 | 190 | * |
191 | 191 | * @param string $query_params |
192 | - * @return array |
|
192 | + * @return string |
|
193 | 193 | */ |
194 | 194 | protected function _disable_default_where_conditions_on_query_param($query_params) |
195 | 195 | { |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
207 | 207 | * model objects will only be soft-deleted. |
208 | 208 | * |
209 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
209 | + * @param EE_Base_Class|null $model_object_or_id |
|
210 | 210 | * @param array $query_params |
211 | 211 | * @return int of how many related models got deleted |
212 | 212 | * @throws \EE_Error |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
238 | 238 | * model objects will only be soft-deleted. |
239 | 239 | * |
240 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
240 | + * @param EE_Base_Class|null $model_object_or_id |
|
241 | 241 | * @param array $query_params |
242 | 242 | * @return int of how many related models got deleted |
243 | 243 | * @throws \EE_Error |
@@ -15,502 +15,502 @@ |
||
15 | 15 | */ |
16 | 16 | abstract class EE_Model_Relation_Base implements HasSchemaInterface |
17 | 17 | { |
18 | - /** |
|
19 | - * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
|
20 | - * |
|
21 | - * @var string eg Event, Question_Group, Registration |
|
22 | - */ |
|
23 | - private $_this_model_name; |
|
24 | - /** |
|
25 | - * The model name pointed to by this relation (ie, the model we want to establish a relationship to) |
|
26 | - * |
|
27 | - * @var string eg Event, Question_Group, Registration |
|
28 | - */ |
|
29 | - private $_other_model_name; |
|
30 | - |
|
31 | - /** |
|
32 | - * this is typically used when calling the relation models to make sure they inherit any set timezone from the |
|
33 | - * initiating model. |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - protected $_timezone; |
|
38 | - |
|
39 | - /** |
|
40 | - * If you try to delete "this_model", and there are related "other_models", |
|
41 | - * and this isn't null, then abandon the deletion and add this warning. |
|
42 | - * This effectively makes it impossible to delete "this_model" while there are |
|
43 | - * related "other_models" along this relation. |
|
44 | - * |
|
45 | - * @var string (internationalized) |
|
46 | - */ |
|
47 | - protected $_blocking_delete_error_message; |
|
48 | - |
|
49 | - protected $_blocking_delete = false; |
|
50 | - |
|
51 | - /** |
|
52 | - * Object representing the relationship between two models. This knows how to join the models, |
|
53 | - * get related models across the relation, and add-and-remove the relationships. |
|
54 | - * |
|
55 | - * @param boolean $block_deletes if there are related models across this relation, block (prevent |
|
56 | - * and add an error) the deletion of this model |
|
57 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
58 | - * default |
|
59 | - */ |
|
60 | - public function __construct($block_deletes, $blocking_delete_error_message) |
|
61 | - { |
|
62 | - $this->_blocking_delete = $block_deletes; |
|
63 | - $this->_blocking_delete_error_message = $blocking_delete_error_message; |
|
64 | - } |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * @param $this_model_name |
|
69 | - * @param $other_model_name |
|
70 | - * @throws EE_Error |
|
71 | - */ |
|
72 | - public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
73 | - { |
|
74 | - $this->_this_model_name = $this_model_name; |
|
75 | - $this->_other_model_name = $other_model_name; |
|
76 | - if (is_string($this->_blocking_delete)) { |
|
77 | - throw new EE_Error(sprintf( |
|
78 | - __( |
|
79 | - "When instantiating the relation of type %s from %s to %s, the \$block_deletes argument should be a boolean, not a string (%s)", |
|
80 | - "event_espresso" |
|
81 | - ), |
|
82 | - get_class($this), |
|
83 | - $this_model_name, |
|
84 | - $other_model_name, |
|
85 | - $this->_blocking_delete |
|
86 | - )); |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * Gets the model where this relation is defined. |
|
93 | - * |
|
94 | - * @return EEM_Base |
|
95 | - */ |
|
96 | - public function get_this_model() |
|
97 | - { |
|
98 | - return $this->_get_model($this->_this_model_name); |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * Gets the model which this relation establishes the relation TO (ie, |
|
104 | - * this relation object was defined on get_this_model(), get_other_model() is the other one) |
|
105 | - * |
|
106 | - * @return EEM_Base |
|
107 | - */ |
|
108 | - public function get_other_model() |
|
109 | - { |
|
110 | - return $this->_get_model($this->_other_model_name); |
|
111 | - } |
|
112 | - |
|
113 | - |
|
114 | - /** |
|
115 | - * Internally used by get_this_model() and get_other_model() |
|
116 | - * |
|
117 | - * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
118 | - * @return EEM_Base |
|
119 | - */ |
|
120 | - protected function _get_model($model_name) |
|
121 | - { |
|
122 | - $modelInstance = EE_Registry::instance()->load_model($model_name); |
|
123 | - $modelInstance->set_timezone($this->_timezone); |
|
124 | - return $modelInstance; |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * entirely possible that relations may be called from a model and we need to make sure those relations have their |
|
130 | - * timezone set correctly. |
|
131 | - * |
|
132 | - * @param string $timezone timezone to set. |
|
133 | - */ |
|
134 | - public function set_timezone($timezone) |
|
135 | - { |
|
136 | - if ($timezone !== null) { |
|
137 | - $this->_timezone = $timezone; |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * @param $other_table |
|
144 | - * @param $other_table_alias |
|
145 | - * @param $other_table_column |
|
146 | - * @param $this_table_alias |
|
147 | - * @param $this_table_join_column |
|
148 | - * @param string $extra_join_sql |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - protected function _left_join( |
|
152 | - $other_table, |
|
153 | - $other_table_alias, |
|
154 | - $other_table_column, |
|
155 | - $this_table_alias, |
|
156 | - $this_table_join_column, |
|
157 | - $extra_join_sql = '' |
|
158 | - ) { |
|
159 | - return " LEFT JOIN " . $other_table . " AS " . $other_table_alias . " ON " . $other_table_alias . "." . $other_table_column . "=" . $this_table_alias . "." . $this_table_join_column . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - /** |
|
164 | - * Gets all the model objects of type of other model related to $model_object, |
|
165 | - * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
|
166 | - * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
|
167 | - * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
|
168 | - * EE_Belongs_To_Relation doesn't need to be saved before querying. |
|
169 | - * |
|
170 | - * @param EE_Base_Class|int $model_object_or_id or the primary key of this model |
|
171 | - * @param array $query_params like EEM_Base::get_all's $query_params |
|
172 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
173 | - * @return EE_Base_Class[] |
|
174 | - * @throws \EE_Error |
|
175 | - */ |
|
176 | - public function get_all_related( |
|
177 | - $model_object_or_id, |
|
178 | - $query_params = array(), |
|
179 | - $values_already_prepared_by_model_object = false |
|
180 | - ) { |
|
181 | - if ($values_already_prepared_by_model_object !== false) { |
|
182 | - EE_Error::doing_it_wrong( |
|
183 | - 'EE_Model_Relation_Base::get_all_related', |
|
184 | - __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
185 | - '4.8.1' |
|
186 | - ); |
|
187 | - } |
|
188 | - $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
189 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() |
|
190 | - . "." |
|
191 | - . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
192 | - $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
193 | - $query_params[0][ $query_param_where_this_model_pk ] = $model_object_id; |
|
194 | - return $this->get_other_model()->get_all($query_params); |
|
195 | - } |
|
196 | - |
|
197 | - |
|
198 | - /** |
|
199 | - * Alters the $query_params to disable default where conditions, unless otherwise specified |
|
200 | - * |
|
201 | - * @param string $query_params |
|
202 | - * @return array |
|
203 | - */ |
|
204 | - protected function _disable_default_where_conditions_on_query_param($query_params) |
|
205 | - { |
|
206 | - if (! isset($query_params['default_where_conditions'])) { |
|
207 | - $query_params['default_where_conditions'] = 'none'; |
|
208 | - } |
|
209 | - return $query_params; |
|
210 | - } |
|
211 | - |
|
212 | - |
|
213 | - /** |
|
214 | - * Deletes the related model objects which meet the query parameters. If no |
|
215 | - * parameters are specified, then all related model objects will be deleted. |
|
216 | - * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
217 | - * model objects will only be soft-deleted. |
|
218 | - * |
|
219 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
220 | - * @param array $query_params |
|
221 | - * @return int of how many related models got deleted |
|
222 | - * @throws \EE_Error |
|
223 | - */ |
|
224 | - public function delete_all_related($model_object_or_id, $query_params = array()) |
|
225 | - { |
|
226 | - // for each thing we would delete, |
|
227 | - $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
228 | - // determine if it's blocked by anything else before it can be deleted |
|
229 | - $deleted_count = 0; |
|
230 | - foreach ($related_model_objects as $related_model_object) { |
|
231 | - $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
232 | - $related_model_object, |
|
233 | - $model_object_or_id |
|
234 | - ); |
|
235 | - /* @var $model_object_or_id EE_Base_Class */ |
|
236 | - if (! $delete_is_blocked) { |
|
237 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
238 | - $related_model_object->delete(); |
|
239 | - $deleted_count++; |
|
240 | - } |
|
241 | - } |
|
242 | - return $deleted_count; |
|
243 | - } |
|
244 | - |
|
245 | - |
|
246 | - /** |
|
247 | - * Deletes the related model objects which meet the query parameters. If no |
|
248 | - * parameters are specified, then all related model objects will be deleted. |
|
249 | - * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
250 | - * model objects will only be soft-deleted. |
|
251 | - * |
|
252 | - * @param EE_Base_Class|int|string $model_object_or_id |
|
253 | - * @param array $query_params |
|
254 | - * @return int of how many related models got deleted |
|
255 | - * @throws \EE_Error |
|
256 | - */ |
|
257 | - public function delete_related_permanently($model_object_or_id, $query_params = array()) |
|
258 | - { |
|
259 | - // for each thing we would delete, |
|
260 | - $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
261 | - // determine if it's blocked by anything else before it can be deleted |
|
262 | - $deleted_count = 0; |
|
263 | - foreach ($related_model_objects as $related_model_object) { |
|
264 | - $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
265 | - $related_model_object, |
|
266 | - $model_object_or_id |
|
267 | - ); |
|
268 | - /* @var $model_object_or_id EE_Base_Class */ |
|
269 | - if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
|
270 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
271 | - $deleted_count++; |
|
272 | - if (! $delete_is_blocked) { |
|
273 | - $related_model_object->delete_permanently(); |
|
274 | - } else { |
|
275 | - // delete is blocked |
|
276 | - // brent and darren, in this case, wanted to just soft delete it then |
|
277 | - $related_model_object->delete(); |
|
278 | - } |
|
279 | - } else { |
|
280 | - // its not a soft-deletable thing anyways. do the normal logic. |
|
281 | - if (! $delete_is_blocked) { |
|
282 | - $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
283 | - $related_model_object->delete(); |
|
284 | - $deleted_count++; |
|
285 | - } |
|
286 | - } |
|
287 | - } |
|
288 | - return $deleted_count; |
|
289 | - } |
|
290 | - |
|
291 | - |
|
292 | - /** |
|
293 | - * this just returns a model_object_id for incoming item that could be an object or id. |
|
294 | - * |
|
295 | - * @param EE_Base_Class|int $model_object_or_id model object or the primary key of this model |
|
296 | - * @throws EE_Error |
|
297 | - * @return int |
|
298 | - */ |
|
299 | - protected function _get_model_object_id($model_object_or_id) |
|
300 | - { |
|
301 | - $model_object_id = $model_object_or_id; |
|
302 | - if ($model_object_or_id instanceof EE_Base_Class) { |
|
303 | - $model_object_id = $model_object_or_id->ID(); |
|
304 | - } |
|
305 | - if (! $model_object_id) { |
|
306 | - throw new EE_Error(sprintf( |
|
307 | - __( |
|
308 | - "Sorry, we cant get the related %s model objects to %s model object before it has an ID. You can solve that by just saving it before trying to get its related model objects", |
|
309 | - "event_espresso" |
|
310 | - ), |
|
311 | - $this->get_other_model()->get_this_model_name(), |
|
312 | - $this->get_this_model()->get_this_model_name() |
|
313 | - )); |
|
314 | - } |
|
315 | - return $model_object_id; |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * Gets the SQL string for performing the join between this model and the other model. |
|
321 | - * |
|
322 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
323 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
324 | - * other_model_primary_table.fk" etc |
|
325 | - */ |
|
326 | - abstract public function get_join_statement($model_relation_chain); |
|
327 | - |
|
328 | - |
|
329 | - /** |
|
330 | - * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
|
331 | - * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
|
332 | - * relationship only allows this model to be related to a single other model of this type) |
|
333 | - * |
|
334 | - * @param $this_obj_or_id |
|
335 | - * @param $other_obj_or_id |
|
336 | - * @param array $extra_join_model_fields_n_values |
|
337 | - * @return \EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
|
338 | - * $other_obj_or_id) |
|
339 | - */ |
|
340 | - abstract public function add_relation_to( |
|
341 | - $this_obj_or_id, |
|
342 | - $other_obj_or_id, |
|
343 | - $extra_join_model_fields_n_values = array() |
|
344 | - ); |
|
345 | - |
|
346 | - |
|
347 | - /** |
|
348 | - * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
|
349 | - * model objects |
|
350 | - * |
|
351 | - * @param $this_obj_or_id |
|
352 | - * @param $other_obj_or_id |
|
353 | - * @param array $where_query |
|
354 | - * @return bool |
|
355 | - */ |
|
356 | - abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()); |
|
357 | - |
|
358 | - |
|
359 | - /** |
|
360 | - * Removes ALL relation instances for this relation obj |
|
361 | - * |
|
362 | - * @param EE_Base_Class|int $this_obj_or_id |
|
363 | - * @param array $where_query_param like EEM_Base::get_all's $query_params[0] (where conditions) |
|
364 | - * @return EE_Base_Class[] |
|
365 | - * @throws \EE_Error |
|
366 | - */ |
|
367 | - public function remove_relations($this_obj_or_id, $where_query_param = array()) |
|
368 | - { |
|
369 | - $related_things = $this->get_all_related($this_obj_or_id, array($where_query_param)); |
|
370 | - $objs_removed = array(); |
|
371 | - foreach ($related_things as $related_thing) { |
|
372 | - $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing); |
|
373 | - } |
|
374 | - return $objs_removed; |
|
375 | - } |
|
376 | - |
|
377 | - |
|
378 | - /** |
|
379 | - * If you aren't allowed to delete this model when there are related models across this |
|
380 | - * relation object, return true. Otherwise, if you can delete this model even though |
|
381 | - * related objects exist, returns false. |
|
382 | - * |
|
383 | - * @return boolean |
|
384 | - */ |
|
385 | - public function block_delete_if_related_models_exist() |
|
386 | - { |
|
387 | - return $this->_blocking_delete; |
|
388 | - } |
|
389 | - |
|
390 | - |
|
391 | - /** |
|
392 | - * Gets the error message to show |
|
393 | - * |
|
394 | - * @return string |
|
395 | - */ |
|
396 | - public function get_deletion_error_message() |
|
397 | - { |
|
398 | - if ($this->_blocking_delete_error_message) { |
|
399 | - return $this->_blocking_delete_error_message; |
|
400 | - } else { |
|
18 | + /** |
|
19 | + * The model name of which this relation is a component (ie, the model that called new EE_Model_Relation_Base) |
|
20 | + * |
|
21 | + * @var string eg Event, Question_Group, Registration |
|
22 | + */ |
|
23 | + private $_this_model_name; |
|
24 | + /** |
|
25 | + * The model name pointed to by this relation (ie, the model we want to establish a relationship to) |
|
26 | + * |
|
27 | + * @var string eg Event, Question_Group, Registration |
|
28 | + */ |
|
29 | + private $_other_model_name; |
|
30 | + |
|
31 | + /** |
|
32 | + * this is typically used when calling the relation models to make sure they inherit any set timezone from the |
|
33 | + * initiating model. |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + protected $_timezone; |
|
38 | + |
|
39 | + /** |
|
40 | + * If you try to delete "this_model", and there are related "other_models", |
|
41 | + * and this isn't null, then abandon the deletion and add this warning. |
|
42 | + * This effectively makes it impossible to delete "this_model" while there are |
|
43 | + * related "other_models" along this relation. |
|
44 | + * |
|
45 | + * @var string (internationalized) |
|
46 | + */ |
|
47 | + protected $_blocking_delete_error_message; |
|
48 | + |
|
49 | + protected $_blocking_delete = false; |
|
50 | + |
|
51 | + /** |
|
52 | + * Object representing the relationship between two models. This knows how to join the models, |
|
53 | + * get related models across the relation, and add-and-remove the relationships. |
|
54 | + * |
|
55 | + * @param boolean $block_deletes if there are related models across this relation, block (prevent |
|
56 | + * and add an error) the deletion of this model |
|
57 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
58 | + * default |
|
59 | + */ |
|
60 | + public function __construct($block_deletes, $blocking_delete_error_message) |
|
61 | + { |
|
62 | + $this->_blocking_delete = $block_deletes; |
|
63 | + $this->_blocking_delete_error_message = $blocking_delete_error_message; |
|
64 | + } |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * @param $this_model_name |
|
69 | + * @param $other_model_name |
|
70 | + * @throws EE_Error |
|
71 | + */ |
|
72 | + public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
73 | + { |
|
74 | + $this->_this_model_name = $this_model_name; |
|
75 | + $this->_other_model_name = $other_model_name; |
|
76 | + if (is_string($this->_blocking_delete)) { |
|
77 | + throw new EE_Error(sprintf( |
|
78 | + __( |
|
79 | + "When instantiating the relation of type %s from %s to %s, the \$block_deletes argument should be a boolean, not a string (%s)", |
|
80 | + "event_espresso" |
|
81 | + ), |
|
82 | + get_class($this), |
|
83 | + $this_model_name, |
|
84 | + $other_model_name, |
|
85 | + $this->_blocking_delete |
|
86 | + )); |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * Gets the model where this relation is defined. |
|
93 | + * |
|
94 | + * @return EEM_Base |
|
95 | + */ |
|
96 | + public function get_this_model() |
|
97 | + { |
|
98 | + return $this->_get_model($this->_this_model_name); |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * Gets the model which this relation establishes the relation TO (ie, |
|
104 | + * this relation object was defined on get_this_model(), get_other_model() is the other one) |
|
105 | + * |
|
106 | + * @return EEM_Base |
|
107 | + */ |
|
108 | + public function get_other_model() |
|
109 | + { |
|
110 | + return $this->_get_model($this->_other_model_name); |
|
111 | + } |
|
112 | + |
|
113 | + |
|
114 | + /** |
|
115 | + * Internally used by get_this_model() and get_other_model() |
|
116 | + * |
|
117 | + * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
118 | + * @return EEM_Base |
|
119 | + */ |
|
120 | + protected function _get_model($model_name) |
|
121 | + { |
|
122 | + $modelInstance = EE_Registry::instance()->load_model($model_name); |
|
123 | + $modelInstance->set_timezone($this->_timezone); |
|
124 | + return $modelInstance; |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * entirely possible that relations may be called from a model and we need to make sure those relations have their |
|
130 | + * timezone set correctly. |
|
131 | + * |
|
132 | + * @param string $timezone timezone to set. |
|
133 | + */ |
|
134 | + public function set_timezone($timezone) |
|
135 | + { |
|
136 | + if ($timezone !== null) { |
|
137 | + $this->_timezone = $timezone; |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * @param $other_table |
|
144 | + * @param $other_table_alias |
|
145 | + * @param $other_table_column |
|
146 | + * @param $this_table_alias |
|
147 | + * @param $this_table_join_column |
|
148 | + * @param string $extra_join_sql |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + protected function _left_join( |
|
152 | + $other_table, |
|
153 | + $other_table_alias, |
|
154 | + $other_table_column, |
|
155 | + $this_table_alias, |
|
156 | + $this_table_join_column, |
|
157 | + $extra_join_sql = '' |
|
158 | + ) { |
|
159 | + return " LEFT JOIN " . $other_table . " AS " . $other_table_alias . " ON " . $other_table_alias . "." . $other_table_column . "=" . $this_table_alias . "." . $this_table_join_column . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + /** |
|
164 | + * Gets all the model objects of type of other model related to $model_object, |
|
165 | + * according to this relation. This is the same code for EE_HABTM_Relation and EE_Has_Many_Relation. |
|
166 | + * For both of those child classes, $model_object must be saved so that it has an ID before querying, |
|
167 | + * otherwise an error will be thrown. Note: by default we disable default_where_conditions |
|
168 | + * EE_Belongs_To_Relation doesn't need to be saved before querying. |
|
169 | + * |
|
170 | + * @param EE_Base_Class|int $model_object_or_id or the primary key of this model |
|
171 | + * @param array $query_params like EEM_Base::get_all's $query_params |
|
172 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
173 | + * @return EE_Base_Class[] |
|
174 | + * @throws \EE_Error |
|
175 | + */ |
|
176 | + public function get_all_related( |
|
177 | + $model_object_or_id, |
|
178 | + $query_params = array(), |
|
179 | + $values_already_prepared_by_model_object = false |
|
180 | + ) { |
|
181 | + if ($values_already_prepared_by_model_object !== false) { |
|
182 | + EE_Error::doing_it_wrong( |
|
183 | + 'EE_Model_Relation_Base::get_all_related', |
|
184 | + __('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
185 | + '4.8.1' |
|
186 | + ); |
|
187 | + } |
|
188 | + $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
189 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() |
|
190 | + . "." |
|
191 | + . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
192 | + $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
193 | + $query_params[0][ $query_param_where_this_model_pk ] = $model_object_id; |
|
194 | + return $this->get_other_model()->get_all($query_params); |
|
195 | + } |
|
196 | + |
|
197 | + |
|
198 | + /** |
|
199 | + * Alters the $query_params to disable default where conditions, unless otherwise specified |
|
200 | + * |
|
201 | + * @param string $query_params |
|
202 | + * @return array |
|
203 | + */ |
|
204 | + protected function _disable_default_where_conditions_on_query_param($query_params) |
|
205 | + { |
|
206 | + if (! isset($query_params['default_where_conditions'])) { |
|
207 | + $query_params['default_where_conditions'] = 'none'; |
|
208 | + } |
|
209 | + return $query_params; |
|
210 | + } |
|
211 | + |
|
212 | + |
|
213 | + /** |
|
214 | + * Deletes the related model objects which meet the query parameters. If no |
|
215 | + * parameters are specified, then all related model objects will be deleted. |
|
216 | + * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
217 | + * model objects will only be soft-deleted. |
|
218 | + * |
|
219 | + * @param EE_Base_Class|int|string $model_object_or_id |
|
220 | + * @param array $query_params |
|
221 | + * @return int of how many related models got deleted |
|
222 | + * @throws \EE_Error |
|
223 | + */ |
|
224 | + public function delete_all_related($model_object_or_id, $query_params = array()) |
|
225 | + { |
|
226 | + // for each thing we would delete, |
|
227 | + $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
228 | + // determine if it's blocked by anything else before it can be deleted |
|
229 | + $deleted_count = 0; |
|
230 | + foreach ($related_model_objects as $related_model_object) { |
|
231 | + $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
232 | + $related_model_object, |
|
233 | + $model_object_or_id |
|
234 | + ); |
|
235 | + /* @var $model_object_or_id EE_Base_Class */ |
|
236 | + if (! $delete_is_blocked) { |
|
237 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
238 | + $related_model_object->delete(); |
|
239 | + $deleted_count++; |
|
240 | + } |
|
241 | + } |
|
242 | + return $deleted_count; |
|
243 | + } |
|
244 | + |
|
245 | + |
|
246 | + /** |
|
247 | + * Deletes the related model objects which meet the query parameters. If no |
|
248 | + * parameters are specified, then all related model objects will be deleted. |
|
249 | + * Note: If the related model is extends EEM_Soft_Delete_Base, then the related |
|
250 | + * model objects will only be soft-deleted. |
|
251 | + * |
|
252 | + * @param EE_Base_Class|int|string $model_object_or_id |
|
253 | + * @param array $query_params |
|
254 | + * @return int of how many related models got deleted |
|
255 | + * @throws \EE_Error |
|
256 | + */ |
|
257 | + public function delete_related_permanently($model_object_or_id, $query_params = array()) |
|
258 | + { |
|
259 | + // for each thing we would delete, |
|
260 | + $related_model_objects = $this->get_all_related($model_object_or_id, $query_params); |
|
261 | + // determine if it's blocked by anything else before it can be deleted |
|
262 | + $deleted_count = 0; |
|
263 | + foreach ($related_model_objects as $related_model_object) { |
|
264 | + $delete_is_blocked = $this->get_other_model()->delete_is_blocked_by_related_models( |
|
265 | + $related_model_object, |
|
266 | + $model_object_or_id |
|
267 | + ); |
|
268 | + /* @var $model_object_or_id EE_Base_Class */ |
|
269 | + if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
|
270 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
271 | + $deleted_count++; |
|
272 | + if (! $delete_is_blocked) { |
|
273 | + $related_model_object->delete_permanently(); |
|
274 | + } else { |
|
275 | + // delete is blocked |
|
276 | + // brent and darren, in this case, wanted to just soft delete it then |
|
277 | + $related_model_object->delete(); |
|
278 | + } |
|
279 | + } else { |
|
280 | + // its not a soft-deletable thing anyways. do the normal logic. |
|
281 | + if (! $delete_is_blocked) { |
|
282 | + $this->remove_relation_to($model_object_or_id, $related_model_object); |
|
283 | + $related_model_object->delete(); |
|
284 | + $deleted_count++; |
|
285 | + } |
|
286 | + } |
|
287 | + } |
|
288 | + return $deleted_count; |
|
289 | + } |
|
290 | + |
|
291 | + |
|
292 | + /** |
|
293 | + * this just returns a model_object_id for incoming item that could be an object or id. |
|
294 | + * |
|
295 | + * @param EE_Base_Class|int $model_object_or_id model object or the primary key of this model |
|
296 | + * @throws EE_Error |
|
297 | + * @return int |
|
298 | + */ |
|
299 | + protected function _get_model_object_id($model_object_or_id) |
|
300 | + { |
|
301 | + $model_object_id = $model_object_or_id; |
|
302 | + if ($model_object_or_id instanceof EE_Base_Class) { |
|
303 | + $model_object_id = $model_object_or_id->ID(); |
|
304 | + } |
|
305 | + if (! $model_object_id) { |
|
306 | + throw new EE_Error(sprintf( |
|
307 | + __( |
|
308 | + "Sorry, we cant get the related %s model objects to %s model object before it has an ID. You can solve that by just saving it before trying to get its related model objects", |
|
309 | + "event_espresso" |
|
310 | + ), |
|
311 | + $this->get_other_model()->get_this_model_name(), |
|
312 | + $this->get_this_model()->get_this_model_name() |
|
313 | + )); |
|
314 | + } |
|
315 | + return $model_object_id; |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * Gets the SQL string for performing the join between this model and the other model. |
|
321 | + * |
|
322 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
323 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
324 | + * other_model_primary_table.fk" etc |
|
325 | + */ |
|
326 | + abstract public function get_join_statement($model_relation_chain); |
|
327 | + |
|
328 | + |
|
329 | + /** |
|
330 | + * Adds a relationships between the two model objects provided. Each type of relationship handles this differently |
|
331 | + * (EE_Belongs_To is a slight exception, it should more accurately be called set_relation_to(...), as this |
|
332 | + * relationship only allows this model to be related to a single other model of this type) |
|
333 | + * |
|
334 | + * @param $this_obj_or_id |
|
335 | + * @param $other_obj_or_id |
|
336 | + * @param array $extra_join_model_fields_n_values |
|
337 | + * @return \EE_Base_Class the EE_Base_Class which was added as a relation. (Convenient if you only pass an ID for |
|
338 | + * $other_obj_or_id) |
|
339 | + */ |
|
340 | + abstract public function add_relation_to( |
|
341 | + $this_obj_or_id, |
|
342 | + $other_obj_or_id, |
|
343 | + $extra_join_model_fields_n_values = array() |
|
344 | + ); |
|
345 | + |
|
346 | + |
|
347 | + /** |
|
348 | + * Similar to 'add_relation_to(...)', performs the opposite action of removing the relationship between the two |
|
349 | + * model objects |
|
350 | + * |
|
351 | + * @param $this_obj_or_id |
|
352 | + * @param $other_obj_or_id |
|
353 | + * @param array $where_query |
|
354 | + * @return bool |
|
355 | + */ |
|
356 | + abstract public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()); |
|
357 | + |
|
358 | + |
|
359 | + /** |
|
360 | + * Removes ALL relation instances for this relation obj |
|
361 | + * |
|
362 | + * @param EE_Base_Class|int $this_obj_or_id |
|
363 | + * @param array $where_query_param like EEM_Base::get_all's $query_params[0] (where conditions) |
|
364 | + * @return EE_Base_Class[] |
|
365 | + * @throws \EE_Error |
|
366 | + */ |
|
367 | + public function remove_relations($this_obj_or_id, $where_query_param = array()) |
|
368 | + { |
|
369 | + $related_things = $this->get_all_related($this_obj_or_id, array($where_query_param)); |
|
370 | + $objs_removed = array(); |
|
371 | + foreach ($related_things as $related_thing) { |
|
372 | + $objs_removed[] = $this->remove_relation_to($this_obj_or_id, $related_thing); |
|
373 | + } |
|
374 | + return $objs_removed; |
|
375 | + } |
|
376 | + |
|
377 | + |
|
378 | + /** |
|
379 | + * If you aren't allowed to delete this model when there are related models across this |
|
380 | + * relation object, return true. Otherwise, if you can delete this model even though |
|
381 | + * related objects exist, returns false. |
|
382 | + * |
|
383 | + * @return boolean |
|
384 | + */ |
|
385 | + public function block_delete_if_related_models_exist() |
|
386 | + { |
|
387 | + return $this->_blocking_delete; |
|
388 | + } |
|
389 | + |
|
390 | + |
|
391 | + /** |
|
392 | + * Gets the error message to show |
|
393 | + * |
|
394 | + * @return string |
|
395 | + */ |
|
396 | + public function get_deletion_error_message() |
|
397 | + { |
|
398 | + if ($this->_blocking_delete_error_message) { |
|
399 | + return $this->_blocking_delete_error_message; |
|
400 | + } else { |
|
401 | 401 | // return sprintf(__('Cannot delete %1$s when there are related %2$s', "event_espresso"),$this->get_this_model()->item_name(2),$this->get_other_model()->item_name(2)); |
402 | - return sprintf( |
|
403 | - __( |
|
404 | - 'This %1$s is currently linked to one or more %2$s records. If this %1$s is incorrect, then please remove it from all %3$s before attempting to delete it.', |
|
405 | - "event_espresso" |
|
406 | - ), |
|
407 | - $this->get_this_model()->item_name(1), |
|
408 | - $this->get_other_model()->item_name(1), |
|
409 | - $this->get_other_model()->item_name(2) |
|
410 | - ); |
|
411 | - } |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Returns whatever is set as the nicename for the object. |
|
416 | - * |
|
417 | - * @return string |
|
418 | - */ |
|
419 | - public function getSchemaDescription() |
|
420 | - { |
|
421 | - $description = $this instanceof EE_Belongs_To_Relation |
|
422 | - ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
|
423 | - : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
|
424 | - return sprintf( |
|
425 | - $description, |
|
426 | - $this->get_other_model()->get_this_model_name(), |
|
427 | - $this->get_this_model()->get_this_model_name() |
|
428 | - ); |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Returns whatever is set as the $_schema_type property for the object. |
|
433 | - * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
434 | - * |
|
435 | - * @return string|array |
|
436 | - */ |
|
437 | - public function getSchemaType() |
|
438 | - { |
|
439 | - return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
444 | - * this method and return the properties for the schema. |
|
445 | - * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
446 | - * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
447 | - * |
|
448 | - * @return array |
|
449 | - */ |
|
450 | - public function getSchemaProperties() |
|
451 | - { |
|
452 | - return array(); |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * If a child class has enum values, they should override this method and provide a simple array |
|
457 | - * of the enum values. |
|
458 | - * The reason this is not a property on the class is because there may be filterable enum values that |
|
459 | - * are set on the instantiated object that could be filtered after construct. |
|
460 | - * |
|
461 | - * @return array |
|
462 | - */ |
|
463 | - public function getSchemaEnum() |
|
464 | - { |
|
465 | - return array(); |
|
466 | - } |
|
467 | - |
|
468 | - /** |
|
469 | - * This returns the value of the $_schema_format property on the object. |
|
470 | - * |
|
471 | - * @return string |
|
472 | - */ |
|
473 | - public function getSchemaFormat() |
|
474 | - { |
|
475 | - return array(); |
|
476 | - } |
|
477 | - |
|
478 | - /** |
|
479 | - * This returns the value of the $_schema_readonly property on the object. |
|
480 | - * |
|
481 | - * @return bool |
|
482 | - */ |
|
483 | - public function getSchemaReadonly() |
|
484 | - { |
|
485 | - return true; |
|
486 | - } |
|
487 | - |
|
488 | - /** |
|
489 | - * This returns elements used to represent this field in the json schema. |
|
490 | - * |
|
491 | - * @link http://json-schema.org/ |
|
492 | - * @return array |
|
493 | - */ |
|
494 | - public function getSchema() |
|
495 | - { |
|
496 | - $schema = array( |
|
497 | - 'description' => $this->getSchemaDescription(), |
|
498 | - 'type' => $this->getSchemaType(), |
|
499 | - 'relation' => true, |
|
500 | - 'relation_type' => get_class($this), |
|
501 | - 'readonly' => $this->getSchemaReadonly() |
|
502 | - ); |
|
503 | - |
|
504 | - if ($this instanceof EE_HABTM_Relation) { |
|
505 | - $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name(); |
|
506 | - } |
|
507 | - |
|
508 | - if ($this->getSchemaType() === 'array') { |
|
509 | - $schema['items'] = array( |
|
510 | - 'type' => 'object' |
|
511 | - ); |
|
512 | - } |
|
513 | - |
|
514 | - return $schema; |
|
515 | - } |
|
402 | + return sprintf( |
|
403 | + __( |
|
404 | + 'This %1$s is currently linked to one or more %2$s records. If this %1$s is incorrect, then please remove it from all %3$s before attempting to delete it.', |
|
405 | + "event_espresso" |
|
406 | + ), |
|
407 | + $this->get_this_model()->item_name(1), |
|
408 | + $this->get_other_model()->item_name(1), |
|
409 | + $this->get_other_model()->item_name(2) |
|
410 | + ); |
|
411 | + } |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Returns whatever is set as the nicename for the object. |
|
416 | + * |
|
417 | + * @return string |
|
418 | + */ |
|
419 | + public function getSchemaDescription() |
|
420 | + { |
|
421 | + $description = $this instanceof EE_Belongs_To_Relation |
|
422 | + ? esc_html__('The related %1$s entity to the %2$s.', 'event_espresso') |
|
423 | + : esc_html__('The related %1$s entities to the %2$s.', 'event_espresso'); |
|
424 | + return sprintf( |
|
425 | + $description, |
|
426 | + $this->get_other_model()->get_this_model_name(), |
|
427 | + $this->get_this_model()->get_this_model_name() |
|
428 | + ); |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Returns whatever is set as the $_schema_type property for the object. |
|
433 | + * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
434 | + * |
|
435 | + * @return string|array |
|
436 | + */ |
|
437 | + public function getSchemaType() |
|
438 | + { |
|
439 | + return $this instanceof EE_Belongs_To_Relation ? 'object' : 'array'; |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
444 | + * this method and return the properties for the schema. |
|
445 | + * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
446 | + * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
447 | + * |
|
448 | + * @return array |
|
449 | + */ |
|
450 | + public function getSchemaProperties() |
|
451 | + { |
|
452 | + return array(); |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * If a child class has enum values, they should override this method and provide a simple array |
|
457 | + * of the enum values. |
|
458 | + * The reason this is not a property on the class is because there may be filterable enum values that |
|
459 | + * are set on the instantiated object that could be filtered after construct. |
|
460 | + * |
|
461 | + * @return array |
|
462 | + */ |
|
463 | + public function getSchemaEnum() |
|
464 | + { |
|
465 | + return array(); |
|
466 | + } |
|
467 | + |
|
468 | + /** |
|
469 | + * This returns the value of the $_schema_format property on the object. |
|
470 | + * |
|
471 | + * @return string |
|
472 | + */ |
|
473 | + public function getSchemaFormat() |
|
474 | + { |
|
475 | + return array(); |
|
476 | + } |
|
477 | + |
|
478 | + /** |
|
479 | + * This returns the value of the $_schema_readonly property on the object. |
|
480 | + * |
|
481 | + * @return bool |
|
482 | + */ |
|
483 | + public function getSchemaReadonly() |
|
484 | + { |
|
485 | + return true; |
|
486 | + } |
|
487 | + |
|
488 | + /** |
|
489 | + * This returns elements used to represent this field in the json schema. |
|
490 | + * |
|
491 | + * @link http://json-schema.org/ |
|
492 | + * @return array |
|
493 | + */ |
|
494 | + public function getSchema() |
|
495 | + { |
|
496 | + $schema = array( |
|
497 | + 'description' => $this->getSchemaDescription(), |
|
498 | + 'type' => $this->getSchemaType(), |
|
499 | + 'relation' => true, |
|
500 | + 'relation_type' => get_class($this), |
|
501 | + 'readonly' => $this->getSchemaReadonly() |
|
502 | + ); |
|
503 | + |
|
504 | + if ($this instanceof EE_HABTM_Relation) { |
|
505 | + $schema['joining_model_name'] = $this->get_join_model()->get_this_model_name(); |
|
506 | + } |
|
507 | + |
|
508 | + if ($this->getSchemaType() === 'array') { |
|
509 | + $schema['items'] = array( |
|
510 | + 'type' => 'object' |
|
511 | + ); |
|
512 | + } |
|
513 | + |
|
514 | + return $schema; |
|
515 | + } |
|
516 | 516 | } |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | $this_table_join_column, |
157 | 157 | $extra_join_sql = '' |
158 | 158 | ) { |
159 | - return " LEFT JOIN " . $other_table . " AS " . $other_table_alias . " ON " . $other_table_alias . "." . $other_table_column . "=" . $this_table_alias . "." . $this_table_join_column . ($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
159 | + return " LEFT JOIN ".$other_table." AS ".$other_table_alias." ON ".$other_table_alias.".".$other_table_column."=".$this_table_alias.".".$this_table_join_column.($extra_join_sql ? " AND $extra_join_sql" : ''); |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | . "." |
191 | 191 | . $this->get_this_model()->get_primary_key_field()->get_name(); |
192 | 192 | $model_object_id = $this->_get_model_object_id($model_object_or_id); |
193 | - $query_params[0][ $query_param_where_this_model_pk ] = $model_object_id; |
|
193 | + $query_params[0][$query_param_where_this_model_pk] = $model_object_id; |
|
194 | 194 | return $this->get_other_model()->get_all($query_params); |
195 | 195 | } |
196 | 196 | |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | */ |
204 | 204 | protected function _disable_default_where_conditions_on_query_param($query_params) |
205 | 205 | { |
206 | - if (! isset($query_params['default_where_conditions'])) { |
|
206 | + if ( ! isset($query_params['default_where_conditions'])) { |
|
207 | 207 | $query_params['default_where_conditions'] = 'none'; |
208 | 208 | } |
209 | 209 | return $query_params; |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | $model_object_or_id |
234 | 234 | ); |
235 | 235 | /* @var $model_object_or_id EE_Base_Class */ |
236 | - if (! $delete_is_blocked) { |
|
236 | + if ( ! $delete_is_blocked) { |
|
237 | 237 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
238 | 238 | $related_model_object->delete(); |
239 | 239 | $deleted_count++; |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | if ($related_model_object instanceof EE_Soft_Delete_Base_Class) { |
270 | 270 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
271 | 271 | $deleted_count++; |
272 | - if (! $delete_is_blocked) { |
|
272 | + if ( ! $delete_is_blocked) { |
|
273 | 273 | $related_model_object->delete_permanently(); |
274 | 274 | } else { |
275 | 275 | // delete is blocked |
@@ -278,7 +278,7 @@ discard block |
||
278 | 278 | } |
279 | 279 | } else { |
280 | 280 | // its not a soft-deletable thing anyways. do the normal logic. |
281 | - if (! $delete_is_blocked) { |
|
281 | + if ( ! $delete_is_blocked) { |
|
282 | 282 | $this->remove_relation_to($model_object_or_id, $related_model_object); |
283 | 283 | $related_model_object->delete(); |
284 | 284 | $deleted_count++; |
@@ -302,7 +302,7 @@ discard block |
||
302 | 302 | if ($model_object_or_id instanceof EE_Base_Class) { |
303 | 303 | $model_object_id = $model_object_or_id->ID(); |
304 | 304 | } |
305 | - if (! $model_object_id) { |
|
305 | + if ( ! $model_object_id) { |
|
306 | 306 | throw new EE_Error(sprintf( |
307 | 307 | __( |
308 | 308 | "Sorry, we cant get the related %s model objects to %s model object before it has an ID. You can solve that by just saving it before trying to get its related model objects", |
@@ -567,7 +567,7 @@ discard block |
||
567 | 567 | * allowed) |
568 | 568 | * |
569 | 569 | * @param string $datetime_string mysql timestamp in UTC |
570 | - * @return mixed null | DateTime |
|
570 | + * @return null|DbSafeDateTime null | DateTime |
|
571 | 571 | * @throws \EE_Error |
572 | 572 | */ |
573 | 573 | public function prepare_for_set_from_db($datetime_string) |
@@ -724,7 +724,7 @@ discard block |
||
724 | 724 | * |
725 | 725 | * @param \DateTimeZone $DateTimeZone |
726 | 726 | * @param int $time |
727 | - * @return mixed |
|
727 | + * @return integer |
|
728 | 728 | * @throws \DomainException |
729 | 729 | */ |
730 | 730 | public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
@@ -15,747 +15,747 @@ |
||
15 | 15 | class EE_Datetime_Field extends EE_Model_Field_Base |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
20 | - * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
21 | - * |
|
22 | - * @type string unix_timestamp_regex |
|
23 | - */ |
|
24 | - const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
25 | - |
|
26 | - /** |
|
27 | - * @type string mysql_timestamp_format |
|
28 | - */ |
|
29 | - const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
30 | - |
|
31 | - /** |
|
32 | - * @type string mysql_date_format |
|
33 | - */ |
|
34 | - const mysql_date_format = 'Y-m-d'; |
|
35 | - |
|
36 | - /** |
|
37 | - * @type string mysql_time_format |
|
38 | - */ |
|
39 | - const mysql_time_format = 'H:i:s'; |
|
40 | - |
|
41 | - /** |
|
42 | - * Const for using in the default value. If the field's default is set to this, |
|
43 | - * then we will return the time of calling `get_default_value()`, not |
|
44 | - * just the current time at construction |
|
45 | - */ |
|
46 | - const now = 'now'; |
|
47 | - |
|
48 | - /** |
|
49 | - * The following properties hold the default formats for date and time. |
|
50 | - * Defaults are set via the constructor and can be overridden on class instantiation. |
|
51 | - * However they can also be overridden later by the set_format() method |
|
52 | - * (and corresponding set_date_format, set_time_format methods); |
|
53 | - */ |
|
54 | - /** |
|
55 | - * @type string $_date_format |
|
56 | - */ |
|
57 | - protected $_date_format = ''; |
|
58 | - |
|
59 | - /** |
|
60 | - * @type string $_time_format |
|
61 | - */ |
|
62 | - protected $_time_format = ''; |
|
63 | - |
|
64 | - /** |
|
65 | - * @type string $_pretty_date_format |
|
66 | - */ |
|
67 | - protected $_pretty_date_format = ''; |
|
68 | - |
|
69 | - /** |
|
70 | - * @type string $_pretty_time_format |
|
71 | - */ |
|
72 | - protected $_pretty_time_format = ''; |
|
73 | - |
|
74 | - /** |
|
75 | - * @type DateTimeZone $_DateTimeZone |
|
76 | - */ |
|
77 | - protected $_DateTimeZone; |
|
78 | - |
|
79 | - /** |
|
80 | - * @type DateTimeZone $_UTC_DateTimeZone |
|
81 | - */ |
|
82 | - protected $_UTC_DateTimeZone; |
|
83 | - |
|
84 | - /** |
|
85 | - * @type DateTimeZone $_blog_DateTimeZone |
|
86 | - */ |
|
87 | - protected $_blog_DateTimeZone; |
|
88 | - |
|
89 | - |
|
90 | - /** |
|
91 | - * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
92 | - * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
93 | - * and time returned via getters. |
|
94 | - * |
|
95 | - * @var mixed (null|string) |
|
96 | - */ |
|
97 | - protected $_date_time_output; |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * timezone string |
|
102 | - * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
103 | - * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
104 | - * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
105 | - * |
|
106 | - * @var string |
|
107 | - */ |
|
108 | - protected $_timezone_string; |
|
109 | - |
|
110 | - |
|
111 | - /** |
|
112 | - * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
113 | - * offsets for comparison purposes). |
|
114 | - * |
|
115 | - * @var int |
|
116 | - */ |
|
117 | - protected $_blog_offset; |
|
118 | - |
|
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 | - * @throws EE_Error |
|
132 | - * @throws InvalidArgumentException |
|
133 | - */ |
|
134 | - public function __construct( |
|
135 | - $table_column, |
|
136 | - $nice_name, |
|
137 | - $nullable, |
|
138 | - $default_value, |
|
139 | - $timezone_string = '', |
|
140 | - $date_format = '', |
|
141 | - $time_format = '', |
|
142 | - $pretty_date_format = '', |
|
143 | - $pretty_time_format = '' |
|
144 | - ) { |
|
145 | - |
|
146 | - $this->_date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
147 | - $this->_time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
148 | - $this->_pretty_date_format = ! empty($pretty_date_format) ? $pretty_date_format : get_option('date_format'); |
|
149 | - $this->_pretty_time_format = ! empty($pretty_time_format) ? $pretty_time_format : get_option('time_format'); |
|
150 | - |
|
151 | - parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
152 | - $this->set_timezone($timezone_string); |
|
153 | - $this->setSchemaFormat('date-time'); |
|
154 | - } |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * @return DateTimeZone |
|
159 | - * @throws \EE_Error |
|
160 | - */ |
|
161 | - public function get_UTC_DateTimeZone() |
|
162 | - { |
|
163 | - return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
164 | - ? $this->_UTC_DateTimeZone |
|
165 | - : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * @return DateTimeZone |
|
171 | - * @throws \EE_Error |
|
172 | - */ |
|
173 | - public function get_blog_DateTimeZone() |
|
174 | - { |
|
175 | - return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
176 | - ? $this->_blog_DateTimeZone |
|
177 | - : $this->_create_timezone_object_from_timezone_string(''); |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - /** |
|
182 | - * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
|
183 | - * |
|
184 | - * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
|
185 | - * timestamp |
|
186 | - * @return DateTime |
|
187 | - */ |
|
188 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
189 | - { |
|
190 | - return $this->_get_date_object($value_inputted_for_field_on_model_object); |
|
191 | - } |
|
192 | - |
|
193 | - |
|
194 | - /** |
|
195 | - * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
196 | - * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
197 | - * |
|
198 | - * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
199 | - * @return string The final assembled format string. |
|
200 | - */ |
|
201 | - protected function _get_date_time_output($pretty = false) |
|
202 | - { |
|
203 | - |
|
204 | - switch ($this->_date_time_output) { |
|
205 | - case 'time': |
|
206 | - return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
207 | - break; |
|
208 | - |
|
209 | - case 'date': |
|
210 | - return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
211 | - break; |
|
212 | - |
|
213 | - default: |
|
214 | - return $pretty |
|
215 | - ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
216 | - : $this->_date_format . ' ' . $this->_time_format; |
|
217 | - } |
|
218 | - } |
|
219 | - |
|
220 | - |
|
221 | - /** |
|
222 | - * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
223 | - * returned (using the format properties) |
|
224 | - * |
|
225 | - * @param string $what acceptable values are 'time' or 'date'. |
|
226 | - * Any other value will be set but will always result |
|
227 | - * in both 'date' and 'time' being returned. |
|
228 | - * @return void |
|
229 | - */ |
|
230 | - public function set_date_time_output($what = null) |
|
231 | - { |
|
232 | - $this->_date_time_output = $what; |
|
233 | - } |
|
234 | - |
|
235 | - |
|
236 | - /** |
|
237 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
238 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
239 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
240 | - * We also set some other properties in this method. |
|
241 | - * |
|
242 | - * @param string $timezone_string A valid timezone string as described by @link |
|
243 | - * http://www.php.net/manual/en/timezones.php |
|
244 | - * @return void |
|
245 | - * @throws InvalidArgumentException |
|
246 | - * @throws InvalidDataTypeException |
|
247 | - * @throws InvalidInterfaceException |
|
248 | - */ |
|
249 | - public function set_timezone($timezone_string) |
|
250 | - { |
|
251 | - if (empty($timezone_string) && $this->_timezone_string !== null) { |
|
252 | - // leave the timezone AS-IS if we already have one and |
|
253 | - // the function arg didn't provide one |
|
254 | - return; |
|
255 | - } |
|
256 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
257 | - $this->_timezone_string = ! empty($timezone_string) ? $timezone_string : 'UTC'; |
|
258 | - $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
259 | - } |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * _create_timezone_object_from_timezone_name |
|
264 | - * |
|
265 | - * @access protected |
|
266 | - * @param string $timezone_string |
|
267 | - * @return \DateTimeZone |
|
268 | - * @throws InvalidArgumentException |
|
269 | - * @throws InvalidDataTypeException |
|
270 | - * @throws InvalidInterfaceException |
|
271 | - */ |
|
272 | - protected function _create_timezone_object_from_timezone_string($timezone_string = '') |
|
273 | - { |
|
274 | - return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
275 | - } |
|
276 | - |
|
277 | - |
|
278 | - /** |
|
279 | - * This just returns whatever is set for the current timezone. |
|
280 | - * |
|
281 | - * @access public |
|
282 | - * @return string timezone string |
|
283 | - */ |
|
284 | - public function get_timezone() |
|
285 | - { |
|
286 | - return $this->_timezone_string; |
|
287 | - } |
|
288 | - |
|
289 | - |
|
290 | - /** |
|
291 | - * set the $_date_format property |
|
292 | - * |
|
293 | - * @access public |
|
294 | - * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
295 | - * @param bool $pretty Whether to set pretty format or not. |
|
296 | - * @return void |
|
297 | - */ |
|
298 | - public function set_date_format($format, $pretty = false) |
|
299 | - { |
|
300 | - if ($pretty) { |
|
301 | - $this->_pretty_date_format = $format; |
|
302 | - } else { |
|
303 | - $this->_date_format = $format; |
|
304 | - } |
|
305 | - } |
|
306 | - |
|
307 | - |
|
308 | - /** |
|
309 | - * return the $_date_format property value. |
|
310 | - * |
|
311 | - * @param bool $pretty Whether to get pretty format or not. |
|
312 | - * @return string |
|
313 | - */ |
|
314 | - public function get_date_format($pretty = false) |
|
315 | - { |
|
316 | - return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
317 | - } |
|
318 | - |
|
319 | - |
|
320 | - /** |
|
321 | - * set the $_time_format property |
|
322 | - * |
|
323 | - * @access public |
|
324 | - * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
325 | - * @param bool $pretty Whether to set pretty format or not. |
|
326 | - * @return void |
|
327 | - */ |
|
328 | - public function set_time_format($format, $pretty = false) |
|
329 | - { |
|
330 | - if ($pretty) { |
|
331 | - $this->_pretty_time_format = $format; |
|
332 | - } else { |
|
333 | - $this->_time_format = $format; |
|
334 | - } |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - /** |
|
339 | - * return the $_time_format property value. |
|
340 | - * |
|
341 | - * @param bool $pretty Whether to get pretty format or not. |
|
342 | - * @return string |
|
343 | - */ |
|
344 | - public function get_time_format($pretty = false) |
|
345 | - { |
|
346 | - return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
347 | - } |
|
348 | - |
|
349 | - |
|
350 | - /** |
|
351 | - * set the $_pretty_date_format property |
|
352 | - * |
|
353 | - * @access public |
|
354 | - * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
355 | - * @return void |
|
356 | - */ |
|
357 | - public function set_pretty_date_format($format) |
|
358 | - { |
|
359 | - $this->_pretty_date_format = $format; |
|
360 | - } |
|
361 | - |
|
362 | - |
|
363 | - /** |
|
364 | - * set the $_pretty_time_format property |
|
365 | - * |
|
366 | - * @access public |
|
367 | - * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
368 | - * @return void |
|
369 | - */ |
|
370 | - public function set_pretty_time_format($format) |
|
371 | - { |
|
372 | - $this->_pretty_time_format = $format; |
|
373 | - } |
|
374 | - |
|
375 | - |
|
376 | - /** |
|
377 | - * Only sets the time portion of the datetime. |
|
378 | - * |
|
379 | - * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
380 | - * @param DateTime $current current DateTime object for the datetime field |
|
381 | - * @return DateTime |
|
382 | - */ |
|
383 | - public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current) |
|
384 | - { |
|
385 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
386 | - // Otherwise parse the string. |
|
387 | - if ($time_to_set_string instanceof DateTime) { |
|
388 | - $parsed = array( |
|
389 | - 'hour' => $time_to_set_string->format('H'), |
|
390 | - 'minute' => $time_to_set_string->format('i'), |
|
391 | - 'second' => $time_to_set_string->format('s'), |
|
392 | - ); |
|
393 | - } else { |
|
394 | - // parse incoming string |
|
395 | - $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
396 | - } |
|
397 | - EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
398 | - return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
399 | - } |
|
400 | - |
|
401 | - |
|
402 | - /** |
|
403 | - * Only sets the date portion of the datetime. |
|
404 | - * |
|
405 | - * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
|
406 | - * @param DateTime $current current DateTime object for the datetime field |
|
407 | - * @return DateTime |
|
408 | - */ |
|
409 | - public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current) |
|
410 | - { |
|
411 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
412 | - // Otherwise parse the string. |
|
413 | - if ($date_to_set_string instanceof DateTime) { |
|
414 | - $parsed = array( |
|
415 | - 'year' => $date_to_set_string->format('Y'), |
|
416 | - 'month' => $date_to_set_string->format('m'), |
|
417 | - 'day' => $date_to_set_string->format('d'), |
|
418 | - ); |
|
419 | - } else { |
|
420 | - // parse incoming string |
|
421 | - $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
422 | - } |
|
423 | - EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
424 | - return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
425 | - } |
|
426 | - |
|
427 | - |
|
428 | - /** |
|
429 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
|
430 | - * datetime gets to this stage it should ALREADY be in UTC time |
|
431 | - * |
|
432 | - * @param DateTime $DateTime |
|
433 | - * @return string formatted date time for given timezone |
|
434 | - * @throws \EE_Error |
|
435 | - */ |
|
436 | - public function prepare_for_get($DateTime) |
|
437 | - { |
|
438 | - return $this->_prepare_for_display($DateTime); |
|
439 | - } |
|
440 | - |
|
441 | - |
|
442 | - /** |
|
443 | - * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
444 | - * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
445 | - * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
446 | - * abbreviation to the date_string. |
|
447 | - * |
|
448 | - * @param mixed $DateTime |
|
449 | - * @param null $schema |
|
450 | - * @return string |
|
451 | - * @throws \EE_Error |
|
452 | - */ |
|
453 | - public function prepare_for_pretty_echoing($DateTime, $schema = null) |
|
454 | - { |
|
455 | - return $this->_prepare_for_display($DateTime, $schema ? $schema : true); |
|
456 | - } |
|
457 | - |
|
458 | - |
|
459 | - /** |
|
460 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
461 | - * timezone). |
|
462 | - * |
|
463 | - * @param DateTime $DateTime |
|
464 | - * @param bool|string $schema |
|
465 | - * @return string |
|
466 | - * @throws \EE_Error |
|
467 | - */ |
|
468 | - protected function _prepare_for_display($DateTime, $schema = false) |
|
469 | - { |
|
470 | - if (! $DateTime instanceof DateTime) { |
|
471 | - if ($this->_nullable) { |
|
472 | - return ''; |
|
473 | - } else { |
|
474 | - if (WP_DEBUG) { |
|
475 | - throw new EE_Error( |
|
476 | - sprintf( |
|
477 | - __( |
|
478 | - '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.', |
|
479 | - 'event_espresso' |
|
480 | - ), |
|
481 | - $this->_nicename |
|
482 | - ) |
|
483 | - ); |
|
484 | - } else { |
|
485 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now); |
|
486 | - EE_Error::add_error( |
|
487 | - sprintf( |
|
488 | - __( |
|
489 | - '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.', |
|
490 | - 'event_espresso' |
|
491 | - ), |
|
492 | - $this->_nicename |
|
493 | - ) |
|
494 | - ); |
|
495 | - } |
|
496 | - } |
|
497 | - } |
|
498 | - $format_string = $this->_get_date_time_output($schema); |
|
499 | - EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
500 | - if ($schema) { |
|
501 | - if ($this->_display_timezone()) { |
|
502 | - // must be explicit because schema could equal true. |
|
503 | - if ($schema === 'no_html') { |
|
504 | - $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
505 | - } else { |
|
506 | - $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
507 | - } |
|
508 | - } else { |
|
509 | - $timezone_string = ''; |
|
510 | - } |
|
511 | - |
|
512 | - return $DateTime->format($format_string) . $timezone_string; |
|
513 | - } |
|
514 | - return $DateTime->format($format_string); |
|
515 | - } |
|
516 | - |
|
517 | - |
|
518 | - /** |
|
519 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
520 | - * timezone). |
|
521 | - * |
|
522 | - * @param mixed $datetime_value u |
|
523 | - * @return string mysql timestamp in UTC |
|
524 | - * @throws \EE_Error |
|
525 | - */ |
|
526 | - public function prepare_for_use_in_db($datetime_value) |
|
527 | - { |
|
528 | - // we allow an empty value or DateTime object, but nothing else. |
|
529 | - if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
530 | - throw new EE_Error( |
|
531 | - sprintf( |
|
532 | - __( |
|
533 | - 'The incoming value being prepared for setting in the database must either be empty or a php |
|
18 | + /** |
|
19 | + * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
20 | + * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
21 | + * |
|
22 | + * @type string unix_timestamp_regex |
|
23 | + */ |
|
24 | + const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
25 | + |
|
26 | + /** |
|
27 | + * @type string mysql_timestamp_format |
|
28 | + */ |
|
29 | + const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
30 | + |
|
31 | + /** |
|
32 | + * @type string mysql_date_format |
|
33 | + */ |
|
34 | + const mysql_date_format = 'Y-m-d'; |
|
35 | + |
|
36 | + /** |
|
37 | + * @type string mysql_time_format |
|
38 | + */ |
|
39 | + const mysql_time_format = 'H:i:s'; |
|
40 | + |
|
41 | + /** |
|
42 | + * Const for using in the default value. If the field's default is set to this, |
|
43 | + * then we will return the time of calling `get_default_value()`, not |
|
44 | + * just the current time at construction |
|
45 | + */ |
|
46 | + const now = 'now'; |
|
47 | + |
|
48 | + /** |
|
49 | + * The following properties hold the default formats for date and time. |
|
50 | + * Defaults are set via the constructor and can be overridden on class instantiation. |
|
51 | + * However they can also be overridden later by the set_format() method |
|
52 | + * (and corresponding set_date_format, set_time_format methods); |
|
53 | + */ |
|
54 | + /** |
|
55 | + * @type string $_date_format |
|
56 | + */ |
|
57 | + protected $_date_format = ''; |
|
58 | + |
|
59 | + /** |
|
60 | + * @type string $_time_format |
|
61 | + */ |
|
62 | + protected $_time_format = ''; |
|
63 | + |
|
64 | + /** |
|
65 | + * @type string $_pretty_date_format |
|
66 | + */ |
|
67 | + protected $_pretty_date_format = ''; |
|
68 | + |
|
69 | + /** |
|
70 | + * @type string $_pretty_time_format |
|
71 | + */ |
|
72 | + protected $_pretty_time_format = ''; |
|
73 | + |
|
74 | + /** |
|
75 | + * @type DateTimeZone $_DateTimeZone |
|
76 | + */ |
|
77 | + protected $_DateTimeZone; |
|
78 | + |
|
79 | + /** |
|
80 | + * @type DateTimeZone $_UTC_DateTimeZone |
|
81 | + */ |
|
82 | + protected $_UTC_DateTimeZone; |
|
83 | + |
|
84 | + /** |
|
85 | + * @type DateTimeZone $_blog_DateTimeZone |
|
86 | + */ |
|
87 | + protected $_blog_DateTimeZone; |
|
88 | + |
|
89 | + |
|
90 | + /** |
|
91 | + * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
92 | + * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
93 | + * and time returned via getters. |
|
94 | + * |
|
95 | + * @var mixed (null|string) |
|
96 | + */ |
|
97 | + protected $_date_time_output; |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * timezone string |
|
102 | + * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
103 | + * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
104 | + * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
105 | + * |
|
106 | + * @var string |
|
107 | + */ |
|
108 | + protected $_timezone_string; |
|
109 | + |
|
110 | + |
|
111 | + /** |
|
112 | + * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
113 | + * offsets for comparison purposes). |
|
114 | + * |
|
115 | + * @var int |
|
116 | + */ |
|
117 | + protected $_blog_offset; |
|
118 | + |
|
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 | + * @throws EE_Error |
|
132 | + * @throws InvalidArgumentException |
|
133 | + */ |
|
134 | + public function __construct( |
|
135 | + $table_column, |
|
136 | + $nice_name, |
|
137 | + $nullable, |
|
138 | + $default_value, |
|
139 | + $timezone_string = '', |
|
140 | + $date_format = '', |
|
141 | + $time_format = '', |
|
142 | + $pretty_date_format = '', |
|
143 | + $pretty_time_format = '' |
|
144 | + ) { |
|
145 | + |
|
146 | + $this->_date_format = ! empty($date_format) ? $date_format : get_option('date_format'); |
|
147 | + $this->_time_format = ! empty($time_format) ? $time_format : get_option('time_format'); |
|
148 | + $this->_pretty_date_format = ! empty($pretty_date_format) ? $pretty_date_format : get_option('date_format'); |
|
149 | + $this->_pretty_time_format = ! empty($pretty_time_format) ? $pretty_time_format : get_option('time_format'); |
|
150 | + |
|
151 | + parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
152 | + $this->set_timezone($timezone_string); |
|
153 | + $this->setSchemaFormat('date-time'); |
|
154 | + } |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * @return DateTimeZone |
|
159 | + * @throws \EE_Error |
|
160 | + */ |
|
161 | + public function get_UTC_DateTimeZone() |
|
162 | + { |
|
163 | + return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
164 | + ? $this->_UTC_DateTimeZone |
|
165 | + : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * @return DateTimeZone |
|
171 | + * @throws \EE_Error |
|
172 | + */ |
|
173 | + public function get_blog_DateTimeZone() |
|
174 | + { |
|
175 | + return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
176 | + ? $this->_blog_DateTimeZone |
|
177 | + : $this->_create_timezone_object_from_timezone_string(''); |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + /** |
|
182 | + * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
|
183 | + * |
|
184 | + * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
|
185 | + * timestamp |
|
186 | + * @return DateTime |
|
187 | + */ |
|
188 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
189 | + { |
|
190 | + return $this->_get_date_object($value_inputted_for_field_on_model_object); |
|
191 | + } |
|
192 | + |
|
193 | + |
|
194 | + /** |
|
195 | + * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
196 | + * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
197 | + * |
|
198 | + * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
199 | + * @return string The final assembled format string. |
|
200 | + */ |
|
201 | + protected function _get_date_time_output($pretty = false) |
|
202 | + { |
|
203 | + |
|
204 | + switch ($this->_date_time_output) { |
|
205 | + case 'time': |
|
206 | + return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
207 | + break; |
|
208 | + |
|
209 | + case 'date': |
|
210 | + return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
211 | + break; |
|
212 | + |
|
213 | + default: |
|
214 | + return $pretty |
|
215 | + ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
216 | + : $this->_date_format . ' ' . $this->_time_format; |
|
217 | + } |
|
218 | + } |
|
219 | + |
|
220 | + |
|
221 | + /** |
|
222 | + * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
223 | + * returned (using the format properties) |
|
224 | + * |
|
225 | + * @param string $what acceptable values are 'time' or 'date'. |
|
226 | + * Any other value will be set but will always result |
|
227 | + * in both 'date' and 'time' being returned. |
|
228 | + * @return void |
|
229 | + */ |
|
230 | + public function set_date_time_output($what = null) |
|
231 | + { |
|
232 | + $this->_date_time_output = $what; |
|
233 | + } |
|
234 | + |
|
235 | + |
|
236 | + /** |
|
237 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
238 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
239 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
240 | + * We also set some other properties in this method. |
|
241 | + * |
|
242 | + * @param string $timezone_string A valid timezone string as described by @link |
|
243 | + * http://www.php.net/manual/en/timezones.php |
|
244 | + * @return void |
|
245 | + * @throws InvalidArgumentException |
|
246 | + * @throws InvalidDataTypeException |
|
247 | + * @throws InvalidInterfaceException |
|
248 | + */ |
|
249 | + public function set_timezone($timezone_string) |
|
250 | + { |
|
251 | + if (empty($timezone_string) && $this->_timezone_string !== null) { |
|
252 | + // leave the timezone AS-IS if we already have one and |
|
253 | + // the function arg didn't provide one |
|
254 | + return; |
|
255 | + } |
|
256 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
257 | + $this->_timezone_string = ! empty($timezone_string) ? $timezone_string : 'UTC'; |
|
258 | + $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
259 | + } |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * _create_timezone_object_from_timezone_name |
|
264 | + * |
|
265 | + * @access protected |
|
266 | + * @param string $timezone_string |
|
267 | + * @return \DateTimeZone |
|
268 | + * @throws InvalidArgumentException |
|
269 | + * @throws InvalidDataTypeException |
|
270 | + * @throws InvalidInterfaceException |
|
271 | + */ |
|
272 | + protected function _create_timezone_object_from_timezone_string($timezone_string = '') |
|
273 | + { |
|
274 | + return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
275 | + } |
|
276 | + |
|
277 | + |
|
278 | + /** |
|
279 | + * This just returns whatever is set for the current timezone. |
|
280 | + * |
|
281 | + * @access public |
|
282 | + * @return string timezone string |
|
283 | + */ |
|
284 | + public function get_timezone() |
|
285 | + { |
|
286 | + return $this->_timezone_string; |
|
287 | + } |
|
288 | + |
|
289 | + |
|
290 | + /** |
|
291 | + * set the $_date_format property |
|
292 | + * |
|
293 | + * @access public |
|
294 | + * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
295 | + * @param bool $pretty Whether to set pretty format or not. |
|
296 | + * @return void |
|
297 | + */ |
|
298 | + public function set_date_format($format, $pretty = false) |
|
299 | + { |
|
300 | + if ($pretty) { |
|
301 | + $this->_pretty_date_format = $format; |
|
302 | + } else { |
|
303 | + $this->_date_format = $format; |
|
304 | + } |
|
305 | + } |
|
306 | + |
|
307 | + |
|
308 | + /** |
|
309 | + * return the $_date_format property value. |
|
310 | + * |
|
311 | + * @param bool $pretty Whether to get pretty format or not. |
|
312 | + * @return string |
|
313 | + */ |
|
314 | + public function get_date_format($pretty = false) |
|
315 | + { |
|
316 | + return $pretty ? $this->_pretty_date_format : $this->_date_format; |
|
317 | + } |
|
318 | + |
|
319 | + |
|
320 | + /** |
|
321 | + * set the $_time_format property |
|
322 | + * |
|
323 | + * @access public |
|
324 | + * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
325 | + * @param bool $pretty Whether to set pretty format or not. |
|
326 | + * @return void |
|
327 | + */ |
|
328 | + public function set_time_format($format, $pretty = false) |
|
329 | + { |
|
330 | + if ($pretty) { |
|
331 | + $this->_pretty_time_format = $format; |
|
332 | + } else { |
|
333 | + $this->_time_format = $format; |
|
334 | + } |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + /** |
|
339 | + * return the $_time_format property value. |
|
340 | + * |
|
341 | + * @param bool $pretty Whether to get pretty format or not. |
|
342 | + * @return string |
|
343 | + */ |
|
344 | + public function get_time_format($pretty = false) |
|
345 | + { |
|
346 | + return $pretty ? $this->_pretty_time_format : $this->_time_format; |
|
347 | + } |
|
348 | + |
|
349 | + |
|
350 | + /** |
|
351 | + * set the $_pretty_date_format property |
|
352 | + * |
|
353 | + * @access public |
|
354 | + * @param string $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
355 | + * @return void |
|
356 | + */ |
|
357 | + public function set_pretty_date_format($format) |
|
358 | + { |
|
359 | + $this->_pretty_date_format = $format; |
|
360 | + } |
|
361 | + |
|
362 | + |
|
363 | + /** |
|
364 | + * set the $_pretty_time_format property |
|
365 | + * |
|
366 | + * @access public |
|
367 | + * @param string $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
368 | + * @return void |
|
369 | + */ |
|
370 | + public function set_pretty_time_format($format) |
|
371 | + { |
|
372 | + $this->_pretty_time_format = $format; |
|
373 | + } |
|
374 | + |
|
375 | + |
|
376 | + /** |
|
377 | + * Only sets the time portion of the datetime. |
|
378 | + * |
|
379 | + * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
380 | + * @param DateTime $current current DateTime object for the datetime field |
|
381 | + * @return DateTime |
|
382 | + */ |
|
383 | + public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current) |
|
384 | + { |
|
385 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
386 | + // Otherwise parse the string. |
|
387 | + if ($time_to_set_string instanceof DateTime) { |
|
388 | + $parsed = array( |
|
389 | + 'hour' => $time_to_set_string->format('H'), |
|
390 | + 'minute' => $time_to_set_string->format('i'), |
|
391 | + 'second' => $time_to_set_string->format('s'), |
|
392 | + ); |
|
393 | + } else { |
|
394 | + // parse incoming string |
|
395 | + $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
396 | + } |
|
397 | + EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
398 | + return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
399 | + } |
|
400 | + |
|
401 | + |
|
402 | + /** |
|
403 | + * Only sets the date portion of the datetime. |
|
404 | + * |
|
405 | + * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
|
406 | + * @param DateTime $current current DateTime object for the datetime field |
|
407 | + * @return DateTime |
|
408 | + */ |
|
409 | + public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current) |
|
410 | + { |
|
411 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
412 | + // Otherwise parse the string. |
|
413 | + if ($date_to_set_string instanceof DateTime) { |
|
414 | + $parsed = array( |
|
415 | + 'year' => $date_to_set_string->format('Y'), |
|
416 | + 'month' => $date_to_set_string->format('m'), |
|
417 | + 'day' => $date_to_set_string->format('d'), |
|
418 | + ); |
|
419 | + } else { |
|
420 | + // parse incoming string |
|
421 | + $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
422 | + } |
|
423 | + EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
424 | + return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
425 | + } |
|
426 | + |
|
427 | + |
|
428 | + /** |
|
429 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
|
430 | + * datetime gets to this stage it should ALREADY be in UTC time |
|
431 | + * |
|
432 | + * @param DateTime $DateTime |
|
433 | + * @return string formatted date time for given timezone |
|
434 | + * @throws \EE_Error |
|
435 | + */ |
|
436 | + public function prepare_for_get($DateTime) |
|
437 | + { |
|
438 | + return $this->_prepare_for_display($DateTime); |
|
439 | + } |
|
440 | + |
|
441 | + |
|
442 | + /** |
|
443 | + * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
444 | + * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
445 | + * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
446 | + * abbreviation to the date_string. |
|
447 | + * |
|
448 | + * @param mixed $DateTime |
|
449 | + * @param null $schema |
|
450 | + * @return string |
|
451 | + * @throws \EE_Error |
|
452 | + */ |
|
453 | + public function prepare_for_pretty_echoing($DateTime, $schema = null) |
|
454 | + { |
|
455 | + return $this->_prepare_for_display($DateTime, $schema ? $schema : true); |
|
456 | + } |
|
457 | + |
|
458 | + |
|
459 | + /** |
|
460 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
461 | + * timezone). |
|
462 | + * |
|
463 | + * @param DateTime $DateTime |
|
464 | + * @param bool|string $schema |
|
465 | + * @return string |
|
466 | + * @throws \EE_Error |
|
467 | + */ |
|
468 | + protected function _prepare_for_display($DateTime, $schema = false) |
|
469 | + { |
|
470 | + if (! $DateTime instanceof DateTime) { |
|
471 | + if ($this->_nullable) { |
|
472 | + return ''; |
|
473 | + } else { |
|
474 | + if (WP_DEBUG) { |
|
475 | + throw new EE_Error( |
|
476 | + sprintf( |
|
477 | + __( |
|
478 | + '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.', |
|
479 | + 'event_espresso' |
|
480 | + ), |
|
481 | + $this->_nicename |
|
482 | + ) |
|
483 | + ); |
|
484 | + } else { |
|
485 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now); |
|
486 | + EE_Error::add_error( |
|
487 | + sprintf( |
|
488 | + __( |
|
489 | + '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.', |
|
490 | + 'event_espresso' |
|
491 | + ), |
|
492 | + $this->_nicename |
|
493 | + ) |
|
494 | + ); |
|
495 | + } |
|
496 | + } |
|
497 | + } |
|
498 | + $format_string = $this->_get_date_time_output($schema); |
|
499 | + EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
500 | + if ($schema) { |
|
501 | + if ($this->_display_timezone()) { |
|
502 | + // must be explicit because schema could equal true. |
|
503 | + if ($schema === 'no_html') { |
|
504 | + $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
505 | + } else { |
|
506 | + $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
507 | + } |
|
508 | + } else { |
|
509 | + $timezone_string = ''; |
|
510 | + } |
|
511 | + |
|
512 | + return $DateTime->format($format_string) . $timezone_string; |
|
513 | + } |
|
514 | + return $DateTime->format($format_string); |
|
515 | + } |
|
516 | + |
|
517 | + |
|
518 | + /** |
|
519 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
520 | + * timezone). |
|
521 | + * |
|
522 | + * @param mixed $datetime_value u |
|
523 | + * @return string mysql timestamp in UTC |
|
524 | + * @throws \EE_Error |
|
525 | + */ |
|
526 | + public function prepare_for_use_in_db($datetime_value) |
|
527 | + { |
|
528 | + // we allow an empty value or DateTime object, but nothing else. |
|
529 | + if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
530 | + throw new EE_Error( |
|
531 | + sprintf( |
|
532 | + __( |
|
533 | + 'The incoming value being prepared for setting in the database must either be empty or a php |
|
534 | 534 | DateTime object, instead of: %1$s %2$s', |
535 | - 'event_espresso' |
|
536 | - ), |
|
537 | - '<br />', |
|
538 | - print_r($datetime_value, true) |
|
539 | - ) |
|
540 | - ); |
|
541 | - } |
|
542 | - |
|
543 | - if ($datetime_value instanceof DateTime) { |
|
544 | - if (! $datetime_value instanceof DbSafeDateTime) { |
|
545 | - $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
546 | - } |
|
547 | - EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
|
548 | - return $datetime_value->format( |
|
549 | - EE_Datetime_Field::mysql_timestamp_format |
|
550 | - ); |
|
551 | - } |
|
552 | - |
|
553 | - // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
554 | - return ! $this->_nullable && empty($datetime_value) ? current_time('mysql', true) : null; |
|
555 | - } |
|
556 | - |
|
557 | - |
|
558 | - /** |
|
559 | - * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
560 | - * allowed) |
|
561 | - * |
|
562 | - * @param string $datetime_string mysql timestamp in UTC |
|
563 | - * @return mixed null | DateTime |
|
564 | - * @throws \EE_Error |
|
565 | - */ |
|
566 | - public function prepare_for_set_from_db($datetime_string) |
|
567 | - { |
|
568 | - // if $datetime_value is empty, and ! $this->_nullable, just use time() |
|
569 | - if (empty($datetime_string) && $this->_nullable) { |
|
570 | - return null; |
|
571 | - } |
|
572 | - // datetime strings from the db should ALWAYS be in UTC+0, so use UTC_DateTimeZone when creating |
|
573 | - if (empty($datetime_string)) { |
|
574 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
575 | - } else { |
|
576 | - $DateTime = DateTime::createFromFormat( |
|
577 | - EE_Datetime_Field::mysql_timestamp_format, |
|
578 | - $datetime_string, |
|
579 | - $this->get_UTC_DateTimeZone() |
|
580 | - ); |
|
581 | - if ($DateTime instanceof \DateTime) { |
|
582 | - $DateTime = new DbSafeDateTime( |
|
583 | - $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
584 | - $this->get_UTC_DateTimeZone() |
|
585 | - ); |
|
586 | - } |
|
587 | - } |
|
588 | - |
|
589 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
590 | - // if still no datetime object, then let's just use now |
|
591 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
592 | - } |
|
593 | - // THEN apply the field's set DateTimeZone |
|
594 | - EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
595 | - return $DateTime; |
|
596 | - } |
|
597 | - |
|
598 | - |
|
599 | - /** |
|
600 | - * All this method does is determine if we're going to display the timezone string or not on any output. |
|
601 | - * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
602 | - * If so, then true. |
|
603 | - * |
|
604 | - * @return bool true for yes false for no |
|
605 | - * @throws \EE_Error |
|
606 | - */ |
|
607 | - protected function _display_timezone() |
|
608 | - { |
|
609 | - |
|
610 | - // first let's do a comparison of timezone strings. |
|
611 | - // If they match then we can get out without any further calculations |
|
612 | - $blog_string = get_option('timezone_string'); |
|
613 | - if ($blog_string === $this->_timezone_string) { |
|
614 | - return false; |
|
615 | - } |
|
616 | - // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
617 | - $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
618 | - $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
619 | - // now compare |
|
620 | - return $blog_offset !== $this_offset; |
|
621 | - } |
|
622 | - |
|
623 | - |
|
624 | - /** |
|
625 | - * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
626 | - * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
627 | - * with. |
|
628 | - * |
|
629 | - * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
|
630 | - * in the format that is set on the date_field (or DateTime |
|
631 | - * object)! |
|
632 | - * @return DateTime |
|
633 | - */ |
|
634 | - protected function _get_date_object($date_string) |
|
635 | - { |
|
636 | - // first if this is an empty date_string and nullable is allowed, just return null. |
|
637 | - if ($this->_nullable && empty($date_string)) { |
|
638 | - return null; |
|
639 | - } |
|
640 | - |
|
641 | - // if incoming date |
|
642 | - if ($date_string instanceof DateTime) { |
|
643 | - EEH_DTT_Helper::setTimezone($date_string, $this->_DateTimeZone); |
|
644 | - return $date_string; |
|
645 | - } |
|
646 | - // if empty date_string and made it here. |
|
647 | - // Return a datetime object for now in the given timezone. |
|
648 | - if (empty($date_string)) { |
|
649 | - return new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
650 | - } |
|
651 | - // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
652 | - if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
653 | - try { |
|
654 | - // This is operating under the assumption that the incoming Unix timestamp |
|
655 | - // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
656 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
657 | - $DateTime->setTimestamp($date_string); |
|
658 | - |
|
659 | - return $DateTime; |
|
660 | - } catch (Exception $e) { |
|
661 | - // should be rare, but if things got fooled then let's just continue |
|
662 | - } |
|
663 | - } |
|
664 | - // not a unix timestamp. So we will use the set format on this object and set timezone to |
|
665 | - // create the DateTime object. |
|
666 | - $format = $this->_date_format . ' ' . $this->_time_format; |
|
667 | - try { |
|
668 | - $DateTime = DateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
669 | - if ($DateTime instanceof DateTime) { |
|
670 | - $DateTime = new DbSafeDateTime( |
|
671 | - $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
672 | - $this->_DateTimeZone |
|
673 | - ); |
|
674 | - } |
|
675 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
676 | - throw new EE_Error( |
|
677 | - sprintf( |
|
678 | - __('"%1$s" does not represent a valid Date Time in the format "%2$s".', 'event_espresso'), |
|
679 | - $date_string, |
|
680 | - $format |
|
681 | - ) |
|
682 | - ); |
|
683 | - } |
|
684 | - } catch (Exception $e) { |
|
685 | - // if we made it here then likely then something went really wrong. |
|
686 | - // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
687 | - $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
688 | - } |
|
689 | - |
|
690 | - return $DateTime; |
|
691 | - } |
|
692 | - |
|
693 | - |
|
694 | - |
|
695 | - /** |
|
696 | - * get_timezone_transitions |
|
697 | - * |
|
698 | - * @param \DateTimeZone $DateTimeZone |
|
699 | - * @param int $time |
|
700 | - * @param bool $first_only |
|
701 | - * @return mixed |
|
702 | - */ |
|
703 | - public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
|
704 | - { |
|
705 | - return EEH_DTT_Helper::get_timezone_transitions($DateTimeZone, $time, $first_only); |
|
706 | - } |
|
707 | - |
|
708 | - |
|
709 | - |
|
710 | - /** |
|
711 | - * get_timezone_offset |
|
712 | - * |
|
713 | - * @param \DateTimeZone $DateTimeZone |
|
714 | - * @param int $time |
|
715 | - * @return mixed |
|
716 | - * @throws \DomainException |
|
717 | - */ |
|
718 | - public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
719 | - { |
|
720 | - return EEH_DTT_Helper::get_timezone_offset($DateTimeZone, $time); |
|
721 | - } |
|
722 | - |
|
723 | - |
|
724 | - /** |
|
725 | - * This will take an incoming timezone string and return the abbreviation for that timezone |
|
726 | - * |
|
727 | - * @param string $timezone_string |
|
728 | - * @return string abbreviation |
|
729 | - * @throws \EE_Error |
|
730 | - */ |
|
731 | - public function get_timezone_abbrev($timezone_string) |
|
732 | - { |
|
733 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
734 | - $dateTime = new DateTime(\EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
735 | - |
|
736 | - return $dateTime->format('T'); |
|
737 | - } |
|
738 | - |
|
739 | - /** |
|
740 | - * Overrides the parent to allow for having a dynamic "now" value |
|
741 | - * |
|
742 | - * @return mixed |
|
743 | - */ |
|
744 | - public function get_default_value() |
|
745 | - { |
|
746 | - if ($this->_default_value === EE_Datetime_Field::now) { |
|
747 | - return time(); |
|
748 | - } else { |
|
749 | - return parent::get_default_value(); |
|
750 | - } |
|
751 | - } |
|
752 | - |
|
753 | - |
|
754 | - public function getSchemaDescription() |
|
755 | - { |
|
756 | - return sprintf( |
|
757 | - esc_html__('%s - the value for this field is in the timezone of the site.', 'event_espresso'), |
|
758 | - $this->get_nicename() |
|
759 | - ); |
|
760 | - } |
|
535 | + 'event_espresso' |
|
536 | + ), |
|
537 | + '<br />', |
|
538 | + print_r($datetime_value, true) |
|
539 | + ) |
|
540 | + ); |
|
541 | + } |
|
542 | + |
|
543 | + if ($datetime_value instanceof DateTime) { |
|
544 | + if (! $datetime_value instanceof DbSafeDateTime) { |
|
545 | + $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
546 | + } |
|
547 | + EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
|
548 | + return $datetime_value->format( |
|
549 | + EE_Datetime_Field::mysql_timestamp_format |
|
550 | + ); |
|
551 | + } |
|
552 | + |
|
553 | + // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
554 | + return ! $this->_nullable && empty($datetime_value) ? current_time('mysql', true) : null; |
|
555 | + } |
|
556 | + |
|
557 | + |
|
558 | + /** |
|
559 | + * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
560 | + * allowed) |
|
561 | + * |
|
562 | + * @param string $datetime_string mysql timestamp in UTC |
|
563 | + * @return mixed null | DateTime |
|
564 | + * @throws \EE_Error |
|
565 | + */ |
|
566 | + public function prepare_for_set_from_db($datetime_string) |
|
567 | + { |
|
568 | + // if $datetime_value is empty, and ! $this->_nullable, just use time() |
|
569 | + if (empty($datetime_string) && $this->_nullable) { |
|
570 | + return null; |
|
571 | + } |
|
572 | + // datetime strings from the db should ALWAYS be in UTC+0, so use UTC_DateTimeZone when creating |
|
573 | + if (empty($datetime_string)) { |
|
574 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
575 | + } else { |
|
576 | + $DateTime = DateTime::createFromFormat( |
|
577 | + EE_Datetime_Field::mysql_timestamp_format, |
|
578 | + $datetime_string, |
|
579 | + $this->get_UTC_DateTimeZone() |
|
580 | + ); |
|
581 | + if ($DateTime instanceof \DateTime) { |
|
582 | + $DateTime = new DbSafeDateTime( |
|
583 | + $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
584 | + $this->get_UTC_DateTimeZone() |
|
585 | + ); |
|
586 | + } |
|
587 | + } |
|
588 | + |
|
589 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
590 | + // if still no datetime object, then let's just use now |
|
591 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
592 | + } |
|
593 | + // THEN apply the field's set DateTimeZone |
|
594 | + EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
595 | + return $DateTime; |
|
596 | + } |
|
597 | + |
|
598 | + |
|
599 | + /** |
|
600 | + * All this method does is determine if we're going to display the timezone string or not on any output. |
|
601 | + * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
602 | + * If so, then true. |
|
603 | + * |
|
604 | + * @return bool true for yes false for no |
|
605 | + * @throws \EE_Error |
|
606 | + */ |
|
607 | + protected function _display_timezone() |
|
608 | + { |
|
609 | + |
|
610 | + // first let's do a comparison of timezone strings. |
|
611 | + // If they match then we can get out without any further calculations |
|
612 | + $blog_string = get_option('timezone_string'); |
|
613 | + if ($blog_string === $this->_timezone_string) { |
|
614 | + return false; |
|
615 | + } |
|
616 | + // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
617 | + $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
618 | + $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
619 | + // now compare |
|
620 | + return $blog_offset !== $this_offset; |
|
621 | + } |
|
622 | + |
|
623 | + |
|
624 | + /** |
|
625 | + * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
626 | + * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
627 | + * with. |
|
628 | + * |
|
629 | + * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
|
630 | + * in the format that is set on the date_field (or DateTime |
|
631 | + * object)! |
|
632 | + * @return DateTime |
|
633 | + */ |
|
634 | + protected function _get_date_object($date_string) |
|
635 | + { |
|
636 | + // first if this is an empty date_string and nullable is allowed, just return null. |
|
637 | + if ($this->_nullable && empty($date_string)) { |
|
638 | + return null; |
|
639 | + } |
|
640 | + |
|
641 | + // if incoming date |
|
642 | + if ($date_string instanceof DateTime) { |
|
643 | + EEH_DTT_Helper::setTimezone($date_string, $this->_DateTimeZone); |
|
644 | + return $date_string; |
|
645 | + } |
|
646 | + // if empty date_string and made it here. |
|
647 | + // Return a datetime object for now in the given timezone. |
|
648 | + if (empty($date_string)) { |
|
649 | + return new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
650 | + } |
|
651 | + // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
652 | + if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
653 | + try { |
|
654 | + // This is operating under the assumption that the incoming Unix timestamp |
|
655 | + // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
656 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
657 | + $DateTime->setTimestamp($date_string); |
|
658 | + |
|
659 | + return $DateTime; |
|
660 | + } catch (Exception $e) { |
|
661 | + // should be rare, but if things got fooled then let's just continue |
|
662 | + } |
|
663 | + } |
|
664 | + // not a unix timestamp. So we will use the set format on this object and set timezone to |
|
665 | + // create the DateTime object. |
|
666 | + $format = $this->_date_format . ' ' . $this->_time_format; |
|
667 | + try { |
|
668 | + $DateTime = DateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
669 | + if ($DateTime instanceof DateTime) { |
|
670 | + $DateTime = new DbSafeDateTime( |
|
671 | + $DateTime->format(\EE_Datetime_Field::mysql_timestamp_format), |
|
672 | + $this->_DateTimeZone |
|
673 | + ); |
|
674 | + } |
|
675 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
676 | + throw new EE_Error( |
|
677 | + sprintf( |
|
678 | + __('"%1$s" does not represent a valid Date Time in the format "%2$s".', 'event_espresso'), |
|
679 | + $date_string, |
|
680 | + $format |
|
681 | + ) |
|
682 | + ); |
|
683 | + } |
|
684 | + } catch (Exception $e) { |
|
685 | + // if we made it here then likely then something went really wrong. |
|
686 | + // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
687 | + $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->_DateTimeZone); |
|
688 | + } |
|
689 | + |
|
690 | + return $DateTime; |
|
691 | + } |
|
692 | + |
|
693 | + |
|
694 | + |
|
695 | + /** |
|
696 | + * get_timezone_transitions |
|
697 | + * |
|
698 | + * @param \DateTimeZone $DateTimeZone |
|
699 | + * @param int $time |
|
700 | + * @param bool $first_only |
|
701 | + * @return mixed |
|
702 | + */ |
|
703 | + public function get_timezone_transitions(DateTimeZone $DateTimeZone, $time = null, $first_only = true) |
|
704 | + { |
|
705 | + return EEH_DTT_Helper::get_timezone_transitions($DateTimeZone, $time, $first_only); |
|
706 | + } |
|
707 | + |
|
708 | + |
|
709 | + |
|
710 | + /** |
|
711 | + * get_timezone_offset |
|
712 | + * |
|
713 | + * @param \DateTimeZone $DateTimeZone |
|
714 | + * @param int $time |
|
715 | + * @return mixed |
|
716 | + * @throws \DomainException |
|
717 | + */ |
|
718 | + public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
719 | + { |
|
720 | + return EEH_DTT_Helper::get_timezone_offset($DateTimeZone, $time); |
|
721 | + } |
|
722 | + |
|
723 | + |
|
724 | + /** |
|
725 | + * This will take an incoming timezone string and return the abbreviation for that timezone |
|
726 | + * |
|
727 | + * @param string $timezone_string |
|
728 | + * @return string abbreviation |
|
729 | + * @throws \EE_Error |
|
730 | + */ |
|
731 | + public function get_timezone_abbrev($timezone_string) |
|
732 | + { |
|
733 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
734 | + $dateTime = new DateTime(\EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
735 | + |
|
736 | + return $dateTime->format('T'); |
|
737 | + } |
|
738 | + |
|
739 | + /** |
|
740 | + * Overrides the parent to allow for having a dynamic "now" value |
|
741 | + * |
|
742 | + * @return mixed |
|
743 | + */ |
|
744 | + public function get_default_value() |
|
745 | + { |
|
746 | + if ($this->_default_value === EE_Datetime_Field::now) { |
|
747 | + return time(); |
|
748 | + } else { |
|
749 | + return parent::get_default_value(); |
|
750 | + } |
|
751 | + } |
|
752 | + |
|
753 | + |
|
754 | + public function getSchemaDescription() |
|
755 | + { |
|
756 | + return sprintf( |
|
757 | + esc_html__('%s - the value for this field is in the timezone of the site.', 'event_espresso'), |
|
758 | + $this->get_nicename() |
|
759 | + ); |
|
760 | + } |
|
761 | 761 | } |
@@ -212,8 +212,8 @@ discard block |
||
212 | 212 | |
213 | 213 | default: |
214 | 214 | return $pretty |
215 | - ? $this->_pretty_date_format . ' ' . $this->_pretty_time_format |
|
216 | - : $this->_date_format . ' ' . $this->_time_format; |
|
215 | + ? $this->_pretty_date_format.' '.$this->_pretty_time_format |
|
216 | + : $this->_date_format.' '.$this->_time_format; |
|
217 | 217 | } |
218 | 218 | } |
219 | 219 | |
@@ -467,7 +467,7 @@ discard block |
||
467 | 467 | */ |
468 | 468 | protected function _prepare_for_display($DateTime, $schema = false) |
469 | 469 | { |
470 | - if (! $DateTime instanceof DateTime) { |
|
470 | + if ( ! $DateTime instanceof DateTime) { |
|
471 | 471 | if ($this->_nullable) { |
472 | 472 | return ''; |
473 | 473 | } else { |
@@ -501,15 +501,15 @@ discard block |
||
501 | 501 | if ($this->_display_timezone()) { |
502 | 502 | // must be explicit because schema could equal true. |
503 | 503 | if ($schema === 'no_html') { |
504 | - $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
504 | + $timezone_string = ' ('.$DateTime->format('T').')'; |
|
505 | 505 | } else { |
506 | - $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
506 | + $timezone_string = ' <span class="ee_dtt_timezone_string">('.$DateTime->format('T').')</span>'; |
|
507 | 507 | } |
508 | 508 | } else { |
509 | 509 | $timezone_string = ''; |
510 | 510 | } |
511 | 511 | |
512 | - return $DateTime->format($format_string) . $timezone_string; |
|
512 | + return $DateTime->format($format_string).$timezone_string; |
|
513 | 513 | } |
514 | 514 | return $DateTime->format($format_string); |
515 | 515 | } |
@@ -526,7 +526,7 @@ discard block |
||
526 | 526 | public function prepare_for_use_in_db($datetime_value) |
527 | 527 | { |
528 | 528 | // we allow an empty value or DateTime object, but nothing else. |
529 | - if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
529 | + if ( ! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
530 | 530 | throw new EE_Error( |
531 | 531 | sprintf( |
532 | 532 | __( |
@@ -541,7 +541,7 @@ discard block |
||
541 | 541 | } |
542 | 542 | |
543 | 543 | if ($datetime_value instanceof DateTime) { |
544 | - if (! $datetime_value instanceof DbSafeDateTime) { |
|
544 | + if ( ! $datetime_value instanceof DbSafeDateTime) { |
|
545 | 545 | $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
546 | 546 | } |
547 | 547 | EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
@@ -586,7 +586,7 @@ discard block |
||
586 | 586 | } |
587 | 587 | } |
588 | 588 | |
589 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
589 | + if ( ! $DateTime instanceof DbSafeDateTime) { |
|
590 | 590 | // if still no datetime object, then let's just use now |
591 | 591 | $DateTime = new DbSafeDateTime(\EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
592 | 592 | } |
@@ -663,7 +663,7 @@ discard block |
||
663 | 663 | } |
664 | 664 | // not a unix timestamp. So we will use the set format on this object and set timezone to |
665 | 665 | // create the DateTime object. |
666 | - $format = $this->_date_format . ' ' . $this->_time_format; |
|
666 | + $format = $this->_date_format.' '.$this->_time_format; |
|
667 | 667 | try { |
668 | 668 | $DateTime = DateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
669 | 669 | if ($DateTime instanceof DateTime) { |
@@ -672,7 +672,7 @@ discard block |
||
672 | 672 | $this->_DateTimeZone |
673 | 673 | ); |
674 | 674 | } |
675 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
675 | + if ( ! $DateTime instanceof DbSafeDateTime) { |
|
676 | 676 | throw new EE_Error( |
677 | 677 | sprintf( |
678 | 678 | __('"%1$s" does not represent a valid Date Time in the format "%2$s".', 'event_espresso'), |
@@ -582,7 +582,7 @@ |
||
582 | 582 | /** |
583 | 583 | * Validates that the incoming format is an allowable string to use for the _schema_format property |
584 | 584 | * @throws InvalidArgumentException |
585 | - * @param $format |
|
585 | + * @param string $format |
|
586 | 586 | */ |
587 | 587 | private function validateSchemaFormat($format) |
588 | 588 | { |
@@ -19,651 +19,650 @@ |
||
19 | 19 | */ |
20 | 20 | abstract class EE_Model_Field_Base implements HasSchemaInterface |
21 | 21 | { |
22 | - /** |
|
23 | - * The alias for the table the column belongs to. |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $_table_alias; |
|
27 | - |
|
28 | - /** |
|
29 | - * The actual db column name for the table |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - protected $_table_column; |
|
33 | - |
|
34 | - |
|
35 | - /** |
|
36 | - * The authoritative name for the table column (used by client code to reference the field). |
|
37 | - * @var string |
|
38 | - */ |
|
39 | - protected $_name; |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * A description for the field. |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - protected $_nicename; |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * Whether the field is nullable or not |
|
51 | - * @var bool |
|
52 | - */ |
|
53 | - protected $_nullable; |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * What the default value for the field should be. |
|
58 | - * @var mixed |
|
59 | - */ |
|
60 | - protected $_default_value; |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * Other configuration for the field |
|
65 | - * @var mixed |
|
66 | - */ |
|
67 | - protected $_other_config; |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * The name of the model this field is instantiated for. |
|
72 | - * @var string |
|
73 | - */ |
|
74 | - protected $_model_name; |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * This should be a json-schema valid data type for the field. |
|
79 | - * @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
80 | - * @var string |
|
81 | - */ |
|
82 | - private $_schema_type = 'string'; |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * If the schema has a defined format then it should be defined via this property. |
|
87 | - * @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
88 | - * @var string |
|
89 | - */ |
|
90 | - private $_schema_format = ''; |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Indicates that the value of the field is managed exclusively by the server/model and not something |
|
95 | - * settable by client code. |
|
96 | - * @link http://json-schema.org/latest/json-schema-hypermedia.html#rfc.section.4.4 |
|
97 | - * @var bool |
|
98 | - */ |
|
99 | - private $_schema_readonly = false; |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $table_column |
|
104 | - * @param string $nicename |
|
105 | - * @param bool $nullable |
|
106 | - * @param null $default_value |
|
107 | - */ |
|
108 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
109 | - { |
|
110 | - $this->_table_column = $table_column; |
|
111 | - $this->_nicename = $nicename; |
|
112 | - $this->_nullable = $nullable; |
|
113 | - $this->_default_value = $default_value; |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * @param $table_alias |
|
119 | - * @param $name |
|
120 | - * @param $model_name |
|
121 | - */ |
|
122 | - public function _construct_finalize($table_alias, $name, $model_name) |
|
123 | - { |
|
124 | - $this->_table_alias = $table_alias; |
|
125 | - $this->_name = $name; |
|
126 | - $this->_model_name = $model_name; |
|
127 | - /** |
|
128 | - * allow for changing the defaults |
|
129 | - */ |
|
130 | - $this->_nicename = apply_filters( |
|
131 | - 'FHEE__EE_Model_Field_Base___construct_finalize___nicename', |
|
132 | - $this->_nicename, |
|
133 | - $this |
|
134 | - ); |
|
135 | - $this->_default_value = apply_filters( |
|
136 | - 'FHEE__EE_Model_Field_Base___construct_finalize___default_value', |
|
137 | - $this->_default_value, |
|
138 | - $this |
|
139 | - ); |
|
140 | - } |
|
141 | - |
|
142 | - public function get_table_alias() |
|
143 | - { |
|
144 | - return $this->_table_alias; |
|
145 | - } |
|
146 | - |
|
147 | - public function get_table_column() |
|
148 | - { |
|
149 | - return $this->_table_column; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Returns the name of the model this field is on. Eg 'Event' or 'Ticket_Datetime' |
|
154 | - * |
|
155 | - * @return string |
|
156 | - */ |
|
157 | - public function get_model_name() |
|
158 | - { |
|
159 | - return $this->_model_name; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @throws \EE_Error |
|
164 | - * @return string |
|
165 | - */ |
|
166 | - public function get_name() |
|
167 | - { |
|
168 | - if ($this->_name) { |
|
169 | - return $this->_name; |
|
170 | - } else { |
|
171 | - throw new EE_Error(sprintf(__( |
|
172 | - "Model field '%s' has no name set. Did you make a model and forget to call the parent model constructor?", |
|
173 | - "event_espresso" |
|
174 | - ), get_class($this))); |
|
175 | - } |
|
176 | - } |
|
177 | - |
|
178 | - public function get_nicename() |
|
179 | - { |
|
180 | - return $this->_nicename; |
|
181 | - } |
|
182 | - |
|
183 | - public function is_nullable() |
|
184 | - { |
|
185 | - return $this->_nullable; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * returns whether this field is an auto-increment field or not. If it is, then |
|
190 | - * on insertion it can be null. However, on updates it must be present. |
|
191 | - * |
|
192 | - * @return boolean |
|
193 | - */ |
|
194 | - public function is_auto_increment() |
|
195 | - { |
|
196 | - return false; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * The default value in the model object's value domain. See lengthy comment about |
|
201 | - * value domains at the top of EEM_Base |
|
202 | - * |
|
203 | - * @return mixed |
|
204 | - */ |
|
205 | - public function get_default_value() |
|
206 | - { |
|
207 | - return $this->_default_value; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Returns the table alias joined to the table column, however this isn't the right |
|
212 | - * table alias if the aliased table is being joined to. In that case, you can use |
|
213 | - * EE_Model_Parser::extract_table_alias_model_relation_chain_prefix() to find the table's current alias |
|
214 | - * in the current query |
|
215 | - * |
|
216 | - * @return string |
|
217 | - */ |
|
218 | - public function get_qualified_column() |
|
219 | - { |
|
220 | - return $this->get_table_alias() . "." . $this->get_table_column(); |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * When get() is called on a model object (eg EE_Event), before returning its value, |
|
225 | - * call this function on it, allowing us to customize the returned value based on |
|
226 | - * the field's type. Eg, we may want to unserialize it, strip tags, etc. By default, |
|
227 | - * we simply return it. |
|
228 | - * |
|
229 | - * @param mixed $value_of_field_on_model_object |
|
230 | - * @return mixed |
|
231 | - */ |
|
232 | - public function prepare_for_get($value_of_field_on_model_object) |
|
233 | - { |
|
234 | - return $value_of_field_on_model_object; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * When inserting or updating a field on a model object, run this function on each |
|
239 | - * value to prepare it for insertion into the db. Generally this converts |
|
240 | - * the validated input on the model object into the format used in the DB. |
|
241 | - * |
|
242 | - * @param mixed $value_of_field_on_model_object |
|
243 | - * @return mixed |
|
244 | - */ |
|
245 | - public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
246 | - { |
|
247 | - return $value_of_field_on_model_object; |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * When creating a brand-new model object, or setting a particular value for one of its fields, this function |
|
252 | - * is called before setting it on the model object. We may want to strip slashes, unserialize the value, etc. |
|
253 | - * By default, we do nothing. |
|
254 | - * |
|
255 | - * If the model field is going to perform any validation on the input, this is where it should be done |
|
256 | - * (once the value is on the model object, it may be used in other ways besides putting it into the DB |
|
257 | - * so it's best to validate it right away). |
|
258 | - * |
|
259 | - * @param mixed $value_inputted_for_field_on_model_object |
|
260 | - * @return mixed |
|
261 | - */ |
|
262 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
263 | - { |
|
264 | - return $value_inputted_for_field_on_model_object; |
|
265 | - } |
|
266 | - |
|
267 | - |
|
268 | - /** |
|
269 | - * When instantiating a model object from DB results, this function is called before setting each field. |
|
270 | - * We may want to serialize the value, etc. By default, we return the value using prepare_for_set() method as that |
|
271 | - * is the one child classes will most often define. |
|
272 | - * |
|
273 | - * @param mixed $value_found_in_db_for_model_object |
|
274 | - * @return mixed |
|
275 | - */ |
|
276 | - public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
277 | - { |
|
278 | - return $this->prepare_for_set($value_found_in_db_for_model_object); |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * When echoing a field's value on a model object, this function is run to prepare the value for presentation in a |
|
283 | - * webpage. For example, we may want to output floats with 2 decimal places by default, dates as "Monday Jan 12, |
|
284 | - * 2013, at 3:23pm" instead of |
|
285 | - * "8765678632", or any other modifications to how the value should be displayed, but not modified itself. |
|
286 | - * |
|
287 | - * @param mixed $value_on_field_to_be_outputted |
|
288 | - * @return mixed |
|
289 | - */ |
|
290 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
291 | - { |
|
292 | - return $value_on_field_to_be_outputted; |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * Returns whatever is set as the nicename for the object. |
|
298 | - * @return string |
|
299 | - */ |
|
300 | - public function getSchemaDescription() |
|
301 | - { |
|
302 | - return $this->get_nicename(); |
|
303 | - } |
|
304 | - |
|
305 | - |
|
306 | - /** |
|
307 | - * Returns whatever is set as the $_schema_type property for the object. |
|
308 | - * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
309 | - * @return string|array |
|
310 | - */ |
|
311 | - public function getSchemaType() |
|
312 | - { |
|
313 | - if ($this->is_nullable()) { |
|
314 | - $this->_schema_type = (array) $this->_schema_type; |
|
315 | - if (! in_array('null', $this->_schema_type)) { |
|
316 | - $this->_schema_type[] = 'null'; |
|
317 | - }; |
|
318 | - } |
|
319 | - return $this->_schema_type; |
|
320 | - } |
|
321 | - |
|
322 | - |
|
323 | - /** |
|
324 | - * Sets the _schema_type property. Child classes should call this in their constructors to override the default state |
|
325 | - * for this property. |
|
326 | - * @param string|array $type |
|
327 | - * @throws InvalidArgumentException |
|
328 | - */ |
|
329 | - protected function setSchemaType($type) |
|
330 | - { |
|
331 | - $this->validateSchemaType($type); |
|
332 | - $this->_schema_type = $type; |
|
333 | - } |
|
334 | - |
|
335 | - |
|
336 | - /** |
|
337 | - * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
338 | - * this method and return the properties for the schema. |
|
339 | - * |
|
340 | - * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
341 | - * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
342 | - * |
|
343 | - * @return array |
|
344 | - */ |
|
345 | - public function getSchemaProperties() |
|
346 | - { |
|
347 | - return array(); |
|
348 | - } |
|
349 | - |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * By default this returns the scalar default value that was sent in on the class prepped according to the class type |
|
354 | - * as the default. However, when there are schema properties, then the default property is setup to mirror the |
|
355 | - * property keys and correctly prepare the default according to that expected property value. |
|
356 | - * The getSchema method validates whether the schema for default is setup correctly or not according to the schema type |
|
357 | - * |
|
358 | - * @return mixed |
|
359 | - */ |
|
360 | - public function getSchemaDefault() |
|
361 | - { |
|
362 | - $default_value = $this->prepare_for_use_in_db($this->prepare_for_set($this->get_default_value())); |
|
363 | - $schema_properties = $this->getSchemaProperties(); |
|
364 | - |
|
365 | - // if this schema has properties than shape the default value to match the properties shape. |
|
366 | - if ($schema_properties) { |
|
367 | - $value_to_return = array(); |
|
368 | - foreach ($schema_properties as $property_key => $property_schema) { |
|
369 | - switch ($property_key) { |
|
370 | - case 'pretty': |
|
371 | - case 'rendered': |
|
372 | - $value_to_return[ $property_key ] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
373 | - break; |
|
374 | - default: |
|
375 | - $value_to_return[ $property_key ] = $default_value; |
|
376 | - break; |
|
377 | - } |
|
378 | - } |
|
379 | - $default_value = $value_to_return; |
|
380 | - } |
|
381 | - return $default_value; |
|
382 | - } |
|
383 | - |
|
384 | - |
|
385 | - |
|
386 | - |
|
387 | - /** |
|
388 | - * If a child class has enum values, they should override this method and provide a simple array |
|
389 | - * of the enum values. |
|
390 | - |
|
391 | - * The reason this is not a property on the class is because there may be filterable enum values that |
|
392 | - * are set on the instantiated object that could be filtered after construct. |
|
393 | - * |
|
394 | - * @return array |
|
395 | - */ |
|
396 | - public function getSchemaEnum() |
|
397 | - { |
|
398 | - return array(); |
|
399 | - } |
|
400 | - |
|
401 | - |
|
402 | - /** |
|
403 | - * This returns the value of the $_schema_format property on the object. |
|
404 | - * @return string |
|
405 | - */ |
|
406 | - public function getSchemaFormat() |
|
407 | - { |
|
408 | - return $this->_schema_format; |
|
409 | - } |
|
410 | - |
|
411 | - |
|
412 | - /** |
|
413 | - * Sets the schema format property. |
|
414 | - * @throws InvalidArgumentException |
|
415 | - * @param string $format |
|
416 | - */ |
|
417 | - protected function setSchemaFormat($format) |
|
418 | - { |
|
419 | - $this->validateSchemaFormat($format); |
|
420 | - $this->_schema_format = $format; |
|
421 | - } |
|
422 | - |
|
423 | - |
|
424 | - /** |
|
425 | - * This returns the value of the $_schema_readonly property on the object. |
|
426 | - * @return bool |
|
427 | - */ |
|
428 | - public function getSchemaReadonly() |
|
429 | - { |
|
430 | - return $this->_schema_readonly; |
|
431 | - } |
|
432 | - |
|
433 | - |
|
434 | - /** |
|
435 | - * This sets the value for the $_schema_readonly property. |
|
436 | - * @param bool $readonly (only explicit boolean values are accepted) |
|
437 | - */ |
|
438 | - protected function setSchemaReadOnly($readonly) |
|
439 | - { |
|
440 | - if (! is_bool($readonly)) { |
|
441 | - throw new InvalidArgumentException( |
|
442 | - sprintf( |
|
443 | - esc_html__('The incoming argument (%s) must be a boolean.', 'event_espresso'), |
|
444 | - print_r($readonly, true) |
|
445 | - ) |
|
446 | - ); |
|
447 | - } |
|
448 | - |
|
449 | - $this->_schema_readonly = $readonly; |
|
450 | - } |
|
451 | - |
|
452 | - |
|
453 | - |
|
454 | - |
|
455 | - /** |
|
456 | - * Return `%d`, `%s` or `%f` to indicate the data type for the field. |
|
457 | - * @uses _get_wpdb_data_type() |
|
458 | - * |
|
459 | - * @return string |
|
460 | - */ |
|
461 | - public function get_wpdb_data_type() |
|
462 | - { |
|
463 | - return $this->_get_wpdb_data_type(); |
|
464 | - } |
|
465 | - |
|
466 | - |
|
467 | - /** |
|
468 | - * Return `%d`, `%s` or `%f` to indicate the data type for the field that should be indicated in wpdb queries. |
|
469 | - * @param string $type Included if a specific type is requested. |
|
470 | - * @uses get_schema_type() |
|
471 | - * @return string |
|
472 | - */ |
|
473 | - protected function _get_wpdb_data_type($type = '') |
|
474 | - { |
|
475 | - $type = empty($type) ? $this->getSchemaType() : $type; |
|
476 | - |
|
477 | - // if type is an array, then different parsing is required. |
|
478 | - if (is_array($type)) { |
|
479 | - return $this->_get_wpdb_data_type_for_type_array($type); |
|
480 | - } |
|
481 | - |
|
482 | - $wpdb_type = '%s'; |
|
483 | - switch ($type) { |
|
484 | - case 'number': |
|
485 | - $wpdb_type = '%f'; |
|
486 | - break; |
|
487 | - case 'integer': |
|
488 | - case 'boolean': |
|
489 | - $wpdb_type = '%d'; |
|
490 | - break; |
|
491 | - case 'object': |
|
492 | - $properties = $this->getSchemaProperties(); |
|
493 | - if (isset($properties['raw'], $properties['raw']['type'])) { |
|
494 | - $wpdb_type = $this->_get_wpdb_data_type($properties['raw']['type']); |
|
495 | - } |
|
496 | - break; // leave at default |
|
497 | - } |
|
498 | - return $wpdb_type; |
|
499 | - } |
|
500 | - |
|
501 | - |
|
502 | - |
|
503 | - protected function _get_wpdb_data_type_for_type_array($type) |
|
504 | - { |
|
505 | - $type = (array) $type; |
|
506 | - // first let's flip because then we can do a faster key check |
|
507 | - $type = array_flip($type); |
|
508 | - |
|
509 | - // check for things that mean '%s' |
|
510 | - if (isset($type['string'], $type['object'], $type['array'])) { |
|
511 | - return '%s'; |
|
512 | - } |
|
513 | - |
|
514 | - // if makes it past the above condition and there's float in the array |
|
515 | - // then the type is %f |
|
516 | - if (isset($type['number'])) { |
|
517 | - return '%f'; |
|
518 | - } |
|
519 | - |
|
520 | - // if it makes it above the above conditions and there is an integer in the array |
|
521 | - // then the type is %d |
|
522 | - if (isset($type['integer'])) { |
|
523 | - return '%d'; |
|
524 | - } |
|
525 | - |
|
526 | - // anything else is a string |
|
527 | - return '%s'; |
|
528 | - } |
|
529 | - |
|
530 | - |
|
531 | - /** |
|
532 | - * This returns elements used to represent this field in the json schema. |
|
533 | - * |
|
534 | - * @link http://json-schema.org/ |
|
535 | - * @return array |
|
536 | - */ |
|
537 | - public function getSchema() |
|
538 | - { |
|
539 | - $schema = array( |
|
540 | - 'description' => $this->getSchemaDescription(), |
|
541 | - 'type' => $this->getSchemaType(), |
|
542 | - 'readonly' => $this->getSchemaReadonly(), |
|
543 | - 'default' => $this->getSchemaDefault() |
|
544 | - ); |
|
545 | - |
|
546 | - // optional properties of the schema |
|
547 | - $enum = $this->getSchemaEnum(); |
|
548 | - $properties = $this->getSchemaProperties(); |
|
549 | - $format = $this->getSchemaFormat(); |
|
550 | - if ($enum) { |
|
551 | - $schema['enum'] = $enum; |
|
552 | - } |
|
553 | - |
|
554 | - if ($properties) { |
|
555 | - $schema['properties'] = $properties; |
|
556 | - } |
|
557 | - |
|
558 | - if ($format) { |
|
559 | - $schema['format'] = $format; |
|
560 | - } |
|
561 | - return $schema; |
|
562 | - } |
|
563 | - |
|
564 | - /** |
|
565 | - * Some fields are in the database-only, (ie, used in queries etc), but shouldn't necessarily be part |
|
566 | - * of the model objects (ie, client code shouldn't care to ever see their value... if client code does |
|
567 | - * want to see their value, then they shouldn't be db-only fields!) |
|
568 | - * Eg, when doing events as custom post types, querying the post_type is essential, but |
|
569 | - * post_type is irrelevant for EE_Event objects (because they will ALL be of post_type 'esp_event'). |
|
570 | - * By default, all fields aren't db-only. |
|
571 | - * |
|
572 | - * @return boolean |
|
573 | - */ |
|
574 | - public function is_db_only_field() |
|
575 | - { |
|
576 | - return false; |
|
577 | - } |
|
578 | - |
|
579 | - |
|
580 | - /** |
|
581 | - * Validates the incoming string|array to ensure its an allowable type. |
|
582 | - * @throws InvalidArgumentException |
|
583 | - * @param string|array $type |
|
584 | - */ |
|
585 | - private function validateSchemaType($type) |
|
586 | - { |
|
587 | - if (! (is_string($type) || is_array($type))) { |
|
588 | - throw new InvalidArgumentException( |
|
589 | - sprintf( |
|
590 | - esc_html__('The incoming argument (%s) must be a string or an array.', 'event_espresso'), |
|
591 | - print_r($type, true) |
|
592 | - ) |
|
593 | - ); |
|
594 | - } |
|
595 | - |
|
596 | - // validate allowable types. |
|
597 | - // @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
598 | - $allowable_types = array_flip( |
|
599 | - array( |
|
600 | - 'string', |
|
601 | - 'number', |
|
602 | - 'null', |
|
603 | - 'object', |
|
604 | - 'array', |
|
605 | - 'boolean', |
|
606 | - 'integer' |
|
607 | - ) |
|
608 | - ); |
|
609 | - |
|
610 | - if (is_array($type)) { |
|
611 | - foreach ($type as $item_in_type) { |
|
612 | - $this->validateSchemaType($item_in_type); |
|
613 | - } |
|
614 | - return; |
|
615 | - } |
|
616 | - |
|
617 | - if (! isset($allowable_types[ $type ])) { |
|
618 | - throw new InvalidArgumentException( |
|
619 | - sprintf( |
|
620 | - esc_html__('The incoming argument (%1$s) must be one of the allowable types: %2$s', 'event_espresso'), |
|
621 | - $type, |
|
622 | - implode(',', array_flip($allowable_types)) |
|
623 | - ) |
|
624 | - ); |
|
625 | - } |
|
626 | - } |
|
627 | - |
|
628 | - |
|
629 | - /** |
|
630 | - * Validates that the incoming format is an allowable string to use for the _schema_format property |
|
631 | - * @throws InvalidArgumentException |
|
632 | - * @param $format |
|
633 | - */ |
|
634 | - private function validateSchemaFormat($format) |
|
635 | - { |
|
636 | - if (! is_string($format)) { |
|
637 | - throw new InvalidArgumentException( |
|
638 | - sprintf( |
|
639 | - esc_html__('The incoming argument (%s) must be a string.', 'event_espresso'), |
|
640 | - print_r($format, true) |
|
641 | - ) |
|
642 | - ); |
|
643 | - } |
|
644 | - |
|
645 | - // validate allowable format values |
|
646 | - // @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
647 | - $allowable_formats = array_flip( |
|
648 | - array( |
|
649 | - 'date-time', |
|
650 | - 'email', |
|
651 | - 'hostname', |
|
652 | - 'ipv4', |
|
653 | - 'ipv6', |
|
654 | - 'uri', |
|
655 | - 'uriref' |
|
656 | - ) |
|
657 | - ); |
|
658 | - |
|
659 | - if (! isset($allowable_formats[ $format ])) { |
|
660 | - throw new InvalidArgumentException( |
|
661 | - sprintf( |
|
662 | - esc_html__('The incoming argument (%1$s) must be one of the allowable formats: %2$s', 'event_espresso'), |
|
663 | - $format, |
|
664 | - implode(',', array_flip($allowable_formats)) |
|
665 | - ) |
|
666 | - ); |
|
667 | - } |
|
668 | - } |
|
22 | + /** |
|
23 | + * The alias for the table the column belongs to. |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $_table_alias; |
|
27 | + |
|
28 | + /** |
|
29 | + * The actual db column name for the table |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + protected $_table_column; |
|
33 | + |
|
34 | + |
|
35 | + /** |
|
36 | + * The authoritative name for the table column (used by client code to reference the field). |
|
37 | + * @var string |
|
38 | + */ |
|
39 | + protected $_name; |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * A description for the field. |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + protected $_nicename; |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * Whether the field is nullable or not |
|
51 | + * @var bool |
|
52 | + */ |
|
53 | + protected $_nullable; |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * What the default value for the field should be. |
|
58 | + * @var mixed |
|
59 | + */ |
|
60 | + protected $_default_value; |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * Other configuration for the field |
|
65 | + * @var mixed |
|
66 | + */ |
|
67 | + protected $_other_config; |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * The name of the model this field is instantiated for. |
|
72 | + * @var string |
|
73 | + */ |
|
74 | + protected $_model_name; |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * This should be a json-schema valid data type for the field. |
|
79 | + * @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
80 | + * @var string |
|
81 | + */ |
|
82 | + private $_schema_type = 'string'; |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * If the schema has a defined format then it should be defined via this property. |
|
87 | + * @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
88 | + * @var string |
|
89 | + */ |
|
90 | + private $_schema_format = ''; |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Indicates that the value of the field is managed exclusively by the server/model and not something |
|
95 | + * settable by client code. |
|
96 | + * @link http://json-schema.org/latest/json-schema-hypermedia.html#rfc.section.4.4 |
|
97 | + * @var bool |
|
98 | + */ |
|
99 | + private $_schema_readonly = false; |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $table_column |
|
104 | + * @param string $nicename |
|
105 | + * @param bool $nullable |
|
106 | + * @param null $default_value |
|
107 | + */ |
|
108 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
109 | + { |
|
110 | + $this->_table_column = $table_column; |
|
111 | + $this->_nicename = $nicename; |
|
112 | + $this->_nullable = $nullable; |
|
113 | + $this->_default_value = $default_value; |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * @param $table_alias |
|
119 | + * @param $name |
|
120 | + * @param $model_name |
|
121 | + */ |
|
122 | + public function _construct_finalize($table_alias, $name, $model_name) |
|
123 | + { |
|
124 | + $this->_table_alias = $table_alias; |
|
125 | + $this->_name = $name; |
|
126 | + $this->_model_name = $model_name; |
|
127 | + /** |
|
128 | + * allow for changing the defaults |
|
129 | + */ |
|
130 | + $this->_nicename = apply_filters( |
|
131 | + 'FHEE__EE_Model_Field_Base___construct_finalize___nicename', |
|
132 | + $this->_nicename, |
|
133 | + $this |
|
134 | + ); |
|
135 | + $this->_default_value = apply_filters( |
|
136 | + 'FHEE__EE_Model_Field_Base___construct_finalize___default_value', |
|
137 | + $this->_default_value, |
|
138 | + $this |
|
139 | + ); |
|
140 | + } |
|
141 | + |
|
142 | + public function get_table_alias() |
|
143 | + { |
|
144 | + return $this->_table_alias; |
|
145 | + } |
|
146 | + |
|
147 | + public function get_table_column() |
|
148 | + { |
|
149 | + return $this->_table_column; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Returns the name of the model this field is on. Eg 'Event' or 'Ticket_Datetime' |
|
154 | + * |
|
155 | + * @return string |
|
156 | + */ |
|
157 | + public function get_model_name() |
|
158 | + { |
|
159 | + return $this->_model_name; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @throws \EE_Error |
|
164 | + * @return string |
|
165 | + */ |
|
166 | + public function get_name() |
|
167 | + { |
|
168 | + if ($this->_name) { |
|
169 | + return $this->_name; |
|
170 | + } else { |
|
171 | + throw new EE_Error(sprintf(__( |
|
172 | + "Model field '%s' has no name set. Did you make a model and forget to call the parent model constructor?", |
|
173 | + "event_espresso" |
|
174 | + ), get_class($this))); |
|
175 | + } |
|
176 | + } |
|
177 | + |
|
178 | + public function get_nicename() |
|
179 | + { |
|
180 | + return $this->_nicename; |
|
181 | + } |
|
182 | + |
|
183 | + public function is_nullable() |
|
184 | + { |
|
185 | + return $this->_nullable; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * returns whether this field is an auto-increment field or not. If it is, then |
|
190 | + * on insertion it can be null. However, on updates it must be present. |
|
191 | + * |
|
192 | + * @return boolean |
|
193 | + */ |
|
194 | + public function is_auto_increment() |
|
195 | + { |
|
196 | + return false; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * The default value in the model object's value domain. See lengthy comment about |
|
201 | + * value domains at the top of EEM_Base |
|
202 | + * |
|
203 | + * @return mixed |
|
204 | + */ |
|
205 | + public function get_default_value() |
|
206 | + { |
|
207 | + return $this->_default_value; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Returns the table alias joined to the table column, however this isn't the right |
|
212 | + * table alias if the aliased table is being joined to. In that case, you can use |
|
213 | + * EE_Model_Parser::extract_table_alias_model_relation_chain_prefix() to find the table's current alias |
|
214 | + * in the current query |
|
215 | + * |
|
216 | + * @return string |
|
217 | + */ |
|
218 | + public function get_qualified_column() |
|
219 | + { |
|
220 | + return $this->get_table_alias() . "." . $this->get_table_column(); |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * When get() is called on a model object (eg EE_Event), before returning its value, |
|
225 | + * call this function on it, allowing us to customize the returned value based on |
|
226 | + * the field's type. Eg, we may want to unserialize it, strip tags, etc. By default, |
|
227 | + * we simply return it. |
|
228 | + * |
|
229 | + * @param mixed $value_of_field_on_model_object |
|
230 | + * @return mixed |
|
231 | + */ |
|
232 | + public function prepare_for_get($value_of_field_on_model_object) |
|
233 | + { |
|
234 | + return $value_of_field_on_model_object; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * When inserting or updating a field on a model object, run this function on each |
|
239 | + * value to prepare it for insertion into the db. Generally this converts |
|
240 | + * the validated input on the model object into the format used in the DB. |
|
241 | + * |
|
242 | + * @param mixed $value_of_field_on_model_object |
|
243 | + * @return mixed |
|
244 | + */ |
|
245 | + public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
246 | + { |
|
247 | + return $value_of_field_on_model_object; |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * When creating a brand-new model object, or setting a particular value for one of its fields, this function |
|
252 | + * is called before setting it on the model object. We may want to strip slashes, unserialize the value, etc. |
|
253 | + * By default, we do nothing. |
|
254 | + * |
|
255 | + * If the model field is going to perform any validation on the input, this is where it should be done |
|
256 | + * (once the value is on the model object, it may be used in other ways besides putting it into the DB |
|
257 | + * so it's best to validate it right away). |
|
258 | + * |
|
259 | + * @param mixed $value_inputted_for_field_on_model_object |
|
260 | + * @return mixed |
|
261 | + */ |
|
262 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
263 | + { |
|
264 | + return $value_inputted_for_field_on_model_object; |
|
265 | + } |
|
266 | + |
|
267 | + |
|
268 | + /** |
|
269 | + * When instantiating a model object from DB results, this function is called before setting each field. |
|
270 | + * We may want to serialize the value, etc. By default, we return the value using prepare_for_set() method as that |
|
271 | + * is the one child classes will most often define. |
|
272 | + * |
|
273 | + * @param mixed $value_found_in_db_for_model_object |
|
274 | + * @return mixed |
|
275 | + */ |
|
276 | + public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
277 | + { |
|
278 | + return $this->prepare_for_set($value_found_in_db_for_model_object); |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * When echoing a field's value on a model object, this function is run to prepare the value for presentation in a |
|
283 | + * webpage. For example, we may want to output floats with 2 decimal places by default, dates as "Monday Jan 12, |
|
284 | + * 2013, at 3:23pm" instead of |
|
285 | + * "8765678632", or any other modifications to how the value should be displayed, but not modified itself. |
|
286 | + * |
|
287 | + * @param mixed $value_on_field_to_be_outputted |
|
288 | + * @return mixed |
|
289 | + */ |
|
290 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
291 | + { |
|
292 | + return $value_on_field_to_be_outputted; |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * Returns whatever is set as the nicename for the object. |
|
298 | + * @return string |
|
299 | + */ |
|
300 | + public function getSchemaDescription() |
|
301 | + { |
|
302 | + return $this->get_nicename(); |
|
303 | + } |
|
304 | + |
|
305 | + |
|
306 | + /** |
|
307 | + * Returns whatever is set as the $_schema_type property for the object. |
|
308 | + * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
309 | + * @return string|array |
|
310 | + */ |
|
311 | + public function getSchemaType() |
|
312 | + { |
|
313 | + if ($this->is_nullable()) { |
|
314 | + $this->_schema_type = (array) $this->_schema_type; |
|
315 | + if (! in_array('null', $this->_schema_type)) { |
|
316 | + $this->_schema_type[] = 'null'; |
|
317 | + }; |
|
318 | + } |
|
319 | + return $this->_schema_type; |
|
320 | + } |
|
321 | + |
|
322 | + |
|
323 | + /** |
|
324 | + * Sets the _schema_type property. Child classes should call this in their constructors to override the default state |
|
325 | + * for this property. |
|
326 | + * @param string|array $type |
|
327 | + * @throws InvalidArgumentException |
|
328 | + */ |
|
329 | + protected function setSchemaType($type) |
|
330 | + { |
|
331 | + $this->validateSchemaType($type); |
|
332 | + $this->_schema_type = $type; |
|
333 | + } |
|
334 | + |
|
335 | + |
|
336 | + /** |
|
337 | + * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
338 | + * this method and return the properties for the schema. |
|
339 | + * |
|
340 | + * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
341 | + * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
342 | + * |
|
343 | + * @return array |
|
344 | + */ |
|
345 | + public function getSchemaProperties() |
|
346 | + { |
|
347 | + return array(); |
|
348 | + } |
|
349 | + |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * By default this returns the scalar default value that was sent in on the class prepped according to the class type |
|
354 | + * as the default. However, when there are schema properties, then the default property is setup to mirror the |
|
355 | + * property keys and correctly prepare the default according to that expected property value. |
|
356 | + * The getSchema method validates whether the schema for default is setup correctly or not according to the schema type |
|
357 | + * |
|
358 | + * @return mixed |
|
359 | + */ |
|
360 | + public function getSchemaDefault() |
|
361 | + { |
|
362 | + $default_value = $this->prepare_for_use_in_db($this->prepare_for_set($this->get_default_value())); |
|
363 | + $schema_properties = $this->getSchemaProperties(); |
|
364 | + |
|
365 | + // if this schema has properties than shape the default value to match the properties shape. |
|
366 | + if ($schema_properties) { |
|
367 | + $value_to_return = array(); |
|
368 | + foreach ($schema_properties as $property_key => $property_schema) { |
|
369 | + switch ($property_key) { |
|
370 | + case 'pretty': |
|
371 | + case 'rendered': |
|
372 | + $value_to_return[ $property_key ] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
373 | + break; |
|
374 | + default: |
|
375 | + $value_to_return[ $property_key ] = $default_value; |
|
376 | + break; |
|
377 | + } |
|
378 | + } |
|
379 | + $default_value = $value_to_return; |
|
380 | + } |
|
381 | + return $default_value; |
|
382 | + } |
|
383 | + |
|
384 | + |
|
385 | + |
|
386 | + |
|
387 | + /** |
|
388 | + * If a child class has enum values, they should override this method and provide a simple array |
|
389 | + * of the enum values. |
|
390 | + * The reason this is not a property on the class is because there may be filterable enum values that |
|
391 | + * are set on the instantiated object that could be filtered after construct. |
|
392 | + * |
|
393 | + * @return array |
|
394 | + */ |
|
395 | + public function getSchemaEnum() |
|
396 | + { |
|
397 | + return array(); |
|
398 | + } |
|
399 | + |
|
400 | + |
|
401 | + /** |
|
402 | + * This returns the value of the $_schema_format property on the object. |
|
403 | + * @return string |
|
404 | + */ |
|
405 | + public function getSchemaFormat() |
|
406 | + { |
|
407 | + return $this->_schema_format; |
|
408 | + } |
|
409 | + |
|
410 | + |
|
411 | + /** |
|
412 | + * Sets the schema format property. |
|
413 | + * @throws InvalidArgumentException |
|
414 | + * @param string $format |
|
415 | + */ |
|
416 | + protected function setSchemaFormat($format) |
|
417 | + { |
|
418 | + $this->validateSchemaFormat($format); |
|
419 | + $this->_schema_format = $format; |
|
420 | + } |
|
421 | + |
|
422 | + |
|
423 | + /** |
|
424 | + * This returns the value of the $_schema_readonly property on the object. |
|
425 | + * @return bool |
|
426 | + */ |
|
427 | + public function getSchemaReadonly() |
|
428 | + { |
|
429 | + return $this->_schema_readonly; |
|
430 | + } |
|
431 | + |
|
432 | + |
|
433 | + /** |
|
434 | + * This sets the value for the $_schema_readonly property. |
|
435 | + * @param bool $readonly (only explicit boolean values are accepted) |
|
436 | + */ |
|
437 | + protected function setSchemaReadOnly($readonly) |
|
438 | + { |
|
439 | + if (! is_bool($readonly)) { |
|
440 | + throw new InvalidArgumentException( |
|
441 | + sprintf( |
|
442 | + esc_html__('The incoming argument (%s) must be a boolean.', 'event_espresso'), |
|
443 | + print_r($readonly, true) |
|
444 | + ) |
|
445 | + ); |
|
446 | + } |
|
447 | + |
|
448 | + $this->_schema_readonly = $readonly; |
|
449 | + } |
|
450 | + |
|
451 | + |
|
452 | + |
|
453 | + |
|
454 | + /** |
|
455 | + * Return `%d`, `%s` or `%f` to indicate the data type for the field. |
|
456 | + * @uses _get_wpdb_data_type() |
|
457 | + * |
|
458 | + * @return string |
|
459 | + */ |
|
460 | + public function get_wpdb_data_type() |
|
461 | + { |
|
462 | + return $this->_get_wpdb_data_type(); |
|
463 | + } |
|
464 | + |
|
465 | + |
|
466 | + /** |
|
467 | + * Return `%d`, `%s` or `%f` to indicate the data type for the field that should be indicated in wpdb queries. |
|
468 | + * @param string $type Included if a specific type is requested. |
|
469 | + * @uses get_schema_type() |
|
470 | + * @return string |
|
471 | + */ |
|
472 | + protected function _get_wpdb_data_type($type = '') |
|
473 | + { |
|
474 | + $type = empty($type) ? $this->getSchemaType() : $type; |
|
475 | + |
|
476 | + // if type is an array, then different parsing is required. |
|
477 | + if (is_array($type)) { |
|
478 | + return $this->_get_wpdb_data_type_for_type_array($type); |
|
479 | + } |
|
480 | + |
|
481 | + $wpdb_type = '%s'; |
|
482 | + switch ($type) { |
|
483 | + case 'number': |
|
484 | + $wpdb_type = '%f'; |
|
485 | + break; |
|
486 | + case 'integer': |
|
487 | + case 'boolean': |
|
488 | + $wpdb_type = '%d'; |
|
489 | + break; |
|
490 | + case 'object': |
|
491 | + $properties = $this->getSchemaProperties(); |
|
492 | + if (isset($properties['raw'], $properties['raw']['type'])) { |
|
493 | + $wpdb_type = $this->_get_wpdb_data_type($properties['raw']['type']); |
|
494 | + } |
|
495 | + break; // leave at default |
|
496 | + } |
|
497 | + return $wpdb_type; |
|
498 | + } |
|
499 | + |
|
500 | + |
|
501 | + |
|
502 | + protected function _get_wpdb_data_type_for_type_array($type) |
|
503 | + { |
|
504 | + $type = (array) $type; |
|
505 | + // first let's flip because then we can do a faster key check |
|
506 | + $type = array_flip($type); |
|
507 | + |
|
508 | + // check for things that mean '%s' |
|
509 | + if (isset($type['string'], $type['object'], $type['array'])) { |
|
510 | + return '%s'; |
|
511 | + } |
|
512 | + |
|
513 | + // if makes it past the above condition and there's float in the array |
|
514 | + // then the type is %f |
|
515 | + if (isset($type['number'])) { |
|
516 | + return '%f'; |
|
517 | + } |
|
518 | + |
|
519 | + // if it makes it above the above conditions and there is an integer in the array |
|
520 | + // then the type is %d |
|
521 | + if (isset($type['integer'])) { |
|
522 | + return '%d'; |
|
523 | + } |
|
524 | + |
|
525 | + // anything else is a string |
|
526 | + return '%s'; |
|
527 | + } |
|
528 | + |
|
529 | + |
|
530 | + /** |
|
531 | + * This returns elements used to represent this field in the json schema. |
|
532 | + * |
|
533 | + * @link http://json-schema.org/ |
|
534 | + * @return array |
|
535 | + */ |
|
536 | + public function getSchema() |
|
537 | + { |
|
538 | + $schema = array( |
|
539 | + 'description' => $this->getSchemaDescription(), |
|
540 | + 'type' => $this->getSchemaType(), |
|
541 | + 'readonly' => $this->getSchemaReadonly(), |
|
542 | + 'default' => $this->getSchemaDefault() |
|
543 | + ); |
|
544 | + |
|
545 | + // optional properties of the schema |
|
546 | + $enum = $this->getSchemaEnum(); |
|
547 | + $properties = $this->getSchemaProperties(); |
|
548 | + $format = $this->getSchemaFormat(); |
|
549 | + if ($enum) { |
|
550 | + $schema['enum'] = $enum; |
|
551 | + } |
|
552 | + |
|
553 | + if ($properties) { |
|
554 | + $schema['properties'] = $properties; |
|
555 | + } |
|
556 | + |
|
557 | + if ($format) { |
|
558 | + $schema['format'] = $format; |
|
559 | + } |
|
560 | + return $schema; |
|
561 | + } |
|
562 | + |
|
563 | + /** |
|
564 | + * Some fields are in the database-only, (ie, used in queries etc), but shouldn't necessarily be part |
|
565 | + * of the model objects (ie, client code shouldn't care to ever see their value... if client code does |
|
566 | + * want to see their value, then they shouldn't be db-only fields!) |
|
567 | + * Eg, when doing events as custom post types, querying the post_type is essential, but |
|
568 | + * post_type is irrelevant for EE_Event objects (because they will ALL be of post_type 'esp_event'). |
|
569 | + * By default, all fields aren't db-only. |
|
570 | + * |
|
571 | + * @return boolean |
|
572 | + */ |
|
573 | + public function is_db_only_field() |
|
574 | + { |
|
575 | + return false; |
|
576 | + } |
|
577 | + |
|
578 | + |
|
579 | + /** |
|
580 | + * Validates the incoming string|array to ensure its an allowable type. |
|
581 | + * @throws InvalidArgumentException |
|
582 | + * @param string|array $type |
|
583 | + */ |
|
584 | + private function validateSchemaType($type) |
|
585 | + { |
|
586 | + if (! (is_string($type) || is_array($type))) { |
|
587 | + throw new InvalidArgumentException( |
|
588 | + sprintf( |
|
589 | + esc_html__('The incoming argument (%s) must be a string or an array.', 'event_espresso'), |
|
590 | + print_r($type, true) |
|
591 | + ) |
|
592 | + ); |
|
593 | + } |
|
594 | + |
|
595 | + // validate allowable types. |
|
596 | + // @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
597 | + $allowable_types = array_flip( |
|
598 | + array( |
|
599 | + 'string', |
|
600 | + 'number', |
|
601 | + 'null', |
|
602 | + 'object', |
|
603 | + 'array', |
|
604 | + 'boolean', |
|
605 | + 'integer' |
|
606 | + ) |
|
607 | + ); |
|
608 | + |
|
609 | + if (is_array($type)) { |
|
610 | + foreach ($type as $item_in_type) { |
|
611 | + $this->validateSchemaType($item_in_type); |
|
612 | + } |
|
613 | + return; |
|
614 | + } |
|
615 | + |
|
616 | + if (! isset($allowable_types[ $type ])) { |
|
617 | + throw new InvalidArgumentException( |
|
618 | + sprintf( |
|
619 | + esc_html__('The incoming argument (%1$s) must be one of the allowable types: %2$s', 'event_espresso'), |
|
620 | + $type, |
|
621 | + implode(',', array_flip($allowable_types)) |
|
622 | + ) |
|
623 | + ); |
|
624 | + } |
|
625 | + } |
|
626 | + |
|
627 | + |
|
628 | + /** |
|
629 | + * Validates that the incoming format is an allowable string to use for the _schema_format property |
|
630 | + * @throws InvalidArgumentException |
|
631 | + * @param $format |
|
632 | + */ |
|
633 | + private function validateSchemaFormat($format) |
|
634 | + { |
|
635 | + if (! is_string($format)) { |
|
636 | + throw new InvalidArgumentException( |
|
637 | + sprintf( |
|
638 | + esc_html__('The incoming argument (%s) must be a string.', 'event_espresso'), |
|
639 | + print_r($format, true) |
|
640 | + ) |
|
641 | + ); |
|
642 | + } |
|
643 | + |
|
644 | + // validate allowable format values |
|
645 | + // @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
646 | + $allowable_formats = array_flip( |
|
647 | + array( |
|
648 | + 'date-time', |
|
649 | + 'email', |
|
650 | + 'hostname', |
|
651 | + 'ipv4', |
|
652 | + 'ipv6', |
|
653 | + 'uri', |
|
654 | + 'uriref' |
|
655 | + ) |
|
656 | + ); |
|
657 | + |
|
658 | + if (! isset($allowable_formats[ $format ])) { |
|
659 | + throw new InvalidArgumentException( |
|
660 | + sprintf( |
|
661 | + esc_html__('The incoming argument (%1$s) must be one of the allowable formats: %2$s', 'event_espresso'), |
|
662 | + $format, |
|
663 | + implode(',', array_flip($allowable_formats)) |
|
664 | + ) |
|
665 | + ); |
|
666 | + } |
|
667 | + } |
|
669 | 668 | } |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | /** |
128 | 128 | * allow for changing the defaults |
129 | 129 | */ |
130 | - $this->_nicename = apply_filters( |
|
130 | + $this->_nicename = apply_filters( |
|
131 | 131 | 'FHEE__EE_Model_Field_Base___construct_finalize___nicename', |
132 | 132 | $this->_nicename, |
133 | 133 | $this |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | */ |
218 | 218 | public function get_qualified_column() |
219 | 219 | { |
220 | - return $this->get_table_alias() . "." . $this->get_table_column(); |
|
220 | + return $this->get_table_alias().".".$this->get_table_column(); |
|
221 | 221 | } |
222 | 222 | |
223 | 223 | /** |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | { |
313 | 313 | if ($this->is_nullable()) { |
314 | 314 | $this->_schema_type = (array) $this->_schema_type; |
315 | - if (! in_array('null', $this->_schema_type)) { |
|
315 | + if ( ! in_array('null', $this->_schema_type)) { |
|
316 | 316 | $this->_schema_type[] = 'null'; |
317 | 317 | }; |
318 | 318 | } |
@@ -369,10 +369,10 @@ discard block |
||
369 | 369 | switch ($property_key) { |
370 | 370 | case 'pretty': |
371 | 371 | case 'rendered': |
372 | - $value_to_return[ $property_key ] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
372 | + $value_to_return[$property_key] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
373 | 373 | break; |
374 | 374 | default: |
375 | - $value_to_return[ $property_key ] = $default_value; |
|
375 | + $value_to_return[$property_key] = $default_value; |
|
376 | 376 | break; |
377 | 377 | } |
378 | 378 | } |
@@ -437,7 +437,7 @@ discard block |
||
437 | 437 | */ |
438 | 438 | protected function setSchemaReadOnly($readonly) |
439 | 439 | { |
440 | - if (! is_bool($readonly)) { |
|
440 | + if ( ! is_bool($readonly)) { |
|
441 | 441 | throw new InvalidArgumentException( |
442 | 442 | sprintf( |
443 | 443 | esc_html__('The incoming argument (%s) must be a boolean.', 'event_espresso'), |
@@ -584,7 +584,7 @@ discard block |
||
584 | 584 | */ |
585 | 585 | private function validateSchemaType($type) |
586 | 586 | { |
587 | - if (! (is_string($type) || is_array($type))) { |
|
587 | + if ( ! (is_string($type) || is_array($type))) { |
|
588 | 588 | throw new InvalidArgumentException( |
589 | 589 | sprintf( |
590 | 590 | esc_html__('The incoming argument (%s) must be a string or an array.', 'event_espresso'), |
@@ -614,7 +614,7 @@ discard block |
||
614 | 614 | return; |
615 | 615 | } |
616 | 616 | |
617 | - if (! isset($allowable_types[ $type ])) { |
|
617 | + if ( ! isset($allowable_types[$type])) { |
|
618 | 618 | throw new InvalidArgumentException( |
619 | 619 | sprintf( |
620 | 620 | esc_html__('The incoming argument (%1$s) must be one of the allowable types: %2$s', 'event_espresso'), |
@@ -633,7 +633,7 @@ discard block |
||
633 | 633 | */ |
634 | 634 | private function validateSchemaFormat($format) |
635 | 635 | { |
636 | - if (! is_string($format)) { |
|
636 | + if ( ! is_string($format)) { |
|
637 | 637 | throw new InvalidArgumentException( |
638 | 638 | sprintf( |
639 | 639 | esc_html__('The incoming argument (%s) must be a string.', 'event_espresso'), |
@@ -656,7 +656,7 @@ discard block |
||
656 | 656 | ) |
657 | 657 | ); |
658 | 658 | |
659 | - if (! isset($allowable_formats[ $format ])) { |
|
659 | + if ( ! isset($allowable_formats[$format])) { |
|
660 | 660 | throw new InvalidArgumentException( |
661 | 661 | sprintf( |
662 | 662 | esc_html__('The incoming argument (%1$s) must be one of the allowable formats: %2$s', 'event_espresso'), |
@@ -12,7 +12,7 @@ |
||
12 | 12 | * @param string $table_column |
13 | 13 | * @param string $nicename |
14 | 14 | * @param bool $nullable |
15 | - * @param null $default_value |
|
15 | + * @param integer $default_value |
|
16 | 16 | */ |
17 | 17 | public function __construct($table_column, $nicename, $nullable, $default_value = null) |
18 | 18 | { |
@@ -7,91 +7,91 @@ |
||
7 | 7 | class EE_Money_Field extends EE_Float_Field |
8 | 8 | { |
9 | 9 | |
10 | - /** |
|
11 | - * @param string $table_column |
|
12 | - * @param string $nicename |
|
13 | - * @param bool $nullable |
|
14 | - * @param null $default_value |
|
15 | - */ |
|
16 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
17 | - { |
|
18 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
19 | - $this->setSchemaType('object'); |
|
20 | - } |
|
10 | + /** |
|
11 | + * @param string $table_column |
|
12 | + * @param string $nicename |
|
13 | + * @param bool $nullable |
|
14 | + * @param null $default_value |
|
15 | + */ |
|
16 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
17 | + { |
|
18 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
19 | + $this->setSchemaType('object'); |
|
20 | + } |
|
21 | 21 | |
22 | 22 | |
23 | - /** |
|
24 | - * Schemas: |
|
25 | - * 'localized_float': "3,023.00" |
|
26 | - * 'no_currency_code': "$3,023.00" |
|
27 | - * null: "$3,023.00<span>USD</span>" |
|
28 | - * |
|
29 | - * @param string $value_on_field_to_be_outputted |
|
30 | - * @param string $schema |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
34 | - { |
|
35 | - $pretty_float = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted); |
|
23 | + /** |
|
24 | + * Schemas: |
|
25 | + * 'localized_float': "3,023.00" |
|
26 | + * 'no_currency_code': "$3,023.00" |
|
27 | + * null: "$3,023.00<span>USD</span>" |
|
28 | + * |
|
29 | + * @param string $value_on_field_to_be_outputted |
|
30 | + * @param string $schema |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
34 | + { |
|
35 | + $pretty_float = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted); |
|
36 | 36 | |
37 | - if ($schema == 'localized_float') { |
|
38 | - return $pretty_float; |
|
39 | - } |
|
40 | - if ($schema == 'no_currency_code') { |
|
37 | + if ($schema == 'localized_float') { |
|
38 | + return $pretty_float; |
|
39 | + } |
|
40 | + if ($schema == 'no_currency_code') { |
|
41 | 41 | // echo "schema no currency!"; |
42 | - $display_code = false; |
|
43 | - } else { |
|
44 | - $display_code = true; |
|
45 | - } |
|
46 | - // we don't use the $pretty_float because format_currency will take care of it. |
|
47 | - return EEH_Template::format_currency($value_on_field_to_be_outputted, false, $display_code); |
|
48 | - } |
|
42 | + $display_code = false; |
|
43 | + } else { |
|
44 | + $display_code = true; |
|
45 | + } |
|
46 | + // we don't use the $pretty_float because format_currency will take care of it. |
|
47 | + return EEH_Template::format_currency($value_on_field_to_be_outputted, false, $display_code); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * If provided with a string, strips out money-related formatting to turn it into a proper float. |
|
52 | - * Rounds the float to the correct number of decimal places for this country's currency. |
|
53 | - * Also, interprets periods and commas according to the country's currency settings. |
|
54 | - * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
|
55 | - * |
|
56 | - * @param string $value_inputted_for_field_on_model_object |
|
57 | - * @return float |
|
58 | - */ |
|
59 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
60 | - { |
|
61 | - // remove any currencies etc. |
|
50 | + /** |
|
51 | + * If provided with a string, strips out money-related formatting to turn it into a proper float. |
|
52 | + * Rounds the float to the correct number of decimal places for this country's currency. |
|
53 | + * Also, interprets periods and commas according to the country's currency settings. |
|
54 | + * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
|
55 | + * |
|
56 | + * @param string $value_inputted_for_field_on_model_object |
|
57 | + * @return float |
|
58 | + */ |
|
59 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
60 | + { |
|
61 | + // remove any currencies etc. |
|
62 | 62 | // if(is_string($value_inputted_for_field_on_model_object)){ |
63 | 63 | // $value_inputted_for_field_on_model_object = preg_replace("/[^0-9,.]/", "", $value_inputted_for_field_on_model_object); |
64 | 64 | // } |
65 | - // now it's a float-style string or number |
|
66 | - $float_val = parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
67 | - // round to the correctly number of decimal places for this currency |
|
68 | - $rounded_value = round($float_val, EE_Registry::instance()->CFG->currency->dec_plc); |
|
69 | - return $rounded_value; |
|
70 | - } |
|
65 | + // now it's a float-style string or number |
|
66 | + $float_val = parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
67 | + // round to the correctly number of decimal places for this currency |
|
68 | + $rounded_value = round($float_val, EE_Registry::instance()->CFG->currency->dec_plc); |
|
69 | + return $rounded_value; |
|
70 | + } |
|
71 | 71 | |
72 | - public function prepare_for_get($value_of_field_on_model_object) |
|
73 | - { |
|
74 | - $c = EE_Registry::instance()->CFG->currency; |
|
75 | - return round(parent::prepare_for_get($value_of_field_on_model_object), $c->dec_plc); |
|
76 | - } |
|
72 | + public function prepare_for_get($value_of_field_on_model_object) |
|
73 | + { |
|
74 | + $c = EE_Registry::instance()->CFG->currency; |
|
75 | + return round(parent::prepare_for_get($value_of_field_on_model_object), $c->dec_plc); |
|
76 | + } |
|
77 | 77 | |
78 | - public function getSchemaProperties() |
|
79 | - { |
|
80 | - return array( |
|
81 | - 'raw' => array( |
|
82 | - 'description' => sprintf( |
|
83 | - __('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'), |
|
84 | - $this->get_nicename() |
|
85 | - ), |
|
86 | - 'type' => 'number' |
|
87 | - ), |
|
88 | - 'pretty' => array( |
|
89 | - 'description' => sprintf( |
|
90 | - __('%s - formatted for display in the set currency and decimal places.', 'event_espresso'), |
|
91 | - $this->get_nicename() |
|
92 | - ), |
|
93 | - 'type' => 'string' |
|
94 | - ) |
|
95 | - ); |
|
96 | - } |
|
78 | + public function getSchemaProperties() |
|
79 | + { |
|
80 | + return array( |
|
81 | + 'raw' => array( |
|
82 | + 'description' => sprintf( |
|
83 | + __('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'), |
|
84 | + $this->get_nicename() |
|
85 | + ), |
|
86 | + 'type' => 'number' |
|
87 | + ), |
|
88 | + 'pretty' => array( |
|
89 | + 'description' => sprintf( |
|
90 | + __('%s - formatted for display in the set currency and decimal places.', 'event_espresso'), |
|
91 | + $this->get_nicename() |
|
92 | + ), |
|
93 | + 'type' => 'string' |
|
94 | + ) |
|
95 | + ); |
|
96 | + } |
|
97 | 97 | } |
@@ -19,7 +19,7 @@ |
||
19 | 19 | public function __construct($table_column, $nicename, $nullable, $default_value = null) |
20 | 20 | { |
21 | 21 | parent::__construct($table_column, $nicename, $nullable, $default_value); |
22 | - $this->setSchemaType(array('object','string')); |
|
22 | + $this->setSchemaType(array('object', 'string')); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 |
@@ -9,72 +9,72 @@ |
||
9 | 9 | class EE_Serialized_Text_Field extends EE_Text_Field_Base |
10 | 10 | { |
11 | 11 | |
12 | - /** |
|
13 | - * @param string $table_column |
|
14 | - * @param string $nicename |
|
15 | - * @param bool $nullable |
|
16 | - * @param null $default_value |
|
17 | - */ |
|
18 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
19 | - { |
|
20 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
21 | - $this->setSchemaType(array('object','string')); |
|
22 | - } |
|
12 | + /** |
|
13 | + * @param string $table_column |
|
14 | + * @param string $nicename |
|
15 | + * @param bool $nullable |
|
16 | + * @param null $default_value |
|
17 | + */ |
|
18 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
19 | + { |
|
20 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
21 | + $this->setSchemaType(array('object','string')); |
|
22 | + } |
|
23 | 23 | |
24 | 24 | |
25 | - /** |
|
26 | - * Value SHOULD be an array, and we want to now convert it to a serialized string |
|
27 | - * |
|
28 | - * @param array $value_of_field_on_model_object |
|
29 | - * @return string |
|
30 | - */ |
|
31 | - public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
32 | - { |
|
33 | - return maybe_serialize($value_of_field_on_model_object); |
|
34 | - } |
|
25 | + /** |
|
26 | + * Value SHOULD be an array, and we want to now convert it to a serialized string |
|
27 | + * |
|
28 | + * @param array $value_of_field_on_model_object |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
32 | + { |
|
33 | + return maybe_serialize($value_of_field_on_model_object); |
|
34 | + } |
|
35 | 35 | |
36 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
37 | - { |
|
38 | - $value_inputted_for_field_on_model_object = EEH_Array::maybe_unserialize($value_inputted_for_field_on_model_object); |
|
39 | - if (is_string($value_inputted_for_field_on_model_object)) { |
|
40 | - return parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
41 | - } elseif (is_array($value_inputted_for_field_on_model_object)) { |
|
42 | - return array_map(array($this, 'prepare_for_set'), $value_inputted_for_field_on_model_object); |
|
43 | - } else {// so they passed NULL or an INT or something wack |
|
44 | - return $value_inputted_for_field_on_model_object; |
|
45 | - } |
|
46 | - } |
|
36 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
37 | + { |
|
38 | + $value_inputted_for_field_on_model_object = EEH_Array::maybe_unserialize($value_inputted_for_field_on_model_object); |
|
39 | + if (is_string($value_inputted_for_field_on_model_object)) { |
|
40 | + return parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
41 | + } elseif (is_array($value_inputted_for_field_on_model_object)) { |
|
42 | + return array_map(array($this, 'prepare_for_set'), $value_inputted_for_field_on_model_object); |
|
43 | + } else {// so they passed NULL or an INT or something wack |
|
44 | + return $value_inputted_for_field_on_model_object; |
|
45 | + } |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Value provided should definetely be a serialized string. We should unserialize into an array |
|
50 | - * |
|
51 | - * @param string $value_found_in_db_for_model_object |
|
52 | - * @return array |
|
53 | - */ |
|
54 | - public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
55 | - { |
|
56 | - return EEH_Array::maybe_unserialize($value_found_in_db_for_model_object); |
|
57 | - } |
|
48 | + /** |
|
49 | + * Value provided should definetely be a serialized string. We should unserialize into an array |
|
50 | + * |
|
51 | + * @param string $value_found_in_db_for_model_object |
|
52 | + * @return array |
|
53 | + */ |
|
54 | + public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
55 | + { |
|
56 | + return EEH_Array::maybe_unserialize($value_found_in_db_for_model_object); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Gets a string representation of the array |
|
61 | - * |
|
62 | - * @param type $value_on_field_to_be_outputted |
|
63 | - * @param string $schema , possible values are ',', others can be added |
|
64 | - * @return string |
|
65 | - */ |
|
66 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
67 | - { |
|
68 | - switch ($schema) { |
|
69 | - case 'print_r': |
|
70 | - $pretty_value = print_r($value_on_field_to_be_outputted, true); |
|
71 | - break; |
|
72 | - case 'as_table': |
|
73 | - $pretty_value = EEH_Template::layout_array_as_table($value_on_field_to_be_outputted); |
|
74 | - break; |
|
75 | - default: |
|
76 | - $pretty_value = implode(", ", $value_on_field_to_be_outputted); |
|
77 | - } |
|
78 | - return $pretty_value; |
|
79 | - } |
|
59 | + /** |
|
60 | + * Gets a string representation of the array |
|
61 | + * |
|
62 | + * @param type $value_on_field_to_be_outputted |
|
63 | + * @param string $schema , possible values are ',', others can be added |
|
64 | + * @return string |
|
65 | + */ |
|
66 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
67 | + { |
|
68 | + switch ($schema) { |
|
69 | + case 'print_r': |
|
70 | + $pretty_value = print_r($value_on_field_to_be_outputted, true); |
|
71 | + break; |
|
72 | + case 'as_table': |
|
73 | + $pretty_value = EEH_Template::layout_array_as_table($value_on_field_to_be_outputted); |
|
74 | + break; |
|
75 | + default: |
|
76 | + $pretty_value = implode(", ", $value_on_field_to_be_outputted); |
|
77 | + } |
|
78 | + return $pretty_value; |
|
79 | + } |
|
80 | 80 | } |
@@ -3,50 +3,50 @@ |
||
3 | 3 | |
4 | 4 | class EE_Boolean_Field extends EE_Integer_Field |
5 | 5 | { |
6 | - /** |
|
7 | - * @param string $table_column |
|
8 | - * @param string $nicename |
|
9 | - * @param bool $nullable |
|
10 | - * @param null $default_value |
|
11 | - */ |
|
12 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | - { |
|
14 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | - $this->setSchemaType('boolean'); |
|
16 | - } |
|
6 | + /** |
|
7 | + * @param string $table_column |
|
8 | + * @param string $nicename |
|
9 | + * @param bool $nullable |
|
10 | + * @param null $default_value |
|
11 | + */ |
|
12 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | + { |
|
14 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | + $this->setSchemaType('boolean'); |
|
16 | + } |
|
17 | 17 | |
18 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
19 | - { |
|
20 | - if ($value_inputted_for_field_on_model_object) { |
|
21 | - return true; |
|
22 | - } else { |
|
23 | - return false; |
|
24 | - } |
|
25 | - } |
|
18 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
19 | + { |
|
20 | + if ($value_inputted_for_field_on_model_object) { |
|
21 | + return true; |
|
22 | + } else { |
|
23 | + return false; |
|
24 | + } |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * Make sure we're returning booleans |
|
29 | - * |
|
30 | - * @param string $value_inputted_for_field_on_model_object |
|
31 | - * @return boolean |
|
32 | - */ |
|
33 | - public function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
|
34 | - { |
|
35 | - return intval($value_inputted_for_field_on_model_object) ? true : false; |
|
36 | - } |
|
27 | + /** |
|
28 | + * Make sure we're returning booleans |
|
29 | + * |
|
30 | + * @param string $value_inputted_for_field_on_model_object |
|
31 | + * @return boolean |
|
32 | + */ |
|
33 | + public function prepare_for_set_from_db($value_inputted_for_field_on_model_object) |
|
34 | + { |
|
35 | + return intval($value_inputted_for_field_on_model_object) ? true : false; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Gets a nice Yes/No value for this field |
|
40 | - * |
|
41 | - * @param boolean $value_on_field_to_be_outputted |
|
42 | - * @return string Yes or No |
|
43 | - */ |
|
44 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
45 | - { |
|
46 | - return apply_filters( |
|
47 | - 'FHEE__EE_Boolean_Field__prepare_for_pretty_echoing__return', |
|
48 | - $value_on_field_to_be_outputted ? __('Yes', 'event_espresso') : __('No', 'event_espresso'), |
|
49 | - $value_on_field_to_be_outputted |
|
50 | - ); |
|
51 | - } |
|
38 | + /** |
|
39 | + * Gets a nice Yes/No value for this field |
|
40 | + * |
|
41 | + * @param boolean $value_on_field_to_be_outputted |
|
42 | + * @return string Yes or No |
|
43 | + */ |
|
44 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
45 | + { |
|
46 | + return apply_filters( |
|
47 | + 'FHEE__EE_Boolean_Field__prepare_for_pretty_echoing__return', |
|
48 | + $value_on_field_to_be_outputted ? __('Yes', 'event_espresso') : __('No', 'event_espresso'), |
|
49 | + $value_on_field_to_be_outputted |
|
50 | + ); |
|
51 | + } |
|
52 | 52 | } |
@@ -3,16 +3,16 @@ |
||
3 | 3 | |
4 | 4 | class EE_DB_Only_Float_Field extends EE_DB_Only_Field_Base |
5 | 5 | { |
6 | - /** |
|
7 | - * @param string $table_column |
|
8 | - * @param string $nicename |
|
9 | - * @param bool $nullable |
|
10 | - * @param null $default_value |
|
11 | - */ |
|
12 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | - { |
|
14 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | - $this->setSchemaType('number'); |
|
16 | - } |
|
6 | + /** |
|
7 | + * @param string $table_column |
|
8 | + * @param string $nicename |
|
9 | + * @param bool $nullable |
|
10 | + * @param null $default_value |
|
11 | + */ |
|
12 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | + { |
|
14 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | + $this->setSchemaType('number'); |
|
16 | + } |
|
17 | 17 | |
18 | 18 | } |
@@ -8,45 +8,45 @@ |
||
8 | 8 | interface EnqueueAssetsInterface |
9 | 9 | { |
10 | 10 | |
11 | - /** |
|
12 | - * a place to register scripts and stylesheets with WordPress core |
|
13 | - * IMPORTANT !!! |
|
14 | - * ALL JavaScript files need to be registered for loading in the footer |
|
15 | - * by setting the 5th parameter of wp_register_script() to ` true ` |
|
16 | - * |
|
17 | - * @return void |
|
18 | - */ |
|
19 | - public function registerScriptsAndStylesheets(); |
|
20 | - |
|
21 | - /** |
|
22 | - * a place to enqueue previously registered stylesheets |
|
23 | - * this will be called during the wp_enqueue_scripts hook for frontend requests |
|
24 | - * |
|
25 | - * @return void |
|
26 | - */ |
|
27 | - public function enqueueStylesheets(); |
|
28 | - |
|
29 | - /** |
|
30 | - * a place to enqueue previously registered stylesheets |
|
31 | - * this will be called during the admin_enqueue_scripts hook for admin requests |
|
32 | - * |
|
33 | - * @return void |
|
34 | - */ |
|
35 | - public function enqueueAdminStylesheets(); |
|
36 | - |
|
37 | - /** |
|
38 | - * a place to enqueue previously registered scripts for frontend requests |
|
39 | - * |
|
40 | - * @return void |
|
41 | - */ |
|
42 | - public function enqueueScripts(); |
|
43 | - |
|
44 | - /** |
|
45 | - * a place to enqueue previously registered scripts for admin requests |
|
46 | - * |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function enqueueAdminScripts(); |
|
11 | + /** |
|
12 | + * a place to register scripts and stylesheets with WordPress core |
|
13 | + * IMPORTANT !!! |
|
14 | + * ALL JavaScript files need to be registered for loading in the footer |
|
15 | + * by setting the 5th parameter of wp_register_script() to ` true ` |
|
16 | + * |
|
17 | + * @return void |
|
18 | + */ |
|
19 | + public function registerScriptsAndStylesheets(); |
|
20 | + |
|
21 | + /** |
|
22 | + * a place to enqueue previously registered stylesheets |
|
23 | + * this will be called during the wp_enqueue_scripts hook for frontend requests |
|
24 | + * |
|
25 | + * @return void |
|
26 | + */ |
|
27 | + public function enqueueStylesheets(); |
|
28 | + |
|
29 | + /** |
|
30 | + * a place to enqueue previously registered stylesheets |
|
31 | + * this will be called during the admin_enqueue_scripts hook for admin requests |
|
32 | + * |
|
33 | + * @return void |
|
34 | + */ |
|
35 | + public function enqueueAdminStylesheets(); |
|
36 | + |
|
37 | + /** |
|
38 | + * a place to enqueue previously registered scripts for frontend requests |
|
39 | + * |
|
40 | + * @return void |
|
41 | + */ |
|
42 | + public function enqueueScripts(); |
|
43 | + |
|
44 | + /** |
|
45 | + * a place to enqueue previously registered scripts for admin requests |
|
46 | + * |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function enqueueAdminScripts(); |
|
50 | 50 | |
51 | 51 | } |
52 | 52 | // End of file EnqueueAssetsInterface.php |
@@ -1,15 +1,15 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | //echo '<br/><h6 style="color:#2EA2CC;">'. __FILE__ . ' <span style="font-weight:normal;color:#E76700"> Line #: ' . __LINE__ . '</span></h6>'; |
3 | 3 | global $post; |
4 | -do_action( 'AHEE_event_details_before_featured_img', $post ); |
|
4 | +do_action('AHEE_event_details_before_featured_img', $post); |
|
5 | 5 | |
6 | -if ( has_post_thumbnail( $post->ID )) : |
|
7 | - if ( $img_ID = get_post_thumbnail_id( $post->ID )) : |
|
8 | - if ( $featured_img = wp_get_attachment_image_src( $img_ID, 'large' )) : |
|
9 | - $caption = esc_attr( get_post( get_post( $img_ID ))->post_excerpt ); |
|
6 | +if (has_post_thumbnail($post->ID)) : |
|
7 | + if ($img_ID = get_post_thumbnail_id($post->ID)) : |
|
8 | + if ($featured_img = wp_get_attachment_image_src($img_ID, 'large')) : |
|
9 | + $caption = esc_attr(get_post(get_post($img_ID))->post_excerpt); |
|
10 | 10 | ?> |
11 | 11 | <div id="ee-event-img-dv-<?php echo $post->ID; ?>" class="ee-event-img-dv"> |
12 | - <a class="ee-event-img-lnk" href="<?php the_permalink(); ?>"<?php echo \EED_Events_Archive::link_target();?>> |
|
12 | + <a class="ee-event-img-lnk" href="<?php the_permalink(); ?>"<?php echo \EED_Events_Archive::link_target(); ?>> |
|
13 | 13 | <img class="ee-event-img" src="<?php echo $featured_img[0]; ?>" width="<?php echo $featured_img[1]; ?>" height="<?php echo $featured_img[2]; ?>" alt="<?php echo $caption; ?>"/> |
14 | 14 | </a> |
15 | 15 | </div> |
@@ -18,4 +18,4 @@ discard block |
||
18 | 18 | endif; |
19 | 19 | endif; |
20 | 20 | ?> |
21 | -<?php do_action( 'AHEE_event_details_after_featured_img', $post );?> |
|
21 | +<?php do_action('AHEE_event_details_after_featured_img', $post); ?> |