Skip to content

Commit

Permalink
Merge pull request #3930 from impress-org/hotfix/ajaxify-dashboard-wi…
Browse files Browse the repository at this point in the history
…dget

feat: load dashboard widget with ajax
  • Loading branch information
Devin Walker committed Jan 10, 2019
2 parents 4a19204 + 69d7981 commit f817bc2
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions includes/admin/dashboard-widgets.php
Expand Up @@ -22,12 +22,64 @@
*/
function give_register_dashboard_widgets() {
if ( current_user_can( apply_filters( 'give_dashboard_stats_cap', 'view_give_reports' ) ) ) {
wp_add_dashboard_widget( 'give_dashboard_sales', __( 'Give: Donation Statistics', 'give' ), 'give_dashboard_sales_widget' );
wp_add_dashboard_widget( 'give_dashboard_sales', __( 'Give: Donation Statistics', 'give' ), 'give_render_dashboard_stats_widget' );
}
}

add_action( 'wp_dashboard_setup', 'give_register_dashboard_widgets', 10 );


/**
* Sales Summary Dashboard Widget render callback
* Note: only for internal use
*
* Builds and renders the ajaxify statistics dashboard widget.
* This widget displays the current month's donations.
*
* @since 2.4.0
* @return void
*/
function give_render_dashboard_stats_widget() {
if ( ! current_user_can( apply_filters( 'give_dashboard_stats_cap', 'view_give_reports' ) ) ) {
return;
}

?>
<div id="give-dashboard-sales-widget">
<span class="spinner is-active" style="float: none;margin: auto 50%;padding-bottom: 15px;"></span>

<script>
jQuery(document).ready(function () {
jQuery.ajax({
url: ajaxurl,
data: {
action: 'give_render_dashboard_stats_widget'
},
success: function (response) {
jQuery('#give-dashboard-sales-widget').html(response);
}
});
})
</script>
</div>
<?php
}

/**
* Ajax handler for dashboard statistic widget render
* Note: only for internal use
*
* @since 2.4.0
*/
function give_ajax_render_dashboard_stats_widget(){
ob_start();
give_dashboard_stats_widget();

wp_send_json( ob_get_clean() );

}
add_action( 'wp_ajax_give_render_dashboard_stats_widget', 'give_ajax_render_dashboard_stats_widget' );

/**
* Sales Summary Dashboard Widget
*
Expand All @@ -36,7 +88,7 @@ function give_register_dashboard_widgets() {
* @since 1.0
* @return void
*/
function give_dashboard_sales_widget() {
function give_dashboard_stats_widget() {

if ( ! current_user_can( apply_filters( 'give_dashboard_stats_cap', 'view_give_reports' ) ) ) {
return;
Expand Down

0 comments on commit f817bc2

Please sign in to comment.