@@ -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,353 +42,353 @@ |
||
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 | - * defaults to "*.php" |
|
95 | - * |
|
96 | - * @var string $file_mask |
|
97 | - */ |
|
98 | - protected $file_mask = ''; |
|
99 | - |
|
100 | - /** |
|
101 | - * if the $identifier_type above is set to CollectionDetails::ID_CALLBACK_METHOD, |
|
102 | - * then this specifies the method to use on each entity. |
|
103 | - * If the callback method does not exist, then an exception will be thrown |
|
104 | - * |
|
105 | - * @var string $identifier_callback |
|
106 | - */ |
|
107 | - protected $identifier_callback = ''; |
|
108 | - |
|
109 | - /** |
|
110 | - * an array of Fully Qualified Class Names |
|
111 | - * for example: |
|
112 | - * $FQCNs = array( |
|
113 | - * '/Fully/Qualified/ClassNameA' |
|
114 | - * '/Fully/Qualified/Other/ClassNameB' |
|
115 | - * ); |
|
116 | - * |
|
117 | - * @var array $collection_FQCNs |
|
118 | - */ |
|
119 | - protected $collection_FQCNs = array(); |
|
120 | - |
|
121 | - /** |
|
122 | - * an array of full server paths to folders containing files to be loaded into collection |
|
123 | - * for example: |
|
124 | - * $paths = array( |
|
125 | - * '/full/server/path/to/ClassNameA.ext.php' // for class ClassNameA |
|
126 | - * '/full/server/path/to/other/ClassNameB.php' // for class ClassNameB |
|
127 | - * ); |
|
128 | - * |
|
129 | - * @var array $collection_paths |
|
130 | - */ |
|
131 | - protected $collection_paths = array(); |
|
132 | - |
|
133 | - /** |
|
134 | - * @var LocatorInterface $file_locator |
|
135 | - */ |
|
136 | - protected $file_locator; |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * CollectionDetails constructor. |
|
141 | - * |
|
142 | - * @access public |
|
143 | - * @param string $collection_name |
|
144 | - * @param string $collection_interface |
|
145 | - * @param array $collection_FQCNs |
|
146 | - * @param array $collection_paths |
|
147 | - * @param string $file_mask |
|
148 | - * @param string $identifier_type |
|
149 | - * @param string $identifier_callback |
|
150 | - * @param LocatorInterface $file_locator |
|
151 | - * @throws CollectionDetailsException |
|
152 | - */ |
|
153 | - public function __construct( |
|
154 | - $collection_name, |
|
155 | - $collection_interface, |
|
156 | - array $collection_FQCNs = array(), |
|
157 | - array $collection_paths = array(), |
|
158 | - $file_mask = '', |
|
159 | - $identifier_type = CollectionDetails::ID_OBJECT_HASH, |
|
160 | - $identifier_callback = '', |
|
161 | - LocatorInterface $file_locator = null |
|
162 | - ) { |
|
163 | - try { |
|
164 | - $this->setCollectionName($collection_name); |
|
165 | - $this->setCollectionInterface($collection_interface); |
|
166 | - $this->setCollectionFQCNs($collection_FQCNs); |
|
167 | - $this->setCollectionPaths($collection_paths); |
|
168 | - $this->setFileMasks($file_mask); |
|
169 | - $this->setIdentifierType($identifier_type); |
|
170 | - $this->setIdentifierCallback($identifier_callback); |
|
171 | - $this->file_locator = $file_locator; |
|
172 | - } catch (Exception $exception) { |
|
173 | - throw new CollectionDetailsException($exception); |
|
174 | - } |
|
175 | - } |
|
176 | - |
|
177 | - |
|
178 | - /** |
|
179 | - * @access public |
|
180 | - * @return mixed |
|
181 | - */ |
|
182 | - public function getCollectionInterface() |
|
183 | - { |
|
184 | - return $this->collection_interface; |
|
185 | - } |
|
186 | - |
|
187 | - |
|
188 | - /** |
|
189 | - * @access protected |
|
190 | - * @param string $collection_interface |
|
191 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
192 | - */ |
|
193 | - protected function setCollectionInterface($collection_interface) |
|
194 | - { |
|
195 | - if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
196 | - throw new InvalidInterfaceException($collection_interface); |
|
197 | - } |
|
198 | - $this->collection_interface = $collection_interface; |
|
199 | - } |
|
200 | - |
|
201 | - |
|
202 | - /** |
|
203 | - * the collection name will be used for creating dynamic filters |
|
204 | - * |
|
205 | - * @access public |
|
206 | - * @return string |
|
207 | - */ |
|
208 | - public function collectionName() |
|
209 | - { |
|
210 | - return $this->collection_name; |
|
211 | - } |
|
212 | - |
|
213 | - |
|
214 | - /** |
|
215 | - * sanitizes collection name and converts spaces and dashes to underscores |
|
216 | - * |
|
217 | - * @access protected |
|
218 | - * @param string $collection_name |
|
219 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
220 | - */ |
|
221 | - protected function setCollectionName($collection_name) |
|
222 | - { |
|
223 | - if (! is_string($collection_name)) { |
|
224 | - throw new InvalidDataTypeException('$collection_name', $collection_name, 'string'); |
|
225 | - } |
|
226 | - $this->collection_name = str_replace( |
|
227 | - '-', |
|
228 | - '_', |
|
229 | - sanitize_title_with_dashes($collection_name, '', 'save') |
|
230 | - ); |
|
231 | - } |
|
232 | - |
|
233 | - |
|
234 | - /** |
|
235 | - * @access public |
|
236 | - * @return string |
|
237 | - */ |
|
238 | - public function identifierType() |
|
239 | - { |
|
240 | - return $this->identifier_type; |
|
241 | - } |
|
242 | - |
|
243 | - |
|
244 | - /** |
|
245 | - * @access protected |
|
246 | - * @param string $identifier_type |
|
247 | - * @throws InvalidIdentifierException |
|
248 | - */ |
|
249 | - protected function setIdentifierType($identifier_type) |
|
250 | - { |
|
251 | - if ( |
|
252 | - ! ($identifier_type === CollectionDetails::ID_CLASS_NAME |
|
253 | - || $identifier_type === CollectionDetails::ID_OBJECT_HASH |
|
254 | - || $identifier_type === CollectionDetails::ID_CALLBACK_METHOD |
|
255 | - ) |
|
256 | - ) { |
|
257 | - throw new InvalidIdentifierException( |
|
258 | - $identifier_type, |
|
259 | - 'CollectionDetails::ID_CLASS_NAME or CollectionDetails::ID_OBJECT_HASH or CollectionDetails::ID_CALLBACK_METHOD' |
|
260 | - ); |
|
261 | - } |
|
262 | - $this->identifier_type = $identifier_type; |
|
263 | - } |
|
264 | - |
|
265 | - |
|
266 | - /** |
|
267 | - * @access public |
|
268 | - * @return string |
|
269 | - */ |
|
270 | - public function identifierCallback() |
|
271 | - { |
|
272 | - return $this->identifier_callback; |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * @access protected |
|
278 | - * @param string $identifier_callback |
|
279 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
280 | - */ |
|
281 | - protected function setIdentifierCallback($identifier_callback = 'identifier') |
|
282 | - { |
|
283 | - if (! is_string($identifier_callback)) { |
|
284 | - throw new InvalidDataTypeException('$identifier_callback', $identifier_callback, 'string'); |
|
285 | - } |
|
286 | - $this->identifier_callback = $identifier_callback; |
|
287 | - } |
|
288 | - |
|
289 | - |
|
290 | - /** |
|
291 | - * @access public |
|
292 | - * @return string |
|
293 | - */ |
|
294 | - public function getFileMask() |
|
295 | - { |
|
296 | - return $this->file_mask; |
|
297 | - } |
|
298 | - |
|
299 | - |
|
300 | - /** |
|
301 | - * sets the file mask which is then used to filter what files get loaded |
|
302 | - * when searching for classes to add to the collection. Defaults to '*.php' |
|
303 | - * |
|
304 | - * @access protected |
|
305 | - * @param string $file_mask |
|
306 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
307 | - */ |
|
308 | - protected function setFileMasks($file_mask) |
|
309 | - { |
|
310 | - $this->file_mask = ! empty($file_mask) ? $file_mask : '*.php'; |
|
311 | - // we know our default is a string, so if it's not a string now, |
|
312 | - // then that means the incoming parameter was something else |
|
313 | - if (! is_string($this->file_mask)) { |
|
314 | - throw new InvalidDataTypeException('$file_mask', $this->file_mask, 'string'); |
|
315 | - } |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * @access public |
|
321 | - * @return array |
|
322 | - */ |
|
323 | - public function getCollectionFQCNs() |
|
324 | - { |
|
325 | - return $this->collection_FQCNs; |
|
326 | - } |
|
327 | - |
|
328 | - |
|
329 | - /** |
|
330 | - * @access public |
|
331 | - * @param string|array $collection_FQCNs |
|
332 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
333 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
334 | - */ |
|
335 | - public function setCollectionFQCNs($collection_FQCNs) |
|
336 | - { |
|
337 | - foreach ((array) $collection_FQCNs as $collection_FQCN) { |
|
338 | - if (! empty($collection_FQCN)) { |
|
339 | - if (class_exists($collection_FQCN)) { |
|
340 | - $this->collection_FQCNs[] = $collection_FQCN; |
|
341 | - } else { |
|
342 | - foreach ($this->getFQCNsFromPartialNamespace($collection_FQCN) as $FQCN) { |
|
343 | - $this->collection_FQCNs[] = $FQCN; |
|
344 | - } |
|
345 | - } |
|
346 | - } |
|
347 | - } |
|
348 | - } |
|
349 | - |
|
350 | - |
|
351 | - /** |
|
352 | - * @access protected |
|
353 | - * @param string $partial_FQCN |
|
354 | - * @return array |
|
355 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
356 | - * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
357 | - */ |
|
358 | - protected function getFQCNsFromPartialNamespace($partial_FQCN) |
|
359 | - { |
|
360 | - if (! $this->file_locator instanceof FqcnLocator) { |
|
361 | - $this->file_locator = new FqcnLocator(); |
|
362 | - } |
|
363 | - $this->file_locator->locate($partial_FQCN); |
|
364 | - return $this->file_locator->getFQCNs(); |
|
365 | - } |
|
366 | - |
|
367 | - |
|
368 | - /** |
|
369 | - * @access public |
|
370 | - * @return array |
|
371 | - */ |
|
372 | - public function getCollectionPaths() |
|
373 | - { |
|
374 | - return $this->collection_paths; |
|
375 | - } |
|
376 | - |
|
377 | - |
|
378 | - /** |
|
379 | - * @access public |
|
380 | - * @param string|array $collection_paths |
|
381 | - * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
|
382 | - */ |
|
383 | - public function setCollectionPaths($collection_paths) |
|
384 | - { |
|
385 | - foreach ((array) $collection_paths as $collection_path) { |
|
386 | - if (! empty($collection_path)) { |
|
387 | - if (! is_readable($collection_path)) { |
|
388 | - throw new InvalidFilePathException($collection_path); |
|
389 | - } |
|
390 | - $this->collection_paths[] = $collection_path; |
|
391 | - } |
|
392 | - } |
|
393 | - } |
|
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 | + * defaults to "*.php" |
|
95 | + * |
|
96 | + * @var string $file_mask |
|
97 | + */ |
|
98 | + protected $file_mask = ''; |
|
99 | + |
|
100 | + /** |
|
101 | + * if the $identifier_type above is set to CollectionDetails::ID_CALLBACK_METHOD, |
|
102 | + * then this specifies the method to use on each entity. |
|
103 | + * If the callback method does not exist, then an exception will be thrown |
|
104 | + * |
|
105 | + * @var string $identifier_callback |
|
106 | + */ |
|
107 | + protected $identifier_callback = ''; |
|
108 | + |
|
109 | + /** |
|
110 | + * an array of Fully Qualified Class Names |
|
111 | + * for example: |
|
112 | + * $FQCNs = array( |
|
113 | + * '/Fully/Qualified/ClassNameA' |
|
114 | + * '/Fully/Qualified/Other/ClassNameB' |
|
115 | + * ); |
|
116 | + * |
|
117 | + * @var array $collection_FQCNs |
|
118 | + */ |
|
119 | + protected $collection_FQCNs = array(); |
|
120 | + |
|
121 | + /** |
|
122 | + * an array of full server paths to folders containing files to be loaded into collection |
|
123 | + * for example: |
|
124 | + * $paths = array( |
|
125 | + * '/full/server/path/to/ClassNameA.ext.php' // for class ClassNameA |
|
126 | + * '/full/server/path/to/other/ClassNameB.php' // for class ClassNameB |
|
127 | + * ); |
|
128 | + * |
|
129 | + * @var array $collection_paths |
|
130 | + */ |
|
131 | + protected $collection_paths = array(); |
|
132 | + |
|
133 | + /** |
|
134 | + * @var LocatorInterface $file_locator |
|
135 | + */ |
|
136 | + protected $file_locator; |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * CollectionDetails constructor. |
|
141 | + * |
|
142 | + * @access public |
|
143 | + * @param string $collection_name |
|
144 | + * @param string $collection_interface |
|
145 | + * @param array $collection_FQCNs |
|
146 | + * @param array $collection_paths |
|
147 | + * @param string $file_mask |
|
148 | + * @param string $identifier_type |
|
149 | + * @param string $identifier_callback |
|
150 | + * @param LocatorInterface $file_locator |
|
151 | + * @throws CollectionDetailsException |
|
152 | + */ |
|
153 | + public function __construct( |
|
154 | + $collection_name, |
|
155 | + $collection_interface, |
|
156 | + array $collection_FQCNs = array(), |
|
157 | + array $collection_paths = array(), |
|
158 | + $file_mask = '', |
|
159 | + $identifier_type = CollectionDetails::ID_OBJECT_HASH, |
|
160 | + $identifier_callback = '', |
|
161 | + LocatorInterface $file_locator = null |
|
162 | + ) { |
|
163 | + try { |
|
164 | + $this->setCollectionName($collection_name); |
|
165 | + $this->setCollectionInterface($collection_interface); |
|
166 | + $this->setCollectionFQCNs($collection_FQCNs); |
|
167 | + $this->setCollectionPaths($collection_paths); |
|
168 | + $this->setFileMasks($file_mask); |
|
169 | + $this->setIdentifierType($identifier_type); |
|
170 | + $this->setIdentifierCallback($identifier_callback); |
|
171 | + $this->file_locator = $file_locator; |
|
172 | + } catch (Exception $exception) { |
|
173 | + throw new CollectionDetailsException($exception); |
|
174 | + } |
|
175 | + } |
|
176 | + |
|
177 | + |
|
178 | + /** |
|
179 | + * @access public |
|
180 | + * @return mixed |
|
181 | + */ |
|
182 | + public function getCollectionInterface() |
|
183 | + { |
|
184 | + return $this->collection_interface; |
|
185 | + } |
|
186 | + |
|
187 | + |
|
188 | + /** |
|
189 | + * @access protected |
|
190 | + * @param string $collection_interface |
|
191 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
192 | + */ |
|
193 | + protected function setCollectionInterface($collection_interface) |
|
194 | + { |
|
195 | + if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
196 | + throw new InvalidInterfaceException($collection_interface); |
|
197 | + } |
|
198 | + $this->collection_interface = $collection_interface; |
|
199 | + } |
|
200 | + |
|
201 | + |
|
202 | + /** |
|
203 | + * the collection name will be used for creating dynamic filters |
|
204 | + * |
|
205 | + * @access public |
|
206 | + * @return string |
|
207 | + */ |
|
208 | + public function collectionName() |
|
209 | + { |
|
210 | + return $this->collection_name; |
|
211 | + } |
|
212 | + |
|
213 | + |
|
214 | + /** |
|
215 | + * sanitizes collection name and converts spaces and dashes to underscores |
|
216 | + * |
|
217 | + * @access protected |
|
218 | + * @param string $collection_name |
|
219 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
220 | + */ |
|
221 | + protected function setCollectionName($collection_name) |
|
222 | + { |
|
223 | + if (! is_string($collection_name)) { |
|
224 | + throw new InvalidDataTypeException('$collection_name', $collection_name, 'string'); |
|
225 | + } |
|
226 | + $this->collection_name = str_replace( |
|
227 | + '-', |
|
228 | + '_', |
|
229 | + sanitize_title_with_dashes($collection_name, '', 'save') |
|
230 | + ); |
|
231 | + } |
|
232 | + |
|
233 | + |
|
234 | + /** |
|
235 | + * @access public |
|
236 | + * @return string |
|
237 | + */ |
|
238 | + public function identifierType() |
|
239 | + { |
|
240 | + return $this->identifier_type; |
|
241 | + } |
|
242 | + |
|
243 | + |
|
244 | + /** |
|
245 | + * @access protected |
|
246 | + * @param string $identifier_type |
|
247 | + * @throws InvalidIdentifierException |
|
248 | + */ |
|
249 | + protected function setIdentifierType($identifier_type) |
|
250 | + { |
|
251 | + if ( |
|
252 | + ! ($identifier_type === CollectionDetails::ID_CLASS_NAME |
|
253 | + || $identifier_type === CollectionDetails::ID_OBJECT_HASH |
|
254 | + || $identifier_type === CollectionDetails::ID_CALLBACK_METHOD |
|
255 | + ) |
|
256 | + ) { |
|
257 | + throw new InvalidIdentifierException( |
|
258 | + $identifier_type, |
|
259 | + 'CollectionDetails::ID_CLASS_NAME or CollectionDetails::ID_OBJECT_HASH or CollectionDetails::ID_CALLBACK_METHOD' |
|
260 | + ); |
|
261 | + } |
|
262 | + $this->identifier_type = $identifier_type; |
|
263 | + } |
|
264 | + |
|
265 | + |
|
266 | + /** |
|
267 | + * @access public |
|
268 | + * @return string |
|
269 | + */ |
|
270 | + public function identifierCallback() |
|
271 | + { |
|
272 | + return $this->identifier_callback; |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * @access protected |
|
278 | + * @param string $identifier_callback |
|
279 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
280 | + */ |
|
281 | + protected function setIdentifierCallback($identifier_callback = 'identifier') |
|
282 | + { |
|
283 | + if (! is_string($identifier_callback)) { |
|
284 | + throw new InvalidDataTypeException('$identifier_callback', $identifier_callback, 'string'); |
|
285 | + } |
|
286 | + $this->identifier_callback = $identifier_callback; |
|
287 | + } |
|
288 | + |
|
289 | + |
|
290 | + /** |
|
291 | + * @access public |
|
292 | + * @return string |
|
293 | + */ |
|
294 | + public function getFileMask() |
|
295 | + { |
|
296 | + return $this->file_mask; |
|
297 | + } |
|
298 | + |
|
299 | + |
|
300 | + /** |
|
301 | + * sets the file mask which is then used to filter what files get loaded |
|
302 | + * when searching for classes to add to the collection. Defaults to '*.php' |
|
303 | + * |
|
304 | + * @access protected |
|
305 | + * @param string $file_mask |
|
306 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
307 | + */ |
|
308 | + protected function setFileMasks($file_mask) |
|
309 | + { |
|
310 | + $this->file_mask = ! empty($file_mask) ? $file_mask : '*.php'; |
|
311 | + // we know our default is a string, so if it's not a string now, |
|
312 | + // then that means the incoming parameter was something else |
|
313 | + if (! is_string($this->file_mask)) { |
|
314 | + throw new InvalidDataTypeException('$file_mask', $this->file_mask, 'string'); |
|
315 | + } |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * @access public |
|
321 | + * @return array |
|
322 | + */ |
|
323 | + public function getCollectionFQCNs() |
|
324 | + { |
|
325 | + return $this->collection_FQCNs; |
|
326 | + } |
|
327 | + |
|
328 | + |
|
329 | + /** |
|
330 | + * @access public |
|
331 | + * @param string|array $collection_FQCNs |
|
332 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
333 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
334 | + */ |
|
335 | + public function setCollectionFQCNs($collection_FQCNs) |
|
336 | + { |
|
337 | + foreach ((array) $collection_FQCNs as $collection_FQCN) { |
|
338 | + if (! empty($collection_FQCN)) { |
|
339 | + if (class_exists($collection_FQCN)) { |
|
340 | + $this->collection_FQCNs[] = $collection_FQCN; |
|
341 | + } else { |
|
342 | + foreach ($this->getFQCNsFromPartialNamespace($collection_FQCN) as $FQCN) { |
|
343 | + $this->collection_FQCNs[] = $FQCN; |
|
344 | + } |
|
345 | + } |
|
346 | + } |
|
347 | + } |
|
348 | + } |
|
349 | + |
|
350 | + |
|
351 | + /** |
|
352 | + * @access protected |
|
353 | + * @param string $partial_FQCN |
|
354 | + * @return array |
|
355 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
356 | + * @throws \EventEspresso\core\exceptions\InvalidClassException |
|
357 | + */ |
|
358 | + protected function getFQCNsFromPartialNamespace($partial_FQCN) |
|
359 | + { |
|
360 | + if (! $this->file_locator instanceof FqcnLocator) { |
|
361 | + $this->file_locator = new FqcnLocator(); |
|
362 | + } |
|
363 | + $this->file_locator->locate($partial_FQCN); |
|
364 | + return $this->file_locator->getFQCNs(); |
|
365 | + } |
|
366 | + |
|
367 | + |
|
368 | + /** |
|
369 | + * @access public |
|
370 | + * @return array |
|
371 | + */ |
|
372 | + public function getCollectionPaths() |
|
373 | + { |
|
374 | + return $this->collection_paths; |
|
375 | + } |
|
376 | + |
|
377 | + |
|
378 | + /** |
|
379 | + * @access public |
|
380 | + * @param string|array $collection_paths |
|
381 | + * @throws \EventEspresso\core\exceptions\InvalidFilePathException |
|
382 | + */ |
|
383 | + public function setCollectionPaths($collection_paths) |
|
384 | + { |
|
385 | + foreach ((array) $collection_paths as $collection_path) { |
|
386 | + if (! empty($collection_path)) { |
|
387 | + if (! is_readable($collection_path)) { |
|
388 | + throw new InvalidFilePathException($collection_path); |
|
389 | + } |
|
390 | + $this->collection_paths[] = $collection_path; |
|
391 | + } |
|
392 | + } |
|
393 | + } |
|
394 | 394 | } |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | // round current time down to closest 5 minutes to simplify scheduling |
58 | 58 | $this->current_time = $this->roundTimestamp(time(), '5-minutes', false); |
59 | 59 | $this->transients = (array) get_option(TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, array()); |
60 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
|
60 | + if ( ! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
|
61 | 61 | add_action('shutdown', array($this, 'checkTransientCleanupSchedule'), 999); |
62 | 62 | } |
63 | 63 | } |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | */ |
191 | 191 | public function get($transient_key, $standard_cache = true) |
192 | 192 | { |
193 | - if (isset($this->transients[ $transient_key ])) { |
|
193 | + if (isset($this->transients[$transient_key])) { |
|
194 | 194 | // to avoid cache stampedes (AKA:dogpiles) for standard cache items, |
195 | 195 | // check if known cache expires within the next minute, |
196 | 196 | // and if so, remove it from our tracking and and return nothing. |
@@ -199,17 +199,17 @@ discard block |
||
199 | 199 | // until it gets replaced with the refreshed content |
200 | 200 | if ( |
201 | 201 | $standard_cache |
202 | - && $this->transients[ $transient_key ] - time() <= MINUTE_IN_SECONDS |
|
202 | + && $this->transients[$transient_key] - time() <= MINUTE_IN_SECONDS |
|
203 | 203 | ) { |
204 | - unset($this->transients[ $transient_key ]); |
|
204 | + unset($this->transients[$transient_key]); |
|
205 | 205 | $this->updateTransients(); |
206 | 206 | return null; |
207 | 207 | } |
208 | 208 | |
209 | 209 | // for non standard cache items, remove the key from our tracking, |
210 | 210 | // but proceed to retrieve the transient so that it also gets removed from the db |
211 | - if ($this->transients[ $transient_key ] <= time()) { |
|
212 | - unset($this->transients[ $transient_key ]); |
|
211 | + if ($this->transients[$transient_key] <= time()) { |
|
212 | + unset($this->transients[$transient_key]); |
|
213 | 213 | $this->updateTransients(); |
214 | 214 | } |
215 | 215 | } |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | // and round to the closest 15 minutes |
283 | 283 | $expiration = $this->roundTimestamp($expiration); |
284 | 284 | // save transients to clear using their ID as the key to avoid duplicates |
285 | - $this->transients[ $transient_key ] = $expiration; |
|
285 | + $this->transients[$transient_key] = $expiration; |
|
286 | 286 | $this->updateTransients(); |
287 | 287 | } |
288 | 288 | |
@@ -334,8 +334,8 @@ discard block |
||
334 | 334 | if ($expiration > $this->current_time) { |
335 | 335 | continue; |
336 | 336 | } |
337 | - if (! $expiration || ! $transient_key) { |
|
338 | - unset($this->transients[ $transient_key ]); |
|
337 | + if ( ! $expiration || ! $transient_key) { |
|
338 | + unset($this->transients[$transient_key]); |
|
339 | 339 | $update = true; |
340 | 340 | continue; |
341 | 341 | } |
@@ -374,7 +374,7 @@ discard block |
||
374 | 374 | ? str_replace('_transient_', '', $transient_key) |
375 | 375 | : $transient_key; |
376 | 376 | delete_transient($transient_key); |
377 | - unset($this->transients[ $transient_key ]); |
|
377 | + unset($this->transients[$transient_key]); |
|
378 | 378 | $counter++; |
379 | 379 | } |
380 | 380 | return $counter > 0; |
@@ -15,367 +15,367 @@ |
||
15 | 15 | */ |
16 | 16 | class TransientCacheStorage implements CacheStorageInterface |
17 | 17 | { |
18 | - /** |
|
19 | - * wp-option option_name for tracking transients |
|
20 | - * |
|
21 | - * @type string |
|
22 | - */ |
|
23 | - const TRANSIENT_SCHEDULE_OPTIONS_KEY = 'ee_transient_schedule'; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var int $current_time |
|
27 | - */ |
|
28 | - private $current_time; |
|
29 | - |
|
30 | - /** |
|
31 | - * how often to perform transient cleanup |
|
32 | - * |
|
33 | - * @var string $transient_cleanup_frequency |
|
34 | - */ |
|
35 | - private $transient_cleanup_frequency; |
|
36 | - |
|
37 | - /** |
|
38 | - * options for how often to perform transient cleanup |
|
39 | - * |
|
40 | - * @var array $transient_cleanup_frequency_options |
|
41 | - */ |
|
42 | - private $transient_cleanup_frequency_options = array(); |
|
43 | - |
|
44 | - /** |
|
45 | - * @var array $transients |
|
46 | - */ |
|
47 | - private $transients; |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * TransientCacheStorage constructor. |
|
52 | - */ |
|
53 | - public function __construct() |
|
54 | - { |
|
55 | - $this->transient_cleanup_frequency = $this->setTransientCleanupFrequency(); |
|
56 | - // round current time down to closest 5 minutes to simplify scheduling |
|
57 | - $this->current_time = $this->roundTimestamp(time(), '5-minutes', false); |
|
58 | - $this->transients = (array) get_option(TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, array()); |
|
59 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
|
60 | - add_action('shutdown', array($this, 'checkTransientCleanupSchedule'), 999); |
|
61 | - } |
|
62 | - } |
|
63 | - |
|
64 | - |
|
65 | - /** |
|
66 | - * Sets how often transient cleanup occurs |
|
67 | - * |
|
68 | - * @return string |
|
69 | - */ |
|
70 | - private function setTransientCleanupFrequency() |
|
71 | - { |
|
72 | - // sets how often transients are cleaned up |
|
73 | - $this->transient_cleanup_frequency_options = apply_filters( |
|
74 | - 'FHEE__TransientCacheStorage__transient_cleanup_schedule_options', |
|
75 | - array( |
|
76 | - 'off', |
|
77 | - '15-minutes', |
|
78 | - 'hour', |
|
79 | - '12-hours', |
|
80 | - 'day', |
|
81 | - ) |
|
82 | - ); |
|
83 | - $transient_cleanup_frequency = apply_filters( |
|
84 | - 'FHEE__TransientCacheStorage__transient_cleanup_schedule', |
|
85 | - 'hour' |
|
86 | - ); |
|
87 | - return in_array( |
|
88 | - $transient_cleanup_frequency, |
|
89 | - $this->transient_cleanup_frequency_options, |
|
90 | - true |
|
91 | - ) |
|
92 | - ? $transient_cleanup_frequency |
|
93 | - : 'hour'; |
|
94 | - } |
|
95 | - |
|
96 | - |
|
97 | - /** |
|
98 | - * we need to be able to round timestamps off to match the set transient cleanup frequency |
|
99 | - * so if a transient is set to expire at 1:17 pm for example, and our cleanup schedule is every hour, |
|
100 | - * then that timestamp needs to be rounded up to 2:00 pm so that it is removed |
|
101 | - * during the next scheduled cleanup after its expiration. |
|
102 | - * We also round off the current time timestamp to the closest 5 minutes |
|
103 | - * just to make the timestamps a little easier to round which helps with debugging. |
|
104 | - * |
|
105 | - * @param int $timestamp [required] |
|
106 | - * @param string $cleanup_frequency |
|
107 | - * @param bool $round_up |
|
108 | - * @return int |
|
109 | - */ |
|
110 | - private function roundTimestamp($timestamp, $cleanup_frequency = 'hour', $round_up = true) |
|
111 | - { |
|
112 | - $cleanup_frequency = $cleanup_frequency ? $cleanup_frequency : $this->transient_cleanup_frequency; |
|
113 | - // in order to round the time to the closest xx minutes (or hours), |
|
114 | - // we take the minutes (or hours) portion of the timestamp and divide it by xx, |
|
115 | - // round down to a whole number, then multiply by xx to bring us almost back up to where we were |
|
116 | - // why round down ? so the minutes (or hours) don't go over 60 (or 24) |
|
117 | - // and bump the hour, which could bump the day, which could bump the month, etc, |
|
118 | - // which would be bad because we don't always want to round up, |
|
119 | - // but when we do we can easily achieve that by simply adding the desired offset, |
|
120 | - $minutes = '00'; |
|
121 | - $hours = 'H'; |
|
122 | - switch ($cleanup_frequency) { |
|
123 | - case '5-minutes': |
|
124 | - $minutes = floor((int) date('i', $timestamp) / 5) * 5; |
|
125 | - $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
126 | - $offset = MINUTE_IN_SECONDS * 5; |
|
127 | - break; |
|
128 | - case '15-minutes': |
|
129 | - $minutes = floor((int) date('i', $timestamp) / 15) * 15; |
|
130 | - $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
131 | - $offset = MINUTE_IN_SECONDS * 15; |
|
132 | - break; |
|
133 | - case '12-hours': |
|
134 | - $hours = floor((int) date('H', $timestamp) / 12) * 12; |
|
135 | - $hours = str_pad($hours, 2, '0', STR_PAD_LEFT); |
|
136 | - $offset = HOUR_IN_SECONDS * 12; |
|
137 | - break; |
|
138 | - case 'day': |
|
139 | - $hours = '03'; // run cleanup at 3:00 am (or first site hit after that) |
|
140 | - $offset = DAY_IN_SECONDS; |
|
141 | - break; |
|
142 | - case 'hour': |
|
143 | - default: |
|
144 | - $offset = HOUR_IN_SECONDS; |
|
145 | - break; |
|
146 | - } |
|
147 | - $rounded_timestamp = (int) strtotime(date("Y-m-d {$hours}:{$minutes}:00", $timestamp)); |
|
148 | - $rounded_timestamp += $round_up ? $offset : 0; |
|
149 | - return apply_filters( |
|
150 | - 'FHEE__TransientCacheStorage__roundTimestamp__timestamp', |
|
151 | - $rounded_timestamp, |
|
152 | - $timestamp, |
|
153 | - $cleanup_frequency, |
|
154 | - $round_up |
|
155 | - ); |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * Saves supplied data to a transient |
|
161 | - * if an expiration is set, then it automatically schedules the transient for cleanup |
|
162 | - * |
|
163 | - * @param string $transient_key [required] |
|
164 | - * @param string $data [required] |
|
165 | - * @param int $expiration number of seconds until the cache expires |
|
166 | - * @return bool |
|
167 | - */ |
|
168 | - public function add($transient_key, $data, $expiration = 0) |
|
169 | - { |
|
170 | - $expiration = (int) abs($expiration); |
|
171 | - $saved = set_transient($transient_key, $data, $expiration); |
|
172 | - if ($saved && $expiration) { |
|
173 | - $this->scheduleTransientCleanup($transient_key, $expiration); |
|
174 | - } |
|
175 | - return $saved; |
|
176 | - } |
|
177 | - |
|
178 | - |
|
179 | - /** |
|
180 | - * retrieves transient data |
|
181 | - * automatically triggers early cache refresh for standard cache items |
|
182 | - * in order to avoid cache stampedes on busy sites. |
|
183 | - * For non-standard cache items like PHP Session data where early refreshing is not wanted, |
|
184 | - * the $standard_cache parameter should be set to false when retrieving data |
|
185 | - * |
|
186 | - * @param string $transient_key [required] |
|
187 | - * @param bool $standard_cache |
|
188 | - * @return mixed|null |
|
189 | - */ |
|
190 | - public function get($transient_key, $standard_cache = true) |
|
191 | - { |
|
192 | - if (isset($this->transients[ $transient_key ])) { |
|
193 | - // to avoid cache stampedes (AKA:dogpiles) for standard cache items, |
|
194 | - // check if known cache expires within the next minute, |
|
195 | - // and if so, remove it from our tracking and and return nothing. |
|
196 | - // this should trigger the cache content to be regenerated during this request, |
|
197 | - // while allowing any following requests to still access the existing cache |
|
198 | - // until it gets replaced with the refreshed content |
|
199 | - if ( |
|
200 | - $standard_cache |
|
201 | - && $this->transients[ $transient_key ] - time() <= MINUTE_IN_SECONDS |
|
202 | - ) { |
|
203 | - unset($this->transients[ $transient_key ]); |
|
204 | - $this->updateTransients(); |
|
205 | - return null; |
|
206 | - } |
|
207 | - |
|
208 | - // for non standard cache items, remove the key from our tracking, |
|
209 | - // but proceed to retrieve the transient so that it also gets removed from the db |
|
210 | - if ($this->transients[ $transient_key ] <= time()) { |
|
211 | - unset($this->transients[ $transient_key ]); |
|
212 | - $this->updateTransients(); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - $content = get_transient($transient_key); |
|
217 | - return $content !== false ? $content : null; |
|
218 | - } |
|
219 | - |
|
220 | - |
|
221 | - /** |
|
222 | - * delete a single transient and remove tracking |
|
223 | - * |
|
224 | - * @param string $transient_key [required] full or partial transient key to be deleted |
|
225 | - */ |
|
226 | - public function delete($transient_key) |
|
227 | - { |
|
228 | - $this->deleteMany(array($transient_key)); |
|
229 | - } |
|
230 | - |
|
231 | - |
|
232 | - /** |
|
233 | - * delete multiple transients and remove tracking |
|
234 | - * |
|
235 | - * @param array $transient_keys [required] array of full or partial transient keys to be deleted |
|
236 | - * @param bool $force_delete [optional] if true, then will not check incoming keys against those being tracked |
|
237 | - * and proceed directly to deleting those entries from the cache storage |
|
238 | - */ |
|
239 | - public function deleteMany(array $transient_keys, $force_delete = false) |
|
240 | - { |
|
241 | - $full_transient_keys = $force_delete ? $transient_keys : array(); |
|
242 | - if (empty($full_transient_keys)) { |
|
243 | - foreach ($this->transients as $transient_key => $expiration) { |
|
244 | - foreach ($transient_keys as $transient_key_to_delete) { |
|
245 | - if (strpos($transient_key, $transient_key_to_delete) !== false) { |
|
246 | - $full_transient_keys[] = $transient_key; |
|
247 | - } |
|
248 | - } |
|
249 | - } |
|
250 | - } |
|
251 | - if ($this->deleteTransientKeys($full_transient_keys)) { |
|
252 | - $this->updateTransients(); |
|
253 | - } |
|
254 | - } |
|
255 | - |
|
256 | - |
|
257 | - /** |
|
258 | - * sorts transients numerically by timestamp |
|
259 | - * then saves the transient schedule to a WP option |
|
260 | - */ |
|
261 | - private function updateTransients() |
|
262 | - { |
|
263 | - asort($this->transients, SORT_NUMERIC); |
|
264 | - update_option( |
|
265 | - TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, |
|
266 | - $this->transients |
|
267 | - ); |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - /** |
|
272 | - * schedules a transient for cleanup by adding it to the transient tracking |
|
273 | - * |
|
274 | - * @param string $transient_key [required] |
|
275 | - * @param int $expiration [required] |
|
276 | - */ |
|
277 | - private function scheduleTransientCleanup($transient_key, $expiration) |
|
278 | - { |
|
279 | - // make sure a valid future timestamp is set |
|
280 | - $expiration += $expiration < time() ? time() : 0; |
|
281 | - // and round to the closest 15 minutes |
|
282 | - $expiration = $this->roundTimestamp($expiration); |
|
283 | - // save transients to clear using their ID as the key to avoid duplicates |
|
284 | - $this->transients[ $transient_key ] = $expiration; |
|
285 | - $this->updateTransients(); |
|
286 | - } |
|
287 | - |
|
288 | - |
|
289 | - /** |
|
290 | - * Since our tracked transients are sorted by their timestamps |
|
291 | - * we can grab the first transient and see when it is scheduled for cleanup. |
|
292 | - * If that timestamp is less than or equal to the current time, |
|
293 | - * then cleanup is triggered |
|
294 | - */ |
|
295 | - public function checkTransientCleanupSchedule() |
|
296 | - { |
|
297 | - if (empty($this->transients)) { |
|
298 | - return; |
|
299 | - } |
|
300 | - // when do we run the next cleanup job? |
|
301 | - reset($this->transients); |
|
302 | - $next_scheduled_cleanup = current($this->transients); |
|
303 | - // if the next cleanup job is scheduled for the current hour |
|
304 | - if ($next_scheduled_cleanup <= $this->current_time) { |
|
305 | - if ($this->cleanupExpiredTransients()) { |
|
306 | - $this->updateTransients(); |
|
307 | - } |
|
308 | - } |
|
309 | - } |
|
310 | - |
|
311 | - |
|
312 | - /** |
|
313 | - * loops through the array of tracked transients, |
|
314 | - * compiles a list of those that have expired, and sends that list off for deletion. |
|
315 | - * Also removes any bad records from the transients array |
|
316 | - * |
|
317 | - * @return bool |
|
318 | - */ |
|
319 | - private function cleanupExpiredTransients() |
|
320 | - { |
|
321 | - $update = false; |
|
322 | - // filter the query limit. Set to 0 to turn off garbage collection |
|
323 | - $limit = (int) abs( |
|
324 | - apply_filters( |
|
325 | - 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
326 | - 50 |
|
327 | - ) |
|
328 | - ); |
|
329 | - // non-zero LIMIT means take out the trash |
|
330 | - if ($limit) { |
|
331 | - $transient_keys = array(); |
|
332 | - foreach ($this->transients as $transient_key => $expiration) { |
|
333 | - if ($expiration > $this->current_time) { |
|
334 | - continue; |
|
335 | - } |
|
336 | - if (! $expiration || ! $transient_key) { |
|
337 | - unset($this->transients[ $transient_key ]); |
|
338 | - $update = true; |
|
339 | - continue; |
|
340 | - } |
|
341 | - $transient_keys[] = $transient_key; |
|
342 | - } |
|
343 | - // delete expired keys, but maintain value of $update if nothing is deleted |
|
344 | - $update = $this->deleteTransientKeys($transient_keys, $limit) ? true : $update; |
|
345 | - do_action('FHEE__TransientCacheStorage__clearExpiredTransients__end', $this); |
|
346 | - } |
|
347 | - return $update; |
|
348 | - } |
|
349 | - |
|
350 | - |
|
351 | - /** |
|
352 | - * calls delete_transient() on each transient key provided, up to the specified limit |
|
353 | - * |
|
354 | - * @param array $transient_keys [required] |
|
355 | - * @param int $limit |
|
356 | - * @return bool |
|
357 | - */ |
|
358 | - private function deleteTransientKeys(array $transient_keys, $limit = 50) |
|
359 | - { |
|
360 | - if (empty($transient_keys)) { |
|
361 | - return false; |
|
362 | - } |
|
363 | - $counter = 0; |
|
364 | - foreach ($transient_keys as $transient_key) { |
|
365 | - if ($counter === $limit) { |
|
366 | - break; |
|
367 | - } |
|
368 | - // remove any transient prefixes |
|
369 | - $transient_key = strpos($transient_key, '_transient_timeout_') === 0 |
|
370 | - ? str_replace('_transient_timeout_', '', $transient_key) |
|
371 | - : $transient_key; |
|
372 | - $transient_key = strpos($transient_key, '_transient_') === 0 |
|
373 | - ? str_replace('_transient_', '', $transient_key) |
|
374 | - : $transient_key; |
|
375 | - delete_transient($transient_key); |
|
376 | - unset($this->transients[ $transient_key ]); |
|
377 | - $counter++; |
|
378 | - } |
|
379 | - return $counter > 0; |
|
380 | - } |
|
18 | + /** |
|
19 | + * wp-option option_name for tracking transients |
|
20 | + * |
|
21 | + * @type string |
|
22 | + */ |
|
23 | + const TRANSIENT_SCHEDULE_OPTIONS_KEY = 'ee_transient_schedule'; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var int $current_time |
|
27 | + */ |
|
28 | + private $current_time; |
|
29 | + |
|
30 | + /** |
|
31 | + * how often to perform transient cleanup |
|
32 | + * |
|
33 | + * @var string $transient_cleanup_frequency |
|
34 | + */ |
|
35 | + private $transient_cleanup_frequency; |
|
36 | + |
|
37 | + /** |
|
38 | + * options for how often to perform transient cleanup |
|
39 | + * |
|
40 | + * @var array $transient_cleanup_frequency_options |
|
41 | + */ |
|
42 | + private $transient_cleanup_frequency_options = array(); |
|
43 | + |
|
44 | + /** |
|
45 | + * @var array $transients |
|
46 | + */ |
|
47 | + private $transients; |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * TransientCacheStorage constructor. |
|
52 | + */ |
|
53 | + public function __construct() |
|
54 | + { |
|
55 | + $this->transient_cleanup_frequency = $this->setTransientCleanupFrequency(); |
|
56 | + // round current time down to closest 5 minutes to simplify scheduling |
|
57 | + $this->current_time = $this->roundTimestamp(time(), '5-minutes', false); |
|
58 | + $this->transients = (array) get_option(TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, array()); |
|
59 | + if (! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
|
60 | + add_action('shutdown', array($this, 'checkTransientCleanupSchedule'), 999); |
|
61 | + } |
|
62 | + } |
|
63 | + |
|
64 | + |
|
65 | + /** |
|
66 | + * Sets how often transient cleanup occurs |
|
67 | + * |
|
68 | + * @return string |
|
69 | + */ |
|
70 | + private function setTransientCleanupFrequency() |
|
71 | + { |
|
72 | + // sets how often transients are cleaned up |
|
73 | + $this->transient_cleanup_frequency_options = apply_filters( |
|
74 | + 'FHEE__TransientCacheStorage__transient_cleanup_schedule_options', |
|
75 | + array( |
|
76 | + 'off', |
|
77 | + '15-minutes', |
|
78 | + 'hour', |
|
79 | + '12-hours', |
|
80 | + 'day', |
|
81 | + ) |
|
82 | + ); |
|
83 | + $transient_cleanup_frequency = apply_filters( |
|
84 | + 'FHEE__TransientCacheStorage__transient_cleanup_schedule', |
|
85 | + 'hour' |
|
86 | + ); |
|
87 | + return in_array( |
|
88 | + $transient_cleanup_frequency, |
|
89 | + $this->transient_cleanup_frequency_options, |
|
90 | + true |
|
91 | + ) |
|
92 | + ? $transient_cleanup_frequency |
|
93 | + : 'hour'; |
|
94 | + } |
|
95 | + |
|
96 | + |
|
97 | + /** |
|
98 | + * we need to be able to round timestamps off to match the set transient cleanup frequency |
|
99 | + * so if a transient is set to expire at 1:17 pm for example, and our cleanup schedule is every hour, |
|
100 | + * then that timestamp needs to be rounded up to 2:00 pm so that it is removed |
|
101 | + * during the next scheduled cleanup after its expiration. |
|
102 | + * We also round off the current time timestamp to the closest 5 minutes |
|
103 | + * just to make the timestamps a little easier to round which helps with debugging. |
|
104 | + * |
|
105 | + * @param int $timestamp [required] |
|
106 | + * @param string $cleanup_frequency |
|
107 | + * @param bool $round_up |
|
108 | + * @return int |
|
109 | + */ |
|
110 | + private function roundTimestamp($timestamp, $cleanup_frequency = 'hour', $round_up = true) |
|
111 | + { |
|
112 | + $cleanup_frequency = $cleanup_frequency ? $cleanup_frequency : $this->transient_cleanup_frequency; |
|
113 | + // in order to round the time to the closest xx minutes (or hours), |
|
114 | + // we take the minutes (or hours) portion of the timestamp and divide it by xx, |
|
115 | + // round down to a whole number, then multiply by xx to bring us almost back up to where we were |
|
116 | + // why round down ? so the minutes (or hours) don't go over 60 (or 24) |
|
117 | + // and bump the hour, which could bump the day, which could bump the month, etc, |
|
118 | + // which would be bad because we don't always want to round up, |
|
119 | + // but when we do we can easily achieve that by simply adding the desired offset, |
|
120 | + $minutes = '00'; |
|
121 | + $hours = 'H'; |
|
122 | + switch ($cleanup_frequency) { |
|
123 | + case '5-minutes': |
|
124 | + $minutes = floor((int) date('i', $timestamp) / 5) * 5; |
|
125 | + $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
126 | + $offset = MINUTE_IN_SECONDS * 5; |
|
127 | + break; |
|
128 | + case '15-minutes': |
|
129 | + $minutes = floor((int) date('i', $timestamp) / 15) * 15; |
|
130 | + $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
131 | + $offset = MINUTE_IN_SECONDS * 15; |
|
132 | + break; |
|
133 | + case '12-hours': |
|
134 | + $hours = floor((int) date('H', $timestamp) / 12) * 12; |
|
135 | + $hours = str_pad($hours, 2, '0', STR_PAD_LEFT); |
|
136 | + $offset = HOUR_IN_SECONDS * 12; |
|
137 | + break; |
|
138 | + case 'day': |
|
139 | + $hours = '03'; // run cleanup at 3:00 am (or first site hit after that) |
|
140 | + $offset = DAY_IN_SECONDS; |
|
141 | + break; |
|
142 | + case 'hour': |
|
143 | + default: |
|
144 | + $offset = HOUR_IN_SECONDS; |
|
145 | + break; |
|
146 | + } |
|
147 | + $rounded_timestamp = (int) strtotime(date("Y-m-d {$hours}:{$minutes}:00", $timestamp)); |
|
148 | + $rounded_timestamp += $round_up ? $offset : 0; |
|
149 | + return apply_filters( |
|
150 | + 'FHEE__TransientCacheStorage__roundTimestamp__timestamp', |
|
151 | + $rounded_timestamp, |
|
152 | + $timestamp, |
|
153 | + $cleanup_frequency, |
|
154 | + $round_up |
|
155 | + ); |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * Saves supplied data to a transient |
|
161 | + * if an expiration is set, then it automatically schedules the transient for cleanup |
|
162 | + * |
|
163 | + * @param string $transient_key [required] |
|
164 | + * @param string $data [required] |
|
165 | + * @param int $expiration number of seconds until the cache expires |
|
166 | + * @return bool |
|
167 | + */ |
|
168 | + public function add($transient_key, $data, $expiration = 0) |
|
169 | + { |
|
170 | + $expiration = (int) abs($expiration); |
|
171 | + $saved = set_transient($transient_key, $data, $expiration); |
|
172 | + if ($saved && $expiration) { |
|
173 | + $this->scheduleTransientCleanup($transient_key, $expiration); |
|
174 | + } |
|
175 | + return $saved; |
|
176 | + } |
|
177 | + |
|
178 | + |
|
179 | + /** |
|
180 | + * retrieves transient data |
|
181 | + * automatically triggers early cache refresh for standard cache items |
|
182 | + * in order to avoid cache stampedes on busy sites. |
|
183 | + * For non-standard cache items like PHP Session data where early refreshing is not wanted, |
|
184 | + * the $standard_cache parameter should be set to false when retrieving data |
|
185 | + * |
|
186 | + * @param string $transient_key [required] |
|
187 | + * @param bool $standard_cache |
|
188 | + * @return mixed|null |
|
189 | + */ |
|
190 | + public function get($transient_key, $standard_cache = true) |
|
191 | + { |
|
192 | + if (isset($this->transients[ $transient_key ])) { |
|
193 | + // to avoid cache stampedes (AKA:dogpiles) for standard cache items, |
|
194 | + // check if known cache expires within the next minute, |
|
195 | + // and if so, remove it from our tracking and and return nothing. |
|
196 | + // this should trigger the cache content to be regenerated during this request, |
|
197 | + // while allowing any following requests to still access the existing cache |
|
198 | + // until it gets replaced with the refreshed content |
|
199 | + if ( |
|
200 | + $standard_cache |
|
201 | + && $this->transients[ $transient_key ] - time() <= MINUTE_IN_SECONDS |
|
202 | + ) { |
|
203 | + unset($this->transients[ $transient_key ]); |
|
204 | + $this->updateTransients(); |
|
205 | + return null; |
|
206 | + } |
|
207 | + |
|
208 | + // for non standard cache items, remove the key from our tracking, |
|
209 | + // but proceed to retrieve the transient so that it also gets removed from the db |
|
210 | + if ($this->transients[ $transient_key ] <= time()) { |
|
211 | + unset($this->transients[ $transient_key ]); |
|
212 | + $this->updateTransients(); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + $content = get_transient($transient_key); |
|
217 | + return $content !== false ? $content : null; |
|
218 | + } |
|
219 | + |
|
220 | + |
|
221 | + /** |
|
222 | + * delete a single transient and remove tracking |
|
223 | + * |
|
224 | + * @param string $transient_key [required] full or partial transient key to be deleted |
|
225 | + */ |
|
226 | + public function delete($transient_key) |
|
227 | + { |
|
228 | + $this->deleteMany(array($transient_key)); |
|
229 | + } |
|
230 | + |
|
231 | + |
|
232 | + /** |
|
233 | + * delete multiple transients and remove tracking |
|
234 | + * |
|
235 | + * @param array $transient_keys [required] array of full or partial transient keys to be deleted |
|
236 | + * @param bool $force_delete [optional] if true, then will not check incoming keys against those being tracked |
|
237 | + * and proceed directly to deleting those entries from the cache storage |
|
238 | + */ |
|
239 | + public function deleteMany(array $transient_keys, $force_delete = false) |
|
240 | + { |
|
241 | + $full_transient_keys = $force_delete ? $transient_keys : array(); |
|
242 | + if (empty($full_transient_keys)) { |
|
243 | + foreach ($this->transients as $transient_key => $expiration) { |
|
244 | + foreach ($transient_keys as $transient_key_to_delete) { |
|
245 | + if (strpos($transient_key, $transient_key_to_delete) !== false) { |
|
246 | + $full_transient_keys[] = $transient_key; |
|
247 | + } |
|
248 | + } |
|
249 | + } |
|
250 | + } |
|
251 | + if ($this->deleteTransientKeys($full_transient_keys)) { |
|
252 | + $this->updateTransients(); |
|
253 | + } |
|
254 | + } |
|
255 | + |
|
256 | + |
|
257 | + /** |
|
258 | + * sorts transients numerically by timestamp |
|
259 | + * then saves the transient schedule to a WP option |
|
260 | + */ |
|
261 | + private function updateTransients() |
|
262 | + { |
|
263 | + asort($this->transients, SORT_NUMERIC); |
|
264 | + update_option( |
|
265 | + TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, |
|
266 | + $this->transients |
|
267 | + ); |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + /** |
|
272 | + * schedules a transient for cleanup by adding it to the transient tracking |
|
273 | + * |
|
274 | + * @param string $transient_key [required] |
|
275 | + * @param int $expiration [required] |
|
276 | + */ |
|
277 | + private function scheduleTransientCleanup($transient_key, $expiration) |
|
278 | + { |
|
279 | + // make sure a valid future timestamp is set |
|
280 | + $expiration += $expiration < time() ? time() : 0; |
|
281 | + // and round to the closest 15 minutes |
|
282 | + $expiration = $this->roundTimestamp($expiration); |
|
283 | + // save transients to clear using their ID as the key to avoid duplicates |
|
284 | + $this->transients[ $transient_key ] = $expiration; |
|
285 | + $this->updateTransients(); |
|
286 | + } |
|
287 | + |
|
288 | + |
|
289 | + /** |
|
290 | + * Since our tracked transients are sorted by their timestamps |
|
291 | + * we can grab the first transient and see when it is scheduled for cleanup. |
|
292 | + * If that timestamp is less than or equal to the current time, |
|
293 | + * then cleanup is triggered |
|
294 | + */ |
|
295 | + public function checkTransientCleanupSchedule() |
|
296 | + { |
|
297 | + if (empty($this->transients)) { |
|
298 | + return; |
|
299 | + } |
|
300 | + // when do we run the next cleanup job? |
|
301 | + reset($this->transients); |
|
302 | + $next_scheduled_cleanup = current($this->transients); |
|
303 | + // if the next cleanup job is scheduled for the current hour |
|
304 | + if ($next_scheduled_cleanup <= $this->current_time) { |
|
305 | + if ($this->cleanupExpiredTransients()) { |
|
306 | + $this->updateTransients(); |
|
307 | + } |
|
308 | + } |
|
309 | + } |
|
310 | + |
|
311 | + |
|
312 | + /** |
|
313 | + * loops through the array of tracked transients, |
|
314 | + * compiles a list of those that have expired, and sends that list off for deletion. |
|
315 | + * Also removes any bad records from the transients array |
|
316 | + * |
|
317 | + * @return bool |
|
318 | + */ |
|
319 | + private function cleanupExpiredTransients() |
|
320 | + { |
|
321 | + $update = false; |
|
322 | + // filter the query limit. Set to 0 to turn off garbage collection |
|
323 | + $limit = (int) abs( |
|
324 | + apply_filters( |
|
325 | + 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
326 | + 50 |
|
327 | + ) |
|
328 | + ); |
|
329 | + // non-zero LIMIT means take out the trash |
|
330 | + if ($limit) { |
|
331 | + $transient_keys = array(); |
|
332 | + foreach ($this->transients as $transient_key => $expiration) { |
|
333 | + if ($expiration > $this->current_time) { |
|
334 | + continue; |
|
335 | + } |
|
336 | + if (! $expiration || ! $transient_key) { |
|
337 | + unset($this->transients[ $transient_key ]); |
|
338 | + $update = true; |
|
339 | + continue; |
|
340 | + } |
|
341 | + $transient_keys[] = $transient_key; |
|
342 | + } |
|
343 | + // delete expired keys, but maintain value of $update if nothing is deleted |
|
344 | + $update = $this->deleteTransientKeys($transient_keys, $limit) ? true : $update; |
|
345 | + do_action('FHEE__TransientCacheStorage__clearExpiredTransients__end', $this); |
|
346 | + } |
|
347 | + return $update; |
|
348 | + } |
|
349 | + |
|
350 | + |
|
351 | + /** |
|
352 | + * calls delete_transient() on each transient key provided, up to the specified limit |
|
353 | + * |
|
354 | + * @param array $transient_keys [required] |
|
355 | + * @param int $limit |
|
356 | + * @return bool |
|
357 | + */ |
|
358 | + private function deleteTransientKeys(array $transient_keys, $limit = 50) |
|
359 | + { |
|
360 | + if (empty($transient_keys)) { |
|
361 | + return false; |
|
362 | + } |
|
363 | + $counter = 0; |
|
364 | + foreach ($transient_keys as $transient_key) { |
|
365 | + if ($counter === $limit) { |
|
366 | + break; |
|
367 | + } |
|
368 | + // remove any transient prefixes |
|
369 | + $transient_key = strpos($transient_key, '_transient_timeout_') === 0 |
|
370 | + ? str_replace('_transient_timeout_', '', $transient_key) |
|
371 | + : $transient_key; |
|
372 | + $transient_key = strpos($transient_key, '_transient_') === 0 |
|
373 | + ? str_replace('_transient_', '', $transient_key) |
|
374 | + : $transient_key; |
|
375 | + delete_transient($transient_key); |
|
376 | + unset($this->transients[ $transient_key ]); |
|
377 | + $counter++; |
|
378 | + } |
|
379 | + return $counter > 0; |
|
380 | + } |
|
381 | 381 | } |
@@ -71,12 +71,12 @@ discard block |
||
71 | 71 | public function getReflectionClass($class_name) |
72 | 72 | { |
73 | 73 | if ( |
74 | - ! isset($this->reflectors[ $class_name ]) |
|
75 | - || ! $this->reflectors[ $class_name ] instanceof ReflectionClass |
|
74 | + ! isset($this->reflectors[$class_name]) |
|
75 | + || ! $this->reflectors[$class_name] instanceof ReflectionClass |
|
76 | 76 | ) { |
77 | - $this->reflectors[ $class_name ] = new ReflectionClass($class_name); |
|
77 | + $this->reflectors[$class_name] = new ReflectionClass($class_name); |
|
78 | 78 | } |
79 | - return $this->reflectors[ $class_name ]; |
|
79 | + return $this->reflectors[$class_name]; |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | |
@@ -91,12 +91,12 @@ discard block |
||
91 | 91 | protected function getConstructor(ReflectionClass $reflector) |
92 | 92 | { |
93 | 93 | if ( |
94 | - ! isset($this->constructors[ $reflector->getName() ]) |
|
95 | - || ! $this->constructors[ $reflector->getName() ] instanceof ReflectionMethod |
|
94 | + ! isset($this->constructors[$reflector->getName()]) |
|
95 | + || ! $this->constructors[$reflector->getName()] instanceof ReflectionMethod |
|
96 | 96 | ) { |
97 | - $this->constructors[ $reflector->getName() ] = $reflector->getConstructor(); |
|
97 | + $this->constructors[$reflector->getName()] = $reflector->getConstructor(); |
|
98 | 98 | } |
99 | - return $this->constructors[ $reflector->getName() ]; |
|
99 | + return $this->constructors[$reflector->getName()]; |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | |
@@ -110,10 +110,10 @@ discard block |
||
110 | 110 | */ |
111 | 111 | protected function getParameters(ReflectionMethod $constructor) |
112 | 112 | { |
113 | - if (! isset($this->parameters[ $constructor->class ])) { |
|
114 | - $this->parameters[ $constructor->class ] = $constructor->getParameters(); |
|
113 | + if ( ! isset($this->parameters[$constructor->class])) { |
|
114 | + $this->parameters[$constructor->class] = $constructor->getParameters(); |
|
115 | 115 | } |
116 | - return $this->parameters[ $constructor->class ]; |
|
116 | + return $this->parameters[$constructor->class]; |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | // let's examine the constructor |
149 | 149 | $constructor = $this->getConstructor($reflector); |
150 | 150 | // whu? huh? nothing? |
151 | - if (! $constructor) { |
|
151 | + if ( ! $constructor) { |
|
152 | 152 | return $arguments; |
153 | 153 | } |
154 | 154 | // get constructor parameters |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | $argument_keys = array_keys($arguments); |
162 | 162 | // now loop thru all of the constructors expected parameters |
163 | 163 | foreach ($params as $index => $param) { |
164 | - if (! $param instanceof ReflectionParameter) { |
|
164 | + if ( ! $param instanceof ReflectionParameter) { |
|
165 | 165 | continue; |
166 | 166 | } |
167 | 167 | // is this a dependency for a specific class ? |
@@ -169,41 +169,41 @@ discard block |
||
169 | 169 | $param_name = $param->getName() ? $param->getName() : ''; |
170 | 170 | if ( |
171 | 171 | // param is not a class but is specified in the list of ingredients for this Recipe |
172 | - is_string($param_name) && isset($ingredients[ $param_name ]) |
|
172 | + is_string($param_name) && isset($ingredients[$param_name]) |
|
173 | 173 | ) { |
174 | 174 | // attempt to inject the dependency |
175 | - $resolved_parameters[ $index ] = $ingredients[ $param_name ]; |
|
175 | + $resolved_parameters[$index] = $ingredients[$param_name]; |
|
176 | 176 | } elseif ( |
177 | 177 | // param is specified in the list of ingredients for this Recipe |
178 | - isset($ingredients[ $param_class ]) |
|
178 | + isset($ingredients[$param_class]) |
|
179 | 179 | ) { // attempt to inject the dependency |
180 | - $resolved_parameters[ $index ] = $this->injectDependency($reflector, $ingredients[ $param_class ]); |
|
180 | + $resolved_parameters[$index] = $this->injectDependency($reflector, $ingredients[$param_class]); |
|
181 | 181 | } elseif ( |
182 | 182 | // param is not even a class |
183 | 183 | empty($param_class) |
184 | 184 | // and something already exists in the incoming arguments for this param |
185 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
185 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
186 | 186 | ) { |
187 | 187 | // add parameter from incoming arguments |
188 | - $resolved_parameters[ $index ] = $arguments[ $argument_keys[ $index ] ]; |
|
188 | + $resolved_parameters[$index] = $arguments[$argument_keys[$index]]; |
|
189 | 189 | } elseif ( |
190 | 190 | // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
191 | 191 | ! empty($param_class) |
192 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
193 | - && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
192 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
193 | + && $arguments[$argument_keys[$index]] instanceof $param_class |
|
194 | 194 | ) { |
195 | 195 | // add parameter from incoming arguments |
196 | - $resolved_parameters[ $index ] = $arguments[ $argument_keys[ $index ] ]; |
|
196 | + $resolved_parameters[$index] = $arguments[$argument_keys[$index]]; |
|
197 | 197 | } elseif ( |
198 | 198 | // parameter is type hinted as a class, and should be injected |
199 | 199 | ! empty($param_class) |
200 | 200 | ) { |
201 | 201 | // attempt to inject the dependency |
202 | - $resolved_parameters[ $index ] = $this->injectDependency($reflector, $param_class); |
|
202 | + $resolved_parameters[$index] = $this->injectDependency($reflector, $param_class); |
|
203 | 203 | } elseif ($param->isOptional()) { |
204 | - $resolved_parameters[ $index ] = $param->getDefaultValue(); |
|
204 | + $resolved_parameters[$index] = $param->getDefaultValue(); |
|
205 | 205 | } else { |
206 | - $resolved_parameters[ $index ] = null; |
|
206 | + $resolved_parameters[$index] = null; |
|
207 | 207 | } |
208 | 208 | } |
209 | 209 | return $resolved_parameters; |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | private function injectDependency(ReflectionClass $reflector, $param_class) |
220 | 220 | { |
221 | 221 | $dependency = $this->coffee_pot->brew($param_class); |
222 | - if (! $dependency instanceof $param_class) { |
|
222 | + if ( ! $dependency instanceof $param_class) { |
|
223 | 223 | throw new UnexpectedValueException( |
224 | 224 | sprintf( |
225 | 225 | esc_html__( |
@@ -20,216 +20,216 @@ |
||
20 | 20 | */ |
21 | 21 | class DependencyInjector implements InjectorInterface |
22 | 22 | { |
23 | - /** |
|
24 | - * @var CoffeePotInterface $coffee_pot |
|
25 | - */ |
|
26 | - private $coffee_pot; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var EEH_Array $array_helper |
|
30 | - */ |
|
31 | - private $array_helper; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var ReflectionClass[] $reflectors |
|
35 | - */ |
|
36 | - private $reflectors; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var ReflectionMethod[] $constructors |
|
40 | - */ |
|
41 | - private $constructors; |
|
42 | - |
|
43 | - /** |
|
44 | - * @var ReflectionParameter[] $parameters |
|
45 | - */ |
|
46 | - private $parameters; |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * DependencyInjector constructor |
|
51 | - * |
|
52 | - * @param CoffeePotInterface $coffee_pot |
|
53 | - * @param EEH_Array $array_helper |
|
54 | - */ |
|
55 | - public function __construct(CoffeePotInterface $coffee_pot, EEH_Array $array_helper) |
|
56 | - { |
|
57 | - $this->coffee_pot = $coffee_pot; |
|
58 | - $this->array_helper = $array_helper; |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * getReflectionClass |
|
64 | - * checks if a ReflectionClass object has already been generated for a class |
|
65 | - * and returns that instead of creating a new one |
|
66 | - * |
|
67 | - * @param string $class_name |
|
68 | - * @return ReflectionClass |
|
69 | - */ |
|
70 | - public function getReflectionClass($class_name) |
|
71 | - { |
|
72 | - if ( |
|
73 | - ! isset($this->reflectors[ $class_name ]) |
|
74 | - || ! $this->reflectors[ $class_name ] instanceof ReflectionClass |
|
75 | - ) { |
|
76 | - $this->reflectors[ $class_name ] = new ReflectionClass($class_name); |
|
77 | - } |
|
78 | - return $this->reflectors[ $class_name ]; |
|
79 | - } |
|
80 | - |
|
81 | - |
|
82 | - /** |
|
83 | - * getConstructor |
|
84 | - * checks if a ReflectionMethod object has already been generated for the class constructor |
|
85 | - * and returns that instead of creating a new one |
|
86 | - * |
|
87 | - * @param ReflectionClass $reflector |
|
88 | - * @return ReflectionMethod |
|
89 | - */ |
|
90 | - protected function getConstructor(ReflectionClass $reflector) |
|
91 | - { |
|
92 | - if ( |
|
93 | - ! isset($this->constructors[ $reflector->getName() ]) |
|
94 | - || ! $this->constructors[ $reflector->getName() ] instanceof ReflectionMethod |
|
95 | - ) { |
|
96 | - $this->constructors[ $reflector->getName() ] = $reflector->getConstructor(); |
|
97 | - } |
|
98 | - return $this->constructors[ $reflector->getName() ]; |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * getParameters |
|
104 | - * checks if an array of ReflectionParameter objects has already been generated for the class constructor |
|
105 | - * and returns that instead of creating a new one |
|
106 | - * |
|
107 | - * @param ReflectionMethod $constructor |
|
108 | - * @return ReflectionParameter[] |
|
109 | - */ |
|
110 | - protected function getParameters(ReflectionMethod $constructor) |
|
111 | - { |
|
112 | - if (! isset($this->parameters[ $constructor->class ])) { |
|
113 | - $this->parameters[ $constructor->class ] = $constructor->getParameters(); |
|
114 | - } |
|
115 | - return $this->parameters[ $constructor->class ]; |
|
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * resolveDependencies |
|
121 | - * examines the constructor for the requested class to determine |
|
122 | - * if any dependencies exist, and if they can be injected. |
|
123 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
124 | - * PLZ NOTE: this is achieved by type hinting the constructor params |
|
125 | - * For example: |
|
126 | - * if attempting to load a class "Foo" with the following constructor: |
|
127 | - * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
128 | - * then $bar_class and $grohl_class will be added to the $arguments array, |
|
129 | - * but only IF they are NOT already present in the incoming arguments array, |
|
130 | - * and the correct classes can be loaded |
|
131 | - * |
|
132 | - * @param RecipeInterface $recipe |
|
133 | - * @param ReflectionClass $reflector |
|
134 | - * @param array $arguments |
|
135 | - * @return array |
|
136 | - * @throws UnexpectedValueException |
|
137 | - */ |
|
138 | - public function resolveDependencies(RecipeInterface $recipe, ReflectionClass $reflector, $arguments = array()) |
|
139 | - { |
|
140 | - // if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
141 | - // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
142 | - $arguments = $this->array_helper->is_array_numerically_and_sequentially_indexed($arguments) |
|
143 | - ? $arguments |
|
144 | - : array($arguments); |
|
145 | - $resolved_parameters = array(); |
|
146 | - // let's examine the constructor |
|
147 | - // let's examine the constructor |
|
148 | - $constructor = $this->getConstructor($reflector); |
|
149 | - // whu? huh? nothing? |
|
150 | - if (! $constructor) { |
|
151 | - return $arguments; |
|
152 | - } |
|
153 | - // get constructor parameters |
|
154 | - $params = $this->getParameters($constructor); |
|
155 | - if (empty($params)) { |
|
156 | - return $resolved_parameters; |
|
157 | - } |
|
158 | - $ingredients = $recipe->ingredients(); |
|
159 | - // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
160 | - $argument_keys = array_keys($arguments); |
|
161 | - // now loop thru all of the constructors expected parameters |
|
162 | - foreach ($params as $index => $param) { |
|
163 | - if (! $param instanceof ReflectionParameter) { |
|
164 | - continue; |
|
165 | - } |
|
166 | - // is this a dependency for a specific class ? |
|
167 | - $param_class = $param->getClass() ? $param->getClass()->name : ''; |
|
168 | - $param_name = $param->getName() ? $param->getName() : ''; |
|
169 | - if ( |
|
23 | + /** |
|
24 | + * @var CoffeePotInterface $coffee_pot |
|
25 | + */ |
|
26 | + private $coffee_pot; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var EEH_Array $array_helper |
|
30 | + */ |
|
31 | + private $array_helper; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var ReflectionClass[] $reflectors |
|
35 | + */ |
|
36 | + private $reflectors; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var ReflectionMethod[] $constructors |
|
40 | + */ |
|
41 | + private $constructors; |
|
42 | + |
|
43 | + /** |
|
44 | + * @var ReflectionParameter[] $parameters |
|
45 | + */ |
|
46 | + private $parameters; |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * DependencyInjector constructor |
|
51 | + * |
|
52 | + * @param CoffeePotInterface $coffee_pot |
|
53 | + * @param EEH_Array $array_helper |
|
54 | + */ |
|
55 | + public function __construct(CoffeePotInterface $coffee_pot, EEH_Array $array_helper) |
|
56 | + { |
|
57 | + $this->coffee_pot = $coffee_pot; |
|
58 | + $this->array_helper = $array_helper; |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * getReflectionClass |
|
64 | + * checks if a ReflectionClass object has already been generated for a class |
|
65 | + * and returns that instead of creating a new one |
|
66 | + * |
|
67 | + * @param string $class_name |
|
68 | + * @return ReflectionClass |
|
69 | + */ |
|
70 | + public function getReflectionClass($class_name) |
|
71 | + { |
|
72 | + if ( |
|
73 | + ! isset($this->reflectors[ $class_name ]) |
|
74 | + || ! $this->reflectors[ $class_name ] instanceof ReflectionClass |
|
75 | + ) { |
|
76 | + $this->reflectors[ $class_name ] = new ReflectionClass($class_name); |
|
77 | + } |
|
78 | + return $this->reflectors[ $class_name ]; |
|
79 | + } |
|
80 | + |
|
81 | + |
|
82 | + /** |
|
83 | + * getConstructor |
|
84 | + * checks if a ReflectionMethod object has already been generated for the class constructor |
|
85 | + * and returns that instead of creating a new one |
|
86 | + * |
|
87 | + * @param ReflectionClass $reflector |
|
88 | + * @return ReflectionMethod |
|
89 | + */ |
|
90 | + protected function getConstructor(ReflectionClass $reflector) |
|
91 | + { |
|
92 | + if ( |
|
93 | + ! isset($this->constructors[ $reflector->getName() ]) |
|
94 | + || ! $this->constructors[ $reflector->getName() ] instanceof ReflectionMethod |
|
95 | + ) { |
|
96 | + $this->constructors[ $reflector->getName() ] = $reflector->getConstructor(); |
|
97 | + } |
|
98 | + return $this->constructors[ $reflector->getName() ]; |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * getParameters |
|
104 | + * checks if an array of ReflectionParameter objects has already been generated for the class constructor |
|
105 | + * and returns that instead of creating a new one |
|
106 | + * |
|
107 | + * @param ReflectionMethod $constructor |
|
108 | + * @return ReflectionParameter[] |
|
109 | + */ |
|
110 | + protected function getParameters(ReflectionMethod $constructor) |
|
111 | + { |
|
112 | + if (! isset($this->parameters[ $constructor->class ])) { |
|
113 | + $this->parameters[ $constructor->class ] = $constructor->getParameters(); |
|
114 | + } |
|
115 | + return $this->parameters[ $constructor->class ]; |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * resolveDependencies |
|
121 | + * examines the constructor for the requested class to determine |
|
122 | + * if any dependencies exist, and if they can be injected. |
|
123 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
124 | + * PLZ NOTE: this is achieved by type hinting the constructor params |
|
125 | + * For example: |
|
126 | + * if attempting to load a class "Foo" with the following constructor: |
|
127 | + * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
128 | + * then $bar_class and $grohl_class will be added to the $arguments array, |
|
129 | + * but only IF they are NOT already present in the incoming arguments array, |
|
130 | + * and the correct classes can be loaded |
|
131 | + * |
|
132 | + * @param RecipeInterface $recipe |
|
133 | + * @param ReflectionClass $reflector |
|
134 | + * @param array $arguments |
|
135 | + * @return array |
|
136 | + * @throws UnexpectedValueException |
|
137 | + */ |
|
138 | + public function resolveDependencies(RecipeInterface $recipe, ReflectionClass $reflector, $arguments = array()) |
|
139 | + { |
|
140 | + // if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
141 | + // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
142 | + $arguments = $this->array_helper->is_array_numerically_and_sequentially_indexed($arguments) |
|
143 | + ? $arguments |
|
144 | + : array($arguments); |
|
145 | + $resolved_parameters = array(); |
|
146 | + // let's examine the constructor |
|
147 | + // let's examine the constructor |
|
148 | + $constructor = $this->getConstructor($reflector); |
|
149 | + // whu? huh? nothing? |
|
150 | + if (! $constructor) { |
|
151 | + return $arguments; |
|
152 | + } |
|
153 | + // get constructor parameters |
|
154 | + $params = $this->getParameters($constructor); |
|
155 | + if (empty($params)) { |
|
156 | + return $resolved_parameters; |
|
157 | + } |
|
158 | + $ingredients = $recipe->ingredients(); |
|
159 | + // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
160 | + $argument_keys = array_keys($arguments); |
|
161 | + // now loop thru all of the constructors expected parameters |
|
162 | + foreach ($params as $index => $param) { |
|
163 | + if (! $param instanceof ReflectionParameter) { |
|
164 | + continue; |
|
165 | + } |
|
166 | + // is this a dependency for a specific class ? |
|
167 | + $param_class = $param->getClass() ? $param->getClass()->name : ''; |
|
168 | + $param_name = $param->getName() ? $param->getName() : ''; |
|
169 | + if ( |
|
170 | 170 | // param is not a class but is specified in the list of ingredients for this Recipe |
171 | - is_string($param_name) && isset($ingredients[ $param_name ]) |
|
172 | - ) { |
|
173 | - // attempt to inject the dependency |
|
174 | - $resolved_parameters[ $index ] = $ingredients[ $param_name ]; |
|
175 | - } elseif ( |
|
171 | + is_string($param_name) && isset($ingredients[ $param_name ]) |
|
172 | + ) { |
|
173 | + // attempt to inject the dependency |
|
174 | + $resolved_parameters[ $index ] = $ingredients[ $param_name ]; |
|
175 | + } elseif ( |
|
176 | 176 | // param is specified in the list of ingredients for this Recipe |
177 | - isset($ingredients[ $param_class ]) |
|
178 | - ) { // attempt to inject the dependency |
|
179 | - $resolved_parameters[ $index ] = $this->injectDependency($reflector, $ingredients[ $param_class ]); |
|
180 | - } elseif ( |
|
177 | + isset($ingredients[ $param_class ]) |
|
178 | + ) { // attempt to inject the dependency |
|
179 | + $resolved_parameters[ $index ] = $this->injectDependency($reflector, $ingredients[ $param_class ]); |
|
180 | + } elseif ( |
|
181 | 181 | // param is not even a class |
182 | - empty($param_class) |
|
183 | - // and something already exists in the incoming arguments for this param |
|
184 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
185 | - ) { |
|
186 | - // add parameter from incoming arguments |
|
187 | - $resolved_parameters[ $index ] = $arguments[ $argument_keys[ $index ] ]; |
|
188 | - } elseif ( |
|
182 | + empty($param_class) |
|
183 | + // and something already exists in the incoming arguments for this param |
|
184 | + && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
185 | + ) { |
|
186 | + // add parameter from incoming arguments |
|
187 | + $resolved_parameters[ $index ] = $arguments[ $argument_keys[ $index ] ]; |
|
188 | + } elseif ( |
|
189 | 189 | // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
190 | - ! empty($param_class) |
|
191 | - && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
192 | - && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
193 | - ) { |
|
194 | - // add parameter from incoming arguments |
|
195 | - $resolved_parameters[ $index ] = $arguments[ $argument_keys[ $index ] ]; |
|
196 | - } elseif ( |
|
190 | + ! empty($param_class) |
|
191 | + && isset($argument_keys[ $index ], $arguments[ $argument_keys[ $index ] ]) |
|
192 | + && $arguments[ $argument_keys[ $index ] ] instanceof $param_class |
|
193 | + ) { |
|
194 | + // add parameter from incoming arguments |
|
195 | + $resolved_parameters[ $index ] = $arguments[ $argument_keys[ $index ] ]; |
|
196 | + } elseif ( |
|
197 | 197 | // parameter is type hinted as a class, and should be injected |
198 | - ! empty($param_class) |
|
199 | - ) { |
|
200 | - // attempt to inject the dependency |
|
201 | - $resolved_parameters[ $index ] = $this->injectDependency($reflector, $param_class); |
|
202 | - } elseif ($param->isOptional()) { |
|
203 | - $resolved_parameters[ $index ] = $param->getDefaultValue(); |
|
204 | - } else { |
|
205 | - $resolved_parameters[ $index ] = null; |
|
206 | - } |
|
207 | - } |
|
208 | - return $resolved_parameters; |
|
209 | - } |
|
210 | - |
|
211 | - |
|
212 | - /** |
|
213 | - * @param ReflectionClass $reflector |
|
214 | - * @param string $param_class |
|
215 | - * @return mixed |
|
216 | - * @throws UnexpectedValueException |
|
217 | - */ |
|
218 | - private function injectDependency(ReflectionClass $reflector, $param_class) |
|
219 | - { |
|
220 | - $dependency = $this->coffee_pot->brew($param_class); |
|
221 | - if (! $dependency instanceof $param_class) { |
|
222 | - throw new UnexpectedValueException( |
|
223 | - sprintf( |
|
224 | - esc_html__( |
|
225 | - 'Could not resolve dependency for "%1$s" for the "%2$s" class constructor.', |
|
226 | - 'event_espresso' |
|
227 | - ), |
|
228 | - $param_class, |
|
229 | - $reflector->getName() |
|
230 | - ) |
|
231 | - ); |
|
232 | - } |
|
233 | - return $dependency; |
|
234 | - } |
|
198 | + ! empty($param_class) |
|
199 | + ) { |
|
200 | + // attempt to inject the dependency |
|
201 | + $resolved_parameters[ $index ] = $this->injectDependency($reflector, $param_class); |
|
202 | + } elseif ($param->isOptional()) { |
|
203 | + $resolved_parameters[ $index ] = $param->getDefaultValue(); |
|
204 | + } else { |
|
205 | + $resolved_parameters[ $index ] = null; |
|
206 | + } |
|
207 | + } |
|
208 | + return $resolved_parameters; |
|
209 | + } |
|
210 | + |
|
211 | + |
|
212 | + /** |
|
213 | + * @param ReflectionClass $reflector |
|
214 | + * @param string $param_class |
|
215 | + * @return mixed |
|
216 | + * @throws UnexpectedValueException |
|
217 | + */ |
|
218 | + private function injectDependency(ReflectionClass $reflector, $param_class) |
|
219 | + { |
|
220 | + $dependency = $this->coffee_pot->brew($param_class); |
|
221 | + if (! $dependency instanceof $param_class) { |
|
222 | + throw new UnexpectedValueException( |
|
223 | + sprintf( |
|
224 | + esc_html__( |
|
225 | + 'Could not resolve dependency for "%1$s" for the "%2$s" class constructor.', |
|
226 | + 'event_espresso' |
|
227 | + ), |
|
228 | + $param_class, |
|
229 | + $reflector->getName() |
|
230 | + ) |
|
231 | + ); |
|
232 | + } |
|
233 | + return $dependency; |
|
234 | + } |
|
235 | 235 | } |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null) |
136 | 136 | { |
137 | 137 | $this->_migration_stages = (array) apply_filters( |
138 | - 'FHEE__' . get_class($this) . '__construct__migration_stages', |
|
138 | + 'FHEE__'.get_class($this).'__construct__migration_stages', |
|
139 | 139 | $this->_migration_stages |
140 | 140 | ); |
141 | 141 | foreach ($this->_migration_stages as $migration_stage) { |
@@ -170,10 +170,10 @@ discard block |
||
170 | 170 | public function set_mapping($old_table, $old_pk, $new_table, $new_pk) |
171 | 171 | { |
172 | 172 | // make sure it has the needed keys |
173 | - if (! isset($this->_mappings[ $old_table ]) || ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
174 | - $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
173 | + if ( ! isset($this->_mappings[$old_table]) || ! isset($this->_mappings[$old_table][$new_table])) { |
|
174 | + $this->_mappings[$old_table][$new_table] = $this->_get_mapping_option($old_table, $new_table); |
|
175 | 175 | } |
176 | - $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] = $new_pk; |
|
176 | + $this->_mappings[$old_table][$new_table][$old_pk] = $new_pk; |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | |
@@ -189,14 +189,14 @@ discard block |
||
189 | 189 | public function get_mapping_new_pk($old_table, $old_pk, $new_table) |
190 | 190 | { |
191 | 191 | if ( |
192 | - ! isset($this->_mappings[ $old_table ]) || |
|
193 | - ! isset($this->_mappings[ $old_table ][ $new_table ]) |
|
192 | + ! isset($this->_mappings[$old_table]) || |
|
193 | + ! isset($this->_mappings[$old_table][$new_table]) |
|
194 | 194 | ) { |
195 | 195 | // try fetching the option |
196 | - $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
196 | + $this->_mappings[$old_table][$new_table] = $this->_get_mapping_option($old_table, $new_table); |
|
197 | 197 | } |
198 | - return isset($this->_mappings[ $old_table ][ $new_table ][ $old_pk ]) |
|
199 | - ? $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] : null; |
|
198 | + return isset($this->_mappings[$old_table][$new_table][$old_pk]) |
|
199 | + ? $this->_mappings[$old_table][$new_table][$old_pk] : null; |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | |
@@ -212,16 +212,16 @@ discard block |
||
212 | 212 | public function get_mapping_old_pk($old_table, $new_table, $new_pk) |
213 | 213 | { |
214 | 214 | if ( |
215 | - ! isset($this->_mappings[ $old_table ]) || |
|
216 | - ! isset($this->_mappings[ $old_table ][ $new_table ]) |
|
215 | + ! isset($this->_mappings[$old_table]) || |
|
216 | + ! isset($this->_mappings[$old_table][$new_table]) |
|
217 | 217 | ) { |
218 | 218 | // try fetching the option |
219 | - $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
219 | + $this->_mappings[$old_table][$new_table] = $this->_get_mapping_option($old_table, $new_table); |
|
220 | 220 | } |
221 | - if (isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
222 | - $new_pk_to_old_pk = array_flip($this->_mappings[ $old_table ][ $new_table ]); |
|
223 | - if (isset($new_pk_to_old_pk[ $new_pk ])) { |
|
224 | - return $new_pk_to_old_pk[ $new_pk ]; |
|
221 | + if (isset($this->_mappings[$old_table][$new_table])) { |
|
222 | + $new_pk_to_old_pk = array_flip($this->_mappings[$old_table][$new_table]); |
|
223 | + if (isset($new_pk_to_old_pk[$new_pk])) { |
|
224 | + return $new_pk_to_old_pk[$new_pk]; |
|
225 | 225 | } |
226 | 226 | } |
227 | 227 | return null; |
@@ -271,7 +271,7 @@ discard block |
||
271 | 271 | $new_table_name_sans_wp = str_replace($wpdb->prefix, "", $new_table_name); |
272 | 272 | $migrates_to = EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
273 | 273 | return substr( |
274 | - EE_Data_Migration_Manager::data_migration_script_mapping_option_prefix . $migrates_to ['slug'] . '_' . $migrates_to['version'] . '_' . $old_table_name_sans_wp . '_' . $new_table_name_sans_wp, |
|
274 | + EE_Data_Migration_Manager::data_migration_script_mapping_option_prefix.$migrates_to ['slug'].'_'.$migrates_to['version'].'_'.$old_table_name_sans_wp.'_'.$new_table_name_sans_wp, |
|
275 | 275 | 0, |
276 | 276 | 64 |
277 | 277 | ); |
@@ -343,12 +343,12 @@ discard block |
||
343 | 343 | $num_records_to_migrate_limit - $num_records_actually_migrated |
344 | 344 | ); |
345 | 345 | $num_records_actually_migrated += $records_migrated_during_stage; |
346 | - $records_migrated_per_stage[ $stage->pretty_name() ] = $records_migrated_during_stage; |
|
346 | + $records_migrated_per_stage[$stage->pretty_name()] = $records_migrated_during_stage; |
|
347 | 347 | } catch (Exception $e) { |
348 | 348 | // yes if we catch an exception here, we consider that migration stage borked. |
349 | 349 | $stage->set_status(EE_Data_Migration_Manager::status_fatal_error); |
350 | 350 | $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
351 | - $stage->add_error($e->getMessage() . ". Stack-trace:" . $e->getTraceAsString()); |
|
351 | + $stage->add_error($e->getMessage().". Stack-trace:".$e->getTraceAsString()); |
|
352 | 352 | throw $e; |
353 | 353 | } |
354 | 354 | // check that the migration stage didn't mark itself as having a fatal error |
@@ -421,8 +421,8 @@ discard block |
||
421 | 421 | private function _maybe_do_schema_changes($before = true) |
422 | 422 | { |
423 | 423 | // so this property will be either _schema_changes_after_migration_ran or _schema_changes_before_migration_ran |
424 | - $property_name = '_schema_changes_' . ($before ? 'before' : 'after') . '_migration_ran'; |
|
425 | - if (! $this->{$property_name}) { |
|
424 | + $property_name = '_schema_changes_'.($before ? 'before' : 'after').'_migration_ran'; |
|
425 | + if ( ! $this->{$property_name}) { |
|
426 | 426 | try { |
427 | 427 | ob_start(); |
428 | 428 | if ($before) { |
@@ -659,7 +659,7 @@ discard block |
||
659 | 659 | try { |
660 | 660 | EEH_Activation::create_table($table_name, $table_definition_sql, $engine_string, $drop_pre_existing_tables); |
661 | 661 | } catch (EE_Error $e) { |
662 | - $message = $e->getMessage() . '<br>Stack Trace:' . $e->getTraceAsString(); |
|
662 | + $message = $e->getMessage().'<br>Stack Trace:'.$e->getTraceAsString(); |
|
663 | 663 | $this->add_error($message); |
664 | 664 | $this->_feedback_message .= $message; |
665 | 665 | } |
@@ -705,7 +705,7 @@ discard block |
||
705 | 705 | public function get_errors() |
706 | 706 | { |
707 | 707 | $all_errors = $this->_errors; |
708 | - if (! is_array($all_errors)) { |
|
708 | + if ( ! is_array($all_errors)) { |
|
709 | 709 | $all_errors = array(); |
710 | 710 | } |
711 | 711 | foreach ($this->stages() as $stage) { |
@@ -739,7 +739,7 @@ discard block |
||
739 | 739 | */ |
740 | 740 | protected function stages() |
741 | 741 | { |
742 | - $stages = apply_filters('FHEE__' . get_class($this) . '__stages', $this->_migration_stages); |
|
742 | + $stages = apply_filters('FHEE__'.get_class($this).'__stages', $this->_migration_stages); |
|
743 | 743 | ksort($stages); |
744 | 744 | return $stages; |
745 | 745 | } |
@@ -768,7 +768,7 @@ discard block |
||
768 | 768 | $properties = parent::properties_as_array(); |
769 | 769 | $properties['_migration_stages'] = array(); |
770 | 770 | foreach ($this->_migration_stages as $migration_stage_priority => $migration_stage_class) { |
771 | - $properties['_migration_stages'][ $migration_stage_priority ] = $migration_stage_class->properties_as_array( |
|
771 | + $properties['_migration_stages'][$migration_stage_priority] = $migration_stage_class->properties_as_array( |
|
772 | 772 | ); |
773 | 773 | } |
774 | 774 | unset($properties['_mappings']); |
@@ -14,918 +14,918 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class EE_Data_Migration_Script_Base extends EE_Data_Migration_Class_Base |
16 | 16 | { |
17 | - /** |
|
18 | - * Set by client code to indicate this DMS is being ran as part of a proper migration, |
|
19 | - * instead of being used to merely setup (or verify) the database structure. |
|
20 | - * Defaults to TRUE, so client code that's NOT using this DMS as part of a proper migration |
|
21 | - * should call EE_Data_Migration_Script_Base::set_migrating( FALSE ) |
|
22 | - * |
|
23 | - * @var boolean |
|
24 | - */ |
|
25 | - protected $_migrating = true; |
|
26 | - |
|
27 | - /** |
|
28 | - * numerically-indexed array where each value is EE_Data_Migration_Script_Stage object |
|
29 | - * |
|
30 | - * @var EE_Data_Migration_Script_Stage[] $migration_functions |
|
31 | - */ |
|
32 | - protected $_migration_stages = array(); |
|
33 | - |
|
34 | - /** |
|
35 | - * Indicates we've already ran the schema changes that needed to happen BEFORE the data migration |
|
36 | - * |
|
37 | - * @var boolean |
|
38 | - */ |
|
39 | - protected $_schema_changes_before_migration_ran = null; |
|
40 | - |
|
41 | - /** |
|
42 | - * Indicates we've already ran the schema changes that needed to happen AFTER the data migration |
|
43 | - * |
|
44 | - * @var boolean |
|
45 | - */ |
|
46 | - protected $_schema_changes_after_migration_ran = null; |
|
47 | - |
|
48 | - /** |
|
49 | - * String which describes what's currently happening in this migration |
|
50 | - * |
|
51 | - * @var string |
|
52 | - */ |
|
53 | - protected $_feedback_message; |
|
54 | - |
|
55 | - /** |
|
56 | - * Indicates the script's priority. Like wp's add_action and add_filter, lower numbers |
|
57 | - * correspond to earlier execution |
|
58 | - * |
|
59 | - * @var int |
|
60 | - */ |
|
61 | - protected $_priority = 5; |
|
62 | - |
|
63 | - /** |
|
64 | - * Multi-dimensional array that defines the mapping from OLD table Primary Keys |
|
65 | - * to NEW table Primary Keys. |
|
66 | - * Top-level array keys are OLD table names (minus the "wp_" part), |
|
67 | - * 2nd-level array keys are NEW table names (again, minus the "wp_" part), |
|
68 | - * 3rd-level array keys are the OLD table primary keys |
|
69 | - * and 3rd-level array values are the NEW table primary keys |
|
70 | - * |
|
71 | - * @var array |
|
72 | - */ |
|
73 | - protected $_mappings = array(); |
|
74 | - |
|
75 | - /** |
|
76 | - * @var EE_Data_Migration_Script_Base |
|
77 | - */ |
|
78 | - protected $previous_dms; |
|
79 | - |
|
80 | - |
|
81 | - /** |
|
82 | - * Returns whether or not this data migration script can operate on the given version of the database. |
|
83 | - * Eg, if this migration script can migrate from 3.1.26 or higher (but not anything after 4.0.0), and |
|
84 | - * it's passed a string like '3.1.38B', it should return true. |
|
85 | - * If this DMS is to migrate data from an EE3 addon, you will probably want to use |
|
86 | - * EventEspresso\core\services\database\TableAnalysis::tableExists() to check for old EE3 tables, and |
|
87 | - * EE_Data_Migration_Manager::get_migration_ran() to check that core was already |
|
88 | - * migrated from EE3 to EE4 (ie, this DMS probably relies on some migration data generated |
|
89 | - * during the Core 4.1.0 DMS. If core didn't run that DMS, you probably don't want |
|
90 | - * to run this DMS). |
|
91 | - * If this DMS migrates data from a previous version of this EE4 addon, just |
|
92 | - * comparing $current_database_state_of[ $this->slug() ] will probably suffice. |
|
93 | - * If this DMS should never migrate data, because it's only used to define the initial |
|
94 | - * database state, just return FALSE (and core's activation process will take care |
|
95 | - * of calling its schema_changes_before_migration() and |
|
96 | - * schema_changes_after_migration() for you. ) |
|
97 | - * |
|
98 | - * @param array $current_database_state_of keys are EE plugin slugs (eg 'Core', 'Calendar', 'Mailchimp', etc) |
|
99 | - * @return boolean |
|
100 | - */ |
|
101 | - abstract public function can_migrate_from_version($current_database_state_of); |
|
102 | - |
|
103 | - |
|
104 | - /** |
|
105 | - * Performs database schema changes that need to occur BEFORE the data is migrated. |
|
106 | - * Eg, if we were going to change user passwords from plaintext to encoded versions |
|
107 | - * during this migration, this would probably add a new column called something like |
|
108 | - * "encoded_password". |
|
109 | - * |
|
110 | - * @return boolean of success |
|
111 | - */ |
|
112 | - abstract public function schema_changes_before_migration(); |
|
113 | - |
|
114 | - |
|
115 | - /** |
|
116 | - * Performs the database schema changes that need to occur AFTER the data has been migrated. |
|
117 | - * Usually this will mean we'll be removing old columns. Eg, if we were changing passwords |
|
118 | - * from plaintext to encoded versions, and we had added a column called "encoded_password", |
|
119 | - * this function would probably remove the old column "password" (which still holds the plaintext password) |
|
120 | - * and possibly rename "encoded_password" to "password" |
|
121 | - * |
|
122 | - * @return boolean of success |
|
123 | - */ |
|
124 | - abstract public function schema_changes_after_migration(); |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * All children of this must call parent::__construct() |
|
129 | - * at the end of their constructor or suffer the consequences! |
|
130 | - * |
|
131 | - * @param TableManager $table_manager |
|
132 | - * @param TableAnalysis $table_analysis |
|
133 | - */ |
|
134 | - public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null) |
|
135 | - { |
|
136 | - $this->_migration_stages = (array) apply_filters( |
|
137 | - 'FHEE__' . get_class($this) . '__construct__migration_stages', |
|
138 | - $this->_migration_stages |
|
139 | - ); |
|
140 | - foreach ($this->_migration_stages as $migration_stage) { |
|
141 | - if ($migration_stage instanceof EE_Data_Migration_Script_Stage) { |
|
142 | - $migration_stage->_construct_finalize($this); |
|
143 | - } |
|
144 | - } |
|
145 | - parent::__construct($table_manager, $table_analysis); |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * Place to add hooks and filters for tweaking the migrations page, in order |
|
151 | - * to customize it |
|
152 | - */ |
|
153 | - public function migration_page_hooks() |
|
154 | - { |
|
155 | - // by default none are added because we normally like the default look of the migration page |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - /** |
|
160 | - * Sets the mapping from old table primary keys to new table primary keys. |
|
161 | - * This mapping is automatically persisted as a property on the migration |
|
162 | - * |
|
163 | - * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
164 | - * @param int|string $old_pk old primary key. Eg events_detail.id's value |
|
165 | - * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
166 | - * @param int|string $new_pk eg posts.ID |
|
167 | - * @return void |
|
168 | - * @throws EE_Error |
|
169 | - */ |
|
170 | - public function set_mapping($old_table, $old_pk, $new_table, $new_pk) |
|
171 | - { |
|
172 | - // make sure it has the needed keys |
|
173 | - if (! isset($this->_mappings[ $old_table ]) || ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
174 | - $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
175 | - } |
|
176 | - $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] = $new_pk; |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * Gets the new primary key, if provided with the OLD table and the primary key |
|
182 | - * of an item in the old table, and the new table |
|
183 | - * |
|
184 | - * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
185 | - * @param int|string $old_pk old primary key. Eg events_detail.id's value |
|
186 | - * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
187 | - * @return mixed the primary key on the new table |
|
188 | - * @throws EE_Error |
|
189 | - */ |
|
190 | - public function get_mapping_new_pk($old_table, $old_pk, $new_table) |
|
191 | - { |
|
192 | - if ( |
|
193 | - ! isset($this->_mappings[ $old_table ]) || |
|
194 | - ! isset($this->_mappings[ $old_table ][ $new_table ]) |
|
195 | - ) { |
|
196 | - // try fetching the option |
|
197 | - $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
198 | - } |
|
199 | - return isset($this->_mappings[ $old_table ][ $new_table ][ $old_pk ]) |
|
200 | - ? $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] : null; |
|
201 | - } |
|
202 | - |
|
203 | - |
|
204 | - /** |
|
205 | - * Gets the old primary key, if provided with the OLD table, |
|
206 | - * and the new table and the primary key of an item in the new table |
|
207 | - * |
|
208 | - * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
209 | - * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
210 | - * @param mixed $new_pk |
|
211 | - * @return mixed |
|
212 | - * @throws EE_Error |
|
213 | - */ |
|
214 | - public function get_mapping_old_pk($old_table, $new_table, $new_pk) |
|
215 | - { |
|
216 | - if ( |
|
217 | - ! isset($this->_mappings[ $old_table ]) || |
|
218 | - ! isset($this->_mappings[ $old_table ][ $new_table ]) |
|
219 | - ) { |
|
220 | - // try fetching the option |
|
221 | - $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
222 | - } |
|
223 | - if (isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
224 | - $new_pk_to_old_pk = array_flip($this->_mappings[ $old_table ][ $new_table ]); |
|
225 | - if (isset($new_pk_to_old_pk[ $new_pk ])) { |
|
226 | - return $new_pk_to_old_pk[ $new_pk ]; |
|
227 | - } |
|
228 | - } |
|
229 | - return null; |
|
230 | - } |
|
231 | - |
|
232 | - |
|
233 | - /** |
|
234 | - * Gets the mapping array option specified by the table names |
|
235 | - * |
|
236 | - * @param string $old_table_name |
|
237 | - * @param string $new_table_name |
|
238 | - * @return array |
|
239 | - * @throws EE_Error |
|
240 | - */ |
|
241 | - protected function _get_mapping_option($old_table_name, $new_table_name) |
|
242 | - { |
|
243 | - return get_option($this->_get_mapping_option_name($old_table_name, $new_table_name), array()); |
|
244 | - } |
|
245 | - |
|
246 | - |
|
247 | - /** |
|
248 | - * Updates the mapping option specified by the table names with the array provided |
|
249 | - * |
|
250 | - * @param string $old_table_name |
|
251 | - * @param string $new_table_name |
|
252 | - * @param array $mapping_array |
|
253 | - * @return boolean success of updating option |
|
254 | - * @throws EE_Error |
|
255 | - */ |
|
256 | - protected function _set_mapping_option($old_table_name, $new_table_name, $mapping_array) |
|
257 | - { |
|
258 | - $success = update_option($this->_get_mapping_option_name($old_table_name, $new_table_name), $mapping_array, false); |
|
259 | - return $success; |
|
260 | - } |
|
261 | - |
|
262 | - |
|
263 | - /** |
|
264 | - * Gets the option name for this script to map from $old_table_name to $new_table_name |
|
265 | - * |
|
266 | - * @param string $old_table_name |
|
267 | - * @param string $new_table_name |
|
268 | - * @return string |
|
269 | - * @throws EE_Error |
|
270 | - */ |
|
271 | - protected function _get_mapping_option_name($old_table_name, $new_table_name) |
|
272 | - { |
|
273 | - global $wpdb; |
|
274 | - $old_table_name_sans_wp = str_replace($wpdb->prefix, "", $old_table_name); |
|
275 | - $new_table_name_sans_wp = str_replace($wpdb->prefix, "", $new_table_name); |
|
276 | - $migrates_to = EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
|
277 | - return substr( |
|
278 | - EE_Data_Migration_Manager::data_migration_script_mapping_option_prefix . $migrates_to ['slug'] . '_' . $migrates_to['version'] . '_' . $old_table_name_sans_wp . '_' . $new_table_name_sans_wp, |
|
279 | - 0, |
|
280 | - 64 |
|
281 | - ); |
|
282 | - } |
|
283 | - |
|
284 | - |
|
285 | - /** |
|
286 | - * Counts all the records that will be migrated during this data migration. |
|
287 | - * For example, if we were changing old user passwords from plaintext to encoded versions, |
|
288 | - * this would be a count of all users who have passwords. If we were going to also split |
|
289 | - * attendee records into transactions, registrations, and attendee records, this would include |
|
290 | - * the count of all attendees currently in existence in the DB (ie, users + attendees). |
|
291 | - * If you can't determine how many records there are to migrate, just provide a guess: this |
|
292 | - * number will only be used in calculating the percent complete. If you estimate there to be |
|
293 | - * 100 records to migrate, and it turns out there's 120, we'll just show the migration as being at |
|
294 | - * 99% until the function "migration_step" returns EE_Data_Migration_Script_Base::status_complete. |
|
295 | - * |
|
296 | - * @return int |
|
297 | - */ |
|
298 | - protected function _count_records_to_migrate() |
|
299 | - { |
|
300 | - $count = 0; |
|
301 | - foreach ($this->stages() as $stage) { |
|
302 | - $count += $stage->count_records_to_migrate(); |
|
303 | - } |
|
304 | - return $count; |
|
305 | - } |
|
306 | - |
|
307 | - |
|
308 | - /** |
|
309 | - * Returns the number of records updated so far. Usually this is easiest to do |
|
310 | - * by just setting a transient and updating it after each migration_step |
|
311 | - * |
|
312 | - * @return int |
|
313 | - */ |
|
314 | - public function count_records_migrated() |
|
315 | - { |
|
316 | - $count = 0; |
|
317 | - foreach ($this->stages() as $stage) { |
|
318 | - $count += $stage->count_records_migrated(); |
|
319 | - } |
|
320 | - $this->_records_migrated = $count; |
|
321 | - return $count; |
|
322 | - } |
|
323 | - |
|
324 | - |
|
325 | - /** |
|
326 | - * @param int $num_records_to_migrate_limit |
|
327 | - * @return int |
|
328 | - * @throws EE_Error |
|
329 | - * @throws Exception |
|
330 | - */ |
|
331 | - public function migration_step($num_records_to_migrate_limit) |
|
332 | - { |
|
333 | - // reset the feedback message |
|
334 | - $this->_feedback_message = ''; |
|
335 | - // if we haven't yet done the 1st schema changes, do them now. buffer any output |
|
336 | - $this->_maybe_do_schema_changes(true); |
|
337 | - |
|
338 | - $num_records_actually_migrated = 0; |
|
339 | - $records_migrated_per_stage = array(); |
|
340 | - // setup the 'stage' variable, which should hold the last run stage of the migration (or none at all if nothing runs) |
|
341 | - $stage = null; |
|
342 | - // get the next stage that isn't complete |
|
343 | - foreach ($this->stages() as $stage) { |
|
344 | - if ($stage->get_status() == EE_Data_Migration_Manager::status_continue) { |
|
345 | - try { |
|
346 | - $records_migrated_during_stage = $stage->migration_step( |
|
347 | - $num_records_to_migrate_limit - $num_records_actually_migrated |
|
348 | - ); |
|
349 | - $num_records_actually_migrated += $records_migrated_during_stage; |
|
350 | - $records_migrated_per_stage[ $stage->pretty_name() ] = $records_migrated_during_stage; |
|
351 | - } catch (Exception $e) { |
|
352 | - // yes if we catch an exception here, we consider that migration stage borked. |
|
353 | - $stage->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
354 | - $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
355 | - $stage->add_error($e->getMessage() . ". Stack-trace:" . $e->getTraceAsString()); |
|
356 | - throw $e; |
|
357 | - } |
|
358 | - // check that the migration stage didn't mark itself as having a fatal error |
|
359 | - if ($stage->is_broken()) { |
|
360 | - $this->set_broken(); |
|
361 | - throw new EE_Error($stage->get_last_error()); |
|
362 | - } |
|
363 | - } |
|
364 | - // once we've migrated all the number we intended to (possibly from different stages), stop migrating |
|
365 | - // or if we had a fatal error |
|
366 | - // or if the current script stopped early- its not done, but it's done all it thinks we should do on this step |
|
367 | - if ( |
|
368 | - $num_records_actually_migrated >= $num_records_to_migrate_limit |
|
369 | - || $stage->is_broken() |
|
370 | - || $stage->has_more_to_do() |
|
371 | - ) { |
|
372 | - break; |
|
373 | - } |
|
374 | - } |
|
375 | - // check if we're all done this data migration... |
|
376 | - // which is indicated by being done early AND the last stage claims to be done |
|
377 | - if ($stage == null) { |
|
378 | - // this migration script apparently has NO stages... which is super weird, but whatever |
|
379 | - $this->set_completed(); |
|
380 | - $this->_maybe_do_schema_changes(false); |
|
381 | - } elseif ($num_records_actually_migrated < $num_records_to_migrate_limit && ! $stage->has_more_to_do()) { |
|
382 | - // apparently we're done, because we couldn't migrate the number we intended to |
|
383 | - $this->set_completed(); |
|
384 | - $this->_update_feedback_message(array_reverse($records_migrated_per_stage)); |
|
385 | - // do schema changes for after the migration now |
|
386 | - // first double-check we haven't already done this |
|
387 | - $this->_maybe_do_schema_changes(false); |
|
388 | - } else { |
|
389 | - // update feedback message, keeping in mind that we show them with the most recent at the top |
|
390 | - $this->_update_feedback_message(array_reverse($records_migrated_per_stage)); |
|
391 | - } |
|
392 | - return $num_records_actually_migrated; |
|
393 | - } |
|
394 | - |
|
395 | - |
|
396 | - /** |
|
397 | - * Updates the feedback message according to what was done during this migration stage. |
|
398 | - * |
|
399 | - * @param array $records_migrated_per_stage KEYS are pretty names for each stage; values are the count of records |
|
400 | - * migrated from that stage |
|
401 | - * @return void |
|
402 | - */ |
|
403 | - private function _update_feedback_message($records_migrated_per_stage) |
|
404 | - { |
|
405 | - $feedback_message_array = array(); |
|
406 | - foreach ($records_migrated_per_stage as $migration_stage_name => $num_records_migrated) { |
|
407 | - $feedback_message_array[] = sprintf( |
|
408 | - esc_html__("Migrated %d records successfully during %s", "event_espresso"), |
|
409 | - $num_records_migrated, |
|
410 | - $migration_stage_name |
|
411 | - ); |
|
412 | - } |
|
413 | - $this->_feedback_message .= implode("<br>", $feedback_message_array); |
|
414 | - } |
|
415 | - |
|
416 | - |
|
417 | - /** |
|
418 | - * Calls either schema_changes_before_migration() (if $before==true) or schema_changes_after_migration |
|
419 | - * (if $before==false). Buffers their outputs and stores them on the class. |
|
420 | - * |
|
421 | - * @param boolean $before |
|
422 | - * @throws Exception |
|
423 | - * @return void |
|
424 | - */ |
|
425 | - private function _maybe_do_schema_changes($before = true) |
|
426 | - { |
|
427 | - // so this property will be either _schema_changes_after_migration_ran or _schema_changes_before_migration_ran |
|
428 | - $property_name = '_schema_changes_' . ($before ? 'before' : 'after') . '_migration_ran'; |
|
429 | - if (! $this->{$property_name}) { |
|
430 | - try { |
|
431 | - ob_start(); |
|
432 | - if ($before) { |
|
433 | - $this->schema_changes_before_migration(); |
|
434 | - } else { |
|
435 | - $this->schema_changes_after_migration(); |
|
436 | - } |
|
437 | - $output = ob_get_contents(); |
|
438 | - ob_end_clean(); |
|
439 | - } catch (Exception $e) { |
|
440 | - $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
441 | - throw $e; |
|
442 | - } |
|
443 | - // record that we've done these schema changes |
|
444 | - $this->{$property_name} = true; |
|
445 | - // if there were any warnings etc, record them as non-fatal errors |
|
446 | - if ($output) { |
|
447 | - // there were some warnings |
|
448 | - $this->_errors[] = $output; |
|
449 | - } |
|
450 | - } |
|
451 | - } |
|
452 | - |
|
453 | - |
|
454 | - /** |
|
455 | - * Wrapper for EEH_Activation::create_table. However, takes into account the request type when |
|
456 | - * deciding what to pass for its 4th arg, $drop_pre_existing_tables. Using this function, instead |
|
457 | - * of _table_should_exist_previously, indicates that this table should be new to the EE version being migrated to |
|
458 | - * or |
|
459 | - * activated currently. If this is a brand new activation or a migration, and we're indicating this table should |
|
460 | - * not |
|
461 | - * previously exist, then we want to set $drop_pre_existing_tables to TRUE (ie, we shouldn't discover that this |
|
462 | - * table exists in the DB in EEH_Activation::create_table- if it DOES exist, something's wrong and the old table |
|
463 | - * should be nuked. |
|
464 | - * |
|
465 | - * Just for a bit of context, the migration script's db_schema_changes_* methods |
|
466 | - * are called basically in 3 cases: on brand new activation of EE4 (ie no previous version of EE existed and the |
|
467 | - * plugin is being activated and we want to add all the brand new tables), upon reactivation of EE4 (it was |
|
468 | - * deactivated and then reactivated, in which case we want to just verify the DB structure is ok) that table should |
|
469 | - * be dropped), and during a migration when we're moving the DB to the state of the migration script |
|
470 | - * |
|
471 | - * @param string $table_name |
|
472 | - * @param string $table_definition_sql |
|
473 | - * @param string $engine_string |
|
474 | - * @throws EE_Error |
|
475 | - * @throws ReflectionException |
|
476 | - */ |
|
477 | - protected function _table_is_new_in_this_version( |
|
478 | - $table_name, |
|
479 | - $table_definition_sql, |
|
480 | - $engine_string = 'ENGINE=InnoDB ' |
|
481 | - ) { |
|
482 | - $this->_create_table_and_catch_errors( |
|
483 | - $table_name, |
|
484 | - $table_definition_sql, |
|
485 | - $engine_string, |
|
486 | - $this->_pre_existing_table_should_be_dropped(true) |
|
487 | - ); |
|
488 | - } |
|
489 | - |
|
490 | - |
|
491 | - /** |
|
492 | - * Like _table_is_new_in_this_version and _table_should_exist_previously, this function verifies the given table |
|
493 | - * exists. But we understand that this table has CHANGED in this version since the previous version. So it's not |
|
494 | - * completely new, but it's different. So we need to treat it like a new table in terms of verifying it's schema is |
|
495 | - * correct on activations, migrations, upgrades; but if it exists when it shouldn't, we need to be as lenient as |
|
496 | - * _table_should_exist_previously. |
|
497 | - * 8656]{Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the |
|
498 | - * table shouldn't exist). |
|
499 | - * |
|
500 | - * @param string $table_name |
|
501 | - * @param string $table_definition_sql |
|
502 | - * @param string $engine_string |
|
503 | - * @throws EE_Error |
|
504 | - * @throws ReflectionException |
|
505 | - */ |
|
506 | - protected function _table_is_changed_in_this_version( |
|
507 | - $table_name, |
|
508 | - $table_definition_sql, |
|
509 | - $engine_string = 'ENGINE=MyISAM' |
|
510 | - ) { |
|
511 | - $this->_create_table_and_catch_errors( |
|
512 | - $table_name, |
|
513 | - $table_definition_sql, |
|
514 | - $engine_string, |
|
515 | - $this->_pre_existing_table_should_be_dropped(false) |
|
516 | - ); |
|
517 | - } |
|
518 | - |
|
519 | - |
|
520 | - /** |
|
521 | - * _old_table_exists |
|
522 | - * returns TRUE if the requested table exists in the current database |
|
523 | - * |
|
524 | - * @param string $table_name |
|
525 | - * @return boolean |
|
526 | - * @throws EE_Error |
|
527 | - */ |
|
528 | - protected function _old_table_exists($table_name) |
|
529 | - { |
|
530 | - return $this->_get_table_analysis()->tableExists($table_name); |
|
531 | - } |
|
532 | - |
|
533 | - |
|
534 | - /** |
|
535 | - * _delete_table_if_empty |
|
536 | - * returns TRUE if the requested table was empty and successfully empty |
|
537 | - * |
|
538 | - * @param string $table_name |
|
539 | - * @return boolean |
|
540 | - * @throws EE_Error |
|
541 | - * @throws ReflectionException |
|
542 | - */ |
|
543 | - protected function _delete_table_if_empty($table_name) |
|
544 | - { |
|
545 | - return EEH_Activation::delete_db_table_if_empty($table_name); |
|
546 | - } |
|
547 | - |
|
548 | - |
|
549 | - /** |
|
550 | - * It is preferred to use _table_has_not_changed_since_previous or _table_is_changed_in_this_version |
|
551 | - * as these are significantly more efficient or explicit. |
|
552 | - * Please see description of _table_is_new_in_this_version. This function will only set |
|
553 | - * EEH_Activation::create_table's $drop_pre_existing_tables to TRUE if it's a brand |
|
554 | - * new activation. ie, a more accurate name for this method would be "_table_added_previously_by_this_plugin" |
|
555 | - * because the table will be cleared out if this is a new activation (ie, if its a new activation, it actually |
|
556 | - * should exist previously). Otherwise, we'll always set $drop_pre_existing_tables to FALSE because the table |
|
557 | - * should have existed. Note, if the table is being MODIFIED in this version being activated or migrated to, then |
|
558 | - * you want _table_is_changed_in_this_version NOT this one. We don't check this table's structure during migrations |
|
559 | - * because apparently it hasn't changed since the previous one, right? |
|
560 | - * |
|
561 | - * @param string $table_name |
|
562 | - * @param string $table_definition_sql |
|
563 | - * @param string $engine_string |
|
564 | - * @throws EE_Error |
|
565 | - * @throws ReflectionException |
|
566 | - */ |
|
567 | - protected function _table_should_exist_previously( |
|
568 | - $table_name, |
|
569 | - $table_definition_sql, |
|
570 | - $engine_string = 'ENGINE=MyISAM' |
|
571 | - ) { |
|
572 | - $this->_create_table_and_catch_errors( |
|
573 | - $table_name, |
|
574 | - $table_definition_sql, |
|
575 | - $engine_string, |
|
576 | - $this->_pre_existing_table_should_be_dropped(false) |
|
577 | - ); |
|
578 | - } |
|
579 | - |
|
580 | - |
|
581 | - /** |
|
582 | - * Exactly the same as _table_should_exist_previously(), except if this migration script is currently doing |
|
583 | - * a migration, we skip checking this table's structure in the database and just assume it's correct. |
|
584 | - * So this is useful only to improve efficiency when doing migrations (not a big deal for single site installs, |
|
585 | - * but important for multisite where migrations can take a very long time otherwise). |
|
586 | - * If the table is known to have changed since previous version, use _table_is_changed_in_this_version(). |
|
587 | - * Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the table |
|
588 | - * shouldn't exist). |
|
589 | - * |
|
590 | - * @param string $table_name |
|
591 | - * @param string $table_definition_sql |
|
592 | - * @param string $engine_string |
|
593 | - * @throws EE_Error |
|
594 | - * @throws ReflectionException |
|
595 | - */ |
|
596 | - protected function _table_has_not_changed_since_previous( |
|
597 | - $table_name, |
|
598 | - $table_definition_sql, |
|
599 | - $engine_string = 'ENGINE=MyISAM' |
|
600 | - ) { |
|
601 | - if ($this->_currently_migrating()) { |
|
602 | - // if we're doing a migration, and this table apparently already exists, then we don't need do anything right? |
|
603 | - return; |
|
604 | - } |
|
605 | - $this->_create_table_and_catch_errors( |
|
606 | - $table_name, |
|
607 | - $table_definition_sql, |
|
608 | - $engine_string, |
|
609 | - $this->_pre_existing_table_should_be_dropped(false) |
|
610 | - ); |
|
611 | - } |
|
612 | - |
|
613 | - /** |
|
614 | - * Returns whether or not this migration script is being used as part of an actual migration |
|
615 | - * |
|
616 | - * @return boolean |
|
617 | - */ |
|
618 | - protected function _currently_migrating() |
|
619 | - { |
|
620 | - // we want to know if we are currently performing a migration. We could just believe what was set on the _migrating property, but let's double-check (ie the script should apply and we should be in MM) |
|
621 | - return $this->_migrating && |
|
622 | - $this->can_migrate_from_version( |
|
623 | - EE_Data_Migration_Manager::instance()->ensure_current_database_state_is_set() |
|
624 | - ) && |
|
625 | - EE_Maintenance_Mode::instance()->real_level() == EE_Maintenance_Mode::level_2_complete_maintenance; |
|
626 | - } |
|
627 | - |
|
628 | - |
|
629 | - /** |
|
630 | - * Determines if a table should be dropped, based on whether it's reported to be new in $table_is_new, |
|
631 | - * and the plugin's request type. |
|
632 | - * Assumes only this plugin could have added the table (ie, if its a new activation of this plugin, the table |
|
633 | - * shouldn't exist no matter what). |
|
634 | - * |
|
635 | - * @param boolean $table_is_new |
|
636 | - * @return boolean |
|
637 | - * @throws EE_Error |
|
638 | - */ |
|
639 | - protected function _pre_existing_table_should_be_dropped($table_is_new) |
|
640 | - { |
|
641 | - if ($table_is_new) { |
|
642 | - if ( |
|
643 | - $this->_get_req_type_for_plugin_corresponding_to_this_dms() == EE_System::req_type_new_activation |
|
644 | - || $this->_currently_migrating() |
|
645 | - ) { |
|
646 | - return true; |
|
647 | - } else { |
|
648 | - return false; |
|
649 | - } |
|
650 | - } else { |
|
651 | - if ( |
|
652 | - in_array( |
|
653 | - $this->_get_req_type_for_plugin_corresponding_to_this_dms(), |
|
654 | - array(EE_System::req_type_new_activation) |
|
655 | - ) |
|
656 | - ) { |
|
657 | - return true; |
|
658 | - } else { |
|
659 | - return false; |
|
660 | - } |
|
661 | - } |
|
662 | - } |
|
663 | - |
|
664 | - |
|
665 | - /** |
|
666 | - * Just wraps EEH_Activation::create_table, but catches any errors it may throw and adds them as errors on the DMS |
|
667 | - * |
|
668 | - * @param string $table_name |
|
669 | - * @param string $table_definition_sql |
|
670 | - * @param string $engine_string |
|
671 | - * @param boolean $drop_pre_existing_tables |
|
672 | - * @throws ReflectionException |
|
673 | - */ |
|
674 | - private function _create_table_and_catch_errors( |
|
675 | - $table_name, |
|
676 | - $table_definition_sql, |
|
677 | - $engine_string = 'ENGINE=MyISAM', |
|
678 | - $drop_pre_existing_tables = false |
|
679 | - ) { |
|
680 | - try { |
|
681 | - EEH_Activation::create_table($table_name, $table_definition_sql, $engine_string, $drop_pre_existing_tables); |
|
682 | - } catch (EE_Error $e) { |
|
683 | - $message = $e->getMessage() . '<br>Stack Trace:' . $e->getTraceAsString(); |
|
684 | - $this->add_error($message); |
|
685 | - $this->_feedback_message .= $message; |
|
686 | - } |
|
687 | - } |
|
688 | - |
|
689 | - |
|
690 | - /** |
|
691 | - * Gets the request type for the plugin (core or addon) that corresponds to this DMS |
|
692 | - * |
|
693 | - * @return int one of EE_System::_req_type_* constants |
|
694 | - * @throws EE_Error |
|
695 | - */ |
|
696 | - private function _get_req_type_for_plugin_corresponding_to_this_dms() |
|
697 | - { |
|
698 | - if ($this->slug() == 'Core') { |
|
699 | - return EE_System::instance()->detect_req_type(); |
|
700 | - } else {// it must be for an addon |
|
701 | - $addon_name = $this->slug(); |
|
702 | - if (EE_Registry::instance()->get_addon_by_name($addon_name)) { |
|
703 | - return EE_Registry::instance()->get_addon_by_name($addon_name)->detect_req_type(); |
|
704 | - } else { |
|
705 | - throw new EE_Error( |
|
706 | - sprintf( |
|
707 | - esc_html__( |
|
708 | - "The DMS slug '%s' should correspond to the addon's name, which should also be '%s', but no such addon was registered. These are the registered addons' names: %s", |
|
709 | - "event_espresso" |
|
710 | - ), |
|
711 | - $this->slug(), |
|
712 | - $addon_name, |
|
713 | - implode(",", array_keys(EE_Registry::instance()->get_addons_by_name())) |
|
714 | - ) |
|
715 | - ); |
|
716 | - } |
|
717 | - } |
|
718 | - } |
|
719 | - |
|
720 | - |
|
721 | - /** |
|
722 | - * returns an array of strings describing errors by all the script's stages |
|
723 | - * |
|
724 | - * @return array |
|
725 | - */ |
|
726 | - public function get_errors() |
|
727 | - { |
|
728 | - $all_errors = $this->_errors; |
|
729 | - if (! is_array($all_errors)) { |
|
730 | - $all_errors = array(); |
|
731 | - } |
|
732 | - foreach ($this->stages() as $stage) { |
|
733 | - $all_errors = array_merge($stage->get_errors(), $all_errors); |
|
734 | - } |
|
735 | - return $all_errors; |
|
736 | - } |
|
737 | - |
|
738 | - |
|
739 | - /** |
|
740 | - * Indicates whether or not this migration script should continue |
|
741 | - * |
|
742 | - * @return boolean |
|
743 | - */ |
|
744 | - public function can_continue() |
|
745 | - { |
|
746 | - return in_array( |
|
747 | - $this->get_status(), |
|
748 | - EE_Data_Migration_Manager::instance()->stati_that_indicate_to_continue_single_migration_script |
|
749 | - ); |
|
750 | - } |
|
751 | - |
|
752 | - |
|
753 | - /** |
|
754 | - * Gets all the data migration stages associated with this script. Note: |
|
755 | - * addons can filter this list to add their own stages, and because the list is |
|
756 | - * numerically-indexed, they can insert their stage wherever they like and it will |
|
757 | - * get ordered by the indexes |
|
758 | - * |
|
759 | - * @return EE_Data_Migration_Script_Stage[] |
|
760 | - */ |
|
761 | - protected function stages() |
|
762 | - { |
|
763 | - $stages = apply_filters('FHEE__' . get_class($this) . '__stages', $this->_migration_stages); |
|
764 | - ksort($stages); |
|
765 | - return $stages; |
|
766 | - } |
|
767 | - |
|
768 | - |
|
769 | - /** |
|
770 | - * Gets a string which should describe what's going on currently with this migration, which |
|
771 | - * can be displayed to the user |
|
772 | - * |
|
773 | - * @return string |
|
774 | - */ |
|
775 | - public function get_feedback_message() |
|
776 | - { |
|
777 | - return $this->_feedback_message; |
|
778 | - } |
|
779 | - |
|
780 | - |
|
781 | - /** |
|
782 | - * A lot like "__sleep()" magic method in purpose, this is meant for persisting this class' |
|
783 | - * properties to the DB. However, we don't want to use __sleep() because its quite |
|
784 | - * possible that this class is defined when it goes to sleep, but NOT available when it |
|
785 | - * awakes (eg, this class is part of an addon that is deactivated at some point). |
|
786 | - */ |
|
787 | - public function properties_as_array() |
|
788 | - { |
|
789 | - $properties = parent::properties_as_array(); |
|
790 | - $properties['_migration_stages'] = array(); |
|
791 | - foreach ($this->_migration_stages as $migration_stage_priority => $migration_stage_class) { |
|
792 | - $properties['_migration_stages'][ $migration_stage_priority ] = $migration_stage_class->properties_as_array( |
|
793 | - ); |
|
794 | - } |
|
795 | - unset($properties['_mappings']); |
|
796 | - unset($properties['previous_dms']); |
|
797 | - |
|
798 | - foreach ($this->_mappings as $old_table_name => $mapping_to_new_table) { |
|
799 | - foreach ($mapping_to_new_table as $new_table_name => $mapping) { |
|
800 | - $this->_set_mapping_option($old_table_name, $new_table_name, $mapping); |
|
801 | - } |
|
802 | - } |
|
803 | - return $properties; |
|
804 | - } |
|
805 | - |
|
806 | - |
|
807 | - /** |
|
808 | - * Sets all of the properties of this script stage to match what's in the array, which is assumed |
|
809 | - * to have been made from the properties_as_array() function. |
|
810 | - * |
|
811 | - * @param array $array_of_properties like what's produced from properties_as_array() method |
|
812 | - * @return void |
|
813 | - */ |
|
814 | - public function instantiate_from_array_of_properties($array_of_properties) |
|
815 | - { |
|
816 | - $stages_properties_arrays = $array_of_properties['_migration_stages']; |
|
817 | - unset($array_of_properties['_migration_stages']); |
|
818 | - unset($array_of_properties['class']); |
|
819 | - foreach ($array_of_properties as $property_name => $property_value) { |
|
820 | - $this->{$property_name} = $property_value; |
|
821 | - } |
|
822 | - // _migration_stages are already instantiated, but have only default data |
|
823 | - foreach ($this->_migration_stages as $stage) { |
|
824 | - $stage_data = $this->_find_migration_stage_data_with_classname( |
|
825 | - get_class($stage), |
|
826 | - $stages_properties_arrays |
|
827 | - ); |
|
828 | - // SO, if we found the stage data that was saved, use it. Otherwise, I guess the stage is new? (maybe added by |
|
829 | - // an addon? Unlikely... not sure why it wouldn't exist, but if it doesn't just treat it like it was never started yet) |
|
830 | - if ($stage_data) { |
|
831 | - $stage->instantiate_from_array_of_properties($stage_data); |
|
832 | - } |
|
833 | - } |
|
834 | - } |
|
835 | - |
|
836 | - |
|
837 | - /** |
|
838 | - * Gets the migration data from the array $migration_stage_data_arrays (which is an array of arrays, each of which |
|
839 | - * is pretty well identical to EE_Data_Migration_Stage objects except all their properties are array indexes) |
|
840 | - * for the given classname |
|
841 | - * |
|
842 | - * @param string $classname |
|
843 | - * @param array $migration_stage_data_arrays |
|
844 | - * @return null |
|
845 | - */ |
|
846 | - private function _find_migration_stage_data_with_classname($classname, $migration_stage_data_arrays) |
|
847 | - { |
|
848 | - foreach ($migration_stage_data_arrays as $migration_stage_data_array) { |
|
849 | - if (isset($migration_stage_data_array['class']) && $migration_stage_data_array['class'] == $classname) { |
|
850 | - return $migration_stage_data_array; |
|
851 | - } |
|
852 | - } |
|
853 | - return null; |
|
854 | - } |
|
855 | - |
|
856 | - |
|
857 | - /** |
|
858 | - * Returns the version that this script migrates to, based on the script's name. |
|
859 | - * Cannot be overwritten because lots of code needs to know which version a script |
|
860 | - * migrates to knowing only its name. |
|
861 | - * |
|
862 | - * @return array where the first key is the plugin's slug, the 2nd is the version of that plugin |
|
863 | - * that will be updated to. Eg array('Core','4.1.0') |
|
864 | - * @throws EE_Error |
|
865 | - */ |
|
866 | - final public function migrates_to_version() |
|
867 | - { |
|
868 | - return EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
|
869 | - } |
|
870 | - |
|
871 | - |
|
872 | - /** |
|
873 | - * Gets this addon's slug as it would appear in the current_db_state wp option, |
|
874 | - * and if this migration script is for an addon, it SHOULD match the addon's slug |
|
875 | - * (and also the addon's classname, minus the 'EE_' prefix.). Eg, 'Calendar' for the EE_Calendar addon. |
|
876 | - * Or 'Core' for core (non-addon). |
|
877 | - * |
|
878 | - * @return string |
|
879 | - * @throws EE_Error |
|
880 | - */ |
|
881 | - public function slug() |
|
882 | - { |
|
883 | - $migrates_to_version_info = $this->migrates_to_version(); |
|
884 | - // the slug is the first part of the array |
|
885 | - return $migrates_to_version_info['slug']; |
|
886 | - } |
|
887 | - |
|
888 | - |
|
889 | - /** |
|
890 | - * Returns the script's priority relative to DMSs from other addons. However, when |
|
891 | - * two DMSs from the same addon/core apply, this is ignored (and instead the version that |
|
892 | - * the script migrates to is used to determine which to run first). The default is 5, but all core DMSs |
|
893 | - * normally have priority 10. (So if you want a DMS "A" to run before DMS "B", both of which are from addons, |
|
894 | - * and both of which CAN run at the same time (ie, "B" doesn't depend on "A" to set |
|
895 | - * the database up so it can run), then you can set "A" to priority 3 or something. |
|
896 | - * |
|
897 | - * @return int |
|
898 | - */ |
|
899 | - public function priority() |
|
900 | - { |
|
901 | - return $this->_priority; |
|
902 | - } |
|
903 | - |
|
904 | - |
|
905 | - /** |
|
906 | - * Sets whether or not this DMS is being ran as part of a migration, instead of |
|
907 | - * just being used to setup (or verify) the current database structure matches |
|
908 | - * what the latest DMS indicates it should be |
|
909 | - * |
|
910 | - * @param boolean $migrating |
|
911 | - * @return void |
|
912 | - */ |
|
913 | - public function set_migrating($migrating = true) |
|
914 | - { |
|
915 | - $this->_migrating = $migrating; |
|
916 | - } |
|
917 | - |
|
918 | - /** |
|
919 | - * Marks that we think this migration class can continue to migrate |
|
920 | - */ |
|
921 | - public function reattempt() |
|
922 | - { |
|
923 | - parent::reattempt(); |
|
924 | - // also, we want to reattempt any stages that were marked as borked |
|
925 | - foreach ($this->stages() as $stage) { |
|
926 | - if ($stage->is_broken()) { |
|
927 | - $stage->reattempt(); |
|
928 | - } |
|
929 | - } |
|
930 | - } |
|
17 | + /** |
|
18 | + * Set by client code to indicate this DMS is being ran as part of a proper migration, |
|
19 | + * instead of being used to merely setup (or verify) the database structure. |
|
20 | + * Defaults to TRUE, so client code that's NOT using this DMS as part of a proper migration |
|
21 | + * should call EE_Data_Migration_Script_Base::set_migrating( FALSE ) |
|
22 | + * |
|
23 | + * @var boolean |
|
24 | + */ |
|
25 | + protected $_migrating = true; |
|
26 | + |
|
27 | + /** |
|
28 | + * numerically-indexed array where each value is EE_Data_Migration_Script_Stage object |
|
29 | + * |
|
30 | + * @var EE_Data_Migration_Script_Stage[] $migration_functions |
|
31 | + */ |
|
32 | + protected $_migration_stages = array(); |
|
33 | + |
|
34 | + /** |
|
35 | + * Indicates we've already ran the schema changes that needed to happen BEFORE the data migration |
|
36 | + * |
|
37 | + * @var boolean |
|
38 | + */ |
|
39 | + protected $_schema_changes_before_migration_ran = null; |
|
40 | + |
|
41 | + /** |
|
42 | + * Indicates we've already ran the schema changes that needed to happen AFTER the data migration |
|
43 | + * |
|
44 | + * @var boolean |
|
45 | + */ |
|
46 | + protected $_schema_changes_after_migration_ran = null; |
|
47 | + |
|
48 | + /** |
|
49 | + * String which describes what's currently happening in this migration |
|
50 | + * |
|
51 | + * @var string |
|
52 | + */ |
|
53 | + protected $_feedback_message; |
|
54 | + |
|
55 | + /** |
|
56 | + * Indicates the script's priority. Like wp's add_action and add_filter, lower numbers |
|
57 | + * correspond to earlier execution |
|
58 | + * |
|
59 | + * @var int |
|
60 | + */ |
|
61 | + protected $_priority = 5; |
|
62 | + |
|
63 | + /** |
|
64 | + * Multi-dimensional array that defines the mapping from OLD table Primary Keys |
|
65 | + * to NEW table Primary Keys. |
|
66 | + * Top-level array keys are OLD table names (minus the "wp_" part), |
|
67 | + * 2nd-level array keys are NEW table names (again, minus the "wp_" part), |
|
68 | + * 3rd-level array keys are the OLD table primary keys |
|
69 | + * and 3rd-level array values are the NEW table primary keys |
|
70 | + * |
|
71 | + * @var array |
|
72 | + */ |
|
73 | + protected $_mappings = array(); |
|
74 | + |
|
75 | + /** |
|
76 | + * @var EE_Data_Migration_Script_Base |
|
77 | + */ |
|
78 | + protected $previous_dms; |
|
79 | + |
|
80 | + |
|
81 | + /** |
|
82 | + * Returns whether or not this data migration script can operate on the given version of the database. |
|
83 | + * Eg, if this migration script can migrate from 3.1.26 or higher (but not anything after 4.0.0), and |
|
84 | + * it's passed a string like '3.1.38B', it should return true. |
|
85 | + * If this DMS is to migrate data from an EE3 addon, you will probably want to use |
|
86 | + * EventEspresso\core\services\database\TableAnalysis::tableExists() to check for old EE3 tables, and |
|
87 | + * EE_Data_Migration_Manager::get_migration_ran() to check that core was already |
|
88 | + * migrated from EE3 to EE4 (ie, this DMS probably relies on some migration data generated |
|
89 | + * during the Core 4.1.0 DMS. If core didn't run that DMS, you probably don't want |
|
90 | + * to run this DMS). |
|
91 | + * If this DMS migrates data from a previous version of this EE4 addon, just |
|
92 | + * comparing $current_database_state_of[ $this->slug() ] will probably suffice. |
|
93 | + * If this DMS should never migrate data, because it's only used to define the initial |
|
94 | + * database state, just return FALSE (and core's activation process will take care |
|
95 | + * of calling its schema_changes_before_migration() and |
|
96 | + * schema_changes_after_migration() for you. ) |
|
97 | + * |
|
98 | + * @param array $current_database_state_of keys are EE plugin slugs (eg 'Core', 'Calendar', 'Mailchimp', etc) |
|
99 | + * @return boolean |
|
100 | + */ |
|
101 | + abstract public function can_migrate_from_version($current_database_state_of); |
|
102 | + |
|
103 | + |
|
104 | + /** |
|
105 | + * Performs database schema changes that need to occur BEFORE the data is migrated. |
|
106 | + * Eg, if we were going to change user passwords from plaintext to encoded versions |
|
107 | + * during this migration, this would probably add a new column called something like |
|
108 | + * "encoded_password". |
|
109 | + * |
|
110 | + * @return boolean of success |
|
111 | + */ |
|
112 | + abstract public function schema_changes_before_migration(); |
|
113 | + |
|
114 | + |
|
115 | + /** |
|
116 | + * Performs the database schema changes that need to occur AFTER the data has been migrated. |
|
117 | + * Usually this will mean we'll be removing old columns. Eg, if we were changing passwords |
|
118 | + * from plaintext to encoded versions, and we had added a column called "encoded_password", |
|
119 | + * this function would probably remove the old column "password" (which still holds the plaintext password) |
|
120 | + * and possibly rename "encoded_password" to "password" |
|
121 | + * |
|
122 | + * @return boolean of success |
|
123 | + */ |
|
124 | + abstract public function schema_changes_after_migration(); |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * All children of this must call parent::__construct() |
|
129 | + * at the end of their constructor or suffer the consequences! |
|
130 | + * |
|
131 | + * @param TableManager $table_manager |
|
132 | + * @param TableAnalysis $table_analysis |
|
133 | + */ |
|
134 | + public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null) |
|
135 | + { |
|
136 | + $this->_migration_stages = (array) apply_filters( |
|
137 | + 'FHEE__' . get_class($this) . '__construct__migration_stages', |
|
138 | + $this->_migration_stages |
|
139 | + ); |
|
140 | + foreach ($this->_migration_stages as $migration_stage) { |
|
141 | + if ($migration_stage instanceof EE_Data_Migration_Script_Stage) { |
|
142 | + $migration_stage->_construct_finalize($this); |
|
143 | + } |
|
144 | + } |
|
145 | + parent::__construct($table_manager, $table_analysis); |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * Place to add hooks and filters for tweaking the migrations page, in order |
|
151 | + * to customize it |
|
152 | + */ |
|
153 | + public function migration_page_hooks() |
|
154 | + { |
|
155 | + // by default none are added because we normally like the default look of the migration page |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + /** |
|
160 | + * Sets the mapping from old table primary keys to new table primary keys. |
|
161 | + * This mapping is automatically persisted as a property on the migration |
|
162 | + * |
|
163 | + * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
164 | + * @param int|string $old_pk old primary key. Eg events_detail.id's value |
|
165 | + * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
166 | + * @param int|string $new_pk eg posts.ID |
|
167 | + * @return void |
|
168 | + * @throws EE_Error |
|
169 | + */ |
|
170 | + public function set_mapping($old_table, $old_pk, $new_table, $new_pk) |
|
171 | + { |
|
172 | + // make sure it has the needed keys |
|
173 | + if (! isset($this->_mappings[ $old_table ]) || ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
174 | + $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
175 | + } |
|
176 | + $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] = $new_pk; |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * Gets the new primary key, if provided with the OLD table and the primary key |
|
182 | + * of an item in the old table, and the new table |
|
183 | + * |
|
184 | + * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
185 | + * @param int|string $old_pk old primary key. Eg events_detail.id's value |
|
186 | + * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
187 | + * @return mixed the primary key on the new table |
|
188 | + * @throws EE_Error |
|
189 | + */ |
|
190 | + public function get_mapping_new_pk($old_table, $old_pk, $new_table) |
|
191 | + { |
|
192 | + if ( |
|
193 | + ! isset($this->_mappings[ $old_table ]) || |
|
194 | + ! isset($this->_mappings[ $old_table ][ $new_table ]) |
|
195 | + ) { |
|
196 | + // try fetching the option |
|
197 | + $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
198 | + } |
|
199 | + return isset($this->_mappings[ $old_table ][ $new_table ][ $old_pk ]) |
|
200 | + ? $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] : null; |
|
201 | + } |
|
202 | + |
|
203 | + |
|
204 | + /** |
|
205 | + * Gets the old primary key, if provided with the OLD table, |
|
206 | + * and the new table and the primary key of an item in the new table |
|
207 | + * |
|
208 | + * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
209 | + * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
210 | + * @param mixed $new_pk |
|
211 | + * @return mixed |
|
212 | + * @throws EE_Error |
|
213 | + */ |
|
214 | + public function get_mapping_old_pk($old_table, $new_table, $new_pk) |
|
215 | + { |
|
216 | + if ( |
|
217 | + ! isset($this->_mappings[ $old_table ]) || |
|
218 | + ! isset($this->_mappings[ $old_table ][ $new_table ]) |
|
219 | + ) { |
|
220 | + // try fetching the option |
|
221 | + $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
222 | + } |
|
223 | + if (isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
224 | + $new_pk_to_old_pk = array_flip($this->_mappings[ $old_table ][ $new_table ]); |
|
225 | + if (isset($new_pk_to_old_pk[ $new_pk ])) { |
|
226 | + return $new_pk_to_old_pk[ $new_pk ]; |
|
227 | + } |
|
228 | + } |
|
229 | + return null; |
|
230 | + } |
|
231 | + |
|
232 | + |
|
233 | + /** |
|
234 | + * Gets the mapping array option specified by the table names |
|
235 | + * |
|
236 | + * @param string $old_table_name |
|
237 | + * @param string $new_table_name |
|
238 | + * @return array |
|
239 | + * @throws EE_Error |
|
240 | + */ |
|
241 | + protected function _get_mapping_option($old_table_name, $new_table_name) |
|
242 | + { |
|
243 | + return get_option($this->_get_mapping_option_name($old_table_name, $new_table_name), array()); |
|
244 | + } |
|
245 | + |
|
246 | + |
|
247 | + /** |
|
248 | + * Updates the mapping option specified by the table names with the array provided |
|
249 | + * |
|
250 | + * @param string $old_table_name |
|
251 | + * @param string $new_table_name |
|
252 | + * @param array $mapping_array |
|
253 | + * @return boolean success of updating option |
|
254 | + * @throws EE_Error |
|
255 | + */ |
|
256 | + protected function _set_mapping_option($old_table_name, $new_table_name, $mapping_array) |
|
257 | + { |
|
258 | + $success = update_option($this->_get_mapping_option_name($old_table_name, $new_table_name), $mapping_array, false); |
|
259 | + return $success; |
|
260 | + } |
|
261 | + |
|
262 | + |
|
263 | + /** |
|
264 | + * Gets the option name for this script to map from $old_table_name to $new_table_name |
|
265 | + * |
|
266 | + * @param string $old_table_name |
|
267 | + * @param string $new_table_name |
|
268 | + * @return string |
|
269 | + * @throws EE_Error |
|
270 | + */ |
|
271 | + protected function _get_mapping_option_name($old_table_name, $new_table_name) |
|
272 | + { |
|
273 | + global $wpdb; |
|
274 | + $old_table_name_sans_wp = str_replace($wpdb->prefix, "", $old_table_name); |
|
275 | + $new_table_name_sans_wp = str_replace($wpdb->prefix, "", $new_table_name); |
|
276 | + $migrates_to = EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
|
277 | + return substr( |
|
278 | + EE_Data_Migration_Manager::data_migration_script_mapping_option_prefix . $migrates_to ['slug'] . '_' . $migrates_to['version'] . '_' . $old_table_name_sans_wp . '_' . $new_table_name_sans_wp, |
|
279 | + 0, |
|
280 | + 64 |
|
281 | + ); |
|
282 | + } |
|
283 | + |
|
284 | + |
|
285 | + /** |
|
286 | + * Counts all the records that will be migrated during this data migration. |
|
287 | + * For example, if we were changing old user passwords from plaintext to encoded versions, |
|
288 | + * this would be a count of all users who have passwords. If we were going to also split |
|
289 | + * attendee records into transactions, registrations, and attendee records, this would include |
|
290 | + * the count of all attendees currently in existence in the DB (ie, users + attendees). |
|
291 | + * If you can't determine how many records there are to migrate, just provide a guess: this |
|
292 | + * number will only be used in calculating the percent complete. If you estimate there to be |
|
293 | + * 100 records to migrate, and it turns out there's 120, we'll just show the migration as being at |
|
294 | + * 99% until the function "migration_step" returns EE_Data_Migration_Script_Base::status_complete. |
|
295 | + * |
|
296 | + * @return int |
|
297 | + */ |
|
298 | + protected function _count_records_to_migrate() |
|
299 | + { |
|
300 | + $count = 0; |
|
301 | + foreach ($this->stages() as $stage) { |
|
302 | + $count += $stage->count_records_to_migrate(); |
|
303 | + } |
|
304 | + return $count; |
|
305 | + } |
|
306 | + |
|
307 | + |
|
308 | + /** |
|
309 | + * Returns the number of records updated so far. Usually this is easiest to do |
|
310 | + * by just setting a transient and updating it after each migration_step |
|
311 | + * |
|
312 | + * @return int |
|
313 | + */ |
|
314 | + public function count_records_migrated() |
|
315 | + { |
|
316 | + $count = 0; |
|
317 | + foreach ($this->stages() as $stage) { |
|
318 | + $count += $stage->count_records_migrated(); |
|
319 | + } |
|
320 | + $this->_records_migrated = $count; |
|
321 | + return $count; |
|
322 | + } |
|
323 | + |
|
324 | + |
|
325 | + /** |
|
326 | + * @param int $num_records_to_migrate_limit |
|
327 | + * @return int |
|
328 | + * @throws EE_Error |
|
329 | + * @throws Exception |
|
330 | + */ |
|
331 | + public function migration_step($num_records_to_migrate_limit) |
|
332 | + { |
|
333 | + // reset the feedback message |
|
334 | + $this->_feedback_message = ''; |
|
335 | + // if we haven't yet done the 1st schema changes, do them now. buffer any output |
|
336 | + $this->_maybe_do_schema_changes(true); |
|
337 | + |
|
338 | + $num_records_actually_migrated = 0; |
|
339 | + $records_migrated_per_stage = array(); |
|
340 | + // setup the 'stage' variable, which should hold the last run stage of the migration (or none at all if nothing runs) |
|
341 | + $stage = null; |
|
342 | + // get the next stage that isn't complete |
|
343 | + foreach ($this->stages() as $stage) { |
|
344 | + if ($stage->get_status() == EE_Data_Migration_Manager::status_continue) { |
|
345 | + try { |
|
346 | + $records_migrated_during_stage = $stage->migration_step( |
|
347 | + $num_records_to_migrate_limit - $num_records_actually_migrated |
|
348 | + ); |
|
349 | + $num_records_actually_migrated += $records_migrated_during_stage; |
|
350 | + $records_migrated_per_stage[ $stage->pretty_name() ] = $records_migrated_during_stage; |
|
351 | + } catch (Exception $e) { |
|
352 | + // yes if we catch an exception here, we consider that migration stage borked. |
|
353 | + $stage->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
354 | + $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
355 | + $stage->add_error($e->getMessage() . ". Stack-trace:" . $e->getTraceAsString()); |
|
356 | + throw $e; |
|
357 | + } |
|
358 | + // check that the migration stage didn't mark itself as having a fatal error |
|
359 | + if ($stage->is_broken()) { |
|
360 | + $this->set_broken(); |
|
361 | + throw new EE_Error($stage->get_last_error()); |
|
362 | + } |
|
363 | + } |
|
364 | + // once we've migrated all the number we intended to (possibly from different stages), stop migrating |
|
365 | + // or if we had a fatal error |
|
366 | + // or if the current script stopped early- its not done, but it's done all it thinks we should do on this step |
|
367 | + if ( |
|
368 | + $num_records_actually_migrated >= $num_records_to_migrate_limit |
|
369 | + || $stage->is_broken() |
|
370 | + || $stage->has_more_to_do() |
|
371 | + ) { |
|
372 | + break; |
|
373 | + } |
|
374 | + } |
|
375 | + // check if we're all done this data migration... |
|
376 | + // which is indicated by being done early AND the last stage claims to be done |
|
377 | + if ($stage == null) { |
|
378 | + // this migration script apparently has NO stages... which is super weird, but whatever |
|
379 | + $this->set_completed(); |
|
380 | + $this->_maybe_do_schema_changes(false); |
|
381 | + } elseif ($num_records_actually_migrated < $num_records_to_migrate_limit && ! $stage->has_more_to_do()) { |
|
382 | + // apparently we're done, because we couldn't migrate the number we intended to |
|
383 | + $this->set_completed(); |
|
384 | + $this->_update_feedback_message(array_reverse($records_migrated_per_stage)); |
|
385 | + // do schema changes for after the migration now |
|
386 | + // first double-check we haven't already done this |
|
387 | + $this->_maybe_do_schema_changes(false); |
|
388 | + } else { |
|
389 | + // update feedback message, keeping in mind that we show them with the most recent at the top |
|
390 | + $this->_update_feedback_message(array_reverse($records_migrated_per_stage)); |
|
391 | + } |
|
392 | + return $num_records_actually_migrated; |
|
393 | + } |
|
394 | + |
|
395 | + |
|
396 | + /** |
|
397 | + * Updates the feedback message according to what was done during this migration stage. |
|
398 | + * |
|
399 | + * @param array $records_migrated_per_stage KEYS are pretty names for each stage; values are the count of records |
|
400 | + * migrated from that stage |
|
401 | + * @return void |
|
402 | + */ |
|
403 | + private function _update_feedback_message($records_migrated_per_stage) |
|
404 | + { |
|
405 | + $feedback_message_array = array(); |
|
406 | + foreach ($records_migrated_per_stage as $migration_stage_name => $num_records_migrated) { |
|
407 | + $feedback_message_array[] = sprintf( |
|
408 | + esc_html__("Migrated %d records successfully during %s", "event_espresso"), |
|
409 | + $num_records_migrated, |
|
410 | + $migration_stage_name |
|
411 | + ); |
|
412 | + } |
|
413 | + $this->_feedback_message .= implode("<br>", $feedback_message_array); |
|
414 | + } |
|
415 | + |
|
416 | + |
|
417 | + /** |
|
418 | + * Calls either schema_changes_before_migration() (if $before==true) or schema_changes_after_migration |
|
419 | + * (if $before==false). Buffers their outputs and stores them on the class. |
|
420 | + * |
|
421 | + * @param boolean $before |
|
422 | + * @throws Exception |
|
423 | + * @return void |
|
424 | + */ |
|
425 | + private function _maybe_do_schema_changes($before = true) |
|
426 | + { |
|
427 | + // so this property will be either _schema_changes_after_migration_ran or _schema_changes_before_migration_ran |
|
428 | + $property_name = '_schema_changes_' . ($before ? 'before' : 'after') . '_migration_ran'; |
|
429 | + if (! $this->{$property_name}) { |
|
430 | + try { |
|
431 | + ob_start(); |
|
432 | + if ($before) { |
|
433 | + $this->schema_changes_before_migration(); |
|
434 | + } else { |
|
435 | + $this->schema_changes_after_migration(); |
|
436 | + } |
|
437 | + $output = ob_get_contents(); |
|
438 | + ob_end_clean(); |
|
439 | + } catch (Exception $e) { |
|
440 | + $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
441 | + throw $e; |
|
442 | + } |
|
443 | + // record that we've done these schema changes |
|
444 | + $this->{$property_name} = true; |
|
445 | + // if there were any warnings etc, record them as non-fatal errors |
|
446 | + if ($output) { |
|
447 | + // there were some warnings |
|
448 | + $this->_errors[] = $output; |
|
449 | + } |
|
450 | + } |
|
451 | + } |
|
452 | + |
|
453 | + |
|
454 | + /** |
|
455 | + * Wrapper for EEH_Activation::create_table. However, takes into account the request type when |
|
456 | + * deciding what to pass for its 4th arg, $drop_pre_existing_tables. Using this function, instead |
|
457 | + * of _table_should_exist_previously, indicates that this table should be new to the EE version being migrated to |
|
458 | + * or |
|
459 | + * activated currently. If this is a brand new activation or a migration, and we're indicating this table should |
|
460 | + * not |
|
461 | + * previously exist, then we want to set $drop_pre_existing_tables to TRUE (ie, we shouldn't discover that this |
|
462 | + * table exists in the DB in EEH_Activation::create_table- if it DOES exist, something's wrong and the old table |
|
463 | + * should be nuked. |
|
464 | + * |
|
465 | + * Just for a bit of context, the migration script's db_schema_changes_* methods |
|
466 | + * are called basically in 3 cases: on brand new activation of EE4 (ie no previous version of EE existed and the |
|
467 | + * plugin is being activated and we want to add all the brand new tables), upon reactivation of EE4 (it was |
|
468 | + * deactivated and then reactivated, in which case we want to just verify the DB structure is ok) that table should |
|
469 | + * be dropped), and during a migration when we're moving the DB to the state of the migration script |
|
470 | + * |
|
471 | + * @param string $table_name |
|
472 | + * @param string $table_definition_sql |
|
473 | + * @param string $engine_string |
|
474 | + * @throws EE_Error |
|
475 | + * @throws ReflectionException |
|
476 | + */ |
|
477 | + protected function _table_is_new_in_this_version( |
|
478 | + $table_name, |
|
479 | + $table_definition_sql, |
|
480 | + $engine_string = 'ENGINE=InnoDB ' |
|
481 | + ) { |
|
482 | + $this->_create_table_and_catch_errors( |
|
483 | + $table_name, |
|
484 | + $table_definition_sql, |
|
485 | + $engine_string, |
|
486 | + $this->_pre_existing_table_should_be_dropped(true) |
|
487 | + ); |
|
488 | + } |
|
489 | + |
|
490 | + |
|
491 | + /** |
|
492 | + * Like _table_is_new_in_this_version and _table_should_exist_previously, this function verifies the given table |
|
493 | + * exists. But we understand that this table has CHANGED in this version since the previous version. So it's not |
|
494 | + * completely new, but it's different. So we need to treat it like a new table in terms of verifying it's schema is |
|
495 | + * correct on activations, migrations, upgrades; but if it exists when it shouldn't, we need to be as lenient as |
|
496 | + * _table_should_exist_previously. |
|
497 | + * 8656]{Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the |
|
498 | + * table shouldn't exist). |
|
499 | + * |
|
500 | + * @param string $table_name |
|
501 | + * @param string $table_definition_sql |
|
502 | + * @param string $engine_string |
|
503 | + * @throws EE_Error |
|
504 | + * @throws ReflectionException |
|
505 | + */ |
|
506 | + protected function _table_is_changed_in_this_version( |
|
507 | + $table_name, |
|
508 | + $table_definition_sql, |
|
509 | + $engine_string = 'ENGINE=MyISAM' |
|
510 | + ) { |
|
511 | + $this->_create_table_and_catch_errors( |
|
512 | + $table_name, |
|
513 | + $table_definition_sql, |
|
514 | + $engine_string, |
|
515 | + $this->_pre_existing_table_should_be_dropped(false) |
|
516 | + ); |
|
517 | + } |
|
518 | + |
|
519 | + |
|
520 | + /** |
|
521 | + * _old_table_exists |
|
522 | + * returns TRUE if the requested table exists in the current database |
|
523 | + * |
|
524 | + * @param string $table_name |
|
525 | + * @return boolean |
|
526 | + * @throws EE_Error |
|
527 | + */ |
|
528 | + protected function _old_table_exists($table_name) |
|
529 | + { |
|
530 | + return $this->_get_table_analysis()->tableExists($table_name); |
|
531 | + } |
|
532 | + |
|
533 | + |
|
534 | + /** |
|
535 | + * _delete_table_if_empty |
|
536 | + * returns TRUE if the requested table was empty and successfully empty |
|
537 | + * |
|
538 | + * @param string $table_name |
|
539 | + * @return boolean |
|
540 | + * @throws EE_Error |
|
541 | + * @throws ReflectionException |
|
542 | + */ |
|
543 | + protected function _delete_table_if_empty($table_name) |
|
544 | + { |
|
545 | + return EEH_Activation::delete_db_table_if_empty($table_name); |
|
546 | + } |
|
547 | + |
|
548 | + |
|
549 | + /** |
|
550 | + * It is preferred to use _table_has_not_changed_since_previous or _table_is_changed_in_this_version |
|
551 | + * as these are significantly more efficient or explicit. |
|
552 | + * Please see description of _table_is_new_in_this_version. This function will only set |
|
553 | + * EEH_Activation::create_table's $drop_pre_existing_tables to TRUE if it's a brand |
|
554 | + * new activation. ie, a more accurate name for this method would be "_table_added_previously_by_this_plugin" |
|
555 | + * because the table will be cleared out if this is a new activation (ie, if its a new activation, it actually |
|
556 | + * should exist previously). Otherwise, we'll always set $drop_pre_existing_tables to FALSE because the table |
|
557 | + * should have existed. Note, if the table is being MODIFIED in this version being activated or migrated to, then |
|
558 | + * you want _table_is_changed_in_this_version NOT this one. We don't check this table's structure during migrations |
|
559 | + * because apparently it hasn't changed since the previous one, right? |
|
560 | + * |
|
561 | + * @param string $table_name |
|
562 | + * @param string $table_definition_sql |
|
563 | + * @param string $engine_string |
|
564 | + * @throws EE_Error |
|
565 | + * @throws ReflectionException |
|
566 | + */ |
|
567 | + protected function _table_should_exist_previously( |
|
568 | + $table_name, |
|
569 | + $table_definition_sql, |
|
570 | + $engine_string = 'ENGINE=MyISAM' |
|
571 | + ) { |
|
572 | + $this->_create_table_and_catch_errors( |
|
573 | + $table_name, |
|
574 | + $table_definition_sql, |
|
575 | + $engine_string, |
|
576 | + $this->_pre_existing_table_should_be_dropped(false) |
|
577 | + ); |
|
578 | + } |
|
579 | + |
|
580 | + |
|
581 | + /** |
|
582 | + * Exactly the same as _table_should_exist_previously(), except if this migration script is currently doing |
|
583 | + * a migration, we skip checking this table's structure in the database and just assume it's correct. |
|
584 | + * So this is useful only to improve efficiency when doing migrations (not a big deal for single site installs, |
|
585 | + * but important for multisite where migrations can take a very long time otherwise). |
|
586 | + * If the table is known to have changed since previous version, use _table_is_changed_in_this_version(). |
|
587 | + * Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the table |
|
588 | + * shouldn't exist). |
|
589 | + * |
|
590 | + * @param string $table_name |
|
591 | + * @param string $table_definition_sql |
|
592 | + * @param string $engine_string |
|
593 | + * @throws EE_Error |
|
594 | + * @throws ReflectionException |
|
595 | + */ |
|
596 | + protected function _table_has_not_changed_since_previous( |
|
597 | + $table_name, |
|
598 | + $table_definition_sql, |
|
599 | + $engine_string = 'ENGINE=MyISAM' |
|
600 | + ) { |
|
601 | + if ($this->_currently_migrating()) { |
|
602 | + // if we're doing a migration, and this table apparently already exists, then we don't need do anything right? |
|
603 | + return; |
|
604 | + } |
|
605 | + $this->_create_table_and_catch_errors( |
|
606 | + $table_name, |
|
607 | + $table_definition_sql, |
|
608 | + $engine_string, |
|
609 | + $this->_pre_existing_table_should_be_dropped(false) |
|
610 | + ); |
|
611 | + } |
|
612 | + |
|
613 | + /** |
|
614 | + * Returns whether or not this migration script is being used as part of an actual migration |
|
615 | + * |
|
616 | + * @return boolean |
|
617 | + */ |
|
618 | + protected function _currently_migrating() |
|
619 | + { |
|
620 | + // we want to know if we are currently performing a migration. We could just believe what was set on the _migrating property, but let's double-check (ie the script should apply and we should be in MM) |
|
621 | + return $this->_migrating && |
|
622 | + $this->can_migrate_from_version( |
|
623 | + EE_Data_Migration_Manager::instance()->ensure_current_database_state_is_set() |
|
624 | + ) && |
|
625 | + EE_Maintenance_Mode::instance()->real_level() == EE_Maintenance_Mode::level_2_complete_maintenance; |
|
626 | + } |
|
627 | + |
|
628 | + |
|
629 | + /** |
|
630 | + * Determines if a table should be dropped, based on whether it's reported to be new in $table_is_new, |
|
631 | + * and the plugin's request type. |
|
632 | + * Assumes only this plugin could have added the table (ie, if its a new activation of this plugin, the table |
|
633 | + * shouldn't exist no matter what). |
|
634 | + * |
|
635 | + * @param boolean $table_is_new |
|
636 | + * @return boolean |
|
637 | + * @throws EE_Error |
|
638 | + */ |
|
639 | + protected function _pre_existing_table_should_be_dropped($table_is_new) |
|
640 | + { |
|
641 | + if ($table_is_new) { |
|
642 | + if ( |
|
643 | + $this->_get_req_type_for_plugin_corresponding_to_this_dms() == EE_System::req_type_new_activation |
|
644 | + || $this->_currently_migrating() |
|
645 | + ) { |
|
646 | + return true; |
|
647 | + } else { |
|
648 | + return false; |
|
649 | + } |
|
650 | + } else { |
|
651 | + if ( |
|
652 | + in_array( |
|
653 | + $this->_get_req_type_for_plugin_corresponding_to_this_dms(), |
|
654 | + array(EE_System::req_type_new_activation) |
|
655 | + ) |
|
656 | + ) { |
|
657 | + return true; |
|
658 | + } else { |
|
659 | + return false; |
|
660 | + } |
|
661 | + } |
|
662 | + } |
|
663 | + |
|
664 | + |
|
665 | + /** |
|
666 | + * Just wraps EEH_Activation::create_table, but catches any errors it may throw and adds them as errors on the DMS |
|
667 | + * |
|
668 | + * @param string $table_name |
|
669 | + * @param string $table_definition_sql |
|
670 | + * @param string $engine_string |
|
671 | + * @param boolean $drop_pre_existing_tables |
|
672 | + * @throws ReflectionException |
|
673 | + */ |
|
674 | + private function _create_table_and_catch_errors( |
|
675 | + $table_name, |
|
676 | + $table_definition_sql, |
|
677 | + $engine_string = 'ENGINE=MyISAM', |
|
678 | + $drop_pre_existing_tables = false |
|
679 | + ) { |
|
680 | + try { |
|
681 | + EEH_Activation::create_table($table_name, $table_definition_sql, $engine_string, $drop_pre_existing_tables); |
|
682 | + } catch (EE_Error $e) { |
|
683 | + $message = $e->getMessage() . '<br>Stack Trace:' . $e->getTraceAsString(); |
|
684 | + $this->add_error($message); |
|
685 | + $this->_feedback_message .= $message; |
|
686 | + } |
|
687 | + } |
|
688 | + |
|
689 | + |
|
690 | + /** |
|
691 | + * Gets the request type for the plugin (core or addon) that corresponds to this DMS |
|
692 | + * |
|
693 | + * @return int one of EE_System::_req_type_* constants |
|
694 | + * @throws EE_Error |
|
695 | + */ |
|
696 | + private function _get_req_type_for_plugin_corresponding_to_this_dms() |
|
697 | + { |
|
698 | + if ($this->slug() == 'Core') { |
|
699 | + return EE_System::instance()->detect_req_type(); |
|
700 | + } else {// it must be for an addon |
|
701 | + $addon_name = $this->slug(); |
|
702 | + if (EE_Registry::instance()->get_addon_by_name($addon_name)) { |
|
703 | + return EE_Registry::instance()->get_addon_by_name($addon_name)->detect_req_type(); |
|
704 | + } else { |
|
705 | + throw new EE_Error( |
|
706 | + sprintf( |
|
707 | + esc_html__( |
|
708 | + "The DMS slug '%s' should correspond to the addon's name, which should also be '%s', but no such addon was registered. These are the registered addons' names: %s", |
|
709 | + "event_espresso" |
|
710 | + ), |
|
711 | + $this->slug(), |
|
712 | + $addon_name, |
|
713 | + implode(",", array_keys(EE_Registry::instance()->get_addons_by_name())) |
|
714 | + ) |
|
715 | + ); |
|
716 | + } |
|
717 | + } |
|
718 | + } |
|
719 | + |
|
720 | + |
|
721 | + /** |
|
722 | + * returns an array of strings describing errors by all the script's stages |
|
723 | + * |
|
724 | + * @return array |
|
725 | + */ |
|
726 | + public function get_errors() |
|
727 | + { |
|
728 | + $all_errors = $this->_errors; |
|
729 | + if (! is_array($all_errors)) { |
|
730 | + $all_errors = array(); |
|
731 | + } |
|
732 | + foreach ($this->stages() as $stage) { |
|
733 | + $all_errors = array_merge($stage->get_errors(), $all_errors); |
|
734 | + } |
|
735 | + return $all_errors; |
|
736 | + } |
|
737 | + |
|
738 | + |
|
739 | + /** |
|
740 | + * Indicates whether or not this migration script should continue |
|
741 | + * |
|
742 | + * @return boolean |
|
743 | + */ |
|
744 | + public function can_continue() |
|
745 | + { |
|
746 | + return in_array( |
|
747 | + $this->get_status(), |
|
748 | + EE_Data_Migration_Manager::instance()->stati_that_indicate_to_continue_single_migration_script |
|
749 | + ); |
|
750 | + } |
|
751 | + |
|
752 | + |
|
753 | + /** |
|
754 | + * Gets all the data migration stages associated with this script. Note: |
|
755 | + * addons can filter this list to add their own stages, and because the list is |
|
756 | + * numerically-indexed, they can insert their stage wherever they like and it will |
|
757 | + * get ordered by the indexes |
|
758 | + * |
|
759 | + * @return EE_Data_Migration_Script_Stage[] |
|
760 | + */ |
|
761 | + protected function stages() |
|
762 | + { |
|
763 | + $stages = apply_filters('FHEE__' . get_class($this) . '__stages', $this->_migration_stages); |
|
764 | + ksort($stages); |
|
765 | + return $stages; |
|
766 | + } |
|
767 | + |
|
768 | + |
|
769 | + /** |
|
770 | + * Gets a string which should describe what's going on currently with this migration, which |
|
771 | + * can be displayed to the user |
|
772 | + * |
|
773 | + * @return string |
|
774 | + */ |
|
775 | + public function get_feedback_message() |
|
776 | + { |
|
777 | + return $this->_feedback_message; |
|
778 | + } |
|
779 | + |
|
780 | + |
|
781 | + /** |
|
782 | + * A lot like "__sleep()" magic method in purpose, this is meant for persisting this class' |
|
783 | + * properties to the DB. However, we don't want to use __sleep() because its quite |
|
784 | + * possible that this class is defined when it goes to sleep, but NOT available when it |
|
785 | + * awakes (eg, this class is part of an addon that is deactivated at some point). |
|
786 | + */ |
|
787 | + public function properties_as_array() |
|
788 | + { |
|
789 | + $properties = parent::properties_as_array(); |
|
790 | + $properties['_migration_stages'] = array(); |
|
791 | + foreach ($this->_migration_stages as $migration_stage_priority => $migration_stage_class) { |
|
792 | + $properties['_migration_stages'][ $migration_stage_priority ] = $migration_stage_class->properties_as_array( |
|
793 | + ); |
|
794 | + } |
|
795 | + unset($properties['_mappings']); |
|
796 | + unset($properties['previous_dms']); |
|
797 | + |
|
798 | + foreach ($this->_mappings as $old_table_name => $mapping_to_new_table) { |
|
799 | + foreach ($mapping_to_new_table as $new_table_name => $mapping) { |
|
800 | + $this->_set_mapping_option($old_table_name, $new_table_name, $mapping); |
|
801 | + } |
|
802 | + } |
|
803 | + return $properties; |
|
804 | + } |
|
805 | + |
|
806 | + |
|
807 | + /** |
|
808 | + * Sets all of the properties of this script stage to match what's in the array, which is assumed |
|
809 | + * to have been made from the properties_as_array() function. |
|
810 | + * |
|
811 | + * @param array $array_of_properties like what's produced from properties_as_array() method |
|
812 | + * @return void |
|
813 | + */ |
|
814 | + public function instantiate_from_array_of_properties($array_of_properties) |
|
815 | + { |
|
816 | + $stages_properties_arrays = $array_of_properties['_migration_stages']; |
|
817 | + unset($array_of_properties['_migration_stages']); |
|
818 | + unset($array_of_properties['class']); |
|
819 | + foreach ($array_of_properties as $property_name => $property_value) { |
|
820 | + $this->{$property_name} = $property_value; |
|
821 | + } |
|
822 | + // _migration_stages are already instantiated, but have only default data |
|
823 | + foreach ($this->_migration_stages as $stage) { |
|
824 | + $stage_data = $this->_find_migration_stage_data_with_classname( |
|
825 | + get_class($stage), |
|
826 | + $stages_properties_arrays |
|
827 | + ); |
|
828 | + // SO, if we found the stage data that was saved, use it. Otherwise, I guess the stage is new? (maybe added by |
|
829 | + // an addon? Unlikely... not sure why it wouldn't exist, but if it doesn't just treat it like it was never started yet) |
|
830 | + if ($stage_data) { |
|
831 | + $stage->instantiate_from_array_of_properties($stage_data); |
|
832 | + } |
|
833 | + } |
|
834 | + } |
|
835 | + |
|
836 | + |
|
837 | + /** |
|
838 | + * Gets the migration data from the array $migration_stage_data_arrays (which is an array of arrays, each of which |
|
839 | + * is pretty well identical to EE_Data_Migration_Stage objects except all their properties are array indexes) |
|
840 | + * for the given classname |
|
841 | + * |
|
842 | + * @param string $classname |
|
843 | + * @param array $migration_stage_data_arrays |
|
844 | + * @return null |
|
845 | + */ |
|
846 | + private function _find_migration_stage_data_with_classname($classname, $migration_stage_data_arrays) |
|
847 | + { |
|
848 | + foreach ($migration_stage_data_arrays as $migration_stage_data_array) { |
|
849 | + if (isset($migration_stage_data_array['class']) && $migration_stage_data_array['class'] == $classname) { |
|
850 | + return $migration_stage_data_array; |
|
851 | + } |
|
852 | + } |
|
853 | + return null; |
|
854 | + } |
|
855 | + |
|
856 | + |
|
857 | + /** |
|
858 | + * Returns the version that this script migrates to, based on the script's name. |
|
859 | + * Cannot be overwritten because lots of code needs to know which version a script |
|
860 | + * migrates to knowing only its name. |
|
861 | + * |
|
862 | + * @return array where the first key is the plugin's slug, the 2nd is the version of that plugin |
|
863 | + * that will be updated to. Eg array('Core','4.1.0') |
|
864 | + * @throws EE_Error |
|
865 | + */ |
|
866 | + final public function migrates_to_version() |
|
867 | + { |
|
868 | + return EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
|
869 | + } |
|
870 | + |
|
871 | + |
|
872 | + /** |
|
873 | + * Gets this addon's slug as it would appear in the current_db_state wp option, |
|
874 | + * and if this migration script is for an addon, it SHOULD match the addon's slug |
|
875 | + * (and also the addon's classname, minus the 'EE_' prefix.). Eg, 'Calendar' for the EE_Calendar addon. |
|
876 | + * Or 'Core' for core (non-addon). |
|
877 | + * |
|
878 | + * @return string |
|
879 | + * @throws EE_Error |
|
880 | + */ |
|
881 | + public function slug() |
|
882 | + { |
|
883 | + $migrates_to_version_info = $this->migrates_to_version(); |
|
884 | + // the slug is the first part of the array |
|
885 | + return $migrates_to_version_info['slug']; |
|
886 | + } |
|
887 | + |
|
888 | + |
|
889 | + /** |
|
890 | + * Returns the script's priority relative to DMSs from other addons. However, when |
|
891 | + * two DMSs from the same addon/core apply, this is ignored (and instead the version that |
|
892 | + * the script migrates to is used to determine which to run first). The default is 5, but all core DMSs |
|
893 | + * normally have priority 10. (So if you want a DMS "A" to run before DMS "B", both of which are from addons, |
|
894 | + * and both of which CAN run at the same time (ie, "B" doesn't depend on "A" to set |
|
895 | + * the database up so it can run), then you can set "A" to priority 3 or something. |
|
896 | + * |
|
897 | + * @return int |
|
898 | + */ |
|
899 | + public function priority() |
|
900 | + { |
|
901 | + return $this->_priority; |
|
902 | + } |
|
903 | + |
|
904 | + |
|
905 | + /** |
|
906 | + * Sets whether or not this DMS is being ran as part of a migration, instead of |
|
907 | + * just being used to setup (or verify) the current database structure matches |
|
908 | + * what the latest DMS indicates it should be |
|
909 | + * |
|
910 | + * @param boolean $migrating |
|
911 | + * @return void |
|
912 | + */ |
|
913 | + public function set_migrating($migrating = true) |
|
914 | + { |
|
915 | + $this->_migrating = $migrating; |
|
916 | + } |
|
917 | + |
|
918 | + /** |
|
919 | + * Marks that we think this migration class can continue to migrate |
|
920 | + */ |
|
921 | + public function reattempt() |
|
922 | + { |
|
923 | + parent::reattempt(); |
|
924 | + // also, we want to reattempt any stages that were marked as borked |
|
925 | + foreach ($this->stages() as $stage) { |
|
926 | + if ($stage->is_broken()) { |
|
927 | + $stage->reattempt(); |
|
928 | + } |
|
929 | + } |
|
930 | + } |
|
931 | 931 | } |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | { |
19 | 19 | $new_post_content = $this->_change_event_list_shortcode($old_row['post_content']); |
20 | 20 | global $wpdb; |
21 | - $wpdb->query($wpdb->prepare("UPDATE " . $this->_old_table . " SET post_content=%s WHERE ID=%d", $new_post_content, $old_row['ID'])); |
|
21 | + $wpdb->query($wpdb->prepare("UPDATE ".$this->_old_table." SET post_content=%s WHERE ID=%d", $new_post_content, $old_row['ID'])); |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | public function _count_records_to_migrate() |
49 | 49 | { |
50 | 50 | global $wpdb; |
51 | - $count = $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table . $this->_sql_to_only_select_non_drafts()); |
|
51 | + $count = $wpdb->get_var("SELECT COUNT(id) FROM ".$this->_old_table.$this->_sql_to_only_select_non_drafts()); |
|
52 | 52 | return $count; |
53 | 53 | } |
54 | 54 |
@@ -2,58 +2,57 @@ |
||
2 | 2 | |
3 | 3 | /** |
4 | 4 | * Goes through all the posts and pages, and converts old shortcodes to new ones |
5 | - |
|
6 | -*/ |
|
5 | + */ |
|
7 | 6 | |
8 | 7 | class EE_DMS_4_1_0_shortcodes extends EE_Data_Migration_Script_Stage |
9 | 8 | { |
10 | - public function __construct() |
|
11 | - { |
|
12 | - global $wpdb; |
|
13 | - $this->_pretty_name = esc_html__("Shortcodes", "event_espresso"); |
|
14 | - $this->_old_table = $wpdb->posts; |
|
15 | - parent::__construct(); |
|
16 | - } |
|
17 | - protected function _migrate_old_row($old_row) |
|
18 | - { |
|
19 | - $new_post_content = $this->_change_event_list_shortcode($old_row['post_content']); |
|
20 | - global $wpdb; |
|
21 | - $wpdb->query($wpdb->prepare("UPDATE " . $this->_old_table . " SET post_content=%s WHERE ID=%d", $new_post_content, $old_row['ID'])); |
|
22 | - } |
|
9 | + public function __construct() |
|
10 | + { |
|
11 | + global $wpdb; |
|
12 | + $this->_pretty_name = esc_html__("Shortcodes", "event_espresso"); |
|
13 | + $this->_old_table = $wpdb->posts; |
|
14 | + parent::__construct(); |
|
15 | + } |
|
16 | + protected function _migrate_old_row($old_row) |
|
17 | + { |
|
18 | + $new_post_content = $this->_change_event_list_shortcode($old_row['post_content']); |
|
19 | + global $wpdb; |
|
20 | + $wpdb->query($wpdb->prepare("UPDATE " . $this->_old_table . " SET post_content=%s WHERE ID=%d", $new_post_content, $old_row['ID'])); |
|
21 | + } |
|
23 | 22 | |
24 | - /** |
|
25 | - * replaces [EVENT_LIST... with [ESPRESSO_EVENTS...] |
|
26 | - * @param string $old_content |
|
27 | - */ |
|
28 | - private function _change_event_list_shortcode($old_content) |
|
29 | - { |
|
30 | - return str_replace("[EVENT_LIST", "[ESPRESSO_EVENTS", $old_content); |
|
31 | - } |
|
23 | + /** |
|
24 | + * replaces [EVENT_LIST... with [ESPRESSO_EVENTS...] |
|
25 | + * @param string $old_content |
|
26 | + */ |
|
27 | + private function _change_event_list_shortcode($old_content) |
|
28 | + { |
|
29 | + return str_replace("[EVENT_LIST", "[ESPRESSO_EVENTS", $old_content); |
|
30 | + } |
|
32 | 31 | |
33 | - public function _migration_step($num_items = 50) |
|
34 | - { |
|
35 | - global $wpdb; |
|
36 | - $start_at_record = $this->count_records_migrated(); |
|
37 | - $rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $this->_old_table {$this->_sql_to_only_select_non_drafts()} LIMIT %d,%d", $start_at_record, $num_items), ARRAY_A); |
|
38 | - $items_actually_migrated = 0; |
|
39 | - foreach ($rows as $old_row) { |
|
40 | - $this->_migrate_old_row($old_row); |
|
41 | - $items_actually_migrated++; |
|
42 | - } |
|
43 | - if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) { |
|
44 | - $this->set_completed(); |
|
45 | - } |
|
46 | - return $items_actually_migrated; |
|
47 | - } |
|
48 | - public function _count_records_to_migrate() |
|
49 | - { |
|
50 | - global $wpdb; |
|
51 | - $count = $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table . $this->_sql_to_only_select_non_drafts()); |
|
52 | - return $count; |
|
53 | - } |
|
32 | + public function _migration_step($num_items = 50) |
|
33 | + { |
|
34 | + global $wpdb; |
|
35 | + $start_at_record = $this->count_records_migrated(); |
|
36 | + $rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $this->_old_table {$this->_sql_to_only_select_non_drafts()} LIMIT %d,%d", $start_at_record, $num_items), ARRAY_A); |
|
37 | + $items_actually_migrated = 0; |
|
38 | + foreach ($rows as $old_row) { |
|
39 | + $this->_migrate_old_row($old_row); |
|
40 | + $items_actually_migrated++; |
|
41 | + } |
|
42 | + if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) { |
|
43 | + $this->set_completed(); |
|
44 | + } |
|
45 | + return $items_actually_migrated; |
|
46 | + } |
|
47 | + public function _count_records_to_migrate() |
|
48 | + { |
|
49 | + global $wpdb; |
|
50 | + $count = $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table . $this->_sql_to_only_select_non_drafts()); |
|
51 | + return $count; |
|
52 | + } |
|
54 | 53 | |
55 | - private function _sql_to_only_select_non_drafts() |
|
56 | - { |
|
57 | - return " WHERE post_type NOT IN ('revision','auto-draft','attachment','nav_menu_item') "; |
|
58 | - } |
|
54 | + private function _sql_to_only_select_non_drafts() |
|
55 | + { |
|
56 | + return " WHERE post_type NOT IN ('revision','auto-draft','attachment','nav_menu_item') "; |
|
57 | + } |
|
59 | 58 | } |
@@ -26,160 +26,160 @@ |
||
26 | 26 | |
27 | 27 | class EE_DMS_4_1_0_checkins extends EE_Data_Migration_Script_Stage_Table |
28 | 28 | { |
29 | - private $_new_table; |
|
30 | - public function __construct() |
|
31 | - { |
|
32 | - global $wpdb; |
|
33 | - $this->_pretty_name = esc_html__('Checkins', 'event_espresso'); |
|
34 | - $this->_old_table = $wpdb->prefix . "events_attendee"; |
|
35 | - $this->select_expression = 'att.*, e.event_status'; |
|
36 | - $this->_extra_where_sql = 'AS att |
|
29 | + private $_new_table; |
|
30 | + public function __construct() |
|
31 | + { |
|
32 | + global $wpdb; |
|
33 | + $this->_pretty_name = esc_html__('Checkins', 'event_espresso'); |
|
34 | + $this->_old_table = $wpdb->prefix . "events_attendee"; |
|
35 | + $this->select_expression = 'att.*, e.event_status'; |
|
36 | + $this->_extra_where_sql = 'AS att |
|
37 | 37 | INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id=e.id |
38 | 38 | WHERE e.event_status!="D"'; |
39 | - $this->_new_table = $wpdb->prefix . "esp_checkin"; |
|
40 | - parent::__construct(); |
|
41 | - } |
|
42 | - protected function _migrate_old_row($old_row) |
|
43 | - { |
|
44 | - global $wpdb; |
|
45 | - $new_reg_table = $wpdb->prefix . "esp_registration"; |
|
39 | + $this->_new_table = $wpdb->prefix . "esp_checkin"; |
|
40 | + parent::__construct(); |
|
41 | + } |
|
42 | + protected function _migrate_old_row($old_row) |
|
43 | + { |
|
44 | + global $wpdb; |
|
45 | + $new_reg_table = $wpdb->prefix . "esp_registration"; |
|
46 | 46 | |
47 | - $num_to_checkin_at_this_time = max(array(intval($old_row['checked_in_quantity']),intval($old_row['checked_in']))) ; |
|
47 | + $num_to_checkin_at_this_time = max(array(intval($old_row['checked_in_quantity']),intval($old_row['checked_in']))) ; |
|
48 | 48 | |
49 | - $new_registrations_for_attendee = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $new_reg_table); |
|
50 | - if (! $new_registrations_for_attendee) { |
|
51 | - $new_registrations_for_attendee = array(); |
|
52 | - } |
|
53 | - $new_datetime = $this->_try_to_find_datetime($old_row); |
|
49 | + $new_registrations_for_attendee = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $new_reg_table); |
|
50 | + if (! $new_registrations_for_attendee) { |
|
51 | + $new_registrations_for_attendee = array(); |
|
52 | + } |
|
53 | + $new_datetime = $this->_try_to_find_datetime($old_row); |
|
54 | 54 | |
55 | - // make sure registrations array is numerically indexed starting at 0 (it probably already is) |
|
56 | - $new_registrations_for_attendee = array_values($new_registrations_for_attendee); |
|
57 | - $new_checkin_ids = array(); |
|
58 | - for ($i = 0; $i < abs($num_to_checkin_at_this_time); $i++) { |
|
59 | - $new_reg_id = $new_registrations_for_attendee[ $i ]; |
|
60 | - if (! $new_reg_id) { |
|
61 | - $this->add_error(sprintf( |
|
62 | - esc_html__( |
|
63 | - /* translators: %1$s database row represented in JSON, %2$s number of registrations to check-in |
|
55 | + // make sure registrations array is numerically indexed starting at 0 (it probably already is) |
|
56 | + $new_registrations_for_attendee = array_values($new_registrations_for_attendee); |
|
57 | + $new_checkin_ids = array(); |
|
58 | + for ($i = 0; $i < abs($num_to_checkin_at_this_time); $i++) { |
|
59 | + $new_reg_id = $new_registrations_for_attendee[ $i ]; |
|
60 | + if (! $new_reg_id) { |
|
61 | + $this->add_error(sprintf( |
|
62 | + esc_html__( |
|
63 | + /* translators: %1$s database row represented in JSON, %2$s number of registrations to check-in |
|
64 | 64 | * %3$s number of registrations for the attendee, %4$s new registration rows represented in JSON |
65 | 65 | */ |
66 | - // @codingStandardsIgnoreStart |
|
67 | - 'It appears we wanted to check-in more registrations than actually exist. The old attendee record (%1$s) indicated we should check-in %2$d registrations, but there are only %3$d registrations for that attendee (%4$s)', |
|
68 | - // @codingStandardsIgnoreEnd |
|
69 | - 'event_espresso' |
|
70 | - ), |
|
71 | - $this->_json_encode($old_row), |
|
72 | - abs($num_to_checkin_at_this_time), |
|
73 | - count($new_registrations_for_attendee), |
|
74 | - $this->_json_encode($new_registrations_for_attendee) |
|
75 | - )); |
|
76 | - break; |
|
77 | - } |
|
78 | - $existing_checkin_record = $wpdb->get_var( |
|
79 | - $wpdb->prepare( |
|
80 | - "SELECT CHK_ID FROM $this->_new_table WHERE REG_ID = %d ORDER BY CHK_ID DESC LIMIT 1", |
|
81 | - $new_reg_id |
|
82 | - ) |
|
83 | - ); |
|
84 | - if (! $existing_checkin_record) { |
|
85 | - $new_id = $this->_insert_checkin_record($new_reg_id, $new_datetime); |
|
86 | - if ($new_id) { |
|
87 | - $new_checkin_ids[] = $new_id; |
|
88 | - } |
|
89 | - } |
|
90 | - } |
|
91 | - if ($new_checkin_ids) { |
|
92 | - $this->get_migration_script()->set_mapping( |
|
93 | - $this->_old_table, |
|
94 | - $old_row['id'], |
|
95 | - $this->_new_table, |
|
96 | - $new_checkin_ids |
|
97 | - ); |
|
98 | - } |
|
99 | - } |
|
66 | + // @codingStandardsIgnoreStart |
|
67 | + 'It appears we wanted to check-in more registrations than actually exist. The old attendee record (%1$s) indicated we should check-in %2$d registrations, but there are only %3$d registrations for that attendee (%4$s)', |
|
68 | + // @codingStandardsIgnoreEnd |
|
69 | + 'event_espresso' |
|
70 | + ), |
|
71 | + $this->_json_encode($old_row), |
|
72 | + abs($num_to_checkin_at_this_time), |
|
73 | + count($new_registrations_for_attendee), |
|
74 | + $this->_json_encode($new_registrations_for_attendee) |
|
75 | + )); |
|
76 | + break; |
|
77 | + } |
|
78 | + $existing_checkin_record = $wpdb->get_var( |
|
79 | + $wpdb->prepare( |
|
80 | + "SELECT CHK_ID FROM $this->_new_table WHERE REG_ID = %d ORDER BY CHK_ID DESC LIMIT 1", |
|
81 | + $new_reg_id |
|
82 | + ) |
|
83 | + ); |
|
84 | + if (! $existing_checkin_record) { |
|
85 | + $new_id = $this->_insert_checkin_record($new_reg_id, $new_datetime); |
|
86 | + if ($new_id) { |
|
87 | + $new_checkin_ids[] = $new_id; |
|
88 | + } |
|
89 | + } |
|
90 | + } |
|
91 | + if ($new_checkin_ids) { |
|
92 | + $this->get_migration_script()->set_mapping( |
|
93 | + $this->_old_table, |
|
94 | + $old_row['id'], |
|
95 | + $this->_new_table, |
|
96 | + $new_checkin_ids |
|
97 | + ); |
|
98 | + } |
|
99 | + } |
|
100 | 100 | |
101 | 101 | |
102 | - /** |
|
103 | - * Tries to find the new datetime the Check-in was for, based on the attendee row |
|
104 | - * (because we know the attendee was for an event as a specific time, and we know |
|
105 | - * the event's OLD ID...) |
|
106 | - * @global type $wpdb |
|
107 | - * @param array $old_attendee_row |
|
108 | - * @return array row of datetime from DB |
|
109 | - */ |
|
110 | - private function _try_to_find_datetime($old_attendee) |
|
111 | - { |
|
112 | - global $wpdb; |
|
102 | + /** |
|
103 | + * Tries to find the new datetime the Check-in was for, based on the attendee row |
|
104 | + * (because we know the attendee was for an event as a specific time, and we know |
|
105 | + * the event's OLD ID...) |
|
106 | + * @global type $wpdb |
|
107 | + * @param array $old_attendee_row |
|
108 | + * @return array row of datetime from DB |
|
109 | + */ |
|
110 | + private function _try_to_find_datetime($old_attendee) |
|
111 | + { |
|
112 | + global $wpdb; |
|
113 | 113 | |
114 | - $new_event_id = $this->get_migration_script()->get_mapping_new_pk($wpdb->prefix . "events_detail", $old_attendee['event_id'], $wpdb->posts); |
|
115 | - if (! $new_event_id) { |
|
116 | - $this->add_error( |
|
117 | - sprintf( |
|
118 | - esc_html__( |
|
119 | - /* translators: 1: original event ID, 2: original attendee database row */ |
|
120 | - // @codingStandardsIgnoreStart |
|
121 | - 'Could not find new event ID with old event ID %1$d, on attendee row %2$s; and because of that couldn\'t find the correct datetime for Check-in', |
|
122 | - // @codingStandardsIgnoreEnd |
|
123 | - 'event_espresso' |
|
124 | - ), |
|
125 | - $old_attendee['event_id'], |
|
126 | - $this->_json_encode($old_attendee) |
|
127 | - ) |
|
128 | - ); |
|
129 | - return 0; |
|
130 | - } |
|
131 | - $old_att_start_date = $old_attendee['start_date']; |
|
132 | - $old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']); |
|
133 | - $old_att_datetime = $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, "$old_att_start_date $old_att_start_time:00"); |
|
114 | + $new_event_id = $this->get_migration_script()->get_mapping_new_pk($wpdb->prefix . "events_detail", $old_attendee['event_id'], $wpdb->posts); |
|
115 | + if (! $new_event_id) { |
|
116 | + $this->add_error( |
|
117 | + sprintf( |
|
118 | + esc_html__( |
|
119 | + /* translators: 1: original event ID, 2: original attendee database row */ |
|
120 | + // @codingStandardsIgnoreStart |
|
121 | + 'Could not find new event ID with old event ID %1$d, on attendee row %2$s; and because of that couldn\'t find the correct datetime for Check-in', |
|
122 | + // @codingStandardsIgnoreEnd |
|
123 | + 'event_espresso' |
|
124 | + ), |
|
125 | + $old_attendee['event_id'], |
|
126 | + $this->_json_encode($old_attendee) |
|
127 | + ) |
|
128 | + ); |
|
129 | + return 0; |
|
130 | + } |
|
131 | + $old_att_start_date = $old_attendee['start_date']; |
|
132 | + $old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']); |
|
133 | + $old_att_datetime = $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, "$old_att_start_date $old_att_start_time:00"); |
|
134 | 134 | |
135 | - $datetime_table = $wpdb->prefix . "esp_datetime"; |
|
136 | - // add all conditions to an array from which we can SHIFT conditions off in order to widen our search |
|
137 | - // the most important condition should be last, as it will be array_shift'ed off last |
|
138 | - $conditions = array( |
|
139 | - $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match? |
|
140 | - $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match? |
|
141 | - ); |
|
142 | - // start running queries, widening search each time by removing a condition |
|
143 | - $datetime_found = null; |
|
144 | - do { |
|
145 | - $full_query = "SELECT * FROM $datetime_table WHERE " . implode(" AND ", $conditions) . " LIMIT 1"; |
|
146 | - $datetime_found = $wpdb->get_row($full_query, ARRAY_A); |
|
147 | - array_shift($conditions); |
|
148 | - } while (! $datetime_found && $conditions); |
|
149 | - return $datetime_found; |
|
150 | - } |
|
135 | + $datetime_table = $wpdb->prefix . "esp_datetime"; |
|
136 | + // add all conditions to an array from which we can SHIFT conditions off in order to widen our search |
|
137 | + // the most important condition should be last, as it will be array_shift'ed off last |
|
138 | + $conditions = array( |
|
139 | + $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match? |
|
140 | + $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match? |
|
141 | + ); |
|
142 | + // start running queries, widening search each time by removing a condition |
|
143 | + $datetime_found = null; |
|
144 | + do { |
|
145 | + $full_query = "SELECT * FROM $datetime_table WHERE " . implode(" AND ", $conditions) . " LIMIT 1"; |
|
146 | + $datetime_found = $wpdb->get_row($full_query, ARRAY_A); |
|
147 | + array_shift($conditions); |
|
148 | + } while (! $datetime_found && $conditions); |
|
149 | + return $datetime_found; |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * Adds a new Check-in/checkout record according for $new_reg_id,$new_datetime_id,$checking_in, and $timestmap |
|
154 | - * @param int $new_reg_id |
|
155 | - * @param int $new_datetime_id |
|
156 | - * @param string $timestamp mysql datetime |
|
157 | - * @return int new Check-in id |
|
158 | - */ |
|
159 | - private function _insert_checkin_record($new_reg_id, $new_datetime) |
|
160 | - { |
|
161 | - global $wpdb; |
|
152 | + /** |
|
153 | + * Adds a new Check-in/checkout record according for $new_reg_id,$new_datetime_id,$checking_in, and $timestmap |
|
154 | + * @param int $new_reg_id |
|
155 | + * @param int $new_datetime_id |
|
156 | + * @param string $timestamp mysql datetime |
|
157 | + * @return int new Check-in id |
|
158 | + */ |
|
159 | + private function _insert_checkin_record($new_reg_id, $new_datetime) |
|
160 | + { |
|
161 | + global $wpdb; |
|
162 | 162 | |
163 | 163 | |
164 | - // ok we can actually do what we set out to do: add a checkin/checkout record |
|
165 | - $cols_n_values = array( |
|
166 | - 'REG_ID' => $new_reg_id, |
|
167 | - 'DTT_ID' => $new_datetime['DTT_ID'], |
|
168 | - 'CHK_in' => true, |
|
169 | - 'CHK_timestamp' => $new_datetime['DTT_EVT_start'] |
|
170 | - ); |
|
171 | - $datatypes = array( |
|
172 | - '%d',// REG_ID |
|
173 | - '%d',// DTT_ID |
|
174 | - '%d',// CHK_in |
|
175 | - '%s',// CHK_timestamp |
|
176 | - ); |
|
177 | - $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes); |
|
178 | - if (! $success) { |
|
179 | - $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_checkin, $this->_new_table, $cols_n_values, $datatypes)); |
|
180 | - return 0; |
|
181 | - } |
|
182 | - $new_id = $wpdb->insert_id; |
|
183 | - return $new_id; |
|
184 | - } |
|
164 | + // ok we can actually do what we set out to do: add a checkin/checkout record |
|
165 | + $cols_n_values = array( |
|
166 | + 'REG_ID' => $new_reg_id, |
|
167 | + 'DTT_ID' => $new_datetime['DTT_ID'], |
|
168 | + 'CHK_in' => true, |
|
169 | + 'CHK_timestamp' => $new_datetime['DTT_EVT_start'] |
|
170 | + ); |
|
171 | + $datatypes = array( |
|
172 | + '%d',// REG_ID |
|
173 | + '%d',// DTT_ID |
|
174 | + '%d',// CHK_in |
|
175 | + '%s',// CHK_timestamp |
|
176 | + ); |
|
177 | + $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes); |
|
178 | + if (! $success) { |
|
179 | + $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_checkin, $this->_new_table, $cols_n_values, $datatypes)); |
|
180 | + return 0; |
|
181 | + } |
|
182 | + $new_id = $wpdb->insert_id; |
|
183 | + return $new_id; |
|
184 | + } |
|
185 | 185 | } |
@@ -31,23 +31,23 @@ discard block |
||
31 | 31 | { |
32 | 32 | global $wpdb; |
33 | 33 | $this->_pretty_name = esc_html__('Checkins', 'event_espresso'); |
34 | - $this->_old_table = $wpdb->prefix . "events_attendee"; |
|
34 | + $this->_old_table = $wpdb->prefix."events_attendee"; |
|
35 | 35 | $this->select_expression = 'att.*, e.event_status'; |
36 | 36 | $this->_extra_where_sql = 'AS att |
37 | - INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id=e.id |
|
37 | + INNER JOIN ' . $wpdb->prefix.'events_detail AS e ON att.event_id=e.id |
|
38 | 38 | WHERE e.event_status!="D"'; |
39 | - $this->_new_table = $wpdb->prefix . "esp_checkin"; |
|
39 | + $this->_new_table = $wpdb->prefix."esp_checkin"; |
|
40 | 40 | parent::__construct(); |
41 | 41 | } |
42 | 42 | protected function _migrate_old_row($old_row) |
43 | 43 | { |
44 | 44 | global $wpdb; |
45 | - $new_reg_table = $wpdb->prefix . "esp_registration"; |
|
45 | + $new_reg_table = $wpdb->prefix."esp_registration"; |
|
46 | 46 | |
47 | - $num_to_checkin_at_this_time = max(array(intval($old_row['checked_in_quantity']),intval($old_row['checked_in']))) ; |
|
47 | + $num_to_checkin_at_this_time = max(array(intval($old_row['checked_in_quantity']), intval($old_row['checked_in']))); |
|
48 | 48 | |
49 | 49 | $new_registrations_for_attendee = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $new_reg_table); |
50 | - if (! $new_registrations_for_attendee) { |
|
50 | + if ( ! $new_registrations_for_attendee) { |
|
51 | 51 | $new_registrations_for_attendee = array(); |
52 | 52 | } |
53 | 53 | $new_datetime = $this->_try_to_find_datetime($old_row); |
@@ -56,8 +56,8 @@ discard block |
||
56 | 56 | $new_registrations_for_attendee = array_values($new_registrations_for_attendee); |
57 | 57 | $new_checkin_ids = array(); |
58 | 58 | for ($i = 0; $i < abs($num_to_checkin_at_this_time); $i++) { |
59 | - $new_reg_id = $new_registrations_for_attendee[ $i ]; |
|
60 | - if (! $new_reg_id) { |
|
59 | + $new_reg_id = $new_registrations_for_attendee[$i]; |
|
60 | + if ( ! $new_reg_id) { |
|
61 | 61 | $this->add_error(sprintf( |
62 | 62 | esc_html__( |
63 | 63 | /* translators: %1$s database row represented in JSON, %2$s number of registrations to check-in |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | $new_reg_id |
82 | 82 | ) |
83 | 83 | ); |
84 | - if (! $existing_checkin_record) { |
|
84 | + if ( ! $existing_checkin_record) { |
|
85 | 85 | $new_id = $this->_insert_checkin_record($new_reg_id, $new_datetime); |
86 | 86 | if ($new_id) { |
87 | 87 | $new_checkin_ids[] = $new_id; |
@@ -111,8 +111,8 @@ discard block |
||
111 | 111 | { |
112 | 112 | global $wpdb; |
113 | 113 | |
114 | - $new_event_id = $this->get_migration_script()->get_mapping_new_pk($wpdb->prefix . "events_detail", $old_attendee['event_id'], $wpdb->posts); |
|
115 | - if (! $new_event_id) { |
|
114 | + $new_event_id = $this->get_migration_script()->get_mapping_new_pk($wpdb->prefix."events_detail", $old_attendee['event_id'], $wpdb->posts); |
|
115 | + if ( ! $new_event_id) { |
|
116 | 116 | $this->add_error( |
117 | 117 | sprintf( |
118 | 118 | esc_html__( |
@@ -132,20 +132,20 @@ discard block |
||
132 | 132 | $old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']); |
133 | 133 | $old_att_datetime = $this->get_migration_script()->convert_date_string_to_utc($this, $old_attendee, "$old_att_start_date $old_att_start_time:00"); |
134 | 134 | |
135 | - $datetime_table = $wpdb->prefix . "esp_datetime"; |
|
135 | + $datetime_table = $wpdb->prefix."esp_datetime"; |
|
136 | 136 | // add all conditions to an array from which we can SHIFT conditions off in order to widen our search |
137 | 137 | // the most important condition should be last, as it will be array_shift'ed off last |
138 | 138 | $conditions = array( |
139 | - $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match? |
|
140 | - $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match? |
|
139 | + $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime), // times match? |
|
140 | + $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id), // events match? |
|
141 | 141 | ); |
142 | 142 | // start running queries, widening search each time by removing a condition |
143 | 143 | $datetime_found = null; |
144 | 144 | do { |
145 | - $full_query = "SELECT * FROM $datetime_table WHERE " . implode(" AND ", $conditions) . " LIMIT 1"; |
|
145 | + $full_query = "SELECT * FROM $datetime_table WHERE ".implode(" AND ", $conditions)." LIMIT 1"; |
|
146 | 146 | $datetime_found = $wpdb->get_row($full_query, ARRAY_A); |
147 | 147 | array_shift($conditions); |
148 | - } while (! $datetime_found && $conditions); |
|
148 | + }while ( ! $datetime_found && $conditions); |
|
149 | 149 | return $datetime_found; |
150 | 150 | } |
151 | 151 | |
@@ -169,13 +169,13 @@ discard block |
||
169 | 169 | 'CHK_timestamp' => $new_datetime['DTT_EVT_start'] |
170 | 170 | ); |
171 | 171 | $datatypes = array( |
172 | - '%d',// REG_ID |
|
173 | - '%d',// DTT_ID |
|
174 | - '%d',// CHK_in |
|
175 | - '%s',// CHK_timestamp |
|
172 | + '%d', // REG_ID |
|
173 | + '%d', // DTT_ID |
|
174 | + '%d', // CHK_in |
|
175 | + '%s', // CHK_timestamp |
|
176 | 176 | ); |
177 | 177 | $success = $wpdb->insert($this->_new_table, $cols_n_values, $datatypes); |
178 | - if (! $success) { |
|
178 | + if ( ! $success) { |
|
179 | 179 | $this->add_error($this->get_migration_script()->_create_error_message_for_db_insertion($this->_old_table, $old_checkin, $this->_new_table, $cols_n_values, $datatypes)); |
180 | 180 | return 0; |
181 | 181 | } |
@@ -9,12 +9,12 @@ discard block |
||
9 | 9 | // unfortunately, this needs to be done upon INCLUSION of this file, |
10 | 10 | // instead of construction, because it only gets constructed on first page load |
11 | 11 | // (all other times it gets resurrected from a wordpress option) |
12 | -$stages = glob(EE_CORE . 'data_migration_scripts/4_9_0_stages/*'); |
|
12 | +$stages = glob(EE_CORE.'data_migration_scripts/4_9_0_stages/*'); |
|
13 | 13 | $class_to_filepath = array(); |
14 | 14 | foreach ($stages as $filepath) { |
15 | 15 | $matches = array(); |
16 | 16 | preg_match('~4_9_0_stages/(.*).dmsstage.php~', $filepath, $matches); |
17 | - $class_to_filepath[ $matches[1] ] = $filepath; |
|
17 | + $class_to_filepath[$matches[1]] = $filepath; |
|
18 | 18 | } |
19 | 19 | // give addons a chance to autoload their stages too |
20 | 20 | $class_to_filepath = apply_filters('FHEE__EE_DMS_4_9_0__autoloaded_stages', $class_to_filepath); |
@@ -65,10 +65,10 @@ discard block |
||
65 | 65 | if (version_compare($version_string, '4.9.0.decaf', '<') && version_compare($version_string, '4.8.0.decaf', '>=')) { |
66 | 66 | // echo "$version_string can be migrated from"; |
67 | 67 | return true; |
68 | - } elseif (! $version_string) { |
|
68 | + } elseif ( ! $version_string) { |
|
69 | 69 | // echo "no version string provided: $version_string"; |
70 | 70 | // no version string provided... this must be pre 4.3 |
71 | - return false;// changed mind. dont want people thinking they should migrate yet because they cant |
|
71 | + return false; // changed mind. dont want people thinking they should migrate yet because they cant |
|
72 | 72 | } else { |
73 | 73 | // echo "$version_string doesnt apply"; |
74 | 74 | return false; |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | */ |
83 | 83 | public function schema_changes_before_migration() |
84 | 84 | { |
85 | - require_once(EE_HELPERS . 'EEH_Activation.helper.php'); |
|
85 | + require_once(EE_HELPERS.'EEH_Activation.helper.php'); |
|
86 | 86 | $now_in_mysql = current_time('mysql', true); |
87 | 87 | $table_name = 'esp_answer'; |
88 | 88 | $sql = " ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
@@ -12,9 +12,9 @@ discard block |
||
12 | 12 | $stages = glob(EE_CORE . 'data_migration_scripts/4_9_0_stages/*'); |
13 | 13 | $class_to_filepath = array(); |
14 | 14 | foreach ($stages as $filepath) { |
15 | - $matches = array(); |
|
16 | - preg_match('~4_9_0_stages/(.*).dmsstage.php~', $filepath, $matches); |
|
17 | - $class_to_filepath[ $matches[1] ] = $filepath; |
|
15 | + $matches = array(); |
|
16 | + preg_match('~4_9_0_stages/(.*).dmsstage.php~', $filepath, $matches); |
|
17 | + $class_to_filepath[ $matches[1] ] = $filepath; |
|
18 | 18 | } |
19 | 19 | // give addons a chance to autoload their stages too |
20 | 20 | $class_to_filepath = apply_filters('FHEE__EE_DMS_4_9_0__autoloaded_stages', $class_to_filepath); |
@@ -33,68 +33,68 @@ discard block |
||
33 | 33 | */ |
34 | 34 | class EE_DMS_Core_4_9_0 extends EE_Data_Migration_Script_Base |
35 | 35 | { |
36 | - /** |
|
37 | - * return EE_DMS_Core_4_9_0 |
|
38 | - * |
|
39 | - * @param TableManager $table_manager |
|
40 | - * @param TableAnalysis $table_analysis |
|
41 | - */ |
|
42 | - public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null) |
|
43 | - { |
|
44 | - $this->_pretty_name = esc_html__("Data Update to Event Espresso 4.9.0", "event_espresso"); |
|
45 | - $this->_priority = 10; |
|
46 | - $this->_migration_stages = array( |
|
47 | - new EE_DMS_4_9_0_Email_System_Question(), |
|
48 | - new EE_DMS_4_9_0_Answers_With_No_Registration(), |
|
49 | - ); |
|
50 | - parent::__construct($table_manager, $table_analysis); |
|
51 | - } |
|
36 | + /** |
|
37 | + * return EE_DMS_Core_4_9_0 |
|
38 | + * |
|
39 | + * @param TableManager $table_manager |
|
40 | + * @param TableAnalysis $table_analysis |
|
41 | + */ |
|
42 | + public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null) |
|
43 | + { |
|
44 | + $this->_pretty_name = esc_html__("Data Update to Event Espresso 4.9.0", "event_espresso"); |
|
45 | + $this->_priority = 10; |
|
46 | + $this->_migration_stages = array( |
|
47 | + new EE_DMS_4_9_0_Email_System_Question(), |
|
48 | + new EE_DMS_4_9_0_Answers_With_No_Registration(), |
|
49 | + ); |
|
50 | + parent::__construct($table_manager, $table_analysis); |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * Whether to migrate or not. |
|
57 | - * |
|
58 | - * @param array $version_array |
|
59 | - * @return bool |
|
60 | - */ |
|
61 | - public function can_migrate_from_version($version_array) |
|
62 | - { |
|
63 | - $version_string = $version_array['Core']; |
|
64 | - if (version_compare($version_string, '4.9.0.decaf', '<') && version_compare($version_string, '4.8.0.decaf', '>=')) { |
|
65 | - // echo "$version_string can be migrated from"; |
|
66 | - return true; |
|
67 | - } elseif (! $version_string) { |
|
68 | - // echo "no version string provided: $version_string"; |
|
69 | - // no version string provided... this must be pre 4.3 |
|
70 | - return false;// changed mind. dont want people thinking they should migrate yet because they cant |
|
71 | - } else { |
|
72 | - // echo "$version_string doesnt apply"; |
|
73 | - return false; |
|
74 | - } |
|
75 | - } |
|
55 | + /** |
|
56 | + * Whether to migrate or not. |
|
57 | + * |
|
58 | + * @param array $version_array |
|
59 | + * @return bool |
|
60 | + */ |
|
61 | + public function can_migrate_from_version($version_array) |
|
62 | + { |
|
63 | + $version_string = $version_array['Core']; |
|
64 | + if (version_compare($version_string, '4.9.0.decaf', '<') && version_compare($version_string, '4.8.0.decaf', '>=')) { |
|
65 | + // echo "$version_string can be migrated from"; |
|
66 | + return true; |
|
67 | + } elseif (! $version_string) { |
|
68 | + // echo "no version string provided: $version_string"; |
|
69 | + // no version string provided... this must be pre 4.3 |
|
70 | + return false;// changed mind. dont want people thinking they should migrate yet because they cant |
|
71 | + } else { |
|
72 | + // echo "$version_string doesnt apply"; |
|
73 | + return false; |
|
74 | + } |
|
75 | + } |
|
76 | 76 | |
77 | 77 | |
78 | 78 | |
79 | - /** |
|
80 | - * @return bool |
|
81 | - */ |
|
82 | - public function schema_changes_before_migration() |
|
83 | - { |
|
84 | - require_once(EE_HELPERS . 'EEH_Activation.helper.php'); |
|
85 | - $now_in_mysql = current_time('mysql', true); |
|
86 | - $table_name = 'esp_answer'; |
|
87 | - $sql = " ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
79 | + /** |
|
80 | + * @return bool |
|
81 | + */ |
|
82 | + public function schema_changes_before_migration() |
|
83 | + { |
|
84 | + require_once(EE_HELPERS . 'EEH_Activation.helper.php'); |
|
85 | + $now_in_mysql = current_time('mysql', true); |
|
86 | + $table_name = 'esp_answer'; |
|
87 | + $sql = " ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
88 | 88 | REG_ID int(10) unsigned NOT NULL, |
89 | 89 | QST_ID int(10) unsigned NOT NULL, |
90 | 90 | ANS_value text NOT NULL, |
91 | 91 | PRIMARY KEY (ANS_ID), |
92 | 92 | KEY REG_ID (REG_ID), |
93 | 93 | KEY QST_ID (QST_ID)"; |
94 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
95 | - $table_name = 'esp_attendee_meta'; |
|
96 | - $this->_get_table_manager()->dropIndexIfSizeNot($table_name, 'ATT_email'); |
|
97 | - $sql = "ATTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
94 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
95 | + $table_name = 'esp_attendee_meta'; |
|
96 | + $this->_get_table_manager()->dropIndexIfSizeNot($table_name, 'ATT_email'); |
|
97 | + $sql = "ATTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
98 | 98 | ATT_ID bigint(20) unsigned NOT NULL, |
99 | 99 | ATT_fname varchar(45) NOT NULL, |
100 | 100 | ATT_lname varchar(45) NOT NULL, |
@@ -111,9 +111,9 @@ discard block |
||
111 | 111 | KEY ATT_email (ATT_email(191)), |
112 | 112 | KEY ATT_lname (ATT_lname), |
113 | 113 | KEY ATT_fname (ATT_fname)"; |
114 | - $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB '); |
|
115 | - $table_name = 'esp_checkin'; |
|
116 | - $sql = "CHK_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
114 | + $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB '); |
|
115 | + $table_name = 'esp_checkin'; |
|
116 | + $sql = "CHK_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
117 | 117 | REG_ID int(10) unsigned NOT NULL, |
118 | 118 | DTT_ID int(10) unsigned NOT NULL, |
119 | 119 | CHK_in tinyint(1) unsigned NOT NULL DEFAULT 1, |
@@ -121,9 +121,9 @@ discard block |
||
121 | 121 | PRIMARY KEY (CHK_ID), |
122 | 122 | KEY REG_ID (REG_ID), |
123 | 123 | KEY DTT_ID (DTT_ID)"; |
124 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
125 | - $table_name = 'esp_country'; |
|
126 | - $sql = "CNT_ISO varchar(2) NOT NULL, |
|
124 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
125 | + $table_name = 'esp_country'; |
|
126 | + $sql = "CNT_ISO varchar(2) NOT NULL, |
|
127 | 127 | CNT_ISO3 varchar(3) NOT NULL, |
128 | 128 | RGN_ID tinyint(3) unsigned DEFAULT NULL, |
129 | 129 | CNT_name varchar(45) NOT NULL, |
@@ -139,18 +139,18 @@ discard block |
||
139 | 139 | CNT_is_EU tinyint(1) DEFAULT '0', |
140 | 140 | CNT_active tinyint(1) DEFAULT '0', |
141 | 141 | PRIMARY KEY (CNT_ISO)"; |
142 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
143 | - $table_name = 'esp_currency'; |
|
144 | - $sql = "CUR_code varchar(6) NOT NULL, |
|
142 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
143 | + $table_name = 'esp_currency'; |
|
144 | + $sql = "CUR_code varchar(6) NOT NULL, |
|
145 | 145 | CUR_single varchar(45) DEFAULT 'dollar', |
146 | 146 | CUR_plural varchar(45) DEFAULT 'dollars', |
147 | 147 | CUR_sign varchar(45) DEFAULT '$', |
148 | 148 | CUR_dec_plc varchar(1) NOT NULL DEFAULT '2', |
149 | 149 | CUR_active tinyint(1) DEFAULT '0', |
150 | 150 | PRIMARY KEY (CUR_code)"; |
151 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
152 | - $table_name = 'esp_datetime'; |
|
153 | - $sql = "DTT_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
151 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
152 | + $table_name = 'esp_datetime'; |
|
153 | + $sql = "DTT_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
154 | 154 | EVT_ID bigint(20) unsigned NOT NULL, |
155 | 155 | DTT_name varchar(255) NOT NULL DEFAULT '', |
156 | 156 | DTT_description text NOT NULL, |
@@ -167,25 +167,25 @@ discard block |
||
167 | 167 | KEY DTT_EVT_start (DTT_EVT_start), |
168 | 168 | KEY EVT_ID (EVT_ID), |
169 | 169 | KEY DTT_is_primary (DTT_is_primary)"; |
170 | - $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
171 | - $table_name = "esp_datetime_ticket"; |
|
172 | - $sql = "DTK_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
170 | + $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
171 | + $table_name = "esp_datetime_ticket"; |
|
172 | + $sql = "DTK_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
173 | 173 | DTT_ID int(10) unsigned NOT NULL, |
174 | 174 | TKT_ID int(10) unsigned NOT NULL, |
175 | 175 | PRIMARY KEY (DTK_ID), |
176 | 176 | KEY DTT_ID (DTT_ID), |
177 | 177 | KEY TKT_ID (TKT_ID)"; |
178 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
179 | - $table_name = 'esp_event_message_template'; |
|
180 | - $sql = "EMT_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
|
178 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
179 | + $table_name = 'esp_event_message_template'; |
|
180 | + $sql = "EMT_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
|
181 | 181 | EVT_ID bigint(20) unsigned NOT NULL DEFAULT 0, |
182 | 182 | GRP_ID int(10) unsigned NOT NULL DEFAULT 0, |
183 | 183 | PRIMARY KEY (EMT_ID), |
184 | 184 | KEY EVT_ID (EVT_ID), |
185 | 185 | KEY GRP_ID (GRP_ID)"; |
186 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
187 | - $table_name = 'esp_event_meta'; |
|
188 | - $sql = "EVTM_ID int(10) NOT NULL AUTO_INCREMENT, |
|
186 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
187 | + $table_name = 'esp_event_meta'; |
|
188 | + $sql = "EVTM_ID int(10) NOT NULL AUTO_INCREMENT, |
|
189 | 189 | EVT_ID bigint(20) unsigned NOT NULL, |
190 | 190 | EVT_display_desc tinyint(1) unsigned NOT NULL DEFAULT 1, |
191 | 191 | EVT_display_ticket_selector tinyint(1) unsigned NOT NULL DEFAULT 1, |
@@ -200,34 +200,34 @@ discard block |
||
200 | 200 | EVT_donations tinyint(1) NULL, |
201 | 201 | PRIMARY KEY (EVTM_ID), |
202 | 202 | KEY EVT_ID (EVT_ID)"; |
203 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
204 | - $table_name = 'esp_event_question_group'; |
|
205 | - $sql = "EQG_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
203 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
204 | + $table_name = 'esp_event_question_group'; |
|
205 | + $sql = "EQG_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
206 | 206 | EVT_ID bigint(20) unsigned NOT NULL, |
207 | 207 | QSG_ID int(10) unsigned NOT NULL, |
208 | 208 | EQG_primary tinyint(1) unsigned NOT NULL DEFAULT 0, |
209 | 209 | PRIMARY KEY (EQG_ID), |
210 | 210 | KEY EVT_ID (EVT_ID), |
211 | 211 | KEY QSG_ID (QSG_ID)"; |
212 | - $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
213 | - $table_name = 'esp_event_venue'; |
|
214 | - $sql = "EVV_ID int(11) NOT NULL AUTO_INCREMENT, |
|
212 | + $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
213 | + $table_name = 'esp_event_venue'; |
|
214 | + $sql = "EVV_ID int(11) NOT NULL AUTO_INCREMENT, |
|
215 | 215 | EVT_ID bigint(20) unsigned NOT NULL, |
216 | 216 | VNU_ID bigint(20) unsigned NOT NULL, |
217 | 217 | EVV_primary tinyint(1) unsigned NOT NULL DEFAULT 0, |
218 | 218 | PRIMARY KEY (EVV_ID)"; |
219 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
220 | - $table_name = 'esp_extra_meta'; |
|
221 | - $sql = "EXM_ID int(11) NOT NULL AUTO_INCREMENT, |
|
219 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
220 | + $table_name = 'esp_extra_meta'; |
|
221 | + $sql = "EXM_ID int(11) NOT NULL AUTO_INCREMENT, |
|
222 | 222 | OBJ_ID int(11) DEFAULT NULL, |
223 | 223 | EXM_type varchar(45) DEFAULT NULL, |
224 | 224 | EXM_key varchar(45) DEFAULT NULL, |
225 | 225 | EXM_value text, |
226 | 226 | PRIMARY KEY (EXM_ID), |
227 | 227 | KEY EXM_type (EXM_type,OBJ_ID,EXM_key)"; |
228 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
229 | - $table_name = 'esp_extra_join'; |
|
230 | - $sql = "EXJ_ID int(11) NOT NULL AUTO_INCREMENT, |
|
228 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
229 | + $table_name = 'esp_extra_join'; |
|
230 | + $sql = "EXJ_ID int(11) NOT NULL AUTO_INCREMENT, |
|
231 | 231 | EXJ_first_model_id varchar(6) NOT NULL, |
232 | 232 | EXJ_first_model_name varchar(20) NOT NULL, |
233 | 233 | EXJ_second_model_id varchar(6) NOT NULL, |
@@ -235,9 +235,9 @@ discard block |
||
235 | 235 | PRIMARY KEY (EXJ_ID), |
236 | 236 | KEY first_model (EXJ_first_model_name,EXJ_first_model_id), |
237 | 237 | KEY second_model (EXJ_second_model_name,EXJ_second_model_id)"; |
238 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB '); |
|
239 | - $table_name = 'esp_line_item'; |
|
240 | - $sql = "LIN_ID int(11) NOT NULL AUTO_INCREMENT, |
|
238 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB '); |
|
239 | + $table_name = 'esp_line_item'; |
|
240 | + $sql = "LIN_ID int(11) NOT NULL AUTO_INCREMENT, |
|
241 | 241 | LIN_code varchar(245) NOT NULL DEFAULT '', |
242 | 242 | TXN_ID int(11) DEFAULT NULL, |
243 | 243 | LIN_name varchar(245) NOT NULL DEFAULT '', |
@@ -258,11 +258,11 @@ discard block |
||
258 | 258 | KEY txn_type_timestamp (TXN_ID,LIN_type,LIN_timestamp), |
259 | 259 | KEY txn_obj_id_obj_type (TXN_ID,OBJ_ID,OBJ_type), |
260 | 260 | KEY obj_id_obj_type (OBJ_ID,OBJ_type)"; |
261 | - $this->_get_table_manager()->dropIndex('esp_line_item', 'TXN_ID'); |
|
262 | - $this->_get_table_manager()->dropIndex('esp_line_item', 'LIN_code'); |
|
263 | - $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
264 | - $table_name = 'esp_log'; |
|
265 | - $sql = "LOG_ID int(11) NOT NULL AUTO_INCREMENT, |
|
261 | + $this->_get_table_manager()->dropIndex('esp_line_item', 'TXN_ID'); |
|
262 | + $this->_get_table_manager()->dropIndex('esp_line_item', 'LIN_code'); |
|
263 | + $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
264 | + $table_name = 'esp_log'; |
|
265 | + $sql = "LOG_ID int(11) NOT NULL AUTO_INCREMENT, |
|
266 | 266 | LOG_time datetime DEFAULT NULL, |
267 | 267 | OBJ_ID varchar(45) DEFAULT NULL, |
268 | 268 | OBJ_type varchar(45) DEFAULT NULL, |
@@ -273,12 +273,12 @@ discard block |
||
273 | 273 | KEY LOG_time (LOG_time), |
274 | 274 | KEY OBJ (OBJ_type,OBJ_ID), |
275 | 275 | KEY LOG_type (LOG_type)"; |
276 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
277 | - $table_name = 'esp_message'; |
|
278 | - $this->_get_table_manager()->dropIndexIfSizeNot($table_name, 'MSG_to'); |
|
279 | - $this->_get_table_manager()->dropIndexIfSizeNot($table_name, 'MSG_from'); |
|
280 | - $this->_get_table_manager()->dropIndexIfSizeNot($table_name, 'MSG_subject'); |
|
281 | - $sql = "MSG_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
|
276 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
277 | + $table_name = 'esp_message'; |
|
278 | + $this->_get_table_manager()->dropIndexIfSizeNot($table_name, 'MSG_to'); |
|
279 | + $this->_get_table_manager()->dropIndexIfSizeNot($table_name, 'MSG_from'); |
|
280 | + $this->_get_table_manager()->dropIndexIfSizeNot($table_name, 'MSG_subject'); |
|
281 | + $sql = "MSG_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
|
282 | 282 | GRP_ID int(10) unsigned NULL, |
283 | 283 | MSG_token varchar(255) NULL, |
284 | 284 | TXN_ID int(10) unsigned NULL, |
@@ -310,18 +310,18 @@ discard block |
||
310 | 310 | KEY STS_ID (STS_ID), |
311 | 311 | KEY MSG_created (MSG_created), |
312 | 312 | KEY MSG_modified (MSG_modified)"; |
313 | - $this->_table_is_new_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
314 | - $table_name = 'esp_message_template'; |
|
315 | - $sql = "MTP_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
313 | + $this->_table_is_new_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
314 | + $table_name = 'esp_message_template'; |
|
315 | + $sql = "MTP_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
316 | 316 | GRP_ID int(10) unsigned NOT NULL, |
317 | 317 | MTP_context varchar(50) NOT NULL, |
318 | 318 | MTP_template_field varchar(30) NOT NULL, |
319 | 319 | MTP_content text NOT NULL, |
320 | 320 | PRIMARY KEY (MTP_ID), |
321 | 321 | KEY GRP_ID (GRP_ID)"; |
322 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
323 | - $table_name = 'esp_message_template_group'; |
|
324 | - $sql = "GRP_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
322 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
323 | + $table_name = 'esp_message_template_group'; |
|
324 | + $sql = "GRP_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
325 | 325 | MTP_user_id int(10) NOT NULL DEFAULT '1', |
326 | 326 | MTP_name varchar(245) NOT NULL DEFAULT '', |
327 | 327 | MTP_description varchar(245) NOT NULL DEFAULT '', |
@@ -333,9 +333,9 @@ discard block |
||
333 | 333 | MTP_is_active tinyint(1) NOT NULL DEFAULT '1', |
334 | 334 | PRIMARY KEY (GRP_ID), |
335 | 335 | KEY MTP_user_id (MTP_user_id)"; |
336 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
337 | - $table_name = 'esp_payment'; |
|
338 | - $sql = "PAY_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
336 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
337 | + $table_name = 'esp_payment'; |
|
338 | + $sql = "PAY_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
339 | 339 | TXN_ID int(10) unsigned DEFAULT NULL, |
340 | 340 | STS_ID varchar(3) DEFAULT NULL, |
341 | 341 | PAY_timestamp datetime NOT NULL DEFAULT '0000-00-00 00:00:00', |
@@ -352,9 +352,9 @@ discard block |
||
352 | 352 | PRIMARY KEY (PAY_ID), |
353 | 353 | KEY PAY_timestamp (PAY_timestamp), |
354 | 354 | KEY TXN_ID (TXN_ID)"; |
355 | - $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB '); |
|
356 | - $table_name = 'esp_payment_method'; |
|
357 | - $sql = "PMD_ID int(11) NOT NULL AUTO_INCREMENT, |
|
355 | + $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB '); |
|
356 | + $table_name = 'esp_payment_method'; |
|
357 | + $sql = "PMD_ID int(11) NOT NULL AUTO_INCREMENT, |
|
358 | 358 | PMD_type varchar(124) DEFAULT NULL, |
359 | 359 | PMD_name varchar(255) DEFAULT NULL, |
360 | 360 | PMD_desc text, |
@@ -370,24 +370,24 @@ discard block |
||
370 | 370 | PRIMARY KEY (PMD_ID), |
371 | 371 | UNIQUE KEY PMD_slug_UNIQUE (PMD_slug), |
372 | 372 | KEY PMD_type (PMD_type)"; |
373 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB '); |
|
374 | - $table_name = "esp_ticket_price"; |
|
375 | - $sql = "TKP_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
373 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB '); |
|
374 | + $table_name = "esp_ticket_price"; |
|
375 | + $sql = "TKP_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
376 | 376 | TKT_ID int(10) unsigned NOT NULL, |
377 | 377 | PRC_ID int(10) unsigned NOT NULL, |
378 | 378 | PRIMARY KEY (TKP_ID), |
379 | 379 | KEY TKT_ID (TKT_ID), |
380 | 380 | KEY PRC_ID (PRC_ID)"; |
381 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
382 | - $table_name = "esp_ticket_template"; |
|
383 | - $sql = "TTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
381 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
382 | + $table_name = "esp_ticket_template"; |
|
383 | + $sql = "TTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
384 | 384 | TTM_name varchar(45) NOT NULL, |
385 | 385 | TTM_description text, |
386 | 386 | TTM_file varchar(45), |
387 | 387 | PRIMARY KEY (TTM_ID)"; |
388 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
389 | - $table_name = 'esp_question'; |
|
390 | - $sql = 'QST_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
388 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
389 | + $table_name = 'esp_question'; |
|
390 | + $sql = 'QST_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
391 | 391 | QST_display_text text NOT NULL, |
392 | 392 | QST_admin_label varchar(255) NOT NULL, |
393 | 393 | QST_system varchar(25) DEFAULT NULL, |
@@ -401,18 +401,18 @@ discard block |
||
401 | 401 | QST_deleted tinyint(2) unsigned NOT NULL DEFAULT 0, |
402 | 402 | PRIMARY KEY (QST_ID), |
403 | 403 | KEY QST_order (QST_order)'; |
404 | - $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
405 | - $table_name = 'esp_question_group_question'; |
|
406 | - $sql = "QGQ_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
404 | + $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
405 | + $table_name = 'esp_question_group_question'; |
|
406 | + $sql = "QGQ_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
407 | 407 | QSG_ID int(10) unsigned NOT NULL, |
408 | 408 | QST_ID int(10) unsigned NOT NULL, |
409 | 409 | QGQ_order int(10) unsigned NOT NULL DEFAULT 0, |
410 | 410 | PRIMARY KEY (QGQ_ID), |
411 | 411 | KEY QST_ID (QST_ID), |
412 | 412 | KEY QSG_ID_order (QSG_ID,QGQ_order)"; |
413 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
414 | - $table_name = 'esp_question_option'; |
|
415 | - $sql = "QSO_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
413 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
414 | + $table_name = 'esp_question_option'; |
|
415 | + $sql = "QSO_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
416 | 416 | QSO_value varchar(255) NOT NULL, |
417 | 417 | QSO_desc text NOT NULL, |
418 | 418 | QST_ID int(10) unsigned NOT NULL, |
@@ -422,9 +422,9 @@ discard block |
||
422 | 422 | PRIMARY KEY (QSO_ID), |
423 | 423 | KEY QST_ID (QST_ID), |
424 | 424 | KEY QSO_order (QSO_order)"; |
425 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
426 | - $table_name = 'esp_registration'; |
|
427 | - $sql = "REG_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
425 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
426 | + $table_name = 'esp_registration'; |
|
427 | + $sql = "REG_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
428 | 428 | EVT_ID bigint(20) unsigned NOT NULL, |
429 | 429 | ATT_ID bigint(20) unsigned NOT NULL, |
430 | 430 | TXN_ID int(10) unsigned NOT NULL, |
@@ -448,18 +448,18 @@ discard block |
||
448 | 448 | KEY TKT_ID (TKT_ID), |
449 | 449 | KEY EVT_ID (EVT_ID), |
450 | 450 | KEY STS_ID (STS_ID)"; |
451 | - $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB '); |
|
452 | - $table_name = 'esp_registration_payment'; |
|
453 | - $sql = "RPY_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
451 | + $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB '); |
|
452 | + $table_name = 'esp_registration_payment'; |
|
453 | + $sql = "RPY_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
454 | 454 | REG_ID int(10) unsigned NOT NULL, |
455 | 455 | PAY_ID int(10) unsigned NULL, |
456 | 456 | RPY_amount decimal(12,3) NOT NULL DEFAULT '0.00', |
457 | 457 | PRIMARY KEY (RPY_ID), |
458 | 458 | KEY REG_ID (REG_ID), |
459 | 459 | KEY PAY_ID (PAY_ID)"; |
460 | - $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB '); |
|
461 | - $table_name = 'esp_state'; |
|
462 | - $sql = "STA_ID smallint(5) unsigned NOT NULL AUTO_INCREMENT, |
|
460 | + $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB '); |
|
461 | + $table_name = 'esp_state'; |
|
462 | + $sql = "STA_ID smallint(5) unsigned NOT NULL AUTO_INCREMENT, |
|
463 | 463 | CNT_ISO varchar(2) NOT NULL, |
464 | 464 | STA_abbrev varchar(24) NOT NULL, |
465 | 465 | STA_name varchar(100) NOT NULL, |
@@ -467,9 +467,9 @@ discard block |
||
467 | 467 | PRIMARY KEY (STA_ID), |
468 | 468 | KEY STA_abbrev (STA_abbrev), |
469 | 469 | KEY CNT_ISO (CNT_ISO)"; |
470 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
471 | - $table_name = 'esp_status'; |
|
472 | - $sql = "STS_ID varchar(3) NOT NULL, |
|
470 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
471 | + $table_name = 'esp_status'; |
|
472 | + $sql = "STS_ID varchar(3) NOT NULL, |
|
473 | 473 | STS_code varchar(45) NOT NULL, |
474 | 474 | STS_type varchar(45) NOT NULL, |
475 | 475 | STS_can_edit tinyint(1) NOT NULL DEFAULT 0, |
@@ -477,9 +477,9 @@ discard block |
||
477 | 477 | STS_open tinyint(1) NOT NULL DEFAULT 1, |
478 | 478 | UNIQUE KEY STS_ID_UNIQUE (STS_ID), |
479 | 479 | KEY STS_type (STS_type)"; |
480 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
481 | - $table_name = 'esp_transaction'; |
|
482 | - $sql = "TXN_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
480 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
481 | + $table_name = 'esp_transaction'; |
|
482 | + $sql = "TXN_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
483 | 483 | TXN_timestamp datetime NOT NULL DEFAULT '0000-00-00 00:00:00', |
484 | 484 | TXN_total decimal(12,3) DEFAULT '0.00', |
485 | 485 | TXN_paid decimal(12,3) NOT NULL DEFAULT '0.00', |
@@ -491,9 +491,9 @@ discard block |
||
491 | 491 | PRIMARY KEY (TXN_ID), |
492 | 492 | KEY TXN_timestamp (TXN_timestamp), |
493 | 493 | KEY STS_ID (STS_ID)"; |
494 | - $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
495 | - $table_name = 'esp_venue_meta'; |
|
496 | - $sql = "VNUM_ID int(11) NOT NULL AUTO_INCREMENT, |
|
494 | + $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
495 | + $table_name = 'esp_venue_meta'; |
|
496 | + $sql = "VNUM_ID int(11) NOT NULL AUTO_INCREMENT, |
|
497 | 497 | VNU_ID bigint(20) unsigned NOT NULL DEFAULT 0, |
498 | 498 | VNU_address varchar(255) DEFAULT NULL, |
499 | 499 | VNU_address2 varchar(255) DEFAULT NULL, |
@@ -512,10 +512,10 @@ discard block |
||
512 | 512 | KEY VNU_ID (VNU_ID), |
513 | 513 | KEY STA_ID (STA_ID), |
514 | 514 | KEY CNT_ISO (CNT_ISO)"; |
515 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
516 | - // modified tables |
|
517 | - $table_name = "esp_price"; |
|
518 | - $sql = "PRC_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
515 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
516 | + // modified tables |
|
517 | + $table_name = "esp_price"; |
|
518 | + $sql = "PRC_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
519 | 519 | PRT_ID tinyint(3) unsigned NOT NULL, |
520 | 520 | PRC_amount decimal(12,3) NOT NULL DEFAULT '0.00', |
521 | 521 | PRC_name varchar(245) NOT NULL, |
@@ -528,9 +528,9 @@ discard block |
||
528 | 528 | PRC_parent int(10) unsigned DEFAULT 0, |
529 | 529 | PRIMARY KEY (PRC_ID), |
530 | 530 | KEY PRT_ID (PRT_ID)"; |
531 | - $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
532 | - $table_name = "esp_price_type"; |
|
533 | - $sql = "PRT_ID tinyint(3) unsigned NOT NULL AUTO_INCREMENT, |
|
531 | + $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
532 | + $table_name = "esp_price_type"; |
|
533 | + $sql = "PRT_ID tinyint(3) unsigned NOT NULL AUTO_INCREMENT, |
|
534 | 534 | PRT_name varchar(45) NOT NULL, |
535 | 535 | PBT_ID tinyint(3) unsigned NOT NULL DEFAULT '1', |
536 | 536 | PRT_is_percent tinyint(1) NOT NULL DEFAULT '0', |
@@ -539,9 +539,9 @@ discard block |
||
539 | 539 | PRT_deleted tinyint(1) NOT NULL DEFAULT '0', |
540 | 540 | UNIQUE KEY PRT_name_UNIQUE (PRT_name), |
541 | 541 | PRIMARY KEY (PRT_ID)"; |
542 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB '); |
|
543 | - $table_name = "esp_ticket"; |
|
544 | - $sql = "TKT_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
542 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB '); |
|
543 | + $table_name = "esp_ticket"; |
|
544 | + $sql = "TKT_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
545 | 545 | TTM_ID int(10) unsigned NOT NULL, |
546 | 546 | TKT_name varchar(245) NOT NULL DEFAULT '', |
547 | 547 | TKT_description text NOT NULL, |
@@ -564,9 +564,9 @@ discard block |
||
564 | 564 | TKT_deleted tinyint(1) NOT NULL DEFAULT '0', |
565 | 565 | PRIMARY KEY (TKT_ID), |
566 | 566 | KEY TKT_start_date (TKT_start_date)"; |
567 | - $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
568 | - $table_name = 'esp_question_group'; |
|
569 | - $sql = 'QSG_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
567 | + $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB'); |
|
568 | + $table_name = 'esp_question_group'; |
|
569 | + $sql = 'QSG_ID int(10) unsigned NOT NULL AUTO_INCREMENT, |
|
570 | 570 | QSG_name varchar(255) NOT NULL, |
571 | 571 | QSG_identifier varchar(100) NOT NULL, |
572 | 572 | QSG_desc text NULL, |
@@ -579,165 +579,165 @@ discard block |
||
579 | 579 | PRIMARY KEY (QSG_ID), |
580 | 580 | UNIQUE KEY QSG_identifier_UNIQUE (QSG_identifier), |
581 | 581 | KEY QSG_order (QSG_order)'; |
582 | - $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
583 | - $this->insert_default_data(); |
|
584 | - return true; |
|
585 | - } |
|
582 | + $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB'); |
|
583 | + $this->insert_default_data(); |
|
584 | + return true; |
|
585 | + } |
|
586 | 586 | |
587 | - /** |
|
588 | - * Inserts default data after parent was called. |
|
589 | - * @since 4.10.0.p |
|
590 | - * @throws EE_Error |
|
591 | - * @throws InvalidArgumentException |
|
592 | - * @throws ReflectionException |
|
593 | - * @throws InvalidDataTypeException |
|
594 | - * @throws InvalidInterfaceException |
|
595 | - */ |
|
596 | - public function insert_default_data() |
|
597 | - { |
|
598 | - /** @var EE_DMS_Core_4_1_0 $script_4_1_defaults */ |
|
599 | - $script_4_1_defaults = EE_Registry::instance()->load_dms('Core_4_1_0'); |
|
600 | - // (because many need to convert old string states to foreign keys into the states table) |
|
601 | - $script_4_1_defaults->insert_default_states(); |
|
602 | - $script_4_1_defaults->insert_default_countries(); |
|
603 | - /** @var EE_DMS_Core_4_5_0 $script_4_5_defaults */ |
|
604 | - $script_4_5_defaults = EE_Registry::instance()->load_dms('Core_4_5_0'); |
|
605 | - $script_4_5_defaults->insert_default_price_types(); |
|
606 | - $script_4_5_defaults->insert_default_prices(); |
|
607 | - $script_4_5_defaults->insert_default_tickets(); |
|
608 | - /** @var EE_DMS_Core_4_6_0 $script_4_6_defaults */ |
|
609 | - $script_4_6_defaults = EE_Registry::instance()->load_dms('Core_4_6_0'); |
|
610 | - $script_4_6_defaults->add_default_admin_only_payments(); |
|
611 | - $script_4_6_defaults->insert_default_currencies(); |
|
612 | - /** @var EE_DMS_Core_4_8_0 $script_4_8_defaults */ |
|
613 | - $script_4_8_defaults = EE_Registry::instance()->load_dms('Core_4_8_0'); |
|
614 | - $script_4_8_defaults->verify_new_countries(); |
|
615 | - $script_4_8_defaults->verify_new_currencies(); |
|
616 | - $this->verify_db_collations(); |
|
617 | - $this->verify_db_collations_again(); |
|
618 | - } |
|
587 | + /** |
|
588 | + * Inserts default data after parent was called. |
|
589 | + * @since 4.10.0.p |
|
590 | + * @throws EE_Error |
|
591 | + * @throws InvalidArgumentException |
|
592 | + * @throws ReflectionException |
|
593 | + * @throws InvalidDataTypeException |
|
594 | + * @throws InvalidInterfaceException |
|
595 | + */ |
|
596 | + public function insert_default_data() |
|
597 | + { |
|
598 | + /** @var EE_DMS_Core_4_1_0 $script_4_1_defaults */ |
|
599 | + $script_4_1_defaults = EE_Registry::instance()->load_dms('Core_4_1_0'); |
|
600 | + // (because many need to convert old string states to foreign keys into the states table) |
|
601 | + $script_4_1_defaults->insert_default_states(); |
|
602 | + $script_4_1_defaults->insert_default_countries(); |
|
603 | + /** @var EE_DMS_Core_4_5_0 $script_4_5_defaults */ |
|
604 | + $script_4_5_defaults = EE_Registry::instance()->load_dms('Core_4_5_0'); |
|
605 | + $script_4_5_defaults->insert_default_price_types(); |
|
606 | + $script_4_5_defaults->insert_default_prices(); |
|
607 | + $script_4_5_defaults->insert_default_tickets(); |
|
608 | + /** @var EE_DMS_Core_4_6_0 $script_4_6_defaults */ |
|
609 | + $script_4_6_defaults = EE_Registry::instance()->load_dms('Core_4_6_0'); |
|
610 | + $script_4_6_defaults->add_default_admin_only_payments(); |
|
611 | + $script_4_6_defaults->insert_default_currencies(); |
|
612 | + /** @var EE_DMS_Core_4_8_0 $script_4_8_defaults */ |
|
613 | + $script_4_8_defaults = EE_Registry::instance()->load_dms('Core_4_8_0'); |
|
614 | + $script_4_8_defaults->verify_new_countries(); |
|
615 | + $script_4_8_defaults->verify_new_currencies(); |
|
616 | + $this->verify_db_collations(); |
|
617 | + $this->verify_db_collations_again(); |
|
618 | + } |
|
619 | 619 | |
620 | 620 | |
621 | 621 | |
622 | - /** |
|
623 | - * @return boolean |
|
624 | - */ |
|
625 | - public function schema_changes_after_migration() |
|
626 | - { |
|
627 | - return true; |
|
628 | - } |
|
622 | + /** |
|
623 | + * @return boolean |
|
624 | + */ |
|
625 | + public function schema_changes_after_migration() |
|
626 | + { |
|
627 | + return true; |
|
628 | + } |
|
629 | 629 | |
630 | 630 | |
631 | 631 | |
632 | - public function migration_page_hooks() |
|
633 | - { |
|
634 | - } |
|
632 | + public function migration_page_hooks() |
|
633 | + { |
|
634 | + } |
|
635 | 635 | |
636 | 636 | |
637 | 637 | |
638 | - /** |
|
639 | - * Verify all EE4 models' tables use utf8mb4 collation |
|
640 | - * |
|
641 | - * @return void |
|
642 | - */ |
|
643 | - public function verify_db_collations() |
|
644 | - { |
|
645 | - global $wpdb; |
|
646 | - // double-check we haven't already done it or that that the DB doesn't support utf8mb4 |
|
647 | - if ( |
|
648 | - 'utf8mb4' !== $wpdb->charset |
|
649 | - || get_option('ee_verified_db_collations', false) |
|
650 | - ) { |
|
651 | - return; |
|
652 | - } |
|
653 | - // grab tables from each model |
|
654 | - $tables_to_check = array(); |
|
655 | - foreach (EE_Registry::instance()->non_abstract_db_models as $model_name) { |
|
656 | - if (method_exists($model_name, 'instance')) { |
|
657 | - $model_obj = call_user_func(array($model_name, 'instance')); |
|
658 | - if ($model_obj instanceof EEM_Base) { |
|
659 | - foreach ($model_obj->get_tables() as $table) { |
|
660 | - if ( |
|
661 | - strpos($table->get_table_name(), 'esp_') |
|
662 | - && (is_main_site()// for main tables, verify global tables |
|
663 | - || ! $table->is_global()// if not the main site, then only verify non-global tables (avoid doubling up) |
|
664 | - ) |
|
665 | - && function_exists('maybe_convert_table_to_utf8mb4') |
|
666 | - ) { |
|
667 | - $tables_to_check[] = $table->get_table_name(); |
|
668 | - } |
|
669 | - } |
|
670 | - } |
|
671 | - } |
|
672 | - } |
|
673 | - // and let's just be sure these addons' tables get migrated too. They already get handled if their addons are active |
|
674 | - // when this code is run, but not otherwise. Once we record what tables EE added, we'll be able to use that instead |
|
675 | - // of hard-coding this |
|
676 | - $addon_tables = array( |
|
677 | - // mailchimp |
|
678 | - 'esp_event_mailchimp_list_group', |
|
679 | - 'esp_event_question_mailchimp_field', |
|
680 | - // multisite |
|
681 | - 'esp_blog_meta', |
|
682 | - // people |
|
683 | - 'esp_people_to_post', |
|
684 | - // promotions |
|
685 | - 'esp_promotion', |
|
686 | - 'esp_promotion_object', |
|
687 | - ); |
|
688 | - foreach ($addon_tables as $table_name) { |
|
689 | - $tables_to_check[] = $table_name; |
|
690 | - } |
|
691 | - $this->_verify_db_collations_for_tables(array_unique($tables_to_check)); |
|
692 | - // ok and now let's remember this was done (without needing to check the db schemas all over again) |
|
693 | - add_option('ee_verified_db_collations', true, null, 'no'); |
|
694 | - // seeing how this ran with the fix from 10435, no need to check again |
|
695 | - add_option('ee_verified_db_collations_again', true, null, 'no'); |
|
696 | - } |
|
638 | + /** |
|
639 | + * Verify all EE4 models' tables use utf8mb4 collation |
|
640 | + * |
|
641 | + * @return void |
|
642 | + */ |
|
643 | + public function verify_db_collations() |
|
644 | + { |
|
645 | + global $wpdb; |
|
646 | + // double-check we haven't already done it or that that the DB doesn't support utf8mb4 |
|
647 | + if ( |
|
648 | + 'utf8mb4' !== $wpdb->charset |
|
649 | + || get_option('ee_verified_db_collations', false) |
|
650 | + ) { |
|
651 | + return; |
|
652 | + } |
|
653 | + // grab tables from each model |
|
654 | + $tables_to_check = array(); |
|
655 | + foreach (EE_Registry::instance()->non_abstract_db_models as $model_name) { |
|
656 | + if (method_exists($model_name, 'instance')) { |
|
657 | + $model_obj = call_user_func(array($model_name, 'instance')); |
|
658 | + if ($model_obj instanceof EEM_Base) { |
|
659 | + foreach ($model_obj->get_tables() as $table) { |
|
660 | + if ( |
|
661 | + strpos($table->get_table_name(), 'esp_') |
|
662 | + && (is_main_site()// for main tables, verify global tables |
|
663 | + || ! $table->is_global()// if not the main site, then only verify non-global tables (avoid doubling up) |
|
664 | + ) |
|
665 | + && function_exists('maybe_convert_table_to_utf8mb4') |
|
666 | + ) { |
|
667 | + $tables_to_check[] = $table->get_table_name(); |
|
668 | + } |
|
669 | + } |
|
670 | + } |
|
671 | + } |
|
672 | + } |
|
673 | + // and let's just be sure these addons' tables get migrated too. They already get handled if their addons are active |
|
674 | + // when this code is run, but not otherwise. Once we record what tables EE added, we'll be able to use that instead |
|
675 | + // of hard-coding this |
|
676 | + $addon_tables = array( |
|
677 | + // mailchimp |
|
678 | + 'esp_event_mailchimp_list_group', |
|
679 | + 'esp_event_question_mailchimp_field', |
|
680 | + // multisite |
|
681 | + 'esp_blog_meta', |
|
682 | + // people |
|
683 | + 'esp_people_to_post', |
|
684 | + // promotions |
|
685 | + 'esp_promotion', |
|
686 | + 'esp_promotion_object', |
|
687 | + ); |
|
688 | + foreach ($addon_tables as $table_name) { |
|
689 | + $tables_to_check[] = $table_name; |
|
690 | + } |
|
691 | + $this->_verify_db_collations_for_tables(array_unique($tables_to_check)); |
|
692 | + // ok and now let's remember this was done (without needing to check the db schemas all over again) |
|
693 | + add_option('ee_verified_db_collations', true, null, 'no'); |
|
694 | + // seeing how this ran with the fix from 10435, no need to check again |
|
695 | + add_option('ee_verified_db_collations_again', true, null, 'no'); |
|
696 | + } |
|
697 | 697 | |
698 | 698 | |
699 | 699 | |
700 | - /** |
|
701 | - * Verifies DB collations because a bug was discovered on https://events.codebasehq.com/projects/event-espresso/tickets/10435 |
|
702 | - * which meant some DB collations might not have been updated |
|
703 | - * @return void |
|
704 | - */ |
|
705 | - public function verify_db_collations_again() |
|
706 | - { |
|
707 | - global $wpdb; |
|
708 | - // double-check we haven't already done this or that the DB doesn't support it |
|
709 | - // compare to how WordPress' upgrade_430() function does this check |
|
710 | - if ( |
|
711 | - 'utf8mb4' !== $wpdb->charset |
|
712 | - || get_option('ee_verified_db_collations_again', false) |
|
713 | - ) { |
|
714 | - return; |
|
715 | - } |
|
716 | - $tables_to_check = array( |
|
717 | - 'esp_attendee_meta', |
|
718 | - 'esp_message' |
|
719 | - ); |
|
720 | - $this->_verify_db_collations_for_tables(array_unique($tables_to_check)); |
|
721 | - add_option('ee_verified_db_collations_again', true, null, 'no'); |
|
722 | - } |
|
700 | + /** |
|
701 | + * Verifies DB collations because a bug was discovered on https://events.codebasehq.com/projects/event-espresso/tickets/10435 |
|
702 | + * which meant some DB collations might not have been updated |
|
703 | + * @return void |
|
704 | + */ |
|
705 | + public function verify_db_collations_again() |
|
706 | + { |
|
707 | + global $wpdb; |
|
708 | + // double-check we haven't already done this or that the DB doesn't support it |
|
709 | + // compare to how WordPress' upgrade_430() function does this check |
|
710 | + if ( |
|
711 | + 'utf8mb4' !== $wpdb->charset |
|
712 | + || get_option('ee_verified_db_collations_again', false) |
|
713 | + ) { |
|
714 | + return; |
|
715 | + } |
|
716 | + $tables_to_check = array( |
|
717 | + 'esp_attendee_meta', |
|
718 | + 'esp_message' |
|
719 | + ); |
|
720 | + $this->_verify_db_collations_for_tables(array_unique($tables_to_check)); |
|
721 | + add_option('ee_verified_db_collations_again', true, null, 'no'); |
|
722 | + } |
|
723 | 723 | |
724 | 724 | |
725 | 725 | |
726 | - /** |
|
727 | - * Runs maybe_convert_table_to_utf8mb4 on the specified tables |
|
728 | - * @param $tables_to_check |
|
729 | - * @return boolean true if logic ran, false if it didn't |
|
730 | - */ |
|
731 | - protected function _verify_db_collations_for_tables($tables_to_check) |
|
732 | - { |
|
733 | - foreach ($tables_to_check as $table_name) { |
|
734 | - $table_name = $this->_table_analysis->ensureTableNameHasPrefix($table_name); |
|
735 | - if ( |
|
736 | - ! apply_filters('FHEE__EE_DMS_Core_4_9_0__verify_db_collations__check_overridden', false, $table_name) |
|
737 | - && $this->_get_table_analysis()->tableExists($table_name) |
|
738 | - ) { |
|
739 | - maybe_convert_table_to_utf8mb4($table_name); |
|
740 | - } |
|
741 | - } |
|
742 | - } |
|
726 | + /** |
|
727 | + * Runs maybe_convert_table_to_utf8mb4 on the specified tables |
|
728 | + * @param $tables_to_check |
|
729 | + * @return boolean true if logic ran, false if it didn't |
|
730 | + */ |
|
731 | + protected function _verify_db_collations_for_tables($tables_to_check) |
|
732 | + { |
|
733 | + foreach ($tables_to_check as $table_name) { |
|
734 | + $table_name = $this->_table_analysis->ensureTableNameHasPrefix($table_name); |
|
735 | + if ( |
|
736 | + ! apply_filters('FHEE__EE_DMS_Core_4_9_0__verify_db_collations__check_overridden', false, $table_name) |
|
737 | + && $this->_get_table_analysis()->tableExists($table_name) |
|
738 | + ) { |
|
739 | + maybe_convert_table_to_utf8mb4($table_name); |
|
740 | + } |
|
741 | + } |
|
742 | + } |
|
743 | 743 | } |
@@ -66,8 +66,8 @@ |
||
66 | 66 | { |
67 | 67 | |
68 | 68 | $templates_relative_path = 'modules/gateways/Invoice/lib/templates/'; |
69 | - $overridden_invoice_body = EEH_Template::locate_template($templates_relative_path . 'invoice_body.template.php', null, false, false, true); |
|
70 | - $overridden_receipt_body = EEH_Template::locate_template($templates_relative_path . 'receipt_body.template.php', null, false, false, true); |
|
69 | + $overridden_invoice_body = EEH_Template::locate_template($templates_relative_path.'invoice_body.template.php', null, false, false, true); |
|
70 | + $overridden_receipt_body = EEH_Template::locate_template($templates_relative_path.'receipt_body.template.php', null, false, false, true); |
|
71 | 71 | if ($overridden_invoice_body || $overridden_receipt_body) { |
72 | 72 | new PersistentAdminNotice( |
73 | 73 | 'invoice_overriding_templates', |
@@ -27,59 +27,59 @@ |
||
27 | 27 | */ |
28 | 28 | class EE_DMS_4_6_0_invoice_settings extends EE_Data_Migration_Script_Stage |
29 | 29 | { |
30 | - /** |
|
31 | - * Just initializes the status of the migration |
|
32 | - */ |
|
33 | - public function __construct() |
|
34 | - { |
|
35 | - $this->_pretty_name = esc_html__('Update Invoice Settings', 'event_espresso'); |
|
36 | - parent::__construct(); |
|
37 | - } |
|
30 | + /** |
|
31 | + * Just initializes the status of the migration |
|
32 | + */ |
|
33 | + public function __construct() |
|
34 | + { |
|
35 | + $this->_pretty_name = esc_html__('Update Invoice Settings', 'event_espresso'); |
|
36 | + parent::__construct(); |
|
37 | + } |
|
38 | 38 | |
39 | 39 | |
40 | 40 | |
41 | - /** |
|
42 | - * _count_records_to_migrate |
|
43 | - * Counts the records to migrate; the public version may cache it |
|
44 | - * |
|
45 | - * @access protected |
|
46 | - * @return int |
|
47 | - */ |
|
48 | - protected function _count_records_to_migrate() |
|
49 | - { |
|
50 | - return 1; |
|
51 | - } |
|
41 | + /** |
|
42 | + * _count_records_to_migrate |
|
43 | + * Counts the records to migrate; the public version may cache it |
|
44 | + * |
|
45 | + * @access protected |
|
46 | + * @return int |
|
47 | + */ |
|
48 | + protected function _count_records_to_migrate() |
|
49 | + { |
|
50 | + return 1; |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * _migration_step |
|
57 | - * |
|
58 | - * @access protected |
|
59 | - * @param int $num_items |
|
60 | - * @throws EE_Error |
|
61 | - * @return int number of items ACTUALLY migrated |
|
62 | - * @throws InvalidDataTypeException |
|
63 | - */ |
|
64 | - protected function _migration_step($num_items = 1) |
|
65 | - { |
|
55 | + /** |
|
56 | + * _migration_step |
|
57 | + * |
|
58 | + * @access protected |
|
59 | + * @param int $num_items |
|
60 | + * @throws EE_Error |
|
61 | + * @return int number of items ACTUALLY migrated |
|
62 | + * @throws InvalidDataTypeException |
|
63 | + */ |
|
64 | + protected function _migration_step($num_items = 1) |
|
65 | + { |
|
66 | 66 | |
67 | - $templates_relative_path = 'modules/gateways/Invoice/lib/templates/'; |
|
68 | - $overridden_invoice_body = EEH_Template::locate_template($templates_relative_path . 'invoice_body.template.php', null, false, false, true); |
|
69 | - $overridden_receipt_body = EEH_Template::locate_template($templates_relative_path . 'receipt_body.template.php', null, false, false, true); |
|
70 | - if ($overridden_invoice_body || $overridden_receipt_body) { |
|
71 | - new PersistentAdminNotice( |
|
72 | - 'invoice_overriding_templates', |
|
73 | - esc_html__( |
|
74 | - 'Note: in this version of Event Espresso, PDF and HTML Invoices and Receipts are now Messages and can be changed just like any other messages; however we noticed you had previously overridden the old default Invoice/Receipt templates. Because of this, your old Invoice/Receipt templates will continue to be used INSTEAD of the new Invoice/Receipt message equivalents (but this will be removed in an upcoming version). We recommend deleting your old Invoice/Receipt templates and using the new messages system. Then modify the new Invoice and Receipt messages\'s content in Messages -> Invoice and Messages -> Receipt.', |
|
75 | - 'event_espresso' |
|
76 | - ), |
|
77 | - true |
|
78 | - ); |
|
79 | - } |
|
67 | + $templates_relative_path = 'modules/gateways/Invoice/lib/templates/'; |
|
68 | + $overridden_invoice_body = EEH_Template::locate_template($templates_relative_path . 'invoice_body.template.php', null, false, false, true); |
|
69 | + $overridden_receipt_body = EEH_Template::locate_template($templates_relative_path . 'receipt_body.template.php', null, false, false, true); |
|
70 | + if ($overridden_invoice_body || $overridden_receipt_body) { |
|
71 | + new PersistentAdminNotice( |
|
72 | + 'invoice_overriding_templates', |
|
73 | + esc_html__( |
|
74 | + 'Note: in this version of Event Espresso, PDF and HTML Invoices and Receipts are now Messages and can be changed just like any other messages; however we noticed you had previously overridden the old default Invoice/Receipt templates. Because of this, your old Invoice/Receipt templates will continue to be used INSTEAD of the new Invoice/Receipt message equivalents (but this will be removed in an upcoming version). We recommend deleting your old Invoice/Receipt templates and using the new messages system. Then modify the new Invoice and Receipt messages\'s content in Messages -> Invoice and Messages -> Receipt.', |
|
75 | + 'event_espresso' |
|
76 | + ), |
|
77 | + true |
|
78 | + ); |
|
79 | + } |
|
80 | 80 | |
81 | - // regardless of whether it worked or not, we ought to continue the migration |
|
82 | - $this->set_completed(); |
|
83 | - return 1; |
|
84 | - } |
|
81 | + // regardless of whether it worked or not, we ought to continue the migration |
|
82 | + $this->set_completed(); |
|
83 | + return 1; |
|
84 | + } |
|
85 | 85 | } |