PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.7
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.7
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 / endpoints / class-charitable-endpoints.php

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

349 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The endpoint registry class, providing a clean way to access details about individual endpoints.
4 *
5 * @package Charitable/Classes/Charitable_Endpoints
6 * @version 1.5.0
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_Endpoints' ) ) :
16
17 /**
18 * Charitable_Endpoints
19 *
20 * @since 1.5.0
21 */
22 class Charitable_Endpoints {
23
24 /**
25 * Registered endpoints.
26 *
27 * @since 1.5.0
28 *
29 * @var Charitable_Endpoint[]
30 */
31 protected $endpoints;
32
33 /**
34 * Current endpoint.
35 *
36 * @since 1.5.0
37 *
38 * @var string
39 */
40 protected $current_endpoint;
41
42 /**
43 * Create class object.
44 *
45 * @since 1.5.0
46 */
47 public function __construct() {
48 $this->endpoints = array();
49
50 add_action( 'init', array( $this, 'setup_rewrite_rules' ) );
51 add_filter( 'query_vars', array( $this, 'add_query_vars' ) );
52 add_filter( 'template_include', array( $this, 'template_loader' ), 12 );
53 add_filter( 'the_content', array( $this, 'get_content' ) );
54 add_filter( 'body_class', array( $this, 'add_body_classes' ) );
55 }
56
57 /**
58 * Register an endpoint.
59 *
60 * @since 1.5.0
61 *
62 * @param Charitable_Endpoint $endpoint The endpoint object.
63 * @return boolean True if the endpoint was registered. False if it was already registered.
64 */
65 public function register( Charitable_Endpoint $endpoint ) {
66 $endpoint_id = $endpoint->get_endpoint_id();
67
68 if ( $this->endpoint_exists( $endpoint_id ) ) {
69 charitable_get_deprecated()->doing_it_wrong(
70 __METHOD__,
71 sprintf( __( 'Endpoint %s has already been registered.', 'charitable' ), $endpoint_id ),
72 '1.5.0'
73 );
74
75 return false;
76 }
77
78 $this->endpoints[ $endpoint_id ] = $endpoint;
79
80 return true;
81 }
82
83 /**
84 * Get the permalink/URL of a particular endpoint.
85 *
86 * @since 1.5.0
87 *
88 * @param string $endpoint The endpoint id.
89 * @param array $args Optional array of arguments.
90 * @return string|false
91 */
92 public function get_page_url( $endpoint, $args = array() ) {
93 $endpoint = $this->sanitize_endpoint( $endpoint );
94 $default = '';
95
96 if ( $this->endpoint_exists( $endpoint ) ) {
97 $default = $this->endpoints[ $endpoint ]->get_page_url( $args );
98 }
99
100 /**
101 * Filter the URL of a particular endpoint.
102 *
103 * The hook takes the format of charitable_permalink_{endpoint}_page. For example,
104 * for the campaign_donation endpoint, the hook is:
105 *
106 * charitable_permalink_campaign_donation_page
107 *
108 * @since 1.0.0
109 *
110 * @param string $default The endpoint's URL.
111 * @param array $args Mixed set of arguments.
112 */
113 return apply_filters( 'charitable_permalink_' . $endpoint . '_page', $default, $args );
114 }
115
116 /**
117 * Checks if we're currently viewing a particular endpoint/page.
118 *
119 * @since 1.5.0
120 *
121 * @param string $endpoint The endpoint id.
122 * @param array $args Optional array of arguments.
123 * @return boolean
124 */
125 public function is_page( $endpoint, $args = array() ) {
126 $endpoint = $this->sanitize_endpoint( $endpoint );
127 $default = '';
128
129 if ( $this->endpoint_exists( $endpoint ) ) {
130 $default = $this->endpoints[ $endpoint ]->is_page( $args );
131 }
132
133 /**
134 * Return whether we are currently viewing a particular endpoint.
135 *
136 * The hook takes the format of charitable_is_page_{endpoint}_page. For example,
137 * for the campaign_donation endpoint, the hook is:
138 *
139 * charitable_is_page_campaign_donation_page
140 *
141 * @since 1.0.0
142 *
143 * @param boolean $default Whether we are currently on the endpoint.
144 * @param array $args Mixed set of arguments.
145 */
146 return apply_filters( 'charitable_is_page_' . $endpoint . '_page', $default, $args );
147 }
148
149 /**
150 * Set up the template for an endpoint.
151 *
152 * @since 1.5.0
153 *
154 * @param string $endpoint The endpoint id.
155 * @param string $default_template The default template to be used if the endpoint doesn't return its own.
156 * @return string $template
157 */
158 public function get_endpoint_template( $endpoint, $default_template ) {
159 $endpoint = $this->sanitize_endpoint( $endpoint );
160
161 if ( ! $this->endpoint_exists( $endpoint ) ) {
162 charitable_get_deprecated()->doing_it_wrong(
163 __METHOD__,
164 sprintf( __( 'Endpoint %s has not been registered.', 'charitable' ), $endpoint ),
165 '1.5.0'
166 );
167
168 return $default_template;
169 }
170
171 return $this->endpoints[ $endpoint ]->get_template( $default_template );
172 }
173
174 /**
175 * Set up the rewrite rules for the site.
176 *
177 * @since 1.5.0
178 *
179 * @return void
180 */
181 public function setup_rewrite_rules() {
182 foreach ( $this->endpoints as $endpoint ) {
183 $endpoint->setup_rewrite_rules();
184 }
185
186 /* Set up any common rewrite tags */
187 add_rewrite_tag( '%donation_id%', '([0-9]+)' );
188 }
189
190 /**
191 * Add custom query vars.
192 *
193 * @since 1.5.0
194 *
195 * @param string[] $vars The query vars.
196 * @return string[]
197 */
198 public function add_query_vars( $vars ) {
199 foreach ( $this->endpoints as $endpoint ) {
200 $vars = $endpoint->add_query_vars( $vars );
201 }
202
203 return array_merge( $vars, array( 'donation_id', 'cancel' ) );
204 }
205
206 /**
207 * Load templates for our endpoints.
208 *
209 * @since 1.5.0
210 *
211 * @param string $template The default template.
212 * @return void
213 */
214 public function template_loader( $template ) {
215 $current_endpoint = $this->get_current_endpoint();
216
217 if ( ! $current_endpoint ) {
218 return $template;
219 }
220
221 $template_options = $this->endpoints[ $current_endpoint ]->get_template( $template );
222
223 if ( $template_options == $template ) {
224 return $template_options;
225 }
226
227 $template_options = apply_filters( 'charitable_' . $current_endpoint . '_page_template', $template_options );
228
229 return charitable_get_template_path( $template_options, $template );
230 }
231
232 /**
233 * Get the content to display for the endpoint we're viewing.
234 *
235 * @since 1.5.0
236 *
237 * @param string $content The default content.
238 * @param false|string $endpoint Fetch the content for a specific endpoint.
239 * @return string
240 */
241 public function get_content( $content, $endpoint = false ) {
242 if ( ! $endpoint ) {
243 $endpoint = $this->get_current_endpoint();
244 }
245
246 if ( ! $endpoint ) {
247 return $content;
248 }
249
250 return $this->endpoints[ $endpoint ]->get_content( $content );
251 }
252
253 /**
254 * Add any custom body classes defined for the endpoint we're viewing.
255 *
256 * @since 1.5.0
257 *
258 * @param string[] $classes The list of body classes.
259 * @return string[]
260 */
261 public function add_body_classes( $classes ) {
262 $endpoint = $this->get_current_endpoint();
263
264 if ( ! $endpoint ) {
265 return $classes;
266 }
267
268 $classes[] = $this->endpoints[ $endpoint ]->get_body_class();
269
270 return $classes;
271 }
272
273 /**
274 * Return the current endpoint.
275 *
276 * @since 1.5.0
277 *
278 * @return string|false String if we're on one of our endpoints. False otherwise.
279 */
280 public function get_current_endpoint() {
281 if ( ! isset( $this->current_endpoint ) ) {
282
283 foreach ( $this->endpoints as $endpoint_id => $endpoint ) {
284 if ( $this->is_page( $endpoint_id, array( 'strict' => true ) ) ) {
285 $this->current_endpoint = $endpoint_id;
286
287 return $this->current_endpoint;
288 }
289 }
290
291 $this->current_endpoint = false;
292 }
293
294 return $this->current_endpoint;
295 }
296
297 /**
298 * Return a list of all endpoints that should not be cached.
299 *
300 * @since 1.5.4
301 *
302 * @return array
303 */
304 public function get_non_cacheable_endpoints() {
305 $endpoints = array();
306
307 foreach ( $this->endpoints as $endpoint_id => $endpoint ) {
308 if ( ! $endpoint->is_cacheable() ) {
309 $endpoints[] = $endpoint_id;
310 }
311 }
312
313 return $endpoints;
314 }
315
316 /**
317 * Checks whether a particular endpoint exists.
318 *
319 * @since 1.5.9
320 *
321 * @param string $endpoint The endpoint ID.
322 * @return boolean
323 */
324 public function endpoint_exists( $endpoint ) {
325 return array_key_exists( $endpoint, $this->endpoints );
326 }
327
328 /**
329 * Remove _page from the endpoint (required for backwards compatibility)
330 * and make sure donation_cancel is changed to donation_cancellation.
331 *
332 * @since 1.5.0
333 *
334 * @param string $endpoint The endpoint id.
335 * @return string
336 */
337 protected function sanitize_endpoint( $endpoint ) {
338 $endpoint = str_replace( '_page', '', $endpoint );
339
340 if ( 'donation_cancel' == $endpoint ) {
341 $endpoint = 'donation_cancellation';
342 }
343
344 return $endpoint;
345 }
346 }
347
348 endif;
349