PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/class-wc-settings-api.php

https://github.com/CammoKing/woocommerce
PHP | 614 lines | 287 code | 105 blank | 222 comment | 65 complexity | 43b11d79d960582cb8ddcd33771fa412 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Admin Settings API used by Shipping Methods and Payment Gateways
  4. *
  5. * @class WC_Settings_API
  6. * @version 1.6.4
  7. * @package WooCommerce/Classes
  8. * @author WooThemes
  9. */
  10. class WC_Settings_API {
  11. /** @var string The plugin ID. Used for option names. */
  12. var $plugin_id = 'woocommerce_';
  13. /** @var array Array of setting values. */
  14. var $settings = array();
  15. /** @var array Array of form option fields. */
  16. var $form_fields = array();
  17. /** @var array Array of validation errors. */
  18. var $errors = array();
  19. /** @var array Sanitized fields after validation. */
  20. var $sanitized_fields = array();
  21. /**
  22. * Admin Options
  23. *
  24. * Setup the gateway settings screen.
  25. * Override this in your gateway.
  26. *
  27. * @since 1.0.0
  28. * @access public
  29. * @return void
  30. */
  31. function admin_options() { ?>
  32. <h3><?php echo ( ! empty( $this->method_title ) ) ? $this->method_title : __( 'Settings','woocommerce' ) ; ?></h3>
  33. <?php echo ( ! empty( $this->method_description ) ) ? wpautop( $this->method_description ) : ''; ?>
  34. <table class="form-table">
  35. <?php $this->generate_settings_html(); ?>
  36. </table><?php
  37. }
  38. /**
  39. * Initialise Settings Form Fields
  40. *
  41. * Add an array of fields to be displayed
  42. * on the gateway's settings screen.
  43. *
  44. * @since 1.0.0
  45. * @access public
  46. * @return string
  47. */
  48. function init_form_fields() {
  49. return __( 'This function needs to be overridden by your payment gateway class.', 'woocommerce' );
  50. }
  51. /**
  52. * Admin Panel Options Processing
  53. * - Saves the options to the DB
  54. *
  55. * @since 1.0.0
  56. * @access public
  57. * @return bool
  58. */
  59. public function process_admin_options() {
  60. $this->validate_settings_fields();
  61. if ( count( $this->errors ) > 0 ) {
  62. $this->display_errors();
  63. return false;
  64. } else {
  65. update_option( $this->plugin_id . $this->id . '_settings', $this->sanitized_fields );
  66. return true;
  67. }
  68. }
  69. /**
  70. * Display admin error messages.
  71. *
  72. * @since 1.0.0
  73. * @access public
  74. * @return void
  75. */
  76. function display_errors() {}
  77. /**
  78. * Initialise Gateway Settings
  79. *
  80. * Store all settings in a single database entry
  81. * and make sure the $settings array is either the default
  82. * or the settings stored in the database.
  83. *
  84. * @since 1.0.0
  85. * @uses get_option(), add_option()
  86. * @access public
  87. * @return void
  88. */
  89. function init_settings() {
  90. // Load form_field settings
  91. if ( $this->form_fields ) {
  92. $form_field_settings = ( array ) get_option( $this->plugin_id . $this->id . '_settings' );
  93. if ( ! $form_field_settings ) {
  94. // If there are no settings defined, load defaults
  95. foreach ( $this->form_fields as $k => $v )
  96. $form_field_settings[ $k ] = isset( $v['default'] ) ? $v['default'] : '';
  97. } else {
  98. // Prevent "undefined index" errors.
  99. foreach ( $this->form_fields as $k => $v )
  100. $form_field_settings[ $k ] = isset( $form_field_settings[ $k ] ) ? $form_field_settings[ $k ] : ( isset( $v['default'] ) ? $v['default'] : '' );
  101. }
  102. // Set and decode escaped values
  103. $this->settings = array_map( array( &$this, 'format_settings' ), $form_field_settings );
  104. }
  105. if ( isset( $this->settings['enabled'] ) && ( $this->settings['enabled'] == 'yes' ) )
  106. $this->enabled = 'yes';
  107. }
  108. /**
  109. * Decode values for settings.
  110. *
  111. * @access public
  112. * @param mixed $value
  113. * @return array
  114. */
  115. function format_settings( $value ) {
  116. return ( is_array( $value ) ) ? $value : html_entity_decode( $value );
  117. }
  118. /**
  119. * Generate Settings HTML.
  120. *
  121. * Generate the HTML for the fields on the "settings" screen.
  122. *
  123. * @access public
  124. * @param bool $form_fields (default: false)
  125. * @since 1.0.0
  126. * @uses method_exists()
  127. * @access public
  128. * @return string the html for the settings
  129. */
  130. function generate_settings_html ( $form_fields = false ) {
  131. if ( ! $form_fields )
  132. $form_fields = $this->form_fields;
  133. $html = '';
  134. foreach ( $form_fields as $k => $v ) {
  135. if ( ! isset( $v['type'] ) || ( $v['type'] == '' ) ) { $v['type'] == 'text'; } // Default to "text" field type.
  136. if ( method_exists( $this, 'generate_' . $v['type'] . '_html' ) ) {
  137. $html .= $this->{'generate_' . $v['type'] . '_html'}( $k, $v );
  138. } else {
  139. $html .= $this->{'generate_text_html'}( $k, $v );
  140. }
  141. }
  142. echo $html;
  143. }
  144. /**
  145. * Generate Text Input HTML.
  146. *
  147. * @access public
  148. * @param mixed $key
  149. * @param mixed $data
  150. * @since 1.0.0
  151. * @return string
  152. */
  153. function generate_text_html( $key, $data ) {
  154. $html = '';
  155. $data['title'] = isset( $data['title'] ) ? $data['title'] : '';
  156. $data['disabled'] = empty( $data['disabled'] ) ? false : true;
  157. $data['class'] = isset( $data['class'] ) ? $data['class'] : '';
  158. $data['css'] = isset( $data['css'] ) ? $data['css'] : '';
  159. $data['placeholder'] = isset( $data['placeholder'] ) ? $data['placeholder'] : '';
  160. $data['type'] = isset( $data['type'] ) ? $data['type'] : 'text';
  161. // Custom attribute handling
  162. $custom_attributes = array();
  163. if ( ! empty( $data['custom_attributes'] ) && is_array( $data['custom_attributes'] ) )
  164. foreach ( $data['custom_attributes'] as $attribute => $attribute_value )
  165. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
  166. $html .= '<tr valign="top">' . "\n";
  167. $html .= '<th scope="row" class="titledesc">';
  168. $html .= '<label for="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '">' . wp_kses_post( $data['title'] ) . '</label>';
  169. $html .= '</th>' . "\n";
  170. $html .= '<td class="forminp">' . "\n";
  171. $html .= '<fieldset><legend class="screen-reader-text"><span>' . wp_kses_post( $data['title'] ) . '</span></legend>' . "\n";
  172. $value = ( isset( $this->settings[ $key ] ) ) ? esc_attr( $this->settings[ $key ] ) : '';
  173. $html .= '<input class="input-text regular-input ' . esc_attr( $data['class'] ) . '" type="' . esc_attr( $data['type'] ) . '" name="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '" id="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '" style="' . esc_attr( $data['css'] ) . '" value="' . $value . '" placeholder="' . esc_attr( $data['placeholder'] ) . '" ' . disabled( $data['disabled'], true, false ) . ' ' . implode( ' ', $custom_attributes ) . ' />';
  174. if ( isset( $data['description'] ) && $data['description'] != '' ) { $html .= ' <p class="description">' . wp_kses_post( $data['description'] ) . '</p>' . "\n"; }
  175. $html .= '</fieldset>';
  176. $html .= '</td>' . "\n";
  177. $html .= '</tr>' . "\n";
  178. return $html;
  179. }
  180. /**
  181. * Generate Password Input HTML.
  182. *
  183. * @access public
  184. * @param mixed $key
  185. * @param mixed $data
  186. * @since 1.0.0
  187. * @return string
  188. */
  189. function generate_password_html( $key, $data ) {
  190. $html = '';
  191. $data['title'] = isset( $data['title'] ) ? $data['title'] : '';
  192. $data['disabled'] = empty( $data['disabled'] ) ? false : true;
  193. $data['class'] = isset( $data['class'] ) ? $data['class'] : '';
  194. $data['css'] = isset( $data['css'] ) ? $data['css'] : '';
  195. // Custom attribute handling
  196. $custom_attributes = array();
  197. if ( ! empty( $data['custom_attributes'] ) && is_array( $data['custom_attributes'] ) )
  198. foreach ( $data['custom_attributes'] as $attribute => $attribute_value )
  199. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
  200. $html .= '<tr valign="top">' . "\n";
  201. $html .= '<th scope="row" class="titledesc">';
  202. $html .= '<label for="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '">' . wp_kses_post( $data['title'] ) . '</label>';
  203. $html .= '</th>' . "\n";
  204. $html .= '<td class="forminp">' . "\n";
  205. $html .= '<fieldset><legend class="screen-reader-text"><span>' . wp_kses_post( $data['title'] ) . '</span></legend>' . "\n";
  206. $value = ( isset( $this->settings[ $key ] ) ) ? esc_attr( $this->settings[ $key ] ) : '';
  207. $html .= '<input class="input-text regular-input ' . esc_attr( $data['class'] ) . '" type="password" name="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '" id="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '" style="' . esc_attr( $data['css'] ) . '" value="' . $value . '" ' . disabled( $data['disabled'], true, false ) . ' ' . implode( ' ', $custom_attributes ) . ' />';
  208. if ( isset( $data['description'] ) && $data['description'] != '' ) { $html .= ' <p class="description">' . esc_attr( $data['description'] ) . '</p>' . "\n"; }
  209. $html .= '</fieldset>';
  210. $html .= '</td>' . "\n";
  211. $html .= '</tr>' . "\n";
  212. return $html;
  213. }
  214. /**
  215. * Generate Textarea HTML.
  216. *
  217. * @access public
  218. * @param mixed $key
  219. * @param mixed $data
  220. * @since 1.0.0
  221. * @return string
  222. */
  223. function generate_textarea_html( $key, $data ) {
  224. $html = '';
  225. $data['title'] = isset( $data['title'] ) ? $data['title'] : '';
  226. $data['disabled'] = empty( $data['disabled'] ) ? false : true;
  227. if ( ! isset( $this->settings[$key] ) ) $this->settings[$key] = '';
  228. $data['class'] = isset( $data['class'] ) ? $data['class'] : '';
  229. $data['css'] = isset( $data['css'] ) ? $data['css'] : '';
  230. // Custom attribute handling
  231. $custom_attributes = array();
  232. if ( ! empty( $data['custom_attributes'] ) && is_array( $data['custom_attributes'] ) )
  233. foreach ( $data['custom_attributes'] as $attribute => $attribute_value )
  234. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
  235. $html .= '<tr valign="top">' . "\n";
  236. $html .= '<th scope="row" class="titledesc">';
  237. $html .= '<label for="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '">' . wp_kses_post( $data['title'] ) . '</label>';
  238. $html .= '</th>' . "\n";
  239. $html .= '<td class="forminp">' . "\n";
  240. $html .= '<fieldset><legend class="screen-reader-text"><span>' . wp_kses_post( $data['title'] ) . '</span></legend>' . "\n";
  241. $value = ( isset( $this->settings[ $key ] ) ) ? esc_textarea( $this->settings[ $key ] ) : '';
  242. $html .= '<textarea rows="3" cols="20" class="input-text wide-input ' . esc_attr( $data['class'] ) . '" name="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '" id="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '" style="' . esc_attr( $data['css'] ) . '" ' . disabled( $data['disabled'], true, false ) . ' ' . implode( ' ', $custom_attributes ) . '>' . $value . '</textarea>';
  243. if ( isset( $data['description'] ) && $data['description'] != '' ) { $html .= ' <p class="description">' . wp_kses_post( $data['description'] ) . '</p>' . "\n"; }
  244. $html .= '</fieldset>';
  245. $html .= '</td>' . "\n";
  246. $html .= '</tr>' . "\n";
  247. return $html;
  248. }
  249. /**
  250. * Generate Checkbox HTML.
  251. *
  252. * @access public
  253. * @param mixed $key
  254. * @param mixed $data
  255. * @since 1.0.0
  256. * @return string
  257. */
  258. function generate_checkbox_html( $key, $data ) {
  259. $html = '';
  260. $data['title'] = isset( $data['title'] ) ? $data['title'] : '';
  261. $data['label'] = isset( $data['label'] ) ? $data['label'] : $data['title'];
  262. $data['disabled'] = empty( $data['disabled'] ) ? false : true;
  263. $data['class'] = isset( $data['class'] ) ? $data['class'] : '';
  264. $data['css'] = isset( $data['css'] ) ? $data['css'] : '';
  265. // Custom attribute handling
  266. $custom_attributes = array();
  267. if ( ! empty( $data['custom_attributes'] ) && is_array( $data['custom_attributes'] ) )
  268. foreach ( $data['custom_attributes'] as $attribute => $attribute_value )
  269. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
  270. $html .= '<tr valign="top">' . "\n";
  271. $html .= '<th scope="row" class="titledesc">' . $data['title'] . '</th>' . "\n";
  272. $html .= '<td class="forminp">' . "\n";
  273. $html .= '<fieldset><legend class="screen-reader-text"><span>' . wp_kses_post( $data['title'] ) . '</span></legend>' . "\n";
  274. $html .= '<label for="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '">';
  275. $html .= '<input style="' . esc_attr( $data['css'] ) . '" name="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '" id="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '" type="checkbox" value="1" ' . checked( $this->settings[$key], 'yes', false ) . ' class="' . esc_attr( $data['class'] ).'" ' . disabled( $data['disabled'], true, false ) . ' ' . implode( ' ', $custom_attributes ) . ' /> ' . wp_kses_post( $data['label'] ) . '</label><br />' . "\n";
  276. if ( isset( $data['description'] ) && $data['description'] != '' ) { $html .= ' <p class="description">' . wp_kses_post( $data['description'] ) . '</p>' . "\n"; }
  277. $html .= '</fieldset>';
  278. $html .= '</td>' . "\n";
  279. $html .= '</tr>' . "\n";
  280. return $html;
  281. }
  282. /**
  283. * Generate Select HTML.
  284. *
  285. * @access public
  286. * @param mixed $key
  287. * @param mixed $data
  288. * @since 1.0.0
  289. * @return string
  290. */
  291. function generate_select_html( $key, $data ) {
  292. $html = '';
  293. $data['title'] = isset( $data['title'] ) ? $data['title'] : '';
  294. $data['disabled'] = empty( $data['disabled'] ) ? false : true;
  295. $data['options'] = isset( $data['options'] ) ? (array) $data['options'] : array();
  296. $data['class'] = isset( $data['class'] ) ? $data['class'] : '';
  297. $data['css'] = isset( $data['css'] ) ? $data['css'] : '';
  298. // Custom attribute handling
  299. $custom_attributes = array();
  300. if ( ! empty( $data['custom_attributes'] ) && is_array( $data['custom_attributes'] ) )
  301. foreach ( $data['custom_attributes'] as $attribute => $attribute_value )
  302. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
  303. $html .= '<tr valign="top">' . "\n";
  304. $html .= '<th scope="row" class="titledesc">';
  305. $html .= '<label for="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '">' . wp_kses_post( $data['title'] ) . '</label>';
  306. $html .= '</th>' . "\n";
  307. $html .= '<td class="forminp">' . "\n";
  308. $html .= '<fieldset><legend class="screen-reader-text"><span>' . wp_kses_post( $data['title'] ) . '</span></legend>' . "\n";
  309. $html .= '<select name="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '" id="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '" style="' . esc_attr( $data['css'] ) . '" class="select ' .esc_attr( $data['class'] ) . '" ' . disabled( $data['disabled'], true, false ) . ' ' . implode( ' ', $custom_attributes ) . '>';
  310. foreach ($data['options'] as $option_key => $option_value) :
  311. $html .= '<option value="' . esc_attr( $option_key ) . '" '.selected($option_key, esc_attr($this->settings[$key]), false).'>' . esc_attr( $option_value ) . '</option>';
  312. endforeach;
  313. $html .= '</select>';
  314. if ( isset( $data['description'] ) && $data['description'] != '' ) { $html .= ' <p class="description">' . wp_kses_post( $data['description'] ) . '</p>' . "\n"; }
  315. $html .= '</fieldset>';
  316. $html .= '</td>' . "\n";
  317. $html .= '</tr>' . "\n";
  318. return $html;
  319. }
  320. /**
  321. * Generate Multiselect HTML.
  322. *
  323. * @access public
  324. * @param mixed $key
  325. * @param mixed $data
  326. * @since 1.0.0
  327. * @return string
  328. */
  329. function generate_multiselect_html( $key, $data ) {
  330. $html = '';
  331. $data['title'] = isset( $data['title'] ) ? $data['title'] : '';
  332. $data['disabled'] = empty( $data['disabled'] ) ? false : true;
  333. $data['options'] = isset( $data['options'] ) ? (array) $data['options'] : array();
  334. $data['class'] = isset( $data['class'] ) ? $data['class'] : '';
  335. $data['css'] = isset( $data['css'] ) ? $data['css'] : '';
  336. // Custom attribute handling
  337. $custom_attributes = array();
  338. if ( ! empty( $data['custom_attributes'] ) && is_array( $data['custom_attributes'] ) )
  339. foreach ( $data['custom_attributes'] as $attribute => $attribute_value )
  340. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
  341. $html .= '<tr valign="top">' . "\n";
  342. $html .= '<th scope="row" class="titledesc">';
  343. $html .= '<label for="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '">' . wp_kses_post( $data['title'] ) . '</label>';
  344. $html .= '</th>' . "\n";
  345. $html .= '<td class="forminp">' . "\n";
  346. $html .= '<fieldset><legend class="screen-reader-text"><span>' . wp_kses_post( $data['title'] ) . '</span></legend>' . "\n";
  347. $html .= '<select multiple="multiple" style="' . esc_attr( $data['css'] ) . '" class="multiselect ' . esc_attr( $data['class'] ) . '" name="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '[]" id="' . esc_attr( $this->plugin_id . $this->id . '_' . $key ) . '" ' . disabled( $data['disabled'], true, false ) . ' ' . implode( ' ', $custom_attributes ) . '>';
  348. foreach ( $data['options'] as $option_key => $option_value) {
  349. $html .= '<option value="' . esc_attr( $option_key ) . '" ';
  350. if ( isset( $this->settings[ $key ] ) && in_array( $option_key, (array) $this->settings[ $key ] ) ) $html .= 'selected="selected"';
  351. $html .= '>' . esc_attr( $option_value ) . '</option>';
  352. }
  353. $html .= '</select>';
  354. if ( isset( $data['description'] ) && $data['description'] != '' ) { $html .= '<p class="description">' . wp_kses_post( $data['description'] ) . '</p>' . "\n"; }
  355. $html .= '</fieldset>';
  356. $html .= '</td>' . "\n";
  357. $html .= '</tr>' . "\n";
  358. return $html;
  359. }
  360. /**
  361. * Generate Title HTML.
  362. *
  363. * @access public
  364. * @param mixed $key
  365. * @param mixed $data
  366. * @since 1.6.2
  367. * @return string
  368. */
  369. function generate_title_html( $key, $data ) {
  370. $html = '';
  371. $data['title'] = isset( $data['title'] ) ? $data['title'] : '';
  372. $data['class'] = isset( $data['class'] ) ? $data['class'] : '';
  373. $data['css'] = isset( $data['css'] ) ? $data['css'] : '';
  374. $html .= '</table>' . "\n";
  375. $html .= '<h4 class="' . esc_attr( $data['class'] ) . '">' . wp_kses_post( $data['title'] ) . '</h4>' . "\n";
  376. if ( isset( $data['description'] ) && $data['description'] != '' ) { $html .= '<p>' . wp_kses_post( $data['description'] ) . '</p>' . "\n"; }
  377. $html .= '<table class="form-table">' . "\n";
  378. return $html;
  379. }
  380. /**
  381. * Validate Settings Field Data.
  382. *
  383. * Validate the data on the "Settings" form.
  384. *
  385. * @since 1.0.0
  386. * @uses method_exists()
  387. * @param bool $form_fields (default: false)
  388. * @return void
  389. */
  390. function validate_settings_fields( $form_fields = false ) {
  391. if ( ! $form_fields )
  392. $form_fields = $this->form_fields;
  393. $this->sanitized_fields = array();
  394. foreach ( $form_fields as $k => $v ) {
  395. if ( empty( $v['type'] ) )
  396. $v['type'] == 'text'; // Default to "text" field type.
  397. if ( method_exists( $this, 'validate_' . $v['type'] . '_field' ) ) {
  398. $field = $this->{'validate_' . $v['type'] . '_field'}( $k );
  399. $this->sanitized_fields[ $k ] = $field;
  400. } else {
  401. $this->sanitized_fields[ $k ] = $this->settings[ $k ];
  402. }
  403. }
  404. }
  405. /**
  406. * Validate Checkbox Field.
  407. *
  408. * If not set, return "no", otherwise return "yes".
  409. *
  410. * @access public
  411. * @param mixed $key
  412. * @since 1.0.0
  413. * @return string
  414. */
  415. function validate_checkbox_field( $key ) {
  416. $status = 'no';
  417. if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) && ( 1 == $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
  418. $status = 'yes';
  419. }
  420. return $status;
  421. }
  422. /**
  423. * Validate Text Field.
  424. *
  425. * Make sure the data is escaped correctly, etc.
  426. *
  427. * @access public
  428. * @param mixed $key
  429. * @since 1.0.0
  430. * @return string
  431. */
  432. function validate_text_field( $key ) {
  433. $text = ( isset( $this->settings[ $key ] ) ) ? $this->settings[ $key ] : '';
  434. if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
  435. $text = esc_attr( trim( stripslashes( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) );
  436. }
  437. return $text;
  438. }
  439. /**
  440. * Validate Password Field.
  441. *
  442. * Make sure the data is escaped correctly, etc.
  443. *
  444. * @access public
  445. * @param mixed $key
  446. * @since 1.0.0
  447. * @return string
  448. */
  449. function validate_password_field( $key ) {
  450. $text = (isset($this->settings[$key])) ? $this->settings[$key] : '';
  451. if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
  452. $text = esc_attr( woocommerce_clean( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) );
  453. }
  454. return $text;
  455. }
  456. /**
  457. * Validate Textarea Field.
  458. *
  459. * Make sure the data is escaped correctly, etc.
  460. *
  461. * @access public
  462. * @param mixed $key
  463. * @since 1.0.0
  464. * @return string
  465. */
  466. function validate_textarea_field( $key ) {
  467. $text = ( isset( $this->settings[ $key ] ) ) ? $this->settings[ $key ] : '';
  468. if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
  469. $text = esc_attr( trim( stripslashes( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) );
  470. }
  471. return $text;
  472. }
  473. /**
  474. * Validate Select Field.
  475. *
  476. * Make sure the data is escaped correctly, etc.
  477. *
  478. * @access public
  479. * @param mixed $key
  480. * @since 1.0.0
  481. * @return string
  482. */
  483. function validate_select_field( $key ) {
  484. $value = ( isset( $this->settings[ $key ] ) ) ? $this->settings[ $key ] : '';
  485. if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
  486. $value = esc_attr( woocommerce_clean( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) );
  487. }
  488. return $value;
  489. }
  490. /**
  491. * Validate Multiselect Field.
  492. *
  493. * Make sure the data is escaped correctly, etc.
  494. *
  495. * @access public
  496. * @param mixed $key
  497. * @since 1.0.0
  498. * @return string
  499. */
  500. function validate_multiselect_field( $key ) {
  501. $value = ( isset( $this->settings[ $key ] ) ) ? $this->settings[ $key ] : '';
  502. if ( isset( $_POST[ $this->plugin_id . $this->id . '_' . $key ] ) ) {
  503. $value = array_map('esc_attr', array_map('woocommerce_clean', (array) $_POST[ $this->plugin_id . $this->id . '_' . $key ] ));
  504. } else {
  505. $value = '';
  506. }
  507. return $value;
  508. }
  509. }