Passed
Push — master ( 6b8ca8...3384db )
by Paul
04:57
created

Akismet::isSpam()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 22
cp 0
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 17
nc 4
nop 1
crap 30
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Commands\SubmitReview;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use Akismet as AkismetPlugin;
8
9
class Akismet
10
{
11
	/**
12
	 * @return bool
13
	 */
14
	public function isSpam( SubmitReview $review )
15
	{
16
		if( !$this->isActive() ) {
17
			return false;
18
		}
19
		$submission = [
20
			'blog' => get_option( 'home' ),
21
			'blog_charset' => get_option( 'blog_charset' ),
22
			'blog_lang' => get_locale(),
23
			'comment_author' => $review->author,
24
			'comment_author_email' => $review->email,
25
			'comment_content' => $review->title."\n\n".$review->content,
26
			'comment_type' => 'review',
27
			'referrer' => filter_input( INPUT_SERVER, 'HTTP_REFERER' ),
28
			'user_agent' => filter_input( INPUT_SERVER, 'HTTP_USER_AGENT' ),
29
			'user_ip' => $review->ipAddress,
30
			// 'user_role' => 'administrator',
1 ignored issue
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
			// 'is_test' => 1,
1 ignored issue
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...
32
		];
33
		foreach( $_SERVER as $key => $value ) {
34
			if( is_array( $value ) || in_array( $key, ['HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW'] ))continue;
35
			$submission[$key] = $value;
36
		}
37
		return $this->check( apply_filters( 'site-reviews/akismet/submission', $submission, $review ));
38
	}
39
40
	/**
41
	 * @return bool
42
	 */
43
	protected function check( array $submission )
44
	{
45
		$response = AkismetPlugin::http_post( $this->buildQuery( $submission ), 'comment-check' );
46
		return apply_filters( 'site-reviews/akismet/is-spam',
47
			$response[1] == 'true',
48
			$submission,
49
			$response
50
		);
51
	}
52
53
	/**
54
	 * @return string
55
	 */
56
	protected function buildQuery( array $data )
57
	{
58
		$query = [];
59
		foreach( $data as $key => $value ) {
60
			if( is_array( $value ) || is_object( $value ))continue;
61
			if( $value === false ) {
62
				$value = '0';
63
			}
64
			$value = trim( $value );
65
			if( !strlen( $value ))continue;
66
			$query[] = urlencode( $key ).'='.urlencode( $value );
67
		}
68
		return implode( '&', $query );
69
	}
70
71
	/**
72
	 * @return bool
73
	 */
74
	protected function isActive()
75
	{
76
		$check = glsr( OptionManager::class )->get( 'settings.reviews-form.akismet' ) != 'yes'
77
			|| !is_callable( ['Akismet', 'get_api_key'] )
78
			|| !is_callable( ['Akismet', 'http_post'] )
79
			? false
80
			: (bool) AkismetPlugin::get_api_key();
81
		return apply_filters( 'site-reviews/akismet/is-active', $check );
82
	}
83
}
84