Passed
Push — master ( 6375bf...7ef419 )
by Paul
05:58
created

Polylang::getPostIds()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 13
ccs 0
cts 13
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
7
class Polylang
8
{
9
	/**
10
	 * @param int|string $postId
11
	 * @return array
12
	 */
13
	public function getPost( $postId )
14
	{
15
		if( $this->isEnabled() ) {
16
			$polylangPostId = pll_get_post( $postId, pll_get_post_language( get_the_ID() ));
2 ignored issues
show
Bug introduced by
The function pll_get_post_language was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

16
			$polylangPostId = pll_get_post( $postId, /** @scrutinizer ignore-call */ pll_get_post_language( get_the_ID() ));
Loading history...
Bug introduced by
The function pll_get_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

16
			$polylangPostId = /** @scrutinizer ignore-call */ pll_get_post( $postId, pll_get_post_language( get_the_ID() ));
Loading history...
17
		}
18
		if( !empty( $polylangPostId )) {
19
			$postId = $polylangPostId;
20
		}
21
		return get_post( intval( $postId ));
0 ignored issues
show
Bug Best Practice introduced by
The expression return get_post(intval($postId)) also could return the type WP_Post which is incompatible with the documented return type array.
Loading history...
22
	}
23
24
	/**
25
	 * @return array
26
	 */
27
	public function getPostIds( array $postIds )
28
	{
29
		if( !$this->isEnabled() ) {
30
			return $postIds;
31
		}
32
		$newPostIds = [];
33
		foreach( $this->cleanIds( $postIds ) as $postId ) {
34
			$newPostIds = array_merge(
35
				$newPostIds,
36
				array_values( pll_get_post_translations( $postId ))
1 ignored issue
show
Bug introduced by
The function pll_get_post_translations was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
				array_values( /** @scrutinizer ignore-call */ pll_get_post_translations( $postId ))
Loading history...
37
			);
38
		}
39
		return $this->cleanIds( $newPostIds );
40
	}
41
42
	/**
43
	 * @return bool
44
	 */
45
	public function isActive()
46
	{
47
		return function_exists( 'PLL' )
48
			&& function_exists( 'pll_get_post' )
49
			&& function_exists( 'pll_get_post_language' )
50
			&& function_exists( 'pll_get_post_translations' );
51
	}
52
53
	/**
54
	 * @return bool
55
	 */
56
	public function isEnabled()
57
	{
58
		return $this->isActive()
59
			&& glsr( OptionManager::class )->get( 'settings.general.support.polylang' ) == 'yes';
60
	}
61
62
	/**
63
	 * @return bool
64
	 */
65
	public function isSupported()
66
	{
67
		return defined( 'POLYLANG_VERSION' )
68
			&& version_compare( POLYLANG_VERSION, '2.3', '>=' );
1 ignored issue
show
Bug introduced by
The constant GeminiLabs\SiteReviews\Modules\POLYLANG_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
69
	}
70
71
	/**
72
	 * @return array
73
	 */
74
	protected function cleanIds( array $postIds )
75
	{
76
		return array_filter( array_unique( $postIds ));
77
	}
78
}
79
80