| 1 |
<?php |
| 2 |
/** |
| 3 |
* Donors shortcode class. |
| 4 |
* |
| 5 |
* @package Charitable/Shortcodes/Donors |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2022, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.5.0 |
| 10 |
* @version 1.5.7 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Donors_Shortcode' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Donors_Shortcode class. |
| 22 |
* |
| 23 |
* @since 1.5.0 |
| 24 |
*/ |
| 25 |
class Charitable_Donors_Shortcode { |
| 26 |
|
| 27 |
/** |
| 28 |
* Display the shortcode output. This is the callback method for the donors shortcode. |
| 29 |
* |
| 30 |
* @since 1.5.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 |
'number' => 10, |
| 38 |
'orderby' => 'date', |
| 39 |
'order' => 'DESC', |
| 40 |
'campaign' => 0, |
| 41 |
'distinct_donors' => 0, |
| 42 |
'orientation' => 'horizontal', |
| 43 |
'show_name' => 1, |
| 44 |
'show_location' => 0, |
| 45 |
'show_amount' => 1, |
| 46 |
'show_avatar' => 1, |
| 47 |
'hide_if_no_donors' => 0, |
| 48 |
); |
| 49 |
|
| 50 |
$args = shortcode_atts( $default, $atts, 'charitable_donors' ); |
| 51 |
$args['donors'] = self::get_donors( $args ); |
| 52 |
|
| 53 |
/** |
| 54 |
* Replace the default template with your own. |
| 55 |
* |
| 56 |
* If you replace the template with your own, it needs to be an instance of Charitable_Template. |
| 57 |
* |
| 58 |
* @since 1.5.0 |
| 59 |
* |
| 60 |
* @param false|Charitable_Template The template. If false (the default), we will use our own template. |
| 61 |
* @param array $args All the parsed arguments. |
| 62 |
* @return false|Charitable_Template |
| 63 |
*/ |
| 64 |
$template = apply_filters( 'charitable_donors_shortcode_template', false, $args ); |
| 65 |
|
| 66 |
/* Fall back to default Charitable_Template if no template returned or if template was not object of 'Charitable_Template' class. */ |
| 67 |
if ( ! is_object( $template ) || ! is_a( $template, 'Charitable_Template' ) ) { |
| 68 |
$template = new Charitable_Template( 'donor-loop.php', false ); |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! $template->template_file_exists() ) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Modify the view arguments that are passed to the donors template. |
| 77 |
* |
| 78 |
* @since 1.5.0 |
| 79 |
* |
| 80 |
* @param array $view_args The arguments to pass. |
| 81 |
* @param array $args All the parsed arguments. |
| 82 |
* @return array |
| 83 |
*/ |
| 84 |
$view_args = apply_filters( |
| 85 |
'charitable_donors_shortcode_view_args', |
| 86 |
charitable_array_subset( |
| 87 |
$args, |
| 88 |
array( |
| 89 |
'donors', |
| 90 |
'number', |
| 91 |
'orderby', |
| 92 |
'order', |
| 93 |
'campaign', |
| 94 |
'orientation', |
| 95 |
'distinct_donors', |
| 96 |
'show_name', |
| 97 |
'show_location', |
| 98 |
'show_amount', |
| 99 |
'show_avatar', |
| 100 |
'hide_if_no_donors', |
| 101 |
) |
| 102 |
), |
| 103 |
$args |
| 104 |
); |
| 105 |
|
| 106 |
$template->set_view_args( $view_args ); |
| 107 |
|
| 108 |
ob_start(); |
| 109 |
|
| 110 |
$template->render(); |
| 111 |
|
| 112 |
/** |
| 113 |
* Customize the output of the shortcode. |
| 114 |
* |
| 115 |
* @since 1.5.0 |
| 116 |
* |
| 117 |
* @param string $content The content to be displayed. |
| 118 |
* @param array $args All the parsed arguments. |
| 119 |
* @return string |
| 120 |
*/ |
| 121 |
return apply_filters( 'charitable_donors_shortcode', ob_get_clean(), $args ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Return donors to display in the donors shortcode. |
| 126 |
* |
| 127 |
* @since 1.5.0 |
| 128 |
* |
| 129 |
* @param array $args The query arguments to be used to retrieve donors. |
| 130 |
* @return Charitable_Donor_Query |
| 131 |
*/ |
| 132 |
public static function get_donors( $args ) { |
| 133 |
/** |
| 134 |
* Filter the arguments passed to Charitable_Donor_Query. |
| 135 |
* |
| 136 |
* @since 1.5.0 |
| 137 |
* |
| 138 |
* @param array $query_args The arguments to be passed to Charitable_Donor_Query::__construct. |
| 139 |
* @param array $args All the parsed arguments. |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
$query_args = apply_filters( 'charitable_donors_shortcode_donor_query_args', |
| 143 |
charitable_array_subset( $args, array( 'number', 'orderby', 'order', 'campaign', 'distinct_donors' ) ), |
| 144 |
$args |
| 145 |
); |
| 146 |
|
| 147 |
return new Charitable_Donor_Query( $query_args ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
endif; |
| 152 |
|