PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-settings.php

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