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