Passed
Push — master ( d98594...cee2ee )
by Paul
07:20 queued 03:47
created

SiteReviewsBlock::filterInterpolation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Blocks;
4
5
use GeminiLabs\SiteReviews\Blocks\BlockGenerator;
6
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsShortcode as Shortcode;
7
8
class SiteReviewsBlock extends BlockGenerator
9
{
10
	/**
11
	 * @return array
12
	 */
13
	public function attributes()
14
	{
15
		return [
16
			'assigned_to' => [
17
				'default' => '',
18
				'type' => 'string',
19
			],
20
			'category' => [
21
				'default' => '',
22
				'type' => 'string',
23
			],
24
			'className' => [
25
				'default' => '',
26
				'type' => 'string',
27
			],
28
			'count' => [
29
				'default' => 5,
30
				'type' => 'number',
31
			],
32
			'hide' => [
33
				'default' => '',
34
				'type' => 'string',
35
			],
36
			'id' => [
37
				'default' => '',
38
				'type' => 'string',
39
			],
40
			'pagination' => [
41
				'default' => '',
42
				'type' => 'string',
43
			],
44
			'post_id' => [
45
				'default' => '',
46
				'type' => 'string',
47
			],
48
			'rating' => [
49
				'default' => 1,
50
				'type' => 'number',
51
			],
52
			'schema' => [
53
				'default' => false,
54
				'type' => 'boolean',
55
			],
56
			'type' => [
57
				'default' => '',
58
				'type' => 'string',
59
			],
60
		];
61
	}
62
63
	/**
64
	 * @return string
65
	 */
66
	public function render( array $attributes )
67
	{
68
		$attributes['class'] = $attributes['className'];
69
		$shortcode = glsr( Shortcode::class );
70
		if( filter_input( INPUT_GET, 'context' ) == 'edit' ) {
71
			$attributes = $this->normalize( $attributes );
72
			$this->filterReviewLinks();
73
			$this->filterShortcodeClass();
74
			$this->filterShowMoreLinks( 'content' );
75
			$this->filterShowMoreLinks( 'response' );
76
			if( !$this->hasVisibleFields( $shortcode, $attributes )) {
77
				$this->filterInterpolation();
78
			}
79
		}
80
		return $shortcode->buildShortcode( $attributes );
81
	}
82
83
	/**
84
	 * @return void
85
	 */
86
	protected function filterInterpolation()
87
	{
88
		add_filter( 'site-reviews/interpolate/reviews', function( $context ) {
89
			$context['class'] = 'glsr-default glsr-block-disabled';
90
			$context['reviews'] = __( 'You have hidden all of the fields for this block.', 'site-reviews' );
91
			return $context;
92
		});
93
	}
94
95
	/**
96
	 * @return void
97
	 */
98
	protected function filterReviewLinks()
99
	{
100
		add_filter( 'site-reviews/rendered/template/reviews', function( $template ) {
101
			return str_replace( '<a', '<a tabindex="-1"', $template );
102
		});
103
	}
104
105
	/**
106
	 * @return void
107
	 */
108
	protected function filterShortcodeClass()
109
	{
110
		add_filter( 'site-reviews/style', function() {
111
			return 'default';
112
		});
113
	}
114
115
	/**
116
	 * @param string $field
117
	 * @return void
118
	 */
119
	protected function filterShowMoreLinks( $field )
120
	{
121
		add_filter( 'site-reviews/review/wrap/'.$field, function( $value ) {
122
			$value = preg_replace(
123
				'/(.*)(<span class="glsr-hidden)(.*)(<\/span>)(.*)/s',
124
				'$1... <a href="#" class="glsr-read-more" tabindex="-1">'.__( 'Show more', 'site-reviews' ).'</a>$5',
125
				$value
126
			);
127
			return $value;
128
		});
129
	}
130
}
131