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