PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.11.3
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.11.3
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 / shortcodes / class-charitable-campaigns-shortcode.php

class-charitable-campaigns-shortcode.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.11.3, at includes/shortcodes/class-charitable-campaigns-shortcode.php

244 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Campaigns shortcode class.
4 *
5 * @package Charitable/Shortcodes/Campaigns
6 * @author David Bisset
7 * @copyright Copyright (c) 2023, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.6.45
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Campaigns_Shortcode' ) ) :
19
20 /**
21 * Charitable_Campaigns_Shortcode class.
22 *
23 * @since 1.0.0
24 */
25 class Charitable_Campaigns_Shortcode {
26
27 /**
28 * Display the shortcode output. This is the callback method for the campaigns shortcode.
29 *
30 * @since 1.0.0
31 * @version 1.8.2 added 'description_limit' to the shortcode_atts and view_args filter.
32 *
33 * @param array $atts The user-defined shortcode attributes.
34 * @return string
35 */
36 public static function display( $atts ) {
37 $default = array(
38 'id' => '',
39 'parent_campaigns' => '', // only show child campaigns of the IDs provided.
40 'orderby' => 'post_date',
41 'order' => '',
42 'number' => get_option( 'posts_per_page' ),
43 'category' => '',
44 'tag' => '',
45 'creator' => '',
46 'exclude' => '',
47 'include_inactive' => false,
48 'columns' => 2,
49 'button' => 'donate',
50 'responsive' => 1,
51 'masonry' => 0,
52 'description_limit' => 100,
53 'shortcode' => 'campaigns',
54 );
55
56 $args = shortcode_atts( $default, $atts, 'campaigns' );
57
58 /**
59 * Modify the arguments passed to the campaigns shortcode.
60 *
61 * As an example, Ambassadors extension can add 'fundraiser_type' to the shortcode_atts which is used to filter campaigns by fundraiser type.
62 *
63 * @since 1.8.3
64 *
65 * @param array $args The arguments passed to the shortcode.
66 */
67 $args = apply_filters( 'charitable_campaigns_shortcode_atts', $args );
68
69 $args['campaigns'] = self::get_campaigns( $args );
70
71 /**
72 * Replace the default template with your own.
73 *
74 * If you replace the template with your own, it needs to be an instance of Charitable_Template.
75 *
76 * @since 1.0.0
77 *
78 * @param false|Charitable_Template The template. If false (the default), we will use our own template.
79 * @param array $args All the parsed arguments.
80 */
81 $template = apply_filters( 'charitable_campaigns_shortcode_template', false, $args );
82
83 /* Fall back to default Charitable_Template if no template returned or if template was not object of 'Charitable_Template' class. */
84 if ( ! is_object( $template ) || ! is_a( $template, 'Charitable_Template' ) ) {
85 $template = new Charitable_Template( 'campaign-loop.php', false );
86 }
87
88 if ( ! $template->template_file_exists() ) {
89 return false;
90 }
91
92 /**
93 * Modify the view arguments that are passed to the campaigns shortcode template.
94 *
95 * @since 1.0.0
96 *
97 * @param array $view_args The arguments to pass.
98 * @param array $args All the parsed arguments.
99 */
100 $view_args = apply_filters( 'charitable_campaigns_shortcode_view_args', charitable_array_subset( $args, array( 'campaigns', 'columns', 'button', 'responsive', 'masonry', 'description_limit', 'shortcode' ) ), $args );
101
102 $template->set_view_args( $view_args );
103
104 ob_start();
105
106 $template->render();
107
108 /**
109 * Customize the output of the shortcode.
110 *
111 * @since 1.0.0
112 *
113 * @param string $content The content to be displayed.
114 * @param array $args All the parsed arguments.
115 * @return string
116 */
117 return apply_filters( 'charitable_campaigns_shortcode', ob_get_clean(), $args );
118 }
119
120 /**
121 * Return campaigns to display in the campaigns shortcode.
122 *
123 * @since 1.0.0
124 * @since 1.8.4.7
125 *
126 * @param array $args The query arguments to be used to retrieve campaigns.
127 * @return WP_Query
128 */
129 public static function get_campaigns( $args ) {
130 $query_args = array(
131 'posts_per_page' => $args['number'],
132 );
133
134 /* Specific campaign IDs */
135 if ( ! empty( $args['id'] ) ) {
136 $query_args['post__in'] = explode( ',', $args['id'] );
137 }
138
139 /* Only show child campaigns of the IDs provided, added in 1.8.4.7 */
140 if ( ! empty( $args['parent_campaigns'] ) ) {
141 $query_args['post_parent__in'] = explode( ',', $args['parent_campaigns'] );
142 }
143
144 /* Parent IDs */
145
146 /* Pagination */
147 if ( ! empty( $args['paged'] ) ) {
148 $query_args['paged'] = $args['paged'];
149 }
150
151 /* Set category constraint */
152 if ( ! empty( $args['category'] ) ) {
153 $query_args['tax_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
154 array(
155 'taxonomy' => 'campaign_category',
156 'field' => 'slug',
157 'terms' => explode( ',', $args['category'] ),
158 ),
159 );
160 }
161
162 /* Set tag constraint */
163 if ( ! empty( $args['tag'] ) ) {
164 if ( ! array_key_exists( 'tax_query', $query_args ) ) {
165 $query_args['tax_query'] = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
166 }
167
168 $query_args['tax_query'][] = array(
169 'taxonomy' => 'campaign_tag',
170 'field' => 'slug',
171 'terms' => explode( ',', $args['tag'] ),
172 );
173 }
174
175 /* Set author constraint */
176 if ( ! empty( $args['creator'] ) ) {
177 $query_args['author'] = $args['creator'];
178 }
179
180 /* Only include active campaigns if flag is set */
181 if ( ! $args['include_inactive'] ) {
182 $query_args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
183 'relation' => 'OR',
184 array(
185 'key' => '_campaign_end_date',
186 'value' => date( 'Y-m-d H:i:s' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
187 'compare' => '>=',
188 'type' => 'datetime',
189 ),
190 array(
191 'key' => '_campaign_end_date',
192 'value' => 0,
193 'compare' => '=',
194 ),
195 );
196 }
197
198 if ( ! empty( $args['exclude'] ) ) {
199 $query_args['post__not_in'] = explode( ',', $args['exclude'] );
200 }
201
202 if ( ! empty( $args['order'] ) && in_array( strtoupper( $args['order'] ), array( 'DESC', 'ASC' ), true ) ) {
203 $query_args['order'] = $args['order'];
204 }
205
206 /* Return campaigns, ordered by date of creation. */
207 if ( 'post_date' === $args['orderby'] ) {
208 $query_args['orderby'] = 'date';
209
210 if ( ! isset( $query_args['order'] ) ) {
211 $query_args['order'] = 'DESC';
212 }
213 } elseif ( ! in_array( $args['orderby'], array( 'popular', 'ending' ), true ) ) {
214 $query_args['orderby'] = $args['orderby'];
215 }
216
217 /**
218 * Filter the campaign query args.
219 *
220 * @since 1.6.28
221 *
222 * @param array $query_args The arguments to be passed to WP_Query.
223 * @param array $args The shortcode args.
224 */
225 $query_args = apply_filters( 'charitable_campaigns_shortcode_query_args', $query_args, $args );
226
227 /**
228 * Finally, query based on the orderby argument.
229 */
230 switch ( $args['orderby'] ) {
231 case 'popular':
232 return Charitable_Campaigns::ordered_by_amount( $query_args );
233
234 case 'ending':
235 return Charitable_Campaigns::ordered_by_ending_soon( $query_args );
236
237 default:
238 return Charitable_Campaigns::query( $query_args );
239 }
240 }
241 }
242
243 endif;
244