PluginProbe
WP-Stateless – Google Cloud Storage / 3.2.0
WP-Stateless – Google Cloud Storage v3.2.0
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 3.2.0, at lib/classes/compatibility/gravity-forms.php

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