Skip to content

Commit da70653

Browse files
Hotfix: Ready for RC 3.14
1 parent ce03464 commit da70653

File tree

7 files changed

+409
-267
lines changed

7 files changed

+409
-267
lines changed
+254
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
<?php
2+
3+
namespace MarkupMarkdown\Abstracts;
4+
5+
defined( 'ABSPATH' ) || exit;
6+
7+
8+
abstract class ImageTinyAPI {
9+
10+
11+
# public function __construct() {
12+
# Overriden, nothing to do here mate !
13+
# }
14+
15+
16+
17+
/**
18+
* Get an attachment ID given a URL
19+
*
20+
* @since 3.14
21+
* @access protected
22+
* @link https://gist.github.com/wpscholar/3b00af01863c9dc562e5#file-get-attachment-id-php
23+
*
24+
* @param string $url
25+
* @return int Attachment ID on success, 0 on failure
26+
*/
27+
protected function get_attachment_id( $url ) {
28+
if ( strpos( $url, $this->upload_dir[ 'baseurl' ] . '/' ) === false ) :
29+
# $url does not contain the upload directory
30+
return 0;
31+
endif;
32+
$file = basename( $url );
33+
$query_args = [ 'post_type' => 'attachment', 'post_status' => 'inherit', 'fields' => 'ids', 'meta_query' => [ 'value' => $file, 'compare' => 'LIKE', 'key' => '_wp_attachment_metadata' ] ];
34+
$query = new \WP_Query( $query_args );
35+
if ( ! $query->have_posts() ) :
36+
return 0;
37+
endif;
38+
$attachment_id = 0;
39+
foreach ( $query->posts as $post_id ) :
40+
$meta = \wp_get_attachment_metadata( $post_id );
41+
$original_file = preg_replace( '#-scaled\.([a-z0-9]+)$#i', '.$1', basename( $meta[ 'file' ] ) );
42+
$cropped_image_files = \wp_list_pluck( $meta[ 'sizes' ], 'file' );
43+
if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) :
44+
$attachment_id = $post_id;
45+
break;
46+
endif;
47+
endforeach;
48+
return $attachment_id;
49+
}
50+
51+
52+
/**
53+
* Retrieve the WP Attachment ID if already cached
54+
*
55+
* @since 3.14
56+
* @access protected
57+
*
58+
* @param string $img_src The image source URL
59+
* @return Integer The attachment ID
60+
*/
61+
protected function get_cached_asset_id( $img_src = '' ) {
62+
if ( empty( $img_src ) ) :
63+
return 0;
64+
elseif ( ! preg_match( '#^/#', $img_src ) && strpos( $img_src, $this->home_url ) === false ) :
65+
return 0;
66+
endif;
67+
$asset_cached_id = $this->asset_cache_dir . '/' . md5( $img_src ) . '.txt';
68+
if ( file_exists( $asset_cached_id ) ) :
69+
return (int)file_get_contents( $asset_cached_id );
70+
endif;
71+
$img_src = $this->home_url . str_replace( $this->home_url, '', preg_replace( '#-(\d+)x(\d+)\.(\w+)$#', '.$3', $img_src ) ); # Wordpress images
72+
$img_id = $this->get_attachment_id( $img_src );
73+
if ( (int)$img_id > 0 ) :
74+
touch( $asset_cached_id );
75+
file_put_contents( $asset_cached_id, $img_id );
76+
endif;
77+
return (int)$img_id;
78+
}
79+
80+
81+
/**
82+
* Extract the text and check for caption data
83+
*
84+
* @since 3.14
85+
* @access protected
86+
*
87+
* @param string|undefined $caption Current image alternative text
88+
* @return array Text used for the image's alternative text and its related caption
89+
*/
90+
protected function check_alt_attribute( $caption ) {
91+
if ( ! isset( $caption ) && empty( $caption ) ) :
92+
return [];
93+
endif;
94+
if ( strpos( $caption, '--' ) !== false ) :
95+
$text = explode( '--', $caption );
96+
return [
97+
'alt' => trim( $text[ 0 ] ),
98+
'caption' => trim( $text[ 1 ] )
99+
];
100+
else :
101+
return [
102+
'alt' => trim( $caption )
103+
];
104+
endif;
105+
}
106+
107+
108+
/**
109+
* Check the align attribute extracted from the HTML image align attribute or an HTML link class attribute if available
110+
*
111+
* @since 3.14
112+
* @access protected
113+
*
114+
* @param string
115+
* @return array WP valide align value
116+
*/
117+
protected function check_align_attribute( $align = '' ) {
118+
if ( ! isset( $align ) || empty( $align ) ) :
119+
return [];
120+
endif;
121+
if ( in_array( $align, array( 'none', 'left', 'right', 'center' ) ) ) :
122+
return [
123+
'align' => $align
124+
];
125+
endif;
126+
}
127+
128+
129+
/**
130+
* Check the width value extracted from the HTML image's width attribute if available
131+
*
132+
* @since 3.14
133+
* @access protected
134+
*
135+
* @param array $width Extracted values of the image's width attribute
136+
* @param string $src Extracted value of the image's source
137+
* @return array Requested width number
138+
*/
139+
protected function check_width_attribute( $width = [], $src = '' ) {
140+
if ( isset( $width ) && is_array( $width ) && isset( $width[ 1 ] ) && is_numeric( $width[ 1 ] ) && (int)$width[ 1 ] > 0 ) :
141+
# Check first value extracted from the width's attribute
142+
return [
143+
'width' => (int)$width[ 1 ]
144+
];
145+
endif;
146+
$img_width = [];
147+
if ( isset( $src ) && ! empty( $src ) && preg_match( '#(\d+)x\d+\.[a-zA-Z0-9]+$#', $src, $img_width ) ) :
148+
# As a fallback try to extract the width from the thumbnail
149+
if ( isset( $img_width ) && is_array( $img_width ) && isset( $img_width[ 1 ] ) ) :
150+
return [
151+
'width' => (int)$img_width[ 1 ]
152+
];
153+
endif;
154+
endif;
155+
return [];
156+
}
157+
158+
159+
/**
160+
* Check the height value extracted from the HTML image's width attribute if available
161+
*
162+
* @since 3.14
163+
* @access protected
164+
*
165+
* @param array $height Extracted value of the image's height attribute
166+
* @param string $src Extracted value of the image's source
167+
* @return array Requested height number
168+
*/
169+
protected function check_height_attribute( $height = [], $src = '' ) {
170+
if ( !isset( $height ) && is_array( $height ) && isset( $height[ 1 ] ) && is_numeric( $height[ 1 ] ) && (int)$height[ 1 ] > 0 ) :
171+
# Check first value extracted from the height's attribute
172+
return [
173+
'height' => (int)$height[ 1 ]
174+
];
175+
endif;
176+
$img_height = [];
177+
if ( isset( $src ) && ! empty( $src ) && preg_match( '#\d+x(\d+)\.[a-zA-Z0-9]+$#', $src, $img_height ) ) :
178+
# As a fallback try to extract the height from the thumbnail
179+
if ( isset( $img_height ) && is_array( $img_height ) && isset( $img_height[ 1 ] ) ) :
180+
return [
181+
'height' => (int)$img_height[ 1 ]
182+
];
183+
endif;
184+
endif;
185+
return [];
186+
}
187+
188+
189+
/**
190+
* Following latest Gutenberg / Theme specification, the image is wrapped by a *figure* with a *figcaption* if need be
191+
*
192+
* @param integer $img_id WP Attachment ID
193+
* @param array $img_attrs Image related attribute
194+
* @return string Modified image tag
195+
*/
196+
protected function wrap_image( $img_id = 0, $img_attrs = [] ) {
197+
if ( ! isset( $img_id ) || ! (int)$img_id || ! isset( $img_attrs ) || ! is_array( $img_attrs ) ) :
198+
return '';
199+
endif;
200+
$img_size = 'full';
201+
if ( isset( $img_attrs[ 'width' ] ) && isset( $img_attrs[ 'height' ] ) ) :
202+
$img_size = [ $img_attrs[ 'width' ], $img_attrs[ 'height' ] ];
203+
unset( $img_attrs[ 'width' ] ); unset( $img_attrs[ 'height' ] );
204+
endif;
205+
$img_caption = '';
206+
if ( isset( $img_attrs[ 'caption' ] ) ) :
207+
$img_caption = trim( $img_attrs[ 'caption' ] );
208+
unset( $img_attrs[ 'caption' ] );
209+
endif;
210+
return '<figure id="attachment_mmd_' . $img_id . '" '
211+
. ( ! empty( $img_caption ) ? 'aria-describedby="caption-attachment-mmd' . $img_id . '" class="wp-block-image wp-caption ' : 'class="wp-block-image ' )
212+
. ( isset( $img_attrs[ 'align' ] ) ? 'align' . $img_attrs[ 'align' ] : '' )
213+
. '">' . \wp_get_attachment_image( $img_id, $img_size, false, $img_attrs )
214+
. ( ! empty( $img_caption ) ? '<figcaption id="caption-attachment-mmd' . $img_id . '" class="wp-caption-text wp-element-caption">' . trim( $img_caption ) . '</figcaption>' : '' )
215+
. '</figure>';
216+
}
217+
218+
219+
/**
220+
* Replace HTML image tags with customized wordpress generated version
221+
*
222+
* @since 3.14
223+
* @access protected
224+
*
225+
* @param string $html Target HTML snippet to parse
226+
* @param string $img_src Image original source
227+
* @return string Modified html image
228+
*/
229+
protected function native_wp_image( $img_id = 0, $img_src = '', $img_html = '' ) {
230+
if ( ! isset( $img_id ) || ! (int)$img_id || ! isset( $img_html ) || empty( $img_html ) || ! isset( $img_src ) || empty( $img_src ) ) :
231+
return '';
232+
endif;
233+
$img_attrs = [ 'decoding' => 'async', 'loading' => 'lazy' ];
234+
$tmp_args = [];
235+
# Check for captions related values
236+
if ( preg_match( '#alt="(.*?)"#', $img_html, $tmp_args ) === 1 ) :
237+
$img_attrs = array_merge( $img_attrs, $this->check_alt_attribute( $tmp_args[ 1 ] ) );
238+
endif;
239+
# Check for a custom align value
240+
if ( preg_match( '#align([a-z]+)#', $img_html, $tmp_args ) === 1 ) :
241+
$img_attrs = array_merge( $img_attrs, $this->check_align_attribute( $tmp_args[ 1 ] ) );
242+
endif;
243+
# Check for a width value
244+
preg_match( '#width="(.*?)"#', $img_html, $tmp_args );
245+
$img_attrs = array_merge( $img_attrs, $this->check_width_attribute( $tmp_args, $img_src ) );
246+
# Check for a height value
247+
preg_match( '#height="(.*?)"#', $img_html, $tmp_args );
248+
$img_attrs = array_merge( $img_attrs, $this->check_height_attribute( $tmp_args, $img_src ) );
249+
$new_img = $this->wrap_image( $img_id, $img_attrs );
250+
return preg_replace( '#<img[^>]+>#', $new_img, $img_html );
251+
}
252+
253+
254+
}

0 commit comments

Comments
 (0)