Completed
Push — issue-2338 ( 933f16 )
by Ravinder
1012:07 queued 1006:24
created

dashboard-columns.php ➔ give_render_form_columns()   D

Complexity

Conditions 16
Paths 14

Size

Total Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
nc 14
nop 2
dl 0
loc 82
rs 4.846
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
 * Dashboard Columns
4
 *
5
 * @package     GIVE
6
 * @subpackage  Admin/Downloads
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
18
/**
19
 * Give Forms Columns
20
 *
21
 * Defines the custom columns and their order
22
 *
23
 * @since 1.0
24
 *
25
 * @param array $give_form_columns Array of forms columns
26
 *
27
 * @return array $form_columns Updated array of forms columns
28
 *  Post Type List Table
29
 */
30
function give_form_columns( $give_form_columns ) {
0 ignored issues
show
Unused Code introduced by
The parameter $give_form_columns is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
32
	// Standard columns
33
	$give_form_columns = array(
34
		'cb'            => '<input type="checkbox"/>',
35
		'title'         => __( 'Name', 'give' ),
36
		'form_category' => __( 'Categories', 'give' ),
37
		'form_tag'      => __( 'Tags', 'give' ),
38
		'price'         => __( 'Amount', 'give' ),
39
		'goal'          => __( 'Goal', 'give' ),
40
		'donations'     => __( 'Donations', 'give' ),
41
		'earnings'      => __( 'Income', 'give' ),
42
		'shortcode'     => __( 'Shortcode', 'give' ),
43
		'date'          => __( 'Date', 'give' ),
44
	);
45
46
	// Does the user want categories / tags?
47
	if ( ! give_is_setting_enabled( give_get_option( 'categories', 'disabled' ) ) ) {
48
		unset( $give_form_columns['form_category'] );
49
	}
50
	if ( ! give_is_setting_enabled( give_get_option( 'tags', 'disabled' ) ) ) {
51
		unset( $give_form_columns['form_tag'] );
52
	}
53
54
	return apply_filters( 'give_forms_columns', $give_form_columns );
55
}
56
57
add_filter( 'manage_edit-give_forms_columns', 'give_form_columns' );
58
59
/**
60
 * Render Give Form Columns
61
 *
62
 * @since 1.0
63
 *
64
 * @param string $column_name Column name
65
 * @param int    $post_id     Give Form (Post) ID
66
 *
67
 * @return void
68
 */
69
function give_render_form_columns( $column_name, $post_id ) {
70
	if ( get_post_type( $post_id ) == 'give_forms' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
71
72
		switch ( $column_name ) {
73
			case 'form_category':
74
				echo get_the_term_list( $post_id, 'give_forms_category', '', ', ', '' );
75
				break;
76
			case 'form_tag':
77
				echo get_the_term_list( $post_id, 'give_forms_tag', '', ', ', '' );
78
				break;
79
			case 'price':
80
				if ( give_has_variable_prices( $post_id ) ) {
81
					echo give_price_range( $post_id );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_price_range'
Loading history...
82
				} else {
83
					echo give_price( $post_id, false );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_price'
Loading history...
84
					printf( '<input type="hidden" class="formprice-%1$s" value="%2$s" />', esc_attr( $post_id ), esc_attr( give_get_form_price( $post_id ) ) );
85
				}
86
				break;
87
			case 'goal':
88
				if ( give_is_setting_enabled( give_get_meta( $post_id, '_give_goal_option', true ) ) ) {
89
90
					$goal_stats = give_goal_progress_stats( $post_id );
91
					$html = '';
92
93
					$html .= sprintf(
94
						( 'percentage' !== $goal_stats['format'] ) ?
95
							'<div class="give-goal-text"><span>%1$s</span> %2$s <a href="%3$s">%4$s</a></div>' :
96
							'<div class="give-goal-text"><a href="%3$s">%1$s</a></div>',
97
						$goal_stats['actual'],
98
						( 'percentage' !== $goal_stats['format'] ) ? __( 'of', 'give' ) : '',
99
						esc_url( admin_url( "post.php?post={$post_id}&action=edit&give_tab=donation_goal_options" ) ),
100
						$goal_stats['goal']
101
					);
102
103
					if ( $goal_stats['raw_actual'] >= $goal_stats['raw_goal'] ) {
104
						$html .= sprintf( '<span class="goal-achieved">%s</span>', __( 'Goal achieved', 'give' ) );
105
					} else {
106
						$html .= sprintf( '<div class="give-progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="%s">', esc_attr( $goal_stats['progress'] ) );
107
						$html .= sprintf( '<span style="width:%s%%;"></span>', esc_attr( $goal_stats['progress'] ) );
108
						$html .= '</div>';
109
					}
110
111
					echo $html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$html'
Loading history...
112
				} else {
113
					esc_html_e( 'No Goal Set', 'give' );
114
				}
115
116
				printf(
117
					'<input type="hidden" class="formgoal-%1$s" value="%2$s" />',
118
					esc_attr( $post_id ),
119
					give_get_form_goal( $post_id )
120
				);
121
122
				break;
123
			case 'donations':
124
				if ( current_user_can( 'view_give_form_stats', $post_id ) ) {
125
					printf(
126
						'<a href="%1$s">%2$s</a>',
127
						esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&form_id=' . $post_id ) ),
128
						give_get_form_sales_stats( $post_id )
129
					);
130
				} else {
131
					echo '-';
132
				}
133
				break;
134
			case 'earnings':
135
				if ( current_user_can( 'view_give_form_stats', $post_id ) ) {
136
					printf(
137
						'<a href="%1$s">%2$s</a>',
138
						esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-reports&tab=forms&form-id=' . $post_id ) ),
139
						give_currency_filter( give_format_amount( give_get_form_earnings_stats( $post_id ), array( 'sanitize' => false ) ) )
140
					);
141
				} else {
142
					echo '-';
143
				}
144
				break;
145
			case 'shortcode':
146
				printf( '<input onclick="this.setSelectionRange(0, this.value.length)" type="text" class="shortcode-input" readonly="" value="[give_form id=&#34;%s&#34;]"', absint( $post_id ) );
147
				break;
148
		}// End switch().
149
	}// End if().
150
}
151
152
add_action( 'manage_posts_custom_column', 'give_render_form_columns', 10, 2 );
153
154
/**
155
 * Registers the sortable columns in the list table
156
 *
157
 * @since 1.0
158
 *
159
 * @param array $columns Array of the columns
160
 *
161
 * @return array $columns Array of sortable columns
162
 */
163
function give_sortable_form_columns( $columns ) {
164
	$columns['price']     = 'amount';
165
	$columns['sales']     = 'sales';
166
	$columns['earnings']  = 'earnings';
167
	$columns['goal']      = 'goal';
168
	$columns['donations'] = 'donations';
169
170
	return $columns;
171
}
172
173
add_filter( 'manage_edit-give_forms_sortable_columns', 'give_sortable_form_columns' );
174
175
/**
176
 * Sorts Columns in the Forms List Table
177
 *
178
 * @since 1.0
179
 *
180
 * @param array $vars Array of all the sort variables.
181
 *
182
 * @return array $vars Array of all the sort variables.
183
 */
184
function give_sort_forms( $vars ) {
185
	// Check if we're viewing the "give_forms" post type.
186
	if ( ! isset( $vars['post_type'] ) || ! isset( $vars['orderby'] ) || 'give_forms' !== $vars['post_type'] ) {
187
		return $vars;
188
	}
189
190
	switch ( $vars['orderby'] ) {
191
		// Check if 'orderby' is set to "sales".
192
		case 'sales':
193
			$vars = array_merge(
194
				$vars,
195
				array(
196
					'meta_key' => '_give_form_sales',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
197
					'orderby'  => 'meta_value_num',
198
				)
199
			);
200
			break;
201
202
		// Check if "orderby" is set to "earnings".
203
		case 'earnings':
204
			$vars = array_merge(
205
				$vars,
206
				array(
207
					'meta_key' => '_give_form_earnings',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
208
					'orderby'  => 'meta_value_num',
209
				)
210
			);
211
			break;
212
213
		// Check if "orderby" is set to "price/amount".
214
		case 'amount':
215
			$multi_level_meta_key = ( 'asc' === $vars['order'] ) ? '_give_levels_minimum_amount' : '_give_levels_maximum_amount';
216
217
			$vars['orderby']    = 'meta_value_num';
218
			$vars['meta_query'] = array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
219
				'relation' => 'OR',
220
				array(
221
					'key'     => $multi_level_meta_key,
222
					'type'    => 'NUMERIC',
223
				),
224
				array(
225
					'key'     => '_give_set_price',
226
					'type'    => 'NUMERIC',
227
				)
228
			);
229
230
			break;
231
232
		// Check if "orderby" is set to "goal".
233
		case 'goal':
234
			$vars = array_merge(
235
				$vars,
236
				array(
237
					'meta_key' => '_give_set_goal',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
238
					'orderby'  => 'meta_value_num',
239
				)
240
			);
241
			break;
242
243
		// Check if "orderby" is set to "donations".
244
		case 'donations':
245
			$vars = array_merge(
246
				$vars,
247
				array(
248
					'meta_key' => '_give_form_sales',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
249
					'orderby'  => 'meta_value_num',
250
				)
251
			);
252
			break;
253
	}// End switch().
254
255
	return $vars;
256
}
257
258
/**
259
 * Sets restrictions on author of Forms List Table
260
 *
261
 * @since  1.0
262
 *
263
 * @param  array $vars Array of all sort variables.
264
 *
265
 * @return array       Array of all sort variables.
266
 */
267
function give_filter_forms( $vars ) {
268
	if ( isset( $vars['post_type'] ) && 'give_forms' == $vars['post_type'] ) {
269
270
		// If an author ID was passed, use it
271
		if ( isset( $_REQUEST['author'] ) && ! current_user_can( 'view_give_reports' ) ) {
272
273
			$author_id = $_REQUEST['author'];
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
274
			if ( (int) $author_id !== get_current_user_id() ) {
275
				wp_die( esc_html__( 'You do not have permission to view this data.', 'give' ), esc_html__( 'Error', 'give' ), array(
276
					'response' => 403,
277
				) );
278
			}
279
			$vars = array_merge(
280
				$vars,
281
				array(
282
					'author' => get_current_user_id(),
283
				)
284
			);
285
286
		}
287
	}
288
289
	return $vars;
290
}
291
292
/**
293
 * Form Load
294
 *
295
 * Sorts the form columns.
296
 *
297
 * @since 1.0
298
 * @return void
299
 */
300
function give_forms_load() {
301
	add_filter( 'request', 'give_sort_forms' );
302
	add_filter( 'request', 'give_filter_forms' );
303
}
304
305
add_action( 'load-edit.php', 'give_forms_load', 9999 );
306
307
/**
308
 * Remove Forms Month Filter
309
 *
310
 * Removes the default drop down filter for forms by date.
311
 *
312
 * @since  1.0
313
 *
314
 * @param array $dates   The preset array of dates.
315
 *
316
 * @global      $typenow The post type we are viewing.
317
 * @return array Empty array disables the dropdown.
318
 */
319
function give_remove_month_filter( $dates ) {
320
	global $typenow;
321
322
	if ( $typenow == 'give_forms' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
323
		$dates = array();
324
	}
325
326
	return $dates;
327
}
328
329
add_filter( 'months_dropdown_results', 'give_remove_month_filter', 99 );
330
331
/**
332
 * Updates price when saving post
333
 *
334
 * @since 1.0
335
 *
336
 * @param int $post_id Download (Post) ID
337
 *
338
 * @return int|null
339
 */
340
function give_price_save_quick_edit( $post_id ) {
341
	if ( ! isset( $_POST['post_type'] ) || 'give_forms' !== $_POST['post_type'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
342
		return;
343
	}
344
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
345
		return $post_id;
346
	}
347
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
348
		return $post_id;
349
	}
350
351
	if ( isset( $_REQUEST['_give_regprice'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
352
		give_update_meta( $post_id, '_give_set_price', give_sanitize_amount_for_db( strip_tags( stripslashes( $_REQUEST['_give_regprice'] ) ) ) );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
353
	}
354
}
355
356
add_action( 'save_post', 'give_price_save_quick_edit' );
357
358
/**
359
 * Process bulk edit actions via AJAX
360
 *
361
 * @since 1.0
362
 * @return void
363
 */
364
function give_save_bulk_edit() {
365
366
	$post_ids = ( isset( $_POST['post_ids'] ) && ! empty( $_POST['post_ids'] ) ) ? $_POST['post_ids'] : array();
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
367
368
	if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {
369
		$price = isset( $_POST['price'] ) ? strip_tags( stripslashes( $_POST['price'] ) ) : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
370
		foreach ( $post_ids as $post_id ) {
371
372
			if ( ! current_user_can( 'edit_post', $post_id ) ) {
373
				continue;
374
			}
375
376
			if ( ! empty( $price ) ) {
377
				give_update_meta( $post_id, '_give_set_price', give_sanitize_amount_for_db( $price ) );
378
			}
379
		}
380
	}
381
382
	die();
383
}
384
385
add_action( 'wp_ajax_give_save_bulk_edit', 'give_save_bulk_edit' );
386