WC_Settings_API

Abstract Settings API Class.

Defined (1)

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

/includes/abstracts/abstract-wc-settings-api.php  
  1. abstract class WC_Settings_API { 
  2.  
  3. /** 
  4. * The plugin ID. Used for option names. 
  5. * @var string 
  6. */ 
  7. public $plugin_id = 'woocommerce_'; 
  8.  
  9. /** 
  10. * ID of the class extending the settings API. Used in option names. 
  11. * @var string 
  12. */ 
  13. public $id = ''; 
  14.  
  15. /** 
  16. * Validation errors. 
  17. * @var array of strings 
  18. */ 
  19. public $errors = array(); 
  20.  
  21. /** 
  22. * Setting values. 
  23. * @var array 
  24. */ 
  25. public $settings = array(); 
  26.  
  27. /** 
  28. * Form option fields. 
  29. * @var array 
  30. */ 
  31. public $form_fields = array(); 
  32.  
  33. /** 
  34. * The posted settings data. When empty, $_POST data will be used. 
  35. * @var array 
  36. */ 
  37. protected $data = array(); 
  38.  
  39. /** 
  40. * Get the form fields after they are initialized. 
  41. * @return array of options 
  42. */ 
  43. public function get_form_fields() { 
  44. return apply_filters( 'woocommerce_settings_api_form_fields_' . $this->id, array_map( array( $this, 'set_defaults' ), $this->form_fields ) ); 
  45.  
  46. /** 
  47. * Set default required properties for each field. 
  48. * @param array 
  49. */ 
  50. protected function set_defaults( $field ) { 
  51. if ( ! isset( $field['default'] ) ) { 
  52. $field['default'] = ''; 
  53. return $field; 
  54.  
  55. /** 
  56. * Output the admin options table. 
  57. */ 
  58. public function admin_options() { 
  59. echo '<table class="form-table">' . $this->generate_settings_html( $this->get_form_fields(), false ) . '</table>'; 
  60.  
  61. /** 
  62. * Initialise settings form fields. 
  63. * Add an array of fields to be displayed 
  64. * on the gateway's settings screen. 
  65. * @since 1.0.0 
  66. * @return string 
  67. */ 
  68. public function init_form_fields() {} 
  69.  
  70. /** 
  71. * Return the name of the option in the WP DB. 
  72. * @since 2.6.0 
  73. * @return string 
  74. */ 
  75. public function get_option_key() { 
  76. return $this->plugin_id . $this->id . '_settings'; 
  77.  
  78. /** 
  79. * Get a fields type. Defaults to "text" if not set. 
  80. * @param array $field 
  81. * @return string 
  82. */ 
  83. public function get_field_type( $field ) { 
  84. return empty( $field['type'] ) ? 'text' : $field['type']; 
  85.  
  86. /** 
  87. * Get a fields default value. Defaults to "" if not set. 
  88. * @param array $field 
  89. * @return string 
  90. */ 
  91. public function get_field_default( $field ) { 
  92. return empty( $field['default'] ) ? '' : $field['default']; 
  93.  
  94. /** 
  95. * Get a field's posted and validated value. 
  96. * @param string $key 
  97. * @param array $field 
  98. * @param array $post_data 
  99. * @return string 
  100. */ 
  101. public function get_field_value( $key, $field, $post_data = array() ) { 
  102. $type = $this->get_field_type( $field ); 
  103. $field_key = $this->get_field_key( $key ); 
  104. $post_data = empty( $post_data ) ? $_POST : $post_data; 
  105. $value = isset( $post_data[ $field_key ] ) ? $post_data[ $field_key ] : null; 
  106.  
  107. // Look for a validate_FIELDID_field method for special handling 
  108. if ( is_callable( array( $this, 'validate_' . $key . '_field' ) ) ) { 
  109. return $this->{'validate_' . $key . '_field'}( $key, $value ); 
  110.  
  111. // Look for a validate_FIELDTYPE_field method 
  112. if ( is_callable( array( $this, 'validate_' . $type . '_field' ) ) ) { 
  113. return $this->{'validate_' . $type . '_field'}( $key, $value ); 
  114.  
  115. // Fallback to text 
  116. return $this->validate_text_field( $key, $value ); 
  117.  
  118. /** 
  119. * Sets the POSTed data. This method can be used to set specific data, instead 
  120. * of taking it from the $_POST array. 
  121. * @param array data 
  122. */ 
  123. public function set_post_data( $data = array() ) { 
  124. $this->data = $data; 
  125.  
  126. /** 
  127. * Returns the POSTed data, to be used to save the settings. 
  128. * @return array 
  129. */ 
  130. public function get_post_data() { 
  131. if ( ! empty( $this->data ) && is_array( $this->data ) ) { 
  132. return $this->data; 
  133. return $_POST; 
  134.  
  135. /** 
  136. * Processes and saves options. 
  137. * If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out. 
  138. * @return bool was anything saved? 
  139. */ 
  140. public function process_admin_options() { 
  141. $this->init_settings(); 
  142.  
  143. $post_data = $this->get_post_data(); 
  144.  
  145. foreach ( $this->get_form_fields() as $key => $field ) { 
  146. if ( 'title' !== $this->get_field_type( $field ) ) { 
  147. try { 
  148. $this->settings[ $key ] = $this->get_field_value( $key, $field, $post_data ); 
  149. } catch ( Exception $e ) { 
  150. $this->add_error( $e->getMessage() ); 
  151.  
  152. return update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->settings ) ); 
  153.  
  154. /** 
  155. * Add an error message for display in admin on save. 
  156. * @param string $error 
  157. */ 
  158. public function add_error( $error ) { 
  159. $this->errors[] = $error; 
  160.  
  161. /** 
  162. * Get admin error messages. 
  163. */ 
  164. public function get_errors() { 
  165. return $this->errors; 
  166.  
  167. /** 
  168. * Display admin error messages. 
  169. */ 
  170. public function display_errors() { 
  171. if ( $this->get_errors() ) { 
  172. echo '<div id="woocommerce_errors" class="error notice is-dismissible">'; 
  173. foreach ( $this->get_errors() as $error ) { 
  174. echo '<p>' . wp_kses_post( $error ) . '</p>'; 
  175. echo '</div>'; 
  176.  
  177. /** 
  178. * Initialise Settings. 
  179. * Store all settings in a single database entry 
  180. * and make sure the $settings array is either the default 
  181. * or the settings stored in the database. 
  182. * @since 1.0.0 
  183. * @uses get_option(), add_option() 
  184. */ 
  185. public function init_settings() { 
  186. $this->settings = get_option( $this->get_option_key(), null ); 
  187.  
  188. // If there are no settings defined, use defaults. 
  189. if ( ! is_array( $this->settings ) ) { 
  190. $form_fields = $this->get_form_fields(); 
  191. $this->settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) ); 
  192.  
  193. /** 
  194. * get_option function. 
  195. * Gets an option from the settings API, using defaults if necessary to prevent undefined notices. 
  196. * @param string $key 
  197. * @param mixed $empty_value 
  198. * @return string The value specified for the option or a default value for the option. 
  199. */ 
  200. public function get_option( $key, $empty_value = null ) { 
  201. if ( empty( $this->settings ) ) { 
  202. $this->init_settings(); 
  203.  
  204. // Get option default if unset. 
  205. if ( ! isset( $this->settings[ $key ] ) ) { 
  206. $form_fields = $this->get_form_fields(); 
  207. $this->settings[ $key ] = isset( $form_fields[ $key ] ) ? $this->get_field_default( $form_fields[ $key ] ) : ''; 
  208.  
  209. if ( ! is_null( $empty_value ) && '' === $this->settings[ $key ] ) { 
  210. $this->settings[ $key ] = $empty_value; 
  211.  
  212. return $this->settings[ $key ]; 
  213.  
  214. /** 
  215. * Prefix key for settings. 
  216. * @param mixed $key 
  217. * @return string 
  218. */ 
  219. public function get_field_key( $key ) { 
  220. return $this->plugin_id . $this->id . '_' . $key; 
  221.  
  222. /** 
  223. * Generate Settings HTML. 
  224. * Generate the HTML for the fields on the "settings" screen. 
  225. * @param array $form_fields (default: array()) 
  226. * @since 1.0.0 
  227. * @uses method_exists() 
  228. * @return string the html for the settings 
  229. */ 
  230. public function generate_settings_html( $form_fields = array(), $echo = true ) { 
  231. if ( empty( $form_fields ) ) { 
  232. $form_fields = $this->get_form_fields(); 
  233.  
  234. $html = ''; 
  235. foreach ( $form_fields as $k => $v ) { 
  236. $type = $this->get_field_type( $v ); 
  237.  
  238. if ( method_exists( $this, 'generate_' . $type . '_html' ) ) { 
  239. $html .= $this->{'generate_' . $type . '_html'}( $k, $v ); 
  240. } else { 
  241. $html .= $this->generate_text_html( $k, $v ); 
  242.  
  243. if ( $echo ) { 
  244. echo $html; 
  245. } else { 
  246. return $html; 
  247.  
  248. /** 
  249. * Get HTML for tooltips. 
  250. * @param array $data 
  251. * @return string 
  252. */ 
  253. public function get_tooltip_html( $data ) { 
  254. if ( true === $data['desc_tip'] ) { 
  255. $tip = $data['description']; 
  256. } elseif ( ! empty( $data['desc_tip'] ) ) { 
  257. $tip = $data['desc_tip']; 
  258. } else { 
  259. $tip = ''; 
  260.  
  261. return $tip ? wc_help_tip( $tip, true ) : ''; 
  262.  
  263. /** 
  264. * Get HTML for descriptions. 
  265. * @param array $data 
  266. * @return string 
  267. */ 
  268. public function get_description_html( $data ) { 
  269. if ( true === $data['desc_tip'] ) { 
  270. $description = ''; 
  271. } elseif ( ! empty( $data['desc_tip'] ) ) { 
  272. $description = $data['description']; 
  273. } elseif ( ! empty( $data['description'] ) ) { 
  274. $description = $data['description']; 
  275. } else { 
  276. $description = ''; 
  277.  
  278. return $description ? '<p class="description">' . wp_kses_post( $description ) . '</p>' . "\n" : ''; 
  279.  
  280. /** 
  281. * Get custom attributes. 
  282. * @param array $data 
  283. * @return string 
  284. */ 
  285. public function get_custom_attribute_html( $data ) { 
  286. $custom_attributes = array(); 
  287.  
  288. if ( ! empty( $data['custom_attributes'] ) && is_array( $data['custom_attributes'] ) ) { 
  289. foreach ( $data['custom_attributes'] as $attribute => $attribute_value ) { 
  290. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; 
  291.  
  292. return implode( ' ', $custom_attributes ); 
  293.  
  294. /** 
  295. * Generate Text Input HTML. 
  296. * @param mixed $key 
  297. * @param mixed $data 
  298. * @since 1.0.0 
  299. * @return string 
  300. */ 
  301. public function generate_text_html( $key, $data ) { 
  302. $field_key = $this->get_field_key( $key ); 
  303. $defaults = array( 
  304. 'title' => '',  
  305. 'disabled' => false,  
  306. 'class' => '',  
  307. 'css' => '',  
  308. 'placeholder' => '',  
  309. 'type' => 'text',  
  310. 'desc_tip' => false,  
  311. 'description' => '',  
  312. 'custom_attributes' => array(),  
  313. ); 
  314.  
  315. $data = wp_parse_args( $data, $defaults ); 
  316.  
  317. ob_start(); 
  318. ?> 
  319. <tr valign="top"> 
  320. <th scope="row" class="titledesc"> 
  321. <?php echo $this->get_tooltip_html( $data ); ?> 
  322. <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  323. </th> 
  324. <td class="forminp"> 
  325. <fieldset> 
  326. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  327. <input class="input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> 
  328. <?php echo $this->get_description_html( $data ); ?> 
  329. </fieldset> 
  330. </td> 
  331. </tr> 
  332. <?php 
  333.  
  334. return ob_get_clean(); 
  335.  
  336. /** 
  337. * Generate Price Input HTML. 
  338. * @param mixed $key 
  339. * @param mixed $data 
  340. * @since 1.0.0 
  341. * @return string 
  342. */ 
  343. public function generate_price_html( $key, $data ) { 
  344. $field_key = $this->get_field_key( $key ); 
  345. $defaults = array( 
  346. 'title' => '',  
  347. 'disabled' => false,  
  348. 'class' => '',  
  349. 'css' => '',  
  350. 'placeholder' => '',  
  351. 'type' => 'text',  
  352. 'desc_tip' => false,  
  353. 'description' => '',  
  354. 'custom_attributes' => array(),  
  355. ); 
  356.  
  357. $data = wp_parse_args( $data, $defaults ); 
  358.  
  359. ob_start(); 
  360. ?> 
  361. <tr valign="top"> 
  362. <th scope="row" class="titledesc"> 
  363. <?php echo $this->get_tooltip_html( $data ); ?> 
  364. <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  365. </th> 
  366. <td class="forminp"> 
  367. <fieldset> 
  368. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  369. <input class="wc_input_price input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( wc_format_localized_price( $this->get_option( $key ) ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> 
  370. <?php echo $this->get_description_html( $data ); ?> 
  371. </fieldset> 
  372. </td> 
  373. </tr> 
  374. <?php 
  375.  
  376. return ob_get_clean(); 
  377.  
  378. /** 
  379. * Generate Decimal Input HTML. 
  380. * @param mixed $key 
  381. * @param mixed $data 
  382. * @since 1.0.0 
  383. * @return string 
  384. */ 
  385. public function generate_decimal_html( $key, $data ) { 
  386. $field_key = $this->get_field_key( $key ); 
  387. $defaults = array( 
  388. 'title' => '',  
  389. 'disabled' => false,  
  390. 'class' => '',  
  391. 'css' => '',  
  392. 'placeholder' => '',  
  393. 'type' => 'text',  
  394. 'desc_tip' => false,  
  395. 'description' => '',  
  396. 'custom_attributes' => array(),  
  397. ); 
  398.  
  399. $data = wp_parse_args( $data, $defaults ); 
  400.  
  401. ob_start(); 
  402. ?> 
  403. <tr valign="top"> 
  404. <th scope="row" class="titledesc"> 
  405. <?php echo $this->get_tooltip_html( $data ); ?> 
  406. <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  407. </th> 
  408. <td class="forminp"> 
  409. <fieldset> 
  410. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  411. <input class="wc_input_decimal input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( wc_format_localized_decimal( $this->get_option( $key ) ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> 
  412. <?php echo $this->get_description_html( $data ); ?> 
  413. </fieldset> 
  414. </td> 
  415. </tr> 
  416. <?php 
  417.  
  418. return ob_get_clean(); 
  419.  
  420. /** 
  421. * Generate Password Input HTML. 
  422. * @param mixed $key 
  423. * @param mixed $data 
  424. * @since 1.0.0 
  425. * @return string 
  426. */ 
  427. public function generate_password_html( $key, $data ) { 
  428. $data['type'] = 'password'; 
  429. return $this->generate_text_html( $key, $data ); 
  430.  
  431. /** 
  432. * Generate Color Picker Input HTML. 
  433. * @param mixed $key 
  434. * @param mixed $data 
  435. * @since 1.0.0 
  436. * @return string 
  437. */ 
  438. public function generate_color_html( $key, $data ) { 
  439. $field_key = $this->get_field_key( $key ); 
  440. $defaults = array( 
  441. 'title' => '',  
  442. 'disabled' => false,  
  443. 'class' => '',  
  444. 'css' => '',  
  445. 'placeholder' => '',  
  446. 'desc_tip' => false,  
  447. 'description' => '',  
  448. 'custom_attributes' => array(),  
  449. ); 
  450.  
  451. $data = wp_parse_args( $data, $defaults ); 
  452.  
  453. ob_start(); 
  454. ?> 
  455. <tr valign="top"> 
  456. <th scope="row" class="titledesc"> 
  457. <?php echo $this->get_tooltip_html( $data ); ?> 
  458. <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  459. </th> 
  460. <td class="forminp"> 
  461. <fieldset> 
  462. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  463. <span class="colorpickpreview" style="background:<?php echo esc_attr( $this->get_option( $key ) ); ?>;"></span> 
  464. <input class="colorpick <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> 
  465. <div id="colorPickerDiv_<?php echo esc_attr( $field_key ); ?>" class="colorpickdiv" style="z-index: 100; background: #eee; border: 1px solid #ccc; position: absolute; display: none;"></div> 
  466. <?php echo $this->get_description_html( $data ); ?> 
  467. </fieldset> 
  468. </td> 
  469. </tr> 
  470. <?php 
  471.  
  472. return ob_get_clean(); 
  473.  
  474. /** 
  475. * Generate Textarea HTML. 
  476. * @param mixed $key 
  477. * @param mixed $data 
  478. * @since 1.0.0 
  479. * @return string 
  480. */ 
  481. public function generate_textarea_html( $key, $data ) { 
  482. $field_key = $this->get_field_key( $key ); 
  483. $defaults = array( 
  484. 'title' => '',  
  485. 'disabled' => false,  
  486. 'class' => '',  
  487. 'css' => '',  
  488. 'placeholder' => '',  
  489. 'type' => 'text',  
  490. 'desc_tip' => false,  
  491. 'description' => '',  
  492. 'custom_attributes' => array(),  
  493. ); 
  494.  
  495. $data = wp_parse_args( $data, $defaults ); 
  496.  
  497. ob_start(); 
  498. ?> 
  499. <tr valign="top"> 
  500. <th scope="row" class="titledesc"> 
  501. <?php echo $this->get_tooltip_html( $data ); ?> 
  502. <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  503. </th> 
  504. <td class="forminp"> 
  505. <fieldset> 
  506. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  507. <textarea rows="3" cols="20" class="input-text wide-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?>><?php echo esc_textarea( $this->get_option( $key ) ); ?></textarea> 
  508. <?php echo $this->get_description_html( $data ); ?> 
  509. </fieldset> 
  510. </td> 
  511. </tr> 
  512. <?php 
  513.  
  514. return ob_get_clean(); 
  515.  
  516. /** 
  517. * Generate Checkbox HTML. 
  518. * @param mixed $key 
  519. * @param mixed $data 
  520. * @since 1.0.0 
  521. * @return string 
  522. */ 
  523. public function generate_checkbox_html( $key, $data ) { 
  524. $field_key = $this->get_field_key( $key ); 
  525. $defaults = array( 
  526. 'title' => '',  
  527. 'label' => '',  
  528. 'disabled' => false,  
  529. 'class' => '',  
  530. 'css' => '',  
  531. 'type' => 'text',  
  532. 'desc_tip' => false,  
  533. 'description' => '',  
  534. 'custom_attributes' => array(),  
  535. ); 
  536.  
  537. $data = wp_parse_args( $data, $defaults ); 
  538.  
  539. if ( ! $data['label'] ) { 
  540. $data['label'] = $data['title']; 
  541.  
  542. ob_start(); 
  543. ?> 
  544. <tr valign="top"> 
  545. <th scope="row" class="titledesc"> 
  546. <?php echo $this->get_tooltip_html( $data ); ?> 
  547. <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  548. </th> 
  549. <td class="forminp"> 
  550. <fieldset> 
  551. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  552. <label for="<?php echo esc_attr( $field_key ); ?>"> 
  553. <input <?php disabled( $data['disabled'], true ); ?> class="<?php echo esc_attr( $data['class'] ); ?>" type="checkbox" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="1" <?php checked( $this->get_option( $key ), 'yes' ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> <?php echo wp_kses_post( $data['label'] ); ?></label><br/> 
  554. <?php echo $this->get_description_html( $data ); ?> 
  555. </fieldset> 
  556. </td> 
  557. </tr> 
  558. <?php 
  559.  
  560. return ob_get_clean(); 
  561.  
  562. /** 
  563. * Generate Select HTML. 
  564. * @param mixed $key 
  565. * @param mixed $data 
  566. * @since 1.0.0 
  567. * @return string 
  568. */ 
  569. public function generate_select_html( $key, $data ) { 
  570. $field_key = $this->get_field_key( $key ); 
  571. $defaults = array( 
  572. 'title' => '',  
  573. 'disabled' => false,  
  574. 'class' => '',  
  575. 'css' => '',  
  576. 'placeholder' => '',  
  577. 'type' => 'text',  
  578. 'desc_tip' => false,  
  579. 'description' => '',  
  580. 'custom_attributes' => array(),  
  581. 'options' => array(),  
  582. ); 
  583.  
  584. $data = wp_parse_args( $data, $defaults ); 
  585.  
  586. ob_start(); 
  587. ?> 
  588. <tr valign="top"> 
  589. <th scope="row" class="titledesc"> 
  590. <?php echo $this->get_tooltip_html( $data ); ?> 
  591. <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  592. </th> 
  593. <td class="forminp"> 
  594. <fieldset> 
  595. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  596. <select class="select <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?>> 
  597. <?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?> 
  598. <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, esc_attr( $this->get_option( $key ) ) ); ?>><?php echo esc_attr( $option_value ); ?></option> 
  599. <?php endforeach; ?> 
  600. </select> 
  601. <?php echo $this->get_description_html( $data ); ?> 
  602. </fieldset> 
  603. </td> 
  604. </tr> 
  605. <?php 
  606.  
  607. return ob_get_clean(); 
  608.  
  609. /** 
  610. * Generate Multiselect HTML. 
  611. * @param mixed $key 
  612. * @param mixed $data 
  613. * @since 1.0.0 
  614. * @return string 
  615. */ 
  616. public function generate_multiselect_html( $key, $data ) { 
  617. $field_key = $this->get_field_key( $key ); 
  618. $defaults = array( 
  619. 'title' => '',  
  620. 'disabled' => false,  
  621. 'class' => '',  
  622. 'css' => '',  
  623. 'placeholder' => '',  
  624. 'type' => 'text',  
  625. 'desc_tip' => false,  
  626. 'description' => '',  
  627. 'custom_attributes' => array(),  
  628. 'select_buttons' => false,  
  629. 'options' => array(),  
  630. ); 
  631.  
  632. $data = wp_parse_args( $data, $defaults ); 
  633. $value = (array) $this->get_option( $key, array() ); 
  634.  
  635. ob_start(); 
  636. ?> 
  637. <tr valign="top"> 
  638. <th scope="row" class="titledesc"> 
  639. <?php echo $this->get_tooltip_html( $data ); ?> 
  640. <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> 
  641. </th> 
  642. <td class="forminp"> 
  643. <fieldset> 
  644. <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> 
  645. <select multiple="multiple" class="multiselect <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>[]" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?>> 
  646. <?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?> 
  647. <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( in_array( $option_key, $value ), true ); ?>><?php echo esc_attr( $option_value ); ?></option> 
  648. <?php endforeach; ?> 
  649. </select> 
  650. <?php echo $this->get_description_html( $data ); ?> 
  651. <?php if ( $data['select_buttons'] ) : ?> 
  652. <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> 
  653. <?php endif; ?> 
  654. </fieldset> 
  655. </td> 
  656. </tr> 
  657. <?php 
  658.  
  659. return ob_get_clean(); 
  660.  
  661. /** 
  662. * Generate Title HTML. 
  663. * @param mixed $key 
  664. * @param mixed $data 
  665. * @since 1.0.0 
  666. * @return string 
  667. */ 
  668. public function generate_title_html( $key, $data ) { 
  669. $field_key = $this->get_field_key( $key ); 
  670. $defaults = array( 
  671. 'title' => '',  
  672. 'class' => '',  
  673. ); 
  674.  
  675. $data = wp_parse_args( $data, $defaults ); 
  676.  
  677. ob_start(); 
  678. ?> 
  679. </table> 
  680. <h3 class="wc-settings-sub-title <?php echo esc_attr( $data['class'] ); ?>" id="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></h3> 
  681. <?php if ( ! empty( $data['description'] ) ) : ?> 
  682. <p><?php echo wp_kses_post( $data['description'] ); ?></p> 
  683. <?php endif; ?> 
  684. <table class="form-table"> 
  685. <?php 
  686.  
  687. return ob_get_clean(); 
  688.  
  689. /** 
  690. * Validate Text Field. 
  691. * Make sure the data is escaped correctly, etc. 
  692. * @param string $key Field key 
  693. * @param string|null $value Posted Value 
  694. * @return string 
  695. */ 
  696. public function validate_text_field( $key, $value ) { 
  697. $value = is_null( $value ) ? '' : $value; 
  698. return wp_kses_post( trim( stripslashes( $value ) ) ); 
  699.  
  700. /** 
  701. * Validate Price Field. 
  702. * Make sure the data is escaped correctly, etc. 
  703. * @param string $key 
  704. * @param string|null $value Posted Value 
  705. * @return string 
  706. */ 
  707. public function validate_price_field( $key, $value ) { 
  708. $value = is_null( $value ) ? '' : $value; 
  709. return ( '' === $value ) ? '' : wc_format_decimal( trim( stripslashes( $value ) ) ); 
  710.  
  711. /** 
  712. * Validate Decimal Field. 
  713. * Make sure the data is escaped correctly, etc. 
  714. * @param string $key 
  715. * @param string|null $value Posted Value 
  716. * @return string 
  717. */ 
  718. public function validate_decimal_field( $key, $value ) { 
  719. $value = is_null( $value ) ? '' : $value; 
  720. return ( '' === $value ) ? '' : wc_format_decimal( trim( stripslashes( $value ) ) ); 
  721.  
  722. /** 
  723. * Validate Password Field. No input sanitization is used to avoid corrupting passwords. 
  724. * @param string $key 
  725. * @param string|null $value Posted Value 
  726. * @return string 
  727. */ 
  728. public function validate_password_field( $key, $value ) { 
  729. $value = is_null( $value ) ? '' : $value; 
  730. return trim( stripslashes( $value ) ); 
  731.  
  732. /** 
  733. * Validate Textarea Field. 
  734. * @param string $key 
  735. * @param string|null $value Posted Value 
  736. * @return string 
  737. */ 
  738. public function validate_textarea_field( $key, $value ) { 
  739. $value = is_null( $value ) ? '' : $value; 
  740. return wp_kses( trim( stripslashes( $value ) ),  
  741. array_merge( 
  742. array( 
  743. 'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true ),  
  744. ),  
  745. wp_kses_allowed_html( 'post' ) 
  746. ); 
  747.  
  748. /** 
  749. * Validate Checkbox Field. 
  750. * If not set, return "no", otherwise return "yes". 
  751. * @param string $key 
  752. * @param string|null $value Posted Value 
  753. * @return string 
  754. */ 
  755. public function validate_checkbox_field( $key, $value ) { 
  756. return ! is_null( $value ) ? 'yes' : 'no'; 
  757.  
  758. /** 
  759. * Validate Select Field. 
  760. * @param string $key 
  761. * @param string $value Posted Value 
  762. * @return string 
  763. */ 
  764. public function validate_select_field( $key, $value ) { 
  765. $value = is_null( $value ) ? '' : $value; 
  766. return wc_clean( stripslashes( $value ) ); 
  767.  
  768. /** 
  769. * Validate Multiselect Field. 
  770. * @param string $key 
  771. * @param string $value Posted Value 
  772. * @return string 
  773. */ 
  774. public function validate_multiselect_field( $key, $value ) { 
  775. return is_array( $value ) ? array_map( 'wc_clean', array_map( 'stripslashes', $value ) ) : ''; 
  776.  
  777. /** 
  778. * Validate the data on the "Settings" form. 
  779. * @deprecated 2.6.0 No longer used 
  780. */ 
  781. public function validate_settings_fields( $form_fields = array() ) { 
  782. wc_deprecated_function( 'validate_settings_fields', '2.6' ); 
  783.  
  784. /** 
  785. * Format settings if needed. 
  786. * @deprecated 2.6.0 Unused 
  787. * @param array $value 
  788. * @return array 
  789. */ 
  790. public function format_settings( $value ) { 
  791. wc_deprecated_function( 'format_settings', '2.6' ); 
  792. return $value;