| 1 |
<?php |
| 2 |
/** |
| 3 |
* Campaigns shortcode class. |
| 4 |
* |
| 5 |
* @package Charitable/Shortcodes/Campaigns |
| 6 |
* @author Eric Daams |
| 7 |
* @copyright Copyright (c) 2022, Studio 164a |
| 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 |
* |
| 32 |
* @param array $atts The user-defined shortcode attributes. |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
public static function display( $atts ) { |
| 36 |
$default = array( |
| 37 |
'id' => '', |
| 38 |
'orderby' => 'post_date', |
| 39 |
'order' => '', |
| 40 |
'number' => get_option( 'posts_per_page' ), |
| 41 |
'category' => '', |
| 42 |
'tag' => '', |
| 43 |
'creator' => '', |
| 44 |
'exclude' => '', |
| 45 |
'include_inactive' => false, |
| 46 |
'columns' => 2, |
| 47 |
'button' => 'donate', |
| 48 |
'responsive' => 1, |
| 49 |
'masonry' => 0, |
| 50 |
); |
| 51 |
|
| 52 |
$args = shortcode_atts( $default, $atts, 'campaigns' ); |
| 53 |
$args['campaigns'] = self::get_campaigns( $args ); |
| 54 |
|
| 55 |
/** |
| 56 |
* Replace the default template with your own. |
| 57 |
* |
| 58 |
* If you replace the template with your own, it needs to be an instance of Charitable_Template. |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
* |
| 62 |
* @param false|Charitable_Template The template. If false (the default), we will use our own template. |
| 63 |
* @param array $args All the parsed arguments. |
| 64 |
*/ |
| 65 |
$template = apply_filters( 'charitable_campaigns_shortcode_template', false, $args ); |
| 66 |
|
| 67 |
/* Fall back to default Charitable_Template if no template returned or if template was not object of 'Charitable_Template' class. */ |
| 68 |
if ( ! is_object( $template ) || ! is_a( $template, 'Charitable_Template' ) ) { |
| 69 |
$template = new Charitable_Template( 'campaign-loop.php', false ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! $template->template_file_exists() ) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Modify the view arguments that are passed to the campaigns shortcode template. |
| 78 |
* |
| 79 |
* @since 1.0.0 |
| 80 |
* |
| 81 |
* @param array $view_args The arguments to pass. |
| 82 |
* @param array $args All the parsed arguments. |
| 83 |
*/ |
| 84 |
$view_args = apply_filters( 'charitable_campaigns_shortcode_view_args', charitable_array_subset( $args, array( 'campaigns', 'columns', 'button', 'responsive', 'masonry' ) ), $args ); |
| 85 |
|
| 86 |
$template->set_view_args( $view_args ); |
| 87 |
|
| 88 |
ob_start(); |
| 89 |
|
| 90 |
$template->render(); |
| 91 |
|
| 92 |
/** |
| 93 |
* Customize the output of the shortcode. |
| 94 |
* |
| 95 |
* @since 1.0.0 |
| 96 |
* |
| 97 |
* @param string $content The content to be displayed. |
| 98 |
* @param array $args All the parsed arguments. |
| 99 |
* @return string |
| 100 |
*/ |
| 101 |
return apply_filters( 'charitable_campaigns_shortcode', ob_get_clean(), $args ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Return campaigns to display in the campaigns shortcode. |
| 106 |
* |
| 107 |
* @since 1.0.0 |
| 108 |
* |
| 109 |
* @param array $args The query arguments to be used to retrieve campaigns. |
| 110 |
* @return WP_Query |
| 111 |
*/ |
| 112 |
public static function get_campaigns( $args ) { |
| 113 |
$query_args = array( |
| 114 |
'posts_per_page' => $args['number'], |
| 115 |
); |
| 116 |
|
| 117 |
/* Specific campaign IDs */ |
| 118 |
if ( ! empty( $args['id'] ) ) { |
| 119 |
$query_args['post__in'] = explode( ',', $args['id'] ); |
| 120 |
} |
| 121 |
|
| 122 |
/* Pagination */ |
| 123 |
if ( ! empty( $args['paged'] ) ) { |
| 124 |
$query_args['paged'] = $args['paged']; |
| 125 |
} |
| 126 |
|
| 127 |
/* Set category constraint */ |
| 128 |
if ( ! empty( $args['category'] ) ) { |
| 129 |
$query_args['tax_query'] = array( |
| 130 |
array( |
| 131 |
'taxonomy' => 'campaign_category', |
| 132 |
'field' => 'slug', |
| 133 |
'terms' => explode( ',', $args['category'] ), |
| 134 |
), |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
/* Set tag constraint */ |
| 139 |
if ( ! empty( $args['tag'] ) ) { |
| 140 |
if ( ! array_key_exists( 'tax_query', $query_args ) ) { |
| 141 |
$query_args['tax_query'] = array(); |
| 142 |
} |
| 143 |
|
| 144 |
$query_args['tax_query'][] = array( |
| 145 |
'taxonomy' => 'campaign_tag', |
| 146 |
'field' => 'slug', |
| 147 |
'terms' => explode( ',', $args['tag'] ), |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
/* Set author constraint */ |
| 152 |
if ( ! empty( $args['creator'] ) ) { |
| 153 |
$query_args['author'] = $args['creator']; |
| 154 |
} |
| 155 |
|
| 156 |
/* Only include active campaigns if flag is set */ |
| 157 |
if ( ! $args['include_inactive'] ) { |
| 158 |
$query_args['meta_query'] = array( |
| 159 |
'relation' => 'OR', |
| 160 |
array( |
| 161 |
'key' => '_campaign_end_date', |
| 162 |
'value' => date( 'Y-m-d H:i:s' ), |
| 163 |
'compare' => '>=', |
| 164 |
'type' => 'datetime', |
| 165 |
), |
| 166 |
array( |
| 167 |
'key' => '_campaign_end_date', |
| 168 |
'value' => 0, |
| 169 |
'compare' => '=', |
| 170 |
), |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
if ( ! empty( $args['exclude'] ) ) { |
| 175 |
$query_args['post__not_in'] = explode( ',', $args['exclude'] ); |
| 176 |
} |
| 177 |
|
| 178 |
if ( ! empty( $args['order'] ) && in_array( strtoupper( $args['order'] ), array( 'DESC', 'ASC' ), true ) ) { |
| 179 |
$query_args['order'] = $args['order']; |
| 180 |
} |
| 181 |
|
| 182 |
/* Return campaigns, ordered by date of creation. */ |
| 183 |
if ( 'post_date' === $args['orderby'] ) { |
| 184 |
$query_args['orderby'] = 'date'; |
| 185 |
|
| 186 |
if ( ! isset( $query_args['order'] ) ) { |
| 187 |
$query_args['order'] = 'DESC'; |
| 188 |
} |
| 189 |
} elseif ( ! in_array( $args['orderby'], array( 'popular', 'ending' ), true ) ) { |
| 190 |
$query_args['orderby'] = $args['orderby']; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Filter the campaign query args. |
| 195 |
* |
| 196 |
* @since 1.6.28 |
| 197 |
* |
| 198 |
* @param array $query_args The arguments to be passed to WP_Query. |
| 199 |
* @param array $args The shortcode args. |
| 200 |
*/ |
| 201 |
$query_args = apply_filters( 'charitable_campaigns_shortcode_query_args', $query_args, $args ); |
| 202 |
|
| 203 |
/** |
| 204 |
* Finally, query based on the orderby argument. |
| 205 |
*/ |
| 206 |
switch ( $args['orderby'] ) { |
| 207 |
case 'popular': |
| 208 |
return Charitable_Campaigns::ordered_by_amount( $query_args ); |
| 209 |
|
| 210 |
case 'ending': |
| 211 |
return Charitable_Campaigns::ordered_by_ending_soon( $query_args ); |
| 212 |
|
| 213 |
default: |
| 214 |
return Charitable_Campaigns::query( $query_args ); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
endif; |
| 220 |
|