Completed
Pull Request — master (#1)
by Evgenii
01:01
created

Content::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Helick\Imgix;
4
5
use Helick\Contracts\Bootable;
6
7
final class Content implements Bootable
8
{
9
    /**
10
     * Boot the service.
11
     *
12
     * @return void
13
     */
14
    public static function boot(): void
15
    {
16
        $self = new static;
17
18
        add_filter('the_content', [$self, 'parse'], 100);
0 ignored issues
show
Bug introduced by
The function add_filter 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

18
        /** @scrutinizer ignore-call */ 
19
        add_filter('the_content', [$self, 'parse'], 100);
Loading history...
19
    }
20
21
    /**
22
     * Parse a given content.
23
     *
24
     * @param string $content
25
     *
26
     * @return string
27
     */
28
    public function parse(string $content): string
29
    {
30
        if (!$images = $this->resolveImages($content)) {
31
            return $content;
32
        }
33
34
        if ($attachmentIds = $this->resolveAttachmentIds($images['img_tags'])) {
35
            _prime_post_caches($attachmentIds, false, true);
0 ignored issues
show
Bug introduced by
The function _prime_post_caches 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

35
            /** @scrutinizer ignore-call */ 
36
            _prime_post_caches($attachmentIds, false, true);
Loading history...
36
        }
37
38
        foreach ($images['img_tags'] as $index => $tag) {
39
            $transform = 'resize';
0 ignored issues
show
Unused Code introduced by
The assignment to $transform is dead and can be removed.
Loading history...
40
41
            $attachmentId = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $attachmentId is dead and can be removed.
Loading history...
42
43
            $fullSizeUrl = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $fullSizeUrl is dead and can be removed.
Loading history...
44
45
            $src = $srcOrig = $images['img_urls'][$index];
0 ignored issues
show
Unused Code introduced by
The assignment to $srcOrig is dead and can be removed.
Loading history...
46
47
            if (!apply_filters('imgix_image_url_processable', true, $src))
0 ignored issues
show
Bug introduced by
The function apply_filters 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
            if (!/** @scrutinizer ignore-call */ apply_filters('imgix_image_url_processable', true, $src))
Loading history...
48
                continue;
49
        }
50
51
        return $content;
52
    }
53
54
    /**
55
     * @param string $content
56
     *
57
     * @return array
58
     */
59
    private function resolveImages(string $content): array
60
    {
61
        $pattern = '#(?:<a[^>]+?href=["|\'](?P<link_urls>[^\s]+?)["|\'][^>]*?>\s*)'
62
            . '?(?P<img_tags><img[^>]+?src=["|\'](?P<img_urls>[^\s]+?)["|\'].*?>){1}'
63
            . '(?:\s*</a>)?#is';
64
65
        if (!preg_match_all($pattern, $content, $images)) {
66
            return [];
67
        }
68
69
        $images = array_filter($images, 'is_string', ARRAY_FILTER_USE_KEY);
70
71
        return $images;
72
    }
73
74
    /**
75
     * @param array $tags
76
     *
77
     * @return array
78
     */
79
    private function resolveAttachmentIds(array $tags): array
80
    {
81
        $attachmentIds = [];
82
83
        foreach ($tags as $tag) {
84
            if (preg_match('/wp-image-([\d]+)/i', $tag, $matches)) {
85
                $attachmentIds[(int)$matches[1]] = true;
86
            }
87
        }
88
89
        return array_keys($attachmentIds);
90
    }
91
}
92