Passed
Push — master ( 78492a...4f268f )
by Paul
04:26
created

Akismet::isSpam()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 18.9614

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 1
dl 0
loc 24
ccs 3
cts 17
cp 0.1765
crap 18.9614
rs 9.2248
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
use Akismet as AkismetPlugin;
7
8
class Akismet
9
{
10
	/**
11
	 * @return bool
12
	 */
13 1
	public function isSpam( array $review )
14
	{
15 1
		if( !$this->isActive() ) {
16 1
			return false;
17
		}
18
		$submission = [
19
			'blog' => get_option( 'home' ),
20
			'blog_charset' => get_option( 'blog_charset' ),
21
			'blog_lang' => get_locale(),
22
			'comment_author' => $review['name'],
23
			'comment_author_email' => $review['email'],
24
			'comment_content' => $review['title']."\n\n".$review['content'],
25
			'comment_type' => 'review',
26
			'referrer' => filter_input( INPUT_SERVER, 'HTTP_REFERER' ),
27
			'user_agent' => filter_input( INPUT_SERVER, 'HTTP_USER_AGENT' ),
28
			'user_ip' => $review['ip_address'],
29
			// 'user_role' => 'administrator',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
30
			// 'is_test' => 1,
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
31
		];
32
		foreach( $_SERVER as $key => $value ) {
33
			if( is_array( $value ) || in_array( $key, ['HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW'] ))continue;
34
			$submission[$key] = $value;
35
		}
36
		return $this->check( apply_filters( 'site-reviews/akismet/submission', $submission, $review ));
37
	}
38
39
	/**
40
	 * @return bool
41
	 */
42
	protected function check( array $submission )
43
	{
44
		$response = AkismetPlugin::http_post( $this->buildQuery( $submission ), 'comment-check' );
45
		return apply_filters( 'site-reviews/akismet/is-spam',
46
			$response[1] == 'true',
47
			$submission,
48
			$response
49
		);
50
	}
51
52
	/**
53
	 * @return string
54
	 */
55
	protected function buildQuery( array $data )
56
	{
57
		$query = [];
58
		foreach( $data as $key => $value ) {
59
			if( is_array( $value ) || is_object( $value ))continue;
60
			if( $value === false ) {
61
				$value = '0';
62
			}
63
			$value = trim( $value );
64
			if( !strlen( $value ))continue;
65
			$query[] = urlencode( $key ).'='.urlencode( $value );
66
		}
67
		return implode( '&', $query );
68
	}
69
70
	/**
71
	 * @return bool
72
	 */
73 1
	protected function isActive()
74
	{
75 1
		$check = glsr( OptionManager::class )->get( 'settings.submissions.akismet' ) != 'yes'
76
			|| !is_callable( ['Akismet', 'get_api_key'] )
77 1
			|| !is_callable( ['Akismet', 'http_post'] )
78 1
			? false
79 1
			: (bool) AkismetPlugin::get_api_key();
80 1
		return apply_filters( 'site-reviews/akismet/is-active', $check );
81
	}
82
}
83