Passed
Push — master ( 2435c4...4321c5 )
by Paul
04:31
created

Wpml::getPostIds()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 1
dl 0
loc 24
ccs 0
cts 24
cp 0
crap 30
rs 9.3888
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
8
class Wpml implements Contract
9
{
10
    const PLUGIN_NAME = 'WPML';
11
    const SUPPORTED_VERSION = '3.3.5';
12
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function getPost($postId)
17
    {
18
        apply_filters('console', $postId);
19
        $postId = trim($postId);
20
        if (!is_numeric($postId)) {
21
            return;
22
        }
23
        if ($this->isEnabled()) {
24
            apply_filters('console', $postId);
25
            $postId = apply_filters('wpml_object_id', $postId, 'any', true);
26
            apply_filters('console', $postId);
27
        }
28
        return get_post(intval($postId));
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getPostIds(array $postIds)
35
    {
36
        apply_filters('console', $postIds);
37
        if (!$this->isEnabled()) {
38
            return $postIds;
39
        }
40
        $newPostIds = [];
41
        foreach ($this->cleanIds($postIds) as $postId) {
42
            $postType = get_post_type($postId);
43
            if (!$postType) {
44
                continue;
45
            }
46
            $elementType = 'post_'.$postType;
47
            $trid = apply_filters('wpml_element_trid', null, $postId, $elementType);
48
            $translations = apply_filters('wpml_get_element_translations', null, $trid, $elementType);
49
            if (!is_array($translations)) {
50
                $translations = [];
51
            }
52
            $newPostIds = array_merge(
53
                $newPostIds,
54
                array_column($translations, 'element_id')
55
            );
56
        }
57
        return $this->cleanIds($newPostIds);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function isActive()
64
    {
65
        return defined('ICL_SITEPRESS_VERSION');
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function isEnabled()
72
    {
73
        return $this->isActive()
74
            && 'wpml' == glsr(OptionManager::class)->get('settings.general.multilingual');
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function isSupported()
81
    {
82
        return $this->isActive()
83
            && version_compare(ICL_SITEPRESS_VERSION, static::SUPPORTED_VERSION, '>=');
0 ignored issues
show
Bug introduced by
The constant GeminiLabs\SiteReviews\M...l\ICL_SITEPRESS_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    protected function cleanIds(array $postIds)
90
    {
91
        return array_filter(array_unique($postIds));
92
    }
93
}
94