PageRenderTime 67ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/woocommerce-admin-settings.php

https://github.com/alexcsandru/woocommerce
PHP | 921 lines | 644 code | 189 blank | 88 comment | 138 complexity | 1f346f937740291129a8581ed4c0ac7d MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Functions for the settings page in admin.
  4. *
  5. * The settings page contains options for the WooCommerce plugin - this file contains functions to display
  6. * and save the list of options.
  7. *
  8. * @author WooThemes
  9. * @category Admin
  10. * @package WooCommerce/Admin/Settings
  11. * @version 1.6.4
  12. */
  13. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  14. /** Store settings in this array */
  15. global $woocommerce_settings;
  16. /** Settings init */
  17. include( 'settings/settings-init.php' );
  18. if ( ! function_exists( 'woocommerce_settings' ) ) {
  19. /**
  20. * Settings page.
  21. *
  22. * Handles the display of the main woocommerce settings page in admin.
  23. *
  24. * @access public
  25. * @return void
  26. */
  27. function woocommerce_settings() {
  28. global $woocommerce, $woocommerce_settings, $current_section, $current_tab;
  29. do_action( 'woocommerce_settings_start' );
  30. // Get current tab/section
  31. $current_tab = ( empty( $_GET['tab'] ) ) ? 'general' : sanitize_text_field( urldecode( $_GET['tab'] ) );
  32. $current_section = ( empty( $_REQUEST['section'] ) ) ? '' : sanitize_text_field( urldecode( $_REQUEST['section'] ) );
  33. // Save settings
  34. if ( ! empty( $_POST ) ) {
  35. if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) )
  36. die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
  37. if ( ! $current_section ) {
  38. $old_base_color = get_option('woocommerce_frontend_css_base_color');
  39. $old_base_color = get_option('woocommerce_frontend_css_base_color');
  40. include_once( 'settings/settings-save.php' );
  41. switch ( $current_tab ) {
  42. case "payment_gateways" :
  43. woocommerce_update_options( $woocommerce_settings[ $current_tab ] );
  44. $woocommerce->payment_gateways->process_admin_options();
  45. break;
  46. case "shipping" :
  47. woocommerce_update_options( $woocommerce_settings[ $current_tab ] );
  48. $woocommerce->shipping->process_admin_options();
  49. break;
  50. default :
  51. if ( isset( $woocommerce_settings[ $current_tab ] ) )
  52. woocommerce_update_options( $woocommerce_settings[ $current_tab ] );
  53. // Trigger action for tab
  54. do_action( 'woocommerce_update_options_' . $current_tab );
  55. break;
  56. }
  57. do_action( 'woocommerce_update_options' );
  58. // Handle Colour Settings
  59. if ( $current_tab == 'general' && get_option('woocommerce_frontend_css') == 'yes' ) {
  60. // Save settings
  61. $primary = ( ! empty( $_POST['woocommerce_frontend_css_primary'] ) ) ? woocommerce_format_hex( $_POST['woocommerce_frontend_css_primary'] ) : '';
  62. $secondary = ( ! empty( $_POST['woocommerce_frontend_css_secondary'] ) ) ? woocommerce_format_hex( $_POST['woocommerce_frontend_css_secondary'] ) : '';
  63. $highlight = ( ! empty( $_POST['woocommerce_frontend_css_highlight'] ) ) ? woocommerce_format_hex( $_POST['woocommerce_frontend_css_highlight'] ) : '';
  64. $content_bg = ( ! empty( $_POST['woocommerce_frontend_css_content_bg'] ) ) ? woocommerce_format_hex( $_POST['woocommerce_frontend_css_content_bg'] ) : '';
  65. $subtext = ( ! empty( $_POST['woocommerce_frontend_css_subtext'] ) ) ? woocommerce_format_hex( $_POST['woocommerce_frontend_css_subtext'] ) : '';
  66. $colors = array(
  67. 'primary' => $primary,
  68. 'secondary' => $secondary,
  69. 'highlight' => $highlight,
  70. 'content_bg' => $content_bg,
  71. 'subtext' => $subtext
  72. );
  73. $old_colors = get_option( 'woocommerce_frontend_css_colors' );
  74. update_option( 'woocommerce_frontend_css_colors', $colors );
  75. if ( $old_colors != $colors )
  76. woocommerce_compile_less_styles();
  77. }
  78. } else {
  79. // If saving a shipping methods options, load 'er up
  80. if ( ( $current_tab == 'shipping' || $current_tab == 'payment_gateways' && class_exists( $current_section ) ) ) {
  81. $current_section_class = new $current_section();
  82. do_action( 'woocommerce_update_options_' . $current_tab . '_' . $current_section_class->id );
  83. // If saving an email's options, load theme
  84. } elseif ( $current_tab == 'email' ) {
  85. // Load mailer
  86. $mailer = $woocommerce->mailer();
  87. if ( class_exists( $current_section ) ) {
  88. $current_section_class = new $current_section();
  89. do_action( 'woocommerce_update_options_' . $current_tab . '_' . $current_section_class->id );
  90. } else {
  91. do_action( 'woocommerce_update_options_' . $current_tab . '_' . $current_section );
  92. }
  93. // Save tax
  94. } elseif ( $current_tab == 'tax' ) {
  95. include_once('settings/settings-tax-rates.php');
  96. woocommerce_tax_rates_setting_save();
  97. } else {
  98. // Save section only
  99. do_action( 'woocommerce_update_options_' . $current_tab . '_' . $current_section );
  100. }
  101. }
  102. // Clear any unwanted data
  103. $woocommerce->clear_product_transients();
  104. // Redirect back to the settings page
  105. $redirect = add_query_arg( 'saved', 'true' );
  106. if ( ! empty( $_POST['subtab'] ) ) $redirect = add_query_arg( 'subtab', esc_attr( str_replace( '#', '', $_POST['subtab'] ) ), $redirect );
  107. wp_safe_redirect( $redirect );
  108. exit;
  109. }
  110. // Get any returned messages
  111. $error = ( empty( $_GET['wc_error'] ) ) ? '' : urldecode( stripslashes( $_GET['wc_error'] ) );
  112. $message = ( empty( $_GET['wc_message'] ) ) ? '' : urldecode( stripslashes( $_GET['wc_message'] ) );
  113. if ( $error || $message ) {
  114. if ( $error ) {
  115. echo '<div id="message" class="error fade"><p><strong>' . esc_html( $error ) . '</strong></p></div>';
  116. } else {
  117. echo '<div id="message" class="updated fade"><p><strong>' . esc_html( $message ) . '</strong></p></div>';
  118. }
  119. } elseif ( ! empty( $_GET['saved'] ) ) {
  120. echo '<div id="message" class="updated fade"><p><strong>' . __( 'Your settings have been saved.', 'woocommerce' ) . '</strong></p></div>';
  121. }
  122. // Were the settings saved?
  123. if ( ! empty( $_GET['saved'] ) ) {
  124. do_action('woocommerce_settings_saved');
  125. }
  126. // Hide WC Link
  127. if (isset($_GET['hide-wc-extensions-message']))
  128. update_option('hide-wc-extensions-message', 1);
  129. // Install/page installer
  130. $install_complete = false;
  131. // Add pages button
  132. if (isset($_GET['install_woocommerce_pages']) && $_GET['install_woocommerce_pages']) {
  133. require_once( 'woocommerce-admin-install.php' );
  134. woocommerce_create_pages();
  135. update_option('skip_install_woocommerce_pages', 1);
  136. $install_complete = true;
  137. // Skip button
  138. } elseif (isset($_GET['skip_install_woocommerce_pages']) && $_GET['skip_install_woocommerce_pages']) {
  139. update_option('skip_install_woocommerce_pages', 1);
  140. $install_complete = true;
  141. }
  142. if ($install_complete) {
  143. ?>
  144. <div id="message" class="updated woocommerce-message wc-connect">
  145. <div class="squeezer">
  146. <h4><?php _e( '<strong>Congratulations!</strong> &#8211; WooCommerce has been installed and setup. Enjoy :)', 'woocommerce' ); ?></h4>
  147. <p><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.woothemes.com/woocommerce/" data-text="A open-source (free) #ecommerce plugin for #WordPress that helps you sell anything. Beautifully." data-via="WooThemes" data-size="large" data-hashtags="WooCommerce">Tweet</a>
  148. <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script></p>
  149. </div>
  150. </div>
  151. <?php
  152. // Flush rules after install
  153. flush_rewrite_rules( false );
  154. // Set installed option
  155. update_option('woocommerce_installed', 0);
  156. }
  157. ?>
  158. <div class="wrap woocommerce">
  159. <form method="post" id="mainform" action="" enctype="multipart/form-data">
  160. <div class="icon32 icon32-woocommerce-settings" id="icon-woocommerce"><br /></div><h2 class="nav-tab-wrapper woo-nav-tab-wrapper">
  161. <?php
  162. $tabs = array(
  163. 'general' => __( 'General', 'woocommerce' ),
  164. 'catalog' => __( 'Catalog', 'woocommerce' ),
  165. 'pages' => __( 'Pages', 'woocommerce' ),
  166. 'inventory' => __( 'Inventory', 'woocommerce' ),
  167. 'tax' => __( 'Tax', 'woocommerce'),
  168. 'shipping' => __( 'Shipping', 'woocommerce' ),
  169. 'payment_gateways' => __( 'Payment Gateways', 'woocommerce' ),
  170. 'email' => __( 'Emails', 'woocommerce' ),
  171. 'integration' => __( 'Integration', 'woocommerce' )
  172. );
  173. if ( ! $woocommerce->integrations->get_integrations() )
  174. unset( $tabs['integration'] );
  175. $tabs = apply_filters('woocommerce_settings_tabs_array', $tabs);
  176. foreach ( $tabs as $name => $label ) {
  177. echo '<a href="' . admin_url( 'admin.php?page=woocommerce_settings&tab=' . $name ) . '" class="nav-tab ';
  178. if( $current_tab == $name ) echo 'nav-tab-active';
  179. echo '">' . $label . '</a>';
  180. }
  181. do_action( 'woocommerce_settings_tabs' );
  182. ?>
  183. </h2>
  184. <?php wp_nonce_field( 'woocommerce-settings', '_wpnonce', true, true ); ?>
  185. <?php if ( ! get_option('hide-wc-extensions-message') ) : ?>
  186. <div id="woocommerce_extensions"><a href="<?php echo add_query_arg('hide-wc-extensions-message', 'true') ?>" class="hide">&times;</a><?php printf(__( 'More functionality and gateway options available via <a href="%s" target="_blank">WC official extensions</a>.', 'woocommerce' ), 'http://www.woothemes.com/extensions/woocommerce-extensions/'); ?></div>
  187. <?php endif; ?>
  188. <?php
  189. switch ($current_tab) :
  190. case "general" :
  191. include_once('settings/settings-frontend-styles.php');
  192. woocommerce_admin_fields( $woocommerce_settings[$current_tab] );
  193. break;
  194. case "tax" :
  195. $links = array(
  196. '<a href="' . admin_url( 'admin.php?page=woocommerce_settings&tab=tax' ) . '" class="' . ( $current_section == '' ? 'current' : '' ) . '">' . __( 'Tax Options', 'woocommerce' ) . '</a>'
  197. );
  198. // Get tax classes and display as links
  199. $tax_classes = array_filter( array_map( 'trim', explode( "\n", get_option('woocommerce_tax_classes' ) ) ) );
  200. $links[] = __( 'Tax Rates', 'woocommerce' ) . ': <a href="' . admin_url( 'admin.php?page=woocommerce_settings&tab=tax&section=standard' ) . '" class="' . ( $current_section == 'standard' ? 'current' : '' ) . '">' . __( 'Standard', 'woocommerce' ) . '</a>';
  201. if ( $tax_classes )
  202. foreach ( $tax_classes as $class )
  203. $links[] = '<a href="' . admin_url( 'admin.php?page=woocommerce_settings&tab=tax&section=' . sanitize_title( $class ) ) . '" class="' . ( $current_section == sanitize_title( $class ) ? 'current' : '' ) . '">' . $class . '</a>';
  204. echo '<ul class="subsubsub"><li>' . implode( ' | </li><li>', $links ) . '</li></ul><br class="clear" />';
  205. if ( $current_section == 'standard' || in_array( $current_section, array_map( 'sanitize_title', $tax_classes ) ) ) {
  206. include_once('settings/settings-tax-rates.php');
  207. woocommerce_tax_rates_setting();
  208. } else {
  209. woocommerce_admin_fields( $woocommerce_settings[ $current_tab ] );
  210. }
  211. break;
  212. case "pages" :
  213. case "catalog" :
  214. case "inventory" :
  215. woocommerce_admin_fields( $woocommerce_settings[$current_tab] );
  216. break;
  217. case "email" :
  218. $current = $current_section ? '' : 'class="current"';
  219. $links = array( '<a href="' . admin_url( 'admin.php?page=woocommerce_settings&tab=email' ) . '" ' . $current . '>' . __( 'Email Options', 'woocommerce' ) . '</a>' );
  220. // Define emails that can be customised here
  221. $mailer = $woocommerce->mailer();
  222. $email_templates = $mailer->get_emails();
  223. foreach ( $email_templates as $email ) {
  224. $title = empty( $email->title ) ? ucwords( $email->id ) : ucwords( $email->title );
  225. $current = ( get_class( $email ) == $current_section ) ? 'class="current"' : '';
  226. $links[] = '<a href="' . add_query_arg( 'section', get_class( $email ), admin_url('admin.php?page=woocommerce_settings&tab=email') ) . '"' . $current . '>' . esc_html( $title ) . '</a>';
  227. }
  228. echo '<ul class="subsubsub"><li>' . implode( ' | </li><li>', $links ) . '</li></ul><br class="clear" />';
  229. // Specific email options
  230. if ( $current_section ) {
  231. foreach ( $email_templates as $email ) {
  232. if ( get_class( $email ) == $current_section ) {
  233. $email->admin_options();
  234. break;
  235. }
  236. }
  237. } else {
  238. woocommerce_admin_fields( $woocommerce_settings[ $current_tab ] );
  239. }
  240. break;
  241. case "shipping" :
  242. include('settings/settings-shipping-methods.php');
  243. $current = $current_section ? '' : 'class="current"';
  244. $links = array( '<a href="' . admin_url('admin.php?page=woocommerce_settings&tab=shipping') . '" ' . $current . '>' . __( 'Shipping Options', 'woocommerce' ) . '</a>' );
  245. // Load shipping methods so we can show any global options they may have
  246. $shipping_methods = $woocommerce->shipping->load_shipping_methods();
  247. foreach ( $shipping_methods as $method ) {
  248. if ( ! $method->has_settings() ) continue;
  249. $title = empty( $method->method_title ) ? ucwords( $method->id ) : ucwords( $method->method_title );
  250. $current = ( get_class( $method ) == $current_section ) ? 'class="current"' : '';
  251. $links[] = '<a href="' . add_query_arg( 'section', get_class( $method ), admin_url('admin.php?page=woocommerce_settings&tab=shipping') ) . '"' . $current . '>' . esc_html( $title ) . '</a>';
  252. }
  253. echo '<ul class="subsubsub"><li>' . implode( ' | </li><li>', $links ) . '</li></ul><br class="clear" />';
  254. // Specific method options
  255. if ( $current_section ) {
  256. foreach ( $shipping_methods as $method ) {
  257. if ( get_class( $method ) == $current_section && $method->has_settings() ) {
  258. $method->admin_options();
  259. break;
  260. }
  261. }
  262. } else {
  263. woocommerce_admin_fields( $woocommerce_settings[ $current_tab ] );
  264. }
  265. break;
  266. case "payment_gateways" :
  267. include('settings/settings-payment-gateways.php');
  268. $current = $current_section ? '' : 'class="current"';
  269. $links = array( '<a href="' . admin_url('admin.php?page=woocommerce_settings&tab=payment_gateways') . '" ' . $current . '>' . __( 'Payment Gateways', 'woocommerce' ) . '</a>' );
  270. // Load shipping methods so we can show any global options they may have
  271. $payment_gateways = $woocommerce->payment_gateways->payment_gateways();
  272. foreach ( $payment_gateways as $gateway ) {
  273. $title = empty( $gateway->method_title ) ? ucwords( $gateway->id ) : ucwords( $gateway->method_title );
  274. $current = ( get_class( $gateway ) == $current_section ) ? 'class="current"' : '';
  275. $links[] = '<a href="' . add_query_arg( 'section', get_class( $gateway ), admin_url('admin.php?page=woocommerce_settings&tab=payment_gateways') ) . '"' . $current . '>' . esc_html( $title ) . '</a>';
  276. }
  277. echo '<ul class="subsubsub"><li>' . implode( ' | </li><li>', $links ) . '</li></ul><br class="clear" />';
  278. // Specific method options
  279. if ( $current_section ) {
  280. foreach ( $payment_gateways as $gateway ) {
  281. if ( get_class( $gateway ) == $current_section ) {
  282. $gateway->admin_options();
  283. break;
  284. }
  285. }
  286. } else {
  287. woocommerce_admin_fields( $woocommerce_settings[ $current_tab ] );
  288. }
  289. break;
  290. case "integration" :
  291. $integrations = $woocommerce->integrations->get_integrations();
  292. $current_section = empty( $current_section ) ? key( $integrations ) : $current_section;
  293. $links = array();
  294. foreach ( $integrations as $integration ) {
  295. $title = empty( $integration->method_title ) ? ucwords( $integration->id ) : ucwords( $integration->method_title );
  296. $current = ( $integration->id == $current_section ) ? 'class="current"' : '';
  297. $links[] = '<a href="' . add_query_arg( 'section', $integration->id, admin_url('admin.php?page=woocommerce_settings&tab=integration') ) . '"' . $current . '>' . esc_html( $title ) . '</a>';
  298. }
  299. echo '<ul class="subsubsub"><li>' . implode( ' | </li><li>', $links ) . '</li></ul><br class="clear" />';
  300. if ( isset( $integrations[ $current_section ] ) )
  301. $integrations[ $current_section ]->admin_options();
  302. break;
  303. default :
  304. do_action( 'woocommerce_settings_tabs_' . $current_tab );
  305. break;
  306. endswitch;
  307. ?>
  308. <p class="submit">
  309. <?php if ( ! isset( $GLOBALS['hide_save_button'] ) ) : ?>
  310. <input name="save" class="button-primary" type="submit" value="<?php _e( 'Save changes', 'woocommerce' ); ?>" />
  311. <?php endif; ?>
  312. <input type="hidden" name="subtab" id="last_tab" />
  313. </p>
  314. </form>
  315. <script type="text/javascript">
  316. jQuery(window).load(function(){
  317. // Countries
  318. jQuery('select#woocommerce_allowed_countries').change(function(){
  319. if (jQuery(this).val()=="specific") {
  320. jQuery(this).parent().parent().next('tr').show();
  321. } else {
  322. jQuery(this).parent().parent().next('tr').hide();
  323. }
  324. }).change();
  325. // Color picker
  326. jQuery('.colorpick').each(function(){
  327. jQuery('.colorpickdiv', jQuery(this).parent()).farbtastic(this);
  328. jQuery(this).click(function() {
  329. if ( jQuery(this).val() == "" ) jQuery(this).val('#');
  330. jQuery('.colorpickdiv', jQuery(this).parent() ).show();
  331. });
  332. });
  333. jQuery(document).mousedown(function(){
  334. jQuery('.colorpickdiv').hide();
  335. });
  336. // Edit prompt
  337. jQuery(function(){
  338. var changed = false;
  339. jQuery('input, textarea, select, checkbox').change(function(){
  340. changed = true;
  341. });
  342. jQuery('.woo-nav-tab-wrapper a').click(function(){
  343. if (changed) {
  344. window.onbeforeunload = function() {
  345. return '<?php echo __( 'The changes you made will be lost if you navigate away from this page.', 'woocommerce' ); ?>';
  346. }
  347. } else {
  348. window.onbeforeunload = '';
  349. }
  350. });
  351. jQuery('.submit input').click(function(){
  352. window.onbeforeunload = '';
  353. });
  354. });
  355. // Sorting
  356. jQuery('table.wc_gateways tbody, table.wc_shipping tbody').sortable({
  357. items:'tr',
  358. cursor:'move',
  359. axis:'y',
  360. handle: 'td',
  361. scrollSensitivity:40,
  362. helper:function(e,ui){
  363. ui.children().each(function(){
  364. jQuery(this).width(jQuery(this).width());
  365. });
  366. ui.css('left', '0');
  367. return ui;
  368. },
  369. start:function(event,ui){
  370. ui.item.css('background-color','#f6f6f6');
  371. },
  372. stop:function(event,ui){
  373. ui.item.removeAttr('style');
  374. }
  375. });
  376. // Chosen selects
  377. jQuery("select.chosen_select").chosen();
  378. jQuery("select.chosen_select_nostd").chosen({
  379. allow_single_deselect: 'true'
  380. });
  381. });
  382. </script>
  383. </div>
  384. <?php
  385. }
  386. }
  387. /**
  388. * Get a setting from the settings API.
  389. *
  390. * @access public
  391. * @param mixed $option
  392. * @return void
  393. */
  394. function woocommerce_settings_get_option( $option_name, $default = '' ) {
  395. // Array value
  396. if ( strstr( $option_name, '[' ) ) {
  397. parse_str( $option_name, $option_array );
  398. // Option name is first key
  399. $option_name = current( array_keys( $option_array ) );
  400. // Get value
  401. $option_values = get_option( $option_name, '' );
  402. $key = key( $option_array[ $option_name ] );
  403. if ( isset( $option_values[ $key ] ) )
  404. $option_value = $option_values[ $key ];
  405. else
  406. $option_value = null;
  407. // Single value
  408. } else {
  409. $option_value = get_option( $option_name, null );
  410. }
  411. if ( is_array( $option_value ) )
  412. $option_value = array_map( 'stripslashes', $option_value );
  413. elseif ( ! is_null( $option_value ) )
  414. $option_value = stripslashes( $option_value );
  415. return $option_value === null ? $default : $option_value;
  416. }
  417. /**
  418. * Output admin fields.
  419. *
  420. * Loops though the woocommerce options array and outputs each field.
  421. *
  422. * @access public
  423. * @param array $options Opens array to output
  424. * @return void
  425. */
  426. function woocommerce_admin_fields( $options ) {
  427. global $woocommerce;
  428. foreach ( $options as $value ) {
  429. if ( ! isset( $value['type'] ) ) continue;
  430. if ( ! isset( $value['id'] ) ) $value['id'] = '';
  431. if ( ! isset( $value['title'] ) ) $value['title'] = isset( $value['name'] ) ? $value['name'] : '';
  432. if ( ! isset( $value['class'] ) ) $value['class'] = '';
  433. if ( ! isset( $value['css'] ) ) $value['css'] = '';
  434. if ( ! isset( $value['default'] ) ) $value['default'] = '';
  435. if ( ! isset( $value['desc'] ) ) $value['desc'] = '';
  436. if ( ! isset( $value['desc_tip'] ) ) $value['desc_tip'] = false;
  437. // Custom attribute handling
  438. $custom_attributes = array();
  439. if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) )
  440. foreach ( $value['custom_attributes'] as $attribute => $attribute_value )
  441. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
  442. // Description handling
  443. if ( $value['desc_tip'] === true ) {
  444. $description = '';
  445. $tip = $value['desc'];
  446. } elseif ( ! empty( $value['desc_tip'] ) ) {
  447. $description = $value['desc'];
  448. $tip = $value['desc_tip'];
  449. } elseif ( ! empty( $value['desc'] ) ) {
  450. $description = $value['desc'];
  451. $tip = '';
  452. } else {
  453. $description = $tip = '';
  454. }
  455. if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ) ) ) {
  456. $description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>';
  457. } elseif ( $description ) {
  458. $description = '<span class="description">' . wp_kses_post( $description ) . '</span>';
  459. }
  460. if ( $tip && in_array( $value['type'], array( 'checkbox' ) ) ) {
  461. $tip = '<p class="description">' . $tip . '</p>';
  462. } elseif ( $tip ) {
  463. $tip = '<img class="help_tip" data-tip="' . esc_attr( $tip ) . '" src="' . $woocommerce->plugin_url() . '/assets/images/help.png" height="16" width="16" />';
  464. }
  465. // Switch based on type
  466. switch( $value['type'] ) {
  467. // Section Titles
  468. case 'title':
  469. if ( ! empty( $value['title'] ) ) echo '<h3>' . esc_html( $value['title'] ) . '</h3>';
  470. if ( ! empty( $value['desc'] ) ) echo wpautop( wptexturize( wp_kses_post( $value['desc'] ) ) );
  471. echo '<table class="form-table">'. "\n\n";
  472. if ( ! empty( $value['id'] ) ) do_action( 'woocommerce_settings_' . sanitize_title( $value['id'] ) );
  473. break;
  474. // Section Ends
  475. case 'sectionend':
  476. if ( ! empty( $value['id'] ) ) do_action( 'woocommerce_settings_' . sanitize_title( $value['id'] ) . '_end' );
  477. echo '</table>';
  478. if ( ! empty( $value['id'] ) ) do_action( 'woocommerce_settings_' . sanitize_title( $value['id'] ) . '_after' );
  479. break;
  480. // Standard text inputs and subtypes like 'number'
  481. case 'text':
  482. case 'email':
  483. case 'number':
  484. case 'color' :
  485. case 'password' :
  486. $type = $value['type'];
  487. $class = '';
  488. $option_value = woocommerce_settings_get_option( $value['id'], $value['default'] );
  489. if ( $value['type'] == 'color' ) {
  490. $type = 'text';
  491. $value['class'] .= 'colorpick';
  492. $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>';
  493. }
  494. ?><tr valign="top">
  495. <th scope="row" class="titledesc">
  496. <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
  497. <?php echo $tip; ?>
  498. </th>
  499. <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
  500. <input
  501. name="<?php echo esc_attr( $value['id'] ); ?>"
  502. id="<?php echo esc_attr( $value['id'] ); ?>"
  503. type="<?php echo esc_attr( $type ); ?>"
  504. style="<?php echo esc_attr( $value['css'] ); ?>"
  505. value="<?php echo esc_attr( $option_value ); ?>"
  506. class="<?php echo esc_attr( $value['class'] ); ?>"
  507. <?php echo implode( ' ', $custom_attributes ); ?>
  508. /> <?php echo $description; ?>
  509. </td>
  510. </tr><?php
  511. break;
  512. // Textarea
  513. case 'textarea':
  514. $option_value = woocommerce_settings_get_option( $value['id'], $value['default'] );
  515. ?><tr valign="top">
  516. <th scope="row" class="titledesc">
  517. <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
  518. <?php echo $tip; ?>
  519. </th>
  520. <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
  521. <?php echo $description; ?>
  522. <textarea
  523. name="<?php echo esc_attr( $value['id'] ); ?>"
  524. id="<?php echo esc_attr( $value['id'] ); ?>"
  525. style="<?php echo esc_attr( $value['css'] ); ?>"
  526. class="<?php echo esc_attr( $value['class'] ); ?>"
  527. <?php echo implode( ' ', $custom_attributes ); ?>
  528. ><?php echo esc_textarea( $option_value ); ?></textarea>
  529. </td>
  530. </tr><?php
  531. break;
  532. // Select boxes
  533. case 'select' :
  534. case 'multiselect' :
  535. $option_value = woocommerce_settings_get_option( $value['id'], $value['default'] );
  536. ?><tr valign="top">
  537. <th scope="row" class="titledesc">
  538. <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
  539. <?php echo $tip; ?>
  540. </th>
  541. <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
  542. <select
  543. name="<?php echo esc_attr( $value['id'] ); ?><?php if ( $value['type'] == 'multiselect' ) echo '[]'; ?>"
  544. id="<?php echo esc_attr( $value['id'] ); ?>"
  545. style="<?php echo esc_attr( $value['css'] ); ?>"
  546. class="<?php echo esc_attr( $value['class'] ); ?>"
  547. <?php echo implode( ' ', $custom_attributes ); ?>
  548. <?php if ( $value['type'] == 'multiselect' ) echo 'multiple="multiple"'; ?>
  549. >
  550. <?php
  551. foreach ( $value['options'] as $key => $val ) {
  552. ?>
  553. <option value="<?php echo esc_attr( $key ); ?>" <?php
  554. if ( is_array( $option_value ) )
  555. selected( in_array( $key, $option_value ), true );
  556. else
  557. selected( $option_value, $key );
  558. ?>><?php echo $val ?></option>
  559. <?php
  560. }
  561. ?>
  562. </select> <?php echo $description; ?>
  563. </td>
  564. </tr><?php
  565. break;
  566. // Radio inputs
  567. case 'radio' :
  568. $option_value = woocommerce_settings_get_option( $value['id'], $value['default'] );
  569. ?><tr valign="top">
  570. <th scope="row" class="titledesc">
  571. <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
  572. <?php echo $tip; ?>
  573. </th>
  574. <td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
  575. <fieldset>
  576. <?php echo $description; ?>
  577. <ul>
  578. <?php
  579. foreach ( $value['options'] as $key => $val ) {
  580. ?>
  581. <li>
  582. <label><input
  583. name="<?php echo esc_attr( $value['id'] ); ?>"
  584. value="<?php echo $key; ?>"
  585. type="radio"
  586. style="<?php echo esc_attr( $value['css'] ); ?>"
  587. class="<?php echo esc_attr( $value['class'] ); ?>"
  588. <?php echo implode( ' ', $custom_attributes ); ?>
  589. <?php checked( $key, $option_value ); ?>
  590. /> <?php echo $val ?></label>
  591. </li>
  592. <?php
  593. }
  594. ?>
  595. </ul>
  596. </fieldset>
  597. </td>
  598. </tr><?php
  599. break;
  600. // Checkbox input
  601. case 'checkbox' :
  602. $option_value = woocommerce_settings_get_option( $value['id'], $value['default'] );
  603. if ( ! isset( $value['hide_if_checked'] ) ) $value['hide_if_checked'] = false;
  604. if ( ! isset( $value['show_if_checked'] ) ) $value['show_if_checked'] = false;
  605. if ( ! isset( $value['checkboxgroup'] ) || ( isset( $value['checkboxgroup'] ) && $value['checkboxgroup'] == 'start' ) ) {
  606. ?>
  607. <tr valign="top" class="<?php
  608. if ( $value['hide_if_checked'] == 'yes' || $value['show_if_checked']=='yes') echo 'hidden_option';
  609. if ( $value['hide_if_checked'] == 'option' ) echo 'hide_options_if_checked';
  610. if ( $value['show_if_checked'] == 'option' ) echo 'show_options_if_checked';
  611. ?>">
  612. <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?></th>
  613. <td class="forminp forminp-checkbox">
  614. <fieldset>
  615. <?php
  616. } else {
  617. ?>
  618. <fieldset class="<?php
  619. if ( $value['hide_if_checked'] == 'yes' || $value['show_if_checked'] == 'yes') echo 'hidden_option';
  620. if ( $value['hide_if_checked'] == 'option') echo 'hide_options_if_checked';
  621. if ( $value['show_if_checked'] == 'option') echo 'show_options_if_checked';
  622. ?>">
  623. <?php
  624. }
  625. ?>
  626. <legend class="screen-reader-text"><span><?php echo esc_html( $value['title'] ) ?></span></legend>
  627. <label for="<?php echo $value['id'] ?>">
  628. <input
  629. name="<?php echo esc_attr( $value['id'] ); ?>"
  630. id="<?php echo esc_attr( $value['id'] ); ?>"
  631. type="checkbox"
  632. value="1"
  633. <?php checked( $option_value, 'yes'); ?>
  634. <?php echo implode( ' ', $custom_attributes ); ?>
  635. /> <?php echo wp_kses_post( $value['desc'] ) ?></label> <?php echo $tip; ?>
  636. <?php
  637. if ( ! isset( $value['checkboxgroup'] ) || ( isset( $value['checkboxgroup'] ) && $value['checkboxgroup'] == 'end' ) ) {
  638. ?>
  639. </fieldset>
  640. </td>
  641. </tr>
  642. <?php
  643. } else {
  644. ?>
  645. </fieldset>
  646. <?php
  647. }
  648. break;
  649. // Image width settings
  650. case 'image_width' :
  651. $width = woocommerce_settings_get_option( $value['id'] . '[width]', $value['default']['width'] );
  652. $height = woocommerce_settings_get_option( $value['id'] . '[height]', $value['default']['height'] );
  653. $crop = checked( 1, woocommerce_settings_get_option( $value['id'] . '[crop]', $value['default']['crop'] ), false );
  654. ?><tr valign="top">
  655. <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?></th>
  656. <td class="forminp">
  657. <?php _e( 'Width', 'woocommerce' ); ?> <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; ?>" />
  658. <?php _e( 'Height', 'woocommerce' ); ?> <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; ?>" />
  659. <label><?php _e( 'Hard Crop', 'woocommerce' ); ?> <input name="<?php echo esc_attr( $value['id'] ); ?>[crop]" id="<?php echo esc_attr( $value['id'] ); ?>-crop" type="checkbox" <?php echo $crop; ?> /></label>
  660. <?php echo $description; ?></td>
  661. </tr><?php
  662. break;
  663. // Single page selects
  664. case 'single_select_page' :
  665. $args = array( 'name' => $value['id'],
  666. 'id' => $value['id'],
  667. 'sort_column' => 'menu_order',
  668. 'sort_order' => 'ASC',
  669. 'show_option_none' => ' ',
  670. 'class' => $value['class'],
  671. 'echo' => false,
  672. 'selected' => absint( woocommerce_settings_get_option( $value['id'] ) )
  673. );
  674. if( isset( $value['args'] ) )
  675. $args = wp_parse_args( $value['args'], $args );
  676. ?><tr valign="top" class="single_select_page">
  677. <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?> <?php echo $tip; ?></th>
  678. <td class="forminp">
  679. <?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; ?>
  680. </td>
  681. </tr><?php
  682. break;
  683. // Single country selects
  684. case 'single_select_country' :
  685. $country_setting = (string) woocommerce_settings_get_option( $value['id'] );
  686. $countries = $woocommerce->countries->countries;
  687. if (strstr($country_setting, ':')) :
  688. $country = current(explode(':', $country_setting));
  689. $state = end(explode(':', $country_setting));
  690. else :
  691. $country = $country_setting;
  692. $state = '*';
  693. endif;
  694. ?><tr valign="top">
  695. <th scope="row" class="titledesc">
  696. <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
  697. <?php echo $tip; ?>
  698. </th>
  699. <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">
  700. <?php echo $woocommerce->countries->country_dropdown_options( $country, $state ); ?>
  701. </select> <?php echo $description; ?>
  702. </td>
  703. </tr><?php
  704. break;
  705. // Country multiselects
  706. case 'multi_select_countries' :
  707. $selections = (array) woocommerce_settings_get_option( $value['id'] );
  708. $countries = $woocommerce->countries->countries;
  709. asort( $countries );
  710. ?><tr valign="top">
  711. <th scope="row" class="titledesc">
  712. <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
  713. <?php echo $tip; ?>
  714. </th>
  715. <td class="forminp">
  716. <select multiple="multiple" name="<?php echo esc_attr( $value['id'] ); ?>[]" style="width:450px;" data-placeholder="<?php _e( 'Choose countries&hellip;', 'woocommerce' ); ?>" title="Country" class="chosen_select">
  717. <?php
  718. if ( $countries )
  719. foreach ( $countries as $key => $val )
  720. echo '<option value="'.$key.'" ' . selected( in_array( $key, $selections ), true, false ).'>' . $val . '</option>';
  721. ?>
  722. </select> <?php echo $description; ?>
  723. </td>
  724. </tr><?php
  725. break;
  726. // Default: run an action
  727. default:
  728. do_action( 'woocommerce_admin_field_' . $value['type'], $value );
  729. break;
  730. }
  731. }
  732. }