WeFoster / buddypress-give

BuddyPress Give Add-on
GNU General Public License v3.0
10 stars 2 forks source link

Add the donation dashboard to the user profile #7

Open BoweFrankema opened 9 years ago

BoweFrankema commented 9 years ago

We should integrate the dashboard into a users profile if they have an account. I've done this before for EDD and I did the following

  1. Create a new BP profile screen + BP nav item
  2. Output the shortcode for the dashboard directly into the new member screen template like this:

           <?php
               echo do_shortcode('[purchase_history]');
       ?>
  3. Do a template redirect on the page that is used for the shortcode like shown here: https://gist.github.com/BoweFrankema/9f9b34235a1f43e3df88

Full snippet can be seen here;

https://gist.github.com/BoweFrankema/685d5a52ef98a29cdc6a#file-edd-buddypress-purchase-php

This worked for me, but I'm sure it can be improved. Especially retrieving the "Donation Dashboard" page without hardcoding the slug!

BoweFrankema commented 9 years ago

I've managed to get this working very easily, although I hardcoded some things we'd probably need to make dynamic!

if ( function_exists( 'bp_is_member' ) ) {
    /**
     * Add Give Donations Page
     *
     */
    function give_bp_setup_donations(){
        global $bp;
        $profile_link = bp_loggedin_user_domain() . $bp->profile->slug . '/';
        $args = array(
                    'name' => __('My Donations','cfctranslation'),
                    'slug' => 'my-donations',
                    'parent_url' => $profile_link,
                    'parent_slug' => $bp->profile->slug,
                    'screen_function' => 'screen_give_donations',
                    'user_has_access'   => ( bp_is_my_profile() || is_super_admin() ),
                    'position' => 40
                );
        bp_core_new_subnav_item($args);
    }
    add_action( 'bp_setup_nav', 'give_bp_setup_donations' );

    function screen_give_donations(){
        global $bp;
        add_action( 'bp_template_title', 'give_bp_page_title');
        add_action( 'bp_template_content', 'give_bp_page_content');
        bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
    }

    function give_bp_page_title(){
            echo __('Your Donations','cfctranslation');
    }

    function give_bp_page_content(){?>

            <div id="give-my-donations">        
                <?php
                    echo do_shortcode('[donation_history]');
                ?>
            </div>

            <div>

                    Make a donation

                    <?php 
                        echo do_shortcode('[give_form id="688"]');
                    ?>

            </div>

            <?php
    }

    function setup_give_donations () {
        if ( !is_admin() ) {
             add_action( 'bp_xprofile_setup_nav', 'give_bp_setup_donations' );
        }
    }
    add_action('wp', 'setup_give_donations');
}

function bp_give_redirect()
//Redirect logged in users from give page to BuddyPress page
{
    if( is_user_logged_in() && is_page('donations') )
    {
        global $bp;
        wp_redirect( bp_loggedin_user_domain() . $bp->profile->slug . '/my-donations/', 301 );
        exit(); 
    }
}
add_action('wp','bp_give_redirect');

screenshot 2015-04-13 16 58 06

henrywright commented 9 years ago

Should I integrate this into the plugin?

BoweFrankema commented 9 years ago

Yep! Let's do this :dancer:

henrywright commented 9 years ago

This has now been incorporated. With reference to the hard-coded things, there's currently some outstanding tasks:

BoweFrankema commented 9 years ago
  1. Maybe we could check in the options table.. I see there is an a serialized array saved in the options table as: give_settings and an value of success_page. I think that is saved as a page ID which you could then user for the wp_redirect.
  2. Maybe we would use a hook and add a option to our BuddyPress settings:

Show a Donation Form on the User Dashboard? Please fill in the donation ID below: [input]

henrywright commented 9 years ago

This is now done. If you go to Donations > BuddyPress you can specify a form ID (see Label 4), which will result in that particular form appearing under the list of donations on "/members/username/my-donations".

Testing it highlighted some minor bugs so I've fixed those too.

If you have any trouble when testing, just delete the old bpg-options meta key from the options table so we can start afresh (but I doubt you will need to).