PluginProbe
WP-Stateless – Google Cloud Storage / 2.3.2
WP-Stateless – Google Cloud Storage v2.3.2
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / compatibility / gravity-forms.php

gravity-forms.php in WP-Stateless – Google Cloud Storage 2.3.2, at lib/classes/compatibility/gravity-forms.php

265 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Gravity Forms
4 * Plugin URI: https://www.gravityforms.com/
5 *
6 * Compatibility Description: Enables support for these Gravity Forms features:
7 * file upload field, post image field, custom file upload field type.
8 *
9 */
10
11 namespace wpCloud\StatelessMedia {
12
13 if(!class_exists('wpCloud\StatelessMedia\GravityForm')) {
14
15 class GravityForm extends ICompatibility {
16 protected $id = 'gravity-form';
17 protected $title = 'Gravity Forms';
18 protected $constant = 'WP_STATELESS_COMPATIBILITY_GF';
19 protected $description = 'Enables support for these Gravity Forms features: file upload field, post image field, custom file upload field type.';
20 protected $plugin_file = 'gravityforms/gravityforms.php';
21 protected $plugin_version;
22 protected $non_library_sync = true;
23
24 public function module_init($sm){
25 if ( class_exists('GFForms') ) {
26 $this->plugin_version = \GFForms::$version;
27 }
28 add_filter( 'gform_save_field_value', array($this, 'gform_save_field_value'), 10, 5 );
29 add_filter( 'stateless_skip_cache_busting', array($this, 'skip_cache_busting'), 10, 2 );
30
31 do_action('sm:sync::register_dir', '/gravity_forms/');
32 add_action( 'sm::synced::nonMediaFiles', array($this, 'modify_db'), 10, 3);
33 add_action( 'gform_file_path_pre_delete_file', array($this, 'gform_file_path_pre_delete_file'), 10, 2);
34 }
35
36 /**
37 * On gform save field value sync file to GCS and alter the file url to GCS link.
38 *
39 * @param $value
40 * @param $lead
41 * @param $field
42 * @param $form
43 * @param $input_id
44 */
45 public function gform_save_field_value( $value, $lead, $field, $form, $input_id ) {
46 if(empty($value)) return $value;
47
48 if ( empty($this->plugin_version) && class_exists('GFForms') ) {
49 $this->plugin_version = \GFForms::$version;
50 }
51
52 $type = \GFFormsModel::get_input_type($field);
53 if($type == 'fileupload'){
54 $dir = wp_upload_dir();
55
56 if ( $field->multipleFiles ) {
57 $value = json_decode( $value );
58 }
59 else{
60 $value = array($value);
61 }
62
63 foreach($value as $k => $v){
64 if(empty($v)) continue;
65 $position = strpos($v, 'gravity_forms/');
66
67 if( $position !== false ){
68 $name = substr($v, $position);
69 $absolutePath = $dir['basedir'] . '/' . $name;
70 $name = apply_filters( 'wp_stateless_file_name', $name);
71 // doing sync
72 do_action( 'sm:sync::syncFile', $name, $absolutePath);
73 $value[$k] = ud_get_stateless_media()->get_gs_host() . '/' . $name;
74 // Todo add filter.
75 }
76 }
77
78 if ( $field->multipleFiles ) {
79 $value = json_encode( $value );
80 }
81 else{
82 $value = array_pop($value);
83 }
84 }
85 else if($type == 'post_image'){
86 add_action( 'gform_after_create_post', function($post_id, $lead, $form) use ($value, $field){
87 global $wpdb;
88 $dir = wp_upload_dir();
89 $lead_detail_id = $lead['id'];
90 $gf_upload_root = \GFFormsModel::get_upload_root();
91 $gf_upload_url_root = \GFFormsModel::get_upload_url_root();
92 $lead_detail_table = \GFFormsModel::get_lead_details_table_name();
93
94 $position = strpos($value, 'gravity_forms/');
95 $_name = substr($value, $position); // gravity_forms/
96 $arr_name = explode('|:|', $_name);
97 $name = rgar( $arr_name, 0 ); // Removed |:| from end of the url.
98
99 // doing sync
100 $absolutePath = $dir['basedir'] . '/' . $name;
101 $name = apply_filters( 'wp_stateless_file_name', $name);
102 do_action( 'sm:sync::syncFile', $name, $absolutePath);
103
104 $value = ud_get_stateless_media()->get_gs_host() . '/' . $name;
105 // Todo add filter.
106 if(version_compare($this->plugin_version, '2.3', '<')){ // older version
107 $result = $wpdb->update( $lead_detail_table, array( 'value' => $value ), array( 'lead_id' => $lead_detail_id, 'form_id' => $form['id'], 'field_number' => $field['id'], ), array( '%s' ), array( '%d' ) );
108 }
109 else{ // New version
110 $result = $wpdb->update( \GFFormsModel::get_entry_meta_table_name(), array( 'meta_value' => $value ), array( 'entry_id' => $lead_detail_id, 'form_id' => $form['id'], 'meta_key' => $field['id'], ), array( '%s' ), array( '%d' ) );
111 }
112 }, 10, 3);
113 }
114 return $value;
115 }
116
117 /**
118 * Modify value in database after sync from Sync tab.
119 *
120 */
121 public function modify_db( $file_path, $fullsizepath, $media ){
122 global $wpdb;
123 $wpdb->hide_errors();
124 $position = strpos($file_path, 'gravity_forms/');
125 $is_index = strpos($file_path, 'index.html');
126 $is_htaccess = strpos($file_path, '.htaccess');
127 $root_dir = ud_get_stateless_media()->get( 'sm.root_dir' );
128
129 if ( empty($this->plugin_version) && class_exists('GFForms') ) {
130 $this->plugin_version = \GFForms::$version;
131 }
132
133 $gf_val_column = 'meta_value';
134 $gf_table = \GFFormsModel::get_entry_meta_table_name();
135 if(version_compare($this->plugin_version, '2.3', '<')){
136 $gf_val_column = 'value';
137 }
138
139 if( $position !== false && !$is_index ){
140 $dir = wp_upload_dir();
141 $file_path = trim($file_path, '/');
142 //EDIT: Use base file name since the URL in the DB could be encoded with in an array
143 $file_single = basename($file_path);
144
145 // Todo add filter.
146
147 // We need to get results from db because of post image field have extra data at the end of url.
148 // Also url could be array and json encoded.
149 // Unless we would loss those data.
150 // xyz.jpg|:|tile|:|description|:|
151 $query = sprintf(
152 "
153 SELECT id, {$gf_val_column} AS value FROM {$gf_table}
154 WHERE {$gf_val_column} like '%s';
155 "
156 , '%' . $file_single . '%'
157 );
158 $results = $wpdb->get_results( $query );
159 $this->throw_db_error();
160
161 foreach ($results as $result) {
162 $position = false;
163 //EDIT: Check if value is json encoded, if so, cycle through array and replace URLs.
164 $value = json_decode($result->value);
165
166 if (json_last_error() === 0) {
167 foreach( $value as $k => $v ){
168 $position = strpos($v, $dir['baseurl']);
169 if($position !== false){
170 $value[$k] = str_replace($dir['baseurl'], ud_get_stateless_media()->get_gs_host() . '/' . $root_dir, $v );
171 }
172 }
173
174 $result->value = json_encode($value);
175 }
176 else{
177 $position = strpos($result->value, $dir['baseurl']);
178 $result->value = str_replace($dir['baseurl'], ud_get_stateless_media()->get_gs_host() . '/' . $root_dir, $result->value);
179 }
180
181 if($position !== false){
182 $query = sprintf(
183 "
184 UPDATE {$gf_table}
185 SET {$gf_val_column} = '%s'
186 WHERE id = %d
187 "
188 , $result->value, $result->id
189 );
190 $entries = $wpdb->get_results( $query );
191 $this->throw_db_error();
192 }
193
194 }
195 }
196 }
197
198 /**
199 * Throw db error from last db query.
200 * We need to throw db error instead of just printing,
201 * so that we can catch them in ajax request.
202 */
203 function throw_db_error(){
204
205 global $wpdb;
206 $wpdb->show_errors();
207
208 if($wpdb->last_error !== '' && wp_doing_ajax()) :
209 ob_start();
210 $wpdb->print_error();
211 $error = ob_get_clean();
212 if($error){
213 throw new \Exception( $error );
214 }
215 endif;
216
217 }
218
219 /**
220 * Delete file from GCS
221 */
222 public function gform_file_path_pre_delete_file( $file_path, $url ){
223 $file_path = wp_normalize_path($file_path);
224 $gs_host = wp_normalize_path( ud_get_stateless_media()->get_gs_host() );
225 $dir = wp_upload_dir();
226 $is_stateless = strpos($file_path, $gs_host);
227
228 // If the url is a GCS link then remove it from GCS.
229 if($is_stateless !== false){
230 $gs_name = substr($file_path, strpos($file_path, '/gravity_forms/'));
231 $file_path = $dir['basedir'] . $gs_name;
232 $gs_name = apply_filters( 'wp_stateless_file_name', $gs_name);
233
234 $client = ud_get_stateless_media()->get_client();
235 if( !is_wp_error( $client ) ) {
236 $client->remove_media( trim($gs_name, '/') );
237 }
238 }
239
240 return $file_path;
241 }
242
243 /**
244 *
245 */
246 public function skip_cache_busting($return, $filename){
247 $backtrace = debug_backtrace(false, 8);
248 if(
249 !empty($backtrace[7]['class']) &&
250 $backtrace[7]['class'] == 'GFExport' &&
251 (
252 $backtrace[7]['function'] == 'write_file' ||
253 $backtrace[7]['function'] == 'ajax_download_export'
254 )
255 ){
256 return $filename;
257 }
258 return $return;
259 }
260 }
261
262 }
263
264 }
265