Completed
Push — master ( 434173...5195b4 )
by Justin
07:15
created

WPSC_Settings_Page::get_current_tab_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * WP eCommerce Settings Page API.
4
 *
5
 * Third-party plugin / theme developers can add their own tabs to WPEC store settings page.
6
 *
7
 * Let's say you want to create a tab for your plugin called "Recommendation System", for example.
8
 * You first need to register the tab ID and title like this:
9
 *
10
 * <code>
11
 * function my_plugin_settings_tabs( $settings_page ) {
12
 * 	$settings_page->register_tab( 'recommendation_system', 'Recommendation System' );
13
 * }
14
 * add_action( 'wpsc_register_settings_tabs', 'my_plugin_settings_tabs', 10, 1 );
15
 * </code>
16
 *
17
 * Note that you need to hook into 'wpsc_register_settings_tabs' to do this.
18
 *
19
 * The next step is to create a class for your tab which inherits from the base 'WPSC_Settings_Tab'.
20
 * The name of the class needs to follow this convention: all the words have to be capitalized and
21
 * separated with an underscore, and prefixed with 'WPSC_Settings_Tab_'.
22
 *
23
 * In our example, because we registered our tab ID as 'recommendation_system', the class name should
24
 * be 'WPSC_Settings_Tab_Recommendation_System'.
25
 *
26
 * <code>
27
 * class WPSC_Settings_Tab_Recommendation_System extends WPSC_Settings_Tab
28
 * {
29
 * 	public function display() {
30
 * 		echo '<h3>Recommendation System Settings</h3>';
31
 * 		// output your tab content here
32
 * 	}
33
 * }
34
 * </code>
35
 *
36
 * All tab has to implement a method `display()` which outputs the HTML content for the tab.
37
 * You don't need to output the <form> element because it will be done for you.
38
 *
39
 * When outputting your form fields for the tab, name the fields 'wpsc_options[$your_option_name]'
40
 * so that they will automatically get saved to the database when the user submits the form. E.g.:
41
 *
42
 * <code>
43
 * <input type="text" value="something" name="wpsc_options[some_option]" />
44
 * </code>
45
 *
46
 * If you need to handle the form submission yourself, create a method in your tab class called
47
 * 'callback_submit_options()'. Then process your submitted fields there.
48
 *
49
 * <code>
50
 * class WPSC_Settings_Tab_Recommendation_System extends WPSC_Settings_Tab {
51
 * 	// ...
52
 * 	public function callback_submit_options() {
53
 * 		if ( isset( $_POST['my_option'] ) )
54
 * 			update_option( 'my_option', $_POST['my_option'] );
55
 * 	}
56
 * 	// ...
57
 * }
58
 * </code>
59
 *
60
 * @package wp-e-commerce
61
 * @subpackage settings-api
62
 */
63
64
/**
65
 * Abstract class for setting tabs
66
 *
67
 * @abstract
68
 * @since 3.8.8
69
 * @package wp-e-commerce
70
 * @subpackage settings-api
71
 */
72
abstract class WPSC_Settings_Tab {
73
	/**
74
	 * Display the content of the tab. This function has to be overridden.
75
	 *
76
	 * @since 3.8.8
77
	 * @abstract
78
	 * @access public
79
	 */
80
	abstract public function display();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
81
82
	/**
83
	 * Whether to display the update message when the options are submitted.
84
	 *
85
	 * @since 3.8.8.1
86
	 * @access private
87
	 */
88
	private $is_update_message_displayed = true;
89
90
	/**
91
	 * Whether to display the "Save Changes" button.
92
	 *
93
	 * @since 3.8.8.1
94
	 * @access private
95
	 */
96
	private $is_submit_button_displayed= true;
97
98
	/**
99
	 * Constructor
100
	 *
101
	 * @since 3.8.8
102
	 * @access public
103
	 */
104
	public function __construct() {}
105
106
	/**
107
	 * Make sure the update message will be displayed
108
	 *
109
	 * @since 3.8.8.1
110
	 * @access protected
111
	 */
112
	protected function display_update_message() {
113
		$this->is_update_message_displayed = true;
114
	}
115
116
	/**
117
	 * Make sure the update message will not be displayed
118
	 *
119
	 * @since 3.8.8.1
120
	 * @access protected
121
	 */
122
	protected function hide_update_message() {
123
		$this->is_update_message_displayed = false;
124
	}
125
126
	/**
127
	 * Query whether the update message is to be displayed or not.
128
	 *
129
	 * @since 3.8.8.1
130
	 * @access public
131
	 */
132
	public function is_update_message_displayed() {
133
		return $this->is_update_message_displayed;
134
	}
135
136
	/**
137
	 * Hide the default "Save Changes" button
138
	 *
139
	 * @since  3.8.8.1
140
	 * @access protected
141
	 */
142
	protected function hide_submit_button() {
143
		$this->is_submit_button_displayed = false;
144
	}
145
146
	/**
147
	 * Show the default "Save Changes" button
148
	 *
149
	 * @since 3.8.8.1
150
	 * @access protected
151
	 */
152
	protected function display_submit_button() {
153
		$this->is_submit_button_displayed = true;
154
	}
155
156
	/**
157
	 * Return whether the default "Save Changes" button is to be displayed.
158
	 *
159
	 * @since 3.8.8.1
160
	 * @access public
161
	 */
162
	public function is_submit_button_displayed() {
163
		return $this->is_submit_button_displayed;
164
	}
165
}
166
167
/**
168
 * Settings Page class. Singleton pattern.
169
 *
170
 * @since 3.8.8
171
 * @package wp-e-commerce
172
 * @subpackage settings-api
173
 * @final
174
 */
175
final class WPSC_Settings_Page {
176
	/**
177
	 * @staticvar object The active object instance
178
	 * @since 3.8.8
179
	 * @access private
180
	 */
181
	private static $instance;
182
183
	/**
184
	 * @staticvar array An array of default tabs containing pairs of id => title
185
	 * @since 3.8.8
186
	 * @access private
187
	 */
188
	private static $default_tabs;
189
190
	/**
191
	 * Initialize default tabs and add necessary action hooks.
192
	 *
193
	 * @since 3.8.8
194
	 *
195
	 * @uses add_action() Attaches to wpsc_register_settings_tabs hook
196
	 * @uses add_action() Attaches to wpsc_load_settings_tab_class hook
197
	 *
198
	 * @see wpsc_load_settings_page()
199
	 *
200
	 * @access public
201
	 * @static
202
	 */
203
	public static function init() {
204
		self::$default_tabs = array(
205
			'general'      => _x( 'General'     , 'General settings tab in Settings->Store page'     , 'wp-e-commerce' ),
206
			'admin'        => _x( 'Admin'       , 'Admin settings tab in Settings->Store page'       , 'wp-e-commerce' ),
207
			'taxes'        => _x( 'Taxes'       , 'Taxes settings tab in Settings->Store page'       , 'wp-e-commerce' ),
208
			'shipping'     => _x( 'Shipping'    , 'Shipping settings tab in Settings->Store page'    , 'wp-e-commerce' ),
209
			'gateway'      => _x( 'Payments'    , 'Payments settings tab in Settings->Store page'    , 'wp-e-commerce' ),
210
			'checkout'     => _x( 'Checkout'    , 'Checkout settings tab in Settings->Store page'    , 'wp-e-commerce' ),
211
			'marketing'    => _x( 'Marketing'   , 'Marketing settings tab in Settings->Store page'   , 'wp-e-commerce' ),
212
			'import'       => _x( 'Import'      , 'Import settings tab in Settings->Store page'      , 'wp-e-commerce' )
213
		);
214
215
		add_action( 'wpsc_register_settings_tabs' , array( 'WPSC_Settings_Page', 'register_default_tabs'  ), 1 );
216
		add_action( 'wpsc_load_settings_tab_class', array( 'WPSC_Settings_Page', 'load_default_tab_class' ), 1 );
217
	}
218
219
	/**
220
	 * Get active object instance
221
	 *
222
	 * @since 3.8.8
223
	 *
224
	 * @access public
225
	 * @static
226
	 * @return object
227
	 */
228
	public static function get_instance() {
229
		if ( ! self::$instance ) {
230
			self::$instance = new WPSC_Settings_Page();
231
		}
232
233
		return self::$instance;
234
	}
235
236
	/**
237
	 * Automatically load tab classes inside wpsc-admin/includes/settings-tabs.
238
	 *
239
	 * @since 3.8.8
240
	 *
241
	 * @see WPSC_Settings_Page::init()
242
	 *
243
	 * @uses WPSC_Settings_Page::get_current_tab_id() Gets current tab ID
244
	 *
245
	 * @access public
246
	 * @param  object $page_instance The WPSC_Settings_Page instance
247
	 * @static
248
	 */
249
	public static function load_default_tab_class( $page_instance ) {
250
		$current_tab_id = $page_instance->get_current_tab_id();
251
		if ( array_key_exists( $current_tab_id, self::$default_tabs ) ) {
252
			require_once( 'includes/settings-tabs/' . $current_tab_id . '.php' );
253
		}
254
	}
255
256
	/**
257
	 * Register the default tabs' ids and titles.
258
	 *
259
	 * @since 3.8.8
260
	 *
261
	 * @see WPSC_Settings_Page::init()
262
	 *
263
	 * @uses WPSC_Settings_Page::register_tab() Registers default tabs' idds and titles.
264
	 *
265
	 * @access public
266
	 * @param  object $page_instance The WPSC_Settings_Page instance
267
	 * @static
268
	 */
269
	public static function register_default_tabs( $page_instance ) {
270
		foreach ( self::$default_tabs as $id => $title ) {
271
			$page_instance->register_tab( $id, $title );
272
		}
273
	}
274
275
	/**
276
	 * Current tab ID
277
	 * @since 3.8.8
278
	 * @access private
279
	 * @var string
280
	 */
281
	private $current_tab_id;
282
283
	/**
284
	 * Current tab object
285
	 * @since 3.8.8
286
	 * @access private
287
	 * @var object
288
	 */
289
	private $current_tab;
290
291
	/**
292
	 * An array containing registered tabs
293
	 * @since 3.8.8
294
	 * @access private
295
	 * @var array
296
	 */
297
	private $tabs;
298
299
	/**
300
	 * Constructor
301
	 *
302
	 * @since 3.8.8
303
	 *
304
	 * @uses do_action()   Calls wpsc_register_settings_tabs hook.
305
	 * @uses apply_filters Calls wpsc_settings_tabs hook.
306
	 * @uses WPSC_Settings_Page::set_current_tab() Set current tab to the specified ID
307
	 *
308
	 * @access public
309
	 * @param string $tab_id Optional. If specified then the current tab will be set to this ID.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $tab_id not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
310
	 */
311
	public function __construct( $tab_id = null ) {
312
		do_action( 'wpsc_register_settings_tabs', $this );
313
		$this->tabs = apply_filters( 'wpsc_settings_tabs', $this->tabs );
314
		$this->set_current_tab( $tab_id );
315
	}
316
317
	/**
318
	 * Returns the current tab object
319
	 *
320
	 * @since 3.8.8
321
	 *
322
	 * @uses do_action()         Calls wpsc_load_settings_tab_class hook.
323
	 * @uses WPSC_Settings_Tab() constructing a new settings tab object
324
	 *
325
	 * @access public
326
	 * @return object WPSC_Settings_Tab object
327
	 */
328
	public function get_current_tab() {
329
		if ( ! $this->current_tab ) {
330
			do_action( 'wpsc_load_settings_tab_class', $this );
331
			$class_name = ucwords( str_replace( array( '-', '_' ), ' ', $this->current_tab_id ) );
332
			$class_name = str_replace( ' ', '_', $class_name );
333
			$class_name = 'WPSC_Settings_Tab_' . $class_name;
334
			if ( class_exists( $class_name ) ) {
335
				$reflection = new ReflectionClass( $class_name );
336
				$this->current_tab = $reflection->newInstance();
337
			}
338
		}
339
340
		return $this->current_tab;
341
	}
342
343
	/**
344
	 * Get current tab ID
345
	 * @since  3.8.8
346
	 * @access public
347
	 * @return string
348
	 */
349
	public function get_current_tab_id() {
350
		return $this->current_tab_id;
351
	}
352
353
	/**
354
	 * Set current tab to the specified tab ID.
355
	 *
356
	 * @since 3.8.8
357
	 *
358
	 * @uses check_admin_referer() Prevent CSRF
359
	 * @uses WPSC_Settings_Page::get_current_tab()        Initializes the current tab object.
360
	 * @uses WPSC_Settings_Page::save_options()           Saves the submitted options to the database.
361
	 * @uses WPSC_Settings_Tab::callback_submit_options() If this method exists in the tab object, it will be called after WPSC_Settings_Page::save_options().
362
	 *
363
	 * @access public
364
	 * @param string $tab_id Optional. The Tab ID. If this is not specified, the $_GET['tab'] variable will be used. If that variable also does not exists, the first tab will be used.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $tab_id not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
365
	 */
366
	public function set_current_tab( $tab_id = null ) {
367
		if ( ! $tab_id ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tab_id of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
368
			$tabs = array_keys( $this->tabs );
369
370
			if ( isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $this->tabs ) )
371
				$this->current_tab_id = $_GET['tab'];
372
			else
373
				$this->current_tab_id = array_shift( $tabs );
0 ignored issues
show
Documentation Bug introduced by
It seems like array_shift($tabs) can also be of type integer. However, the property $current_tab_id is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
374
375
		} else {
376
			$this->current_tab_id = $tab_id;
377
		}
378
379
		$this->current_tab = $this->get_current_tab();
380
381
		if ( isset( $_REQUEST['wpsc_admin_action'] ) && ( $_REQUEST['wpsc_admin_action'] == 'submit_options' ) ) {
382
			check_admin_referer( 'update-options', 'wpsc-update-options' );
383
384
			$this->save_options();
385
			do_action( 'wpsc_save_' . $this->current_tab_id . '_settings', $this->current_tab );
386
387
			$query_args = array();
388
			if ( is_callable( array( $this->current_tab, 'callback_submit_options' ) ) ) {
389
				$additional_query_args = $this->current_tab->callback_submit_options();
390
				if ( ! empty( $additional_query_args ) )
391
					$query_args += $additional_query_args;
392
			}
393
			if ( $this->current_tab->is_update_message_displayed() ) {
394
				if ( ! count( get_settings_errors() ) )
395
					add_settings_error( 'wpsc-settings', 'settings_updated', __( 'Settings saved.', 'wp-e-commerce' ), 'updated' );
396
				set_transient( 'settings_errors', get_settings_errors(), 30 );
397
				$query_args['settings-updated'] = true;
398
			}
399
			wp_redirect( esc_url_raw( add_query_arg( $query_args ) ) );
400
			exit;
401
		}
402
	}
403
404
	/**
405
	 * Register a tab's ID and title
406
	 *
407
	 * @since 3.8.8
408
	 *
409
	 * @access public
410
	 * @param  string $id    Tab ID.
411
	 * @param  string $title Tab title.
412
	 */
413
	public function register_tab( $id, $title ) {
414
		$this->tabs[$id] = $title;
415
	}
416
417
	/**
418
	 * Get an array containing tabs' IDs and titles
419
	 *
420
	 * @since 3.8.8
421
	 *
422
	 * @access public
423
	 * @return array
424
	 */
425
	public function get_tabs() {
426
		return $this->tabs;
427
	}
428
429
	/**
430
	 * Get the HTML class of a tab.
431
	 * @since 3.8.8
432
	 * @param  string $id Tab ID
433
	 * @return string
434
	 */
435
	private function tab_class( $id ) {
436
		$class = 'nav-tab';
437
		if ( $id == $this->current_tab_id )
438
			$class .= ' nav-tab-active';
439
		return $class;
440
	}
441
442
	/**
443
	 * Get the form's submit (action) url.
444
	 * @since 3.8.8
445
	 * @access private
446
	 * @return string
447
	 */
448
	private function submit_url() {
449
		$location = add_query_arg( 'tab', $this->current_tab_id );
450
		$location = apply_filters( 'wpsc_settings_page_submit_url', $location, $this, $this->current_tab );
451
		return esc_url( $location );
452
	}
453
454
	/**
455
	 * Output HTML of tab navigation.
456
	 * @since 3.8.8
457
	 * @access public
458
	 * @uses esc_html Prevents xss
459
	 */
460
	public function output_tabs() {
461
		?>
462
			<h2 class="nav-tab-wrapper">
463
				<?php foreach ( $this->tabs as $id => $title ): ?>
464
					<a data-tab-id="<?php echo esc_attr( $id ); ?>" class="<?php echo $this->tab_class( $id ); ?>" href="<?php echo esc_attr( '?page=wpsc-settings&tab=' . $id ); ?>"><?php echo esc_html( $this->tabs[$id] ); ?></a>
465
				<?php endforeach ?>
466
			</h2>
467
		<?php
468
	}
469
470
	/**
471
	 * Display the current tab.
472
	 * @since 3.8.8
473
	 * @uses do_action() Calls wpsc_{$current_tab_id}_settings_page hook.
474
	 * @uses WPSC_Settings_Tab::display() Displays the tab.
475
	 * @access public
476
	 */
477
	public function display_current_tab() {
478
		?>
479
			<div id="options_<?php echo esc_attr( $this->current_tab_id ); ?>" class="tab-content">
480
				<?php
481
					if ( is_callable( array( $this->current_tab, 'display' ) ) ) {
482
						do_action( 'wpsc_before_settings_tab', $this, $this->current_tab );
483
						$this->current_tab->display();
484
						do_action( 'wpsc_after_settings_tab', $this, $this->current_tab );
485
					}
486
				?>
487
488
				<?php do_action( 'wpsc_' . $this->current_tab_id . '_settings_page' ); ?>
489
				<div class="submit">
490
					<?php if ( $this->current_tab->is_submit_button_displayed() ): ?>
491
						<?php submit_button( __( 'Save Changes', 'wp-e-commerce' ) ); ?>
492
					<?php endif ?>
493
				</div>
494
			</div>
495
		<?php
496
	}
497
498
	/**
499
	 * Display the settings page.
500
	 * @since 3.8.8
501
	 * @uses esc_html_e()     Sanitize HTML
502
	 * @uses esc_attr()       Sanitize HTML attributes
503
	 * @uses wp_nonce_field() Prevent CSRF
504
	 * @uses WPSC_Settings_Page::output_tabs()         Display tab navigation.
505
	 * @uses WPSC_Settings_Page::display_current_tab() Display current tab.
506
	 * @access public
507
	 */
508
	public function display() {
509
		?>
510
			<div id="wpsc_options" class="wrap">
511
				<div id="icon_card" class="icon32"></div>
512
				<h2 id="wpsc-settings-page-title">
513
					<?php esc_html_e( 'Store Settings', 'wp-e-commerce' ); ?>
514
					<?php
515
						if ( current_user_can( 'customize' ) ) :
516
							printf(
517
							' <a class="page-title-action hide-if-no-customize" href="%1$s">%2$s</a>',
518
							esc_url( add_query_arg( array(
519
							array( 'autofocus' => array( 'panel' => 'wpsc' ) ),
520
							'return' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ),
521
							), admin_url( 'customize.php' ) ) ),
522
							__( 'Manage in Customizer' )
523
							);
524
						endif;
525
					?>
526
					<img src="<?php echo esc_url( wpsc_get_ajax_spinner() ); ?>" class="ajax-feedback" title="" alt="" />
527
				</h2>
528
				<?php $this->output_tabs(); ?>
529
				<div id='wpsc_options_page'>
530
					<form method='post' action='<?php echo esc_url( $this->submit_url() ); ?>' enctype='multipart/form-data' id='wpsc-settings-form'>
531
						<?php $this->display_current_tab(); ?>
532
					</form>
533
				</div>
534
			</div>
535
		<?php
536
	}
537
538
	/**
539
	 * Save submitted options to the database.
540
	 * @since 3.8.8
541
	 * @uses check_admin_referer() Prevents CSRF.
542
	 * @uses update_option() Saves options to the database.
543
	 * @uses wpdb::query() Queries the database.
544
	 * @uses wpdb::get_col() Queries the database.
545
	 * @access public
546
	 */
547
	private function save_options( $selected = '' ) {
548
		global $wpdb, $wpsc_gateways;
549
		$updated = 0;
550
551
		//This is to change the Overall target market selection
552
		check_admin_referer( 'update-options', 'wpsc-update-options' );
553
554
		//Should be refactored along with the Marketing tab
555
		if ( isset( $_POST['change-settings'] ) ) {
556
557
			if ( isset( $_POST['wpsc_also_bought'] ) && $_POST['wpsc_also_bought'] == 'on' )
558
				update_option( 'wpsc_also_bought', 1 );
559
			else
560
				update_option( 'wpsc_also_bought', 0 );
561
562
			if ( isset( $_POST['display_find_us'] ) && $_POST['display_find_us'] == 'on' )
563
				update_option( 'display_find_us', 1 );
564
			else
565
				update_option( 'display_find_us', 0 );
566
567
			if ( isset( $_POST['wpsc_share_this'] ) && $_POST['wpsc_share_this'] == 'on' )
568
				update_option( 'wpsc_share_this', 1 );
569
			else
570
				update_option( 'wpsc_share_this', 0 );
571
572
			if ( isset( $_POST['wpsc_ga_disable_tracking'] ) && $_POST['wpsc_ga_disable_tracking'] == '1' )
573
				update_option( 'wpsc_ga_disable_tracking', 1 );
574
			else
575
				update_option( 'wpsc_ga_disable_tracking', 0 );
576
577
			if ( isset( $_POST['wpsc_ga_currently_tracking'] ) && $_POST['wpsc_ga_currently_tracking'] == '1' )
578
				update_option( 'wpsc_ga_currently_tracking', 1 );
579
			else
580
				update_option( 'wpsc_ga_currently_tracking', 0 );
581
582
			if ( isset( $_POST['wpsc_ga_advanced'] ) && $_POST['wpsc_ga_advanced'] == '1' ) {
583
				update_option( 'wpsc_ga_advanced', 1 );
584
				update_option( 'wpsc_ga_currently_tracking', 1 );
585
			} else  {
586
				update_option( 'wpsc_ga_advanced', 0 );
587
			}
588
589
			if ( isset( $_POST['wpsc_ga_tracking_id'] ) && ! empty( $_POST['wpsc_ga_tracking_id'] ) )
590
				update_option( 'wpsc_ga_tracking_id', esc_attr( $_POST['wpsc_ga_tracking_id'] ) );
591
			else
592
				update_option( 'wpsc_ga_tracking_id', '' );
593
594
			if ( isset( $_POST['wpsc_ga_use_universal'] ) && $_POST['wpsc_ga_use_universal'] == '1' ) {
595
				update_option( 'wpsc_ga_use_universal', 1 );
596
			} else {
597
				update_option( 'wpsc_ga_use_universal', 0 );
598
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
599
600
		}
601
602
		if (empty($_POST['countrylist2']) && !empty($_POST['wpsc_options']['currency_sign_location']))
603
			$selected = 'none';
604
605
		if ( !isset( $_POST['countrylist2'] ) )
606
			$_POST['countrylist2'] = '';
607
		if ( !isset( $_POST['country_id'] ) )
608
			$_POST['country_id'] = '';
609
		if ( !isset( $_POST['country_tax'] ) )
610
			$_POST['country_tax'] = '';
611
612
		if ( $_POST['countrylist2'] != null || !empty($selected) ) {
613
			$AllSelected = false;
614
			if ( $selected == 'all' ) {
615
				$wpdb->query( "UPDATE `" . WPSC_TABLE_CURRENCY_LIST . "` SET visible = '1'" );
616
				$AllSelected = true;
617
			}
618
			if ( $selected == 'none' ) {
619
				$wpdb->query( "UPDATE `" . WPSC_TABLE_CURRENCY_LIST . "` SET visible = '0'" );
620
				$AllSelected = true;
621
			}
622
			if ( $AllSelected != true ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
623
				$countrylist = $wpdb->get_col( "SELECT id FROM `" . WPSC_TABLE_CURRENCY_LIST . "` ORDER BY country ASC " );
624
				//find the countries not selected
625
				$unselectedCountries = array_diff( $countrylist, $_POST['countrylist2'] );
626
				foreach ( $unselectedCountries as $unselected ) {
627
					$wpdb->update(
628
						WPSC_TABLE_CURRENCY_LIST,
629
						array(
630
						    'visible' => 0
631
						),
632
						array(
633
						    'id' => $unselected
634
						),
635
						'%d',
636
						'%d'
637
					    );
638
				}
639
640
				//find the countries that are selected
641
				$selectedCountries = array_intersect( $countrylist, $_POST['countrylist2'] );
642
				foreach ( $selectedCountries as $selected ) {
643
					$wpdb->update(
644
						WPSC_TABLE_CURRENCY_LIST,
645
						array(
646
						    'visible' => 1
647
						),
648
						array(
649
						    'id' => $selected
650
						),
651
						'%d',
652
						'%d'
653
					    );
654
				}
655
			}
656
657
			WPSC_Countries::clear_cache();
658
			wpsc_core_flush_temporary_data();
659
		}
660
		$previous_currency = get_option( 'currency_type' );
661
662
		//To update options
663
		if ( isset( $_POST['wpsc_options'] ) ) {
664
			$_POST['wpsc_options'] = stripslashes_deep( $_POST['wpsc_options'] );
665
			// make sure stock keeping time is a number
666
			if ( isset( $_POST['wpsc_options']['wpsc_stock_keeping_time'] ) ) {
667
				$skt = $_POST['wpsc_options']['wpsc_stock_keeping_time']; // I hate repeating myself
668
				$skt = (float) $skt;
669
				if ( $skt <= 0 || ( $skt < 1 && $_POST['wpsc_options']['wpsc_stock_keeping_interval'] == 'hour' ) ) {
670
					unset( $_POST['wpsc_options']['wpsc_stock_keeping_time'] );
671
					unset( $_POST['wpsc_options']['wpsc_stock_keeping_interval'] );
672
				}
673
			}
674
675
			foreach ( $_POST['wpsc_options'] as $key => $value ) {
676
				if ( $value != get_option( $key ) ) {
677
					update_option( $key, $value );
678
					$updated++;
679
680
				}
681
			}
682
		}
683
684
		if ( $previous_currency != get_option( 'currency_type' ) ) {
685
			$currency_code = $wpdb->get_var( "SELECT `code` FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `id` IN ('" . absint( get_option( 'currency_type' ) ) . "')" );
686
687
			$selected_gateways = get_option( 'custom_gateway_options' );
688
			$already_changed = array( );
0 ignored issues
show
introduced by
Empty array declaration must have no space between the parentheses
Loading history...
689
			foreach ( $selected_gateways as $selected_gateway ) {
690
				if ( isset( $wpsc_gateways[$selected_gateway]['supported_currencies'] ) ) {
691
					if ( in_array( $currency_code, $wpsc_gateways[$selected_gateway]['supported_currencies']['currency_list'] ) ) {
692
693
						$option_name = $wpsc_gateways[$selected_gateway]['supported_currencies']['option_name'];
694
695
						if ( ! in_array( $option_name, $already_changed ) ) {
696
							update_option( $option_name, $currency_code );
697
							$already_changed[] = $option_name;
698
						}
699
					}
700
				}
701
			}
702
		}
703
704
		foreach ( $GLOBALS['wpsc_shipping_modules'] as $shipping ) {
705
			if ( is_object( $shipping ) )
706
				$shipping->submit_form();
707
		}
708
709
		//This is for submitting shipping details to the shipping module
710
		if ( !isset( $_POST['update_gateways'] ) )
711
			$_POST['update_gateways'] = '';
712
713
		if ( !isset( $_POST['custom_shipping_options'] ) )
714
			$_POST['custom_shipping_options'] = null;
715
716
		if ( $_POST['update_gateways'] == 'true' ) {
717
718
			update_option( 'custom_shipping_options', array_map( 'sanitize_text_field', $_POST['custom_shipping_options'] ) );
719
720
			$shipadd = 0;
721
			foreach ( $GLOBALS['wpsc_shipping_modules'] as $shipping ) {
722
				foreach ( (array)$_POST['custom_shipping_options'] as $shippingoption ) {
723
					if ( $shipping->internal_name == $shippingoption ) {
724
						$shipadd++;
725
					}
726
				}
727
			}
728
		}
729
	}
730
}
731
732
WPSC_Settings_Page::init();
733
734
add_action( 'wpsc_after_settings_tab', '_wpsc_action_after_settings_tab' );
735
736
function _wpsc_action_after_settings_tab() {
737
	?>
738
	<input type='hidden' name='wpsc_admin_action' value='submit_options' />
739
	<?php
740
	wp_nonce_field( 'update-options', 'wpsc-update-options' );
741
}
742