Completed
Pull Request — trunk (#848)
by
unknown
04:35
created

helper-functions.php ➔ cmb2_get_field()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 8
nop 4
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 5
rs 8.8571
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 120.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * CMB2 Helper Functions
4
 *
5
 * @category  WordPress_Plugin
6
 * @package   CMB2
7
 * @author    WebDevStudios
8
 * @license   GPL-2.0+
9 6
 * @link      http://webdevstudios.com
10 1
 */
11
12
/**
13 5
 * Helper function to provide directory path to CMB2
14 5
 * @since  2.0.0
15 5
 * @param  string  $path Path to append
16 5
 * @return string        Directory with optional path appended
17 5
 */
18
function cmb2_dir( $path = '' ) {
19
	return CMB2_DIR . $path;
20
}
21
22
/**
23
 * Autoloads files with CMB2 classes when needed
24
 * @since  1.0.0
25
 * @param  string $class_name Name of the class being requested
26 10
 */
27 10
function cmb2_autoload_classes( $class_name ) {
28 10
	if ( 0 !== strpos( $class_name, 'CMB2' ) ) {
29
		return;
30
	}
31
32
	$path = 'includes';
33
34
	if ( 'CMB2_Type' === $class_name || 0 === strpos( $class_name, 'CMB2_Type_' ) ) {
35
		$path .= '/types';
36
	}
37 2
38 2
	if ( 'CMB2_REST' === $class_name || 0 === strpos( $class_name, 'CMB2_REST_' ) ) {
39 2
		$path .= '/rest-api';
40
	}
41
42
	include_once( cmb2_dir( "$path/{$class_name}.php" ) );
43
}
44
45
/**
46
 * Get instance of the CMB2_Utils class
47
 * @since  2.0.0
48 4
 * @return CMB2_Utils object CMB2 utilities class
49
 */
50
function cmb2_utils() {
51
	static $cmb2_utils;
52
	$cmb2_utils = $cmb2_utils ? $cmb2_utils : new CMB2_Utils();
53
	return $cmb2_utils;
54
}
55
56
/**
57
 * Get instance of the CMB2_Ajax class
58
 * @since  2.0.0
59
 * @return CMB2_Ajax object CMB2 ajax class
60
 */
61
function cmb2_ajax() {
62
	return CMB2_Ajax::get_instance();
63
}
64
65
/**
66 1
 * Get instance of the CMB2_Option class for the passed metabox ID
67
 * @since  2.0.0
68
 * @return CMB2_Option object Options class for setting/getting options for metabox
69
 */
70
function cmb2_options( $key ) {
71
	return CMB2_Options::get( $key );
72
}
73
74
/**
75
 * Get a cmb oEmbed. Handles oEmbed getting for non-post objects
76
 * @since  2.0.0
77 2
 * @param  array   $args Arguments. Accepts:
78
 *
79
 *         'url'         - URL to retrieve the oEmbed from,
80
 *         'object_id'   - $post_id,
81
 *         'object_type' - 'post',
82
 *         'oembed_args' - $embed_args, // array containing 'width', etc
83
 *         'field_id'    - false,
84
 *         'cache_key'   - false,
85
 *         'wp_error'    - true/false, // To return a wp_error object if no embed found.
86
 *
87
 * @return string        oEmbed string
88
 */
89
function cmb2_get_oembed( $args = array() ) {
90 1
	$oembed = cmb2_ajax()->get_oembed_no_edit( $args );
91 1
92
	// Send back our embed
93
	if ( $oembed['embed'] && $oembed['embed'] != $oembed['fallback'] ) {
94
		return '<div class="cmb2-oembed">' . $oembed['embed'] . '</div>';
95
	}
96
97
	$error = sprintf(
98
		/* translators: 1: results for. 2: link to codex.wordpress.org/Embeds */
99
		esc_html__( 'No oEmbed Results Found for %1$s. View more info at %2$s.', 'cmb2' ),
100
		$oembed['fallback'],
101
		'<a href="https://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>'
102
	);
103
104
	if ( isset( $args['wp_error'] ) && $args['wp_error'] ) {
105
		return new WP_Error( 'cmb2_get_oembed_result', $error, compact( 'oembed', 'args' ) );
106
	}
107
108 6
	// Otherwise, send back error info that no oEmbeds were found
109 6
	return '<p class="ui-state-error-text">' . $error . '</p>';
110
}
111 6
112
/**
113
 * Outputs the return of cmb2_get_oembed.
114
 * @since  2.2.2
115 6
 * @see cmb2_get_oembed
116 6
 */
117
function cmb2_do_oembed( $args = array() ) {
118 6
	echo cmb2_get_oembed( $args );
119
}
120
add_action( 'cmb2_do_oembed', 'cmb2_do_oembed' );
121
122
/**
123
 * A helper function to get an option from a CMB2 options array
124
 * @since  1.0.1
125
 * @param  string  $option_key Option key
126 6
 * @param  string  $field_id   Option array field key
127 6
 * @param  mixed   $default    Optional default fallback value
128 6
 * @return array               Options array or specific field
129
 */
130 6
function cmb2_get_option( $option_key, $field_id = '', $default = false ) {
131 6
	return cmb2_options( $option_key )->get( $field_id, $default );
132 6
}
133 6
134 6
/**
135
 * A helper function to update an option in a CMB2 options array
136
 * @since  2.0.0
137
 * @param  string  $option_key Option key
138
 * @param  string  $field_id   Option array field key
139
 * @param  mixed   $value      Value to update data with
140
 * @param  boolean $single     Whether data should not be an array
141
 * @return boolean             Success/Failure
142
 */
143
function cmb2_update_option( $option_key, $field_id, $value, $single = true ) {
144
	if ( cmb2_options( $option_key )->update( $field_id, $value, false, $single ) ) {
145
		return cmb2_options( $option_key )->set();
146
	}
147
148
	return false;
149
}
150 1
151 1
/**
152
 * Get a CMB2 field object.
153
 * @since  1.1.0
154
 * @param  array  $meta_box    Metabox ID or Metabox config array
155
 * @param  array  $field_id    Field ID or all field arguments
156
 * @param  int    $object_id   Object ID
157
 * @param  string $object_type Type of object being saved. (e.g., post, user, comment, or options-page).
158
 *                             Defaults to metabox object type.
159
 * @return CMB2_Field|null     CMB2_Field object unless metabox config cannot be found
160
 */
161
function cmb2_get_field( $meta_box, $field_id, $object_id = 0, $object_type = '' ) {
162 8
163 1
	$object_id = $object_id ? $object_id : get_the_ID();
164
	$cmb = $meta_box instanceof CMB2 ? $meta_box : cmb2_get_metabox( $meta_box, $object_id );
165
166 8
	if ( ! $cmb ) {
167 8
		return;
168 8
	}
169
170 1
	$cmb->object_type( $object_type ? $object_type : $cmb->mb_object_type() );
171
172 1
	return $cmb->get_field( $field_id );
173
}
174
175 8
/**
176 7
 * Get a field's value.
177 7
 * @since  1.1.0
178 8
 * @param  array  $meta_box    Metabox ID or Metabox config array
179
 * @param  array  $field_id    Field ID or all field arguments
180
 * @param  int    $object_id   Object ID
181
 * @param  string $object_type Type of object being saved. (e.g., post, user, comment, or options-page).
182
 *                             Defaults to metabox object type.
183
 * @return mixed               Maybe escaped value
184
 */
185
function cmb2_get_field_value( $meta_box, $field_id, $object_id = 0, $object_type = '' ) {
186
	$field = cmb2_get_field( $meta_box, $field_id, $object_id, $object_type );
187
	return $field->escaped_value();
188
}
189
190
/**
191 1
 * Because OOP can be scary
192 1
 * @since  2.0.2
193
 * @param  array $meta_box_config Metabox Config array
194 1
 * @return CMB2 object            Instantiated CMB2 object
195
 */
196 1
function new_cmb2_box( array $meta_box_config ) {
197 1
	return cmb2_get_metabox( $meta_box_config );
198 1
}
199
200 1
/**
201
 * Retrieve a CMB2 instance by the metabox ID
202
 * @since  2.0.0
203
 * @param  mixed  $meta_box    Metabox ID or Metabox config array
204
 * @param  int    $object_id   Object ID
205
 * @param  string $object_type Type of object being saved. (e.g., post, user, comment, or options-page).
206
 *                             Defaults to metabox object type.
207
 * @return CMB2 object
208
 */
209
function cmb2_get_metabox( $meta_box, $object_id = 0, $object_type = '' ) {
210
211
	if ( $meta_box instanceof CMB2 ) {
212 1
		return $meta_box;
213 1
	}
214
215
	if ( is_string( $meta_box ) ) {
216 1
		$cmb = CMB2_Boxes::get( $meta_box );
217
	} else {
218
		// See if we already have an instance of this metabox
219
		$cmb = CMB2_Boxes::get( $meta_box['id'] );
220
		// If not, we'll initate a new metabox
221 1
		$cmb = $cmb ? $cmb : new CMB2( $meta_box, $object_id );
222
	}
223
224
	if ( $cmb && $object_id ) {
225
		$cmb->object_id( $object_id );
226
	}
227
228 1
	if ( $cmb && $object_type ) {
229 1
		$cmb->object_type( $object_type );
230 1
	}
231 1
232
	return $cmb;
233
}
234
235
/**
236 1
 * Returns array of sanitized field values from a metabox (without saving them)
237 1
 * @since  2.0.3
238 1
 * @param  mixed $meta_box         Metabox ID or Metabox config array
239 1
 * @param  array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data).
240
 * @return mixed                   Array of sanitized values or false if no CMB2 object found
241 1
 */
242 1
function cmb2_get_metabox_sanitized_values( $meta_box, array $data_to_sanitize ) {
243 1
	$cmb = cmb2_get_metabox( $meta_box );
244 1
	return $cmb ? $cmb->get_sanitized_values( $data_to_sanitize ) : false;
245
}
246 1
247
/**
248 1
 * Retrieve a metabox form
249
 * @since  2.0.0
250
 * @param  mixed   $meta_box  Metabox config array or Metabox ID
251 1
 * @param  int     $object_id Object ID
252 1
 * @param  array   $args      Optional arguments array
253
 * @return string             CMB2 html form markup
254 1
 */
255 1
function cmb2_get_metabox_form( $meta_box, $object_id = 0, $args = array() ) {
256 1
257
	$object_id = $object_id ? $object_id : get_the_ID();
258 1
	$cmb       = cmb2_get_metabox( $meta_box, $object_id );
259
260
	ob_start();
261
	// Get cmb form
262
	cmb2_print_metabox_form( $cmb, $object_id, $args );
263
	$form = ob_get_clean();
264
265
	return apply_filters( 'cmb2_get_metabox_form', $form, $object_id, $cmb );
266
}
267
268
/**
269
 * Display a metabox form & save it on submission
270
 * @since  1.0.0
271
 * @param  mixed   $meta_box  Metabox config array or Metabox ID
272
 * @param  int     $object_id Object ID
273
 * @param  array   $args      Optional arguments array
274
 */
275
function cmb2_print_metabox_form( $meta_box, $object_id = 0, $args = array() ) {
0 ignored issues
show
Coding Style introduced by
cmb2_print_metabox_form uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
276
277
	$object_id = $object_id ? $object_id : get_the_ID();
278
	$cmb = cmb2_get_metabox( $meta_box, $object_id );
279
280
	// if passing a metabox ID, and that ID was not found
281
	if ( ! $cmb ) {
282
		return;
283
	}
284
285
	$args = wp_parse_args( $args, array(
286
		'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>',
287
		'save_button' => esc_html__( 'Save', 'cmb2' ),
288
		'object_type' => $cmb->mb_object_type(),
289
		'cmb_styles'  => $cmb->prop( 'cmb_styles' ),
290
		'enqueue_js'  => $cmb->prop( 'enqueue_js' ),
291
	) );
292
293
	// Set object type explicitly (rather than trying to guess from context)
294
	$cmb->object_type( $args['object_type'] );
295
296
	// Save the metabox if it's been submitted
297
	// check permissions
298
	// @todo more hardening?
299
	if (
300
		$cmb->prop( 'save_fields' )
301
		// check nonce
302
		&& isset( $_POST['submit-cmb'], $_POST['object_id'], $_POST[ $cmb->nonce() ] )
303
		&& wp_verify_nonce( $_POST[ $cmb->nonce() ], $cmb->nonce() )
304
		&& $object_id && $_POST['object_id'] == $object_id
305
	) {
306
		$cmb->save_fields( $object_id, $cmb->object_type(), $_POST );
307
	}
308
309
	// Enqueue JS/CSS
310
	if ( $args['cmb_styles'] ) {
311
		CMB2_hookup::enqueue_cmb_css();
312
	}
313
314
	if ( $args['enqueue_js'] ) {
315
		CMB2_hookup::enqueue_cmb_js();
316
	}
317
318
	$form_format = apply_filters( 'cmb2_get_metabox_form_format', $args['form_format'], $object_id, $cmb );
319
320
	$format_parts = explode( '%3$s', $form_format );
321
322
	// Show cmb form
323
	printf( $format_parts[0], $cmb->cmb_id, $object_id );
324
	$cmb->show_form();
325
326
	if ( isset( $format_parts[1] ) && $format_parts[1] ) {
327
		printf( str_ireplace( '%4$s', '%1$s', $format_parts[1] ), $args['save_button'] );
328
	}
329
330
}
331
332
/**
333
 * Display a metabox form (or optionally return it) & save it on submission
334
 * @since  1.0.0
335
 * @param  mixed   $meta_box  Metabox config array or Metabox ID
336
 * @param  int     $object_id Object ID
337
 * @param  array   $args      Optional arguments array
338
 */
339
function cmb2_metabox_form( $meta_box, $object_id = 0, $args = array() ) {
340
	if ( ! isset( $args['echo'] ) || $args['echo'] ) {
341
		cmb2_print_metabox_form( $meta_box, $object_id, $args );
342
	} else {
343
		return cmb2_get_metabox_form( $meta_box, $object_id, $args );
344
	}
345
}
346
347
if ( ! function_exists( 'date_create_from_format' ) ) {
348
349
	/**
350
	 * Reimplementation of DateTime::createFromFormat for PHP < 5.3. :(
351
	 * Borrowed from http://stackoverflow.com/questions/5399075/php-datetimecreatefromformat-in-5-2
352
	 *
353
	 * @param $date_format
354
	 * @param $date_value
355
	 *
356
	 * @return DateTime
357
	 */
358
	function date_create_from_format( $date_format, $date_value ) {
359
360
		$schedule_format = str_replace(
361
			array( 'M', 'Y', 'm', 'd', 'H', 'i', 'a' ),
362
			array('%b', '%Y', '%m', '%d', '%H', '%M', '%p' ),
363
			$date_format
364
		);
365
366
		/*
367
		 * %Y, %m and %d correspond to date()'s Y m and d.
368
		 * %I corresponds to H, %M to i and %p to a
369
		 */
370
		$parsed_time = strptime( $date_value, $schedule_format );
371
372
		$ymd = sprintf(
373
			/*
374
			 * This is a format string that takes six total decimal
375
			 * arguments, then left-pads them with zeros to either
376
			 * 4 or 2 characters, as needed
377
			 */
378
			'%04d-%02d-%02d %02d:%02d:%02d',
379
			$parsed_time['tm_year'] + 1900,  // This will be "111", so we need to add 1900.
380
			$parsed_time['tm_mon'] + 1,      // This will be the month minus one, so we add one.
381
			$parsed_time['tm_mday'],
382
			$parsed_time['tm_hour'],
383
			$parsed_time['tm_min'],
384
			$parsed_time['tm_sec']
385
		);
386
387
		return new DateTime($ymd);
388
	}
389
}
390