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

Akismet   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 73
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B buildQuery() 0 13 6
A isActive() 0 8 4
A check() 0 7 1
B isSpam() 0 24 5
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