PluginProbe
Hash Form – Drag & Drop Form Builder / trunk
Hash Form – Drag & Drop Form Builder vtrunk
1.4.4 1.4.3 1.4.2 1.4.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.6.1 1.2.7 1.2.8 1.2.9 1.3.0 All 47 releases
hash-form / includes / fields / HashFormFieldImage.php

HashFormFieldImage.php in Hash Form – Drag & Drop Form Builder trunk, at includes/fields/HashFormFieldImage.php

129 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined('ABSPATH') || die();
3
4 class HashFormFieldImage extends HashFormFieldType {
5
6 protected $type = 'image';
7
8 public function field_settings_for_type() {
9 return array(
10 'label' => false,
11 'default' => false,
12 'description' => false,
13 'required' => false,
14 'image' => true,
15 'field_alignment' => true,
16 );
17 }
18
19 protected function extra_field_default_opts() {
20 return array(
21 'image_id' => '',
22 // 'full' is what the field has always served, so an existing image
23 // keeps the size it had; the smaller ones are now a choice.
24 'image_size' => 'full',
25 'image_alt' => '',
26 'field_alignment' => 'left',
27 );
28 }
29
30 /**
31 * The attachment sizes worth offering.
32 *
33 * WordPress makes these for every upload. A theme's own registered sizes
34 * are left out: they are named for where the theme uses them and mean
35 * nothing in a form.
36 */
37 public static function image_sizes() {
38 return array(
39 'thumbnail' => esc_html__('Thumbnail', 'hash-form'),
40 'medium' => esc_html__('Medium', 'hash-form'),
41 'large' => esc_html__('Large', 'hash-form'),
42 'full' => esc_html__('Full Size', 'hash-form'),
43 );
44 }
45
46 private function image_size($field) {
47 $size = isset($field['image_size']) ? $field['image_size'] : '';
48
49 return array_key_exists($size, self::image_sizes()) ? $size : 'full';
50 }
51
52 /**
53 * Where each size of this image lives, for the builder to switch between.
54 *
55 * The canvas is drawn by PHP but the size setting is changed in the
56 * browser, so without this the preview kept whichever size was last saved.
57 */
58 private function size_urls($image_id) {
59 $urls = array();
60
61 foreach (array_keys(self::image_sizes()) as $size) {
62 $src = wp_get_attachment_image_src($image_id, $size);
63
64 if (isset($src[0])) {
65 // Dimensions as well as the file: the preview writes them onto
66 // the element, or a thumbnail is drawn stretched to whatever
67 // the previous size measured.
68 $urls[$size] = array(
69 'url' => $src[0],
70 'width' => isset($src[1]) ? (int) $src[1] : '',
71 'height' => isset($src[2]) ? (int) $src[2] : '',
72 );
73 }
74 }
75
76 return $urls;
77 }
78
79 protected function input_html() {
80 $field = $this->get_field();
81 $image_id = isset($field['image_id']) ? absint($field['image_id']) : 0;
82 $has_image = $image_id && wp_attachment_is_image($image_id);
83
84 /*
85 * With no image there is nothing to draw on the page. The placeholder
86 * used to be written there as well, so every visitor to a form with an
87 * unfinished image field read "IMAGE FIELD - NO IMAGE"; it is kept for
88 * the builder, where it is the only sign the field is there at all.
89 */
90 if (!$has_image && !is_admin()) {
91 return;
92 }
93
94 $attrs = array('class' => 'hf-image-field');
95 $alt = isset($field['image_alt']) ? trim($field['image_alt']) : '';
96
97 // Left empty, the alt text set on the attachment itself is used, which
98 // is what wp_get_attachment_image() does on its own.
99 if ('' !== $alt) {
100 $attrs['alt'] = $alt;
101 }
102 ?>
103 <div class="hf-image-preview-front hf-field-image-<?php echo absint($field['id']); ?>"<?php echo ($has_image && is_admin()) ? ' data-sizes="' . esc_attr(wp_json_encode($this->size_urls($image_id))) . '"' : ''; ?>>
104 <?php
105 if (is_admin()) {
106 ?>
107 <div class="hf-no-image-field<?php echo $has_image ? ' hf-hidden' : ''; ?>">
108 <?php esc_html_e('Image Field - No Image', 'hash-form'); ?>
109 </div>
110 <?php
111 }
112
113 if ($has_image) {
114 /*
115 * wp_get_attachment_image(), rather than an <img> built by
116 * hand from the full-size URL. That tag carried nothing but a
117 * src: no alt for a screen reader to read, no srcset, so a
118 * phone fetched the original at whatever size it happened to
119 * be, and no lazy loading.
120 */
121 echo wp_get_attachment_image($image_id, $this->image_size($field), false, $attrs);
122 }
123 ?>
124 </div>
125 <?php
126 }
127
128 }
129