1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* GravityView Migrate Class - where awesome features become even better, seamlessly! |
4
|
|
|
* |
5
|
|
|
* @package GravityView |
6
|
|
|
* @author Zack Katz <[email protected]> |
7
|
|
|
* @license ToBeDefined |
8
|
|
|
* @link http://www.katzwebservices.com |
9
|
|
|
* @copyright Copyright 2014, Katz Web Services, Inc. |
10
|
|
|
* |
11
|
|
|
* @since 1.2 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class GravityView_Migrate { |
16
|
|
|
|
17
|
|
|
function __construct() { |
|
|
|
|
18
|
|
|
add_action( 'admin_init', array( $this, 'update_settings' ), 1 ); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function update_settings() { |
22
|
|
|
|
23
|
|
|
$this->maybe_migrate_search_widget(); |
24
|
|
|
|
25
|
|
|
$this->migrate_redux_settings(); |
26
|
|
|
|
27
|
|
|
$this->maybe_migrate_approved_meta(); |
28
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Convert approval meta values to enumerated values |
33
|
|
|
* |
34
|
|
|
* @since 1.18 |
35
|
|
|
*/ |
36
|
|
|
private function maybe_migrate_approved_meta() { |
37
|
|
|
|
38
|
|
|
// check if search migration is already performed |
39
|
|
|
$is_updated = get_option( 'gv_migrated_approved_meta' ); |
40
|
|
|
|
41
|
|
|
if ( $is_updated ) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->update_approved_meta(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Convert "Approved" approval status to "1" |
50
|
|
|
* |
51
|
|
|
* @since 1.18 |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
private function update_approved_meta() { |
56
|
|
|
global $wpdb; |
|
|
|
|
57
|
|
|
|
58
|
|
|
if ( ! class_exists( 'GFFormsModel' ) ) { |
59
|
|
|
do_action( 'gravityview_log_error', __METHOD__ . ': GFFormsModel does not exist.' ); |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$table_name = GFFormsModel::get_lead_meta_table_name(); |
64
|
|
|
|
65
|
|
|
$sql = "UPDATE {$table_name} SET `meta_value` = %s WHERE `meta_key` = 'is_approved' AND `meta_value` = %s"; |
66
|
|
|
|
67
|
|
|
$approved_result = $wpdb->query( $wpdb->prepare( $sql, GravityView_Entry_Approval_Status::APPROVED, 'Approved' ) ); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$disapproved_result = $wpdb->query( $wpdb->prepare( $sql, GravityView_Entry_Approval_Status::DISAPPROVED, '0' ) ); |
|
|
|
|
70
|
|
|
|
71
|
|
|
if( false === $approved_result || false === $disapproved_result ) { |
72
|
|
|
do_action( 'gravityview_log_error', __METHOD__ . ': There was an error processing the query.', $wpdb->last_error ); |
73
|
|
|
} else { |
74
|
|
|
// All done: Meta values are migrated |
75
|
|
|
update_option( 'gv_migrated_approved_meta', true ); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @since 1.7.4 |
81
|
|
|
*/ |
82
|
|
|
private function maybe_migrate_search_widget() { |
83
|
|
|
|
84
|
|
|
// check if search migration is already performed |
85
|
|
|
$is_updated = get_option( 'gv_migrate_searchwidget' ); |
86
|
|
|
if ( $is_updated ) { |
87
|
|
|
return; |
88
|
|
|
} else { |
89
|
|
|
$this->update_search_on_views(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set app settings from prior Redux settings, if exists |
95
|
|
|
* |
96
|
|
|
* @since 1.7.4 |
97
|
|
|
* @return mixed|void |
98
|
|
|
*/ |
99
|
|
|
private function migrate_redux_settings() { |
100
|
|
|
|
101
|
|
|
$redux_settings = $this->get_redux_settings(); |
102
|
|
|
|
103
|
|
|
// No need to process |
104
|
|
|
if( false === $redux_settings ) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if( empty( $redux_settings['license_key_status'] ) ) { |
|
|
|
|
109
|
|
|
$redux_settings = $this->get_redux_license_status( $redux_settings ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Get the current app settings (just defaults) |
113
|
|
|
$current = GravityView_Settings::get_instance()->get_app_settings(); |
114
|
|
|
|
115
|
|
|
// Merge the redux settings with the defaults |
116
|
|
|
$updated_settings = wp_parse_args( $redux_settings, $current ); |
117
|
|
|
|
118
|
|
|
// Update the defaults to the new merged |
119
|
|
|
GravityView_Settings::get_instance()->update_app_settings( $updated_settings ); |
120
|
|
|
|
121
|
|
|
// And now remove the previous option, so this is a one-time thing. |
122
|
|
|
delete_option('gravityview_settings'); |
|
|
|
|
123
|
|
|
delete_option('gravityview_settings-transients'); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* If the settings transient wasn't set, we need to set the default status for the license |
128
|
|
|
* |
129
|
|
|
* @since 1.7.4 |
130
|
|
|
* |
131
|
|
|
* @param array $redux_settings |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
function get_redux_license_status( $redux_settings = array() ) { |
|
|
|
|
136
|
|
|
|
137
|
|
|
$data = array( |
138
|
|
|
'edd_action' => 'check_license', |
139
|
|
|
'license' => rgget('license_key', $redux_settings ), |
|
|
|
|
140
|
|
|
'update' => false, |
141
|
|
|
'format' => 'object', |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$license_call = GravityView_Settings::get_instance()->get_license_handler()->license_call( $data ); |
145
|
|
|
|
146
|
|
|
if( is_object( $license_call ) && isset( $license_call->license ) ) { |
147
|
|
|
$redux_settings['license_key_status'] = $license_call->license; |
148
|
|
|
$redux_settings['license_key_response'] = json_encode( $license_call ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $redux_settings; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get Redux settings, if they exist |
156
|
|
|
* @since 1.7.4 |
157
|
|
|
* @return array|bool |
158
|
|
|
*/ |
159
|
|
|
function get_redux_settings() { |
|
|
|
|
160
|
|
|
|
161
|
|
|
// Previous settings set by Redux |
162
|
|
|
$redux_option = get_option('gravityview_settings'); |
|
|
|
|
163
|
|
|
|
164
|
|
|
// No Redux settings? Don't proceed. |
165
|
|
|
if( false === $redux_option ) { |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
170
|
|
|
$redux_settings = array( |
171
|
|
|
'support-email' => rgget( 'support-email', $redux_option ), |
172
|
|
|
'no-conflict-mode' => ( rgget( 'no-conflict-mode', $redux_option ) ? '1' : '0' ), |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
if( $license_array = rgget( 'license', $redux_option ) ) { |
176
|
|
|
|
177
|
|
|
$redux_settings['license_key'] = $license_key = rgget( 'license', $license_array ); |
178
|
|
|
|
179
|
|
|
$redux_last_changed_values = get_option('gravityview_settings-transients'); |
|
|
|
|
180
|
|
|
|
181
|
|
|
// This contains the last response for license validation |
182
|
|
|
if( !empty( $redux_last_changed_values ) && $saved_values = rgget( 'changed_values', $redux_last_changed_values ) ) { |
|
|
|
|
183
|
|
|
|
184
|
|
|
$saved_license = rgget('license', $saved_values ); |
|
|
|
|
185
|
|
|
|
186
|
|
|
// Only use the last-saved values if they are for the same license |
187
|
|
|
if( $saved_license && rgget( 'license', $saved_license ) === $license_key ) { |
188
|
|
|
$redux_settings['license_key_status'] = rgget( 'status', $saved_license ); |
189
|
|
|
$redux_settings['license_key_response'] = rgget( 'response', $saved_license ); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $redux_settings; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** ---- Migrate from old search widget to new search widget ---- */ |
199
|
|
|
function update_search_on_views() { |
|
|
|
|
200
|
|
|
|
201
|
|
|
if( !class_exists('GravityView_Widget_Search') ) { |
|
|
|
|
202
|
|
|
include_once( GRAVITYVIEW_DIR .'includes/extensions/search-widget/class-search-widget.php' ); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Loop through all the views |
206
|
|
|
$query_args = array( |
207
|
|
|
'post_type' => 'gravityview', |
208
|
|
|
'post_status' => 'any', |
209
|
|
|
'posts_per_page' => -1, |
|
|
|
|
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
$views = get_posts( $query_args ); |
213
|
|
|
|
214
|
|
|
foreach( $views as $view ) { |
215
|
|
|
|
216
|
|
|
$widgets = gravityview_get_directory_widgets( $view->ID ); |
217
|
|
|
$search_fields = null; |
218
|
|
|
|
219
|
|
|
if( empty( $widgets ) || !is_array( $widgets ) ) { continue; } |
|
|
|
|
220
|
|
|
|
221
|
|
|
do_action( 'gravityview_log_debug', '[GravityView_Migrate/update_search_on_views] Loading View ID: ', $view->ID ); |
222
|
|
|
|
223
|
|
|
foreach( $widgets as $area => $ws ) { |
224
|
|
|
foreach( $ws as $k => $widget ) { |
225
|
|
|
if( $widget['id'] !== 'search_bar' ) { continue; } |
|
|
|
|
226
|
|
|
|
227
|
|
|
if( is_null( $search_fields ) ) { |
228
|
|
|
$search_fields = $this->get_search_fields( $view->ID ); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// check widget settings: |
232
|
|
|
// [search_free] => 1 |
|
|
|
|
233
|
|
|
// [search_date] => 1 |
|
|
|
|
234
|
|
|
$search_generic = array(); |
235
|
|
|
if( !empty( $widget['search_free'] ) ) { |
|
|
|
|
236
|
|
|
$search_generic[] = array( 'field' => 'search_all', 'input' => 'input_text' ); |
237
|
|
|
} |
238
|
|
|
if( !empty( $widget['search_date'] ) ) { |
|
|
|
|
239
|
|
|
$search_generic[] = array( 'field' => 'entry_date', 'input' => 'date' ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$search_config = array_merge( $search_generic, $search_fields ); |
243
|
|
|
|
244
|
|
|
// don't throw '[]' when json_encode an empty array |
245
|
|
|
if( empty( $search_config ) ) { |
246
|
|
|
$search_config = ''; |
247
|
|
|
} else { |
248
|
|
|
$search_config = json_encode( $search_config ); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
$widgets[ $area ][ $k ]['search_fields'] = $search_config; |
252
|
|
|
$widgets[ $area ][ $k ]['search_layout'] = 'horizontal'; |
253
|
|
|
|
254
|
|
|
do_action( 'gravityview_log_debug', '[GravityView_Migrate/update_search_on_views] Updated Widget: ', $widgets[ $area ][ $k ] ); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
// update widgets view |
259
|
|
|
gravityview_set_directory_widgets( $view->ID, $widgets ); |
260
|
|
|
|
261
|
|
|
} // foreach Views |
262
|
|
|
|
263
|
|
|
// all done! enjoy the new Search Widget! |
264
|
|
|
update_option( 'gv_migrate_searchwidget', true ); |
265
|
|
|
|
266
|
|
|
do_action( 'gravityview_log_debug', '[GravityView_Migrate/update_search_on_views] All done! enjoy the new Search Widget!' ); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
function get_search_fields( $view_id ) { |
|
|
|
|
271
|
|
|
|
272
|
|
|
$form_id = gravityview_get_form_id( $view_id ); |
273
|
|
|
$form = gravityview_get_form( $form_id ); |
274
|
|
|
|
275
|
|
|
$search_fields = array(); |
276
|
|
|
|
277
|
|
|
// check view fields' settings |
278
|
|
|
$fields = gravityview_get_directory_fields( $view_id, false ); |
279
|
|
|
|
280
|
|
|
if( !empty( $fields ) && is_array( $fields ) ) { |
|
|
|
|
281
|
|
|
|
282
|
|
|
foreach( $fields as $t => $fs ) { |
283
|
|
|
|
284
|
|
|
foreach( $fs as $k => $field ) { |
285
|
|
|
// is field a search_filter ? |
286
|
|
|
if( empty( $field['search_filter'] ) ) { continue; } |
287
|
|
|
|
288
|
|
|
// get field type & calculate the input type (by default) |
289
|
|
|
$form_field = gravityview_get_field( $form, $field['id'] ); |
290
|
|
|
|
291
|
|
|
if( empty( $form_field['type'] ) ) { |
292
|
|
|
continue; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
// depending on the field type assign a group of possible search field types |
296
|
|
|
$type = GravityView_Widget_Search::get_search_input_types( $field['id'], $form_field['type'] ); |
297
|
|
|
|
298
|
|
|
// add field to config |
299
|
|
|
$search_fields[] = array( 'field' => $field['id'], 'input' => $type ); |
300
|
|
|
|
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return $search_fields; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
|
309
|
|
|
|
310
|
|
|
} // end class |
311
|
|
|
|
312
|
|
|
new GravityView_Migrate; |
313
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.