Completed
Pull Request — trunk (#821)
by
unknown
06:11
created

CMB2_Field_Display::get()   C

Complexity

Conditions 27
Paths 27

Size

Total Lines 69
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 65
CRAP Score 27.0194

Importance

Changes 0
Metric Value
cc 27
eloc 66
nc 27
nop 1
dl 0
loc 69
ccs 65
cts 67
cp 0.9701
crap 27.0194
rs 5.5275
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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 'time':
60
				$type = new CMB2_Display_Time( $field );
0 ignored issues
show
Bug Compatibility introduced by
The expression new \CMB2_Display_Time($field); of type CMB2_Display_Time adds the type CMB2_Display_Time to the return on line 102 which is incompatible with the return type documented by CMB2_Field_Display::get of type CMB2_Field_Display.
Loading history...
61
				break;
62 24
			case 'text_date':
63 24
			case 'text_date_timestamp':
64 24
			case 'text_datetime_timestamp':
65 3
				$type = new CMB2_Display_Text_Date( $field );
66 3
				break;
67 21
			case 'text_datetime_timestamp_timezone':
68 1
				$type = new CMB2_Display_Text_Date_Timezone( $field );
69 1
				break;
70 20
			case 'select':
71 20
			case 'radio':
72 20
			case 'radio_inline':
73 3
				$type = new CMB2_Display_Select( $field );
74 3
				break;
75 17
			case 'multicheck':
76 17
			case 'multicheck_inline':
77 2
				$type = new CMB2_Display_Multicheck( $field );
78 2
				break;
79 15
			case 'taxonomy_radio':
80 15
			case 'taxonomy_radio_inline':
81 15
			case 'taxonomy_select':
82 3
				$type = new CMB2_Display_Taxonomy_Radio( $field );
83 3
				break;
84 12
			case 'taxonomy_multicheck':
85 12
			case 'taxonomy_multicheck_inline':
86 2
				$type = new CMB2_Display_Taxonomy_Multicheck( $field );
87 2
				break;
88 10
			case 'file':
89 1
				$type = new CMB2_Display_File( $field );
90 1
				break;
91 9
			case 'file_list':
92 1
				$type = new CMB2_Display_File_List( $field );
93 1
				break;
94 8
			case 'oembed':
95 1
				$type = new CMB2_Display_oEmbed( $field );
96 1
				break;
97 7
			default:
98 7
				$type = new self( $field );
99 7
				break;
100 33
		}
101
102 33
		return $type;
103
	}
104
105
	/**
106
	 * Setup our class vars
107
	 * @since 2.2.2
108
	 * @param CMB2_Field $field A CMB2 field object
109
	 */
110 33
	public function __construct( CMB2_Field $field ) {
111 33
		$this->field = $field;
112 33
		$this->value = $this->field->value;
113 33
	}
114
115
	/**
116
	 * Catchall method if field's 'display_cb' is NOT defined, or field type does
117
	 * not have a corresponding display method
118
	 * @since 2.2.2
119
	 */
120 33
	public function display() {
121
		// If repeatable
122 33
		if ( $this->field->args( 'repeatable' ) ) {
123
124
			// And has a repeatable value
125
			if ( is_array( $this->field->value ) ) {
126
127
				// Then loop and output.
128
				echo '<ul class="cmb2-'. str_replace( '_', '-', $this->field->type() ) .'">';
129
				foreach ( $this->field->value as $val ) {
130
					$this->value = $val;
131
					echo '<li>', $this->_display(), '</li>';
132
					;
133
				}
134
				echo '</ul>';
135
			}
136
137
		} else {
138 33
			$this->_display();
139
		}
140 33
	}
141
142
	/**
143
	 * Default fallback display method.
144
	 * @since 2.2.2
145
	 */
146 7
	protected function _display() {
147 7
		print_r( $this->value );
148 7
	}
149
}
150
151
class CMB2_Display_Text_Url extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
152
	/**
153
	 * Display url value.
154
	 * @since 2.2.2
155
	 */
156 1
	protected function _display() {
157 1
		echo make_clickable( esc_url( $this->value ) );
158 1
	}
159
}
160
161
class CMB2_Display_Text_Money extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
162
	/**
163
	 * Display text_money value.
164
	 * @since 2.2.2
165
	 */
166 1
	protected function _display() {
167 1
		$this->value = $this->value ? $this->value : '0';
168 1
		echo ( ! $this->field->get_param_callback_result( 'before_field' ) ? '$' : ' ' ), $this->value;
169 1
	}
170
}
171
172
class CMB2_Display_Colorpicker extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
173
	/**
174
	 * Display color picker value.
175
	 * @since 2.2.2
176
	 */
177 1
	protected function _display() {
178 1
		echo '<span class="cmb2-colorpicker-swatch"><span style="background-color:', esc_attr( $this->value ), '"></span> ', esc_html( $this->value ), '</span>';
179 1
	}
180
}
181
182
class CMB2_Display_Checkbox extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
183
	/**
184
	 * Display multicheck value.
185
	 * @since 2.2.2
186
	 */
187 1
	protected function _display() {
188 1
		echo $this->value === 'on' ? 'on' : 'off';
189 1
	}
190
}
191
192
class CMB2_Display_Select extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
193
	/**
194
	 * Display select value.
195
	 * @since 2.2.2
196
	 */
197 3
	protected function _display() {
198 3
		$options = $this->field->options();
199
200 3
		$fallback = $this->field->args( 'show_option_none' );
201 3
		if ( ! $fallback && isset( $options[''] ) ) {
202
			$fallback = $options[''];
203
		}
204 3
		if ( ! $this->value && $fallback ) {
205
			echo $fallback;
206 3
		} elseif ( isset( $options[ $this->value ] ) ) {
207 3
			echo $options[ $this->value ];
208 3
		} else {
209
			echo esc_attr( $this->value );
210
		}
211 3
	}
212
}
213
214
class CMB2_Display_Multicheck extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
215
	/**
216
	 * Display multicheck value.
217
	 * @since 2.2.2
218
	 */
219 2
	protected function _display() {
220 2
		if ( empty( $this->value ) || ! is_array( $this->value ) ) {
221
			return;
222
		}
223
224 2
		$options = $this->field->options();
225
226 2
		$output = array();
227 2
		foreach ( $this->value as $val ) {
228 2
			if ( isset( $options[ $val ] ) ) {
229 2
				$output[] = $options[ $val ];
230 2
			} else {
231
				$output[] = esc_attr( $val );
232
			}
233 2
		}
234
235 2
		echo implode( ', ', $output );
236 2
	}
237
}
238
239
class CMB2_Display_Textarea extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
240
	/**
241
	 * Display textarea value.
242
	 * @since 2.2.2
243
	 */
244 3
	protected function _display() {
245 3
		echo wpautop( wp_kses_post( $this->value ) );
246 3
	}
247
}
248
249
class CMB2_Display_Textarea_Code extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
250
	/**
251
	 * Display textarea_code value.
252
	 * @since 2.2.2
253
	 */
254 1
	protected function _display() {
255 1
		echo '<xmp class="cmb2-code">'. print_r( $this->value, true ) .'</xmp>';
256 1
	}
257
}
258
259
class CMB2_Display_Text_Time extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
260
	/**
261
	 * Display text_time value.
262
	 * @since 2.2.2
263
	 */
264 1
	protected function _display() {
265 1
		echo $this->field->get_timestamp_format( 'time_format', $this->value );
266 1
	}
267
}
268
269
class CMB2_Display_Text_Date extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
270
	/**
271
	 * Display text_date value.
272
	 * @since 2.2.2
273
	 */
274 3
	protected function _display() {
275 3
		echo $this->field->get_timestamp_format( 'date_format', $this->value );
276 3
	}
277
}
278
279
class CMB2_Display_Text_Date_Timezone extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
280
	/**
281
	 * Display text_datetime_timestamp_timezone value.
282
	 * @since 2.2.2
283
	 */
284 1
	protected function _display() {
285 1
		$field = $this->field;
0 ignored issues
show
Unused Code introduced by
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
286
287 1
		if ( empty( $this->value ) ) {
288
			return;
289
		}
290
291 1
		$datetime = maybe_unserialize( $this->value );
292 1
		$this->value = $tzstring = '';
293
294 1 View Code Duplication
		if ( $datetime && $datetime instanceof DateTime ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
295 1
			$tz       = $datetime->getTimezone();
296 1
			$tzstring = $tz->getName();
297 1
			$this->value    = $datetime->getTimestamp();
298 1
		}
299
300 1
		$date = $this->field->get_timestamp_format( 'date_format', $this->value );
301 1
		$time = $this->field->get_timestamp_format( 'time_format', $this->value );
302
303 1
		echo $date, ( $time ? ' ' . $time : '' ), ( $tzstring ? ', ' . $tzstring : '' );
304 1
	}
305
}
306
307
class CMB2_Display_Taxonomy_Radio extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
308
	/**
309
	 * Display single taxonomy value.
310
	 * @since 2.2.2
311
	 */
312 3
	protected function _display() {
313 3
		$taxonomy   = $this->field->args( 'taxonomy' );
314 3
		$field_type = new CMB2_Type_Taxonomy_Radio( new CMB2_Types( $this->field ) );
315 3
		$terms      = $field_type->get_object_terms();
316 3
		$term       = false;
317
318 3
		if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) {
319
			$term = get_term_by( 'slug', $default, $taxonomy );
0 ignored issues
show
Bug introduced by
The variable $default does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
320 3
		} elseif ( ! empty( $terms ) ) {
321 3
			$term = $terms[key( $terms )];
322 3
		}
323
324 3
		if ( $term ) {
325 3
			$link = get_edit_term_link( $term->term_id, $taxonomy );
326 3
			echo '<a href="', esc_url( $link ), '">', esc_html( $term->name ), '</a>';
327 3
		}
328 3
	}
329
}
330
331
class CMB2_Display_Taxonomy_Multicheck extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
332
	/**
333
	 * Display taxonomy values.
334
	 * @since 2.2.2
335
	 */
336 2
	protected function _display() {
337 2
		$taxonomy   = $this->field->args( 'taxonomy' );
338 2
		$field_type = new CMB2_Type_Taxonomy_Multicheck( new CMB2_Types( $this->field ) );
339 2
		$terms      = $field_type->get_object_terms();
340
341 2
		if ( is_wp_error( $terms ) || empty( $terms ) && ( $default = $this->field->get_default() ) ) {
342
			$terms = array();
343
			if ( is_array( $default ) ) {
344
				foreach ( $default as $slug ) {
0 ignored issues
show
Bug introduced by
The variable $default does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
345
					$terms[] = get_term_by( 'slug', $slug, $taxonomy );
346
				}
347
			} else {
348
				$terms[] = get_term_by( 'slug', $default, $taxonomy );
349
			}
350
		}
351
352 2
		if ( is_array( $terms ) ) {
353
354 2
			$links = array();
355 2
			foreach ( $terms as $term ) {
356 2
				$link = get_edit_term_link( $term->term_id, $taxonomy );
357 2
				$links[] = '<a href="'. esc_url( $link ) .'">'. esc_html( $term->name ) .'</a>';
358 2
			}
359
			// Then loop and output.
360 2
			echo '<div class="cmb2-taxonomy-terms-', esc_attr( $taxonomy ), '">';
361 2
			echo implode( ', ', $links );
362 2
			echo '</div>';
363 2
		}
364 2
	}
365
}
366
367
class CMB2_Display_File extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
368
	/**
369
	 * Display file value.
370
	 * @since 2.2.2
371
	 */
372 1
	protected function _display() {
373 1
		if ( empty( $this->value ) ) {
374
			return;
375
		}
376
377 1
		$this->value = esc_url_raw( $this->value );
378
379 1
		$field_type = new CMB2_Type_File_Base( new CMB2_Types( $this->field ) );
380
381 1
		$id = $this->field->get_field_clone( array(
382 1
			'id' => $this->field->_id() . '_id',
383 1
		) )->escaped_value( 'absint' );
384
385 1
		$this->file_output( $this->value, $id, $field_type );
386 1
	}
387
388 2
	protected function file_output( $url_value, $id, CMB2_Type_File_Base $field_type ) {
389
		// If there is no ID saved yet, try to get it from the url
390 2
		if ( $url_value && ! $id ) {
391
			$id = CMB2_Utils::image_id_from_url( esc_url_raw( $url_value ) );
392
		}
393
394 2
		if ( $field_type->is_valid_img_ext( $url_value ) ) {
395
			$img_size = $this->field->args( 'preview_size' );
396
397 View Code Duplication
			if ( $id ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
398
				$image = wp_get_attachment_image( $id, $img_size, null, array( 'class' => 'cmb-image-display' ) );
399
			} else {
400
				$size = is_array( $img_size ) ? $img_size[0] : 200;
401
				$image = '<img class="cmb-image-display" style="max-width: ' . absint( $size ) . 'px; width: 100%; height: auto;" src="' . $url_value . '" alt="" />';
402
			}
403
404
			echo $image;
405
406
		} else {
407
408 2
			printf( '<div class="file-status"><span>%1$s <strong><a href="%2$s">%3$s</a></strong></span></div>',
409 2
				esc_html( $field_type->_text( 'file_text', esc_html__( 'File:', 'cmb2' ) ) ),
0 ignored issues
show
Documentation Bug introduced by
The method _text does not exist on object<CMB2_Type_File_Base>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
410 2
				$url_value,
411 2
				CMB2_Utils::get_file_name_from_path( $url_value )
412 2
			);
413
414
		}
415 2
	}
416
}
417
418
class CMB2_Display_File_List extends CMB2_Display_File {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
419
	/**
420
	 * Display file_list value.
421
	 * @since 2.2.2
422
	 */
423 1
	protected function _display() {
424 1
		if ( empty( $this->value ) || ! is_array( $this->value ) ) {
425
			return;
426
		}
427
428 1
		$field_type = new CMB2_Type_File_Base( new CMB2_Types( $this->field ) );
429
430 1
		echo '<ul class="cmb2-display-file-list">';
431 1
		foreach ( $this->value as $id => $fullurl ) {
432 1
			echo '<li>', $this->file_output( esc_url_raw( $fullurl ), $id, $field_type ), '</li>';
433 1
		}
434 1
		echo '</ul>';
435 1
	}
436
}
437
438
class CMB2_Display_oEmbed extends CMB2_Field_Display {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
439
	/**
440
	 * Display oembed value.
441
	 * @since 2.2.2
442
	 */
443 1
	protected function _display() {
444 1
		if ( ! $this->value ) {
445
			return;
446
		}
447
448 1
		cmb2_do_oembed( array(
449 1
			'url'         => $this->value,
450 1
			'object_id'   => $this->field->object_id,
0 ignored issues
show
Documentation introduced by
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...
451 1
			'object_type' => $this->field->object_type,
0 ignored issues
show
Documentation introduced by
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...
452 1
			'oembed_args' => array( 'width' => '300' ),
453 1
			'field_id'    => $this->field->id(),
454 1
		) );
455 1
	}
456
}
457