woocommerce/admin/settings/settings-save.php

197 lines
5.3 KiB
PHP
Raw Normal View History

<?php
/**
* Update options
2012-08-14 13:33:15 +00:00
*
* Updates the options on the woocommerce settings pages. Returns true if saved.
2012-08-14 13:33:15 +00:00
*
* @author WooThemes
* @category Admin
2012-08-14 13:33:15 +00:00
* @package WooCommerce/Admin/Settings
* @version 1.6.4
*/
2012-10-15 10:32:24 +00:00
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2012-08-14 13:33:15 +00:00
/**
* Update all settings which are passed.
*
* @access public
* @param array $options
* @return void
*/
function woocommerce_update_options( $options ) {
2012-08-14 13:33:15 +00:00
2012-11-27 16:22:47 +00:00
if ( empty( $_POST ) )
return false;
2012-11-27 16:22:47 +00:00
// Options to update will be stored here
$update_options = array();
2012-11-27 16:22:47 +00:00
// Loop options and get values to save
foreach ( $options as $value ) {
2012-11-27 16:22:47 +00:00
if ( ! isset( $value['id'] ) )
continue;
2012-11-27 16:22:47 +00:00
$type = isset( $value['type'] ) ? sanitize_title( $value['type'] ) : '';
2012-11-27 16:22:47 +00:00
// Get the option name
$option_value = null;
2012-11-27 16:22:47 +00:00
switch ( $type ) {
2012-11-27 16:22:47 +00:00
// Standard types
case "checkbox" :
2012-11-27 16:22:47 +00:00
if ( isset( $_POST[ $value['id'] ] ) ) {
$option_value = 'yes';
} else {
$option_value = 'no';
}
2012-11-27 16:22:47 +00:00
break;
2012-11-27 16:22:47 +00:00
case "textarea" :
2012-11-27 16:22:47 +00:00
if ( isset( $_POST[$value['id']] ) ) {
$option_value = wp_kses_post( trim( stripslashes( $_POST[ $value['id'] ] ) ) );
} else {
$option_value = '';
}
2012-11-27 16:22:47 +00:00
break;
2012-11-27 16:22:47 +00:00
case "text" :
2012-11-26 13:49:41 +00:00
case 'email':
case 'number':
case "select" :
case "color" :
2012-12-28 19:22:47 +00:00
case 'password' :
case "single_select_page" :
case "single_select_country" :
case 'radio' :
2012-11-27 16:22:47 +00:00
if ( $value['id'] == 'woocommerce_price_thousand_sep' || $value['id'] == 'woocommerce_price_decimal_sep' ) {
// price separators get a special treatment as they should allow a spaces (don't trim)
if ( isset( $_POST[ $value['id'] ] ) ) {
$option_value = wp_kses_post( stripslashes( $_POST[ $value['id'] ] ) );
} else {
$option_value = '';
}
2012-11-27 16:22:47 +00:00
} elseif ( $value['id'] == 'woocommerce_price_num_decimals' ) {
// price separators get a special treatment as they should allow a spaces (don't trim)
if ( isset( $_POST[ $value['id'] ] ) ) {
$option_value = absint( $_POST[ $value['id'] ] );
} else {
$option_value = 2;
}
} elseif ( $value['id'] == 'woocommerce_hold_stock_minutes' ) {
// Allow > 0 or set to ''
if ( ! empty( $_POST[ $value['id'] ] ) ) {
$option_value = absint( $_POST[ $value['id'] ] );
} else {
$option_value = '';
}
wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
if ( $option_value != '' )
wp_schedule_single_event( time() + ( absint( $option_value ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
} else {
2012-11-27 16:22:47 +00:00
if ( isset( $_POST[$value['id']] ) ) {
$option_value = woocommerce_clean( stripslashes( $_POST[ $value['id'] ] ) );
} else {
$option_value = '';
2012-11-27 16:22:47 +00:00
}
}
2012-11-27 16:22:47 +00:00
break;
2012-11-27 16:22:47 +00:00
// Special types
2013-01-21 13:10:48 +00:00
case "multiselect" :
case "multi_select_countries" :
2012-11-27 16:22:47 +00:00
// Get countries array
2012-11-27 16:22:47 +00:00
if ( isset( $_POST[ $value['id'] ] ) )
$selected_countries = array_map( 'woocommerce_clean', array_map( 'stripslashes', (array) $_POST[ $value['id'] ] ) );
2012-11-27 16:22:47 +00:00
else
$selected_countries = array();
2012-11-27 16:22:47 +00:00
$option_value = $selected_countries;
2012-11-27 16:22:47 +00:00
break;
2012-11-27 16:22:47 +00:00
case "image_width" :
if ( isset( $_POST[$value['id'] ]['width'] ) ) {
2012-11-27 16:22:47 +00:00
$update_options[ $value['id'] ]['width'] = woocommerce_clean( stripslashes( $_POST[ $value['id'] ]['width'] ) );
$update_options[ $value['id'] ]['height'] = woocommerce_clean( stripslashes( $_POST[ $value['id'] ]['height'] ) );
2012-11-27 16:22:47 +00:00
if ( isset( $_POST[ $value['id'] ]['crop'] ) )
$update_options[ $value['id'] ]['crop'] = 1;
else
$update_options[ $value['id'] ]['crop'] = 0;
2012-11-27 16:22:47 +00:00
} else {
$update_options[ $value['id'] ]['width'] = $value['default']['width'];
$update_options[ $value['id'] ]['height'] = $value['default']['height'];
$update_options[ $value['id'] ]['crop'] = $value['default']['crop'];
}
2012-11-27 16:22:47 +00:00
break;
2012-11-27 16:22:47 +00:00
// Custom handling
default :
2012-11-27 16:22:47 +00:00
do_action( 'woocommerce_update_option_' . $type, $value );
2012-11-27 16:22:47 +00:00
break;
}
2012-11-27 16:22:47 +00:00
if ( ! is_null( $option_value ) ) {
// Check if option is an array
if ( strstr( $value['id'], '[' ) ) {
2012-11-27 16:22:47 +00:00
parse_str( $value['id'], $option_array );
2012-11-27 16:22:47 +00:00
// Option name is first key
$option_name = current( array_keys( $option_array ) );
2012-11-27 16:22:47 +00:00
// Get old option value
if ( ! isset( $update_options[ $option_name ] ) )
$update_options[ $option_name ] = get_option( $option_name, array() );
2012-11-27 16:22:47 +00:00
if ( ! is_array( $update_options[ $option_name ] ) )
$update_options[ $option_name ] = array();
2012-11-27 16:22:47 +00:00
// Set keys and value
$key = key( $option_array[ $option_name ] );
2012-11-27 16:22:47 +00:00
$update_options[ $option_name ][ $key ] = $option_value;
2012-11-27 16:22:47 +00:00
// Single value
} else {
$update_options[ $value['id'] ] = $option_value;
}
}
2012-11-27 16:22:47 +00:00
// Custom handling
do_action( 'woocommerce_update_option', $value );
}
2012-11-27 16:22:47 +00:00
// Now save the options
foreach( $update_options as $name => $value )
update_option( $name, $value );
2012-11-27 16:22:47 +00:00
return true;
}