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

Entry_Link   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B render() 0 36 6
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 Entry_Link extends Block {
11
	const BLOCK_NAME = 'entry-link';
12
13
	/**
14
	 * Generate `[gv_entry_link]` shortcode
15
	 *
16
	 * @param array $attributes
17
	 *                         array['view_id']         string  The ID for the View where the entry is displayed
18
	 *                         array['entry_id']        string  ID of the entry to edit
19
	 *                         array['action']          string  Define which type of link you want to display. valid values are : read, edit, delete
20
	 *                         array['post_id']         string  If you want to have the Edit Entry link go to an embedded View, pass the ID of the post or page where the View is embedded
21
	 *                         array['return']          string  What should the shortcode return. valid values are : html, url
22
	 *                         array['link_atts']       string  Attributes to pass to the link tag generator to add to the <a> tag
23
	 *                         array['field_values']    string  Parameters to pass URL arguments to the link
24
	 *                         array['content']         string  The anchor text can be set by this attribute
25
	 *
26
	 * @return string $output
27
	 */
28
	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...
29
		$accepted_attributes = array(
30
			'view_id',
31
			'entry_id',
32
			'action',
33
			'post_id',
34
			'return',
35
			'link_atts',
36
			'field_values',
37
			'content',
38
		);
39
40
		$shortcode_attributes = array();
41
42
		foreach ( $attributes as $attribute => $value ) {
43
			$value = esc_attr( sanitize_text_field( $value ) );
44
45
			if ( 'content' !== $attribute && in_array( $attribute, $accepted_attributes ) && ! empty( $value ) ) {
46
				$shortcode_attributes[] = "{$attribute}={$value}";
47
			}
48
		}
49
50
		if ( ! empty( $attributes['content'] ) ) {
51
			$shortcode = sprintf(
52
				'[gv_entry_link %s]%s[/gv_entry_link]',
53
				implode( ' ', $shortcode_attributes ),
54
				wp_kses_post( $attributes['content'] )
55
			);
56
		} else {
57
			$shortcode = sprintf( '[gv_entry_link %s/]', implode( ' ', $shortcode_attributes ) );
58
		}
59
60
		$output = do_shortcode( $shortcode );
61
62
		return $output;
63
	}
64
}
65