jigoshop /admin/jigoshop-admin-settings-api.php

Language PHP Lines 1313
MD5 Hash 8db6c22dc4385fa2d5e5ad36038835ae
Repository https://github.com/scottpoulin/jigoshop.git View Raw File
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
<?php
/**
 * Jigoshop_Admin_Settings class for management and display of all Jigoshop option settings
 *
 * DISCLAIMER
 *
 * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
 * versions in the future. If you wish to customise Jigoshop core for your needs,
 * please use our GitHub repository to publish essential changes for consideration.
 *
 * @package     Jigoshop
 * @category    Admin
 * @author      Jigoshop
 * @copyright   Copyright Š 2011-2013 Jigoshop.
 * @license     http://jigoshop.com/license/commercial-edition
 */

class Jigoshop_Admin_Settings extends Jigoshop_Singleton {

	private $our_parser;

	/**
	 * Constructor
	 *
	 * @since 1.3
	 */
	protected function __construct() {
		
		$this->our_parser = new Jigoshop_Options_Parser(
			self::get_options()->get_default_options(),
			JIGOSHOP_OPTIONS
		);

		add_action( 'current_screen', array( $this, 'register_settings' ) );

	}

	/**
	 * Scripts for the Options page
	 *
	 * @since 1.3
	 */
	public function settings_scripts() {

    	// http://jquerytools.org/documentation/rangeinput/index.html
    	wp_register_script( 'jquery-tools', jigoshop::assets_url() . '/assets/js/jquery.tools.min.js', array( 'jquery' ), '1.2.7' );
    	wp_enqueue_script( 'jquery-tools' );

    	wp_register_script( 'jigoshop-bootstrap-tooltip', jigoshop::assets_url() . '/assets/js/bootstrap-tooltip.min.js', array( 'jquery' ), '2.0.3' );
    	wp_enqueue_script( 'jigoshop-bootstrap-tooltip' );

    	wp_register_script( 'jigoshop-select2', jigoshop::assets_url() . '/assets/js/select2.min.js', array( 'jquery' ), '3.1' );
    	wp_enqueue_script( 'jigoshop-select2' );

	}


	/**
	 * Styling for the options page
	 *
	 * @since 1.3
	 */
	public function settings_styles() {

		wp_register_style( 'jigoshop-select2', jigoshop::assets_url() . '/assets/css/select2.css', '', '3.1', 'screen' );
		wp_enqueue_style( 'jigoshop-select2' );

		do_action( 'jigoshop_settings_styles' );	// user defined stylesheets should be registered and queued

	}


	/**
	 * Register settings
	 *
	 * @since 1.3
	 */
	public function register_settings() {
		
		// Weed out all admin pages except the Jigoshop Settings page hits
		global $pagenow;
		if ( $pagenow <> 'admin.php' && $pagenow <> 'options.php' ) return;
		$screen = get_current_screen();
		if ( $screen->base <> 'jigoshop_page_jigoshop_settings' && $screen->base <> 'options' ) return;
		
		$slug = $this->get_current_tab_slug();
		$options = isset( $this->our_parser->tabs[$slug] ) ? $this->our_parser->tabs[$slug] : '';

		if ( ! is_array( $options ) ) {
			jigoshop_log( "Jigoshop Settings API: -NO- valid options for 'register_settings()' - EXITING with:" );
			jigoshop_log( $slug );
			return;
		}

		register_setting( JIGOSHOP_OPTIONS, JIGOSHOP_OPTIONS, array( $this, 'validate_settings' ) );

		if ( is_array( $options )) foreach ( $options as $index => $option ) {
			switch ( $option['type'] ) {
			case 'title':
				add_settings_section( $option['section'], $option['name'], array( $this, 'display_section' ), JIGOSHOP_OPTIONS );
				break;

			default:
				$this->create_setting( $index, $option );
				break;
			}
		}

	}


	/**
	 * Create a settings field
	 *
	 * @since 1.3
	 */
	public function create_setting( $index, $option = array() ) {

		$defaults = array(
			'tab'			=> '',
			'section'		=> '',
			'id'			=> null,
			'type'			=> '',
			'name'			=> '',
			'desc'			=> '',
			'tip'			=> '',
			'std'			=> '',
			'choices'		=> array(),
			'class'			=> '',
			'display'		=> null,
			'update'		=> null,
			'extra'			=> null
		);

		extract( wp_parse_args( $option, $defaults ) );
		$id = ! empty( $id ) ? $id : $section.$index;

		$field_args = array(
			'tab'			=> $tab,
			'section'		=> $section,
			'id'			=> $id,
			'type'			=> $type,
			'name'			=> $name,
			'desc'			=> $desc,
			'tip'			=> $tip,
			"std"			=> $std,
			'choices'		=> $choices,
			'label_for'		=> $id,
			'class'			=> $class,
			'display'		=> $display,
			'update'		=> $update,
			'extra'			=> $extra
		);

		if ( $type <> 'tab' ) {
			add_settings_field(
				$id,
				($type == 'checkbox') ? '' : esc_attr( $name ),
				array( $this, 'display_option' ),
				JIGOSHOP_OPTIONS,
				$section,
				$field_args
			);
		}
	}


	/**
	 * Format markup for an option and output it
	 *
	 * @since 1.3
	 */
	public function display_option( $option ) {
		echo $this->our_parser->format_option_for_display( $option );
	}


	/**
	 * Description for section
	 *
	 * @since 1.3
	 */
	public function display_section( $section ) {

		$options = $this->our_parser->these_options;
		foreach ( $options as $index => $option ) {
			if ( isset( $option['name'] ) && $section['title'] == $option['name'] ) {
				if ( ! empty( $option['desc'] )) {
					echo '<p class="section_description">' . $option['desc'] . '</p>';
				}
			}
		}

	}


	/**
	 * Render the Options page
	 *
	 * @since 1.3
	 */
	public function output_markup() {
		?>
			<div class="wrap jigoshop">

				<div class="icon32 icon32-jigoshop-settings" id="icon-jigoshop"><br></div>
				<?php do_action( 'jigoshop_admin_settings_notices' ); ?>
				<h2 class="nav-tab-wrapper jigoshop-nav-tab-wrapper">
					<?php echo $this->build_tab_menu_items(); ?>
				</h2>

				<noscript>
					<div id="jigoshop-js-warning" class="error"><?php _e( 'Warning- This options panel may not work properly without javascript!', 'jigoshop' ); ?></div>
				</noscript>

				<?php settings_errors(); ?>

				<form action="options.php" id="mainform" method="post">

					<div class="jigoshop-settings">
						<div id="tabs-wrap">

							<?php settings_fields( JIGOSHOP_OPTIONS ); ?>
							<?php do_settings_sections( JIGOSHOP_OPTIONS ); ?>

							<p class="submit">
								<input name="Submit" type="submit" class="button-primary" value="<?php echo sprintf( __( "Save %s Changes", 'jigoshop' ), $this->get_current_tab_name() ); ?>" />
							</p>

						</div>
					</div>

				</form>

			</div>

			<script type="text/javascript">
				/*<![CDATA[*/
				jQuery(function($) {

					// Fade out the status message
					jQuery('.updated').delay(2500).fadeOut(1500);

					// jQuery Tools range tool
					jQuery(":range").rangeinput();

					// Countries
					jQuery('select#jigoshop_allowed_countries').change(function(){
						// hide-show multi_select_countries
						if (jQuery(this).val()=="specific") {
							jQuery(this).parent().parent().next('tr').show();
						} else {
							jQuery(this).parent().parent().next('tr').hide();
						}
					}).change();

					// permalink double save hack (do we need this anymore -JAP-)
					jQuery.get('<?php echo admin_url('options-permalink.php') ?>');

				});
				/*]]>*/
			</script>

			<?php do_action( 'jigoshop_settings_scripts' ); ?>

		<?php
	}


	/**
	 * Create the Navigation Menu Tabs
	 *
	 * @since 1.3
	 */
	function build_tab_menu_items() {
		$menus_li = '';
		$slug = $this->get_current_tab_slug();
		foreach ( $this->our_parser->tab_headers as $tab ) {
			$this_slug = sanitize_title( $tab );
			if ( $slug == $this_slug ) {
				$menus_li .= '<a class="nav-tab nav-tab-active"
					title="'.$tab.'"
					href="?page=jigoshop_settings&tab='.$this_slug.'">' . $tab . '</a>';
			} else {
				$menus_li .= '<a class="nav-tab"
					title="'.$tab.'"
					href="?page=jigoshop_settings&tab='.$this_slug.'">' . $tab . '</a>';
			}
		}
		return $menus_li;
	}


	/**
	 * Return the current Tab slug in view
	 *
	 * @since 1.3
	 */
	public function get_current_tab_slug() {
		$current = "";

		if ( isset( $_GET['tab'] ) ) {
			$current = $_GET['tab'];
		} else if ( isset( $_POST['_wp_http_referer'] ) && strpos($_POST['_wp_http_referer'], '&tab=') !== false ) {
			// /site/wp-admin/admin.php?page=jigoshop_settings&tab=products-inventory&settings-updated=true
			// find the 'tab'
			$result = strstr( $_POST['_wp_http_referer'], '&tab=' );
			// &tab=products-inventory&settings-updated=true
			$result = substr( $result, 5 );
			// products-inventory&settings-updated=true
			$end_pos = strpos( $result, '&' );
			$current = substr( $result, 0 , $end_pos !== false ? $end_pos : strlen( $result ) );
			// products-inventory
		} else {
			$current = $this->our_parser->these_options[0]['name'];
		}
		return sanitize_title( $current );
	}


	/**
	 * Return the current Tab full name in view
	 *
	 * @since 1.3
	 */
	public function get_current_tab_name() {

		$current = $this->our_parser->these_options[0]['name'];

		$slug = $this->get_current_tab_slug();
		foreach ( $this->our_parser->tab_headers as $tab ) {
			$this_slug = sanitize_title( $tab );
			if ( $slug == $this_slug ) {
				$current = $tab;
			}
		}
		return $current;
	}


	/**
	 * Validate settings
	 *
	 * @since 1.3
	 */
	public function validate_settings( $input ) {

		if ( empty( $_POST ) ) {
			return $input;
		}

		$defaults = $this->our_parser->these_options;
		$current_options = self::get_options()->get_current_options();

		$valid_input = $current_options;			// we start with the current options

		// Find the current TAB we are working with and use it's option settings
		$this_section = $this->get_current_tab_name();
		$tab = $this->our_parser->tabs[sanitize_title( $this_section )];

		// with each option, get it's type and validate it
		if ( ! empty( $tab )) foreach ( $tab as $index => $setting ) {
			if ( isset( $setting['id'] ) ) {
			
				// special case tax classes should be updated, they will do nothing if this is not the right TAB
				if ( $setting['id'] == 'jigoshop_tax_rates' ) {
					$valid_input['jigoshop_tax_rates'] = $this->get_updated_tax_classes();
					update_option( $setting['id'], $valid_input['jigoshop_tax_rates'] ); // TODO: remove in v1.5 - provides compatibility
					continue;
				}
				
				// get this settings options
				foreach ( $defaults as $default_index => $option ) {
					if ( in_array( $setting['id'], $option ) ) {
						break;
					}
				}
				$value = isset( $input[$setting['id']] ) ? $input[$setting['id']] : null ;

				// we have a $setting
				// $value has the WordPress user submitted value for this $setting
				// $option has this $setting parameters
				// validate for $option 'type' checking for a submitted $value
				switch ( $option['type'] ) {
				case 'user_defined' :
					if ( isset( $option['update'] ) ) {
						if ( is_callable( $option['update'], true ) ) {
							$result = call_user_func( $option['update'] );
							$valid_input[$setting['id']] = $result;
							update_option( $setting['id'], $valid_input[$setting['id']] ); // TODO: remove in v1.5 - provides compatibility
						}
					}
					break;

				case 'multi_select_countries' :
					if ( isset( $value ) ) {
						$countries = jigoshop_countries::$countries;
						asort( $countries );
						$selected = array();
						foreach ( $countries as $key => $val ) {
							if ( in_array( $key, (array)$value ) ) {
								$selected[] = $key;
							}
						}
						$valid_input[$setting['id']] = $selected;
						update_option( $setting['id'], $valid_input[$setting['id']] ); // TODO: remove in v1.5 - provides compatibility
					}
					break;

				case 'checkbox' :
					// there will be no $value for a false checkbox, set it now
					$valid_input[$setting['id']] = isset( $value ) ? 'yes' : 'no';
					update_option( $setting['id'], $valid_input[$setting['id']] ); // TODO: remove in v1.5 - provides compatibility
					break;

				case 'multicheck' :
					$selected = array();
					foreach ( $option['choices'] as $key => $val ) {
						if ( isset( $value[$key] ) ) {
							$selected[$key] = true;
						} else {
							$selected[$key] = false;
						}
					}
					$valid_input[$setting['id']] = $selected;
					update_option( $setting['id'], $valid_input[$setting['id']] ); // TODO: remove in v1.5 - provides compatibility
					break;

				case 'text' :
				case 'longtext' :
				case 'textarea' :
					$valid_input[$setting['id']] = esc_attr( jigowatt_clean( $value ) );
					update_option( $setting['id'], $valid_input[$setting['id']] ); // TODO: remove in v1.5 - provides compatibility
					break;

				case 'email' :
					$email = sanitize_email( $value );
					if ( $email <> $value ) {
						add_settings_error(
							$setting['id'],
							'jigoshop_email_error',
							sprintf(__('You entered "%s" as the value for "%s" and it was not a valid email address.  It was not saved and the original is still in use.','jigoshop'), $value, $setting['name']),
							'error'
						);
						$valid_input[$setting['id']] = $current_options[$setting['id']];
					} else {
						$valid_input[$setting['id']] = esc_attr( jigowatt_clean( $email ) );
					}
					update_option( $setting['id'], $valid_input[$setting['id']] ); // TODO: remove in v1.5 - provides compatibility
					break;

				case 'decimal' :
					$cleaned = jigowatt_clean( $value );
					if ( ! jigoshop_validation::is_decimal( $cleaned ) && $cleaned <> '' ) {
						add_settings_error(
							$setting['id'],
							'jigoshop_decimal_error',
							sprintf(__('You entered "%s" as the value for "%s" in "%s" and it was not a valid decimal number (may have leading negative sign, with optional decimal point, numbers 0-9).  It was not saved and the original is still in use.','jigoshop'), $value, $setting['name'], $setting['section'] ),
							'error'
						);
						$valid_input[$setting['id']] = $current_options[$setting['id']];
					} else {
						$valid_input[$setting['id']] = $cleaned;
					}
					update_option( $setting['id'], $valid_input[$setting['id']] ); // TODO: remove in v1.5 - provides compatibility
					break;

				case 'integer' :
					$cleaned = jigowatt_clean( $value );
					if ( ! jigoshop_validation::is_integer( $cleaned ) && $cleaned <> '' ) {
						add_settings_error(
							$setting['id'],
							'jigoshop_integer_error',
							sprintf(__('You entered "%s" as the value for "%s" in "%s" and it was not a valid integer number (may have leading negative sign, numbers 0-9).  It was not saved and the original is still in use.','jigoshop'), $value, $setting['name'], $setting['section'] ),
							'error'
						);
						$valid_input[$setting['id']] = $current_options[$setting['id']];
					} else {
						$valid_input[$setting['id']] = $cleaned;
					}
					update_option( $setting['id'], $valid_input[$setting['id']] ); // TODO: remove in v1.5 - provides compatibility
					break;

				case 'natural' :
					$cleaned = jigowatt_clean( $value );
					if ( ! jigoshop_validation::is_natural( $cleaned ) && $cleaned <> '' ) {
						add_settings_error(
							$setting['id'],
							'jigoshop_natural_error',
							sprintf(__('You entered "%s" as the value for "%s" in "%s" and it was not a valid natural number (numbers 0-9).  It was not saved and the original is still in use.','jigoshop'), $value, $setting['name'], $setting['section'] ),
							'error'
						);
						$valid_input[$setting['id']] = $current_options[$setting['id']];
					} else {
						$valid_input[$setting['id']] = $cleaned;
					}
					update_option( $setting['id'], $valid_input[$setting['id']] ); // TODO: remove in v1.5 - provides compatibility
					break;

				default :
					if ( isset( $value ) ) {
						$valid_input[$setting['id']] = $value;
						update_option( $setting['id'], $valid_input[$setting['id']] ); // TODO: remove in v1.5 - provides compatibility
					}
					break;
				}
			}
		}


        // remove all jigoshop_update_options actions on shipping classes when not on the shipping tab
        if ( $this_section != __('Shipping','jigoshop') ) {
            $this->remove_update_options( jigoshop_shipping::get_all_methods() );
        }

        if ( $this_section != __('Payment Gateways','jigoshop') ) {
            $this->remove_update_options( jigoshop_payment_gateways::payment_gateways() );
        }

		// Allow any hooked in option updating
		do_action( 'jigoshop_update_options' );

		$errors = get_settings_errors();
		if ( empty( $errors ) ) {
			add_settings_error(
				'',
				'settings_updated',
				sprintf(__('"%s" settings were updated successfully.','jigoshop'), $this_section ),
				'updated'
			);
		}

		return $valid_input;	// send it back to WordPress for saving

	}

	/**
	 * Remove all jigoshop_update_options actions on shipping and payment classes when not on those tabs
	 *
	 * @since 	1.3
	 */
	private function remove_update_options( $classes ) {

        if ( empty( $classes )) return;

        foreach ( $classes as $class ) :
            remove_action( 'jigoshop_update_options', array( $class, 'process_admin_options' ));
        endforeach;

    }

	/**
	 * Defines a custom sort for the tax_rates array. The sort that is needed is that the array is sorted
	 * by country, followed by state, followed by compound tax. The difference is that compound must be sorted based
	 * on compound = no before compound = yes. Ultimately, the purpose of the sort is to make sure that country, state
	 * are all consecutive in the array, and that within those groups, compound = 'yes' always appears last. This is
	 * so that tax classes that are compounded will be executed last in comparison to those that aren't.
	 * last.
	 * <br>
	 * <pre>
	 * eg. country = 'CA', state = 'QC', compound = 'yes'<br>
	 *     country = 'CA', state = 'QC', compound = 'no'<br>
	 *
	 * will be sorted to have <br>
	 *     country = 'CA', state = 'QC', compound = 'no'<br>
	 *     country = 'CA', state = 'QC', compound = 'yes' <br>
	 * </pre>
	 *
	 * @param type $a the first object to compare with (our inner array)
	 * @param type $b the second object to compare with (our inner array)
	 * @return int the results of strcmp
	 */
	function csort_tax_rates($a, $b) {
		$str1 = '';
		$str2 = '';

		$str1 .= $a['country'] . $a['state'] . ($a['compound'] == 'no' ? 'a' : 'b');
		$str2 .= $b['country'] . $b['state'] . ($b['compound'] == 'no' ? 'a' : 'b');

		return strcmp($str1, $str2);
	}


	/**
	 * When Options are saved, return the 'jigoshop_tax_rates' option values
	 *
	 * @return	mixed	false if not rax rates, array of tax rates otherwise
	 *
	 * @since 	1.3
	 */
	function get_updated_tax_classes() {

		$taxFields = array(
			'tax_classes' => '',
			'tax_country' => '',
			'tax_rate'    => '',
			'tax_label'   => '',
			'tax_shipping'=> '',
			'tax_compound'=> ''
		);

		$tax_rates = array();

		/* Save each array key to a variable */
		foreach ( $taxFields as $name => $val )
			if ( isset( $_POST[$name] )) $taxFields[$name] = $_POST[$name];

		extract( $taxFields );

		for ( $i = 0; $i < sizeof( $tax_classes); $i++ ) :

			if ( empty( $tax_rate[$i] )) continue;

			$countries = $tax_country[$i];
			$label     = trim($tax_label[$i]);
			$rate      = number_format((float)jigowatt_clean($tax_rate[$i]), 4);
			$class     = jigowatt_clean($tax_classes[$i]);

			/* Checkboxes */
			$shipping = !empty($tax_shipping[$i]) ? 'yes' : 'no';
			$compound = !empty($tax_compound[$i]) ? 'yes' : 'no';

			/* Save the state & country separately from options eg US:OH */
			$states  = array();
			foreach ( $countries as $k => $countryCode ) :
				if ( strstr($countryCode, ':')) :
					$cr = explode(':', $countryCode);
					$states[$cr[1]]  = $cr[0];
					unset($countries[$k]);
				endif;
			endforeach;

			/* Save individual state taxes, eg OH => US (State => Country) */
			foreach ( $states as $state => $country ) :
				$tax_rates[] = array(
					'country'      => $country,
					'label'        => $label,
					'state'        => $state,
					'rate'         => $rate,
					'shipping'     => $shipping,
					'class'        => $class,
					'compound'     => $compound,
					'is_all_states'=> false //determines if admin panel should show 'all_states'
				);
			endforeach;

			foreach ( $countries as $country ) :

				/* Countries with states */
				if ( jigoshop_countries::country_has_states( $country )) {

					foreach ( array_keys( jigoshop_countries::$states[$country] ) as $state ) :
						$tax_rates[] = array(
							'country'      => $country,
							'label'        => $label,
							'state'        => $state,
							'rate'         => $rate,
							'shipping'     => $shipping,
							'class'        => $class,
							'compound'     => $compound,
							'is_all_states'=> true
						);
					endforeach;

				} else {  /* This country has no states, eg AF */

					 $tax_rates[] = array(
						'country'      => $country,
						'label'        => $label,
						'state'        => '*',
						'rate'         => $rate,
						'shipping'     => $shipping,
						'class'        => $class,
						'compound'     => $compound,
						'is_all_states'=> false
					);

				}

			endforeach;

		endfor;

		usort( $tax_rates, array( $this, 'csort_tax_rates' ) );

		return $tax_rates;

	}

}


/**
 * Options Parser Class
 *
 * Used by the Jigoshop_Admin_Settings class to parse the Jigoshop_Options into sections
 * Provides formatted output for display of all Option types
 *
 * @since 	1.3
 */
class Jigoshop_Options_Parser {

	var $these_options;		// The array of default options items to parse
	var $tab_headers;
	var $tabs;
	var $sections;


	function __construct( $option_items, $this_options_entry ) {
		$this->these_options = $option_items;
		$this->parse_options();
	}


	private function parse_options() {

		$tab_headers = array();
		$tabs = array();
		$sections = array();

		foreach ( $this->these_options as $item ) {

			$defaults = array(
				'tab'			=> '',
				'section'		=> '',
				'id'			=> null,
				'type'			=> '',
				'name'			=> '',
				'desc'			=> '',
				'tip'			=> '',
				'std'			=> '',
				'choices'		=> array(),
				'class'			=> '',
				'display'		=> null,
				'update'		=> null,
				'extra'			=> null
			);

			$item = wp_parse_args( $item, $defaults );

			if ( isset( $item['id'] ) ) $item['id'] = sanitize_title( $item['id'] );

			if ( $item['type'] == 'tab' ) {
				$tab_name = sanitize_title( $item['name'] );
				$tab_headers[$tab_name] = $item['name'];    // used by get_current_tab_name()
				continue;
			}

			if ( $item['type'] == 'title' ) {
				$section_name = sanitize_title( $item['name'] );
			}

			$item['tab'] = $tab_name;
			$item['section'] = isset( $section_name ) ? $section_name : $tab_name;
			$tabs[$tab_name][] = $item;
			$sections[$item['section']][] = $item;

		}

		$this->tab_headers = $tab_headers;
		$this->tabs = $tabs;
		$this->sections = $sections;
	}


	public function format_option_for_display( $item ) {

		$data = Jigoshop_Base::get_options()->get_current_options();

		if ( ! isset( $item['id'] )) return '';         // ensure we have an id to work with
		
		$display = "";					// each item builds it's output into this and it's returned for echoing
		$class = "";
		
		if ( isset( $item['class'] ) ) {
			$class = $item['class'];
		}

		// display a tooltip if there is one in it's own table data element before the item to display
		$display .= '<td class="jigoshop-tooltips">';
        if ( ! empty( $item['tip'] )) {
			$display .= '<a href="#" tip="'.esc_attr( $item['tip'] ).'" class="tips" tabindex="99"></a>';
		}
		$display .= '</td>';

		$display .= '<td class="forminp">';

		// work off the option type and format output for display for each type
		switch ( $item['type'] ) {
		case 'user_defined':
			if ( isset( $item['display'] ) ) {
				if ( is_callable( $item['display'], true ) ) {
					$display .= call_user_func( $item['display'] );
				}
			}
			break;

		case 'gateway_options':
			foreach ( jigoshop_payment_gateways::payment_gateways() as $gateway ) :
				$gateway->admin_options();
			endforeach;
			break;

		case 'shipping_options':
			foreach ( jigoshop_shipping::get_all_methods() as $shipping_method ) :
				$shipping_method->admin_options();
			endforeach;
			break;

		case 'tax_rates':
			$display .= $this->format_tax_rates_for_display( $item );
			break;

/*		case 'image_size' :			// may not use this, needs work, unhooking (-JAP-)
			$width = $data[$item['id']];
			$display .= '<input
				name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
				id="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
				class="jigoshop-input jigoshop-text"
				type="text"
				value="'.esc_attr( $data[$item['id']] ).'" />';
			break;
*/
		case 'single_select_page':
			$page_setting = (int) $data[$item['id']];
			$args = array(
				'name' => JIGOSHOP_OPTIONS . '[' . $item['id'] . ']',
				'id' => $item['id'],
				'sort_order' => 'ASC',
				'echo' => 0,
				'selected' => $page_setting
			);
			if ( isset( $item['extra'] )) $args = wp_parse_args( $item['extra'], $args );
			$display .= wp_dropdown_pages( $args );
			$parts = explode( '<select', $display );
			$id = $item['id'];
			$display = $parts[0] . '<select id="'.$id.'" class="'.$class.'"' . $parts[1];
			?>
				<script type="text/javascript">
					jQuery(function() {
						jQuery("#<?php echo $id; ?>").select2({ width: '250px' });
					});
				</script>
			<?php
			break;

		case 'single_select_country':
			$countries = jigoshop_countries::$countries;
			$country_setting = (string) $data[$item['id']];
			if ( strstr( $country_setting, ':' )) :
				$country = current( explode( ':', $country_setting) );
				$state = end( explode( ':', $country_setting) );
			else :
				$country = $country_setting;
				$state = '*';
			endif;
			$id = $item['id'];
			$display .= '<select id="'.$id.'" class="single_select_country '.$class.'" name="' . JIGOSHOP_OPTIONS . '[' . $item['id'] . ']">';
			$display .= jigoshop_countries::country_dropdown_options($country, $state, true, false, false);
			$display .= '</select>';
			?>
				<script type="text/javascript">
					jQuery(function() {
						jQuery("#<?php echo $id; ?>").select2({ width: '500px' });
					});
				</script>
			<?php
			break;

		case 'multi_select_countries':
			$countries = jigoshop_countries::$countries;
			asort( $countries );
			$selections = (array) $data[$item['id']];

			$display .= '<select multiple="multiple"
				id="'.$item['id'].'"
				class="jigoshop-input jigoshop-select '.$class.'"
				name="'.JIGOSHOP_OPTIONS.'['.$item['id'].'][]" >';
			foreach ( $countries as $key => $val ) {
				$display .= '<option value="'.esc_attr( $key ).'" '.(in_array( $key, $selections ) ? 'selected="selected"' : '').' />'.$val.'</option>';
			}
			$display .= '</select>';
			$id = $item['id'];
			?>
				<script type="text/javascript">
					jQuery(function() {
						jQuery("#<?php echo $id; ?>").select2({ width: '500px' });
					});
				</script>
			<?php
			break;

		case 'button':
			if ( isset( $item['extra'] ) )
				$display .= '<a  id="'.$item['id'].'"
					class="button '.$class.'"
					href="'. esc_attr( $item['extra'] ) .'"
					>'. esc_attr( $item['desc'] ) .'</a>';
			$item['desc'] = '';     // temporarily remove it so it doesn't display twice
			break;

		case 'decimal':				// decimal numbers are positive or negative 0-9 inclusive, may include decimal
			$display .= '<input
				id="'.$item['id'].'"
				class="jigoshop-input jigoshop-text '.$class.'"
				name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
				type="number"
				step="any"
				size="20"
				value="'. esc_attr( $data[$item['id']] ).'" />';
			break;

		case 'integer':				// integer numbers are positive or negative 0-9 inclusive
		case 'natural':				// natural numbers are positive 0-9 inclusive
			$display .= '<input
				id="'.$item['id'].'"
				class="jigoshop-input jigoshop-text '.$class.'"
				name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
				type="number"
				size="20"
				value="'. esc_attr( $data[$item['id']] ).'" />';
			break;

		case 'text':				// any character sequence
			$display .= '<input
				id="'.$item['id'].'"
				class="jigoshop-input jigoshop-text '.$class.'"
				name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
				type="text"
				size="20"
				value="'. esc_attr( $data[$item['id']] ).'" />';
			break;

		case 'midtext':
			$display .= '<input
				id="'.$item['id'].'"
				class="jigoshop-input jigoshop-text '.$class.'"
				name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
				type="text"
				size="40"
				value="'. esc_attr( $data[$item['id']] ).'" />';
			break;

		case 'longtext':
			$display .= '<input
				id="'.$item['id'].'"
				class="jigoshop-input jigoshop-text '.$class.'"
				name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
				type="text"
				size="80"
				value="'. esc_attr( $data[$item['id']] ).'" />';
			break;

		case 'email':
			$display .= '<input
				id="'.$item['id'].'"
				class="jigoshop-input jigoshop-text jigoshop-email '.$class.'"
				name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
				type="text"
				size="40"
				value="'. esc_attr( $data[$item['id']] ).'" />';
			break;

		case 'textarea':
			$cols = '60';
			$ta_value = '';
			if ( isset( $item['choices'] ) ) {
				$ta_options = $item['choices'];
				if ( isset( $ta_options['cols'] ) ) {
					$cols = $ta_options['cols'];
				}
			}
			$ta_value = stripslashes( $data[$item['id']] );
			$display .= '<textarea
				id="'.$item['id'].'"
				class="jigoshop-input jigoshop-textarea '.$class.'"
				name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
				cols="'.$cols.'"
				rows="4">'.esc_textarea( $ta_value ).'</textarea>';
			break;

		case "radio":
			// default to horizontal display of choices ( 'horizontal' may or may not be defined )
			if ( ! isset( $item['extra'] ) || ! in_array( 'vertical', $item['extra'] ) ) {

				$display .= '<div class="jigoshop-radio-horz">';
				foreach ( $item['choices'] as $option => $name ) {
					$display .= '<input
						class="jigoshop-input jigoshop-radio '.$class.'"
						name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
						id="' . $item['id'] . '[' . $option . ']"
						type="radio"
						value="'.$option.'" '.checked( $data[$item['id']], $option, false ).' /><label for="' . $item['id'] . '[' . $option . ']">'.$name.'</label>';
				}
				$display .= '</div>';

			} else if ( isset( $item['extra'] ) && in_array( 'vertical', $item['extra'] ) ) {

				$display .= '<ul class="jigoshop-radio-vert">';
				foreach ( $item['choices'] as $option => $name ) {
					$display .= '<li><input
						class="jigoshop-input jigoshop-radio '.$class.'"
						name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
						id="' . $item['id'] . '[' . $option . ']"
						type="radio"
						value="'.$option.'" '.checked( $data[$item['id']], $option, false ).' /><label for="' . $item['id'] . '[' . $option . ']">'.$name.'</label></li>';
				}
				$display .= '</ul>';

			}
			break;

		case 'checkbox':
			$display .= '<span class="jigoshop-container"><input
				id="'.$item['id'].'"
				type="checkbox"
				class="jigoshop-input jigoshop-checkbox '.$class.'"
				name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
				'.checked($data[$item['id']], 'yes', false).' />
				<label for="'.$item['id'].'">'.$item['name'].'</label></span>';
			break;

		case 'multicheck':
			$multi_stored = $data[$item['id']];

			// default to horizontal display of choices ( 'horizontal' may or may not be defined )
			if ( ! isset( $item['extra'] ) || ! in_array( 'vertical', $item['extra'] ) ) {

				$display .= '<div class="jigoshop-multi-checkbox-horz '.$class.'">';
				foreach ( $item['choices'] as $key => $option ) {
					$display .= '<input
						id="'.$item['id'].'_'.$key.'"
						class="jigoshop-input"
						name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']['.$key.']"
						type="checkbox"
						'.checked( $multi_stored[$key], true, false ).' />
						<label for="'.$item['id'].'_'.$key.'">'.$option.'</label>';
				}
				$display .= '</div>';

			} else if ( isset( $item['extra'] ) && in_array( 'vertical', $item['extra'] ) ) {

				$display .= '<ul class="jigoshop-multi-checkbox-vert '.$class.'">';
				foreach ( $item['choices'] as $key => $option ) {
					$display .= '<li><input
						id="'.$item['id'].'_'.$key.'"
						class="jigoshop-input"
						name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']['.$key.']"
						type="checkbox"
						'.checked( $multi_stored[$key], true, false ).' />
						<label for="'.$item['id'].'_'.$key.'">'.$option.'</label></li>';
				}
				$display .= '</ul>';
			}
			break;

		case 'range':
			$display .= '<input
				id="'.$item['id'].'"
				class="jigoshop-input jigoshop-range '.$class.'"
				name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']"
				type="range"
				min="'.$item['extra']['min'].'"
				max="'.$item['extra']['max'].'"
				step="'.$item['extra']['step'].'"
				value="'.$data[$item['id']].'" />';
			break;

		case 'select':
			$display .= '<select
				id="'.$item['id'].'"
				class="jigoshop-input jigoshop-select '.$class.'"
				name="'.JIGOSHOP_OPTIONS.'['.$item['id'].']" >';
			foreach ( $item['choices'] as $value => $label ) {
				if ( is_array( $label )) {
					$display .= '<optgroup label="'.$value.'">';
					foreach ( $label as $subValue => $subLabel ) {
						$display .= '<option
							value="'.esc_attr( $subValue ).'" '.selected( $data[$item['id']], $subValue, false ).' />'.$subLabel.'
							</option>';
					}
					$display .= '</optgroup>';             
				} 
				else {
					$display .= '<option
						value="'.esc_attr( $value ).'" '.selected( $data[$item['id']], $value, false ).' />'.$label.'
						</option>';
				}
			}
			$display .= '</select>';
			$id = $item['id'];
			?>
				<script type="text/javascript">
					jQuery(function() {
						jQuery("#<?php echo $id; ?>").select2({ width: '250px' });
					});
				</script>
			<?php
			break;

		default:
			jigoshop_log( "UNKOWN _type_ in Options parsing" );
			jigoshop_log( $item );
		}
		
		if ( $item['type'] != 'tab' ) {
			if ( empty( $item['desc'] ) ) {
				$explain_value = '';
			} else {
				$explain_value = $item['desc'];
			}
			$display .= '<div class="jigoshop-explain"><small>' . $explain_value . '</small></div>';
			$display .= '</td>';
		}

		return $display;
	}


	function array_find( $needle, $haystack ) {
		foreach ( $haystack as $key => $val ):
			if ( $needle == array( "label" => $val['label'], "compound" => $val['compound'], 'rate' => $val['rate'], 'shipping' => $val['shipping'], 'class' => $val['class'] ) ):
				return $key;
			endif;
		endforeach;
		return false;
	}
	
	
	function array_compare( $tax_rates ) {
		$after = array();
		foreach ( $tax_rates as $key => $val ):
			$first_two = array("label" => $val['label'], "compound" => $val['compound'], 'rate' => $val['rate'], 'shipping' => $val['shipping'], 'class' => $val['class'] );
			$found = $this->array_find( $first_two, $after );
			if ( $found !== false ):
				$combined  = $after[$found]["state"];
				$combined2 = $after[$found]["country"];
				$combined = !is_array($combined) ? array($combined) : $combined;
				$combined2 = !is_array($combined2) ? array($combined2) : $combined2;
				$after[$found] = array_merge($first_two,array( "state" => array_merge($combined,array($val['state'])), "country" => array_merge($combined2,array($val['country'])) ));
			else:
				$after = array_merge($after,array(array_merge($first_two,array("state" => $val['state'], "country" => $val['country']))));
			endif;
		endforeach;
		return $after;
	}
	
	
	/*
	 *	Format tax rates array for display
	 */
	function format_tax_rates_for_display( $value ) {

		$_tax = new jigoshop_tax();
		$tax_classes = $_tax->get_tax_classes();
		$tax_rates = (array) Jigoshop_Base::get_options()->get_option( 'jigoshop_tax_rates' );
		$applied_all_states = array();

		ob_start();
		?>
		<div id="jigoshop_tax_rates">
			<table class="tax_rate_rules" cellspacing="0">
				<thead>
					<tr>
						<th><?php _e('Remove', 'jigoshop'); ?></th>
						<th><?php _e('Tax Classes', 'jigoshop'); ?></th>
						<th><?php _e('Online Label', 'jigoshop'); ?></th>
						<th><?php _e('Country/State', 'jigoshop'); ?></th>
						<th><?php _e("Rate (%)", 'jigoshop'); ?></th>
						<th><?php _e('Apply to shipping', 'jigoshop'); ?></th>
						<th><?php _e('Compound', 'jigoshop'); ?></th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<th><?php _e('Remove', 'jigoshop'); ?></th>
						<th><?php _e('Tax Classes', 'jigoshop'); ?></th>
						<th><?php _e('Online Label', 'jigoshop'); ?></th>
						<th><?php _e('Country/State', 'jigoshop'); ?></th>
						<th><?php _e("Rate (%)", 'jigoshop'); ?></th>
						<th><?php _e('Apply to shipping', 'jigoshop'); ?></th>
						<th><?php _e('Compound', 'jigoshop'); ?></th>
					</tr>
				</tfoot>
				<tbody>
					<?php
					$i = -1;
					if ( $tax_rates && is_array( $tax_rates ) && sizeof( $tax_rates ) > 0 ) :

						$tax_rates = $this->array_compare( $tax_rates );
						
						foreach ( $tax_rates as $rate ) :
							if ( isset($rate['is_all_states']) && in_array($tax_rate['country'].$tax_rate['class'], $applied_all_states) )
								continue;

							$i++;// increment counter after check for all states having been applied

							echo '<tr class="tax_rate"><td><a href="#" class="remove button">&times;</a></td>';

							echo '<td><select id="tax_classes[' . esc_attr( $i ) . ']" name="tax_classes[' . esc_attr( $i ) . ']"><option value="*">' . __('Standard Rate', 'jigoshop') . '</option>';
							if ( $tax_classes ) {
								foreach ( $tax_classes as $class ) :
									echo '<option value="' . sanitize_title( $class ) . '"';

									if ( isset($rate['class']) && $rate['class'] == sanitize_title( $class )) echo 'selected="selected"';

									echo '>' . $class . '</option>';
								endforeach;
							}
							echo '</select></td>';

							echo '<td><input type="text" value="' . esc_attr( $rate['label']  ) . '" name="tax_label[' . esc_attr( $i ) . ']" placeholder="' . __('Online Label', 'jigoshop') . '" size="10" /></td>';

							echo '<td><select name="tax_country[' . esc_attr( $i ) . '][]" id="tax_country_' . esc_attr( $i ) . '" class="tax_select2" multiple="multiple" style="width:220px;">';
							if ( isset($rate['is_all_states']) ) :
								if ( is_array( $applied_all_states ) && !in_array( $tax_rate['country'].$tax_rate['class'], $applied_all_states )) :
									$applied_all_states[] = $tax_rate['country'].$tax_rate['class'];
									jigoshop_countries::country_dropdown_options( $rate['country'], '*', true ); //all-states
								else :
									continue;
								endif;
							else :
								jigoshop_countries::country_dropdown_options( $rate['country'], $rate['state'], true );
							endif;
							echo '</select>';

							echo '<button class="select_none button">'.__('None', 'jigoshop').'</button><button class="button select_us_states">'.__('US States', 'jigoshop').'</button><button class="button select_europe">'.__('EU States', 'jigoshop').'</button></td>';
							echo '<td><input type="text" value="' . esc_attr( $rate['rate']  ) . '" name="tax_rate[' . esc_attr( $i ) . ']" placeholder="' . __('Rate (%)', 'jigoshop') . '" size="6" /></td>';

							echo '<td><input type="checkbox" name="tax_shipping[' . esc_attr( $i ) . ']" ';
							if ( isset( $rate['shipping'] ) && $rate['shipping'] == 'yes' ) echo 'checked="checked"';
							echo ' /></td>';

							echo '<td><input type="checkbox" name="tax_compound[' . esc_attr( $i ) . ']" ';

							if ( isset( $rate['compound'] ) && $rate['compound'] == 'yes' ) echo 'checked="checked"';
							echo ' /></td></tr>';
							?><script type="text/javascript">
								jQuery(function() {
									jQuery("#tax_country_<?php echo esc_attr( $i ); ?>").select2();
								});
							</script><?php
						endforeach;
					endif;
					?>
				</tbody>

			</table>
			<div><a href="#" class="add button"><?php _e('+ Add Tax Rule', 'jigoshop'); ?></a></div>
		</div>

		<script type="text/javascript">
		/* <![CDATA[ */
			jQuery(function() {

				jQuery(document.body).on('click', 'tr.tax_rate .select_none', function(){
					jQuery(this).closest('td').find('select option').removeAttr("selected");
					jQuery(this).closest('td').find('select.tax_select2').trigger("change");
					return false;
				});
				jQuery(document.body).on('click', 'tr.tax_rate .select_us_states', function(e){
					jQuery(this).closest('td').find('select optgroup[label="<?php _e( 'United States', 'jigoshop' ); ?>"] option').attr("selected","selected");
					jQuery(this).closest('td').find('select.tax_select2').trigger("change");
					return false;
				});
				jQuery(document.body).on('change', 'tr.tax_rate .options select', function(e){
					jQuery(this).trigger("liszt:updated");
					jQuery(this).closest('td').find('label').text( jQuery(":selected", this).length + ' ' + '<?php _e('countries/states selected', 'jigoshop'); ?>' );
				});
				jQuery(document.body).on('click', 'tr.tax_rate .select_europe', function(e){
					jQuery(this).closest('td').find('option[value="BE"],option[value="FR"],option[value="DE"],option[value="IT"],option[value="LU"],option[value="NL"],option[value="DK"],option[value="IE"],option[value="GR"],option[value="PT"],option[value="ES"],option[value="AT"],option[value="FI"],option[value="SE"],option[value="CY"],option[value="CZ"],option[value="EE"],option[value="HU"],option[value="LV"],option[value="LT"],option[value="MT"],option[value="PL"],option[value="SK"],option[value="SI"],option[value="RO"],option[value="BG"],option[value="IM"],option[value="GB"]').attr("selected","selected");
					jQuery(this).closest('td').find('select.tax_select2').trigger("change");
					return false;
				});

				jQuery(document.body).on('click', '#jigoshop_tax_rates a.add', function() {
					var size = jQuery('.tax_rate_rules tbody tr').size();
					jQuery('<tr> \
							<td><a href="#" class="remove button">&times;</a></td> \
							<td><select name="tax_classes[' + size + ']"> \
								<option value="*"><?php _e('Standard Rate', 'jigoshop'); ?></option> \
								<?php $tax_classes = $_tax->get_tax_classes(); if ( $tax_classes ) : foreach ( $tax_classes as $class ) : echo '<option value="' . sanitize_title($class) . '">' . $class . '</option>'; endforeach; endif; ?> \
								</select></td> \
							<td><input type="text" name="tax_label[' + size + ']" placeholder="<?php _e('Online Label', 'jigoshop'); ?>" size="10" /></td> \
							<td><select name="tax_country[' + size + '][]" id="tax_country_' + size +'" multiple="multiple" style="width:220px;"> \
									<?php jigoshop_countries::country_dropdown_options('', '', true); ?></select></td> \
							<td><input type="text" name="tax_rate[' + size + ']" placeholder="<?php _e('Rate (%)', 'jigoshop'); ?>" size="6" /> \
							<td><input type="checkbox" name="tax_shipping[' + size + ']" /></td> \
							<td><input type="checkbox" name="tax_compound[' + size + ']" /></td> \
							</tr>'
					).appendTo('#jigoshop_tax_rates .tax_rate_rules tbody');
					jQuery('#tax_country_' + size).select2();
					return false;
				});
				jQuery(document.body).on('click', '#jigoshop_tax_rates a.remove', function(){
					var answer = confirm("<?php _e('Delete this rule?', 'jigoshop'); ?>");
					if (answer) jQuery(this).parent().parent().remove();
					return false;
				});
			});
			/* ]]> */
			</script>
		<?php

		$output = ob_get_contents();
		ob_end_clean();

		return $output;
	}

}

?>
Back to Top