Completed
Pull Request — master (#858)
by
unknown
40:08 queued 20:01
created

Yikes_Inc_Easy_Mailchimp_Extender_Widget::widget()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 64
nop 2
dl 0
loc 40
ccs 0
cts 22
cp 0
crap 56
rs 8.3466
c 0
b 0
f 0
1
<?php
2
/*
3
*	Front facing Mailchimp widget in sidebars
4
*	@since 6.0.0
5
*	By: Yikes Inc. | https://www.yikesinc.com
6
*/
7
class Yikes_Inc_Easy_Mailchimp_Extender_Widget extends WP_Widget {
8
9
	/**
10
	 * Our form interface.
11
	 *
12
	 * @var Yikes_Inc_Easy_Mailchimp_Extender_Form_Interface
13
	 */
14
	protected $form_interface;
15
16
	/**
17
	 * Yikes_Inc_Easy_Mailchimp_Extender_Widget constructor.
18
	 */
19
	public function __construct() {
20
		$this->form_interface = yikes_easy_mailchimp_extender_get_form_interface();
21
22
		parent::__construct(
23
			// Base ID of your widget
24
			'yikes_easy_mc_widget',
25
			// Widget name will appear in UI
26
			__( 'Easy Mailchimp Forms', 'yikes-inc-easy-mailchimp-extender' ),
27
			// Widget description
28
			array( 'description' => __( 'Mailchimp opt-in widget for your sidebar.', 'yikes-inc-easy-mailchimp-extender' ), )
29
		);
30
	}
31
32
	/**
33
	 * Display the widget on the front-end.
34
	 *
35
	 * @param array $args
36
	 * @param array $instance
37
	 */
38
	public function widget( $args, $instance ) {
39
		// Get all form IDs so we can use the first one as a default.
40
		$form_ids = $this->form_interface->get_form_ids();
41
42
		$title              = isset( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : __( 'Mailchimp Signup Form', 'yikes-inc-easy-mailchimp-extender' );
43
		$form_id            = isset( $instance['form_id'] ) ? $instance['form_id'] : $form_ids[0];
44
		$form_description   = isset( $instance['form_description'] ) ? $instance['form_description'] : '';
45
		$submit_button_text = isset( $instance['submit_text'] ) ? $instance['submit_text'] : __( 'Submit', 'yikes-inc-easy-mailchimp-extender' );
46
47
		// Build our array based on settings chosen
48
		$shortcode_attributes = array(
49
			'form'        => $form_id,
50
			'submit'      => $submit_button_text,
51
			'description' => ( ! empty( $form_description ) ) ? '1' : '',
52
		);
53
54
		$shortcode_attributes = apply_filters( 'yikes_mailchimp_widget_shortcode_attributes', $shortcode_attributes, $instance );
55
56
		// before and after widget arguments are defined by themes
57
		echo $args['before_widget'];
58
59
		if ( ! empty( $title ) ) {
60
			echo $args['before_title'] . $title . $args['after_title'];
61
		}
62
63
		// Custom action hooks to place content in the widget before the form
64
		// See FAQ for examples on usage
65
		do_action( 'yikes-mailchimp-before-form-' . $form_id );
66
		do_action( 'yikes-mailchimp-before-form' );
67
68
		// This is where you run the code and display the output
69
		echo process_mailchimp_shortcode( $shortcode_attributes );
70
71
		// Custom action hooks to place content in the widget after the form
72
		// See FAQ for examples on usage
73
		do_action( 'yikes-mailchimp-after-form-' . $form_id );
74
		do_action( 'yikes-mailchimp-after-form' );
75
76
		echo $args['after_widget'];
77
	}
78
79
	// Widget Backend 
80
	public function form( $instance ) {
81
		$all_forms = $this->form_interface->get_all_forms();
82
	
83
		if ( empty( $all_forms ) ) {
84
			?>
85
				<section class="no-forms-widget">
86
				<strong><span class="dashicons dashicons-no-alt no-forms-found-icon"></span><?php echo sprintf( __( 'No forms found. It looks like you need to <a href="%s" title="%s">%s</a>.', 'yikes-inc-easy-mailchimp-extender' ), esc_url_raw( admin_url( 'admin.php?page=yikes-inc-easy-mailchimp' ) ), __( 'Create a form' , 'yikes-inc-easy-mailchimp-extender' ), __( 'create a form' , 'yikes-inc-easy-mailchimp-extender' ) ); ?></strong>
87
				</section>
88
			<?php
89
			return;
90
		}
91
		
92
		// Title
93 View Code Duplication
		if ( isset( $instance[ 'title' ] ) ) {
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...
94
			$title = $instance[ 'title' ];
95
		} else {
96
			$title = __( 'Mailchimp Signup Form', 'yikes-inc-easy-mailchimp-extender' );
97
		}
98
99
		// Selected Form
100
		if ( isset( $instance[ 'form_id' ] ) ) {
101
			$selected_form = $instance[ 'form_id' ];
102
		} else {
103
			$selected_form = '';
104
		}
105
106
		// Submit Button Text
107 View Code Duplication
		if ( isset( $instance[ 'submit_text' ] ) ) {
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...
108
			$submit_text = $instance[ 'submit_text' ];
109
		} else {
110
			$submit_text = __( 'Submit', 'yikes-inc-easy-mailchimp-extender' );
111
		}
112
113
		 if( get_option( 'yikes-mc-api-validation' , 'invalid_api_key' ) == 'invalid_api_key' ) {
114
			?>
115
				<p class="enter-valid-api-error-widget"><strong><?php _e( 'Please enter a valid Mailchimp API key to connect your site to Mailchimp.' , 'yikes-inc-easy-mailchimp-extender' ); ?></strong></p>
116
			<?php
117
			return;
118
		}
119
120
		// Widget admin form
121
		?>
122
		<p>
123
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
124
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
125
		</p>
126
127
		<p>
128
			<label for="<?php echo $this->get_field_id( 'form_id' ); ?>"><?php _e( 'Form:' ); ?></label>
129
			<select id="<?php echo $this->get_field_id( 'form_id' ); ?>" name="<?php echo $this->get_field_name( 'form_id' ); ?>" class="widefat">
130
				<?php
131
				// build our array
132
				foreach( $all_forms as $id => $form ) {
133
					?>
134
						<option <?php selected( $selected_form , $id ); ?> name="<?php echo $this->get_field_name( 'form_id' ); ?>" value="<?php echo $id; ?>"><?php echo stripslashes( $form['form_name'] ); ?></option>
135
					<?php
136
				}
137
				?>
138
			</select>
139
		</p>
140
141
		<p>
142
			<label for="<?php echo $this->get_field_id( 'form_description' ); ?>"><?php _e( 'Display Form Description:' ); ?></label>
143
			<input class="widefat" id="<?php echo $this->get_field_id( 'form_description' ); ?>" name="<?php echo $this->get_field_name( 'form_description' ); ?>" type="checkbox" value="1" <?php if( isset( $instance['form_description'] ) ) { checked( $instance['form_description'] , 1 ); } ?> />
144
		</p>
145
146
		<p>
147
			<label for="<?php echo $this->get_field_id( 'submit_button_text' ); ?>"><?php _e( 'Submit Button Text:' ); ?></label>
148
			<input class="widefat" id="<?php echo $this->get_field_id( 'submit_text' ); ?>" name="<?php echo $this->get_field_name( 'submit_text' ); ?>" type="text" value="<?php echo esc_attr( $submit_text ); ?>" />
149
		</p>
150
		<?php
151
	}
152
		
153
	// Updating widget replacing old instances with new
154
	public function update( $new_instance, $old_instance ) {
155
		$instance = array();
156
		$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
157
		$instance['form_id'] = $new_instance['form_id'];
158
		$instance['form_description'] = isset( $new_instance['form_description'] ) ? '1' : '';
159
		$instance['submit_text'] = ( ! empty( $new_instance['submit_text'] ) ) ? strip_tags( $new_instance['submit_text'] ) : 'Submit';
160
		return $instance;
161
	}
162
	
163
}
164