Skip to content

Commit

Permalink
refactor(donor-wall): adjustments made for issue #3312
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinWalker committed Jun 1, 2018
1 parent eda8fae commit 9f5e74a
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 140 deletions.
1 change: 1 addition & 0 deletions give.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ private function includes() {
require_once GIVE_PLUGIN_DIR . 'includes/class-give-db-form-meta.php';
require_once GIVE_PLUGIN_DIR . 'includes/class-give-db-sequential-ordering.php';
require_once GIVE_PLUGIN_DIR . 'includes/class-give-donor.php';
require_once GIVE_PLUGIN_DIR . 'includes/class-give-donor-grid.php';
require_once GIVE_PLUGIN_DIR . 'includes/class-give-donor-wall-widget.php';
require_once GIVE_PLUGIN_DIR . 'includes/class-give-stats.php';
require_once GIVE_PLUGIN_DIR . 'includes/class-give-session.php';
Expand Down
193 changes: 193 additions & 0 deletions includes/class-give-donor-grid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php
/**
* Donors Gravatars
*
* @package Give
* @subpackage Classes/Give_Donors_Gravatars
* @copyright Copyright (c) 2016, WordImpress
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @since 1.0
*/

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Give_Donor_Wall Class
*
* This class handles donors.
*
* @since 2.1
*/
class Give_Donor_Wall {

/**
* Class Constructor
*
* Set up the Give Donors Gravatars Class.
*
* @since 2.1
* @access public
*/
public function __construct() {
$this->setup_actions();
}

/**
* Setup the default hooks and actions
*
* @since 2.1
*
* @return void
*/
public function setup_actions() {

add_shortcode( 'give_donor_grid', array( $this, 'donor_grid_shortcode' ) );

}


/**
* Displays donors in a grid layout.
*
* @since 2.1.0
*
* @param array $atts {
* Optional. Attributes of the form grid shortcode.
*
* @type int $donors_per_page Number of donors per page. Default '20'.
* @type int $form_id The donation form to filter donors by. Default is all forms (no filter).
* @type bool $paged Whether to paginate donors. Default 'true'.
* @type string $ids A comma-separated list of donor IDs to display. Default empty.
* @type string $columns Maximum columns to display. Default 'best-fit'.
* Accepts 'best-fit', '1', '2', '3', '4'.
* @type bool $show_avatar Whether to display the donor's gravatar image if available. Default 'true'.
* @type bool $show_name Whether to display the donor's full name, first and last. Default 'true'.
* @type bool $show_total Whether to display the donor's donation amount. Default 'true'.
* @type bool $show_time Whether to display date of the last donation. Default 'true'.
* @type bool $show_comments Whether to display the donor's comment if they left one. Default 'true'.
* @type int $comment_length The number of words to display for the comments before a "Read more" field
* displays. Default '20'.
* @type int $avatar_size Avatar image size in pixels without the "px". Default "60"
* @type string $orderby The order in which you want the donors to appear. Accepts "donation_amount",
* "donation_count", "". Default "donation_count".
* }
* @return string|bool The markup of the form grid or false.
*/
public function donor_grid_shortcode( $atts ) {

$give_settings = give_get_settings();

$atts = shortcode_atts(
array(
'donors_per_page' => 20,
'form_id' => 0,
'paged' => true,
'ids' => '',
'columns' => 'best-fit',
'show_avatar' => true,
'show_name' => true,
'show_total' => true,
'show_time' => true,
'show_comments' => true,
'comment_length' => 20,
'readmore_text' => esc_html__( 'Read More', 'give' ),
'avatar_size' => 60,
'orderby' => 'donation_count',
'order' => 'DESC',
'hide_empty' => true,
), $atts
);

// Validate integer attributes.
$atts['donors_per_page'] = intval( $atts['donors_per_page'] );

// Validate boolean attributes.
$boolean_attributes = array(
'paged',
'show_avatar',
'show_name',
'show_total',
'show_time',
'hide_empty',
);

foreach ( $boolean_attributes as $att ) {
$atts[ $att ] = filter_var( $atts[ $att ], FILTER_VALIDATE_BOOLEAN );
}

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

// Set default form query args.
$donor_args = array(
'number' => $atts['donors_per_page'],
'offset' => $atts['donors_per_page'] * ( $paged - 1 ),
'orderby' => $atts['orderby'],
'order' => $atts['order'],
);

// Hide donors with zero donation amount.
if ( $atts['hide_empty'] ) {
$donor_args['donation_amount'] = array(
'compare' => '>=',
'amount' => 1,
);
}

// Hide donors with zero donation amount.
if ( $atts['form_id'] ) {
$donor_args['give_forms'] = $atts['form_id'];
}

// Query to output donation forms.
$donor_query = new Give_Donors_Query( $donor_args );
$donors = $donor_query->get_donors();

if ( $donors ) {

ob_start();

echo '<div class="give-wrap">';
echo '<div class="give-grid give-grid--' . esc_attr( $atts['columns'] ) . '">';

foreach ( $donors as $donor ) {
// Give/templates/shortcode-donor-grid.php.
give_get_template( 'shortcode-donor-grid', array( $donor, $give_settings, $atts ) );
}

echo '</div><!-- .give-grid -->';

if ( false !== $atts['paged'] ) {

$_donor_query['number'] = - 1;
$_donor_query['offset'] = 0;
$donor_count = count( Give()->donors->get_donors( $_donor_query ) );

$paginate_args = array(
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => ceil( $donor_count / $atts['donors_per_page'] ),
'show_all' => false,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => __( '« Previous', 'give' ),
'next_text' => __( 'Next »', 'give' ),
'type' => 'plain',
'add_args' => false,
);

printf( '<div class="give-page-numbers">%s</div>', paginate_links( $paginate_args ) );
echo '</div><!-- .give-wrap -->';
}

return ob_get_clean();
}
}


}


new Give_Donor_Wall();
36 changes: 33 additions & 3 deletions includes/donors/class-give-donors-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function __construct( $args = array() ) {
'count' => false,
'give_forms' => array(),

/*
/**
* donation_amount will contain value like:
* array(
* 'compare' => *compare symbol* (by default set to > )
Expand Down Expand Up @@ -235,6 +235,8 @@ public function get_sql() {
// WordPress consider LIKE condition as placeholder if start with s,f, or d.
$sql = str_replace( 'LIMIT', "{$where} {$orderby} {$this->args['order']} LIMIT", $sql );

error_log( print_r( $sql, true ) . "\n", 3, WP_CONTENT_DIR . '/debug_new.log' );

return $sql;
}

Expand Down Expand Up @@ -263,7 +265,8 @@ private function get_where_query() {
$where[] = $this->get_where_donor();
$where[] = $this->get_where_user();
$where[] = $this->get_where_date();
$where[] = $this->get_where_purchase_count();
$where[] = $this->get_where_donation_amount();
$where[] = $this->get_where_donation_count();
$where[] = $this->get_where_give_forms();

$where = array_filter( $where );
Expand Down Expand Up @@ -444,6 +447,33 @@ private function get_order_query() {
return $query;
}

/**
* Set donation count value where clause.
* @todo: add phpunit test
*
* @since 2.2.0
* @access private
*
* @global wpdb $wpdb
* @return string
*/
private function get_where_donation_count() {
$where = '';

if ( ! empty( $this->args['donation_count'] ) ) {
$compare = '>';
$amount = $this->args['donation_count'];
if ( is_array( $this->args['donation_count'] ) ) {
$compare = $this->args['donation_count'] ['compare'];
$amount = $this->args['donation_count']['amount'];
}

$where .= "AND {$this->table_name}.purchase_count{$compare}{$amount}";
}

return $where;
}

/**
* Set purchase value where clause.
* @todo: add phpunit test
Expand All @@ -454,7 +484,7 @@ private function get_order_query() {
* @global wpdb $wpdb
* @return string
*/
private function get_where_purchase_count() {
private function get_where_donation_amount() {
$where = '';

if ( ! empty( $this->args['donation_amount'] ) ) {
Expand Down
4 changes: 2 additions & 2 deletions includes/forms/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ class="give-input required"
<?php if ( give_field_is_required( 'give_comment', $form_id ) ) { ?>
<span class="give-required-indicator">*</span>
<?php } ?>
<?php echo Give()->tooltips->render_help( __( 'Need description.', 'give' ) ); ?>
<?php echo Give()->tooltips->render_help( esc_html__( 'Would you like to prevent this donation from being displayed publicy?', 'give' ) ); ?>
</label>
</p>
<?php endif; ?>
Expand All @@ -835,7 +835,7 @@ class="give-input required"
<?php if ( give_field_is_required( 'give_comment', $form_id ) ) { ?>
<span class="give-required-indicator">*</span>
<?php } ?>
<?php echo Give()->tooltips->render_help( __( 'Would you like to prevent this donation from being displayed publicy? ', 'give' ) ); ?>
<?php echo Give()->tooltips->render_help( __( 'Would you like to add a comment to this donation?', 'give' ) ); ?>
</label>

<textarea
Expand Down

0 comments on commit 9f5e74a

Please sign in to comment.