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

/wp-content/themes/tosin/framework/CMB2/includes/helper-functions.php

https://gitlab.com/websumon/tosnib
PHP | 308 lines | 125 code | 42 blank | 141 comment | 24 complexity | 2b9336483b4d8d3b1770845f2b52bad6 MD5 | raw file
  1. <?php
  2. /**
  3. * CMB2 Helper Functions
  4. *
  5. * @category WordPress_Plugin
  6. * @package CMB2
  7. * @author WebDevStudios
  8. * @license GPL-2.0+
  9. * @link http://webdevstudios.com
  10. */
  11. /**
  12. * Helper function to provide directory path to CMB2
  13. * @since 2.0.0
  14. * @param string $path Path to append
  15. * @return string Directory with optional path appended
  16. */
  17. function cmb2_dir( $path = '' ) {
  18. return CMB2_DIR . $path;
  19. }
  20. /**
  21. * Autoloads files with CMB2 classes when needed
  22. * @since 1.0.0
  23. * @param string $class_name Name of the class being requested
  24. */
  25. function cmb2_autoload_classes( $class_name ) {
  26. if ( 0 !== strpos( $class_name, 'CMB2' ) ) {
  27. return;
  28. }
  29. include_once( cmb2_dir( "includes/{$class_name}.php" ) );
  30. }
  31. /**
  32. * Get instance of the CMB2_Utils class
  33. * @since 2.0.0
  34. * @return CMB2_Utils object CMB2 utilities class
  35. */
  36. function cmb2_utils() {
  37. static $cmb2_utils;
  38. $cmb2_utils = $cmb2_utils ? $cmb2_utils : new CMB2_Utils();
  39. return $cmb2_utils;
  40. }
  41. /**
  42. * Get instance of the CMB2_Ajax class
  43. * @since 2.0.0
  44. * @return CMB2_Ajax object CMB2 utilities class
  45. */
  46. function cmb2_ajax() {
  47. static $cmb2_ajax;
  48. $cmb2_ajax = $cmb2_ajax ? $cmb2_ajax : new CMB2_Ajax();
  49. return $cmb2_ajax;
  50. }
  51. /**
  52. * Get instance of the CMB2_Option class for the passed metabox ID
  53. * @since 2.0.0
  54. * @return CMB2_Option object Options class for setting/getting options for metabox
  55. */
  56. function cmb2_options( $key ) {
  57. return CMB2_Options::get( $key );
  58. }
  59. /**
  60. * Get a cmb oEmbed. Handles oEmbed getting for non-post objects
  61. * @since 2.0.0
  62. * @param array $args Arguments. Accepts:
  63. *
  64. * 'url' - URL to retrieve the oEmbed from,
  65. * 'object_id' - $post_id,
  66. * 'object_type' - 'post',
  67. * 'oembed_args' - $embed_args, // array containing 'width', etc
  68. * 'field_id' - false,
  69. * 'cache_key' - false,
  70. *
  71. * @return string oEmbed string
  72. */
  73. function cmb2_get_oembed( $args = array() ) {
  74. return cmb2_ajax()->get_oembed( $args );
  75. }
  76. /**
  77. * A helper function to get an option from a CMB2 options array
  78. * @since 1.0.1
  79. * @param string $option_key Option key
  80. * @param string $field_id Option array field key
  81. * @param mixed $default Optional default fallback value
  82. * @return array Options array or specific field
  83. */
  84. function cmb2_get_option( $option_key, $field_id = '', $default = false ) {
  85. return cmb2_options( $option_key )->get( $field_id, $default );
  86. }
  87. /**
  88. * A helper function to update an option in a CMB2 options array
  89. * @since 2.0.0
  90. * @param string $option_key Option key
  91. * @param string $field_id Option array field key
  92. * @param mixed $value Value to update data with
  93. * @param boolean $single Whether data should not be an array
  94. * @return boolean Success/Failure
  95. */
  96. function cmb2_update_option( $option_key, $field_id, $value, $single = true ) {
  97. if ( cmb2_options( $option_key )->update( $field_id, $value, false, $single ) ) {
  98. return cmb2_options( $option_key )->set();
  99. }
  100. return false;
  101. }
  102. /**
  103. * Get a CMB2 field object.
  104. * @since 1.1.0
  105. * @param array $meta_box Metabox ID or Metabox config array
  106. * @param array $field_id Field ID or all field arguments
  107. * @param int $object_id Object ID
  108. * @param string $object_type Type of object being saved. (e.g., post, user, comment, or options-page).
  109. * Defaults to metabox object type.
  110. * @return CMB2_Field|null CMB2_Field object unless metabox config cannot be found
  111. */
  112. function cmb2_get_field( $meta_box, $field_id, $object_id = 0, $object_type = '' ) {
  113. $object_id = $object_id ? $object_id : get_the_ID();
  114. $cmb = $meta_box instanceof CMB2 ? $meta_box : cmb2_get_metabox( $meta_box, $object_id );
  115. if ( ! $cmb ) {
  116. return;
  117. }
  118. $cmb->object_type( $object_type ? $object_type : $cmb->mb_object_type() );
  119. return $cmb->get_field( $field_id );
  120. }
  121. /**
  122. * Get a field's value.
  123. * @since 1.1.0
  124. * @param array $meta_box Metabox ID or Metabox config array
  125. * @param array $field_id Field ID or all field arguments
  126. * @param int $object_id Object ID
  127. * @param string $object_type Type of object being saved. (e.g., post, user, comment, or options-page).
  128. * Defaults to metabox object type.
  129. * @return mixed Maybe escaped value
  130. */
  131. function cmb2_get_field_value( $meta_box, $field_id, $object_id = 0, $object_type = '' ) {
  132. $field = cmb2_get_field( $meta_box, $field_id, $object_id, $object_type );
  133. return $field->escaped_value();
  134. }
  135. /**
  136. * Because OOP can be scary
  137. * @since 2.0.2
  138. * @param array $meta_box_config Metabox Config array
  139. * @return CMB2 object Instantiated CMB2 object
  140. */
  141. function new_cmb2_box( array $meta_box_config ) {
  142. return cmb2_get_metabox( $meta_box_config );
  143. }
  144. /**
  145. * Retrieve a CMB2 instance by the metabox ID
  146. * @since 2.0.0
  147. * @param mixed $meta_box Metabox ID or Metabox config array
  148. * @param int $object_id Object ID
  149. * @param string $object_type Type of object being saved. (e.g., post, user, comment, or options-page).
  150. * Defaults to metabox object type.
  151. * @return CMB2 object
  152. */
  153. function cmb2_get_metabox( $meta_box, $object_id = 0, $object_type = '' ) {
  154. if ( $meta_box instanceof CMB2 ) {
  155. return $meta_box;
  156. }
  157. if ( is_string( $meta_box ) ) {
  158. $cmb = CMB2_Boxes::get( $meta_box );
  159. } else {
  160. // See if we already have an instance of this metabox
  161. $cmb = CMB2_Boxes::get( $meta_box['id'] );
  162. // If not, we'll initate a new metabox
  163. $cmb = $cmb ? $cmb : new CMB2( $meta_box, $object_id );
  164. }
  165. if ( $cmb && $object_id ) {
  166. $cmb->object_id( $object_id );
  167. }
  168. if ( $cmb && $object_type ) {
  169. $cmb->object_type( $object_type );
  170. }
  171. return $cmb;
  172. }
  173. /**
  174. * Returns array of sanitized field values from a metabox (without saving them)
  175. * @since 2.0.3
  176. * @param mixed $meta_box Metabox ID or Metabox config array
  177. * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data).
  178. * @return mixed Array of sanitized values or false if no CMB2 object found
  179. */
  180. function cmb2_get_metabox_sanitized_values( $meta_box, array $data_to_sanitize ) {
  181. $cmb = cmb2_get_metabox( $meta_box );
  182. return $cmb ? $cmb->get_sanitized_values( $data_to_sanitize ) : false;
  183. }
  184. /**
  185. * Retrieve a metabox form
  186. * @since 2.0.0
  187. * @param mixed $meta_box Metabox config array or Metabox ID
  188. * @param int $object_id Object ID
  189. * @param array $args Optional arguments array
  190. * @return string CMB2 html form markup
  191. */
  192. function cmb2_get_metabox_form( $meta_box, $object_id = 0, $args = array() ) {
  193. $object_id = $object_id ? $object_id : get_the_ID();
  194. $cmb = cmb2_get_metabox( $meta_box, $object_id );
  195. ob_start();
  196. // Get cmb form
  197. cmb2_print_metabox_form( $cmb, $object_id, $args );
  198. $form = ob_get_contents();
  199. ob_end_clean();
  200. return apply_filters( 'cmb2_get_metabox_form', $form, $object_id, $cmb );
  201. }
  202. /**
  203. * Display a metabox form & save it on submission
  204. * @since 1.0.0
  205. * @param mixed $meta_box Metabox config array or Metabox ID
  206. * @param int $object_id Object ID
  207. * @param array $args Optional arguments array
  208. */
  209. function cmb2_print_metabox_form( $meta_box, $object_id = 0, $args = array() ) {
  210. $object_id = $object_id ? $object_id : get_the_ID();
  211. $cmb = cmb2_get_metabox( $meta_box, $object_id );
  212. // if passing a metabox ID, and that ID was not found
  213. if ( ! $cmb ) {
  214. return;
  215. }
  216. $args = wp_parse_args( $args, array(
  217. 'form_format' => '<form class="cmb-form" method="post" id="%1$s" enctype="multipart/form-data" encoding="multipart/form-data"><input type="hidden" name="object_id" value="%2$s">%3$s<input type="submit" name="submit-cmb" value="%4$s" class="button-primary"></form>',
  218. 'save_button' => __( 'Save', 'cmb2' ),
  219. 'object_type' => $cmb->mb_object_type(),
  220. 'cmb_styles' => $cmb->prop( 'cmb_styles' ),
  221. 'enqueue_js' => $cmb->prop( 'enqueue_js' ),
  222. ) );
  223. // Set object type explicitly (rather than trying to guess from context)
  224. $cmb->object_type( $args['object_type'] );
  225. // Save the metabox if it's been submitted
  226. // check permissions
  227. // @todo more hardening?
  228. if (
  229. $cmb->prop( 'save_fields' )
  230. // check nonce
  231. && isset( $_POST['submit-cmb'], $_POST['object_id'], $_POST[ $cmb->nonce() ] )
  232. && wp_verify_nonce( $_POST[ $cmb->nonce() ], $cmb->nonce() )
  233. && $object_id && $_POST['object_id'] == $object_id
  234. ) {
  235. $cmb->save_fields( $object_id, $cmb->object_type(), $_POST );
  236. }
  237. // Enqueue JS/CSS
  238. if ( $args['cmb_styles'] ) {
  239. CMB2_hookup::enqueue_cmb_css();
  240. }
  241. if ( $args['enqueue_js'] ) {
  242. CMB2_hookup::enqueue_cmb_js();
  243. }
  244. $form_format = apply_filters( 'cmb2_get_metabox_form_format', $args['form_format'], $object_id, $cmb );
  245. $format_parts = explode( '%3$s', $form_format );
  246. // Show cmb form
  247. printf( $format_parts[0], $cmb->cmb_id, $object_id );
  248. $cmb->show_form();
  249. if ( isset( $format_parts[1] ) && $format_parts[1] ) {
  250. printf( str_ireplace( '%4$s', '%1$s', $format_parts[1] ), $args['save_button'] );
  251. }
  252. }
  253. /**
  254. * Display a metabox form (or optionally return it) & save it on submission
  255. * @since 1.0.0
  256. * @param mixed $meta_box Metabox config array or Metabox ID
  257. * @param int $object_id Object ID
  258. * @param array $args Optional arguments array
  259. */
  260. function cmb2_metabox_form( $meta_box, $object_id = 0, $args = array() ) {
  261. if ( ! isset( $args['echo'] ) || $args['echo'] ) {
  262. cmb2_print_metabox_form( $meta_box, $object_id, $args );
  263. } else {
  264. return cmb2_get_metabox_form( $meta_box, $object_id, $args );
  265. }
  266. }