Passed
Pull Request — master (#123)
by
unknown
05:33
created

Image::acf_load_value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 10
rs 10
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' );
0 ignored issues
show
Bug introduced by
The function acf_get_store was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        $this->store = /** @scrutinizer ignore-call */ acf_get_store( 'values' );
Loading history...
Bug Best Practice introduced by
The property store does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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 );
0 ignored issues
show
Bug introduced by
The function acf_format_value was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            $formatted = /** @scrutinizer ignore-call */ acf_format_value( $formatted, $post_id, $field );
Loading history...
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
}