-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-image-select-field.php
482 lines (392 loc) · 18.8 KB
/
class-image-select-field.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?php
/*
MIT License
Copyright (c) 2017 Ionata Digital
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
class GFImageSelectField extends GF_Field {
/**
* Sets the field type.
*
* @var string The type of field.
*/
public $type = 'image_select';
public function __construct( $data = array() ) {
parent::__construct( $data );
// TODO(evo): work out if the only way to manage inputs is this action ???
add_action( 'gform_editor_js_set_default_values', array( &$this, 'set_default_values' ) );
// $this->set_inputs();
}
//*
function set_default_values() { ?>
case 'image_select' :
if (!field.label) {
field.label = <?php echo json_encode( esc_html__( 'Image Select', GF_IMAGE_SELECT_DOMAIN ) ); ?>;
}
var attachmentId = new Input(field.id + '.1', <?php echo json_encode( gf_apply_filters( array( 'gf_image_select_attachment_id', rgget( 'id' ) ), esc_html__( 'ID', GF_IMAGE_SELECT_DOMAIN ), rgget( 'id' ) ) ) ?>);
var attachmentSrc = new Input(field.id + '.2', <?php echo json_encode( gf_apply_filters( array( 'gf_image_select_attachment_id', rgget( 'id' ) ), esc_html__( 'Source', GF_IMAGE_SELECT_DOMAIN ) ) ) ?>);
var width = new Input(field.id + '.3', <?php echo json_encode( gf_apply_filters( array( 'gf_image_select_attachment_id', rgget( 'id' ) ), esc_html__( 'Width', GF_IMAGE_SELECT_DOMAIN ) ) ) ?>);
var height = new Input(field.id + '.4', <?php echo json_encode( gf_apply_filters( array( 'gf_image_select_attachment_id', rgget( 'id' ) ), esc_html__( 'Height', GF_IMAGE_SELECT_DOMAIN ) ) ) ?>);
var crop = new Input(field.id + '.5', <?php echo json_encode( gf_apply_filters( array( 'gf_image_select_attachment_id', rgget( 'id' ) ), esc_html__( 'Crop', GF_IMAGE_SELECT_DOMAIN ) ) ) ?>);
field.inputs = [attachmentId, attachmentSrc, width, height, crop];
break;
<?php
}
/**/
/*
private function set_inputs() {
if ( $this->id ) {
$inputs = array(
array(
'id' => $this->id . '.1',
'label' => __( 'ID', GF_IMAGE_SELECT_DOMAIN )
),
array(
'id' => $this->id . '.2',
'label' => __( 'Source', GF_IMAGE_SELECT_DOMAIN )
),
array(
'id' => $this->id . '.3',
'label' => __( 'Width', GF_IMAGE_SELECT_DOMAIN )
),
array(
'id' => $this->id . '.4',
'label' => __( 'Height', GF_IMAGE_SELECT_DOMAIN )
),
array(
'id' => $this->id . '.5',
'label' => __( 'Crop', GF_IMAGE_SELECT_DOMAIN )
),
);
$this->inputs = $inputs;
}
}
/**/
/**
* Return the field title, for use in the form editor.
*
* @return string
*/
public function get_form_editor_field_title() {
return esc_attr__( 'Image Select', GF_IMAGE_SELECT_DOMAIN );
}
/**
* Assign the Coupon button to the Pricing Fields group.
*
* @return array
*/
public function get_form_editor_button() {
return array(
'group' => 'advanced_fields',
'text' => $this->get_form_editor_field_title(),
);
}
/**
* Return the settings which should be available on the field in the form editor.
*
* @return array
*/
function get_form_editor_field_settings() {
return array(
'css_class_setting',
'description_setting',
// 'file_extensions_setting',
'label_placement_setting',
'label_setting',
'prepopulate_field_setting',
// 'size_setting',
// 'default_value_setting',
// 'conditional_logic_field_setting',
// 'error_message_setting',
// 'admin_label_setting',
// 'rules_setting',
// 'visibility_setting',
// 'duplicate_setting',
// 'placeholder_setting',
// 'phone_format_setting',
// 'next_button_setting',
// 'previous_button_setting',
);
}
/**
* Outputs any inline scripts to be used when the page is rendered.
*
* @param array $form The Form Object.
* @return string The inline scripts.
*/
public function get_form_inline_script_on_page_render( $form ) {
$is_entry_detail = $this->is_entry_detail();
$is_form_editor = $this->is_form_editor();
$is_admin = $is_entry_detail || $is_form_editor;
$form_id = absint( $form['id'] );
$id = absint( $this->id );
$field_id = $is_admin || $form_id == 0 ? "$id" : "{$form_id}_$id";
add_filter( 'media_view_strings', array( &$this, 'custom_media_uploader' ) );
wp_enqueue_media();
$ver = '1.0.0';
$min = '.min';
// $min = '';
wp_enqueue_script( 'cropperjs', plugins_url( "cropperjs/cropper$min.js", __FILE__ ), $deps = array(), $ver, $in_footer = true );
wp_enqueue_style( 'cropperjs', plugins_url( "cropperjs/cropper$min.css", __FILE__ ), $deps = array(), $ver );
$ver = GF_IMAGE_SELECT_VERSION;
// $ver = rand( 10000, 20000 );
wp_enqueue_script( 'gf_image_select', plugins_url( 'image-select.js', __FILE__ ), $deps = array( 'jquery', 'cropperjs' ), $ver, $in_footer = true );
wp_enqueue_style( 'gf_image_select', plugins_url( 'image-select.css', __FILE__ ), $deps = array(), $ver );
return ";new GFImageSelect({ key: '$field_id' });";
}
/**
* Returns the field inner markup.
*
* @param array $form The Form Object currently being processed.
* @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission.
* @param null|array $entry Null or the Entry Object currently being edited.
* @return string The field input HTML markup.
*/
public function get_field_input( $form, $value = '', $entry = null ) {
$is_entry_detail = $this->is_entry_detail();
$is_form_editor = $this->is_form_editor();
$is_admin = $is_entry_detail || $is_form_editor;
$form_id = absint( $form['id'] );
$id = absint( $this->id );
$field_id = $is_admin || $form_id == 0 ? "$id" : "{$form_id}_$id";
$form_id = $is_admin && empty( $form_id ) ? rgget( 'id' ) : $form_id;
$size = $this->size;
$class_suffix = $is_entry_detail ? '_admin' : '';
$class = $size . $class_suffix;
$disabled_text = $is_form_editor ? 'disabled="disabled"' : '';
$extensions_message = '';
// $allowed_extensions = ! empty( $this->allowedExtensions ) ? join( ',', GFCommon::clean_extensions( explode( ',', strtolower( $this->allowedExtensions ) ) ) ) : array();
// if ( ! empty( $allowed_extensions ) ) {
// $extensions_message = esc_attr( sprintf( __( 'Accepted file types: %s.', 'gravityforms' ), str_replace( ',', ', ', $allowed_extensions ) ) );
// }
$attachment_id = '';
$attachment_src = '';
$width = '100';
$height = '100';
$crop = '';
if ( is_array( $value ) ) {
$attachment_id = esc_attr( GFForms::get( $this->id . '.1', $value ) );
$attachment_src = esc_attr( GFForms::get( $this->id . '.2', $value ) );
$width = esc_attr( GFForms::get( $this->id . '.3', $value ) );
$height = esc_attr( GFForms::get( $this->id . '.4', $value ) );
$crop = esc_attr( GFForms::get( $this->id . '.5', $value ) );
}
if ( $is_entry_detail ) {
$input = '';
$input .= "<input type='hidden' name='input_{$id}.1' value='{$attachment_id}' />";
$input .= "<input type='hidden' name='input_{$id}.2' value='{$attachment_src}' />";
$input .= "<input type='hidden' name='input_{$id}.3' value='{$width}' />";
$input .= "<input type='hidden' name='input_{$id}.4' value='{$height}' />";
$input .= "<input type='hidden' name='input_{$id}.5' value='{$crop}' />";
return $input . '<br/>' . esc_html__( 'Image selecton is not editable', GF_IMAGE_SELECT_DOMAIN );
}
if ( $attachment_id ) {
$image = wp_get_attachment_image_src( $attachment_id, 'large' );
if ( $image ) {
$attachment_src = $image[0];
$width = $image[1];
$height = $image[2];
} else {
$attachment_id = '';
$attachment_src = '';
$width = '100';
$height = '100';
}
} else if ( ! $attachment_src ) {
$width = '100';
$height = '100';
}
$has_src = '';
if ( $attachment_src ) {
$has_src = 'gf_image_select_has_src';
$img_src = $attachment_src;
} else {
$img_src = '//:0';
}
// $required_attribute = $this->isRequired ? 'aria-required="true"' : '';
// $invalid_attribute = $this->failed_validation ? 'aria-invalid="true"' : 'aria-invalid="false"';
// $logic_event = $this->get_conditional_logic_event( 'change' );
// $tabindex1 = $this->get_tabindex();
// $tabindex2 = $this->get_tabindex();
$no_display_admin = $is_admin ? "style='display:none'" : '';
$non_admin_html = '';
if ( ! $is_admin ) {
$non_admin_html .= "<span id='extensions_message' class='screen-reader-text'>{$extensions_message}</span>"; // NOTE(evo): screen reader only
}
$string_button = esc_attr__( 'Select image', GF_IMAGE_SELECT_DOMAIN );
$string_choose = esc_attr__( 'Select an image', GF_IMAGE_SELECT_DOMAIN );
$string_update = esc_attr__( 'Use this image', GF_IMAGE_SELECT_DOMAIN );
$string_crop = esc_attr__( 'Preview', GF_IMAGE_SELECT_DOMAIN );
$string_back = esc_attr__( 'Back', GF_IMAGE_SELECT_DOMAIN );
// gfield_trigger_change
$input = <<<TEMPLATE
<div class='ginput_complex$class_suffix ginput_container ginput_container_image_select $class $has_src' id='gf_image_select_container_{$field_id}'>
$non_admin_html
<input type='hidden' name='input_{$id}.1' id='gf_image_select_{$field_id}_1' value='$attachment_id' />
<input type='hidden' name='input_{$id}.2' id='gf_image_select_{$field_id}_2' value='$attachment_src' />
<input type='hidden' name='input_{$id}.3' id='gf_image_select_{$field_id}_3' value='$width' />
<input type='hidden' name='input_{$id}.4' id='gf_image_select_{$field_id}_4' value='$height' />
<input type='hidden' name='input_{$id}.5' id='gf_image_select_{$field_id}_5' value='$crop' />
<div class='gf_image_select_crop_wrapper'>
<img id='gf_image_select_crop_{$field_id}' src='$img_src' width='$width' height='$height' class='gf_image_select_crop' />
</div>
<div id='gf_image_select_preview_{$field_id}' class='gf_image_select_preview'></div>
<div class='gf_image_select_buttons'>
<button
type='button'
id='gf_image_select_button_crop_{$field_id}'
class='button gf_image_select_button gf_image_select_button_crop'
$disabled_text
$no_display_admin
>$string_crop</button>
<button
type='button'
id='gf_image_select_button_back_{$field_id}'
class='button gf_image_select_button gf_image_select_button_back'
$disabled_text
$no_display_admin
>$string_back</button>
<button
type='button'
id='gf_image_select_button_{$field_id}'
class='button gf_image_select_button gf_image_select_button_select'
data-choose='$string_choose'
data-update='$string_update'
$disabled_text
>$string_button</button>
</div>
<div class='gf_clear gf_clear_complex'></div>
</div>
TEMPLATE;
return $input;
}
/**
* Re-validate the attachment id and crop region.
*
* @param string $value The field value from the $_POST
* @param array $form The form object currently being processed.
*/
public function validate( $value, $form ) {
if ( empty( $value ) ) {
return;
}
$message = '';
if ( is_array( $value ) ) {
$attachment_id = trim( rgget( $this->id . '.1', $value ) );
$attachment_src = trim( rgget( $this->id . '.2', $value ) );
$width = trim( rgget( $this->id . '.3', $value ) );
$height = trim( rgget( $this->id . '.4', $value ) );
$crop = trim( rgget( $this->id . '.5', $value ) );
if ( $attachment_id ) {
$image = wp_get_attachment_image_src( $attachment_id, 'large' );
if ( ! $image || ! $attachment_src ) {
$message = __( 'Invalid attachment', GF_IMAGE_SELECT_DOMAIN );
} else if ( $attachment_src !== $image[0] ) {
$message = __( 'Invalid source', GF_IMAGE_SELECT_DOMAIN );
} else if ( ! $crop ) {
$message = __( 'Missing crop region', GF_IMAGE_SELECT_DOMAIN );
} else {
$_crop = json_decode( $crop );
if ( ! isset( $_crop[0] ) || ! isset( $_crop[1] ) || ! isset( $_crop[2] ) || ! isset( $_crop[3] ) ) {
$message = __( 'Ivalid crop region', GF_IMAGE_SELECT_DOMAIN );
}
}
} else {
$message = __( 'Missing attachment', GF_IMAGE_SELECT_DOMAIN );
}
}
if ( ! empty( $message ) ) {
$this->failed_validation = true;
$this->validation_message = empty( $this->errorMessage ) ? $message : $this->errorMessage;
}
}
/**
* Gets the field value to be displayed on the entry detail page.
*
* @param array|string $value The value of the field input.
* @param string $currency Not used.
* @param bool $use_text Not used.
* @param string $format The format to output the value. Defaults to 'html'.
* @param string $media Not used.
* @return array|string The value to be displayed on the entry detail page.
*/
public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
if ( is_array( $value ) ) {
$attachment_id = trim( rgget( $this->id . '.1', $value ) );
$attachment_src = trim( rgget( $this->id . '.2', $value ) );
$width = trim( rgget( $this->id . '.3', $value ) );
$height = trim( rgget( $this->id . '.4', $value ) );
$crop = trim( rgget( $this->id . '.5', $value ) );
$image = null;
$src_match_id = false;
$ratio = 0;
if ( $attachment_id ) {
$image = wp_get_attachment_image_src( $attachment_id, 'large' );
$src_match_id = $image && $attachment_src === $image[0];
if ( $width > 0 ) {
$ratio = $image[1] / $width;
}
}
if ( $format === 'html' ) {
$return = '<div style="position:relative;">';
if ( $attachment_id && $src_match_id ) {
$thumb = wp_get_attachment_image( $attachment_id, 'large' );
$return .= sprintf( '<a href="%s" target="_blank">%s</a>', $image[0], $thumb );
} else if ( $attachment_src && ! $src_match_id ) {
$return .= sprintf( '<a href="%s" target="_blank">%s</a>', esc_attr( $attachment_src ), esc_html( $attachment_src ) );
if ( $width && $height ) {
$return .= sprintf( '<br/>Width: %s, Height: %s', esc_html( $width ), esc_html( $height ) );
}
}
if ( $crop ) {
list( $cx, $cy, $cw, $ch ) = json_decode( $crop );
if ( $ratio > 0 ) {
$return .= sprintf( '<div style="position:absolute;left:%0.3fpx;top:%0.3fpx;width:%0.3fpx;height:%0.3fpx;border:2px solid rgba(255,255,255,.7);box-shadow:0 2px 5px rgba(0,0,0,0.5);"></div>', $cx * $ratio, $cy * $ratio, $cw * $ratio, $ch * $ratio );
}
$return .= sprintf( '<br/>Crop: x: %0.3f, y: %0.3f, w: %0.3f, h: %0.3f', $cx, $cy, $cw, $ch );
}
$return .= sprintf( '<br/>Dimensions: %dx%d', $width, $height );
$return .= '</div>';
} else {
$return = $attachment_src;
if ( $crop ) {
list( $cx, $cy, $cw, $ch ) = json_decode( $crop );
$return .= "\n" . sprintf( '<br/>Crop: x: %0.3f, y: %0.3f, w: %0.3f, h: %0.3f', $cx, $cy, $cw, $ch );
}
}
} else {
$return = $value;
}
return $return;
}
/**
* Configure the Media Picker
*
* @param array $strings
* @return array
*/
public function custom_media_uploader( $strings ) {
unset( $strings['selected'] ); //Removes Upload Files & Media Library links in Insert Media tab
unset( $strings['insertMediaTitle'] ); //Insert Media
unset( $strings['uploadFilesTitle'] ); //Upload Files
// unset( $strings['mediaLibraryTitle'] ); //Media Library
unset( $strings['createGalleryTitle'] ); //Create Gallery
unset( $strings['setFeaturedImageTitle'] ); //Set Featured Image
unset( $strings['insertFromUrlTitle'] ); //Insert from URL
return $strings;
}
}