wordpoints_ajax_save_points_hook()   F
last analyzed

Complexity

Conditions 27
Paths 2320

Size

Total Lines 173
Code Lines 72

Duplication

Lines 38
Ratio 21.97 %

Importance

Changes 0
Metric Value
cc 27
eloc 72
nc 2320
nop 0
dl 38
loc 173
rs 2
c 0
b 0
f 0

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
/**
4
 * AJAX callbacks for the points component administration screens.
5
 *
6
 * @package WordPoints\Points\Administration
7
 * @since 1.2.0
8
 */
9
10
/**
11
 * Save points hooks order via AJAX.
12
 *
13
 * @since 1.0.0
14
 *
15
 * @WordPress\action wp_ajax_wordpoints-points-hooks-order
16
 */
17
function wordpoints_ajax_points_hooks_order() {
18
19 View Code Duplication
	if ( check_ajax_referer( 'save-network-wordpoints-points-hooks', 'savehooks', false ) ) {
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...
Bug introduced by
The function check_ajax_referer was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
	if ( /** @scrutinizer ignore-call */ check_ajax_referer( 'save-network-wordpoints-points-hooks', 'savehooks', false ) ) {
Loading history...
20
21
		if ( ! current_user_can( 'manage_network_wordpoints_points_hooks' ) ) {
0 ignored issues
show
Bug introduced by
The function current_user_can was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
		if ( ! /** @scrutinizer ignore-call */ current_user_can( 'manage_network_wordpoints_points_hooks' ) ) {
Loading history...
22
			wp_die( -1, '', array( 'response' => 403 ) );
0 ignored issues
show
Bug introduced by
The function wp_die was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
			/** @scrutinizer ignore-call */ 
23
   wp_die( -1, '', array( 'response' => 403 ) );
Loading history...
23
		}
24
25
		// Saving network hooks order, turn on network mode.
26
		WordPoints_Points_Hooks::set_network_mode( true );
27
28
	} elseif ( check_ajax_referer( 'save-wordpoints-points-hooks', 'savehooks', false ) ) {
29
30
		if ( ! current_user_can( 'manage_options' ) ) {
31
			wp_die( -1, '', array( 'response' => 403 ) );
32
		}
33
34
	} else {
35
36
		// CSRF attack (or, more probably, the user left the browser open too long).
37
		wp_die( -1, '', array( 'response' => 403 ) );
38
	}
39
40
	// Save hooks order for all points types.
41
	if ( ! isset( $_POST['points_types'] ) || ! is_array( $_POST['points_types'] ) ) {
42
		wp_die( -1, '', array( 'response' => 400 ) );
43
	}
44
45
	$points_types_hooks = array();
46
47
	foreach ( array_keys( wordpoints_get_points_types() ) as $points_type ) {
48
49
		if ( empty( $_POST['points_types'][ $points_type ] ) ) {
50
			continue;
51
		}
52
53
		$hooks = sanitize_text_field( wp_unslash( $_POST['points_types'][ $points_type ] ) );
0 ignored issues
show
Bug introduced by
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
		$hooks = /** @scrutinizer ignore-call */ sanitize_text_field( wp_unslash( $_POST['points_types'][ $points_type ] ) );
Loading history...
Bug introduced by
The function wp_unslash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
		$hooks = sanitize_text_field( /** @scrutinizer ignore-call */ wp_unslash( $_POST['points_types'][ $points_type ] ) );
Loading history...
54
55
		$points_type_hooks = array();
56
57
		$hooks = explode( ',', $hooks );
58
59
		foreach ( $hooks as $order => $hook_id ) {
60
61
			if ( false === strpos( $hook_id, 'hook-' ) ) {
62
				continue;
63
			}
64
65
			$points_type_hooks[ $order ] = substr( $hook_id, strpos( $hook_id, '_' ) + 1 );
66
		}
67
68
		$points_types_hooks[ $points_type ] = $points_type_hooks;
69
	}
70
71
	WordPoints_Points_Hooks::save_points_types_hooks( $points_types_hooks );
72
73
	wp_die( 1, '', array( 'response' => 200 ) );
74
}
75
76
/**
77
 * Save points hook settings via AJAX.
78
 *
79
 * @since 1.0.0
80
 * @since 2.1.0 Deprecated handling of CRUD actions for points types.
81
 *
82
 * @WordPress\action wp_ajax_save-wordpoints-points-hook
83
 */
84
function wordpoints_ajax_save_points_hook() {
85
86 View Code Duplication
	if ( check_ajax_referer( 'save-network-wordpoints-points-hooks', 'savehooks', false ) ) {
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...
Bug introduced by
The function check_ajax_referer was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
	if ( /** @scrutinizer ignore-call */ check_ajax_referer( 'save-network-wordpoints-points-hooks', 'savehooks', false ) ) {
Loading history...
87
88
		if ( ! current_user_can( 'manage_network_wordpoints_points_hooks' ) ) {
0 ignored issues
show
Bug introduced by
The function current_user_can was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
		if ( ! /** @scrutinizer ignore-call */ current_user_can( 'manage_network_wordpoints_points_hooks' ) ) {
Loading history...
89
			wp_die( -1, '', array( 'response' => 403 ) );
0 ignored issues
show
Bug introduced by
The function wp_die was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
			/** @scrutinizer ignore-call */ 
90
   wp_die( -1, '', array( 'response' => 403 ) );
Loading history...
90
		}
91
92
		// Saving network hooks, turn on network mode.
93
		WordPoints_Points_Hooks::set_network_mode( true );
94
95
	} elseif ( check_ajax_referer( 'save-wordpoints-points-hooks', 'savehooks', false ) ) {
96
97
		if ( ! current_user_can( 'manage_options' ) ) {
98
			wp_die( -1, '', array( 'response' => 403 ) );
99
		}
100
101
	} else {
102
103
		// CSRF attack (or, more probably the user left the browser open too long).
104
		wp_die( -1, '', array( 'response' => 403 ) );
105
	}
106
107
	$error = __( 'An error has occurred. Please reload the page and try again.', 'wordpoints' );
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
	$error = /** @scrutinizer ignore-call */ __( 'An error has occurred. Please reload the page and try again.', 'wordpoints' );
Loading history...
108
109
	if ( isset( $_POST['points-slug'] ) ) {
110
111
		// - We are saving the settings for a points type.
112
113
		// We don't use this part after 2.1.0, but we keep it around for back-compat.
114
		_deprecated_argument(
0 ignored issues
show
Bug introduced by
The function _deprecated_argument was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
		/** @scrutinizer ignore-call */ 
115
  _deprecated_argument(
Loading history...
115
			__FUNCTION__
116
			, '2.1.0'
117
			, 'Performing CRUD actions on points types using this function is deprecated.'
118
		);
119
120
		if ( ! current_user_can( 'manage_wordpoints_points_types' ) ) {
121
			wp_die( -1, '', array( 'response' => 403 ) );
122
		}
123
124
		$settings = array();
125
126 View Code Duplication
		if ( isset( $_POST['points-name'] ) ) {
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...
127
			$settings['name'] = trim( sanitize_text_field( wp_unslash( $_POST['points-name'] ) ) );
0 ignored issues
show
Bug introduced by
The function wp_unslash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
			$settings['name'] = trim( sanitize_text_field( /** @scrutinizer ignore-call */ wp_unslash( $_POST['points-name'] ) ) );
Loading history...
Bug introduced by
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
			$settings['name'] = trim( /** @scrutinizer ignore-call */ sanitize_text_field( wp_unslash( $_POST['points-name'] ) ) );
Loading history...
128
		}
129
130 View Code Duplication
		if ( isset( $_POST['points-prefix'] ) ) {
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...
131
			$settings['prefix'] = ltrim( sanitize_text_field( wp_unslash( $_POST['points-prefix'] ) ) );
132
		}
133
134 View Code Duplication
		if ( isset( $_POST['points-suffix'] ) ) {
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...
135
			$settings['suffix'] = rtrim( sanitize_text_field( wp_unslash( $_POST['points-suffix'] ) ) );
136
		}
137
138
		$points_type = sanitize_key( $_POST['points-slug'] );
0 ignored issues
show
Bug introduced by
The function sanitize_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
		$points_type = /** @scrutinizer ignore-call */ sanitize_key( $_POST['points-slug'] );
Loading history...
139
140
		$old_settings = wordpoints_get_points_type( $points_type );
141
142
		if ( false === $old_settings ) {
143
			wp_die( -1, '', array( 'response' => 200 ) );
144
		}
145
146
		if ( is_array( $old_settings ) ) {
147
			$settings = array_merge( $old_settings, $settings );
148
		}
149
150
		if ( ! wordpoints_update_points_type( $points_type, $settings ) ) {
151
152
			// If this fails, show the user a message along with the form.
153
			echo '<p>' . esc_html__( 'An error has occurred. Please try again.', 'wordpoints' ) . '</p>';
0 ignored issues
show
Bug introduced by
The function esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
			echo '<p>' . /** @scrutinizer ignore-call */ esc_html__( 'An error has occurred. Please try again.', 'wordpoints' ) . '</p>';
Loading history...
154
155
			WordPoints_Points_Hooks::points_type_form( $points_type, 'none' );
0 ignored issues
show
Deprecated Code introduced by
The function WordPoints_Points_Hooks::points_type_form() has been deprecated: 2.1.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

155
			/** @scrutinizer ignore-deprecated */ WordPoints_Points_Hooks::points_type_form( $points_type, 'none' );

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
156
		}
157
158
	} else {
159
160
		// - We are creating/updating/deleting an instance of a hook.
161
162
		if ( ! isset( $_POST['id_base'], $_POST['hook-id'], $_POST['points_type'], $_POST['hook_number'] ) ) {
163
			wp_die( -1, '', array( 'response' => 400 ) );
164
		}
165
166
		$id_base        = sanitize_key( $_POST['id_base'] );
167
		$hook_id        = sanitize_key( $_POST['hook-id'] );
168
		$points_type_id = sanitize_key( $_POST['points_type'] );
169
		$number         = (int) $_POST['hook_number'];
170
171
		/*
172
		 * Normally the hook ID will be in 'hook-id' when we are updating a hook.
173
		 * But when we are saving a brand new instance of a hook or updating a newly
174
		 * created hook, the ID won't have been set when the form was output, so
175
		 * 'hook-id' will be empty, and we'll get the ID from 'multi_number'.
176
		 */
177
		if ( ! $number ) {
178
179
			// This holds the ID number if the hook is brand new.
180
			if ( ! isset( $_POST['multi_number'] ) || ! wordpoints_posint( $_POST['multi_number'] ) ) {
181
				wp_die( '<p>' . esc_html( $error ) . '</p>', '', array( 'response' => 400 ) );
0 ignored issues
show
Bug introduced by
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

181
				wp_die( '<p>' . /** @scrutinizer ignore-call */ esc_html( $error ) . '</p>', '', array( 'response' => 400 ) );
Loading history...
182
			}
183
184
			$number  = (int) $_POST['multi_number'];
185
			$hook_id = $id_base . '-' . $number;
186
		}
187
188
		$hook = WordPoints_Points_Hooks::get_handler( $hook_id );
189
190
		$settings = false;
191
192 View Code Duplication
		if ( isset( $_POST[ 'hook-' . $id_base ] ) && is_array( $_POST[ 'hook-' . $id_base ] ) ) {
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...
193
			$settings = wp_unslash( $_POST[ 'hook-' . $id_base ] ); // WPCS: sanitization OK.
194
		}
195
196
		$points_types_hooks = WordPoints_Points_Hooks::get_points_types_hooks();
197
198
		// Get the hooks for this points type.
199
		$points_type_hooks = ( isset( $points_types_hooks[ $points_type_id ] ) ) ? $points_types_hooks[ $points_type_id ] : array();
200
201
		if ( ! empty( $_POST['delete_hook'] ) ) {
202
203
			// - We are deleting a hook instance.
204
205 View Code Duplication
			if ( false === $hook ) {
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...
206
				wp_die( '<p>' . esc_html( $error ) . '</p>', '', array( 'response' => 400 ) );
207
			}
208
209
			$hook->delete_callback( $number );
210
211
			// Remove this instance of the hook, and reset the positions (keys).
212
			$points_types_hooks[ $points_type_id ] = array_diff( $points_type_hooks, array( $hook_id ) );
213
214
			WordPoints_Points_Hooks::save_points_types_hooks( $points_types_hooks );
215
216
			wp_die( esc_html( "deleted:{$hook_id}" ), '', array( 'response' => 200 ) );
217
218
		} elseif ( $settings && false === $hook ) {
219
220
			// - We are creating a new instance of a hook.
221
222
			/*
223
			 * Get a hook object for this type of hook. We have to do this because
224
			 * since the hook is new, it hasn't been assigned an ID yet, so we can't
225
			 * just get it from the array of hooks by ID.
226
			 */
227
			$hook = WordPoints_Points_Hooks::get_handler_by_id_base( $id_base );
228
229
			$new_instance = reset( $settings );
230
231
			// Save the points types-hooks associations.
232
			$points_type_hooks[] = $hook->get_id( $number );
233
			$points_types_hooks[ $points_type_id ] = $points_type_hooks;
234
			WordPoints_Points_Hooks::save_points_types_hooks( $points_types_hooks );
235
236
		} else {
237
238
			// - We are updating the settings for an instance of a hook.
239
240 View Code Duplication
			if ( false === $hook ) {
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...
241
				wp_die( '<p>' . esc_html( $error ) . '</p>', '', array( 'response' => 400 ) );
242
			}
243
244
			$new_instance = ( ! empty( $settings ) ) ? reset( $settings ) : array();
245
246
		} // End if ( deleting ) elseif ( creating ) else { updating }.
247
248
		$hook->update_callback( wp_unslash( $new_instance ), $number );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $new_instance does not seem to be defined for all execution paths leading up to this point.
Loading history...
249
250
		if ( empty( $_POST['add_new'] ) ) {
251
			$hook->form_callback( $number );
252
		}
253
254
	} // End if ( saving points type ) else { deleting/creation/updating hook }.
255
256
	wp_die( '', '', array( 'response' => 200 ) );
257
}
258
259
// EOF
260