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 Polylang implements Contract |
9
|
|
|
{ |
10
|
|
|
public $pluginName = 'Polylang'; |
11
|
|
|
public $supportedVersion = '2.3'; |
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
|
|
|
$polylangPostId = pll_get_post($postId, pll_get_post_language(get_the_ID())); |
24
|
|
|
} |
25
|
|
|
if (!empty($polylangPostId)) { |
26
|
|
|
$postId = $polylangPostId; |
27
|
|
|
} |
28
|
|
|
return get_post(intval($postId)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function getPostIds(array $postIds) |
35
|
|
|
{ |
36
|
|
|
if (!$this->isEnabled()) { |
37
|
|
|
return $postIds; |
38
|
|
|
} |
39
|
|
|
$newPostIds = []; |
40
|
|
|
foreach ($this->cleanIds($postIds) as $postId) { |
41
|
|
|
$newPostIds = array_merge( |
42
|
|
|
$newPostIds, |
43
|
|
|
array_values(pll_get_post_translations($postId)) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
return $this->cleanIds($newPostIds); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function isActive() |
53
|
|
|
{ |
54
|
|
|
return function_exists('PLL') |
55
|
|
|
&& function_exists('pll_get_post') |
56
|
|
|
&& function_exists('pll_get_post_language') |
57
|
|
|
&& function_exists('pll_get_post_translations'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function isEnabled() |
64
|
|
|
{ |
65
|
|
|
return $this->isActive() |
66
|
|
|
&& 'polylang' == glsr(OptionManager::class)->get('settings.general.multilingual'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function isSupported() |
73
|
|
|
{ |
74
|
|
|
return defined('POLYLANG_VERSION') |
75
|
|
|
&& version_compare(POLYLANG_VERSION, $this->supportedVersion, '>='); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
protected function cleanIds(array $postIds) |
82
|
|
|
{ |
83
|
|
|
return array_filter(array_unique($postIds)); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|