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

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