/
class-llms-blocks-php-template-block.php
144 lines (123 loc) · 3.37 KB
/
class-llms-blocks-php-template-block.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* PHP Template block class file.
*
* @package LifterLMS_Blocks/Blocks
*
* @since 1.0.0
* @version 2.3.0
*
* @render_hook llms_php-template_render
*/
defined( 'ABSPATH' ) || exit;
/**
* PHP Template block.
*
* @since 2.3.0
*/
class LLMS_Blocks_PHP_Template_Block extends LLMS_Blocks_Abstract_Block {
/**
* Block ID.
*
* @var string
*/
protected $id = 'php-template';
/**
* Is block dynamic (rendered in PHP).
*
* @var bool
*/
protected $is_dynamic = true;
/**
* Templates map, where the keys are the template attribute value and the values are the php file names (w/o extension).
*
* @var array
*/
protected $templates = array(
'archive-course' => 'loop-main',
'archive-llms_membership' => 'loop-main',
'taxonomy-course_cat' => 'loop-main',
'taxonomy-course_difficulty' => 'loop-main',
'taxonomy-course_tag' => 'loop-main',
'taxonomy-course_track' => 'loop-main',
'taxonomy-membership_cat' => 'loop-main',
'taxonomy-membership_tag' => 'loop-main',
'single-certificate' => 'content-certificate',
'single-no-access' => 'content-no-access',
);
/**
* Add actions attached to the render function action.
*
* @since 2.3.0
*
* @param array $attributes Optional. Block attributes. Default empty array.
* @param string $content Optional. Block content. Default empty string.
* @return void
*/
public function add_hooks( $attributes = array(), $content = '' ) {
add_action( $this->get_render_hook(), array( $this, 'output' ), 10 );
}
/**
* Retrieve custom block attributes.
*
* Necessary to override when creating ServerSideRender blocks.
*
* @since 2.3.0
*
* @return array
*/
public function get_attributes() {
return array(
'template' => array(
'type' => 'string',
'default' => '',
),
'title' => array(
'type' => 'string',
'default' => '',
),
);
}
/**
* Output the template.
*
* @since 2.3.0
*
* @param array $attributes Optional. Block attributes. Default empty array.
* @return void
*/
public function output( $attributes = array() ) {
if ( empty( $attributes['template'] ) ) {
return;
}
/**
* Filters the php templates that can be render via this block.
*
* @since 2.3.0
*
* @param array $templates Templates map, where the keys are the template attribute value and the values are the php file names (w/o extension).
*/
$templates = apply_filters( 'llms_blocks_php_templates_block', $this->templates );
if ( ! array_key_exists( $attributes['template'], $templates ) ) {
return;
}
ob_start();
llms_get_template( "{$templates[$attributes['template']]}.php" );
$block_content = ob_get_clean();
/**
* Filters the block html.
*
* @since 2.3.0
*
* @param string $block_content The block's html.
* @param array $attributes The block's array of attributes.
* @param array $template The template file basename to be rendered.
* @param LLMS_Blocks_PHP_Template_Block $block This block object.
*/
$block_content = apply_filters( 'llms_blocks_render_php_template_block', $block_content, $attributes, $templates[ $attributes['template'] ], $this );
if ( $block_content ) {
echo $block_content;
}
}
}
return new LLMS_Blocks_PHP_Template_Block();