Completed
Push — master ( c38d6c...15bc19 )
by Zack
14:07 queued 10:41
created

GravityView_Migrate::maybe_migrate_approved_meta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 312.

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.

Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
		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' ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
68
69
		$disapproved_result = $wpdb->query( $wpdb->prepare( $sql, GravityView_Entry_Approval_Status::DISAPPROVED, '0' ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
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'] ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 2 found
Loading history...
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');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
123
		delete_option('gravityview_settings-transients');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
136
137
		$data = array(
138
			'edd_action' => 'check_license',
139
			'license' => rgget('license_key', $redux_settings ),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
160
161
		// Previous settings set by Redux
162
		$redux_option = get_option('gravityview_settings');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
163
164
		// No Redux settings? Don't proceed.
165
		if( false === $redux_option ) {
166
			return false;
167
		}
168
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
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');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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 ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
183
184
				$saved_license = rgget('license', $saved_values );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
200
201
		if( !class_exists('GravityView_Widget_Search') ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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,
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set posts_per_page to -1 ever.
Loading history...
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; }
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
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; }
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
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
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
233
			        //  [search_date] => 1
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
234
			        $search_generic = array();
235
					if( !empty( $widget['search_free'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
236
						$search_generic[] = array( 'field' => 'search_all', 'input' => 'input_text' );
237
					}
238
					if( !empty( $widget['search_date'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
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 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
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