Fundraising - Display Latest Donors in Widget

I am trying to display a list of the latest donors in a widget.

The following thread gave me high hopes but unfortunately the code provided doesn't work for me: https://premium.wpmudev.org/forums/topic/is-there-a-shortcode-to-display-an-autoupdated-list-of-donors-for-a-fundraiser-campaign

Any idea / pointers on how to achieve this?

Simon

  • Brashell
    • The Bug Hunter

    @Vaughan I am having a bit of difficulty coding this, I am not sure why I can't quite get it. Below is my thinking for if it were a multisite, obviously we would remove $blog_prefix for a single site install. I apologize for not being able to complete this on my own but I am still learning and can't really complete this. I was thinking of basically doing SELECT meta_value FROM ".$wpdb->base_prefix.$blog_prefix."postmeta" WHERE meta_key = "wdf_transaction" then arranging the meta_value in an array to output the first_name, last_name, payer_email, currency_code, and gross. Here is the meta_value example in the DB a:10:{s:5:"gross";s:4:"1000";s:4:"type";s:6:"simple";s:13:"currency_code";s:3:"USD";s:10:"first_name";s:7:"Brandon";s:9:"last_name";s:7:"Gelfand";s:11:"payer_email";s:24:"brandongelfand@yahoo.com";s:14:"gateway_public";s:6:"Manual";s:7:"gateway";s:6:"manual";s:6:"status";s:16:"Pending/Approved";s:11:"gateway_msg";s:15:"Manual Payment.";} I hope you kind of get my idea, be nice if you could comment your code so I know how to do this. I tried for about 3 hours but couldn't quite get it to work. Write back if you have any questions with my thinking. If I can figure out how to arrange the meta_values before you write the code I will reply back, but at the moment I am clueless.

  • simonswiss
    • WPMU DEV Initiate

    @Brashell I'm a front-end developer primarily - i usually work with premium WP themes for clients and tweak the looks and basic functionalities. My PHP and SQL are limited, hence why i'm struggling with getting this widget to work.

    Thanks a lot for your time and input, i really appreciate that!

  • Brashell
    • The Bug Hunter

    json_encode just keeps running around in my head but I am not sure how I can set the labels to the meta_values. Hopefully this will give @Vaughan some ideas how to do it maybe... @simonswiss I could use some input in the near future with your thoughts about an LMS I am working on. The overall design of it. ATM part of its broken else I would show you now what it looks like.

  • Brashell
    • The Bug Hunter

    Sounds good, I do a bit of design but still have a few tricks of the trade to learn. Going to be interesting when I start adding multiple themes to choose from. I like to keep all the designs looking like they go together, most of my CSS is custom done. I hate wordpress sites where one design is like a blue and one is a red etc.

  • Brashell
    • The Bug Hunter

    I gave it another shot, but IDK why it just keeps returning Array and I am not sure. Here is like the fourth thing I tried, it just won't work though. GL @Vaughan

    function get_user_donation_pledge_list($post_id = false) {
    		global $post;
    		$post_id = ($post_id != false ? $post_id : $post->ID);
    		$args = array(
    			'numberposts' => -1,
    			'post_type' => 'donation',
    			'post_status' => array('wdf_complete','wdf_approved')
    		);
    		$list = get_posts($args);
    		if( !$list || empty($list) )
    			return false;
    		else
    			return $list;
    	} add_shortcode( 'userpledgesnames', 'get_user_donation_pledge_list' );
  • bhuskilsonVLC
    • WPMU DEV Initiate

    @simonswiss
    I've tested the widget I posted a while back and it still works.

    I just ran into a new problem with it and I've fixed it.

    Let me know if this works for you.

    *Edit: just noticed something else and fixed it.

    <?php
    /*
    Plugin Name: Top Funders Widget
    Plugin URI: http://huskor.com/
    Description: Selects highest contributions from WPMU Dev's fundraising script.
    Author: Bryce Huskilson
    Version: 1
    Author URI: http://huskor.com/
    */
    
    class TopFundersWidget extends WP_Widget
    {
      function TopFundersWidget()
      {
        $widget_ops = array('classname' => 'TopFundersWidget', 'description' => 'Displays top contributions from WPMUDEV fundraising script' );
        $this->WP_Widget('TopFundersWidget', 'Top Funders List', $widget_ops);
      }
    
      function form($instance)
      {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
        $title = $instance['title'];
    ?>
      <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
    <?php
      }
    
      function update($new_instance, $old_instance)
      {
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        return $instance;
      }
    
      function widget($args, $instance)
      {
        extract($args, EXTR_SKIP);
    
        echo $before_widget;
        $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
    
        if (!empty($title))
          echo $before_title . $title . $after_title;;
    
        // WIDGET CODE GOES HERE
    
        function get_pledge_list_all($post_id = false) {
    		global $post;
    		$post_id = ($post_id != false ? $post_id : $post->ID);
    		$args = array(
    			'numberposts' => -1,
    			'post_type' => 'donation',
    			'post_status' => array('wdf_complete','wdf_approved')
    		);
    		$list = get_posts($args);
    		if( !$list || empty($list) )
    			return false;
    		else
    			return $list;
    	}
    
    	global $wp_query;
    
    // If fundraising is installed then continue
    if(class_exists('WDF')) {
    	global $wdf;
    	// get_pledge_list() returns standard array() of post objects
    
    	if ( 'funder' == get_post_type() ) {
    	    $pledge_list = $wdf->get_pledge_list($post_id);
    	} else {
    	    $pledge_list = get_pledge_list_all();
    	}
    
    	//print_r(get_post($post_id));
    	if ($pledge_list) {
    
    	$pledgesort = array();
    	// Build array to compare email accounts
    	foreach($pledge_list as $pledge) {
    		$transaction = $wdf->get_transaction($pledge->ID);
    
    		$lname = $transaction['last_name'];
    		$pledgesortdata = array(
    		'name' => $transaction['first_name'].' '.$lname[0],
    		'amount' => intval(preg_replace('~\.0+$~','',$transaction['gross'])),
    		'email' => $transaction['payer_email']
    		);
    
    		$pledgesort[] = $pledgesortdata;
    	}
    
    	// Function to group donation by paypal email address
    	function arraySort($input,$sortkey){
    	  foreach ($input as $key=>$val) $output[$val[$sortkey]][]=$val;
    	  return $output;
    	}
    	$pledgesort = arraySort($pledgesort,'email');
    
    	$backerlist = array();
    	foreach($pledgesort as $backeremail) {
    
    			$amount = 0;
    			foreach($backeremail as $key) {
    				$amount = $amount + $key['amount'];
    			}
    
    		$backerlistdata = array(
    		'name' => $key['name'],
    		'amount' => $amount,
    		'email' => $key['email']
    		);
    		$backerlist[] = $backerlistdata;
    
    	}
    
    	$backerlist = arraySort($backerlist,'amount');
    
    	krsort($backerlist);
    
    	$backerlist = array_slice($backerlist, 0, 10);	
    
    	//print_r($backerlist);
    
    	echo '<div id="topbackers">
    	<span class="homehilight" style="float:right; margin-right:20px;">
        Total Funded</span>
    	<div style="clear:both"></div>
    	<div id="backerscroll" style="overflow:auto; height:150px">
      <ul>
    	';
    	$fan = 0;
    	foreach($backerlist as $backer) {
    		$fan = $fan + 1;
    
    		echo '<li><span class="homehilight"># '.$fan.' Fan Spot:</span><br />';
    		$i = 0;
    		foreach($backer as $key) {
    		echo '<div>'.$key['name'].' ';
    		echo ' <span style="float:right; margin-right:5px;">$'.$key['amount'].'</span></div>';
    		}
    		echo '</li>';
    
    	}
    	echo '</ul></div></div>';
    	}
    
    }
    
        echo $after_widget;
      }
    
    }
    add_action( 'widgets_init', create_function('', 'return register_widget("TopFundersWidget");') );?>
  • bhuskilsonVLC
    • WPMU DEV Initiate

    I should add that the issue I fixed was that it was not displaying any of the top funders on the home page if there were any posts on the site.

    I'm not sure why, but get_pledge_list() only returns all donations if there are no posts on the site. I didn't realize this until I set up a blog and created a post.

    To fix this I created a new function get_pledge_list_all() that is used to call all donations if you are on any page other than a fundraiser page.

    So, the widget pulls top funders for a specific fundraiser if it is on that page and top funders for all fundraisers if it is on any other page.

    Let me know if you get stuck and I will try my best to help.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.