|
1
|
|
|
<?php # -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
namespace MultisiteGlobalMedia\ACF; |
|
4
|
|
|
|
|
5
|
|
|
use MultisiteGlobalMedia\Helper; |
|
6
|
|
|
use MultisiteGlobalMedia\SingleSwitcher; |
|
7
|
|
|
use MultisiteGlobalMedia\Site; |
|
8
|
|
|
|
|
9
|
|
|
class Image |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
use Helper; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Site |
|
16
|
|
|
*/ |
|
17
|
|
|
private $site; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var SingleSwitcher |
|
21
|
|
|
*/ |
|
22
|
|
|
private $siteSwitcher; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Image constructor |
|
26
|
|
|
* |
|
27
|
|
|
* @param Site $site |
|
28
|
|
|
* @param SingleSwitcher $siteSwitcher |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(Site $site, SingleSwitcher $siteSwitcher){ |
|
31
|
|
|
$this->site = $site; |
|
32
|
|
|
$this->siteSwitcher = $siteSwitcher; |
|
33
|
|
|
$this->store = acf_get_store( 'values' ); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// Fetch ACF file fields across sites when the global prefix is used. |
|
37
|
|
|
// We hook into 'load_value' which usually runs just before 'format_value'. |
|
38
|
|
|
// Then get the formatted output of the field in the global media site's context, |
|
39
|
|
|
// and store it in ACF's cache. So when format_value tries to use this value, |
|
40
|
|
|
// it will find the formatted one already in the cache. |
|
41
|
|
|
// This works around acf_format_value requiring a valid att ID as input, but |
|
42
|
|
|
// returning a string/array as output, so it can't be easily filtered. |
|
43
|
|
|
public function acf_load_value( $value, $post_id, $field ) { |
|
44
|
|
|
if ( $this->idPrefixIncludedInAttachmentId( (int)$value, $this->site->idSitePrefix() ) ) { |
|
45
|
|
|
$formatted = $this->stripSiteIdPrefixFromAttachmentId( $this->site->idSitePrefix(), $value ); |
|
46
|
|
|
$this->siteSwitcher->switchToBlog( $this->site->id() ); |
|
47
|
|
|
$formatted = acf_format_value( $formatted, $post_id, $field ); |
|
|
|
|
|
|
48
|
|
|
$this->siteSwitcher->restoreBlog(); |
|
49
|
|
|
$this->store->set( "$post_id:{$field['name']}:formatted", $formatted ); |
|
50
|
|
|
} |
|
51
|
|
|
// This filter doesn't modify the loaded value. Return it as-is. |
|
52
|
|
|
return $value; |
|
53
|
|
|
} |
|
54
|
|
|
} |