Created
December 16, 2014 19:05
-
-
Save jameslaws/c6c0bd3cdcb6169f8d70 to your computer and use it in GitHub Desktop.
Using Slack Slash Commands to get EDD sales info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function nftweak_slash_command() { | |
# Check to make sure this is a Slash Command Request | |
if ( ! isset( $_REQUEST['slack_slash'] ) && 'your_custom_string' != $_REQUEST['slack_slash'] ) | |
return false; | |
# Check to see if a token has been passed as well | |
if ( ! isset( $_REQUEST['token'] ) ) | |
return false; | |
# Set additional text strings if sent | |
$text = isset ( $_REQUEST['text'] ) ? $_REQUEST['text'] : ''; | |
# Check that the token is the one that was set in Slack | |
if ( 'your_slack_token' == $_REQUEST['token'] ) { | |
# Gather EDD sales data | |
$stats = new EDD_Payment_Stats; | |
$earnings_today = edd_format_amount( $stats->get_earnings( 0, 'today', false ) ); | |
$monthly_sales = edd_format_amount( $stats->get_earnings( 0, 'this_month' ) ); | |
$yearly_sales = edd_format_amount( $stats->get_earnings( 0, 'this_year' ) ); | |
$total_sales = edd_format_amount( edd_get_total_earnings() ); | |
$month = $stats->get_earnings( 0, 'this_month' ); | |
$daily_average = edd_format_amount( $month / date( 'j' ) ); | |
# check additional text to display appriate data | |
switch ( $_REQUEST['text'] ) { | |
case 'day': | |
echo '$' . $earnings_today; | |
break; | |
case 'month': | |
echo '$' . $monthly_sales; | |
break; | |
case 'year': | |
echo '$' . $yearly_sales; | |
break; | |
case 'lifetime': | |
echo '$' . $total_sales; | |
break; | |
case 'average': | |
echo '$' . $daily_average; | |
break; | |
default : | |
echo 'So far Ninja Forms has had *$' . $earnings_today . '* in sales today. Sales this month are currently at *$' . $monthly_sales . '* and this year, *$' . $yearly_sales . '*. Lifetime sales are currently at *$' . $total_sales . '*. This months daily average is currently *$' . $daily_average . '*.'; | |
break; | |
} | |
# We need to die or you will send a bunch of html that Slack won't be able to handle | |
die(); | |
} | |
} | |
add_action( 'init', 'nftweak_slash_command' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment