← All changes
|
includes/campaigns/charitable-campaign-functions.php
+325
-3
1.6.7
→
1.8.13
View file →
| @@ -4,13 +4,13 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * Campaign related functions. |
| 6 | 6 | * |
| 7 | 7 | * @package Charitable/Functions/Campaign |
| 8 | - * @author Eric Daams | |
| 9 | - * @copyright Copyright (c) 2018, Studio 164a | |
| 8 | + * @author David Bisset | |
| 9 | + * @copyright Copyright (c) 2023, WP Charitable LLC | |
| 10 | 10 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 11 | 11 | * @since 1.0.0 |
| 12 | - * @version 1.5.14 | |
| 12 | + * @version 1.6.54 | |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | 15 | // Exit if accessed directly. |
| 16 | 16 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -65,8 +65,41 @@ | ||
| 65 | 65 | return charitable_get_request()->get_current_campaign_id(); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 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 | +/** | |
| 69 | 102 | * Returns whether the current user is the creator of the given campaign. |
| 70 | 103 | * |
| 71 | 104 | * @since 1.0.0 |
| 72 | 105 | * |
| @@ -108,12 +141,28 @@ | ||
| 108 | 141 | * @param string $key The meta key. |
| 109 | 142 | * @return mixed |
| 110 | 143 | */ |
| 111 | 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 | + | |
| 112 | 149 | return get_post_field( $key, $campaign->ID ); |
| 113 | 150 | } |
| 114 | 151 | |
| 115 | 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 | +/** | |
| 116 | 165 | * Get a particular user field for a campaign's creator. |
| 117 | 166 | * |
| 118 | 167 | * @since 1.6.5 |
| 119 | 168 | * |
| @@ -125,5 +174,278 @@ | ||
| 125 | 174 | $creator = charitable_get_user( $campaign->get_campaign_creator() ); |
| 126 | 175 | $key = str_replace( 'campaign_creator_', '', $key ); |
| 127 | 176 | |
| 128 | 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 ); | |
| 129 | 451 | } |