@@ -17,189 +17,189 @@ |
||
17 | 17 | class EE_Select_Ajax_Model_Rest_Input extends EE_Form_Input_With_Options_Base |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var string $_model_name |
|
22 | - */ |
|
23 | - protected $_model_name; |
|
20 | + /** |
|
21 | + * @var string $_model_name |
|
22 | + */ |
|
23 | + protected $_model_name; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var string $_display_field_name |
|
27 | - */ |
|
28 | - protected $_display_field_name; |
|
25 | + /** |
|
26 | + * @var string $_display_field_name |
|
27 | + */ |
|
28 | + protected $_display_field_name; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @var string $_value_field_name |
|
32 | - */ |
|
33 | - protected $_value_field_name; |
|
30 | + /** |
|
31 | + * @var string $_value_field_name |
|
32 | + */ |
|
33 | + protected $_value_field_name; |
|
34 | 34 | |
35 | - /** |
|
36 | - * @var array $_extra_select_columns |
|
37 | - */ |
|
38 | - protected $_extra_select_columns = array(); |
|
35 | + /** |
|
36 | + * @var array $_extra_select_columns |
|
37 | + */ |
|
38 | + protected $_extra_select_columns = array(); |
|
39 | 39 | |
40 | 40 | |
41 | - /** |
|
42 | - * @param array $input_settings { |
|
43 | - * @type string $model_name the name of model to be used for searching, both via the REST API and server-side model queries |
|
44 | - * @type array $query_params default query parameters which will apply to both REST API queries and server-side queries. This should be |
|
45 | - * in the exact format that will be used for server-side model usage (eg use index 0 for where conditions, not |
|
46 | - * the string "where") |
|
47 | - * @type string $value_field_name the name of the model field on this model to |
|
48 | - * be used for the HTML select's option's values |
|
49 | - * @type string $display_field_name the name of the model field on this model |
|
50 | - * to be used for the HTML select's option's display text |
|
51 | - * @type array $select2_args arguments to be passed directly into the select2's JS constructor |
|
52 | - * } |
|
53 | - * And the arguments accepted by EE_Form_Input_With_Options_Base |
|
54 | - * } |
|
55 | - * @throws EE_Error |
|
56 | - * @throws InvalidArgumentException |
|
57 | - * @throws InvalidDataTypeException |
|
58 | - * @throws InvalidInterfaceException |
|
59 | - */ |
|
60 | - public function __construct($input_settings = array()) |
|
61 | - { |
|
62 | - // needed input settings: |
|
63 | - // select2_args |
|
64 | - $this->_model_name = EEH_Array::is_set( |
|
65 | - $input_settings, |
|
66 | - 'model_name', |
|
67 | - null |
|
68 | - ); |
|
69 | - $model = $this->_get_model(); |
|
70 | - $query_params = EEH_Array::is_set( |
|
71 | - $input_settings, |
|
72 | - 'query_params', |
|
73 | - array() |
|
74 | - ); |
|
75 | - // make sure limit and caps are always set |
|
76 | - $query_params = array_merge( |
|
77 | - array( 'limit' => 10, 'caps' => EEM_Base::caps_read_admin ), |
|
78 | - $query_params |
|
79 | - ); |
|
80 | - $this->_value_field_name = EEH_Array::is_set( |
|
81 | - $input_settings, |
|
82 | - 'value_field_name', |
|
83 | - $model->primary_key_name() |
|
84 | - ); |
|
85 | - $this->_display_field_name = EEH_Array::is_set( |
|
86 | - $input_settings, |
|
87 | - 'display_field_name', |
|
88 | - $model->get_a_field_of_type('EE_Text_Field_Base')->get_name() |
|
89 | - ); |
|
90 | - $this->_extra_select_columns = EEH_Array::is_set( |
|
91 | - $input_settings, |
|
92 | - 'extra_select_columns', |
|
93 | - array() |
|
94 | - ); |
|
95 | - $this->_add_validation_strategy( |
|
96 | - new EE_Model_Matching_Query_Validation_Strategy( |
|
97 | - '', |
|
98 | - $this->_model_name, |
|
99 | - $query_params, |
|
100 | - $this->_value_field_name |
|
101 | - ) |
|
102 | - ); |
|
103 | - // get resource endpoint |
|
104 | - $rest_controller = LoaderFactory::getLoader()->getNew( |
|
105 | - 'EventEspresso\core\libraries\rest_api\controllers\model\Read' |
|
106 | - ); |
|
107 | - $rest_controller->setRequestedVersion(EED_Core_Rest_Api::latest_rest_api_version()); |
|
108 | - $default_select2_args = array( |
|
109 | - 'ajax' => array( |
|
110 | - 'url' => $rest_controller->getVersionedLinkTo( |
|
111 | - EEH_Inflector::pluralize_and_lower($this->_model_name) |
|
112 | - ), |
|
113 | - 'dataType' => 'json', |
|
114 | - 'delay' => '250', |
|
115 | - 'data_interface' => 'EE_Select2_REST_API_Interface', |
|
116 | - 'data_interface_args' => array( |
|
117 | - 'default_query_params' => (object) ModelDataTranslator::prepareQueryParamsForRestApi( |
|
118 | - $query_params, |
|
119 | - $model |
|
120 | - ), |
|
121 | - 'display_field' => $this->_display_field_name, |
|
122 | - 'value_field' => $this->_value_field_name, |
|
123 | - 'nonce' => wp_create_nonce('wp_rest'), |
|
124 | - 'locale' => str_replace('_', '-', strtolower(get_locale())) |
|
125 | - ), |
|
126 | - ), |
|
127 | - 'cache' => true, |
|
128 | - 'width' => 'resolve' |
|
129 | - ); |
|
130 | - $select2_args = array_replace_recursive( |
|
131 | - $default_select2_args, |
|
132 | - (array) EEH_Array::is_set($input_settings, 'select2_args', array()) |
|
133 | - ); |
|
134 | - $this->set_display_strategy(new EE_Select2_Display_Strategy($select2_args)); |
|
135 | - parent::__construct(array(), $input_settings); |
|
136 | - } |
|
41 | + /** |
|
42 | + * @param array $input_settings { |
|
43 | + * @type string $model_name the name of model to be used for searching, both via the REST API and server-side model queries |
|
44 | + * @type array $query_params default query parameters which will apply to both REST API queries and server-side queries. This should be |
|
45 | + * in the exact format that will be used for server-side model usage (eg use index 0 for where conditions, not |
|
46 | + * the string "where") |
|
47 | + * @type string $value_field_name the name of the model field on this model to |
|
48 | + * be used for the HTML select's option's values |
|
49 | + * @type string $display_field_name the name of the model field on this model |
|
50 | + * to be used for the HTML select's option's display text |
|
51 | + * @type array $select2_args arguments to be passed directly into the select2's JS constructor |
|
52 | + * } |
|
53 | + * And the arguments accepted by EE_Form_Input_With_Options_Base |
|
54 | + * } |
|
55 | + * @throws EE_Error |
|
56 | + * @throws InvalidArgumentException |
|
57 | + * @throws InvalidDataTypeException |
|
58 | + * @throws InvalidInterfaceException |
|
59 | + */ |
|
60 | + public function __construct($input_settings = array()) |
|
61 | + { |
|
62 | + // needed input settings: |
|
63 | + // select2_args |
|
64 | + $this->_model_name = EEH_Array::is_set( |
|
65 | + $input_settings, |
|
66 | + 'model_name', |
|
67 | + null |
|
68 | + ); |
|
69 | + $model = $this->_get_model(); |
|
70 | + $query_params = EEH_Array::is_set( |
|
71 | + $input_settings, |
|
72 | + 'query_params', |
|
73 | + array() |
|
74 | + ); |
|
75 | + // make sure limit and caps are always set |
|
76 | + $query_params = array_merge( |
|
77 | + array( 'limit' => 10, 'caps' => EEM_Base::caps_read_admin ), |
|
78 | + $query_params |
|
79 | + ); |
|
80 | + $this->_value_field_name = EEH_Array::is_set( |
|
81 | + $input_settings, |
|
82 | + 'value_field_name', |
|
83 | + $model->primary_key_name() |
|
84 | + ); |
|
85 | + $this->_display_field_name = EEH_Array::is_set( |
|
86 | + $input_settings, |
|
87 | + 'display_field_name', |
|
88 | + $model->get_a_field_of_type('EE_Text_Field_Base')->get_name() |
|
89 | + ); |
|
90 | + $this->_extra_select_columns = EEH_Array::is_set( |
|
91 | + $input_settings, |
|
92 | + 'extra_select_columns', |
|
93 | + array() |
|
94 | + ); |
|
95 | + $this->_add_validation_strategy( |
|
96 | + new EE_Model_Matching_Query_Validation_Strategy( |
|
97 | + '', |
|
98 | + $this->_model_name, |
|
99 | + $query_params, |
|
100 | + $this->_value_field_name |
|
101 | + ) |
|
102 | + ); |
|
103 | + // get resource endpoint |
|
104 | + $rest_controller = LoaderFactory::getLoader()->getNew( |
|
105 | + 'EventEspresso\core\libraries\rest_api\controllers\model\Read' |
|
106 | + ); |
|
107 | + $rest_controller->setRequestedVersion(EED_Core_Rest_Api::latest_rest_api_version()); |
|
108 | + $default_select2_args = array( |
|
109 | + 'ajax' => array( |
|
110 | + 'url' => $rest_controller->getVersionedLinkTo( |
|
111 | + EEH_Inflector::pluralize_and_lower($this->_model_name) |
|
112 | + ), |
|
113 | + 'dataType' => 'json', |
|
114 | + 'delay' => '250', |
|
115 | + 'data_interface' => 'EE_Select2_REST_API_Interface', |
|
116 | + 'data_interface_args' => array( |
|
117 | + 'default_query_params' => (object) ModelDataTranslator::prepareQueryParamsForRestApi( |
|
118 | + $query_params, |
|
119 | + $model |
|
120 | + ), |
|
121 | + 'display_field' => $this->_display_field_name, |
|
122 | + 'value_field' => $this->_value_field_name, |
|
123 | + 'nonce' => wp_create_nonce('wp_rest'), |
|
124 | + 'locale' => str_replace('_', '-', strtolower(get_locale())) |
|
125 | + ), |
|
126 | + ), |
|
127 | + 'cache' => true, |
|
128 | + 'width' => 'resolve' |
|
129 | + ); |
|
130 | + $select2_args = array_replace_recursive( |
|
131 | + $default_select2_args, |
|
132 | + (array) EEH_Array::is_set($input_settings, 'select2_args', array()) |
|
133 | + ); |
|
134 | + $this->set_display_strategy(new EE_Select2_Display_Strategy($select2_args)); |
|
135 | + parent::__construct(array(), $input_settings); |
|
136 | + } |
|
137 | 137 | |
138 | 138 | |
139 | 139 | |
140 | - /** |
|
141 | - * Before setting the raw value (usually because we're setting the default, |
|
142 | - * or we've received a form submission and this might be re-displayed to the user), |
|
143 | - * sets the options so that the current selections appear on initial display. |
|
144 | - * |
|
145 | - * Note: because this input uses EE_Model_Matching_Query_Validation_Strategy |
|
146 | - * for validation, this input's options only affect DISPLAY and NOT validation, |
|
147 | - * which is why its ok to just assume the provided $value to be in the list of acceptable values |
|
148 | - * |
|
149 | - * @param mixed $value |
|
150 | - * @return void |
|
151 | - * @throws \EE_Error |
|
152 | - */ |
|
153 | - public function _set_raw_value($value) |
|
154 | - { |
|
155 | - $values_for_options = (array) $value; |
|
156 | - $value_field = $this->_get_model()->field_settings_for($this->_value_field_name); |
|
157 | - $display_field = $this->_get_model()->field_settings_for($this->_display_field_name); |
|
158 | - $this->_extra_select_columns[] = $value_field->get_qualified_column() . ' AS ' . $this->_value_field_name; |
|
159 | - $this->_extra_select_columns[] = $display_field->get_qualified_column() . ' AS ' . $this->_display_field_name; |
|
160 | - $display_values = $this->_get_model()->get_all_wpdb_results( |
|
161 | - array( |
|
162 | - array( |
|
163 | - $this->_value_field_name => array( 'IN', $values_for_options ) |
|
164 | - ) |
|
165 | - ), |
|
166 | - ARRAY_A, |
|
167 | - implode(',', $this->_extra_select_columns) |
|
168 | - ); |
|
169 | - $select_options = array(); |
|
170 | - if (is_array($select_options)) { |
|
171 | - foreach ($display_values as $db_rows) { |
|
172 | - $db_rows = (array) $db_rows; |
|
173 | - $select_options[ $db_rows[ $this->_value_field_name ] ] = apply_filters( |
|
174 | - 'FHEE__EE_Select_Ajax_Model_Rest_Input___set_raw_value__select_option_value', |
|
175 | - $db_rows[ $this->_display_field_name ], |
|
176 | - $db_rows |
|
177 | - ); |
|
178 | - } |
|
179 | - } |
|
180 | - $this->set_select_options($select_options); |
|
181 | - parent::_set_raw_value($value); |
|
182 | - } |
|
140 | + /** |
|
141 | + * Before setting the raw value (usually because we're setting the default, |
|
142 | + * or we've received a form submission and this might be re-displayed to the user), |
|
143 | + * sets the options so that the current selections appear on initial display. |
|
144 | + * |
|
145 | + * Note: because this input uses EE_Model_Matching_Query_Validation_Strategy |
|
146 | + * for validation, this input's options only affect DISPLAY and NOT validation, |
|
147 | + * which is why its ok to just assume the provided $value to be in the list of acceptable values |
|
148 | + * |
|
149 | + * @param mixed $value |
|
150 | + * @return void |
|
151 | + * @throws \EE_Error |
|
152 | + */ |
|
153 | + public function _set_raw_value($value) |
|
154 | + { |
|
155 | + $values_for_options = (array) $value; |
|
156 | + $value_field = $this->_get_model()->field_settings_for($this->_value_field_name); |
|
157 | + $display_field = $this->_get_model()->field_settings_for($this->_display_field_name); |
|
158 | + $this->_extra_select_columns[] = $value_field->get_qualified_column() . ' AS ' . $this->_value_field_name; |
|
159 | + $this->_extra_select_columns[] = $display_field->get_qualified_column() . ' AS ' . $this->_display_field_name; |
|
160 | + $display_values = $this->_get_model()->get_all_wpdb_results( |
|
161 | + array( |
|
162 | + array( |
|
163 | + $this->_value_field_name => array( 'IN', $values_for_options ) |
|
164 | + ) |
|
165 | + ), |
|
166 | + ARRAY_A, |
|
167 | + implode(',', $this->_extra_select_columns) |
|
168 | + ); |
|
169 | + $select_options = array(); |
|
170 | + if (is_array($select_options)) { |
|
171 | + foreach ($display_values as $db_rows) { |
|
172 | + $db_rows = (array) $db_rows; |
|
173 | + $select_options[ $db_rows[ $this->_value_field_name ] ] = apply_filters( |
|
174 | + 'FHEE__EE_Select_Ajax_Model_Rest_Input___set_raw_value__select_option_value', |
|
175 | + $db_rows[ $this->_display_field_name ], |
|
176 | + $db_rows |
|
177 | + ); |
|
178 | + } |
|
179 | + } |
|
180 | + $this->set_select_options($select_options); |
|
181 | + parent::_set_raw_value($value); |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * Returns the model, or throws an exception if the model name provided in constructor doesn't exist |
|
186 | - * @return EEM_Base |
|
187 | - * @throws EE_Error |
|
188 | - */ |
|
189 | - protected function _get_model() |
|
190 | - { |
|
191 | - if (! EE_Registry::instance()->is_model_name($this->_model_name)) { |
|
192 | - throw new EE_Error( |
|
193 | - sprintf( |
|
194 | - __( |
|
195 | - '%1$s is not a proper model name. Please provide a model name in the "model_name" form input argument', |
|
196 | - 'event_espresso' |
|
197 | - ), |
|
198 | - $this->_model_name |
|
199 | - ) |
|
200 | - ); |
|
201 | - } else { |
|
202 | - return EE_Registry::instance()->load_model($this->_model_name); |
|
203 | - } |
|
204 | - } |
|
184 | + /** |
|
185 | + * Returns the model, or throws an exception if the model name provided in constructor doesn't exist |
|
186 | + * @return EEM_Base |
|
187 | + * @throws EE_Error |
|
188 | + */ |
|
189 | + protected function _get_model() |
|
190 | + { |
|
191 | + if (! EE_Registry::instance()->is_model_name($this->_model_name)) { |
|
192 | + throw new EE_Error( |
|
193 | + sprintf( |
|
194 | + __( |
|
195 | + '%1$s is not a proper model name. Please provide a model name in the "model_name" form input argument', |
|
196 | + 'event_espresso' |
|
197 | + ), |
|
198 | + $this->_model_name |
|
199 | + ) |
|
200 | + ); |
|
201 | + } else { |
|
202 | + return EE_Registry::instance()->load_model($this->_model_name); |
|
203 | + } |
|
204 | + } |
|
205 | 205 | } |
@@ -14,78 +14,78 @@ |
||
14 | 14 | class EE_Select_Display_Strategy extends EE_Display_Strategy_Base |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * |
|
19 | - * @throws EE_Error |
|
20 | - * @return string of html to display the field |
|
21 | - */ |
|
22 | - public function display() |
|
23 | - { |
|
24 | - if (! $this->_input instanceof EE_Form_Input_With_Options_Base) { |
|
25 | - throw new EE_Error(sprintf(__('Cannot use Select Display Strategy with an input that doesn\'t have options', 'event_espresso'))); |
|
26 | - } |
|
17 | + /** |
|
18 | + * |
|
19 | + * @throws EE_Error |
|
20 | + * @return string of html to display the field |
|
21 | + */ |
|
22 | + public function display() |
|
23 | + { |
|
24 | + if (! $this->_input instanceof EE_Form_Input_With_Options_Base) { |
|
25 | + throw new EE_Error(sprintf(__('Cannot use Select Display Strategy with an input that doesn\'t have options', 'event_espresso'))); |
|
26 | + } |
|
27 | 27 | |
28 | - $html = EEH_HTML::nl(0, 'select'); |
|
29 | - $html .= '<select'; |
|
30 | - $html .= $this->_attributes_string( |
|
31 | - $this->_standard_attributes_array() |
|
32 | - ); |
|
33 | - $html .= '>'; |
|
28 | + $html = EEH_HTML::nl(0, 'select'); |
|
29 | + $html .= '<select'; |
|
30 | + $html .= $this->_attributes_string( |
|
31 | + $this->_standard_attributes_array() |
|
32 | + ); |
|
33 | + $html .= '>'; |
|
34 | 34 | |
35 | - if (EEH_Array::is_multi_dimensional_array($this->_input->options())) { |
|
36 | - EEH_HTML::indent(1, 'optgroup'); |
|
37 | - foreach ($this->_input->options() as $opt_group_label => $opt_group) { |
|
38 | - if (! empty($opt_group_label)) { |
|
39 | - $html .= EEH_HTML::nl(0, 'optgroup') . '<optgroup label="' . esc_attr($opt_group_label) . '">'; |
|
40 | - } |
|
41 | - EEH_HTML::indent(1, 'option'); |
|
42 | - $html .= $this->_display_options($opt_group); |
|
43 | - EEH_HTML::indent(-1, 'option'); |
|
44 | - if (! empty($opt_group_label)) { |
|
45 | - $html .= EEH_HTML::nl(0, 'optgroup') . '</optgroup>'; |
|
46 | - } |
|
47 | - } |
|
48 | - EEH_HTML::indent(-1, 'optgroup'); |
|
49 | - } else { |
|
50 | - $html.=$this->_display_options($this->_input->options()); |
|
51 | - } |
|
35 | + if (EEH_Array::is_multi_dimensional_array($this->_input->options())) { |
|
36 | + EEH_HTML::indent(1, 'optgroup'); |
|
37 | + foreach ($this->_input->options() as $opt_group_label => $opt_group) { |
|
38 | + if (! empty($opt_group_label)) { |
|
39 | + $html .= EEH_HTML::nl(0, 'optgroup') . '<optgroup label="' . esc_attr($opt_group_label) . '">'; |
|
40 | + } |
|
41 | + EEH_HTML::indent(1, 'option'); |
|
42 | + $html .= $this->_display_options($opt_group); |
|
43 | + EEH_HTML::indent(-1, 'option'); |
|
44 | + if (! empty($opt_group_label)) { |
|
45 | + $html .= EEH_HTML::nl(0, 'optgroup') . '</optgroup>'; |
|
46 | + } |
|
47 | + } |
|
48 | + EEH_HTML::indent(-1, 'optgroup'); |
|
49 | + } else { |
|
50 | + $html.=$this->_display_options($this->_input->options()); |
|
51 | + } |
|
52 | 52 | |
53 | - $html.= EEH_HTML::nl(0, 'select') . '</select>'; |
|
54 | - return $html; |
|
55 | - } |
|
53 | + $html.= EEH_HTML::nl(0, 'select') . '</select>'; |
|
54 | + return $html; |
|
55 | + } |
|
56 | 56 | |
57 | 57 | |
58 | 58 | |
59 | - /** |
|
60 | - * Displays a flat list of options as option tags |
|
61 | - * @param array $options |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - protected function _display_options($options) |
|
65 | - { |
|
66 | - $html = ''; |
|
67 | - EEH_HTML::indent(1, 'option'); |
|
68 | - foreach ($options as $value => $display_text) { |
|
69 | - // even if this input uses EE_Text_Normalization if one of the array keys is a numeric string, like "123", |
|
70 | - // PHP will have converted it to a PHP integer (eg 123). So we need to make sure it's a string |
|
71 | - $unnormalized_value = $this->_input->get_normalization_strategy()->unnormalize_one($value); |
|
72 | - $selected = $this->_check_if_option_selected($unnormalized_value) ? ' selected="selected"' : ''; |
|
73 | - $html.= EEH_HTML::nl(0, 'option') . '<option value="' . esc_attr($unnormalized_value) . '"' . $selected . '>' . $display_text . '</option>'; |
|
74 | - } |
|
75 | - EEH_HTML::indent(-1, 'option'); |
|
76 | - return $html; |
|
77 | - } |
|
59 | + /** |
|
60 | + * Displays a flat list of options as option tags |
|
61 | + * @param array $options |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + protected function _display_options($options) |
|
65 | + { |
|
66 | + $html = ''; |
|
67 | + EEH_HTML::indent(1, 'option'); |
|
68 | + foreach ($options as $value => $display_text) { |
|
69 | + // even if this input uses EE_Text_Normalization if one of the array keys is a numeric string, like "123", |
|
70 | + // PHP will have converted it to a PHP integer (eg 123). So we need to make sure it's a string |
|
71 | + $unnormalized_value = $this->_input->get_normalization_strategy()->unnormalize_one($value); |
|
72 | + $selected = $this->_check_if_option_selected($unnormalized_value) ? ' selected="selected"' : ''; |
|
73 | + $html.= EEH_HTML::nl(0, 'option') . '<option value="' . esc_attr($unnormalized_value) . '"' . $selected . '>' . $display_text . '</option>'; |
|
74 | + } |
|
75 | + EEH_HTML::indent(-1, 'option'); |
|
76 | + return $html; |
|
77 | + } |
|
78 | 78 | |
79 | 79 | |
80 | 80 | |
81 | - /** |
|
82 | - * Checks if that value is the one selected |
|
83 | - * |
|
84 | - * @param string|int $option_value unnormalized value option (string). How it will appear in the HTML. |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - protected function _check_if_option_selected($option_value) |
|
88 | - { |
|
89 | - return $option_value === $this->_input->raw_value(); |
|
90 | - } |
|
81 | + /** |
|
82 | + * Checks if that value is the one selected |
|
83 | + * |
|
84 | + * @param string|int $option_value unnormalized value option (string). How it will appear in the HTML. |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + protected function _check_if_option_selected($option_value) |
|
88 | + { |
|
89 | + return $option_value === $this->_input->raw_value(); |
|
90 | + } |
|
91 | 91 | } |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | */ |
22 | 22 | public function display() |
23 | 23 | { |
24 | - if (! $this->_input instanceof EE_Form_Input_With_Options_Base) { |
|
24 | + if ( ! $this->_input instanceof EE_Form_Input_With_Options_Base) { |
|
25 | 25 | throw new EE_Error(sprintf(__('Cannot use Select Display Strategy with an input that doesn\'t have options', 'event_espresso'))); |
26 | 26 | } |
27 | 27 | |
@@ -35,22 +35,22 @@ discard block |
||
35 | 35 | if (EEH_Array::is_multi_dimensional_array($this->_input->options())) { |
36 | 36 | EEH_HTML::indent(1, 'optgroup'); |
37 | 37 | foreach ($this->_input->options() as $opt_group_label => $opt_group) { |
38 | - if (! empty($opt_group_label)) { |
|
39 | - $html .= EEH_HTML::nl(0, 'optgroup') . '<optgroup label="' . esc_attr($opt_group_label) . '">'; |
|
38 | + if ( ! empty($opt_group_label)) { |
|
39 | + $html .= EEH_HTML::nl(0, 'optgroup').'<optgroup label="'.esc_attr($opt_group_label).'">'; |
|
40 | 40 | } |
41 | 41 | EEH_HTML::indent(1, 'option'); |
42 | 42 | $html .= $this->_display_options($opt_group); |
43 | 43 | EEH_HTML::indent(-1, 'option'); |
44 | - if (! empty($opt_group_label)) { |
|
45 | - $html .= EEH_HTML::nl(0, 'optgroup') . '</optgroup>'; |
|
44 | + if ( ! empty($opt_group_label)) { |
|
45 | + $html .= EEH_HTML::nl(0, 'optgroup').'</optgroup>'; |
|
46 | 46 | } |
47 | 47 | } |
48 | 48 | EEH_HTML::indent(-1, 'optgroup'); |
49 | 49 | } else { |
50 | - $html.=$this->_display_options($this->_input->options()); |
|
50 | + $html .= $this->_display_options($this->_input->options()); |
|
51 | 51 | } |
52 | 52 | |
53 | - $html.= EEH_HTML::nl(0, 'select') . '</select>'; |
|
53 | + $html .= EEH_HTML::nl(0, 'select').'</select>'; |
|
54 | 54 | return $html; |
55 | 55 | } |
56 | 56 | |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | // PHP will have converted it to a PHP integer (eg 123). So we need to make sure it's a string |
71 | 71 | $unnormalized_value = $this->_input->get_normalization_strategy()->unnormalize_one($value); |
72 | 72 | $selected = $this->_check_if_option_selected($unnormalized_value) ? ' selected="selected"' : ''; |
73 | - $html.= EEH_HTML::nl(0, 'option') . '<option value="' . esc_attr($unnormalized_value) . '"' . $selected . '>' . $display_text . '</option>'; |
|
73 | + $html .= EEH_HTML::nl(0, 'option').'<option value="'.esc_attr($unnormalized_value).'"'.$selected.'>'.$display_text.'</option>'; |
|
74 | 74 | } |
75 | 75 | EEH_HTML::indent(-1, 'option'); |
76 | 76 | return $html; |
@@ -15,80 +15,80 @@ |
||
15 | 15 | class EE_Select2_Display_Strategy extends EE_Select_Display_Strategy |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * Arguments that will be passed into the select2 javascript constructor |
|
20 | - * @var array |
|
21 | - */ |
|
22 | - protected $_select2_js_args = array(); |
|
18 | + /** |
|
19 | + * Arguments that will be passed into the select2 javascript constructor |
|
20 | + * @var array |
|
21 | + */ |
|
22 | + protected $_select2_js_args = array(); |
|
23 | 23 | |
24 | - /** |
|
25 | - * |
|
26 | - * @param array $select2_js_args pass in the EXACT array of JS arguments you want |
|
27 | - * to pass into the select2 js/html input. See https://select2.github.io |
|
28 | - */ |
|
29 | - public function __construct($select2_js_args = array()) |
|
30 | - { |
|
31 | - $this->_select2_js_args = $select2_js_args; |
|
32 | - parent::__construct(); |
|
33 | - } |
|
24 | + /** |
|
25 | + * |
|
26 | + * @param array $select2_js_args pass in the EXACT array of JS arguments you want |
|
27 | + * to pass into the select2 js/html input. See https://select2.github.io |
|
28 | + */ |
|
29 | + public function __construct($select2_js_args = array()) |
|
30 | + { |
|
31 | + $this->_select2_js_args = $select2_js_args; |
|
32 | + parent::__construct(); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * enqueues the select2 initializing js (which depends on the select2 js) and |
|
37 | - * the select2 css |
|
38 | - */ |
|
39 | - public function enqueue_js() |
|
40 | - { |
|
41 | - // need to first deregister the select2 script in case some other plugin **cough cough Toolset Types cough** |
|
42 | - // is carelessly registering an older version of Select2 on admin pages that don't even belong to them |
|
43 | - wp_deregister_script('select2'); |
|
44 | - wp_deregister_style('select2'); |
|
45 | - wp_register_script('select2', EE_GLOBAL_ASSETS_URL . 'scripts/select2.min.js', array(), '4.0.2', true); |
|
46 | - wp_register_style('select2', EE_GLOBAL_ASSETS_URL . 'css/select2.min.css', array(), '4.0.2', 'all'); |
|
47 | - wp_enqueue_script('form_section_select2_init', EE_GLOBAL_ASSETS_URL . 'scripts/form_section_select2_init.js', array( 'select2' ), '1.0.0', true); |
|
48 | - wp_enqueue_style('select2', EE_GLOBAL_ASSETS_URL . 'css/select2.min.css', array(), '4.0.2', 'all'); |
|
49 | - } |
|
35 | + /** |
|
36 | + * enqueues the select2 initializing js (which depends on the select2 js) and |
|
37 | + * the select2 css |
|
38 | + */ |
|
39 | + public function enqueue_js() |
|
40 | + { |
|
41 | + // need to first deregister the select2 script in case some other plugin **cough cough Toolset Types cough** |
|
42 | + // is carelessly registering an older version of Select2 on admin pages that don't even belong to them |
|
43 | + wp_deregister_script('select2'); |
|
44 | + wp_deregister_style('select2'); |
|
45 | + wp_register_script('select2', EE_GLOBAL_ASSETS_URL . 'scripts/select2.min.js', array(), '4.0.2', true); |
|
46 | + wp_register_style('select2', EE_GLOBAL_ASSETS_URL . 'css/select2.min.css', array(), '4.0.2', 'all'); |
|
47 | + wp_enqueue_script('form_section_select2_init', EE_GLOBAL_ASSETS_URL . 'scripts/form_section_select2_init.js', array( 'select2' ), '1.0.0', true); |
|
48 | + wp_enqueue_style('select2', EE_GLOBAL_ASSETS_URL . 'css/select2.min.css', array(), '4.0.2', 'all'); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Gets the javascript args which will be localized and passed into the select2 js/html input |
|
53 | - * @return array |
|
54 | - */ |
|
55 | - public function get_js_args() |
|
56 | - { |
|
57 | - return $this->_select2_js_args; |
|
58 | - } |
|
51 | + /** |
|
52 | + * Gets the javascript args which will be localized and passed into the select2 js/html input |
|
53 | + * @return array |
|
54 | + */ |
|
55 | + public function get_js_args() |
|
56 | + { |
|
57 | + return $this->_select2_js_args; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Sets the exact js args which will be passed into the select2 js/html input |
|
62 | - * @param array $js_args |
|
63 | - */ |
|
64 | - public function set_js_args($js_args) |
|
65 | - { |
|
66 | - $this->_select2_js_args = $js_args; |
|
67 | - } |
|
60 | + /** |
|
61 | + * Sets the exact js args which will be passed into the select2 js/html input |
|
62 | + * @param array $js_args |
|
63 | + */ |
|
64 | + public function set_js_args($js_args) |
|
65 | + { |
|
66 | + $this->_select2_js_args = $js_args; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Adds select2 data for localization |
|
71 | - * @param array $other_js_data |
|
72 | - * @return array |
|
73 | - */ |
|
74 | - public function get_other_js_data($other_js_data = array()) |
|
75 | - { |
|
76 | - $other_js_data = parent::get_other_js_data($other_js_data); |
|
77 | - if (! isset($other_js_data['select2s'])) { |
|
78 | - $other_js_data['select2s'] = array(); |
|
79 | - } |
|
80 | - $other_js_data['select2s'][ $this->_input->html_id() ] = $this->get_js_args(); |
|
81 | - return $other_js_data; |
|
82 | - } |
|
69 | + /** |
|
70 | + * Adds select2 data for localization |
|
71 | + * @param array $other_js_data |
|
72 | + * @return array |
|
73 | + */ |
|
74 | + public function get_other_js_data($other_js_data = array()) |
|
75 | + { |
|
76 | + $other_js_data = parent::get_other_js_data($other_js_data); |
|
77 | + if (! isset($other_js_data['select2s'])) { |
|
78 | + $other_js_data['select2s'] = array(); |
|
79 | + } |
|
80 | + $other_js_data['select2s'][ $this->_input->html_id() ] = $this->get_js_args(); |
|
81 | + return $other_js_data; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Overrides standard attributes array to add the CSS class "ee-select2" |
|
86 | - * @return array |
|
87 | - */ |
|
88 | - protected function _standard_attributes_array() |
|
89 | - { |
|
90 | - $standard_attributes = parent::_standard_attributes_array(); |
|
91 | - $standard_attributes['class'] .= ' ee-select2'; |
|
92 | - return $standard_attributes; |
|
93 | - } |
|
84 | + /** |
|
85 | + * Overrides standard attributes array to add the CSS class "ee-select2" |
|
86 | + * @return array |
|
87 | + */ |
|
88 | + protected function _standard_attributes_array() |
|
89 | + { |
|
90 | + $standard_attributes = parent::_standard_attributes_array(); |
|
91 | + $standard_attributes['class'] .= ' ee-select2'; |
|
92 | + return $standard_attributes; |
|
93 | + } |
|
94 | 94 | } |
@@ -42,10 +42,10 @@ discard block |
||
42 | 42 | // is carelessly registering an older version of Select2 on admin pages that don't even belong to them |
43 | 43 | wp_deregister_script('select2'); |
44 | 44 | wp_deregister_style('select2'); |
45 | - wp_register_script('select2', EE_GLOBAL_ASSETS_URL . 'scripts/select2.min.js', array(), '4.0.2', true); |
|
46 | - wp_register_style('select2', EE_GLOBAL_ASSETS_URL . 'css/select2.min.css', array(), '4.0.2', 'all'); |
|
47 | - wp_enqueue_script('form_section_select2_init', EE_GLOBAL_ASSETS_URL . 'scripts/form_section_select2_init.js', array( 'select2' ), '1.0.0', true); |
|
48 | - wp_enqueue_style('select2', EE_GLOBAL_ASSETS_URL . 'css/select2.min.css', array(), '4.0.2', 'all'); |
|
45 | + wp_register_script('select2', EE_GLOBAL_ASSETS_URL.'scripts/select2.min.js', array(), '4.0.2', true); |
|
46 | + wp_register_style('select2', EE_GLOBAL_ASSETS_URL.'css/select2.min.css', array(), '4.0.2', 'all'); |
|
47 | + wp_enqueue_script('form_section_select2_init', EE_GLOBAL_ASSETS_URL.'scripts/form_section_select2_init.js', array('select2'), '1.0.0', true); |
|
48 | + wp_enqueue_style('select2', EE_GLOBAL_ASSETS_URL.'css/select2.min.css', array(), '4.0.2', 'all'); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | /** |
@@ -74,10 +74,10 @@ discard block |
||
74 | 74 | public function get_other_js_data($other_js_data = array()) |
75 | 75 | { |
76 | 76 | $other_js_data = parent::get_other_js_data($other_js_data); |
77 | - if (! isset($other_js_data['select2s'])) { |
|
77 | + if ( ! isset($other_js_data['select2s'])) { |
|
78 | 78 | $other_js_data['select2s'] = array(); |
79 | 79 | } |
80 | - $other_js_data['select2s'][ $this->_input->html_id() ] = $this->get_js_args(); |
|
80 | + $other_js_data['select2s'][$this->_input->html_id()] = $this->get_js_args(); |
|
81 | 81 | return $other_js_data; |
82 | 82 | } |
83 | 83 |
@@ -15,886 +15,886 @@ |
||
15 | 15 | abstract class EE_Data_Migration_Script_Base extends EE_Data_Migration_Class_Base |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * Set by client code to indicate this DMS is being ran as part of a proper migration, |
|
20 | - * instead of being used to merely setup (or verify) the database structure. |
|
21 | - * Defaults to TRUE, so client code that's NOT using this DMS as part of a proper migration |
|
22 | - * should call EE_Data_Migration_Script_Base::set_migrating( FALSE ) |
|
23 | - * |
|
24 | - * @var boolean |
|
25 | - */ |
|
26 | - protected $_migrating = true; |
|
27 | - |
|
28 | - /** |
|
29 | - * numerically-indexed array where each value is EE_Data_Migration_Script_Stage object |
|
30 | - * |
|
31 | - * @var EE_Data_Migration_Script_Stage[] $migration_functions |
|
32 | - */ |
|
33 | - protected $_migration_stages = array(); |
|
34 | - |
|
35 | - /** |
|
36 | - * Indicates we've already ran the schema changes that needed to happen BEFORE the data migration |
|
37 | - * |
|
38 | - * @var boolean |
|
39 | - */ |
|
40 | - protected $_schema_changes_before_migration_ran = null; |
|
41 | - |
|
42 | - /** |
|
43 | - * Indicates we've already ran the schema changes that needed to happen AFTER the data migration |
|
44 | - * |
|
45 | - * @var boolean |
|
46 | - */ |
|
47 | - protected $_schema_changes_after_migration_ran = null; |
|
48 | - |
|
49 | - /** |
|
50 | - * String which describes what's currently happening in this migration |
|
51 | - * |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $_feedback_message; |
|
55 | - |
|
56 | - /** |
|
57 | - * Indicates the script's priority. Like wp's add_action and add_filter, lower numbers |
|
58 | - * correspond to earlier execution |
|
59 | - * |
|
60 | - * @var int |
|
61 | - */ |
|
62 | - protected $_priority = 5; |
|
63 | - |
|
64 | - /** |
|
65 | - * Multi-dimensional array that defines the mapping from OLD table Primary Keys |
|
66 | - * to NEW table Primary Keys. |
|
67 | - * Top-level array keys are OLD table names (minus the "wp_" part), |
|
68 | - * 2nd-level array keys are NEW table names (again, minus the "wp_" part), |
|
69 | - * 3rd-level array keys are the OLD table primary keys |
|
70 | - * and 3rd-level array values are the NEW table primary keys |
|
71 | - * |
|
72 | - * @var array |
|
73 | - */ |
|
74 | - protected $_mappings = array(); |
|
75 | - |
|
76 | - /** |
|
77 | - * @var EE_Data_Migration_Script_Base |
|
78 | - */ |
|
79 | - protected $previous_dms; |
|
80 | - |
|
81 | - |
|
82 | - /** |
|
83 | - * Returns whether or not this data migration script can operate on the given version of the database. |
|
84 | - * Eg, if this migration script can migrate from 3.1.26 or higher (but not anything after 4.0.0), and |
|
85 | - * it's passed a string like '3.1.38B', it should return true. |
|
86 | - * If this DMS is to migrate data from an EE3 addon, you will probably want to use |
|
87 | - * EventEspresso\core\services\database\TableAnalysis::tableExists() to check for old EE3 tables, and |
|
88 | - * EE_Data_Migration_Manager::get_migration_ran() to check that core was already |
|
89 | - * migrated from EE3 to EE4 (ie, this DMS probably relies on some migration data generated |
|
90 | - * during the Core 4.1.0 DMS. If core didn't run that DMS, you probably don't want |
|
91 | - * to run this DMS). |
|
92 | - * If this DMS migrates data from a previous version of this EE4 addon, just |
|
93 | - * comparing $current_database_state_of[ $this->slug() ] will probably suffice. |
|
94 | - * If this DMS should never migrate data, because it's only used to define the initial |
|
95 | - * database state, just return FALSE (and core's activation process will take care |
|
96 | - * of calling its schema_changes_before_migration() and |
|
97 | - * schema_changes_after_migration() for you. ) |
|
98 | - * |
|
99 | - * @param array $current_database_state_of keys are EE plugin slugs (eg 'Core', 'Calendar', 'Mailchimp', etc) |
|
100 | - * @return boolean |
|
101 | - */ |
|
102 | - abstract public function can_migrate_from_version($current_database_state_of); |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * Performs database schema changes that need to occur BEFORE the data is migrated. |
|
107 | - * Eg, if we were going to change user passwords from plaintext to encoded versions |
|
108 | - * during this migration, this would probably add a new column called something like |
|
109 | - * "encoded_password". |
|
110 | - * |
|
111 | - * @return boolean of success |
|
112 | - */ |
|
113 | - abstract public function schema_changes_before_migration(); |
|
114 | - |
|
115 | - |
|
116 | - /** |
|
117 | - * Performs the database schema changes that need to occur AFTER the data has been migrated. |
|
118 | - * Usually this will mean we'll be removing old columns. Eg, if we were changing passwords |
|
119 | - * from plaintext to encoded versions, and we had added a column called "encoded_password", |
|
120 | - * this function would probably remove the old column "password" (which still holds the plaintext password) |
|
121 | - * and possibly rename "encoded_password" to "password" |
|
122 | - * |
|
123 | - * @return boolean of success |
|
124 | - */ |
|
125 | - abstract public function schema_changes_after_migration(); |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * All children of this must call parent::__construct() |
|
130 | - * at the end of their constructor or suffer the consequences! |
|
131 | - * |
|
132 | - * @param TableManager $table_manager |
|
133 | - * @param TableAnalysis $table_analysis |
|
134 | - */ |
|
135 | - public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null) |
|
136 | - { |
|
137 | - $this->_migration_stages = (array) apply_filters( |
|
138 | - 'FHEE__' . get_class($this) . '__construct__migration_stages', |
|
139 | - $this->_migration_stages |
|
140 | - ); |
|
141 | - foreach ($this->_migration_stages as $migration_stage) { |
|
142 | - if ($migration_stage instanceof EE_Data_Migration_Script_Stage) { |
|
143 | - $migration_stage->_construct_finalize($this); |
|
144 | - } |
|
145 | - } |
|
146 | - parent::__construct($table_manager, $table_analysis); |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * Place to add hooks and filters for tweaking the migrations page, in order |
|
152 | - * to customize it |
|
153 | - */ |
|
154 | - public function migration_page_hooks() |
|
155 | - { |
|
156 | - // by default none are added because we normally like the default look of the migration page |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * Sets the mapping from old table primary keys to new table primary keys. |
|
162 | - * This mapping is automatically persisted as a property on the migration |
|
163 | - * |
|
164 | - * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
165 | - * @param int|string $old_pk old primary key. Eg events_detail.id's value |
|
166 | - * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
167 | - * @param int|string $new_pk eg posts.ID |
|
168 | - * @return void |
|
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 | - */ |
|
189 | - public function get_mapping_new_pk($old_table, $old_pk, $new_table) |
|
190 | - { |
|
191 | - if (! isset($this->_mappings[ $old_table ]) || |
|
192 | - ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
193 | - // try fetching the option |
|
194 | - $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
195 | - } |
|
196 | - return isset($this->_mappings[ $old_table ][ $new_table ][ $old_pk ]) |
|
197 | - ? $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] : null; |
|
198 | - } |
|
199 | - |
|
200 | - |
|
201 | - /** |
|
202 | - * Gets the old primary key, if provided with the OLD table, |
|
203 | - * and the new table and the primary key of an item in the new table |
|
204 | - * |
|
205 | - * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
206 | - * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
207 | - * @param mixed $new_pk |
|
208 | - * @return mixed |
|
209 | - */ |
|
210 | - public function get_mapping_old_pk($old_table, $new_table, $new_pk) |
|
211 | - { |
|
212 | - if (! isset($this->_mappings[ $old_table ]) || |
|
213 | - ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
214 | - // try fetching the option |
|
215 | - $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
216 | - } |
|
217 | - if (isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
218 | - $new_pk_to_old_pk = array_flip($this->_mappings[ $old_table ][ $new_table ]); |
|
219 | - if (isset($new_pk_to_old_pk[ $new_pk ])) { |
|
220 | - return $new_pk_to_old_pk[ $new_pk ]; |
|
221 | - } |
|
222 | - } |
|
223 | - return null; |
|
224 | - } |
|
225 | - |
|
226 | - |
|
227 | - /** |
|
228 | - * Gets the mapping array option specified by the table names |
|
229 | - * |
|
230 | - * @param string $old_table_name |
|
231 | - * @param string $new_table_name |
|
232 | - * @return array |
|
233 | - */ |
|
234 | - protected function _get_mapping_option($old_table_name, $new_table_name) |
|
235 | - { |
|
236 | - $option = get_option($this->_get_mapping_option_name($old_table_name, $new_table_name), array()); |
|
237 | - return $option; |
|
238 | - } |
|
239 | - |
|
240 | - |
|
241 | - /** |
|
242 | - * Updates the mapping option specified by the table names with the array provided |
|
243 | - * |
|
244 | - * @param string $old_table_name |
|
245 | - * @param string $new_table_name |
|
246 | - * @param array $mapping_array |
|
247 | - * @return boolean success of updating option |
|
248 | - */ |
|
249 | - protected function _set_mapping_option($old_table_name, $new_table_name, $mapping_array) |
|
250 | - { |
|
251 | - $success = update_option($this->_get_mapping_option_name($old_table_name, $new_table_name), $mapping_array, false); |
|
252 | - return $success; |
|
253 | - } |
|
254 | - |
|
255 | - |
|
256 | - /** |
|
257 | - * Gets the option name for this script to map from $old_table_name to $new_table_name |
|
258 | - * |
|
259 | - * @param string $old_table_name |
|
260 | - * @param string $new_table_name |
|
261 | - * @return string |
|
262 | - */ |
|
263 | - protected function _get_mapping_option_name($old_table_name, $new_table_name) |
|
264 | - { |
|
265 | - global $wpdb; |
|
266 | - $old_table_name_sans_wp = str_replace($wpdb->prefix, "", $old_table_name); |
|
267 | - $new_table_name_sans_wp = str_replace($wpdb->prefix, "", $new_table_name); |
|
268 | - $migrates_to = EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
|
269 | - return substr( |
|
270 | - 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, |
|
271 | - 0, |
|
272 | - 64 |
|
273 | - ); |
|
274 | - } |
|
275 | - |
|
276 | - |
|
277 | - /** |
|
278 | - * Counts all the records that will be migrated during this data migration. |
|
279 | - * For example, if we were changing old user passwords from plaintext to encoded versions, |
|
280 | - * this would be a count of all users who have passwords. If we were going to also split |
|
281 | - * attendee records into transactions, registrations, and attendee records, this would include |
|
282 | - * the count of all attendees currently in existence in the DB (ie, users + attendees). |
|
283 | - * If you can't determine how many records there are to migrate, just provide a guess: this |
|
284 | - * number will only be used in calculating the percent complete. If you estimate there to be |
|
285 | - * 100 records to migrate, and it turns out there's 120, we'll just show the migration as being at |
|
286 | - * 99% until the function "migration_step" returns EE_Data_Migration_Script_Base::status_complete. |
|
287 | - * |
|
288 | - * @return int |
|
289 | - */ |
|
290 | - protected function _count_records_to_migrate() |
|
291 | - { |
|
292 | - $count = 0; |
|
293 | - foreach ($this->stages() as $stage) { |
|
294 | - $count += $stage->count_records_to_migrate(); |
|
295 | - } |
|
296 | - return $count; |
|
297 | - } |
|
298 | - |
|
299 | - |
|
300 | - /** |
|
301 | - * Returns the number of records updated so far. Usually this is easiest to do |
|
302 | - * by just setting a transient and updating it after each migration_step |
|
303 | - * |
|
304 | - * @return int |
|
305 | - */ |
|
306 | - public function count_records_migrated() |
|
307 | - { |
|
308 | - $count = 0; |
|
309 | - foreach ($this->stages() as $stage) { |
|
310 | - $count += $stage->count_records_migrated(); |
|
311 | - } |
|
312 | - $this->_records_migrated = $count; |
|
313 | - return $count; |
|
314 | - } |
|
315 | - |
|
316 | - |
|
317 | - /** |
|
318 | - * @param int $num_records_to_migrate_limit |
|
319 | - * @return int |
|
320 | - * @throws EE_Error |
|
321 | - * @throws Exception |
|
322 | - */ |
|
323 | - public function migration_step($num_records_to_migrate_limit) |
|
324 | - { |
|
325 | - // reset the feedback message |
|
326 | - $this->_feedback_message = ''; |
|
327 | - // if we haven't yet done the 1st schema changes, do them now. buffer any output |
|
328 | - $this->_maybe_do_schema_changes(true); |
|
329 | - |
|
330 | - $num_records_actually_migrated = 0; |
|
331 | - $records_migrated_per_stage = array(); |
|
332 | - // setup the 'stage' variable, which should hold the last run stage of the migration (or none at all if nothing runs) |
|
333 | - $stage = null; |
|
334 | - // get the next stage that isn't complete |
|
335 | - foreach ($this->stages() as $stage) { |
|
336 | - if ($stage->get_status() == EE_Data_Migration_Manager::status_continue) { |
|
337 | - try { |
|
338 | - $records_migrated_during_stage = $stage->migration_step( |
|
339 | - $num_records_to_migrate_limit - $num_records_actually_migrated |
|
340 | - ); |
|
341 | - $num_records_actually_migrated += $records_migrated_during_stage; |
|
342 | - $records_migrated_per_stage[ $stage->pretty_name() ] = $records_migrated_during_stage; |
|
343 | - } catch (Exception $e) { |
|
344 | - // yes if we catch an exception here, we consider that migration stage borked. |
|
345 | - $stage->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
346 | - $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
347 | - $stage->add_error($e->getMessage() . ". Stack-trace:" . $e->getTraceAsString()); |
|
348 | - throw $e; |
|
349 | - } |
|
350 | - // check that the migration stage didn't mark itself as having a fatal error |
|
351 | - if ($stage->is_broken()) { |
|
352 | - $this->set_broken(); |
|
353 | - throw new EE_Error($stage->get_last_error()); |
|
354 | - } |
|
355 | - } |
|
356 | - // once we've migrated all the number we intended to (possibly from different stages), stop migrating |
|
357 | - // or if we had a fatal error |
|
358 | - // or if the current script stopped early- its not done, but it's done all it thinks we should do on this step |
|
359 | - if ($num_records_actually_migrated >= $num_records_to_migrate_limit |
|
360 | - || $stage->is_broken() |
|
361 | - || $stage->has_more_to_do() |
|
362 | - ) { |
|
363 | - break; |
|
364 | - } |
|
365 | - } |
|
366 | - // check if we're all done this data migration... |
|
367 | - // which is indicated by being done early AND the last stage claims to be done |
|
368 | - if ($stage == null) { |
|
369 | - // this migration script apparently has NO stages... which is super weird, but whatever |
|
370 | - $this->set_completed(); |
|
371 | - $this->_maybe_do_schema_changes(false); |
|
372 | - } elseif ($num_records_actually_migrated < $num_records_to_migrate_limit && ! $stage->has_more_to_do()) { |
|
373 | - // apparently we're done, because we couldn't migrate the number we intended to |
|
374 | - $this->set_completed(); |
|
375 | - $this->_update_feedback_message(array_reverse($records_migrated_per_stage)); |
|
376 | - // do schema changes for after the migration now |
|
377 | - // first double-check we haven't already done this |
|
378 | - $this->_maybe_do_schema_changes(false); |
|
379 | - } else { |
|
380 | - // update feedback message, keeping in mind that we show them with the most recent at the top |
|
381 | - $this->_update_feedback_message(array_reverse($records_migrated_per_stage)); |
|
382 | - } |
|
383 | - return $num_records_actually_migrated; |
|
384 | - } |
|
385 | - |
|
386 | - |
|
387 | - /** |
|
388 | - * Updates the feedback message according to what was done during this migration stage. |
|
389 | - * |
|
390 | - * @param array $records_migrated_per_stage KEYS are pretty names for each stage; values are the count of records |
|
391 | - * migrated from that stage |
|
392 | - * @return void |
|
393 | - */ |
|
394 | - private function _update_feedback_message($records_migrated_per_stage) |
|
395 | - { |
|
396 | - $feedback_message_array = array(); |
|
397 | - foreach ($records_migrated_per_stage as $migration_stage_name => $num_records_migrated) { |
|
398 | - $feedback_message_array[] = sprintf( |
|
399 | - __("Migrated %d records successfully during %s", "event_espresso"), |
|
400 | - $num_records_migrated, |
|
401 | - $migration_stage_name |
|
402 | - ); |
|
403 | - } |
|
404 | - $this->_feedback_message .= implode("<br>", $feedback_message_array); |
|
405 | - } |
|
406 | - |
|
407 | - |
|
408 | - /** |
|
409 | - * Calls either schema_changes_before_migration() (if $before==true) or schema_changes_after_migration |
|
410 | - * (if $before==false). Buffers their outputs and stores them on the class. |
|
411 | - * |
|
412 | - * @param boolean $before |
|
413 | - * @throws Exception |
|
414 | - * @return void |
|
415 | - */ |
|
416 | - private function _maybe_do_schema_changes($before = true) |
|
417 | - { |
|
418 | - // so this property will be either _schema_changes_after_migration_ran or _schema_changes_before_migration_ran |
|
419 | - $property_name = '_schema_changes_' . ($before ? 'before' : 'after') . '_migration_ran'; |
|
420 | - if (! $this->{$property_name}) { |
|
421 | - try { |
|
422 | - ob_start(); |
|
423 | - if ($before) { |
|
424 | - $this->schema_changes_before_migration(); |
|
425 | - } else { |
|
426 | - $this->schema_changes_after_migration(); |
|
427 | - } |
|
428 | - $output = ob_get_contents(); |
|
429 | - ob_end_clean(); |
|
430 | - } catch (Exception $e) { |
|
431 | - $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
432 | - throw $e; |
|
433 | - } |
|
434 | - // record that we've done these schema changes |
|
435 | - $this->{$property_name} = true; |
|
436 | - // if there were any warnings etc, record them as non-fatal errors |
|
437 | - if ($output) { |
|
438 | - // there were some warnings |
|
439 | - $this->_errors[] = $output; |
|
440 | - } |
|
441 | - } |
|
442 | - } |
|
443 | - |
|
444 | - |
|
445 | - /** |
|
446 | - * Wrapper for EEH_Activation::create_table. However, takes into account the request type when |
|
447 | - * deciding what to pass for its 4th arg, $drop_pre_existing_tables. Using this function, instead |
|
448 | - * of _table_should_exist_previously, indicates that this table should be new to the EE version being migrated to |
|
449 | - * or |
|
450 | - * activated currently. If this is a brand new activation or a migration, and we're indicating this table should |
|
451 | - * not |
|
452 | - * previously exist, then we want to set $drop_pre_existing_tables to TRUE (ie, we shouldn't discover that this |
|
453 | - * table exists in the DB in EEH_Activation::create_table- if it DOES exist, something's wrong and the old table |
|
454 | - * should be nuked. |
|
455 | - * |
|
456 | - * Just for a bit of context, the migration script's db_schema_changes_* methods |
|
457 | - * are called basically in 3 cases: on brand new activation of EE4 (ie no previous version of EE existed and the |
|
458 | - * plugin is being activated and we want to add all the brand new tables), upon reactivation of EE4 (it was |
|
459 | - * deactivated and then reactivated, in which case we want to just verify the DB structure is ok) that table should |
|
460 | - * be dropped), and during a migration when we're moving the DB to the state of the migration script |
|
461 | - * |
|
462 | - * @param string $table_name |
|
463 | - * @param string $table_definition_sql |
|
464 | - * @param string $engine_string |
|
465 | - */ |
|
466 | - protected function _table_is_new_in_this_version( |
|
467 | - $table_name, |
|
468 | - $table_definition_sql, |
|
469 | - $engine_string = 'ENGINE=InnoDB ' |
|
470 | - ) { |
|
471 | - $this->_create_table_and_catch_errors( |
|
472 | - $table_name, |
|
473 | - $table_definition_sql, |
|
474 | - $engine_string, |
|
475 | - $this->_pre_existing_table_should_be_dropped(true) |
|
476 | - ); |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Like _table_is_new_in_this_version and _table_should_exist_previously, this function verifies the given table |
|
481 | - * exists. But we understand that this table has CHANGED in this version since the previous version. So it's not |
|
482 | - * completely new, but it's different. So we need to treat it like a new table in terms of verifying it's schema is |
|
483 | - * correct on activations, migrations, upgrades; but if it exists when it shouldn't, we need to be as lenient as |
|
484 | - * _table_should_exist_previously. |
|
485 | - * 8656]{Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the |
|
486 | - * table shouldn't exist). |
|
487 | - * |
|
488 | - * @param string $table_name |
|
489 | - * @param string $table_definition_sql |
|
490 | - * @param string $engine_string |
|
491 | - */ |
|
492 | - protected function _table_is_changed_in_this_version( |
|
493 | - $table_name, |
|
494 | - $table_definition_sql, |
|
495 | - $engine_string = 'ENGINE=MyISAM' |
|
496 | - ) { |
|
497 | - $this->_create_table_and_catch_errors( |
|
498 | - $table_name, |
|
499 | - $table_definition_sql, |
|
500 | - $engine_string, |
|
501 | - $this->_pre_existing_table_should_be_dropped(false) |
|
502 | - ); |
|
503 | - } |
|
504 | - |
|
505 | - |
|
506 | - /** |
|
507 | - * _old_table_exists |
|
508 | - * returns TRUE if the requested table exists in the current database |
|
509 | - * |
|
510 | - * @param string $table_name |
|
511 | - * @return boolean |
|
512 | - */ |
|
513 | - protected function _old_table_exists($table_name) |
|
514 | - { |
|
515 | - return $this->_get_table_analysis()->tableExists($table_name); |
|
516 | - } |
|
517 | - |
|
518 | - |
|
519 | - /** |
|
520 | - * _delete_table_if_empty |
|
521 | - * returns TRUE if the requested table was empty and successfully empty |
|
522 | - * |
|
523 | - * @param string $table_name |
|
524 | - * @return boolean |
|
525 | - */ |
|
526 | - protected function _delete_table_if_empty($table_name) |
|
527 | - { |
|
528 | - return EEH_Activation::delete_db_table_if_empty($table_name); |
|
529 | - } |
|
530 | - |
|
531 | - |
|
532 | - /** |
|
533 | - * It is preferred to use _table_has_not_changed_since_previous or _table_is_changed_in_this_version |
|
534 | - * as these are significantly more efficient or explicit. |
|
535 | - * Please see description of _table_is_new_in_this_version. This function will only set |
|
536 | - * EEH_Activation::create_table's $drop_pre_existing_tables to TRUE if it's a brand |
|
537 | - * new activation. ie, a more accurate name for this method would be "_table_added_previously_by_this_plugin" |
|
538 | - * because the table will be cleared out if this is a new activation (ie, if its a new activation, it actually |
|
539 | - * should exist previously). Otherwise, we'll always set $drop_pre_existing_tables to FALSE because the table |
|
540 | - * should have existed. Note, if the table is being MODIFIED in this version being activated or migrated to, then |
|
541 | - * you want _table_is_changed_in_this_version NOT this one. We don't check this table's structure during migrations |
|
542 | - * because apparently it hasn't changed since the previous one, right? |
|
543 | - * |
|
544 | - * @param string $table_name |
|
545 | - * @param string $table_definition_sql |
|
546 | - * @param string $engine_string |
|
547 | - */ |
|
548 | - protected function _table_should_exist_previously( |
|
549 | - $table_name, |
|
550 | - $table_definition_sql, |
|
551 | - $engine_string = 'ENGINE=MyISAM' |
|
552 | - ) { |
|
553 | - $this->_create_table_and_catch_errors( |
|
554 | - $table_name, |
|
555 | - $table_definition_sql, |
|
556 | - $engine_string, |
|
557 | - $this->_pre_existing_table_should_be_dropped(false) |
|
558 | - ); |
|
559 | - } |
|
560 | - |
|
561 | - /** |
|
562 | - * Exactly the same as _table_should_exist_previously(), except if this migration script is currently doing |
|
563 | - * a migration, we skip checking this table's structure in the database and just assume it's correct. |
|
564 | - * So this is useful only to improve efficiency when doing migrations (not a big deal for single site installs, |
|
565 | - * but important for multisite where migrations can take a very long time otherwise). |
|
566 | - * If the table is known to have changed since previous version, use _table_is_changed_in_this_version(). |
|
567 | - * Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the table |
|
568 | - * shouldn't exist). |
|
569 | - * |
|
570 | - * @param string $table_name |
|
571 | - * @param string $table_definition_sql |
|
572 | - * @param string $engine_string |
|
573 | - */ |
|
574 | - protected function _table_has_not_changed_since_previous( |
|
575 | - $table_name, |
|
576 | - $table_definition_sql, |
|
577 | - $engine_string = 'ENGINE=MyISAM' |
|
578 | - ) { |
|
579 | - if ($this->_currently_migrating()) { |
|
580 | - // if we're doing a migration, and this table apparently already exists, then we don't need do anything right? |
|
581 | - return; |
|
582 | - } |
|
583 | - $this->_create_table_and_catch_errors( |
|
584 | - $table_name, |
|
585 | - $table_definition_sql, |
|
586 | - $engine_string, |
|
587 | - $this->_pre_existing_table_should_be_dropped(false) |
|
588 | - ); |
|
589 | - } |
|
590 | - |
|
591 | - /** |
|
592 | - * Returns whether or not this migration script is being used as part of an actual migration |
|
593 | - * |
|
594 | - * @return boolean |
|
595 | - */ |
|
596 | - protected function _currently_migrating() |
|
597 | - { |
|
598 | - // 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) |
|
599 | - return $this->_migrating && |
|
600 | - $this->can_migrate_from_version( |
|
601 | - EE_Data_Migration_Manager::instance()->ensure_current_database_state_is_set() |
|
602 | - ) && |
|
603 | - EE_Maintenance_Mode::instance()->real_level() == EE_Maintenance_Mode::level_2_complete_maintenance; |
|
604 | - } |
|
605 | - |
|
606 | - /** |
|
607 | - * Determines if a table should be dropped, based on whether it's reported to be new in $table_is_new, |
|
608 | - * and the plugin's request type. |
|
609 | - * Assumes only this plugin could have added the table (ie, if its a new activation of this plugin, the table |
|
610 | - * shouldn't exist no matter what). |
|
611 | - * |
|
612 | - * @param boolean $table_is_new |
|
613 | - * @return boolean |
|
614 | - */ |
|
615 | - protected function _pre_existing_table_should_be_dropped($table_is_new) |
|
616 | - { |
|
617 | - if ($table_is_new) { |
|
618 | - if ($this->_get_req_type_for_plugin_corresponding_to_this_dms() == EE_System::req_type_new_activation |
|
619 | - || $this->_currently_migrating() |
|
620 | - ) { |
|
621 | - return true; |
|
622 | - } else { |
|
623 | - return false; |
|
624 | - } |
|
625 | - } else { |
|
626 | - if (in_array( |
|
627 | - $this->_get_req_type_for_plugin_corresponding_to_this_dms(), |
|
628 | - array(EE_System::req_type_new_activation) |
|
629 | - )) { |
|
630 | - return true; |
|
631 | - } else { |
|
632 | - return false; |
|
633 | - } |
|
634 | - } |
|
635 | - } |
|
636 | - |
|
637 | - /** |
|
638 | - * Just wraps EEH_Activation::create_table, but catches any errors it may throw and adds them as errors on the DMS |
|
639 | - * |
|
640 | - * @param string $table_name |
|
641 | - * @param string $table_definition_sql |
|
642 | - * @param string $engine_string |
|
643 | - * @param boolean $drop_pre_existing_tables |
|
644 | - */ |
|
645 | - private function _create_table_and_catch_errors( |
|
646 | - $table_name, |
|
647 | - $table_definition_sql, |
|
648 | - $engine_string = 'ENGINE=MyISAM', |
|
649 | - $drop_pre_existing_tables = false |
|
650 | - ) { |
|
651 | - try { |
|
652 | - EEH_Activation::create_table($table_name, $table_definition_sql, $engine_string, $drop_pre_existing_tables); |
|
653 | - } catch (EE_Error $e) { |
|
654 | - $message = $e->getMessage() . '<br>Stack Trace:' . $e->getTraceAsString(); |
|
655 | - $this->add_error($message); |
|
656 | - $this->_feedback_message .= $message; |
|
657 | - } |
|
658 | - } |
|
659 | - |
|
660 | - |
|
661 | - /** |
|
662 | - * Gets the request type for the plugin (core or addon) that corresponds to this DMS |
|
663 | - * |
|
664 | - * @return int one of EE_System::_req_type_* constants |
|
665 | - * @throws EE_Error |
|
666 | - */ |
|
667 | - private function _get_req_type_for_plugin_corresponding_to_this_dms() |
|
668 | - { |
|
669 | - if ($this->slug() == 'Core') { |
|
670 | - return EE_System::instance()->detect_req_type(); |
|
671 | - } else {// it must be for an addon |
|
672 | - $addon_name = $this->slug(); |
|
673 | - if (EE_Registry::instance()->get_addon_by_name($addon_name)) { |
|
674 | - return EE_Registry::instance()->get_addon_by_name($addon_name)->detect_req_type(); |
|
675 | - } else { |
|
676 | - throw new EE_Error( |
|
677 | - sprintf( |
|
678 | - __( |
|
679 | - "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", |
|
680 | - "event_espresso" |
|
681 | - ), |
|
682 | - $this->slug(), |
|
683 | - $addon_name, |
|
684 | - implode(",", array_keys(EE_Registry::instance()->get_addons_by_name())) |
|
685 | - ) |
|
686 | - ); |
|
687 | - } |
|
688 | - } |
|
689 | - } |
|
690 | - |
|
691 | - |
|
692 | - /** |
|
693 | - * returns an array of strings describing errors by all the script's stages |
|
694 | - * |
|
695 | - * @return array |
|
696 | - */ |
|
697 | - public function get_errors() |
|
698 | - { |
|
699 | - $all_errors = $this->_errors; |
|
700 | - if (! is_array($all_errors)) { |
|
701 | - $all_errors = array(); |
|
702 | - } |
|
703 | - foreach ($this->stages() as $stage) { |
|
704 | - $all_errors = array_merge($stage->get_errors(), $all_errors); |
|
705 | - } |
|
706 | - return $all_errors; |
|
707 | - } |
|
708 | - |
|
709 | - |
|
710 | - /** |
|
711 | - * Indicates whether or not this migration script should continue |
|
712 | - * |
|
713 | - * @return boolean |
|
714 | - */ |
|
715 | - public function can_continue() |
|
716 | - { |
|
717 | - return in_array( |
|
718 | - $this->get_status(), |
|
719 | - EE_Data_Migration_Manager::instance()->stati_that_indicate_to_continue_single_migration_script |
|
720 | - ); |
|
721 | - } |
|
722 | - |
|
723 | - |
|
724 | - /** |
|
725 | - * Gets all the data migration stages associated with this script. Note: |
|
726 | - * addons can filter this list to add their own stages, and because the list is |
|
727 | - * numerically-indexed, they can insert their stage wherever they like and it will |
|
728 | - * get ordered by the indexes |
|
729 | - * |
|
730 | - * @return EE_Data_Migration_Script_Stage[] |
|
731 | - */ |
|
732 | - protected function stages() |
|
733 | - { |
|
734 | - $stages = apply_filters('FHEE__' . get_class($this) . '__stages', $this->_migration_stages); |
|
735 | - ksort($stages); |
|
736 | - return $stages; |
|
737 | - } |
|
738 | - |
|
739 | - |
|
740 | - /** |
|
741 | - * Gets a string which should describe what's going on currently with this migration, which |
|
742 | - * can be displayed to the user |
|
743 | - * |
|
744 | - * @return string |
|
745 | - */ |
|
746 | - public function get_feedback_message() |
|
747 | - { |
|
748 | - return $this->_feedback_message; |
|
749 | - } |
|
750 | - |
|
751 | - |
|
752 | - /** |
|
753 | - * A lot like "__sleep()" magic method in purpose, this is meant for persisting this class' |
|
754 | - * properties to the DB. However, we don't want to use __sleep() because its quite |
|
755 | - * possible that this class is defined when it goes to sleep, but NOT available when it |
|
756 | - * awakes (eg, this class is part of an addon that is deactivated at some point). |
|
757 | - */ |
|
758 | - public function properties_as_array() |
|
759 | - { |
|
760 | - $properties = parent::properties_as_array(); |
|
761 | - $properties['_migration_stages'] = array(); |
|
762 | - foreach ($this->_migration_stages as $migration_stage_priority => $migration_stage_class) { |
|
763 | - $properties['_migration_stages'][ $migration_stage_priority ] = $migration_stage_class->properties_as_array( |
|
764 | - ); |
|
765 | - } |
|
766 | - unset($properties['_mappings']); |
|
767 | - unset($properties['previous_dms']); |
|
768 | - |
|
769 | - foreach ($this->_mappings as $old_table_name => $mapping_to_new_table) { |
|
770 | - foreach ($mapping_to_new_table as $new_table_name => $mapping) { |
|
771 | - $this->_set_mapping_option($old_table_name, $new_table_name, $mapping); |
|
772 | - } |
|
773 | - } |
|
774 | - return $properties; |
|
775 | - } |
|
776 | - |
|
777 | - |
|
778 | - /** |
|
779 | - * Sets all of the properties of this script stage to match what's in the array, which is assumed |
|
780 | - * to have been made from the properties_as_array() function. |
|
781 | - * |
|
782 | - * @param array $array_of_properties like what's produced from properties_as_array() method |
|
783 | - * @return void |
|
784 | - */ |
|
785 | - public function instantiate_from_array_of_properties($array_of_properties) |
|
786 | - { |
|
787 | - $stages_properties_arrays = $array_of_properties['_migration_stages']; |
|
788 | - unset($array_of_properties['_migration_stages']); |
|
789 | - unset($array_of_properties['class']); |
|
790 | - foreach ($array_of_properties as $property_name => $property_value) { |
|
791 | - $this->{$property_name} = $property_value; |
|
792 | - } |
|
793 | - // _migration_stages are already instantiated, but have only default data |
|
794 | - foreach ($this->_migration_stages as $stage) { |
|
795 | - $stage_data = $this->_find_migration_stage_data_with_classname( |
|
796 | - get_class($stage), |
|
797 | - $stages_properties_arrays |
|
798 | - ); |
|
799 | - // SO, if we found the stage data that was saved, use it. Otherwise, I guess the stage is new? (maybe added by |
|
800 | - // an addon? Unlikely... not sure why it wouldn't exist, but if it doesn't just treat it like it was never started yet) |
|
801 | - if ($stage_data) { |
|
802 | - $stage->instantiate_from_array_of_properties($stage_data); |
|
803 | - } |
|
804 | - } |
|
805 | - } |
|
806 | - |
|
807 | - |
|
808 | - /** |
|
809 | - * Gets the migration data from the array $migration_stage_data_arrays (which is an array of arrays, each of which |
|
810 | - * is pretty well identical to EE_Data_Migration_Stage objects except all their properties are array indexes) |
|
811 | - * for the given classname |
|
812 | - * |
|
813 | - * @param string $classname |
|
814 | - * @param array $migration_stage_data_arrays |
|
815 | - * @return null |
|
816 | - */ |
|
817 | - private function _find_migration_stage_data_with_classname($classname, $migration_stage_data_arrays) |
|
818 | - { |
|
819 | - foreach ($migration_stage_data_arrays as $migration_stage_data_array) { |
|
820 | - if (isset($migration_stage_data_array['class']) && $migration_stage_data_array['class'] == $classname) { |
|
821 | - return $migration_stage_data_array; |
|
822 | - } |
|
823 | - } |
|
824 | - return null; |
|
825 | - } |
|
826 | - |
|
827 | - |
|
828 | - /** |
|
829 | - * Returns the version that this script migrates to, based on the script's name. |
|
830 | - * Cannot be overwritten because lots of code needs to know which version a script |
|
831 | - * migrates to knowing only its name. |
|
832 | - * |
|
833 | - * @return array where the first key is the plugin's slug, the 2nd is the version of that plugin |
|
834 | - * that will be updated to. Eg array('Core','4.1.0') |
|
835 | - */ |
|
836 | - final public function migrates_to_version() |
|
837 | - { |
|
838 | - return EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
|
839 | - } |
|
840 | - |
|
841 | - |
|
842 | - /** |
|
843 | - * Gets this addon's slug as it would appear in the current_db_state wp option, |
|
844 | - * and if this migration script is for an addon, it SHOULD match the addon's slug |
|
845 | - * (and also the addon's classname, minus the 'EE_' prefix.). Eg, 'Calendar' for the EE_Calendar addon. |
|
846 | - * Or 'Core' for core (non-addon). |
|
847 | - * |
|
848 | - * @return string |
|
849 | - */ |
|
850 | - public function slug() |
|
851 | - { |
|
852 | - $migrates_to_version_info = $this->migrates_to_version(); |
|
853 | - // the slug is the first part of the array |
|
854 | - return $migrates_to_version_info['slug']; |
|
855 | - } |
|
856 | - |
|
857 | - |
|
858 | - /** |
|
859 | - * Returns the script's priority relative to DMSs from other addons. However, when |
|
860 | - * two DMSs from the same addon/core apply, this is ignored (and instead the version that |
|
861 | - * the script migrates to is used to determine which to run first). The default is 5, but all core DMSs |
|
862 | - * normally have priority 10. (So if you want a DMS "A" to run before DMS "B", both of which are from addons, |
|
863 | - * and both of which CAN run at the same time (ie, "B" doesn't depend on "A" to set |
|
864 | - * the database up so it can run), then you can set "A" to priority 3 or something. |
|
865 | - * |
|
866 | - * @return int |
|
867 | - */ |
|
868 | - public function priority() |
|
869 | - { |
|
870 | - return $this->_priority; |
|
871 | - } |
|
872 | - |
|
873 | - |
|
874 | - /** |
|
875 | - * Sets whether or not this DMS is being ran as part of a migration, instead of |
|
876 | - * just being used to setup (or verify) the current database structure matches |
|
877 | - * what the latest DMS indicates it should be |
|
878 | - * |
|
879 | - * @param boolean $migrating |
|
880 | - * @return void |
|
881 | - */ |
|
882 | - public function set_migrating($migrating = true) |
|
883 | - { |
|
884 | - $this->_migrating = $migrating; |
|
885 | - } |
|
886 | - |
|
887 | - /** |
|
888 | - * Marks that we think this migration class can continue to migrate |
|
889 | - */ |
|
890 | - public function reattempt() |
|
891 | - { |
|
892 | - parent::reattempt(); |
|
893 | - // also, we want to reattempt any stages that were marked as borked |
|
894 | - foreach ($this->stages() as $stage) { |
|
895 | - if ($stage->is_broken()) { |
|
896 | - $stage->reattempt(); |
|
897 | - } |
|
898 | - } |
|
899 | - } |
|
18 | + /** |
|
19 | + * Set by client code to indicate this DMS is being ran as part of a proper migration, |
|
20 | + * instead of being used to merely setup (or verify) the database structure. |
|
21 | + * Defaults to TRUE, so client code that's NOT using this DMS as part of a proper migration |
|
22 | + * should call EE_Data_Migration_Script_Base::set_migrating( FALSE ) |
|
23 | + * |
|
24 | + * @var boolean |
|
25 | + */ |
|
26 | + protected $_migrating = true; |
|
27 | + |
|
28 | + /** |
|
29 | + * numerically-indexed array where each value is EE_Data_Migration_Script_Stage object |
|
30 | + * |
|
31 | + * @var EE_Data_Migration_Script_Stage[] $migration_functions |
|
32 | + */ |
|
33 | + protected $_migration_stages = array(); |
|
34 | + |
|
35 | + /** |
|
36 | + * Indicates we've already ran the schema changes that needed to happen BEFORE the data migration |
|
37 | + * |
|
38 | + * @var boolean |
|
39 | + */ |
|
40 | + protected $_schema_changes_before_migration_ran = null; |
|
41 | + |
|
42 | + /** |
|
43 | + * Indicates we've already ran the schema changes that needed to happen AFTER the data migration |
|
44 | + * |
|
45 | + * @var boolean |
|
46 | + */ |
|
47 | + protected $_schema_changes_after_migration_ran = null; |
|
48 | + |
|
49 | + /** |
|
50 | + * String which describes what's currently happening in this migration |
|
51 | + * |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $_feedback_message; |
|
55 | + |
|
56 | + /** |
|
57 | + * Indicates the script's priority. Like wp's add_action and add_filter, lower numbers |
|
58 | + * correspond to earlier execution |
|
59 | + * |
|
60 | + * @var int |
|
61 | + */ |
|
62 | + protected $_priority = 5; |
|
63 | + |
|
64 | + /** |
|
65 | + * Multi-dimensional array that defines the mapping from OLD table Primary Keys |
|
66 | + * to NEW table Primary Keys. |
|
67 | + * Top-level array keys are OLD table names (minus the "wp_" part), |
|
68 | + * 2nd-level array keys are NEW table names (again, minus the "wp_" part), |
|
69 | + * 3rd-level array keys are the OLD table primary keys |
|
70 | + * and 3rd-level array values are the NEW table primary keys |
|
71 | + * |
|
72 | + * @var array |
|
73 | + */ |
|
74 | + protected $_mappings = array(); |
|
75 | + |
|
76 | + /** |
|
77 | + * @var EE_Data_Migration_Script_Base |
|
78 | + */ |
|
79 | + protected $previous_dms; |
|
80 | + |
|
81 | + |
|
82 | + /** |
|
83 | + * Returns whether or not this data migration script can operate on the given version of the database. |
|
84 | + * Eg, if this migration script can migrate from 3.1.26 or higher (but not anything after 4.0.0), and |
|
85 | + * it's passed a string like '3.1.38B', it should return true. |
|
86 | + * If this DMS is to migrate data from an EE3 addon, you will probably want to use |
|
87 | + * EventEspresso\core\services\database\TableAnalysis::tableExists() to check for old EE3 tables, and |
|
88 | + * EE_Data_Migration_Manager::get_migration_ran() to check that core was already |
|
89 | + * migrated from EE3 to EE4 (ie, this DMS probably relies on some migration data generated |
|
90 | + * during the Core 4.1.0 DMS. If core didn't run that DMS, you probably don't want |
|
91 | + * to run this DMS). |
|
92 | + * If this DMS migrates data from a previous version of this EE4 addon, just |
|
93 | + * comparing $current_database_state_of[ $this->slug() ] will probably suffice. |
|
94 | + * If this DMS should never migrate data, because it's only used to define the initial |
|
95 | + * database state, just return FALSE (and core's activation process will take care |
|
96 | + * of calling its schema_changes_before_migration() and |
|
97 | + * schema_changes_after_migration() for you. ) |
|
98 | + * |
|
99 | + * @param array $current_database_state_of keys are EE plugin slugs (eg 'Core', 'Calendar', 'Mailchimp', etc) |
|
100 | + * @return boolean |
|
101 | + */ |
|
102 | + abstract public function can_migrate_from_version($current_database_state_of); |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * Performs database schema changes that need to occur BEFORE the data is migrated. |
|
107 | + * Eg, if we were going to change user passwords from plaintext to encoded versions |
|
108 | + * during this migration, this would probably add a new column called something like |
|
109 | + * "encoded_password". |
|
110 | + * |
|
111 | + * @return boolean of success |
|
112 | + */ |
|
113 | + abstract public function schema_changes_before_migration(); |
|
114 | + |
|
115 | + |
|
116 | + /** |
|
117 | + * Performs the database schema changes that need to occur AFTER the data has been migrated. |
|
118 | + * Usually this will mean we'll be removing old columns. Eg, if we were changing passwords |
|
119 | + * from plaintext to encoded versions, and we had added a column called "encoded_password", |
|
120 | + * this function would probably remove the old column "password" (which still holds the plaintext password) |
|
121 | + * and possibly rename "encoded_password" to "password" |
|
122 | + * |
|
123 | + * @return boolean of success |
|
124 | + */ |
|
125 | + abstract public function schema_changes_after_migration(); |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * All children of this must call parent::__construct() |
|
130 | + * at the end of their constructor or suffer the consequences! |
|
131 | + * |
|
132 | + * @param TableManager $table_manager |
|
133 | + * @param TableAnalysis $table_analysis |
|
134 | + */ |
|
135 | + public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null) |
|
136 | + { |
|
137 | + $this->_migration_stages = (array) apply_filters( |
|
138 | + 'FHEE__' . get_class($this) . '__construct__migration_stages', |
|
139 | + $this->_migration_stages |
|
140 | + ); |
|
141 | + foreach ($this->_migration_stages as $migration_stage) { |
|
142 | + if ($migration_stage instanceof EE_Data_Migration_Script_Stage) { |
|
143 | + $migration_stage->_construct_finalize($this); |
|
144 | + } |
|
145 | + } |
|
146 | + parent::__construct($table_manager, $table_analysis); |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * Place to add hooks and filters for tweaking the migrations page, in order |
|
152 | + * to customize it |
|
153 | + */ |
|
154 | + public function migration_page_hooks() |
|
155 | + { |
|
156 | + // by default none are added because we normally like the default look of the migration page |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * Sets the mapping from old table primary keys to new table primary keys. |
|
162 | + * This mapping is automatically persisted as a property on the migration |
|
163 | + * |
|
164 | + * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
165 | + * @param int|string $old_pk old primary key. Eg events_detail.id's value |
|
166 | + * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
167 | + * @param int|string $new_pk eg posts.ID |
|
168 | + * @return void |
|
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 | + */ |
|
189 | + public function get_mapping_new_pk($old_table, $old_pk, $new_table) |
|
190 | + { |
|
191 | + if (! isset($this->_mappings[ $old_table ]) || |
|
192 | + ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
193 | + // try fetching the option |
|
194 | + $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
195 | + } |
|
196 | + return isset($this->_mappings[ $old_table ][ $new_table ][ $old_pk ]) |
|
197 | + ? $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] : null; |
|
198 | + } |
|
199 | + |
|
200 | + |
|
201 | + /** |
|
202 | + * Gets the old primary key, if provided with the OLD table, |
|
203 | + * and the new table and the primary key of an item in the new table |
|
204 | + * |
|
205 | + * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
206 | + * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
207 | + * @param mixed $new_pk |
|
208 | + * @return mixed |
|
209 | + */ |
|
210 | + public function get_mapping_old_pk($old_table, $new_table, $new_pk) |
|
211 | + { |
|
212 | + if (! isset($this->_mappings[ $old_table ]) || |
|
213 | + ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
214 | + // try fetching the option |
|
215 | + $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
216 | + } |
|
217 | + if (isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
218 | + $new_pk_to_old_pk = array_flip($this->_mappings[ $old_table ][ $new_table ]); |
|
219 | + if (isset($new_pk_to_old_pk[ $new_pk ])) { |
|
220 | + return $new_pk_to_old_pk[ $new_pk ]; |
|
221 | + } |
|
222 | + } |
|
223 | + return null; |
|
224 | + } |
|
225 | + |
|
226 | + |
|
227 | + /** |
|
228 | + * Gets the mapping array option specified by the table names |
|
229 | + * |
|
230 | + * @param string $old_table_name |
|
231 | + * @param string $new_table_name |
|
232 | + * @return array |
|
233 | + */ |
|
234 | + protected function _get_mapping_option($old_table_name, $new_table_name) |
|
235 | + { |
|
236 | + $option = get_option($this->_get_mapping_option_name($old_table_name, $new_table_name), array()); |
|
237 | + return $option; |
|
238 | + } |
|
239 | + |
|
240 | + |
|
241 | + /** |
|
242 | + * Updates the mapping option specified by the table names with the array provided |
|
243 | + * |
|
244 | + * @param string $old_table_name |
|
245 | + * @param string $new_table_name |
|
246 | + * @param array $mapping_array |
|
247 | + * @return boolean success of updating option |
|
248 | + */ |
|
249 | + protected function _set_mapping_option($old_table_name, $new_table_name, $mapping_array) |
|
250 | + { |
|
251 | + $success = update_option($this->_get_mapping_option_name($old_table_name, $new_table_name), $mapping_array, false); |
|
252 | + return $success; |
|
253 | + } |
|
254 | + |
|
255 | + |
|
256 | + /** |
|
257 | + * Gets the option name for this script to map from $old_table_name to $new_table_name |
|
258 | + * |
|
259 | + * @param string $old_table_name |
|
260 | + * @param string $new_table_name |
|
261 | + * @return string |
|
262 | + */ |
|
263 | + protected function _get_mapping_option_name($old_table_name, $new_table_name) |
|
264 | + { |
|
265 | + global $wpdb; |
|
266 | + $old_table_name_sans_wp = str_replace($wpdb->prefix, "", $old_table_name); |
|
267 | + $new_table_name_sans_wp = str_replace($wpdb->prefix, "", $new_table_name); |
|
268 | + $migrates_to = EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
|
269 | + return substr( |
|
270 | + 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, |
|
271 | + 0, |
|
272 | + 64 |
|
273 | + ); |
|
274 | + } |
|
275 | + |
|
276 | + |
|
277 | + /** |
|
278 | + * Counts all the records that will be migrated during this data migration. |
|
279 | + * For example, if we were changing old user passwords from plaintext to encoded versions, |
|
280 | + * this would be a count of all users who have passwords. If we were going to also split |
|
281 | + * attendee records into transactions, registrations, and attendee records, this would include |
|
282 | + * the count of all attendees currently in existence in the DB (ie, users + attendees). |
|
283 | + * If you can't determine how many records there are to migrate, just provide a guess: this |
|
284 | + * number will only be used in calculating the percent complete. If you estimate there to be |
|
285 | + * 100 records to migrate, and it turns out there's 120, we'll just show the migration as being at |
|
286 | + * 99% until the function "migration_step" returns EE_Data_Migration_Script_Base::status_complete. |
|
287 | + * |
|
288 | + * @return int |
|
289 | + */ |
|
290 | + protected function _count_records_to_migrate() |
|
291 | + { |
|
292 | + $count = 0; |
|
293 | + foreach ($this->stages() as $stage) { |
|
294 | + $count += $stage->count_records_to_migrate(); |
|
295 | + } |
|
296 | + return $count; |
|
297 | + } |
|
298 | + |
|
299 | + |
|
300 | + /** |
|
301 | + * Returns the number of records updated so far. Usually this is easiest to do |
|
302 | + * by just setting a transient and updating it after each migration_step |
|
303 | + * |
|
304 | + * @return int |
|
305 | + */ |
|
306 | + public function count_records_migrated() |
|
307 | + { |
|
308 | + $count = 0; |
|
309 | + foreach ($this->stages() as $stage) { |
|
310 | + $count += $stage->count_records_migrated(); |
|
311 | + } |
|
312 | + $this->_records_migrated = $count; |
|
313 | + return $count; |
|
314 | + } |
|
315 | + |
|
316 | + |
|
317 | + /** |
|
318 | + * @param int $num_records_to_migrate_limit |
|
319 | + * @return int |
|
320 | + * @throws EE_Error |
|
321 | + * @throws Exception |
|
322 | + */ |
|
323 | + public function migration_step($num_records_to_migrate_limit) |
|
324 | + { |
|
325 | + // reset the feedback message |
|
326 | + $this->_feedback_message = ''; |
|
327 | + // if we haven't yet done the 1st schema changes, do them now. buffer any output |
|
328 | + $this->_maybe_do_schema_changes(true); |
|
329 | + |
|
330 | + $num_records_actually_migrated = 0; |
|
331 | + $records_migrated_per_stage = array(); |
|
332 | + // setup the 'stage' variable, which should hold the last run stage of the migration (or none at all if nothing runs) |
|
333 | + $stage = null; |
|
334 | + // get the next stage that isn't complete |
|
335 | + foreach ($this->stages() as $stage) { |
|
336 | + if ($stage->get_status() == EE_Data_Migration_Manager::status_continue) { |
|
337 | + try { |
|
338 | + $records_migrated_during_stage = $stage->migration_step( |
|
339 | + $num_records_to_migrate_limit - $num_records_actually_migrated |
|
340 | + ); |
|
341 | + $num_records_actually_migrated += $records_migrated_during_stage; |
|
342 | + $records_migrated_per_stage[ $stage->pretty_name() ] = $records_migrated_during_stage; |
|
343 | + } catch (Exception $e) { |
|
344 | + // yes if we catch an exception here, we consider that migration stage borked. |
|
345 | + $stage->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
346 | + $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
347 | + $stage->add_error($e->getMessage() . ". Stack-trace:" . $e->getTraceAsString()); |
|
348 | + throw $e; |
|
349 | + } |
|
350 | + // check that the migration stage didn't mark itself as having a fatal error |
|
351 | + if ($stage->is_broken()) { |
|
352 | + $this->set_broken(); |
|
353 | + throw new EE_Error($stage->get_last_error()); |
|
354 | + } |
|
355 | + } |
|
356 | + // once we've migrated all the number we intended to (possibly from different stages), stop migrating |
|
357 | + // or if we had a fatal error |
|
358 | + // or if the current script stopped early- its not done, but it's done all it thinks we should do on this step |
|
359 | + if ($num_records_actually_migrated >= $num_records_to_migrate_limit |
|
360 | + || $stage->is_broken() |
|
361 | + || $stage->has_more_to_do() |
|
362 | + ) { |
|
363 | + break; |
|
364 | + } |
|
365 | + } |
|
366 | + // check if we're all done this data migration... |
|
367 | + // which is indicated by being done early AND the last stage claims to be done |
|
368 | + if ($stage == null) { |
|
369 | + // this migration script apparently has NO stages... which is super weird, but whatever |
|
370 | + $this->set_completed(); |
|
371 | + $this->_maybe_do_schema_changes(false); |
|
372 | + } elseif ($num_records_actually_migrated < $num_records_to_migrate_limit && ! $stage->has_more_to_do()) { |
|
373 | + // apparently we're done, because we couldn't migrate the number we intended to |
|
374 | + $this->set_completed(); |
|
375 | + $this->_update_feedback_message(array_reverse($records_migrated_per_stage)); |
|
376 | + // do schema changes for after the migration now |
|
377 | + // first double-check we haven't already done this |
|
378 | + $this->_maybe_do_schema_changes(false); |
|
379 | + } else { |
|
380 | + // update feedback message, keeping in mind that we show them with the most recent at the top |
|
381 | + $this->_update_feedback_message(array_reverse($records_migrated_per_stage)); |
|
382 | + } |
|
383 | + return $num_records_actually_migrated; |
|
384 | + } |
|
385 | + |
|
386 | + |
|
387 | + /** |
|
388 | + * Updates the feedback message according to what was done during this migration stage. |
|
389 | + * |
|
390 | + * @param array $records_migrated_per_stage KEYS are pretty names for each stage; values are the count of records |
|
391 | + * migrated from that stage |
|
392 | + * @return void |
|
393 | + */ |
|
394 | + private function _update_feedback_message($records_migrated_per_stage) |
|
395 | + { |
|
396 | + $feedback_message_array = array(); |
|
397 | + foreach ($records_migrated_per_stage as $migration_stage_name => $num_records_migrated) { |
|
398 | + $feedback_message_array[] = sprintf( |
|
399 | + __("Migrated %d records successfully during %s", "event_espresso"), |
|
400 | + $num_records_migrated, |
|
401 | + $migration_stage_name |
|
402 | + ); |
|
403 | + } |
|
404 | + $this->_feedback_message .= implode("<br>", $feedback_message_array); |
|
405 | + } |
|
406 | + |
|
407 | + |
|
408 | + /** |
|
409 | + * Calls either schema_changes_before_migration() (if $before==true) or schema_changes_after_migration |
|
410 | + * (if $before==false). Buffers their outputs and stores them on the class. |
|
411 | + * |
|
412 | + * @param boolean $before |
|
413 | + * @throws Exception |
|
414 | + * @return void |
|
415 | + */ |
|
416 | + private function _maybe_do_schema_changes($before = true) |
|
417 | + { |
|
418 | + // so this property will be either _schema_changes_after_migration_ran or _schema_changes_before_migration_ran |
|
419 | + $property_name = '_schema_changes_' . ($before ? 'before' : 'after') . '_migration_ran'; |
|
420 | + if (! $this->{$property_name}) { |
|
421 | + try { |
|
422 | + ob_start(); |
|
423 | + if ($before) { |
|
424 | + $this->schema_changes_before_migration(); |
|
425 | + } else { |
|
426 | + $this->schema_changes_after_migration(); |
|
427 | + } |
|
428 | + $output = ob_get_contents(); |
|
429 | + ob_end_clean(); |
|
430 | + } catch (Exception $e) { |
|
431 | + $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
432 | + throw $e; |
|
433 | + } |
|
434 | + // record that we've done these schema changes |
|
435 | + $this->{$property_name} = true; |
|
436 | + // if there were any warnings etc, record them as non-fatal errors |
|
437 | + if ($output) { |
|
438 | + // there were some warnings |
|
439 | + $this->_errors[] = $output; |
|
440 | + } |
|
441 | + } |
|
442 | + } |
|
443 | + |
|
444 | + |
|
445 | + /** |
|
446 | + * Wrapper for EEH_Activation::create_table. However, takes into account the request type when |
|
447 | + * deciding what to pass for its 4th arg, $drop_pre_existing_tables. Using this function, instead |
|
448 | + * of _table_should_exist_previously, indicates that this table should be new to the EE version being migrated to |
|
449 | + * or |
|
450 | + * activated currently. If this is a brand new activation or a migration, and we're indicating this table should |
|
451 | + * not |
|
452 | + * previously exist, then we want to set $drop_pre_existing_tables to TRUE (ie, we shouldn't discover that this |
|
453 | + * table exists in the DB in EEH_Activation::create_table- if it DOES exist, something's wrong and the old table |
|
454 | + * should be nuked. |
|
455 | + * |
|
456 | + * Just for a bit of context, the migration script's db_schema_changes_* methods |
|
457 | + * are called basically in 3 cases: on brand new activation of EE4 (ie no previous version of EE existed and the |
|
458 | + * plugin is being activated and we want to add all the brand new tables), upon reactivation of EE4 (it was |
|
459 | + * deactivated and then reactivated, in which case we want to just verify the DB structure is ok) that table should |
|
460 | + * be dropped), and during a migration when we're moving the DB to the state of the migration script |
|
461 | + * |
|
462 | + * @param string $table_name |
|
463 | + * @param string $table_definition_sql |
|
464 | + * @param string $engine_string |
|
465 | + */ |
|
466 | + protected function _table_is_new_in_this_version( |
|
467 | + $table_name, |
|
468 | + $table_definition_sql, |
|
469 | + $engine_string = 'ENGINE=InnoDB ' |
|
470 | + ) { |
|
471 | + $this->_create_table_and_catch_errors( |
|
472 | + $table_name, |
|
473 | + $table_definition_sql, |
|
474 | + $engine_string, |
|
475 | + $this->_pre_existing_table_should_be_dropped(true) |
|
476 | + ); |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Like _table_is_new_in_this_version and _table_should_exist_previously, this function verifies the given table |
|
481 | + * exists. But we understand that this table has CHANGED in this version since the previous version. So it's not |
|
482 | + * completely new, but it's different. So we need to treat it like a new table in terms of verifying it's schema is |
|
483 | + * correct on activations, migrations, upgrades; but if it exists when it shouldn't, we need to be as lenient as |
|
484 | + * _table_should_exist_previously. |
|
485 | + * 8656]{Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the |
|
486 | + * table shouldn't exist). |
|
487 | + * |
|
488 | + * @param string $table_name |
|
489 | + * @param string $table_definition_sql |
|
490 | + * @param string $engine_string |
|
491 | + */ |
|
492 | + protected function _table_is_changed_in_this_version( |
|
493 | + $table_name, |
|
494 | + $table_definition_sql, |
|
495 | + $engine_string = 'ENGINE=MyISAM' |
|
496 | + ) { |
|
497 | + $this->_create_table_and_catch_errors( |
|
498 | + $table_name, |
|
499 | + $table_definition_sql, |
|
500 | + $engine_string, |
|
501 | + $this->_pre_existing_table_should_be_dropped(false) |
|
502 | + ); |
|
503 | + } |
|
504 | + |
|
505 | + |
|
506 | + /** |
|
507 | + * _old_table_exists |
|
508 | + * returns TRUE if the requested table exists in the current database |
|
509 | + * |
|
510 | + * @param string $table_name |
|
511 | + * @return boolean |
|
512 | + */ |
|
513 | + protected function _old_table_exists($table_name) |
|
514 | + { |
|
515 | + return $this->_get_table_analysis()->tableExists($table_name); |
|
516 | + } |
|
517 | + |
|
518 | + |
|
519 | + /** |
|
520 | + * _delete_table_if_empty |
|
521 | + * returns TRUE if the requested table was empty and successfully empty |
|
522 | + * |
|
523 | + * @param string $table_name |
|
524 | + * @return boolean |
|
525 | + */ |
|
526 | + protected function _delete_table_if_empty($table_name) |
|
527 | + { |
|
528 | + return EEH_Activation::delete_db_table_if_empty($table_name); |
|
529 | + } |
|
530 | + |
|
531 | + |
|
532 | + /** |
|
533 | + * It is preferred to use _table_has_not_changed_since_previous or _table_is_changed_in_this_version |
|
534 | + * as these are significantly more efficient or explicit. |
|
535 | + * Please see description of _table_is_new_in_this_version. This function will only set |
|
536 | + * EEH_Activation::create_table's $drop_pre_existing_tables to TRUE if it's a brand |
|
537 | + * new activation. ie, a more accurate name for this method would be "_table_added_previously_by_this_plugin" |
|
538 | + * because the table will be cleared out if this is a new activation (ie, if its a new activation, it actually |
|
539 | + * should exist previously). Otherwise, we'll always set $drop_pre_existing_tables to FALSE because the table |
|
540 | + * should have existed. Note, if the table is being MODIFIED in this version being activated or migrated to, then |
|
541 | + * you want _table_is_changed_in_this_version NOT this one. We don't check this table's structure during migrations |
|
542 | + * because apparently it hasn't changed since the previous one, right? |
|
543 | + * |
|
544 | + * @param string $table_name |
|
545 | + * @param string $table_definition_sql |
|
546 | + * @param string $engine_string |
|
547 | + */ |
|
548 | + protected function _table_should_exist_previously( |
|
549 | + $table_name, |
|
550 | + $table_definition_sql, |
|
551 | + $engine_string = 'ENGINE=MyISAM' |
|
552 | + ) { |
|
553 | + $this->_create_table_and_catch_errors( |
|
554 | + $table_name, |
|
555 | + $table_definition_sql, |
|
556 | + $engine_string, |
|
557 | + $this->_pre_existing_table_should_be_dropped(false) |
|
558 | + ); |
|
559 | + } |
|
560 | + |
|
561 | + /** |
|
562 | + * Exactly the same as _table_should_exist_previously(), except if this migration script is currently doing |
|
563 | + * a migration, we skip checking this table's structure in the database and just assume it's correct. |
|
564 | + * So this is useful only to improve efficiency when doing migrations (not a big deal for single site installs, |
|
565 | + * but important for multisite where migrations can take a very long time otherwise). |
|
566 | + * If the table is known to have changed since previous version, use _table_is_changed_in_this_version(). |
|
567 | + * Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the table |
|
568 | + * shouldn't exist). |
|
569 | + * |
|
570 | + * @param string $table_name |
|
571 | + * @param string $table_definition_sql |
|
572 | + * @param string $engine_string |
|
573 | + */ |
|
574 | + protected function _table_has_not_changed_since_previous( |
|
575 | + $table_name, |
|
576 | + $table_definition_sql, |
|
577 | + $engine_string = 'ENGINE=MyISAM' |
|
578 | + ) { |
|
579 | + if ($this->_currently_migrating()) { |
|
580 | + // if we're doing a migration, and this table apparently already exists, then we don't need do anything right? |
|
581 | + return; |
|
582 | + } |
|
583 | + $this->_create_table_and_catch_errors( |
|
584 | + $table_name, |
|
585 | + $table_definition_sql, |
|
586 | + $engine_string, |
|
587 | + $this->_pre_existing_table_should_be_dropped(false) |
|
588 | + ); |
|
589 | + } |
|
590 | + |
|
591 | + /** |
|
592 | + * Returns whether or not this migration script is being used as part of an actual migration |
|
593 | + * |
|
594 | + * @return boolean |
|
595 | + */ |
|
596 | + protected function _currently_migrating() |
|
597 | + { |
|
598 | + // 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) |
|
599 | + return $this->_migrating && |
|
600 | + $this->can_migrate_from_version( |
|
601 | + EE_Data_Migration_Manager::instance()->ensure_current_database_state_is_set() |
|
602 | + ) && |
|
603 | + EE_Maintenance_Mode::instance()->real_level() == EE_Maintenance_Mode::level_2_complete_maintenance; |
|
604 | + } |
|
605 | + |
|
606 | + /** |
|
607 | + * Determines if a table should be dropped, based on whether it's reported to be new in $table_is_new, |
|
608 | + * and the plugin's request type. |
|
609 | + * Assumes only this plugin could have added the table (ie, if its a new activation of this plugin, the table |
|
610 | + * shouldn't exist no matter what). |
|
611 | + * |
|
612 | + * @param boolean $table_is_new |
|
613 | + * @return boolean |
|
614 | + */ |
|
615 | + protected function _pre_existing_table_should_be_dropped($table_is_new) |
|
616 | + { |
|
617 | + if ($table_is_new) { |
|
618 | + if ($this->_get_req_type_for_plugin_corresponding_to_this_dms() == EE_System::req_type_new_activation |
|
619 | + || $this->_currently_migrating() |
|
620 | + ) { |
|
621 | + return true; |
|
622 | + } else { |
|
623 | + return false; |
|
624 | + } |
|
625 | + } else { |
|
626 | + if (in_array( |
|
627 | + $this->_get_req_type_for_plugin_corresponding_to_this_dms(), |
|
628 | + array(EE_System::req_type_new_activation) |
|
629 | + )) { |
|
630 | + return true; |
|
631 | + } else { |
|
632 | + return false; |
|
633 | + } |
|
634 | + } |
|
635 | + } |
|
636 | + |
|
637 | + /** |
|
638 | + * Just wraps EEH_Activation::create_table, but catches any errors it may throw and adds them as errors on the DMS |
|
639 | + * |
|
640 | + * @param string $table_name |
|
641 | + * @param string $table_definition_sql |
|
642 | + * @param string $engine_string |
|
643 | + * @param boolean $drop_pre_existing_tables |
|
644 | + */ |
|
645 | + private function _create_table_and_catch_errors( |
|
646 | + $table_name, |
|
647 | + $table_definition_sql, |
|
648 | + $engine_string = 'ENGINE=MyISAM', |
|
649 | + $drop_pre_existing_tables = false |
|
650 | + ) { |
|
651 | + try { |
|
652 | + EEH_Activation::create_table($table_name, $table_definition_sql, $engine_string, $drop_pre_existing_tables); |
|
653 | + } catch (EE_Error $e) { |
|
654 | + $message = $e->getMessage() . '<br>Stack Trace:' . $e->getTraceAsString(); |
|
655 | + $this->add_error($message); |
|
656 | + $this->_feedback_message .= $message; |
|
657 | + } |
|
658 | + } |
|
659 | + |
|
660 | + |
|
661 | + /** |
|
662 | + * Gets the request type for the plugin (core or addon) that corresponds to this DMS |
|
663 | + * |
|
664 | + * @return int one of EE_System::_req_type_* constants |
|
665 | + * @throws EE_Error |
|
666 | + */ |
|
667 | + private function _get_req_type_for_plugin_corresponding_to_this_dms() |
|
668 | + { |
|
669 | + if ($this->slug() == 'Core') { |
|
670 | + return EE_System::instance()->detect_req_type(); |
|
671 | + } else {// it must be for an addon |
|
672 | + $addon_name = $this->slug(); |
|
673 | + if (EE_Registry::instance()->get_addon_by_name($addon_name)) { |
|
674 | + return EE_Registry::instance()->get_addon_by_name($addon_name)->detect_req_type(); |
|
675 | + } else { |
|
676 | + throw new EE_Error( |
|
677 | + sprintf( |
|
678 | + __( |
|
679 | + "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", |
|
680 | + "event_espresso" |
|
681 | + ), |
|
682 | + $this->slug(), |
|
683 | + $addon_name, |
|
684 | + implode(",", array_keys(EE_Registry::instance()->get_addons_by_name())) |
|
685 | + ) |
|
686 | + ); |
|
687 | + } |
|
688 | + } |
|
689 | + } |
|
690 | + |
|
691 | + |
|
692 | + /** |
|
693 | + * returns an array of strings describing errors by all the script's stages |
|
694 | + * |
|
695 | + * @return array |
|
696 | + */ |
|
697 | + public function get_errors() |
|
698 | + { |
|
699 | + $all_errors = $this->_errors; |
|
700 | + if (! is_array($all_errors)) { |
|
701 | + $all_errors = array(); |
|
702 | + } |
|
703 | + foreach ($this->stages() as $stage) { |
|
704 | + $all_errors = array_merge($stage->get_errors(), $all_errors); |
|
705 | + } |
|
706 | + return $all_errors; |
|
707 | + } |
|
708 | + |
|
709 | + |
|
710 | + /** |
|
711 | + * Indicates whether or not this migration script should continue |
|
712 | + * |
|
713 | + * @return boolean |
|
714 | + */ |
|
715 | + public function can_continue() |
|
716 | + { |
|
717 | + return in_array( |
|
718 | + $this->get_status(), |
|
719 | + EE_Data_Migration_Manager::instance()->stati_that_indicate_to_continue_single_migration_script |
|
720 | + ); |
|
721 | + } |
|
722 | + |
|
723 | + |
|
724 | + /** |
|
725 | + * Gets all the data migration stages associated with this script. Note: |
|
726 | + * addons can filter this list to add their own stages, and because the list is |
|
727 | + * numerically-indexed, they can insert their stage wherever they like and it will |
|
728 | + * get ordered by the indexes |
|
729 | + * |
|
730 | + * @return EE_Data_Migration_Script_Stage[] |
|
731 | + */ |
|
732 | + protected function stages() |
|
733 | + { |
|
734 | + $stages = apply_filters('FHEE__' . get_class($this) . '__stages', $this->_migration_stages); |
|
735 | + ksort($stages); |
|
736 | + return $stages; |
|
737 | + } |
|
738 | + |
|
739 | + |
|
740 | + /** |
|
741 | + * Gets a string which should describe what's going on currently with this migration, which |
|
742 | + * can be displayed to the user |
|
743 | + * |
|
744 | + * @return string |
|
745 | + */ |
|
746 | + public function get_feedback_message() |
|
747 | + { |
|
748 | + return $this->_feedback_message; |
|
749 | + } |
|
750 | + |
|
751 | + |
|
752 | + /** |
|
753 | + * A lot like "__sleep()" magic method in purpose, this is meant for persisting this class' |
|
754 | + * properties to the DB. However, we don't want to use __sleep() because its quite |
|
755 | + * possible that this class is defined when it goes to sleep, but NOT available when it |
|
756 | + * awakes (eg, this class is part of an addon that is deactivated at some point). |
|
757 | + */ |
|
758 | + public function properties_as_array() |
|
759 | + { |
|
760 | + $properties = parent::properties_as_array(); |
|
761 | + $properties['_migration_stages'] = array(); |
|
762 | + foreach ($this->_migration_stages as $migration_stage_priority => $migration_stage_class) { |
|
763 | + $properties['_migration_stages'][ $migration_stage_priority ] = $migration_stage_class->properties_as_array( |
|
764 | + ); |
|
765 | + } |
|
766 | + unset($properties['_mappings']); |
|
767 | + unset($properties['previous_dms']); |
|
768 | + |
|
769 | + foreach ($this->_mappings as $old_table_name => $mapping_to_new_table) { |
|
770 | + foreach ($mapping_to_new_table as $new_table_name => $mapping) { |
|
771 | + $this->_set_mapping_option($old_table_name, $new_table_name, $mapping); |
|
772 | + } |
|
773 | + } |
|
774 | + return $properties; |
|
775 | + } |
|
776 | + |
|
777 | + |
|
778 | + /** |
|
779 | + * Sets all of the properties of this script stage to match what's in the array, which is assumed |
|
780 | + * to have been made from the properties_as_array() function. |
|
781 | + * |
|
782 | + * @param array $array_of_properties like what's produced from properties_as_array() method |
|
783 | + * @return void |
|
784 | + */ |
|
785 | + public function instantiate_from_array_of_properties($array_of_properties) |
|
786 | + { |
|
787 | + $stages_properties_arrays = $array_of_properties['_migration_stages']; |
|
788 | + unset($array_of_properties['_migration_stages']); |
|
789 | + unset($array_of_properties['class']); |
|
790 | + foreach ($array_of_properties as $property_name => $property_value) { |
|
791 | + $this->{$property_name} = $property_value; |
|
792 | + } |
|
793 | + // _migration_stages are already instantiated, but have only default data |
|
794 | + foreach ($this->_migration_stages as $stage) { |
|
795 | + $stage_data = $this->_find_migration_stage_data_with_classname( |
|
796 | + get_class($stage), |
|
797 | + $stages_properties_arrays |
|
798 | + ); |
|
799 | + // SO, if we found the stage data that was saved, use it. Otherwise, I guess the stage is new? (maybe added by |
|
800 | + // an addon? Unlikely... not sure why it wouldn't exist, but if it doesn't just treat it like it was never started yet) |
|
801 | + if ($stage_data) { |
|
802 | + $stage->instantiate_from_array_of_properties($stage_data); |
|
803 | + } |
|
804 | + } |
|
805 | + } |
|
806 | + |
|
807 | + |
|
808 | + /** |
|
809 | + * Gets the migration data from the array $migration_stage_data_arrays (which is an array of arrays, each of which |
|
810 | + * is pretty well identical to EE_Data_Migration_Stage objects except all their properties are array indexes) |
|
811 | + * for the given classname |
|
812 | + * |
|
813 | + * @param string $classname |
|
814 | + * @param array $migration_stage_data_arrays |
|
815 | + * @return null |
|
816 | + */ |
|
817 | + private function _find_migration_stage_data_with_classname($classname, $migration_stage_data_arrays) |
|
818 | + { |
|
819 | + foreach ($migration_stage_data_arrays as $migration_stage_data_array) { |
|
820 | + if (isset($migration_stage_data_array['class']) && $migration_stage_data_array['class'] == $classname) { |
|
821 | + return $migration_stage_data_array; |
|
822 | + } |
|
823 | + } |
|
824 | + return null; |
|
825 | + } |
|
826 | + |
|
827 | + |
|
828 | + /** |
|
829 | + * Returns the version that this script migrates to, based on the script's name. |
|
830 | + * Cannot be overwritten because lots of code needs to know which version a script |
|
831 | + * migrates to knowing only its name. |
|
832 | + * |
|
833 | + * @return array where the first key is the plugin's slug, the 2nd is the version of that plugin |
|
834 | + * that will be updated to. Eg array('Core','4.1.0') |
|
835 | + */ |
|
836 | + final public function migrates_to_version() |
|
837 | + { |
|
838 | + return EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
|
839 | + } |
|
840 | + |
|
841 | + |
|
842 | + /** |
|
843 | + * Gets this addon's slug as it would appear in the current_db_state wp option, |
|
844 | + * and if this migration script is for an addon, it SHOULD match the addon's slug |
|
845 | + * (and also the addon's classname, minus the 'EE_' prefix.). Eg, 'Calendar' for the EE_Calendar addon. |
|
846 | + * Or 'Core' for core (non-addon). |
|
847 | + * |
|
848 | + * @return string |
|
849 | + */ |
|
850 | + public function slug() |
|
851 | + { |
|
852 | + $migrates_to_version_info = $this->migrates_to_version(); |
|
853 | + // the slug is the first part of the array |
|
854 | + return $migrates_to_version_info['slug']; |
|
855 | + } |
|
856 | + |
|
857 | + |
|
858 | + /** |
|
859 | + * Returns the script's priority relative to DMSs from other addons. However, when |
|
860 | + * two DMSs from the same addon/core apply, this is ignored (and instead the version that |
|
861 | + * the script migrates to is used to determine which to run first). The default is 5, but all core DMSs |
|
862 | + * normally have priority 10. (So if you want a DMS "A" to run before DMS "B", both of which are from addons, |
|
863 | + * and both of which CAN run at the same time (ie, "B" doesn't depend on "A" to set |
|
864 | + * the database up so it can run), then you can set "A" to priority 3 or something. |
|
865 | + * |
|
866 | + * @return int |
|
867 | + */ |
|
868 | + public function priority() |
|
869 | + { |
|
870 | + return $this->_priority; |
|
871 | + } |
|
872 | + |
|
873 | + |
|
874 | + /** |
|
875 | + * Sets whether or not this DMS is being ran as part of a migration, instead of |
|
876 | + * just being used to setup (or verify) the current database structure matches |
|
877 | + * what the latest DMS indicates it should be |
|
878 | + * |
|
879 | + * @param boolean $migrating |
|
880 | + * @return void |
|
881 | + */ |
|
882 | + public function set_migrating($migrating = true) |
|
883 | + { |
|
884 | + $this->_migrating = $migrating; |
|
885 | + } |
|
886 | + |
|
887 | + /** |
|
888 | + * Marks that we think this migration class can continue to migrate |
|
889 | + */ |
|
890 | + public function reattempt() |
|
891 | + { |
|
892 | + parent::reattempt(); |
|
893 | + // also, we want to reattempt any stages that were marked as borked |
|
894 | + foreach ($this->stages() as $stage) { |
|
895 | + if ($stage->is_broken()) { |
|
896 | + $stage->reattempt(); |
|
897 | + } |
|
898 | + } |
|
899 | + } |
|
900 | 900 | } |