/plugin/fituet-event/metaboxio/inc/helpers.php

https://gitlab.com/thongta/fituet · PHP · 240 lines · 126 code · 25 blank · 89 comment · 25 complexity · 74550666125a05cc5be9dc3b98353ced MD5 · raw file

  1. <?php
  2. /**
  3. * This file contains all helpers/public functions
  4. * that can be used both on the back-end or front-end
  5. */
  6. // Prevent loading this file directly
  7. defined( 'ABSPATH' ) || exit;
  8. if ( ! class_exists( 'RWMB_Helper' ) ) {
  9. /**
  10. * Wrapper class for helper functions
  11. */
  12. class RWMB_Helper {
  13. /**
  14. * Find field by field ID
  15. * This function finds field in meta boxes registered by 'rwmb_meta_boxes' filter
  16. * Note: if users use old code to add meta boxes, this function might not work properly
  17. *
  18. * @param string $field_id Field ID
  19. *
  20. * @return array|false Field params (array) if success. False otherwise.
  21. */
  22. static function find_field( $field_id ) {
  23. // Get all meta boxes registered with 'rwmb_meta_boxes' hook
  24. $meta_boxes = apply_filters( 'rwmb_meta_boxes', array() );
  25. // Find field
  26. foreach ( $meta_boxes as $meta_box ) {
  27. $meta_box = RW_Meta_Box::normalize( $meta_box );
  28. foreach ( $meta_box['fields'] as $field ) {
  29. if ( $field_id == $field['id'] ) {
  30. return $field;
  31. }
  32. }
  33. }
  34. return false;
  35. }
  36. /**
  37. * Get post meta
  38. *
  39. * @param string $key Meta key. Required.
  40. * @param int|null $post_id Post ID. null for current post. Optional
  41. * @param array $args Array of arguments. Optional.
  42. *
  43. * @return mixed
  44. */
  45. static function meta( $key, $args = array(), $post_id = null ) {
  46. $post_id = empty( $post_id ) ? get_the_ID() : $post_id;
  47. $args = wp_parse_args(
  48. $args, array(
  49. 'type' => 'text',
  50. 'multiple' => false,
  51. )
  52. );
  53. // Always set 'multiple' true for following field types
  54. if ( in_array( $args['type'], array( 'checkbox_list', 'file', 'file_advanced', 'image', 'image_advanced', 'plupload_image', 'thickbox_image' ) ) ) {
  55. $args['multiple'] = true;
  56. }
  57. $meta = get_post_meta( $post_id, $key, ! $args['multiple'] );
  58. // Get uploaded files info
  59. if ( in_array( $args['type'], array( 'file', 'file_advanced' ) ) ) {
  60. if ( is_array( $meta ) && ! empty( $meta ) ) {
  61. $files = array();
  62. foreach ( $meta as $id ) {
  63. // Get only info of existing attachments
  64. if ( get_attached_file( $id ) ) {
  65. $files[$id] = RWMB_File_Field::file_info( $id );
  66. }
  67. }
  68. $meta = $files;
  69. }
  70. } // Get uploaded images info
  71. elseif ( in_array( $args['type'], array( 'image', 'plupload_image', 'thickbox_image', 'image_advanced' ) ) ) {
  72. if ( is_array( $meta ) && ! empty( $meta ) ) {
  73. $images = array();
  74. foreach ( $meta as $id ) {
  75. // Get only info of existing attachments
  76. if ( get_attached_file( $id ) ) {
  77. $images[$id] = RWMB_Image_Field::file_info( $id, $args );
  78. }
  79. }
  80. $meta = $images;
  81. }
  82. } // Get terms
  83. elseif ( 'taxonomy_advanced' == $args['type'] ) {
  84. if ( ! empty( $args['taxonomy'] ) ) {
  85. $term_ids = array_map( 'intval', array_filter( explode( ',', $meta . ',' ) ) );
  86. // Allow to pass more arguments to "get_terms"
  87. $func_args = wp_parse_args(
  88. array(
  89. 'include' => $term_ids,
  90. 'hide_empty' => false,
  91. ), $args
  92. );
  93. unset( $func_args['type'], $func_args['taxonomy'], $func_args['multiple'] );
  94. $meta = get_terms( $args['taxonomy'], $func_args );
  95. } else {
  96. $meta = array();
  97. }
  98. } // Get post terms
  99. elseif ( 'taxonomy' == $args['type'] ) {
  100. $meta = empty( $args['taxonomy'] ) ? array() : wp_get_post_terms( $post_id, $args['taxonomy'] );
  101. } // Get map
  102. elseif ( 'map' == $args['type'] ) {
  103. $field = array(
  104. 'id' => $key,
  105. 'multiple' => false,
  106. 'clone' => false,
  107. );
  108. $meta = RWMB_Map_Field::the_value( $field, $args, $post_id );
  109. } elseif ( 'oembed' == $args['type'] ) {
  110. $meta = ( $embed = @wp_oembed_get( $meta ) ) ? $embed : $meta;
  111. }
  112. return apply_filters( 'rwmb_meta', $meta, $key, $args, $post_id );
  113. }
  114. }
  115. }
  116. if ( ! function_exists( 'rwmb_meta' ) ) {
  117. /**
  118. * Get post meta
  119. *
  120. * @param string $key Meta key. Required.
  121. * @param int|null $post_id Post ID. null for current post. Optional
  122. * @param array $args Array of arguments. Optional.
  123. *
  124. * @return mixed
  125. */
  126. function rwmb_meta( $key, $args = array(), $post_id = null ) {
  127. return RWMB_Helper::meta( $key, $args, $post_id );
  128. }
  129. }
  130. if ( ! function_exists( 'rwmb_get_field' ) ) {
  131. /**
  132. * Get value of custom field.
  133. * This is used to replace old version of rwmb_meta key.
  134. *
  135. * @param string $field_id Field ID. Required.
  136. * @param array $args Additional arguments. Rarely used. See specific fields for details
  137. * @param int|null $post_id Post ID. null for current post. Optional.
  138. *
  139. * @return mixed false if field doesn't exist. Field value otherwise.
  140. */
  141. function rwmb_get_field( $field_id, $args = array(), $post_id = null ) {
  142. $field = RWMB_Helper::find_field( $field_id );
  143. // Get field value
  144. $value = $field ? call_user_func( array( RW_Meta_Box::get_class_name( $field ), 'get_value' ), $field, $args, $post_id ) : false;
  145. /**
  146. * Allow developers to change the returned value of field
  147. *
  148. * @param mixed $value Field value
  149. * @param array $field Field parameter
  150. * @param array $args Additional arguments. Rarely used. See specific fields for details
  151. * @param int|null $post_id Post ID. null for current post. Optional.
  152. */
  153. $value = apply_filters( 'rwmb_get_field', $value, $field, $args, $post_id );
  154. return $value;
  155. }
  156. }
  157. if ( ! function_exists( 'rwmb_the_field' ) ) {
  158. /**
  159. * Display the value of a field
  160. *
  161. * @param string $field_id Field ID. Required.
  162. * @param array $args Additional arguments. Rarely used. See specific fields for details
  163. * @param int|null $post_id Post ID. null for current post. Optional.
  164. * @param bool $echo Display field meta value? Default `true` which works in almost all cases. We use `false` for the [rwmb_meta] shortcode
  165. *
  166. * @return string
  167. */
  168. function rwmb_the_field( $field_id, $args = array(), $post_id = null, $echo = true ) {
  169. // Find field
  170. $field = RWMB_Helper::find_field( $field_id );
  171. if ( ! $field ) {
  172. return '';
  173. }
  174. $output = call_user_func( array( RW_Meta_Box::get_class_name( $field ), 'the_value' ), $field, $args, $post_id );
  175. /**
  176. * Allow developers to change the returned value of field
  177. *
  178. * @param mixed $value Field HTML output
  179. * @param array $field Field parameter
  180. * @param array $args Additional arguments. Rarely used. See specific fields for details
  181. * @param int|null $post_id Post ID. null for current post. Optional.
  182. */
  183. $output = apply_filters( 'rwmb_the_field', $output, $field, $args, $post_id );
  184. if ( $echo ) {
  185. echo $output;
  186. }
  187. return $output;
  188. }
  189. }
  190. if ( ! function_exists( 'rwmb_meta_shortcode' ) ) {
  191. /**
  192. * Shortcode to display meta value
  193. *
  194. * @param $atts Array of shortcode attributes, same as meta() function, but has more "meta_key" parameter
  195. *
  196. * @see meta() function below
  197. *
  198. * @return string
  199. */
  200. function rwmb_meta_shortcode( $atts ) {
  201. $atts = wp_parse_args(
  202. $atts, array(
  203. 'post_id' => get_the_ID(),
  204. )
  205. );
  206. if ( empty( $atts['meta_key'] ) ) {
  207. return '';
  208. }
  209. $field_id = $atts['meta_key'];
  210. $post_id = $atts['post_id'];
  211. unset( $atts['meta_key'], $atts['post_id'] );
  212. return rwmb_the_field( $field_id, $atts, $post_id, false );
  213. }
  214. add_shortcode( 'rwmb_meta', 'rwmb_meta_shortcode' );
  215. }