Passed
Push — master ( 0b0376...69634b )
by Eric
13:11 queued 11s
created

charitable_sanitize_campaign_args()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 21
rs 9.2222
1
<?php
2
/**
3
 * Charitable Campaign Functions.
4
 *
5
 * Campaign related functions.
6
 *
7
 * @package   Charitable/Functions/Campaign
8
 * @author    Eric Daams
9
 * @copyright Copyright (c) 2021, Studio 164a
10
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU Public License
11
 * @since     1.0.0
12
 * @version   1.5.14
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.7.0
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 a particular user field for a campaign's creator.
117
 *
118
 * @since  1.6.5
119
 *
120
 * @param  Charitable_Campaign $campaign The campaign object.
121
 * @param  string              $key      The meta key.
122
 * @return string
123
 */
124
function charitable_get_campaign_creator_field( Charitable_Campaign $campaign, $key ) {
125
	$creator = charitable_get_user( $campaign->get_campaign_creator() );
126
	$key     = str_replace( 'campaign_creator_', '', $key );
127
128
	return $creator->get( $key );
129
}
130
131
/**
132
 * Get a particular taxonomy field for a campaign.
133
 *
134
 * @since  1.6.19
135
 *
136
 * @param  Charitable_Campaign $campaign The campaign object.
137
 * @param  string              $key      The meta key.
138
 * @return string
139
 */
140
function charitable_get_campaign_taxonomy_terms_list( Charitable_Campaign $campaign, $key ) {
141
	$taxonomies = array(
142
		'categories' => 'campaign_category',
143
		'tags'       => 'campaign_tag',
144
	);
145
146
	$taxonomy = array_key_exists( $key, $taxonomies ) ? $taxonomies[ $key ] : $key;
147
	$terms    = wp_get_object_terms( $campaign->ID, $taxonomy, array( 'fields' => 'names' ) );
148
149
	if ( is_wp_error( $terms ) ) {
150
		return '';
151
	}
152
153
	return implode( ', ', $terms );
154
}
155
156
/**
157
 * Get the featured image for a particular campaign.
158
 *
159
 * @since  1.6.25
160
 *
161
 * @param  Charitable_Campaign $campaign The campaign object.
162
 * @return string|int
163
 */
164
function charitable_get_campaign_featured_image( Charitable_Campaign $campaign ) {
165
	return get_post_thumbnail_id( $campaign->ID );
0 ignored issues
show
Bug Best Practice introduced by
The expression return get_post_thumbnail_id($campaign->ID) could also return false which is incompatible with the documented return type integer|string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
166
}
167
168
/**
169
 * Sanitize a campaign argument.
170
 *
171
 * @since  1.7.0
172
 *
173
 * @param mixed $campaign The array of campaigns in string or int form
174
 * @return int/int[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment int/int[] at position 0 could not be parsed: Unknown type name 'int/int' at position 0 in int/int[].
Loading history...
175
 */
176
function charitable_sanitize_campaign_args( $campaign ) {
177
	if ( is_array( $campaign ) ) {
178
		$campaigns = [];
179
180
		foreach( $campaign as $single_campaign ) {
181
			$campaigns[] = (int) charitable_sanitize_campaign_args( $single_campaign );
182
		}
183
184
		return $campaigns;
185
	}
186
187
	switch ( $campaign ) {
188
		case '':
189
		case 'all':
190
			return 0;
191
192
		case 'current':
193
			return charitable_get_current_campaign_id();
194
195
		default:
196
			return $campaign;
197
	}
198
}
199