Skip to content

Instantly share code, notes, and snippets.

@DrewAPicture
Last active May 1, 2018 16:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrewAPicture/69acc0a74af184f11443687802ac89a4 to your computer and use it in GitHub Desktop.
Save DrewAPicture/69acc0a74af184f11443687802ac89a4 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Easy Digital Downloads - Example Report
* Plugin URI: https://easydigitaldownloads.com
* Description: Implements an example report for the 3DD reports API.
* License: GPL
* Author: Bulk WP
* Version: 1.0.0
* Text Domain: edd-example-report
* Domain Path: languages/
*
* Easy Digital Downloads is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* Easy Digital Downloads is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Easy Digital Downloads. If not, see <http://www.gnu.org/licenses/>.
*
* @package EDD
* @category Core
* @author Drew Jaynes
* @version 1.0.0
*/
/**
* Implements an Example report in the EDD 3.0 Reports API.
*
* @since 1.0.0
*/
class EDD_Example_Report {
/**
* Represents the one true class instance.
*
* @since 1.0.0
* @var \EDD_Example_Report
*/
private static $instance;
/**
* Plugin version.
*
* @since 1.0.0
* @var string
*/
public $version = '1.0.0';
/**
* Retrieves the class instance.
*
* @since 1.0.0
*
* @return \EDD_Example_Report Class instance.
*/
public static function get_instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceOf EDD_Example_Report ) ) {
self::$instance = new EDD_Example_Report;
self::$instance->includes();
self::$instance->hooks();
}
return self::$instance;
}
/**
* Includes required files.
*
* @since 1.0.0
*/
private function includes() {}
/**
* Register hook callbacks for the example report.
*
* @since 1.0.0
*/
private function hooks() {
add_action( 'edd_reports_init', array( $this, 'register_report' ) );
add_action( 'edd_reports_init', array( $this, 'register_endpoints' ) );
}
/**
* Registers the report.
*
* @since 1.0.0
*
* @param \EDD\Reports\Data\Reports_Registry $reports Reports registry instance.
*/
public function register_report( $reports ) {
try {
$reports->add_report( 'example', array(
'label' => __( 'Example Report', 'edd-example-report' ),
'priority' => 6,
'endpoints' => array(
'tiles' => array( 'test_data_1', 'test_data_2', 'test_data_3' ),
'tables' => array( 'test_data_2' ),
'charts' => array( 'test_data_1', 'test_data_2', 'test_data_3' ),
),
'filters' => array( 'dates' ),
) );
$reports->add_report( 'logs_example', array(
'label' => __( 'Logs Example Report', 'edd-example-report' ),
'group' => 'logs',
'priority' => 6,
'endpoints' => array(
'tiles' => array( 'test_data_1', 'test_data_2', 'test_data_3' ),
'tables' => array( 'test_data_2' ),
'charts' => array( 'test_data_3', 'test_data_2' ),
),
'filters' => array( 'dates' ),
) );
} catch ( \EDD_Exception $exception ) {
edd_debug_log_exception( $exception );
}
}
/**
* Registers the report endpoints.
*
* @since 1.0.0
*
* @param EDD\Reports\Data\Reports_Registry $reports Reports registry instance.
*/
public function register_endpoints( $reports ) {
try {
$reports->register_endpoint( 'test_data_1', array(
'label' => __( 'Test Data 1', 'edd-example-report' ),
'views' => array(
'tile' => array(
'data_callback' => '__return_true',
),
'chart' => array(
'data_callback' => function() {
return array(
'sales' => array( 30, 20, 10, 15, 25 ),
);
},
'type' => 'pie',
'options' => array(
'cutoutPercentage' => 50,
'datasets' => array(
'sales' => array(
'label' => __( 'Sales' ),
'backgroundColor' => array(
'rgb(133,175,91)',
'rgb(9,149,199)',
'rgb(8,189,231)',
'rgb(137,163,87)',
'rgb(27,98,122)',
),
),
),
'labels' => array( 'First', 'Second', 'Third', 'Fourth', 'Fifth' ),
),
),
)
) );
$added = $reports->register_endpoint( 'test_data_2', array(
'label' => __( 'Test Data 2', 'edd-example-report' ),
'views' => array(
'tile' => array(
'data_callback' => '__return_true',
'display_args' => array(
'context' => 'secondary',
'comparison_label' => 'Nothing to see here.'
)
),
'table' => array(
'display_args' => array(
'class_name' => 'EDD_Gateawy_Reports_Table',
'class_file' => EDD_PLUGIN_DIR . 'includes/admin/reporting/class-gateways-reports-table.php',
)
),
'chart' => array(
'data_callback' => function() {
return array(
'sales' => array( 40, 20, 30, 10 ),
);
},
'type' => 'pie',
'options' => array(
'cutoutPercentage' => 50,
'datasets' => array(
'sales' => array(
'label' => __( 'Sales' ),
'backgroundColor' => array(
'rgb(234,16,109)',
'rgb(98,133,193)',
'rgb(151,99,143)',
'rgb(244,10,43)',
),
),
),
'labels' => array( 'First', 'Second', 'Third', 'Fourth' ),
),
),
)
) );
$reports->register_endpoint( 'test_data_3', array(
'label' => __( 'Test Data 3', 'edd-example-report' ),
'views' => array(
'tile' => array(
'data_callback' => '__return_true',
'display_args' => array(
'context' => 'tertiary',
'comparison_label' => 'Nothing to see here either.'
),
),
'chart' => array(
'data_callback' => function() {
$earnings = array();
for ( $i = 0; $i <= 20; $i++ ) {
$earnings[ $i ][] = current_time( 'timestamp' ) - MONTH_IN_SECONDS + ( DAY_IN_SECONDS * $i );
$earnings[ $i ][] = $i + rand( 1, 5 );
}
for ( $i = 0; $i <= 20; $i++ ) {
$sales[ $i ][] = current_time( 'timestamp' ) + 3600 - MONTH_IN_SECONDS + ( DAY_IN_SECONDS * $i );
$sales[ $i ][] = $i + rand( 1, 5 );
}
return array(
'earnings' => $earnings,
'sales' => $sales,
);
},
'type' => 'line',
'options' => array(
'datasets' => array(
'sales' => array(
'label' => __( 'Sales' ),
'borderColor' => 'rgb(237,194,64)',
'backgroundColor' => 'rgb(237,194,64)',
'fill' => false,
),
'earnings' => array(
'label' => __( 'Earnings' ),
'borderColor' => 'rgb(175,216,248)',
'backgroundColor' => 'rgb(175,216,248)',
'fill' => false,
),
),
),
),
),
) );
} catch ( \EDD_Exception $exception ) {
edd_debug_log_exception( $exception );
}
}
}
add_action( 'plugins_loaded', array( 'EDD_Example_Report', 'get_instance' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment