@@ -9,153 +9,153 @@ |
||
9 | 9 | */ |
10 | 10 | class EE_Model_Parser |
11 | 11 | { |
12 | - const table_alias_model_relation_chain_separator = '__'; |
|
13 | - const table_alias_model_relation_chain_prefix_end = '___'; |
|
14 | - /** |
|
15 | - * Adds a period onto the front and end of the string. This often helps in searching. |
|
16 | - * For example, if we want to find the model name "Event", it can be tricky when the following are possible |
|
17 | - * "","Event.EVT_ID","Event","Event_Venue.Venue.VNU_ID",etc. It's easier to look for ".Event." in |
|
18 | - * "..",".Event.EVT_ID.", ".Event.", and ".Event_Venue.Venue.VNU_ID", especially when the last example should NOT |
|
19 | - * be found because the "Event" model isn't mentioned- it's just a string that has a model name that coincidentally |
|
20 | - * has it as a substring |
|
21 | - * @param string $string_to_pad |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - public static function pad_with_periods($string_to_pad) |
|
25 | - { |
|
26 | - return "." . $string_to_pad . "."; |
|
27 | - } |
|
28 | - /** |
|
29 | - * Basically undoes _pad_with_periods |
|
30 | - * @param string $string_to_trim |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public static function trim_periods($string_to_trim) |
|
34 | - { |
|
35 | - return trim($string_to_trim, '.'); |
|
36 | - } |
|
12 | + const table_alias_model_relation_chain_separator = '__'; |
|
13 | + const table_alias_model_relation_chain_prefix_end = '___'; |
|
14 | + /** |
|
15 | + * Adds a period onto the front and end of the string. This often helps in searching. |
|
16 | + * For example, if we want to find the model name "Event", it can be tricky when the following are possible |
|
17 | + * "","Event.EVT_ID","Event","Event_Venue.Venue.VNU_ID",etc. It's easier to look for ".Event." in |
|
18 | + * "..",".Event.EVT_ID.", ".Event.", and ".Event_Venue.Venue.VNU_ID", especially when the last example should NOT |
|
19 | + * be found because the "Event" model isn't mentioned- it's just a string that has a model name that coincidentally |
|
20 | + * has it as a substring |
|
21 | + * @param string $string_to_pad |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + public static function pad_with_periods($string_to_pad) |
|
25 | + { |
|
26 | + return "." . $string_to_pad . "."; |
|
27 | + } |
|
28 | + /** |
|
29 | + * Basically undoes _pad_with_periods |
|
30 | + * @param string $string_to_trim |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public static function trim_periods($string_to_trim) |
|
34 | + { |
|
35 | + return trim($string_to_trim, '.'); |
|
36 | + } |
|
37 | 37 | |
38 | 38 | |
39 | 39 | |
40 | - /** |
|
41 | - * Gets the calculated table's alias |
|
42 | - * @param string $model_relation_chain or query param |
|
43 | - * @param $this_model_name |
|
44 | - * @return string which can be added onto table aliases to make them unique |
|
45 | - */ |
|
46 | - public static function extract_table_alias_model_relation_chain_prefix($model_relation_chain, $this_model_name) |
|
47 | - { |
|
48 | - // eg $model_relation_chain = 'Venue.Event_Venue.Event.Registration", and $this_model_name = 'Event' |
|
49 | - $model_relation_chain = self::pad_with_periods($model_relation_chain); |
|
50 | - $this_model_name = self::pad_with_periods($this_model_name); |
|
51 | - // eg '.Venue.Event_Venue.Event.Registration." and '.Event.' |
|
52 | - // remove this model name and everything afterwards |
|
53 | - $pos_of_model_name = strpos($model_relation_chain, $this_model_name); |
|
54 | - $model_relation_chain = substr($model_relation_chain, 0, $pos_of_model_name); |
|
55 | - // eg '.Venue.Event_Venue.' |
|
56 | - // trim periods |
|
57 | - $model_relation_chain = self::trim_periods($model_relation_chain); |
|
58 | - // eg 'Venue.Event_Venue' |
|
59 | - // replace periods with double-underscores |
|
60 | - $model_relation_chain = str_replace(".", self::table_alias_model_relation_chain_separator, $model_relation_chain); |
|
61 | - // eg 'Venue__Event_Venue' |
|
62 | - if ($model_relation_chain != '') { |
|
63 | - $model_relation_chain = $model_relation_chain . self::table_alias_model_relation_chain_prefix_end; |
|
64 | - } |
|
65 | - // eg 'Venue_Event_Venue___' |
|
66 | - return $model_relation_chain; |
|
67 | - } |
|
68 | - /** |
|
69 | - * Gets the table's alias (without prefix or anything) |
|
70 | - * @param string $table_alias_with_model_relation_chain_prefix which CAN have a table alias model relation chain prefix (or not) |
|
71 | - * @return string |
|
72 | - */ |
|
73 | - public static function remove_table_alias_model_relation_chain_prefix($table_alias_with_model_relation_chain_prefix) |
|
74 | - { |
|
75 | - // does this actually have a table alias model relation chain prefix? |
|
76 | - $pos = strpos($table_alias_with_model_relation_chain_prefix, self::table_alias_model_relation_chain_prefix_end); |
|
77 | - if ($pos !== false) { |
|
78 | - // yes |
|
79 | - // find that triple underscore and remove it and everything before it |
|
80 | - $table_alias = substr($table_alias_with_model_relation_chain_prefix, $pos + strlen(self::table_alias_model_relation_chain_prefix_end)); |
|
81 | - } else { |
|
82 | - $table_alias = $table_alias_with_model_relation_chain_prefix; |
|
83 | - } |
|
84 | - return $table_alias; |
|
85 | - } |
|
86 | - /** |
|
87 | - * Gets the table alias model relation chain prefix from the table alias already containing it |
|
88 | - * @param string $table_alias_with_model_relation_chain_prefix |
|
89 | - * @return string |
|
90 | - */ |
|
91 | - public static function get_prefix_from_table_alias_with_model_relation_chain_prefix($table_alias_with_model_relation_chain_prefix) |
|
92 | - { |
|
93 | - // does this actually have a table alias model relation chain prefix? |
|
94 | - $pos = strpos($table_alias_with_model_relation_chain_prefix, self::table_alias_model_relation_chain_prefix_end); |
|
95 | - if ($pos !== false) { |
|
96 | - // yes |
|
97 | - // find that triple underscore and remove it and everything before it |
|
98 | - $prefix = substr($table_alias_with_model_relation_chain_prefix, 0, $pos + strlen(self::table_alias_model_relation_chain_prefix_end)); |
|
99 | - } else { |
|
100 | - $prefix = ''; |
|
101 | - } |
|
102 | - return $prefix; |
|
103 | - } |
|
40 | + /** |
|
41 | + * Gets the calculated table's alias |
|
42 | + * @param string $model_relation_chain or query param |
|
43 | + * @param $this_model_name |
|
44 | + * @return string which can be added onto table aliases to make them unique |
|
45 | + */ |
|
46 | + public static function extract_table_alias_model_relation_chain_prefix($model_relation_chain, $this_model_name) |
|
47 | + { |
|
48 | + // eg $model_relation_chain = 'Venue.Event_Venue.Event.Registration", and $this_model_name = 'Event' |
|
49 | + $model_relation_chain = self::pad_with_periods($model_relation_chain); |
|
50 | + $this_model_name = self::pad_with_periods($this_model_name); |
|
51 | + // eg '.Venue.Event_Venue.Event.Registration." and '.Event.' |
|
52 | + // remove this model name and everything afterwards |
|
53 | + $pos_of_model_name = strpos($model_relation_chain, $this_model_name); |
|
54 | + $model_relation_chain = substr($model_relation_chain, 0, $pos_of_model_name); |
|
55 | + // eg '.Venue.Event_Venue.' |
|
56 | + // trim periods |
|
57 | + $model_relation_chain = self::trim_periods($model_relation_chain); |
|
58 | + // eg 'Venue.Event_Venue' |
|
59 | + // replace periods with double-underscores |
|
60 | + $model_relation_chain = str_replace(".", self::table_alias_model_relation_chain_separator, $model_relation_chain); |
|
61 | + // eg 'Venue__Event_Venue' |
|
62 | + if ($model_relation_chain != '') { |
|
63 | + $model_relation_chain = $model_relation_chain . self::table_alias_model_relation_chain_prefix_end; |
|
64 | + } |
|
65 | + // eg 'Venue_Event_Venue___' |
|
66 | + return $model_relation_chain; |
|
67 | + } |
|
68 | + /** |
|
69 | + * Gets the table's alias (without prefix or anything) |
|
70 | + * @param string $table_alias_with_model_relation_chain_prefix which CAN have a table alias model relation chain prefix (or not) |
|
71 | + * @return string |
|
72 | + */ |
|
73 | + public static function remove_table_alias_model_relation_chain_prefix($table_alias_with_model_relation_chain_prefix) |
|
74 | + { |
|
75 | + // does this actually have a table alias model relation chain prefix? |
|
76 | + $pos = strpos($table_alias_with_model_relation_chain_prefix, self::table_alias_model_relation_chain_prefix_end); |
|
77 | + if ($pos !== false) { |
|
78 | + // yes |
|
79 | + // find that triple underscore and remove it and everything before it |
|
80 | + $table_alias = substr($table_alias_with_model_relation_chain_prefix, $pos + strlen(self::table_alias_model_relation_chain_prefix_end)); |
|
81 | + } else { |
|
82 | + $table_alias = $table_alias_with_model_relation_chain_prefix; |
|
83 | + } |
|
84 | + return $table_alias; |
|
85 | + } |
|
86 | + /** |
|
87 | + * Gets the table alias model relation chain prefix from the table alias already containing it |
|
88 | + * @param string $table_alias_with_model_relation_chain_prefix |
|
89 | + * @return string |
|
90 | + */ |
|
91 | + public static function get_prefix_from_table_alias_with_model_relation_chain_prefix($table_alias_with_model_relation_chain_prefix) |
|
92 | + { |
|
93 | + // does this actually have a table alias model relation chain prefix? |
|
94 | + $pos = strpos($table_alias_with_model_relation_chain_prefix, self::table_alias_model_relation_chain_prefix_end); |
|
95 | + if ($pos !== false) { |
|
96 | + // yes |
|
97 | + // find that triple underscore and remove it and everything before it |
|
98 | + $prefix = substr($table_alias_with_model_relation_chain_prefix, 0, $pos + strlen(self::table_alias_model_relation_chain_prefix_end)); |
|
99 | + } else { |
|
100 | + $prefix = ''; |
|
101 | + } |
|
102 | + return $prefix; |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * Gets the table alias model relation chain prefix (ie, what can be prepended onto |
|
107 | - * EE_Model_Field::get_qualified_column() to get the proper column name for that field |
|
108 | - * in a specific query) from teh query param (eg 'Registration.Event.EVT_ID'). |
|
109 | - * |
|
110 | - * @param string $model_name of the model on which the related query param was found to be belong |
|
111 | - * @param string $original_query_param |
|
112 | - * @return string |
|
113 | - */ |
|
114 | - public static function extract_table_alias_model_relation_chain_from_query_param($model_name, $original_query_param) |
|
115 | - { |
|
116 | - $relation_chain = self::extract_model_relation_chain($model_name, $original_query_param); |
|
117 | - $table_alias_with_model_relation_chain_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($relation_chain, $model_name); |
|
118 | - return $table_alias_with_model_relation_chain_prefix; |
|
119 | - } |
|
120 | - /** |
|
121 | - * Gets the model relation chain to $model_name from the $original_query_param. |
|
122 | - * Eg, if $model_name were 'Payment', and $original_query_param were 'Registration.Transaction.Payment.PAY_ID', |
|
123 | - * this would return 'Registration.Transaction.Payment'. Also if the query param were 'Registration.Transaction.Payment' |
|
124 | - * and $model_name were 'Payment', it should return 'Registration.Transaction.Payment' |
|
125 | - * @param string $model_name |
|
126 | - * @param string $original_query_param |
|
127 | - * @return string |
|
128 | - */ |
|
129 | - public static function extract_model_relation_chain($model_name, $original_query_param) |
|
130 | - { |
|
131 | - // prefix and postfix both with a period, as this facilitates searching |
|
132 | - $model_name = EE_Model_Parser::pad_with_periods($model_name); |
|
133 | - $original_query_param = EE_Model_Parser::pad_with_periods($original_query_param); |
|
134 | - $pos_of_model_string = strpos($original_query_param, $model_name); |
|
135 | - // eg, if we're looking for the model relation chain from Event to Payment, the original query param is probably something like |
|
136 | - // "Registration.Transaction.Payment.PAY_ID", $pos_of_model_string points to the 'P' or Payment. We want the string |
|
137 | - // "Registration.Transaction.Payment" |
|
138 | - $model_relation_chain = substr($original_query_param, 0, $pos_of_model_string + strlen($model_name)); |
|
139 | - return EE_Model_Parser::trim_periods($model_relation_chain); |
|
140 | - } |
|
105 | + /** |
|
106 | + * Gets the table alias model relation chain prefix (ie, what can be prepended onto |
|
107 | + * EE_Model_Field::get_qualified_column() to get the proper column name for that field |
|
108 | + * in a specific query) from teh query param (eg 'Registration.Event.EVT_ID'). |
|
109 | + * |
|
110 | + * @param string $model_name of the model on which the related query param was found to be belong |
|
111 | + * @param string $original_query_param |
|
112 | + * @return string |
|
113 | + */ |
|
114 | + public static function extract_table_alias_model_relation_chain_from_query_param($model_name, $original_query_param) |
|
115 | + { |
|
116 | + $relation_chain = self::extract_model_relation_chain($model_name, $original_query_param); |
|
117 | + $table_alias_with_model_relation_chain_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($relation_chain, $model_name); |
|
118 | + return $table_alias_with_model_relation_chain_prefix; |
|
119 | + } |
|
120 | + /** |
|
121 | + * Gets the model relation chain to $model_name from the $original_query_param. |
|
122 | + * Eg, if $model_name were 'Payment', and $original_query_param were 'Registration.Transaction.Payment.PAY_ID', |
|
123 | + * this would return 'Registration.Transaction.Payment'. Also if the query param were 'Registration.Transaction.Payment' |
|
124 | + * and $model_name were 'Payment', it should return 'Registration.Transaction.Payment' |
|
125 | + * @param string $model_name |
|
126 | + * @param string $original_query_param |
|
127 | + * @return string |
|
128 | + */ |
|
129 | + public static function extract_model_relation_chain($model_name, $original_query_param) |
|
130 | + { |
|
131 | + // prefix and postfix both with a period, as this facilitates searching |
|
132 | + $model_name = EE_Model_Parser::pad_with_periods($model_name); |
|
133 | + $original_query_param = EE_Model_Parser::pad_with_periods($original_query_param); |
|
134 | + $pos_of_model_string = strpos($original_query_param, $model_name); |
|
135 | + // eg, if we're looking for the model relation chain from Event to Payment, the original query param is probably something like |
|
136 | + // "Registration.Transaction.Payment.PAY_ID", $pos_of_model_string points to the 'P' or Payment. We want the string |
|
137 | + // "Registration.Transaction.Payment" |
|
138 | + $model_relation_chain = substr($original_query_param, 0, $pos_of_model_string + strlen($model_name)); |
|
139 | + return EE_Model_Parser::trim_periods($model_relation_chain); |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Replaces the specified model in teh model relation chain with teh join model. |
|
144 | - * Eg EE_Model_Parser::replace_model_name_with_join_model_name_in_model_relation_chain( |
|
145 | - * "Ticket", "Datetime_Ticket", "Datetime.Ticket" ) will return |
|
146 | - * "Datetime.Datetime_Ticket" which can be used to find the table alias model relation chain prefix |
|
147 | - * using EE_Model_Parser::extract_table_alias_model_relation_chain_prefix |
|
148 | - * @param string $model_name |
|
149 | - * @param string $join_model_name |
|
150 | - * @param string $model_relation_chain |
|
151 | - * @return string |
|
152 | - */ |
|
153 | - public static function replace_model_name_with_join_model_name_in_model_relation_chain($model_name, $join_model_name, $model_relation_chain) |
|
154 | - { |
|
155 | - $model_name = EE_Model_Parser::pad_with_periods($model_name); |
|
156 | - $join_model_name = EE_Model_Parser::pad_with_periods($join_model_name); |
|
157 | - $model_relation_chain = EE_Model_Parser::pad_with_periods($model_relation_chain); |
|
158 | - $replaced_with_periods = str_replace($model_name, $join_model_name, $model_relation_chain); |
|
159 | - return EE_Model_Parser::trim_periods($replaced_with_periods); |
|
160 | - } |
|
142 | + /** |
|
143 | + * Replaces the specified model in teh model relation chain with teh join model. |
|
144 | + * Eg EE_Model_Parser::replace_model_name_with_join_model_name_in_model_relation_chain( |
|
145 | + * "Ticket", "Datetime_Ticket", "Datetime.Ticket" ) will return |
|
146 | + * "Datetime.Datetime_Ticket" which can be used to find the table alias model relation chain prefix |
|
147 | + * using EE_Model_Parser::extract_table_alias_model_relation_chain_prefix |
|
148 | + * @param string $model_name |
|
149 | + * @param string $join_model_name |
|
150 | + * @param string $model_relation_chain |
|
151 | + * @return string |
|
152 | + */ |
|
153 | + public static function replace_model_name_with_join_model_name_in_model_relation_chain($model_name, $join_model_name, $model_relation_chain) |
|
154 | + { |
|
155 | + $model_name = EE_Model_Parser::pad_with_periods($model_name); |
|
156 | + $join_model_name = EE_Model_Parser::pad_with_periods($join_model_name); |
|
157 | + $model_relation_chain = EE_Model_Parser::pad_with_periods($model_relation_chain); |
|
158 | + $replaced_with_periods = str_replace($model_name, $join_model_name, $model_relation_chain); |
|
159 | + return EE_Model_Parser::trim_periods($replaced_with_periods); |
|
160 | + } |
|
161 | 161 | } |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | */ |
24 | 24 | public static function pad_with_periods($string_to_pad) |
25 | 25 | { |
26 | - return "." . $string_to_pad . "."; |
|
26 | + return ".".$string_to_pad."."; |
|
27 | 27 | } |
28 | 28 | /** |
29 | 29 | * Basically undoes _pad_with_periods |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | $model_relation_chain = str_replace(".", self::table_alias_model_relation_chain_separator, $model_relation_chain); |
61 | 61 | // eg 'Venue__Event_Venue' |
62 | 62 | if ($model_relation_chain != '') { |
63 | - $model_relation_chain = $model_relation_chain . self::table_alias_model_relation_chain_prefix_end; |
|
63 | + $model_relation_chain = $model_relation_chain.self::table_alias_model_relation_chain_prefix_end; |
|
64 | 64 | } |
65 | 65 | // eg 'Venue_Event_Venue___' |
66 | 66 | return $model_relation_chain; |
@@ -6,45 +6,45 @@ |
||
6 | 6 | */ |
7 | 7 | class EE_Index |
8 | 8 | { |
9 | - protected $_name; |
|
10 | - protected $_field_names; |
|
11 | - protected $_model_name; |
|
12 | - public function __construct($fields) |
|
13 | - { |
|
14 | - $this->_field_names = $fields; |
|
15 | - } |
|
16 | - public function _construct_finalize($name, $model_name) |
|
17 | - { |
|
18 | - $this->_name = $name; |
|
19 | - $this->_model_name = $model_name; |
|
20 | - } |
|
21 | - public function field_names() |
|
22 | - { |
|
23 | - return $this->_field_names; |
|
24 | - } |
|
25 | - /** |
|
26 | - * Internally used by get_this_model() and get_other_model() |
|
27 | - * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
28 | - * @return EEM_Base |
|
29 | - */ |
|
30 | - protected function _get_model($model_name) |
|
31 | - { |
|
32 | - $modelInstance = call_user_func("EEM_" . $model_name . "::instance"); |
|
33 | - return $modelInstance; |
|
34 | - } |
|
35 | - /** |
|
36 | - * Gets all the fields for this index |
|
37 | - * @return EE_Model_Field_Base[] |
|
38 | - */ |
|
39 | - public function fields() |
|
40 | - { |
|
41 | - $fields = array(); |
|
42 | - $model = $this->_get_model($this->_model_name); |
|
43 | - foreach ($model->field_settings() as $field_name => $field_obj) { |
|
44 | - if (in_array($field_name, $this->field_names())) { |
|
45 | - $fields[ $field_name ] = $field_obj; |
|
46 | - } |
|
47 | - } |
|
48 | - return $fields; |
|
49 | - } |
|
9 | + protected $_name; |
|
10 | + protected $_field_names; |
|
11 | + protected $_model_name; |
|
12 | + public function __construct($fields) |
|
13 | + { |
|
14 | + $this->_field_names = $fields; |
|
15 | + } |
|
16 | + public function _construct_finalize($name, $model_name) |
|
17 | + { |
|
18 | + $this->_name = $name; |
|
19 | + $this->_model_name = $model_name; |
|
20 | + } |
|
21 | + public function field_names() |
|
22 | + { |
|
23 | + return $this->_field_names; |
|
24 | + } |
|
25 | + /** |
|
26 | + * Internally used by get_this_model() and get_other_model() |
|
27 | + * @param string $model_name like Event, Question_Group, etc. omit the EEM_ |
|
28 | + * @return EEM_Base |
|
29 | + */ |
|
30 | + protected function _get_model($model_name) |
|
31 | + { |
|
32 | + $modelInstance = call_user_func("EEM_" . $model_name . "::instance"); |
|
33 | + return $modelInstance; |
|
34 | + } |
|
35 | + /** |
|
36 | + * Gets all the fields for this index |
|
37 | + * @return EE_Model_Field_Base[] |
|
38 | + */ |
|
39 | + public function fields() |
|
40 | + { |
|
41 | + $fields = array(); |
|
42 | + $model = $this->_get_model($this->_model_name); |
|
43 | + foreach ($model->field_settings() as $field_name => $field_obj) { |
|
44 | + if (in_array($field_name, $this->field_names())) { |
|
45 | + $fields[ $field_name ] = $field_obj; |
|
46 | + } |
|
47 | + } |
|
48 | + return $fields; |
|
49 | + } |
|
50 | 50 | } |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | */ |
30 | 30 | protected function _get_model($model_name) |
31 | 31 | { |
32 | - $modelInstance = call_user_func("EEM_" . $model_name . "::instance"); |
|
32 | + $modelInstance = call_user_func("EEM_".$model_name."::instance"); |
|
33 | 33 | return $modelInstance; |
34 | 34 | } |
35 | 35 | /** |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | $model = $this->_get_model($this->_model_name); |
43 | 43 | foreach ($model->field_settings() as $field_name => $field_obj) { |
44 | 44 | if (in_array($field_name, $this->field_names())) { |
45 | - $fields[ $field_name ] = $field_obj; |
|
45 | + $fields[$field_name] = $field_obj; |
|
46 | 46 | } |
47 | 47 | } |
48 | 48 | return $fields; |
@@ -25,6 +25,6 @@ |
||
25 | 25 | */ |
26 | 26 | public function get_table_sql() |
27 | 27 | { |
28 | - return " " . $this->get_table_name() . " AS " . $this->get_table_alias() . " "; |
|
28 | + return " ".$this->get_table_name()." AS ".$this->get_table_alias()." "; |
|
29 | 29 | } |
30 | 30 | } |
@@ -6,24 +6,24 @@ |
||
6 | 6 | */ |
7 | 7 | class EE_Primary_Table extends EE_Table_Base |
8 | 8 | { |
9 | - /** |
|
10 | - * |
|
11 | - * @global wpdb $wpdb |
|
12 | - * @param string $table_name with or without wpdb prefix |
|
13 | - * @param string $pk_column name of primary key column |
|
14 | - * @param boolean $global whether the table is "global" as in there is only 1 table on an entire multisite install, |
|
15 | - * or whether each site on a multisite install has a copy of this table |
|
16 | - */ |
|
17 | - public function __construct($table_name, $pk_column = null, $global = false) |
|
18 | - { |
|
19 | - parent::__construct($table_name, $pk_column, $global); |
|
20 | - } |
|
21 | - /** |
|
22 | - * Gets SQL for this table and assigning it an alias. Eg " wp_esp_attendee AS Attendee " |
|
23 | - * @return string |
|
24 | - */ |
|
25 | - public function get_table_sql() |
|
26 | - { |
|
27 | - return " " . $this->get_table_name() . " AS " . $this->get_table_alias() . " "; |
|
28 | - } |
|
9 | + /** |
|
10 | + * |
|
11 | + * @global wpdb $wpdb |
|
12 | + * @param string $table_name with or without wpdb prefix |
|
13 | + * @param string $pk_column name of primary key column |
|
14 | + * @param boolean $global whether the table is "global" as in there is only 1 table on an entire multisite install, |
|
15 | + * or whether each site on a multisite install has a copy of this table |
|
16 | + */ |
|
17 | + public function __construct($table_name, $pk_column = null, $global = false) |
|
18 | + { |
|
19 | + parent::__construct($table_name, $pk_column, $global); |
|
20 | + } |
|
21 | + /** |
|
22 | + * Gets SQL for this table and assigning it an alias. Eg " wp_esp_attendee AS Attendee " |
|
23 | + * @return string |
|
24 | + */ |
|
25 | + public function get_table_sql() |
|
26 | + { |
|
27 | + return " " . $this->get_table_name() . " AS " . $this->get_table_alias() . " "; |
|
28 | + } |
|
29 | 29 | } |
@@ -33,384 +33,384 @@ |
||
33 | 33 | */ |
34 | 34 | class EEH_Inflector |
35 | 35 | { |
36 | - // ------ CLASS METHODS ------ // |
|
37 | - // ---- Public methods ---- // |
|
38 | - // {{{ pluralize() |
|
39 | - |
|
40 | - /** |
|
41 | - * Just calls self::pluralize and strtolower on $word and returns it |
|
42 | - * @param string $word |
|
43 | - * @return string |
|
44 | - */ |
|
45 | - public static function pluralize_and_lower($word) |
|
46 | - { |
|
47 | - return strtolower(self::pluralize($word)); |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * @param string $word |
|
54 | - * @return mixed |
|
55 | - */ |
|
56 | - public static function singularize_and_upper($word) |
|
57 | - { |
|
58 | - return str_replace(' ', '_', self::humanize(self::singularize($word), 'all')); |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * Pluralizes English nouns. |
|
65 | - * |
|
66 | - * @access public |
|
67 | - * @static |
|
68 | - * @param string $word English noun to pluralize |
|
69 | - * @return string Plural noun |
|
70 | - */ |
|
71 | - public static function pluralize($word) |
|
72 | - { |
|
73 | - $plural = array( |
|
74 | - '/(quiz)$/i' => '\1zes', |
|
75 | - '/^(ox)$/i' => '\1en', |
|
76 | - '/([m|l])ouse$/i' => '\1ice', |
|
77 | - '/(matr|vert|ind)ix|ex$/i' => '\1ices', |
|
78 | - '/(x|ch|ss|sh)$/i' => '\1es', |
|
79 | - '/([^aeiouy]|qu)ies$/i' => '\1y', |
|
80 | - '/([^aeiouy]|qu)y$/i' => '\1ies', |
|
81 | - '/(hive)$/i' => '\1s', |
|
82 | - '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', |
|
83 | - '/sis$/i' => 'ses', |
|
84 | - '/([ti])um$/i' => '\1a', |
|
85 | - '/(buffal|tomat)o$/i' => '\1oes', |
|
86 | - '/(bu)s$/i' => '\1ses', |
|
87 | - '/(alias|status)/i' => '\1es', |
|
88 | - '/(octop|vir)us$/i' => '\1i', |
|
89 | - '/(ax|test)is$/i' => '\1es', |
|
90 | - '/s$/i' => 's', |
|
91 | - '/$/' => 's'); |
|
92 | - |
|
93 | - $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep'); |
|
94 | - |
|
95 | - $irregular = array( |
|
96 | - 'person' => 'people', |
|
97 | - 'man' => 'men', |
|
98 | - 'child' => 'children', |
|
99 | - 'sex' => 'sexes', |
|
100 | - 'move' => 'moves'); |
|
101 | - |
|
102 | - $lowercased_word = strtolower($word); |
|
103 | - |
|
104 | - foreach ($uncountable as $_uncountable) { |
|
105 | - if ( |
|
106 | - substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable && // even though the word "price" ends in "rice", it can be pluralized, so check the previous character isnt a letter |
|
107 | - ! ctype_alpha($lowercased_word[ strlen($lowercased_word) - strlen($_uncountable) ]) |
|
108 | - ) { |
|
109 | - return $word; |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - foreach ($irregular as $_plural => $_singular) { |
|
114 | - if (preg_match('/(' . $_plural . ')$/i', $word, $arr)) { |
|
115 | - return preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word); |
|
116 | - } |
|
117 | - } |
|
118 | - |
|
119 | - foreach ($plural as $rule => $replacement) { |
|
120 | - if (preg_match($rule, $word)) { |
|
121 | - return preg_replace($rule, $replacement, $word); |
|
122 | - } |
|
123 | - } |
|
124 | - return false; |
|
125 | - } |
|
126 | - |
|
127 | - // }}} |
|
128 | - // {{{ singularize() |
|
129 | - |
|
130 | - /** |
|
131 | - * Singularizes English nouns. |
|
132 | - * |
|
133 | - * @access public |
|
134 | - * @static |
|
135 | - * @param string $word English noun to singularize |
|
136 | - * @return string Singular noun. |
|
137 | - */ |
|
138 | - public static function singularize($word) |
|
139 | - { |
|
140 | - $singular = array( |
|
141 | - '/(quiz)zes$/i' => '\1', |
|
142 | - '/(matr)ices$/i' => '\1ix', |
|
143 | - '/(vert|ind)ices$/i' => '\1ex', |
|
144 | - '/^(ox)en/i' => '\1', |
|
145 | - '/(alias|status)es$/i' => '\1', |
|
146 | - '/([octop|vir])i$/i' => '\1us', |
|
147 | - '/(cris|ax|test)es$/i' => '\1is', |
|
148 | - '/(shoe)s$/i' => '\1', |
|
149 | - '/(o)es$/i' => '\1', |
|
150 | - '/(bus)es$/i' => '\1', |
|
151 | - '/([m|l])ice$/i' => '\1ouse', |
|
152 | - '/(x|ch|ss|sh)es$/i' => '\1', |
|
153 | - '/(m)ovies$/i' => '\1ovie', |
|
154 | - '/(s)eries$/i' => '\1eries', |
|
155 | - '/([^aeiouy]|qu)ies$/i' => '\1y', |
|
156 | - '/([lr])ves$/i' => '\1f', |
|
157 | - '/(tive)s$/i' => '\1', |
|
158 | - '/(hive)s$/i' => '\1', |
|
159 | - '/([^f])ves$/i' => '\1fe', |
|
160 | - '/(^analy)ses$/i' => '\1sis', |
|
161 | - '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', |
|
162 | - '/([ti])a$/i' => '\1um', |
|
163 | - '/(n)ews$/i' => '\1ews', |
|
164 | - '/s$/i' => '', |
|
165 | - ); |
|
166 | - |
|
167 | - $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep'); |
|
168 | - |
|
169 | - $irregular = array( |
|
170 | - 'person' => 'people', |
|
171 | - 'man' => 'men', |
|
172 | - 'child' => 'children', |
|
173 | - 'sex' => 'sexes', |
|
174 | - 'move' => 'moves'); |
|
175 | - |
|
176 | - $lowercased_word = strtolower($word); |
|
177 | - foreach ($uncountable as $_uncountable) { |
|
178 | - if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) { |
|
179 | - return $word; |
|
180 | - } |
|
181 | - } |
|
182 | - |
|
183 | - foreach ($irregular as $_plural => $_singular) { |
|
184 | - if (preg_match('/(' . $_singular . ')$/i', $word, $arr)) { |
|
185 | - return preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word); |
|
186 | - } |
|
187 | - } |
|
188 | - |
|
189 | - foreach ($singular as $rule => $replacement) { |
|
190 | - if (preg_match($rule, $word)) { |
|
191 | - return preg_replace($rule, $replacement, $word); |
|
192 | - } |
|
193 | - } |
|
194 | - |
|
195 | - return $word; |
|
196 | - } |
|
197 | - |
|
198 | - // }}} |
|
199 | - // {{{ titleize() |
|
200 | - |
|
201 | - /** |
|
202 | - * Converts an underscored or CamelCase word into a English |
|
203 | - * sentence. |
|
204 | - * |
|
205 | - * The titleize static function converts text like "WelcomePage", |
|
206 | - * "welcome_page" or "welcome page" to this "Welcome |
|
207 | - * Page". |
|
208 | - * If second parameter is set to 'first' it will only |
|
209 | - * capitalize the first character of the title. |
|
210 | - * |
|
211 | - * @access public |
|
212 | - * @static |
|
213 | - * @param string $word Word to format as tile |
|
214 | - * @param string $uppercase If set to 'first' it will only uppercase the |
|
215 | - * first character. Otherwise it will uppercase all |
|
216 | - * the words in the title. |
|
217 | - * @return string Text formatted as title |
|
218 | - */ |
|
219 | - public static function titleize($word, $uppercase = '') |
|
220 | - { |
|
221 | - $uppercase = $uppercase === 'first' ? 'ucfirst' : 'ucwords'; |
|
222 | - return $uppercase(EEH_Inflector::humanize(EEH_Inflector::underscore($word))); |
|
223 | - } |
|
224 | - |
|
225 | - // }}} |
|
226 | - // {{{ camelize() |
|
227 | - |
|
228 | - /** |
|
229 | - * Returns given word as CamelCased |
|
230 | - * |
|
231 | - * Converts a word like "send_email" to "SendEmail". It |
|
232 | - * will remove non alphanumeric character from the word, so |
|
233 | - * "who's online" will be converted to "WhoSOnline" |
|
234 | - * |
|
235 | - * @access public |
|
236 | - * @static |
|
237 | - * @see variablize |
|
238 | - * @param string $word Word to convert to camel case |
|
239 | - * @return string UpperCamelCasedWord |
|
240 | - */ |
|
241 | - public static function camelize($word) |
|
242 | - { |
|
243 | - return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9]+/', ' ', $word))); |
|
244 | - } |
|
245 | - |
|
246 | - |
|
247 | - |
|
248 | - /** |
|
249 | - * Camelizes all but the first word. This is handy converting a method which followed EE4 legacy naming convention |
|
250 | - * with the new PSR-based naming conventions |
|
251 | - * @param $word |
|
252 | - * @return string |
|
253 | - */ |
|
254 | - public static function camelize_all_but_first($word) |
|
255 | - { |
|
256 | - return lcfirst(EEH_Inflector::camelize($word)); |
|
257 | - } |
|
258 | - // }}} |
|
259 | - // {{{ underscore() |
|
260 | - |
|
261 | - /** |
|
262 | - * Converts a word "into_it_s_underscored_version" |
|
263 | - * |
|
264 | - * Convert any "CamelCased" or "ordinary Word" into an |
|
265 | - * "underscored_word". |
|
266 | - * |
|
267 | - * This can be really useful for creating friendly URLs. |
|
268 | - * |
|
269 | - * @access public |
|
270 | - * @static |
|
271 | - * @param string $word Word to underscore |
|
272 | - * @return string Underscored word |
|
273 | - */ |
|
274 | - public static function underscore($word) |
|
275 | - { |
|
276 | - return strtolower(preg_replace('/[^A-Z^a-z^0-9]+/', '_', preg_replace('/([a-zd])([A-Z])/', '1_2', preg_replace('/([A-Z]+)([A-Z][a-z])/', '1_2', $word)))); |
|
277 | - } |
|
278 | - |
|
279 | - // }}} |
|
280 | - // {{{ humanize() |
|
281 | - |
|
282 | - /** |
|
283 | - * Returns a human-readable string from $word |
|
284 | - * |
|
285 | - * Returns a human-readable string from $word, by replacing |
|
286 | - * underscores with a space, and by upper-casing the initial |
|
287 | - * character by default. |
|
288 | - * |
|
289 | - * If you need to uppercase all the words you just have to |
|
290 | - * pass 'all' as a second parameter. |
|
291 | - * |
|
292 | - * @access public |
|
293 | - * @static |
|
294 | - * @param string $word String to "humanize" |
|
295 | - * @param string $uppercase If set to 'all' it will uppercase all the words |
|
296 | - * instead of just the first one. |
|
297 | - * @return string Human-readable word |
|
298 | - */ |
|
299 | - public static function humanize($word, $uppercase = '') |
|
300 | - { |
|
301 | - // make special exceptions for acronyms |
|
302 | - $word = str_replace('wp_', 'WP_', $word); |
|
303 | - $uppercase = $uppercase === 'all' ? 'ucwords' : 'ucfirst'; |
|
304 | - return $uppercase(str_replace('_', ' ', preg_replace('/_id$/', '', $word))); |
|
305 | - } |
|
306 | - |
|
307 | - // }}} |
|
308 | - // {{{ variablize() |
|
309 | - |
|
310 | - /** |
|
311 | - * Same as camelize but first char is underscored |
|
312 | - * |
|
313 | - * Converts a word like "send_email" to "sendEmail". It |
|
314 | - * will remove non alphanumeric character from the word, so |
|
315 | - * "who's online" will be converted to "whoSOnline" |
|
316 | - * |
|
317 | - * @access public |
|
318 | - * @static |
|
319 | - * @see camelize |
|
320 | - * @param string $word Word to lowerCamelCase |
|
321 | - * @return string Returns a lowerCamelCasedWord |
|
322 | - */ |
|
323 | - public static function variablize($word) |
|
324 | - { |
|
325 | - $word = EEH_Inflector::camelize($word); |
|
326 | - return strtolower($word[0]) . substr($word, 1); |
|
327 | - } |
|
328 | - |
|
329 | - // }}} |
|
330 | - // {{{ tableize() |
|
331 | - |
|
332 | - /** |
|
333 | - * Converts a class name to its table name according to rails |
|
334 | - * naming conventions. |
|
335 | - * |
|
336 | - * Converts "Person" to "people" |
|
337 | - * |
|
338 | - * @access public |
|
339 | - * @static |
|
340 | - * @see classify |
|
341 | - * @param string $class_name Class name for getting related table_name. |
|
342 | - * @return string plural_table_name |
|
343 | - */ |
|
344 | - public static function tableize($class_name) |
|
345 | - { |
|
346 | - return EEH_Inflector::pluralize(EEH_Inflector::underscore($class_name)); |
|
347 | - } |
|
348 | - |
|
349 | - // }}} |
|
350 | - // {{{ classify() |
|
351 | - |
|
352 | - /** |
|
353 | - * Converts a table name to its class name according to rails |
|
354 | - * naming conventions. |
|
355 | - * |
|
356 | - * Converts "people" to "Person" |
|
357 | - * |
|
358 | - * @access public |
|
359 | - * @static |
|
360 | - * @see tableize |
|
361 | - * @param string $table_name Table name for getting related ClassName. |
|
362 | - * @return string SingularClassName |
|
363 | - */ |
|
364 | - public static function classify($table_name) |
|
365 | - { |
|
366 | - return EEH_Inflector::camelize(EEH_Inflector::singularize($table_name)); |
|
367 | - } |
|
368 | - |
|
369 | - // }}} |
|
370 | - // {{{ ordinalize() |
|
371 | - |
|
372 | - /** |
|
373 | - * Converts number to its ordinal English form. |
|
374 | - * |
|
375 | - * This method converts 13 to 13th, 2 to 2nd ... |
|
376 | - * |
|
377 | - * @access public |
|
378 | - * @static |
|
379 | - * @param integer $number Number to get its ordinal value |
|
380 | - * @return string Ordinal representation of given string. |
|
381 | - */ |
|
382 | - public static function ordinalize($number) |
|
383 | - { |
|
384 | - if (in_array(($number % 100), range(11, 13))) { |
|
385 | - return $number . 'th'; |
|
386 | - } else { |
|
387 | - switch (($number % 10)) { |
|
388 | - case 1: |
|
389 | - return $number . 'st'; |
|
390 | - break; |
|
391 | - case 2: |
|
392 | - return $number . 'nd'; |
|
393 | - break; |
|
394 | - case 3: |
|
395 | - return $number . 'rd'; |
|
396 | - default: |
|
397 | - return $number . 'th'; |
|
398 | - break; |
|
399 | - } |
|
400 | - } |
|
401 | - } |
|
402 | - |
|
403 | - |
|
404 | - |
|
405 | - /** |
|
406 | - * @param $string |
|
407 | - * @return string |
|
408 | - */ |
|
409 | - public static function add_indefinite_article($string) |
|
410 | - { |
|
411 | - if (strtolower($string) === 'null') { |
|
412 | - return $string; |
|
413 | - } |
|
414 | - return (stripos('aeiou', $string[0]) !== false ? 'an ' : 'a ') . $string; |
|
415 | - } |
|
36 | + // ------ CLASS METHODS ------ // |
|
37 | + // ---- Public methods ---- // |
|
38 | + // {{{ pluralize() |
|
39 | + |
|
40 | + /** |
|
41 | + * Just calls self::pluralize and strtolower on $word and returns it |
|
42 | + * @param string $word |
|
43 | + * @return string |
|
44 | + */ |
|
45 | + public static function pluralize_and_lower($word) |
|
46 | + { |
|
47 | + return strtolower(self::pluralize($word)); |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * @param string $word |
|
54 | + * @return mixed |
|
55 | + */ |
|
56 | + public static function singularize_and_upper($word) |
|
57 | + { |
|
58 | + return str_replace(' ', '_', self::humanize(self::singularize($word), 'all')); |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * Pluralizes English nouns. |
|
65 | + * |
|
66 | + * @access public |
|
67 | + * @static |
|
68 | + * @param string $word English noun to pluralize |
|
69 | + * @return string Plural noun |
|
70 | + */ |
|
71 | + public static function pluralize($word) |
|
72 | + { |
|
73 | + $plural = array( |
|
74 | + '/(quiz)$/i' => '\1zes', |
|
75 | + '/^(ox)$/i' => '\1en', |
|
76 | + '/([m|l])ouse$/i' => '\1ice', |
|
77 | + '/(matr|vert|ind)ix|ex$/i' => '\1ices', |
|
78 | + '/(x|ch|ss|sh)$/i' => '\1es', |
|
79 | + '/([^aeiouy]|qu)ies$/i' => '\1y', |
|
80 | + '/([^aeiouy]|qu)y$/i' => '\1ies', |
|
81 | + '/(hive)$/i' => '\1s', |
|
82 | + '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', |
|
83 | + '/sis$/i' => 'ses', |
|
84 | + '/([ti])um$/i' => '\1a', |
|
85 | + '/(buffal|tomat)o$/i' => '\1oes', |
|
86 | + '/(bu)s$/i' => '\1ses', |
|
87 | + '/(alias|status)/i' => '\1es', |
|
88 | + '/(octop|vir)us$/i' => '\1i', |
|
89 | + '/(ax|test)is$/i' => '\1es', |
|
90 | + '/s$/i' => 's', |
|
91 | + '/$/' => 's'); |
|
92 | + |
|
93 | + $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep'); |
|
94 | + |
|
95 | + $irregular = array( |
|
96 | + 'person' => 'people', |
|
97 | + 'man' => 'men', |
|
98 | + 'child' => 'children', |
|
99 | + 'sex' => 'sexes', |
|
100 | + 'move' => 'moves'); |
|
101 | + |
|
102 | + $lowercased_word = strtolower($word); |
|
103 | + |
|
104 | + foreach ($uncountable as $_uncountable) { |
|
105 | + if ( |
|
106 | + substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable && // even though the word "price" ends in "rice", it can be pluralized, so check the previous character isnt a letter |
|
107 | + ! ctype_alpha($lowercased_word[ strlen($lowercased_word) - strlen($_uncountable) ]) |
|
108 | + ) { |
|
109 | + return $word; |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + foreach ($irregular as $_plural => $_singular) { |
|
114 | + if (preg_match('/(' . $_plural . ')$/i', $word, $arr)) { |
|
115 | + return preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word); |
|
116 | + } |
|
117 | + } |
|
118 | + |
|
119 | + foreach ($plural as $rule => $replacement) { |
|
120 | + if (preg_match($rule, $word)) { |
|
121 | + return preg_replace($rule, $replacement, $word); |
|
122 | + } |
|
123 | + } |
|
124 | + return false; |
|
125 | + } |
|
126 | + |
|
127 | + // }}} |
|
128 | + // {{{ singularize() |
|
129 | + |
|
130 | + /** |
|
131 | + * Singularizes English nouns. |
|
132 | + * |
|
133 | + * @access public |
|
134 | + * @static |
|
135 | + * @param string $word English noun to singularize |
|
136 | + * @return string Singular noun. |
|
137 | + */ |
|
138 | + public static function singularize($word) |
|
139 | + { |
|
140 | + $singular = array( |
|
141 | + '/(quiz)zes$/i' => '\1', |
|
142 | + '/(matr)ices$/i' => '\1ix', |
|
143 | + '/(vert|ind)ices$/i' => '\1ex', |
|
144 | + '/^(ox)en/i' => '\1', |
|
145 | + '/(alias|status)es$/i' => '\1', |
|
146 | + '/([octop|vir])i$/i' => '\1us', |
|
147 | + '/(cris|ax|test)es$/i' => '\1is', |
|
148 | + '/(shoe)s$/i' => '\1', |
|
149 | + '/(o)es$/i' => '\1', |
|
150 | + '/(bus)es$/i' => '\1', |
|
151 | + '/([m|l])ice$/i' => '\1ouse', |
|
152 | + '/(x|ch|ss|sh)es$/i' => '\1', |
|
153 | + '/(m)ovies$/i' => '\1ovie', |
|
154 | + '/(s)eries$/i' => '\1eries', |
|
155 | + '/([^aeiouy]|qu)ies$/i' => '\1y', |
|
156 | + '/([lr])ves$/i' => '\1f', |
|
157 | + '/(tive)s$/i' => '\1', |
|
158 | + '/(hive)s$/i' => '\1', |
|
159 | + '/([^f])ves$/i' => '\1fe', |
|
160 | + '/(^analy)ses$/i' => '\1sis', |
|
161 | + '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', |
|
162 | + '/([ti])a$/i' => '\1um', |
|
163 | + '/(n)ews$/i' => '\1ews', |
|
164 | + '/s$/i' => '', |
|
165 | + ); |
|
166 | + |
|
167 | + $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep'); |
|
168 | + |
|
169 | + $irregular = array( |
|
170 | + 'person' => 'people', |
|
171 | + 'man' => 'men', |
|
172 | + 'child' => 'children', |
|
173 | + 'sex' => 'sexes', |
|
174 | + 'move' => 'moves'); |
|
175 | + |
|
176 | + $lowercased_word = strtolower($word); |
|
177 | + foreach ($uncountable as $_uncountable) { |
|
178 | + if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) { |
|
179 | + return $word; |
|
180 | + } |
|
181 | + } |
|
182 | + |
|
183 | + foreach ($irregular as $_plural => $_singular) { |
|
184 | + if (preg_match('/(' . $_singular . ')$/i', $word, $arr)) { |
|
185 | + return preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word); |
|
186 | + } |
|
187 | + } |
|
188 | + |
|
189 | + foreach ($singular as $rule => $replacement) { |
|
190 | + if (preg_match($rule, $word)) { |
|
191 | + return preg_replace($rule, $replacement, $word); |
|
192 | + } |
|
193 | + } |
|
194 | + |
|
195 | + return $word; |
|
196 | + } |
|
197 | + |
|
198 | + // }}} |
|
199 | + // {{{ titleize() |
|
200 | + |
|
201 | + /** |
|
202 | + * Converts an underscored or CamelCase word into a English |
|
203 | + * sentence. |
|
204 | + * |
|
205 | + * The titleize static function converts text like "WelcomePage", |
|
206 | + * "welcome_page" or "welcome page" to this "Welcome |
|
207 | + * Page". |
|
208 | + * If second parameter is set to 'first' it will only |
|
209 | + * capitalize the first character of the title. |
|
210 | + * |
|
211 | + * @access public |
|
212 | + * @static |
|
213 | + * @param string $word Word to format as tile |
|
214 | + * @param string $uppercase If set to 'first' it will only uppercase the |
|
215 | + * first character. Otherwise it will uppercase all |
|
216 | + * the words in the title. |
|
217 | + * @return string Text formatted as title |
|
218 | + */ |
|
219 | + public static function titleize($word, $uppercase = '') |
|
220 | + { |
|
221 | + $uppercase = $uppercase === 'first' ? 'ucfirst' : 'ucwords'; |
|
222 | + return $uppercase(EEH_Inflector::humanize(EEH_Inflector::underscore($word))); |
|
223 | + } |
|
224 | + |
|
225 | + // }}} |
|
226 | + // {{{ camelize() |
|
227 | + |
|
228 | + /** |
|
229 | + * Returns given word as CamelCased |
|
230 | + * |
|
231 | + * Converts a word like "send_email" to "SendEmail". It |
|
232 | + * will remove non alphanumeric character from the word, so |
|
233 | + * "who's online" will be converted to "WhoSOnline" |
|
234 | + * |
|
235 | + * @access public |
|
236 | + * @static |
|
237 | + * @see variablize |
|
238 | + * @param string $word Word to convert to camel case |
|
239 | + * @return string UpperCamelCasedWord |
|
240 | + */ |
|
241 | + public static function camelize($word) |
|
242 | + { |
|
243 | + return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9]+/', ' ', $word))); |
|
244 | + } |
|
245 | + |
|
246 | + |
|
247 | + |
|
248 | + /** |
|
249 | + * Camelizes all but the first word. This is handy converting a method which followed EE4 legacy naming convention |
|
250 | + * with the new PSR-based naming conventions |
|
251 | + * @param $word |
|
252 | + * @return string |
|
253 | + */ |
|
254 | + public static function camelize_all_but_first($word) |
|
255 | + { |
|
256 | + return lcfirst(EEH_Inflector::camelize($word)); |
|
257 | + } |
|
258 | + // }}} |
|
259 | + // {{{ underscore() |
|
260 | + |
|
261 | + /** |
|
262 | + * Converts a word "into_it_s_underscored_version" |
|
263 | + * |
|
264 | + * Convert any "CamelCased" or "ordinary Word" into an |
|
265 | + * "underscored_word". |
|
266 | + * |
|
267 | + * This can be really useful for creating friendly URLs. |
|
268 | + * |
|
269 | + * @access public |
|
270 | + * @static |
|
271 | + * @param string $word Word to underscore |
|
272 | + * @return string Underscored word |
|
273 | + */ |
|
274 | + public static function underscore($word) |
|
275 | + { |
|
276 | + return strtolower(preg_replace('/[^A-Z^a-z^0-9]+/', '_', preg_replace('/([a-zd])([A-Z])/', '1_2', preg_replace('/([A-Z]+)([A-Z][a-z])/', '1_2', $word)))); |
|
277 | + } |
|
278 | + |
|
279 | + // }}} |
|
280 | + // {{{ humanize() |
|
281 | + |
|
282 | + /** |
|
283 | + * Returns a human-readable string from $word |
|
284 | + * |
|
285 | + * Returns a human-readable string from $word, by replacing |
|
286 | + * underscores with a space, and by upper-casing the initial |
|
287 | + * character by default. |
|
288 | + * |
|
289 | + * If you need to uppercase all the words you just have to |
|
290 | + * pass 'all' as a second parameter. |
|
291 | + * |
|
292 | + * @access public |
|
293 | + * @static |
|
294 | + * @param string $word String to "humanize" |
|
295 | + * @param string $uppercase If set to 'all' it will uppercase all the words |
|
296 | + * instead of just the first one. |
|
297 | + * @return string Human-readable word |
|
298 | + */ |
|
299 | + public static function humanize($word, $uppercase = '') |
|
300 | + { |
|
301 | + // make special exceptions for acronyms |
|
302 | + $word = str_replace('wp_', 'WP_', $word); |
|
303 | + $uppercase = $uppercase === 'all' ? 'ucwords' : 'ucfirst'; |
|
304 | + return $uppercase(str_replace('_', ' ', preg_replace('/_id$/', '', $word))); |
|
305 | + } |
|
306 | + |
|
307 | + // }}} |
|
308 | + // {{{ variablize() |
|
309 | + |
|
310 | + /** |
|
311 | + * Same as camelize but first char is underscored |
|
312 | + * |
|
313 | + * Converts a word like "send_email" to "sendEmail". It |
|
314 | + * will remove non alphanumeric character from the word, so |
|
315 | + * "who's online" will be converted to "whoSOnline" |
|
316 | + * |
|
317 | + * @access public |
|
318 | + * @static |
|
319 | + * @see camelize |
|
320 | + * @param string $word Word to lowerCamelCase |
|
321 | + * @return string Returns a lowerCamelCasedWord |
|
322 | + */ |
|
323 | + public static function variablize($word) |
|
324 | + { |
|
325 | + $word = EEH_Inflector::camelize($word); |
|
326 | + return strtolower($word[0]) . substr($word, 1); |
|
327 | + } |
|
328 | + |
|
329 | + // }}} |
|
330 | + // {{{ tableize() |
|
331 | + |
|
332 | + /** |
|
333 | + * Converts a class name to its table name according to rails |
|
334 | + * naming conventions. |
|
335 | + * |
|
336 | + * Converts "Person" to "people" |
|
337 | + * |
|
338 | + * @access public |
|
339 | + * @static |
|
340 | + * @see classify |
|
341 | + * @param string $class_name Class name for getting related table_name. |
|
342 | + * @return string plural_table_name |
|
343 | + */ |
|
344 | + public static function tableize($class_name) |
|
345 | + { |
|
346 | + return EEH_Inflector::pluralize(EEH_Inflector::underscore($class_name)); |
|
347 | + } |
|
348 | + |
|
349 | + // }}} |
|
350 | + // {{{ classify() |
|
351 | + |
|
352 | + /** |
|
353 | + * Converts a table name to its class name according to rails |
|
354 | + * naming conventions. |
|
355 | + * |
|
356 | + * Converts "people" to "Person" |
|
357 | + * |
|
358 | + * @access public |
|
359 | + * @static |
|
360 | + * @see tableize |
|
361 | + * @param string $table_name Table name for getting related ClassName. |
|
362 | + * @return string SingularClassName |
|
363 | + */ |
|
364 | + public static function classify($table_name) |
|
365 | + { |
|
366 | + return EEH_Inflector::camelize(EEH_Inflector::singularize($table_name)); |
|
367 | + } |
|
368 | + |
|
369 | + // }}} |
|
370 | + // {{{ ordinalize() |
|
371 | + |
|
372 | + /** |
|
373 | + * Converts number to its ordinal English form. |
|
374 | + * |
|
375 | + * This method converts 13 to 13th, 2 to 2nd ... |
|
376 | + * |
|
377 | + * @access public |
|
378 | + * @static |
|
379 | + * @param integer $number Number to get its ordinal value |
|
380 | + * @return string Ordinal representation of given string. |
|
381 | + */ |
|
382 | + public static function ordinalize($number) |
|
383 | + { |
|
384 | + if (in_array(($number % 100), range(11, 13))) { |
|
385 | + return $number . 'th'; |
|
386 | + } else { |
|
387 | + switch (($number % 10)) { |
|
388 | + case 1: |
|
389 | + return $number . 'st'; |
|
390 | + break; |
|
391 | + case 2: |
|
392 | + return $number . 'nd'; |
|
393 | + break; |
|
394 | + case 3: |
|
395 | + return $number . 'rd'; |
|
396 | + default: |
|
397 | + return $number . 'th'; |
|
398 | + break; |
|
399 | + } |
|
400 | + } |
|
401 | + } |
|
402 | + |
|
403 | + |
|
404 | + |
|
405 | + /** |
|
406 | + * @param $string |
|
407 | + * @return string |
|
408 | + */ |
|
409 | + public static function add_indefinite_article($string) |
|
410 | + { |
|
411 | + if (strtolower($string) === 'null') { |
|
412 | + return $string; |
|
413 | + } |
|
414 | + return (stripos('aeiou', $string[0]) !== false ? 'an ' : 'a ') . $string; |
|
415 | + } |
|
416 | 416 | } |
@@ -104,15 +104,15 @@ discard block |
||
104 | 104 | foreach ($uncountable as $_uncountable) { |
105 | 105 | if ( |
106 | 106 | substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable && // even though the word "price" ends in "rice", it can be pluralized, so check the previous character isnt a letter |
107 | - ! ctype_alpha($lowercased_word[ strlen($lowercased_word) - strlen($_uncountable) ]) |
|
107 | + ! ctype_alpha($lowercased_word[strlen($lowercased_word) - strlen($_uncountable)]) |
|
108 | 108 | ) { |
109 | 109 | return $word; |
110 | 110 | } |
111 | 111 | } |
112 | 112 | |
113 | 113 | foreach ($irregular as $_plural => $_singular) { |
114 | - if (preg_match('/(' . $_plural . ')$/i', $word, $arr)) { |
|
115 | - return preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word); |
|
114 | + if (preg_match('/('.$_plural.')$/i', $word, $arr)) { |
|
115 | + return preg_replace('/('.$_plural.')$/i', substr($arr[0], 0, 1).substr($_singular, 1), $word); |
|
116 | 116 | } |
117 | 117 | } |
118 | 118 | |
@@ -181,8 +181,8 @@ discard block |
||
181 | 181 | } |
182 | 182 | |
183 | 183 | foreach ($irregular as $_plural => $_singular) { |
184 | - if (preg_match('/(' . $_singular . ')$/i', $word, $arr)) { |
|
185 | - return preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word); |
|
184 | + if (preg_match('/('.$_singular.')$/i', $word, $arr)) { |
|
185 | + return preg_replace('/('.$_singular.')$/i', substr($arr[0], 0, 1).substr($_plural, 1), $word); |
|
186 | 186 | } |
187 | 187 | } |
188 | 188 | |
@@ -323,7 +323,7 @@ discard block |
||
323 | 323 | public static function variablize($word) |
324 | 324 | { |
325 | 325 | $word = EEH_Inflector::camelize($word); |
326 | - return strtolower($word[0]) . substr($word, 1); |
|
326 | + return strtolower($word[0]).substr($word, 1); |
|
327 | 327 | } |
328 | 328 | |
329 | 329 | // }}} |
@@ -382,19 +382,19 @@ discard block |
||
382 | 382 | public static function ordinalize($number) |
383 | 383 | { |
384 | 384 | if (in_array(($number % 100), range(11, 13))) { |
385 | - return $number . 'th'; |
|
385 | + return $number.'th'; |
|
386 | 386 | } else { |
387 | 387 | switch (($number % 10)) { |
388 | 388 | case 1: |
389 | - return $number . 'st'; |
|
389 | + return $number.'st'; |
|
390 | 390 | break; |
391 | 391 | case 2: |
392 | - return $number . 'nd'; |
|
392 | + return $number.'nd'; |
|
393 | 393 | break; |
394 | 394 | case 3: |
395 | - return $number . 'rd'; |
|
395 | + return $number.'rd'; |
|
396 | 396 | default: |
397 | - return $number . 'th'; |
|
397 | + return $number.'th'; |
|
398 | 398 | break; |
399 | 399 | } |
400 | 400 | } |
@@ -411,6 +411,6 @@ discard block |
||
411 | 411 | if (strtolower($string) === 'null') { |
412 | 412 | return $string; |
413 | 413 | } |
414 | - return (stripos('aeiou', $string[0]) !== false ? 'an ' : 'a ') . $string; |
|
414 | + return (stripos('aeiou', $string[0]) !== false ? 'an ' : 'a ').$string; |
|
415 | 415 | } |
416 | 416 | } |
@@ -208,14 +208,14 @@ discard block |
||
208 | 208 | if ($sc_obj instanceof EE_Shortcodes) { |
209 | 209 | // we need to setup any dynamic shortcodes so that they work with the array_key_exists |
210 | 210 | preg_match_all('/(\[[A-Za-z0-9\_]+_\*)/', $shortcode, $matches); |
211 | - $sc_to_verify = ! empty($matches[0]) ? $matches[0][0] . ']' : $shortcode; |
|
211 | + $sc_to_verify = ! empty($matches[0]) ? $matches[0][0].']' : $shortcode; |
|
212 | 212 | |
213 | - if (! array_key_exists($sc_to_verify, $sc_obj->get_shortcodes())) { |
|
213 | + if ( ! array_key_exists($sc_to_verify, $sc_obj->get_shortcodes())) { |
|
214 | 214 | continue; // the given shortcode isn't in this object |
215 | 215 | } |
216 | 216 | |
217 | 217 | // if this isn't a "list" type shortcode then we'll send along the data vanilla instead of in an array. |
218 | - if (! in_array($sc_to_verify, $list_type_shortcodes)) { |
|
218 | + if ( ! in_array($sc_to_verify, $list_type_shortcodes)) { |
|
219 | 219 | $data_send = ! is_object($this->_data) && isset($this->_data['data']) ? $this->_data['data'] : $this->_data; |
220 | 220 | } else { |
221 | 221 | $data_send = $this->_data; |
@@ -271,7 +271,7 @@ discard block |
||
271 | 271 | foreach ($valid_shortcodes as $shortcode_ref) { |
272 | 272 | $ref = ucwords(str_replace('_', ' ', $shortcode_ref)); |
273 | 273 | $ref = str_replace(' ', '_', $ref); |
274 | - $classname = 'EE_' . $ref . '_Shortcodes'; |
|
274 | + $classname = 'EE_'.$ref.'_Shortcodes'; |
|
275 | 275 | if (class_exists($classname)) { |
276 | 276 | $this->_shortcode_objs[] = new $classname(); |
277 | 277 | } |
@@ -11,267 +11,267 @@ |
||
11 | 11 | */ |
12 | 12 | class EEH_Parse_Shortcodes |
13 | 13 | { |
14 | - /** |
|
15 | - * holds the template |
|
16 | - * |
|
17 | - * @access private |
|
18 | - * @var mixed (string|array) |
|
19 | - */ |
|
20 | - private $_template; |
|
21 | - |
|
22 | - |
|
23 | - /** |
|
24 | - * holds the incoming data object |
|
25 | - * |
|
26 | - * @access private |
|
27 | - * @var object |
|
28 | - */ |
|
29 | - private $_data; |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * will hold an array of EE_Shortcodes library objects. |
|
34 | - * |
|
35 | - * @access private |
|
36 | - * @var EE_Shortcodes[] |
|
37 | - */ |
|
38 | - private $_shortcode_objs = array(); |
|
39 | - |
|
40 | - |
|
41 | - public function __construct() |
|
42 | - { |
|
43 | - } |
|
44 | - |
|
45 | - |
|
46 | - /** |
|
47 | - * This kicks off the parsing of shortcodes in message templates |
|
48 | - * |
|
49 | - * @param string $template This is the incoming string to be parsed |
|
50 | - * @param EE_Messages_Addressee $data This is the incoming data object |
|
51 | - * @param array $valid_shortcodes An array of strings that correspond to EE_Shortcode libraries |
|
52 | - * @param EE_message_type $message_type The message type that called the parser |
|
53 | - * @param EE_messenger $messenger The active messenger for this parsing session. |
|
54 | - * @param EE_Message $message |
|
55 | - * @return string The parsed template string |
|
56 | - */ |
|
57 | - public function parse_message_template( |
|
58 | - $template, |
|
59 | - EE_Messages_Addressee $data, |
|
60 | - $valid_shortcodes, |
|
61 | - EE_message_type $message_type, |
|
62 | - EE_messenger $messenger, |
|
63 | - EE_Message $message |
|
64 | - ) { |
|
65 | - $extra_data = array( |
|
66 | - 'messenger' => $messenger, |
|
67 | - 'message_type' => $message_type, |
|
68 | - 'message' => $message, |
|
69 | - ); |
|
70 | - $this->_init_data($template, $data, $valid_shortcodes, $extra_data); |
|
71 | - $this->_template = is_array($template) ? $template['main'] : $template; |
|
72 | - return $this->_parse_message_template(); |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - public function parse_attendee_list_template( |
|
77 | - $template, |
|
78 | - EE_Registration $registration, |
|
79 | - $valid_shortcodes, |
|
80 | - $extra_data = array() |
|
81 | - ) { |
|
82 | - $this->_init_data($template, $registration, $valid_shortcodes, $extra_data); |
|
83 | - $this->_template = is_array($template) ? $template['attendee_list'] : $template; |
|
84 | - return $this->_parse_message_template(); |
|
85 | - } |
|
86 | - |
|
87 | - public function parse_event_list_template($template, EE_Event $event, $valid_shortcodes, $extra_data = array()) |
|
88 | - { |
|
89 | - $this->_init_data($template, $event, $valid_shortcodes, $extra_data); |
|
90 | - $this->_template = is_array($template) ? $template['event_list'] : $template; |
|
91 | - return $this->_parse_message_template(); |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - public function parse_ticket_list_template($template, EE_Ticket $ticket, $valid_shortcodes, $extra_data = array()) |
|
96 | - { |
|
97 | - $this->_init_data($template, $ticket, $valid_shortcodes, $extra_data); |
|
98 | - $this->_template = is_array($template) ? $template['ticket_list'] : $template; |
|
99 | - return $this->_parse_message_template(); |
|
100 | - } |
|
101 | - |
|
102 | - |
|
103 | - public function parse_line_item_list_template( |
|
104 | - $template, |
|
105 | - EE_Line_Item $line_item, |
|
106 | - $valid_shortcodes, |
|
107 | - $extra_data = array() |
|
108 | - ) { |
|
109 | - $this->_init_data($template, $line_item, $valid_shortcodes, $extra_data); |
|
110 | - $this->_template = is_array($template) ? $template['ticket_line_item_no_pms'] : $template; |
|
111 | - return $this->_parse_message_template(); |
|
112 | - } |
|
113 | - |
|
114 | - |
|
115 | - public function parse_payment_list_template( |
|
116 | - $template, |
|
117 | - EE_Payment $payment_item, |
|
118 | - $valid_shortcodes, |
|
119 | - $extra_data = array() |
|
120 | - ) { |
|
121 | - $this->_init_data($template, $payment_item, $valid_shortcodes, $extra_data); |
|
122 | - $this->_template = is_array($template) ? $template['payment_list'] : $template; |
|
123 | - return $this->_parse_message_template(); |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - public function parse_datetime_list_template( |
|
128 | - $template, |
|
129 | - EE_Datetime $datetime, |
|
130 | - $valid_shortcodes, |
|
131 | - $extra_data = array() |
|
132 | - ) { |
|
133 | - $this->_init_data($template, $datetime, $valid_shortcodes, $extra_data); |
|
134 | - $this->_template = is_array($template) ? $template['datetime_list'] : $template; |
|
135 | - return $this->_parse_message_template(); |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - public function parse_question_list_template($template, EE_Answer $answer, $valid_shortcodes, $extra_data = array()) |
|
140 | - { |
|
141 | - $this->_init_data($template, $answer, $valid_shortcodes, $extra_data); |
|
142 | - $this->_template = is_array($template) ? $template['question_list'] : $template; |
|
143 | - return $this->_parse_message_template(); |
|
144 | - } |
|
145 | - |
|
146 | - |
|
147 | - private function _init_data($template, $data, $valid_shortcodes, $extra_data = array()) |
|
148 | - { |
|
149 | - $this->_reset_props(); |
|
150 | - $this->_data['template'] = $template; |
|
151 | - $this->_data['data'] = $data; |
|
152 | - $this->_data['extra_data'] = $extra_data; |
|
153 | - $this->_set_shortcodes($valid_shortcodes); |
|
154 | - } |
|
155 | - |
|
156 | - |
|
157 | - private function _reset_props() |
|
158 | - { |
|
159 | - $this->_template = $this->_data = null; |
|
160 | - $this->_shortcode_objs = array(); |
|
161 | - } |
|
162 | - |
|
163 | - |
|
164 | - /** |
|
165 | - * takes the given template and parses it with the $_shortcodes property |
|
166 | - * |
|
167 | - * @access private |
|
168 | - * @return string |
|
169 | - */ |
|
170 | - private function _parse_message_template() |
|
171 | - { |
|
172 | - // now let's get a list of shortcodes that are found in the given template |
|
173 | - preg_match_all('/(\[.+?\])/', $this->_template, $matches); |
|
174 | - $shortcodes = (array) $matches[0]; // this should be an array of shortcodes in the template string. |
|
175 | - |
|
176 | - $matched_code = array(); |
|
177 | - $sc_values = array(); |
|
178 | - |
|
179 | - $list_type_shortcodes = array( |
|
180 | - '[ATTENDEE_LIST]', |
|
181 | - '[EVENT_LIST]', |
|
182 | - '[TICKET_LIST]', |
|
183 | - '[DATETIME_LIST]', |
|
184 | - '[QUESTION_LIST]', |
|
185 | - '[RECIPIENT_QUESTION_LIST]', |
|
186 | - '[PRIMARY_REGISTRANT_QUESTION_LIST]', |
|
187 | - '[RECIPIENT_TICKET_LIST]', |
|
188 | - '[PRIMARY_REGISTRANT_TICKET_LIST]', |
|
189 | - '[RECIPIENT_DATETIME_LIST]', |
|
190 | - '[PRIMARY_REGISTRANT_DATETIME_LIST]', |
|
191 | - '[TICKET_LINE_ITEM_LIST]', |
|
192 | - '[TAX_LINE_ITEM_LIST]', |
|
193 | - '[ADDITIONAL_LINE_ITEM_LIST]', |
|
194 | - '[PRICE_MODIFIER_LINE_ITEM_LIST]', |
|
195 | - '[PAYMENT_LIST_*]', |
|
196 | - ); |
|
197 | - |
|
198 | - $list_type_shortcodes = apply_filters( |
|
199 | - 'FHEE__EEH_Parse_Shortcodes___parse_message_template__list_type_shortcodes', |
|
200 | - $list_type_shortcodes |
|
201 | - ); |
|
202 | - |
|
203 | - // now lets go ahead and loop through our parsers for each shortcode and setup the values |
|
204 | - foreach ($shortcodes as $shortcode) { |
|
205 | - foreach ($this->_shortcode_objs as $sc_obj) { |
|
206 | - if ($sc_obj instanceof EE_Shortcodes) { |
|
207 | - // we need to setup any dynamic shortcodes so that they work with the array_key_exists |
|
208 | - preg_match_all('/(\[[A-Za-z0-9\_]+_\*)/', $shortcode, $matches); |
|
209 | - $sc_to_verify = ! empty($matches[0]) ? $matches[0][0] . ']' : $shortcode; |
|
210 | - |
|
211 | - if (! array_key_exists($sc_to_verify, $sc_obj->get_shortcodes())) { |
|
212 | - continue; // the given shortcode isn't in this object |
|
213 | - } |
|
214 | - |
|
215 | - // if this isn't a "list" type shortcode then we'll send along the data vanilla instead of in an array. |
|
216 | - if (! in_array($sc_to_verify, $list_type_shortcodes)) { |
|
217 | - $data_send = ! is_object($this->_data) && isset($this->_data['data']) ? $this->_data['data'] : $this->_data; |
|
218 | - } else { |
|
219 | - $data_send = $this->_data; |
|
220 | - } |
|
221 | - |
|
222 | - // is this a conditional type shortcode? If it is then we actually parse the template here. |
|
223 | - if ($this->_is_conditional_shortcode($shortcode)) { |
|
224 | - // most shortcode parsers are not going to have a match for this shortcode and will return an |
|
225 | - // empty string so we need to make sure that we're only replacing the template when there is a non empty string. |
|
226 | - $parsed = $sc_obj->parser($shortcode, $data_send, $this->_data['extra_data']); |
|
227 | - if ($parsed) { |
|
228 | - $this->_template = $parsed; |
|
229 | - } |
|
230 | - } |
|
231 | - |
|
232 | - $parsed = $sc_obj->parser($shortcode, $data_send, $this->_data['extra_data']); |
|
233 | - |
|
234 | - $matched_code[] = $shortcode; |
|
235 | - $sc_values[] = $parsed; |
|
236 | - } |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - // now we've got parsed values for all the shortcodes in the template so we can go ahead and swap the shortcodes out. |
|
241 | - return str_replace(array_values($matched_code), array_values($sc_values), $this->_template); |
|
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - /** |
|
246 | - * Simply returns whether the given shortcode matches the structure for a conditional shortcode. |
|
247 | - * |
|
248 | - * Does it match this format: `[IF_` |
|
249 | - * |
|
250 | - * @param $shortcode |
|
251 | - */ |
|
252 | - protected function _is_conditional_shortcode($shortcode) |
|
253 | - { |
|
254 | - return strpos($shortcode, '[IF_') === 0; |
|
255 | - } |
|
256 | - |
|
257 | - |
|
258 | - /** |
|
259 | - * This sets the shortcodes property from the incoming array of valid shortcodes that corresponds to names of |
|
260 | - * various EE_Shortcode library objects |
|
261 | - * |
|
262 | - * @access private |
|
263 | - * @param array $valid_shortcodes an array of strings corresponding to EE_Shortcode Library objects |
|
264 | - * @return void |
|
265 | - */ |
|
266 | - private function _set_shortcodes($valid_shortcodes) |
|
267 | - { |
|
268 | - foreach ($valid_shortcodes as $shortcode_ref) { |
|
269 | - $ref = ucwords(str_replace('_', ' ', $shortcode_ref)); |
|
270 | - $ref = str_replace(' ', '_', $ref); |
|
271 | - $classname = 'EE_' . $ref . '_Shortcodes'; |
|
272 | - if (class_exists($classname)) { |
|
273 | - $this->_shortcode_objs[] = new $classname(); |
|
274 | - } |
|
275 | - } |
|
276 | - } |
|
14 | + /** |
|
15 | + * holds the template |
|
16 | + * |
|
17 | + * @access private |
|
18 | + * @var mixed (string|array) |
|
19 | + */ |
|
20 | + private $_template; |
|
21 | + |
|
22 | + |
|
23 | + /** |
|
24 | + * holds the incoming data object |
|
25 | + * |
|
26 | + * @access private |
|
27 | + * @var object |
|
28 | + */ |
|
29 | + private $_data; |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * will hold an array of EE_Shortcodes library objects. |
|
34 | + * |
|
35 | + * @access private |
|
36 | + * @var EE_Shortcodes[] |
|
37 | + */ |
|
38 | + private $_shortcode_objs = array(); |
|
39 | + |
|
40 | + |
|
41 | + public function __construct() |
|
42 | + { |
|
43 | + } |
|
44 | + |
|
45 | + |
|
46 | + /** |
|
47 | + * This kicks off the parsing of shortcodes in message templates |
|
48 | + * |
|
49 | + * @param string $template This is the incoming string to be parsed |
|
50 | + * @param EE_Messages_Addressee $data This is the incoming data object |
|
51 | + * @param array $valid_shortcodes An array of strings that correspond to EE_Shortcode libraries |
|
52 | + * @param EE_message_type $message_type The message type that called the parser |
|
53 | + * @param EE_messenger $messenger The active messenger for this parsing session. |
|
54 | + * @param EE_Message $message |
|
55 | + * @return string The parsed template string |
|
56 | + */ |
|
57 | + public function parse_message_template( |
|
58 | + $template, |
|
59 | + EE_Messages_Addressee $data, |
|
60 | + $valid_shortcodes, |
|
61 | + EE_message_type $message_type, |
|
62 | + EE_messenger $messenger, |
|
63 | + EE_Message $message |
|
64 | + ) { |
|
65 | + $extra_data = array( |
|
66 | + 'messenger' => $messenger, |
|
67 | + 'message_type' => $message_type, |
|
68 | + 'message' => $message, |
|
69 | + ); |
|
70 | + $this->_init_data($template, $data, $valid_shortcodes, $extra_data); |
|
71 | + $this->_template = is_array($template) ? $template['main'] : $template; |
|
72 | + return $this->_parse_message_template(); |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + public function parse_attendee_list_template( |
|
77 | + $template, |
|
78 | + EE_Registration $registration, |
|
79 | + $valid_shortcodes, |
|
80 | + $extra_data = array() |
|
81 | + ) { |
|
82 | + $this->_init_data($template, $registration, $valid_shortcodes, $extra_data); |
|
83 | + $this->_template = is_array($template) ? $template['attendee_list'] : $template; |
|
84 | + return $this->_parse_message_template(); |
|
85 | + } |
|
86 | + |
|
87 | + public function parse_event_list_template($template, EE_Event $event, $valid_shortcodes, $extra_data = array()) |
|
88 | + { |
|
89 | + $this->_init_data($template, $event, $valid_shortcodes, $extra_data); |
|
90 | + $this->_template = is_array($template) ? $template['event_list'] : $template; |
|
91 | + return $this->_parse_message_template(); |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + public function parse_ticket_list_template($template, EE_Ticket $ticket, $valid_shortcodes, $extra_data = array()) |
|
96 | + { |
|
97 | + $this->_init_data($template, $ticket, $valid_shortcodes, $extra_data); |
|
98 | + $this->_template = is_array($template) ? $template['ticket_list'] : $template; |
|
99 | + return $this->_parse_message_template(); |
|
100 | + } |
|
101 | + |
|
102 | + |
|
103 | + public function parse_line_item_list_template( |
|
104 | + $template, |
|
105 | + EE_Line_Item $line_item, |
|
106 | + $valid_shortcodes, |
|
107 | + $extra_data = array() |
|
108 | + ) { |
|
109 | + $this->_init_data($template, $line_item, $valid_shortcodes, $extra_data); |
|
110 | + $this->_template = is_array($template) ? $template['ticket_line_item_no_pms'] : $template; |
|
111 | + return $this->_parse_message_template(); |
|
112 | + } |
|
113 | + |
|
114 | + |
|
115 | + public function parse_payment_list_template( |
|
116 | + $template, |
|
117 | + EE_Payment $payment_item, |
|
118 | + $valid_shortcodes, |
|
119 | + $extra_data = array() |
|
120 | + ) { |
|
121 | + $this->_init_data($template, $payment_item, $valid_shortcodes, $extra_data); |
|
122 | + $this->_template = is_array($template) ? $template['payment_list'] : $template; |
|
123 | + return $this->_parse_message_template(); |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + public function parse_datetime_list_template( |
|
128 | + $template, |
|
129 | + EE_Datetime $datetime, |
|
130 | + $valid_shortcodes, |
|
131 | + $extra_data = array() |
|
132 | + ) { |
|
133 | + $this->_init_data($template, $datetime, $valid_shortcodes, $extra_data); |
|
134 | + $this->_template = is_array($template) ? $template['datetime_list'] : $template; |
|
135 | + return $this->_parse_message_template(); |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + public function parse_question_list_template($template, EE_Answer $answer, $valid_shortcodes, $extra_data = array()) |
|
140 | + { |
|
141 | + $this->_init_data($template, $answer, $valid_shortcodes, $extra_data); |
|
142 | + $this->_template = is_array($template) ? $template['question_list'] : $template; |
|
143 | + return $this->_parse_message_template(); |
|
144 | + } |
|
145 | + |
|
146 | + |
|
147 | + private function _init_data($template, $data, $valid_shortcodes, $extra_data = array()) |
|
148 | + { |
|
149 | + $this->_reset_props(); |
|
150 | + $this->_data['template'] = $template; |
|
151 | + $this->_data['data'] = $data; |
|
152 | + $this->_data['extra_data'] = $extra_data; |
|
153 | + $this->_set_shortcodes($valid_shortcodes); |
|
154 | + } |
|
155 | + |
|
156 | + |
|
157 | + private function _reset_props() |
|
158 | + { |
|
159 | + $this->_template = $this->_data = null; |
|
160 | + $this->_shortcode_objs = array(); |
|
161 | + } |
|
162 | + |
|
163 | + |
|
164 | + /** |
|
165 | + * takes the given template and parses it with the $_shortcodes property |
|
166 | + * |
|
167 | + * @access private |
|
168 | + * @return string |
|
169 | + */ |
|
170 | + private function _parse_message_template() |
|
171 | + { |
|
172 | + // now let's get a list of shortcodes that are found in the given template |
|
173 | + preg_match_all('/(\[.+?\])/', $this->_template, $matches); |
|
174 | + $shortcodes = (array) $matches[0]; // this should be an array of shortcodes in the template string. |
|
175 | + |
|
176 | + $matched_code = array(); |
|
177 | + $sc_values = array(); |
|
178 | + |
|
179 | + $list_type_shortcodes = array( |
|
180 | + '[ATTENDEE_LIST]', |
|
181 | + '[EVENT_LIST]', |
|
182 | + '[TICKET_LIST]', |
|
183 | + '[DATETIME_LIST]', |
|
184 | + '[QUESTION_LIST]', |
|
185 | + '[RECIPIENT_QUESTION_LIST]', |
|
186 | + '[PRIMARY_REGISTRANT_QUESTION_LIST]', |
|
187 | + '[RECIPIENT_TICKET_LIST]', |
|
188 | + '[PRIMARY_REGISTRANT_TICKET_LIST]', |
|
189 | + '[RECIPIENT_DATETIME_LIST]', |
|
190 | + '[PRIMARY_REGISTRANT_DATETIME_LIST]', |
|
191 | + '[TICKET_LINE_ITEM_LIST]', |
|
192 | + '[TAX_LINE_ITEM_LIST]', |
|
193 | + '[ADDITIONAL_LINE_ITEM_LIST]', |
|
194 | + '[PRICE_MODIFIER_LINE_ITEM_LIST]', |
|
195 | + '[PAYMENT_LIST_*]', |
|
196 | + ); |
|
197 | + |
|
198 | + $list_type_shortcodes = apply_filters( |
|
199 | + 'FHEE__EEH_Parse_Shortcodes___parse_message_template__list_type_shortcodes', |
|
200 | + $list_type_shortcodes |
|
201 | + ); |
|
202 | + |
|
203 | + // now lets go ahead and loop through our parsers for each shortcode and setup the values |
|
204 | + foreach ($shortcodes as $shortcode) { |
|
205 | + foreach ($this->_shortcode_objs as $sc_obj) { |
|
206 | + if ($sc_obj instanceof EE_Shortcodes) { |
|
207 | + // we need to setup any dynamic shortcodes so that they work with the array_key_exists |
|
208 | + preg_match_all('/(\[[A-Za-z0-9\_]+_\*)/', $shortcode, $matches); |
|
209 | + $sc_to_verify = ! empty($matches[0]) ? $matches[0][0] . ']' : $shortcode; |
|
210 | + |
|
211 | + if (! array_key_exists($sc_to_verify, $sc_obj->get_shortcodes())) { |
|
212 | + continue; // the given shortcode isn't in this object |
|
213 | + } |
|
214 | + |
|
215 | + // if this isn't a "list" type shortcode then we'll send along the data vanilla instead of in an array. |
|
216 | + if (! in_array($sc_to_verify, $list_type_shortcodes)) { |
|
217 | + $data_send = ! is_object($this->_data) && isset($this->_data['data']) ? $this->_data['data'] : $this->_data; |
|
218 | + } else { |
|
219 | + $data_send = $this->_data; |
|
220 | + } |
|
221 | + |
|
222 | + // is this a conditional type shortcode? If it is then we actually parse the template here. |
|
223 | + if ($this->_is_conditional_shortcode($shortcode)) { |
|
224 | + // most shortcode parsers are not going to have a match for this shortcode and will return an |
|
225 | + // empty string so we need to make sure that we're only replacing the template when there is a non empty string. |
|
226 | + $parsed = $sc_obj->parser($shortcode, $data_send, $this->_data['extra_data']); |
|
227 | + if ($parsed) { |
|
228 | + $this->_template = $parsed; |
|
229 | + } |
|
230 | + } |
|
231 | + |
|
232 | + $parsed = $sc_obj->parser($shortcode, $data_send, $this->_data['extra_data']); |
|
233 | + |
|
234 | + $matched_code[] = $shortcode; |
|
235 | + $sc_values[] = $parsed; |
|
236 | + } |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + // now we've got parsed values for all the shortcodes in the template so we can go ahead and swap the shortcodes out. |
|
241 | + return str_replace(array_values($matched_code), array_values($sc_values), $this->_template); |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + /** |
|
246 | + * Simply returns whether the given shortcode matches the structure for a conditional shortcode. |
|
247 | + * |
|
248 | + * Does it match this format: `[IF_` |
|
249 | + * |
|
250 | + * @param $shortcode |
|
251 | + */ |
|
252 | + protected function _is_conditional_shortcode($shortcode) |
|
253 | + { |
|
254 | + return strpos($shortcode, '[IF_') === 0; |
|
255 | + } |
|
256 | + |
|
257 | + |
|
258 | + /** |
|
259 | + * This sets the shortcodes property from the incoming array of valid shortcodes that corresponds to names of |
|
260 | + * various EE_Shortcode library objects |
|
261 | + * |
|
262 | + * @access private |
|
263 | + * @param array $valid_shortcodes an array of strings corresponding to EE_Shortcode Library objects |
|
264 | + * @return void |
|
265 | + */ |
|
266 | + private function _set_shortcodes($valid_shortcodes) |
|
267 | + { |
|
268 | + foreach ($valid_shortcodes as $shortcode_ref) { |
|
269 | + $ref = ucwords(str_replace('_', ' ', $shortcode_ref)); |
|
270 | + $ref = str_replace(' ', '_', $ref); |
|
271 | + $classname = 'EE_' . $ref . '_Shortcodes'; |
|
272 | + if (class_exists($classname)) { |
|
273 | + $this->_shortcode_objs[] = new $classname(); |
|
274 | + } |
|
275 | + } |
|
276 | + } |
|
277 | 277 | } |
@@ -17,340 +17,340 @@ |
||
17 | 17 | */ |
18 | 18 | class CustomSelects |
19 | 19 | { |
20 | - const TYPE_SIMPLE = 'simple'; |
|
21 | - const TYPE_COMPLEX = 'complex'; |
|
22 | - const TYPE_STRUCTURED = 'structured'; |
|
23 | - |
|
24 | - private $valid_operators = array('COUNT', 'SUM'); |
|
25 | - |
|
26 | - |
|
27 | - /** |
|
28 | - * Original incoming select array |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - private $original_selects; |
|
33 | - |
|
34 | - /** |
|
35 | - * Select string that can be added to the query |
|
36 | - * |
|
37 | - * @var string |
|
38 | - */ |
|
39 | - private $columns_to_select_expression; |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * An array of aliases for the columns included in the incoming select array. |
|
44 | - * |
|
45 | - * @var array |
|
46 | - */ |
|
47 | - private $column_aliases_in_select; |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Enum representation of the "type" of array coming into this value object. |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - private $type = ''; |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * CustomSelects constructor. |
|
60 | - * Incoming selects can be in one of the following formats: |
|
61 | - * ---- self::TYPE_SIMPLE array ---- |
|
62 | - * This is considered the "simple" type. In this case the array is an numerically indexed array with single or |
|
63 | - * multiple columns to select as the values. |
|
64 | - * eg. array( 'ATT_ID', 'REG_ID' ) |
|
65 | - * eg. array( '*' ) |
|
66 | - * If you want to use the columns in any WHERE, GROUP BY, or HAVING clauses, you must instead use the "complex" or |
|
67 | - * "structured" method. |
|
68 | - * ---- self::TYPE_COMPLEX array ---- |
|
69 | - * This is considered the "complex" type. In this case the array is indexed by arbitrary strings that serve as |
|
70 | - * column alias, and the value is an numerically indexed array where there are two values. The first value (0) is |
|
71 | - * the selection and the second value (1) is the data type. Data types must be one of the types defined in |
|
72 | - * EEM_Base::$_valid_wpdb_data_types. |
|
73 | - * eg. array( 'count' => array('count(REG_ID)', '%d') ) |
|
74 | - * Complex array configuration allows for using the column alias in any WHERE, GROUP BY, or HAVING clauses. |
|
75 | - * ---- self::TYPE_STRUCTURED array --- |
|
76 | - * This is considered the "structured" type. This type is similar to the complex type except that the array attached |
|
77 | - * to the column alias contains three values. The first value is the qualified column name (which can include |
|
78 | - * join syntax for models). The second value is the operator performed on the column (i.e. 'COUNT', 'SUM' etc)., |
|
79 | - * the third value is the data type. Note, if the select does not have an operator, you can use an empty string for |
|
80 | - * the second value. |
|
81 | - * Note: for now SUM is only for simple single column expressions (i.e. SUM(Transaction.TXN_total)) |
|
82 | - * eg. array( 'registration_count' => array('Registration.REG_ID', 'count', '%d') ); |
|
83 | - * NOTE: mixing array types in the incoming $select will cause errors. |
|
84 | - * |
|
85 | - * @param array $selects |
|
86 | - * @throws InvalidArgumentException |
|
87 | - */ |
|
88 | - public function __construct(array $selects) |
|
89 | - { |
|
90 | - $this->original_selects = $selects; |
|
91 | - $this->deriveType($selects); |
|
92 | - $this->deriveParts($selects); |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * Derives what type of custom select has been sent in. |
|
98 | - * |
|
99 | - * @param array $selects |
|
100 | - * @throws InvalidArgumentException |
|
101 | - */ |
|
102 | - private function deriveType(array $selects) |
|
103 | - { |
|
104 | - // first if the first key for this array is an integer then its coming in as a simple format, so we'll also |
|
105 | - // ensure all elements of the array are simple. |
|
106 | - if (is_int(key($selects))) { |
|
107 | - // let's ensure all keys are ints |
|
108 | - $invalid_keys = array_filter( |
|
109 | - array_keys($selects), |
|
110 | - function ($value) { |
|
111 | - return ! is_int($value); |
|
112 | - } |
|
113 | - ); |
|
114 | - if (! empty($invalid_keys)) { |
|
115 | - throw new InvalidArgumentException( |
|
116 | - sprintf( |
|
117 | - esc_html__( |
|
118 | - 'Incoming array looks like its formatted for "%1$s" type selects, however it has elements that are not indexed numerically', |
|
119 | - 'event_espresso' |
|
120 | - ), |
|
121 | - self::TYPE_SIMPLE |
|
122 | - ) |
|
123 | - ); |
|
124 | - } |
|
125 | - $this->type = self::TYPE_SIMPLE; |
|
126 | - return; |
|
127 | - } |
|
128 | - // made it here so that means we've got either complex or structured selects. Let's find out which by popping |
|
129 | - // the first array element off. |
|
130 | - $first_element = reset($selects); |
|
131 | - |
|
132 | - if (! is_array($first_element)) { |
|
133 | - throw new InvalidArgumentException( |
|
134 | - sprintf( |
|
135 | - esc_html__( |
|
136 | - 'Incoming array looks like its formatted as a "%1$s" or "%2$s" type. However, the values in the array must be arrays themselves and they are not.', |
|
137 | - 'event_espresso' |
|
138 | - ), |
|
139 | - self::TYPE_COMPLEX, |
|
140 | - self::TYPE_STRUCTURED |
|
141 | - ) |
|
142 | - ); |
|
143 | - } |
|
144 | - $this->type = count($first_element) === 2 |
|
145 | - ? self::TYPE_COMPLEX |
|
146 | - : self::TYPE_STRUCTURED; |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * Sets up the various properties for the vo depending on type. |
|
152 | - * |
|
153 | - * @param array $selects |
|
154 | - * @throws InvalidArgumentException |
|
155 | - */ |
|
156 | - private function deriveParts(array $selects) |
|
157 | - { |
|
158 | - $column_parts = array(); |
|
159 | - switch ($this->type) { |
|
160 | - case self::TYPE_SIMPLE: |
|
161 | - $column_parts = $selects; |
|
162 | - $this->column_aliases_in_select = $selects; |
|
163 | - break; |
|
164 | - case self::TYPE_COMPLEX: |
|
165 | - foreach ($selects as $alias => $parts) { |
|
166 | - $this->validateSelectValueForType($parts, $alias); |
|
167 | - $column_parts[] = "{$parts[0]} AS {$alias}"; |
|
168 | - $this->column_aliases_in_select[] = $alias; |
|
169 | - } |
|
170 | - break; |
|
171 | - case self::TYPE_STRUCTURED: |
|
172 | - foreach ($selects as $alias => $parts) { |
|
173 | - $this->validateSelectValueForType($parts, $alias); |
|
174 | - $column_parts[] = $parts[1] !== '' |
|
175 | - ? $this->assembleSelectStringWithOperator($parts, $alias) |
|
176 | - : "{$parts[0]} AS {$alias}"; |
|
177 | - $this->column_aliases_in_select[] = $alias; |
|
178 | - } |
|
179 | - break; |
|
180 | - } |
|
181 | - $this->columns_to_select_expression = implode(', ', $column_parts); |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - /** |
|
186 | - * Validates self::TYPE_COMPLEX and self::TYPE_STRUCTURED select statement parts. |
|
187 | - * |
|
188 | - * @param array $select_parts |
|
189 | - * @param string $alias |
|
190 | - * @throws InvalidArgumentException |
|
191 | - */ |
|
192 | - private function validateSelectValueForType(array $select_parts, $alias) |
|
193 | - { |
|
194 | - $valid_data_types = array('%d', '%s', '%f'); |
|
195 | - if (count($select_parts) !== $this->expectedSelectPartCountForType()) { |
|
196 | - throw new InvalidArgumentException( |
|
197 | - sprintf( |
|
198 | - esc_html__( |
|
199 | - 'The provided select part array for the %1$s column is expected to have a count of %2$d because the incoming select array is of type %3$s. However the count was %4$d.', |
|
200 | - 'event_espresso' |
|
201 | - ), |
|
202 | - $alias, |
|
203 | - $this->expectedSelectPartCountForType(), |
|
204 | - $this->type, |
|
205 | - count($select_parts) |
|
206 | - ) |
|
207 | - ); |
|
208 | - } |
|
209 | - // validate data type. |
|
210 | - $data_type = $this->type === self::TYPE_COMPLEX ? $select_parts[1] : ''; |
|
211 | - $data_type = $this->type === self::TYPE_STRUCTURED ? $select_parts[2] : $data_type; |
|
212 | - |
|
213 | - if (! in_array($data_type, $valid_data_types, true)) { |
|
214 | - throw new InvalidArgumentException( |
|
215 | - sprintf( |
|
216 | - esc_html__( |
|
217 | - 'Datatype %1$s (for selection "%2$s" and alias "%3$s") is not a valid wpdb datatype (eg %%s)', |
|
218 | - 'event_espresso' |
|
219 | - ), |
|
220 | - $data_type, |
|
221 | - $select_parts[0], |
|
222 | - $alias, |
|
223 | - implode(', ', $valid_data_types) |
|
224 | - ) |
|
225 | - ); |
|
226 | - } |
|
227 | - } |
|
228 | - |
|
229 | - |
|
230 | - /** |
|
231 | - * Each type will have an expected count of array elements, this returns what that expected count is. |
|
232 | - * |
|
233 | - * @param string $type |
|
234 | - * @return int |
|
235 | - */ |
|
236 | - private function expectedSelectPartCountForType($type = '') |
|
237 | - { |
|
238 | - $type = $type === '' ? $this->type : $type; |
|
239 | - $types_count_map = array( |
|
240 | - self::TYPE_COMPLEX => 2, |
|
241 | - self::TYPE_STRUCTURED => 3, |
|
242 | - ); |
|
243 | - return isset($types_count_map[ $type ]) ? $types_count_map[ $type ] : 0; |
|
244 | - } |
|
245 | - |
|
246 | - |
|
247 | - /** |
|
248 | - * Prepares the select statement part for for structured type selects. |
|
249 | - * |
|
250 | - * @param array $select_parts |
|
251 | - * @param string $alias |
|
252 | - * @return string |
|
253 | - * @throws InvalidArgumentException |
|
254 | - */ |
|
255 | - private function assembleSelectStringWithOperator(array $select_parts, $alias) |
|
256 | - { |
|
257 | - $operator = strtoupper($select_parts[1]); |
|
258 | - // validate operator |
|
259 | - if (! in_array($operator, $this->valid_operators, true)) { |
|
260 | - throw new InvalidArgumentException( |
|
261 | - sprintf( |
|
262 | - esc_html__( |
|
263 | - 'An invalid operator has been provided (%1$s) for the column %2$s. Valid operators must be one of the following: %3$s.', |
|
264 | - 'event_espresso' |
|
265 | - ), |
|
266 | - $operator, |
|
267 | - $alias, |
|
268 | - implode(', ', $this->valid_operators) |
|
269 | - ) |
|
270 | - ); |
|
271 | - } |
|
272 | - return $operator . '(' . $select_parts[0] . ') AS ' . $alias; |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * Return the datatype from the given select part. |
|
278 | - * Remember the select_part has already been validated on object instantiation. |
|
279 | - * |
|
280 | - * @param array $select_part |
|
281 | - * @return string |
|
282 | - */ |
|
283 | - private function getDataTypeForSelectType(array $select_part) |
|
284 | - { |
|
285 | - switch ($this->type) { |
|
286 | - case self::TYPE_COMPLEX: |
|
287 | - return $select_part[1]; |
|
288 | - case self::TYPE_STRUCTURED: |
|
289 | - return $select_part[2]; |
|
290 | - default: |
|
291 | - return ''; |
|
292 | - } |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * Returns the original select array sent into the VO. |
|
298 | - * |
|
299 | - * @return array |
|
300 | - */ |
|
301 | - public function originalSelects() |
|
302 | - { |
|
303 | - return $this->original_selects; |
|
304 | - } |
|
305 | - |
|
306 | - |
|
307 | - /** |
|
308 | - * Returns the final assembled select expression derived from the incoming select array. |
|
309 | - * |
|
310 | - * @return string |
|
311 | - */ |
|
312 | - public function columnsToSelectExpression() |
|
313 | - { |
|
314 | - return $this->columns_to_select_expression; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - /** |
|
319 | - * Returns all the column aliases derived from the incoming select array. |
|
320 | - * |
|
321 | - * @return array |
|
322 | - */ |
|
323 | - public function columnAliases() |
|
324 | - { |
|
325 | - return $this->column_aliases_in_select; |
|
326 | - } |
|
327 | - |
|
328 | - |
|
329 | - /** |
|
330 | - * Returns the enum type for the incoming select array. |
|
331 | - * |
|
332 | - * @return string |
|
333 | - */ |
|
334 | - public function type() |
|
335 | - { |
|
336 | - return $this->type; |
|
337 | - } |
|
338 | - |
|
339 | - |
|
340 | - /** |
|
341 | - * Return the datatype for the given column_alias |
|
342 | - * |
|
343 | - * @param string $column_alias |
|
344 | - * @return string (if there's no data type we return string as the default). |
|
345 | - */ |
|
346 | - public function getDataTypeForAlias($column_alias) |
|
347 | - { |
|
348 | - if ( |
|
349 | - isset($this->original_selects[ $column_alias ]) |
|
350 | - && in_array($column_alias, $this->columnAliases(), true) |
|
351 | - ) { |
|
352 | - return $this->getDataTypeForSelectType($this->original_selects[ $column_alias ]); |
|
353 | - } |
|
354 | - return '%s'; |
|
355 | - } |
|
20 | + const TYPE_SIMPLE = 'simple'; |
|
21 | + const TYPE_COMPLEX = 'complex'; |
|
22 | + const TYPE_STRUCTURED = 'structured'; |
|
23 | + |
|
24 | + private $valid_operators = array('COUNT', 'SUM'); |
|
25 | + |
|
26 | + |
|
27 | + /** |
|
28 | + * Original incoming select array |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + private $original_selects; |
|
33 | + |
|
34 | + /** |
|
35 | + * Select string that can be added to the query |
|
36 | + * |
|
37 | + * @var string |
|
38 | + */ |
|
39 | + private $columns_to_select_expression; |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * An array of aliases for the columns included in the incoming select array. |
|
44 | + * |
|
45 | + * @var array |
|
46 | + */ |
|
47 | + private $column_aliases_in_select; |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Enum representation of the "type" of array coming into this value object. |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + private $type = ''; |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * CustomSelects constructor. |
|
60 | + * Incoming selects can be in one of the following formats: |
|
61 | + * ---- self::TYPE_SIMPLE array ---- |
|
62 | + * This is considered the "simple" type. In this case the array is an numerically indexed array with single or |
|
63 | + * multiple columns to select as the values. |
|
64 | + * eg. array( 'ATT_ID', 'REG_ID' ) |
|
65 | + * eg. array( '*' ) |
|
66 | + * If you want to use the columns in any WHERE, GROUP BY, or HAVING clauses, you must instead use the "complex" or |
|
67 | + * "structured" method. |
|
68 | + * ---- self::TYPE_COMPLEX array ---- |
|
69 | + * This is considered the "complex" type. In this case the array is indexed by arbitrary strings that serve as |
|
70 | + * column alias, and the value is an numerically indexed array where there are two values. The first value (0) is |
|
71 | + * the selection and the second value (1) is the data type. Data types must be one of the types defined in |
|
72 | + * EEM_Base::$_valid_wpdb_data_types. |
|
73 | + * eg. array( 'count' => array('count(REG_ID)', '%d') ) |
|
74 | + * Complex array configuration allows for using the column alias in any WHERE, GROUP BY, or HAVING clauses. |
|
75 | + * ---- self::TYPE_STRUCTURED array --- |
|
76 | + * This is considered the "structured" type. This type is similar to the complex type except that the array attached |
|
77 | + * to the column alias contains three values. The first value is the qualified column name (which can include |
|
78 | + * join syntax for models). The second value is the operator performed on the column (i.e. 'COUNT', 'SUM' etc)., |
|
79 | + * the third value is the data type. Note, if the select does not have an operator, you can use an empty string for |
|
80 | + * the second value. |
|
81 | + * Note: for now SUM is only for simple single column expressions (i.e. SUM(Transaction.TXN_total)) |
|
82 | + * eg. array( 'registration_count' => array('Registration.REG_ID', 'count', '%d') ); |
|
83 | + * NOTE: mixing array types in the incoming $select will cause errors. |
|
84 | + * |
|
85 | + * @param array $selects |
|
86 | + * @throws InvalidArgumentException |
|
87 | + */ |
|
88 | + public function __construct(array $selects) |
|
89 | + { |
|
90 | + $this->original_selects = $selects; |
|
91 | + $this->deriveType($selects); |
|
92 | + $this->deriveParts($selects); |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * Derives what type of custom select has been sent in. |
|
98 | + * |
|
99 | + * @param array $selects |
|
100 | + * @throws InvalidArgumentException |
|
101 | + */ |
|
102 | + private function deriveType(array $selects) |
|
103 | + { |
|
104 | + // first if the first key for this array is an integer then its coming in as a simple format, so we'll also |
|
105 | + // ensure all elements of the array are simple. |
|
106 | + if (is_int(key($selects))) { |
|
107 | + // let's ensure all keys are ints |
|
108 | + $invalid_keys = array_filter( |
|
109 | + array_keys($selects), |
|
110 | + function ($value) { |
|
111 | + return ! is_int($value); |
|
112 | + } |
|
113 | + ); |
|
114 | + if (! empty($invalid_keys)) { |
|
115 | + throw new InvalidArgumentException( |
|
116 | + sprintf( |
|
117 | + esc_html__( |
|
118 | + 'Incoming array looks like its formatted for "%1$s" type selects, however it has elements that are not indexed numerically', |
|
119 | + 'event_espresso' |
|
120 | + ), |
|
121 | + self::TYPE_SIMPLE |
|
122 | + ) |
|
123 | + ); |
|
124 | + } |
|
125 | + $this->type = self::TYPE_SIMPLE; |
|
126 | + return; |
|
127 | + } |
|
128 | + // made it here so that means we've got either complex or structured selects. Let's find out which by popping |
|
129 | + // the first array element off. |
|
130 | + $first_element = reset($selects); |
|
131 | + |
|
132 | + if (! is_array($first_element)) { |
|
133 | + throw new InvalidArgumentException( |
|
134 | + sprintf( |
|
135 | + esc_html__( |
|
136 | + 'Incoming array looks like its formatted as a "%1$s" or "%2$s" type. However, the values in the array must be arrays themselves and they are not.', |
|
137 | + 'event_espresso' |
|
138 | + ), |
|
139 | + self::TYPE_COMPLEX, |
|
140 | + self::TYPE_STRUCTURED |
|
141 | + ) |
|
142 | + ); |
|
143 | + } |
|
144 | + $this->type = count($first_element) === 2 |
|
145 | + ? self::TYPE_COMPLEX |
|
146 | + : self::TYPE_STRUCTURED; |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * Sets up the various properties for the vo depending on type. |
|
152 | + * |
|
153 | + * @param array $selects |
|
154 | + * @throws InvalidArgumentException |
|
155 | + */ |
|
156 | + private function deriveParts(array $selects) |
|
157 | + { |
|
158 | + $column_parts = array(); |
|
159 | + switch ($this->type) { |
|
160 | + case self::TYPE_SIMPLE: |
|
161 | + $column_parts = $selects; |
|
162 | + $this->column_aliases_in_select = $selects; |
|
163 | + break; |
|
164 | + case self::TYPE_COMPLEX: |
|
165 | + foreach ($selects as $alias => $parts) { |
|
166 | + $this->validateSelectValueForType($parts, $alias); |
|
167 | + $column_parts[] = "{$parts[0]} AS {$alias}"; |
|
168 | + $this->column_aliases_in_select[] = $alias; |
|
169 | + } |
|
170 | + break; |
|
171 | + case self::TYPE_STRUCTURED: |
|
172 | + foreach ($selects as $alias => $parts) { |
|
173 | + $this->validateSelectValueForType($parts, $alias); |
|
174 | + $column_parts[] = $parts[1] !== '' |
|
175 | + ? $this->assembleSelectStringWithOperator($parts, $alias) |
|
176 | + : "{$parts[0]} AS {$alias}"; |
|
177 | + $this->column_aliases_in_select[] = $alias; |
|
178 | + } |
|
179 | + break; |
|
180 | + } |
|
181 | + $this->columns_to_select_expression = implode(', ', $column_parts); |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + /** |
|
186 | + * Validates self::TYPE_COMPLEX and self::TYPE_STRUCTURED select statement parts. |
|
187 | + * |
|
188 | + * @param array $select_parts |
|
189 | + * @param string $alias |
|
190 | + * @throws InvalidArgumentException |
|
191 | + */ |
|
192 | + private function validateSelectValueForType(array $select_parts, $alias) |
|
193 | + { |
|
194 | + $valid_data_types = array('%d', '%s', '%f'); |
|
195 | + if (count($select_parts) !== $this->expectedSelectPartCountForType()) { |
|
196 | + throw new InvalidArgumentException( |
|
197 | + sprintf( |
|
198 | + esc_html__( |
|
199 | + 'The provided select part array for the %1$s column is expected to have a count of %2$d because the incoming select array is of type %3$s. However the count was %4$d.', |
|
200 | + 'event_espresso' |
|
201 | + ), |
|
202 | + $alias, |
|
203 | + $this->expectedSelectPartCountForType(), |
|
204 | + $this->type, |
|
205 | + count($select_parts) |
|
206 | + ) |
|
207 | + ); |
|
208 | + } |
|
209 | + // validate data type. |
|
210 | + $data_type = $this->type === self::TYPE_COMPLEX ? $select_parts[1] : ''; |
|
211 | + $data_type = $this->type === self::TYPE_STRUCTURED ? $select_parts[2] : $data_type; |
|
212 | + |
|
213 | + if (! in_array($data_type, $valid_data_types, true)) { |
|
214 | + throw new InvalidArgumentException( |
|
215 | + sprintf( |
|
216 | + esc_html__( |
|
217 | + 'Datatype %1$s (for selection "%2$s" and alias "%3$s") is not a valid wpdb datatype (eg %%s)', |
|
218 | + 'event_espresso' |
|
219 | + ), |
|
220 | + $data_type, |
|
221 | + $select_parts[0], |
|
222 | + $alias, |
|
223 | + implode(', ', $valid_data_types) |
|
224 | + ) |
|
225 | + ); |
|
226 | + } |
|
227 | + } |
|
228 | + |
|
229 | + |
|
230 | + /** |
|
231 | + * Each type will have an expected count of array elements, this returns what that expected count is. |
|
232 | + * |
|
233 | + * @param string $type |
|
234 | + * @return int |
|
235 | + */ |
|
236 | + private function expectedSelectPartCountForType($type = '') |
|
237 | + { |
|
238 | + $type = $type === '' ? $this->type : $type; |
|
239 | + $types_count_map = array( |
|
240 | + self::TYPE_COMPLEX => 2, |
|
241 | + self::TYPE_STRUCTURED => 3, |
|
242 | + ); |
|
243 | + return isset($types_count_map[ $type ]) ? $types_count_map[ $type ] : 0; |
|
244 | + } |
|
245 | + |
|
246 | + |
|
247 | + /** |
|
248 | + * Prepares the select statement part for for structured type selects. |
|
249 | + * |
|
250 | + * @param array $select_parts |
|
251 | + * @param string $alias |
|
252 | + * @return string |
|
253 | + * @throws InvalidArgumentException |
|
254 | + */ |
|
255 | + private function assembleSelectStringWithOperator(array $select_parts, $alias) |
|
256 | + { |
|
257 | + $operator = strtoupper($select_parts[1]); |
|
258 | + // validate operator |
|
259 | + if (! in_array($operator, $this->valid_operators, true)) { |
|
260 | + throw new InvalidArgumentException( |
|
261 | + sprintf( |
|
262 | + esc_html__( |
|
263 | + 'An invalid operator has been provided (%1$s) for the column %2$s. Valid operators must be one of the following: %3$s.', |
|
264 | + 'event_espresso' |
|
265 | + ), |
|
266 | + $operator, |
|
267 | + $alias, |
|
268 | + implode(', ', $this->valid_operators) |
|
269 | + ) |
|
270 | + ); |
|
271 | + } |
|
272 | + return $operator . '(' . $select_parts[0] . ') AS ' . $alias; |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * Return the datatype from the given select part. |
|
278 | + * Remember the select_part has already been validated on object instantiation. |
|
279 | + * |
|
280 | + * @param array $select_part |
|
281 | + * @return string |
|
282 | + */ |
|
283 | + private function getDataTypeForSelectType(array $select_part) |
|
284 | + { |
|
285 | + switch ($this->type) { |
|
286 | + case self::TYPE_COMPLEX: |
|
287 | + return $select_part[1]; |
|
288 | + case self::TYPE_STRUCTURED: |
|
289 | + return $select_part[2]; |
|
290 | + default: |
|
291 | + return ''; |
|
292 | + } |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * Returns the original select array sent into the VO. |
|
298 | + * |
|
299 | + * @return array |
|
300 | + */ |
|
301 | + public function originalSelects() |
|
302 | + { |
|
303 | + return $this->original_selects; |
|
304 | + } |
|
305 | + |
|
306 | + |
|
307 | + /** |
|
308 | + * Returns the final assembled select expression derived from the incoming select array. |
|
309 | + * |
|
310 | + * @return string |
|
311 | + */ |
|
312 | + public function columnsToSelectExpression() |
|
313 | + { |
|
314 | + return $this->columns_to_select_expression; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + /** |
|
319 | + * Returns all the column aliases derived from the incoming select array. |
|
320 | + * |
|
321 | + * @return array |
|
322 | + */ |
|
323 | + public function columnAliases() |
|
324 | + { |
|
325 | + return $this->column_aliases_in_select; |
|
326 | + } |
|
327 | + |
|
328 | + |
|
329 | + /** |
|
330 | + * Returns the enum type for the incoming select array. |
|
331 | + * |
|
332 | + * @return string |
|
333 | + */ |
|
334 | + public function type() |
|
335 | + { |
|
336 | + return $this->type; |
|
337 | + } |
|
338 | + |
|
339 | + |
|
340 | + /** |
|
341 | + * Return the datatype for the given column_alias |
|
342 | + * |
|
343 | + * @param string $column_alias |
|
344 | + * @return string (if there's no data type we return string as the default). |
|
345 | + */ |
|
346 | + public function getDataTypeForAlias($column_alias) |
|
347 | + { |
|
348 | + if ( |
|
349 | + isset($this->original_selects[ $column_alias ]) |
|
350 | + && in_array($column_alias, $this->columnAliases(), true) |
|
351 | + ) { |
|
352 | + return $this->getDataTypeForSelectType($this->original_selects[ $column_alias ]); |
|
353 | + } |
|
354 | + return '%s'; |
|
355 | + } |
|
356 | 356 | } |
@@ -107,11 +107,11 @@ discard block |
||
107 | 107 | // let's ensure all keys are ints |
108 | 108 | $invalid_keys = array_filter( |
109 | 109 | array_keys($selects), |
110 | - function ($value) { |
|
110 | + function($value) { |
|
111 | 111 | return ! is_int($value); |
112 | 112 | } |
113 | 113 | ); |
114 | - if (! empty($invalid_keys)) { |
|
114 | + if ( ! empty($invalid_keys)) { |
|
115 | 115 | throw new InvalidArgumentException( |
116 | 116 | sprintf( |
117 | 117 | esc_html__( |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | // the first array element off. |
130 | 130 | $first_element = reset($selects); |
131 | 131 | |
132 | - if (! is_array($first_element)) { |
|
132 | + if ( ! is_array($first_element)) { |
|
133 | 133 | throw new InvalidArgumentException( |
134 | 134 | sprintf( |
135 | 135 | esc_html__( |
@@ -210,7 +210,7 @@ discard block |
||
210 | 210 | $data_type = $this->type === self::TYPE_COMPLEX ? $select_parts[1] : ''; |
211 | 211 | $data_type = $this->type === self::TYPE_STRUCTURED ? $select_parts[2] : $data_type; |
212 | 212 | |
213 | - if (! in_array($data_type, $valid_data_types, true)) { |
|
213 | + if ( ! in_array($data_type, $valid_data_types, true)) { |
|
214 | 214 | throw new InvalidArgumentException( |
215 | 215 | sprintf( |
216 | 216 | esc_html__( |
@@ -240,7 +240,7 @@ discard block |
||
240 | 240 | self::TYPE_COMPLEX => 2, |
241 | 241 | self::TYPE_STRUCTURED => 3, |
242 | 242 | ); |
243 | - return isset($types_count_map[ $type ]) ? $types_count_map[ $type ] : 0; |
|
243 | + return isset($types_count_map[$type]) ? $types_count_map[$type] : 0; |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | { |
257 | 257 | $operator = strtoupper($select_parts[1]); |
258 | 258 | // validate operator |
259 | - if (! in_array($operator, $this->valid_operators, true)) { |
|
259 | + if ( ! in_array($operator, $this->valid_operators, true)) { |
|
260 | 260 | throw new InvalidArgumentException( |
261 | 261 | sprintf( |
262 | 262 | esc_html__( |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | ) |
270 | 270 | ); |
271 | 271 | } |
272 | - return $operator . '(' . $select_parts[0] . ') AS ' . $alias; |
|
272 | + return $operator.'('.$select_parts[0].') AS '.$alias; |
|
273 | 273 | } |
274 | 274 | |
275 | 275 | |
@@ -346,10 +346,10 @@ discard block |
||
346 | 346 | public function getDataTypeForAlias($column_alias) |
347 | 347 | { |
348 | 348 | if ( |
349 | - isset($this->original_selects[ $column_alias ]) |
|
349 | + isset($this->original_selects[$column_alias]) |
|
350 | 350 | && in_array($column_alias, $this->columnAliases(), true) |
351 | 351 | ) { |
352 | - return $this->getDataTypeForSelectType($this->original_selects[ $column_alias ]); |
|
352 | + return $this->getDataTypeForSelectType($this->original_selects[$column_alias]); |
|
353 | 353 | } |
354 | 354 | return '%s'; |
355 | 355 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | $data = []; |
81 | 81 | if (empty($this->countries)) { |
82 | 82 | $this->data_version = $this->getCountrySubRegionDataVersion(); |
83 | - $data = $this->retrieveJsonData(self::REPO_URL . 'countries.json'); |
|
83 | + $data = $this->retrieveJsonData(self::REPO_URL.'countries.json'); |
|
84 | 84 | } |
85 | 85 | if (empty($data)) { |
86 | 86 | EE_Error::add_error( |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | */ |
145 | 145 | private function processCountryData($CNT_ISO, $countries = array()) |
146 | 146 | { |
147 | - if (! empty($countries)) { |
|
147 | + if ( ! empty($countries)) { |
|
148 | 148 | foreach ($countries as $key => $country) { |
149 | 149 | if ( |
150 | 150 | $country instanceof stdClass |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | && ! empty($country->filename) |
154 | 154 | ) { |
155 | 155 | $country->sub_regions = $this->retrieveJsonData( |
156 | - self::REPO_URL . 'countries/' . $country->filename . '.json' |
|
156 | + self::REPO_URL.'countries/'.$country->filename.'.json' |
|
157 | 157 | ); |
158 | 158 | return $this->saveSubRegionData($country, $country->sub_regions); |
159 | 159 | } |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | foreach ($sub_regions as $sub_region) { |
216 | 216 | // remove country code from sub region code |
217 | 217 | $abbrev = str_replace( |
218 | - $country->code . '-', |
|
218 | + $country->code.'-', |
|
219 | 219 | '', |
220 | 220 | sanitize_text_field($sub_region->code) |
221 | 221 | ); |
@@ -25,252 +25,252 @@ |
||
25 | 25 | */ |
26 | 26 | class CountrySubRegionDao |
27 | 27 | { |
28 | - const REPO_URL = 'https://raw.githubusercontent.com/eventespresso/countries-and-subregions/master/'; |
|
28 | + const REPO_URL = 'https://raw.githubusercontent.com/eventespresso/countries-and-subregions/master/'; |
|
29 | 29 | |
30 | - const OPTION_NAME_COUNTRY_DATA_VERSION = 'espresso-country-sub-region-data-version'; |
|
30 | + const OPTION_NAME_COUNTRY_DATA_VERSION = 'espresso-country-sub-region-data-version'; |
|
31 | 31 | |
32 | - /** |
|
33 | - * @var EEM_State $state_model |
|
34 | - */ |
|
35 | - private $state_model; |
|
32 | + /** |
|
33 | + * @var EEM_State $state_model |
|
34 | + */ |
|
35 | + private $state_model; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @var JsonValidator $json_validator |
|
39 | - */ |
|
40 | - private $json_validator; |
|
37 | + /** |
|
38 | + * @var JsonValidator $json_validator |
|
39 | + */ |
|
40 | + private $json_validator; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @var string $data_version |
|
44 | - */ |
|
45 | - private $data_version; |
|
42 | + /** |
|
43 | + * @var string $data_version |
|
44 | + */ |
|
45 | + private $data_version; |
|
46 | 46 | |
47 | - /** |
|
48 | - * @var array $countries |
|
49 | - */ |
|
50 | - private $countries = array(); |
|
47 | + /** |
|
48 | + * @var array $countries |
|
49 | + */ |
|
50 | + private $countries = array(); |
|
51 | 51 | |
52 | 52 | |
53 | - /** |
|
54 | - * CountrySubRegionDao constructor. |
|
55 | - * |
|
56 | - * @param EEM_State $state_model |
|
57 | - * @param JsonValidator $json_validator |
|
58 | - */ |
|
59 | - public function __construct(EEM_State $state_model, JsonValidator $json_validator) |
|
60 | - { |
|
61 | - $this->state_model = $state_model; |
|
62 | - $this->json_validator = $json_validator; |
|
63 | - } |
|
53 | + /** |
|
54 | + * CountrySubRegionDao constructor. |
|
55 | + * |
|
56 | + * @param EEM_State $state_model |
|
57 | + * @param JsonValidator $json_validator |
|
58 | + */ |
|
59 | + public function __construct(EEM_State $state_model, JsonValidator $json_validator) |
|
60 | + { |
|
61 | + $this->state_model = $state_model; |
|
62 | + $this->json_validator = $json_validator; |
|
63 | + } |
|
64 | 64 | |
65 | 65 | |
66 | - /** |
|
67 | - * @param EE_Country $country_object |
|
68 | - * @return bool |
|
69 | - * @throws EE_Error |
|
70 | - * @throws InvalidArgumentException |
|
71 | - * @throws InvalidDataTypeException |
|
72 | - * @throws InvalidInterfaceException |
|
73 | - * @throws ReflectionException |
|
74 | - */ |
|
75 | - public function saveCountrySubRegions(EE_Country $country_object) |
|
76 | - { |
|
77 | - $CNT_ISO = $country_object->ID(); |
|
78 | - $has_sub_regions = $this->state_model->count(array(array('Country.CNT_ISO' => $CNT_ISO))); |
|
79 | - $data = []; |
|
80 | - if (empty($this->countries)) { |
|
81 | - $this->data_version = $this->getCountrySubRegionDataVersion(); |
|
82 | - $data = $this->retrieveJsonData(self::REPO_URL . 'countries.json'); |
|
83 | - } |
|
84 | - if (empty($data)) { |
|
85 | - EE_Error::add_error( |
|
86 | - 'Country Subregion Data could not be retrieved', |
|
87 | - __FILE__, |
|
88 | - __METHOD__, |
|
89 | - __LINE__ |
|
90 | - ); |
|
91 | - } |
|
92 | - if ( |
|
93 | - ! $has_sub_regions |
|
94 | - || (isset($data->version) && version_compare($data->version, $this->data_version)) |
|
95 | - ) { |
|
96 | - if ( |
|
97 | - isset($data->countries) |
|
98 | - && $this->processCountryData($CNT_ISO, $data->countries) > 0 |
|
99 | - ) { |
|
100 | - $this->countries = $data->countries; |
|
101 | - $this->updateCountrySubRegionDataVersion($data->version); |
|
102 | - return true; |
|
103 | - } |
|
104 | - } |
|
105 | - return false; |
|
106 | - } |
|
66 | + /** |
|
67 | + * @param EE_Country $country_object |
|
68 | + * @return bool |
|
69 | + * @throws EE_Error |
|
70 | + * @throws InvalidArgumentException |
|
71 | + * @throws InvalidDataTypeException |
|
72 | + * @throws InvalidInterfaceException |
|
73 | + * @throws ReflectionException |
|
74 | + */ |
|
75 | + public function saveCountrySubRegions(EE_Country $country_object) |
|
76 | + { |
|
77 | + $CNT_ISO = $country_object->ID(); |
|
78 | + $has_sub_regions = $this->state_model->count(array(array('Country.CNT_ISO' => $CNT_ISO))); |
|
79 | + $data = []; |
|
80 | + if (empty($this->countries)) { |
|
81 | + $this->data_version = $this->getCountrySubRegionDataVersion(); |
|
82 | + $data = $this->retrieveJsonData(self::REPO_URL . 'countries.json'); |
|
83 | + } |
|
84 | + if (empty($data)) { |
|
85 | + EE_Error::add_error( |
|
86 | + 'Country Subregion Data could not be retrieved', |
|
87 | + __FILE__, |
|
88 | + __METHOD__, |
|
89 | + __LINE__ |
|
90 | + ); |
|
91 | + } |
|
92 | + if ( |
|
93 | + ! $has_sub_regions |
|
94 | + || (isset($data->version) && version_compare($data->version, $this->data_version)) |
|
95 | + ) { |
|
96 | + if ( |
|
97 | + isset($data->countries) |
|
98 | + && $this->processCountryData($CNT_ISO, $data->countries) > 0 |
|
99 | + ) { |
|
100 | + $this->countries = $data->countries; |
|
101 | + $this->updateCountrySubRegionDataVersion($data->version); |
|
102 | + return true; |
|
103 | + } |
|
104 | + } |
|
105 | + return false; |
|
106 | + } |
|
107 | 107 | |
108 | 108 | |
109 | - /** |
|
110 | - * @since 4.9.70.p |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - private function getCountrySubRegionDataVersion() |
|
114 | - { |
|
115 | - return get_option(self::OPTION_NAME_COUNTRY_DATA_VERSION, null); |
|
116 | - } |
|
109 | + /** |
|
110 | + * @since 4.9.70.p |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + private function getCountrySubRegionDataVersion() |
|
114 | + { |
|
115 | + return get_option(self::OPTION_NAME_COUNTRY_DATA_VERSION, null); |
|
116 | + } |
|
117 | 117 | |
118 | 118 | |
119 | - /** |
|
120 | - * @param string $version |
|
121 | - */ |
|
122 | - private function updateCountrySubRegionDataVersion($version = '') |
|
123 | - { |
|
124 | - // add version option if it has never been added before, or update existing |
|
125 | - if ($this->data_version === null) { |
|
126 | - add_option(self::OPTION_NAME_COUNTRY_DATA_VERSION, $version, '', false); |
|
127 | - } else { |
|
128 | - update_option(self::OPTION_NAME_COUNTRY_DATA_VERSION, $version); |
|
129 | - } |
|
130 | - } |
|
119 | + /** |
|
120 | + * @param string $version |
|
121 | + */ |
|
122 | + private function updateCountrySubRegionDataVersion($version = '') |
|
123 | + { |
|
124 | + // add version option if it has never been added before, or update existing |
|
125 | + if ($this->data_version === null) { |
|
126 | + add_option(self::OPTION_NAME_COUNTRY_DATA_VERSION, $version, '', false); |
|
127 | + } else { |
|
128 | + update_option(self::OPTION_NAME_COUNTRY_DATA_VERSION, $version); |
|
129 | + } |
|
130 | + } |
|
131 | 131 | |
132 | 132 | |
133 | - /** |
|
134 | - * @param string $CNT_ISO |
|
135 | - * @param array $countries |
|
136 | - * @return int |
|
137 | - * @throws EE_Error |
|
138 | - * @throws InvalidArgumentException |
|
139 | - * @throws InvalidDataTypeException |
|
140 | - * @throws InvalidInterfaceException |
|
141 | - * @throws ReflectionException |
|
142 | - * @since 4.9.70.p |
|
143 | - */ |
|
144 | - private function processCountryData($CNT_ISO, $countries = array()) |
|
145 | - { |
|
146 | - if (! empty($countries)) { |
|
147 | - foreach ($countries as $key => $country) { |
|
148 | - if ( |
|
149 | - $country instanceof stdClass |
|
150 | - && $country->code === $CNT_ISO |
|
151 | - && empty($country->sub_regions) |
|
152 | - && ! empty($country->filename) |
|
153 | - ) { |
|
154 | - $country->sub_regions = $this->retrieveJsonData( |
|
155 | - self::REPO_URL . 'countries/' . $country->filename . '.json' |
|
156 | - ); |
|
157 | - return $this->saveSubRegionData($country, $country->sub_regions); |
|
158 | - } |
|
159 | - } |
|
160 | - } |
|
161 | - return 0; |
|
162 | - } |
|
133 | + /** |
|
134 | + * @param string $CNT_ISO |
|
135 | + * @param array $countries |
|
136 | + * @return int |
|
137 | + * @throws EE_Error |
|
138 | + * @throws InvalidArgumentException |
|
139 | + * @throws InvalidDataTypeException |
|
140 | + * @throws InvalidInterfaceException |
|
141 | + * @throws ReflectionException |
|
142 | + * @since 4.9.70.p |
|
143 | + */ |
|
144 | + private function processCountryData($CNT_ISO, $countries = array()) |
|
145 | + { |
|
146 | + if (! empty($countries)) { |
|
147 | + foreach ($countries as $key => $country) { |
|
148 | + if ( |
|
149 | + $country instanceof stdClass |
|
150 | + && $country->code === $CNT_ISO |
|
151 | + && empty($country->sub_regions) |
|
152 | + && ! empty($country->filename) |
|
153 | + ) { |
|
154 | + $country->sub_regions = $this->retrieveJsonData( |
|
155 | + self::REPO_URL . 'countries/' . $country->filename . '.json' |
|
156 | + ); |
|
157 | + return $this->saveSubRegionData($country, $country->sub_regions); |
|
158 | + } |
|
159 | + } |
|
160 | + } |
|
161 | + return 0; |
|
162 | + } |
|
163 | 163 | |
164 | 164 | |
165 | - /** |
|
166 | - * @param string $url |
|
167 | - * @return array |
|
168 | - */ |
|
169 | - private function retrieveJsonData($url) |
|
170 | - { |
|
171 | - if (empty($url)) { |
|
172 | - EE_Error::add_error( |
|
173 | - 'No URL was provided!', |
|
174 | - __FILE__, |
|
175 | - __METHOD__, |
|
176 | - __LINE__ |
|
177 | - ); |
|
178 | - return array(); |
|
179 | - } |
|
180 | - $request = wp_safe_remote_get($url); |
|
181 | - if ($request instanceof WP_Error) { |
|
182 | - EE_Error::add_error( |
|
183 | - $request->get_error_message(), |
|
184 | - __FILE__, |
|
185 | - __METHOD__, |
|
186 | - __LINE__ |
|
187 | - ); |
|
188 | - return array(); |
|
189 | - } |
|
190 | - $body = wp_remote_retrieve_body($request); |
|
191 | - $json = json_decode($body); |
|
192 | - if ($this->json_validator->isValid(__FILE__, __METHOD__, __LINE__)) { |
|
193 | - return $json; |
|
194 | - } |
|
195 | - return array(); |
|
196 | - } |
|
165 | + /** |
|
166 | + * @param string $url |
|
167 | + * @return array |
|
168 | + */ |
|
169 | + private function retrieveJsonData($url) |
|
170 | + { |
|
171 | + if (empty($url)) { |
|
172 | + EE_Error::add_error( |
|
173 | + 'No URL was provided!', |
|
174 | + __FILE__, |
|
175 | + __METHOD__, |
|
176 | + __LINE__ |
|
177 | + ); |
|
178 | + return array(); |
|
179 | + } |
|
180 | + $request = wp_safe_remote_get($url); |
|
181 | + if ($request instanceof WP_Error) { |
|
182 | + EE_Error::add_error( |
|
183 | + $request->get_error_message(), |
|
184 | + __FILE__, |
|
185 | + __METHOD__, |
|
186 | + __LINE__ |
|
187 | + ); |
|
188 | + return array(); |
|
189 | + } |
|
190 | + $body = wp_remote_retrieve_body($request); |
|
191 | + $json = json_decode($body); |
|
192 | + if ($this->json_validator->isValid(__FILE__, __METHOD__, __LINE__)) { |
|
193 | + return $json; |
|
194 | + } |
|
195 | + return array(); |
|
196 | + } |
|
197 | 197 | |
198 | 198 | |
199 | - /** |
|
200 | - * @param stdClass $country |
|
201 | - * @param array $sub_regions |
|
202 | - * @return int |
|
203 | - * @throws EE_Error |
|
204 | - * @throws InvalidArgumentException |
|
205 | - * @throws InvalidDataTypeException |
|
206 | - * @throws InvalidInterfaceException |
|
207 | - * @throws ReflectionException |
|
208 | - */ |
|
209 | - private function saveSubRegionData(stdClass $country, $sub_regions = array()) |
|
210 | - { |
|
211 | - $results = 0; |
|
212 | - if (is_array($sub_regions)) { |
|
213 | - $existing_sub_regions = $this->getExistingStateAbbreviations($country->code); |
|
214 | - foreach ($sub_regions as $sub_region) { |
|
215 | - // remove country code from sub region code |
|
216 | - $abbrev = str_replace( |
|
217 | - $country->code . '-', |
|
218 | - '', |
|
219 | - sanitize_text_field($sub_region->code) |
|
220 | - ); |
|
221 | - // but NOT if sub region code results in only a number |
|
222 | - if (absint($abbrev) !== 0) { |
|
223 | - $abbrev = sanitize_text_field($sub_region->code); |
|
224 | - } |
|
225 | - if ( |
|
226 | - ! in_array($abbrev, $existing_sub_regions, true) |
|
227 | - && $this->state_model->insert( |
|
228 | - [ |
|
229 | - // STA_ID CNT_ISO STA_abbrev STA_name STA_active |
|
230 | - 'CNT_ISO' => $country->code, |
|
231 | - 'STA_abbrev' => $abbrev, |
|
232 | - 'STA_name' => sanitize_text_field($sub_region->name), |
|
233 | - 'STA_active' => 1, |
|
234 | - ] |
|
235 | - ) |
|
236 | - ) { |
|
237 | - $results++; |
|
238 | - } |
|
239 | - } |
|
240 | - } |
|
241 | - return $results; |
|
242 | - } |
|
199 | + /** |
|
200 | + * @param stdClass $country |
|
201 | + * @param array $sub_regions |
|
202 | + * @return int |
|
203 | + * @throws EE_Error |
|
204 | + * @throws InvalidArgumentException |
|
205 | + * @throws InvalidDataTypeException |
|
206 | + * @throws InvalidInterfaceException |
|
207 | + * @throws ReflectionException |
|
208 | + */ |
|
209 | + private function saveSubRegionData(stdClass $country, $sub_regions = array()) |
|
210 | + { |
|
211 | + $results = 0; |
|
212 | + if (is_array($sub_regions)) { |
|
213 | + $existing_sub_regions = $this->getExistingStateAbbreviations($country->code); |
|
214 | + foreach ($sub_regions as $sub_region) { |
|
215 | + // remove country code from sub region code |
|
216 | + $abbrev = str_replace( |
|
217 | + $country->code . '-', |
|
218 | + '', |
|
219 | + sanitize_text_field($sub_region->code) |
|
220 | + ); |
|
221 | + // but NOT if sub region code results in only a number |
|
222 | + if (absint($abbrev) !== 0) { |
|
223 | + $abbrev = sanitize_text_field($sub_region->code); |
|
224 | + } |
|
225 | + if ( |
|
226 | + ! in_array($abbrev, $existing_sub_regions, true) |
|
227 | + && $this->state_model->insert( |
|
228 | + [ |
|
229 | + // STA_ID CNT_ISO STA_abbrev STA_name STA_active |
|
230 | + 'CNT_ISO' => $country->code, |
|
231 | + 'STA_abbrev' => $abbrev, |
|
232 | + 'STA_name' => sanitize_text_field($sub_region->name), |
|
233 | + 'STA_active' => 1, |
|
234 | + ] |
|
235 | + ) |
|
236 | + ) { |
|
237 | + $results++; |
|
238 | + } |
|
239 | + } |
|
240 | + } |
|
241 | + return $results; |
|
242 | + } |
|
243 | 243 | |
244 | 244 | |
245 | - /** |
|
246 | - * @param string $CNT_ISO |
|
247 | - * @since 4.9.76.p |
|
248 | - * @return array |
|
249 | - * @throws EE_Error |
|
250 | - * @throws InvalidArgumentException |
|
251 | - * @throws InvalidDataTypeException |
|
252 | - * @throws InvalidInterfaceException |
|
253 | - * @throws ReflectionException |
|
254 | - */ |
|
255 | - private function getExistingStateAbbreviations($CNT_ISO) |
|
256 | - { |
|
257 | - $existing_sub_region_IDs = []; |
|
258 | - $existing_sub_regions = $this->state_model->get_all(array( |
|
259 | - array( |
|
260 | - 'Country.CNT_ISO' => array( |
|
261 | - 'IN', |
|
262 | - [$CNT_ISO] |
|
263 | - ) |
|
264 | - ), |
|
265 | - 'order_by' => array('Country.CNT_name' => 'ASC', 'STA_name' => 'ASC') |
|
266 | - )); |
|
267 | - if (is_array($existing_sub_regions)) { |
|
268 | - foreach ($existing_sub_regions as $existing_sub_region) { |
|
269 | - if ($existing_sub_region instanceof EE_State) { |
|
270 | - $existing_sub_region_IDs[] = $existing_sub_region->abbrev(); |
|
271 | - } |
|
272 | - } |
|
273 | - } |
|
274 | - return $existing_sub_region_IDs; |
|
275 | - } |
|
245 | + /** |
|
246 | + * @param string $CNT_ISO |
|
247 | + * @since 4.9.76.p |
|
248 | + * @return array |
|
249 | + * @throws EE_Error |
|
250 | + * @throws InvalidArgumentException |
|
251 | + * @throws InvalidDataTypeException |
|
252 | + * @throws InvalidInterfaceException |
|
253 | + * @throws ReflectionException |
|
254 | + */ |
|
255 | + private function getExistingStateAbbreviations($CNT_ISO) |
|
256 | + { |
|
257 | + $existing_sub_region_IDs = []; |
|
258 | + $existing_sub_regions = $this->state_model->get_all(array( |
|
259 | + array( |
|
260 | + 'Country.CNT_ISO' => array( |
|
261 | + 'IN', |
|
262 | + [$CNT_ISO] |
|
263 | + ) |
|
264 | + ), |
|
265 | + 'order_by' => array('Country.CNT_name' => 'ASC', 'STA_name' => 'ASC') |
|
266 | + )); |
|
267 | + if (is_array($existing_sub_regions)) { |
|
268 | + foreach ($existing_sub_regions as $existing_sub_region) { |
|
269 | + if ($existing_sub_region instanceof EE_State) { |
|
270 | + $existing_sub_region_IDs[] = $existing_sub_region->abbrev(); |
|
271 | + } |
|
272 | + } |
|
273 | + } |
|
274 | + return $existing_sub_region_IDs; |
|
275 | + } |
|
276 | 276 | } |
@@ -20,202 +20,202 @@ |
||
20 | 20 | */ |
21 | 21 | class PayPalSettingsForm extends EE_Payment_Method_Form |
22 | 22 | { |
23 | - /** |
|
24 | - * @var string of HTML being the help tab link |
|
25 | - */ |
|
26 | - protected $helpTabLink; |
|
23 | + /** |
|
24 | + * @var string of HTML being the help tab link |
|
25 | + */ |
|
26 | + protected $helpTabLink; |
|
27 | 27 | |
28 | - public function __construct(array $options_array = array(), $help_tab_link = '') |
|
29 | - { |
|
30 | - $this->helpTabLink = $help_tab_link; |
|
31 | - $options_array = array_replace_recursive( |
|
32 | - array( |
|
33 | - 'extra_meta_inputs' => array( |
|
34 | - 'api_username' => new EE_Text_Input( |
|
35 | - array( |
|
36 | - 'html_label_text' => sprintf( |
|
37 | - // translators: %s link to help doc |
|
38 | - esc_html__('API Username %s', 'event_espresso'), |
|
39 | - $help_tab_link |
|
40 | - ), |
|
41 | - 'required' => true, |
|
42 | - ) |
|
43 | - ), |
|
44 | - 'api_password' => new EE_Text_Input( |
|
45 | - array( |
|
46 | - 'html_label_text' => sprintf( |
|
47 | - // translators: %s link to help doc |
|
48 | - esc_html__('API Password %s', 'event_espresso'), |
|
49 | - $help_tab_link |
|
50 | - ), |
|
51 | - 'required' => true, |
|
52 | - ) |
|
53 | - ), |
|
54 | - 'api_signature' => new EE_Text_Input( |
|
55 | - array( |
|
56 | - 'html_label_text' => sprintf( |
|
57 | - // translators: %s link to help doc |
|
58 | - esc_html__('API Signature %s', 'event_espresso'), |
|
59 | - $help_tab_link |
|
60 | - ), |
|
61 | - 'required' => true, |
|
62 | - ) |
|
63 | - ), |
|
64 | - ) |
|
65 | - ), |
|
66 | - $options_array |
|
67 | - ); |
|
68 | - parent::__construct($options_array); |
|
69 | - } |
|
28 | + public function __construct(array $options_array = array(), $help_tab_link = '') |
|
29 | + { |
|
30 | + $this->helpTabLink = $help_tab_link; |
|
31 | + $options_array = array_replace_recursive( |
|
32 | + array( |
|
33 | + 'extra_meta_inputs' => array( |
|
34 | + 'api_username' => new EE_Text_Input( |
|
35 | + array( |
|
36 | + 'html_label_text' => sprintf( |
|
37 | + // translators: %s link to help doc |
|
38 | + esc_html__('API Username %s', 'event_espresso'), |
|
39 | + $help_tab_link |
|
40 | + ), |
|
41 | + 'required' => true, |
|
42 | + ) |
|
43 | + ), |
|
44 | + 'api_password' => new EE_Text_Input( |
|
45 | + array( |
|
46 | + 'html_label_text' => sprintf( |
|
47 | + // translators: %s link to help doc |
|
48 | + esc_html__('API Password %s', 'event_espresso'), |
|
49 | + $help_tab_link |
|
50 | + ), |
|
51 | + 'required' => true, |
|
52 | + ) |
|
53 | + ), |
|
54 | + 'api_signature' => new EE_Text_Input( |
|
55 | + array( |
|
56 | + 'html_label_text' => sprintf( |
|
57 | + // translators: %s link to help doc |
|
58 | + esc_html__('API Signature %s', 'event_espresso'), |
|
59 | + $help_tab_link |
|
60 | + ), |
|
61 | + 'required' => true, |
|
62 | + ) |
|
63 | + ), |
|
64 | + ) |
|
65 | + ), |
|
66 | + $options_array |
|
67 | + ); |
|
68 | + parent::__construct($options_array); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Tests the the PayPal API credentials work ok |
|
73 | - * @return string of an error using the credentials, otherwise, if the credentials work, returns a blank string |
|
74 | - * @throws EE_Error |
|
75 | - */ |
|
76 | - protected function checkForCredentialsErrors() |
|
77 | - { |
|
78 | - $request_params = array( |
|
79 | - 'METHOD' => 'GetBalance', |
|
80 | - 'VERSION' => '204.0', |
|
81 | - 'USER' => $this->get_input_value('api_username'), |
|
82 | - 'PWD' => $this->get_input_value('api_password'), |
|
83 | - 'SIGNATURE' => $this->get_input_value('api_signature'), |
|
84 | - ); |
|
85 | - $gateway_url = $this->get_input_value('PMD_debug_mode') |
|
86 | - ? 'https://api-3t.sandbox.paypal.com/nvp' |
|
87 | - : 'https://api-3t.paypal.com/nvp'; |
|
88 | - // Request Customer Details. |
|
89 | - $response = wp_remote_post( |
|
90 | - $gateway_url, |
|
91 | - array( |
|
92 | - 'method' => 'POST', |
|
93 | - 'timeout' => 45, |
|
94 | - 'httpversion' => '1.1', |
|
95 | - 'cookies' => array(), |
|
96 | - 'headers' => array(), |
|
97 | - 'body' => http_build_query($request_params, '', '&'), |
|
98 | - ) |
|
99 | - ); |
|
100 | - if (is_wp_error($response) || empty($response['body'])) { |
|
101 | - // If we got here then there was an error in this request. |
|
102 | - // maybe is turned off. We don't know the credentials are invalid |
|
103 | - EE_Error::add_error( |
|
104 | - sprintf( |
|
105 | - // translators: %1$s Error message received from PayPal |
|
106 | - esc_html__( |
|
107 | - // @codingStandardsIgnoreStart |
|
108 | - 'Your PayPal credentials could not be verified. The following error occurred while communicating with PayPal: %1$s', |
|
109 | - // @codingStandardsIgnoreEnd |
|
110 | - 'event_espresso' |
|
111 | - ), |
|
112 | - $response->get_error_message() |
|
113 | - ), |
|
114 | - __FILE__, |
|
115 | - __FUNCTION__, |
|
116 | - __LINE__ |
|
117 | - ); |
|
118 | - } |
|
119 | - $response_args = array(); |
|
120 | - parse_str(urldecode($response['body']), $response_args); |
|
71 | + /** |
|
72 | + * Tests the the PayPal API credentials work ok |
|
73 | + * @return string of an error using the credentials, otherwise, if the credentials work, returns a blank string |
|
74 | + * @throws EE_Error |
|
75 | + */ |
|
76 | + protected function checkForCredentialsErrors() |
|
77 | + { |
|
78 | + $request_params = array( |
|
79 | + 'METHOD' => 'GetBalance', |
|
80 | + 'VERSION' => '204.0', |
|
81 | + 'USER' => $this->get_input_value('api_username'), |
|
82 | + 'PWD' => $this->get_input_value('api_password'), |
|
83 | + 'SIGNATURE' => $this->get_input_value('api_signature'), |
|
84 | + ); |
|
85 | + $gateway_url = $this->get_input_value('PMD_debug_mode') |
|
86 | + ? 'https://api-3t.sandbox.paypal.com/nvp' |
|
87 | + : 'https://api-3t.paypal.com/nvp'; |
|
88 | + // Request Customer Details. |
|
89 | + $response = wp_remote_post( |
|
90 | + $gateway_url, |
|
91 | + array( |
|
92 | + 'method' => 'POST', |
|
93 | + 'timeout' => 45, |
|
94 | + 'httpversion' => '1.1', |
|
95 | + 'cookies' => array(), |
|
96 | + 'headers' => array(), |
|
97 | + 'body' => http_build_query($request_params, '', '&'), |
|
98 | + ) |
|
99 | + ); |
|
100 | + if (is_wp_error($response) || empty($response['body'])) { |
|
101 | + // If we got here then there was an error in this request. |
|
102 | + // maybe is turned off. We don't know the credentials are invalid |
|
103 | + EE_Error::add_error( |
|
104 | + sprintf( |
|
105 | + // translators: %1$s Error message received from PayPal |
|
106 | + esc_html__( |
|
107 | + // @codingStandardsIgnoreStart |
|
108 | + 'Your PayPal credentials could not be verified. The following error occurred while communicating with PayPal: %1$s', |
|
109 | + // @codingStandardsIgnoreEnd |
|
110 | + 'event_espresso' |
|
111 | + ), |
|
112 | + $response->get_error_message() |
|
113 | + ), |
|
114 | + __FILE__, |
|
115 | + __FUNCTION__, |
|
116 | + __LINE__ |
|
117 | + ); |
|
118 | + } |
|
119 | + $response_args = array(); |
|
120 | + parse_str(urldecode($response['body']), $response_args); |
|
121 | 121 | |
122 | - if (empty($response_args['ACK'])) { |
|
123 | - EE_Error::add_error( |
|
124 | - esc_html__( |
|
125 | - 'Your PayPal credentials could not be verified. Part of their response was missing.', |
|
126 | - 'event_espresso' |
|
127 | - ), |
|
128 | - __FILE__, |
|
129 | - __FUNCTION__, |
|
130 | - __LINE__ |
|
131 | - ); |
|
132 | - } |
|
133 | - if ( |
|
134 | - in_array( |
|
135 | - $response_args['ACK'], |
|
136 | - array( |
|
137 | - 'Success', |
|
138 | - 'SuccessWithWarning' |
|
139 | - ), |
|
140 | - true |
|
141 | - ) |
|
142 | - ) { |
|
143 | - return ''; |
|
144 | - } else { |
|
145 | - return sprintf( |
|
146 | - // translators: %1$s: PayPal response message, %2$s: PayPal response code |
|
147 | - esc_html__( |
|
148 | - // @codingStandardsIgnoreStart |
|
149 | - 'Your PayPal API credentials appear to be invalid. PayPal said "%1$s (%2$s)". Please see tips below.', |
|
150 | - // @codingStandardsIgnoreEnd |
|
151 | - 'event_espresso' |
|
152 | - ), |
|
153 | - isset($response_args['L_LONGMESSAGE0']) |
|
154 | - ? $response_args['L_LONGMESSAGE0'] |
|
155 | - : esc_html__('No error message received from PayPal', 'event_espresso'), |
|
156 | - isset($response_args['L_ERRORCODE0']) ? $response_args['L_ERRORCODE0'] : 0 |
|
157 | - ); |
|
158 | - } |
|
159 | - } |
|
122 | + if (empty($response_args['ACK'])) { |
|
123 | + EE_Error::add_error( |
|
124 | + esc_html__( |
|
125 | + 'Your PayPal credentials could not be verified. Part of their response was missing.', |
|
126 | + 'event_espresso' |
|
127 | + ), |
|
128 | + __FILE__, |
|
129 | + __FUNCTION__, |
|
130 | + __LINE__ |
|
131 | + ); |
|
132 | + } |
|
133 | + if ( |
|
134 | + in_array( |
|
135 | + $response_args['ACK'], |
|
136 | + array( |
|
137 | + 'Success', |
|
138 | + 'SuccessWithWarning' |
|
139 | + ), |
|
140 | + true |
|
141 | + ) |
|
142 | + ) { |
|
143 | + return ''; |
|
144 | + } else { |
|
145 | + return sprintf( |
|
146 | + // translators: %1$s: PayPal response message, %2$s: PayPal response code |
|
147 | + esc_html__( |
|
148 | + // @codingStandardsIgnoreStart |
|
149 | + 'Your PayPal API credentials appear to be invalid. PayPal said "%1$s (%2$s)". Please see tips below.', |
|
150 | + // @codingStandardsIgnoreEnd |
|
151 | + 'event_espresso' |
|
152 | + ), |
|
153 | + isset($response_args['L_LONGMESSAGE0']) |
|
154 | + ? $response_args['L_LONGMESSAGE0'] |
|
155 | + : esc_html__('No error message received from PayPal', 'event_espresso'), |
|
156 | + isset($response_args['L_ERRORCODE0']) ? $response_args['L_ERRORCODE0'] : 0 |
|
157 | + ); |
|
158 | + } |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * Gets the HTML to show the link to the help tab |
|
163 | - * @return string |
|
164 | - */ |
|
165 | - protected function helpTabLink() |
|
166 | - { |
|
167 | - return $this->helpTabLink; |
|
168 | - } |
|
161 | + /** |
|
162 | + * Gets the HTML to show the link to the help tab |
|
163 | + * @return string |
|
164 | + */ |
|
165 | + protected function helpTabLink() |
|
166 | + { |
|
167 | + return $this->helpTabLink; |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * Does the normal validation, but also verifies the PayPal API credentials work. |
|
172 | - * If they don't, sets a validation error on the entire form, and adds validation errors (which are really more |
|
173 | - * tips) on each of the inputs that could be the cause of the problem. |
|
174 | - * @throws EE_Error |
|
175 | - */ |
|
176 | - public function _validate() |
|
177 | - { |
|
178 | - parent::_validate(); |
|
179 | - $credentials_message = $this->checkForCredentialsErrors(); |
|
180 | - if ($credentials_message !== '') { |
|
181 | - $this->add_validation_error($credentials_message); |
|
182 | - $this->get_input('PMD_debug_mode')->add_validation_error( |
|
183 | - esc_html__( |
|
184 | - // @codingStandardsIgnoreStart |
|
185 | - 'If you are using PayPal Sandbox (test) credentials, Debug mode should be set to "Yes". Otherwise, if you are using live PayPal credentials, set this to "No".', |
|
186 | - // @codingStandardsIgnoreEnd |
|
187 | - 'event_espresso' |
|
188 | - ) |
|
189 | - ); |
|
190 | - $this->get_input('api_username')->add_validation_error( |
|
191 | - sprintf( |
|
192 | - // translators: $1$s HTML for a link to the help tab |
|
193 | - esc_html__( |
|
194 | - 'Are you sure this is your API username, not your login username? %1$s', |
|
195 | - 'event_espresso' |
|
196 | - ), |
|
197 | - $this->helpTabLink() |
|
198 | - ) |
|
199 | - ); |
|
200 | - $this->get_input('api_password')->add_validation_error( |
|
201 | - sprintf( |
|
202 | - // translators: $1$s HTML for a link to the help tab |
|
203 | - esc_html__( |
|
204 | - 'Are you sure this is your API password, not your login password? %1$s', |
|
205 | - 'event_espresso' |
|
206 | - ), |
|
207 | - $this->helpTabLink() |
|
208 | - ) |
|
209 | - ); |
|
210 | - $this->get_input('api_signature')->add_validation_error( |
|
211 | - sprintf( |
|
212 | - // translators: $1$s HTML for a link to the help tab |
|
213 | - esc_html__('Please verify your API signature is correct. %1$s', 'event_espresso'), |
|
214 | - $this->helpTabLink() |
|
215 | - ) |
|
216 | - ); |
|
217 | - } |
|
218 | - } |
|
170 | + /** |
|
171 | + * Does the normal validation, but also verifies the PayPal API credentials work. |
|
172 | + * If they don't, sets a validation error on the entire form, and adds validation errors (which are really more |
|
173 | + * tips) on each of the inputs that could be the cause of the problem. |
|
174 | + * @throws EE_Error |
|
175 | + */ |
|
176 | + public function _validate() |
|
177 | + { |
|
178 | + parent::_validate(); |
|
179 | + $credentials_message = $this->checkForCredentialsErrors(); |
|
180 | + if ($credentials_message !== '') { |
|
181 | + $this->add_validation_error($credentials_message); |
|
182 | + $this->get_input('PMD_debug_mode')->add_validation_error( |
|
183 | + esc_html__( |
|
184 | + // @codingStandardsIgnoreStart |
|
185 | + 'If you are using PayPal Sandbox (test) credentials, Debug mode should be set to "Yes". Otherwise, if you are using live PayPal credentials, set this to "No".', |
|
186 | + // @codingStandardsIgnoreEnd |
|
187 | + 'event_espresso' |
|
188 | + ) |
|
189 | + ); |
|
190 | + $this->get_input('api_username')->add_validation_error( |
|
191 | + sprintf( |
|
192 | + // translators: $1$s HTML for a link to the help tab |
|
193 | + esc_html__( |
|
194 | + 'Are you sure this is your API username, not your login username? %1$s', |
|
195 | + 'event_espresso' |
|
196 | + ), |
|
197 | + $this->helpTabLink() |
|
198 | + ) |
|
199 | + ); |
|
200 | + $this->get_input('api_password')->add_validation_error( |
|
201 | + sprintf( |
|
202 | + // translators: $1$s HTML for a link to the help tab |
|
203 | + esc_html__( |
|
204 | + 'Are you sure this is your API password, not your login password? %1$s', |
|
205 | + 'event_espresso' |
|
206 | + ), |
|
207 | + $this->helpTabLink() |
|
208 | + ) |
|
209 | + ); |
|
210 | + $this->get_input('api_signature')->add_validation_error( |
|
211 | + sprintf( |
|
212 | + // translators: $1$s HTML for a link to the help tab |
|
213 | + esc_html__('Please verify your API signature is correct. %1$s', 'event_espresso'), |
|
214 | + $this->helpTabLink() |
|
215 | + ) |
|
216 | + ); |
|
217 | + } |
|
218 | + } |
|
219 | 219 | } |
220 | 220 | // End of file PayPalSettingsForm.php |
221 | 221 | // Location: ${NAMESPACE}/PayPalSettingsForm.php |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | */ |
194 | 194 | protected function setCollectionInterface($collection_interface) |
195 | 195 | { |
196 | - if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
196 | + if ( ! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
197 | 197 | throw new InvalidInterfaceException($collection_interface); |
198 | 198 | } |
199 | 199 | $this->collection_interface = $collection_interface; |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | */ |
222 | 222 | protected function setCollectionName($collection_name) |
223 | 223 | { |
224 | - if (! is_string($collection_name)) { |
|
224 | + if ( ! is_string($collection_name)) { |
|
225 | 225 | throw new InvalidDataTypeException('$collection_name', $collection_name, 'string'); |
226 | 226 | } |
227 | 227 | $this->collection_name = str_replace( |
@@ -281,7 +281,7 @@ discard block |
||
281 | 281 | */ |
282 | 282 | protected function setIdentifierCallback($identifier_callback = 'identifier') |
283 | 283 | { |
284 | - if (! is_string($identifier_callback)) { |
|
284 | + if ( ! is_string($identifier_callback)) { |
|
285 | 285 | throw new InvalidDataTypeException('$identifier_callback', $identifier_callback, 'string'); |
286 | 286 | } |
287 | 287 | $this->identifier_callback = $identifier_callback; |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | $this->file_mask = ! empty($file_mask) ? $file_mask : '*.php'; |
312 | 312 | // we know our default is a string, so if it's not a string now, |
313 | 313 | // then that means the incoming parameter was something else |
314 | - if (! is_string($this->file_mask)) { |
|
314 | + if ( ! is_string($this->file_mask)) { |
|
315 | 315 | throw new InvalidDataTypeException('$file_mask', $this->file_mask, 'string'); |
316 | 316 | } |
317 | 317 | } |
@@ -336,7 +336,7 @@ discard block |
||
336 | 336 | public function setCollectionFQCNs($collection_FQCNs) |
337 | 337 | { |
338 | 338 | foreach ((array) $collection_FQCNs as $collection_FQCN) { |
339 | - if (! empty($collection_FQCN)) { |
|
339 | + if ( ! empty($collection_FQCN)) { |
|
340 | 340 | if (class_exists($collection_FQCN)) { |
341 | 341 | $this->collection_FQCNs[] = $collection_FQCN; |
342 | 342 | } else { |
@@ -358,7 +358,7 @@ discard block |
||
358 | 358 | */ |
359 | 359 | protected function getFQCNsFromPartialNamespace($partial_FQCN) |
360 | 360 | { |
361 | - if (! $this->file_locator instanceof FqcnLocator) { |
|
361 | + if ( ! $this->file_locator instanceof FqcnLocator) { |
|
362 | 362 | $this->file_locator = new FqcnLocator(); |
363 | 363 | } |
364 | 364 | $this->file_locator->locate($partial_FQCN); |
@@ -384,8 +384,8 @@ discard block |
||
384 | 384 | public function setCollectionPaths($collection_paths) |
385 | 385 | { |
386 | 386 | foreach ((array) $collection_paths as $collection_path) { |
387 | - if (! empty($collection_path)) { |
|
388 | - if (! is_readable($collection_path)) { |
|
387 | + if ( ! empty($collection_path)) { |
|
388 | + if ( ! is_readable($collection_path)) { |
|
389 | 389 | throw new InvalidFilePathException($collection_path); |
390 | 390 | } |
391 | 391 | $this->collection_paths[] = $collection_path; |
@@ -42,354 +42,354 @@ |
||
42 | 42 | */ |
43 | 43 | class CollectionDetails implements CollectionDetailsInterface |
44 | 44 | { |
45 | - /** |
|
46 | - * if $identifier_type is set to this, |
|
47 | - * then the collection will use each object's spl_object_hash() as it's identifier |
|
48 | - */ |
|
49 | - const ID_OBJECT_HASH = 'identifier-uses-spl-object-hash'; |
|
50 | - |
|
51 | - /** |
|
52 | - * if $identifier_type is set to this, |
|
53 | - * then the collection will use each object's class name as it's identifier |
|
54 | - */ |
|
55 | - const ID_CLASS_NAME = 'identifier-uses-object-class-name'; |
|
56 | - |
|
57 | - /** |
|
58 | - * if $identifier_type is set to this, |
|
59 | - * then the collection will use the return value from a specified callback method on each object |
|
60 | - */ |
|
61 | - const ID_CALLBACK_METHOD = 'identifier-uses-callback-method'; |
|
62 | - |
|
63 | - /** |
|
64 | - * The interface used for controlling what gets added to the collection |
|
65 | - * |
|
66 | - * @var string $collection_interface |
|
67 | - */ |
|
68 | - protected $collection_interface = ''; |
|
69 | - |
|
70 | - /** |
|
71 | - * a unique name used to identify the collection in filter names |
|
72 | - * supplied value is run through sanitize_title_with_dashes(), |
|
73 | - * but then also converts dashes to underscores |
|
74 | - * |
|
75 | - * @var string $collection_name |
|
76 | - */ |
|
77 | - protected $collection_name = ''; |
|
78 | - |
|
79 | - /** |
|
80 | - * what the collection uses for the object identifier. |
|
81 | - * corresponds to one of the class constants above. |
|
82 | - * CollectionDetails::ID_OBJECT_HASH will use spl_object_hash( object ) for the identifier |
|
83 | - * CollectionDetails::ID_CLASS_NAME will use get_class( object ) for the identifier |
|
84 | - * CollectionDetails::ID_CALLBACK_METHOD will use a callback for the identifier |
|
85 | - * defaults to using spl_object_hash() so that multiple objects of the same class can be added |
|
86 | - * |
|
87 | - * @var string $identifier_type |
|
88 | - */ |
|
89 | - protected $identifier_type = CollectionDetails::ID_OBJECT_HASH; |
|
90 | - |
|
91 | - /** |
|
92 | - * the pattern applied to paths when searching for class files to add to the collection |
|
93 | - * ie: "My_Awesome_*.class.php" |
|
94 | - * computed value (setter) defaults to "*.php" |
|
95 | - * |
|
96 | - * @var string $file_mask |
|
97 | - * @see CollectionDetails::setFileMask() for details on how computed value is resolved |
|
98 | - */ |
|
99 | - protected $file_mask = ''; |
|
100 | - |
|
101 | - /** |
|
102 | - * if the $identifier_type above is set to CollectionDetails::ID_CALLBACK_METHOD, |
|
103 | - * then this specifies the method to use on each entity. |
|
104 | - * If the callback method does not exist, then an exception will be thrown |
|
105 | - * |
|
106 | - * @var string $identifier_callback |
|
107 | - */ |
|
108 | - protected $identifier_callback = ''; |
|
109 | - |
|
110 | - /** |
|
111 | - * an array of Fully Qualified Class Names |
|
112 | - * for example: |
|
113 | - * $FQCNs = array( |
|
114 | - * '/Fully/Qualified/ClassNameA' |
|
115 | - * '/Fully/Qualified/Other/ClassNameB' |
|
116 | - * ); |
|
117 | - * |
|
118 | - * @var array $collection_FQCNs |
|
119 | - */ |
|
120 | - protected $collection_FQCNs = array(); |
|
121 | - |
|
122 | - /** |
|
123 | - * an array of full server paths to folders containing files to be loaded into collection |
|
124 | - * for example: |
|
125 | - * $paths = array( |
|
126 | - * '/full/server/path/to/ClassNameA.ext.php' // for class ClassNameA |
|
127 | - * '/full/server/path/to/other/ClassNameB.php' // for class ClassNameB |
|
128 | - * ); |
|
129 | - * |
|
130 | - * @var array $collection_paths |
|
131 | - */ |
|
132 | - protected $collection_paths = array(); |
|
133 | - |
|
134 | - /** |
|
135 | - * @var LocatorInterface $file_locator |
|
136 | - */ |
|
137 | - protected $file_locator; |
|
138 | - |
|
139 | - |
|
140 | - /** |
|
141 | - * CollectionDetails constructor. |
|
142 | - * |
|
143 | - * @access public |
|
144 | - * @param string $collection_name |
|
145 | - * @param string $collection_interface |
|
146 | - * @param array $collection_FQCNs |
|
147 | - * @param array $collection_paths |
|
148 | - * @param string $file_mask |
|
149 | - * @param string $identifier_type |
|
150 | - * @param string $identifier_callback |
|
151 | - * @param LocatorInterface $file_locator |
|
152 | - * @throws CollectionDetailsException |
|
153 | - */ |
|
154 | - public function __construct( |
|
155 | - $collection_name, |
|
156 | - $collection_interface, |
|
157 | - array $collection_FQCNs = array(), |
|
158 | - array $collection_paths = array(), |
|
159 | - $file_mask = '', |
|
160 | - $identifier_type = CollectionDetails::ID_OBJECT_HASH, |
|
161 | - $identifier_callback = '', |
|
162 | - LocatorInterface $file_locator = null |
|
163 | - ) { |
|
164 | - try { |
|
165 | - $this->setCollectionName($collection_name); |
|
166 | - $this->setCollectionInterface($collection_interface); |
|
167 | - $this->setCollectionFQCNs($collection_FQCNs); |
|
168 | - $this->setCollectionPaths($collection_paths); |
|
169 | - $this->setFileMask($file_mask); |
|
170 | - $this->setIdentifierType($identifier_type); |
|
171 | - $this->setIdentifierCallback($identifier_callback); |
|
172 | - $this->file_locator = $file_locator; |
|
173 | - } catch (Exception $exception) { |
|
174 | - throw new CollectionDetailsException($exception); |
|
175 | - } |
|
176 | - } |
|
177 | - |
|
178 | - |
|
179 | - /** |
|
180 | - * @access public |
|
181 | - * @return mixed |
|
182 | - */ |
|
183 | - public function getCollectionInterface() |
|
184 | - { |
|
185 | - return $this->collection_interface; |
|
186 | - } |
|
187 | - |
|
188 | - |
|
189 | - /** |
|
190 | - * @access protected |
|
191 | - * @param string $collection_interface |
|
192 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
193 | - */ |
|
194 | - protected function setCollectionInterface($collection_interface) |
|
195 | - { |
|
196 | - if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
197 | - throw new InvalidInterfaceException($collection_interface); |
|
198 | - } |
|
199 | - $this->collection_interface = $collection_interface; |
|
200 | - } |
|
201 | - |
|
202 | - |
|
203 | - /** |
|
204 | - * the collection name will be used for creating dynamic filters |
|
205 | - * |
|
206 | - * @access public |
|
207 | - * @return string |
|
208 | - */ |
|
209 | - public function collectionName() |
|
210 | - { |
|
211 | - return $this->collection_name; |
|
212 | - } |
|
213 | - |
|
214 | - |
|
215 | - /** |
|
216 | - * sanitizes collection name and converts spaces and dashes to underscores |
|
217 | - * |
|
218 | - * @access protected |
|
219 | - * @param string $collection_name |
|
220 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
221 | - */ |
|
222 | - protected function setCollectionName($collection_name) |
|
223 | - { |
|
224 | - if (! is_string($collection_name)) { |
|
225 | - throw new InvalidDataTypeException('$collection_name', $collection_name, 'string'); |
|
226 | - } |
|
227 | - $this->collection_name = str_replace( |
|
228 | - '-', |
|
229 | - '_', |
|
230 | - sanitize_title_with_dashes($collection_name, '', 'save') |
|
231 | - ); |
|
232 | - } |
|
233 | - |
|
234 | - |
|
235 | - /** |
|
236 | - * @access public |
|
237 | - * @return string |
|
238 | - */ |
|
239 | - public function identifierType() |
|
240 | - { |
|
241 | - return $this->identifier_type; |
|
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - /** |
|
246 | - * @access protected |
|
247 | - * @param string $identifier_type |
|
248 | - * @throws InvalidIdentifierException |
|
249 | - */ |
|
250 | - protected function setIdentifierType($identifier_type) |
|
251 | - { |
|
252 | - if ( |
|
253 | - ! ($identifier_type === CollectionDetails::ID_CLASS_NAME |
|
254 | - || $identifier_type === CollectionDetails::ID_OBJECT_HASH |
|
255 | - || $identifier_type === CollectionDetails::ID_CALLBACK_METHOD |
|
256 | - ) |
|
257 | - ) { |
|
258 | - throw new InvalidIdentifierException( |
|
259 | - $identifier_type, |
|
260 | - 'CollectionDetails::ID_CLASS_NAME or CollectionDetails::ID_OBJECT_HASH or CollectionDetails::ID_CALLBACK_METHOD' |
|
261 | - ); |
|
262 | - } |
|
263 | - $this->identifier_type = $identifier_type; |
|
264 | - } |
|
265 | - |
|
266 | - |
|
267 | - /** |
|
268 | - * @access public |
|
269 | - * @return string |
|
270 | - */ |
|
271 | - public function identifierCallback() |
|
272 | - { |
|
273 | - return $this->identifier_callback; |
|
274 | - } |
|
275 | - |
|
276 | - |
|
277 | - /** |
|
278 | - * @access protected |
|
279 | - * @param string $identifier_callback |
|
280 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
281 | - */ |
|
282 | - protected function setIdentifierCallback($identifier_callback = 'identifier') |
|
283 | - { |
|
284 | - if (! is_string($identifier_callback)) { |
|
285 | - throw new InvalidDataTypeException('$identifier_callback', $identifier_callback, 'string'); |
|
286 | - } |
|
287 | - $this->identifier_callback = $identifier_callback; |
|
288 | - } |
|
289 | - |
|
290 | - |
|
291 | - /** |
|
292 | - * @access public |
|
293 | - * @return string |
|
294 | - */ |
|
295 | - public function getFileMask() |
|
296 | - { |
|
297 | - return $this->file_mask; |
|
298 | - } |
|
299 | - |
|
300 | - |
|
301 | - /** |
|
302 | - * sets the file mask which is then used to filter what files get loaded |
|
303 | - * when searching for classes to add to the collection. Defaults to '*.php' when parameter is an empty string |
|
304 | - * |
|
305 | - * @access protected |
|
306 | - * @param string $file_mask |
|
307 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
308 | - */ |
|
309 | - protected function setFileMask($file_mask) |
|
310 | - { |
|
311 | - $this->file_mask = ! empty($file_mask) ? $file_mask : '*.php'; |
|
312 | - // we know our default is a string, so if it's not a string now, |
|
313 | - // then that means the incoming parameter was something else |
|
314 | - if (! is_string($this->file_mask)) { |
|
315 | - throw new InvalidDataTypeException('$file_mask', $this->file_mask, 'string'); |
|
316 | - } |
|
317 | - } |
|
318 | - |
|
319 | - |
|
320 | - /** |
|
321 | - * @access public |
|
322 | - * @return array |
|
323 | - */ |
|
324 | - public function getCollectionFQCNs() |
|
325 | - { |
|
326 | - return $this->collection_FQCNs; |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * @access public |
|
332 | - * @param string|array $collection_FQCNs |
|
333 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
334 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
335 | - */ |
|
336 | - public function setCollectionFQCNs($collection_FQCNs) |
|
337 | - { |
|
338 | - foreach ((array) $collection_FQCNs as $collection_FQCN) { |
|
339 | - if (! empty($collection_FQCN)) { |
|
340 | - if (class_exists($collection_FQCN)) { |
|
341 | - $this->collection_FQCNs[] = $collection_FQCN; |
|
342 | - } else { |
|
343 | - foreach ($this->getFQCNsFromPartialNamespace($collection_FQCN) as $FQCN) { |
|
344 | - $this->collection_FQCNs[] = $FQCN; |
|
345 | - } |
|
346 | - } |
|
347 | - } |
|
348 | - } |
|
349 | - } |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * @access protected |
|
354 | - * @param string $partial_FQCN |
|
355 | - * @return array |
|
356 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
357 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
358 | - */ |
|
359 | - protected function getFQCNsFromPartialNamespace($partial_FQCN) |
|
360 | - { |
|
361 | - if (! $this->file_locator instanceof FqcnLocator) { |
|
362 | - $this->file_locator = new FqcnLocator(); |
|
363 | - } |
|
364 | - $this->file_locator->locate($partial_FQCN); |
|
365 | - return $this->file_locator->getFQCNs(); |
|
366 | - } |
|
367 | - |
|
368 | - |
|
369 | - /** |
|
370 | - * @access public |
|
371 | - * @return array |
|
372 | - */ |
|
373 | - public function getCollectionPaths() |
|
374 | - { |
|
375 | - return $this->collection_paths; |
|
376 | - } |
|
377 | - |
|
378 | - |
|
379 | - /** |
|
380 | - * @access public |
|
381 | - * @param string|array $collection_paths |
|
382 | - * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
|
383 | - */ |
|
384 | - public function setCollectionPaths($collection_paths) |
|
385 | - { |
|
386 | - foreach ((array) $collection_paths as $collection_path) { |
|
387 | - if (! empty($collection_path)) { |
|
388 | - if (! is_readable($collection_path)) { |
|
389 | - throw new InvalidFilePathException($collection_path); |
|
390 | - } |
|
391 | - $this->collection_paths[] = $collection_path; |
|
392 | - } |
|
393 | - } |
|
394 | - } |
|
45 | + /** |
|
46 | + * if $identifier_type is set to this, |
|
47 | + * then the collection will use each object's spl_object_hash() as it's identifier |
|
48 | + */ |
|
49 | + const ID_OBJECT_HASH = 'identifier-uses-spl-object-hash'; |
|
50 | + |
|
51 | + /** |
|
52 | + * if $identifier_type is set to this, |
|
53 | + * then the collection will use each object's class name as it's identifier |
|
54 | + */ |
|
55 | + const ID_CLASS_NAME = 'identifier-uses-object-class-name'; |
|
56 | + |
|
57 | + /** |
|
58 | + * if $identifier_type is set to this, |
|
59 | + * then the collection will use the return value from a specified callback method on each object |
|
60 | + */ |
|
61 | + const ID_CALLBACK_METHOD = 'identifier-uses-callback-method'; |
|
62 | + |
|
63 | + /** |
|
64 | + * The interface used for controlling what gets added to the collection |
|
65 | + * |
|
66 | + * @var string $collection_interface |
|
67 | + */ |
|
68 | + protected $collection_interface = ''; |
|
69 | + |
|
70 | + /** |
|
71 | + * a unique name used to identify the collection in filter names |
|
72 | + * supplied value is run through sanitize_title_with_dashes(), |
|
73 | + * but then also converts dashes to underscores |
|
74 | + * |
|
75 | + * @var string $collection_name |
|
76 | + */ |
|
77 | + protected $collection_name = ''; |
|
78 | + |
|
79 | + /** |
|
80 | + * what the collection uses for the object identifier. |
|
81 | + * corresponds to one of the class constants above. |
|
82 | + * CollectionDetails::ID_OBJECT_HASH will use spl_object_hash( object ) for the identifier |
|
83 | + * CollectionDetails::ID_CLASS_NAME will use get_class( object ) for the identifier |
|
84 | + * CollectionDetails::ID_CALLBACK_METHOD will use a callback for the identifier |
|
85 | + * defaults to using spl_object_hash() so that multiple objects of the same class can be added |
|
86 | + * |
|
87 | + * @var string $identifier_type |
|
88 | + */ |
|
89 | + protected $identifier_type = CollectionDetails::ID_OBJECT_HASH; |
|
90 | + |
|
91 | + /** |
|
92 | + * the pattern applied to paths when searching for class files to add to the collection |
|
93 | + * ie: "My_Awesome_*.class.php" |
|
94 | + * computed value (setter) defaults to "*.php" |
|
95 | + * |
|
96 | + * @var string $file_mask |
|
97 | + * @see CollectionDetails::setFileMask() for details on how computed value is resolved |
|
98 | + */ |
|
99 | + protected $file_mask = ''; |
|
100 | + |
|
101 | + /** |
|
102 | + * if the $identifier_type above is set to CollectionDetails::ID_CALLBACK_METHOD, |
|
103 | + * then this specifies the method to use on each entity. |
|
104 | + * If the callback method does not exist, then an exception will be thrown |
|
105 | + * |
|
106 | + * @var string $identifier_callback |
|
107 | + */ |
|
108 | + protected $identifier_callback = ''; |
|
109 | + |
|
110 | + /** |
|
111 | + * an array of Fully Qualified Class Names |
|
112 | + * for example: |
|
113 | + * $FQCNs = array( |
|
114 | + * '/Fully/Qualified/ClassNameA' |
|
115 | + * '/Fully/Qualified/Other/ClassNameB' |
|
116 | + * ); |
|
117 | + * |
|
118 | + * @var array $collection_FQCNs |
|
119 | + */ |
|
120 | + protected $collection_FQCNs = array(); |
|
121 | + |
|
122 | + /** |
|
123 | + * an array of full server paths to folders containing files to be loaded into collection |
|
124 | + * for example: |
|
125 | + * $paths = array( |
|
126 | + * '/full/server/path/to/ClassNameA.ext.php' // for class ClassNameA |
|
127 | + * '/full/server/path/to/other/ClassNameB.php' // for class ClassNameB |
|
128 | + * ); |
|
129 | + * |
|
130 | + * @var array $collection_paths |
|
131 | + */ |
|
132 | + protected $collection_paths = array(); |
|
133 | + |
|
134 | + /** |
|
135 | + * @var LocatorInterface $file_locator |
|
136 | + */ |
|
137 | + protected $file_locator; |
|
138 | + |
|
139 | + |
|
140 | + /** |
|
141 | + * CollectionDetails constructor. |
|
142 | + * |
|
143 | + * @access public |
|
144 | + * @param string $collection_name |
|
145 | + * @param string $collection_interface |
|
146 | + * @param array $collection_FQCNs |
|
147 | + * @param array $collection_paths |
|
148 | + * @param string $file_mask |
|
149 | + * @param string $identifier_type |
|
150 | + * @param string $identifier_callback |
|
151 | + * @param LocatorInterface $file_locator |
|
152 | + * @throws CollectionDetailsException |
|
153 | + */ |
|
154 | + public function __construct( |
|
155 | + $collection_name, |
|
156 | + $collection_interface, |
|
157 | + array $collection_FQCNs = array(), |
|
158 | + array $collection_paths = array(), |
|
159 | + $file_mask = '', |
|
160 | + $identifier_type = CollectionDetails::ID_OBJECT_HASH, |
|
161 | + $identifier_callback = '', |
|
162 | + LocatorInterface $file_locator = null |
|
163 | + ) { |
|
164 | + try { |
|
165 | + $this->setCollectionName($collection_name); |
|
166 | + $this->setCollectionInterface($collection_interface); |
|
167 | + $this->setCollectionFQCNs($collection_FQCNs); |
|
168 | + $this->setCollectionPaths($collection_paths); |
|
169 | + $this->setFileMask($file_mask); |
|
170 | + $this->setIdentifierType($identifier_type); |
|
171 | + $this->setIdentifierCallback($identifier_callback); |
|
172 | + $this->file_locator = $file_locator; |
|
173 | + } catch (Exception $exception) { |
|
174 | + throw new CollectionDetailsException($exception); |
|
175 | + } |
|
176 | + } |
|
177 | + |
|
178 | + |
|
179 | + /** |
|
180 | + * @access public |
|
181 | + * @return mixed |
|
182 | + */ |
|
183 | + public function getCollectionInterface() |
|
184 | + { |
|
185 | + return $this->collection_interface; |
|
186 | + } |
|
187 | + |
|
188 | + |
|
189 | + /** |
|
190 | + * @access protected |
|
191 | + * @param string $collection_interface |
|
192 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
193 | + */ |
|
194 | + protected function setCollectionInterface($collection_interface) |
|
195 | + { |
|
196 | + if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
197 | + throw new InvalidInterfaceException($collection_interface); |
|
198 | + } |
|
199 | + $this->collection_interface = $collection_interface; |
|
200 | + } |
|
201 | + |
|
202 | + |
|
203 | + /** |
|
204 | + * the collection name will be used for creating dynamic filters |
|
205 | + * |
|
206 | + * @access public |
|
207 | + * @return string |
|
208 | + */ |
|
209 | + public function collectionName() |
|
210 | + { |
|
211 | + return $this->collection_name; |
|
212 | + } |
|
213 | + |
|
214 | + |
|
215 | + /** |
|
216 | + * sanitizes collection name and converts spaces and dashes to underscores |
|
217 | + * |
|
218 | + * @access protected |
|
219 | + * @param string $collection_name |
|
220 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
221 | + */ |
|
222 | + protected function setCollectionName($collection_name) |
|
223 | + { |
|
224 | + if (! is_string($collection_name)) { |
|
225 | + throw new InvalidDataTypeException('$collection_name', $collection_name, 'string'); |
|
226 | + } |
|
227 | + $this->collection_name = str_replace( |
|
228 | + '-', |
|
229 | + '_', |
|
230 | + sanitize_title_with_dashes($collection_name, '', 'save') |
|
231 | + ); |
|
232 | + } |
|
233 | + |
|
234 | + |
|
235 | + /** |
|
236 | + * @access public |
|
237 | + * @return string |
|
238 | + */ |
|
239 | + public function identifierType() |
|
240 | + { |
|
241 | + return $this->identifier_type; |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + /** |
|
246 | + * @access protected |
|
247 | + * @param string $identifier_type |
|
248 | + * @throws InvalidIdentifierException |
|
249 | + */ |
|
250 | + protected function setIdentifierType($identifier_type) |
|
251 | + { |
|
252 | + if ( |
|
253 | + ! ($identifier_type === CollectionDetails::ID_CLASS_NAME |
|
254 | + || $identifier_type === CollectionDetails::ID_OBJECT_HASH |
|
255 | + || $identifier_type === CollectionDetails::ID_CALLBACK_METHOD |
|
256 | + ) |
|
257 | + ) { |
|
258 | + throw new InvalidIdentifierException( |
|
259 | + $identifier_type, |
|
260 | + 'CollectionDetails::ID_CLASS_NAME or CollectionDetails::ID_OBJECT_HASH or CollectionDetails::ID_CALLBACK_METHOD' |
|
261 | + ); |
|
262 | + } |
|
263 | + $this->identifier_type = $identifier_type; |
|
264 | + } |
|
265 | + |
|
266 | + |
|
267 | + /** |
|
268 | + * @access public |
|
269 | + * @return string |
|
270 | + */ |
|
271 | + public function identifierCallback() |
|
272 | + { |
|
273 | + return $this->identifier_callback; |
|
274 | + } |
|
275 | + |
|
276 | + |
|
277 | + /** |
|
278 | + * @access protected |
|
279 | + * @param string $identifier_callback |
|
280 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
281 | + */ |
|
282 | + protected function setIdentifierCallback($identifier_callback = 'identifier') |
|
283 | + { |
|
284 | + if (! is_string($identifier_callback)) { |
|
285 | + throw new InvalidDataTypeException('$identifier_callback', $identifier_callback, 'string'); |
|
286 | + } |
|
287 | + $this->identifier_callback = $identifier_callback; |
|
288 | + } |
|
289 | + |
|
290 | + |
|
291 | + /** |
|
292 | + * @access public |
|
293 | + * @return string |
|
294 | + */ |
|
295 | + public function getFileMask() |
|
296 | + { |
|
297 | + return $this->file_mask; |
|
298 | + } |
|
299 | + |
|
300 | + |
|
301 | + /** |
|
302 | + * sets the file mask which is then used to filter what files get loaded |
|
303 | + * when searching for classes to add to the collection. Defaults to '*.php' when parameter is an empty string |
|
304 | + * |
|
305 | + * @access protected |
|
306 | + * @param string $file_mask |
|
307 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
308 | + */ |
|
309 | + protected function setFileMask($file_mask) |
|
310 | + { |
|
311 | + $this->file_mask = ! empty($file_mask) ? $file_mask : '*.php'; |
|
312 | + // we know our default is a string, so if it's not a string now, |
|
313 | + // then that means the incoming parameter was something else |
|
314 | + if (! is_string($this->file_mask)) { |
|
315 | + throw new InvalidDataTypeException('$file_mask', $this->file_mask, 'string'); |
|
316 | + } |
|
317 | + } |
|
318 | + |
|
319 | + |
|
320 | + /** |
|
321 | + * @access public |
|
322 | + * @return array |
|
323 | + */ |
|
324 | + public function getCollectionFQCNs() |
|
325 | + { |
|
326 | + return $this->collection_FQCNs; |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * @access public |
|
332 | + * @param string|array $collection_FQCNs |
|
333 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
334 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
335 | + */ |
|
336 | + public function setCollectionFQCNs($collection_FQCNs) |
|
337 | + { |
|
338 | + foreach ((array) $collection_FQCNs as $collection_FQCN) { |
|
339 | + if (! empty($collection_FQCN)) { |
|
340 | + if (class_exists($collection_FQCN)) { |
|
341 | + $this->collection_FQCNs[] = $collection_FQCN; |
|
342 | + } else { |
|
343 | + foreach ($this->getFQCNsFromPartialNamespace($collection_FQCN) as $FQCN) { |
|
344 | + $this->collection_FQCNs[] = $FQCN; |
|
345 | + } |
|
346 | + } |
|
347 | + } |
|
348 | + } |
|
349 | + } |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * @access protected |
|
354 | + * @param string $partial_FQCN |
|
355 | + * @return array |
|
356 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
357 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
358 | + */ |
|
359 | + protected function getFQCNsFromPartialNamespace($partial_FQCN) |
|
360 | + { |
|
361 | + if (! $this->file_locator instanceof FqcnLocator) { |
|
362 | + $this->file_locator = new FqcnLocator(); |
|
363 | + } |
|
364 | + $this->file_locator->locate($partial_FQCN); |
|
365 | + return $this->file_locator->getFQCNs(); |
|
366 | + } |
|
367 | + |
|
368 | + |
|
369 | + /** |
|
370 | + * @access public |
|
371 | + * @return array |
|
372 | + */ |
|
373 | + public function getCollectionPaths() |
|
374 | + { |
|
375 | + return $this->collection_paths; |
|
376 | + } |
|
377 | + |
|
378 | + |
|
379 | + /** |
|
380 | + * @access public |
|
381 | + * @param string|array $collection_paths |
|
382 | + * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
|
383 | + */ |
|
384 | + public function setCollectionPaths($collection_paths) |
|
385 | + { |
|
386 | + foreach ((array) $collection_paths as $collection_path) { |
|
387 | + if (! empty($collection_path)) { |
|
388 | + if (! is_readable($collection_path)) { |
|
389 | + throw new InvalidFilePathException($collection_path); |
|
390 | + } |
|
391 | + $this->collection_paths[] = $collection_path; |
|
392 | + } |
|
393 | + } |
|
394 | + } |
|
395 | 395 | } |