GravityView  2.17
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 <[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 
16 
17  function __construct() {
18  add_action( 'admin_init', array( $this, 'update_settings' ), 1 );
19 
20  add_action( 'admin_menu', array( $this, 'redirect_old_admin_pages' ) );
21  }
22 
23  public function update_settings() {
24 
26 
27  $this->migrate_redux_settings();
28 
30 
31  }
32 
33  /**
34  * Redirects old GravityView admin pages to the new ones.
35  *
36  * @since 2.16
37  *
38  * @return void
39  */
40  public function redirect_old_admin_pages() {
41  global $pagenow;
42 
43  if ( ! $pagenow || ! is_admin() ) {
44  return;
45  }
46 
47  // Provide redirect for old GravityView settings page.
48  if( 'edit.php' !== $pagenow ) {
49  return;
50  }
51 
52  switch ( \GV\Utils::_GET( 'page' ) ) {
53  case 'gravityview_settings':
54  wp_safe_redirect( admin_url( 'admin.php?page=gk_settings&p=gravityview&s=0' ) );
55  die();
56  case 'grant-gravityview-access':
57  wp_safe_redirect( admin_url( 'admin.php?page=gk_foundation_trustedlogin' ) );
58  die();
59  case 'gv-admin-installer':
60  wp_safe_redirect( admin_url( 'admin.php?page=gk_licenses' ) );
61  die();
62  }
63  }
64 
65  /**
66  * Convert approval meta values to enumerated values
67  *
68  * @since 1.18
69  */
70  private function maybe_migrate_approved_meta() {
71 
72  // check if search migration is already performed
73  $is_updated = get_option( 'gv_migrated_approved_meta' );
74 
75  if ( $is_updated ) {
76  return;
77  }
78 
79  $this->update_approved_meta();
80  }
81 
82  /**
83  * Convert "Approved" approval status to "1"
84  *
85  * @since 1.18
86  *
87  * @return void
88  */
89  private function update_approved_meta() {
90  global $wpdb;
91 
92  if ( ! class_exists( 'GFFormsModel' ) ) {
93  gravityview()->log->error( 'GFFormsModel does not exist.' );
94  return;
95  }
96 
97  if ( version_compare( GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) ) {
98  $table_name = GFFormsModel::get_entry_meta_table_name();
99  } else {
100  $table_name = GFFormsModel::get_lead_meta_table_name();
101  }
102 
103  $sql = "UPDATE {$table_name} SET `meta_value` = %s WHERE `meta_key` = 'is_approved' AND `meta_value` = %s";
104 
105  $approved_result = $wpdb->query( $wpdb->prepare( $sql, GravityView_Entry_Approval_Status::APPROVED, 'Approved' ) );
106 
107  $disapproved_result = $wpdb->query( $wpdb->prepare( $sql, GravityView_Entry_Approval_Status::DISAPPROVED, '0' ) );
108 
109  if( false === $approved_result || false === $disapproved_result ) {
110  gravityview()->log->error( 'There was an error processing the query. {error}', array( 'error' => $wpdb->last_error ) );
111  } else {
112  // All done: Meta values are migrated
113  update_option( 'gv_migrated_approved_meta', true );
114  }
115  }
116 
117  /**
118  * @since 1.7.4
119  */
120  private function maybe_migrate_search_widget() {
121 
122  // check if search migration is already performed
123  $is_updated = get_option( 'gv_migrate_searchwidget' );
124  if ( $is_updated ) {
125  return;
126  } else {
127  $this->update_search_on_views();
128  }
129  }
130 
131  /**
132  * Set app settings from prior Redux settings, if exists
133  *
134  * @since 1.7.4
135  * @return mixed|void
136  */
137  private function migrate_redux_settings() {
138 
139  $redux_settings = $this->get_redux_settings();
140 
141  // No need to process
142  if( false === $redux_settings ) {
143  return;
144  }
145 
146  // Get the current app settings (just defaults)
147  $current = gravityview()->plugin->settings->all();
148 
149  // Merge the redux settings with the defaults
150  $updated_settings = wp_parse_args( $redux_settings, $current );
151 
152  // Update the defaults to the new merged
153  gravityview()->plugin->settings->update( $updated_settings );
154 
155  // And now remove the previous option, so this is a one-time thing.
156  delete_option('gravityview_settings');
157  delete_option('gravityview_settings-transients');
158  }
159 
160  /**
161  * Get Redux settings, if they exist
162  * @since 1.7.4
163  * @return array|bool
164  */
165  function get_redux_settings() {
166 
167  // Previous settings set by Redux
168  $redux_option = get_option('gravityview_settings');
169 
170  // No Redux settings? Don't proceed.
171  if( false === $redux_option ) {
172  return false;
173  }
174 
175  $redux_settings = array(
176  'support-email' => \GV\Utils::get( $redux_option, 'support-email' ),
177  'no-conflict-mode' => \GV\Utils::get( $redux_option, 'no-conflict-mode' ) ? '1' : '0',
178  );
179 
180  return $redux_settings;
181  }
182 
183 
184  /** ---- Migrate from old search widget to new search widget ---- */
186 
187  if( !class_exists('GravityView_Widget_Search') ) {
188  include_once( GRAVITYVIEW_DIR .'includes/extensions/search-widget/class-search-widget.php' );
189  }
190 
191  // Loop through all the views
192  $query_args = array(
193  'post_type' => 'gravityview',
194  'post_status' => 'any',
195  'posts_per_page' => -1,
196  );
197 
198  $views = get_posts( $query_args );
199 
200  foreach( $views as $view ) {
201 
202  $widgets = gravityview_get_directory_widgets( $view->ID );
203  $search_fields = null;
204 
205  if( empty( $widgets ) || !is_array( $widgets ) ) { continue; }
206 
207  gravityview()->log->debug( '[GravityView_Migrate/update_search_on_views] Loading View ID: {view_id}', array( 'view_id' => $view->ID ) );
208 
209  foreach( $widgets as $area => $ws ) {
210  foreach( $ws as $k => $widget ) {
211  if( $widget['id'] !== 'search_bar' ) { continue; }
212 
213  if( is_null( $search_fields ) ) {
214  $search_fields = $this->get_search_fields( $view->ID );
215  }
216 
217  // check widget settings:
218  // [search_free] => 1
219  // [search_date] => 1
220  $search_generic = array();
221  if( !empty( $widget['search_free'] ) ) {
222  $search_generic[] = array( 'field' => 'search_all', 'input' => 'input_text' );
223  }
224  if( !empty( $widget['search_date'] ) ) {
225  $search_generic[] = array( 'field' => 'entry_date', 'input' => 'date' );
226  }
227 
228  $search_config = array_merge( $search_generic, $search_fields );
229 
230  // don't throw '[]' when json_encode an empty array
231  if( empty( $search_config ) ) {
232  $search_config = '';
233  } else {
234  $search_config = json_encode( $search_config );
235  }
236 
237  $widgets[ $area ][ $k ]['search_fields'] = $search_config;
238  $widgets[ $area ][ $k ]['search_layout'] = 'horizontal';
239 
240  gravityview()->log->debug( '[GravityView_Migrate/update_search_on_views] Updated Widget: ', array( 'data' => $widgets[ $area ][ $k ] ) );
241  }
242  }
243 
244  // update widgets view
245  gravityview_set_directory_widgets( $view->ID, $widgets );
246 
247  } // foreach Views
248 
249  // all done! enjoy the new Search Widget!
250  update_option( 'gv_migrate_searchwidget', true );
251 
252  gravityview()->log->debug( '[GravityView_Migrate/update_search_on_views] All done! enjoy the new Search Widget!' );
253  }
254 
255 
257 
260 
261  $search_fields = array();
262 
263  // check view fields' settings
264  $fields = gravityview_get_directory_fields( $view_id, false );
265 
266  if( !empty( $fields ) && is_array( $fields ) ) {
267 
268  foreach( $fields as $t => $fs ) {
269 
270  foreach( $fs as $k => $field ) {
271  // is field a search_filter ?
272  if( empty( $field['search_filter'] ) ) { continue; }
273 
274  // get field type & calculate the input type (by default)
275  $form_field = gravityview_get_field( $form, $field['id'] );
276 
277  if( empty( $form_field['type'] ) ) {
278  continue;
279  }
280 
281  // depending on the field type assign a group of possible search field types
282  $type = GravityView_Widget_Search::get_search_input_types( $field['id'], $form_field['type'] );
283 
284  // add field to config
285  $search_fields[] = array( 'field' => $field['id'], 'input' => $type );
286 
287  }
288  }
289  }
290 
291  return $search_fields;
292  }
293 
294 
295 
296 } // end class
297 
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:49
gravityview_set_directory_widgets( $post_id, $widgets=array())
Set the widgets, as configured for a View.
redirect_old_admin_pages()
Redirects old GravityView admin pages to the new ones.
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.
gravityview_get_directory_fields( $post_id, $apply_filter=true, $form_id=0)
Get the field configuration for the View.
update_search_on_views()
-— Migrate from old search widget to new search widget -—
if(gravityview() ->plugin->is_GF_25()) $form
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
gravityview()
The main GravityView wrapper function.
update_approved_meta()
Convert "Approved" approval status to "1".