PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.2
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.2, at includes/public/class-charitable-template.php

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