@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | use EventEspresso\core\exceptions\InvalidInterfaceException; |
6 | 6 | |
7 | 7 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
8 | - exit('No direct script access allowed'); |
|
8 | + exit('No direct script access allowed'); |
|
9 | 9 | } |
10 | 10 | |
11 | 11 | /** |
@@ -21,170 +21,170 @@ discard block |
||
21 | 21 | { |
22 | 22 | |
23 | 23 | |
24 | - /** |
|
25 | - * Constructor. |
|
26 | - * |
|
27 | - * @param EE_Registration[] $data expecting an array of EE_Registration objects. |
|
28 | - * @throws EE_Error |
|
29 | - * @access protected |
|
30 | - */ |
|
31 | - public function __construct($data = array()) |
|
32 | - { |
|
33 | - |
|
34 | - //validate that the first element in the array is an EE_Registration object. |
|
35 | - if (! reset($data) instanceof EE_Registration) { |
|
36 | - throw new EE_Error( |
|
37 | - esc_html__( |
|
38 | - 'The EE_Message_Registrations_incoming_data class expects an array of EE_Registration objects.', |
|
39 | - 'event_espresso' |
|
40 | - ) |
|
41 | - ); |
|
42 | - } |
|
43 | - parent::__construct($data); |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * setup the data. |
|
49 | - * Sets up the expected data object for the messages prep using incoming registration objects. |
|
50 | - * |
|
51 | - * @return void |
|
52 | - * @throws EE_Error |
|
53 | - * @throws EntityNotFoundException |
|
54 | - * @access protected |
|
55 | - */ |
|
56 | - protected function _setup_data() |
|
57 | - { |
|
58 | - //we'll loop through each contact and setup the data needed. Note that many properties will just be set as |
|
59 | - // empty because this data handler is for a very specific set of data (i.e. just what's related to the |
|
60 | - // registration). |
|
61 | - |
|
62 | - $this->reg_objs = $this->data(); |
|
63 | - $this->txn = $this->_maybe_get_transaction(); |
|
64 | - $this->_assemble_data(); |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * If the incoming registrations all share the same transaction then this will return the transaction object shared |
|
70 | - * among the registrations. Otherwise the transaction object is set to null because its intended to only represent |
|
71 | - * one transaction. |
|
72 | - * |
|
73 | - * @return EE_Transaction|null |
|
74 | - * @throws EE_Error |
|
75 | - * @throws EntityNotFoundException |
|
76 | - */ |
|
77 | - protected function _maybe_get_transaction() |
|
78 | - { |
|
79 | - $transactions = array(); |
|
80 | - foreach ($this->reg_objs as $registration) { |
|
81 | - if ($registration instanceof EE_Registration) { |
|
82 | - $transaction = $registration->transaction(); |
|
83 | - if ($transaction instanceof EE_Transaction) { |
|
84 | - $transactions[$transaction->ID()] = $transaction; |
|
85 | - } |
|
86 | - } |
|
87 | - } |
|
88 | - return count($transactions) === 1 ? reset($transactions) : null; |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * Returns database safe representation of the data later used to when instantiating this object. |
|
94 | - * |
|
95 | - * @param array $registrations The incoming data to be prepped. |
|
96 | - * @return EE_Registration[] The data being prepared for the db |
|
97 | - * @throws EE_Error |
|
98 | - * @throws InvalidArgumentException |
|
99 | - * @throws InvalidDataTypeException |
|
100 | - * @throws InvalidInterfaceException |
|
101 | - */ |
|
102 | - public static function convert_data_for_persistent_storage($registrations) |
|
103 | - { |
|
104 | - if (! self::validateRegistrationsForConversion($registrations)) { |
|
105 | - return array(); |
|
106 | - } |
|
107 | - |
|
108 | - //is this an array of ints? |
|
109 | - $first_item = reset($registrations); |
|
110 | - if (is_int($first_item)) { |
|
111 | - return $registrations; |
|
112 | - } |
|
113 | - |
|
114 | - //k nope so let's pull from the registrations |
|
115 | - $registration_ids = array_filter( |
|
116 | - array_map( |
|
117 | - function ($registration) { |
|
118 | - if ($registration instanceof EE_Registration) { |
|
119 | - return $registration->ID(); |
|
120 | - } |
|
121 | - return false; |
|
122 | - }, |
|
123 | - $registrations |
|
124 | - ) |
|
125 | - ); |
|
126 | - |
|
127 | - return $registration_ids; |
|
128 | - } |
|
129 | - |
|
130 | - |
|
131 | - /** |
|
132 | - * This validates incoming registrations (considers whether they are ids or EE_Registration objects. |
|
133 | - * |
|
134 | - * @param array $registrations Could be EE_Registration[] or int[] |
|
135 | - * @return bool |
|
136 | - * @throws EE_Error |
|
137 | - * @throws InvalidArgumentException |
|
138 | - * @throws InvalidDataTypeException |
|
139 | - * @throws InvalidInterfaceException |
|
140 | - */ |
|
141 | - protected static function validateRegistrationsForConversion($registrations) |
|
142 | - { |
|
143 | - if (is_array($registrations)) { |
|
144 | - $first_item = reset($registrations); |
|
145 | - if ($first_item instanceof EE_Registration) { |
|
146 | - return true; |
|
147 | - } |
|
148 | - if (is_int($first_item)) { |
|
149 | - //k let's some basic validation here. This isn't foolproof but better than nothing. |
|
150 | - $count_for_ids = EEM_Registration::instance()->count( |
|
151 | - array( |
|
152 | - array( |
|
153 | - 'REG_ID' => array('IN', $registrations) |
|
154 | - ) |
|
155 | - ) |
|
156 | - ); |
|
157 | - return $count_for_ids === count($registrations); |
|
158 | - } |
|
159 | - } |
|
160 | - return false; |
|
161 | - } |
|
162 | - |
|
163 | - |
|
164 | - /** |
|
165 | - * Data that has been stored in persistent storage that was prepped by _convert_data_for_persistent_storage |
|
166 | - * can be sent into this method and converted back into the format used for instantiating with this data handler. |
|
167 | - * |
|
168 | - * @param array $data |
|
169 | - * @return EE_Registration[] |
|
170 | - * @throws EE_Error |
|
171 | - * @throws InvalidArgumentException |
|
172 | - * @throws InvalidDataTypeException |
|
173 | - * @throws InvalidInterfaceException |
|
174 | - */ |
|
175 | - public static function convert_data_from_persistent_storage($data) |
|
176 | - { |
|
177 | - //since this was added later, we need to account of possible back compat issues where data already queued for |
|
178 | - // generation is in the old format, which is an array of EE_Registration objects. So if that's the case, then |
|
179 | - // let's just return them |
|
180 | - //@see https://events.codebasehq.com/projects/event-espresso/tickets/10127 |
|
181 | - if (is_array($data) && reset($data) instanceof EE_Registration) { |
|
182 | - return $data; |
|
183 | - } |
|
184 | - |
|
185 | - $registrations = is_array($data) |
|
186 | - ? EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $data)))) |
|
187 | - : array(); |
|
188 | - return $registrations; |
|
189 | - } |
|
24 | + /** |
|
25 | + * Constructor. |
|
26 | + * |
|
27 | + * @param EE_Registration[] $data expecting an array of EE_Registration objects. |
|
28 | + * @throws EE_Error |
|
29 | + * @access protected |
|
30 | + */ |
|
31 | + public function __construct($data = array()) |
|
32 | + { |
|
33 | + |
|
34 | + //validate that the first element in the array is an EE_Registration object. |
|
35 | + if (! reset($data) instanceof EE_Registration) { |
|
36 | + throw new EE_Error( |
|
37 | + esc_html__( |
|
38 | + 'The EE_Message_Registrations_incoming_data class expects an array of EE_Registration objects.', |
|
39 | + 'event_espresso' |
|
40 | + ) |
|
41 | + ); |
|
42 | + } |
|
43 | + parent::__construct($data); |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * setup the data. |
|
49 | + * Sets up the expected data object for the messages prep using incoming registration objects. |
|
50 | + * |
|
51 | + * @return void |
|
52 | + * @throws EE_Error |
|
53 | + * @throws EntityNotFoundException |
|
54 | + * @access protected |
|
55 | + */ |
|
56 | + protected function _setup_data() |
|
57 | + { |
|
58 | + //we'll loop through each contact and setup the data needed. Note that many properties will just be set as |
|
59 | + // empty because this data handler is for a very specific set of data (i.e. just what's related to the |
|
60 | + // registration). |
|
61 | + |
|
62 | + $this->reg_objs = $this->data(); |
|
63 | + $this->txn = $this->_maybe_get_transaction(); |
|
64 | + $this->_assemble_data(); |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * If the incoming registrations all share the same transaction then this will return the transaction object shared |
|
70 | + * among the registrations. Otherwise the transaction object is set to null because its intended to only represent |
|
71 | + * one transaction. |
|
72 | + * |
|
73 | + * @return EE_Transaction|null |
|
74 | + * @throws EE_Error |
|
75 | + * @throws EntityNotFoundException |
|
76 | + */ |
|
77 | + protected function _maybe_get_transaction() |
|
78 | + { |
|
79 | + $transactions = array(); |
|
80 | + foreach ($this->reg_objs as $registration) { |
|
81 | + if ($registration instanceof EE_Registration) { |
|
82 | + $transaction = $registration->transaction(); |
|
83 | + if ($transaction instanceof EE_Transaction) { |
|
84 | + $transactions[$transaction->ID()] = $transaction; |
|
85 | + } |
|
86 | + } |
|
87 | + } |
|
88 | + return count($transactions) === 1 ? reset($transactions) : null; |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * Returns database safe representation of the data later used to when instantiating this object. |
|
94 | + * |
|
95 | + * @param array $registrations The incoming data to be prepped. |
|
96 | + * @return EE_Registration[] The data being prepared for the db |
|
97 | + * @throws EE_Error |
|
98 | + * @throws InvalidArgumentException |
|
99 | + * @throws InvalidDataTypeException |
|
100 | + * @throws InvalidInterfaceException |
|
101 | + */ |
|
102 | + public static function convert_data_for_persistent_storage($registrations) |
|
103 | + { |
|
104 | + if (! self::validateRegistrationsForConversion($registrations)) { |
|
105 | + return array(); |
|
106 | + } |
|
107 | + |
|
108 | + //is this an array of ints? |
|
109 | + $first_item = reset($registrations); |
|
110 | + if (is_int($first_item)) { |
|
111 | + return $registrations; |
|
112 | + } |
|
113 | + |
|
114 | + //k nope so let's pull from the registrations |
|
115 | + $registration_ids = array_filter( |
|
116 | + array_map( |
|
117 | + function ($registration) { |
|
118 | + if ($registration instanceof EE_Registration) { |
|
119 | + return $registration->ID(); |
|
120 | + } |
|
121 | + return false; |
|
122 | + }, |
|
123 | + $registrations |
|
124 | + ) |
|
125 | + ); |
|
126 | + |
|
127 | + return $registration_ids; |
|
128 | + } |
|
129 | + |
|
130 | + |
|
131 | + /** |
|
132 | + * This validates incoming registrations (considers whether they are ids or EE_Registration objects. |
|
133 | + * |
|
134 | + * @param array $registrations Could be EE_Registration[] or int[] |
|
135 | + * @return bool |
|
136 | + * @throws EE_Error |
|
137 | + * @throws InvalidArgumentException |
|
138 | + * @throws InvalidDataTypeException |
|
139 | + * @throws InvalidInterfaceException |
|
140 | + */ |
|
141 | + protected static function validateRegistrationsForConversion($registrations) |
|
142 | + { |
|
143 | + if (is_array($registrations)) { |
|
144 | + $first_item = reset($registrations); |
|
145 | + if ($first_item instanceof EE_Registration) { |
|
146 | + return true; |
|
147 | + } |
|
148 | + if (is_int($first_item)) { |
|
149 | + //k let's some basic validation here. This isn't foolproof but better than nothing. |
|
150 | + $count_for_ids = EEM_Registration::instance()->count( |
|
151 | + array( |
|
152 | + array( |
|
153 | + 'REG_ID' => array('IN', $registrations) |
|
154 | + ) |
|
155 | + ) |
|
156 | + ); |
|
157 | + return $count_for_ids === count($registrations); |
|
158 | + } |
|
159 | + } |
|
160 | + return false; |
|
161 | + } |
|
162 | + |
|
163 | + |
|
164 | + /** |
|
165 | + * Data that has been stored in persistent storage that was prepped by _convert_data_for_persistent_storage |
|
166 | + * can be sent into this method and converted back into the format used for instantiating with this data handler. |
|
167 | + * |
|
168 | + * @param array $data |
|
169 | + * @return EE_Registration[] |
|
170 | + * @throws EE_Error |
|
171 | + * @throws InvalidArgumentException |
|
172 | + * @throws InvalidDataTypeException |
|
173 | + * @throws InvalidInterfaceException |
|
174 | + */ |
|
175 | + public static function convert_data_from_persistent_storage($data) |
|
176 | + { |
|
177 | + //since this was added later, we need to account of possible back compat issues where data already queued for |
|
178 | + // generation is in the old format, which is an array of EE_Registration objects. So if that's the case, then |
|
179 | + // let's just return them |
|
180 | + //@see https://events.codebasehq.com/projects/event-espresso/tickets/10127 |
|
181 | + if (is_array($data) && reset($data) instanceof EE_Registration) { |
|
182 | + return $data; |
|
183 | + } |
|
184 | + |
|
185 | + $registrations = is_array($data) |
|
186 | + ? EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $data)))) |
|
187 | + : array(); |
|
188 | + return $registrations; |
|
189 | + } |
|
190 | 190 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | use EventEspresso\core\exceptions\InvalidDataTypeException; |
5 | 5 | use EventEspresso\core\exceptions\InvalidInterfaceException; |
6 | 6 | |
7 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
7 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
8 | 8 | exit('No direct script access allowed'); |
9 | 9 | } |
10 | 10 | |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | { |
33 | 33 | |
34 | 34 | //validate that the first element in the array is an EE_Registration object. |
35 | - if (! reset($data) instanceof EE_Registration) { |
|
35 | + if ( ! reset($data) instanceof EE_Registration) { |
|
36 | 36 | throw new EE_Error( |
37 | 37 | esc_html__( |
38 | 38 | 'The EE_Message_Registrations_incoming_data class expects an array of EE_Registration objects.', |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | */ |
102 | 102 | public static function convert_data_for_persistent_storage($registrations) |
103 | 103 | { |
104 | - if (! self::validateRegistrationsForConversion($registrations)) { |
|
104 | + if ( ! self::validateRegistrationsForConversion($registrations)) { |
|
105 | 105 | return array(); |
106 | 106 | } |
107 | 107 | |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | //k nope so let's pull from the registrations |
115 | 115 | $registration_ids = array_filter( |
116 | 116 | array_map( |
117 | - function ($registration) { |
|
117 | + function($registration) { |
|
118 | 118 | if ($registration instanceof EE_Registration) { |
119 | 119 | return $registration->ID(); |
120 | 120 | } |