functions-ajax.php ➔ wpbo_mc_display_groups()   C
last analyzed

Complexity

Conditions 10
Paths 28

Size

Total Lines 64
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 33
c 1
b 0
f 1
nc 28
nop 0
dl 0
loc 64
rs 6.309

How to fix   Long Method    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
2
/**
3
 * BetterOptin Provider MailChimp
4
 *
5
 * @package   BetterOptin/Provider/MailChimp
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_action( 'wp_ajax_mc_get_groups', 'wpbo_mc_display_groups' );
18
/**
19
 * Display a list of groups.
20
 *
21
 * If groups are available for the specified list then we display a list
22
 * with checkboxes.
23
 *
24
 * @since  1.1.0
25
 * @return string List markup
26
 */
27
function wpbo_mc_display_groups() {
28
29
	/* Make sure we have a list ID to check */
30
	if ( ! isset( $_POST['mc_list_id'] ) ) {
31
		echo '<div class="form-invalid" style="padding: 1em;">' . __( 'An error occurred during the request. The list ID is missing.', 'betteroptin' ) . '</div>';
32
		wp_die();
33
	}
34
35
	$list_id = sanitize_key( $_POST['mc_list_id'] );
36
	$post_id = (int) $_POST['mc_post_id'];
37
38
	/* Get the groups */
39
	$groups = WPBO_MC()->get_groups( $list_id );
40
41
	if ( is_wp_error( $groups ) ) {
42
43
		/**
44
		 * @var WP_Error $groups
45
		 */
46
47
		echo '<div class="form-invalid" style="padding: 1em;">' . $groups->get_error_message() . '</div>';
48
		wp_die();
49
	}
50
51
	if ( is_array( $groups ) && ! empty( $groups ) ) {
52
53
		foreach ( $groups as $group ) {
54
55
			$show_groups   = new WPMC_MailChimp_Groups( $group['id'], $group['groups'], $post_id );
56
			$group_name    = $group['name'];
57
			$group_options = $group['form_field'];
58
59
			echo "<p>$group_name</p>";
60
61
			switch ( $group_options ) {
62
63
				case 'checkboxes':
64
					$show_groups->show_group_type_checkboxes();
65
					break;
66
67
				case 'radio':
68
					$show_groups->show_group_type_radio();
69
					break;
70
71
				case 'dropdown':
72
					$show_groups->show_group_type_dropdown();
73
					break;
74
75
				case 'hidden':
76
					$show_groups->show_group_type_checkboxes();
77
					break;
78
79
			}
80
81
		}
82
83
		wp_die();
84
85
	} else {
86
		echo '<div class="form-invalid" style="padding: 1em;">' . __( 'This list does not have any groups.', 'betteroptin' ) . '</div>';
87
		wp_die();
88
	}
89
90
}