PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
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 1.7.0.9 All 211 releases
charitable / includes / campaigns / charitable-campaign-functions.php

charitable-campaign-functions.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.13, at includes/campaigns/charitable-campaign-functions.php

452 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Campaign Functions.
4 *
5 * Campaign related functions.
6 *
7 * @package Charitable/Functions/Campaign
8 * @author David Bisset
9 * @copyright Copyright (c) 2023, 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.54
13 */
14
15 // Exit if accessed directly.
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 /**
21 * Returns the given campaign.
22 *
23 * @since 1.0.0
24 *
25 * @param int $campaign_id The campaign ID.
26 * @return Charitable_Campaign
27 */
28 function charitable_get_campaign( $campaign_id ) {
29 return new Charitable_Campaign( $campaign_id );
30 }
31
32 /**
33 * Create a campaign.
34 *
35 * @since 1.5.9
36 *
37 * @param array $args Values for the campaign.
38 * @return int
39 */
40 function charitable_create_campaign( array $args ) {
41 $processor = new Charitable_Campaign_Processor( $args );
42 return $processor->save();
43 }
44
45 /**
46 * Returns the current campaign.
47 *
48 * @since 1.0.0
49 *
50 * @return Charitable_Campaign|false Campaign object if we're viewing a campaign
51 * within a loop. False otherwise.
52 */
53 function charitable_get_current_campaign() {
54 return charitable_get_request()->get_current_campaign();
55 }
56
57 /**
58 * Returns the current campaign ID.
59 *
60 * @since 1.0.0
61 *
62 * @return int
63 */
64 function charitable_get_current_campaign_id() {
65 return charitable_get_request()->get_current_campaign_id();
66 }
67
68 /**
69 * Returns the current campaign template 'slug'.
70 *
71 * @since 1.8.0
72 *
73 * @return string
74 */
75 function charitable_get_current_campaign_template() {
76 return charitable_get_request()->get_current_campaign_template();
77 }
78
79 /**
80 * Returns the current campaign template 'slug'.
81 *
82 * @since 1.8.0
83 *
84 * @return string
85 */
86 function charitable_get_current_campaign_template_id() {
87 return charitable_get_request()->get_current_campaign_template_id();
88 }
89
90 /**
91 * Returns the campaign data (v2).
92 *
93 * @since 1.8.0
94 *
95 * @return string
96 */
97 function charitable_get_current_campaign_data() {
98 return charitable_get_request()->get_current_campaign_builder_data();
99 }
100
101 /**
102 * Returns whether the current user is the creator of the given campaign.
103 *
104 * @since 1.0.0
105 *
106 * @param int $campaign_id The campaign ID.
107 * @return boolean
108 */
109 function charitable_is_current_campaign_creator( $campaign_id = null ) {
110 if ( is_null( $campaign_id ) ) {
111 $campaign_id = charitable_get_current_campaign_id();
112 }
113
114 return get_post_field( 'post_author', $campaign_id ) == get_current_user_id();
115 }
116
117 /**
118 * Returns whether the given campaign can receive donations.
119 *
120 * @since 1.5.14
121 *
122 * @param int $campaign_id The campaign ID.
123 * @return boolean
124 */
125 function charitable_campaign_can_receive_donations( $campaign_id ) {
126 if ( Charitable::CAMPAIGN_POST_TYPE !== get_post_type( $campaign_id ) ) {
127 return false;
128 }
129
130 $campaign = charitable_get_campaign( $campaign_id );
131
132 return $campaign && $campaign->can_receive_donations();
133 }
134
135 /**
136 * Given a campaign ID and a key, return the post field.
137 *
138 * @since 1.6.0
139 *
140 * @param Charitable_Campaign $campaign The campaign object.
141 * @param string $key The meta key.
142 * @return mixed
143 */
144 function charitable_get_campaign_post_field( Charitable_Campaign $campaign, $key ) {
145 if ( ! isset( $campaign->ID ) || 0 === intval( $campaign->ID ) ) {
146 return false;
147 }
148
149 return get_post_field( $key, $campaign->ID );
150 }
151
152 /**
153 * Get the campaign date as a timestamp.
154 *
155 * @since 1.6.54
156 *
157 * @param Charitable_Campaign $campaign The campaign instance.
158 * @return int
159 */
160 function charitable_get_campaign_date_as_timestamp( Charitable_Campaign $campaign ) {
161 return strtotime( $campaign->post_date );
162 }
163
164 /**
165 * Get a particular user field for a campaign's creator.
166 *
167 * @since 1.6.5
168 *
169 * @param Charitable_Campaign $campaign The campaign object.
170 * @param string $key The meta key.
171 * @return string
172 */
173 function charitable_get_campaign_creator_field( Charitable_Campaign $campaign, $key ) {
174 $creator = charitable_get_user( $campaign->get_campaign_creator() );
175 $key = str_replace( 'campaign_creator_', '', $key );
176
177 return $creator->get( $key );
178 }
179
180 /**
181 * Get a particular taxonomy field for a campaign.
182 *
183 * @since 1.6.19
184 *
185 * @param Charitable_Campaign $campaign The campaign object.
186 * @param string $key The meta key.
187 * @return string
188 */
189 function charitable_get_campaign_taxonomy_terms_list( Charitable_Campaign $campaign, $key ) {
190 $taxonomies = array(
191 'categories' => 'campaign_category',
192 'tags' => 'campaign_tag',
193 );
194
195 $taxonomy = array_key_exists( $key, $taxonomies ) ? $taxonomies[ $key ] : $key;
196 $terms = wp_get_object_terms( $campaign->ID, $taxonomy, array( 'fields' => 'names' ) );
197
198 if ( is_wp_error( $terms ) ) {
199 return '';
200 }
201
202 return implode( ', ', $terms );
203 }
204
205 /**
206 * Get the featured image for a particular campaign.
207 *
208 * @since 1.6.25
209 *
210 * @param Charitable_Campaign $campaign The campaign object.
211 * @return string|int
212 */
213 function charitable_get_campaign_featured_image( Charitable_Campaign $campaign ) {
214 return get_post_thumbnail_id( $campaign->ID );
215 }
216
217 /**
218 * Return the default templaate (usually for front-end previews or campaign pages/shortcodes) for campaign builder campaign areas.
219 *
220 * @since 1.8.0
221 *
222 * @return string
223 */
224 function charitable_campaign_builder_default_template() {
225
226 return apply_filters( 'charitable_campaign_builder_default_template', 'basic' );
227 }
228
229 /**
230 * Return name of user - trying first/last name and falling back on "display name".
231 *
232 * @since 1.8.0
233 *
234 * @param int $user_id ID of user.
235 * @return string
236 */
237 function charitable_get_creator_data( $user_id = null ) {
238
239 $user_info = $user_id ? new WP_User( $user_id ) : wp_get_current_user();
240
241 if ( false === $user_info || is_wp_error( $user_info ) ) {
242 return false;
243 }
244
245 if ( $user_info->first_name ) {
246 if ( $user_info->last_name ) {
247 return $user_info->first_name . ' ' . $user_info->last_name;
248 }
249 return $user_info->first_name;
250 }
251
252 return $user_info->display_name;
253 }
254
255 /**
256 * Return the admin URL for editing a campaign.
257 *
258 * @since 1.8.1
259 *
260 * @param int $campaign_id ID of donation, assuming it still exists.
261 * @return string
262 */
263 function charitable_get_admin_campaign_edit_url( $campaign_id = false ) {
264
265 if ( false === $campaign_id || intval( $campaign_id ) === 0 ) {
266 return false;
267 }
268
269 // Determine if this is a "legacy" campaign.
270 $is_legacy_campaign = charitable_is_campaign_legacy( $campaign_id );
271
272 // Determines if we add anything due to debug mode(s).
273 $suffix = ( charitable_is_debug() || charitable_is_script_debug() ) ? '#charitabledebug' : false;
274
275 $admin_campaign_edit_url = ( ! $is_legacy_campaign ) ? admin_url( 'admin.php?page=charitable-campaign-builder&campaign_id=' . $campaign_id ) . $suffix : admin_url( 'post.php?post=' . $campaign_id . '&action=edit' );
276
277 return $admin_campaign_edit_url;
278 }
279
280 /**
281 * Return the admin URL for editing a campaign.
282 *
283 * @since 1.8.1
284 *
285 * @param int $donation_id ID of donation, assuming it still exists.
286 * @return string
287 */
288 function charitable_get_admin_donation_edit_url( $donation_id = false ) {
289
290 if ( false === $donation_id || intval( $donation_id ) === 0 ) {
291 return false;
292 }
293
294 $admin_donation_edit_url = admin_url( 'post.php?post=' . $donation_id ) . '&action=edit';
295
296 return $admin_donation_edit_url;
297 }
298
299 /**
300 * Find the photo in the (v2) campaign settings.
301 *
302 * @since 1.8.2
303 *
304 * @param array $campaign_settings The campaign settings.
305 *
306 * @return mixed
307 */
308 function charitable_find_photo_in_campaign_settings( $campaign_settings ) {
309
310 $layout = isset( $campaign_settings['layout'] ) ? $campaign_settings['layout'] : false;
311
312 if ( ! $layout ) {
313 return false;
314 }
315
316 $found_row_id = -1;
317 $found_field_id = -1;
318
319 foreach ( $layout['rows'] as $row_id => $layout_row ) {
320 $found_row_id = $row_id;
321 if ( ! empty( $layout_row['fields'] ) ) {
322 foreach ( $layout_row['fields'] as $field_id => $layout_field_type ) {
323 if ( $layout_field_type === 'photo' ) {
324 $found_field_id = $field_id;
325 break;
326 }
327 }
328 }
329 }
330
331 if ( $found_field_id < 0 || $found_row_id < 0 ) {
332 return false;
333 }
334
335 $found_field_id = apply_filters( 'charitable_campaign_photo_field_id', $found_field_id, $campaign_settings );
336
337 // get file url.
338 $file_url = ! empty( $campaign_settings['fields'][ $found_field_id ] ) ? ( $campaign_settings['fields'][ $found_field_id ] ) : false;
339 $media_id = chartitable_get_image_attached_id_from_url( $file_url['file'] );
340
341 return array(
342 'file' => $file_url['file'],
343 'media_id' => $media_id,
344 );
345 }
346
347 /**
348 * Charitable's function to get the attachment ID from the image URL.
349 * To be used sparingly, and likely only in the context of the campaign builder. Cache the results to be planned.
350 *
351 * @since 1.8.2
352 *
353 * @param string $image_url The image URL.
354 *
355 * @return mixed
356 */
357 function chartitable_get_image_attached_id_from_url( $image_url = false ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is a legacy function that cannot be renamed.
358 global $wpdb;
359 if ( ! $image_url ) {
360 return false;
361 }
362 $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ) ); // phpcs:ignore
363 if ( ! empty( $attachment[0] ) ) {
364 return $attachment[0];
365 }
366 return false;
367 }
368
369 /**
370 * Find the description in the (v2) campaign settings.
371 *
372 * @since 1.8.2
373 *
374 * @param array $campaign_settings The campaign settings.
375 * @param int $description_limit The description limit.
376 *
377 * @return string
378 */
379 function charitable_find_description_in_campaign_settings( $campaign_settings = false, $description_limit = 0 ) {
380
381 if ( ! $campaign_settings ) {
382 return false;
383 }
384
385 $layout = isset( $campaign_settings['layout'] ) ? $campaign_settings['layout'] : false;
386
387 if ( ! $layout ) {
388 return false;
389 }
390
391 $description = '';
392 $found_row_id = -1;
393 $found_field_id = -1;
394
395 foreach ( $layout['rows'] as $row_id => $layout_row ) {
396 $found_row_id = (int) $row_id;
397 if ( ! empty( $layout_row['fields'] ) ) {
398 foreach ( $layout_row['fields'] as $field_id => $layout_field_type ) {
399 if ( $layout_field_type === 'campaign-description' ) {
400 $found_field_id = (int) $field_id;
401 break;
402 }
403 }
404 }
405 }
406
407 $found_field_id = apply_filters( 'charitable_campaign_description_field_id', $found_field_id, $campaign_settings );
408
409 if ( $found_field_id >= 0 && $found_row_id >= 0 ) {
410 $description = ! empty( $campaign_settings['fields'][ $found_field_id ] ) && ! empty( $campaign_settings['fields'][ $found_field_id ]['content'] ) ? ( $campaign_settings['fields'][ $found_field_id ]['content'] ) : false;
411 }
412
413 // limit the description if the value is greater than 0.
414 // add the "..." if the description is longer than the limit.
415 if ( false !== $description && '' !== trim( $description ) && $description_limit > 0 ) {
416 $description = wp_trim_words( $description, $description_limit, '...' );
417 }
418
419 if ( $description ) {
420 return $description;
421 }
422
423 $found_row_id = -1;
424 $found_field_id = -1;
425
426 foreach ( $layout['rows'] as $row_id => $layout_row ) {
427 $found_row_id = $row_id;
428 if ( ! empty( $layout_row['fields'] ) ) {
429 foreach ( $layout_row['fields'] as $field_id => $layout_field_type ) {
430 if ( $layout_field_type === 'text' ) {
431 $found_field_id = (int) $field_id;
432 break;
433 }
434 }
435 }
436 }
437
438 if ( $found_field_id < 0 || $found_row_id < 0 ) {
439 return false;
440 }
441
442 $description = ! empty( $campaign_settings['fields'][ $found_field_id ] ) ? ( $campaign_settings['fields'][ $found_field_id ]['content'] ) : false;
443
444 // limit the description if the value is greater than 0.
445 // add the "..." if the description is longer than the limit.
446 if ( false !== $description && '' !== trim( $description ) && $description_limit > 0 ) {
447 $description = wp_trim_words( $description, $description_limit, '...' );
448 }
449
450 return apply_filters( 'charitable_campaign_settings_description', $description, $campaign_settings );
451 }
452