Passed
Push — feature/rebusify ( fe0687...495106 )
by Paul
05:25 queued 15s
created

Wpml::getPostIds()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 16
c 1
b 0
f 1
nc 5
nop 1
dl 0
loc 23
ccs 0
cts 23
cp 0
crap 30
rs 9.4222
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
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
        $postId = trim($postId);
19
        if (!is_numeric($postId)) {
20
            return;
21
        }
22
        if ($this->isEnabled()) {
23
            $postId = apply_filters('wpml_object_id', $postId, 'any', true);
24
        }
25
        return get_post(intval($postId));
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getPostIds(array $postIds)
32
    {
33
        if (!$this->isEnabled()) {
34
            return $postIds;
35
        }
36
        $newPostIds = [];
37
        foreach ($this->cleanIds($postIds) as $postId) {
38
            $postType = get_post_type($postId);
39
            if (!$postType) {
40
                continue;
41
            }
42
            $elementType = 'post_'.$postType;
43
            $trid = apply_filters('wpml_element_trid', null, $postId, $elementType);
44
            $translations = apply_filters('wpml_get_element_translations', null, $trid, $elementType);
45
            if (!is_array($translations)) {
46
                $translations = [];
47
            }
48
            $newPostIds = array_merge(
49
                $newPostIds,
50
                array_column($translations, 'element_id')
51
            );
52
        }
53
        return $this->cleanIds($newPostIds);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function isActive()
60
    {
61
        return defined('ICL_SITEPRESS_VERSION');
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function isEnabled()
68
    {
69
        return $this->isActive()
70
            && 'wpml' == glsr(OptionManager::class)->get('settings.general.support.multilingual');
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function isSupported()
77
    {
78
        return $this->isActive()
79
            && version_compare(ICL_SITEPRESS_VERSION, static::SUPPORTED_VERSION, '>=');
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    protected function cleanIds(array $postIds)
86
    {
87
        return array_filter(array_unique($postIds));
88
    }
89
}
90