Completed
Pull Request — master (#42)
by
unknown
02:29
created

WP_Fields_API_Image_Control   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 21.31 %

Coupling/Cohesion

Components 0
Dependencies 1
Metric Value
wmc 5
lcom 0
cbo 1
dl 13
loc 61
rs 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Fields API Text Control class.
4
 *
5
 * @see WP_Fields_API_Control
6
 */
7
class WP_Fields_API_Text_Control extends WP_Fields_API_Control {
8
9
	/**
10
	 * {@inheritdoc}
11
	 */
12
	public function render_content() {
13
14
		?>
15
		<label>
16
			<?php if ( ! empty( $this->label ) ) : ?>
17
				<span class="fields-control-title"><?php echo esc_html( $this->label ); ?></span>
18
			<?php endif;
19
			if ( ! empty( $this->description ) ) : ?>
20
				<span class="description fields-control-description"><?php echo $this->description; ?></span>
21
			<?php endif; ?>
22
			<input type="<?php echo esc_attr( $this->type ); ?>" <?php $this->input_attrs(); ?> value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> />
23
		</label>
24
		<?php
25
26
	}
27
28
}
29
30
/**
31
 * Fields API Checkbox Control class.
32
 *
33
 * @see WP_Fields_API_Control
34
 */
35
class WP_Fields_API_Checkbox_Control extends WP_Fields_API_Control {
36
37
	/**
38
	 * Checkbox Value override.
39
	 *
40
	 * @access public
41
	 *
42
	 * @see WP_Fields_API_Checkbox_Control::checkbox_value()
43
	 *
44
	 * @var string Checkbox value, allowing for separation of value, 'default' value, and checkbox value.
45
	 */
46
	public $checkbox_value;
47
48
	/**
49
	 * {@inheritdoc}
50
	 */
51
	public function render_content() {
52
53
			$checkbox_value = $this->checkbox_value();
54
		?>
55
		<label>
56
			<input type="checkbox" value="<?php echo esc_attr( $checkbox_value ); ?>" <?php $this->link(); checked( $checkbox_value, $this->value() ); ?> />
57
			<?php echo esc_html( $this->label ); ?>
58
			<?php if ( ! empty( $this->description ) ) : ?>
59
				<span class="description fields-control-description"><?php echo $this->description; ?></span>
60
			<?php endif; ?>
61
		</label>
62
		<?php
63
64
	}
65
66
	/**
67
	 * Fetch a checkbox value and allows overriding for separation from field value and 'default' value.
68
	 *
69
	 * @return string The requested checkbox value.
70
	 */
71
	final public function checkbox_value() {
72
73
		$value = $this->value();
74
75
		if ( isset( $this->checkbox_value ) ) {
76
			$value = $this->checkbox_value;
77
		}
78
79
		return $value;
80
81
	}
82
83
}
84
85
/**
86
 * Fields API Multiple Checkbox Control class.
87
 *
88
 * @see WP_Fields_API_Control
89
 */
90 View Code Duplication
class WP_Fields_API_Multi_Checkbox_Control extends WP_Fields_API_Control {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
91
92
	/**
93
	 * {@inheritdoc}
94
	 */
95
	public function render_content() {
96
97
		if ( empty( $this->choices ) ) {
98
			return;
99
		}
100
101
		$name = '_fields-checkbox-' . $this->id;
102
103
		if ( ! empty( $this->label ) ) : ?>
104
			<span class="fields-control-title"><?php echo esc_html( $this->label ); ?></span>
105
		<?php endif;
106
		if ( ! empty( $this->description ) ) : ?>
107
			<span class="description fields-control-description"><?php echo $this->description ; ?></span>
108
		<?php endif;
109
110
		foreach ( $this->choices as $value => $label ) :
111
			?>
112
			<label>
113
				<input type="checkbox" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> />
114
				<?php echo esc_html( $label ); ?><br/>
115
			</label>
116
			<?php
117
		endforeach;
118
119
	}
120
121
}
122
123
/**
124
 * Fields API Radio Control class.
125
 *
126
 * @see WP_Fields_API_Control
127
 */
128 View Code Duplication
class WP_Fields_API_Radio_Control extends WP_Fields_API_Control {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
129
130
	/**
131
	 * {@inheritdoc}
132
	 */
133
	public function render_content() {
134
135
		if ( empty( $this->choices ) ) {
136
			return;
137
		}
138
139
		$name = '_fields-radio-' . $this->id;
140
141
		if ( ! empty( $this->label ) ) : ?>
142
			<span class="fields-control-title"><?php echo esc_html( $this->label ); ?></span>
143
		<?php endif;
144
		if ( ! empty( $this->description ) ) : ?>
145
			<span class="description fields-control-description"><?php echo $this->description ; ?></span>
146
		<?php endif;
147
148
		foreach ( $this->choices as $value => $label ) :
149
			?>
150
			<label>
151
				<input type="radio" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> />
152
				<?php echo esc_html( $label ); ?><br/>
153
			</label>
154
			<?php
155
		endforeach;
156
157
	}
158
159
}
160
161
/**
162
 * Fields API Select Control class.
163
 *
164
 * @see WP_Fields_API_Control
165
 */
166
class WP_Fields_API_Select_Control extends WP_Fields_API_Control {
167
168
	/**
169
	 * {@inheritdoc}
170
	 */
171
	public function render_content() {
172
173
		if ( empty( $this->choices ) ) {
174
			return;
175
		}
176
		?>
177
		<label>
178
			<?php if ( ! empty( $this->label ) ) : ?>
179
				<span class="fields-control-title"><?php echo esc_html( $this->label ); ?></span>
180
			<?php endif;
181
			if ( ! empty( $this->description ) ) : ?>
182
				<span class="description fields-control-description"><?php echo $this->description; ?></span>
183
			<?php endif; ?>
184
185
			<select <?php $this->link(); ?>>
186
				<?php
187
				foreach ( $this->choices as $value => $label )
188
					echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
189
				?>
190
			</select>
191
		</label>
192
		<?php
193
194
	}
195
196
}
197
198
/**
199
 * Fields API Dropdown Pages Control class.
200
 *
201
 * @see WP_Fields_API_Control
202
 */
203
class WP_Fields_API_Dropdown_Pages_Control extends WP_Fields_API_Select_Control {
204
205
	/**
206
	 * Setup page choices for use by control
207
	 *
208
	 * @return array
209
	 */
210
	public function choices() {
211
212
		$choices = array(
213
			'0' => __( '&mdash; Select &mdash;' ),
214
		);
215
216
		$pages = get_pages();
217
218
		$choices = $this->get_page_choices_recurse( $choices, $pages );
219
220
		return $choices;
221
222
	}
223
224
	/**
225
	 * Recursively build choices array the full depth
226
	 *
227
	 * @param array     $choices List of choices.
228
	 * @param WP_Post[] $pages   List of pages.
229
	 * @param int       $depth   Current depth.
230
	 * @param int       $parent  Current parent page ID.
231
	 *
232
	 * @return array
233
	 */
234
	public function get_page_choices_recurse( $choices, $pages, $depth = 0, $parent = 0 ) {
235
236
		$pad = str_repeat( '&nbsp;', $depth * 3 );
237
238
		foreach ( $pages as $page ) {
239
			if ( $parent == $page->post_parent ) {
240
				$title = $page->post_title;
241
242
				if ( '' === $title ) {
243
					/* translators: %d: ID of a post */
244
					$title = sprintf( __( '#%d (no title)' ), $page->ID );
245
				}
246
247
				/**
248
				 * Filter the page title when creating an HTML drop-down list of pages.
249
				 *
250
				 * @since 3.1.0
251
				 *
252
				 * @param string $title Page title.
253
				 * @param object $page  Page data object.
254
				 */
255
				$title = apply_filters( 'list_pages', $title, $page );
256
257
				$choices[ $page->ID ] = $pad . $title;
258
259
				$choices = $this->get_page_choices_recurse( $choices, $pages, $depth + 1, $page->ID );
260
			}
261
		}
262
263
		return $choices;
264
265
	}
266
267
}
268
269
/**
270
 * Fields API Color Control class.
271
 *
272
 * @see WP_Fields_API_Control
273
 */
274
class WP_Fields_API_Color_Control extends WP_Fields_API_Control {
275
276
	/**
277
	 * @access public
278
	 * @var string
279
	 */
280
	public $type = 'color';
281
282
	/**
283
	 * @access public
284
	 * @var array
285
	 */
286
	public $statuses = array();
287
288
	/**
289
	 * Constructor.
290
	 *
291
	 * @since 3.4.0
292
	 * @uses WP_Fields_API_Control::__construct()
293
	 *
294
	 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
295
	 * @param string               $id      Control ID.
296
	 * @param array                $args    Optional. Arguments to override class property defaults.
297
	 */
298
	public function __construct( $manager, $id, $args = array() ) {
299
300
		$this->statuses = array(
301
			'' => __( 'Default' ),
302
		);
303
304
		parent::__construct( $manager, $id, $args );
305
306
	}
307
308
	/**
309
	 * Enqueue scripts/styles for the color picker.
310
	 *
311
	 * @since 3.4.0
312
	 */
313
	public function enqueue() {
314
		wp_enqueue_script( 'wp-color-picker' );
315
		wp_enqueue_style( 'wp-color-picker' );
316
	}
317
318
	/**
319
	 * Refresh the parameters passed to the JavaScript via JSON.
320
	 *
321
	 * @uses WP_Fields_API_Control::json()
322
	 */
323
	public function json() {
324
325
		$json = parent::json();
326
327
		$json['statuses']     = $this->statuses;
328
		$json['defaultValue'] = $this->setting->default;
0 ignored issues
show
Documentation introduced by
The property setting does not exist on object<WP_Fields_API_Color_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
329
330
		return $json;
331
332
	}
333
334
	/**
335
	 * Don't render the control content from PHP, as it's rendered via JS on load.
336
	 */
337
	public function render_content() {
338
339
		// @todo Figure out what to do for render_content vs content_template for purposes of Customizer vs other Fields implementations
340
341
	}
342
343
	/**
344
	 * Render a JS template for the content of the color picker control.
345
	 */
346
	public function content_template() {
347
348
		// @todo Figure out what to do for render_content vs content_template for purposes of Customizer vs other Fields implementations
349
		?>
350
		<# var defaultValue = '';
351
		if ( data.defaultValue ) {
352
			if ( '#' !== data.defaultValue.substring( 0, 1 ) ) {
353
				defaultValue = '#' + data.defaultValue;
354
			} else {
355
				defaultValue = data.defaultValue;
356
			}
357
			defaultValue = ' data-default-color=' + defaultValue; // Quotes added automatically.
358
		} #>
359
		<label>
360
			<# if ( data.label ) { #>
361
				<span class="customize-control-title">{{{ data.label }}}</span>
362
			<# } #>
363
			<# if ( data.description ) { #>
364
				<span class="description customize-control-description">{{{ data.description }}}</span>
365
			<# } #>
366
			<div class="customize-control-content">
367
				<input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e( 'Hex Value' ); ?>" {{ defaultValue }} />
368
			</div>
369
		</label>
370
		<?php
371
	}
372
}
373
374
/**
375
 * Customize Media Control class.
376
 *
377
 * @since 4.2.0
378
 *
379
 * @see WP_Fields_API_Control
380
 */
381
class WP_Fields_API_Media_Control extends WP_Fields_API_Control {
382
	/**
383
	 * Control type.
384
	 *
385
	 * @since 4.2.0
386
	 * @access public
387
	 * @var string
388
	 */
389
	public $type = 'media';
390
391
	/**
392
	 * Media control mime type.
393
	 *
394
	 * @since 4.2.0
395
	 * @access public
396
	 * @var string
397
	 */
398
	public $mime_type = '';
399
400
	/**
401
	 * Button labels.
402
	 *
403
	 * @since 4.2.0
404
	 * @access public
405
	 * @var array
406
	 */
407
	public $button_labels = array();
408
409
	/**
410
	 * Constructor.
411
	 *
412
	 * @since 4.1.0
413
	 * @since 4.2.0 Moved from WP_Fields_API_Upload_Control.
414
	 *
415
	 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
416
	 * @param string               $id      Control ID.
417
	 * @param array                $args    Optional. Arguments to override class property defaults.
418
	 */
419
	public function __construct( $manager, $id, $args = array() ) {
420
		parent::__construct( $manager, $id, $args );
421
422
		$this->button_labels = array(
423
			'select'       => __( 'Select File' ),
424
			'change'       => __( 'Change File' ),
425
			'default'      => __( 'Default' ),
426
			'remove'       => __( 'Remove' ),
427
			'placeholder'  => __( 'No file selected' ),
428
			'frame_title'  => __( 'Select File' ),
429
			'frame_button' => __( 'Choose File' ),
430
		);
431
	}
432
433
	/**
434
	 * Enqueue control related scripts/styles.
435
	 *
436
	 * @since 3.4.0
437
	 * @since 4.2.0 Moved from WP_Fields_API_Upload_Control.
438
	 */
439
	public function enqueue() {
440
		wp_enqueue_media();
441
	}
442
443
	/**
444
	 * Refresh the parameters passed to the JavaScript via JSON.
445
	 *
446
	 * @since 3.4.0
447
	 * @since 4.2.0 Moved from WP_Fields_API_Upload_Control.
448
	 *
449
	 * @see WP_Fields_API_Control::to_json()
450
	 */
451
	public function to_json() {
452
		parent::to_json();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class WP_Fields_API_Control as the method to_json() does only exist in the following sub-classes of WP_Fields_API_Control: WP_Fields_API_Media_Control, WP_Fields_API_Media_File_Control. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
453
		$this->json['label'] = html_entity_decode( $this->label, ENT_QUOTES, get_bloginfo( 'charset' ) );
0 ignored issues
show
Documentation introduced by
The property json does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
454
		$this->json['mime_type'] = $this->mime_type;
0 ignored issues
show
Documentation introduced by
The property json does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
455
		$this->json['button_labels'] = $this->button_labels;
0 ignored issues
show
Documentation introduced by
The property json does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
456
		$this->json['canUpload'] = current_user_can( 'upload_files' );
0 ignored issues
show
Documentation introduced by
The property json does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
457
458
		$value = $this->value();
459
460
		if ( is_object( $this->setting ) ) {
0 ignored issues
show
Documentation introduced by
The property setting does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
461
			if ( $this->setting->default ) {
0 ignored issues
show
Documentation introduced by
The property setting does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
462
				// Fake an attachment model - needs all fields used by template.
463
				// Note that the default value must be a URL, NOT an attachment ID.
464
				$type = in_array( substr( $this->setting->default, -3 ), array( 'jpg', 'png', 'gif', 'bmp' ) ) ? 'image' : 'document';
0 ignored issues
show
Documentation introduced by
The property setting does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
465
				$default_attachment = array(
466
					'id' => 1,
467
					'url' => $this->setting->default,
0 ignored issues
show
Documentation introduced by
The property setting does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
468
					'type' => $type,
469
					'icon' => wp_mime_type_icon( $type ),
470
					'title' => basename( $this->setting->default ),
0 ignored issues
show
Documentation introduced by
The property setting does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
471
				);
472
473
				if ( 'image' === $type ) {
474
					$default_attachment['sizes'] = array(
475
						'full' => array( 'url' => $this->setting->default ),
0 ignored issues
show
Documentation introduced by
The property setting does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
476
					);
477
				}
478
479
				$this->json['defaultAttachment'] = $default_attachment;
0 ignored issues
show
Documentation introduced by
The property json does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
480
			}
481
482
			if ( $value && $this->setting->default && $value === $this->setting->default ) {
0 ignored issues
show
Documentation introduced by
The property setting does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
483
				// Set the default as the attachment.
484
				$this->json['attachment'] = $this->json['defaultAttachment'];
0 ignored issues
show
Documentation introduced by
The property json does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
485
			} elseif ( $value ) {
486
				$this->json['attachment'] = wp_prepare_attachment_for_js( $value );
0 ignored issues
show
Documentation introduced by
The property json does not exist on object<WP_Fields_API_Media_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
487
			}
488
		}
489
	}
490
491
	/**
492
	 * Don't render any content for this control from PHP.
493
	 *
494
	 * @since 3.4.0
495
	 * @since 4.2.0 Moved from WP_Fields_API_Upload_Control.
496
	 *
497
	 * @see WP_Fields_API_Media_Control::content_template()
498
	 */
499
	public function render_content() {}
500
501
	/**
502
	 * Render a JS template for the content of the media control.
503
	 *
504
	 * @since 4.1.0
505
	 * @since 4.2.0 Moved from WP_Fields_API_Upload_Control.
506
	 */
507
	public function content_template() {
508
		?>
509
		<label for="{{ data.settings['default'] }}-button">
510
			<# if ( data.label ) { #>
511
				<span class="customize-control-title">{{ data.label }}</span>
512
			<# } #>
513
			<# if ( data.description ) { #>
514
				<span class="description customize-control-description">{{{ data.description }}}</span>
515
			<# } #>
516
		</label>
517
518
		<# if ( data.attachment && data.attachment.id ) { #>
519
			<div class="current">
520
				<div class="container">
521
					<div class="attachment-media-view attachment-media-view-{{ data.attachment.type }} {{ data.attachment.orientation }}">
522
						<div class="thumbnail thumbnail-{{ data.attachment.type }}">
523
							<# if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.medium ) { #>
524
								<img class="attachment-thumb" src="{{ data.attachment.sizes.medium.url }}" draggable="false" />
525
							<# } else if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.full ) { #>
526
								<img class="attachment-thumb" src="{{ data.attachment.sizes.full.url }}" draggable="false" />
527
							<# } else if ( 'audio' === data.attachment.type ) { #>
528
								<# if ( data.attachment.image && data.attachment.image.src && data.attachment.image.src !== data.attachment.icon ) { #>
529
									<img src="{{ data.attachment.image.src }}" class="thumbnail" draggable="false" />
530
								<# } else { #>
531
									<img src="{{ data.attachment.icon }}" class="attachment-thumb type-icon" draggable="false" />
532
								<# } #>
533
								<p class="attachment-meta attachment-meta-title">&#8220;{{ data.attachment.title }}&#8221;</p>
534
								<# if ( data.attachment.album || data.attachment.meta.album ) { #>
535
									<p class="attachment-meta"><em>{{ data.attachment.album || data.attachment.meta.album }}</em></p>
536
								<# } #>
537
								<# if ( data.attachment.artist || data.attachment.meta.artist ) { #>
538
									<p class="attachment-meta">{{ data.attachment.artist || data.attachment.meta.artist }}</p>
539
								<# } #>
540
								<audio style="visibility: hidden" controls class="wp-audio-shortcode" width="100%" preload="none">
541
									<source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
542
								</audio>
543
							<# } else if ( 'video' === data.attachment.type ) { #>
544
								<div class="wp-media-wrapper wp-video">
545
									<video controls="controls" class="wp-video-shortcode" preload="metadata"
546
										<# if ( data.attachment.image && data.attachment.image.src !== data.attachment.icon ) { #>poster="{{ data.attachment.image.src }}"<# } #>>
547
										<source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
548
									</video>
549
								</div>
550
							<# } else { #>
551
								<img class="attachment-thumb type-icon icon" src="{{ data.attachment.icon }}" draggable="false" />
552
								<p class="attachment-title">{{ data.attachment.title }}</p>
553
							<# } #>
554
						</div>
555
					</div>
556
				</div>
557
			</div>
558
			<div class="actions">
559
				<# if ( data.canUpload ) { #>
560
					<button type="button" class="button remove-button"><?php echo $this->button_labels['remove']; ?></button>
561
					<button type="button" class="button upload-button" id="{{ data.settings['default'] }}-button"><?php echo $this->button_labels['change']; ?></button>
562
					<div style="clear:both"></div>
563
				<# } #>
564
			</div>
565
		<# } else { #>
566
			<div class="current">
567
				<div class="container">
568
					<div class="placeholder">
569
						<div class="inner">
570
							<span>
571
								<?php echo $this->button_labels['placeholder']; ?>
572
							</span>
573
						</div>
574
					</div>
575
				</div>
576
			</div>
577
			<div class="actions">
578
				<# if ( data.defaultAttachment ) { #>
579
					<button type="button" class="button default-button"><?php echo $this->button_labels['default']; ?></button>
580
				<# } #>
581
				<# if ( data.canUpload ) { #>
582
					<button type="button" class="button upload-button" id="{{ data.settings['default'] }}-button"><?php echo $this->button_labels['select']; ?></button>
583
				<# } #>
584
				<div style="clear:both"></div>
585
			</div>
586
		<# } #>
587
		<?php
588
	}
589
}
590
591
/**
592
 * Media File Control Class.
593
 *
594
 * This control stores the media file's URL in the corresponding setting, whereas the base media control saves the attachment id.
595
 * 
596
 * @since 3.4.0
597
 *
598
 * @see WP_Fields_API_Media_Control
599
 */
600
class WP_Fields_API_Media_File_Control extends WP_Fields_API_Media_Control {
601
	public $type          = 'media_file';
602
	public $mime_type     = '';
603
	public $button_labels = array();
604
605
	/**
606
	 * Refresh the parameters passed to the JavaScript via JSON.
607
	 *
608
	 * @since 3.4.0
609
	 *
610
	 * @uses WP_Fields_API_Media_Control::to_json()
611
	 */
612
	public function to_json() {
613
		parent::to_json();
614
615
		$value = $this->value();
616
		if ( $value ) {
617
			// Get the attachment model for the existing file.
618
			$attachment_id = attachment_url_to_postid( $value );
619
			if ( $attachment_id ) {
620
				$this->json['attachment'] = wp_prepare_attachment_for_js( $attachment_id );
0 ignored issues
show
Documentation introduced by
The property json does not exist on object<WP_Fields_API_Media_File_Control>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
621
			}
622
		}
623
	}
624
}
625