| 1 |
<?php |
| 2 |
/** |
| 3 |
* Campaigns shortcode class. |
| 4 |
* |
| 5 |
* @version 1.0.0 |
| 6 |
* @package Charitable/Shortcodes/Campaigns |
| 7 |
* @category Class |
| 8 |
* @author Eric Daams |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
if ( ! class_exists( 'Charitable_Campaigns_Shortcode' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* Charitable_Campaigns_Shortcode class. |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class Charitable_Campaigns_Shortcode { |
| 23 |
|
| 24 |
/** |
| 25 |
* Display the shortcode output. This is the callback method for the campaigns shortcode. |
| 26 |
* |
| 27 |
* @param array $atts The user-defined shortcode attributes. |
| 28 |
* @return string |
| 29 |
* @access public |
| 30 |
* @static |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
public static function display( $atts ) { |
| 34 |
$default = array( |
| 35 |
'id' => '', |
| 36 |
'orderby' => 'post_date', |
| 37 |
'order' => '', |
| 38 |
'number' => get_option( 'posts_per_page' ), |
| 39 |
'category' => '', |
| 40 |
'creator' => '', |
| 41 |
'exclude' => '', |
| 42 |
'include_inactive' => false, |
| 43 |
'columns' => 2, |
| 44 |
'button' => 'donate', |
| 45 |
'responsive' => 1, |
| 46 |
); |
| 47 |
|
| 48 |
$args = shortcode_atts( $default, $atts, 'campaigns' ); |
| 49 |
$args['campaigns'] = self::get_campaigns( $args ); |
| 50 |
|
| 51 |
/* Allows extensions/themes to plug in their own template objects here. */ |
| 52 |
$template = apply_filters( 'charitable_campaigns_shortcode_template', false, $args ); |
| 53 |
|
| 54 |
/* Fall back to default Charitable_Template if no template returned or if template was not object of 'Charitable_Template' class. */ |
| 55 |
if ( ! is_object( $template ) || ! is_a( $template, 'Charitable_Template' ) ) { |
| 56 |
$template = new Charitable_Template( 'campaign-loop.php', false ); |
| 57 |
} |
| 58 |
|
| 59 |
if ( ! $template->template_file_exists() ) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
$view_args = apply_filters( 'charitable_campaigns_shortcode_view_args', array( |
| 64 |
'campaigns' => $args['campaigns'], |
| 65 |
'columns' => $args['columns'], |
| 66 |
'button' => $args['button'], |
| 67 |
'responsive' => $args['responsive'], |
| 68 |
), $args ); |
| 69 |
|
| 70 |
$template->set_view_args( $view_args ); |
| 71 |
|
| 72 |
ob_start(); |
| 73 |
|
| 74 |
$template->render(); |
| 75 |
|
| 76 |
return apply_filters( 'charitable_campaigns_shortcode', ob_get_clean(), $args ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Return campaigns to display in the campaigns shortcode. |
| 81 |
* |
| 82 |
* @param array $args The query arguments to be used to retrieve campaigns. |
| 83 |
* @return WP_Query |
| 84 |
* @access public |
| 85 |
* @static |
| 86 |
* @since 1.0.0 |
| 87 |
*/ |
| 88 |
public static function get_campaigns( $args ) { |
| 89 |
$query_args = array( |
| 90 |
'posts_per_page' => $args['number'], |
| 91 |
); |
| 92 |
|
| 93 |
/* Specific campaign IDs */ |
| 94 |
if ( ! empty( $args['id'] ) ) { |
| 95 |
$query_args['post__in'] = explode( ',', $args['id'] ); |
| 96 |
} |
| 97 |
|
| 98 |
/* Pagination */ |
| 99 |
if ( ! empty( $args['paged'] ) ) { |
| 100 |
$query_args['paged'] = $args['paged']; |
| 101 |
} |
| 102 |
|
| 103 |
/* Set category constraint */ |
| 104 |
if ( ! empty( $args['category'] ) ) { |
| 105 |
$query_args['tax_query'] = array( |
| 106 |
array( |
| 107 |
'taxonomy' => 'campaign_category', |
| 108 |
'field' => 'slug', |
| 109 |
'terms' => $args['category'], |
| 110 |
), |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
/* Set author constraint */ |
| 115 |
if ( ! empty( $args['creator'] ) ) { |
| 116 |
$query_args['author'] = $args['creator']; |
| 117 |
} |
| 118 |
|
| 119 |
/* Only include active campaigns if flag is set */ |
| 120 |
if ( ! $args['include_inactive'] ) { |
| 121 |
$query_args['meta_query'] = array( |
| 122 |
'relation' => 'OR', |
| 123 |
array( |
| 124 |
'key' => '_campaign_end_date', |
| 125 |
'value' => date( 'Y-m-d H:i:s' ), |
| 126 |
'compare' => '>=', |
| 127 |
'type' => 'datetime', |
| 128 |
), |
| 129 |
array( |
| 130 |
'key' => '_campaign_end_date', |
| 131 |
'value' => 0, |
| 132 |
'compare' => '=', |
| 133 |
), |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
if ( ! empty( $args['exclude'] ) ) { |
| 138 |
$query_args['post__not_in'] = explode( ',', $args['exclude'] ); |
| 139 |
} |
| 140 |
|
| 141 |
// echo '<pre>'; var_dump( $query_args ); echo '</pre>'; |
| 142 |
// die; |
| 143 |
|
| 144 |
if ( ! empty( $args['order'] ) && in_array( strtoupper( $args['order'] ), array( 'DESC', 'ASC' ), true ) ) { |
| 145 |
$query_args['order'] = $args['order']; |
| 146 |
} |
| 147 |
|
| 148 |
/* Return campaigns, ordered by how much money has been raised. */ |
| 149 |
if ( 'popular' === $args['orderby'] ) { |
| 150 |
return Charitable_Campaigns::ordered_by_amount( $query_args ); |
| 151 |
} |
| 152 |
|
| 153 |
/* Return campaigns, ordered by how soon they are ending. */ |
| 154 |
if ( 'ending' === $args['orderby'] ) { |
| 155 |
return Charitable_Campaigns::ordered_by_ending_soon( $query_args ); |
| 156 |
} |
| 157 |
|
| 158 |
/* Return campaigns, ordered by date of creation. */ |
| 159 |
if ( 'post_date' === $args['orderby'] ) { |
| 160 |
$query_args['orderby'] = 'date'; |
| 161 |
|
| 162 |
if ( ! isset( $query_args['order'] ) ) { |
| 163 |
$query_args['order'] = 'DESC'; |
| 164 |
} |
| 165 |
} else { |
| 166 |
$query_args['orderby'] = $args['orderby']; |
| 167 |
} |
| 168 |
|
| 169 |
return Charitable_Campaigns::query( $query_args ); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
endif; |
| 174 |
|