@@ -3,16 +3,16 @@ |
||
3 | 3 | |
4 | 4 | class EE_DB_Only_Float_Field extends EE_DB_Only_Field_Base |
5 | 5 | { |
6 | - /** |
|
7 | - * @param string $table_column |
|
8 | - * @param string $nicename |
|
9 | - * @param bool $nullable |
|
10 | - * @param null $default_value |
|
11 | - */ |
|
12 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | - { |
|
14 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | - $this->setSchemaType('number'); |
|
16 | - } |
|
6 | + /** |
|
7 | + * @param string $table_column |
|
8 | + * @param string $nicename |
|
9 | + * @param bool $nullable |
|
10 | + * @param null $default_value |
|
11 | + */ |
|
12 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | + { |
|
14 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | + $this->setSchemaType('number'); |
|
16 | + } |
|
17 | 17 | |
18 | 18 | } |
@@ -8,91 +8,91 @@ |
||
8 | 8 | class EE_Money_Field extends EE_Float_Field |
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($table_column, $nicename, $nullable, $default_value = null) |
|
18 | - { |
|
19 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
20 | - $this->setSchemaType('object'); |
|
21 | - } |
|
11 | + /** |
|
12 | + * @param string $table_column |
|
13 | + * @param string $nicename |
|
14 | + * @param bool $nullable |
|
15 | + * @param null $default_value |
|
16 | + */ |
|
17 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
18 | + { |
|
19 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
20 | + $this->setSchemaType('object'); |
|
21 | + } |
|
22 | 22 | |
23 | 23 | |
24 | - /** |
|
25 | - * Schemas: |
|
26 | - * 'localized_float': "3,023.00" |
|
27 | - * 'no_currency_code': "$3,023.00" |
|
28 | - * null: "$3,023.00<span>USD</span>" |
|
29 | - * |
|
30 | - * @param string $value_on_field_to_be_outputted |
|
31 | - * @param string $schema |
|
32 | - * @return string |
|
33 | - */ |
|
34 | - function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
35 | - { |
|
36 | - $pretty_float = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted); |
|
24 | + /** |
|
25 | + * Schemas: |
|
26 | + * 'localized_float': "3,023.00" |
|
27 | + * 'no_currency_code': "$3,023.00" |
|
28 | + * null: "$3,023.00<span>USD</span>" |
|
29 | + * |
|
30 | + * @param string $value_on_field_to_be_outputted |
|
31 | + * @param string $schema |
|
32 | + * @return string |
|
33 | + */ |
|
34 | + function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
35 | + { |
|
36 | + $pretty_float = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted); |
|
37 | 37 | |
38 | - if ($schema == 'localized_float') { |
|
39 | - return $pretty_float; |
|
40 | - } |
|
41 | - if ($schema == 'no_currency_code') { |
|
38 | + if ($schema == 'localized_float') { |
|
39 | + return $pretty_float; |
|
40 | + } |
|
41 | + if ($schema == 'no_currency_code') { |
|
42 | 42 | // echo "schema no currency!"; |
43 | - $display_code = false; |
|
44 | - } else { |
|
45 | - $display_code = true; |
|
46 | - } |
|
47 | - //we don't use the $pretty_float because format_currency will take care of it. |
|
48 | - return EEH_Template::format_currency($value_on_field_to_be_outputted, false, $display_code); |
|
49 | - } |
|
43 | + $display_code = false; |
|
44 | + } else { |
|
45 | + $display_code = true; |
|
46 | + } |
|
47 | + //we don't use the $pretty_float because format_currency will take care of it. |
|
48 | + return EEH_Template::format_currency($value_on_field_to_be_outputted, false, $display_code); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * If provided with a string, strips out money-related formatting to turn it into a proper float. |
|
53 | - * Rounds the float to the correct number of decimal places for this country's currency. |
|
54 | - * Also, interprets periods and commas according to the country's currency settings. |
|
55 | - * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
|
56 | - * |
|
57 | - * @param string $value_inputted_for_field_on_model_object |
|
58 | - * @return float |
|
59 | - */ |
|
60 | - function prepare_for_set($value_inputted_for_field_on_model_object) |
|
61 | - { |
|
62 | - //remove any currencies etc. |
|
51 | + /** |
|
52 | + * If provided with a string, strips out money-related formatting to turn it into a proper float. |
|
53 | + * Rounds the float to the correct number of decimal places for this country's currency. |
|
54 | + * Also, interprets periods and commas according to the country's currency settings. |
|
55 | + * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
|
56 | + * |
|
57 | + * @param string $value_inputted_for_field_on_model_object |
|
58 | + * @return float |
|
59 | + */ |
|
60 | + function prepare_for_set($value_inputted_for_field_on_model_object) |
|
61 | + { |
|
62 | + //remove any currencies etc. |
|
63 | 63 | // if(is_string($value_inputted_for_field_on_model_object)){ |
64 | 64 | // $value_inputted_for_field_on_model_object = preg_replace("/[^0-9,.]/", "", $value_inputted_for_field_on_model_object); |
65 | 65 | // } |
66 | - //now it's a float-style string or number |
|
67 | - $float_val = parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
68 | - //round to the correctly number of decimal places for this currency |
|
69 | - $rounded_value = round($float_val, EE_Registry::instance()->CFG->currency->dec_plc); |
|
70 | - return $rounded_value; |
|
71 | - } |
|
66 | + //now it's a float-style string or number |
|
67 | + $float_val = parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
68 | + //round to the correctly number of decimal places for this currency |
|
69 | + $rounded_value = round($float_val, EE_Registry::instance()->CFG->currency->dec_plc); |
|
70 | + return $rounded_value; |
|
71 | + } |
|
72 | 72 | |
73 | - function prepare_for_get($value_of_field_on_model_object) |
|
74 | - { |
|
75 | - $c = EE_Registry::instance()->CFG->currency; |
|
76 | - return round(parent::prepare_for_get($value_of_field_on_model_object), $c->dec_plc); |
|
77 | - } |
|
73 | + function prepare_for_get($value_of_field_on_model_object) |
|
74 | + { |
|
75 | + $c = EE_Registry::instance()->CFG->currency; |
|
76 | + return round(parent::prepare_for_get($value_of_field_on_model_object), $c->dec_plc); |
|
77 | + } |
|
78 | 78 | |
79 | - public function getSchemaProperties() |
|
80 | - { |
|
81 | - return array( |
|
82 | - 'raw' => array( |
|
83 | - 'description' => sprintf( |
|
84 | - __('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'), |
|
85 | - $this->get_nicename() |
|
86 | - ), |
|
87 | - 'type' => 'number' |
|
88 | - ), |
|
89 | - 'pretty' => array( |
|
90 | - 'description' => sprintf( |
|
91 | - __('%s - formatted for display in the set currency and decimal places.', 'event_espresso'), |
|
92 | - $this->get_nicename() |
|
93 | - ), |
|
94 | - 'type' => 'string' |
|
95 | - ) |
|
96 | - ); |
|
97 | - } |
|
79 | + public function getSchemaProperties() |
|
80 | + { |
|
81 | + return array( |
|
82 | + 'raw' => array( |
|
83 | + 'description' => sprintf( |
|
84 | + __('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'), |
|
85 | + $this->get_nicename() |
|
86 | + ), |
|
87 | + 'type' => 'number' |
|
88 | + ), |
|
89 | + 'pretty' => array( |
|
90 | + 'description' => sprintf( |
|
91 | + __('%s - formatted for display in the set currency and decimal places.', 'event_espresso'), |
|
92 | + $this->get_nicename() |
|
93 | + ), |
|
94 | + 'type' => 'string' |
|
95 | + ) |
|
96 | + ); |
|
97 | + } |
|
98 | 98 | } |
99 | 99 | \ No newline at end of file |
@@ -4,27 +4,27 @@ |
||
4 | 4 | class EE_Primary_Key_Int_Field extends EE_Primary_Key_Field_Base |
5 | 5 | { |
6 | 6 | |
7 | - public function __construct($table_column, $nicename) |
|
8 | - { |
|
9 | - parent::__construct($table_column, $nicename, 0); |
|
10 | - $this->setSchemaType('integer'); |
|
11 | - } |
|
7 | + public function __construct($table_column, $nicename) |
|
8 | + { |
|
9 | + parent::__construct($table_column, $nicename, 0); |
|
10 | + $this->setSchemaType('integer'); |
|
11 | + } |
|
12 | 12 | |
13 | - function prepare_for_set($value_inputted_for_field_on_model_object) |
|
14 | - { |
|
15 | - if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
16 | - $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
17 | - } |
|
18 | - return absint($value_inputted_for_field_on_model_object); |
|
19 | - } |
|
13 | + function prepare_for_set($value_inputted_for_field_on_model_object) |
|
14 | + { |
|
15 | + if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
16 | + $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
17 | + } |
|
18 | + return absint($value_inputted_for_field_on_model_object); |
|
19 | + } |
|
20 | 20 | |
21 | - function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
22 | - { |
|
23 | - return intval($value_found_in_db_for_model_object); |
|
24 | - } |
|
21 | + function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
22 | + { |
|
23 | + return intval($value_found_in_db_for_model_object); |
|
24 | + } |
|
25 | 25 | |
26 | - function is_auto_increment() |
|
27 | - { |
|
28 | - return true; |
|
29 | - } |
|
26 | + function is_auto_increment() |
|
27 | + { |
|
28 | + return true; |
|
29 | + } |
|
30 | 30 | } |
31 | 31 | \ No newline at end of file |
@@ -12,131 +12,131 @@ |
||
12 | 12 | class EE_Enum_Text_Field extends EE_Text_Field_Base |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * @var array $_allowed_enum_values |
|
17 | - */ |
|
18 | - public $_allowed_enum_values; |
|
19 | - |
|
20 | - /** |
|
21 | - * @param string $table_column |
|
22 | - * @param string $nice_name |
|
23 | - * @param boolean $nullable |
|
24 | - * @param mixed $default_value |
|
25 | - * @param array $allowed_enum_values keys are values to be used in the DB, values are how they should be displayed |
|
26 | - */ |
|
27 | - function __construct($table_column, $nice_name, $nullable, $default_value, $allowed_enum_values) |
|
28 | - { |
|
29 | - $this->_allowed_enum_values = $allowed_enum_values; |
|
30 | - parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
31 | - $this->setSchemaType('object'); |
|
32 | - } |
|
33 | - |
|
34 | - |
|
35 | - |
|
36 | - /** |
|
37 | - * Returns the list of allowed enum options, but filterable. |
|
38 | - * This is used internally |
|
39 | - * |
|
40 | - * @return array |
|
41 | - */ |
|
42 | - protected function _allowed_enum_values() |
|
43 | - { |
|
44 | - return apply_filters( |
|
45 | - 'FHEE__EE_Enum_Text_Field___allowed_enum_options', |
|
46 | - $this->_allowed_enum_values, |
|
47 | - $this |
|
48 | - ); |
|
49 | - } |
|
50 | - |
|
51 | - |
|
52 | - |
|
53 | - /** |
|
54 | - * When setting, just verify that the value being used matches what we've defined as allowable enum values. |
|
55 | - * If not, throw an error (but if WP_DEBUG is false, just set the value to default). |
|
56 | - * |
|
57 | - * @param string $value_inputted_for_field_on_model_object |
|
58 | - * @return string |
|
59 | - * @throws EE_Error |
|
60 | - */ |
|
61 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
62 | - { |
|
63 | - if ( |
|
64 | - $value_inputted_for_field_on_model_object !== null |
|
65 | - && ! array_key_exists($value_inputted_for_field_on_model_object, $this->_allowed_enum_values()) |
|
66 | - ) { |
|
67 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
68 | - $msg = sprintf( |
|
69 | - __('System is assigning incompatible value "%1$s" to field "%2$s"', 'event_espresso'), |
|
70 | - $value_inputted_for_field_on_model_object, |
|
71 | - $this->_name |
|
72 | - ); |
|
73 | - $msg2 = sprintf( |
|
74 | - __('Allowed values for "%1$s" are "%2$s". You provided: "%3$s"', 'event_espresso'), |
|
75 | - $this->_name, |
|
76 | - implode(', ', array_keys($this->_allowed_enum_values())), |
|
77 | - $value_inputted_for_field_on_model_object |
|
78 | - ); |
|
79 | - EE_Error::add_error("{$msg}||{$msg2}", __FILE__, __FUNCTION__, __LINE__); |
|
80 | - } |
|
81 | - return $this->get_default_value(); |
|
82 | - } |
|
83 | - return $value_inputted_for_field_on_model_object; |
|
84 | - } |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * Gets the pretty version of the enum's value. |
|
89 | - * |
|
90 | - * @param int |string $value_on_field_to_be_outputted |
|
91 | - * @param null $schema |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
95 | - { |
|
96 | - $options = $this->_allowed_enum_values(); |
|
97 | - if (isset($options[$value_on_field_to_be_outputted])) { |
|
98 | - return $options[$value_on_field_to_be_outputted]; |
|
99 | - } else { |
|
100 | - return $value_on_field_to_be_outputted; |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * When retrieving something from the DB, don't enforce the enum's options. If it's in the DB, we just have to live |
|
108 | - * with that. Note also: when we're saving to the DB again, we also don't enforce the enum options. It's ONLY |
|
109 | - * when we're receiving USER input from prepare_for_set() that we enforce the enum options. |
|
110 | - * |
|
111 | - * @param mixed $value_in_db |
|
112 | - * @return mixed |
|
113 | - */ |
|
114 | - public function prepare_for_set_from_db($value_in_db) |
|
115 | - { |
|
116 | - return $value_in_db; |
|
117 | - } |
|
118 | - |
|
119 | - |
|
120 | - public function getSchemaProperties() |
|
121 | - { |
|
122 | - return array( |
|
123 | - 'raw' => array( |
|
124 | - 'description' => sprintf( |
|
125 | - __('%s - the value in the database.', 'event_espresso'), |
|
126 | - $this->get_nicename() |
|
127 | - ), |
|
128 | - 'type' => 'string', |
|
129 | - 'enum' => array_keys($this->_allowed_enum_values) |
|
130 | - ), |
|
131 | - 'pretty' => array( |
|
132 | - 'description' => sprintf( |
|
133 | - __('%s - the value for display.', 'event_espresso'), |
|
134 | - $this->get_nicename() |
|
135 | - ), |
|
136 | - 'type' => 'string', |
|
137 | - 'enum' => array_values($this->_allowed_enum_values), |
|
138 | - 'read_only' => true |
|
139 | - ) |
|
140 | - ); |
|
141 | - } |
|
15 | + /** |
|
16 | + * @var array $_allowed_enum_values |
|
17 | + */ |
|
18 | + public $_allowed_enum_values; |
|
19 | + |
|
20 | + /** |
|
21 | + * @param string $table_column |
|
22 | + * @param string $nice_name |
|
23 | + * @param boolean $nullable |
|
24 | + * @param mixed $default_value |
|
25 | + * @param array $allowed_enum_values keys are values to be used in the DB, values are how they should be displayed |
|
26 | + */ |
|
27 | + function __construct($table_column, $nice_name, $nullable, $default_value, $allowed_enum_values) |
|
28 | + { |
|
29 | + $this->_allowed_enum_values = $allowed_enum_values; |
|
30 | + parent::__construct($table_column, $nice_name, $nullable, $default_value); |
|
31 | + $this->setSchemaType('object'); |
|
32 | + } |
|
33 | + |
|
34 | + |
|
35 | + |
|
36 | + /** |
|
37 | + * Returns the list of allowed enum options, but filterable. |
|
38 | + * This is used internally |
|
39 | + * |
|
40 | + * @return array |
|
41 | + */ |
|
42 | + protected function _allowed_enum_values() |
|
43 | + { |
|
44 | + return apply_filters( |
|
45 | + 'FHEE__EE_Enum_Text_Field___allowed_enum_options', |
|
46 | + $this->_allowed_enum_values, |
|
47 | + $this |
|
48 | + ); |
|
49 | + } |
|
50 | + |
|
51 | + |
|
52 | + |
|
53 | + /** |
|
54 | + * When setting, just verify that the value being used matches what we've defined as allowable enum values. |
|
55 | + * If not, throw an error (but if WP_DEBUG is false, just set the value to default). |
|
56 | + * |
|
57 | + * @param string $value_inputted_for_field_on_model_object |
|
58 | + * @return string |
|
59 | + * @throws EE_Error |
|
60 | + */ |
|
61 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
62 | + { |
|
63 | + if ( |
|
64 | + $value_inputted_for_field_on_model_object !== null |
|
65 | + && ! array_key_exists($value_inputted_for_field_on_model_object, $this->_allowed_enum_values()) |
|
66 | + ) { |
|
67 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
68 | + $msg = sprintf( |
|
69 | + __('System is assigning incompatible value "%1$s" to field "%2$s"', 'event_espresso'), |
|
70 | + $value_inputted_for_field_on_model_object, |
|
71 | + $this->_name |
|
72 | + ); |
|
73 | + $msg2 = sprintf( |
|
74 | + __('Allowed values for "%1$s" are "%2$s". You provided: "%3$s"', 'event_espresso'), |
|
75 | + $this->_name, |
|
76 | + implode(', ', array_keys($this->_allowed_enum_values())), |
|
77 | + $value_inputted_for_field_on_model_object |
|
78 | + ); |
|
79 | + EE_Error::add_error("{$msg}||{$msg2}", __FILE__, __FUNCTION__, __LINE__); |
|
80 | + } |
|
81 | + return $this->get_default_value(); |
|
82 | + } |
|
83 | + return $value_inputted_for_field_on_model_object; |
|
84 | + } |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * Gets the pretty version of the enum's value. |
|
89 | + * |
|
90 | + * @param int |string $value_on_field_to_be_outputted |
|
91 | + * @param null $schema |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
95 | + { |
|
96 | + $options = $this->_allowed_enum_values(); |
|
97 | + if (isset($options[$value_on_field_to_be_outputted])) { |
|
98 | + return $options[$value_on_field_to_be_outputted]; |
|
99 | + } else { |
|
100 | + return $value_on_field_to_be_outputted; |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * When retrieving something from the DB, don't enforce the enum's options. If it's in the DB, we just have to live |
|
108 | + * with that. Note also: when we're saving to the DB again, we also don't enforce the enum options. It's ONLY |
|
109 | + * when we're receiving USER input from prepare_for_set() that we enforce the enum options. |
|
110 | + * |
|
111 | + * @param mixed $value_in_db |
|
112 | + * @return mixed |
|
113 | + */ |
|
114 | + public function prepare_for_set_from_db($value_in_db) |
|
115 | + { |
|
116 | + return $value_in_db; |
|
117 | + } |
|
118 | + |
|
119 | + |
|
120 | + public function getSchemaProperties() |
|
121 | + { |
|
122 | + return array( |
|
123 | + 'raw' => array( |
|
124 | + 'description' => sprintf( |
|
125 | + __('%s - the value in the database.', 'event_espresso'), |
|
126 | + $this->get_nicename() |
|
127 | + ), |
|
128 | + 'type' => 'string', |
|
129 | + 'enum' => array_keys($this->_allowed_enum_values) |
|
130 | + ), |
|
131 | + 'pretty' => array( |
|
132 | + 'description' => sprintf( |
|
133 | + __('%s - the value for display.', 'event_espresso'), |
|
134 | + $this->get_nicename() |
|
135 | + ), |
|
136 | + 'type' => 'string', |
|
137 | + 'enum' => array_values($this->_allowed_enum_values), |
|
138 | + 'read_only' => true |
|
139 | + ) |
|
140 | + ); |
|
141 | + } |
|
142 | 142 | } |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | */ |
212 | 212 | public function get_qualified_column() |
213 | 213 | { |
214 | - return $this->get_table_alias() . "." . $this->get_table_column(); |
|
214 | + return $this->get_table_alias().".".$this->get_table_column(); |
|
215 | 215 | } |
216 | 216 | |
217 | 217 | /** |
@@ -302,7 +302,7 @@ discard block |
||
302 | 302 | { |
303 | 303 | if ($this->is_nullable()) { |
304 | 304 | $this->_schema_type = (array) $this->_schema_type; |
305 | - if (! in_array('null', $this->_schema_type)) { |
|
305 | + if ( ! in_array('null', $this->_schema_type)) { |
|
306 | 306 | $this->_schema_type[] = 'null'; |
307 | 307 | }; |
308 | 308 | } |
@@ -391,7 +391,7 @@ discard block |
||
391 | 391 | */ |
392 | 392 | protected function setSchemaReadOnly($readonly) |
393 | 393 | { |
394 | - if (! is_bool($readonly)) { |
|
394 | + if ( ! is_bool($readonly)) { |
|
395 | 395 | throw new InvalidArgumentException( |
396 | 396 | sprintf( |
397 | 397 | esc_html__('The incoming argument (%s) must be a boolean.', 'event_espresso'), |
@@ -424,7 +424,7 @@ discard block |
||
424 | 424 | * @uses get_schema_type() |
425 | 425 | * @return string |
426 | 426 | */ |
427 | - protected function _get_wpdb_data_type($type='') |
|
427 | + protected function _get_wpdb_data_type($type = '') |
|
428 | 428 | { |
429 | 429 | $type = empty($type) ? $this->getSchemaType() : $type; |
430 | 430 | |
@@ -461,7 +461,7 @@ discard block |
||
461 | 461 | $type = array_flip($type); |
462 | 462 | |
463 | 463 | //check for things that mean '%s' |
464 | - if (isset($type['string'],$type['object'],$type['array'])) { |
|
464 | + if (isset($type['string'], $type['object'], $type['array'])) { |
|
465 | 465 | return '%s'; |
466 | 466 | } |
467 | 467 | |
@@ -537,7 +537,7 @@ discard block |
||
537 | 537 | */ |
538 | 538 | private function validateSchemaType($type) |
539 | 539 | { |
540 | - if (! (is_string($type) || is_array($type))) { |
|
540 | + if ( ! (is_string($type) || is_array($type))) { |
|
541 | 541 | throw new InvalidArgumentException( |
542 | 542 | sprintf( |
543 | 543 | esc_html__('The incoming argument (%s) must be a string or an array.', 'event_espresso'), |
@@ -567,7 +567,7 @@ discard block |
||
567 | 567 | return; |
568 | 568 | } |
569 | 569 | |
570 | - if (! isset($allowable_types[$type])) { |
|
570 | + if ( ! isset($allowable_types[$type])) { |
|
571 | 571 | throw new InvalidArgumentException( |
572 | 572 | sprintf( |
573 | 573 | esc_html__('The incoming argument (%1$s) must be one of the allowable types: %2$s', 'event_espresso'), |
@@ -586,7 +586,7 @@ discard block |
||
586 | 586 | */ |
587 | 587 | private function validateSchemaFormat($format) |
588 | 588 | { |
589 | - if (! is_string($format)) { |
|
589 | + if ( ! is_string($format)) { |
|
590 | 590 | throw new InvalidArgumentException( |
591 | 591 | sprintf( |
592 | 592 | esc_html__('The incoming argument (%s) must be a string.', 'event_espresso'), |
@@ -609,7 +609,7 @@ discard block |
||
609 | 609 | ) |
610 | 610 | ); |
611 | 611 | |
612 | - if (! isset($allowable_formats[$format])) { |
|
612 | + if ( ! isset($allowable_formats[$format])) { |
|
613 | 613 | throw new InvalidArgumentException( |
614 | 614 | sprintf( |
615 | 615 | esc_html__('The incoming argument (%1$s) must be one of the allowable formats: %2$s', 'event_espresso'), |
@@ -21,643 +21,642 @@ |
||
21 | 21 | */ |
22 | 22 | abstract class EE_Model_Field_Base implements HasSchemaInterface |
23 | 23 | { |
24 | - /** |
|
25 | - * The alias for the table the column belongs to. |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected $_table_alias; |
|
29 | - |
|
30 | - /** |
|
31 | - * The actual db column name for the table |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - protected $_table_column; |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * The authoritative name for the table column (used by client code to reference the field). |
|
39 | - * @var string |
|
40 | - */ |
|
41 | - protected $_name; |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * A description for the field. |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $_nicename; |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * Whether the field is nullable or not |
|
53 | - * @var bool |
|
54 | - */ |
|
55 | - protected $_nullable; |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * What the default value for the field should be. |
|
60 | - * @var mixed |
|
61 | - */ |
|
62 | - protected $_default_value; |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * Other configuration for the field |
|
67 | - * @var mixed |
|
68 | - */ |
|
69 | - protected $_other_config; |
|
70 | - |
|
71 | - |
|
72 | - /** |
|
73 | - * The name of the model this field is instantiated for. |
|
74 | - * @var string |
|
75 | - */ |
|
76 | - protected $_model_name; |
|
77 | - |
|
78 | - |
|
79 | - /** |
|
80 | - * This should be a json-schema valid data type for the field. |
|
81 | - * @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
82 | - * @var string |
|
83 | - */ |
|
84 | - private $_schema_type = 'string'; |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * If the schema has a defined format then it should be defined via this property. |
|
89 | - * @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
90 | - * @var string |
|
91 | - */ |
|
92 | - private $_schema_format = ''; |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * Indicates that the value of the field is managed exclusively by the server/model and not something |
|
97 | - * settable by client code. |
|
98 | - * @link http://json-schema.org/latest/json-schema-hypermedia.html#rfc.section.4.4 |
|
99 | - * @var bool |
|
100 | - */ |
|
101 | - private $_schema_readonly = false; |
|
102 | - |
|
103 | - |
|
104 | - /** |
|
105 | - * @param string $table_column |
|
106 | - * @param string $nicename |
|
107 | - * @param bool $nullable |
|
108 | - * @param null $default_value |
|
109 | - */ |
|
110 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
111 | - { |
|
112 | - $this->_table_column = $table_column; |
|
113 | - $this->_nicename = $nicename; |
|
114 | - $this->_nullable = $nullable; |
|
115 | - $this->_default_value = $default_value; |
|
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * @param $table_alias |
|
121 | - * @param $name |
|
122 | - * @param $model_name |
|
123 | - */ |
|
124 | - public function _construct_finalize($table_alias, $name, $model_name) |
|
125 | - { |
|
126 | - $this->_table_alias = $table_alias; |
|
127 | - $this->_name = $name; |
|
128 | - $this->_model_name = $model_name; |
|
129 | - /** |
|
130 | - * allow for changing the defaults |
|
131 | - */ |
|
132 | - $this->_nicename = apply_filters('FHEE__EE_Model_Field_Base___construct_finalize___nicename', |
|
133 | - $this->_nicename, $this); |
|
134 | - $this->_default_value = apply_filters('FHEE__EE_Model_Field_Base___construct_finalize___default_value', |
|
135 | - $this->_default_value, $this); |
|
136 | - } |
|
137 | - |
|
138 | - public function get_table_alias() |
|
139 | - { |
|
140 | - return $this->_table_alias; |
|
141 | - } |
|
142 | - |
|
143 | - public function get_table_column() |
|
144 | - { |
|
145 | - return $this->_table_column; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Returns the name of the model this field is on. Eg 'Event' or 'Ticket_Datetime' |
|
150 | - * |
|
151 | - * @return string |
|
152 | - */ |
|
153 | - public function get_model_name() |
|
154 | - { |
|
155 | - return $this->_model_name; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * @throws \EE_Error |
|
160 | - * @return string |
|
161 | - */ |
|
162 | - public function get_name() |
|
163 | - { |
|
164 | - if ($this->_name) { |
|
165 | - return $this->_name; |
|
166 | - } else { |
|
167 | - throw new EE_Error(sprintf(__("Model field '%s' has no name set. Did you make a model and forget to call the parent model constructor?", |
|
168 | - "event_espresso"), get_class($this))); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - public function get_nicename() |
|
173 | - { |
|
174 | - return $this->_nicename; |
|
175 | - } |
|
176 | - |
|
177 | - public function is_nullable() |
|
178 | - { |
|
179 | - return $this->_nullable; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * returns whether this field is an auto-increment field or not. If it is, then |
|
184 | - * on insertion it can be null. However, on updates it must be present. |
|
185 | - * |
|
186 | - * @return boolean |
|
187 | - */ |
|
188 | - public function is_auto_increment() |
|
189 | - { |
|
190 | - return false; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * The default value in the model object's value domain. See lengthy comment about |
|
195 | - * value domains at the top of EEM_Base |
|
196 | - * |
|
197 | - * @return mixed |
|
198 | - */ |
|
199 | - public function get_default_value() |
|
200 | - { |
|
201 | - return $this->_default_value; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Returns the table alias joined to the table column, however this isn't the right |
|
206 | - * table alias if the aliased table is being joined to. In that case, you can use |
|
207 | - * EE_Model_Parser::extract_table_alias_model_relation_chain_prefix() to find the table's current alias |
|
208 | - * in the current query |
|
209 | - * |
|
210 | - * @return string |
|
211 | - */ |
|
212 | - public function get_qualified_column() |
|
213 | - { |
|
214 | - return $this->get_table_alias() . "." . $this->get_table_column(); |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * When get() is called on a model object (eg EE_Event), before returning its value, |
|
219 | - * call this function on it, allowing us to customize the returned value based on |
|
220 | - * the field's type. Eg, we may want to unserialize it, strip tags, etc. By default, |
|
221 | - * we simply return it. |
|
222 | - * |
|
223 | - * @param mixed $value_of_field_on_model_object |
|
224 | - * @return mixed |
|
225 | - */ |
|
226 | - public function prepare_for_get($value_of_field_on_model_object) |
|
227 | - { |
|
228 | - return $value_of_field_on_model_object; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * When inserting or updating a field on a model object, run this function on each |
|
233 | - * value to prepare it for insertion into the db. Generally this converts |
|
234 | - * the validated input on the model object into the format used in the DB. |
|
235 | - * |
|
236 | - * @param mixed $value_of_field_on_model_object |
|
237 | - * @return mixed |
|
238 | - */ |
|
239 | - public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
240 | - { |
|
241 | - return $value_of_field_on_model_object; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * When creating a brand-new model object, or setting a particular value for one of its fields, this function |
|
246 | - * is called before setting it on the model object. We may want to strip slashes, unserialize the value, etc. |
|
247 | - * By default, we do nothing. |
|
248 | - * |
|
249 | - * If the model field is going to perform any validation on the input, this is where it should be done |
|
250 | - * (once the value is on the model object, it may be used in other ways besides putting it into the DB |
|
251 | - * so it's best to validate it right away). |
|
252 | - * |
|
253 | - * @param mixed $value_inputted_for_field_on_model_object |
|
254 | - * @return mixed |
|
255 | - */ |
|
256 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
257 | - { |
|
258 | - return $value_inputted_for_field_on_model_object; |
|
259 | - } |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * When instantiating a model object from DB results, this function is called before setting each field. |
|
264 | - * We may want to serialize the value, etc. By default, we return the value using prepare_for_set() method as that |
|
265 | - * is the one child classes will most often define. |
|
266 | - * |
|
267 | - * @param mixed $value_found_in_db_for_model_object |
|
268 | - * @return mixed |
|
269 | - */ |
|
270 | - public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
271 | - { |
|
272 | - return $this->prepare_for_set($value_found_in_db_for_model_object); |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * When echoing a field's value on a model object, this function is run to prepare the value for presentation in a |
|
277 | - * webpage. For example, we may want to output floats with 2 decimal places by default, dates as "Monday Jan 12, |
|
278 | - * 2013, at 3:23pm" instead of |
|
279 | - * "8765678632", or any other modifications to how the value should be displayed, but not modified itself. |
|
280 | - * |
|
281 | - * @param mixed $value_on_field_to_be_outputted |
|
282 | - * @return mixed |
|
283 | - */ |
|
284 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
285 | - { |
|
286 | - return $value_on_field_to_be_outputted; |
|
287 | - } |
|
288 | - |
|
289 | - |
|
290 | - /** |
|
291 | - * Returns whatever is set as the nicename for the object. |
|
292 | - * @return string |
|
293 | - */ |
|
294 | - public function getSchemaDescription() |
|
295 | - { |
|
296 | - return $this->get_nicename(); |
|
297 | - } |
|
298 | - |
|
299 | - |
|
300 | - /** |
|
301 | - * Returns whatever is set as the $_schema_type property for the object. |
|
302 | - * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
303 | - * @return string|array |
|
304 | - */ |
|
305 | - public function getSchemaType() |
|
306 | - { |
|
307 | - if ($this->is_nullable()) { |
|
308 | - $this->_schema_type = (array) $this->_schema_type; |
|
309 | - if (! in_array('null', $this->_schema_type)) { |
|
310 | - $this->_schema_type[] = 'null'; |
|
311 | - }; |
|
312 | - } |
|
313 | - return $this->_schema_type; |
|
314 | - } |
|
315 | - |
|
316 | - |
|
317 | - /** |
|
318 | - * Sets the _schema_type property. Child classes should call this in their constructors to override the default state |
|
319 | - * for this property. |
|
320 | - * @param string|array $type |
|
321 | - * @throws InvalidArgumentException |
|
322 | - */ |
|
323 | - protected function setSchemaType($type) |
|
324 | - { |
|
325 | - $this->validateSchemaType($type); |
|
326 | - $this->_schema_type = $type; |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
332 | - * this method and return the properties for the schema. |
|
333 | - * |
|
334 | - * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
335 | - * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
336 | - * |
|
337 | - * @return array |
|
338 | - */ |
|
339 | - public function getSchemaProperties() |
|
340 | - { |
|
341 | - return array(); |
|
342 | - } |
|
343 | - |
|
344 | - |
|
345 | - |
|
346 | - /** |
|
347 | - * By default this returns the scalar default value that was sent in on the class prepped according to the class type |
|
348 | - * as the default. However, when there are schema properties, then the default property is setup to mirror the |
|
349 | - * property keys and correctly prepare the default according to that expected property value. |
|
350 | - * The getSchema method validates whether the schema for default is setup correctly or not according to the schema type |
|
351 | - * |
|
352 | - * @return mixed |
|
353 | - */ |
|
354 | - public function getSchemaDefault() |
|
355 | - { |
|
356 | - $default_value = $this->prepare_for_use_in_db($this->prepare_for_set($this->get_default_value())); |
|
357 | - $schema_properties = $this->getSchemaProperties(); |
|
358 | - |
|
359 | - //if this schema has properties than shape the default value to match the properties shape. |
|
360 | - if ($schema_properties) { |
|
361 | - $value_to_return = array(); |
|
362 | - foreach ($schema_properties as $property_key => $property_schema) { |
|
363 | - switch ($property_key) { |
|
364 | - case 'pretty': |
|
365 | - case 'rendered': |
|
366 | - $value_to_return[$property_key] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
367 | - break; |
|
368 | - default: |
|
369 | - $value_to_return[$property_key] = $default_value; |
|
370 | - break; |
|
371 | - } |
|
372 | - } |
|
373 | - $default_value = $value_to_return; |
|
374 | - } |
|
375 | - return $default_value; |
|
376 | - } |
|
377 | - |
|
378 | - |
|
379 | - |
|
380 | - |
|
381 | - /** |
|
382 | - * If a child class has enum values, they should override this method and provide a simple array |
|
383 | - * of the enum values. |
|
384 | - |
|
385 | - * The reason this is not a property on the class is because there may be filterable enum values that |
|
386 | - * are set on the instantiated object that could be filtered after construct. |
|
387 | - * |
|
388 | - * @return array |
|
389 | - */ |
|
390 | - public function getSchemaEnum() |
|
391 | - { |
|
392 | - return array(); |
|
393 | - } |
|
394 | - |
|
395 | - |
|
396 | - /** |
|
397 | - * This returns the value of the $_schema_format property on the object. |
|
398 | - * @return string |
|
399 | - */ |
|
400 | - public function getSchemaFormat() |
|
401 | - { |
|
402 | - return $this->_schema_format; |
|
403 | - } |
|
404 | - |
|
405 | - |
|
406 | - /** |
|
407 | - * Sets the schema format property. |
|
408 | - * @throws InvalidArgumentException |
|
409 | - * @param string $format |
|
410 | - */ |
|
411 | - protected function setSchemaFormat($format) |
|
412 | - { |
|
413 | - $this->validateSchemaFormat($format); |
|
414 | - $this->_schema_format = $format; |
|
415 | - } |
|
416 | - |
|
417 | - |
|
418 | - /** |
|
419 | - * This returns the value of the $_schema_readonly property on the object. |
|
420 | - * @return bool |
|
421 | - */ |
|
422 | - public function getSchemaReadonly() |
|
423 | - { |
|
424 | - return $this->_schema_readonly; |
|
425 | - } |
|
426 | - |
|
427 | - |
|
428 | - /** |
|
429 | - * This sets the value for the $_schema_readonly property. |
|
430 | - * @param bool $readonly (only explicit boolean values are accepted) |
|
431 | - */ |
|
432 | - protected function setSchemaReadOnly($readonly) |
|
433 | - { |
|
434 | - if (! is_bool($readonly)) { |
|
435 | - throw new InvalidArgumentException( |
|
436 | - sprintf( |
|
437 | - esc_html__('The incoming argument (%s) must be a boolean.', 'event_espresso'), |
|
438 | - print_r($readonly, true) |
|
439 | - ) |
|
440 | - ); |
|
441 | - } |
|
442 | - |
|
443 | - $this->_schema_readonly = $readonly; |
|
444 | - } |
|
445 | - |
|
446 | - |
|
447 | - |
|
448 | - |
|
449 | - /** |
|
450 | - * Return `%d`, `%s` or `%f` to indicate the data type for the field. |
|
451 | - * @uses _get_wpdb_data_type() |
|
452 | - * |
|
453 | - * @return string |
|
454 | - */ |
|
455 | - public function get_wpdb_data_type() |
|
456 | - { |
|
457 | - return $this->_get_wpdb_data_type(); |
|
458 | - } |
|
459 | - |
|
460 | - |
|
461 | - /** |
|
462 | - * Return `%d`, `%s` or `%f` to indicate the data type for the field that should be indicated in wpdb queries. |
|
463 | - * @param string $type Included if a specific type is requested. |
|
464 | - * @uses get_schema_type() |
|
465 | - * @return string |
|
466 | - */ |
|
467 | - protected function _get_wpdb_data_type($type='') |
|
468 | - { |
|
469 | - $type = empty($type) ? $this->getSchemaType() : $type; |
|
470 | - |
|
471 | - //if type is an array, then different parsing is required. |
|
472 | - if (is_array($type)) { |
|
473 | - return $this->_get_wpdb_data_type_for_type_array($type); |
|
474 | - } |
|
475 | - |
|
476 | - $wpdb_type = '%s'; |
|
477 | - switch ($type) { |
|
478 | - case 'number': |
|
479 | - $wpdb_type = '%f'; |
|
480 | - break; |
|
481 | - case 'integer': |
|
482 | - case 'boolean': |
|
483 | - $wpdb_type = '%d'; |
|
484 | - break; |
|
485 | - case 'object': |
|
486 | - $properties = $this->getSchemaProperties(); |
|
487 | - if (isset($properties['raw'], $properties['raw']['type'])) { |
|
488 | - $wpdb_type = $this->_get_wpdb_data_type($properties['raw']['type']); |
|
489 | - } |
|
490 | - break; //leave at default |
|
491 | - } |
|
492 | - return $wpdb_type; |
|
493 | - } |
|
494 | - |
|
495 | - |
|
496 | - |
|
497 | - protected function _get_wpdb_data_type_for_type_array($type) |
|
498 | - { |
|
499 | - $type = (array) $type; |
|
500 | - //first let's flip because then we can do a faster key check |
|
501 | - $type = array_flip($type); |
|
502 | - |
|
503 | - //check for things that mean '%s' |
|
504 | - if (isset($type['string'],$type['object'],$type['array'])) { |
|
505 | - return '%s'; |
|
506 | - } |
|
507 | - |
|
508 | - //if makes it past the above condition and there's float in the array |
|
509 | - //then the type is %f |
|
510 | - if (isset($type['number'])) { |
|
511 | - return '%f'; |
|
512 | - } |
|
513 | - |
|
514 | - //if it makes it above the above conditions and there is an integer in the array |
|
515 | - //then the type is %d |
|
516 | - if (isset($type['integer'])) { |
|
517 | - return '%d'; |
|
518 | - } |
|
519 | - |
|
520 | - //anything else is a string |
|
521 | - return '%s'; |
|
522 | - } |
|
523 | - |
|
524 | - |
|
525 | - /** |
|
526 | - * This returns elements used to represent this field in the json schema. |
|
527 | - * |
|
528 | - * @link http://json-schema.org/ |
|
529 | - * @return array |
|
530 | - */ |
|
531 | - public function getSchema() |
|
532 | - { |
|
533 | - $schema = array( |
|
534 | - 'description' => $this->getSchemaDescription(), |
|
535 | - 'type' => $this->getSchemaType(), |
|
536 | - 'readonly' => $this->getSchemaReadonly(), |
|
537 | - 'default' => $this->getSchemaDefault() |
|
538 | - ); |
|
539 | - |
|
540 | - //optional properties of the schema |
|
541 | - $enum = $this->getSchemaEnum(); |
|
542 | - $properties = $this->getSchemaProperties(); |
|
543 | - $format = $this->getSchemaFormat(); |
|
544 | - if ($enum) { |
|
545 | - $schema['enum'] = $enum; |
|
546 | - } |
|
547 | - |
|
548 | - if ($properties) { |
|
549 | - $schema['properties'] = $properties; |
|
550 | - } |
|
551 | - |
|
552 | - if ($format) { |
|
553 | - $schema['format'] = $format; |
|
554 | - } |
|
555 | - return $schema; |
|
556 | - } |
|
557 | - |
|
558 | - /** |
|
559 | - * Some fields are in the database-only, (ie, used in queries etc), but shouldn't necessarily be part |
|
560 | - * of the model objects (ie, client code shouldn't care to ever see their value... if client code does |
|
561 | - * want to see their value, then they shouldn't be db-only fields!) |
|
562 | - * Eg, when doing events as custom post types, querying the post_type is essential, but |
|
563 | - * post_type is irrelevant for EE_Event objects (because they will ALL be of post_type 'esp_event'). |
|
564 | - * By default, all fields aren't db-only. |
|
565 | - * |
|
566 | - * @return boolean |
|
567 | - */ |
|
568 | - public function is_db_only_field() |
|
569 | - { |
|
570 | - return false; |
|
571 | - } |
|
572 | - |
|
573 | - |
|
574 | - /** |
|
575 | - * Validates the incoming string|array to ensure its an allowable type. |
|
576 | - * @throws InvalidArgumentException |
|
577 | - * @param string|array $type |
|
578 | - */ |
|
579 | - private function validateSchemaType($type) |
|
580 | - { |
|
581 | - if (! (is_string($type) || is_array($type))) { |
|
582 | - throw new InvalidArgumentException( |
|
583 | - sprintf( |
|
584 | - esc_html__('The incoming argument (%s) must be a string or an array.', 'event_espresso'), |
|
585 | - print_r($type, true) |
|
586 | - ) |
|
587 | - ); |
|
588 | - } |
|
589 | - |
|
590 | - //validate allowable types. |
|
591 | - //@link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
592 | - $allowable_types = array_flip( |
|
593 | - array( |
|
594 | - 'string', |
|
595 | - 'number', |
|
596 | - 'null', |
|
597 | - 'object', |
|
598 | - 'array', |
|
599 | - 'boolean', |
|
600 | - 'integer' |
|
601 | - ) |
|
602 | - ); |
|
603 | - |
|
604 | - if (is_array($type)) { |
|
605 | - foreach ($type as $item_in_type) { |
|
606 | - $this->validateSchemaType($item_in_type); |
|
607 | - } |
|
608 | - return; |
|
609 | - } |
|
610 | - |
|
611 | - if (! isset($allowable_types[$type])) { |
|
612 | - throw new InvalidArgumentException( |
|
613 | - sprintf( |
|
614 | - esc_html__('The incoming argument (%1$s) must be one of the allowable types: %2$s', 'event_espresso'), |
|
615 | - $type, |
|
616 | - implode(',', array_flip($allowable_types)) |
|
617 | - ) |
|
618 | - ); |
|
619 | - } |
|
620 | - } |
|
621 | - |
|
622 | - |
|
623 | - /** |
|
624 | - * Validates that the incoming format is an allowable string to use for the _schema_format property |
|
625 | - * @throws InvalidArgumentException |
|
626 | - * @param $format |
|
627 | - */ |
|
628 | - private function validateSchemaFormat($format) |
|
629 | - { |
|
630 | - if (! is_string($format)) { |
|
631 | - throw new InvalidArgumentException( |
|
632 | - sprintf( |
|
633 | - esc_html__('The incoming argument (%s) must be a string.', 'event_espresso'), |
|
634 | - print_r($format, true) |
|
635 | - ) |
|
636 | - ); |
|
637 | - } |
|
638 | - |
|
639 | - //validate allowable format values |
|
640 | - //@link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
641 | - $allowable_formats = array_flip( |
|
642 | - array( |
|
643 | - 'date-time', |
|
644 | - 'email', |
|
645 | - 'hostname', |
|
646 | - 'ipv4', |
|
647 | - 'ipv6', |
|
648 | - 'uri', |
|
649 | - 'uriref' |
|
650 | - ) |
|
651 | - ); |
|
652 | - |
|
653 | - if (! isset($allowable_formats[$format])) { |
|
654 | - throw new InvalidArgumentException( |
|
655 | - sprintf( |
|
656 | - esc_html__('The incoming argument (%1$s) must be one of the allowable formats: %2$s', 'event_espresso'), |
|
657 | - $format, |
|
658 | - implode(',', array_flip($allowable_formats)) |
|
659 | - ) |
|
660 | - ); |
|
661 | - } |
|
662 | - } |
|
24 | + /** |
|
25 | + * The alias for the table the column belongs to. |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected $_table_alias; |
|
29 | + |
|
30 | + /** |
|
31 | + * The actual db column name for the table |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + protected $_table_column; |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * The authoritative name for the table column (used by client code to reference the field). |
|
39 | + * @var string |
|
40 | + */ |
|
41 | + protected $_name; |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * A description for the field. |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $_nicename; |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * Whether the field is nullable or not |
|
53 | + * @var bool |
|
54 | + */ |
|
55 | + protected $_nullable; |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * What the default value for the field should be. |
|
60 | + * @var mixed |
|
61 | + */ |
|
62 | + protected $_default_value; |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * Other configuration for the field |
|
67 | + * @var mixed |
|
68 | + */ |
|
69 | + protected $_other_config; |
|
70 | + |
|
71 | + |
|
72 | + /** |
|
73 | + * The name of the model this field is instantiated for. |
|
74 | + * @var string |
|
75 | + */ |
|
76 | + protected $_model_name; |
|
77 | + |
|
78 | + |
|
79 | + /** |
|
80 | + * This should be a json-schema valid data type for the field. |
|
81 | + * @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
82 | + * @var string |
|
83 | + */ |
|
84 | + private $_schema_type = 'string'; |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * If the schema has a defined format then it should be defined via this property. |
|
89 | + * @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
90 | + * @var string |
|
91 | + */ |
|
92 | + private $_schema_format = ''; |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * Indicates that the value of the field is managed exclusively by the server/model and not something |
|
97 | + * settable by client code. |
|
98 | + * @link http://json-schema.org/latest/json-schema-hypermedia.html#rfc.section.4.4 |
|
99 | + * @var bool |
|
100 | + */ |
|
101 | + private $_schema_readonly = false; |
|
102 | + |
|
103 | + |
|
104 | + /** |
|
105 | + * @param string $table_column |
|
106 | + * @param string $nicename |
|
107 | + * @param bool $nullable |
|
108 | + * @param null $default_value |
|
109 | + */ |
|
110 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
111 | + { |
|
112 | + $this->_table_column = $table_column; |
|
113 | + $this->_nicename = $nicename; |
|
114 | + $this->_nullable = $nullable; |
|
115 | + $this->_default_value = $default_value; |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * @param $table_alias |
|
121 | + * @param $name |
|
122 | + * @param $model_name |
|
123 | + */ |
|
124 | + public function _construct_finalize($table_alias, $name, $model_name) |
|
125 | + { |
|
126 | + $this->_table_alias = $table_alias; |
|
127 | + $this->_name = $name; |
|
128 | + $this->_model_name = $model_name; |
|
129 | + /** |
|
130 | + * allow for changing the defaults |
|
131 | + */ |
|
132 | + $this->_nicename = apply_filters('FHEE__EE_Model_Field_Base___construct_finalize___nicename', |
|
133 | + $this->_nicename, $this); |
|
134 | + $this->_default_value = apply_filters('FHEE__EE_Model_Field_Base___construct_finalize___default_value', |
|
135 | + $this->_default_value, $this); |
|
136 | + } |
|
137 | + |
|
138 | + public function get_table_alias() |
|
139 | + { |
|
140 | + return $this->_table_alias; |
|
141 | + } |
|
142 | + |
|
143 | + public function get_table_column() |
|
144 | + { |
|
145 | + return $this->_table_column; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Returns the name of the model this field is on. Eg 'Event' or 'Ticket_Datetime' |
|
150 | + * |
|
151 | + * @return string |
|
152 | + */ |
|
153 | + public function get_model_name() |
|
154 | + { |
|
155 | + return $this->_model_name; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * @throws \EE_Error |
|
160 | + * @return string |
|
161 | + */ |
|
162 | + public function get_name() |
|
163 | + { |
|
164 | + if ($this->_name) { |
|
165 | + return $this->_name; |
|
166 | + } else { |
|
167 | + throw new EE_Error(sprintf(__("Model field '%s' has no name set. Did you make a model and forget to call the parent model constructor?", |
|
168 | + "event_espresso"), get_class($this))); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + public function get_nicename() |
|
173 | + { |
|
174 | + return $this->_nicename; |
|
175 | + } |
|
176 | + |
|
177 | + public function is_nullable() |
|
178 | + { |
|
179 | + return $this->_nullable; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * returns whether this field is an auto-increment field or not. If it is, then |
|
184 | + * on insertion it can be null. However, on updates it must be present. |
|
185 | + * |
|
186 | + * @return boolean |
|
187 | + */ |
|
188 | + public function is_auto_increment() |
|
189 | + { |
|
190 | + return false; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * The default value in the model object's value domain. See lengthy comment about |
|
195 | + * value domains at the top of EEM_Base |
|
196 | + * |
|
197 | + * @return mixed |
|
198 | + */ |
|
199 | + public function get_default_value() |
|
200 | + { |
|
201 | + return $this->_default_value; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Returns the table alias joined to the table column, however this isn't the right |
|
206 | + * table alias if the aliased table is being joined to. In that case, you can use |
|
207 | + * EE_Model_Parser::extract_table_alias_model_relation_chain_prefix() to find the table's current alias |
|
208 | + * in the current query |
|
209 | + * |
|
210 | + * @return string |
|
211 | + */ |
|
212 | + public function get_qualified_column() |
|
213 | + { |
|
214 | + return $this->get_table_alias() . "." . $this->get_table_column(); |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * When get() is called on a model object (eg EE_Event), before returning its value, |
|
219 | + * call this function on it, allowing us to customize the returned value based on |
|
220 | + * the field's type. Eg, we may want to unserialize it, strip tags, etc. By default, |
|
221 | + * we simply return it. |
|
222 | + * |
|
223 | + * @param mixed $value_of_field_on_model_object |
|
224 | + * @return mixed |
|
225 | + */ |
|
226 | + public function prepare_for_get($value_of_field_on_model_object) |
|
227 | + { |
|
228 | + return $value_of_field_on_model_object; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * When inserting or updating a field on a model object, run this function on each |
|
233 | + * value to prepare it for insertion into the db. Generally this converts |
|
234 | + * the validated input on the model object into the format used in the DB. |
|
235 | + * |
|
236 | + * @param mixed $value_of_field_on_model_object |
|
237 | + * @return mixed |
|
238 | + */ |
|
239 | + public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
240 | + { |
|
241 | + return $value_of_field_on_model_object; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * When creating a brand-new model object, or setting a particular value for one of its fields, this function |
|
246 | + * is called before setting it on the model object. We may want to strip slashes, unserialize the value, etc. |
|
247 | + * By default, we do nothing. |
|
248 | + * |
|
249 | + * If the model field is going to perform any validation on the input, this is where it should be done |
|
250 | + * (once the value is on the model object, it may be used in other ways besides putting it into the DB |
|
251 | + * so it's best to validate it right away). |
|
252 | + * |
|
253 | + * @param mixed $value_inputted_for_field_on_model_object |
|
254 | + * @return mixed |
|
255 | + */ |
|
256 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
257 | + { |
|
258 | + return $value_inputted_for_field_on_model_object; |
|
259 | + } |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * When instantiating a model object from DB results, this function is called before setting each field. |
|
264 | + * We may want to serialize the value, etc. By default, we return the value using prepare_for_set() method as that |
|
265 | + * is the one child classes will most often define. |
|
266 | + * |
|
267 | + * @param mixed $value_found_in_db_for_model_object |
|
268 | + * @return mixed |
|
269 | + */ |
|
270 | + public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
271 | + { |
|
272 | + return $this->prepare_for_set($value_found_in_db_for_model_object); |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * When echoing a field's value on a model object, this function is run to prepare the value for presentation in a |
|
277 | + * webpage. For example, we may want to output floats with 2 decimal places by default, dates as "Monday Jan 12, |
|
278 | + * 2013, at 3:23pm" instead of |
|
279 | + * "8765678632", or any other modifications to how the value should be displayed, but not modified itself. |
|
280 | + * |
|
281 | + * @param mixed $value_on_field_to_be_outputted |
|
282 | + * @return mixed |
|
283 | + */ |
|
284 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
285 | + { |
|
286 | + return $value_on_field_to_be_outputted; |
|
287 | + } |
|
288 | + |
|
289 | + |
|
290 | + /** |
|
291 | + * Returns whatever is set as the nicename for the object. |
|
292 | + * @return string |
|
293 | + */ |
|
294 | + public function getSchemaDescription() |
|
295 | + { |
|
296 | + return $this->get_nicename(); |
|
297 | + } |
|
298 | + |
|
299 | + |
|
300 | + /** |
|
301 | + * Returns whatever is set as the $_schema_type property for the object. |
|
302 | + * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
303 | + * @return string|array |
|
304 | + */ |
|
305 | + public function getSchemaType() |
|
306 | + { |
|
307 | + if ($this->is_nullable()) { |
|
308 | + $this->_schema_type = (array) $this->_schema_type; |
|
309 | + if (! in_array('null', $this->_schema_type)) { |
|
310 | + $this->_schema_type[] = 'null'; |
|
311 | + }; |
|
312 | + } |
|
313 | + return $this->_schema_type; |
|
314 | + } |
|
315 | + |
|
316 | + |
|
317 | + /** |
|
318 | + * Sets the _schema_type property. Child classes should call this in their constructors to override the default state |
|
319 | + * for this property. |
|
320 | + * @param string|array $type |
|
321 | + * @throws InvalidArgumentException |
|
322 | + */ |
|
323 | + protected function setSchemaType($type) |
|
324 | + { |
|
325 | + $this->validateSchemaType($type); |
|
326 | + $this->_schema_type = $type; |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
332 | + * this method and return the properties for the schema. |
|
333 | + * |
|
334 | + * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
335 | + * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
336 | + * |
|
337 | + * @return array |
|
338 | + */ |
|
339 | + public function getSchemaProperties() |
|
340 | + { |
|
341 | + return array(); |
|
342 | + } |
|
343 | + |
|
344 | + |
|
345 | + |
|
346 | + /** |
|
347 | + * By default this returns the scalar default value that was sent in on the class prepped according to the class type |
|
348 | + * as the default. However, when there are schema properties, then the default property is setup to mirror the |
|
349 | + * property keys and correctly prepare the default according to that expected property value. |
|
350 | + * The getSchema method validates whether the schema for default is setup correctly or not according to the schema type |
|
351 | + * |
|
352 | + * @return mixed |
|
353 | + */ |
|
354 | + public function getSchemaDefault() |
|
355 | + { |
|
356 | + $default_value = $this->prepare_for_use_in_db($this->prepare_for_set($this->get_default_value())); |
|
357 | + $schema_properties = $this->getSchemaProperties(); |
|
358 | + |
|
359 | + //if this schema has properties than shape the default value to match the properties shape. |
|
360 | + if ($schema_properties) { |
|
361 | + $value_to_return = array(); |
|
362 | + foreach ($schema_properties as $property_key => $property_schema) { |
|
363 | + switch ($property_key) { |
|
364 | + case 'pretty': |
|
365 | + case 'rendered': |
|
366 | + $value_to_return[$property_key] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
367 | + break; |
|
368 | + default: |
|
369 | + $value_to_return[$property_key] = $default_value; |
|
370 | + break; |
|
371 | + } |
|
372 | + } |
|
373 | + $default_value = $value_to_return; |
|
374 | + } |
|
375 | + return $default_value; |
|
376 | + } |
|
377 | + |
|
378 | + |
|
379 | + |
|
380 | + |
|
381 | + /** |
|
382 | + * If a child class has enum values, they should override this method and provide a simple array |
|
383 | + * of the enum values. |
|
384 | + * The reason this is not a property on the class is because there may be filterable enum values that |
|
385 | + * are set on the instantiated object that could be filtered after construct. |
|
386 | + * |
|
387 | + * @return array |
|
388 | + */ |
|
389 | + public function getSchemaEnum() |
|
390 | + { |
|
391 | + return array(); |
|
392 | + } |
|
393 | + |
|
394 | + |
|
395 | + /** |
|
396 | + * This returns the value of the $_schema_format property on the object. |
|
397 | + * @return string |
|
398 | + */ |
|
399 | + public function getSchemaFormat() |
|
400 | + { |
|
401 | + return $this->_schema_format; |
|
402 | + } |
|
403 | + |
|
404 | + |
|
405 | + /** |
|
406 | + * Sets the schema format property. |
|
407 | + * @throws InvalidArgumentException |
|
408 | + * @param string $format |
|
409 | + */ |
|
410 | + protected function setSchemaFormat($format) |
|
411 | + { |
|
412 | + $this->validateSchemaFormat($format); |
|
413 | + $this->_schema_format = $format; |
|
414 | + } |
|
415 | + |
|
416 | + |
|
417 | + /** |
|
418 | + * This returns the value of the $_schema_readonly property on the object. |
|
419 | + * @return bool |
|
420 | + */ |
|
421 | + public function getSchemaReadonly() |
|
422 | + { |
|
423 | + return $this->_schema_readonly; |
|
424 | + } |
|
425 | + |
|
426 | + |
|
427 | + /** |
|
428 | + * This sets the value for the $_schema_readonly property. |
|
429 | + * @param bool $readonly (only explicit boolean values are accepted) |
|
430 | + */ |
|
431 | + protected function setSchemaReadOnly($readonly) |
|
432 | + { |
|
433 | + if (! is_bool($readonly)) { |
|
434 | + throw new InvalidArgumentException( |
|
435 | + sprintf( |
|
436 | + esc_html__('The incoming argument (%s) must be a boolean.', 'event_espresso'), |
|
437 | + print_r($readonly, true) |
|
438 | + ) |
|
439 | + ); |
|
440 | + } |
|
441 | + |
|
442 | + $this->_schema_readonly = $readonly; |
|
443 | + } |
|
444 | + |
|
445 | + |
|
446 | + |
|
447 | + |
|
448 | + /** |
|
449 | + * Return `%d`, `%s` or `%f` to indicate the data type for the field. |
|
450 | + * @uses _get_wpdb_data_type() |
|
451 | + * |
|
452 | + * @return string |
|
453 | + */ |
|
454 | + public function get_wpdb_data_type() |
|
455 | + { |
|
456 | + return $this->_get_wpdb_data_type(); |
|
457 | + } |
|
458 | + |
|
459 | + |
|
460 | + /** |
|
461 | + * Return `%d`, `%s` or `%f` to indicate the data type for the field that should be indicated in wpdb queries. |
|
462 | + * @param string $type Included if a specific type is requested. |
|
463 | + * @uses get_schema_type() |
|
464 | + * @return string |
|
465 | + */ |
|
466 | + protected function _get_wpdb_data_type($type='') |
|
467 | + { |
|
468 | + $type = empty($type) ? $this->getSchemaType() : $type; |
|
469 | + |
|
470 | + //if type is an array, then different parsing is required. |
|
471 | + if (is_array($type)) { |
|
472 | + return $this->_get_wpdb_data_type_for_type_array($type); |
|
473 | + } |
|
474 | + |
|
475 | + $wpdb_type = '%s'; |
|
476 | + switch ($type) { |
|
477 | + case 'number': |
|
478 | + $wpdb_type = '%f'; |
|
479 | + break; |
|
480 | + case 'integer': |
|
481 | + case 'boolean': |
|
482 | + $wpdb_type = '%d'; |
|
483 | + break; |
|
484 | + case 'object': |
|
485 | + $properties = $this->getSchemaProperties(); |
|
486 | + if (isset($properties['raw'], $properties['raw']['type'])) { |
|
487 | + $wpdb_type = $this->_get_wpdb_data_type($properties['raw']['type']); |
|
488 | + } |
|
489 | + break; //leave at default |
|
490 | + } |
|
491 | + return $wpdb_type; |
|
492 | + } |
|
493 | + |
|
494 | + |
|
495 | + |
|
496 | + protected function _get_wpdb_data_type_for_type_array($type) |
|
497 | + { |
|
498 | + $type = (array) $type; |
|
499 | + //first let's flip because then we can do a faster key check |
|
500 | + $type = array_flip($type); |
|
501 | + |
|
502 | + //check for things that mean '%s' |
|
503 | + if (isset($type['string'],$type['object'],$type['array'])) { |
|
504 | + return '%s'; |
|
505 | + } |
|
506 | + |
|
507 | + //if makes it past the above condition and there's float in the array |
|
508 | + //then the type is %f |
|
509 | + if (isset($type['number'])) { |
|
510 | + return '%f'; |
|
511 | + } |
|
512 | + |
|
513 | + //if it makes it above the above conditions and there is an integer in the array |
|
514 | + //then the type is %d |
|
515 | + if (isset($type['integer'])) { |
|
516 | + return '%d'; |
|
517 | + } |
|
518 | + |
|
519 | + //anything else is a string |
|
520 | + return '%s'; |
|
521 | + } |
|
522 | + |
|
523 | + |
|
524 | + /** |
|
525 | + * This returns elements used to represent this field in the json schema. |
|
526 | + * |
|
527 | + * @link http://json-schema.org/ |
|
528 | + * @return array |
|
529 | + */ |
|
530 | + public function getSchema() |
|
531 | + { |
|
532 | + $schema = array( |
|
533 | + 'description' => $this->getSchemaDescription(), |
|
534 | + 'type' => $this->getSchemaType(), |
|
535 | + 'readonly' => $this->getSchemaReadonly(), |
|
536 | + 'default' => $this->getSchemaDefault() |
|
537 | + ); |
|
538 | + |
|
539 | + //optional properties of the schema |
|
540 | + $enum = $this->getSchemaEnum(); |
|
541 | + $properties = $this->getSchemaProperties(); |
|
542 | + $format = $this->getSchemaFormat(); |
|
543 | + if ($enum) { |
|
544 | + $schema['enum'] = $enum; |
|
545 | + } |
|
546 | + |
|
547 | + if ($properties) { |
|
548 | + $schema['properties'] = $properties; |
|
549 | + } |
|
550 | + |
|
551 | + if ($format) { |
|
552 | + $schema['format'] = $format; |
|
553 | + } |
|
554 | + return $schema; |
|
555 | + } |
|
556 | + |
|
557 | + /** |
|
558 | + * Some fields are in the database-only, (ie, used in queries etc), but shouldn't necessarily be part |
|
559 | + * of the model objects (ie, client code shouldn't care to ever see their value... if client code does |
|
560 | + * want to see their value, then they shouldn't be db-only fields!) |
|
561 | + * Eg, when doing events as custom post types, querying the post_type is essential, but |
|
562 | + * post_type is irrelevant for EE_Event objects (because they will ALL be of post_type 'esp_event'). |
|
563 | + * By default, all fields aren't db-only. |
|
564 | + * |
|
565 | + * @return boolean |
|
566 | + */ |
|
567 | + public function is_db_only_field() |
|
568 | + { |
|
569 | + return false; |
|
570 | + } |
|
571 | + |
|
572 | + |
|
573 | + /** |
|
574 | + * Validates the incoming string|array to ensure its an allowable type. |
|
575 | + * @throws InvalidArgumentException |
|
576 | + * @param string|array $type |
|
577 | + */ |
|
578 | + private function validateSchemaType($type) |
|
579 | + { |
|
580 | + if (! (is_string($type) || is_array($type))) { |
|
581 | + throw new InvalidArgumentException( |
|
582 | + sprintf( |
|
583 | + esc_html__('The incoming argument (%s) must be a string or an array.', 'event_espresso'), |
|
584 | + print_r($type, true) |
|
585 | + ) |
|
586 | + ); |
|
587 | + } |
|
588 | + |
|
589 | + //validate allowable types. |
|
590 | + //@link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
591 | + $allowable_types = array_flip( |
|
592 | + array( |
|
593 | + 'string', |
|
594 | + 'number', |
|
595 | + 'null', |
|
596 | + 'object', |
|
597 | + 'array', |
|
598 | + 'boolean', |
|
599 | + 'integer' |
|
600 | + ) |
|
601 | + ); |
|
602 | + |
|
603 | + if (is_array($type)) { |
|
604 | + foreach ($type as $item_in_type) { |
|
605 | + $this->validateSchemaType($item_in_type); |
|
606 | + } |
|
607 | + return; |
|
608 | + } |
|
609 | + |
|
610 | + if (! isset($allowable_types[$type])) { |
|
611 | + throw new InvalidArgumentException( |
|
612 | + sprintf( |
|
613 | + esc_html__('The incoming argument (%1$s) must be one of the allowable types: %2$s', 'event_espresso'), |
|
614 | + $type, |
|
615 | + implode(',', array_flip($allowable_types)) |
|
616 | + ) |
|
617 | + ); |
|
618 | + } |
|
619 | + } |
|
620 | + |
|
621 | + |
|
622 | + /** |
|
623 | + * Validates that the incoming format is an allowable string to use for the _schema_format property |
|
624 | + * @throws InvalidArgumentException |
|
625 | + * @param $format |
|
626 | + */ |
|
627 | + private function validateSchemaFormat($format) |
|
628 | + { |
|
629 | + if (! is_string($format)) { |
|
630 | + throw new InvalidArgumentException( |
|
631 | + sprintf( |
|
632 | + esc_html__('The incoming argument (%s) must be a string.', 'event_espresso'), |
|
633 | + print_r($format, true) |
|
634 | + ) |
|
635 | + ); |
|
636 | + } |
|
637 | + |
|
638 | + //validate allowable format values |
|
639 | + //@link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
640 | + $allowable_formats = array_flip( |
|
641 | + array( |
|
642 | + 'date-time', |
|
643 | + 'email', |
|
644 | + 'hostname', |
|
645 | + 'ipv4', |
|
646 | + 'ipv6', |
|
647 | + 'uri', |
|
648 | + 'uriref' |
|
649 | + ) |
|
650 | + ); |
|
651 | + |
|
652 | + if (! isset($allowable_formats[$format])) { |
|
653 | + throw new InvalidArgumentException( |
|
654 | + sprintf( |
|
655 | + esc_html__('The incoming argument (%1$s) must be one of the allowable formats: %2$s', 'event_espresso'), |
|
656 | + $format, |
|
657 | + implode(',', array_flip($allowable_formats)) |
|
658 | + ) |
|
659 | + ); |
|
660 | + } |
|
661 | + } |
|
663 | 662 | } |
664 | 663 | \ No newline at end of file |
@@ -4,37 +4,37 @@ |
||
4 | 4 | class EE_Foreign_Key_Int_Field extends EE_Foreign_Key_Field_Base |
5 | 5 | { |
6 | 6 | |
7 | - /** |
|
8 | - * @param string $table_column name fo column for field |
|
9 | - * @param string $nicename should eb internationalized with __('blah','event_espresso') |
|
10 | - * @param boolean $nullable |
|
11 | - * @param mixed $default_value if this is a integer field, it shoudl be an int. if it's a string field, it shoul |
|
12 | - * dbe a string |
|
13 | - * @param string $model_name eg 'Event','Answer','Term', etc. Basically its the model class's name without the |
|
14 | - * "EEM_" |
|
15 | - */ |
|
16 | - public function __construct($table_column, $nicename, $nullable, $default_value, $model_name) |
|
17 | - { |
|
18 | - parent::__construct($table_column, $nicename, $nullable, $default_value, $model_name); |
|
19 | - $this->setSchemaType('integer'); |
|
20 | - } |
|
7 | + /** |
|
8 | + * @param string $table_column name fo column for field |
|
9 | + * @param string $nicename should eb internationalized with __('blah','event_espresso') |
|
10 | + * @param boolean $nullable |
|
11 | + * @param mixed $default_value if this is a integer field, it shoudl be an int. if it's a string field, it shoul |
|
12 | + * dbe a string |
|
13 | + * @param string $model_name eg 'Event','Answer','Term', etc. Basically its the model class's name without the |
|
14 | + * "EEM_" |
|
15 | + */ |
|
16 | + public function __construct($table_column, $nicename, $nullable, $default_value, $model_name) |
|
17 | + { |
|
18 | + parent::__construct($table_column, $nicename, $nullable, $default_value, $model_name); |
|
19 | + $this->setSchemaType('integer'); |
|
20 | + } |
|
21 | 21 | |
22 | 22 | |
23 | - /** |
|
24 | - * @param int|EE_Base_Class $value_inputted_for_field_on_model_object |
|
25 | - * @return int |
|
26 | - */ |
|
27 | - function prepare_for_set($value_inputted_for_field_on_model_object) |
|
28 | - { |
|
29 | - if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
30 | - $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
31 | - } |
|
32 | - return absint($value_inputted_for_field_on_model_object); |
|
33 | - } |
|
23 | + /** |
|
24 | + * @param int|EE_Base_Class $value_inputted_for_field_on_model_object |
|
25 | + * @return int |
|
26 | + */ |
|
27 | + function prepare_for_set($value_inputted_for_field_on_model_object) |
|
28 | + { |
|
29 | + if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
30 | + $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
31 | + } |
|
32 | + return absint($value_inputted_for_field_on_model_object); |
|
33 | + } |
|
34 | 34 | |
35 | - function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
36 | - { |
|
37 | - return intval($value_found_in_db_for_model_object); |
|
38 | - } |
|
35 | + function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
36 | + { |
|
37 | + return intval($value_found_in_db_for_model_object); |
|
38 | + } |
|
39 | 39 | |
40 | 40 | } |
41 | 41 | \ No newline at end of file |
@@ -15,57 +15,57 @@ |
||
15 | 15 | class EE_Full_HTML_Field extends EE_Text_Field_Base |
16 | 16 | { |
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('object'); |
|
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('object'); |
|
28 | + } |
|
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * Does shortcodes and auto-paragraphs the content (unless schema is 'no_wpautop') |
|
33 | - * |
|
34 | - * @param type $value_on_field_to_be_outputted |
|
35 | - * @param type $schema |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $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 type $value_on_field_to_be_outputted |
|
35 | + * @param type $schema |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $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 | 50 | |
51 | - public function getSchemaProperties() |
|
52 | - { |
|
53 | - return array( |
|
54 | - 'raw' => array( |
|
55 | - 'description' => sprintf( |
|
56 | - __('%s - the value in the database.', 'event_espresso'), |
|
57 | - $this->get_nicename() |
|
58 | - ), |
|
59 | - 'type' => 'string' |
|
60 | - ), |
|
61 | - 'rendered' => array( |
|
62 | - 'description' => sprintf( |
|
63 | - __('%s - transformed for display.', 'event_espresso'), |
|
64 | - $this->get_nicename() |
|
65 | - ), |
|
66 | - 'type' => 'string', |
|
67 | - 'readonly' => true |
|
68 | - ) |
|
69 | - ); |
|
70 | - } |
|
51 | + public function getSchemaProperties() |
|
52 | + { |
|
53 | + return array( |
|
54 | + 'raw' => array( |
|
55 | + 'description' => sprintf( |
|
56 | + __('%s - the value in the database.', 'event_espresso'), |
|
57 | + $this->get_nicename() |
|
58 | + ), |
|
59 | + 'type' => 'string' |
|
60 | + ), |
|
61 | + 'rendered' => array( |
|
62 | + 'description' => sprintf( |
|
63 | + __('%s - transformed for display.', 'event_espresso'), |
|
64 | + $this->get_nicename() |
|
65 | + ), |
|
66 | + 'type' => 'string', |
|
67 | + 'readonly' => true |
|
68 | + ) |
|
69 | + ); |
|
70 | + } |
|
71 | 71 | } |
72 | 72 | \ No newline at end of file |
@@ -18,31 +18,31 @@ |
||
18 | 18 | |
19 | 19 | |
20 | 20 | |
21 | - /** |
|
22 | - * @param string $name |
|
23 | - * @param array $widget_options |
|
24 | - * @param array $control_options |
|
25 | - */ |
|
26 | - public function __construct($name = '', array $widget_options = array(), array $control_options = array()) |
|
27 | - { |
|
28 | - $id_base = EspressoWidget::getIdBase(get_class($this)); |
|
29 | - $control_options['id_base'] = $id_base; |
|
30 | - $control_options['height'] = isset($control_options['height']) ? $control_options['height'] : 300; |
|
31 | - $control_options['width'] = isset($control_options['width']) ? $control_options['width'] : 350; |
|
32 | - // Register widget with WordPress |
|
33 | - parent::__construct($id_base, $name, $widget_options, $control_options); |
|
34 | - } |
|
35 | - |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * @param string $widget_class |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - public static function getIdBase($widget_class) |
|
43 | - { |
|
44 | - return sanitize_title(str_replace(array('EEW_', '_'), array('EE_', '-'), $widget_class)) . '-widget'; |
|
45 | - } |
|
21 | + /** |
|
22 | + * @param string $name |
|
23 | + * @param array $widget_options |
|
24 | + * @param array $control_options |
|
25 | + */ |
|
26 | + public function __construct($name = '', array $widget_options = array(), array $control_options = array()) |
|
27 | + { |
|
28 | + $id_base = EspressoWidget::getIdBase(get_class($this)); |
|
29 | + $control_options['id_base'] = $id_base; |
|
30 | + $control_options['height'] = isset($control_options['height']) ? $control_options['height'] : 300; |
|
31 | + $control_options['width'] = isset($control_options['width']) ? $control_options['width'] : 350; |
|
32 | + // Register widget with WordPress |
|
33 | + parent::__construct($id_base, $name, $widget_options, $control_options); |
|
34 | + } |
|
35 | + |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * @param string $widget_class |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + public static function getIdBase($widget_class) |
|
43 | + { |
|
44 | + return sanitize_title(str_replace(array('EEW_', '_'), array('EE_', '-'), $widget_class)) . '-widget'; |
|
45 | + } |
|
46 | 46 | |
47 | 47 | |
48 | 48 | } |
@@ -41,7 +41,7 @@ |
||
41 | 41 | */ |
42 | 42 | public static function getIdBase($widget_class) |
43 | 43 | { |
44 | - return sanitize_title(str_replace(array('EEW_', '_'), array('EE_', '-'), $widget_class)) . '-widget'; |
|
44 | + return sanitize_title(str_replace(array('EEW_', '_'), array('EE_', '-'), $widget_class)).'-widget'; |
|
45 | 45 | } |
46 | 46 | |
47 | 47 |
@@ -2,8 +2,7 @@ |
||
2 | 2 | |
3 | 3 | /** |
4 | 4 | * Goes through all the posts and pages, and converts old shortcodes to new ones |
5 | - |
|
6 | -*/ |
|
5 | + */ |
|
7 | 6 | |
8 | 7 | class EE_DMS_4_1_0_shortcodes extends EE_Data_Migration_Script_Stage{ |
9 | 8 | function __construct() { |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | |
6 | 6 | */ |
7 | 7 | |
8 | -class EE_DMS_4_1_0_shortcodes extends EE_Data_Migration_Script_Stage{ |
|
8 | +class EE_DMS_4_1_0_shortcodes extends EE_Data_Migration_Script_Stage { |
|
9 | 9 | function __construct() { |
10 | 10 | global $wpdb; |
11 | 11 | $this->_pretty_name = __("Shortcodes", "event_espresso"); |
@@ -15,27 +15,27 @@ discard block |
||
15 | 15 | protected function _migrate_old_row($old_row) { |
16 | 16 | $new_post_content = $this->_change_event_list_shortcode($old_row['post_content']); |
17 | 17 | global $wpdb; |
18 | - $wpdb->query($wpdb->prepare("UPDATE ".$this->_old_table." SET post_content=%s WHERE ID=%d",$new_post_content,$old_row['ID'])); |
|
18 | + $wpdb->query($wpdb->prepare("UPDATE ".$this->_old_table." SET post_content=%s WHERE ID=%d", $new_post_content, $old_row['ID'])); |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | /** |
22 | 22 | * replaces [EVENT_LIST... with [ESPRESSO_EVENTS...] |
23 | 23 | * @param string $old_content |
24 | 24 | */ |
25 | - private function _change_event_list_shortcode($old_content){ |
|
26 | - return str_replace("[EVENT_LIST","[ESPRESSO_EVENTS",$old_content); |
|
25 | + private function _change_event_list_shortcode($old_content) { |
|
26 | + return str_replace("[EVENT_LIST", "[ESPRESSO_EVENTS", $old_content); |
|
27 | 27 | } |
28 | 28 | |
29 | - function _migration_step($num_items=50){ |
|
29 | + function _migration_step($num_items = 50) { |
|
30 | 30 | global $wpdb; |
31 | 31 | $start_at_record = $this->count_records_migrated(); |
32 | - $rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $this->_old_table {$this->_sql_to_only_select_non_drafts()} LIMIT %d,%d",$start_at_record,$num_items),ARRAY_A); |
|
32 | + $rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $this->_old_table {$this->_sql_to_only_select_non_drafts()} LIMIT %d,%d", $start_at_record, $num_items), ARRAY_A); |
|
33 | 33 | $items_actually_migrated = 0; |
34 | - foreach($rows as $old_row){ |
|
34 | + foreach ($rows as $old_row) { |
|
35 | 35 | $this->_migrate_old_row($old_row); |
36 | 36 | $items_actually_migrated++; |
37 | 37 | } |
38 | - if($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()){ |
|
38 | + if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) { |
|
39 | 39 | $this->set_completed(); |
40 | 40 | } |
41 | 41 | return $items_actually_migrated; |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | return $count; |
47 | 47 | } |
48 | 48 | |
49 | - private function _sql_to_only_select_non_drafts(){ |
|
49 | + private function _sql_to_only_select_non_drafts() { |
|
50 | 50 | return " WHERE post_type NOT IN ('revision','auto-draft') "; |
51 | 51 | } |
52 | 52 |