Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fetch sales & earnings (or refunded sales & earnings) for different products, date-wise - for Easy Digital Downloads
<?php
/*
Plugin Name: Extra API Endpoints for Easy Digital Downloads
Plugin URI: https://www.tychesoftwares.com
Description: This plugin allows to fetch some additional statistics for your Easy Digital Downloads store.
Author: Tyche Softwares
Version: 1.0
Author URI: http://www.tychesoftwares.com/about
Contributor: Tyche Softwares, http://www.tychesoftwares.com/
*/
class EDD_EXTRA_API_ENDPOINTS {
public function __construct() {
add_filter( 'edd_api_valid_query_modes', array( &$this, 'edd_register_routes' ), 10, 1 );
add_filter( 'edd_api_output_data', array( &$this, 'edd_api_output_data' ), 10, 3 );
add_filter( 'query_vars', array( &$this, 'query_vars' ) );
}
/**
* Registers custom routes with the EDD API
*
* @access public
* @return array
*/
public function edd_register_routes( $end_points ) {
$end_points[] = 'create_coupon'; // create_coupon route added by Moxa
$end_points[] = 'advanced_stats'; // advanced_stats route added by Vishal
$end_points[] = 'sites_activated_for_license'; // get sites activated by each license, added by Vishal
$end_points[] = 'plugin_wise_renewals'; // added by Vishal
return $end_points;
}
public function edd_api_output_data( $data, $endpoint, $this_var ) {
switch ( $endpoint ) {
case 'create_coupon':
if( isset( $_POST ) ) {
$discount_id = edd_store_discount( $_POST );
$discount_code = edd_get_discount_code( $discount_id );
$this->send_email( $discount_id );
$data[ 'dcode' ] = $discount_code;
$data[ 'success' ] = 'true';
} else {
$data[ 'success' ] = 'false';
}
break;
case 'advanced_stats':
$data[ 'success' ] = true;
$data[ 'stats' ] = $this->get_advanced_stats();
break;
case 'sites_activated_for_license':
$data[ 'success' ] = true;
$data[ 'licenses_count' ] = $this->get_sites_activated_for_license();
break;
case 'plugin_wise_renewals':
$data[ 'success' ] = true;
$data[ 'renewals' ] = $this->get_plugin_wise_renewals();
break;
default:
# code..
break;
}
return $data;
}
public function query_vars( $vars ) {
$vars[] = 'status';
$vars[] = 'start_date';
$vars[] = 'end_date';
$vars[] = 'download_type'; // type can be 'bundle' or 'addon' or 'download', default = download (11 core plugins)
return $vars;
}
/**
* Retrieves Advanced Stats like year-wise, download-wise refunds or sales
*
* Usage:
*
* <edd-store-url>/edd-api/advanced_stats/?key=<public key>&token=<token>&status=publish&start_date=2017-01-01&end_date=2017-12-31
* status can be publish or refunded, where status=publish will display sales & corresponding earnings
* status=refunded will display refunded sales & corresponding revenue
* start_date - date from which to fetch the sales, yyyy-mm-dd format
* end_date - date until which to fetch the sales, yyyy-mm-dd format
* download_type - all, addon or bundle, default value is set to 'all'
*
* @access public
* @return array
*/
public function get_advanced_stats() {
global $wp_query;
$sales = array();
if( isset( $wp_query->query_vars['id'] ) ) {
$query = array();
$query[] = new EDD_Payment( $wp_query->query_vars['id'] );
} elseif( isset( $wp_query->query_vars['purchasekey'] ) ) {
$query = array();
$query[] = edd_get_payment_by( 'key', $wp_query->query_vars['purchasekey'] );
} elseif( isset( $wp_query->query_vars['email'] ) ) {
$query = edd_get_payments( array( 'fields' => 'ids', 'meta_key' => '_edd_payment_user_email', 'meta_value' => $wp_query->query_vars['email'], 'number' => $this->per_page(), 'page' => $this->get_paged(), 'status' => 'publish' ) );
} elseif ( isset( $wp_query->query_vars['status'] ) ) {
$start_date = isset( $wp_query->query_vars['start_date'] ) ? $wp_query->query_vars['start_date'] : date( 'Y-01-01' );
$end_date = isset( $wp_query->query_vars['end_date'] ) ? $wp_query->query_vars['end_date'] : date( 'Y-m-d' );
$query = edd_get_payments( array( 'fields' => 'ids',
'number' => -1,
'status' => $wp_query->query_vars['status'],
'start_date' => $start_date,
'end_date' => $end_date ) );
} else {
$query = edd_get_payments( array( 'fields' => 'ids', 'number' => $this->per_page(), 'page' => $this->get_paged(), 'status' => 'publish' ) );
}
if ( $query ) {
$i = 0;
$item_wise_sales = array();
$item_wise_revenue = array();
$item_type = 'all';
if ( isset( $wp_query->query_vars[ 'download_type' ] ) ) {
$item_type = $wp_query->query_vars[ 'download_type' ];
}
foreach ( $query as $payment ) {
if ( is_numeric( $payment ) ) {
$payment = new EDD_Payment( $payment );
}
$c = 0;
// if fetching for 'addon' or 'bundle' & not all downloads
if ( $item_type != 'all' ) {
foreach ( $payment->cart_details as $key => $item ) {
$item_id = isset( $item['id'] ) ? $item['id'] : $item;
$download_name = get_the_title( $item_id );
$price_name = '';
if ( isset( $item['item_number'] ) && isset( $item['item_number']['options'] ) ) {
$price_options = $item['item_number']['options'];
if ( isset( $price_options['price_id'] ) ) {
$price_name = edd_get_price_option_name( $item_id, $price_options['price_id'], $payment->ID );
}
}
$download_full_name = $download_name . ' - ' . $price_name;
$price = isset( $item['price'] ) ? $item['price'] : false;
$pattern = '/' . $item_type . '/i';
if ( preg_match( $pattern, strtolower( $download_name ) ) != 0 ) {
$item_wise_sales[ $item_type ][ $download_full_name ] = $item_wise_sales[ $item_type ][ $download_full_name ] + 1;
$item_wise_revenue[ $item_type ][ $download_full_name ] = $item_wise_revenue[ $item_type ][ $download_full_name ] + $price;
$c++;
}
}
} else {
foreach ( $payment->cart_details as $key => $item ) {
$item_id = isset( $item['id'] ) ? $item['id'] : $item;
$download_name = get_the_title( $item_id );
$price_name = '';
if ( isset( $item['item_number'] ) && isset( $item['item_number']['options'] ) ) {
$price_options = $item['item_number']['options'];
if ( isset( $price_options['price_id'] ) ) {
$price_name = edd_get_price_option_name( $item_id, $price_options['price_id'], $payment->ID );
}
}
$download_full_name = $download_name . ' - ' . $price_name;
$price = isset( $item['price'] ) ? $item['price'] : false;
$item_wise_sales[ $download_full_name ] = $item_wise_sales[ $download_full_name ] + 1;
$item_wise_revenue[ $download_full_name ] = $item_wise_revenue[ $download_full_name ] + $price;
$c++;
}
}
$i++;
}
}
//echo '<pre>';print_r($item_wise_sales);echo '</pre>';
//echo '<pre>';print_r($item_wise_revenue);echo '</pre>';
if( $wp_query->query_vars['status'] == 'refunded' || $wp_query->query_vars['status'] == 'publish' )
{
return array(
'sales' => $item_wise_sales,
'revenues' => $item_wise_revenue );
}
}
/**
* Retrieves Maximum number of sites that are active per license key
*
* @access public
* @return array
*/
public function get_sites_activated_for_license() {
global $wp_query, $wpdb;
$args = array( 'post_type' => 'edd_license',
'posts_per_page' => 5000 );
$licenses = new WP_Query( $args );
$licenses_count = array();
while ( $licenses->have_posts() ) {
$site_count = 0;
$license_id = 0;
$licenses->the_post();
$license = get_the_ID();
$license_key = get_post_meta( $license, '_edd_sl_key', true );
$license_status = get_post_meta( $license, '_edd_sl_status', true );
if ( isset( $license_key ) && '' != $license_key ) {
$license_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}edd_licenses WHERE license_key = %s LIMIT 1", $license_key ) );
if ( isset( $license_id ) && 0 < $license_id ) {
$site_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(site_id) FROM {$wpdb->prefix}edd_license_activations WHERE license_id = %s", $license_id ) );
// display only license keys that are active on more then 1 site
if ( 1 < $site_count ) {
//$licenses_count[ $license_status ][ $license_key ] = $site_count;
$licenses_count[ $license_key ] = $site_count;
}
}
}
}
arsort( $licenses_count );
//echo '<pre>';print_r( $licenses_count );echo '</pre>';
//exit;
return $licenses_count;
}
/**
* Gets how many renewals are done for each plugin
*
* @access public
* @return array
*/
public function get_plugin_wise_renewals() {
global $wp_query;
$sales = array();
if( isset( $wp_query->query_vars['email'] ) ) {
$query = edd_get_payments( array( 'fields' => 'ids', 'meta_key' => '_edd_payment_user_email', 'meta_value' => $wp_query->query_vars['email'], 'number' => $this->per_page(), 'page' => $this->get_paged(), 'status' => 'publish' ) );
} elseif ( isset( $wp_query->query_vars['download_id'] ) ) {
$start_date = isset( $wp_query->query_vars['start_date'] ) ? $wp_query->query_vars['start_date'] : date( 'Y-01-01' );
$end_date = isset( $wp_query->query_vars['end_date'] ) ? $wp_query->query_vars['end_date'] : date( 'Y-m-d' );
$query = edd_get_payments( array( 'fields' => 'ids',
'number' => -1,
'status' => $wp_query->query_vars['status'],
'start_date' => $start_date,
'end_date' => $end_date ) );
} else {
$start_date = isset( $wp_query->query_vars['start_date'] ) ? $wp_query->query_vars['start_date'] : date( 'Y-01-01' );
$end_date = isset( $wp_query->query_vars['end_date'] ) ? $wp_query->query_vars['end_date'] : date( 'Y-m-d' );
$query = edd_get_payments( array( 'fields' => 'ids',
'number' => -1,
'status' => 'publish',
'start_date' => $start_date,
'end_date' => $end_date ) );
}
if ( $query ) {
$i = 0;
$item_wise_sales = array();
$item_wise_revenue = array();
$item_type = 'all';
if ( isset( $wp_query->query_vars[ 'download_type' ] ) ) {
$item_type = $wp_query->query_vars[ 'download_type' ];
}
foreach ( $query as $payment ) {
if ( is_numeric( $payment ) ) {
$payment = new EDD_Payment( $payment );
}
$c = 0;
// if fetching for 'addon' or 'bundle' & not all downloads
if ( $item_type != 'all' ) {
foreach ( $payment->cart_details as $key => $item ) {
$item_id = isset( $item['id'] ) ? $item['id'] : $item;
$download_name = get_the_title( $item_id );
$price_name = '';
if ( isset( $item['item_number'] ) && isset( $item['item_number']['options'] ) ) {
$price_options = $item['item_number']['options'];
if ( isset( $price_options['price_id'] ) ) {
$price_name = edd_get_price_option_name( $item_id, $price_options['price_id'], $payment->ID );
}
}
$download_full_name = $download_name . ' - ' . $price_name;
$price = isset( $item['price'] ) ? $item['price'] : false;
$pattern = '/' . $item_type . '/i';
if ( preg_match( $pattern, strtolower( $download_name ) ) != 0 ) {
$item_wise_sales[ $item_type ][ $download_full_name ] = $item_wise_sales[ $item_type ][ $download_full_name ] + 1;
$item_wise_revenue[ $item_type ][ $download_full_name ] = $item_wise_revenue[ $item_type ][ $download_full_name ] + $price;
$c++;
}
}
} else {
foreach ( $payment->cart_details as $key => $item ) {
$item_id = isset( $item['id'] ) ? $item['id'] : $item;
$download_name = get_the_title( $item_id );
$price_name = '';
if ( isset( $item['item_number'] ) && isset( $item['item_number']['options'] ) ) {
$price_options = $item['item_number']['options'];
if ( isset( $price_options['price_id'] ) ) {
$price_name = edd_get_price_option_name( $item_id, $price_options['price_id'], $payment->ID );
}
}
$download_full_name = $download_name . ' - ' . $price_name;
$price = isset( $item['price'] ) ? $item['price'] : false;
if ( isset( $price_options['is_renewal'] ) && 1 == $price_options['is_renewal'] ) {
$item_wise_renewals[ $download_full_name ] = $item_wise_renewals[ $download_full_name ] + 1;
$item_wise_revenue[ $download_full_name ] = $item_wise_revenue[ $download_full_name ] + $price;
$total_renewals = $total_renewals + 1;
$total_renewals_revenue = $total_renewals_revenue + $price;
}
$c++;
}
}
$i++;
}
}
return array(
'renewals' => $item_wise_renewals,
'revenues' => $item_wise_revenue,
'total_renewals' => $total_renewals,
'total_revenue' => $total_renewals_revenue );
}
public function send_email( $discount_id ) {
$discount_code = edd_get_discount_code( $discount_id );
$expiration = date_i18n( get_option( 'date_format' ), strtotime( edd_get_discount_expiration( $discount_id ) ) );
$edd_emails = new EDD_Emails();
$message = "Thank you for allowing us to track our plugin's usage data.<br><br>You can use the coupon code " . $discount_code . " to avail 20% discount on your next purchase with <a href='https://www.tychesoftwares.com/'>Tyche Softwares</a>.<br><br>The coupon is valid until " . $expiration . ".";
$message = $edd_emails->build_email( $message );
$from_name = edd_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
$from_email = edd_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
if( empty( $to_email ) ) {
$to_email = $_POST[ 'admin_email' ];
}
$subject = edd_get_option( 'discount_subject', __( 'Discount Coupon', 'easy-digital-downloads' ) );
$heading = edd_get_option( 'discount_heading', __( 'Discount Coupon', 'easy-digital-downloads' ) );
$edd_emails->__set( 'from_name', $from_name );
$edd_emails->__set( 'from_email', $from_email );
$edd_emails->__set( 'heading', $heading );
wp_mail( $to_email, $subject, $message, $edd_emails->get_headers() );
}
}
new EDD_EXTRA_API_ENDPOINTS();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.