/wp-content/plugins/learnpress/inc/libraries/meta-box/inc/field.php

https://gitlab.com/gregtyka/lfmawordpress · PHP · 319 lines · 149 code · 44 blank · 126 comment · 21 complexity · db9b220e023eba29d6f43ed5340afe45 MD5 · raw file

  1. <?php
  2. if ( ! class_exists( 'RWMB_Field ' ) ) {
  3. class RWMB_Field {
  4. /**
  5. * Add actions
  6. *
  7. * @return void
  8. */
  9. static function add_actions() {
  10. }
  11. /**
  12. * Enqueue scripts and styles
  13. *
  14. * @return void
  15. */
  16. static function admin_enqueue_scripts() {
  17. }
  18. /**
  19. * Show field HTML
  20. * Filters are put inside this method, not inside methods such as "meta", "html", "begin_html", etc.
  21. * That ensures the returned value are always been applied filters
  22. * This method is not meant to be overwritten in specific fields
  23. *
  24. * @param array $field
  25. * @param bool $saved
  26. *
  27. * @return string
  28. */
  29. static function show( $field, $saved ) {
  30. global $post;
  31. $field_class = RW_Meta_Box::get_class_name( $field );
  32. $meta = call_user_func( array( $field_class, 'meta' ), $post->ID, $saved, $field );
  33. // Apply filter to field meta value
  34. // 1st filter applies to all fields
  35. // 2nd filter applies to all fields with the same type
  36. // 3rd filter applies to current field only
  37. $meta = apply_filters( 'rwmb_field_meta', $meta, $field, $saved );
  38. $meta = apply_filters( "rwmb_{$field['type']}_meta", $meta, $field, $saved );
  39. $meta = apply_filters( "rwmb_{$field['id']}_meta", $meta, $field, $saved );
  40. $type = $field['type'];
  41. $id = $field['id'];
  42. $begin = call_user_func( array( $field_class, 'begin_html' ), $meta, $field );
  43. // Apply filter to field begin HTML
  44. // 1st filter applies to all fields
  45. // 2nd filter applies to all fields with the same type
  46. // 3rd filter applies to current field only
  47. $begin = apply_filters( 'rwmb_begin_html', $begin, $field, $meta );
  48. $begin = apply_filters( "rwmb_{$type}_begin_html", $begin, $field, $meta );
  49. $begin = apply_filters( "rwmb_{$id}_begin_html", $begin, $field, $meta );
  50. // Separate code for cloneable and non-cloneable fields to make easy to maintain
  51. // Cloneable fields
  52. if ( $field['clone'] ) {
  53. $meta = (array) $meta;
  54. $field_html = '';
  55. foreach ( $meta as $index => $sub_meta ) {
  56. $sub_field = $field;
  57. $sub_field['field_name'] = $field['field_name'] . "[{$index}]";
  58. if ( $index > 0 ) {
  59. if ( isset( $sub_field['address_field'] ) ) {
  60. $sub_field['address_field'] = $field['address_field'] . "_{$index}";
  61. }
  62. $sub_field['id'] = $field['id'] . "_{$index}";
  63. }
  64. if ( $field['multiple'] ) {
  65. $sub_field['field_name'] .= '[]';
  66. }
  67. // Wrap field HTML in a div with class="rwmb-clone" if needed
  68. $input_html = '<div class="rwmb-clone">';
  69. // Call separated methods for displaying each type of field
  70. $input_html .= call_user_func( array( $field_class, 'html' ), $sub_meta, $sub_field );
  71. // Apply filter to field HTML
  72. // 1st filter applies to all fields with the same type
  73. // 2nd filter applies to current field only
  74. $input_html = apply_filters( "rwmb_{$type}_html", $input_html, $field, $sub_meta );
  75. $input_html = apply_filters( "rwmb_{$id}_html", $input_html, $field, $sub_meta );
  76. // Remove clone button
  77. $input_html .= call_user_func( array( $field_class, 'remove_clone_button' ), $sub_meta, $sub_field );
  78. $input_html .= '</div>';
  79. $field_html .= $input_html;
  80. }
  81. } // Non-cloneable fields
  82. else {
  83. // Call separated methods for displaying each type of field
  84. $field_html = call_user_func( array( $field_class, 'html' ), $meta, $field );
  85. // Apply filter to field HTML
  86. // 1st filter applies to all fields with the same type
  87. // 2nd filter applies to current field only
  88. $field_html = apply_filters( "rwmb_{$type}_html", $field_html, $field, $meta );
  89. $field_html = apply_filters( "rwmb_{$id}_html", $field_html, $field, $meta );
  90. }
  91. $end = call_user_func( array( $field_class, 'end_html' ), $meta, $field );
  92. // Apply filter to field end HTML
  93. // 1st filter applies to all fields
  94. // 2nd filter applies to all fields with the same type
  95. // 3rd filter applies to current field only
  96. $end = apply_filters( 'rwmb_end_html', $end, $field, $meta );
  97. $end = apply_filters( "rwmb_{$type}_end_html", $end, $field, $meta );
  98. $end = apply_filters( "rwmb_{$id}_end_html", $end, $field, $meta );
  99. // Apply filter to field wrapper
  100. // This allow users to change whole HTML markup of the field wrapper (i.e. table row)
  101. // 1st filter applies to all fields
  102. // 1st filter applies to all fields with the same type
  103. // 2nd filter applies to current field only
  104. $html = apply_filters( 'rwmb_wrapper_html', "{$begin}{$field_html}{$end}", $field, $meta );
  105. $html = apply_filters( "rwmb_{$type}_wrapper_html", $html, $field, $meta );
  106. $html = apply_filters( "rwmb_{$id}_wrapper_html", $html, $field, $meta );
  107. // Display label and input in DIV and allow user-defined classes to be appended
  108. $classes = array( 'rwmb-field', "rwmb-{$type}-wrapper" );
  109. if ( 'hidden' === $field['type'] ) {
  110. $classes[] = 'hidden';
  111. }
  112. if ( ! empty( $field['required'] ) ) {
  113. $classes[] = 'required';
  114. }
  115. if ( ! empty( $field['class'] ) ) {
  116. $classes[] = $field['class'];
  117. }
  118. $outer_html = sprintf(
  119. $field['before'] . '<div class="%s">%s</div>' . $field['after'],
  120. implode( ' ', $classes ),
  121. $html
  122. );
  123. // Allow to change output of outer div
  124. // 1st filter applies to all fields
  125. // 1st filter applies to all fields with the same type
  126. // 2nd filter applies to current field only
  127. $outer_html = apply_filters( 'rwmb_outer_html', $outer_html, $field, $meta );
  128. $outer_html = apply_filters( "rwmb_{$type}_outer_html", $outer_html, $field, $meta );
  129. $outer_html = apply_filters( "rwmb_{$id}_outer_html", $outer_html, $field, $meta );
  130. echo $outer_html;
  131. }
  132. /**
  133. * Get field HTML
  134. *
  135. * @param mixed $meta
  136. * @param array $field
  137. *
  138. * @return string
  139. */
  140. static function html( $meta, $field ) {
  141. return '';
  142. }
  143. /**
  144. * Show begin HTML markup for fields
  145. *
  146. * @param mixed $meta
  147. * @param array $field
  148. *
  149. * @return string
  150. */
  151. static function begin_html( $meta, $field ) {
  152. if ( empty( $field['name'] ) ) {
  153. return '<div class="rwmb-input">';
  154. }
  155. return sprintf(
  156. '<div class="rwmb-label">
  157. <label for="%s">%s</label>
  158. </div>
  159. <div class="rwmb-input">',
  160. $field['id'],
  161. $field['name']
  162. );
  163. }
  164. /**
  165. * Show end HTML markup for fields
  166. *
  167. * @param mixed $meta
  168. * @param array $field
  169. *
  170. * @return string
  171. */
  172. static function end_html( $meta, $field ) {
  173. $button = $field['clone'] ? call_user_func( array( RW_Meta_Box::get_class_name( $field ), 'add_clone_button' ) ) : '';
  174. $desc = $field['desc'] ? "<p id='{$field['id']}_description' class='description'>{$field['desc']}</p>" : '';
  175. // Closes the container
  176. $html = "{$button}{$desc}</div>";
  177. return $html;
  178. }
  179. /**
  180. * Add clone button
  181. *
  182. * @return string $html
  183. */
  184. static function add_clone_button() {
  185. return '<a href="#" class="rwmb-button button-primary add-clone">' . __( '+', 'learnpress'/*'meta-box'*/ ) . '</a>';
  186. }
  187. /**
  188. * Remove clone button
  189. *
  190. * @return string $html
  191. */
  192. static function remove_clone_button() {
  193. return '<a href="#" class="rwmb-button button remove-clone">' . __( '&#8211;', 'learnpress'/*'meta-box'*/ ) . '</a>';
  194. }
  195. /**
  196. * Get meta value
  197. *
  198. * @param int $post_id
  199. * @param bool $saved
  200. * @param array $field
  201. *
  202. * @return mixed
  203. */
  204. static function meta( $post_id, $saved, $field ) {
  205. $meta = get_post_meta( $post_id, $field['id'], ! $field['multiple'] );
  206. // Use $field['std'] only when the meta box hasn't been saved (i.e. the first time we run)
  207. $meta = ( ! $saved && '' === $meta || array() === $meta ) ? $field['std'] : $meta;
  208. // Escape attributes for non-wysiwyg fields
  209. if ( 'wysiwyg' !== $field['type'] ) {
  210. $meta = is_array( $meta ) ? array_map( 'esc_attr', $meta ) : esc_attr( $meta );
  211. }
  212. return $meta;
  213. }
  214. /**
  215. * Set value of meta before saving into database
  216. *
  217. * @param mixed $new
  218. * @param mixed $old
  219. * @param int $post_id
  220. * @param array $field
  221. *
  222. * @return int
  223. */
  224. static function value( $new, $old, $post_id, $field ) {
  225. return $new;
  226. }
  227. /**
  228. * Save meta value
  229. *
  230. * @param $new
  231. * @param $old
  232. * @param $post_id
  233. * @param $field
  234. */
  235. static function save( $new, $old, $post_id, $field ) {
  236. $name = $field['id'];
  237. if ( '' === $new || array() === $new ) {
  238. delete_post_meta( $post_id, $name );
  239. return;
  240. }
  241. if ( $field['multiple'] ) {
  242. foreach ( $new as $new_value ) {
  243. if ( ! in_array( $new_value, $old ) ) {
  244. add_post_meta( $post_id, $name, $new_value, false );
  245. }
  246. }
  247. foreach ( $old as $old_value ) {
  248. if ( ! in_array( $old_value, $new ) ) {
  249. delete_post_meta( $post_id, $name, $old_value );
  250. }
  251. }
  252. } else {
  253. if ( $field['clone'] ) {
  254. $new = (array) $new;
  255. foreach ( $new as $k => $v ) {
  256. if ( '' === $v ) {
  257. unset( $new[$k] );
  258. }
  259. }
  260. }
  261. update_post_meta( $post_id, $name, $new );
  262. }
  263. }
  264. /**
  265. * Normalize parameters for field
  266. *
  267. * @param array $field
  268. *
  269. * @return array
  270. */
  271. static function normalize_field( $field ) {
  272. return $field;
  273. }
  274. }
  275. }