Passed
Push — master ( ffa8d9...6a6a4c )
by Paul
03:54
created

Polylang::getPost()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 5
nop 1
dl 0
loc 13
ccs 0
cts 13
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Multilingual;
4
5
use GeminiLabs\SiteReviews\Contracts\MultilingualContract as Contract;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
9
class Polylang implements Contract
10
{
11
    public $pluginName = 'Polylang';
12
    public $supportedVersion = '2.3';
13
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function getPost($postId)
18
    {
19
        $postId = trim($postId);
20
        if (!is_numeric($postId)) {
21
            return;
22
        }
23
        if ($this->isEnabled()) {
24
            $polylangPostId = pll_get_post($postId, pll_get_post_language(get_the_ID()));
25
        }
26
        if (!empty($polylangPostId)) {
27
            $postId = $polylangPostId;
28
        }
29
        return get_post(intval($postId));
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getPostIds(array $postIds)
36
    {
37
        if (!$this->isEnabled()) {
38
            return $postIds;
39
        }
40
        $newPostIds = [];
41
        foreach (Arr::unique($postIds) as $postId) {
42
            $newPostIds = array_merge(
43
                $newPostIds,
44
                array_values(pll_get_post_translations($postId))
45
            );
46
        }
47
        return Arr::unique($newPostIds);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function isActive()
54
    {
55
        return function_exists('PLL')
56
            && function_exists('pll_get_post')
57
            && function_exists('pll_get_post_language')
58
            && function_exists('pll_get_post_translations');
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function isEnabled()
65
    {
66
        return $this->isActive()
67
            && 'polylang' == glsr(OptionManager::class)->get('settings.general.multilingual');
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function isSupported()
74
    {
75
        return defined('POLYLANG_VERSION')
76
            && version_compare(POLYLANG_VERSION, $this->supportedVersion, '>=');
77
    }
78
}
79