Test Failed
Push — release/1.8.12 ( b58a2f...d255b1 )
by Ravinder
375:09 queued 372:17
created

includes/admin/forms/dashboard-columns.php (27 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 ) {
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
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
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
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_price'
Loading history...
84
					echo '<input type="hidden" class="formprice-' . $post_id . '" value="' . give_get_form_price( $post_id ) . '" />';
0 ignored issues
show
Expected next thing to be a escaping function, not '$post_id'
Loading history...
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_form_price'
Loading history...
85
				}
86
				break;
87
			case 'goal':
88
				if ( give_is_setting_enabled( give_get_meta( $post_id, '_give_goal_option', true ) ) ) {
89
					echo give_goal( $post_id, false );
0 ignored issues
show
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_goal'
Loading history...
90
				} else {
91
					esc_html_e( 'No Goal Set', 'give' );
92
				}
93
94
				echo '<input type="hidden" class="formgoal-' . $post_id . '" value="' . give_get_form_goal( $post_id ) . '" />';
0 ignored issues
show
Expected next thing to be a escaping function, not '$post_id'
Loading history...
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_form_goal'
Loading history...
95
				break;
96
			case 'donations':
97
				if ( current_user_can( 'view_give_form_stats', $post_id ) ) {
98
					echo '<a href="' . esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&form_id=' . $post_id ) ) . '">';
99
					echo give_get_form_sales_stats( $post_id );
0 ignored issues
show
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_form_sales_stats'
Loading history...
100
					echo '</a>';
101
				} else {
102
					echo '-';
103
				}
104
				break;
105
			case 'earnings':
106
				if ( current_user_can( 'view_give_form_stats', $post_id ) ) {
107
					echo '<a href="' . esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-reports&tab=forms&form-id=' . $post_id ) ) . '">';
108
					echo give_currency_filter( give_format_amount( give_get_form_earnings_stats( $post_id ), array( 'sanitize' => false ) ) );
0 ignored issues
show
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_currency_filter'
Loading history...
109
					echo '</a>';
110
				} else {
111
					echo '-';
112
				}
113
				break;
114
			case 'shortcode':
115
				echo '<input onclick="this.setSelectionRange(0, this.value.length)" type="text" class="shortcode-input" readonly="" value="[give_form id=&#34;' . absint( $post_id ) . '&#34;]">';
116
				break;
117
		}// End switch().
118
	}// End if().
119
}
120
121
add_action( 'manage_posts_custom_column', 'give_render_form_columns', 10, 2 );
122
123
/**
124
 * Registers the sortable columns in the list table
125
 *
126
 * @since 1.0
127
 *
128
 * @param array $columns Array of the columns
129
 *
130
 * @return array $columns Array of sortable columns
131
 */
132
function give_sortable_form_columns( $columns ) {
133
	$columns['price']     = 'amount';
134
	$columns['sales']     = 'sales';
135
	$columns['earnings']  = 'earnings';
136
	$columns['goal']      = 'goal';
137
	$columns['donations'] = 'donations';
138
139
	return $columns;
140
}
141
142
add_filter( 'manage_edit-give_forms_sortable_columns', 'give_sortable_form_columns' );
143
144
/**
145
 * Sorts Columns in the Forms List Table
146
 *
147
 * @since 1.0
148
 *
149
 * @param array $vars Array of all the sort variables.
150
 *
151
 * @return array $vars Array of all the sort variables.
152
 */
153
function give_sort_forms( $vars ) {
154
	// Check if we're viewing the "give_forms" post type.
155
	if ( ! isset( $vars['post_type'] ) || ! isset( $vars['orderby'] ) || 'give_forms' !== $vars['post_type'] ) {
156
		return $vars;
157
	}
158
159
	switch ( $vars['orderby'] ) {
160
		// Check if 'orderby' is set to "sales".
161
		case 'sales':
162
			$vars = array_merge(
163
				$vars,
164
				array(
165
					'meta_key' => '_give_form_sales',
0 ignored issues
show
Detected usage of meta_key, possible slow query.
Loading history...
166
					'orderby'  => 'meta_value_num',
167
				)
168
			);
169
			break;
170
171
		// Check if "orderby" is set to "earnings".
172
		case 'earnings':
173
			$vars = array_merge(
174
				$vars,
175
				array(
176
					'meta_key' => '_give_form_earnings',
0 ignored issues
show
Detected usage of meta_key, possible slow query.
Loading history...
177
					'orderby'  => 'meta_value_num',
178
				)
179
			);
180
			break;
181
182
		// Check if "orderby" is set to "price/amount".
183
		case 'amount':
184
			$multi_level_meta_key = ( 'asc' === $vars['order'] ) ? '_give_levels_minimum_amount' : '_give_levels_maximum_amount';
185
186
			$vars['orderby']    = 'meta_value_num';
187
			$vars['meta_query'] = array(
0 ignored issues
show
Detected usage of meta_query, possible slow query.
Loading history...
188
				'relation' => 'OR',
189
				array(
190
					'key'     => $multi_level_meta_key,
191
					'type'    => 'NUMERIC',
192
				),
193
				array(
194
					'key'     => '_give_set_price',
195
					'type'    => 'NUMERIC',
196
				)
197
			);
198
199
			break;
200
201
		// Check if "orderby" is set to "goal".
202
		case 'goal':
203
			$vars = array_merge(
204
				$vars,
205
				array(
206
					'meta_key' => '_give_set_goal',
0 ignored issues
show
Detected usage of meta_key, possible slow query.
Loading history...
207
					'orderby'  => 'meta_value_num',
208
				)
209
			);
210
			break;
211
212
		// Check if "orderby" is set to "donations".
213
		case 'donations':
214
			$vars = array_merge(
215
				$vars,
216
				array(
217
					'meta_key' => '_give_form_sales',
0 ignored issues
show
Detected usage of meta_key, possible slow query.
Loading history...
218
					'orderby'  => 'meta_value_num',
219
				)
220
			);
221
			break;
222
	}// End switch().
223
224
	return $vars;
225
}
226
227
/**
228
 * Sets restrictions on author of Forms List Table
229
 *
230
 * @since  1.0
231
 *
232
 * @param  array $vars Array of all sort variables.
233
 *
234
 * @return array       Array of all sort variables.
235
 */
236
function give_filter_forms( $vars ) {
237
	if ( isset( $vars['post_type'] ) && 'give_forms' == $vars['post_type'] ) {
238
239
		// If an author ID was passed, use it
240
		if ( isset( $_REQUEST['author'] ) && ! current_user_can( 'view_give_reports' ) ) {
241
242
			$author_id = $_REQUEST['author'];
0 ignored issues
show
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
243
			if ( (int) $author_id !== get_current_user_id() ) {
244
				wp_die( esc_html__( 'You do not have permission to view this data.', 'give' ), esc_html__( 'Error', 'give' ), array(
245
					'response' => 403,
246
				) );
247
			}
248
			$vars = array_merge(
249
				$vars,
250
				array(
251
					'author' => get_current_user_id(),
252
				)
253
			);
254
255
		}
256
	}
257
258
	return $vars;
259
}
260
261
/**
262
 * Form Load
263
 *
264
 * Sorts the form columns.
265
 *
266
 * @since 1.0
267
 * @return void
268
 */
269
function give_forms_load() {
270
	add_filter( 'request', 'give_sort_forms' );
271
	add_filter( 'request', 'give_filter_forms' );
272
}
273
274
add_action( 'load-edit.php', 'give_forms_load', 9999 );
275
276
/**
277
 * Remove Forms Month Filter
278
 *
279
 * Removes the default drop down filter for forms by date.
280
 *
281
 * @since  1.0
282
 *
283
 * @param array $dates   The preset array of dates.
284
 *
285
 * @global      $typenow The post type we are viewing.
286
 * @return array Empty array disables the dropdown.
287
 */
288
function give_remove_month_filter( $dates ) {
289
	global $typenow;
290
291
	if ( $typenow == 'give_forms' ) {
0 ignored issues
show
Found "== '". Use Yoda Condition checks, you must
Loading history...
292
		$dates = array();
293
	}
294
295
	return $dates;
296
}
297
298
add_filter( 'months_dropdown_results', 'give_remove_month_filter', 99 );
299
300
/**
301
 * Updates price when saving post
302
 *
303
 * @since 1.0
304
 *
305
 * @param int $post_id Download (Post) ID
306
 *
307
 * @return int|null
308
 */
309
function give_price_save_quick_edit( $post_id ) {
310
	if ( ! isset( $_POST['post_type'] ) || 'give_forms' !== $_POST['post_type'] ) {
0 ignored issues
show
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
Detected usage of a non-sanitized input variable: $_POST
Loading history...
311
		return;
312
	}
313
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
314
		return $post_id;
315
	}
316
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
317
		return $post_id;
318
	}
319
320
	if ( isset( $_REQUEST['_give_regprice'] ) ) {
0 ignored issues
show
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
321
		give_update_meta( $post_id, '_give_set_price', give_sanitize_amount_for_db( strip_tags( stripslashes( $_REQUEST['_give_regprice'] ) ) ) );
0 ignored issues
show
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
322
	}
323
}
324
325
add_action( 'save_post', 'give_price_save_quick_edit' );
326
327
/**
328
 * Process bulk edit actions via AJAX
329
 *
330
 * @since 1.0
331
 * @return void
332
 */
333
function give_save_bulk_edit() {
334
335
	$post_ids = ( isset( $_POST['post_ids'] ) && ! empty( $_POST['post_ids'] ) ) ? $_POST['post_ids'] : array();
0 ignored issues
show
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
Detected usage of a non-sanitized input variable: $_POST
Loading history...
336
337
	if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {
338
		$price = isset( $_POST['price'] ) ? strip_tags( stripslashes( $_POST['price'] ) ) : 0;
0 ignored issues
show
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
Detected usage of a non-sanitized input variable: $_POST
Loading history...
339
		foreach ( $post_ids as $post_id ) {
340
341
			if ( ! current_user_can( 'edit_post', $post_id ) ) {
342
				continue;
343
			}
344
345
			if ( ! empty( $price ) ) {
346
				give_update_meta( $post_id, '_give_set_price', give_sanitize_amount_for_db( $price ) );
347
			}
348
		}
349
	}
350
351
	die();
352
}
353
354
add_action( 'wp_ajax_give_save_bulk_edit', 'give_save_bulk_edit' );
355