Completed
Pull Request — staging (#840)
by
unknown
19:58
created

BaseShortcode   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 128
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 4

8 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
A process_shortcode() 0 6 1
A render() 0 11 2
A process_attributes() 0 3 1
A get_context() 0 3 1
A get_tag() 0 7 2
A get_view_uri() 0 7 2
get_default_atts() 0 1 ?
1
<?php
2
/**
3
 * YIKES Inc. Easy Forms.
4
 *
5
 * @package   YIKES\EasyForms
6
 * @author    Freddie Mixell
7
 * @license   GPL2
8
 */
9
10
namespace YIKES\EasyForms\Shortcode;
11
12
use YIKES\EasyForms\Assets\AssetsAware;
13
use YIKES\EasyForms\Assets\AssetsAwareness;
14
use YIKES\EasyForms\Exception\MustExtend;
15
use YIKES\EasyForms\Renderable;
16
use YIKES\EasyForms\Service;
17
use YIKES\EasyForms\View\PostEscapedView;
18
use YIKES\EasyForms\View\TemplatedView;
19
20
/**
21
 * Abstract class BaseShortcode.
22
 *
23
 * @since   %VERSION%
24
 *
25
 * @package YIKES\EasyForms
26
 * @author  Freddie Mixell
27
 */
28
abstract class BaseShortcode implements Renderable, AssetsAware, Service {
29
30
	use AssetsAwareness;
31
32
	const TAG      = '_baseshortcode_';
33
	const VIEW_URI = '_baseviewuri_';
34
35
	/**
36
	 * Register the Shortcode.
37
	 *
38
	 * @since %VERSION%
39
	 */
40
	public function register() {
41
		$this->register_assets();
42
43
		add_action( 'init', function () {
44
			add_shortcode( $this->get_tag(), [ $this, 'process_shortcode' ] );
45
		} );
46
	}
47
48
	/**
49
	 * Process the shortcode attributes and prepare rendering.
50
	 *
51
	 * @since %VERSION%
52
	 *
53
	 * @param array|string $atts Attributes as passed to the shortcode.
54
	 *
55
	 * @return string Rendered HTML of the shortcode.
56
	 */
57
	public function process_shortcode( $atts ) {
58
		$atts    = $this->process_attributes( $atts );
59
		$context = $this->get_context( $atts );
60
61
		return $this->render( array_merge( $atts, $context ) );
62
	}
63
64
	/**
65
	 * Render the current Renderable.
66
	 *
67
	 * @since %VERSION%
68
	 *
69
	 * @param array $context Context in which to render.
70
	 *
71
	 * @return string Rendered HTML.
72
	 */
73
	public function render( array $context = [] ) {
74
		try {
75
			$this->enqueue_assets();
76
			$view = new PostEscapedView( new TemplatedView( $this->get_view_uri() ) );
77
78
			return $view->render( $context );
79
		} catch ( \Exception $exception ) {
80
			// Don't let exceptions bubble up. Just render an empty shortcode instead.
81
			return '';
82
		}
83
	}
84
85
	/**
86
	 * Process the shortcode attributes.
87
	 *
88
	 * Override to add accepted attributes and their default values.
89
	 *
90
	 * @since %VERSION%
91
	 *
92
	 * @param array|string $atts Raw shortcode attributes passed into the shortcode function.
93
	 *
94
	 * @return array Processed shortcode attributes.
95
	 */
96
	protected function process_attributes( $atts ) {
97
		return shortcode_atts( $this->get_default_atts(), $atts, $this->get_tag() );
98
	}
99
100
	/**
101
	 * Get the context to pass onto the view.
102
	 *
103
	 * Override to provide data to the view that is not part of the shortcode
104
	 * attributes.
105
	 *
106
	 * @since %VERSION%
107
	 *
108
	 * @param array $atts Array of shortcode attributes.
109
	 *
110
	 * @return array Context to pass onto view.
111
	 */
112
	protected function get_context( array $atts ) {
113
		return [];
114
	}
115
116
	/**
117
	 * Get the tag to use for the shortcode.
118
	 *
119
	 * @since %VERSION%
120
	 *
121
	 * @return string Tag of the shortcode.
122
	 * @throws MustExtend When the default tag has not been extended.
123
	 */
124
	protected function get_tag() {
125
		if ( self::TAG === static::TAG ) {
126
			throw MustExtend::default_tag( self::TAG );
127
		}
128
129
		return static::TAG;
130
	}
131
132
	/**
133
	 * Get the View URI to use for rendering the shortcode.
134
	 *
135
	 * @since %VERSION%
136
	 *
137
	 * @return string View URI.
138
	 * @throws MustExtend When the default view URI has not been extended.
139
	 */
140
	protected function get_view_uri() {
141
		if ( self::VIEW_URI === static::VIEW_URI ) {
142
			throw MustExtend::default_view( self::VIEW_URI );
143
		}
144
145
		return static::VIEW_URI;
146
	}
147
148
	/**
149
	 * Get the default array of attributes for the shortcode.
150
	 *
151
	 * @since %VERSION%
152
	 * @return array
153
	 */
154
	abstract protected function get_default_atts();
155
}
156