acf_field_oembed::format_value()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
*  ACF oEmbed Field Class
5
*
6
*  All the logic for this field type
7
*
8
*  @class 		acf_field_oembed
9
*  @extends		acf_field
10
*  @package		ACF
11
*  @subpackage	Fields
12
*/
13
14
if( ! class_exists('acf_field_oembed') ) :
15
16
class acf_field_oembed extends acf_field {
17
	
18
	
19
	/*
20
	*  __construct
21
	*
22
	*  This function will setup the field type data
23
	*
24
	*  @type	function
25
	*  @date	5/03/2014
26
	*  @since	5.0.0
27
	*
28
	*  @param	n/a
29
	*  @return	n/a
30
	*/
0 ignored issues
show
Documentation introduced by
The doc-type n/a could not be parsed: Unknown type name "n/a" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
31
	
32 View Code Duplication
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method 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...
33
		
34
		// vars
35
		$this->name = 'oembed';
36
		$this->label = __("oEmbed",'acf');
0 ignored issues
show
Bug introduced by
The property label does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
		$this->category = 'content';
38
		$this->defaults = array(
39
			'width'		=> '',
40
			'height'	=> '',
41
		);
42
		$this->default_values = array(
0 ignored issues
show
Bug introduced by
The property default_values does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
			'width' 	=> 640,
44
			'height'	=> 390
45
		);
46
47
		
48
		// extra
49
		add_action('wp_ajax_acf/fields/oembed/search',			array($this, 'ajax_search'));
50
		add_action('wp_ajax_nopriv_acf/fields/oembed/search',	array($this, 'ajax_search'));
51
		
52
		
53
		// do not delete!
54
    	parent::__construct();
55
    	
56
	}
57
	
58
	
59
	/*
60
	*  wp_oembed_get
61
	*
62
	*  description
63
	*
64
	*  @type	function
65
	*  @date	24/01/2014
66
	*  @since	5.0.0
67
	*
68
	*  @param	$post_id (int)
69
	*  @return	$post_id (int)
70
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
71
	
72
	function wp_oembed_get( $url = '', $width = 0, $height = 0 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
73
		
74
		// vars
75
		$embed = '';
0 ignored issues
show
Unused Code introduced by
$embed 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...
76
		$res = array(
77
			'width'		=> $width,
78
			'height'	=> $height
79
		);
80
		
81
		
82
		// get emebed
83
		$embed = @wp_oembed_get( $url, $res );
84
		
85
		
86
		// try shortcode
87
		if( !$embed ) {
88
			
89
			 // global
90
			global $wp_embed;
91
			
92
			
93
			// get emebed
94
			$embed = $wp_embed->shortcode($res, $url);
95
		
96
		}
97
				
98
		
99
		// return
100
		return $embed;
101
	}
102
	
103
	
104
	/*
105
	*  ajax_search
106
	*
107
	*  description
108
	*
109
	*  @type	function
110
	*  @date	24/10/13
111
	*  @since	5.0.0
112
	*
113
	*  @param	$post_id (int)
114
	*  @return	$post_id (int)
115
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
116
	
117
	function ajax_search() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
118
		
119
   		// options
120
   		$args = acf_parse_args( $_POST, array(
121
			's'			=> '',
122
			'nonce'		=> '',
123
			'width'		=> 0,
124
			'height'	=> 0,
125
		));
126
		
127
		
128
		// width and height
129
		if( !$args['width'] ) {
130
		
131
			$args['width'] = $this->default_values['width'];
132
			
133
		}
134
		
135
		if( !$args['height'] ) {
136
		
137
			$args['height'] = $this->default_values['height'];
138
			
139
		}
140
		
141
		
142
		// validate
143
		if( ! wp_verify_nonce($args['nonce'], 'acf_nonce') ) {
144
		
145
			die();
146
			
147
		}
148
		
149
		
150
		// get oembed
151
		echo $this->wp_oembed_get($args['s'], $args['width'], $args['height']);
152
		
153
		
154
		// die
155
		die();
156
			
157
	}
158
	
159
	
160
	/*
161
	*  render_field()
162
	*
163
	*  Create the HTML interface for your field
164
	*
165
	*  @param	$field - an array holding all the field's data
166
	*
167
	*  @type	action
168
	*  @since	3.6
169
	*  @date	23/01/13
170
	*/
171
	
172
	function render_field( $field ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
173
		
174
		// default options
175
		foreach( $this->default_values as $k => $v ) {
176
		
177
			if( empty($field[ $k ]) ) {
178
			
179
				$field[ $k ] = $v;
180
				
181
			}
182
			
183
		}
184
		
185
		
186
		// atts
187
		$atts = array(
188
			'class'			=> 'acf-oembed',
189
			'data-width'	=> $field['width'],
190
			'data-height'	=> $field['height']
191
		);
192
		
193
		if( $field['value'] ) {
194
		
195
			$atts['class'] .= ' has-value';
196
			
197
		}
198
		
199
?>
200
<div <?php acf_esc_attr_e($atts) ?>>
201
	<div class="acf-hidden">
202
		<input type="hidden" data-name="value-input" name="<?php echo esc_attr($field['name']); ?>" value="<?php echo esc_attr($field['value']); ?>" />
203
	</div>
204
	<div class="title acf-soh">
205
		
206
		<div class="title-value">
207
			<h4 data-name="value-title"><?php echo $field['value']; ?></h4>
208
		</div>
209
		
210
		<div class="title-search">
211
			
212
			<input data-name="search-input" type="text" placeholder="<?php _e("Enter URL", 'acf'); ?>" autocomplete="off" />
213
		</div>
214
		
215
		<a data-name="clear-button" href="#" class="acf-icon -cancel grey acf-soh-target"></a>
216
		
217
	</div>
218
	<div class="canvas">
219
		
220
		<div class="canvas-loading">
221
			<i class="acf-loading"></i>
222
		</div>
223
		
224
		<div class="canvas-error">
225
			<p><strong><?php _e("Error", 'acf'); ?></strong>. <?php _e("No embed found for the given URL", 'acf'); ?></p>
226
		</div>
227
		
228
		<div class="canvas-media" data-name="value-embed">
229
			<?php if( !empty( $field['value'] ) ): ?>
230
				<?php echo $this->wp_oembed_get($field['value'], $field['width'], $field['height']); ?>
231
			<?php endif; ?>
232
		</div>
233
		
234
		<i class="acf-icon -picture hide-if-value"></i>
235
		
236
	</div>
237
	
238
</div>
239
<?php
240
		
241
	}
242
	
243
	
244
	/*
245
	*  render_field_settings()
246
	*
247
	*  Create extra options for your field. This is rendered when editing a field.
248
	*  The value of $field['name'] can be used (like bellow) to save extra data to the $field
249
	*
250
	*  @param	$field	- an array holding all the field's data
251
	*
252
	*  @type	action
253
	*  @since	3.6
254
	*  @date	23/01/13
255
	*/
256
	
257
	function render_field_settings( $field ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
258
		
259
		// width
260
		acf_render_field_setting( $field, array(
261
			'label'			=> __('Embed Size','acf'),
262
			'type'			=> 'text',
263
			'name'			=> 'width',
264
			'prepend'		=> __('Width', 'acf'),
265
			'append'		=> 'px',
266
			'placeholder'	=> $this->default_values['width']
267
		));
268
		
269
		
270
		// height
271
		acf_render_field_setting( $field, array(
272
			'label'			=> __('Embed Size','acf'),
273
			'type'			=> 'text',
274
			'name'			=> 'height',
275
			'prepend'		=> __('Height', 'acf'),
276
			'append'		=> 'px',
277
			'placeholder'	=> $this->default_values['height'],
278
			'wrapper'		=> array(
279
				'data-append' => 'width'
280
			)
281
		));
282
		
283
	}
284
	
285
	
286
	/*
287
	*  format_value()
288
	*
289
	*  This filter is appied to the $value after it is loaded from the db and before it is returned to the template
290
	*
291
	*  @type	filter
292
	*  @since	3.6
293
	*  @date	23/01/13
294
	*
295
	*  @param	$value (mixed) the value which was loaded from the database
296
	*  @param	$post_id (mixed) the $post_id from which the value was loaded
297
	*  @param	$field (array) the field array holding all the field options
298
	*
299
	*  @return	$value (mixed) the modified value
300
	*/
0 ignored issues
show
Documentation introduced by
The doc-type $value could not be parsed: Unknown type name "$value" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
301
	
302
	function format_value( $value, $post_id, $field ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
303
		
304
		// bail early if no value
305
		if( empty($value) ) {
306
			
307
			return $value;
308
		
309
		}
310
		
311
		
312
		// get oembed
313
		$value = $this->wp_oembed_get($value, $field['width'], $field['height']);
314
		
315
		
316
		// return
317
		return $value;
318
		
319
	}
320
	
321
}
322
323
new acf_field_oembed();
324
325
endif;
326
327
?>
328