/wp-content/plugins/woocommerce/includes/admin/wc-meta-box-functions.php

https://gitlab.com/webkod3r/tripolis · PHP · 255 lines · 165 code · 48 blank · 42 comment · 30 complexity · d66790cac71caa68e919713670f599c0 MD5 · raw file

  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. $data_type = empty( $field['data_type'] ) ? '' : $field['data_type'];
  29. switch ( $data_type ) {
  30. case 'price' :
  31. $field['class'] .= ' wc_input_price';
  32. $field['value'] = wc_format_localized_price( $field['value'] );
  33. break;
  34. case 'decimal' :
  35. $field['class'] .= ' wc_input_decimal';
  36. $field['value'] = wc_format_localized_decimal( $field['value'] );
  37. break;
  38. case 'stock' :
  39. $field['class'] .= ' wc_input_stock';
  40. $field['value'] = wc_stock_amount( $field['value'] );
  41. break;
  42. case 'url' :
  43. $field['class'] .= ' wc_input_url';
  44. $field['value'] = esc_url( $field['value'] );
  45. break;
  46. default :
  47. break;
  48. }
  49. // Custom attribute handling
  50. $custom_attributes = array();
  51. if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
  52. foreach ( $field['custom_attributes'] as $attribute => $value ){
  53. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
  54. }
  55. }
  56. echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label><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 ) . ' /> ';
  57. if ( ! empty( $field['description'] ) ) {
  58. if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
  59. echo wc_help_tip( $field['description'] );
  60. } else {
  61. echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
  62. }
  63. }
  64. echo '</p>';
  65. }
  66. /**
  67. * Output a hidden input box.
  68. *
  69. * @param array $field
  70. */
  71. function woocommerce_wp_hidden_input( $field ) {
  72. global $thepostid, $post;
  73. $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
  74. $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
  75. $field['class'] = isset( $field['class'] ) ? $field['class'] : '';
  76. echo '<input type="hidden" class="' . esc_attr( $field['class'] ) . '" name="' . esc_attr( $field['id'] ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['value'] ) . '" /> ';
  77. }
  78. /**
  79. * Output a textarea input box.
  80. *
  81. * @param array $field
  82. */
  83. function woocommerce_wp_textarea_input( $field ) {
  84. global $thepostid, $post;
  85. $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
  86. $field['placeholder'] = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
  87. $field['class'] = isset( $field['class'] ) ? $field['class'] : 'short';
  88. $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
  89. $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
  90. $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
  91. // Custom attribute handling
  92. $custom_attributes = array();
  93. if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
  94. foreach ( $field['custom_attributes'] as $attribute => $value ){
  95. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
  96. }
  97. }
  98. echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label><textarea class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['id'] ) . '" id="' . esc_attr( $field['id'] ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" rows="2" cols="20" ' . implode( ' ', $custom_attributes ) . '>' . esc_textarea( $field['value'] ) . '</textarea> ';
  99. if ( ! empty( $field['description'] ) ) {
  100. if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
  101. echo wc_help_tip( $field['description'] );
  102. } else {
  103. echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
  104. }
  105. }
  106. echo '</p>';
  107. }
  108. /**
  109. * Output a checkbox input box.
  110. *
  111. * @param array $field
  112. */
  113. function woocommerce_wp_checkbox( $field ) {
  114. global $thepostid, $post;
  115. $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
  116. $field['class'] = isset( $field['class'] ) ? $field['class'] : 'checkbox';
  117. $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
  118. $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
  119. $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
  120. $field['cbvalue'] = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'yes';
  121. $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
  122. // Custom attribute handling
  123. $custom_attributes = array();
  124. if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
  125. foreach ( $field['custom_attributes'] as $attribute => $value ){
  126. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
  127. }
  128. }
  129. echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label><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 ) . '/> ';
  130. if ( ! empty( $field['description'] ) ) {
  131. if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
  132. echo wc_help_tip( $field['description'] );
  133. } else {
  134. echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
  135. }
  136. }
  137. echo '</p>';
  138. }
  139. /**
  140. * Output a select input box.
  141. *
  142. * @param array $field
  143. */
  144. function woocommerce_wp_select( $field ) {
  145. global $thepostid, $post;
  146. $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
  147. $field['class'] = isset( $field['class'] ) ? $field['class'] : 'select short';
  148. $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
  149. $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
  150. $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
  151. $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
  152. // Custom attribute handling
  153. $custom_attributes = array();
  154. if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
  155. foreach ( $field['custom_attributes'] as $attribute => $value ){
  156. $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
  157. }
  158. }
  159. echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label><select id="' . esc_attr( $field['id'] ) . '" name="' . esc_attr( $field['name'] ) . '" class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" ' . implode( ' ', $custom_attributes ) . '>';
  160. foreach ( $field['options'] as $key => $value ) {
  161. echo '<option value="' . esc_attr( $key ) . '" ' . selected( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
  162. }
  163. echo '</select> ';
  164. if ( ! empty( $field['description'] ) ) {
  165. if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
  166. echo wc_help_tip( $field['description'] );
  167. } else {
  168. echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
  169. }
  170. }
  171. echo '</p>';
  172. }
  173. /**
  174. * Output a radio input box.
  175. *
  176. * @param array $field
  177. */
  178. function woocommerce_wp_radio( $field ) {
  179. global $thepostid, $post;
  180. $thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
  181. $field['class'] = isset( $field['class'] ) ? $field['class'] : 'select short';
  182. $field['style'] = isset( $field['style'] ) ? $field['style'] : '';
  183. $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
  184. $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
  185. $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
  186. echo '<fieldset class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><legend>' . wp_kses_post( $field['label'] ) . '</legend><ul class="wc-radios">';
  187. foreach ( $field['options'] as $key => $value ) {
  188. echo '<li><label><input
  189. name="' . esc_attr( $field['name'] ) . '"
  190. value="' . esc_attr( $key ) . '"
  191. type="radio"
  192. class="' . esc_attr( $field['class'] ) . '"
  193. style="' . esc_attr( $field['style'] ) . '"
  194. ' . checked( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '
  195. /> ' . esc_html( $value ) . '</label>
  196. </li>';
  197. }
  198. echo '</ul>';
  199. if ( ! empty( $field['description'] ) ) {
  200. if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
  201. echo wc_help_tip( $field['description'] );
  202. } else {
  203. echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
  204. }
  205. }
  206. echo '</fieldset>';
  207. }