woocommerce/admin/admin-settings-forms.php

444 lines
20 KiB
PHP

<?php
/**
* Functions for outputting and updating settings pages
*
* @author WooThemes
* @category Admin
* @package WooCommerce
*/
/**
* Update options
*
* Updates the options on the woocommerce settings pages. Returns true if saved.
*/
function woocommerce_update_options($options) {
if(!isset($_POST) || !$_POST) return false;
if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'woocommerce-settings') ) die( __('Action failed. Please refresh the page and retry.', 'woothemes') );
foreach ($options as $value) {
if (isset($value['id']) && $value['id']=='woocommerce_tax_rates') :
// Tate rates saving
$tax_classes = array();
$tax_countries = array();
$tax_rate = array();
$tax_rates = array();
$tax_shipping = array();
if (isset($_POST['tax_class'])) $tax_classes = $_POST['tax_class'];
if (isset($_POST['tax_country'])) $tax_countries = $_POST['tax_country'];
if (isset($_POST['tax_rate'])) $tax_rate = $_POST['tax_rate'];
if (isset($_POST['tax_shipping'])) $tax_shipping = $_POST['tax_shipping'];
for ($i=0; $i<sizeof($tax_classes); $i++) :
if (isset($tax_classes[$i]) && isset($tax_countries[$i]) && isset($tax_rate[$i]) && $tax_rate[$i] && is_numeric($tax_rate[$i])) :
$rate = number_format(woocommerce_clean($tax_rate[$i]), 4);
$class = woocommerce_clean($tax_classes[$i]);
if (isset($tax_shipping[$i]) && $tax_shipping[$i]) $shipping = 'yes'; else $shipping = 'no';
// Handle countries
$counties_array = array();
$countries = $tax_countries[$i];
if ($countries) foreach ($countries as $country) :
$country = woocommerce_clean($country);
$state = '*';
if (strstr($country, ':')) :
$cr = explode(':', $country);
$country = current($cr);
$state = end($cr);
endif;
$counties_array[trim($country)][] = trim($state);
endforeach;
$tax_rates[] = array(
'countries' => $counties_array,
'rate' => $rate,
'shipping' => $shipping,
'class' => $class
);
endif;
endfor;
update_option($value['id'], $tax_rates);
elseif (isset($value['type']) && $value['type']=='multi_select_countries') :
// Get countries array
if (isset($_POST[$value['id']])) $selected_countries = $_POST[$value['id']]; else $selected_countries = array();
update_option($value['id'], $selected_countries);
elseif ( isset($value['id']) && ( $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'] ] ) ) {
update_option($value['id'], $_POST[$value['id']] );
} else {
delete_option($value['id']);
}
elseif (isset($value['type']) && $value['type']=='checkbox') :
if(isset($value['id']) && isset($_POST[$value['id']])) {
update_option($value['id'], 'yes');
} else {
update_option($value['id'], 'no');
}
elseif (isset($value['type']) && $value['type']=='image_width') :
if(isset($value['id'])) {
update_option($value['id'].'_width', (int) woocommerce_clean($_POST[$value['id'].'_width']));
update_option($value['id'].'_height', (int) woocommerce_clean($_POST[$value['id'].'_height']));
} else {
update_option($value['id'].'_width', $value['std']);
update_option($value['id'].'_height', $value['std']);
}
else :
if(isset($value['id']) && isset($_POST[$value['id']])) {
update_option($value['id'], woocommerce_clean($_POST[$value['id']]));
} else {
delete_option($value['id']);
}
endif;
}
return true;
}
/**
* Admin fields
*
* Loops though the woocommerce options array and outputs each field.
*/
function woocommerce_admin_fields($options) {
global $woocommerce;
$counter = 1;
foreach ($options as $value) :
switch($value['type']) :
case 'tab':
echo '<div id="'.$value['type'].$counter.'" class="panel">';
break;
case 'title':
if (isset($value['name']) && $value['name']) echo '<h3>'.$value['name'].'</h3>';
if (isset($value['desc']) && $value['desc']) echo wpautop(wptexturize($value['desc']));
echo '<table class="form-table">'. "\n\n";
if (isset($value['id']) && $value['id']) do_action('woocommerce_settings_'.sanitize_title($value['id']));
break;
case 'sectionend':
if (isset($value['id']) && $value['id']) do_action('woocommerce_settings_'.sanitize_title($value['id']).'_end');
echo '</table>';
break;
case 'text':
?><tr valign="top">
<th scope="row" class="titledesc"><?php echo $value['name'] ?></th>
<td class="forminp"><input name="<?php echo $value['id'] ?>" id="<?php echo $value['id'] ?>" type="<?php echo $value['type'] ?>" style="<?php echo $value['css'] ?>" value="<?php if ( get_option( $value['id']) !== false && get_option( $value['id']) !== null ) echo stripslashes(get_option($value['id'])); else echo $value['std'] ?>" /> <span class="description"><?php echo $value['desc'] ?></span></td>
</tr><?php
break;
case 'image_width' :
?><tr valign="top">
<th scope="row" class="titledesc"><?php echo $value['name'] ?></th>
<td class="forminp"><?php _e('Width'); ?> <input name="<?php echo $value['id'] ?>_width" id="<?php echo $value['id'] ?>_width" type="text" size="3" value="<?php if ( get_option( $value['id'].'_width') ) echo stripslashes(get_option($value['id'].'_width')); else echo $value['std'] ?>" /> <?php _e('Height'); ?> <input name="<?php echo $value['id'] ?>_height" id="<?php echo $value['id'] ?>_height" type="text" size="3"" value="<?php if ( get_option( $value['id'].'_height') ) echo stripslashes(get_option($value['id'].'_height')); else echo $value['std'] ?>" /> <span class="description"><?php echo $value['desc'] ?></span></td>
</tr><?php
break;
case 'select':
?><tr valign="top">
<th scope="row" class="titledesc"><?php echo $value['name'] ?></th>
<td class="forminp"><select name="<?php echo $value['id'] ?>" id="<?php echo $value['id'] ?>" style="<?php echo $value['css'] ?>">
<?php
foreach ($value['options'] as $key => $val) {
?>
<option value="<?php echo $key ?>" <?php if (get_option($value['id']) == $key) { ?> selected="selected" <?php } ?>><?php echo ucfirst($val) ?></option>
<?php
}
?>
</select> <span class="description"><?php echo $value['desc'] ?></span>
</td>
</tr><?php
break;
case 'checkbox' :
if (!isset($value['checkboxgroup']) || (isset($value['checkboxgroup']) && $value['checkboxgroup']=='start')) :
?>
<tr valign="top">
<th scope="row" class="titledesc"><?php echo $value['name'] ?></th>
<td class="forminp">
<?php
endif;
?>
<fieldset><legend class="screen-reader-text"><span><?php echo $value['name'] ?></span></legend>
<label for="<?php echo $value['id'] ?>">
<input name="<?php echo $value['id'] ?>" id="<?php echo $value['id'] ?>" type="checkbox" value="1" <?php checked(get_option($value['id']), 'yes'); ?> />
<?php echo $value['desc'] ?></label><br>
</fieldset>
<?php
if (!isset($value['checkboxgroup']) || (isset($value['checkboxgroup']) && $value['checkboxgroup']=='end')) :
?>
</td>
</tr>
<?php
endif;
break;
case 'textarea':
?><tr valign="top">
<th scope="row" class="titledesc"><?php echo $value['name'] ?></th>
<td class="forminp">
<textarea <?php if ( isset($value['args']) ) echo $value['args'] . ' '; ?>name="<?php echo $value['id'] ?>" id="<?php echo $value['id'] ?>" style="<?php echo $value['css'] ?>"><?php if (get_option($value['id'])) echo stripslashes(get_option($value['id'])); else echo $value['std']; ?></textarea> <span class="description"><?php echo $value['desc'] ?></span>
</td>
</tr><?php
break;
case 'tabend':
echo '</div>';
$counter++;
break;
case 'single_select_page' :
$page_setting = (int) get_option($value['id']);
$args = array( 'name' => $value['id'],
'id' => $value['id']. '" style="width: 200px;',
'sort_column' => 'menu_order',
'sort_order' => 'ASC',
'selected' => $page_setting);
if( isset($value['args']) ) $args = wp_parse_args($value['args'], $args);
?><tr valign="top" class="single_select_page">
<th scope="row" class="titledesc"><?php echo $value['name'] ?></th>
<td class="forminp">
<?php wp_dropdown_pages($args); ?> <span class="description"><?php echo $value['desc'] ?></span>
</td>
</tr><?php
break;
case 'single_select_country' :
$countries = $woocommerce->countries->countries;
$country_setting = (string) get_option($value['id']);
if (strstr($country_setting, ':')) :
$country = current(explode(':', $country_setting));
$state = end(explode(':', $country_setting));
else :
$country = $country_setting;
$state = '*';
endif;
?><tr valign="top" class="multi_select_countries">
<th scope="rpw" class="titledesc"><?php echo $value['name'] ?></th>
<td class="forminp"><select name="<?php echo $value['id'] ?>" title="Country" style="width: 175px;">
<?php echo $woocommerce->countries->country_dropdown_options($country, $state); ?>
</select> <span class="description"><?php echo $value['desc'] ?></span>
</td>
</tr><?php
break;
case 'multi_select_countries' :
$countries = $woocommerce->countries->countries;
asort($countries);
$selections = (array) get_option($value['id']);
?><tr valign="top" class="multi_select_countries">
<th scope="row" class="titledesc"><?php echo $value['name'] ?></th>
<td class="forminp">
<div class="multi_select_countries"><ul><?php
if ($countries) foreach ($countries as $key=>$val) :
echo '<li><label><input type="checkbox" name="'. $value['id'] .'[]" value="'. $key .'" ';
if (in_array($key, $selections)) echo 'checked="checked"';
echo ' />'. $val .'</label></li>';
endforeach;
?></ul></div>
</td>
</tr><?php
break;
case 'tax_rates' :
$_tax = new woocommerce_tax();
$tax_classes = $_tax->get_tax_classes();
$tax_rates = get_option('woocommerce_tax_rates');
?><tr valign="top">
<th scope="row" class="titledesc"><?php echo $value['name'] ?></th>
<td class="forminp" id="tax_rates">
<div class="taxrows">
<?php $i = -1; if ($tax_rates && is_array($tax_rates) && sizeof($tax_rates)>0) foreach( $tax_rates as $rate ) : $i++; ?>
<div class="taxrow">
<select name="tax_country[<?php echo $i; ?>][]" title="Country" class="country_multiselect" size="10" multiple="multiple">
<?php echo $woocommerce->countries->country_multiselect_options( $rate['countries'] ); ?>
</select>
<select name="tax_class[<?php echo $i; ?>]" title="Tax Class">
<option value=""><?php _e('Standard Rate', 'woothemes'); ?></option>
<?php
if ($tax_classes) foreach ($tax_classes as $class) :
echo '<option value="'.sanitize_title($class).'"';
selected($rate['class'], sanitize_title($class));
echo '>'.$class.'</option>';
endforeach;
?>
</select>
<input type="text" class="text" value="<?php echo $rate['rate']; ?>" name="tax_rate[<?php echo $i; ?>]" title="<?php _e('Rate', 'woothemes'); ?>" placeholder="<?php _e('Rate', 'woothemes'); ?>" maxlength="8" />%
<label class="checkbox"><input type="checkbox" name="tax_shipping[<?php echo $i; ?>]" <?php if (isset($rate['shipping'])) checked($rate['shipping'], 'yes'); ?> /> <?php _e('Apply to shipping', 'woothemes'); ?></label><a href="#" class="remove button">&times;</a>
</div>
<?php endforeach; ?>
</div>
<p><a href="#" class="add button"><?php _e('+ Add Tax Rule', 'woothemes'); ?></a></p>
<script type="text/javascript">
jQuery(function() {
// Tax
jQuery('#tax_rates a.add').live('click', function(){
var size = jQuery('.taxrows .taxrow').size();
// Add the row
jQuery('<div class="taxrow">\
<select name="tax_country[' + size + '][]" title="Country" class="country_multiselect" size="10" multiple="multiple"><?php echo $woocommerce->countries->country_multiselect_options('',true); ?></select>\
<select name="tax_class[' + size + ']" title="Tax Class"><option value=""><?php _e('Standard Rate', 'woothemes'); ?></option><?php
if ($tax_classes) foreach ($tax_classes as $class) :
echo '<option value="'.sanitize_title($class).'">'.$class.'</option>';
endforeach;
?></select>\
<input type="text" class="text" name="tax_rate[' + size + ']" title="<?php _e('Rate', 'woothemes'); ?>" placeholder="<?php _e('Rate', 'woothemes'); ?>" maxlength="8" />% \
<label class="checkbox"><input type="checkbox" name="tax_shipping[' + size + ']" checked="checked" /> <?php _e('Apply to shipping', 'woothemes'); ?></label><a href="#" class="remove button">&times;</a>\
</div>').appendTo('#tax_rates div.taxrows');
// Multiselect
jQuery(".country_multiselect").multiselect({
noneSelectedText: '<?php _e('Select countries/states', 'woothemes'); ?>',
selectedList: 4
});
return false;
});
jQuery('#tax_rates a.remove').live('click', function(){
var answer = confirm("<?php _e('Delete this rule?', 'woothemes'); ?>");
if (answer) {
jQuery('input', jQuery(this).parent()).val('');
jQuery(this).parent().hide();
}
return false;
});
});
</script>
</td>
</tr>
<?php
break;
case "shipping_options" :
$links = array();
foreach ($woocommerce->shipping->shipping_methods as $method) :
$title = ($method->method_title) ? ucwords($method->method_title) : ucwords($method->id);
$links[] = '<a href="#shipping-'.$method->id.'">'.$title.'</a>';
endforeach;
echo '<div class="subsubsub_section"><ul class="subsubsub"><li>' . implode(' | </li><li>', $links) . '</li></ul><br class="clear" />';
foreach ($woocommerce->shipping->shipping_methods as $method) :
echo '<div class="section" id="shipping-'.$method->id.'">';
$method->admin_options();
echo '</div>';
endforeach;
echo '</div>';
break;
case "gateway_options" :
$links = array();
foreach ($woocommerce->payment_gateways->payment_gateways() as $gateway) :
$title = ($gateway->method_title) ? ucwords($gateway->method_title) : ucwords($gateway->id);
$links[] = '<a href="#gateway-'.$gateway->id.'">'.$title.'</a>';
endforeach;
echo '<div class="subsubsub_section"><ul class="subsubsub"><li>' . implode(' | </li><li>', $links) . '</li></ul><br class="clear" />';
foreach ($woocommerce->payment_gateways->payment_gateways() as $gateway) :
echo '<div class="section" id="gateway-'.$gateway->id.'">';
$gateway->admin_options();
echo '</div>';
endforeach;
echo '</div>';
break;
default :
do_action('woocommerce_settings_field_type_' . $value['type']);
break;
endswitch;
endforeach;
?>
<script type="text/javascript">
// Tabs
jQuery('.woo-nav-tab-wrapper a:first').addClass('nav-tab-active');
jQuery('div.panel:not(div.panel:first)').hide();
jQuery('.woo-nav-tab-wrapper a').click(function(){
jQuery('.woo-nav-tab-wrapper a').removeClass('nav-tab-active');
jQuery(this).addClass('nav-tab-active');
jQuery('div.panel').hide();
jQuery( jQuery(this).attr('href') ).show();
jQuery.cookie('woocommerce_settings_tab_index', jQuery(this).index('.woo-nav-tab-wrapper a'))
return false;
});
<?php if (isset($_COOKIE['woocommerce_settings_tab_index']) && $_COOKIE['woocommerce_settings_tab_index'] > 0) : ?>
jQuery('.woo-nav-tab-wrapper a:eq(<?php echo $_COOKIE['woocommerce_settings_tab_index']; ?>)').click();
<?php endif; ?>
// Subsubsub tabs
jQuery('ul.subsubsub li a').click(function(){
jQuery('a', jQuery(this).closest('ul.subsubsub')).removeClass('current');
jQuery(this).addClass('current');
jQuery('.section', jQuery(this).closest('.subsubsub_section')).hide();
jQuery( jQuery(this).attr('href') ).show();
return false;
});
jQuery('ul.subsubsub').each(function(){
jQuery('li a:eq(0)', jQuery(this)).click();
});
// Countries
jQuery('select#woocommerce_allowed_countries').change(function(){
if (jQuery(this).val()=="specific") {
jQuery(this).parent().parent().next('tr.multi_select_countries').show();
} else {
jQuery(this).parent().parent().next('tr.multi_select_countries').hide();
}
}).change();
// Country Multiselect boxes
jQuery(".country_multiselect").multiselect({
noneSelectedText: '<?php _e('Select countries/states', 'woothemes'); ?>',
selectedList: 4
});
</script>
<?php
}