GravityView  1.22.6
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-migrate.php
Go to the documentation of this file.
1 <?php
2 /**
3  * GravityView Migrate Class - where awesome features become even better, seamlessly!
4  *
5  * @package GravityView
6  * @author Zack Katz <zack@katzwebservices.com>
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 
16 
17  function __construct() {
18  add_action( 'admin_init', array( $this, 'update_settings' ), 1 );
19  }
20 
21  public function update_settings() {
22 
24 
25  $this->migrate_redux_settings();
26 
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  if ( version_compare( GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) ) {
64  $table_name = GFFormsModel::get_entry_meta_table_name();
65  } else {
66  $table_name = GFFormsModel::get_lead_meta_table_name();
67  }
68 
69  $sql = "UPDATE {$table_name} SET `meta_value` = %s WHERE `meta_key` = 'is_approved' AND `meta_value` = %s";
70 
71  $approved_result = $wpdb->query( $wpdb->prepare( $sql, GravityView_Entry_Approval_Status::APPROVED, 'Approved' ) );
72 
73  $disapproved_result = $wpdb->query( $wpdb->prepare( $sql, GravityView_Entry_Approval_Status::DISAPPROVED, '0' ) );
74 
75  if( false === $approved_result || false === $disapproved_result ) {
76  do_action( 'gravityview_log_error', __METHOD__ . ': There was an error processing the query.', $wpdb->last_error );
77  } else {
78  // All done: Meta values are migrated
79  update_option( 'gv_migrated_approved_meta', true );
80  }
81  }
82 
83  /**
84  * @since 1.7.4
85  */
86  private function maybe_migrate_search_widget() {
87 
88  // check if search migration is already performed
89  $is_updated = get_option( 'gv_migrate_searchwidget' );
90  if ( $is_updated ) {
91  return;
92  } else {
93  $this->update_search_on_views();
94  }
95  }
96 
97  /**
98  * Set app settings from prior Redux settings, if exists
99  *
100  * @since 1.7.4
101  * @return mixed|void
102  */
103  private function migrate_redux_settings() {
104 
105  $redux_settings = $this->get_redux_settings();
106 
107  // No need to process
108  if( false === $redux_settings ) {
109  return;
110  }
111 
112  if( empty( $redux_settings['license_key_status'] ) ) {
113  $redux_settings = $this->get_redux_license_status( $redux_settings );
114  }
115 
116  // Get the current app settings (just defaults)
117  $current = GravityView_Settings::get_instance()->get_app_settings();
118 
119  // Merge the redux settings with the defaults
120  $updated_settings = wp_parse_args( $redux_settings, $current );
121 
122  // Update the defaults to the new merged
123  GravityView_Settings::get_instance()->update_app_settings( $updated_settings );
124 
125  // And now remove the previous option, so this is a one-time thing.
126  delete_option('gravityview_settings');
127  delete_option('gravityview_settings-transients');
128  }
129 
130  /**
131  * If the settings transient wasn't set, we need to set the default status for the license
132  *
133  * @since 1.7.4
134  *
135  * @param array $redux_settings
136  *
137  * @return array
138  */
139  function get_redux_license_status( $redux_settings = array() ) {
140 
141  $data = array(
142  'edd_action' => 'check_license',
143  'license' => rgget('license_key', $redux_settings ),
144  'update' => false,
145  'format' => 'object',
146  );
147 
148  $license_call = GravityView_Settings::get_instance()->get_license_handler()->license_call( $data );
149 
150  if( is_object( $license_call ) && isset( $license_call->license ) ) {
151  $redux_settings['license_key_status'] = $license_call->license;
152  $redux_settings['license_key_response'] = json_encode( $license_call );
153  }
154 
155  return $redux_settings;
156  }
157 
158  /**
159  * Get Redux settings, if they exist
160  * @since 1.7.4
161  * @return array|bool
162  */
163  function get_redux_settings() {
164 
165  // Previous settings set by Redux
166  $redux_option = get_option('gravityview_settings');
167 
168  // No Redux settings? Don't proceed.
169  if( false === $redux_option ) {
170  return false;
171  }
172 
173 
174  $redux_settings = array(
175  'support-email' => rgget( 'support-email', $redux_option ),
176  'no-conflict-mode' => ( rgget( 'no-conflict-mode', $redux_option ) ? '1' : '0' ),
177  );
178 
179  if( $license_array = rgget( 'license', $redux_option ) ) {
180 
181  $redux_settings['license_key'] = $license_key = rgget( 'license', $license_array );
182 
183  $redux_last_changed_values = get_option('gravityview_settings-transients');
184 
185  // This contains the last response for license validation
186  if( !empty( $redux_last_changed_values ) && $saved_values = rgget( 'changed_values', $redux_last_changed_values ) ) {
187 
188  $saved_license = rgget('license', $saved_values );
189 
190  // Only use the last-saved values if they are for the same license
191  if( $saved_license && rgget( 'license', $saved_license ) === $license_key ) {
192  $redux_settings['license_key_status'] = rgget( 'status', $saved_license );
193  $redux_settings['license_key_response'] = rgget( 'response', $saved_license );
194  }
195  }
196  }
197 
198  return $redux_settings;
199  }
200 
201 
202  /** ---- Migrate from old search widget to new search widget ---- */
204 
205  if( !class_exists('GravityView_Widget_Search') ) {
206  include_once( GRAVITYVIEW_DIR .'includes/extensions/search-widget/class-search-widget.php' );
207  }
208 
209  // Loop through all the views
210  $query_args = array(
211  'post_type' => 'gravityview',
212  'post_status' => 'any',
213  'posts_per_page' => -1,
214  );
215 
216  $views = get_posts( $query_args );
217 
218  foreach( $views as $view ) {
219 
220  $widgets = gravityview_get_directory_widgets( $view->ID );
221  $search_fields = null;
222 
223  if( empty( $widgets ) || !is_array( $widgets ) ) { continue; }
224 
225  do_action( 'gravityview_log_debug', '[GravityView_Migrate/update_search_on_views] Loading View ID: ', $view->ID );
226 
227  foreach( $widgets as $area => $ws ) {
228  foreach( $ws as $k => $widget ) {
229  if( $widget['id'] !== 'search_bar' ) { continue; }
230 
231  if( is_null( $search_fields ) ) {
232  $search_fields = $this->get_search_fields( $view->ID );
233  }
234 
235  // check widget settings:
236  // [search_free] => 1
237  // [search_date] => 1
238  $search_generic = array();
239  if( !empty( $widget['search_free'] ) ) {
240  $search_generic[] = array( 'field' => 'search_all', 'input' => 'input_text' );
241  }
242  if( !empty( $widget['search_date'] ) ) {
243  $search_generic[] = array( 'field' => 'entry_date', 'input' => 'date' );
244  }
245 
246  $search_config = array_merge( $search_generic, $search_fields );
247 
248  // don't throw '[]' when json_encode an empty array
249  if( empty( $search_config ) ) {
250  $search_config = '';
251  } else {
252  $search_config = json_encode( $search_config );
253  }
254 
255  $widgets[ $area ][ $k ]['search_fields'] = $search_config;
256  $widgets[ $area ][ $k ]['search_layout'] = 'horizontal';
257 
258  do_action( 'gravityview_log_debug', '[GravityView_Migrate/update_search_on_views] Updated Widget: ', $widgets[ $area ][ $k ] );
259  }
260  }
261 
262  // update widgets view
263  gravityview_set_directory_widgets( $view->ID, $widgets );
264 
265  } // foreach Views
266 
267  // all done! enjoy the new Search Widget!
268  update_option( 'gv_migrate_searchwidget', true );
269 
270  do_action( 'gravityview_log_debug', '[GravityView_Migrate/update_search_on_views] All done! enjoy the new Search Widget!' );
271  }
272 
273 
275 
278 
279  $search_fields = array();
280 
281  // check view fields' settings
282  $fields = gravityview_get_directory_fields( $view_id, false );
283 
284  if( !empty( $fields ) && is_array( $fields ) ) {
285 
286  foreach( $fields as $t => $fs ) {
287 
288  foreach( $fs as $k => $field ) {
289  // is field a search_filter ?
290  if( empty( $field['search_filter'] ) ) { continue; }
291 
292  // get field type & calculate the input type (by default)
293  $form_field = gravityview_get_field( $form, $field['id'] );
294 
295  if( empty( $form_field['type'] ) ) {
296  continue;
297  }
298 
299  // depending on the field type assign a group of possible search field types
300  $type = GravityView_Widget_Search::get_search_input_types( $field['id'], $form_field['type'] );
301 
302  // add field to config
303  $search_fields[] = array( 'field' => $field['id'], 'input' => $type );
304 
305  }
306  }
307  }
308 
309  return $search_fields;
310  }
311 
312 
313 
314 } // end class
315 
get_redux_settings()
Get Redux settings, if they exist.
const GRAVITYVIEW_DIR
"GRAVITYVIEW_DIR" "./" The absolute path to the plugin directory, with trailing slash ...
Definition: gravityview.php:35
gravityview_set_directory_widgets( $post_id, $widgets=array())
Set the widgets, as configured for a View.
gravityview_get_directory_fields( $post_id, $apply_filter=true)
Get the field configuration for the View.
maybe_migrate_approved_meta()
Convert approval meta values to enumerated values.
gravityview_get_form( $form_id)
Returns the form object for a given Form ID.
gravityview_get_directory_widgets( $post_id)
Get the widgets, as configured for a View.
update_search_on_views()
-— Migrate from old search widget to new search widget -—
gravityview_get_field( $form, $field_id)
Returns the field details array of a specific form given the field id.
const APPROVED
new GravityView_Migrate
gravityview_get_form_id( $view_id)
Get the connected form ID from a View ID.
const DISAPPROVED
migrate_redux_settings()
Set app settings from prior Redux settings, if exists.
static get_search_input_types( $field_id='', $field_type=null)
Assign an input type according to the form field type.
if(empty( $created_by)) $form_id
update_approved_meta()
Convert "Approved" approval status to "1".
get_redux_license_status( $redux_settings=array())
If the settings transient wasn&#39;t set, we need to set the default status for the license.
$field
Definition: gquiz_grade.php:11