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

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