Completed
Push — staging ( a3874c...2b8e16 )
by
unknown
04:09
created

yikes_mailchimp_unsubscribe()   D

Complexity

Conditions 11
Paths 144

Size

Total Lines 42
Code Lines 20

Duplication

Lines 8
Ratio 19.05 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 20
nc 144
nop 0
dl 8
loc 42
ccs 0
cts 27
cp 0
crap 132
rs 4.9629
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 3 and the first side effect is on line 58.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
class YIKES_Mailchimp_Process_Unsubscribe {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
	
5
	public function __construct() {
6
		$this->define_unsubscribe_ajax();
7
	}
8
9
	public function define_unsubscribe_ajax() {
10
		add_action( 'wp_ajax_nopriv_yikes_mailchimp_unsubscribe', array( $this, 'yikes_mailchimp_unsubscribe' ) );
11
		add_action( 'wp_ajax_yikes_mailchimp_unsubscribe', array( $this, 'yikes_mailchimp_unsubscribe' ) );
12
	}
13
14
	public function yikes_mailchimp_unsubscribe() {
0 ignored issues
show
Coding Style introduced by
yikes_mailchimp_unsubscribe uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
15
16
		// Verify nonce:
17
		// First, check our option - this is set in the general settings page
18
		if ( get_option( 'yikes-mailchimp-use-nonce' ) === '1' ) {
19
			if ( check_ajax_referer( 'yikes-mailchimp-unsubscribe-nonce', 'nonce', false ) ) {
20
				wp_send_json_error( '1' );
21
			}
22
		}
23
24
		// Verify Honeypot
25
		if ( ! empty( $_POST['hp'] ) ) {
26
			wp_send_json_error( '2' );
27
		}
28
29
		// Get email, list ID
30
		$email   = isset( $_POST['email'] ) ? $_POST['email'] : '';
31
		$list_id = isset( $_POST['list_id'] ) ? $_POST['list_id'] : '';
32
33
		if ( empty( $email ) || empty( $list_id ) ) {
34
			wp_send_json_error( '3' );
35
		}
36
37
		$email   = md5( strtolower( $email ) );
38
		$list_id = filter_var( $list_id, FILTER_SANITIZE_STRING );
39
40
		// Unsubscribe the member
41
		$list_handler  = yikes_get_mc_api_manager()->get_list_handler();
42
		$unsubscribe   = $list_handler->member_unsubscribe( $list_id, $email );
43
44
		// If error, log it.
45 View Code Duplication
		if ( is_wp_error( $unsubscribe ) && class_exists( 'Yikes_Inc_Easy_Mailchimp_Error_Logging' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
			$error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging();
47
			if ( method_exists( $error_logging, 'maybe_write_to_log' ) ) {
48
				$error_logging->maybe_write_to_log( $unsubscribe->get_error_code(), __( "Member Unsubscribe", 'yikes-inc-easy-mailchimp-extender' ), 'process-unsubscribe.php' );
49
			}
50
51
			wp_send_json_error( '4' );
52
		}
53
54
		wp_send_json_success();
55
	}
56
}
57
58
$YIKES_Mailchimp_Process_Unsubscribe = new YIKES_Mailchimp_Process_Unsubscribe();