Completed
Push — master ( 2cfa6f...8927a4 )
by Zack
10:00 queued 06:05
created

includes/class-gravityview-migrate.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
57
58
		$table_name = GFFormsModel::get_lead_meta_table_name();
59
60
		$sql = "UPDATE {$table_name} SET `meta_value` = %s WHERE `meta_key` = 'is_approved' AND `meta_value` = %s";
61
62
		$approved_result = $wpdb->query( $wpdb->prepare( $sql, GravityView_Entry_Approval_Status::APPROVED, 'Approved' ) );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
63
64
		$disapproved_result = $wpdb->query( $wpdb->prepare( $sql, GravityView_Entry_Approval_Status::DISAPPROVED, '0' ) );
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
65
66
		if( false === $approved_result || false === $disapproved_result ) {
67
			do_action( 'gravityview_log_error', __METHOD__ . ': There was an error processing the query.', $wpdb->last_error );
68
		} else {
69
			// All done: Meta values are migrated
70
			update_option( 'gv_migrated_approved_meta', true );
71
		}
72
	}
73
74
	/**
75
	 * @since 1.7.4
76
	 */
77
	private function maybe_migrate_search_widget() {
78
79
		// check if search migration is already performed
80
		$is_updated = get_option( 'gv_migrate_searchwidget' );
81
		if ( $is_updated ) {
82
			return;
83
		} else {
84
			$this->update_search_on_views();
85
		}
86
	}
87
88
	/**
89
	 * Set app settings from prior Redux settings, if exists
90
	 *
91
	 * @since 1.7.4
92
	 * @return mixed|void
93
	 */
94
	private function migrate_redux_settings() {
95
96
		$redux_settings = $this->get_redux_settings();
97
98
		// No need to process
99
		if( false === $redux_settings ) {
100
			return;
101
		}
102
103
		if( empty(  $redux_settings['license_key_status'] ) ) {
104
			$redux_settings = $this->get_redux_license_status( $redux_settings );
105
		}
106
107
		// Get the current app settings (just defaults)
108
		$current = GravityView_Settings::get_instance()->get_app_settings();
109
110
		// Merge the redux settings with the defaults
111
		$updated_settings = wp_parse_args( $redux_settings, $current );
112
113
		// Update the defaults to the new merged
114
		GravityView_Settings::get_instance()->update_app_settings( $updated_settings );
115
116
		// And now remove the previous option, so this is a one-time thing.
117
		delete_option('gravityview_settings');
118
		delete_option('gravityview_settings-transients');
119
	}
120
121
	/**
122
	 * If the settings transient wasn't set, we need to set the default status for the license
123
	 *
124
	 * @since 1.7.4
125
	 *
126
	 * @param array $redux_settings
127
	 *
128
	 * @return array
129
	 */
130
	function get_redux_license_status( $redux_settings = array() ) {
131
132
		$data = array(
133
			'edd_action' => 'check_license',
134
			'license' => rgget('license_key', $redux_settings ),
135
			'update' => false,
136
			'format' => 'object',
137
		);
138
139
		$license_call = GravityView_Settings::get_instance()->get_license_handler()->license_call( $data );
140
141
		if( is_object( $license_call ) && isset( $license_call->license ) ) {
142
			$redux_settings['license_key_status'] = $license_call->license;
143
			$redux_settings['license_key_response'] = json_encode( $license_call );
144
		}
145
146
		return $redux_settings;
147
	}
148
149
	/**
150
	 * Get Redux settings, if they exist
151
	 * @since 1.7.4
152
	 * @return array|bool
153
	 */
154
	function get_redux_settings() {
155
156
		// Previous settings set by Redux
157
		$redux_option = get_option('gravityview_settings');
158
159
		// No Redux settings? Don't proceed.
160
		if( false === $redux_option ) {
161
			return false;
162
		}
163
164
165
		$redux_settings = array(
166
			'support-email' => rgget( 'support-email', $redux_option ),
167
			'no-conflict-mode' => ( rgget( 'no-conflict-mode', $redux_option ) ? '1' : '0' ),
168
		);
169
170
		if( $license_array = rgget( 'license', $redux_option ) ) {
171
172
			$redux_settings['license_key'] = $license_key = rgget( 'license', $license_array );
173
174
			$redux_last_changed_values = get_option('gravityview_settings-transients');
175
176
			// This contains the last response for license validation
177
			if( !empty( $redux_last_changed_values ) && $saved_values = rgget( 'changed_values', $redux_last_changed_values ) ) {
178
179
				$saved_license = rgget('license', $saved_values );
180
181
				// Only use the last-saved values if they are for the same license
182
				if( $saved_license && rgget( 'license', $saved_license ) === $license_key ) {
183
					$redux_settings['license_key_status'] = rgget( 'status', $saved_license );
184
					$redux_settings['license_key_response'] = rgget( 'response', $saved_license );
185
				}
186
			}
187
		}
188
189
		return $redux_settings;
190
	}
191
192
193
	/** ----  Migrate from old search widget to new search widget  ---- */
194
	function update_search_on_views() {
195
196
		if( !class_exists('GravityView_Widget_Search') ) {
197
			include_once( GRAVITYVIEW_DIR .'includes/extensions/search-widget/class-search-widget.php' );
198
		}
199
200
		// Loop through all the views
201
		$query_args = array(
202
			'post_type' => 'gravityview',
203
			'post_status' => 'any',
204
			'posts_per_page' => -1,
205
		);
206
207
		$views = get_posts( $query_args );
208
209
		foreach( $views as $view ) {
210
211
			$widgets = gravityview_get_directory_widgets( $view->ID );
212
			$search_fields = null;
213
214
			if( empty( $widgets ) || !is_array( $widgets ) ) { continue; }
215
216
			do_action( 'gravityview_log_debug', '[GravityView_Migrate/update_search_on_views] Loading View ID: ', $view->ID );
217
218
			foreach( $widgets as $area => $ws ) {
219
				foreach( $ws as $k => $widget ) {
220
					if( $widget['id'] !== 'search_bar' ) { continue; }
221
222
					if( is_null( $search_fields ) ) {
223
						$search_fields = $this->get_search_fields( $view->ID );
224
					}
225
226
					// check widget settings:
227
					//  [search_free] => 1
228
			        //  [search_date] => 1
229
			        $search_generic = array();
230
					if( !empty( $widget['search_free'] ) ) {
231
						$search_generic[] = array( 'field' => 'search_all', 'input' => 'input_text' );
232
					}
233
					if( !empty( $widget['search_date'] ) ) {
234
						$search_generic[] = array( 'field' => 'entry_date', 'input' => 'date' );
235
					}
236
237
					$search_config = array_merge( $search_generic, $search_fields );
238
239
					// don't throw '[]' when json_encode an empty array
240
					if( empty( $search_config ) ) {
241
						$search_config = '';
242
					} else {
243
						$search_config = json_encode( $search_config );
244
					}
245
246
					$widgets[ $area ][ $k ]['search_fields'] = $search_config;
247
					$widgets[ $area ][ $k ]['search_layout'] = 'horizontal';
248
249
					do_action( 'gravityview_log_debug', '[GravityView_Migrate/update_search_on_views] Updated Widget: ', $widgets[ $area ][ $k ] );
250
				}
251
			}
252
253
			// update widgets view
254
			gravityview_set_directory_widgets( $view->ID, $widgets );
255
256
		} // foreach Views
257
258
		// all done! enjoy the new Search Widget!
259
		update_option( 'gv_migrate_searchwidget', true );
260
261
		do_action( 'gravityview_log_debug', '[GravityView_Migrate/update_search_on_views] All done! enjoy the new Search Widget!' );
262
	}
263
264
265
	function get_search_fields( $view_id ) {
266
267
		$form_id = gravityview_get_form_id( $view_id );
268
		$form = gravityview_get_form( $form_id );
269
270
		$search_fields = array();
271
272
		// check view fields' settings
273
		$fields = gravityview_get_directory_fields( $view_id, false );
274
275
		if( !empty( $fields ) && is_array( $fields ) ) {
276
277
			foreach( $fields as $t => $fs ) {
278
279
				foreach( $fs as $k => $field ) {
280
					// is field a search_filter ?
281
					if( empty( $field['search_filter'] ) ) { continue; }
282
283
					// get field type & calculate the input type (by default)
284
					$form_field = gravityview_get_field( $form, $field['id'] );
285
286
					if( empty( $form_field['type'] ) ) {
287
						continue;
288
					}
289
290
					// depending on the field type assign a group of possible search field types
291
					$type = GravityView_Widget_Search::get_search_input_types( $field['id'], $form_field['type'] );
292
293
					// add field to config
294
					$search_fields[] = array( 'field' => $field['id'], 'input' => $type );
295
296
				}
297
			}
298
		}
299
300
		return $search_fields;
301
	}
302
303
304
305
} // end class
306
307
new GravityView_Migrate;
308