| 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) 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.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 whether the current user is the creator of the given campaign. |
| 70 |
* |
| 71 |
* @since 1.0.0 |
| 72 |
* |
| 73 |
* @param int $campaign_id The campaign ID. |
| 74 |
* @return boolean |
| 75 |
*/ |
| 76 |
function charitable_is_current_campaign_creator( $campaign_id = null ) { |
| 77 |
if ( is_null( $campaign_id ) ) { |
| 78 |
$campaign_id = charitable_get_current_campaign_id(); |
| 79 |
} |
| 80 |
|
| 81 |
return get_post_field( 'post_author', $campaign_id ) == get_current_user_id(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns whether the given campaign can receive donations. |
| 86 |
* |
| 87 |
* @since 1.5.14 |
| 88 |
* |
| 89 |
* @param int $campaign_id The campaign ID. |
| 90 |
* @return boolean |
| 91 |
*/ |
| 92 |
function charitable_campaign_can_receive_donations( $campaign_id ) { |
| 93 |
if ( Charitable::CAMPAIGN_POST_TYPE !== get_post_type( $campaign_id ) ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
$campaign = charitable_get_campaign( $campaign_id ); |
| 98 |
|
| 99 |
return $campaign && $campaign->can_receive_donations(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Given a campaign ID and a key, return the post field. |
| 104 |
* |
| 105 |
* @since 1.6.0 |
| 106 |
* |
| 107 |
* @param Charitable_Campaign $campaign The campaign object. |
| 108 |
* @param string $key The meta key. |
| 109 |
* @return mixed |
| 110 |
*/ |
| 111 |
function charitable_get_campaign_post_field( Charitable_Campaign $campaign, $key ) { |
| 112 |
return get_post_field( $key, $campaign->ID ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get the campaign date as a timestamp. |
| 117 |
* |
| 118 |
* @since 1.6.54 |
| 119 |
* |
| 120 |
* @param Charitable_Campaign $campaign The campaign instance. |
| 121 |
* @return int |
| 122 |
*/ |
| 123 |
function charitable_get_campaign_date_as_timestamp( Charitable_Campaign $campaign ) { |
| 124 |
return strtotime( $campaign->post_date ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get a particular user field for a campaign's creator. |
| 129 |
* |
| 130 |
* @since 1.6.5 |
| 131 |
* |
| 132 |
* @param Charitable_Campaign $campaign The campaign object. |
| 133 |
* @param string $key The meta key. |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
function charitable_get_campaign_creator_field( Charitable_Campaign $campaign, $key ) { |
| 137 |
$creator = charitable_get_user( $campaign->get_campaign_creator() ); |
| 138 |
$key = str_replace( 'campaign_creator_', '', $key ); |
| 139 |
|
| 140 |
return $creator->get( $key ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get a particular taxonomy field for a campaign. |
| 145 |
* |
| 146 |
* @since 1.6.19 |
| 147 |
* |
| 148 |
* @param Charitable_Campaign $campaign The campaign object. |
| 149 |
* @param string $key The meta key. |
| 150 |
* @return string |
| 151 |
*/ |
| 152 |
function charitable_get_campaign_taxonomy_terms_list( Charitable_Campaign $campaign, $key ) { |
| 153 |
$taxonomies = array( |
| 154 |
'categories' => 'campaign_category', |
| 155 |
'tags' => 'campaign_tag', |
| 156 |
); |
| 157 |
|
| 158 |
$taxonomy = array_key_exists( $key, $taxonomies ) ? $taxonomies[ $key ] : $key; |
| 159 |
$terms = wp_get_object_terms( $campaign->ID, $taxonomy, array( 'fields' => 'names' ) ); |
| 160 |
|
| 161 |
if ( is_wp_error( $terms ) ) { |
| 162 |
return ''; |
| 163 |
} |
| 164 |
|
| 165 |
return implode( ', ', $terms ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Get the featured image for a particular campaign. |
| 170 |
* |
| 171 |
* @since 1.6.25 |
| 172 |
* |
| 173 |
* @param Charitable_Campaign $campaign The campaign object. |
| 174 |
* @return string|int |
| 175 |
*/ |
| 176 |
function charitable_get_campaign_featured_image( Charitable_Campaign $campaign ) { |
| 177 |
return get_post_thumbnail_id( $campaign->ID ); |
| 178 |
} |
| 179 |
|