PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.2
1.8.13.1 1.8.13 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 All 212 releases
charitable / includes / public / charitable-template-helpers.php

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

410 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Template Helpers.
4 *
5 * Functions used to assist with rendering templates.
6 *
7 * @package Charitable/Functions/Templates
8 * @author David Bisset
9 * @copyright Copyright (c) 2022, WP Charitable LLC
10 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11 * @since 1.0.0
12 * @version 1.6.34
13 */
14
15 // Exit if accessed directly.
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 /**
21 * Displays a template.
22 *
23 * @since 1.0.0
24 * @since 1.5.2 Added $classname parameter.
25 *
26 * @param string|string[] $template_name A single template name or an ordered array of template.
27 * @param mixed[] $args Optional array of arguments to pass to the view.
28 * @param string $classname Template class name. Allows for extensions to use charitable_template().
29 * @return Charitable_Template
30 */
31 function charitable_template( $template_name, array $args = array(), $classname = 'Charitable_Template' ) {
32 $classname = apply_filters( 'charitable_template_class_name', $classname, $template_name, $args );
33
34 if ( ! class_exists( $classname ) ) {
35 $classname = 'Charitable_Template';
36 }
37
38 $class = class_exists( $classname ) ? $classname : 'Charitable_Template';
39 if ( empty( $args ) ) {
40 $template = new $classname( $template_name );
41 } else {
42 $template = new $classname( $template_name, false );
43 $template->set_view_args( $args );
44 $template->render();
45 }
46
47 return $template;
48 }
49
50 /**
51 * When a session-reliant template is displayed without a session ID being
52 * set, wrap the output in a wrapper that tells our client-side session
53 * handler to fetch content via AJAX.
54 *
55 * @since 1.5.0
56 *
57 * @param string|string[] $template_name A single template name or an ordered array of template.
58 * @param mixed[] $args Array of arguments to pass to the view.
59 * @param string $template_key A key representing the template, which will be passed
60 * as one of the arguments in the AJAX request.
61 * @param array $wrapper_args A mixed set of arguments that need to be passed along
62 * in the AJAX request.
63 * @param string $classname Template class name. Allows for extensions to use charitable_template().
64 * @return void Content is echoed.
65 */
66 function charitable_template_from_session( $template_name, array $args, $template_key, $wrapper_args = array(), $classname = 'Charitable_Template' ) {
67 ob_start();
68
69 charitable_template( $template_name, $args, $classname );
70
71 echo charitable_template_from_session_content( $template_key, $wrapper_args, ob_get_clean() );
72 }
73
74 /**
75 * Returns a piece of content or an empty string, wrapped in the correct markup
76 * for on-demand session content retrieval.
77 *
78 * @since 1.5.0
79 *
80 * @param string $template_key A key representing the template, which will be passed
81 * as one of the arguments in the AJAX request.
82 * @param array $wrapper_args A mixed set of arguments that need to be passed along
83 * in the AJAX request.
84 * @param string $default The default content to be displayed if no content is
85 * returned from the AJAX request, or the user does not
86 * have Javascript enabled.
87 * @return string
88 */
89 function charitable_template_from_session_content( $template_key, $wrapper_args = array(), $default = '' ) {
90 /* When we have a session ID, we just print the default. */
91 if ( charitable_get_session()->has_session_id() ) {
92 return $default;
93 }
94
95 $content = '<div class="charitable-session-content" data-template="' . esc_attr( $template_key ) . '" data-args="' . esc_attr( json_encode( $wrapper_args ) ) . '" style="display: none;">' . $default . '</div>';
96 $content .= '<noscript>' . $default . '</noscript>';
97
98 return $content;
99 }
100
101 /**
102 * Return the template path if the template exists. Otherwise, return default.
103 *
104 * @since 1.0.0
105 *
106 * @param string|string[] $template The template to load.
107 * @param string $default The default to load if the template file doesn't exist.
108 * @return string The template path if the template exists. Otherwise, return default.
109 */
110 function charitable_get_template_path( $template, $default = '' ) {
111 $t = new Charitable_Template( $template, false );
112 $path = $t->locate_template();
113
114 if ( ! file_exists( $path ) ) {
115 $path = $default;
116 }
117
118 return $path;
119 }
120
121 /**
122 * Insert a particular template into an array of templates.
123 *
124 * @since 1.5.0
125 *
126 * @param string $template The template to be inserted.
127 * @param array $templates The default set of templates.
128 * @param int $index The position at which to insert the template.
129 * @return array
130 */
131 function charitable_splice_template( $template, $templates, $index = 1 ) {
132 if ( empty( $template ) ) {
133 return $templates;
134 }
135
136 array_splice( $templates, $index, 0, $template );
137
138 return $templates;
139 }
140
141 /**
142 * Simple CSS compression.
143 *
144 * Removes all comments, removes spaces after colons and strips out all the whitespace.
145 *
146 * Based on http://manas.tungare.name/software/css-compression-in-php/
147 *
148 * @since 1.2.0
149 *
150 * @param string $css The block of CSS to be compressed.
151 * @return string The compressed CSS
152 */
153 function charitable_compress_css( $css ) {
154 /* 1. Remove comments */
155 $css = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css );
156
157 /* 2. Remove space after colons */
158 $css = str_replace( ': ', ':', $css );
159
160 /* 3. Remove whitespace */
161 $css = str_replace( array( "\r\n", "\r", "\n", "\t", ' ', ' ', ' ' ), '', $css );
162
163 return $css;
164 }
165
166 /**
167 * Provides arguments passed to campaigns within the loop.
168 *
169 * @since 1.2.3
170 *
171 * @param mixed[] $view_args Optional. If called by the shortcode, this will contain the arguments passed to the shortcode.
172 * @return mixed[]
173 */
174 function charitable_campaign_loop_args( $view_args = array() ) {
175 $defaults = array(
176 'button' => 'donate',
177 );
178
179 $args = wp_parse_args( $view_args, $defaults );
180
181 return apply_filters( 'charitable_campaign_loop_args', $args );
182 }
183
184 /**
185 * Provides arguments passed to campaigns within the loop.
186 *
187 * @since 1.5.7
188 *
189 * @param mixed[] $view_args Optional. If called by the shortcode, this will contain the arguments passed to the shortcode.
190 * @return mixed[]
191 */
192 function charitable_campaign_loop_class( $view_args = array() ) {
193 $classes = array( 'campaign-loop' );
194
195 if ( $view_args['columns'] > 1 ) {
196 $classes = array_merge( $classes, array( 'campaign-grid campaign-grid-' . $view_args['columns'] ) );
197
198 if ( $view_args['masonry'] ) {
199 $classes[] = 'masonry';
200 }
201 } else {
202 $classes[] = 'campaign-list';
203 }
204
205 /**
206 * Filter the classes to be applied to the campaign loop.
207 *
208 * @since 1.5.7
209 *
210 * @param array $classes Array of classes.
211 * @param array $view_args View args.
212 */
213 $classes = apply_filters( 'charitable_campaign_loop_classes', $classes, $view_args );
214
215 return implode( ' ', $classes );
216 }
217
218 /**
219 * Processes arbitrary form attributes into HTML-safe key/value pairs
220 *
221 * @since 1.3.0
222 *
223 * @param array $field Array defining the form field attributes.
224 * @return string The formatted HTML-safe attributes.
225 */
226 function charitable_get_arbitrary_attributes( $field ) {
227 if ( ! isset( $field['attrs'] ) ) {
228 $field['attrs'] = array();
229 }
230
231 /* Add backwards compatibility support for placeholder, min, max, step, pattern, rows and required. */
232 foreach ( array( 'placeholder', 'min', 'max', 'step', 'pattern', 'rows', 'required' ) as $attr ) {
233
234 /* Skip if the attribute is not defined in the field, or is explicitly set in the 'attrs' property. */
235 if ( ! array_key_exists( $attr, $field ) || array_key_exists( $attr, $field['attrs'] ) ) {
236 continue;
237 }
238
239 if ( 'required' == $attr ) {
240 /* Skip required attribute if it's not true. */
241 if ( ! $field['required'] ) {
242 continue;
243 }
244
245 $field['attrs'][ $attr ] = 'required';
246 } else {
247 $field['attrs'][ $attr ] = $field[ $attr ];
248 }
249 }
250
251 $output = '';
252
253 foreach ( $field['attrs'] as $key => $value ) {
254 $escaped_value = esc_attr( $value );
255 $output .= " $key=\"$escaped_value\" ";
256 }
257
258 return apply_filters( 'charitable_arbitrary_field_attributes', $output );
259 }
260
261 /**
262 * Checks whether we are currently in the main loop on a singular page.
263 *
264 * This should be used in any functions run on the_content hook, to prevent
265 * Charitable's filters touching other the_content instances outside the main
266 * loop.
267 *
268 * @since 1.4.11
269 *
270 * @return boolean
271 */
272 function charitable_is_main_loop() {
273 return is_single() && in_the_loop() && is_main_query();
274 }
275
276 /**
277 * Returns the current URL.
278 *
279 * @see https://gist.github.com/leereamsnyder/fac3b9ccb6b99ab14f36
280 *
281 * @since 1.0.0
282 *
283 * @global WP $wp
284 * @return string
285 */
286 function charitable_get_current_url() {
287 global $wp;
288
289 return trailingslashit(
290 add_query_arg(
291 array_key_exists( 'QUERY_STRING', $_SERVER ) ? $_SERVER['QUERY_STRING'] : '',
292 '',
293 home_url( $wp->request )
294 )
295 );
296 }
297
298 /**
299 * Returns the URL to which the user should be redirected after signing on or registering an account.
300 *
301 * @since 1.0.0
302 *
303 * @return string
304 */
305 function charitable_get_login_redirect_url() {
306 if ( isset( $_REQUEST['redirect_to'] ) ) {
307 $redirect = $_REQUEST['redirect_to'];
308 } elseif ( charitable_get_permalink( 'profile_page' ) ) {
309 $redirect = charitable_get_permalink( 'profile_page' );
310 } else {
311 $redirect = home_url();
312 }
313
314 return apply_filters( 'charitable_signon_redirect_url', $redirect );
315 }
316
317 /**
318 * Displays a table.
319 *
320 * @since 1.5.0
321 *
322 * @param array $columns The set of table columns.
323 * @param array $data The data to display in the table.
324 * @param array $args Optional set of extra arguments.
325 * @return void
326 */
327 function charitable_table_template( array $columns, array $data, $args = array() ) {
328 $table = new Charitable_Table_Helper( $columns, $data, $args );
329 $table->render();
330 }
331
332 /**
333 * Return the email verification link.
334 *
335 * @since 1.5.0
336 * @since 1.6.32 Added $force_send argument.
337 *
338 * @param WP_User $user An instance of `WP_User`.
339 * @param string|false $redirect_url The URL to redirect to after verification.
340 * @param boolean $force_send Whether the link should include an argument to force
341 * resending the email, even if it was sent recently.
342 * @return string
343 */
344 function charitable_get_email_verification_link( WP_User $user, $redirect_url = false, $force_send = false ) {
345 return add_query_arg(
346 array(
347 'charitable_action' => 'verify_email',
348 'user' => $user->ID,
349 'redirect_url' => $redirect_url,
350 'force_send' => (int) $force_send,
351 )
352 );
353 }
354
355 /**
356 * Get the button classes for a Charitable button.
357 *
358 * @since 1.6.29
359 *
360 * @param string $button Which button we're showing.
361 * @return string
362 */
363 function charitable_get_button_class( $button ) {
364 $classes = [ 'button', 'charitable-button' ];
365
366 switch ( $button ) {
367 case 'donate':
368 $classes[] = 'donate-button';
369 $classes[] = 'button-primary';
370 break;
371
372 default:
373 $classes[] = $button . '-button';
374 }
375
376 /**
377 * Filter the button classes.
378 *
379 * @since 1.6.29
380 *
381 * @param array $classes The button classes.
382 * @param string $button The specific button we're showing.
383 */
384 $classes = apply_filters( 'charitable_button_class', $classes, $button );
385
386 return implode( ' ', $classes );
387 }
388
389 /**
390 * Get a unique donation form ID.
391 *
392 * @since 1.6.57
393 *
394 * @return string
395 */
396 function charitable_get_donation_form_id() {
397 static $form_number;
398
399 if ( ! isset( $form_number ) ) {
400 $form_id = 'charitable-donation-form';
401 $form_number = 1;
402 } else {
403 $form_id = 'charitable-donation-form-' . $form_number;
404 $form_number += 1;
405 }
406
407 // $form_number = ;
408
409 return $form_id;
410 }