wc-meta-box-functions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * WooCommerce Meta Box Functions
  4. *
  5. * @author WooThemes
  6. * @category Core
  7. * @package WooCommerce/Admin/Functions
  8. * @version 2.3.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit; // Exit if accessed directly
  12. }
  13. /**
  14. * Output a text input box.
  15. *
  16. * @param array $field
  17. */
  18. function woocommerce_wp_text_input( $field ) {
  19. global $thepostid, $post;
  20. $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
  21. $field['placeholder'] = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
  22. $field['class'] = isset( $field['class'] ) ? $field['class'] : 'short';
  23. $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
  24. $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
  25. $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
  26. $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
  27. $field['type'] = isset( $field['type'] ) ? $field['type'] : 'text';
  28. $field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
  29. $data_type = empty( $field['data_type'] ) ? '' : $field['data_type'];
  30. switch ( $data_type ) {
  31. case 'price':
  32. $field['class'] .= ' wc_input_price';
  33. $field['value'] = wc_format_localized_price( $field['value'] );
  34. break;
  35. case 'decimal':
  36. $field['class'] .= ' wc_input_decimal';
  37. $field['value'] = wc_format_localized_decimal( $field['value'] );
  38. break;
  39. case 'stock':
  40. $field['class'] .= ' wc_input_stock';
  41. $field['value'] = wc_stock_amount( $field['value'] );
  42. break;
  43. case 'url':
  44. $field['class'] .= ' wc_input_url';
  45. $field['value'] = esc_url( $field['value'] );
  46. break;
  47. default:
  48. break;
  49. }
  50. // Custom attribute handling
  51. $custom_attributes = array();
  52. if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
  53. foreach ( $field['custom_attributes'] as $attribute => $value ) {
  54. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
  55. }
  56. }
  57. echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">
  58. <label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>';
  59. if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
  60. echo wc_help_tip( $field['description'] );
  61. }
  62. echo '<input type="' . esc_attr( $field['type'] ) . '" class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['value'] ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" ' . implode( ' ', $custom_attributes ) . ' /> ';
  63. if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
  64. echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
  65. }
  66. echo '</p>';
  67. }
  68. /**
  69. * Output a hidden input box.
  70. *
  71. * @param array $field
  72. */
  73. function woocommerce_wp_hidden_input( $field ) {
  74. global $thepostid, $post;
  75. $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
  76. $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
  77. $field['class'] = isset( $field['class'] ) ? $field['class'] : '';
  78. echo '<input type="hidden" class="' . esc_attr( $field['class'] ) . '" name="' . esc_attr( $field['id'] ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['value'] ) . '" /> ';
  79. }
  80. /**
  81. * Output a textarea input box.
  82. *
  83. * @param array $field
  84. */
  85. function woocommerce_wp_textarea_input( $field ) {
  86. global $thepostid, $post;
  87. $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
  88. $field['placeholder'] = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
  89. $field['class'] = isset( $field['class'] ) ? $field['class'] : 'short';
  90. $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
  91. $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
  92. $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
  93. $field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
  94. $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
  95. $field['rows'] = isset( $field['rows'] ) ? $field['rows'] : 2;
  96. $field['cols'] = isset( $field['cols'] ) ? $field['cols'] : 20;
  97. // Custom attribute handling
  98. $custom_attributes = array();
  99. if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
  100. foreach ( $field['custom_attributes'] as $attribute => $value ) {
  101. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
  102. }
  103. }
  104. echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">
  105. <label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>';
  106. if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
  107. echo wc_help_tip( $field['description'] );
  108. }
  109. echo '<textarea class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '" id="' . esc_attr( $field['id'] ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" rows="' . esc_attr( $field['rows'] ) . '" cols="' . esc_attr( $field['cols'] ) . '" ' . implode( ' ', $custom_attributes ) . '>' . esc_textarea( $field['value'] ) . '</textarea> ';
  110. if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
  111. echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
  112. }
  113. echo '</p>';
  114. }
  115. /**
  116. * Output a checkbox input box.
  117. *
  118. * @param array $field
  119. */
  120. function woocommerce_wp_checkbox( $field ) {
  121. global $thepostid, $post;
  122. $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
  123. $field['class'] = isset( $field['class'] ) ? $field['class'] : 'checkbox';
  124. $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
  125. $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
  126. $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
  127. $field['cbvalue'] = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'yes';
  128. $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
  129. $field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
  130. // Custom attribute handling
  131. $custom_attributes = array();
  132. if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
  133. foreach ( $field['custom_attributes'] as $attribute => $value ) {
  134. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
  135. }
  136. }
  137. echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">
  138. <label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>';
  139. if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
  140. echo wc_help_tip( $field['description'] );
  141. }
  142. echo '<input type="checkbox" class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['cbvalue'] ) . '" ' . checked( $field['value'], $field['cbvalue'], false ) . ' ' . implode( ' ', $custom_attributes ) . '/> ';
  143. if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
  144. echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
  145. }
  146. echo '</p>';
  147. }
  148. /**
  149. * Output a select input box.
  150. *
  151. * @param array $field Data about the field to render.
  152. */
  153. function woocommerce_wp_select( $field ) {
  154. global $thepostid, $post;
  155. $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
  156. $field = wp_parse_args(
  157. $field, array(
  158. 'class' => 'select short',
  159. 'style' => '',
  160. 'wrapper_class' => '',
  161. 'value' => get_post_meta( $thepostid, $field['id'], true ),
  162. 'name' => $field['id'],
  163. 'desc_tip' => false,
  164. 'custom_attributes' => array(),
  165. )
  166. );
  167. $wrapper_attributes = array(
  168. 'class' => $field['wrapper_class'] . " form-field {$field['id']}_field",
  169. );
  170. $label_attributes = array(
  171. 'for' => $field['id'],
  172. );
  173. $field_attributes = (array) $field['custom_attributes'];
  174. $field_attributes['style'] = $field['style'];
  175. $field_attributes['id'] = $field['id'];
  176. $field_attributes['name'] = $field['name'];
  177. $field_attributes['class'] = $field['class'];
  178. $tooltip = ! empty( $field['description'] ) && false !== $field['desc_tip'] ? $field['description'] : '';
  179. $description = ! empty( $field['description'] ) && false === $field['desc_tip'] ? $field['description'] : '';
  180. ?>
  181. <p <?php echo wc_implode_html_attributes( $wrapper_attributes ); // WPCS: XSS ok. ?>>
  182. <label <?php echo wc_implode_html_attributes( $label_attributes ); // WPCS: XSS ok. ?>><?php echo wp_kses_post( $field['label'] ); ?></label>
  183. <?php if ( $tooltip ) : ?>
  184. <?php echo wc_help_tip( $tooltip ); // WPCS: XSS ok. ?>
  185. <?php endif; ?>
  186. <select <?php echo wc_implode_html_attributes( $field_attributes ); // WPCS: XSS ok. ?>>
  187. <?php
  188. foreach ( $field['options'] as $key => $value ) {
  189. echo '<option value="' . esc_attr( $key ) . '"' . wc_selected( $key, $field['value'] ) . '>' . esc_html( $value ) . '</option>';
  190. }
  191. ?>
  192. </select>
  193. <?php if ( $description ) : ?>
  194. <span class="description"><?php echo wp_kses_post( $description ); ?></span>
  195. <?php endif; ?>
  196. </p>
  197. <?php
  198. }
  199. /**
  200. * Output a radio input box.
  201. *
  202. * @param array $field
  203. */
  204. function woocommerce_wp_radio( $field ) {
  205. global $thepostid, $post;
  206. $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
  207. $field['class'] = isset( $field['class'] ) ? $field['class'] : 'select short';
  208. $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
  209. $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
  210. $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
  211. $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
  212. $field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
  213. echo '<fieldset class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><legend>' . wp_kses_post( $field['label'] ) . '</legend>';
  214. if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
  215. echo wc_help_tip( $field['description'] );
  216. }
  217. echo '<ul class="wc-radios">';
  218. foreach ( $field['options'] as $key => $value ) {
  219. echo '<li><label><input
  220. name="' . esc_attr( $field['name'] ) . '"
  221. value="' . esc_attr( $key ) . '"
  222. type="radio"
  223. class="' . esc_attr( $field['class'] ) . '"
  224. style="' . esc_attr( $field['style'] ) . '"
  225. ' . checked( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '
  226. /> ' . esc_html( $value ) . '</label>
  227. </li>';
  228. }
  229. echo '</ul>';
  230. if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
  231. echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
  232. }
  233. echo '</fieldset>';
  234. }