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

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