-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-gravity-forms-signature.php
147 lines (119 loc) · 4.38 KB
/
class-gravity-forms-signature.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace SLCA\GravityFormSignature;
use wpCloud\StatelessMedia\Compatibility;
/**
* Class GravityFormSignature
*/
class GravityFormSignature extends Compatibility {
const GF_SIGNATURE_PATH = 'gravity_forms/signatures/';
protected $id = 'gravity-form-signature';
protected $title = 'Gravity Forms Signature Add-On';
protected $constant = 'WP_STATELESS_COMPATIBILITY_GF_SIG';
protected $description = 'Enables support for signature images generated by the signature add-on for Gravity Forms.';
protected $plugin_file = 'gravityformssignature/signature.php';
protected $plugin_version;
/**
* @param $sm
*/
public function module_init($sm) {
$this->plugin_version = defined('GF_SIGNATURE_VERSION') ? GF_SIGNATURE_VERSION : '';
add_filter('gform_save_field_value', array($this, 'gform_save_field_value'), 10, 5);
add_filter('gform_signature_delete_file_pre_delete_entry', array($this, 'delete_signature'), 10, 4);
add_filter('gform_signature_url', array($this, 'get_signature_url'), 10, 4);
add_filter('sm:sync::syncArgs', array($this, 'sync_args'), 10, 4);
}
/**
* On gform save field value sync file to GCS and alter the file url to GCS link.
* @param $value
* @param $lead
* @param $field
* @param $form
* @param $input_id
* @return array|false|mixed|string
*/
public function gform_save_field_value($value, $lead, $field, $form, $input_id) {
if (empty($value)) return $value;
$type = \GFFormsModel::get_input_type($field);
if ($type == 'signature') {
$is_stateless = ud_get_stateless_media()->get('sm.mode') === 'stateless';
try {
$folder = \GFSignature::get_signatures_folder();
$file_path = $folder . $value;
// For stateless mode there is no way to override signature upload path in GFSignature::get_signatures_folder()
// so we have to move the file to the proper location
if ( $is_stateless ) {
$old_path = $file_path;
$file_path = ud_get_stateless_media()->get_gs_path() . '/' . self::GF_SIGNATURE_PATH . $value;
if ( !class_exists('\WP_Filesystem_Direct') ) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php');
}
$filesystem = new \WP_Filesystem_Direct( false );
$res = $filesystem->move($old_path, $file_path, true);
}
$name = self::GF_SIGNATURE_PATH . $value;
$name = apply_filters('wp_stateless_file_name', $name, 0);
do_action( 'sm:sync::syncFile', $name, $file_path, true );
} catch (\Throwable $th) {
}
}
return $value;
}
/**
* [newer version of Gravity Forms Signature]
* Get GSC URL for signature image.
*
* Also doing sync on the fly for previous entries.
*/
public function get_signature_url($url, $filename, $form_id, $field_id) {
$mode = ud_get_stateless_media()->get('sm.mode');
if ( $mode === 'disabled' ) {
return $url;
}
// Sync signature file
try {
$folder = \GFSignature::get_signatures_folder();
$absolute_path = $folder . $filename . '.png';
$name = apply_filters('wp_stateless_file_name', $absolute_path, false);
do_action('sm:sync::syncFile', $name, $absolute_path);
if ( $mode !== 'backup' ) {
$url = ud_get_stateless_media()->get_gs_host() . '/' . $name;
}
} catch (\Throwable $th) {
}
return $url;
}
/**
* Deleting signature file from GCS.
*/
public function delete_signature($return, $form, $lead_id, $field_id) {
try {
$lead = \RGFormsModel::get_lead($lead_id);
$name = rgar($lead, $field_id);
do_action('sm:sync::deleteFile', self::GF_SIGNATURE_PATH . $name);
} catch (\Throwable $th) {
}
return $return;
}
/**
* Update args when uploading/syncing GF file to GCS.
*
* @param array $args
* @param string $name
* @param string $file
* @param bool $force
*
* @return array
*/
public function sync_args($args, $name, $file, $force) {
if ( strpos($name, self::GF_SIGNATURE_PATH) === false ) {
return $args;
}
if ( ud_get_stateless_media()->is_mode('stateless') ) {
$args['name_with_root'] = false;
}
$args['source'] = 'Gravity Forms Signature';
$args['source_version'] = $this->plugin_version;
return $args;
}
}