View.php
1.76 KB
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
<?php
/**
* A base class for all views.
*
* @since 4.9.0
*
* @package LearnDash\Core
*/
namespace LearnDash\Core\Template;
/**
* A base class for all views.
*
* @since 4.9.0
*/
abstract class View {
/**
* View slug.
*
* @since 4.9.0
*
* @var string
*/
protected $view_slug;
/**
* Context.
*
* @since 4.9.0
*
* @var array<string, mixed>
*/
protected $context;
/**
* Template.
*
* @since 4.9.0
*
* @var ?Template
*/
protected $template;
/**
* Whether the view is for an admin page.
*
* @since 4.9.0
*
* @var bool
*/
protected $is_admin;
/**
* Constructor.
*
* @since 4.9.0
*
* @param string $view_slug View slug.
* @param array<mixed> $context Context.
* @param bool $is_admin Whether the view is for an admin page. Default false.
*/
public function __construct( string $view_slug, array $context = [], bool $is_admin = false ) {
$this->view_slug = $view_slug;
$this->is_admin = $is_admin;
$this->context = array_merge(
$context,
array(
'user' => wp_get_current_user(),
)
);
}
/**
* Gets the view HTML.
*
* @since 4.9.0
*
* @return string
*/
public function get_html(): string {
$template = new Template( $this->view_slug, $this->context, $this->is_admin, $this );
$this->set_template( $template );
return $template->get_content();
}
/**
* Gets the template object.
*
* @since 4.9.0
*
* @return Template|null
*/
public function get_template(): ?Template {
return $this->template;
}
/**
* Sets the template object.
*
* @since 4.9.0
*
* @param Template $template The template object.
*
* @return View
*/
public function set_template( Template $template ): View {
$this->template = $template;
return $this;
}
}