Completed
Push — trunk ( ddc286...f54c26 )
by Justin
07:14
created

includes/CMB2_Field_Display.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * CMB2 field display base.
4
 *
5
 * @since 2.2.2
6
 *
7
 * @category  WordPress_Plugin
8
 * @package   CMB2
9
 * @author    WebDevStudios
10
 * @license   GPL-2.0+
11
 * @link      http://webdevstudios.com
12
 */
13
class CMB2_Field_Display {
14
15
	/**
16
	 * A CMB field object
17
	 * @var   CMB2_Field object
18
	 * @since 2.2.2
19
	 */
20
	public $field;
21
22
	/**
23
	 * The CMB field object's value.
24
	 * @var   mixed
25
	 * @since 2.2.2
26
	 */
27
	public $value;
28
29
	/**
30
	 * Get the corresponding display class for the field type.
31
	 * @since  2.2.2
32
	 * @param  CMB2_Field $field
33
	 * @return CMB2_Field_Display
34
	 */
35 33
	public static function get( CMB2_Field $field ) {
36 33
		switch ( $field->type() ) {
37 33
			case 'text_url':
38 1
				$type = new CMB2_Display_Text_Url( $field );
39 1
				break;
40 32
			case 'text_money':
41 1
				$type = new CMB2_Display_Text_Money( $field );
42 1
				break;
43 31
			case 'colorpicker':
44 1
				$type = new CMB2_Display_Colorpicker( $field );
45 1
				break;
46 30
			case 'checkbox':
47 3
				$type = new CMB2_Display_Checkbox( $field );
48 1
				break;
49 29
			case 'wysiwyg':
50 29
			case 'textarea_small':
51 3
				$type = new CMB2_Display_Textarea( $field );
52 3
				break;
53 26
			case 'textarea_code':
54 1
				$type = new CMB2_Display_Textarea_Code( $field );
55 1
				break;
56 25
			case 'text_time':
57 1
				$type = new CMB2_Display_Text_Time( $field );
58 1
				break;
59 24
			case 'text_date':
60 24
			case 'text_date_timestamp':
61 24
			case 'text_datetime_timestamp':
62 3
				$type = new CMB2_Display_Text_Date( $field );
63 3
				break;
64 21
			case 'text_datetime_timestamp_timezone':
65 1
				$type = new CMB2_Display_Text_Date_Timezone( $field );
66 1
				break;
67 20
			case 'select':
68 20
			case 'radio':
69 20
			case 'radio_inline':
70 3
				$type = new CMB2_Display_Select( $field );
71 3
				break;
72 17
			case 'multicheck':
73 17
			case 'multicheck_inline':
74 2
				$type = new CMB2_Display_Multicheck( $field );
75 2
				break;
76 15
			case 'taxonomy_radio':
77 15
			case 'taxonomy_radio_inline':
78 15
			case 'taxonomy_select':
79 3
				$type = new CMB2_Display_Taxonomy_Radio( $field );
80 3
				break;
81 12
			case 'taxonomy_multicheck':
82 12
			case 'taxonomy_multicheck_inline':
83 2
				$type = new CMB2_Display_Taxonomy_Multicheck( $field );
84 2
				break;
85 10
			case 'file':
86 1
				$type = new CMB2_Display_File( $field );
87 1
				break;
88 9
			case 'file_list':
89 1
				$type = new CMB2_Display_File_List( $field );
90 1
				break;
91 8
			case 'oembed':
92 1
				$type = new CMB2_Display_oEmbed( $field );
93 1
				break;
94 7
			default:
95 7
				$type = new self( $field );
96 7
				break;
97 33
		}
98
99 33
		return $type;
100
	}
101
102
	/**
103
	 * Setup our class vars
104
	 * @since 2.2.2
105
	 * @param CMB2_Field $field A CMB2 field object
106
	 */
107 33
	public function __construct( CMB2_Field $field ) {
108 33
		$this->field = $field;
109 33
		$this->value = $this->field->value;
110 33
	}
111
112
	/**
113
	 * Catchall method if field's 'display_cb' is NOT defined, or field type does
114
	 * not have a corresponding display method
115
	 * @since 2.2.2
116
	 * @param  string $method    Non-existent method name
117
	 * @param  array  $arguments All arguments passed to the method
118
	 */
119 33
	public function display() {
120
		// If repeatable
121 33
		if ( $this->field->args( 'repeatable' ) ) {
122
123
			// And has a repeatable value
124
			if ( is_array( $this->field->value ) ) {
125
126
				// Then loop and output.
127
				echo '<ul class="cmb2-'. str_replace( '_', '-', $this->field->type() ) .'">';
128
				foreach ( $this->field->value as $val ) {
129
					$this->value = $val;
130
					echo '<li>', $this->_display(), '</li>';
131
					;
132
				}
133
				echo '</ul>';
134
			}
135
136
		} else {
137 33
			$this->_display();
138
		}
139 33
	}
140
141
	/**
142
	 * Default fallback display method.
143
	 * @since 2.2.2
144
	 */
145 7
	protected function _display() {
146 7
		print_r( $this->value );
147 7
	}
148
}
149
150
class CMB2_Display_Text_Url extends CMB2_Field_Display {
151
	/**
152
	 * Display url value.
153
	 * @since 2.2.2
154
	 */
155 1
	protected function _display() {
156 1
		echo make_clickable( esc_url( $this->value ) );
157 1
	}
158
}
159
160
class CMB2_Display_Text_Money extends CMB2_Field_Display {
161
	/**
162
	 * Display text_money value.
163
	 * @since 2.2.2
164
	 */
165 1
	protected function _display() {
166 1
		$this->value = $this->value ? $this->value : '0';
167 1
		echo ( ! $this->field->get_param_callback_result( 'before_field' ) ? '$' : ' ' ), $this->value;
168 1
	}
169
}
170
171
class CMB2_Display_Colorpicker extends CMB2_Field_Display {
172
	/**
173
	 * Display color picker value.
174
	 * @since 2.2.2
175
	 */
176 1
	protected function _display() {
177 1
		echo '<span class="cmb2-colorpicker-swatch"><span style="background-color:', esc_attr( $this->value ), '"></span> ', esc_html( $this->value ), '</span>';
178 1
	}
179
}
180
181
class CMB2_Display_Checkbox extends CMB2_Field_Display {
182
	/**
183
	 * Display multicheck value.
184
	 * @since 2.2.2
185
	 */
186 1
	protected function _display() {
187 1
		echo $this->value === 'on' ? 'on' : 'off';
188 1
	}
189
}
190
191
class CMB2_Display_Select extends CMB2_Field_Display {
192
	/**
193
	 * Display select value.
194
	 * @since 2.2.2
195
	 */
196 3
	protected function _display() {
197 3
		$options = $this->field->options();
198
199 3
		$fallback = $this->field->args( 'show_option_none' );
200 3
		if ( ! $fallback && isset( $options[''] ) ) {
201
			$fallback = $options[''];
202
		}
203 3
		if ( ! $this->value && $fallback ) {
204
			echo $fallback;
205 3
		} elseif ( isset( $options[ $this->value ] ) ) {
206 3
			echo $options[ $this->value ];
207 3
		} else {
208
			echo esc_attr( $this->value );
209
		}
210 3
	}
211
}
212
213
class CMB2_Display_Multicheck extends CMB2_Field_Display {
214
	/**
215
	 * Display multicheck value.
216
	 * @since 2.2.2
217
	 */
218 2
	protected function _display() {
219 2
		if ( empty( $this->value ) || ! is_array( $this->value ) ) {
220
			return;
221
		}
222
223 2
		$options = $this->field->options();
224
225 2
		$output = array();
226 2
		foreach ( $this->value as $val ) {
227 2
			if ( isset( $options[ $val ] ) ) {
228 2
				$output[] = $options[ $val ];
229 2
			} else {
230
				$output[] = esc_attr( $val );
231
			}
232 2
		}
233
234 2
		echo implode( ', ', $output );
235 2
	}
236
}
237
238
class CMB2_Display_Textarea extends CMB2_Field_Display {
239
	/**
240
	 * Display textarea value.
241
	 * @since 2.2.2
242
	 */
243 3
	protected function _display() {
244 3
		echo wpautop( wp_kses_post( $this->value ) );
245 3
	}
246
}
247
248
class CMB2_Display_Textarea_Code extends CMB2_Field_Display {
249
	/**
250
	 * Display textarea_code value.
251
	 * @since 2.2.2
252
	 */
253 1
	protected function _display() {
254 1
		echo '<xmp class="cmb2-code">'. print_r( $this->value, true ) .'</xmp>';
255 1
	}
256
}
257
258
class CMB2_Display_Text_Time extends CMB2_Field_Display {
259
	/**
260
	 * Display text_time value.
261
	 * @since 2.2.2
262
	 */
263 1
	protected function _display() {
264 1
		echo $this->field->get_timestamp_format( 'time_format', $this->value );
265 1
	}
266
}
267
268
class CMB2_Display_Text_Date extends CMB2_Field_Display {
269
	/**
270
	 * Display text_date value.
271
	 * @since 2.2.2
272
	 */
273 3
	protected function _display() {
274 3
		echo $this->field->get_timestamp_format( 'date_format', $this->value );
275 3
	}
276
}
277
278
class CMB2_Display_Text_Date_Timezone extends CMB2_Field_Display {
279
	/**
280
	 * Display text_datetime_timestamp_timezone value.
281
	 * @since 2.2.2
282
	 */
283 1
	protected function _display() {
284 1
		$field = $this->field;
285
286 1
		if ( empty( $this->value ) ) {
287
			return;
288
		}
289
290 1
		$datetime = maybe_unserialize( $this->value );
291 1
		$this->value = $tzstring = '';
292
293 1 View Code Duplication
		if ( $datetime && $datetime instanceof DateTime ) {
294 1
			$tz       = $datetime->getTimezone();
295 1
			$tzstring = $tz->getName();
296 1
			$this->value    = $datetime->getTimestamp();
297 1
		}
298
299 1
		$date = $this->field->get_timestamp_format( 'date_format', $this->value );
300 1
		$time = $this->field->get_timestamp_format( 'time_format', $this->value );
301
302 1
		echo $date, ( $time ? ' ' . $time : '' ), ( $tzstring ? ', ' . $tzstring : '' );
303 1
	}
304
}
305
306
class CMB2_Display_Taxonomy_Radio extends CMB2_Field_Display {
307
	/**
308
	 * Display single taxonomy value.
309
	 * @since 2.2.2
310
	 */
311 3
	protected function _display() {
312 3
		$taxonomy   = $this->field->args( 'taxonomy' );
313 3
		$field_type = new CMB2_Type_Taxonomy_Radio( new CMB2_Types( $this->field ) );
314 3
		$terms      = $field_type->get_object_terms();
315 3
		$term       = false;
316
317 3
		if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) {
318
			$term = get_term_by( 'slug', $default, $taxonomy );
319 3
		} elseif ( ! empty( $terms ) ) {
320 3
			$term = $terms[key( $terms )];
321 3
		}
322
323 3
		if ( $term ) {
324 3
			$link = get_edit_term_link( $term->term_id, $taxonomy );
325 3
			echo '<a href="', esc_url( $link ), '">', esc_html( $term->name ), '</a>';
326 3
		}
327 3
	}
328
}
329
330
class CMB2_Display_Taxonomy_Multicheck extends CMB2_Field_Display {
331
	/**
332
	 * Display taxonomy values.
333
	 * @since 2.2.2
334
	 */
335 2
	protected function _display() {
336 2
		$taxonomy   = $this->field->args( 'taxonomy' );
337 2
		$field_type = new CMB2_Type_Taxonomy_Multicheck( new CMB2_Types( $this->field ) );
338 2
		$terms      = $field_type->get_object_terms();
339
340 2
		if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) {
341
			$terms = array();
342
			if ( is_array( $default ) ) {
343
				foreach ( $default as $slug ) {
344
					$terms[] = get_term_by( 'slug', $slug, $taxonomy );
345
				}
346
			} else {
347
				$terms[] = get_term_by( 'slug', $default, $taxonomy );
348
			}
349
		}
350
351 2
		if ( is_array( $terms ) ) {
352
353 2
			$links = array();
354 2
			foreach ( $terms as $term ) {
355 2
				$link = get_edit_term_link( $term->term_id, $taxonomy );
356 2
				$links[] = '<a href="'. esc_url( $link ) .'">'. esc_html( $term->name ) .'</a>';
357 2
			}
358
			// Then loop and output.
359 2
			echo '<div class="cmb2-taxonomy-terms-', esc_attr( $taxonomy ), '">';
360 2
			echo implode( ', ', $links );
361 2
			echo '</div>';
362 2
		}
363 2
	}
364
}
365
366
class CMB2_Display_File extends CMB2_Field_Display {
367
	/**
368
	 * Display file value.
369
	 * @since 2.2.2
370
	 */
371 1
	protected function _display() {
372 1
		if ( empty( $this->value ) ) {
373
			return;
374
		}
375
376 1
		$this->value = esc_url_raw( $this->value );
377
378 1
		$field_type = new CMB2_Type_File_Base( new CMB2_Types( $this->field ) );
379
380 1
		$id = $this->field->get_field_clone( array(
381 1
			'id' => $field_type->_id() . '_id',
382 1
		) )->escaped_value( 'absint' );
383
384 1
		$this->file_output( $this->value, $id, $field_type );
385 1
	}
386
387 2
	protected function file_output( $url_value, $id, CMB2_Type_File_Base $field_type ) {
388
		// If there is no ID saved yet, try to get it from the url
389 2
		if ( $url_value && ! $id ) {
390
			$id = cmb2_utils()->image_id_from_url( esc_url_raw( $url_value ) );
391
		}
392
393 2
		if ( $field_type->is_valid_img_ext( $url_value ) ) {
394
			$img_size = $this->field->args( 'preview_size' );
395
396 View Code Duplication
			if ( $id ) {
397
				$image = wp_get_attachment_image( $id, $img_size, null, array( 'class' => 'cmb-image-display' ) );
398
			} else {
399
				$size = is_array( $img_size ) ? $img_size[0] : 200;
400
				$image = '<img class="cmb-image-display" style="max-width: ' . absint( $size ) . 'px; width: 100%; height: auto;" src="' . $url_value . '" alt="" />';
401
			}
402
403
			echo $image;
404
405
		} else {
406
407 2
			printf( '<div class="file-status"><span>%1$s <strong><a href="%2$s">%3$s</a></strong></span></div>',
408 2
				esc_html( $field_type->_text( 'file_text', __( 'File:', 'cmb2' ) ) ),
409 2
				$url_value,
410 2
				cmb2_utils()->get_file_name_from_path( $url_value )
411 2
			);
412
413
		}
414 2
	}
415
}
416
417
class CMB2_Display_File_List extends CMB2_Display_File {
418
	/**
419
	 * Display file_list value.
420
	 * @since 2.2.2
421
	 */
422 1
	protected function _display() {
423 1
		if ( empty( $this->value ) || ! is_array( $this->value ) ) {
424
			return;
425
		}
426
427 1
		$field_type = new CMB2_Type_File_Base( new CMB2_Types( $this->field ) );
428
429 1
		echo '<ul class="cmb2-display-file-list">';
430 1
		foreach ( $this->value as $id => $fullurl ) {
431 1
			echo '<li>', $this->file_output( esc_url_raw( $fullurl ), $id, $field_type ), '</li>';
432 1
		}
433 1
		echo '</ul>';
434 1
	}
435
}
436
437
class CMB2_Display_oEmbed extends CMB2_Field_Display {
438
	/**
439
	 * Display oembed value.
440
	 * @since 2.2.2
441
	 */
442 1
	protected function _display() {
443 1
		if ( ! $this->value ) {
444
			return;
445
		}
446
447 1
		cmb2_do_oembed( array(
448 1
			'url'         => $this->value,
449 1
			'object_id'   => $this->field->object_id,
0 ignored issues
show
The property $object_id is declared protected in CMB2_Base. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
450 1
			'object_type' => $this->field->object_type,
0 ignored issues
show
The property $object_type is declared protected in CMB2_Base. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
451 1
			'oembed_args' => array( 'width' => '300' ),
452 1
			'field_id'    => $this->field->id(),
453 1
		) );
454 1
	}
455
}
456