settings.php ➔ wpbo_mc_settings()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 89
Code Lines 60

Duplication

Lines 12
Ratio 13.48 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 60
c 1
b 0
f 1
nc 3
nop 1
dl 12
loc 89
rs 8.2842

How to fix   Long Method   

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
2
/**
3
 * BetterOptin Provider MailChimp
4
 *
5
 * @package   BetterOptin/Provider/MailChimp/Settings
6
 * @author    ThemeAvenue <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://themeavenue.net
9
 * @copyright 2015 ThemeAvenue
10
 */
11
12
// If this file is called directly, abort.
13
if ( ! defined( 'WPINC' ) ) {
14
	die;
15
}
16
17
add_filter( 'wpbo_plugin_settings', 'wpbo_mc_settings' );
18
/**
19
 * Addon settings.
20
 *
21
 * Add new settings to the plugin settings page.
22
 *
23
 * @since  1.0.0
24
 *
25
 * @param  array $settings Pre-existing settings
26
 *
27
 * @return array           Updated settings containing MailChimp options
28
 */
29
function wpbo_mc_settings( $settings ) {
30
	
31
	$lists = WPBO_MC()->get_lists();
32
33
	if ( false === $lists ) {
34
35
		$list_id = array(
36
			'name'    => __( 'List ID', 'betteroptin' ),
37
			'id'      => 'mc_list_id',
38
			'type'    => 'text',
39
			'default' => '',
40
			'desc'    => sprintf( __( 'Input your API key and save if you want to see a dropdown of all your lists. If you don\'t know how to get your API key please <a href="%s" target="_blank">read this documentation</a>.', 'betteroptin' ), esc_url( 'http://eepurl.com/im9k' ) )
41
		);
42
43
	} else {
44
45
		if ( isset( $lists['data'] ) && is_array( $lists['data'] ) ) {
46
47
			$opts[''] = __( 'Please select...', 'betteroptin' );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$opts was never initialized. Although not strictly required by PHP, it is generally a good practice to add $opts = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
48
49
			foreach ( $lists['data'] as $key => $list ) {
50
				$opts[ $list['id'] ] = $list['name'];
51
			}
52
53
			$list_id = array(
54
				'name'    => __( 'List ID', 'betteroptin' ),
55
				'id'      => 'mc_list_id',
56
				'type'    => 'select',
57
				'options' => $opts,
58
				'default' => ''
59
			);
60
61 View Code Duplication
		} else {
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...
62
63
			$list_id = array(
64
				'name'    => __( 'List ID', 'betteroptin' ),
65
				'id'      => 'mc_list_id',
66
				'type'    => 'text',
67
				'default' => '',
68
				'desc'    => __( 'Input your API key and save if you want to see a dropdown of all your lists.', 'betteroptin' ),
69
				'default' => ''
70
			);
71
72
		}
73
74
	}
75
76
	$settings['mailchimp'] = array(
77
		'name'    => __( 'MailChimp', 'betteroptin' ),
78
		'options' => array(
79
			array(
80
				'type' => 'note',
81
				'desc' => __( 'First of all, please input your MailChimp API key. Then hit the blue &laquo;Save Changes&raquo; button at the bottom of the screen. On page refresh, the <code>List ID</code> field will turn into a select dropdown showing all your MailChimp lists. If something goes wrong and you don\'t see the select dropdown, you can always input the list ID manually.', 'wpbo' )
82
			),
83
			array(
84
				'name'    => __( 'API Key', 'betteroptin' ),
85
				'id'      => 'mc_api_key',
86
				'type'    => 'text',
87
				'default' => '',
88
				'desc'    => sprintf( __( 'If you don\'t know how to get your API key please <a href="%s" target="_blank">read this documentation</a>.', 'wpmc' ), esc_url( 'http://eepurl.com/im9k' ) )
89
			),
90
			$list_id,
91
			array(
92
				'name'    => __( 'Double-Optin', 'betteroptin' ),
93
				'id'      => 'mc_double_optin',
94
				'type'    => 'checkbox',
95
				'default' => true,
96
				'desc'    => __( 'MailChimp asks for a subscription confirmation. It is advised NOT to disable it.', 'betteroptin' )
97
			),
98
			array(
99
				'name'    => __( 'Update Existing', 'betteroptin' ),
100
				'id'      => 'mc_update_existing',
101
				'type'    => 'checkbox',
102
				'default' => true,
103
				'desc'    => __( 'Update contacts if already subscribed.', 'betteroptin' )
104
			),
105
			array(
106
				'name'    => __( 'Welcome E-Mail', 'betteroptin' ),
107
				'id'      => 'mc_welcome',
108
				'type'    => 'checkbox',
109
				'default' => true,
110
				'desc'    => __( 'Send a welcome e-mail after subscription.', 'betteroptin' )
111
			),
112
		)
113
	);
114
115
	return $settings;
116
117
}