PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/kiwi-social-share/includes/lib/cmb2/includes/helper-functions.php

https://bitbucket.org/Netbog/mormirkam
PHP | 406 lines | 165 code | 56 blank | 185 comment | 34 complexity | 6f839a5267f76f9158039df475b43b3e MD5 | raw file
Possible License(s): GPL-2.0, MIT
  1. <?php
  2. /**
  3. * CMB2 Helper Functions
  4. *
  5. * @category WordPress_Plugin
  6. * @package CMB2
  7. * @author CMB2 team
  8. * @license GPL-2.0+
  9. * @link https://cmb2.io
  10. */
  11. /**
  12. * Helper function to provide directory path to CMB2
  13. *
  14. * @since 2.0.0
  15. * @param string $path Path to append
  16. * @return string Directory with optional path appended
  17. */
  18. function cmb2_dir( $path = '' ) {
  19. return CMB2_DIR . $path;
  20. }
  21. /**
  22. * Autoloads files with CMB2 classes when needed
  23. *
  24. * @since 1.0.0
  25. * @param string $class_name Name of the class being requested
  26. */
  27. function cmb2_autoload_classes( $class_name ) {
  28. if ( 0 !== strpos( $class_name, 'CMB2' ) ) {
  29. return;
  30. }
  31. $path = 'includes';
  32. if ( 'CMB2_Type' === $class_name || 0 === strpos( $class_name, 'CMB2_Type_' ) ) {
  33. $path .= '/types';
  34. }
  35. if ( 'CMB2_REST' === $class_name || 0 === strpos( $class_name, 'CMB2_REST_' ) ) {
  36. $path .= '/rest-api';
  37. }
  38. include_once( cmb2_dir( "$path/{$class_name}.php" ) );
  39. }
  40. /**
  41. * Get instance of the CMB2_Utils class
  42. *
  43. * @since 2.0.0
  44. * @return CMB2_Utils object CMB2 utilities class
  45. */
  46. function cmb2_utils() {
  47. static $cmb2_utils;
  48. $cmb2_utils = $cmb2_utils ? $cmb2_utils : new CMB2_Utils();
  49. return $cmb2_utils;
  50. }
  51. /**
  52. * Get instance of the CMB2_Ajax class
  53. *
  54. * @since 2.0.0
  55. * @return CMB2_Ajax object CMB2 ajax class
  56. */
  57. function cmb2_ajax() {
  58. return CMB2_Ajax::get_instance();
  59. }
  60. /**
  61. * Get instance of the CMB2_Option class for the passed metabox ID
  62. *
  63. * @since 2.0.0
  64. * @return CMB2_Option object Options class for setting/getting options for metabox
  65. */
  66. function cmb2_options( $key ) {
  67. return CMB2_Options::get( $key );
  68. }
  69. /**
  70. * Get a cmb oEmbed. Handles oEmbed getting for non-post objects
  71. *
  72. * @since 2.0.0
  73. * @param array $args Arguments. Accepts:
  74. *
  75. * 'url' - URL to retrieve the oEmbed from,
  76. * 'object_id' - $post_id,
  77. * 'object_type' - 'post',
  78. * 'oembed_args' - $embed_args, // array containing 'width', etc
  79. * 'field_id' - false,
  80. * 'cache_key' - false,
  81. * 'wp_error' - true/false, // To return a wp_error object if no embed found.
  82. *
  83. * @return string oEmbed string
  84. */
  85. function cmb2_get_oembed( $args = array() ) {
  86. $oembed = cmb2_ajax()->get_oembed_no_edit( $args );
  87. // Send back our embed
  88. if ( $oembed['embed'] && $oembed['embed'] != $oembed['fallback'] ) {
  89. return '<div class="cmb2-oembed">' . $oembed['embed'] . '</div>';
  90. }
  91. $error = sprintf(
  92. /* translators: 1: results for. 2: link to codex.wordpress.org/Embeds */
  93. esc_html__( 'No oEmbed Results Found for %1$s. View more info at %2$s.', 'cmb2' ),
  94. $oembed['fallback'],
  95. '<a href="https://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>'
  96. );
  97. if ( isset( $args['wp_error'] ) && $args['wp_error'] ) {
  98. return new WP_Error( 'cmb2_get_oembed_result', $error, compact( 'oembed', 'args' ) );
  99. }
  100. // Otherwise, send back error info that no oEmbeds were found
  101. return '<p class="ui-state-error-text">' . $error . '</p>';
  102. }
  103. /**
  104. * Outputs the return of cmb2_get_oembed.
  105. *
  106. * @since 2.2.2
  107. * @see cmb2_get_oembed
  108. */
  109. function cmb2_do_oembed( $args = array() ) {
  110. echo cmb2_get_oembed( $args );
  111. }
  112. add_action( 'cmb2_do_oembed', 'cmb2_do_oembed' );
  113. /**
  114. * A helper function to get an option from a CMB2 options array
  115. *
  116. * @since 1.0.1
  117. * @param string $option_key Option key
  118. * @param string $field_id Option array field key
  119. * @param mixed $default Optional default fallback value
  120. * @return array Options array or specific field
  121. */
  122. function cmb2_get_option( $option_key, $field_id = '', $default = false ) {
  123. return cmb2_options( $option_key )->get( $field_id, $default );
  124. }
  125. /**
  126. * A helper function to update an option in a CMB2 options array
  127. *
  128. * @since 2.0.0
  129. * @param string $option_key Option key
  130. * @param string $field_id Option array field key
  131. * @param mixed $value Value to update data with
  132. * @param boolean $single Whether data should not be an array
  133. * @return boolean Success/Failure
  134. */
  135. function cmb2_update_option( $option_key, $field_id, $value, $single = true ) {
  136. if ( cmb2_options( $option_key )->update( $field_id, $value, false, $single ) ) {
  137. return cmb2_options( $option_key )->set();
  138. }
  139. return false;
  140. }
  141. /**
  142. * Get a CMB2 field object.
  143. *
  144. * @since 1.1.0
  145. * @param array $meta_box Metabox ID or Metabox config array
  146. * @param array $field_id Field ID or all field arguments
  147. * @param int $object_id Object ID
  148. * @param string $object_type Type of object being saved. (e.g., post, user, comment, or options-page).
  149. * Defaults to metabox object type.
  150. * @return CMB2_Field|null CMB2_Field object unless metabox config cannot be found
  151. */
  152. function cmb2_get_field( $meta_box, $field_id, $object_id = 0, $object_type = '' ) {
  153. $object_id = $object_id ? $object_id : get_the_ID();
  154. $cmb = $meta_box instanceof CMB2 ? $meta_box : cmb2_get_metabox( $meta_box, $object_id );
  155. if ( ! $cmb ) {
  156. return;
  157. }
  158. $cmb->object_type( $object_type ? $object_type : $cmb->mb_object_type() );
  159. return $cmb->get_field( $field_id );
  160. }
  161. /**
  162. * Get a field's value.
  163. *
  164. * @since 1.1.0
  165. * @param array $meta_box Metabox ID or Metabox config array
  166. * @param array $field_id Field ID or all field arguments
  167. * @param int $object_id Object ID
  168. * @param string $object_type Type of object being saved. (e.g., post, user, comment, or options-page).
  169. * Defaults to metabox object type.
  170. * @return mixed Maybe escaped value
  171. */
  172. function cmb2_get_field_value( $meta_box, $field_id, $object_id = 0, $object_type = '' ) {
  173. $field = cmb2_get_field( $meta_box, $field_id, $object_id, $object_type );
  174. return $field->escaped_value();
  175. }
  176. /**
  177. * Because OOP can be scary
  178. *
  179. * @since 2.0.2
  180. * @param array $meta_box_config Metabox Config array
  181. * @return CMB2 object Instantiated CMB2 object
  182. */
  183. function new_cmb2_box( array $meta_box_config ) {
  184. return cmb2_get_metabox( $meta_box_config );
  185. }
  186. /**
  187. * Retrieve a CMB2 instance by the metabox ID
  188. *
  189. * @since 2.0.0
  190. * @param mixed $meta_box Metabox ID or Metabox config array
  191. * @param int $object_id Object ID
  192. * @param string $object_type Type of object being saved. (e.g., post, user, comment, or options-page).
  193. * Defaults to metabox object type.
  194. * @return CMB2 object
  195. */
  196. function cmb2_get_metabox( $meta_box, $object_id = 0, $object_type = '' ) {
  197. if ( $meta_box instanceof CMB2 ) {
  198. return $meta_box;
  199. }
  200. if ( is_string( $meta_box ) ) {
  201. $cmb = CMB2_Boxes::get( $meta_box );
  202. } else {
  203. // See if we already have an instance of this metabox
  204. $cmb = CMB2_Boxes::get( $meta_box['id'] );
  205. // If not, we'll initate a new metabox
  206. $cmb = $cmb ? $cmb : new CMB2( $meta_box, $object_id );
  207. }
  208. if ( $cmb && $object_id ) {
  209. $cmb->object_id( $object_id );
  210. }
  211. if ( $cmb && $object_type ) {
  212. $cmb->object_type( $object_type );
  213. }
  214. return $cmb;
  215. }
  216. /**
  217. * Returns array of sanitized field values from a metabox (without saving them)
  218. *
  219. * @since 2.0.3
  220. * @param mixed $meta_box Metabox ID or Metabox config array
  221. * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data).
  222. * @return mixed Array of sanitized values or false if no CMB2 object found
  223. */
  224. function cmb2_get_metabox_sanitized_values( $meta_box, array $data_to_sanitize ) {
  225. $cmb = cmb2_get_metabox( $meta_box );
  226. return $cmb ? $cmb->get_sanitized_values( $data_to_sanitize ) : false;
  227. }
  228. /**
  229. * Retrieve a metabox form
  230. *
  231. * @since 2.0.0
  232. * @param mixed $meta_box Metabox config array or Metabox ID
  233. * @param int $object_id Object ID
  234. * @param array $args Optional arguments array
  235. * @return string CMB2 html form markup
  236. */
  237. function cmb2_get_metabox_form( $meta_box, $object_id = 0, $args = array() ) {
  238. $object_id = $object_id ? $object_id : get_the_ID();
  239. $cmb = cmb2_get_metabox( $meta_box, $object_id );
  240. ob_start();
  241. // Get cmb form
  242. cmb2_print_metabox_form( $cmb, $object_id, $args );
  243. $form = ob_get_clean();
  244. return apply_filters( 'cmb2_get_metabox_form', $form, $object_id, $cmb );
  245. }
  246. /**
  247. * Display a metabox form & save it on submission
  248. *
  249. * @since 1.0.0
  250. * @param mixed $meta_box Metabox config array or Metabox ID
  251. * @param int $object_id Object ID
  252. * @param array $args Optional arguments array
  253. */
  254. function cmb2_print_metabox_form( $meta_box, $object_id = 0, $args = array() ) {
  255. $object_id = $object_id ? $object_id : get_the_ID();
  256. $cmb = cmb2_get_metabox( $meta_box, $object_id );
  257. // if passing a metabox ID, and that ID was not found
  258. if ( ! $cmb ) {
  259. return;
  260. }
  261. $args = wp_parse_args( $args, array(
  262. '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>',
  263. 'save_button' => esc_html__( 'Save', 'cmb2' ),
  264. 'object_type' => $cmb->mb_object_type(),
  265. 'cmb_styles' => $cmb->prop( 'cmb_styles' ),
  266. 'enqueue_js' => $cmb->prop( 'enqueue_js' ),
  267. ) );
  268. // Set object type explicitly (rather than trying to guess from context)
  269. $cmb->object_type( $args['object_type'] );
  270. // Save the metabox if it's been submitted
  271. // check permissions
  272. // @todo more hardening?
  273. if (
  274. $cmb->prop( 'save_fields' )
  275. // check nonce
  276. && isset( $_POST['submit-cmb'], $_POST['object_id'], $_POST[ $cmb->nonce() ] )
  277. && wp_verify_nonce( $_POST[ $cmb->nonce() ], $cmb->nonce() )
  278. && $object_id && $_POST['object_id'] == $object_id
  279. ) {
  280. $cmb->save_fields( $object_id, $cmb->object_type(), $_POST );
  281. }
  282. // Enqueue JS/CSS
  283. if ( $args['cmb_styles'] ) {
  284. CMB2_hookup::enqueue_cmb_css();
  285. }
  286. if ( $args['enqueue_js'] ) {
  287. CMB2_hookup::enqueue_cmb_js();
  288. }
  289. $form_format = apply_filters( 'cmb2_get_metabox_form_format', $args['form_format'], $object_id, $cmb );
  290. $format_parts = explode( '%3$s', $form_format );
  291. // Show cmb form
  292. printf( $format_parts[0], $cmb->cmb_id, $object_id );
  293. $cmb->show_form();
  294. if ( isset( $format_parts[1] ) && $format_parts[1] ) {
  295. printf( str_ireplace( '%4$s', '%1$s', $format_parts[1] ), $args['save_button'] );
  296. }
  297. }
  298. /**
  299. * Display a metabox form (or optionally return it) & save it on submission
  300. *
  301. * @since 1.0.0
  302. * @param mixed $meta_box Metabox config array or Metabox ID
  303. * @param int $object_id Object ID
  304. * @param array $args Optional arguments array
  305. */
  306. function cmb2_metabox_form( $meta_box, $object_id = 0, $args = array() ) {
  307. if ( ! isset( $args['echo'] ) || $args['echo'] ) {
  308. cmb2_print_metabox_form( $meta_box, $object_id, $args );
  309. } else {
  310. return cmb2_get_metabox_form( $meta_box, $object_id, $args );
  311. }
  312. }
  313. if ( ! function_exists( 'date_create_from_format' ) ) {
  314. /**
  315. * Reimplementation of DateTime::createFromFormat for PHP < 5.3. :(
  316. * Borrowed from http://stackoverflow.com/questions/5399075/php-datetimecreatefromformat-in-5-2
  317. *
  318. * @param $date_format
  319. * @param $date_value
  320. *
  321. * @return DateTime
  322. */
  323. function date_create_from_format( $date_format, $date_value ) {
  324. $schedule_format = str_replace(
  325. array( 'M', 'Y', 'm', 'd', 'H', 'i', 'a' ),
  326. array( '%b', '%Y', '%m', '%d', '%H', '%M', '%p' ),
  327. $date_format
  328. );
  329. /*
  330. * %Y, %m and %d correspond to date()'s Y m and d.
  331. * %I corresponds to H, %M to i and %p to a
  332. */
  333. $parsed_time = strptime( $date_value, $schedule_format );
  334. $ymd = sprintf(
  335. /*
  336. * This is a format string that takes six total decimal
  337. * arguments, then left-pads them with zeros to either
  338. * 4 or 2 characters, as needed
  339. */
  340. '%04d-%02d-%02d %02d:%02d:%02d',
  341. $parsed_time['tm_year'] + 1900, // This will be "111", so we need to add 1900.
  342. $parsed_time['tm_mon'] + 1, // This will be the month minus one, so we add one.
  343. $parsed_time['tm_mday'],
  344. $parsed_time['tm_hour'],
  345. $parsed_time['tm_min'],
  346. $parsed_time['tm_sec']
  347. );
  348. return new DateTime( $ymd );
  349. }
  350. }// End if().