← All changes
|
includes/shortcodes/class-charitable-campaigns-shortcode.php
+73
-35
1.6.9
→
1.8.13
View file →
| @@ -2,17 +2,19 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Campaigns shortcode class. |
| 4 | 4 | * |
| 5 | 5 | * @package Charitable/Shortcodes/Campaigns |
| 6 | - * @author Eric Daams | |
| 7 | - * @copyright Copyright (c) 2019, Studio 164a | |
| 6 | + * @author David Bisset | |
| 7 | + * @copyright Copyright (c) 2023, WP Charitable LLC | |
| 8 | 8 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 | 9 | * @since 1.0.0 |
| 10 | - * @version 1.5.7 | |
| 10 | + * @version 1.6.45 | |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | // Exit if accessed directly. |
| 14 | -if ( ! defined( 'ABSPATH' ) ) { exit; } | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit; | |
| 16 | +} | |
| 15 | 17 | |
| 16 | 18 | if ( ! class_exists( 'Charitable_Campaigns_Shortcode' ) ) : |
| 17 | 19 | |
| 18 | 20 | /** |
| @@ -25,8 +27,9 @@ | ||
| 25 | 27 | /** |
| 26 | 28 | * Display the shortcode output. This is the callback method for the campaigns shortcode. |
| 27 | 29 | * |
| 28 | 30 | * @since 1.0.0 |
| 31 | + * @version 1.8.2 added 'description_limit' to the shortcode_atts and view_args filter. | |
| 29 | 32 | * |
| 30 | 33 | * @param array $atts The user-defined shortcode attributes. |
| 31 | 34 | * @return string |
| 32 | 35 | */ |
| @@ -31,24 +34,39 @@ | ||
| 31 | 34 | * @return string |
| 32 | 35 | */ |
| 33 | 36 | public static function display( $atts ) { |
| 34 | 37 | $default = array( |
| 35 | - 'id' => '', | |
| 36 | - 'orderby' => 'post_date', | |
| 37 | - 'order' => '', | |
| 38 | - 'number' => get_option( 'posts_per_page' ), | |
| 39 | - 'category' => '', | |
| 40 | - 'tag' => '', | |
| 41 | - 'creator' => '', | |
| 42 | - 'exclude' => '', | |
| 43 | - 'include_inactive' => false, | |
| 44 | - 'columns' => 2, | |
| 45 | - 'button' => 'donate', | |
| 46 | - 'responsive' => 1, | |
| 47 | - 'masonry' => 0, | |
| 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', | |
| 48 | 54 | ); |
| 49 | 55 | |
| 50 | - $args = shortcode_atts( $default, $atts, 'campaigns' ); | |
| 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 | + | |
| 51 | 69 | $args['campaigns'] = self::get_campaigns( $args ); |
| 52 | 70 | |
| 53 | 71 | /** |
| 54 | 72 | * Replace the default template with your own. |
| @@ -78,9 +96,9 @@ | ||
| 78 | 96 | * |
| 79 | 97 | * @param array $view_args The arguments to pass. |
| 80 | 98 | * @param array $args All the parsed arguments. |
| 81 | 99 | */ |
| 82 | - $view_args = apply_filters( 'charitable_campaigns_shortcode_view_args', charitable_array_subset( $args, array( 'campaigns', 'columns', 'button', 'responsive', 'masonry' ) ), $args ); | |
| 100 | + $view_args = apply_filters( 'charitable_campaigns_shortcode_view_args', charitable_array_subset( $args, array( 'campaigns', 'columns', 'button', 'responsive', 'masonry', 'description_limit', 'shortcode' ) ), $args ); | |
| 83 | 101 | |
| 84 | 102 | $template->set_view_args( $view_args ); |
| 85 | 103 | |
| 86 | 104 | ob_start(); |
| @@ -102,8 +120,9 @@ | ||
| 102 | 120 | /** |
| 103 | 121 | * Return campaigns to display in the campaigns shortcode. |
| 104 | 122 | * |
| 105 | 123 | * @since 1.0.0 |
| 124 | + * @since 1.8.4.7 | |
| 106 | 125 | * |
| 107 | 126 | * @param array $args The query arguments to be used to retrieve campaigns. |
| 108 | 127 | * @return WP_Query |
| 109 | 128 | */ |
| @@ -116,8 +135,15 @@ | ||
| 116 | 135 | if ( ! empty( $args['id'] ) ) { |
| 117 | 136 | $query_args['post__in'] = explode( ',', $args['id'] ); |
| 118 | 137 | } |
| 119 | 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 | + | |
| 120 | 146 | /* Pagination */ |
| 121 | 147 | if ( ! empty( $args['paged'] ) ) { |
| 122 | 148 | $query_args['paged'] = $args['paged']; |
| 123 | 149 | } |
| @@ -123,9 +149,9 @@ | ||
| 123 | 149 | } |
| 124 | 150 | |
| 125 | 151 | /* Set category constraint */ |
| 126 | 152 | if ( ! empty( $args['category'] ) ) { |
| 127 | - $query_args['tax_query'] = array( | |
| 153 | + $query_args['tax_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query | |
| 128 | 154 | array( |
| 129 | 155 | 'taxonomy' => 'campaign_category', |
| 130 | 156 | 'field' => 'slug', |
| 131 | 157 | 'terms' => explode( ',', $args['category'] ), |
| @@ -135,9 +161,9 @@ | ||
| 135 | 161 | |
| 136 | 162 | /* Set tag constraint */ |
| 137 | 163 | if ( ! empty( $args['tag'] ) ) { |
| 138 | 164 | if ( ! array_key_exists( 'tax_query', $query_args ) ) { |
| 139 | - $query_args['tax_query'] = array(); | |
| 165 | + $query_args['tax_query'] = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query | |
| 140 | 166 | } |
| 141 | 167 | |
| 142 | 168 | $query_args['tax_query'][] = array( |
| 143 | 169 | 'taxonomy' => 'campaign_tag', |
| @@ -152,13 +178,13 @@ | ||
| 152 | 178 | } |
| 153 | 179 | |
| 154 | 180 | /* Only include active campaigns if flag is set */ |
| 155 | 181 | if ( ! $args['include_inactive'] ) { |
| 156 | - $query_args['meta_query'] = array( | |
| 182 | + $query_args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query | |
| 157 | 183 | 'relation' => 'OR', |
| 158 | 184 | array( |
| 159 | 185 | 'key' => '_campaign_end_date', |
| 160 | - 'value' => date( 'Y-m-d H:i:s' ), | |
| 186 | + 'value' => date( 'Y-m-d H:i:s' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date | |
| 161 | 187 | 'compare' => '>=', |
| 162 | 188 | 'type' => 'datetime', |
| 163 | 189 | ), |
| 164 | 190 | array( |
| @@ -176,18 +202,8 @@ | ||
| 176 | 202 | if ( ! empty( $args['order'] ) && in_array( strtoupper( $args['order'] ), array( 'DESC', 'ASC' ), true ) ) { |
| 177 | 203 | $query_args['order'] = $args['order']; |
| 178 | 204 | } |
| 179 | 205 | |
| 180 | - /* Return campaigns, ordered by how much money has been raised. */ | |
| 181 | - if ( 'popular' === $args['orderby'] ) { | |
| 182 | - return Charitable_Campaigns::ordered_by_amount( $query_args ); | |
| 183 | - } | |
| 184 | - | |
| 185 | - /* Return campaigns, ordered by how soon they are ending. */ | |
| 186 | - if ( 'ending' === $args['orderby'] ) { | |
| 187 | - return Charitable_Campaigns::ordered_by_ending_soon( $query_args ); | |
| 188 | - } | |
| 189 | - | |
| 190 | 206 | /* Return campaigns, ordered by date of creation. */ |
| 191 | 207 | if ( 'post_date' === $args['orderby'] ) { |
| 192 | 208 | $query_args['orderby'] = 'date'; |
| 193 | 209 | |
| @@ -193,13 +209,35 @@ | ||
| 193 | 209 | |
| 194 | 210 | if ( ! isset( $query_args['order'] ) ) { |
| 195 | 211 | $query_args['order'] = 'DESC'; |
| 196 | 212 | } |
| 197 | - } else { | |
| 213 | + } elseif ( ! in_array( $args['orderby'], array( 'popular', 'ending' ), true ) ) { | |
| 198 | 214 | $query_args['orderby'] = $args['orderby']; |
| 199 | 215 | } |
| 200 | 216 | |
| 201 | - return Charitable_Campaigns::query( $query_args ); | |
| 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 | + } | |
| 202 | 240 | } |
| 203 | 241 | } |
| 204 | 242 | |
| 205 | 243 | endif; |