Completed
Push — master ( 9d71d3...e65542 )
by Justin
06:52
created

display-coupons.php ➔ wpsc_display_coupons_page()   F

Complexity

Conditions 34
Paths 462

Size

Total Lines 138
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 34
eloc 82
c 3
b 1
f 0
nc 462
nop 0
dl 0
loc 138
rs 3.0932

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
function wpsc_display_coupons_page() {
4
	global $wpdb;
5
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
6
7
	/**
8
	 * Update / create code that will be abstracted to its own class at some point
9
	 */
10
11
	if ( isset( $_POST ) && is_array( $_POST ) && ! empty( $_POST ) ) {
12
13
		if ( isset( $_POST['add_coupon'] ) && (!isset( $_POST['is_edit_coupon'] ) || !($_POST['is_edit_coupon'] == 'true')) ) {
14
15
			check_admin_referer( 'wpsc_coupon', 'wpsc-coupon-add' );
16
17
			if ( ! function_exists( 'wpsc_is_store_admin' ) || ! wpsc_is_store_admin() ) {
18
				wp_die( __( 'Permission denied', 'wpsc' ) );
19
			}
20
21
			$coupon_code   = $_POST['add_coupon_code'];
22
			$discount      = (double)$_POST['add_discount'];
23
			$discount_type = (int)$_POST['add_discount_type'];
24
			$use_once      = (int)(bool)$_POST['add_use-once'];
25
			$every_product = (int)(bool)$_POST['add_every_product'];
26
			$is_active     = (int)(bool)$_POST['add_active'];
27
			$start_date    = ! empty( $_POST['add_start'] ) ? date( 'Y-m-d', strtotime( $_POST['add_start'] ) ) . " 00:00:00" : "0000-00-00 00:00:00";
28
			$end_date      = ! empty( $_POST['add_end'] ) ? date( 'Y-m-d', strtotime( $_POST['add_end'] ) ) . " 23:59:59" : "0000-00-00 00:00:00";
29
			$rules         = $_POST['rules'];
30
			$new_rules     = array();
31
32
			foreach ( $rules as $key => $rule ) {
33
				foreach ( $rule as $k => $r ) {
34
					$new_rules[$k][$key] = $r;
35
				}
36
			}
37
38
			foreach ( $new_rules as $key => $rule ) {
39
				if ( '' == $rule['value'] )
40
					unset( $new_rules[$key] );
41
			}
42
43
			$new_coupon = new WPSC_Coupon( array(
44
				'coupon_code'   => $coupon_code,
45
				'value'         => $discount,
46
				'is-percentage' => $discount_type,
47
				'use-once'      => $use_once,
48
				'is-used'       => 0,
49
				'active'        => $is_active,
50
				'every_product' => $every_product,
51
				'start'         => $start_date,
52
				'expiry'        => $end_date,
53
				'condition'     => $new_rules
54
			) );
55
			$insert = $new_coupon->save();
56
57
			if ( $insert ) {
58
				echo '<div class="updated"><p>' . __( 'The coupon has been added.', 'wp-e-commerce' ) . '</p></div>';
59
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
60
61
		}
62
63
		// update an existing coupon
64
		if ( isset( $_POST['is_edit_coupon'] ) && ($_POST['is_edit_coupon'] == 'true') && !(isset( $_POST['delete_condition'] )) && !(isset( $_POST['submit_condition'] )) ) {
65
66
			check_admin_referer( 'wpsc_coupon', 'wpsc-coupon-edit' );
67
68
			if ( ! function_exists( 'wpsc_is_store_admin' ) || ! wpsc_is_store_admin() ) {
69
				wp_die( __( 'Permission denied', 'wpsc' ) );
70
			}
71
72
			$rules     = isset( $_POST['rules'] ) ? $_POST['rules'] : array();
73
			$new_rules = array();
74
75
			foreach ( $rules as $key => $rule ) {
76
				foreach ( $rule as $k => $r ) {
77
					$new_rules[$k][$key] = $r;
78
				}
79
			}
80
81
			foreach ( $new_rules as $key => $rule ) {
82
				if ( '' == $rule['value'] )
83
					unset( $new_rules[$key] );
84
			}
85
86
			$update_coupon = new WPSC_Coupon( $_POST['coupon_id'] );
87
			$update_coupon->set( array(
88
				'coupon_code'   => $_POST['edit_coupon_code'],
89
				'value'         => $_POST['edit_coupon_amount'],
90
				'is-percentage' => $_POST['edit_discount_type'],
91
				'use-once'      => $_POST['edit_coupon_use_once'],
92
				'is-used'       => $_POST['edit_coupon_is_used'],
93
				'active'        => $_POST['edit_coupon_active'],
94
				'every_product' => $_POST['edit_coupon_every_product'],
95
				'start'         => ! empty( $_POST['edit_coupon_start'] ) ? get_gmt_from_date( $_POST['edit_coupon_start'] . ' 00:00:00' ) : "0000-00-00 00:00:00",
96
				'expiry'        => ! empty( $_POST['edit_coupon_end'] ) ? get_gmt_from_date( $_POST['edit_coupon_end'] . ' 23:59:59' ) : "0000-00-00 00:00:00",
97
				'condition'     => $new_rules
98
			) );
99
100
			$update = $update_coupon->save();
101
102
			if ( $update ) {
103
				echo '<div class="updated"><p>' . __( 'The coupon has been updated.', 'wp-e-commerce' ) . '</p></div>';
104
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
105
106
		}
107
	}
108
109
	/**
110
	 * Load the selected view
111
	 */
112
113
	if( isset( $_GET['wpsc-action'] ) && $_GET['wpsc-action'] == 'add_coupon' ) {
114
		// load the coupon add screen
115
		include( dirname( __FILE__ ) . '/display-coupon-add.php' );
116
117
	} elseif( isset( $_GET['wpsc-action'] ) && $_GET['wpsc-action'] == 'edit_coupon' ) {
118
		// load the coupon add screen
119
		include( dirname( __FILE__ ) . '/display-coupon-edit.php' );
120
121
	} else {
122
		require_once WPSC_FILE_PATH . '/wpsc-admin/includes/coupon-list-table-class.php';
123
		$coupons_table = new WPSC_Coupons_List_Table();
124
		$coupons_table->prepare_items(); ?>
125
		<div class="wrap">
126
			<h2><?php _e( 'Coupons', 'wp-e-commerce' ); ?><a href="<?php echo esc_url( add_query_arg( 'wpsc-action', 'add_coupon' ) ); ?>" class="add-new-h2"><?php _e( 'Add Coupon', 'wp-e-commerce' ); ?></a></h2>
127
			<?php do_action( 'wpsc_coupons_page_top' ); ?>
128
			<form id="wpsc-coupons-filter" method="get" action="<?php echo admin_url( 'edit.php?post_type=wpsc-product&page=wpsc-edit-coupons' ); ?>">
129
130
				<input type="hidden" name="post_type" value="wpsc-product" />
131
				<input type="hidden" name="page" value="wpsc-edit-coupons" />
132
133
				<?php $coupons_table->views() ?>
134
				<?php $coupons_table->display() ?>
135
			</form>
136
			<?php do_action( 'wpsc_coupons_page_bottom' ); ?>
137
		</div>
138
		<?php
139
	} // end view check
140
}
141