| 1 |
<?php |
| 2 |
/** |
| 3 |
* Campaign donors widget class. |
| 4 |
* |
| 5 |
* @version 1.0.0 |
| 6 |
* @package Charitable/Widgets/Donors |
| 7 |
* @author David Bisset |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
if ( ! class_exists( 'Charitable_Donors_Widget' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* Charitable_Donors_Widget class. |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class Charitable_Donors_Widget extends WP_Widget { |
| 23 |
|
| 24 |
/** |
| 25 |
* Instantiate the widget and set up basic configuration. |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
parent::__construct( |
| 31 |
'charitable_donors_widget', |
| 32 |
__( 'Donors', 'charitable' ), |
| 33 |
array( |
| 34 |
'description' => __( 'Display a list of donors.', 'charitable' ), |
| 35 |
'customize_selective_refresh' => true, |
| 36 |
) |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Display the widget contents on the front-end. |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
* |
| 45 |
* @param array $args Display arguments including 'before_title', 'after_title', 'before_widget' and 'after_widget'. |
| 46 |
* @param array $instance The settings for the particular instance of the widget. |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public function widget( $args, $instance ) { |
| 50 |
$instance = $this->get_parsed_args( $instance ); |
| 51 |
$view_args = array_merge( $args, $instance ); |
| 52 |
$instance['campaign'] = $view_args['campaign_id']; |
| 53 |
$view_args['campaign'] = $view_args['campaign_id']; |
| 54 |
$view_args['orderby'] = $view_args['order']; |
| 55 |
$view_args['order'] = 'DESC'; |
| 56 |
$view_args['donors'] = $this->get_widget_donors( $instance ); |
| 57 |
|
| 58 |
charitable_template( 'widgets/donors.php', $view_args ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Display the widget form in the admin. |
| 63 |
* |
| 64 |
* @since 1.0.0 |
| 65 |
* |
| 66 |
* @param array $instance The current settings for the widget options. |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function form( $instance ) { |
| 70 |
$args = $this->get_parsed_args( $instance ); |
| 71 |
$campaigns = Charitable_Campaigns::query( array( 'posts_per_page' => -1 ) ); |
| 72 |
?> |
| 73 |
<p> |
| 74 |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title', 'charitable' ); ?>:</label> |
| 75 |
<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" value="<?php echo esc_attr( $args['title'] ); ?>" class="widefat" /> |
| 76 |
</p> |
| 77 |
<p> |
| 78 |
<label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e( 'Number of donors to display', 'charitable' ); ?>:</label> |
| 79 |
<input type="number" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" value="<?php echo intval( $args['number'] ); ?>" min="1" size="3" /> |
| 80 |
</p> |
| 81 |
<p> |
| 82 |
<label for="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>"><?php esc_html_e( 'Order by', 'charitable' ); ?>:</label> |
| 83 |
<select name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>"> |
| 84 |
<option value="recent" <?php selected( 'recent', $args['order'] ); ?>><?php esc_html_e( 'Most recent', 'charitable' ); ?></option> |
| 85 |
<option value="amount" <?php selected( 'amount', $args['order'] ); ?>><?php esc_html_e( 'Amount donated', 'charitable' ); ?></option> |
| 86 |
</select> |
| 87 |
</p> |
| 88 |
<p> |
| 89 |
<label for="<?php echo esc_attr( $this->get_field_id( 'campaign_id' ) ); ?>"><?php esc_html_e( 'Show donors by campaign', 'charitable' ); ?>:</label> |
| 90 |
<select name="<?php echo esc_attr( $this->get_field_name( 'campaign_id' ) ); ?>"> |
| 91 |
<option value="all" <?php selected( 'all', $args['campaign_id'] ); ?>><?php esc_html_e( 'Include all campaigns', 'charitable' ); ?></option> |
| 92 |
<option value="current" <?php selected( 'current', $args['campaign_id'] ); ?>><?php esc_html_e( 'Campaign currently viewed', 'charitable' ); ?></option> |
| 93 |
<optgroup label="<?php esc_html_e( 'Specific campaign', 'charitable' ); ?>"> |
| 94 |
<?php foreach ( $campaigns->posts as $campaign ) : ?> |
| 95 |
<option value="<?php echo intval( $campaign->ID ); ?>" <?php selected( $campaign->ID, $args['campaign_id'] ); ?>><?php echo esc_html( $campaign->post_title ); ?></option> |
| 96 |
<?php endforeach ?> |
| 97 |
</optgroup> |
| 98 |
</select> |
| 99 |
</p> |
| 100 |
<p> |
| 101 |
<input id="<?php echo esc_attr( $this->get_field_id( 'show_distinct' ) ); ?>" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_distinct' ) ); ?>" <?php checked( $args['show_distinct'] ); ?>> |
| 102 |
<label for="<?php echo esc_attr( $this->get_field_id( 'show_distinct' ) ); ?>"><?php esc_html_e( 'Group donations by the same person', 'charitable' ); ?></label> |
| 103 |
</p> |
| 104 |
<p> |
| 105 |
<input id="<?php echo esc_attr( $this->get_field_id( 'show_avatar' ) ); ?>" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_avatar' ) ); ?>" <?php checked( $args['show_avatar'] ); ?>> |
| 106 |
<label for="<?php echo esc_attr( $this->get_field_id( 'show_avatar' ) ); ?>"><?php esc_html_e( 'Show donor\'s avatar', 'charitable' ); ?></label> |
| 107 |
</p> |
| 108 |
<p> |
| 109 |
<input id="<?php echo esc_attr( $this->get_field_id( 'show_name' ) ); ?>" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_name' ) ); ?>" <?php checked( $args['show_name'] ); ?>> |
| 110 |
<label for="<?php echo esc_attr( $this->get_field_id( 'show_name' ) ); ?>"><?php esc_html_e( 'Show donor\'s name', 'charitable' ); ?></label> |
| 111 |
</p> |
| 112 |
<p> |
| 113 |
<input id="<?php echo esc_attr( $this->get_field_id( 'show_amount' ) ); ?>" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_amount' ) ); ?>" <?php checked( $args['show_amount'] ); ?>> |
| 114 |
<label for="<?php echo esc_attr( $this->get_field_id( 'show_amount' ) ); ?>"><?php esc_html_e( 'Show donor\'s donation amount', 'charitable' ); ?></label> |
| 115 |
</p> |
| 116 |
<p> |
| 117 |
<input id="<?php echo esc_attr( $this->get_field_id( 'show_location' ) ); ?>" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_location' ) ); ?>" <?php checked( $args['show_location'] ); ?>> |
| 118 |
<label for="<?php echo esc_attr( $this->get_field_id( 'show_location' ) ); ?>"><?php esc_html_e( 'Show donor\'s location', 'charitable' ); ?></label> |
| 119 |
</p> |
| 120 |
<p> |
| 121 |
<input id="<?php echo esc_attr( $this->get_field_id( 'hide_if_no_donors' ) ); ?>" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'hide_if_no_donors' ) ); ?>" <?php checked( $args['hide_if_no_donors'] ); ?>> |
| 122 |
<label for="<?php echo esc_attr( $this->get_field_id( 'hide_if_no_donors' ) ); ?>"><?php esc_html_e( 'Hide if there are no donors', 'charitable' ); ?></label> |
| 123 |
</p> |
| 124 |
<?php |
| 125 |
/** |
| 126 |
* Add extra settings for the widget. |
| 127 |
* |
| 128 |
* @param array $args Widget instance arguments. |
| 129 |
* @param Charitable_Donors_Widget $this This widget instance. |
| 130 |
*/ |
| 131 |
do_action( 'charitable_donor_widget_settings_bottom', $args, $this ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Update the widget settings in the admin. |
| 136 |
* |
| 137 |
* @since 1.0.0 |
| 138 |
* @version 1.8.5.3 |
| 139 |
* |
| 140 |
* @param array $new_instance The updated settings. |
| 141 |
* @param array $old_instance The old settings. |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
public function update( $new_instance, $old_instance ) { |
| 145 |
$instance = $new_instance; |
| 146 |
|
| 147 |
foreach ( array( 'show_distinct', 'show_avatar', 'show_location', 'show_amount', 'show_name', 'hide_if_no_donors' ) as $key ) { |
| 148 |
$instance[ $key ] = array_key_exists( $key, $instance ) |
| 149 |
? charitable_sanitize_checkbox( $instance[ $key ] ) |
| 150 |
: 0; |
| 151 |
} |
| 152 |
|
| 153 |
$instance['title'] = sanitize_text_field( $instance['title'] ); |
| 154 |
$instance['campaign_id'] = sanitize_text_field( $instance['campaign_id'] ); |
| 155 |
$instance['order'] = sanitize_text_field( $instance['order'] ); |
| 156 |
|
| 157 |
/** |
| 158 |
* Filter the instance arguments. |
| 159 |
* |
| 160 |
* @since 1.0.0 |
| 161 |
* |
| 162 |
* @param array $instance The parsed instance settings. |
| 163 |
* @param array $new_instance The updated settings. |
| 164 |
* @param array $old_instance The old settings. |
| 165 |
*/ |
| 166 |
return apply_filters( 'charitable_donors_widget_update_instance', $instance, $new_instance, $old_instance ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Return parsed array of arguments. |
| 171 |
* |
| 172 |
* @since 1.0.0 |
| 173 |
* |
| 174 |
* @param array $instance The settings for the particular instance of the widget. |
| 175 |
* @return mixed[] |
| 176 |
*/ |
| 177 |
protected function get_parsed_args( $instance ) { |
| 178 |
/** |
| 179 |
* Filter the default widget arguments. |
| 180 |
* |
| 181 |
* @since 1.0.0 |
| 182 |
* |
| 183 |
* @param array $args The default arguments. |
| 184 |
* @param array $instance The widget instance settings. |
| 185 |
*/ |
| 186 |
$defaults = apply_filters( |
| 187 |
'charitable_donors_widget_default_args', |
| 188 |
array( |
| 189 |
'title' => '', |
| 190 |
'number' => 10, |
| 191 |
'order' => 'recent', |
| 192 |
'campaign_id' => 'all', |
| 193 |
'show_distinct' => 1, |
| 194 |
'show_avatar' => 1, |
| 195 |
'show_location' => 0, |
| 196 |
'show_amount' => 0, |
| 197 |
'show_name' => 0, |
| 198 |
'hide_if_no_donors' => 0, |
| 199 |
), |
| 200 |
$instance |
| 201 |
); |
| 202 |
|
| 203 |
return wp_parse_args( $instance, $defaults ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Return the donors to display in the widget. |
| 208 |
* |
| 209 |
* @since 1.0.0 |
| 210 |
* |
| 211 |
* @param mixed[] $instance The widget instance. |
| 212 |
* @return Charitable_Donor_Query |
| 213 |
*/ |
| 214 |
protected function get_widget_donors( $instance ) { |
| 215 |
$query_args = charitable_array_subset( $instance, array( 'number', 'campaign' ) ); |
| 216 |
|
| 217 |
if ( 'amount' == $instance['order'] ) { |
| 218 |
$query_args['orderby'] = 'amount'; |
| 219 |
} |
| 220 |
|
| 221 |
$query_args['distinct_donors'] = $instance['show_distinct']; |
| 222 |
|
| 223 |
/** |
| 224 |
* Filter the arguments passed to Charitable_Donor_Query. |
| 225 |
* |
| 226 |
* @since 1.0.0 |
| 227 |
* |
| 228 |
* @param array $query_args The arguments to be passed to Charitable_Donor_Query::__construct. |
| 229 |
* @param array $args All the parsed arguments. |
| 230 |
*/ |
| 231 |
$query_args = apply_filters( 'charitable_donors_widget_donor_query_args', $query_args, $instance ); |
| 232 |
|
| 233 |
return new Charitable_Donor_Query( $query_args ); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
endif; |
| 238 |
|