WC_Admin_Settings

WC_Admin_Settings Class.

Defined (1)

The class is defined in the following location(s).

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