class-wc-admin-settings.php 31 KB

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