Completed
Pull Request — develop (#1588)
by Zack
37:04 queued 17:05
created

Field::render()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
namespace GV\Gutenberg\Blocks\Block;
4
5
// Exit if accessed directly
6
if ( ! defined( 'ABSPATH' ) ) {
7
	exit;
8
}
9
10
class Field extends Block {
11
	const BLOCK_NAME = 'field';
12
13
	/**
14
	 * Generate `[gvfield]` shortcode
15
	 *
16
	 * @param array $attributes
17
	 *                         array['view_id']         string  The numeric View ID the entry should be displayed from
18
	 *                         array['entry_id']        string  A numeric ID or slug referencing the entry. Or the last, first entry from the View. The View's sorting and filtering settings will be applied to the entries
19
	 *                         array['field_id']        string  The field ID that should be ouput. Required. If this is a merge of several form feeds multiple fields can be provided separated by a comma
20
	 *                         array['custom_label']    string  Custom label for the field
21
	 *
22
	 * @return string $output
23
	 */
24
	static function render( $attributes = array() ) {
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...
25
26
		$accepted_attributes = array(
27
			'view_id',
28
			'entry_id',
29
			'field_id',
30
			'custom_label',
31
		);
32
33
		$shortcode_attributes = array();
34
35
		foreach ( $attributes as $attribute => $value ) {
36
			$value = esc_attr( sanitize_text_field( $value ) );
37
38
			if ( in_array( $attribute, $accepted_attributes ) && ! empty( $value ) ) {
39
				$shortcode_attributes[] = "{$attribute}={$value}";
40
			}
41
		}
42
43
		$shortcode = sprintf( '[gvfield %s]', implode( ' ', $shortcode_attributes ) );
44
45
		$output = do_shortcode( $shortcode );
46
47
		return $output;
48
	}
49
}
50