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

Wpml::isEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 6
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 Wpml implements Contract
10
{
11
    public $pluginName = 'WPML';
12
    public $supportedVersion = '3.3.5';
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
            $postId = apply_filters('wpml_object_id', $postId, 'any', true);
25
        }
26
        return get_post(intval($postId));
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getPostIds(array $postIds)
33
    {
34
        if (!$this->isEnabled()) {
35
            return $postIds;
36
        }
37
        $newPostIds = [];
38
        foreach (Arr::unique($postIds) as $postId) {
39
            $postType = get_post_type($postId);
40
            if (!$postType) {
41
                continue;
42
            }
43
            $elementType = 'post_'.$postType;
44
            $trid = apply_filters('wpml_element_trid', null, $postId, $elementType);
45
            $translations = apply_filters('wpml_get_element_translations', null, $trid, $elementType);
46
            if (!is_array($translations)) {
47
                $translations = [];
48
            }
49
            $newPostIds = array_merge(
50
                $newPostIds,
51
                array_column($translations, 'element_id')
52
            );
53
        }
54
        return Arr::unique($newPostIds);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function isActive()
61
    {
62
        return defined('ICL_SITEPRESS_VERSION');
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function isEnabled()
69
    {
70
        return $this->isActive()
71
            && 'wpml' == glsr(OptionManager::class)->get('settings.general.multilingual');
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function isSupported()
78
    {
79
        return $this->isActive()
80
            && version_compare(ICL_SITEPRESS_VERSION, $this->supportedVersion, '>=');
81
    }
82
}
83