1 <?php
  2 /**
  3  * WooCommerce Admin Settings Class.
  4  *
  5  * @author      WooThemes
  6  * @category    Admin
  7  * @package     WooCommerce/Admin
  8  * @version     2.1.0
  9  */
 10 
 11 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 12 
 13 if ( ! class_exists( 'WC_Admin_Settings' ) ) :
 14 
 15 /**
 16  * WC_Admin_Settings
 17  */
 18 class WC_Admin_Settings {
 19 
 20     private static $settings = array();
 21     private static $errors   = array();
 22     private static $messages = array();
 23 
 24     /**
 25      * Include the settings page classes
 26      */
 27     public static function get_settings_pages() {
 28         if ( empty( self::$settings ) ) {
 29             $settings = array();
 30 
 31             include_once( 'settings/class-wc-settings-page.php' );
 32 
 33             $settings[] = include( 'settings/class-wc-settings-general.php' );
 34             $settings[] = include( 'settings/class-wc-settings-products.php' );
 35             $settings[] = include( 'settings/class-wc-settings-tax.php' );
 36             $settings[] = include( 'settings/class-wc-settings-checkout.php' );
 37             $settings[] = include( 'settings/class-wc-settings-shipping.php' );
 38             $settings[] = include( 'settings/class-wc-settings-accounts.php' );
 39             $settings[] = include( 'settings/class-wc-settings-emails.php' );
 40             $settings[] = include( 'settings/class-wc-settings-integrations.php' );
 41 
 42             self::$settings = apply_filters( 'woocommerce_get_settings_pages', $settings );
 43         }
 44         return self::$settings;
 45     }
 46 
 47     /**
 48      * Save the settings
 49      */
 50     public static function save() {
 51         global $current_section, $current_tab;
 52 
 53         if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) )
 54                 die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
 55 
 56         // Trigger actions
 57         do_action( 'woocommerce_settings_save_' . $current_tab );
 58         do_action( 'woocommerce_update_options_' . $current_tab );
 59         do_action( 'woocommerce_update_options' );
 60 
 61         // Clear any unwanted data
 62         wc_delete_product_transients();
 63         delete_transient( 'woocommerce_cache_excluded_uris' );
 64 
 65         self::add_message( __( 'Your settings have been saved.', 'woocommerce' ) );
 66         self::check_download_folder_protection();
 67 
 68         // Re-add endpoints and flush rules
 69         WC()->query->init_query_vars();
 70         WC()->query->add_endpoints();
 71         flush_rewrite_rules();
 72 
 73         do_action( 'woocommerce_settings_saved' );
 74     }
 75 
 76     /**
 77      * Add a message
 78      * @param string $text
 79      */
 80     public static function add_message( $text ) {
 81         self::$messages[] = $text;
 82     }
 83 
 84     /**
 85      * Add an error
 86      * @param string $text
 87      */
 88     public static function add_error( $text ) {
 89         self::$errors[] = $text;
 90     }
 91 
 92     /**
 93      * Output messages + errors
 94      */
 95     public static function show_messages() {
 96         if ( sizeof( self::$errors ) > 0 ) {
 97             foreach ( self::$errors as $error )
 98                 echo '<div id="message" class="error fade"><p><strong>' . esc_html( $error ) . '</strong></p></div>';
 99         } elseif ( sizeof( self::$messages ) > 0 ) {
100             foreach ( self::$messages as $message )
101                 echo '<div id="message" class="updated fade"><p><strong>' . esc_html( $message ) . '</strong></p></div>';
102         }
103     }
104 
105     /**
106      * Settings page.
107      *
108      * Handles the display of the main woocommerce settings page in admin.
109      *
110      * @access public
111      * @return void
112      */
113     public static function output() {
114         global $current_section, $current_tab;
115 
116         do_action( 'woocommerce_settings_start' );
117 
118         wp_enqueue_script( 'woocommerce_settings', WC()->plugin_url() . '/assets/js/admin/settings.min.js', array( 'jquery', 'jquery-ui-datepicker', 'jquery-ui-sortable', 'iris', 'chosen' ), WC()->version, true );
119 
120         wp_localize_script( 'woocommerce_settings', 'woocommerce_settings_params', array(
121             'i18n_nav_warning' => __( 'The changes you made will be lost if you navigate away from this page.', 'woocommerce' )
122         ) );
123 
124         // Include settings pages
125         self::get_settings_pages();
126 
127         // Get current tab/section
128         $current_tab     = empty( $_GET['tab'] ) ? 'general' : sanitize_title( $_GET['tab'] );
129         $current_section = empty( $_REQUEST['section'] ) ? '' : sanitize_title( $_REQUEST['section'] );
130 
131         // Save settings if data has been posted
132         if ( ! empty( $_POST ) )
133             self::save();
134 
135         // Add any posted messages
136         if ( ! empty( $_GET['wc_error'] ) )
137             self::add_error( stripslashes( $_GET['wc_error'] ) );
138 
139          if ( ! empty( $_GET['wc_message'] ) )
140             self::add_message( stripslashes( $_GET['wc_message'] ) );
141 
142         self::show_messages();
143 
144         // Get tabs for the settings page
145         $tabs = apply_filters( 'woocommerce_settings_tabs_array', array() );
146 
147         include 'views/html-admin-settings.php';
148     }
149 
150     /**
151      * Get a setting from the settings API.
152      *
153      * @param mixed $option
154      * @return string
155      */
156     public static function get_option( $option_name, $default = '' ) {
157         // Array value
158         if ( strstr( $option_name, '[' ) ) {
159 
160             parse_str( $option_name, $option_array );
161 
162             // Option name is first key
163             $option_name = current( array_keys( $option_array ) );
164 
165             // Get value
166             $option_values = get_option( $option_name, '' );
167 
168             $key = key( $option_array[ $option_name ] );
169 
170             if ( isset( $option_values[ $key ] ) )
171                 $option_value = $option_values[ $key ];
172             else
173                 $option_value = null;
174 
175         // Single value
176         } else {
177             $option_value = get_option( $option_name, null );
178         }
179 
180         if ( is_array( $option_value ) )
181             $option_value = array_map( 'stripslashes', $option_value );
182         elseif ( ! is_null( $option_value ) )
183             $option_value = stripslashes( $option_value );
184 
185         return $option_value === null ? $default : $option_value;
186     }
187 
188     /**
189      * Output admin fields.
190      *
191      * Loops though the woocommerce options array and outputs each field.
192      *
193      * @access public
194      * @param array $options Opens array to output
195      */
196     public static function output_fields( $options ) {
197         foreach ( $options as $value ) {
198             if ( ! isset( $value['type'] ) ) continue;
199             if ( ! isset( $value['id'] ) ) $value['id'] = '';
200             if ( ! isset( $value['title'] ) ) $value['title'] = isset( $value['name'] ) ? $value['name'] : '';
201             if ( ! isset( $value['class'] ) ) $value['class'] = '';
202             if ( ! isset( $value['css'] ) ) $value['css'] = '';
203             if ( ! isset( $value['default'] ) ) $value['default'] = '';
204             if ( ! isset( $value['desc'] ) ) $value['desc'] = '';
205             if ( ! isset( $value['desc_tip'] ) ) $value['desc_tip'] = false;
206 
207             // Custom attribute handling
208             $custom_attributes = array();
209 
210             if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) )
211                 foreach ( $value['custom_attributes'] as $attribute => $attribute_value )
212                     $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
213 
214             // Description handling
215             if ( $value['desc_tip'] === true ) {
216                 $description = '';
217                 $tip = $value['desc'];
218             } elseif ( ! empty( $value['desc_tip'] ) ) {
219                 $description = $value['desc'];
220                 $tip = $value['desc_tip'];
221             } elseif ( ! empty( $value['desc'] ) ) {
222                 $description = $value['desc'];
223                 $tip = '';
224             } else {
225                 $description = $tip = '';
226             }
227 
228             if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ) ) ) {
229                 $description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>';
230             } elseif ( $description && in_array( $value['type'], array( 'checkbox' ) ) ) {
231                 $description =  wp_kses_post( $description );
232             } elseif ( $description ) {
233                 $description = '<span class="description">' . wp_kses_post( $description ) . '</span>';
234             }
235 
236             if ( $tip && in_array( $value['type'], array( 'checkbox' ) ) ) {
237 
238                 $tip = '<p class="description">' . $tip . '</p>';
239 
240             } elseif ( $tip ) {
241 
242                 $tip = '<img class="help_tip" data-tip="' . esc_attr( $tip ) . '" src="' . WC()->plugin_url() . '/assets/images/help.png" height="16" width="16" />';
243 
244             }
245 
246             // Switch based on type
247             switch( $value['type'] ) {
248 
249                 // Section Titles
250                 case 'title':
251                     if ( ! empty( $value['title'] ) ) {
252                         echo '<h3>' . esc_html( $value['title'] ) . '</h3>';
253                     }
254                     if ( ! empty( $value['desc'] ) ) {
255                         echo wpautop( wptexturize( wp_kses_post( $value['desc'] ) ) );
256                     }
257                     echo '<table class="form-table">'. "\n\n";
258                     if ( ! empty( $value['id'] ) ) {
259                         do_action( 'woocommerce_settings_' . sanitize_title( $value['id'] ) );
260                     }
261                 break;
262 
263                 // Section Ends
264                 case 'sectionend':
265                     if ( ! empty( $value['id'] ) ) {
266                         do_action( 'woocommerce_settings_' . sanitize_title( $value['id'] ) . '_end' );
267                     }
268                     echo '</table>';
269                     if ( ! empty( $value['id'] ) ) {
270                         do_action( 'woocommerce_settings_' . sanitize_title( $value['id'] ) . '_after' );
271                     }
272                 break;
273 
274                 // Standard text inputs and subtypes like 'number'
275                 case 'text':
276                 case 'email':
277                 case 'number':
278                 case 'color' :
279                 case 'password' :
280 
281                     $type           = $value['type'];
282                     $class          = '';
283                     $option_value   = self::get_option( $value['id'], $value['default'] );
284 
285                     if ( $value['type'] == 'color' ) {
286                         $type = 'text';
287                         $value['class'] .= 'colorpick';
288                         $description .= '<div id="colorPickerDiv_' . esc_attr( $value['id'] ) . '" class="colorpickdiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"></div>';
289                     }
290 
291                     ?><tr valign="top">
292                         <th scope="row" class="titledesc">
293                             <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
294                             <?php echo $tip; ?>
295                         </th>
296                         <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
297                             <input
298                                 name="<?php echo esc_attr( $value['id'] ); ?>"
299                                 id="<?php echo esc_attr( $value['id'] ); ?>"
300                                 type="<?php echo esc_attr( $type ); ?>"
301                                 style="<?php echo esc_attr( $value['css'] ); ?>"
302                                 value="<?php echo esc_attr( $option_value ); ?>"
303                                 class="<?php echo esc_attr( $value['class'] ); ?>"
304                                 <?php echo implode( ' ', $custom_attributes ); ?>
305                                 /> <?php echo $description; ?>
306                         </td>
307                     </tr><?php
308                 break;
309 
310                 // Textarea
311                 case 'textarea':
312 
313                     $option_value   = self::get_option( $value['id'], $value['default'] );
314 
315                     ?><tr valign="top">
316                         <th scope="row" class="titledesc">
317                             <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
318                             <?php echo $tip; ?>
319                         </th>
320                         <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
321                             <?php echo $description; ?>
322 
323                             <textarea
324                                 name="<?php echo esc_attr( $value['id'] ); ?>"
325                                 id="<?php echo esc_attr( $value['id'] ); ?>"
326                                 style="<?php echo esc_attr( $value['css'] ); ?>"
327                                 class="<?php echo esc_attr( $value['class'] ); ?>"
328                                 <?php echo implode( ' ', $custom_attributes ); ?>
329                                 ><?php echo esc_textarea( $option_value );  ?></textarea>
330                         </td>
331                     </tr><?php
332                 break;
333 
334                 // Select boxes
335                 case 'select' :
336                 case 'multiselect' :
337 
338                     $option_value   = self::get_option( $value['id'], $value['default'] );
339 
340                     ?><tr valign="top">
341                         <th scope="row" class="titledesc">
342                             <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
343                             <?php echo $tip; ?>
344                         </th>
345                         <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
346                             <select
347                                 name="<?php echo esc_attr( $value['id'] ); ?><?php if ( $value['type'] == 'multiselect' ) echo '[]'; ?>"
348                                 id="<?php echo esc_attr( $value['id'] ); ?>"
349                                 style="<?php echo esc_attr( $value['css'] ); ?>"
350                                 class="<?php echo esc_attr( $value['class'] ); ?>"
351                                 <?php echo implode( ' ', $custom_attributes ); ?>
352                                 <?php if ( $value['type'] == 'multiselect' ) echo 'multiple="multiple"'; ?>
353                                 >
354                                 <?php
355                                     foreach ( $value['options'] as $key => $val ) {
356                                         ?>
357                                         <option value="<?php echo esc_attr( $key ); ?>" <?php
358 
359                                             if ( is_array( $option_value ) )
360                                                 selected( in_array( $key, $option_value ), true );
361                                             else
362                                                 selected( $option_value, $key );
363 
364                                         ?>><?php echo $val ?></option>
365                                         <?php
366                                     }
367                                 ?>
368                            </select> <?php echo $description; ?>
369                         </td>
370                     </tr><?php
371                 break;
372 
373                 // Radio inputs
374                 case 'radio' :
375 
376                     $option_value   = self::get_option( $value['id'], $value['default'] );
377 
378                     ?><tr valign="top">
379                         <th scope="row" class="titledesc">
380                             <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
381                             <?php echo $tip; ?>
382                         </th>
383                         <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
384                             <fieldset>
385                                 <?php echo $description; ?>
386                                 <ul>
387                                 <?php
388                                     foreach ( $value['options'] as $key => $val ) {
389                                         ?>
390                                         <li>
391                                             <label><input
392                                                 name="<?php echo esc_attr( $value['id'] ); ?>"
393                                                 value="<?php echo $key; ?>"
394                                                 type="radio"
395                                                 style="<?php echo esc_attr( $value['css'] ); ?>"
396                                                 class="<?php echo esc_attr( $value['class'] ); ?>"
397                                                 <?php echo implode( ' ', $custom_attributes ); ?>
398                                                 <?php checked( $key, $option_value ); ?>
399                                                 /> <?php echo $val ?></label>
400                                         </li>
401                                         <?php
402                                     }
403                                 ?>
404                                 </ul>
405                             </fieldset>
406                         </td>
407                     </tr><?php
408                 break;
409 
410                 // Checkbox input
411                 case 'checkbox' :
412 
413                     $option_value    = self::get_option( $value['id'], $value['default'] );
414                     $visbility_class = array();
415 
416                     if ( ! isset( $value['hide_if_checked'] ) ) {
417                         $value['hide_if_checked'] = false;
418                     }
419                     if ( ! isset( $value['show_if_checked'] ) ) {
420                         $value['show_if_checked'] = false;
421                     }
422                     if ( $value['hide_if_checked'] == 'yes' || $value['show_if_checked'] == 'yes' ) {
423                         $visbility_class[] = 'hidden_option';
424                     }
425                     if ( $value['hide_if_checked'] == 'option' ) {
426                         $visbility_class[] = 'hide_options_if_checked';
427                     }
428                     if ( $value['show_if_checked'] == 'option' ) {
429                         $visbility_class[] = 'show_options_if_checked';
430                     }
431 
432                     if ( ! isset( $value['checkboxgroup'] ) || 'start' == $value['checkboxgroup'] ) {
433                         ?>
434                             <tr valign="top" class="<?php echo esc_attr( implode( ' ', $visbility_class ) ); ?>">
435                                 <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?></th>
436                                 <td class="forminp forminp-checkbox">
437                                     <fieldset>
438                         <?php
439                     } else {
440                         ?>
441                             <fieldset class="<?php echo esc_attr( implode( ' ', $visbility_class ) ); ?>">
442                         <?php
443                     }
444 
445                     if ( ! empty( $value['title'] ) ) {
446                         ?>
447                             <legend class="screen-reader-text"><span><?php echo esc_html( $value['title'] ) ?></span></legend>
448                         <?php
449                     }
450 
451                     ?>
452                         <label for="<?php echo $value['id'] ?>">
453                             <input
454                                 name="<?php echo esc_attr( $value['id'] ); ?>"
455                                 id="<?php echo esc_attr( $value['id'] ); ?>"
456                                 type="checkbox"
457                                 value="1"
458                                 <?php checked( $option_value, 'yes'); ?>
459                                 <?php echo implode( ' ', $custom_attributes ); ?>
460                             /> <?php echo $description ?>
461                         </label> <?php echo $tip; ?>
462                     <?php
463 
464                     if ( ! isset( $value['checkboxgroup'] ) || 'end' == $value['checkboxgroup'] ) {
465                                     ?>
466                                     </fieldset>
467                                 </td>
468                             </tr>
469                         <?php
470                     } else {
471                         ?>
472                             </fieldset>
473                         <?php
474                     }
475                 break;
476 
477                 // Image width settings
478                 case 'image_width' :
479 
480                     $width  = self::get_option( $value['id'] . '[width]', $value['default']['width'] );
481                     $height = self::get_option( $value['id'] . '[height]', $value['default']['height'] );
482                     $crop   = checked( 1, self::get_option( $value['id'] . '[crop]', $value['default']['crop'] ), false );
483 
484                     ?><tr valign="top">
485                         <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?> <?php echo $tip; ?></th>
486                         <td class="forminp image_width_settings">
487 
488                             <input name="<?php echo esc_attr( $value['id'] ); ?>[width]" id="<?php echo esc_attr( $value['id'] ); ?>-width" type="text" size="3" value="<?php echo $width; ?>" /> &times; <input name="<?php echo esc_attr( $value['id'] ); ?>[height]" id="<?php echo esc_attr( $value['id'] ); ?>-height" type="text" size="3" value="<?php echo $height; ?>" />px
489 
490                             <label><input name="<?php echo esc_attr( $value['id'] ); ?>[crop]" id="<?php echo esc_attr( $value['id'] ); ?>-crop" type="checkbox" <?php echo $crop; ?> /> <?php _e( 'Hard Crop?', 'woocommerce' ); ?></label>
491 
492                             </td>
493                     </tr><?php
494                 break;
495 
496                 // Single page selects
497                 case 'single_select_page' :
498 
499                     $args = array( 'name'               => $value['id'],
500                                    'id'                 => $value['id'],
501                                    'sort_column'        => 'menu_order',
502                                    'sort_order'         => 'ASC',
503                                    'show_option_none'   => ' ',
504                                    'class'              => $value['class'],
505                                    'echo'               => false,
506                                    'selected'           => absint( self::get_option( $value['id'] ) )
507                                    );
508 
509                     if( isset( $value['args'] ) )
510                         $args = wp_parse_args( $value['args'], $args );
511 
512                     ?><tr valign="top" class="single_select_page">
513                         <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?> <?php echo $tip; ?></th>
514                         <td class="forminp">
515                             <?php echo str_replace(' id=', " data-placeholder='" . __( 'Select a page&hellip;', 'woocommerce' ) .  "' style='" . $value['css'] . "' class='" . $value['class'] . "' id=", wp_dropdown_pages( $args ) ); ?> <?php echo $description; ?>
516                         </td>
517                     </tr><?php
518                 break;
519 
520                 // Single country selects
521                 case 'single_select_country' :
522                     $country_setting = (string) self::get_option( $value['id'] );
523                     $countries       = WC()->countries->countries;
524 
525                     if ( strstr( $country_setting, ':' ) ) {
526                         $country_setting = explode( ':', $country_setting );
527                         $country         = current( $country_setting );
528                         $state           = end( $country_setting );
529                     } else {
530                         $country = $country_setting;
531                         $state   = '*';
532                     }
533                     ?><tr valign="top">
534                         <th scope="row" class="titledesc">
535                             <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
536                             <?php echo $tip; ?>
537                         </th>
538                         <td class="forminp"><select name="<?php echo esc_attr( $value['id'] ); ?>" style="<?php echo esc_attr( $value['css'] ); ?>" data-placeholder="<?php _e( 'Choose a country&hellip;', 'woocommerce' ); ?>" title="Country" class="chosen_select">
539                             <?php WC()->countries->country_dropdown_options( $country, $state ); ?>
540                         </select> <?php echo $description; ?>
541                         </td>
542                     </tr><?php
543                 break;
544 
545                 // Country multiselects
546                 case 'multi_select_countries' :
547 
548                     $selections = (array) self::get_option( $value['id'] );
549 
550                     if ( ! empty( $value['options'] ) )
551                         $countries = $value['options'];
552                     else
553                         $countries = WC()->countries->countries;
554 
555                     asort( $countries );
556                     ?><tr valign="top">
557                         <th scope="row" class="titledesc">
558                             <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
559                             <?php echo $tip; ?>
560                         </th>
561                         <td class="forminp">
562                             <select multiple="multiple" name="<?php echo esc_attr( $value['id'] ); ?>[]" style="width:350px" data-placeholder="<?php _e( 'Choose countries&hellip;', 'woocommerce' ); ?>" title="Country" class="chosen_select">
563                                 <?php
564                                     if ( $countries )
565                                         foreach ( $countries as $key => $val )
566                                             echo '<option value="' . esc_attr( $key ) . '" ' . selected( in_array( $key, $selections ), true, false ).'>' . $val . '</option>';
567                                 ?>
568                             </select> <?php if ( $description ) echo $description; ?> </br><a class="select_all button" href="#"><?php _e( 'Select all', 'woocommerce' ); ?></a> <a class="select_none button" href="#"><?php _e( 'Select none', 'woocommerce' ); ?></a>
569                         </td>
570                     </tr><?php
571                 break;
572 
573                 // Default: run an action
574                 default:
575                     do_action( 'woocommerce_admin_field_' . $value['type'], $value );
576                 break;
577             }
578         }
579     }
580 
581     /**
582      * Save admin fields.
583      *
584      * Loops though the woocommerce options array and outputs each field.
585      *
586      * @access public
587      * @param array $options Opens array to output
588      * @return bool
589      */
590     public static function save_fields( $options ) {
591         if ( empty( $_POST ) )
592             return false;
593 
594         // Options to update will be stored here
595         $update_options = array();
596 
597         // Loop options and get values to save
598         foreach ( $options as $value ) {
599 
600             if ( ! isset( $value['id'] ) )
601                 continue;
602 
603             $type = isset( $value['type'] ) ? sanitize_title( $value['type'] ) : '';
604 
605             // Get the option name
606             $option_value = null;
607 
608             switch ( $type ) {
609 
610                 // Standard types
611                 case "checkbox" :
612 
613                     if ( isset( $_POST[ $value['id'] ] ) ) {
614                         $option_value = 'yes';
615                     } else {
616                         $option_value = 'no';
617                     }
618 
619                 break;
620 
621                 case "textarea" :
622 
623                     if ( isset( $_POST[$value['id']] ) ) {
624                         $option_value = wp_kses_post( trim( stripslashes( $_POST[ $value['id'] ] ) ) );
625                     } else {
626                         $option_value = '';
627                     }
628 
629                 break;
630 
631                 case "text" :
632                 case 'email':
633                 case 'number':
634                 case "select" :
635                 case "color" :
636                 case 'password' :
637                 case "single_select_page" :
638                 case "single_select_country" :
639                 case 'radio' :
640 
641                     if ( $value['id'] == 'woocommerce_price_thousand_sep' || $value['id'] == 'woocommerce_price_decimal_sep' ) {
642 
643                         // price separators get a special treatment as they should allow a spaces (don't trim)
644                         if ( isset( $_POST[ $value['id'] ] )  ) {
645                             $option_value = wp_kses_post( stripslashes( $_POST[ $value['id'] ] ) );
646                         } else {
647                             $option_value = '';
648                         }
649 
650                     } elseif ( $value['id'] == 'woocommerce_price_num_decimals' ) {
651 
652                         // price separators get a special treatment as they should allow a spaces (don't trim)
653                         if ( isset( $_POST[ $value['id'] ] )  ) {
654                             $option_value = absint( $_POST[ $value['id'] ] );
655                         } else {
656                            $option_value = 2;
657                         }
658 
659                     } elseif ( $value['id'] == 'woocommerce_hold_stock_minutes' ) {
660 
661                         // Allow > 0 or set to ''
662                         if ( ! empty( $_POST[ $value['id'] ] )  ) {
663                             $option_value = absint( $_POST[ $value['id'] ] );
664                         } else {
665                             $option_value = '';
666                         }
667 
668                         wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
669 
670                         if ( $option_value != '' )
671                             wp_schedule_single_event( time() + ( absint( $option_value ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
672 
673                     } else {
674 
675                        if ( isset( $_POST[$value['id']] ) ) {
676                             $option_value = wc_clean( stripslashes( $_POST[ $value['id'] ] ) );
677                         } else {
678                             $option_value = '';
679                         }
680 
681                     }
682 
683                 break;
684 
685                 // Special types
686                 case "multiselect" :
687                 case "multi_select_countries" :
688 
689                     // Get countries array
690                     if ( isset( $_POST[ $value['id'] ] ) )
691                         $selected_countries = array_map( 'wc_clean', array_map( 'stripslashes', (array) $_POST[ $value['id'] ] ) );
692                     else
693                         $selected_countries = array();
694 
695                     $option_value = $selected_countries;
696 
697                 break;
698 
699                 case "image_width" :
700 
701                     if ( isset( $_POST[$value['id'] ]['width'] ) ) {
702 
703                         $update_options[ $value['id'] ]['width']  = wc_clean( stripslashes( $_POST[ $value['id'] ]['width'] ) );
704                         $update_options[ $value['id'] ]['height'] = wc_clean( stripslashes( $_POST[ $value['id'] ]['height'] ) );
705 
706                         if ( isset( $_POST[ $value['id'] ]['crop'] ) )
707                             $update_options[ $value['id'] ]['crop'] = 1;
708                         else
709                             $update_options[ $value['id'] ]['crop'] = 0;
710 
711                     } else {
712                         $update_options[ $value['id'] ]['width']    = $value['default']['width'];
713                         $update_options[ $value['id'] ]['height']   = $value['default']['height'];
714                         $update_options[ $value['id'] ]['crop']     = $value['default']['crop'];
715                     }
716 
717                 break;
718 
719                 // Custom handling
720                 default :
721 
722                     do_action( 'woocommerce_update_option_' . $type, $value );
723 
724                 break;
725 
726             }
727 
728             if ( ! is_null( $option_value ) ) {
729                 // Check if option is an array
730                 if ( strstr( $value['id'], '[' ) ) {
731 
732                     parse_str( $value['id'], $option_array );
733 
734                     // Option name is first key
735                     $option_name = current( array_keys( $option_array ) );
736 
737                     // Get old option value
738                     if ( ! isset( $update_options[ $option_name ] ) )
739                          $update_options[ $option_name ] = get_option( $option_name, array() );
740 
741                     if ( ! is_array( $update_options[ $option_name ] ) )
742                         $update_options[ $option_name ] = array();
743 
744                     // Set keys and value
745                     $key = key( $option_array[ $option_name ] );
746 
747                     $update_options[ $option_name ][ $key ] = $option_value;
748 
749                 // Single value
750                 } else {
751                     $update_options[ $value['id'] ] = $option_value;
752                 }
753             }
754 
755             // Custom handling
756             do_action( 'woocommerce_update_option', $value );
757         }
758 
759         // Now save the options
760         foreach( $update_options as $name => $value )
761             update_option( $name, $value );
762 
763         return true;
764     }
765 
766     /**
767      * Checks which method we're using to serve downloads
768      *
769      * If using force or x-sendfile, this ensures the .htaccess is in place
770      *
771      * @access public
772      * @return void
773      */
774     public static function check_download_folder_protection() {
775         $upload_dir         = wp_upload_dir();
776         $downloads_url      = $upload_dir['basedir'] . '/woocommerce_uploads';
777         $download_method    = get_option('woocommerce_file_download_method');
778 
779         if ( $download_method == 'redirect' ) {
780 
781             // Redirect method - don't protect
782             if ( file_exists( $downloads_url . '/.htaccess' ) )
783                 unlink( $downloads_url . '/.htaccess' );
784 
785         } else {
786 
787             // Force method - protect, add rules to the htaccess file
788             if ( ! file_exists( $downloads_url . '/.htaccess' ) ) {
789                 if ( $file_handle = @fopen( $downloads_url . '/.htaccess', 'w' ) ) {
790                     fwrite( $file_handle, 'deny from all' );
791                     fclose( $file_handle );
792                 }
793             }
794         }
795     }
796 }
797 
798 endif;
799 
WooCommerce API documentation generated by ApiGen 2.8.0