ImageSetting.php
5.79 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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Settings;
use YahnisElsts\AdminMenuEditor\Customizable\Storage\StorageInterface;
class ImageSetting extends CompositeSetting {
protected $label = 'Image';
/**
* @var array
*/
protected $settings = array();
protected $externalUrlsAllowed = true;
public function __construct($id, StorageInterface $store = null, $params = array()) {
parent::__construct($id, $store, $params);
if ( isset($params['externalUrlsAllowed']) ) {
$this->externalUrlsAllowed = $params['externalUrlsAllowed'];
}
//Image attachment ID.
$this->createChild(
'attachmentId',
IntegerSetting::class,
array('default' => 0, 'minValue' => 0)
);
//Site ID for Multisite.
$this->createChild(
'attachmentSiteId',
IntegerSetting::class,
array('default' => 0, 'minValue' => 0)
);
//Cached attachment URL.
$this->createChild(
'attachmentUrl',
UrlSetting::class,
array('default' => null)
);
//TODO: Allow shortcodes in the URL. This will require more lenient validation (also in JS).
//External image URL. Alternative to attachments.
$this->createChild(
'externalUrl',
UrlSetting::class,
array('default' => null)
);
//Cached width and height for features that need them.
$this->createChild(
'width',
IntegerSetting::class,
array('default' => null, 'minValue' => 0)
);
$this->createChild(
'height',
IntegerSetting::class,
array('default' => null, 'minValue' => 0)
);
}
/**
* Get a fully qualified URL for the image.
*
* @return string|null
*/
public function getImageUrl($useCachedDetails = true) {
//Try the external URL.
$externalUrl = $this->settings['externalUrl']->getValue();
if ( !empty($externalUrl) ) {
return $externalUrl;
}
if ( $useCachedDetails ) {
//Try the cached attachment URL.
$attachmentUrl = $this->settings['attachmentUrl']->getValue();
if ( !empty($attachmentUrl) ) {
return $attachmentUrl;
}
}
$result = $this->getImage($useCachedDetails);
return $result['url'];
}
/**
* Get the URL and dimensions of the image.
*
* If there is no image, returns an array of NULLs.
*
* @return array{url: string, width: int, height: int}
*/
public function getImage($useCachedDetails = true) {
//Prioritize external URLs over attachments.
$externalUrl = $this->settings['externalUrl']->getValue();
if ( !empty($externalUrl) ) {
return array(
'url' => $externalUrl,
'width' => $this->settings['width']->getValue(0),
'height' => $this->settings['height']->getValue(0),
);
}
$attachmentId = $this->settings['attachmentId']->getValue(0);
$siteId = $this->settings['attachmentSiteId']->getValue(0);
if ( ($attachmentId > 0) ) {
if ( $useCachedDetails ) {
$attachmentUrl = $this->settings['attachmentUrl']->getValue();
$width = $this->settings['width']->getValue(0);
$height = $this->settings['height']->getValue(0);
}
if (
//If caching is disabled
!$useCachedDetails
//Or any of the cached details are missing...
|| (empty($attachmentUrl) || empty($width) || empty($height))
) {
//Load the attachment from the database.
list($attachmentUrl, $width, $height) = $this->fetchImageAttachment($attachmentId, $siteId);
}
if ( !empty($attachmentUrl) ) {
return array(
'url' => $attachmentUrl,
'width' => $width,
'height' => $height,
);
}
}
return array('url' => null, 'width' => null, 'height' => null,);
}
public function validate($errors, $value, $stopOnFirstError = false) {
$validatedValues = parent::validate($errors, $value);
if ( is_wp_error($validatedValues) || ($validatedValues === null) ) {
return $validatedValues;
}
//Validate an image attachment.
$attachmentId = isset($validatedValues['attachmentId']) ? intval($validatedValues['attachmentId']) : 0;
$siteId = isset($validatedValues['attachmentSiteId']) ? intval($validatedValues['attachmentSiteId']) : 0;
if ( $attachmentId > 0 ) {
$switched = false;
if ( is_multisite() && ($siteId > 0) && ($siteId !== get_current_blog_id()) ) {
switch_to_blog($siteId);
$switched = true;
}
if ( !wp_attachment_is_image($attachmentId) ) {
$errors->add('invalid_attachment_type', 'Attachment must be a valid image');
}
if ( $switched ) {
restore_current_blog();
}
}
if ( $errors->has_errors() ) {
return $errors;
}
return $validatedValues;
}
/**
* @param int $attachmentId
* @param int $siteId
* @return array{0: string, 1: int, 2: int} URL, width, height. All NULLs if no image.
*/
protected function fetchImageAttachment($attachmentId, $siteId) {
if ( !is_numeric($attachmentId) || ($attachmentId < 0) ) {
return array(null, null, null);
}
$switched = false;
if ( is_multisite() && ($siteId > 0) && ($siteId !== get_current_blog_id()) ) {
switch_to_blog($siteId);
$switched = true;
}
$attachment = wp_get_attachment_image_src($attachmentId, 'full');
if ( $switched ) {
restore_current_blog();
}
if ( empty($attachment) || (count($attachment) < 3) ) {
return array(null, null, null);
}
return array_slice($attachment, 0, 3);
}
public function filterNewValues($values) {
$values = parent::filterNewValues($values);
//If we're using an attachment, cache its size and URL.
$attachmentId = isset($values['attachmentId']) ? intval($values['attachmentId']) : 0;
$siteId = isset($values['attachmentSiteId']) ? intval($values['attachmentSiteId']) : 0;
if ( $attachmentId > 0 ) {
list($url, $width, $height) = $this->fetchImageAttachment($attachmentId, $siteId);
if ( !empty($url) && isset($width, $height) ) {
$values['width'] = $width;
$values['height'] = $height;
$values['attachmentUrl'] = $url;
}
}
return $values;
}
public function areExternalUrlsAllowed() {
return $this->externalUrlsAllowed;
}
}