woocommerce/admin/class-wc-admin-reports.php

213 lines
6.8 KiB
PHP
Raw Normal View History

<?php
/**
2012-08-14 12:21:34 +00:00
* Admin Reports
*
* Functions used for displaying sales and customer reports in admin.
*
* @author WooThemes
* @category Admin
2012-08-14 12:21:34 +00:00
* @package WooCommerce/Admin/Reports
2012-12-03 19:19:58 +00:00
* @version 2.0.0
*/
2012-10-15 10:32:24 +00:00
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2012-08-14 12:21:34 +00:00
/**
2013-06-22 10:37:59 +00:00
* WC_Admin_Reports Class
2012-08-14 12:21:34 +00:00
*/
2013-06-22 10:37:59 +00:00
class WC_Admin_Reports {
2013-06-24 16:47:28 +00:00
private $start_date;
private $end_date;
2013-06-22 10:37:59 +00:00
/**
* Constructor
*/
public function __construct() {
add_filter( 'admin_menu', array( $this, 'add_menu_item' ), 20 );
add_filter( 'woocommerce_screen_ids', array( $this, 'add_screen_id' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'scripts_and_styles' ) );
}
2013-06-22 10:37:59 +00:00
/**
* Add menu item
*/
public function add_menu_item() {
add_submenu_page( 'woocommerce', __( 'Reports', 'woocommerce' ), __( 'Reports', 'woocommerce' ) , 'view_woocommerce_reports', 'wc_reports', array( $this, 'admin_page' ) );
}
2012-08-14 12:21:34 +00:00
2013-06-22 10:37:59 +00:00
/**
* Add screen ID
* @param array $ids
*/
public function add_screen_id( $ids ) {
$wc_screen_id = strtolower( __( 'WooCommerce', 'woocommerce' ) );
$ids[] = $wc_screen_id . '_page_wc_reports';
return $ids;
}
2012-08-14 12:21:34 +00:00
2013-06-22 10:37:59 +00:00
/**
* Script and styles
*/
public function scripts_and_styles() {
$screen = get_current_screen();
$wc_screen_id = strtolower( __( 'WooCommerce', 'woocommerce' ) );
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
if ( in_array( $screen->id, apply_filters( 'woocommerce_reports_screen_ids', array( $wc_screen_id . '_page_wc_reports' ) ) ) ) {
2013-06-24 16:47:28 +00:00
wp_enqueue_script( 'wc-reports', WC()->plugin_url() . '/assets/js/admin/reports' . $suffix . '.js', array( 'jquery', 'jquery-ui-datepicker' ), '1.0' );
2013-06-22 10:37:59 +00:00
wp_enqueue_script( 'flot', WC()->plugin_url() . '/assets/js/admin/jquery.flot' . $suffix . '.js', array( 'jquery' ), '1.0' );
wp_enqueue_script( 'flot-resize', WC()->plugin_url() . '/assets/js/admin/jquery.flot.resize' . $suffix . '.js', array('jquery', 'flot'), '1.0' );
wp_enqueue_script( 'flot-time', WC()->plugin_url() . '/assets/js/admin/jquery.flot.time' . $suffix . '.js', array( 'jquery', 'flot' ), '1.0' );
wp_enqueue_script( 'flot-pie', WC()->plugin_url() . '/assets/js/admin/jquery.flot.pie' . $suffix . '.js', array( 'jquery', 'flot' ), '1.0' );
2013-07-09 14:31:22 +00:00
wp_enqueue_script( 'flot-stack', WC()->plugin_url() . '/assets/js/admin/jquery.flot.stack' . $suffix . '.js', array( 'jquery', 'flot' ), '1.0' );
2013-06-22 10:37:59 +00:00
}
}
2012-08-14 12:21:34 +00:00
2013-06-22 10:37:59 +00:00
/**
* Returns the definitions for the reports to show in admin.
*
* @return array
*/
public function get_reports() {
$reports = array(
2013-06-28 16:33:37 +00:00
'orders' => array(
'title' => __( 'Orders', 'woocommerce' ),
2013-06-22 10:37:59 +00:00
'reports' => array(
"sales_by_date" => array(
2013-06-25 13:59:20 +00:00
'title' => __( 'Sales by date', 'woocommerce' ),
2013-06-22 10:37:59 +00:00
'description' => '',
'hide_title' => true,
'callback' => array( $this, 'get_report' )
2013-06-22 10:37:59 +00:00
),
"sales_by_product" => array(
'title' => __( 'Sales by product', 'woocommerce' ),
2013-06-22 10:37:59 +00:00
'description' => '',
2013-06-27 15:12:17 +00:00
'hide_title' => true,
'callback' => array( $this, 'get_report' )
2013-06-22 10:37:59 +00:00
),
"sales_by_category" => array(
'title' => __( 'Sales by category', 'woocommerce' ),
'description' => '',
2013-06-27 17:39:55 +00:00
'hide_title' => true,
'callback' => array( $this, 'get_report' )
2013-06-22 10:37:59 +00:00
),
2013-07-08 15:36:09 +00:00
"coupon_usage" => array(
'title' => __( 'Coupons by date', 'woocommerce' ),
2013-06-22 10:37:59 +00:00
'description' => '',
2013-07-08 15:36:09 +00:00
'hide_title' => true,
'callback' => array( $this, 'get_report' )
2013-06-22 10:37:59 +00:00
)
)
),
'customers' => array(
'title' => __( 'Customers', 'woocommerce' ),
'reports' => array(
2013-07-09 14:31:22 +00:00
"customers" => array(
2013-07-09 14:45:42 +00:00
'title' => __( 'Customers vs. Guests', 'woocommerce' ),
'description' => '',
'hide_title' => true,
'callback' => array( $this, 'get_report' )
),
"customer_list" => array(
'title' => __( 'Customer List', 'woocommerce' ),
2013-06-22 10:37:59 +00:00
'description' => '',
'hide_title' => true,
2013-07-09 14:31:22 +00:00
'callback' => array( $this, 'get_report' )
2013-06-22 10:37:59 +00:00
),
)
),
'stock' => array(
'title' => __( 'Stock', 'woocommerce' ),
'reports' => array(
2013-07-09 10:48:56 +00:00
"low_in_stock" => array(
'title' => __( 'Low in stock', 'woocommerce' ),
'description' => '',
'hide_title' => true,
'callback' => array( $this, 'get_report' )
),
"out_of_stock" => array(
'title' => __( 'Out of stock', 'woocommerce' ),
'description' => '',
'hide_title' => true,
'callback' => array( $this, 'get_report' )
),
"most_stocked" => array(
'title' => __( 'Most Stocked', 'woocommerce' ),
2013-06-22 10:37:59 +00:00
'description' => '',
'hide_title' => true,
2013-07-08 16:01:26 +00:00
'callback' => array( $this, 'get_report' )
2013-06-22 10:37:59 +00:00
),
)
)
);
2012-08-14 12:21:34 +00:00
2013-06-22 10:37:59 +00:00
if ( get_option( 'woocommerce_calc_taxes' ) == 'yes' ) {
$reports['taxes'] = array(
2013-07-10 11:06:29 +00:00
'title' => __( 'Taxes', 'woocommerce' ),
'reports' => array(
2013-07-10 11:05:45 +00:00
"taxes_by_code" => array(
'title' => __( 'Taxes by code', 'woocommerce' ),
'description' => '',
2013-07-10 11:05:45 +00:00
'hide_title' => true,
'callback' => array( $this, 'get_report' )
),
"taxes_by_date" => array(
'title' => __( 'Taxes by date', 'woocommerce' ),
'description' => '',
'hide_title' => true,
'callback' => array( $this, 'get_report' )
),
)
2013-06-22 10:37:59 +00:00
);
}
2012-08-14 12:21:34 +00:00
2013-06-22 10:37:59 +00:00
$reports = apply_filters( 'woocommerce_admin_reports', $reports );
2012-08-14 12:21:34 +00:00
2013-06-22 10:37:59 +00:00
// Backwards compat
$reports = apply_filters( 'woocommerce_reports_charts', $reports );
2012-08-14 12:21:34 +00:00
2013-06-22 10:37:59 +00:00
foreach ( $reports as $key => $report_group ) {
if ( isset( $reports[ $key ]['charts'] ) )
2013-07-08 11:57:56 +00:00
$reports[ $key ]['reports'] = $reports[ $key ]['charts'];
2012-08-14 12:21:34 +00:00
2013-07-08 11:57:56 +00:00
foreach ( $reports[ $key ]['reports'] as $report_key => $report ) {
if ( isset( $reports[ $key ]['reports'][ $report_key ]['function'] ) )
$reports[ $key ]['reports'][ $report_key ]['callback'] = $reports[ $key ]['reports'][ $report_key ]['function'];
2013-06-22 10:37:59 +00:00
}
2012-06-12 17:26:34 +00:00
}
2013-04-29 14:17:03 +00:00
2013-06-22 10:37:59 +00:00
return $reports;
}
2012-08-14 12:21:34 +00:00
2013-06-22 10:37:59 +00:00
/**
* Handles output of the reports page in admin.
*/
public function admin_page() {
$reports = $this->get_reports();
$first_tab = array_keys( $reports );
2013-06-25 13:59:20 +00:00
$current_tab = ! empty( $_GET['tab'] ) ? sanitize_title( urldecode( $_GET['tab'] ) ) : $first_tab[0];
$current_report = isset( $_GET['report'] ) ? sanitize_title( urldecode( $_GET['report'] ) ) : current( array_keys( $reports[ $current_tab ]['reports'] ) );
2012-08-14 12:21:34 +00:00
include_once( 'reports/class-wc-admin-report.php' );
include_once( 'views/html-admin-page-reports.php' );
2013-06-22 10:37:59 +00:00
}
2012-08-14 12:21:34 +00:00
2013-06-24 16:47:28 +00:00
/**
* Get a report from our reports subfolder
2013-06-24 16:47:28 +00:00
*/
public function get_report( $name ) {
$name = sanitize_title( str_replace( '_', '-', $name ) );
$class = 'WC_Report_' . str_replace( '-', '_', $name );
2013-06-25 13:59:20 +00:00
include_once( 'reports/class-wc-report-' . $name . '.php' );
if ( ! class_exists( $class ) )
return;
2012-08-14 12:21:34 +00:00
$report = new $class();
$report->output_report();
}
}
2012-08-14 12:21:34 +00:00
2013-07-10 11:05:45 +00:00
new WC_Admin_Reports();