class-gravity-forms.php
5.29 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Integration with Gravity Forms: Gravity_Forms class
*
* This integration will automatically compress images on Gravity Forms upload.
*
* @since 3.9.10
*
* @package Smush\Core\Integrations
*/
namespace Smush\Core\Integrations;
use GFFormsModel;
use Smush\Core\Core;
use Smush\Core\Helper;
use WP_Smush;
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Class Gravity_Forms for Gravity Forms integration.
*
* This integration will automatically compress images on Gravity Forms upload.
*
* @since 3.9.10
*
* @see Abstract_Integration
*/
class Gravity_Forms extends Abstract_Integration {
/**
* Gravity_Forms constructor.
*
* @since 3.9.10
*/
public function __construct() {
$this->module = 'gform';
$this->class = 'free';
$this->enabled = defined( 'GF_SUPPORTED_WP_VERSION' ) && class_exists( 'GFForms' );
parent::__construct();
// Hook at the end of setting row to output an error div.
add_action( 'smush_setting_column_right_inside', array( $this, 'additional_notice' ) );
// Return if Gravity Form integration or auto compression is not enabled.
if ( ! $this->enabled || ! $this->settings->get( 'gform' ) || ! $this->settings->get( 'auto' ) ) {
return;
}
// Track gravity form submission and validate if there is any image uploaded in Image or File Upload fields.
add_action( 'gform_after_submission', array( $this, 'smush_gform_after_submission' ), 10, 2 );
}
/*
* ************************************
*
* OVERWRITE PARENT CLASS FUNCTIONALITY
*/
/**
* Filters the setting variable to add Gravity Form setting title and description
*
* @since 3.9.10
*
* @param array $settings Settings.
*
* @return array
*/
public function register( $settings ) {
$settings[ $this->module ] = array(
'label' => esc_html__( 'Enable Gravity Forms integration', 'wp-smushit' ),
'short_label' => esc_html__( 'Gravity Forms', 'wp-smushit' ),
'desc' => esc_html__( 'Allow compressing images uploaded with Gravity Forms.', 'wp-smushit' ),
);
return $settings;
}
/**
* Show additional notice if the required plugins are not installed.
*
* @since 3.9.10
*
* @param string $name Setting name.
*/
public function additional_notice( $name ) {
if ( $this->module === $name && ! $this->enabled ) {
?>
<div class="sui-toggle-content">
<div class="sui-notice">
<div class="sui-notice-content">
<div class="sui-notice-message">
<i class="sui-notice-icon sui-icon-info" aria-hidden="true"></i>
<p><?php esc_html_e( 'To use this feature you need be using Gravity Forms.', 'wp-smushit' ); ?></p>
</div>
</div>
</div>
</div>
<?php
}
}
/**
* Processing automatic image smush on Gravity Forms upload.
*
* @since 3.9.10
*
* @param Object $entry Entry Object.
* @param Object $form Form Object.
*/
public function smush_gform_after_submission( $entry, $form ) {
$fields = $form['fields'];
foreach ( $fields as $field ) {
if ( 'fileupload' !== $field->type && 'post_image' !== $field->type ) {
continue;
}
if ( ! function_exists( 'rgar' ) ) {
continue;
}
$uploaded_files = rgar( $entry, $field->id );
$uploaded_files = $this->smush_parse_files( $uploaded_files, $field );
if ( ! is_array( $uploaded_files ) || empty( $uploaded_files ) ) {
continue;
}
foreach ( $uploaded_files as $_file ) {
$dir = $this->get_gform_upload_dir( $form['id'] );
if ( ! $dir || ! isset( $dir['url'] ) || ! isset( $dir['path'] ) ) {
continue;
}
$file = str_replace( $dir['url'], $dir['path'], $_file );
// Get mime type from file path.
$mime = Helper::get_mime_type( $file );
// If image file not exist or image type not supported.
if ( ! file_exists( $file ) || ! in_array( $mime, Core::$mime_types, true ) ) {
continue;
}
WP_Smush::get_instance()->core()->mod->smush->do_smushit( $file );
}
}
}
/**
* Get upload directory url and path.
*
* @since 3.9.10
*
* @param int $form_id Form ID.
* @return bool|array
*/
public function get_gform_upload_dir( $form_id ) {
if ( ! class_exists( 'GFFormsModel' ) ) {
return false;
}
$dir = GFFormsModel::get_file_upload_path( $form_id, 'PLACEHOLDER' );
$dir['path'] = dirname( $dir['path'] );
$dir['url'] = dirname( $dir['url'] );
return $dir;
}
/**
* Parsing uploaded files.
*
* @since 3.9.10
*
* @param mixed $files File path.
* @param Object $field Form field object.
*
* @return array
*/
public function smush_parse_files( $files, $field ) {
if ( empty( $files ) ) {
return array();
}
if ( $this->smush_is_json( $files ) ) {
$files = json_decode( $files );
} elseif ( 'post_image' === $field->get_input_type() ) {
$file_bits = explode( '|:|', $files );
$files = array( $file_bits[0] );
} else {
$files = array( $files );
}
return $files;
}
/**
* Check entry files in JSON format.
*
* @since 3.9.10
*
* @param String $string File string.
*
* @return bool
*/
public function smush_is_json( $string ) {
// Duplicate contents of GFCommon::is_json() here to supports versions of GF older than GF 2.5.
if ( is_string( $string ) && in_array( substr( $string, 0, 1 ), array( '{', '[' ) ) && is_array( json_decode( $string, ARRAY_A ) ) ) {
return true;
}
return false;
}
}