WC_Settings_Tax::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * WooCommerce Tax Settings
4
 *
5
 * @author      WooThemes
6
 * @category    Admin
7
 * @package     WooCommerce/Admin
8
 * @version     2.1.0
9
 */
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
if ( ! class_exists( 'WC_Settings_Tax' ) ) :
16
17
/**
18
 * WC_Settings_Tax.
19
 */
20
class WC_Settings_Tax extends WC_Settings_Page {
21
22
	/**
23
	 * Setting page id.
24
	 *
25
	 * @var string
26
	 */
27
	protected $id = 'tax';
28
29
	/**
30
	 * Constructor.
31
	 */
32
	public function __construct() {
33
		$this->label = __( 'Tax', 'woocommerce' );
34
		parent::__construct();
35
	}
36
37
	/**
38
	 * Add this page to settings.
39
	 */
40
	public function add_settings_page( $pages ) {
41
		if ( wc_tax_enabled() ) {
42
			return parent::add_settings_page( $pages );
43
		} else {
44
			return $pages;
45
		}
46
	}
47
48
	/**
49
	 * Get sections.
50
	 *
51
	 * @return array
52
	 */
53
	public function get_sections() {
54
		$sections = array(
55
			''         => __( 'Tax Options', 'woocommerce' ),
56
			'standard' => __( 'Standard Rates', 'woocommerce' )
57
		);
58
59
		// Get tax classes and display as links
60
		$tax_classes = WC_Tax::get_tax_classes();
61
62
		foreach ( $tax_classes as $class ) {
63
			$sections[ sanitize_title( $class ) ] = sprintf( __( '%s Rates', 'woocommerce' ), $class );
64
		}
65
66
		return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
67
	}
68
69
	/**
70
	 * Get settings array.
71
	 *
72
	 * @return array
73
	 */
74
	public function get_settings() {
75
		$tax_classes     = WC_Tax::get_tax_classes();
76
		$classes_options = array();
77
78
		foreach ( $tax_classes as $class ) {
79
			$classes_options[ sanitize_title( $class ) ] = esc_html( $class );
80
		}
81
82
		return apply_filters( 'woocommerce_get_settings_' . $this->id, include( 'views/settings-tax.php' ) );
83
	}
84
85
	/**
86
	 * Output the settings.
87
	 */
88
	public function output() {
89
		global $current_section;
90
91
		$tax_classes = WC_Tax::get_tax_classes();
92
93
		if ( $current_section == 'standard' || in_array( $current_section, array_map( 'sanitize_title', $tax_classes ) ) ) {
94
			$this->output_tax_rates();
95
		} else {
96
			$settings = $this->get_settings();
97
98
			WC_Admin_Settings::output_fields( $settings );
99
		}
100
	}
101
102
	/**
103
	 * Save settings.
104
	 */
105
	public function save() {
106
		global $current_section, $wpdb;
107
108
		if ( ! $current_section ) {
109
			$settings = $this->get_settings();
110
			WC_Admin_Settings::save_fields( $settings );
111
112
		} elseif ( ! empty( $_POST['tax_rate_country'] ) ) {
113
			$this->save_tax_rates();
114
		}
115
116
		WC_Cache_Helper::incr_cache_prefix( 'taxes' );
117
	}
118
119
	/**
120
	 * Output tax rate tables.
121
	 */
122
	public function output_tax_rates() {
123
		global $wpdb, $current_section;
124
125
		$current_class = $this->get_current_tax_class();
126
127
		$countries = array();
128 View Code Duplication
		foreach ( WC()->countries->get_allowed_countries() as $value => $label ) {
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...
129
			$countries[] = array(
130
				'value' => $value,
131
				'label' => esc_js( html_entity_decode( $label ) ),
132
			);
133
		}
134
135
		$states = array();
136 View Code Duplication
		foreach ( WC()->countries->get_allowed_country_states() as $label ) {
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...
137
			foreach ( $label as $code => $state ) {
138
				$states[] = array(
139
					'value' => $code,
140
					'label' => esc_js( html_entity_decode( $state ) ),
141
				);
142
			}
143
		}
144
145
		$base_url = admin_url( add_query_arg( array(
146
			'page'    => 'wc-settings',
147
			'tab'     => 'tax',
148
			'section' => $current_section,
149
		), 'admin.php' ) );
150
151
		// Localize and enqueue our js.
152
		wp_localize_script( 'wc-settings-tax', 'htmlSettingsTaxLocalizeScript', array(
153
			'current_class' => $current_class,
154
			'wc_tax_nonce'  => wp_create_nonce( 'wc_tax_nonce-class:' . $current_class ),
155
			'base_url'      => $base_url,
156
			'rates'         => array_values( WC_Tax::get_rates_for_tax_class( $current_class ) ),
157
			'page'          => ! empty( $_GET['p'] ) ? absint( $_GET['p'] ) : 1,
158
			'limit'         => 100,
159
			'countries'     => $countries,
160
			'states'        => $states,
161
			'default_rate'  => array(
162
				'tax_rate_id'       => 0,
163
				'tax_rate_country'  => '',
164
				'tax_rate_state'    => '',
165
				'tax_rate'          => '',
166
				'tax_rate_name'     => '',
167
				'tax_rate_priority' => 1,
168
				'tax_rate_compound' => 0,
169
				'tax_rate_shipping' => 1,
170
				'tax_rate_order'    => null,
171
				'tax_rate_class'    => $current_class,
172
			),
173
			'strings'       => array(
174
				'no_rows_selected' => __( 'No row(s) selected', 'woocommerce' ),
175
				'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),
176
				'csv_data_cols' => array(
177
					__( 'Country Code', 'woocommerce' ),
178
					__( 'State Code', 'woocommerce' ),
179
					__( 'ZIP/Postcode', 'woocommerce' ),
180
					__( 'City', 'woocommerce' ),
181
					__( 'Rate %', 'woocommerce' ),
182
					__( 'Tax Name', 'woocommerce' ),
183
					__( 'Priority', 'woocommerce' ),
184
					__( 'Compound', 'woocommerce' ),
185
					__( 'Shipping', 'woocommerce' ),
186
					__( 'Tax Class', 'woocommerce' ),
187
				),
188
			),
189
		) );
190
		wp_enqueue_script( 'wc-settings-tax' );
191
192
		include( 'views/html-settings-tax.php' );
193
	}
194
195
	/**
196
	 * Get tax class being edited.
197
	 * @return string
198
	 */
199
	private static function get_current_tax_class() {
200
		global $current_section;
201
202
		$tax_classes   = WC_Tax::get_tax_classes();
203
		$current_class = '';
204
205
		foreach( $tax_classes as $class ) {
206
			if ( sanitize_title( $class ) == $current_section ) {
207
				$current_class = $class;
208
			}
209
		}
210
211
		return $current_class;
212
	}
213
214
	/**
215
	 * Get a posted tax rate.
216
	 * @param  string $key   Key of tax rate in the post data array
217
	 * @param  int $order Position/order of rate
218
	 * @param  string $class Tax class for rate
219
	 * @return array
220
	 */
221
	private function get_posted_tax_rate( $key, $order, $class ) {
222
		$tax_rate     = array();
223
		$tax_rate_keys = array(
224
			'tax_rate_country',
225
			'tax_rate_state',
226
			'tax_rate',
227
			'tax_rate_name',
228
			'tax_rate_priority'
229
		);
230
231
		foreach ( $tax_rate_keys as $tax_rate_key ) {
232
			if ( isset( $_POST[ $tax_rate_key ] ) && isset( $_POST[ $tax_rate_key ][ $key ] ) ) {
233
				$tax_rate[ $tax_rate_key ] = wc_clean( $_POST[ $tax_rate_key ][ $key ] );
234
			}
235
		}
236
237
		$tax_rate['tax_rate_compound'] = isset( $_POST['tax_rate_compound'][ $key ] ) ? 1 : 0;
238
		$tax_rate['tax_rate_shipping'] = isset( $_POST['tax_rate_shipping'][ $key ] ) ? 1 : 0;
239
		$tax_rate['tax_rate_order']    = $order;
240
		$tax_rate['tax_rate_class']    = $class;
241
242
		return $tax_rate;
243
	}
244
245
	/**
246
	 * Save tax rates.
247
	 */
248
	public function save_tax_rates() {
249
		global $wpdb;
250
251
		$current_class = sanitize_title( $this->get_current_tax_class() );
252
253
		// get the tax rate id of the first submited row
254
		$first_tax_rate_id = key( $_POST['tax_rate_country'] );
255
256
		// get the order position of the first tax rate id
257
		$tax_rate_order = absint( $wpdb->get_var( $wpdb->prepare( "SELECT tax_rate_order FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %s", $first_tax_rate_id ) ) );
258
259
		$index = isset( $tax_rate_order ) ? $tax_rate_order : 0;
260
261
		// Loop posted fields
262
		foreach ( $_POST['tax_rate_country'] as $key => $value ) {
263
			$mode        = 0 === strpos( $key, 'new-' ) ? 'insert' : 'update';
264
			$tax_rate    = $this->get_posted_tax_rate( $key, $index ++, $current_class );
265
266
			if ( 'insert' === $mode ) {
267
				$tax_rate_id = WC_Tax::_insert_tax_rate( $tax_rate );
268
			} elseif ( 1 == $_POST['remove_tax_rate'][ $key ] ) {
269
				$tax_rate_id = absint( $key );
270
				WC_Tax::_delete_tax_rate( $tax_rate_id );
271
				continue;
272
			} else {
273
				$tax_rate_id = absint( $key );
274
				WC_Tax::_update_tax_rate( $tax_rate_id, $tax_rate );
275
			}
276
277
			if ( isset( $_POST['tax_rate_postcode'][ $key ] ) ) {
278
				WC_Tax::_update_tax_rate_postcodes( $tax_rate_id, wc_clean( $_POST['tax_rate_postcode'][ $key ] ) );
279
			}
280
			if ( isset( $_POST['tax_rate_city'][ $key ] ) ) {
281
				WC_Tax::_update_tax_rate_cities( $tax_rate_id, wc_clean( $_POST['tax_rate_city'][ $key ] ) );
282
			}
283
		}
284
	}
285
}
286
287
endif;
288
289
return new WC_Settings_Tax();
290