Completed
Push — staging ( 78e662...0f9e2f )
by
unknown
05:16
created

YIKES_Inc_Easy_Mailchimp_Process_Ajax   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 218
ccs 0
cts 126
cp 0
rs 8.8798
c 0
b 0
f 0
wmc 44
lcom 0
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 1
A add_notification_to_form() 0 6 2
A get_new_list_data() 0 16 2
B check_list_for_interest_groups() 0 28 6
A send_field_to_form() 0 4 1
A send_interest_group_to_form() 0 4 1
B add_tags_to_form() 0 29 10
B remove_tag_from_form() 0 21 10
A findMCListIndex() 0 6 2
B save_field_label_edits() 0 37 9

How to fix   Complexity   

Complex Class

Complex classes like YIKES_Inc_Easy_Mailchimp_Process_Ajax often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use YIKES_Inc_Easy_Mailchimp_Process_Ajax, and based on these observations, apply Extract Interface, too.

1
<?php
2
	/*
3
	* 	Main Ajax handler
4
	*
5
	* 	Handles many of the ajax functionality on the admin side (ie. Adding new field to form, updating fields, grabbing list data etc.)
6
	*
7
	*	@since 6.0.0
8
	*	Author: Yikes Inc. | https://www.yikesinc.com
9
	*/
10
	class YIKES_Inc_Easy_Mailchimp_Process_Ajax {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
12
		public function __construct() {
13
14
			// Ajax send merge variable to form builder.
15
			add_action( 'wp_ajax_add_field_to_form', array( $this , 'send_field_to_form' ), 10 );
16
17
			// Ajax send interest group to form builder.
18
			add_action( 'wp_ajax_add_interest_group_to_form', array( $this, 'send_interest_group_to_form' ), 10 );
19
20
			// Ajax add a tag to the form.
21
			add_action( 'wp_ajax_add_tag_to_form', array( $this, 'add_tags_to_form' ), 10 );
22
23
			// Ajax remove tag from form.
24
			add_action( 'wp_ajax_remove_tag_from_form', array( $this, 'remove_tag_from_form' ), 10 );
25
26
			// Return new list data + activity (for dashboard widget).
27
			add_action( 'wp_ajax_get_new_list_data', array( $this, 'get_new_list_data' ), 10 );
28
29
			// Return new list data + activity (for dashboard widget).
30
			add_action( 'wp_ajax_check_list_for_interest_groups', array( $this, 'check_list_for_interest_groups' ), 10 );
31
32
			// Add a new notification to a form.
33
			add_action( 'wp_ajax_add_notification_to_form', array( $this, 'add_notification_to_form' ), 10, 1 );
34
35
			// Save field label edits.
36
			add_action( 'wp_ajax_save_field_label_edits', array( $this, 'save_field_label_edits' ), 10, 1 );
37
		}
38
39
		/*
40
		*	Assign a new notification to the form
41
		*	- return a single container
42
		*/
43
		public function add_notification_to_form() {
44
			if ( isset( $_POST['notification_name'] ) ) {
45
				include_once YIKES_MC_PATH . 'admin/partials/ajax/add_notification_to_form.php';
46
			}
47
			exit();
48
		}
49
50
		// Process our AJAX request,
51
		// when the user wants to switch which form data
52
		// is displayed on the dashboard
53
		public function get_new_list_data() {
54
			$list_id   = $_POST['list_id'];
55
			$list_data = yikes_get_mc_api_manager()->get_list_handler()->get_list( $list_id );
56
			if ( is_wp_error( $list_data ) ) {
57
				$error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging();
58
				$error_logging->maybe_write_to_log(
59
					$list_data->get_error_code(),
60
					__( "Get Account Lists", 'yikes-inc-easy-mailchimp-extender' ),
61
					__( "Mailchimp Widget", 'yikes-inc-easy-mailchimp-extender' )
62
				);
63
				exit();
64
			}
65
66
			include_once( YIKES_MC_PATH . 'admin/partials/dashboard-widgets/templates/stats-list-template.php' );
67
			exit();
68
		}
69
70
		// Process our AJAX request,
71
		// when the user switches lists in the integration settings page
72
		// we want to return the interest groups associated with this list,
73
		// to allow users to pre-check anything they want to assign users appropriately
74
		/* note: this function is called statically from the integration settings page */
75
		public static function check_list_for_interest_groups( $list_id = '', $integration_type = '', $load = false ) {
76
			if ( ! $list_id ) {
77
				$list_id = $_POST['list_id'];
78
			}
79
			if ( ! $integration_type ) {
80
				$integration_type = $_POST['integration'];
81
			}
82
83
84
			$interest_groupings = yikes_get_mc_api_manager()->get_list_handler()->get_interest_categories( $list_id );
85
			if ( is_wp_error( $interest_groupings ) ) {
86
				$error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging();
87
				$error_logging->maybe_write_to_log( 
88
					$interest_groupings->get_error_code(), 
89
					__( "Get Interest Groups", 'yikes-inc-easy-mailchimp-extender' ), 
90
					"class.ajax.php" 
91
				);
92
				$interest_groupings = array();
93
			}
94
95
			if ( ! empty( $interest_groupings ) ) {
96
				require( YIKES_MC_PATH . 'admin/partials/menu/options-sections/templates/integration-interest-groups.php' );
97
			}
98
			// do not kill off execution on load, only on an ajax request
99
			if ( ! $load ) {
100
				exit();
101
			}
102
		}
103
104
		// Process our Ajax Request
105
		// send a field to our form
106
		public function send_field_to_form() {
107
			include YIKES_MC_PATH . 'admin/partials/ajax/add_field_to_form.php';
108
			exit();
109
		}
110
111
		// send interest group to our form
112
		public function send_interest_group_to_form() {
113
			include YIKES_MC_PATH . 'admin/partials/ajax/add_interest_group_to_form.php';
114
			exit();
115
		}
116
117
		/**
118
		 * Add a tag to the form.
119
		 */
120
		public function add_tags_to_form() {
121
			// Verify Nonce.
122
			if ( ! check_ajax_referer( 'add-tag', 'nonce', false ) ) {
123
				wp_send_json_error( '1' );
124
			}
125
			$tags    = isset( $_POST['tags'] ) ? wp_unslash( $_POST['tags'] ) : array();
126
			$list_id = isset( $_POST['list_id'] ) ? filter_var( wp_unslash( $_POST['list_id'] ), FILTER_SANITIZE_STRING ) : '';
127
			$form_id = isset( $_POST['form_id'] ) ? filter_var( wp_unslash( $_POST['form_id'] ), FILTER_SANITIZE_NUMBER_INT ) : 0;
128
129
			if ( empty( $tags ) || empty( $list_id ) || empty( $form_id ) ) {
130
				wp_send_json_error( '2' );
131
			}
132
133
			$form_interface = yikes_easy_mailchimp_extender_get_form_interface();
134
			$form           = $form_interface->get_form( $form_id );
135
			$form_tags      = array();
136
137
			// This data came from $_POST so sanitize it.
138
			foreach ( $tags as $tag ) {
139
				$form_tags[ filter_var( $tag['tag_id'], FILTER_SANITIZE_NUMBER_INT ) ] = array(
140
					'name' => filter_var( $tag['tag_name'], FILTER_SANITIZE_STRING ),
141
					'id'   => filter_var( $tag['tag_id'], FILTER_SANITIZE_NUMBER_INT ),
142
				);
143
			}
144
145
			$form['tags'] = $form_tags + ( isset( $form['tags'] ) ? $form['tags'] : array() );
146
			$form_interface->update_form( $form_id, $form );
147
			wp_send_json_success( array( 'tags' => $form_tags ) );
148
		}
149
150
		/**
151
		 * Remove a tag from a form.
152
		 */
153
		public function remove_tag_from_form() {
154
			// Verify Nonce.
155
			if ( ! check_ajax_referer( 'remove-tag', 'nonce', false ) ) {
156
				wp_send_json_error( '1' );
157
			}
158
			$tag     = isset( $_POST['tag'] ) ? filter_var( wp_unslash( $_POST['tag'] ), FILTER_SANITIZE_NUMBER_INT ) : array();
159
			$list_id = isset( $_POST['list_id'] ) ? filter_var( wp_unslash( $_POST['list_id'] ), FILTER_SANITIZE_STRING ) : '';
160
			$form_id = isset( $_POST['form_id'] ) ? filter_var( wp_unslash( $_POST['form_id'] ), FILTER_SANITIZE_NUMBER_INT ) : 0;
161
162
			if ( empty( $tag ) || empty( $list_id ) || empty( $form_id ) ) {
163
				wp_send_json_error( '2' );
164
			}
165
166
			$form_interface = yikes_easy_mailchimp_extender_get_form_interface();
167
			$form           = $form_interface->get_form( $form_id );
168
			if ( isset( $form['tags'] ) && isset( $form['tags'][ $tag ] ) ) {
169
				unset( $form['tags'][ $tag ] );
170
			}
171
			$form_interface->update_form( $form_id, $form );
172
			wp_send_json_success();			
173
		}
174
175
		/*
176
		*	Search through multi dimensional array
177
		*	and return the index ( used to find the list name assigned to a form )
178
		*	- http://stackoverflow.com/questions/6661530/php-multi-dimensional-array-search
179
		*/
180
		public function findMCListIndex( $id, $array, $tag ) {
181
			$mapping = array_flip( wp_list_pluck( $array, $tag ) );
182
			$index   = isset( $mapping[ $id ] ) ? $mapping[ $id ] : null;
183
184
			return $index;
185
		}
186
187
		/**
188
		* Save changes to a field's label
189
		*/ 
190
		public function save_field_label_edits() {
191
192
			// Capture our $_POST variables
193
			$list_id	= isset( $_POST['list_id'] ) ? $_POST['list_id'] : '';
194
			$field_data = isset( $_POST['field_data'] ) ? $_POST['field_data'] : array();
195
			$field_name = isset( $field_data['field_name'] ) ? $field_data['field_name'] : '';
196
			$field_id	= isset( $field_data['field_id'] ) ? $field_data['field_id'] : '';
197
198
			// Make sure we have our required variables before continuing
199
			if ( $list_id === '' || $field_name === '' || $field_id === '' ) {
200
				wp_send_json_error( array(
201
						'message' => __( 'Could not update the field label: missing required field.', 'yikes-inc-easy-mailchimp-extender' ),
202
						'developer-info' => "One of the following variables was empty: list_id: $list_id, field_name: $field_name, field_id: $field_id."
203
					)
204
				);
205
			}
206
207
			// Update the field!
208
			$merge_field = yikes_get_mc_api_manager()->get_list_handler()->update_merge_field( $list_id, $field_id, array( 'name' => $field_name ), true );
209
			
210
			// Check for an error. If error, log it and return error
211
			if ( is_wp_error( $merge_field ) ) {
212
				$error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging();
213
				$error_logging->maybe_write_to_log( 
214
					$merge_field->get_error_code(), 
215
					__( "Updating merge field", 'yikes-inc-easy-mailchimp-extender' ), 
216
					"class.ajax.php"
217
				);
218
				wp_send_json_error( array(
219
						'message' => __( 'Could not update the field label: API request failed.', 'yikes-inc-easy-mailchimp-extender' ),
220
						'developer-info' => $error
0 ignored issues
show
Bug introduced by
The variable $error does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
221
					)
222
				);
223
			}
224
225
			wp_send_json_success();
226
		}
227
	}
228