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