Diff: STRATO-apps/wordpress_03/app/wp-content/themes/blocksy/inc/classes/class-ct-attributes-parser.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/**
3
+
* Parse attributes in images
4
+
*
5
+
* @copyright 2019-present Creative Themes
6
+
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
7
+
* @package Blocksy
8
+
*/
9
+
10
+
/**
11
+
* Simple parser for images attributes.
12
+
*/
13
+
class Blocksy_Attributes_Parser {
14
+
/**
15
+
* Add attribute to images with tag.
16
+
*
17
+
* @param string $content content to replaces images into.
18
+
* @param string $attribute_name - attribute name.
19
+
* @param string $attribute_value - attribute value.
20
+
*/
21
+
public function add_attribute_to_images(
22
+
$content,
23
+
$attribute_name,
24
+
$attribute_value
25
+
) {
26
+
$new_content = $this->add_attribute_to_images_with_tag(
27
+
$content,
28
+
$attribute_name,
29
+
$attribute_value,
30
+
'img'
31
+
);
32
+
33
+
return $this->add_attribute_to_images_with_tag(
34
+
$new_content,
35
+
$attribute_name,
36
+
$attribute_value,
37
+
'source'
38
+
);
39
+
}
40
+
41
+
/**
42
+
* Rename attribute from images with tag.
43
+
*
44
+
* @param string $content content to replaces images into.
45
+
* @param string $attribute - attribute name.
46
+
*/
47
+
public function remove_attribute_from_images( $content, $attribute ) {
48
+
$new_content = $this->remove_attribute_from_images_with_tag(
49
+
$content,
50
+
$attribute,
51
+
'img'
52
+
);
53
+
54
+
return $this->remove_attribute_from_images_with_tag(
55
+
$new_content,
56
+
$attribute,
57
+
'source'
58
+
);
59
+
}
60
+
61
+
/**
62
+
* Rename attribute from images with tag.
63
+
*
64
+
* @param string $content content to replaces images into.
65
+
* @param string $old_attribute_name - attribute name.
66
+
* @param string $new_attribute_name - attribute name.
67
+
*/
68
+
public function rename_attribute_from_images(
69
+
$content,
70
+
$old_attribute_name,
71
+
$new_attribute_name
72
+
) {
73
+
$new_content = $this->rename_attribute_from_images_with_tag(
74
+
$content,
75
+
$old_attribute_name,
76
+
$new_attribute_name,
77
+
'img'
78
+
);
79
+
80
+
return $this->rename_attribute_from_images_with_tag(
81
+
$new_content,
82
+
$old_attribute_name,
83
+
$new_attribute_name,
84
+
'source'
85
+
);
86
+
}
87
+
88
+
/**
89
+
* Rename attribute from images with tag.
90
+
*
91
+
* @param string $content content to replaces images into.
92
+
* @param string $old_attribute_name - attribute name.
93
+
* @param string $new_attribute_name - attribute name.
94
+
* @param string $tag - img | source.
95
+
*/
96
+
private function rename_attribute_from_images_with_tag(
97
+
$content,
98
+
$old_attribute_name,
99
+
$new_attribute_name,
100
+
$tag = 'img'
101
+
) {
102
+
if ( ! preg_match_all( '/<' . $tag . ' [^>]+>/', $content, $matches ) ) {
103
+
return $content;
104
+
}
105
+
106
+
$selected_images = array();
107
+
108
+
foreach ( $matches[0] as $image ) {
109
+
$selected_images[] = $image;
110
+
}
111
+
112
+
foreach ( $selected_images as $image ) {
113
+
$content = str_replace(
114
+
$image,
115
+
$this->rename_attribute_for_single_image(
116
+
$image,
117
+
$old_attribute_name,
118
+
$new_attribute_name,
119
+
$tag
120
+
),
121
+
$content
122
+
);
123
+
}
124
+
125
+
return $content;
126
+
}
127
+
128
+
/**
129
+
* Add specific attribute to an image. Tag that has to be parsed is specified.
130
+
*
131
+
* @param string $content content to replace images into.
132
+
* @param string $attribute_name attribute name.
133
+
* @param string $attribute_value attribute value.
134
+
* @param string $tag img | source.
135
+
*/
136
+
public function add_attribute_to_images_with_tag(
137
+
$content,
138
+
$attribute_name,
139
+
$attribute_value,
140
+
$tag = 'img',
141
+
$self_closing = true
142
+
) {
143
+
if (! preg_match_all('/<' . $tag . ' [^>]+>/', $content, $matches)) {
144
+
return $content;
145
+
}
146
+
147
+
$selected_images = array();
148
+
149
+
foreach ($matches[0] as $image) {
150
+
$selected_images[] = $image;
151
+
}
152
+
153
+
foreach ($selected_images as $image) {
154
+
$content = str_replace(
155
+
$image,
156
+
$this->add_attribute_to_single_image(
157
+
$image,
158
+
$attribute_name,
159
+
$attribute_value,
160
+
$tag,
161
+
$self_closing
162
+
),
163
+
$content
164
+
);
165
+
}
166
+
167
+
return $content;
168
+
}
169
+
170
+
/**
171
+
* Remove attribute from a specific HTML tag.
172
+
*
173
+
* @param string $content content to replaces images into.
174
+
* @param string $attribute attribute name.
175
+
* @param string $tag - img | source.
176
+
*/
177
+
public function remove_attribute_from_images_with_tag(
178
+
$content,
179
+
$attribute,
180
+
$tag = 'img'
181
+
) {
182
+
if ( ! preg_match_all( '/<' . $tag . ' [^>]+>/', $content, $matches ) ) {
183
+
return $content;
184
+
}
185
+
186
+
$selected_images = array();
187
+
188
+
foreach ( $matches[0] as $image ) {
189
+
$selected_images[] = $image;
190
+
}
191
+
192
+
foreach ( $selected_images as $image ) {
193
+
$content = str_replace(
194
+
$image,
195
+
$this->remove_attribute_from_single_image(
196
+
$image,
197
+
$attribute,
198
+
$tag
199
+
),
200
+
$content
201
+
);
202
+
}
203
+
204
+
return $content;
205
+
}
206
+
207
+
/**
208
+
* Remove existing $attribute from <img> html, if it exists.
209
+
*
210
+
* @param string $image - image HTML.
211
+
* @param string $attribute - attribute name.
212
+
* @param string $tag - img | source.
213
+
*/
214
+
public function remove_attribute_from_single_image(
215
+
$image,
216
+
$attribute,
217
+
$tag = 'img'
218
+
) {
219
+
return preg_replace(
220
+
'/(\\<' .
221
+
$tag .
222
+
'[^>]*)(\\s?' .
223
+
$attribute .
224
+
'\\="[^"]*"\\s?)([^>]+)(>)/',
225
+
'${1}${3}${4}',
226
+
$image
227
+
);
228
+
}
229
+
230
+
/**
231
+
* Remove existing $attribute from <img> html, if it exists.
232
+
*
233
+
* @param string $image - image HTML.
234
+
* @param string $old_attribute_name - attribute name.
235
+
* @param string $new_attribute_name - attribute name.
236
+
* @param string $tag - img | source.
237
+
*/
238
+
public function rename_attribute_for_single_image(
239
+
$image,
240
+
$old_attribute_name,
241
+
$new_attribute_name,
242
+
$tag = 'img'
243
+
) {
244
+
$old_attribute_value = ltrim(
245
+
rtrim(
246
+
trim(
247
+
preg_replace(
248
+
'/(\\<' .
249
+
$tag .
250
+
'[^>]+)(\\s?' .
251
+
$old_attribute_name .
252
+
'\\="[^"]+"\\s?)([^>]+)(>)/',
253
+
'${2}',
254
+
$image
255
+
)
256
+
),
257
+
'"'
258
+
),
259
+
$old_attribute_name . '="'
260
+
);
261
+
262
+
$removed = $this->remove_attribute_from_images(
263
+
$image,
264
+
$old_attribute_name
265
+
);
266
+
267
+
$res = $this->add_attribute_to_single_image(
268
+
$removed,
269
+
$new_attribute_name,
270
+
$old_attribute_value,
271
+
$tag
272
+
);
273
+
274
+
return $res;
275
+
}
276
+
277
+
/**
278
+
* Add an attribute with a specific value an img element. Remove the
279
+
* attribute if it exists already.
280
+
*
281
+
* @param string $image - image HTML.
282
+
* @param string $attribute_name attribute name that will be set.
283
+
* @param string $attribute_value value for the attribute.
284
+
* @param string $tag - img | source.
285
+
*/
286
+
public function add_attribute_to_single_image(
287
+
$image,
288
+
$attribute_name,
289
+
$attribute_value,
290
+
$tag = 'img',
291
+
$self_closing = true
292
+
) {
293
+
$attr = blocksy_safe_sprintf(
294
+
' %s="%s"',
295
+
esc_attr($attribute_name),
296
+
esc_attr($attribute_value)
297
+
);
298
+
299
+
if ($self_closing) {
300
+
$val = preg_replace(
301
+
'/<' . $tag . ' ([^>]+?)[\\/ ]*>/',
302
+
'<' . $tag . ' $1' . $attr . ' />',
303
+
$this->remove_attribute_from_images($image, $attribute_name)
304
+
);
305
+
} else {
306
+
$val = preg_replace(
307
+
'/<' . $tag . ' ([^>]+?)[\\/ ]*>/',
308
+
'<' . $tag . ' $1' . $attr . ' >',
309
+
$this->remove_attribute_from_images($image, $attribute_name)
310
+
);
311
+
}
312
+
313
+
return $val;
314
+
}
315
+
}
316
+
317
+