|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace lloc\Msls\ContentImport; |
|
4
|
|
|
|
|
5
|
|
|
use lloc\Msls\MslsRegistryInstance; |
|
6
|
|
|
|
|
7
|
|
|
class AttachmentPathFinder extends MslsRegistryInstance { |
|
8
|
|
|
|
|
9
|
|
|
const LINKED = '_msls_linked'; |
|
10
|
|
|
|
|
11
|
|
|
public function filter_srcset( array $sources, $sizeArray, $imageSrc, $imageMeta, $attachmentId ) { |
|
|
|
|
|
|
12
|
|
|
if ( ! $msls_imported = $this->has_import_data( $attachmentId ) ) { |
|
13
|
|
|
return $sources; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
$source_post = get_blog_post( $msls_imported['blog'], $msls_imported['post'] ); |
|
17
|
|
|
|
|
18
|
|
|
if ( false === $source_post ) { |
|
19
|
|
|
return $sources; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$extension = '.' . pathinfo( $source_post->guid, PATHINFO_EXTENSION ); |
|
23
|
|
|
$pattern = '/(-[\\d]+x[\\d]+)*' . preg_quote( $extension, '/' ) . '$/'; |
|
24
|
|
|
$srcWithoutExtension = preg_replace( $pattern, '', $imageSrc ); |
|
25
|
|
|
|
|
26
|
|
|
foreach ( $sources as $key => &$value ) { |
|
27
|
|
|
preg_match( $pattern, $value['url'], $matches ); |
|
28
|
|
|
$w_and_h = ! empty( $matches[1] ) ? $matches[1] : ''; |
|
29
|
|
|
$value['url'] = $srcWithoutExtension . $w_and_h . $extension; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $sources; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function has_import_data( $attachment_id ) { |
|
36
|
|
|
if ( empty( $attachment_id ) ) { |
|
37
|
|
|
return false; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$msls_imported = get_post_meta( $attachment_id, self::LINKED, true ); |
|
41
|
|
|
|
|
42
|
|
|
if ( ! ( |
|
43
|
|
|
is_array( $msls_imported ) |
|
44
|
|
|
&& array_key_exists( 'blog', $msls_imported ) |
|
45
|
|
|
&& array_key_exists( 'post', $msls_imported ) |
|
46
|
|
|
) |
|
47
|
|
|
) { |
|
48
|
|
|
delete_post_meta( $attachment_id, self::LINKED ); |
|
49
|
|
|
|
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $msls_imported; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function filter_attachment_url( $url, $attachment_id ) { |
|
57
|
|
|
if ( ! $msls_imported = $this->has_import_data( $attachment_id ) ) { |
|
58
|
|
|
return $url; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$source_post = $this->get_source_post( $attachment_id, $msls_imported ); |
|
62
|
|
|
|
|
63
|
|
|
if ( false === $source_post ) { |
|
64
|
|
|
return $url; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $source_post->guid; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param int $attachment_id |
|
72
|
|
|
* @param array $msls_imported |
|
73
|
|
|
* |
|
74
|
|
|
* @return \WP_Post|false |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function get_source_post( $attachment_id, $msls_imported ) { |
|
77
|
|
|
$source_post = get_blog_post( $msls_imported['blog'], $msls_imported['post'] ); |
|
78
|
|
|
|
|
79
|
|
|
if ( empty( $source_post ) || ! $source_post instanceof \WP_Post ) { |
|
|
|
|
|
|
80
|
|
|
delete_post_meta( $attachment_id, self::LINKED ); |
|
81
|
|
|
|
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $source_post; |
|
86
|
|
|
} |
|
87
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.