PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.31
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.31
1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 1.8.0 All 210 releases
charitable / includes / public / class-charitable-template.php

class-charitable-template.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.6.31, at includes/public/class-charitable-template.php

255 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable template.
4 *
5 * @package Charitable/Classes/Charitable_Template
6 * @author Eric Daams
7 * @copyright Copyright (c) 2020, Studio 164a
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.0.0
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Template' ) ) :
19
20 /**
21 * Charitable_Template
22 *
23 * @since 1.0.0
24 */
25 class Charitable_Template {
26
27 /**
28 * Theme template path.
29 *
30 * @since 1.0.0
31 *
32 * @var string
33 */
34 protected $theme_template_path;
35
36 /**
37 * Template names to be loaded.
38 *
39 * @since 1.0.0
40 *
41 * @var string[]
42 */
43 protected $template_names;
44
45 /**
46 * Whether to load template file if it is found.
47 *
48 * @since 1.0.0
49 *
50 * @var boolean
51 */
52 protected $load;
53
54 /**
55 * Whether to use require_once or require.
56 *
57 * @since 1.0.0
58 *
59 * @var boolean
60 */
61 protected $require_once;
62
63 /**
64 * The arguments available in the view.
65 *
66 * @since 1.0.0
67 *
68 * @var array
69 */
70 protected $view_args;
71
72 /**
73 * Class constructor.
74 *
75 * @since 1.0.0
76 *
77 * @param string|array $template_name A single template name or an ordered array of template.
78 * @param boolean $load If true the template file will be loaded if it is found.
79 * @param boolean $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
80 */
81 public function __construct( $template_name, $load = true, $require_once = true ) {
82 $this->load = $load;
83 $this->theme_template_path = $this->get_theme_template_path();
84 $this->base_template_path = $this->get_base_template_path();
85
86 /**
87 * Filter the template name.
88 *
89 * @since 1.0.0
90 *
91 * @param array $template_names Set of template names to check for.
92 */
93 $this->template_names = apply_filters( 'charitable_template_name', (array) $template_name );
94 $this->view_args = array();
95
96 if ( $this->load ) {
97 $this->render( $require_once );
98 }
99 }
100
101 /**
102 * Set theme template path.
103 *
104 * @since 1.0.0
105 *
106 * @return string
107 */
108 public function get_theme_template_path() {
109 /**
110 * Customize the directory to use for template files in themes/child themes.
111 *
112 * @since 1.0.0
113 *
114 * @param string $directory The directory, relative to the theme or child theme's root directory.
115 */
116 return trailingslashit( apply_filters( 'charitable_theme_template_path', 'charitable' ) );
117 }
118
119 /**
120 * Return the base template path.
121 *
122 * @since 1.0.0
123 *
124 * @return string
125 */
126 public function get_base_template_path() {
127 return charitable()->get_path( 'templates' );
128 }
129
130 /**
131 * Adds an array of view arguments.
132 *
133 * @since 1.0.0
134 *
135 * @param array $args Arguments to include in the view.
136 * @return void
137 */
138 public function set_view_args( $args ) {
139 $this->view_args = array_merge( $this->view_args, $args );
140 }
141
142 /**
143 * Adds an argument to be accessed within the view.
144 *
145 * @since 1.0.0
146 *
147 * @param string $key The key of the argument.
148 * @param mixed $value The vaalue of the argument.
149 * @return void
150 */
151 public function set_view_arg( $key, $value ) {
152 $this->view_args[ $key ] = $value;
153 }
154
155 /**
156 * Renders the template.
157 *
158 * @since 1.0.0
159 *
160 * @param boolean $require_once Whether the template should be loaded with include_once instead of include.
161 * @return false|string False if the template does not exist. Template file otherwise.
162 */
163 public function render( $require_once = true ) {
164 /* Make sure that the template file exists. */
165 $template = $this->locate_template();
166
167 if ( ! $this->template_file_exists( $template ) ) {
168 charitable_get_deprecated()->doing_it_wrong(
169 __FUNCTION__,
170 sprintf( '<code>%s</code> does not exist.', $template ),
171 '1.0.0'
172 );
173 return false;
174 }
175
176 if ( $template ) {
177
178 $view_args = $this->view_args;
179
180 if ( $this->require_once ) {
181 include_once( $template );
182 } else {
183 include( $template );
184 }
185 }
186
187 return $template;
188 }
189
190 /**
191 * Locate the template file of the highest priority.
192 *
193 * @uses locate_template()
194 *
195 * @since 1.0.0
196 *
197 * @return string
198 */
199 public function locate_template() {
200 /* Template options are first checked in the theme/child theme using locate_template. */
201 $template = locate_template( $this->get_theme_template_options(), false );
202
203 /* No templates found in the theme/child theme, so use the plugin's default template. */
204 if ( ! $template ) {
205 $template = $this->base_template_path . $this->template_names[0];
206 }
207
208 /**
209 * Filter the template path before rendering it.
210 *
211 * @since 1.0.0
212 *
213 * @param string $template_path The template path to render.
214 * @param array $template_names Set of template names to check for.
215 */
216 return apply_filters( 'charitable_locate_template', $template, $this->template_names );
217 }
218
219 /**
220 * Checks whether the template file exists.
221 *
222 * @since 1.0.0
223 *
224 * @param string $template The template file to check for.
225 * @return boolean
226 */
227 public function template_file_exists( $template = '' ) {
228 if ( empty( $template ) ) {
229 $template = $this->locate_template();
230 }
231
232 return file_exists( $template );
233 }
234
235 /**
236 * Return the theme template options for a specific template.
237 *
238 * @since 1.0.0
239 *
240 * @return string[]
241 */
242 protected function get_theme_template_options() {
243 $options = array();
244
245 foreach ( $this->template_names as $template_name ) {
246 $options[] = $this->theme_template_path . $template_name;
247 $options[] = $template_name;
248 }
249
250 return $options;
251 }
252 }
253
254 endif;
255