PluginProbe
Cool Flipbox – Shortcode & Gutenberg Block / 1.4
Cool Flipbox – Shortcode & Gutenberg Block v1.4
trunk 1.4 1.5 1.6 1.6.2 1.7.1 1.8.3 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1
flip-boxes / CMB2 / includes / helper-functions.php

helper-functions.php in Cool Flipbox – Shortcode & Gutenberg Block 1.4, at CMB2/includes/helper-functions.php

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