@@ -2,25 +2,25 @@ |
||
2 | 2 | |
3 | 3 | class EE_Primary_Key_String_Field extends EE_Primary_Key_Field_Base |
4 | 4 | { |
5 | - public function __construct($table_column, $nicename) |
|
6 | - { |
|
7 | - parent::__construct($table_column, $nicename, null); |
|
8 | - } |
|
5 | + public function __construct($table_column, $nicename) |
|
6 | + { |
|
7 | + parent::__construct($table_column, $nicename, null); |
|
8 | + } |
|
9 | 9 | |
10 | 10 | |
11 | - /** |
|
12 | - * removes all tags when setting |
|
13 | - * |
|
14 | - * @param EE_Base_Class|string|null $value_inputted_for_field_on_model_object |
|
15 | - * @return string |
|
16 | - * @throws EE_Error |
|
17 | - * @throws ReflectionException |
|
18 | - */ |
|
19 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
20 | - { |
|
21 | - if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
22 | - $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
23 | - } |
|
24 | - return wp_strip_all_tags((string) $value_inputted_for_field_on_model_object); |
|
25 | - } |
|
11 | + /** |
|
12 | + * removes all tags when setting |
|
13 | + * |
|
14 | + * @param EE_Base_Class|string|null $value_inputted_for_field_on_model_object |
|
15 | + * @return string |
|
16 | + * @throws EE_Error |
|
17 | + * @throws ReflectionException |
|
18 | + */ |
|
19 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
20 | + { |
|
21 | + if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
22 | + $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
23 | + } |
|
24 | + return wp_strip_all_tags((string) $value_inputted_for_field_on_model_object); |
|
25 | + } |
|
26 | 26 | } |
@@ -11,108 +11,108 @@ |
||
11 | 11 | */ |
12 | 12 | class EE_Enum_Integer_Field extends EE_Integer_Field |
13 | 13 | { |
14 | - public array $_allowed_enum_values; |
|
14 | + public array $_allowed_enum_values; |
|
15 | 15 | |
16 | 16 | |
17 | - /** |
|
18 | - * @param string $table_column |
|
19 | - * @param string $nicename |
|
20 | - * @param boolean $nullable |
|
21 | - * @param int $default_value |
|
22 | - * @param array $allowed_enum_values keys are values to be used in the DB, values are how they should be displayed |
|
23 | - */ |
|
24 | - public function __construct($table_column, $nicename, $nullable, $default_value, array $allowed_enum_values) |
|
25 | - { |
|
26 | - $this->_allowed_enum_values = $allowed_enum_values; |
|
27 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
28 | - $this->setSchemaType(SchemaType::OBJECT); |
|
29 | - } |
|
17 | + /** |
|
18 | + * @param string $table_column |
|
19 | + * @param string $nicename |
|
20 | + * @param boolean $nullable |
|
21 | + * @param int $default_value |
|
22 | + * @param array $allowed_enum_values keys are values to be used in the DB, values are how they should be displayed |
|
23 | + */ |
|
24 | + public function __construct($table_column, $nicename, $nullable, $default_value, array $allowed_enum_values) |
|
25 | + { |
|
26 | + $this->_allowed_enum_values = $allowed_enum_values; |
|
27 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
28 | + $this->setSchemaType(SchemaType::OBJECT); |
|
29 | + } |
|
30 | 30 | |
31 | 31 | |
32 | - /** |
|
33 | - * Returns the list of allowed enum options, but filterable. |
|
34 | - * This is used internally |
|
35 | - * |
|
36 | - * @return array |
|
37 | - */ |
|
38 | - protected function _allowed_enum_values(): array |
|
39 | - { |
|
40 | - return (array) apply_filters( |
|
41 | - 'FHEE__EE_Enum_Integer_Field___allowed_enum_options', |
|
42 | - $this->_allowed_enum_values, |
|
43 | - $this |
|
44 | - ); |
|
45 | - } |
|
32 | + /** |
|
33 | + * Returns the list of allowed enum options, but filterable. |
|
34 | + * This is used internally |
|
35 | + * |
|
36 | + * @return array |
|
37 | + */ |
|
38 | + protected function _allowed_enum_values(): array |
|
39 | + { |
|
40 | + return (array) apply_filters( |
|
41 | + 'FHEE__EE_Enum_Integer_Field___allowed_enum_options', |
|
42 | + $this->_allowed_enum_values, |
|
43 | + $this |
|
44 | + ); |
|
45 | + } |
|
46 | 46 | |
47 | 47 | |
48 | - /** |
|
49 | - * When setting, just verify that the value being used matches what we've defined as allowable enum values. |
|
50 | - * If not, throw an error (but if WP_DEBUG is false, just set the value to default) |
|
51 | - * |
|
52 | - * @param int $value_inputted_for_field_on_model_object |
|
53 | - * @return int |
|
54 | - */ |
|
55 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
56 | - { |
|
57 | - $allowed_enum_values = $this->_allowed_enum_values(); |
|
58 | - if ( |
|
59 | - $value_inputted_for_field_on_model_object !== null |
|
60 | - && ! array_key_exists($value_inputted_for_field_on_model_object, $allowed_enum_values) |
|
61 | - ) { |
|
62 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
63 | - $msg = sprintf( |
|
64 | - esc_html__('System is assigning incompatible value "%1$s" to field "%2$s"', 'event_espresso'), |
|
65 | - $value_inputted_for_field_on_model_object, |
|
66 | - $this->_name |
|
67 | - ); |
|
68 | - $msg2 = sprintf( |
|
69 | - esc_html__('Allowed values for "%1$s" are "%2$s". You provided "%3$s"', 'event_espresso'), |
|
70 | - $this->_name, |
|
71 | - implode(', ', array_keys($allowed_enum_values)), |
|
72 | - $value_inputted_for_field_on_model_object |
|
73 | - ); |
|
74 | - EE_Error::add_error("$msg||$msg2", __FILE__, __FUNCTION__, __LINE__); |
|
75 | - } |
|
76 | - return $this->get_default_value(); |
|
77 | - } |
|
78 | - return (int) $value_inputted_for_field_on_model_object; |
|
79 | - } |
|
48 | + /** |
|
49 | + * When setting, just verify that the value being used matches what we've defined as allowable enum values. |
|
50 | + * If not, throw an error (but if WP_DEBUG is false, just set the value to default) |
|
51 | + * |
|
52 | + * @param int $value_inputted_for_field_on_model_object |
|
53 | + * @return int |
|
54 | + */ |
|
55 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
56 | + { |
|
57 | + $allowed_enum_values = $this->_allowed_enum_values(); |
|
58 | + if ( |
|
59 | + $value_inputted_for_field_on_model_object !== null |
|
60 | + && ! array_key_exists($value_inputted_for_field_on_model_object, $allowed_enum_values) |
|
61 | + ) { |
|
62 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
63 | + $msg = sprintf( |
|
64 | + esc_html__('System is assigning incompatible value "%1$s" to field "%2$s"', 'event_espresso'), |
|
65 | + $value_inputted_for_field_on_model_object, |
|
66 | + $this->_name |
|
67 | + ); |
|
68 | + $msg2 = sprintf( |
|
69 | + esc_html__('Allowed values for "%1$s" are "%2$s". You provided "%3$s"', 'event_espresso'), |
|
70 | + $this->_name, |
|
71 | + implode(', ', array_keys($allowed_enum_values)), |
|
72 | + $value_inputted_for_field_on_model_object |
|
73 | + ); |
|
74 | + EE_Error::add_error("$msg||$msg2", __FILE__, __FUNCTION__, __LINE__); |
|
75 | + } |
|
76 | + return $this->get_default_value(); |
|
77 | + } |
|
78 | + return (int) $value_inputted_for_field_on_model_object; |
|
79 | + } |
|
80 | 80 | |
81 | 81 | |
82 | - /** |
|
83 | - * Gets the pretty version of the enum's value. |
|
84 | - * |
|
85 | - * @param int | string $value_on_field_to_be_outputted |
|
86 | - * @param string|null $schema |
|
87 | - * @return string |
|
88 | - */ |
|
89 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, ?string $schema = null) |
|
90 | - { |
|
91 | - $options = $this->_allowed_enum_values(); |
|
92 | - return $options[ $value_on_field_to_be_outputted ] ?? $value_on_field_to_be_outputted; |
|
93 | - } |
|
82 | + /** |
|
83 | + * Gets the pretty version of the enum's value. |
|
84 | + * |
|
85 | + * @param int | string $value_on_field_to_be_outputted |
|
86 | + * @param string|null $schema |
|
87 | + * @return string |
|
88 | + */ |
|
89 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, ?string $schema = null) |
|
90 | + { |
|
91 | + $options = $this->_allowed_enum_values(); |
|
92 | + return $options[ $value_on_field_to_be_outputted ] ?? $value_on_field_to_be_outputted; |
|
93 | + } |
|
94 | 94 | |
95 | 95 | |
96 | - public function getSchemaProperties(): array |
|
97 | - { |
|
98 | - return [ |
|
99 | - 'raw' => [ |
|
100 | - 'description' => sprintf( |
|
101 | - esc_html__('%s - the value in the database.', 'event_espresso'), |
|
102 | - $this->get_nicename() |
|
103 | - ), |
|
104 | - 'enum' => array_keys($this->_allowed_enum_values()), |
|
105 | - 'type' => SchemaType::INTEGER, |
|
106 | - ], |
|
107 | - 'pretty' => [ |
|
108 | - 'description' => sprintf( |
|
109 | - esc_html__('%s - the value for display.', 'event_espresso'), |
|
110 | - $this->get_nicename() |
|
111 | - ), |
|
112 | - 'enum' => array_values($this->_allowed_enum_values()), |
|
113 | - 'type' => SchemaType::STRING, |
|
114 | - 'read_only' => true, |
|
115 | - ], |
|
116 | - ]; |
|
117 | - } |
|
96 | + public function getSchemaProperties(): array |
|
97 | + { |
|
98 | + return [ |
|
99 | + 'raw' => [ |
|
100 | + 'description' => sprintf( |
|
101 | + esc_html__('%s - the value in the database.', 'event_espresso'), |
|
102 | + $this->get_nicename() |
|
103 | + ), |
|
104 | + 'enum' => array_keys($this->_allowed_enum_values()), |
|
105 | + 'type' => SchemaType::INTEGER, |
|
106 | + ], |
|
107 | + 'pretty' => [ |
|
108 | + 'description' => sprintf( |
|
109 | + esc_html__('%s - the value for display.', 'event_espresso'), |
|
110 | + $this->get_nicename() |
|
111 | + ), |
|
112 | + 'enum' => array_values($this->_allowed_enum_values()), |
|
113 | + 'type' => SchemaType::STRING, |
|
114 | + 'read_only' => true, |
|
115 | + ], |
|
116 | + ]; |
|
117 | + } |
|
118 | 118 | } |
@@ -5,74 +5,74 @@ |
||
5 | 5 | |
6 | 6 | class EE_JSON_Field extends EE_Model_Field_Base |
7 | 7 | { |
8 | - private JsonDataHandler $json_data_handler; |
|
8 | + private JsonDataHandler $json_data_handler; |
|
9 | 9 | |
10 | 10 | |
11 | - /** |
|
12 | - * @param string $table_column |
|
13 | - * @param string $nicename |
|
14 | - * @param bool $nullable |
|
15 | - * @param null $default_value |
|
16 | - */ |
|
17 | - public function __construct( |
|
18 | - $table_column, |
|
19 | - $nicename, |
|
20 | - $nullable, |
|
21 | - $default_value = null |
|
22 | - ) { |
|
23 | - $this->json_data_handler = new JsonDataHandler(); |
|
24 | - $this->json_data_handler->configure( |
|
25 | - JsonDataHandler::DATA_TYPE_OBJECT |
|
26 | - ); |
|
27 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
28 | - $this->setSchemaType(SchemaType::STRING); |
|
29 | - } |
|
11 | + /** |
|
12 | + * @param string $table_column |
|
13 | + * @param string $nicename |
|
14 | + * @param bool $nullable |
|
15 | + * @param null $default_value |
|
16 | + */ |
|
17 | + public function __construct( |
|
18 | + $table_column, |
|
19 | + $nicename, |
|
20 | + $nullable, |
|
21 | + $default_value = null |
|
22 | + ) { |
|
23 | + $this->json_data_handler = new JsonDataHandler(); |
|
24 | + $this->json_data_handler->configure( |
|
25 | + JsonDataHandler::DATA_TYPE_OBJECT |
|
26 | + ); |
|
27 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
28 | + $this->setSchemaType(SchemaType::STRING); |
|
29 | + } |
|
30 | 30 | |
31 | 31 | |
32 | - // /** |
|
33 | - // * When get() is called on a model object (eg EE_Event), before returning its value, |
|
34 | - // * call this function on it, allowing us to customize the returned value based on |
|
35 | - // * the field's type. Eg, we may want to unserialize it, strip tags, etc. By default, |
|
36 | - // * we simply return it. |
|
37 | - // * |
|
38 | - // * @param mixed $value_of_field_on_model_object |
|
39 | - // * @return mixed |
|
40 | - // */ |
|
41 | - // public function prepare_for_get($value_of_field_on_model_object) |
|
42 | - // { |
|
43 | - // // return $this->json_data_handler->decodeJson($value_of_field_on_model_object); |
|
44 | - // return $value_of_field_on_model_object; |
|
45 | - // } |
|
32 | + // /** |
|
33 | + // * When get() is called on a model object (eg EE_Event), before returning its value, |
|
34 | + // * call this function on it, allowing us to customize the returned value based on |
|
35 | + // * the field's type. Eg, we may want to unserialize it, strip tags, etc. By default, |
|
36 | + // * we simply return it. |
|
37 | + // * |
|
38 | + // * @param mixed $value_of_field_on_model_object |
|
39 | + // * @return mixed |
|
40 | + // */ |
|
41 | + // public function prepare_for_get($value_of_field_on_model_object) |
|
42 | + // { |
|
43 | + // // return $this->json_data_handler->decodeJson($value_of_field_on_model_object); |
|
44 | + // return $value_of_field_on_model_object; |
|
45 | + // } |
|
46 | 46 | |
47 | 47 | |
48 | - /** |
|
49 | - * When creating a brand-new model object, or setting a particular value for one of its fields, this function |
|
50 | - * is called before setting it on the model object. We may want to strip slashes, unserialize the value, etc. |
|
51 | - * By default, we do nothing. |
|
52 | - * |
|
53 | - * If the model field is going to perform any validation on the input, this is where it should be done |
|
54 | - * (once the value is on the model object, it may be used in other ways besides putting it into the DB |
|
55 | - * so it's best to validate it right away). |
|
56 | - * |
|
57 | - * @param mixed $value_inputted_for_field_on_model_object |
|
58 | - * @return string |
|
59 | - */ |
|
60 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
61 | - { |
|
62 | - return $this->json_data_handler->encodeData($value_inputted_for_field_on_model_object); |
|
63 | - } |
|
48 | + /** |
|
49 | + * When creating a brand-new model object, or setting a particular value for one of its fields, this function |
|
50 | + * is called before setting it on the model object. We may want to strip slashes, unserialize the value, etc. |
|
51 | + * By default, we do nothing. |
|
52 | + * |
|
53 | + * If the model field is going to perform any validation on the input, this is where it should be done |
|
54 | + * (once the value is on the model object, it may be used in other ways besides putting it into the DB |
|
55 | + * so it's best to validate it right away). |
|
56 | + * |
|
57 | + * @param mixed $value_inputted_for_field_on_model_object |
|
58 | + * @return string |
|
59 | + */ |
|
60 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
61 | + { |
|
62 | + return $this->json_data_handler->encodeData($value_inputted_for_field_on_model_object); |
|
63 | + } |
|
64 | 64 | |
65 | 65 | |
66 | - /** |
|
67 | - * When inserting or updating a field on a model object, run this function on each |
|
68 | - * value to prepare it for insertion into the db. Generally this converts |
|
69 | - * the validated input on the model object into the format used in the DB. |
|
70 | - * |
|
71 | - * @param mixed $value_of_field_on_model_object |
|
72 | - * @return string |
|
73 | - */ |
|
74 | - public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
75 | - { |
|
76 | - return $this->json_data_handler->encodeData($value_of_field_on_model_object); |
|
77 | - } |
|
66 | + /** |
|
67 | + * When inserting or updating a field on a model object, run this function on each |
|
68 | + * value to prepare it for insertion into the db. Generally this converts |
|
69 | + * the validated input on the model object into the format used in the DB. |
|
70 | + * |
|
71 | + * @param mixed $value_of_field_on_model_object |
|
72 | + * @return string |
|
73 | + */ |
|
74 | + public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
75 | + { |
|
76 | + return $this->json_data_handler->encodeData($value_of_field_on_model_object); |
|
77 | + } |
|
78 | 78 | } |
@@ -15,56 +15,56 @@ |
||
15 | 15 | */ |
16 | 16 | class EE_Full_HTML_Field extends EE_Text_Field_Base |
17 | 17 | { |
18 | - /** |
|
19 | - * @param string $table_column |
|
20 | - * @param string $nicename |
|
21 | - * @param bool $nullable |
|
22 | - * @param null $default_value |
|
23 | - */ |
|
24 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
25 | - { |
|
26 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
27 | - $this->setSchemaType(SchemaType::STRING); |
|
28 | - } |
|
18 | + /** |
|
19 | + * @param string $table_column |
|
20 | + * @param string $nicename |
|
21 | + * @param bool $nullable |
|
22 | + * @param null $default_value |
|
23 | + */ |
|
24 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
25 | + { |
|
26 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
27 | + $this->setSchemaType(SchemaType::STRING); |
|
28 | + } |
|
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * Does shortcodes and auto-paragraphs the content (unless schema is 'no_wpautop') |
|
33 | - * |
|
34 | - * @param mixed $value_on_field_to_be_outputted |
|
35 | - * @param string|null $schema |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, ?string $schema = null) |
|
39 | - { |
|
40 | - if ($schema == 'form_input') { |
|
41 | - return parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema); |
|
42 | - } elseif ($schema == 'no_wpautop') { |
|
43 | - return do_shortcode(parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema)); |
|
44 | - } else { |
|
45 | - return wpautop(do_shortcode(parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema))); |
|
46 | - } |
|
47 | - } |
|
31 | + /** |
|
32 | + * Does shortcodes and auto-paragraphs the content (unless schema is 'no_wpautop') |
|
33 | + * |
|
34 | + * @param mixed $value_on_field_to_be_outputted |
|
35 | + * @param string|null $schema |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, ?string $schema = null) |
|
39 | + { |
|
40 | + if ($schema == 'form_input') { |
|
41 | + return parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema); |
|
42 | + } elseif ($schema == 'no_wpautop') { |
|
43 | + return do_shortcode(parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema)); |
|
44 | + } else { |
|
45 | + return wpautop(do_shortcode(parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema))); |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | 49 | |
50 | - public function getSchemaProperties(): array |
|
51 | - { |
|
52 | - return [ |
|
53 | - 'raw' => [ |
|
54 | - 'description' => sprintf( |
|
55 | - esc_html__('%s - the value in the database.', 'event_espresso'), |
|
56 | - $this->get_nicename() |
|
57 | - ), |
|
58 | - 'type' => SchemaType::STRING, |
|
59 | - ], |
|
60 | - 'rendered' => [ |
|
61 | - 'description' => sprintf( |
|
62 | - esc_html__('%s - transformed for display.', 'event_espresso'), |
|
63 | - $this->get_nicename() |
|
64 | - ), |
|
65 | - 'type' => SchemaType::STRING, |
|
66 | - 'readonly' => true, |
|
67 | - ], |
|
68 | - ]; |
|
69 | - } |
|
50 | + public function getSchemaProperties(): array |
|
51 | + { |
|
52 | + return [ |
|
53 | + 'raw' => [ |
|
54 | + 'description' => sprintf( |
|
55 | + esc_html__('%s - the value in the database.', 'event_espresso'), |
|
56 | + $this->get_nicename() |
|
57 | + ), |
|
58 | + 'type' => SchemaType::STRING, |
|
59 | + ], |
|
60 | + 'rendered' => [ |
|
61 | + 'description' => sprintf( |
|
62 | + esc_html__('%s - transformed for display.', 'event_espresso'), |
|
63 | + $this->get_nicename() |
|
64 | + ), |
|
65 | + 'type' => SchemaType::STRING, |
|
66 | + 'readonly' => true, |
|
67 | + ], |
|
68 | + ]; |
|
69 | + } |
|
70 | 70 | } |
@@ -11,29 +11,29 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class EE_DB_Only_Field_Base extends EE_Model_Field_Base |
13 | 13 | { |
14 | - /** |
|
15 | - * @param string $table_column |
|
16 | - * @param string $nicename |
|
17 | - * @param bool $nullable |
|
18 | - * @param null $default_value |
|
19 | - */ |
|
20 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
21 | - { |
|
22 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
23 | - $this->setSchemaType(SchemaType::STRING); |
|
24 | - $this->setSchemaReadOnly(true); |
|
25 | - } |
|
14 | + /** |
|
15 | + * @param string $table_column |
|
16 | + * @param string $nicename |
|
17 | + * @param bool $nullable |
|
18 | + * @param null $default_value |
|
19 | + */ |
|
20 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
21 | + { |
|
22 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
23 | + $this->setSchemaType(SchemaType::STRING); |
|
24 | + $this->setSchemaReadOnly(true); |
|
25 | + } |
|
26 | 26 | |
27 | 27 | |
28 | - /** |
|
29 | - * All these children classes are for the db-only (meaning, we should select them |
|
30 | - * on get_all queries, update, delete, and will still want to set their default value |
|
31 | - * on inserts, but the model object won't have reference to these fields) |
|
32 | - * |
|
33 | - * @return boolean |
|
34 | - */ |
|
35 | - public function is_db_only_field(): bool |
|
36 | - { |
|
37 | - return true; |
|
38 | - } |
|
28 | + /** |
|
29 | + * All these children classes are for the db-only (meaning, we should select them |
|
30 | + * on get_all queries, update, delete, and will still want to set their default value |
|
31 | + * on inserts, but the model object won't have reference to these fields) |
|
32 | + * |
|
33 | + * @return boolean |
|
34 | + */ |
|
35 | + public function is_db_only_field(): bool |
|
36 | + { |
|
37 | + return true; |
|
38 | + } |
|
39 | 39 | } |
@@ -2,41 +2,41 @@ |
||
2 | 2 | |
3 | 3 | class EE_Foreign_Key_String_Field extends EE_Foreign_Key_Field_Base |
4 | 4 | { |
5 | - /** |
|
6 | - * Whether the value should be converted to uppercase on insertion. |
|
7 | - * |
|
8 | - * @var bool |
|
9 | - */ |
|
10 | - protected bool $is_uppercase; |
|
5 | + /** |
|
6 | + * Whether the value should be converted to uppercase on insertion. |
|
7 | + * |
|
8 | + * @var bool |
|
9 | + */ |
|
10 | + protected bool $is_uppercase; |
|
11 | 11 | |
12 | 12 | |
13 | - /** |
|
14 | - * @param string $table_column name of column for field |
|
15 | - * @param string $nicename should be internationalized with __('blah','event_espresso') |
|
16 | - * @param boolean $nullable |
|
17 | - * @param int|string $default_value data type should match field type |
|
18 | - * @param string|string[] $model_name eg 'Event','Answer','Term', etc. |
|
19 | - * Basically its the model class's name without the "EEM_" |
|
20 | - * @param boolean $is_uppercase Whether the value should be converted to uppercase on insertion. |
|
21 | - */ |
|
22 | - public function __construct($table_column, $nicename, $nullable, $default_value, $model_name, bool $is_uppercase = true) |
|
23 | - { |
|
24 | - $this->is_uppercase = $is_uppercase; |
|
25 | - parent::__construct($table_column, $nicename, $nullable, $default_value, $model_name); |
|
26 | - } |
|
13 | + /** |
|
14 | + * @param string $table_column name of column for field |
|
15 | + * @param string $nicename should be internationalized with __('blah','event_espresso') |
|
16 | + * @param boolean $nullable |
|
17 | + * @param int|string $default_value data type should match field type |
|
18 | + * @param string|string[] $model_name eg 'Event','Answer','Term', etc. |
|
19 | + * Basically its the model class's name without the "EEM_" |
|
20 | + * @param boolean $is_uppercase Whether the value should be converted to uppercase on insertion. |
|
21 | + */ |
|
22 | + public function __construct($table_column, $nicename, $nullable, $default_value, $model_name, bool $is_uppercase = true) |
|
23 | + { |
|
24 | + $this->is_uppercase = $is_uppercase; |
|
25 | + parent::__construct($table_column, $nicename, $nullable, $default_value, $model_name); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * removes all tags when setting |
|
30 | - * |
|
31 | - * @param string|null $value_inputted_for_field_on_model_object |
|
32 | - * @return string |
|
33 | - */ |
|
34 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
35 | - { |
|
36 | - if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
37 | - $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
38 | - } |
|
39 | - $clean_value = wp_strip_all_tags((string) $value_inputted_for_field_on_model_object); |
|
40 | - return $this->is_uppercase ? strtoupper($clean_value) : $clean_value; |
|
41 | - } |
|
28 | + /** |
|
29 | + * removes all tags when setting |
|
30 | + * |
|
31 | + * @param string|null $value_inputted_for_field_on_model_object |
|
32 | + * @return string |
|
33 | + */ |
|
34 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
35 | + { |
|
36 | + if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
37 | + $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
38 | + } |
|
39 | + $clean_value = wp_strip_all_tags((string) $value_inputted_for_field_on_model_object); |
|
40 | + return $this->is_uppercase ? strtoupper($clean_value) : $clean_value; |
|
41 | + } |
|
42 | 42 | } |
@@ -12,38 +12,38 @@ |
||
12 | 12 | */ |
13 | 13 | class EE_WP_User_Field extends EE_Foreign_Key_Int_Field |
14 | 14 | { |
15 | - /** |
|
16 | - * No need to provide a default or the model pointed to- the default is |
|
17 | - * always get_current_user_id() and the model pointed to is always WP_User |
|
18 | - * |
|
19 | - * @param string $table_column name fo column for field |
|
20 | - * @param string $nicename should eb internationalized with esc_html__('blah','event_espresso') |
|
21 | - * @param boolean $nullable |
|
22 | - */ |
|
23 | - public function __construct($table_column, $nicename, $nullable) |
|
24 | - { |
|
25 | - parent::__construct($table_column, $nicename, $nullable, null, 'WP_User'); |
|
26 | - } |
|
15 | + /** |
|
16 | + * No need to provide a default or the model pointed to- the default is |
|
17 | + * always get_current_user_id() and the model pointed to is always WP_User |
|
18 | + * |
|
19 | + * @param string $table_column name fo column for field |
|
20 | + * @param string $nicename should eb internationalized with esc_html__('blah','event_espresso') |
|
21 | + * @param boolean $nullable |
|
22 | + */ |
|
23 | + public function __construct($table_column, $nicename, $nullable) |
|
24 | + { |
|
25 | + parent::__construct($table_column, $nicename, $nullable, null, 'WP_User'); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * Gets the default which is always the current user. This can't be set when initially |
|
30 | - * constructing the model field because that's done before $current_user is set |
|
31 | - * |
|
32 | - * @return int |
|
33 | - */ |
|
34 | - public function get_default_value() |
|
35 | - { |
|
36 | - if (did_action('init')) { |
|
37 | - return get_current_user_id(); |
|
38 | - } |
|
39 | - EE_Error::doing_it_wrong( |
|
40 | - 'EE_WP_User_Field::get_default_value', |
|
41 | - esc_html__( |
|
42 | - 'You cant get a default value for a wp_User_Field because the "init" action is called, because current_user global hasnt yet been setup. Consider doing your business logic on the "init" hook or later.', |
|
43 | - 'event_espresso' |
|
44 | - ), |
|
45 | - '4.6.20' |
|
46 | - ); |
|
47 | - return 1; |
|
48 | - } |
|
28 | + /** |
|
29 | + * Gets the default which is always the current user. This can't be set when initially |
|
30 | + * constructing the model field because that's done before $current_user is set |
|
31 | + * |
|
32 | + * @return int |
|
33 | + */ |
|
34 | + public function get_default_value() |
|
35 | + { |
|
36 | + if (did_action('init')) { |
|
37 | + return get_current_user_id(); |
|
38 | + } |
|
39 | + EE_Error::doing_it_wrong( |
|
40 | + 'EE_WP_User_Field::get_default_value', |
|
41 | + esc_html__( |
|
42 | + 'You cant get a default value for a wp_User_Field because the "init" action is called, because current_user global hasnt yet been setup. Consider doing your business logic on the "init" hook or later.', |
|
43 | + 'event_espresso' |
|
44 | + ), |
|
45 | + '4.6.20' |
|
46 | + ); |
|
47 | + return 1; |
|
48 | + } |
|
49 | 49 | } |
@@ -10,77 +10,77 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_Serialized_Text_Field extends EE_Text_Field_Base |
12 | 12 | { |
13 | - /** |
|
14 | - * @param string $table_column |
|
15 | - * @param string $nicename |
|
16 | - * @param bool $nullable |
|
17 | - * @param null $default_value |
|
18 | - */ |
|
19 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
20 | - { |
|
21 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
22 | - $this->setSchemaType([SchemaType::OBJECT, SchemaType::STRING]); |
|
23 | - } |
|
13 | + /** |
|
14 | + * @param string $table_column |
|
15 | + * @param string $nicename |
|
16 | + * @param bool $nullable |
|
17 | + * @param null $default_value |
|
18 | + */ |
|
19 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
20 | + { |
|
21 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
22 | + $this->setSchemaType([SchemaType::OBJECT, SchemaType::STRING]); |
|
23 | + } |
|
24 | 24 | |
25 | 25 | |
26 | - /** |
|
27 | - * Value SHOULD be an array, and we want to now convert it to a serialized string |
|
28 | - * |
|
29 | - * @param array $value_of_field_on_model_object |
|
30 | - * @return string |
|
31 | - */ |
|
32 | - public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
33 | - { |
|
34 | - return maybe_serialize($value_of_field_on_model_object); |
|
35 | - } |
|
26 | + /** |
|
27 | + * Value SHOULD be an array, and we want to now convert it to a serialized string |
|
28 | + * |
|
29 | + * @param array $value_of_field_on_model_object |
|
30 | + * @return string |
|
31 | + */ |
|
32 | + public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
33 | + { |
|
34 | + return maybe_serialize($value_of_field_on_model_object); |
|
35 | + } |
|
36 | 36 | |
37 | 37 | |
38 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
39 | - { |
|
40 | - $value_inputted_for_field_on_model_object = |
|
41 | - EEH_Array::maybe_unserialize($value_inputted_for_field_on_model_object); |
|
42 | - if (is_string($value_inputted_for_field_on_model_object)) { |
|
43 | - return parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
44 | - } |
|
45 | - if (is_array($value_inputted_for_field_on_model_object)) { |
|
46 | - return array_map([$this, 'prepare_for_set'], $value_inputted_for_field_on_model_object); |
|
47 | - } |
|
48 | - // so they passed NULL or an INT or something wack |
|
49 | - return $value_inputted_for_field_on_model_object; |
|
50 | - } |
|
38 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
39 | + { |
|
40 | + $value_inputted_for_field_on_model_object = |
|
41 | + EEH_Array::maybe_unserialize($value_inputted_for_field_on_model_object); |
|
42 | + if (is_string($value_inputted_for_field_on_model_object)) { |
|
43 | + return parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
44 | + } |
|
45 | + if (is_array($value_inputted_for_field_on_model_object)) { |
|
46 | + return array_map([$this, 'prepare_for_set'], $value_inputted_for_field_on_model_object); |
|
47 | + } |
|
48 | + // so they passed NULL or an INT or something wack |
|
49 | + return $value_inputted_for_field_on_model_object; |
|
50 | + } |
|
51 | 51 | |
52 | 52 | |
53 | - /** |
|
54 | - * Value provided should definetely be a serialized string. We should unserialize into an array |
|
55 | - * |
|
56 | - * @param string $value_found_in_db_for_model_object |
|
57 | - * @return array |
|
58 | - */ |
|
59 | - public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
60 | - { |
|
61 | - return EEH_Array::maybe_unserialize($value_found_in_db_for_model_object); |
|
62 | - } |
|
53 | + /** |
|
54 | + * Value provided should definetely be a serialized string. We should unserialize into an array |
|
55 | + * |
|
56 | + * @param string $value_found_in_db_for_model_object |
|
57 | + * @return array |
|
58 | + */ |
|
59 | + public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
60 | + { |
|
61 | + return EEH_Array::maybe_unserialize($value_found_in_db_for_model_object); |
|
62 | + } |
|
63 | 63 | |
64 | 64 | |
65 | - /** |
|
66 | - * Gets a string representation of the array |
|
67 | - * |
|
68 | - * @param mixed $value_on_field_to_be_outputted |
|
69 | - * @param string|null $schema , possible values are ',', others can be added |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, ?string $schema = null) |
|
73 | - { |
|
74 | - switch ($schema) { |
|
75 | - case 'print_r': |
|
76 | - $pretty_value = print_r($value_on_field_to_be_outputted, true); |
|
77 | - break; |
|
78 | - case 'as_table': |
|
79 | - $pretty_value = EEH_Template::layout_array_as_table($value_on_field_to_be_outputted); |
|
80 | - break; |
|
81 | - default: |
|
82 | - $pretty_value = implode(", ", $value_on_field_to_be_outputted); |
|
83 | - } |
|
84 | - return $pretty_value; |
|
85 | - } |
|
65 | + /** |
|
66 | + * Gets a string representation of the array |
|
67 | + * |
|
68 | + * @param mixed $value_on_field_to_be_outputted |
|
69 | + * @param string|null $schema , possible values are ',', others can be added |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, ?string $schema = null) |
|
73 | + { |
|
74 | + switch ($schema) { |
|
75 | + case 'print_r': |
|
76 | + $pretty_value = print_r($value_on_field_to_be_outputted, true); |
|
77 | + break; |
|
78 | + case 'as_table': |
|
79 | + $pretty_value = EEH_Template::layout_array_as_table($value_on_field_to_be_outputted); |
|
80 | + break; |
|
81 | + default: |
|
82 | + $pretty_value = implode(", ", $value_on_field_to_be_outputted); |
|
83 | + } |
|
84 | + return $pretty_value; |
|
85 | + } |
|
86 | 86 | } |
@@ -19,765 +19,765 @@ |
||
19 | 19 | */ |
20 | 20 | class EE_Datetime_Field extends EE_Model_Field_Base |
21 | 21 | { |
22 | - /** |
|
23 | - * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
24 | - * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
25 | - * |
|
26 | - * @type string unix_timestamp_regex |
|
27 | - */ |
|
28 | - const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
29 | - |
|
30 | - /** |
|
31 | - * @type string mysql_timestamp_format |
|
32 | - */ |
|
33 | - const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
34 | - |
|
35 | - /** |
|
36 | - * @type string mysql_date_format |
|
37 | - */ |
|
38 | - const mysql_date_format = 'Y-m-d'; |
|
39 | - |
|
40 | - /** |
|
41 | - * @type string mysql_time_format |
|
42 | - */ |
|
43 | - const mysql_time_format = 'H:i:s'; |
|
44 | - |
|
45 | - /** |
|
46 | - * Const for using in the default value. If the field's default is set to this, |
|
47 | - * then we will return the time of calling `get_default_value()`, not |
|
48 | - * just the current time at construction |
|
49 | - */ |
|
50 | - const now = 'now'; |
|
51 | - |
|
52 | - /** |
|
53 | - * The following properties hold the default formats for date and time. |
|
54 | - * Defaults are set via the constructor and can be overridden on class instantiation. |
|
55 | - * However they can also be overridden later by the set_format() method |
|
56 | - * (and corresponding set_date_format, set_time_format methods); |
|
57 | - */ |
|
58 | - |
|
59 | - protected string $_date_format = ''; |
|
60 | - |
|
61 | - protected string $_time_format = ''; |
|
62 | - |
|
63 | - protected string $_pretty_date_format = ''; |
|
64 | - |
|
65 | - protected string $_pretty_time_format = ''; |
|
66 | - |
|
67 | - protected ?DateTimeZone $_DateTimeZone = null; |
|
68 | - |
|
69 | - protected ?DateTimeZone $_UTC_DateTimeZone = null; |
|
70 | - |
|
71 | - protected ?DateTimeZone $_blog_DateTimeZone = null; |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
76 | - * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
77 | - * and time returned via getters. |
|
78 | - * |
|
79 | - * @var mixed (null|string) |
|
80 | - */ |
|
81 | - protected $_date_time_output; |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * timezone string |
|
86 | - * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
87 | - * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
88 | - * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
89 | - */ |
|
90 | - protected ?string $_timezone_string = null; |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
95 | - * offsets for comparison purposes). |
|
96 | - * |
|
97 | - * @var int |
|
98 | - */ |
|
99 | - protected int $_blog_offset = 0; |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $table_column |
|
104 | - * @param string $nice_name |
|
105 | - * @param bool $nullable |
|
106 | - * @param string|null $default_value |
|
107 | - * @param string|null $timezone_string |
|
108 | - * @param string|null $date_format |
|
109 | - * @param string|null $time_format |
|
110 | - * @param string|null $pretty_date_format |
|
111 | - * @param string|null $pretty_time_format |
|
112 | - * @throws InvalidArgumentException |
|
113 | - * @throws Exception |
|
114 | - */ |
|
115 | - public function __construct( |
|
116 | - string $table_column, |
|
117 | - string $nice_name, |
|
118 | - bool $nullable, |
|
119 | - ?string $default_value, |
|
120 | - ?string $timezone_string = '', |
|
121 | - ?string $date_format = '', |
|
122 | - ?string $time_format = '', |
|
123 | - ?string $pretty_date_format = '', |
|
124 | - ?string $pretty_time_format = '' |
|
125 | - ) { |
|
126 | - $this->set_date_format($date_format); |
|
127 | - $this->set_time_format($time_format); |
|
128 | - $this->set_date_format($pretty_date_format, true); |
|
129 | - $this->set_time_format($pretty_time_format, true); |
|
130 | - |
|
131 | - parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
132 | - $this->set_timezone($timezone_string); |
|
133 | - $this->setSchemaType(SchemaType::STRING); |
|
134 | - $this->setSchemaFormat(SchemaFormat::DATETIME); |
|
135 | - } |
|
136 | - |
|
137 | - |
|
138 | - /** |
|
139 | - * @return DateTimeZone |
|
140 | - * @throws Exception |
|
141 | - */ |
|
142 | - public function get_UTC_DateTimeZone(): DateTimeZone |
|
143 | - { |
|
144 | - return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
145 | - ? $this->_UTC_DateTimeZone |
|
146 | - : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * @return DateTimeZone |
|
152 | - * @throws Exception |
|
153 | - */ |
|
154 | - public function get_blog_DateTimeZone(): DateTimeZone |
|
155 | - { |
|
156 | - return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
157 | - ? $this->_blog_DateTimeZone |
|
158 | - : $this->_create_timezone_object_from_timezone_string(); |
|
159 | - } |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
|
164 | - * |
|
165 | - * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
|
166 | - * timestamp |
|
167 | - * @return DateTime |
|
168 | - * @throws Exception |
|
169 | - */ |
|
170 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
171 | - { |
|
172 | - return $this->_get_date_object($value_inputted_for_field_on_model_object); |
|
173 | - } |
|
174 | - |
|
175 | - |
|
176 | - /** |
|
177 | - * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
178 | - * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
179 | - * |
|
180 | - * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
181 | - * @return string The final assembled format string. |
|
182 | - */ |
|
183 | - protected function _get_date_time_output(bool $pretty = false): string |
|
184 | - { |
|
185 | - switch ($this->_date_time_output) { |
|
186 | - case 'time': |
|
187 | - return $pretty |
|
188 | - ? $this->_pretty_time_format |
|
189 | - : $this->_time_format; |
|
190 | - |
|
191 | - case 'date': |
|
192 | - return $pretty |
|
193 | - ? $this->_pretty_date_format |
|
194 | - : $this->_date_format; |
|
195 | - |
|
196 | - default: |
|
197 | - return $pretty |
|
198 | - ? trim($this->_pretty_date_format . ' ' . $this->_pretty_time_format) |
|
199 | - : trim($this->_date_format . ' ' . $this->_time_format); |
|
200 | - } |
|
201 | - } |
|
202 | - |
|
203 | - |
|
204 | - /** |
|
205 | - * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
206 | - * returned (using the format properties) |
|
207 | - * |
|
208 | - * @param string|null $what acceptable values are 'time' or 'date'. |
|
209 | - * Any other value will be set but will always result |
|
210 | - * in both 'date' and 'time' being returned. |
|
211 | - * @return void |
|
212 | - */ |
|
213 | - public function set_date_time_output(?string $what = null) |
|
214 | - { |
|
215 | - $this->_date_time_output = $what; |
|
216 | - } |
|
217 | - |
|
218 | - |
|
219 | - /** |
|
220 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
221 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
222 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
223 | - * We also set some other properties in this method. |
|
224 | - * |
|
225 | - * @param string|null $timezone_string A valid timezone string as described by @link |
|
226 | - * http://www.php.net/manual/en/timezones.php |
|
227 | - * @return void |
|
228 | - * @throws InvalidArgumentException |
|
229 | - * @throws InvalidDataTypeException |
|
230 | - * @throws InvalidInterfaceException |
|
231 | - * @throws Exception |
|
232 | - */ |
|
233 | - public function set_timezone(?string $timezone_string = '') |
|
234 | - { |
|
235 | - if (empty($timezone_string) && $this->_timezone_string !== null) { |
|
236 | - // leave the timezone AS-IS if we already have one and |
|
237 | - // the function arg didn't provide one |
|
238 | - return; |
|
239 | - } |
|
240 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
241 | - $this->_timezone_string = ! empty($timezone_string) |
|
242 | - ? $timezone_string |
|
243 | - : 'UTC'; |
|
244 | - $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
245 | - } |
|
246 | - |
|
247 | - |
|
248 | - /** |
|
249 | - * _create_timezone_object_from_timezone_name |
|
250 | - * |
|
251 | - * @access protected |
|
252 | - * @param string|null $timezone_string |
|
253 | - * @return DateTimeZone |
|
254 | - * @throws InvalidArgumentException |
|
255 | - * @throws InvalidDataTypeException |
|
256 | - * @throws InvalidInterfaceException |
|
257 | - * @throws Exception |
|
258 | - */ |
|
259 | - protected function _create_timezone_object_from_timezone_string(?string $timezone_string = ''): DateTimeZone |
|
260 | - { |
|
261 | - return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
262 | - } |
|
263 | - |
|
264 | - |
|
265 | - /** |
|
266 | - * This just returns whatever is set for the current timezone. |
|
267 | - * |
|
268 | - * @access public |
|
269 | - * @return string timezone string |
|
270 | - */ |
|
271 | - public function get_timezone(): ?string |
|
272 | - { |
|
273 | - return $this->_timezone_string; |
|
274 | - } |
|
275 | - |
|
276 | - |
|
277 | - /** |
|
278 | - * set the $_date_format property |
|
279 | - * |
|
280 | - * @access public |
|
281 | - * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
282 | - * @param bool $pretty Whether to set pretty format or not. |
|
283 | - * @return void |
|
284 | - */ |
|
285 | - public function set_date_format(string $format, bool $pretty = false) |
|
286 | - { |
|
287 | - if ($pretty) { |
|
288 | - $this->_pretty_date_format = new DateFormat($format); |
|
289 | - } else { |
|
290 | - $this->_date_format = new DateFormat($format); |
|
291 | - } |
|
292 | - } |
|
293 | - |
|
294 | - |
|
295 | - /** |
|
296 | - * return the $_date_format property value. |
|
297 | - * |
|
298 | - * @param bool $pretty Whether to get pretty format or not. |
|
299 | - * @return string |
|
300 | - */ |
|
301 | - public function get_date_format(bool $pretty = false): string |
|
302 | - { |
|
303 | - return $pretty |
|
304 | - ? $this->_pretty_date_format |
|
305 | - : $this->_date_format; |
|
306 | - } |
|
307 | - |
|
308 | - |
|
309 | - /** |
|
310 | - * set the $_time_format property |
|
311 | - * |
|
312 | - * @access public |
|
313 | - * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
314 | - * @param bool $pretty Whether to set pretty format or not. |
|
315 | - * @return void |
|
316 | - */ |
|
317 | - public function set_time_format(string $format, bool $pretty = false) |
|
318 | - { |
|
319 | - if ($pretty) { |
|
320 | - $this->_pretty_time_format = new TimeFormat($format); |
|
321 | - } else { |
|
322 | - $this->_time_format = new TimeFormat($format); |
|
323 | - } |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * return the $_time_format property value. |
|
329 | - * |
|
330 | - * @param bool $pretty Whether to get pretty format or not. |
|
331 | - * @return string |
|
332 | - */ |
|
333 | - public function get_time_format(bool $pretty = false): string |
|
334 | - { |
|
335 | - return $pretty |
|
336 | - ? $this->_pretty_time_format |
|
337 | - : $this->_time_format; |
|
338 | - } |
|
339 | - |
|
340 | - |
|
341 | - /** |
|
342 | - * set the $_pretty_date_format property |
|
343 | - * |
|
344 | - * @access public |
|
345 | - * @param string|null $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
346 | - * @return void |
|
347 | - */ |
|
348 | - public function set_pretty_date_format(?string $format) |
|
349 | - { |
|
350 | - $this->set_date_format($format, true); |
|
351 | - } |
|
352 | - |
|
353 | - |
|
354 | - /** |
|
355 | - * set the $_pretty_time_format property |
|
356 | - * |
|
357 | - * @access public |
|
358 | - * @param string|null $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
359 | - * @return void |
|
360 | - */ |
|
361 | - public function set_pretty_time_format(?string $format) |
|
362 | - { |
|
363 | - $this->set_time_format($format, true); |
|
364 | - } |
|
365 | - |
|
366 | - |
|
367 | - /** |
|
368 | - * Only sets the time portion of the datetime. |
|
369 | - * |
|
370 | - * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
371 | - * @param DateTime $current current DateTime object for the datetime field |
|
372 | - * @return DateTime |
|
373 | - */ |
|
374 | - public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current): DateTime |
|
375 | - { |
|
376 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
377 | - // Otherwise parse the string. |
|
378 | - if ($time_to_set_string instanceof DateTime) { |
|
379 | - $parsed = [ |
|
380 | - 'hour' => $time_to_set_string->format('H'), |
|
381 | - 'minute' => $time_to_set_string->format('i'), |
|
382 | - 'second' => $time_to_set_string->format('s'), |
|
383 | - ]; |
|
384 | - } else { |
|
385 | - // parse incoming string |
|
386 | - $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
387 | - } |
|
388 | - EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
389 | - return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
390 | - } |
|
391 | - |
|
392 | - |
|
393 | - /** |
|
394 | - * Only sets the date portion of the datetime. |
|
395 | - * |
|
396 | - * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
|
397 | - * @param DateTime $current current DateTime object for the datetime field |
|
398 | - * @return DateTime |
|
399 | - */ |
|
400 | - public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current): DateTime |
|
401 | - { |
|
402 | - // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
403 | - // Otherwise parse the string. |
|
404 | - if ($date_to_set_string instanceof DateTime) { |
|
405 | - $parsed = [ |
|
406 | - 'year' => $date_to_set_string->format('Y'), |
|
407 | - 'month' => $date_to_set_string->format('m'), |
|
408 | - 'day' => $date_to_set_string->format('d'), |
|
409 | - ]; |
|
410 | - } else { |
|
411 | - // parse incoming string |
|
412 | - $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
413 | - } |
|
414 | - EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
415 | - return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
416 | - } |
|
417 | - |
|
418 | - |
|
419 | - /** |
|
420 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
|
421 | - * datetime gets to this stage it should ALREADY be in UTC time |
|
422 | - * |
|
423 | - * @param DateTime $DateTime |
|
424 | - * @return string formatted date time for given timezone |
|
425 | - * @throws EE_Error |
|
426 | - */ |
|
427 | - public function prepare_for_get($DateTime): string |
|
428 | - { |
|
429 | - return $this->_prepare_for_display($DateTime); |
|
430 | - } |
|
431 | - |
|
432 | - |
|
433 | - /** |
|
434 | - * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
435 | - * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
436 | - * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
437 | - * abbreviation to the date_string. |
|
438 | - * |
|
439 | - * @param mixed $DateTime |
|
440 | - * @param string|null $schema |
|
441 | - * @return string |
|
442 | - * @throws EE_Error |
|
443 | - */ |
|
444 | - public function prepare_for_pretty_echoing($DateTime, ?string $schema = null): string |
|
445 | - { |
|
446 | - $schema = $schema ?: true; |
|
447 | - return $this->_prepare_for_display($DateTime, $schema); |
|
448 | - } |
|
449 | - |
|
450 | - |
|
451 | - /** |
|
452 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
453 | - * timezone). |
|
454 | - * |
|
455 | - * @param DateTime|null $DateTime |
|
456 | - * @param bool|string $schema |
|
457 | - * @return string |
|
458 | - * @throws EE_Error |
|
459 | - * @throws Exception |
|
460 | - */ |
|
461 | - protected function _prepare_for_display(?DateTime $DateTime, $schema = false): string |
|
462 | - { |
|
463 | - if (! $DateTime instanceof DateTime) { |
|
464 | - if ($this->_nullable) { |
|
465 | - return ''; |
|
466 | - } |
|
467 | - if (WP_DEBUG) { |
|
468 | - throw new EE_Error( |
|
469 | - sprintf( |
|
470 | - esc_html__( |
|
471 | - '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.', |
|
472 | - 'event_espresso' |
|
473 | - ), |
|
474 | - $this->_nicename |
|
475 | - ) |
|
476 | - ); |
|
477 | - } |
|
478 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now); |
|
479 | - EE_Error::add_error( |
|
480 | - sprintf( |
|
481 | - esc_html__( |
|
482 | - '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.', |
|
483 | - 'event_espresso' |
|
484 | - ), |
|
485 | - $this->_nicename |
|
486 | - ), |
|
487 | - __FILE__, |
|
488 | - __FUNCTION__, |
|
489 | - __LINE__ |
|
490 | - ); |
|
491 | - } |
|
492 | - $format_string = $this->_get_date_time_output($schema); |
|
493 | - EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
494 | - if ($schema) { |
|
495 | - $timezone_string = ''; |
|
496 | - if ($this->_display_timezone()) { |
|
497 | - // must be explicit because schema could equal true. |
|
498 | - if ($schema === 'no_html') { |
|
499 | - $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
500 | - } else { |
|
501 | - $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
502 | - } |
|
503 | - } |
|
504 | - |
|
505 | - return $DateTime->format($format_string) . $timezone_string; |
|
506 | - } |
|
507 | - return $DateTime->format($format_string); |
|
508 | - } |
|
509 | - |
|
510 | - |
|
511 | - /** |
|
512 | - * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
513 | - * timezone). |
|
514 | - * |
|
515 | - * @param mixed $datetime_value u |
|
516 | - * @return string mysql timestamp in UTC |
|
517 | - * @throws EE_Error |
|
518 | - * @throws Exception |
|
519 | - */ |
|
520 | - public function prepare_for_use_in_db($datetime_value): ?string |
|
521 | - { |
|
522 | - // we allow an empty value or DateTime object, but nothing else. |
|
523 | - if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
524 | - $datetime_value = $this->_get_date_object($datetime_value); |
|
525 | - if (! $datetime_value instanceof DateTime) { |
|
526 | - throw new EE_Error( |
|
527 | - sprintf( |
|
528 | - esc_html__( |
|
529 | - 'The incoming value being prepared for setting in the database for the %1$s field must either be empty or a php DateTime object, instead of: %2$s %3$s', |
|
530 | - 'event_espresso' |
|
531 | - ), |
|
532 | - $this->get_name(), |
|
533 | - '<br />', |
|
534 | - print_r($datetime_value, true) |
|
535 | - ) |
|
536 | - ); |
|
537 | - } |
|
538 | - } |
|
539 | - |
|
540 | - if ($datetime_value instanceof DateTime) { |
|
541 | - if (! $datetime_value instanceof DbSafeDateTime) { |
|
542 | - $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
543 | - } |
|
544 | - EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
|
545 | - return $datetime_value->format( |
|
546 | - EE_Datetime_Field::mysql_timestamp_format |
|
547 | - ); |
|
548 | - } |
|
549 | - |
|
550 | - // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
551 | - return ! $this->_nullable |
|
552 | - ? current_time('mysql', true) |
|
553 | - : null; |
|
554 | - } |
|
555 | - |
|
556 | - |
|
557 | - /** |
|
558 | - * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
559 | - * allowed) |
|
560 | - * |
|
561 | - * @param string $datetime_string mysql timestamp in UTC |
|
562 | - * @return bool|DbSafeDateTime|null |
|
563 | - * @throws EE_Error |
|
564 | - * @throws Exception |
|
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 | - $DateTime = empty($datetime_string) |
|
574 | - ? new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()) |
|
575 | - : DbSafeDateTime::createFromFormat( |
|
576 | - EE_Datetime_Field::mysql_timestamp_format, |
|
577 | - $datetime_string, |
|
578 | - $this->get_UTC_DateTimeZone() |
|
579 | - ); |
|
580 | - |
|
581 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
582 | - // if still no datetime object, then let's just use now |
|
583 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
584 | - } |
|
585 | - // THEN apply the field's set DateTimeZone |
|
586 | - EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
587 | - return $DateTime; |
|
588 | - } |
|
589 | - |
|
590 | - |
|
591 | - /** |
|
592 | - * All this method does is determine if we're going to display the timezone string or not on any output. |
|
593 | - * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
594 | - * If so, then true. |
|
595 | - * |
|
596 | - * @return bool true for yes false for no |
|
597 | - * @throws Exception |
|
598 | - */ |
|
599 | - protected function _display_timezone(): bool |
|
600 | - { |
|
601 | - // first let's do a comparison of timezone strings. |
|
602 | - // If they match then we can get out without any further calculations |
|
603 | - $blog_string = get_option('timezone_string'); |
|
604 | - if ($blog_string === $this->_timezone_string) { |
|
605 | - return false; |
|
606 | - } |
|
607 | - // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
608 | - $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
609 | - $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
610 | - // now compare |
|
611 | - return $blog_offset !== $this_offset; |
|
612 | - } |
|
613 | - |
|
614 | - |
|
615 | - /** |
|
616 | - * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
617 | - * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
618 | - * with. |
|
619 | - * |
|
620 | - * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
|
621 | - * in the format that is set on the date_field (or DateTime |
|
622 | - * object)! |
|
623 | - * @return DateTime |
|
624 | - * @throws Exception |
|
625 | - * @throws Exception |
|
626 | - */ |
|
627 | - protected function _get_date_object($date_string) |
|
628 | - { |
|
629 | - // first if this is an empty date_string and nullable is allowed, just return null. |
|
630 | - if ($this->_nullable && empty($date_string)) { |
|
631 | - return null; |
|
632 | - } |
|
633 | - |
|
634 | - // if incoming date |
|
635 | - if ($date_string instanceof DateTime) { |
|
636 | - EEH_DTT_Helper::setTimezone($date_string, $this->_DateTimeZone); |
|
637 | - return $date_string; |
|
638 | - } |
|
639 | - // if empty date_string and made it here. |
|
640 | - // Return a datetime object for now in the given timezone. |
|
641 | - if (empty($date_string)) { |
|
642 | - return new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
643 | - } |
|
644 | - // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
645 | - if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
646 | - try { |
|
647 | - // This is operating under the assumption that the incoming Unix timestamp |
|
648 | - // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
649 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
650 | - $DateTime->setTimestamp($date_string); |
|
651 | - |
|
652 | - return $DateTime; |
|
653 | - } catch (Exception $e) { |
|
654 | - // should be rare, but if things got fooled then let's just continue |
|
655 | - } |
|
656 | - } |
|
657 | - // not a unix timestamp. So we will use the set format on this object and set timezone to |
|
658 | - // create the DateTime object. |
|
659 | - $format = $this->_date_format . ' ' . $this->_time_format; |
|
660 | - try { |
|
661 | - $DateTime = DbSafeDateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
662 | - if (! $DateTime instanceof DbSafeDateTime) { |
|
663 | - throw new EE_Error( |
|
664 | - sprintf( |
|
665 | - esc_html__( |
|
666 | - '"%1$s" does not represent a valid Date Time in the format "%2$s".', |
|
667 | - 'event_espresso' |
|
668 | - ), |
|
669 | - $date_string, |
|
670 | - $format |
|
671 | - ) |
|
672 | - ); |
|
673 | - } |
|
674 | - } catch (Exception $e) { |
|
675 | - // if we made it here then likely then something went really wrong. |
|
676 | - // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
677 | - $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
678 | - } |
|
679 | - |
|
680 | - return $DateTime; |
|
681 | - } |
|
682 | - |
|
683 | - |
|
684 | - /** |
|
685 | - * get_timezone_transitions |
|
686 | - * |
|
687 | - * @param DateTimeZone $DateTimeZone |
|
688 | - * @param int|null $time |
|
689 | - * @param bool|null $first_only |
|
690 | - * @return array |
|
691 | - */ |
|
692 | - public function get_timezone_transitions( |
|
693 | - DateTimeZone $DateTimeZone, |
|
694 | - ?int $time = null, |
|
695 | - bool $first_only = true |
|
696 | - ): array { |
|
697 | - return EEH_DTT_Helper::get_timezone_transitions($DateTimeZone, $time, $first_only); |
|
698 | - } |
|
699 | - |
|
700 | - |
|
701 | - /** |
|
702 | - * get_timezone_offset |
|
703 | - * |
|
704 | - * @param DateTimeZone $DateTimeZone |
|
705 | - * @param int|string|null $time |
|
706 | - * @return mixed |
|
707 | - * @throws DomainException |
|
708 | - */ |
|
709 | - public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
710 | - { |
|
711 | - return EEH_DTT_Helper::get_timezone_offset($DateTimeZone, $time); |
|
712 | - } |
|
713 | - |
|
714 | - |
|
715 | - /** |
|
716 | - * This will take an incoming timezone string and return the abbreviation for that timezone |
|
717 | - * |
|
718 | - * @param string|null $timezone_string |
|
719 | - * @return string abbreviation |
|
720 | - * @throws Exception |
|
721 | - */ |
|
722 | - public function get_timezone_abbrev(?string $timezone_string = ''): string |
|
723 | - { |
|
724 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
725 | - $dateTime = new DateTime(EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
726 | - return $dateTime->format('T'); |
|
727 | - } |
|
728 | - |
|
729 | - |
|
730 | - /** |
|
731 | - * Overrides the parent to allow for having a dynamic "now" value |
|
732 | - * |
|
733 | - * @return mixed |
|
734 | - */ |
|
735 | - public function get_default_value() |
|
736 | - { |
|
737 | - if ($this->_default_value === EE_Datetime_Field::now) { |
|
738 | - return time(); |
|
739 | - } |
|
740 | - return parent::get_default_value(); |
|
741 | - } |
|
742 | - |
|
743 | - |
|
744 | - /** |
|
745 | - * Gets the default datetime object from the field's default time |
|
746 | - * |
|
747 | - * @return DbSafeDateTime|DateTime|null |
|
748 | - * @throws InvalidArgumentException |
|
749 | - * @throws InvalidDataTypeException |
|
750 | - * @throws InvalidInterfaceException |
|
751 | - * @throws Exception |
|
752 | - * @since 4.9.66.p |
|
753 | - */ |
|
754 | - public function getDefaultDateTimeObj() |
|
755 | - { |
|
756 | - $default_raw = $this->get_default_value(); |
|
757 | - if ($default_raw instanceof DateTime) { |
|
758 | - return $default_raw; |
|
759 | - } |
|
760 | - if (is_null($default_raw)) { |
|
761 | - return null; |
|
762 | - } |
|
763 | - $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($this->get_timezone()); |
|
764 | - $timezone = new DateTimeZone($timezone_string); |
|
765 | - |
|
766 | - // can't pass unix timestamps directly to Datetime constructor |
|
767 | - if (is_numeric($default_raw)) { |
|
768 | - $datetime = new DbSafeDateTime(); |
|
769 | - $datetime->setTimestamp($default_raw); |
|
770 | - return $datetime; |
|
771 | - } |
|
772 | - return new DbSafeDateTime($default_raw, $timezone); |
|
773 | - } |
|
774 | - |
|
775 | - |
|
776 | - public function getSchemaDescription(): string |
|
777 | - { |
|
778 | - return sprintf( |
|
779 | - esc_html__('%s - the value for this field is in the timezone of the site.', 'event_espresso'), |
|
780 | - $this->get_nicename() |
|
781 | - ); |
|
782 | - } |
|
22 | + /** |
|
23 | + * The pattern we're looking for is if only the characters 0-9 are found and there are only |
|
24 | + * 10 or more numbers (because 9 numbers even with all 9's would be sometime in 2001 ) |
|
25 | + * |
|
26 | + * @type string unix_timestamp_regex |
|
27 | + */ |
|
28 | + const unix_timestamp_regex = '/[0-9]{10,}/'; |
|
29 | + |
|
30 | + /** |
|
31 | + * @type string mysql_timestamp_format |
|
32 | + */ |
|
33 | + const mysql_timestamp_format = 'Y-m-d H:i:s'; |
|
34 | + |
|
35 | + /** |
|
36 | + * @type string mysql_date_format |
|
37 | + */ |
|
38 | + const mysql_date_format = 'Y-m-d'; |
|
39 | + |
|
40 | + /** |
|
41 | + * @type string mysql_time_format |
|
42 | + */ |
|
43 | + const mysql_time_format = 'H:i:s'; |
|
44 | + |
|
45 | + /** |
|
46 | + * Const for using in the default value. If the field's default is set to this, |
|
47 | + * then we will return the time of calling `get_default_value()`, not |
|
48 | + * just the current time at construction |
|
49 | + */ |
|
50 | + const now = 'now'; |
|
51 | + |
|
52 | + /** |
|
53 | + * The following properties hold the default formats for date and time. |
|
54 | + * Defaults are set via the constructor and can be overridden on class instantiation. |
|
55 | + * However they can also be overridden later by the set_format() method |
|
56 | + * (and corresponding set_date_format, set_time_format methods); |
|
57 | + */ |
|
58 | + |
|
59 | + protected string $_date_format = ''; |
|
60 | + |
|
61 | + protected string $_time_format = ''; |
|
62 | + |
|
63 | + protected string $_pretty_date_format = ''; |
|
64 | + |
|
65 | + protected string $_pretty_time_format = ''; |
|
66 | + |
|
67 | + protected ?DateTimeZone $_DateTimeZone = null; |
|
68 | + |
|
69 | + protected ?DateTimeZone $_UTC_DateTimeZone = null; |
|
70 | + |
|
71 | + protected ?DateTimeZone $_blog_DateTimeZone = null; |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * This property holds how we want the output returned when getting a datetime string. It is set for the |
|
76 | + * set_date_time_output() method. By default this is empty. When empty, we are assuming that we want both date |
|
77 | + * and time returned via getters. |
|
78 | + * |
|
79 | + * @var mixed (null|string) |
|
80 | + */ |
|
81 | + protected $_date_time_output; |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * timezone string |
|
86 | + * This gets set by the constructor and can be changed by the "set_timezone()" method so that we know what timezone |
|
87 | + * incoming strings|timestamps are in. This can also be used before a get to set what timezone you want strings |
|
88 | + * coming out of the object to be in. Default timezone is the current WP timezone option setting |
|
89 | + */ |
|
90 | + protected ?string $_timezone_string = null; |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * This holds whatever UTC offset for the blog (we automatically convert timezone strings into their related |
|
95 | + * offsets for comparison purposes). |
|
96 | + * |
|
97 | + * @var int |
|
98 | + */ |
|
99 | + protected int $_blog_offset = 0; |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $table_column |
|
104 | + * @param string $nice_name |
|
105 | + * @param bool $nullable |
|
106 | + * @param string|null $default_value |
|
107 | + * @param string|null $timezone_string |
|
108 | + * @param string|null $date_format |
|
109 | + * @param string|null $time_format |
|
110 | + * @param string|null $pretty_date_format |
|
111 | + * @param string|null $pretty_time_format |
|
112 | + * @throws InvalidArgumentException |
|
113 | + * @throws Exception |
|
114 | + */ |
|
115 | + public function __construct( |
|
116 | + string $table_column, |
|
117 | + string $nice_name, |
|
118 | + bool $nullable, |
|
119 | + ?string $default_value, |
|
120 | + ?string $timezone_string = '', |
|
121 | + ?string $date_format = '', |
|
122 | + ?string $time_format = '', |
|
123 | + ?string $pretty_date_format = '', |
|
124 | + ?string $pretty_time_format = '' |
|
125 | + ) { |
|
126 | + $this->set_date_format($date_format); |
|
127 | + $this->set_time_format($time_format); |
|
128 | + $this->set_date_format($pretty_date_format, true); |
|
129 | + $this->set_time_format($pretty_time_format, true); |
|
130 | + |
|
131 | + parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
132 | + $this->set_timezone($timezone_string); |
|
133 | + $this->setSchemaType(SchemaType::STRING); |
|
134 | + $this->setSchemaFormat(SchemaFormat::DATETIME); |
|
135 | + } |
|
136 | + |
|
137 | + |
|
138 | + /** |
|
139 | + * @return DateTimeZone |
|
140 | + * @throws Exception |
|
141 | + */ |
|
142 | + public function get_UTC_DateTimeZone(): DateTimeZone |
|
143 | + { |
|
144 | + return $this->_UTC_DateTimeZone instanceof DateTimeZone |
|
145 | + ? $this->_UTC_DateTimeZone |
|
146 | + : $this->_create_timezone_object_from_timezone_string('UTC'); |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * @return DateTimeZone |
|
152 | + * @throws Exception |
|
153 | + */ |
|
154 | + public function get_blog_DateTimeZone(): DateTimeZone |
|
155 | + { |
|
156 | + return $this->_blog_DateTimeZone instanceof DateTimeZone |
|
157 | + ? $this->_blog_DateTimeZone |
|
158 | + : $this->_create_timezone_object_from_timezone_string(); |
|
159 | + } |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * this prepares any incoming date data and make sure its converted to a utc unix timestamp |
|
164 | + * |
|
165 | + * @param string|int $value_inputted_for_field_on_model_object could be a string formatted date time or int unix |
|
166 | + * timestamp |
|
167 | + * @return DateTime |
|
168 | + * @throws Exception |
|
169 | + */ |
|
170 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
171 | + { |
|
172 | + return $this->_get_date_object($value_inputted_for_field_on_model_object); |
|
173 | + } |
|
174 | + |
|
175 | + |
|
176 | + /** |
|
177 | + * This returns the format string to be used by getters depending on what the $_date_time_output property is set at. |
|
178 | + * getters need to know whether we're just returning the date or the time or both. By default we return both. |
|
179 | + * |
|
180 | + * @param bool $pretty If we're returning the pretty formats or standard format string. |
|
181 | + * @return string The final assembled format string. |
|
182 | + */ |
|
183 | + protected function _get_date_time_output(bool $pretty = false): string |
|
184 | + { |
|
185 | + switch ($this->_date_time_output) { |
|
186 | + case 'time': |
|
187 | + return $pretty |
|
188 | + ? $this->_pretty_time_format |
|
189 | + : $this->_time_format; |
|
190 | + |
|
191 | + case 'date': |
|
192 | + return $pretty |
|
193 | + ? $this->_pretty_date_format |
|
194 | + : $this->_date_format; |
|
195 | + |
|
196 | + default: |
|
197 | + return $pretty |
|
198 | + ? trim($this->_pretty_date_format . ' ' . $this->_pretty_time_format) |
|
199 | + : trim($this->_date_format . ' ' . $this->_time_format); |
|
200 | + } |
|
201 | + } |
|
202 | + |
|
203 | + |
|
204 | + /** |
|
205 | + * This just sets the $_date_time_output property so we can flag how date and times are formatted before being |
|
206 | + * returned (using the format properties) |
|
207 | + * |
|
208 | + * @param string|null $what acceptable values are 'time' or 'date'. |
|
209 | + * Any other value will be set but will always result |
|
210 | + * in both 'date' and 'time' being returned. |
|
211 | + * @return void |
|
212 | + */ |
|
213 | + public function set_date_time_output(?string $what = null) |
|
214 | + { |
|
215 | + $this->_date_time_output = $what; |
|
216 | + } |
|
217 | + |
|
218 | + |
|
219 | + /** |
|
220 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
221 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
222 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). |
|
223 | + * We also set some other properties in this method. |
|
224 | + * |
|
225 | + * @param string|null $timezone_string A valid timezone string as described by @link |
|
226 | + * http://www.php.net/manual/en/timezones.php |
|
227 | + * @return void |
|
228 | + * @throws InvalidArgumentException |
|
229 | + * @throws InvalidDataTypeException |
|
230 | + * @throws InvalidInterfaceException |
|
231 | + * @throws Exception |
|
232 | + */ |
|
233 | + public function set_timezone(?string $timezone_string = '') |
|
234 | + { |
|
235 | + if (empty($timezone_string) && $this->_timezone_string !== null) { |
|
236 | + // leave the timezone AS-IS if we already have one and |
|
237 | + // the function arg didn't provide one |
|
238 | + return; |
|
239 | + } |
|
240 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
241 | + $this->_timezone_string = ! empty($timezone_string) |
|
242 | + ? $timezone_string |
|
243 | + : 'UTC'; |
|
244 | + $this->_DateTimeZone = $this->_create_timezone_object_from_timezone_string($this->_timezone_string); |
|
245 | + } |
|
246 | + |
|
247 | + |
|
248 | + /** |
|
249 | + * _create_timezone_object_from_timezone_name |
|
250 | + * |
|
251 | + * @access protected |
|
252 | + * @param string|null $timezone_string |
|
253 | + * @return DateTimeZone |
|
254 | + * @throws InvalidArgumentException |
|
255 | + * @throws InvalidDataTypeException |
|
256 | + * @throws InvalidInterfaceException |
|
257 | + * @throws Exception |
|
258 | + */ |
|
259 | + protected function _create_timezone_object_from_timezone_string(?string $timezone_string = ''): DateTimeZone |
|
260 | + { |
|
261 | + return new DateTimeZone(EEH_DTT_Helper::get_valid_timezone_string($timezone_string)); |
|
262 | + } |
|
263 | + |
|
264 | + |
|
265 | + /** |
|
266 | + * This just returns whatever is set for the current timezone. |
|
267 | + * |
|
268 | + * @access public |
|
269 | + * @return string timezone string |
|
270 | + */ |
|
271 | + public function get_timezone(): ?string |
|
272 | + { |
|
273 | + return $this->_timezone_string; |
|
274 | + } |
|
275 | + |
|
276 | + |
|
277 | + /** |
|
278 | + * set the $_date_format property |
|
279 | + * |
|
280 | + * @access public |
|
281 | + * @param string $format a new date format (corresponding to formats accepted by PHP date() function) |
|
282 | + * @param bool $pretty Whether to set pretty format or not. |
|
283 | + * @return void |
|
284 | + */ |
|
285 | + public function set_date_format(string $format, bool $pretty = false) |
|
286 | + { |
|
287 | + if ($pretty) { |
|
288 | + $this->_pretty_date_format = new DateFormat($format); |
|
289 | + } else { |
|
290 | + $this->_date_format = new DateFormat($format); |
|
291 | + } |
|
292 | + } |
|
293 | + |
|
294 | + |
|
295 | + /** |
|
296 | + * return the $_date_format property value. |
|
297 | + * |
|
298 | + * @param bool $pretty Whether to get pretty format or not. |
|
299 | + * @return string |
|
300 | + */ |
|
301 | + public function get_date_format(bool $pretty = false): string |
|
302 | + { |
|
303 | + return $pretty |
|
304 | + ? $this->_pretty_date_format |
|
305 | + : $this->_date_format; |
|
306 | + } |
|
307 | + |
|
308 | + |
|
309 | + /** |
|
310 | + * set the $_time_format property |
|
311 | + * |
|
312 | + * @access public |
|
313 | + * @param string $format a new time format (corresponding to formats accepted by PHP date() function) |
|
314 | + * @param bool $pretty Whether to set pretty format or not. |
|
315 | + * @return void |
|
316 | + */ |
|
317 | + public function set_time_format(string $format, bool $pretty = false) |
|
318 | + { |
|
319 | + if ($pretty) { |
|
320 | + $this->_pretty_time_format = new TimeFormat($format); |
|
321 | + } else { |
|
322 | + $this->_time_format = new TimeFormat($format); |
|
323 | + } |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * return the $_time_format property value. |
|
329 | + * |
|
330 | + * @param bool $pretty Whether to get pretty format or not. |
|
331 | + * @return string |
|
332 | + */ |
|
333 | + public function get_time_format(bool $pretty = false): string |
|
334 | + { |
|
335 | + return $pretty |
|
336 | + ? $this->_pretty_time_format |
|
337 | + : $this->_time_format; |
|
338 | + } |
|
339 | + |
|
340 | + |
|
341 | + /** |
|
342 | + * set the $_pretty_date_format property |
|
343 | + * |
|
344 | + * @access public |
|
345 | + * @param string|null $format a new pretty date format (corresponding to formats accepted by PHP date() function) |
|
346 | + * @return void |
|
347 | + */ |
|
348 | + public function set_pretty_date_format(?string $format) |
|
349 | + { |
|
350 | + $this->set_date_format($format, true); |
|
351 | + } |
|
352 | + |
|
353 | + |
|
354 | + /** |
|
355 | + * set the $_pretty_time_format property |
|
356 | + * |
|
357 | + * @access public |
|
358 | + * @param string|null $format a new pretty time format (corresponding to formats accepted by PHP date() function) |
|
359 | + * @return void |
|
360 | + */ |
|
361 | + public function set_pretty_time_format(?string $format) |
|
362 | + { |
|
363 | + $this->set_time_format($format, true); |
|
364 | + } |
|
365 | + |
|
366 | + |
|
367 | + /** |
|
368 | + * Only sets the time portion of the datetime. |
|
369 | + * |
|
370 | + * @param string|DateTime $time_to_set_string like 8am OR a DateTime object. |
|
371 | + * @param DateTime $current current DateTime object for the datetime field |
|
372 | + * @return DateTime |
|
373 | + */ |
|
374 | + public function prepare_for_set_with_new_time($time_to_set_string, DateTime $current): DateTime |
|
375 | + { |
|
376 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
377 | + // Otherwise parse the string. |
|
378 | + if ($time_to_set_string instanceof DateTime) { |
|
379 | + $parsed = [ |
|
380 | + 'hour' => $time_to_set_string->format('H'), |
|
381 | + 'minute' => $time_to_set_string->format('i'), |
|
382 | + 'second' => $time_to_set_string->format('s'), |
|
383 | + ]; |
|
384 | + } else { |
|
385 | + // parse incoming string |
|
386 | + $parsed = date_parse_from_format($this->_time_format, $time_to_set_string); |
|
387 | + } |
|
388 | + EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
389 | + return $current->setTime($parsed['hour'], $parsed['minute'], $parsed['second']); |
|
390 | + } |
|
391 | + |
|
392 | + |
|
393 | + /** |
|
394 | + * Only sets the date portion of the datetime. |
|
395 | + * |
|
396 | + * @param string|DateTime $date_to_set_string like Friday, January 8th or a DateTime object. |
|
397 | + * @param DateTime $current current DateTime object for the datetime field |
|
398 | + * @return DateTime |
|
399 | + */ |
|
400 | + public function prepare_for_set_with_new_date($date_to_set_string, DateTime $current): DateTime |
|
401 | + { |
|
402 | + // if $time_to_set_string is datetime object, then let's use it to set the parse array. |
|
403 | + // Otherwise parse the string. |
|
404 | + if ($date_to_set_string instanceof DateTime) { |
|
405 | + $parsed = [ |
|
406 | + 'year' => $date_to_set_string->format('Y'), |
|
407 | + 'month' => $date_to_set_string->format('m'), |
|
408 | + 'day' => $date_to_set_string->format('d'), |
|
409 | + ]; |
|
410 | + } else { |
|
411 | + // parse incoming string |
|
412 | + $parsed = date_parse_from_format($this->_date_format, $date_to_set_string); |
|
413 | + } |
|
414 | + EEH_DTT_Helper::setTimezone($current, $this->_DateTimeZone); |
|
415 | + return $current->setDate($parsed['year'], $parsed['month'], $parsed['day']); |
|
416 | + } |
|
417 | + |
|
418 | + |
|
419 | + /** |
|
420 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 timezone). When the |
|
421 | + * datetime gets to this stage it should ALREADY be in UTC time |
|
422 | + * |
|
423 | + * @param DateTime $DateTime |
|
424 | + * @return string formatted date time for given timezone |
|
425 | + * @throws EE_Error |
|
426 | + */ |
|
427 | + public function prepare_for_get($DateTime): string |
|
428 | + { |
|
429 | + return $this->_prepare_for_display($DateTime); |
|
430 | + } |
|
431 | + |
|
432 | + |
|
433 | + /** |
|
434 | + * This differs from prepare_for_get in that it considers whether the internal $_timezone differs |
|
435 | + * from the set wp timezone. If so, then it returns the datetime string formatted via |
|
436 | + * _pretty_date_format, and _pretty_time_format. However, it also appends a timezone |
|
437 | + * abbreviation to the date_string. |
|
438 | + * |
|
439 | + * @param mixed $DateTime |
|
440 | + * @param string|null $schema |
|
441 | + * @return string |
|
442 | + * @throws EE_Error |
|
443 | + */ |
|
444 | + public function prepare_for_pretty_echoing($DateTime, ?string $schema = null): string |
|
445 | + { |
|
446 | + $schema = $schema ?: true; |
|
447 | + return $this->_prepare_for_display($DateTime, $schema); |
|
448 | + } |
|
449 | + |
|
450 | + |
|
451 | + /** |
|
452 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
453 | + * timezone). |
|
454 | + * |
|
455 | + * @param DateTime|null $DateTime |
|
456 | + * @param bool|string $schema |
|
457 | + * @return string |
|
458 | + * @throws EE_Error |
|
459 | + * @throws Exception |
|
460 | + */ |
|
461 | + protected function _prepare_for_display(?DateTime $DateTime, $schema = false): string |
|
462 | + { |
|
463 | + if (! $DateTime instanceof DateTime) { |
|
464 | + if ($this->_nullable) { |
|
465 | + return ''; |
|
466 | + } |
|
467 | + if (WP_DEBUG) { |
|
468 | + throw new EE_Error( |
|
469 | + sprintf( |
|
470 | + esc_html__( |
|
471 | + '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.', |
|
472 | + 'event_espresso' |
|
473 | + ), |
|
474 | + $this->_nicename |
|
475 | + ) |
|
476 | + ); |
|
477 | + } |
|
478 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now); |
|
479 | + EE_Error::add_error( |
|
480 | + sprintf( |
|
481 | + esc_html__( |
|
482 | + '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.', |
|
483 | + 'event_espresso' |
|
484 | + ), |
|
485 | + $this->_nicename |
|
486 | + ), |
|
487 | + __FILE__, |
|
488 | + __FUNCTION__, |
|
489 | + __LINE__ |
|
490 | + ); |
|
491 | + } |
|
492 | + $format_string = $this->_get_date_time_output($schema); |
|
493 | + EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
494 | + if ($schema) { |
|
495 | + $timezone_string = ''; |
|
496 | + if ($this->_display_timezone()) { |
|
497 | + // must be explicit because schema could equal true. |
|
498 | + if ($schema === 'no_html') { |
|
499 | + $timezone_string = ' (' . $DateTime->format('T') . ')'; |
|
500 | + } else { |
|
501 | + $timezone_string = ' <span class="ee_dtt_timezone_string">(' . $DateTime->format('T') . ')</span>'; |
|
502 | + } |
|
503 | + } |
|
504 | + |
|
505 | + return $DateTime->format($format_string) . $timezone_string; |
|
506 | + } |
|
507 | + return $DateTime->format($format_string); |
|
508 | + } |
|
509 | + |
|
510 | + |
|
511 | + /** |
|
512 | + * This prepares the EE_DateTime value to be saved to the db as mysql timestamp (UTC +0 |
|
513 | + * timezone). |
|
514 | + * |
|
515 | + * @param mixed $datetime_value u |
|
516 | + * @return string mysql timestamp in UTC |
|
517 | + * @throws EE_Error |
|
518 | + * @throws Exception |
|
519 | + */ |
|
520 | + public function prepare_for_use_in_db($datetime_value): ?string |
|
521 | + { |
|
522 | + // we allow an empty value or DateTime object, but nothing else. |
|
523 | + if (! empty($datetime_value) && ! $datetime_value instanceof DateTime) { |
|
524 | + $datetime_value = $this->_get_date_object($datetime_value); |
|
525 | + if (! $datetime_value instanceof DateTime) { |
|
526 | + throw new EE_Error( |
|
527 | + sprintf( |
|
528 | + esc_html__( |
|
529 | + 'The incoming value being prepared for setting in the database for the %1$s field must either be empty or a php DateTime object, instead of: %2$s %3$s', |
|
530 | + 'event_espresso' |
|
531 | + ), |
|
532 | + $this->get_name(), |
|
533 | + '<br />', |
|
534 | + print_r($datetime_value, true) |
|
535 | + ) |
|
536 | + ); |
|
537 | + } |
|
538 | + } |
|
539 | + |
|
540 | + if ($datetime_value instanceof DateTime) { |
|
541 | + if (! $datetime_value instanceof DbSafeDateTime) { |
|
542 | + $datetime_value = DbSafeDateTime::createFromDateTime($datetime_value); |
|
543 | + } |
|
544 | + EEH_DTT_Helper::setTimezone($datetime_value, $this->get_UTC_DateTimeZone()); |
|
545 | + return $datetime_value->format( |
|
546 | + EE_Datetime_Field::mysql_timestamp_format |
|
547 | + ); |
|
548 | + } |
|
549 | + |
|
550 | + // if $datetime_value is empty, and ! $this->_nullable, use current_time() but set the GMT flag to true |
|
551 | + return ! $this->_nullable |
|
552 | + ? current_time('mysql', true) |
|
553 | + : null; |
|
554 | + } |
|
555 | + |
|
556 | + |
|
557 | + /** |
|
558 | + * This prepares the datetime for internal usage as a PHP DateTime object OR null (if nullable is |
|
559 | + * allowed) |
|
560 | + * |
|
561 | + * @param string $datetime_string mysql timestamp in UTC |
|
562 | + * @return bool|DbSafeDateTime|null |
|
563 | + * @throws EE_Error |
|
564 | + * @throws Exception |
|
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 | + $DateTime = empty($datetime_string) |
|
574 | + ? new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()) |
|
575 | + : DbSafeDateTime::createFromFormat( |
|
576 | + EE_Datetime_Field::mysql_timestamp_format, |
|
577 | + $datetime_string, |
|
578 | + $this->get_UTC_DateTimeZone() |
|
579 | + ); |
|
580 | + |
|
581 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
582 | + // if still no datetime object, then let's just use now |
|
583 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->get_UTC_DateTimeZone()); |
|
584 | + } |
|
585 | + // THEN apply the field's set DateTimeZone |
|
586 | + EEH_DTT_Helper::setTimezone($DateTime, $this->_DateTimeZone); |
|
587 | + return $DateTime; |
|
588 | + } |
|
589 | + |
|
590 | + |
|
591 | + /** |
|
592 | + * All this method does is determine if we're going to display the timezone string or not on any output. |
|
593 | + * To determine this we check if the set timezone offset is different than the blog's set timezone offset. |
|
594 | + * If so, then true. |
|
595 | + * |
|
596 | + * @return bool true for yes false for no |
|
597 | + * @throws Exception |
|
598 | + */ |
|
599 | + protected function _display_timezone(): bool |
|
600 | + { |
|
601 | + // first let's do a comparison of timezone strings. |
|
602 | + // If they match then we can get out without any further calculations |
|
603 | + $blog_string = get_option('timezone_string'); |
|
604 | + if ($blog_string === $this->_timezone_string) { |
|
605 | + return false; |
|
606 | + } |
|
607 | + // now we need to calc the offset for the timezone string so we can compare with the blog offset. |
|
608 | + $this_offset = $this->get_timezone_offset($this->_DateTimeZone); |
|
609 | + $blog_offset = $this->get_timezone_offset($this->get_blog_DateTimeZone()); |
|
610 | + // now compare |
|
611 | + return $blog_offset !== $this_offset; |
|
612 | + } |
|
613 | + |
|
614 | + |
|
615 | + /** |
|
616 | + * This method returns a php DateTime object for setting on the EE_Base_Class model. |
|
617 | + * EE passes around DateTime objects because they are MUCH easier to manipulate and deal |
|
618 | + * with. |
|
619 | + * |
|
620 | + * @param int|string|DateTime $date_string This should be the incoming date string. It's assumed to be |
|
621 | + * in the format that is set on the date_field (or DateTime |
|
622 | + * object)! |
|
623 | + * @return DateTime |
|
624 | + * @throws Exception |
|
625 | + * @throws Exception |
|
626 | + */ |
|
627 | + protected function _get_date_object($date_string) |
|
628 | + { |
|
629 | + // first if this is an empty date_string and nullable is allowed, just return null. |
|
630 | + if ($this->_nullable && empty($date_string)) { |
|
631 | + return null; |
|
632 | + } |
|
633 | + |
|
634 | + // if incoming date |
|
635 | + if ($date_string instanceof DateTime) { |
|
636 | + EEH_DTT_Helper::setTimezone($date_string, $this->_DateTimeZone); |
|
637 | + return $date_string; |
|
638 | + } |
|
639 | + // if empty date_string and made it here. |
|
640 | + // Return a datetime object for now in the given timezone. |
|
641 | + if (empty($date_string)) { |
|
642 | + return new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
643 | + } |
|
644 | + // if $date_string is matches something that looks like a Unix timestamp let's just use it. |
|
645 | + if (preg_match(EE_Datetime_Field::unix_timestamp_regex, $date_string)) { |
|
646 | + try { |
|
647 | + // This is operating under the assumption that the incoming Unix timestamp |
|
648 | + // is an ACTUAL Unix timestamp and not the calculated one output by current_time('timestamp'); |
|
649 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
650 | + $DateTime->setTimestamp($date_string); |
|
651 | + |
|
652 | + return $DateTime; |
|
653 | + } catch (Exception $e) { |
|
654 | + // should be rare, but if things got fooled then let's just continue |
|
655 | + } |
|
656 | + } |
|
657 | + // not a unix timestamp. So we will use the set format on this object and set timezone to |
|
658 | + // create the DateTime object. |
|
659 | + $format = $this->_date_format . ' ' . $this->_time_format; |
|
660 | + try { |
|
661 | + $DateTime = DbSafeDateTime::createFromFormat($format, $date_string, $this->_DateTimeZone); |
|
662 | + if (! $DateTime instanceof DbSafeDateTime) { |
|
663 | + throw new EE_Error( |
|
664 | + sprintf( |
|
665 | + esc_html__( |
|
666 | + '"%1$s" does not represent a valid Date Time in the format "%2$s".', |
|
667 | + 'event_espresso' |
|
668 | + ), |
|
669 | + $date_string, |
|
670 | + $format |
|
671 | + ) |
|
672 | + ); |
|
673 | + } |
|
674 | + } catch (Exception $e) { |
|
675 | + // if we made it here then likely then something went really wrong. |
|
676 | + // Instead of throwing an exception, let's just return a DateTime object for now, in the set timezone. |
|
677 | + $DateTime = new DbSafeDateTime(EE_Datetime_Field::now, $this->_DateTimeZone); |
|
678 | + } |
|
679 | + |
|
680 | + return $DateTime; |
|
681 | + } |
|
682 | + |
|
683 | + |
|
684 | + /** |
|
685 | + * get_timezone_transitions |
|
686 | + * |
|
687 | + * @param DateTimeZone $DateTimeZone |
|
688 | + * @param int|null $time |
|
689 | + * @param bool|null $first_only |
|
690 | + * @return array |
|
691 | + */ |
|
692 | + public function get_timezone_transitions( |
|
693 | + DateTimeZone $DateTimeZone, |
|
694 | + ?int $time = null, |
|
695 | + bool $first_only = true |
|
696 | + ): array { |
|
697 | + return EEH_DTT_Helper::get_timezone_transitions($DateTimeZone, $time, $first_only); |
|
698 | + } |
|
699 | + |
|
700 | + |
|
701 | + /** |
|
702 | + * get_timezone_offset |
|
703 | + * |
|
704 | + * @param DateTimeZone $DateTimeZone |
|
705 | + * @param int|string|null $time |
|
706 | + * @return mixed |
|
707 | + * @throws DomainException |
|
708 | + */ |
|
709 | + public function get_timezone_offset(DateTimeZone $DateTimeZone, $time = null) |
|
710 | + { |
|
711 | + return EEH_DTT_Helper::get_timezone_offset($DateTimeZone, $time); |
|
712 | + } |
|
713 | + |
|
714 | + |
|
715 | + /** |
|
716 | + * This will take an incoming timezone string and return the abbreviation for that timezone |
|
717 | + * |
|
718 | + * @param string|null $timezone_string |
|
719 | + * @return string abbreviation |
|
720 | + * @throws Exception |
|
721 | + */ |
|
722 | + public function get_timezone_abbrev(?string $timezone_string = ''): string |
|
723 | + { |
|
724 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($timezone_string); |
|
725 | + $dateTime = new DateTime(EE_Datetime_Field::now, new DateTimeZone($timezone_string)); |
|
726 | + return $dateTime->format('T'); |
|
727 | + } |
|
728 | + |
|
729 | + |
|
730 | + /** |
|
731 | + * Overrides the parent to allow for having a dynamic "now" value |
|
732 | + * |
|
733 | + * @return mixed |
|
734 | + */ |
|
735 | + public function get_default_value() |
|
736 | + { |
|
737 | + if ($this->_default_value === EE_Datetime_Field::now) { |
|
738 | + return time(); |
|
739 | + } |
|
740 | + return parent::get_default_value(); |
|
741 | + } |
|
742 | + |
|
743 | + |
|
744 | + /** |
|
745 | + * Gets the default datetime object from the field's default time |
|
746 | + * |
|
747 | + * @return DbSafeDateTime|DateTime|null |
|
748 | + * @throws InvalidArgumentException |
|
749 | + * @throws InvalidDataTypeException |
|
750 | + * @throws InvalidInterfaceException |
|
751 | + * @throws Exception |
|
752 | + * @since 4.9.66.p |
|
753 | + */ |
|
754 | + public function getDefaultDateTimeObj() |
|
755 | + { |
|
756 | + $default_raw = $this->get_default_value(); |
|
757 | + if ($default_raw instanceof DateTime) { |
|
758 | + return $default_raw; |
|
759 | + } |
|
760 | + if (is_null($default_raw)) { |
|
761 | + return null; |
|
762 | + } |
|
763 | + $timezone_string = EEH_DTT_Helper::get_valid_timezone_string($this->get_timezone()); |
|
764 | + $timezone = new DateTimeZone($timezone_string); |
|
765 | + |
|
766 | + // can't pass unix timestamps directly to Datetime constructor |
|
767 | + if (is_numeric($default_raw)) { |
|
768 | + $datetime = new DbSafeDateTime(); |
|
769 | + $datetime->setTimestamp($default_raw); |
|
770 | + return $datetime; |
|
771 | + } |
|
772 | + return new DbSafeDateTime($default_raw, $timezone); |
|
773 | + } |
|
774 | + |
|
775 | + |
|
776 | + public function getSchemaDescription(): string |
|
777 | + { |
|
778 | + return sprintf( |
|
779 | + esc_html__('%s - the value for this field is in the timezone of the site.', 'event_espresso'), |
|
780 | + $this->get_nicename() |
|
781 | + ); |
|
782 | + } |
|
783 | 783 | } |