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

shortcode-unsubscribe.php ➔ process_yikes_unsubscribe_shortcode()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 86
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 47
nc 18
nop 1
dl 0
loc 86
ccs 0
cts 29
cp 0
crap 72
rs 5.7638
c 0
b 0
f 0

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
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 90.

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
function process_yikes_unsubscribe_shortcode( $args ) {
4
5
	$defaults = array(
6
		'list'              => '',
7
		'form'              => '',
8
		'email_label'       => 'Email Address',
9
		'submit_label'      => 'Unsubscribe',
10
		'email_placeholder' => ''
11
	);
12
13
	$values = is_array( $args ) ? array_merge( $defaults, $args ) : $defaults;
14
15
	if ( empty( $values['list'] ) && empty( $values['form'] ) ) {
16
		return '<!-- YIKES Easy Forms Error: no list ID / form ID -->';
17
	}
18
19
	if ( ! empty( $values['form'] ) ) {
20
21
		// Get the list ID from the form ID
22
		$interface = yikes_easy_mailchimp_extender_get_form_interface();
23
		$form_data = $interface->get_form( $values['form'] );
24
		$list_id   = isset( $form_data['list_id'] ) ? $form_data['list_id'] : null;
25
26
	} else if ( ! empty( $values['list'] ) ) {
27
28
		$list_id = $values['list'];
29
	}
30
31
	if ( empty( $list_id ) ) {
32
		return '<!-- YIKES Easy Forms Error: no list ID -->';
33
	}
34
35
	// Include our JS AJAX functions
36
	wp_register_script( 'yikes-mailchimp-unsubscribe-script', plugin_dir_url( __FILE__ ) . '/unsubscribe.js', array( 'jquery' ), YIKES_MC_VERSION, true );
37
	wp_localize_script( 'yikes-mailchimp-unsubscribe-script', 'yikes_unsubscribe_data', 
38
		array( 
39
			'ajax_url' => admin_url( 'admin-ajax.php' ),
40
			'error1'   => apply_filters( 'yikes-mailchimp-unsubscribe-general-error', __( 'An error occurred.', 'yikes-inc-easy-mailchimp-extender' ) ),
41
			'error2'   => apply_filters( 'yikes-mailchimp-unsubscribe-not-found-error', __( 'It looks like you\'re already unsubscribed.', 'yikes-inc-easy-mailchimp-extender' ) ),
42
			'success'  => apply_filters( 'yikes-mailchimp-unsubscribe-success', __( 'Success! You\'ve been unsubscribed from this list.', 'yikes-inc-easy-mailchimp-extender' ) ),
43
			'loader'   => apply_filters( 'yikes-mailchimp-unsubscribe-loader', YIKES_MC_URL . 'includes/images/ripple.svg' ),
44
		) 
45
	);
46
	wp_enqueue_script ( 'yikes-mailchimp-unsubscribe-script' );
47
48
	// Include our styles
49
	wp_enqueue_style( 'yikes-mailchimp-unsubscribe-styles', plugin_dir_url( __FILE__ ) . '/unsubscribe.css', array(), YIKES_MC_VERSION, 'all' );
50
51
	ob_start();
52
	?>
53
		<section id="yikes-mailchimp-unsubscribe-container-<?php echo $list_id; ?>" class="yikes-mailchimp-unsubscribe-container">
54
55
			<?php do_action( 'yikes-mailchimp-unsubscribe-before-form' ); ?>
56
57
			<div class="yikes-mailchimp-unsubscribe-feedback" style="display: none;"></div>
58
59
			<form id="yikes-mailchimp-unsubscribe-form-<?php echo $list_id; ?>" class="yikes-mailchimp-unsubscribe-form" method="POST">
60
61
				<!-- Email -->
62
				<?php do_action( 'yikes-mailchimp-unsubscribe-before-email' ); ?>
63
				<label for="yikes-mailchimp-unsubscribe-email" class="EMAIL-label">
64
					<span class="EMAIL-label"><?php echo $values['email_label']; ?></span>
65
					<input name="EMAIL" placeholder="<?php echo $values['email_placeholder']; ?>" class="yikes-mailchimp-unsubscribe-email" id="yikes-mailchimp-unsubscribe-email" required="required" type="email" value="<?php echo esc_attr( apply_filters( 'yikes-mailchimp-unsubscribe-email-default', '' ) ); ?>">
66
				</label>
67
				<?php do_action( 'yikes-mailchimp-unsubscribe-after-email' ); ?>
68
69
				<!-- Honeypot Trap -->
70
				<input type="hidden" class="yikes-mailchimp-honeypot" name="yikes-mailchimp-honeypot" value="">
71
72
				<!-- List ID -->
73
				<input type="hidden" class="yikes-mailchimp-unsubscribe-list-id" name="yikes-mailchimp-unsubscribe-list-id" value="<?php echo $list_id; ?>">
74
75
				<!-- Submit Button -->
76
				<button type="submit" class="yikes-mailchimp-unsubscribe-submit-button">
77
					<span class="yikes-mailchimp-submit-button-span-text"><?php echo $values['submit_label']; ?></span>
78
				</button>
79
80
				<input type="hidden" class="yikes-mailchimp-unsubscribe-nonce" name="yikes-mailchimp-unsubscribe-nonce" value="<?php echo wp_create_nonce( 'yikes-mailchimp-unsubscribe' ); ?>">
81
			</form>
82
83
			<?php do_action( 'yikes-mailchimp-unsubscribe-after-form' ); ?>
84
		</section>
85
	<?php
86
87
	return ob_get_clean();
88
}
89
90
add_shortcode( 'yikes-mailchimp-unsubscribe', 'process_yikes_unsubscribe_shortcode' );