Skip to content

Commit d9e469d

Browse files
author
Md. Alimuzzaman Alim
committed
Gravity form fix #285
1 parent ce0e47f commit d9e469d

File tree

2 files changed

+76
-16
lines changed

2 files changed

+76
-16
lines changed

lib/classes/compatibility/gravity-forms.php

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class GravityForm extends ICompatibility {
2020
protected $plugin_file = 'gravityforms/gravityforms.php';
2121

2222
public function module_init($sm){
23+
$this->plugin_version = \GFForms::$version;
2324
do_action('sm:sync::register_dir', '/gravity_forms/');
2425
add_filter( 'gform_save_field_value', array($this, 'gform_save_field_value'), 10, 5 );
2526
add_action( 'sm::synced::nonMediaFiles', array($this, 'modify_db'), 10, 3);
@@ -37,18 +38,37 @@ public function module_init($sm){
3738
* @param $input_id
3839
*/
3940
public function gform_save_field_value( $value, $lead, $field, $form, $input_id ) {
41+
if(empty($value)) return $value;
4042
$type = \GFFormsModel::get_input_type($field);
4143
if($type == 'fileupload'){
4244
$dir = wp_upload_dir();
43-
$position = strpos($value, 'gravity_forms/');
45+
46+
if ( $field->multipleFiles ) {
47+
$value = json_decode( $value );
48+
}
49+
else{
50+
$value = array($value);
51+
}
4452

45-
if( $position !== false ){
46-
$name = substr($value, $position);
47-
$absolutePath = $dir['basedir'] . '/' . $name;
48-
// doing sync
49-
do_action( 'sm:sync::syncFile', $name, $absolutePath);
50-
$value = ud_get_stateless_media()->get_gs_host() . '/' . $name;
51-
// Todo add filter.
53+
foreach($value as $k => $v){
54+
if(empty($v)) continue;
55+
$position = strpos($v, 'gravity_forms/');
56+
57+
if( $position !== false ){
58+
$name = substr($v, $position);
59+
$absolutePath = $dir['basedir'] . '/' . $name;
60+
// doing sync
61+
do_action( 'sm:sync::syncFile', $name, $absolutePath);
62+
$value[$k] = ud_get_stateless_media()->get_gs_host() . '/' . $name;
63+
// Todo add filter.
64+
}
65+
}
66+
67+
if ( $field->multipleFiles ) {
68+
$value = json_encode( $value );
69+
}
70+
else{
71+
$value = array_pop($value);
5272
}
5373
}
5474
else if($type == 'post_image'){
@@ -70,7 +90,12 @@ public function gform_save_field_value( $value, $lead, $field, $form, $input_id
7090

7191
$value = ud_get_stateless_media()->get_gs_host() . '/' . $_name;
7292
// Todo add filter.
73-
$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' ) );
93+
if(version_compare($this->plugin_version, '2.3', '<')){ // older version
94+
$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' ) );
95+
}
96+
else{ // New version
97+
$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' ) );
98+
}
7499
}, 10, 3);
75100
}
76101
return $value;
@@ -82,9 +107,16 @@ public function gform_save_field_value( $value, $lead, $field, $form, $input_id
82107
*/
83108
public function modify_db( $file_path, $fullsizepath, $media ){
84109
global $wpdb;
110+
$wpdb->hide_errors();
85111
$position = strpos($file_path, 'gravity_forms/');
86112
$is_index = strpos($file_path, 'index.html');
87113

114+
$gf_val_column = 'meta_value';
115+
$gf_table = \GFFormsModel::get_entry_meta_table_name();
116+
if(version_compare($this->plugin_version, '2.3', '<')){
117+
$gf_val_column = 'value';
118+
}
119+
88120
if( $position !== false && !$is_index ){
89121
$dir = wp_upload_dir();
90122
$file_path = trim($file_path, '/');
@@ -97,33 +129,56 @@ public function modify_db( $file_path, $fullsizepath, $media ){
97129
// xyz.jpg|:|tile|:|description|:|
98130
$query = sprintf(
99131
"
100-
SELECT id, value FROM {$wpdb->prefix}rg_lead_detail
101-
WHERE value like '%s';
132+
SELECT id, {$gf_val_column} AS value FROM {$gf_table}
133+
WHERE {$gf_val_column} like '%s';
102134
"
103135
, '%' . $file_path . '%'
104136
);
105137
$results = $wpdb->get_results( $query );
138+
$this->throw_db_error();
106139

107140
foreach ($results as $result) {
108141
$position = strpos($result->value, $dir['baseurl']);
109142
if($position !== false){
110143
$result->value = str_replace($dir['baseurl'], ud_get_stateless_media()->get_gs_host(), $result->value);
111144
$query = sprintf(
112145
"
113-
UPDATE {$wpdb->prefix}rg_lead_detail
114-
SET value = '%s'
146+
UPDATE {$gf_table}
147+
SET {$gf_val_column} = '%s'
115148
WHERE id = %d
116149
"
117150
, $result->value, $result->id
118151
);
119152
$entries = $wpdb->get_results( $query );
153+
$this->throw_db_error();
120154
}
121155

122156
}
123157

124158
}
125159
}
126160

161+
/**
162+
* Throw db error from last db query.
163+
* We need to throw db error instead of just printing,
164+
* so that we can catch them in ajax request.
165+
*/
166+
function throw_db_error(){
167+
168+
global $wpdb;
169+
$wpdb->show_errors();
170+
171+
if($wpdb->last_error !== '' && wp_doing_ajax()) :
172+
ob_start();
173+
$wpdb->print_error();
174+
$error = ob_get_clean();
175+
if($error){
176+
throw new \Exception( $error );
177+
}
178+
endif;
179+
180+
}
181+
127182
/**
128183
* Delete file from GCS
129184
*/

static/scripts/wp-stateless.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ var wpStatelessApp = angular.module('wpStatelessApp', [])
9494
* Get error message
9595
*/
9696
$scope.getError = function(response, message) {
97+
if(response.data && typeof response.data.data !== 'undefined' && typeof response.data.success !== 'undefined' && response.data.success == false){
98+
$scope.extraStatus = response.data.data;
99+
return message;
100+
}
101+
97102
if(response.data && typeof response.data.data !== 'undefined'){
98103
return response.data.data;
99104
}
@@ -621,7 +626,7 @@ var wpStatelessApp = angular.module('wpStatelessApp', [])
621626
}).then(
622627
function(response) {
623628
var data = response.data || {};
624-
$scope.log.push({message:data.data || "Sync single file: Failed"});
629+
$scope.log.push({message: $scope.getError(response, "Sync single file: Failed")});
625630

626631
jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 );
627632
jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" );
@@ -678,7 +683,7 @@ var wpStatelessApp = angular.module('wpStatelessApp', [])
678683
}).then(
679684
function(response) {
680685
var data = response.data || {};
681-
$scope.log.push({message:data.data || "Sync non library file: Failed"});
686+
$scope.log.push({message: $scope.getError(response, "Faild to sync " + id)});
682687

683688
jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 );
684689
jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" );
@@ -712,7 +717,7 @@ var wpStatelessApp = angular.module('wpStatelessApp', [])
712717
$scope.isRunning = false;
713718

714719
if(WP_DEBUG){
715-
console.log("WP-Stateless sync non library file: Request failed", response, response.headers());
720+
console.log("WP-Stateless sync non library file: Request failed", response, typeof response.headers === 'function'?response.headers():{});
716721
}
717722
}
718723
);

0 commit comments

Comments
 (0)