Completed
Push — staging ( 68fed3...485a71 )
by Evan
04:30
created

yikes-mailchimp-subscriber-count.php ➔ yikes_mailchimp_subscriber_count_shortcode()   C

Complexity

Conditions 14
Paths 34

Size

Total Lines 71
Code Lines 35

Duplication

Lines 15
Ratio 21.13 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 0
Metric Value
cc 14
eloc 35
nc 34
nop 1
dl 15
loc 71
rs 5.5568
c 0
b 0
f 0
ccs 0
cts 36
cp 0
crap 210

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
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 8 and the first side effect is on line 80.

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
*	Shortcode to display subscriber counts for a given list
4
*	@usage	[yikes-mailchimp-subscriber-count list="list_id"]
5
*	@since v6.0.2.4
6
*/
7
8
function yikes_mailchimp_subscriber_count_shortcode( $attributes ) {
9
10
	// Attributes
11
	shortcode_atts(
12
		array(
13
			'form' => '', // pass in a form, which will retreive the associated list ID -- takes precendence
14
			'list' => '', // pass in a specific list ID
15
		),
16
		$attributes,
17
		'yikes-mailchimp-subscriber-count'
18
	);
19
20
	/* If the user hasn't authenticated yet - bail */
21 View Code Duplication
	if ( get_option( 'yikes-mc-api-validation', 'invalid_api_key' ) != 'valid_api_key' ) {
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...
22
		if ( WP_DEBUG ) {
23
			return '<strong>' . __( "You don't appear to be connected to MailChimp.", "yikes-inc-easy-mailchimp-extender" ) . '</strong>';
24
		}
25
26
		return '';
27
	}
28
29
	$form    = ( ! empty( $attributes['form'] ) ) ? str_replace( '&quot;', '', $attributes['form'] ) : false;
30
	$list_id = ( ! empty( $attributes['list'] ) ) ? $attributes['list'] : false;
31
32
	/* If no list ID was passed into the shortcode - bail */
33
	if ( ! $list_id && ! $form ) {
34
		if ( WP_DEBUG ) {
35
			return '<strong>' . __( 'You forgot to include the list or form ID.', 'yikes-inc-easy-mailchimp-extender' ) . '</strong>';
36
		}
37
38
		return '';
39
	}
40
41
	/* if a form ID and a list ID were passed in, use the form ID */
42
	if ( ( $form ) || ( $form && $list_id ) ) {
43
		$interface = yikes_easy_mailchimp_extender_get_form_interface();
44
		$form_data = $interface->get_form( $form );
45
46
		// confirm we have some results, or return an error
47
		if ( ! $form_data ) {
48
			if ( WP_DEBUG ) {
49
				return __( "Oh no...This form doesn't exist. Head back to the manage forms page and select a different form.", 'yikes-inc-easy-mailchimp-extender' );
50
			}
51
52
			return '';
53
		}
54
55
		$list_id = sanitize_key( $form_data['list_id'] ); // associated list id (users who fill out the form will be subscribed to this list)
56
	}
57
58
	// object buffer
59
	ob_start();
60
61
	// submit the request the get the subscriber count
62
	$list_data = yikes_get_mc_api_manager()->get_list_handler()->get_list( $list_id, array( 'stats.member_count' => true ) );
63
64 View Code Duplication
	if ( is_wp_error( $list_data ) ) {
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...
65
		$error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging();
66
		$error_logging->maybe_write_to_log(
67
			$list_data->get_error_code(),
68
			__( "Get Account Lists", 'yikes-inc-easy-mailchimp-extender' ),
69
			"yikes-mailchimp-subscriber-count.php"
70
		);
71
	}
72
73
	/* type cast the returned value as an integer */
74
	echo (int) apply_filters( 'yikes-mailchimp-subscriber-count-value', $list_data['stats']['member_count'] );
75
76
	return ob_get_clean();
77
78
}
79
80
add_shortcode( 'yikes-mailchimp-subscriber-count', 'yikes_mailchimp_subscriber_count_shortcode' );
81