Passed
Pull Request — master (#123)
by
unknown
03:59
created

Image   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 46
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A acfLoadValue() 0 11 2
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
    {
32
        $this->site = $site;
33
        $this->siteSwitcher = $siteSwitcher;
34
        $this->store = acf_get_store('values');
0 ignored issues
show
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...
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

34
        $this->store = /** @scrutinizer ignore-call */ acf_get_store('values');
Loading history...
35
    }
36
37
    // Fetch ACF file fields across sites when the global prefix is used.
38
    // We hook into 'load_value' which usually runs just before 'format_value'.
39
    // Then get the formatted output of the field in the global media site's context,
40
    // and store it in ACF's cache. So when format_value tries to use this value,
41
    // it will find the formatted one already in the cache.
42
    // This works around acf_format_value requiring a valid att ID as input, but
43
    // returning a string/array as output, so it can't be easily filtered.
44
    public function acfLoadValue($value, $post_id, $field)
45
    {
46
        if ($this->idPrefixIncludedInAttachmentId((int)$value, $this->site->idSitePrefix())) {
47
            $formatted = $this->stripSiteIdPrefixFromAttachmentId($this->site->idSitePrefix(), $value);
48
            $this->siteSwitcher->switchToBlog($this->site->id());
49
            $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

49
            $formatted = /** @scrutinizer ignore-call */ acf_format_value($formatted, $post_id, $field);
Loading history...
50
            $this->siteSwitcher->restoreBlog();
51
            $this->store->set("$post_id:{$field['name']}:formatted", $formatted);
52
        }
53
        // This filter doesn't modify the loaded value. Return it as-is.
54
        return $value;
55
    }
56
}
57