/woocommerce-location-report.php

  1. <?php 
  2. /** 
  3. * Plugin Name: WooCommerce Sales By Location Report 
  4. * Plugin URI: http://www.chuckmac.info 
  5. * Description: WooCommerce report to visualize sales by location. 
  6. * Author: Chuck Mac 
  7. * Author URI: http://www.chuckmac.info 
  8. * Version: 1.1 
  9. * Text Domain: wc_location_report 
  10. * Domain Path: /languages/ 
  11. * 
  12. * Copyright: (c) 2014 ChuckMac Development LLC 
  13. * 
  14. * License: GNU General Public License v3.0 
  15. * License URI: http://www.gnu.org/licenses/gpl-3.0.html 
  16. * 
  17. * @package WC-Location-Report 
  18. * @author WooThemes 
  19. * @category Reports 
  20. * @copyright Copyright (c) 2014, ChuckMac Development LLC 
  21. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 
  22. */ 
  23.  
  24. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly 
  25.  
  26. // Check if WooCommerce is active 
  27. if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { 
  28. return; 
  29.  
  30.  
  31. /** 
  32. * # WooCommerce Location Report Main Plugin Class 
  33. * 
  34. * ## Plugin Overview 
  35. * 
  36. * This plugin adds a new section in the WooCommerce Reports -> Orders area called 'Sales by location'. 
  37. * The report visualizes the customer purchases by location into a Choropleth map to show where the orders 
  38. * are being placed. 
  39. * 
  40. * This plugin utilizes jVectorMap (http://jvectormap.com) for its map functions. 
  41. * 
  42. */ 
  43. class WC_Location_Report { 
  44.  
  45. /** plugin version number */ 
  46. public static $version = '1.1'; 
  47.  
  48. /** @var string the plugin file */ 
  49. public static $plugin_file = __FILE__; 
  50.  
  51. /** @var string the plugin file */ 
  52. public static $plugin_dir; 
  53.  
  54.  
  55. /** 
  56. * Initializes the plugin 
  57. * 
  58. * @since 1.0 
  59. */ 
  60. public function init() { 
  61.  
  62. global $wpdb; 
  63.  
  64. self::$plugin_dir = dirname( __FILE__ ); 
  65.  
  66. // Add any necessary css / scripts 
  67. add_action( 'admin_enqueue_scripts', __CLASS__ . '::location_report_admin_css_scripts' ); 
  68.  
  69. // Add the reports layout to the WooCommerce -> Reports admin section 
  70. add_filter( 'woocommerce_admin_reports', __CLASS__ . '::initialize_location_admin_report', 12, 1 ); 
  71.  
  72. // Add the path to the report class so WooCommerce can parse it 
  73. add_filter( 'wc_admin_reports_path', __CLASS__ . '::initialize_location_admin_reports_path', 12, 3 ); 
  74.  
  75. // Load translation files 
  76. add_action( 'plugins_loaded', __CLASS__ . '::load_plugin_textdomain' ); 
  77.  
  78.  
  79.  
  80. /** 
  81. * Add any location report javascript & css to the admin pages. Only  
  82. * add it to our specific report areas. 
  83. * 
  84. * @since 1.0 
  85. */ 
  86. public static function location_report_admin_css_scripts() { 
  87.  
  88. $wc_screen_id = sanitize_title( __( 'WooCommerce', 'woocommerce' ) ); 
  89. $screen = get_current_screen(); 
  90.  
  91. if ( in_array( $screen->id, apply_filters( 'woocommerce_geo_reports_screen_ids', array( $wc_screen_id . '_page_wc-reports' ) ) ) && isset( $_REQUEST['report'] ) && in_array($_REQUEST['report'], apply_filters( 'woocommerce_geo_reports_report_ids', array( 'sales_by_location' ) )) ) { 
  92.  
  93. //jVector includes - needs to be done in the footer so we can localize data as part of the report generation 
  94. wp_enqueue_script( 'jvectormap', plugins_url( '/lib/jquery-jvectormap-1.2.2.min.js', self::$plugin_file ), array( 'jquery' ), self::$version, true ); 
  95. wp_enqueue_script( 'jvectormap-world', plugins_url( '/lib/map-data/jquery-jvectormap-world-mill-en', self::$plugin_file ), array( 'jquery', 'jvectormap' ), self::$version, true ); 
  96.  
  97. //jVector css 
  98. wp_enqueue_style( 'jvectormap', plugins_url( '/lib/jquery-jvectormap-1.2.2.css', self::$plugin_file ), array( 'woocommerce_admin_styles' ), self::$version ); 
  99.  
  100.  
  101.  
  102.  
  103. /** 
  104. * Add our location report to the WooCommerce order reports array. 
  105. * 
  106. * @param array Array of All Report types & their labels 
  107. * @return array Array of All Report types & their labels, including the 'Sales by location' report. 
  108. * @since 1.0 
  109. */ 
  110. public static function initialize_location_admin_report ( $report ) { 
  111.  
  112. $report['orders']['reports']['sales_by_location'] = array ( 
  113. 'title' => __( 'Sales by location', 'woocommerce-location-report' ),  
  114. 'description' => '',  
  115. 'hide_title' => true,  
  116. 'callback' => array( 'WC_Admin_Reports', 'get_report' ) 
  117. ); 
  118.  
  119. return $report; 
  120.  
  121.  
  122.  
  123. /** 
  124. * If we hit one of our reports in the WC get_report function, change the path to our dir. 
  125. * 
  126. * @param array Array of Report types & their labels 
  127. * @return array Array of Report types & their labels, including the Subscription product type. 
  128. * @since 1.0 
  129. */ 
  130. public static function initialize_location_admin_reports_path( $report_path, $name, $class ) { 
  131.  
  132. if ( 'WC_Report_sales_by_location' == $class ) { 
  133. $report_path = self::$plugin_dir . '/classes/class-wc-report-' . $name . '.php'; 
  134.  
  135. return $report_path; 
  136.  
  137.  
  138.  
  139. /** 
  140. * Load our language settings for internationalization 
  141. * 
  142. * @since 1.0 
  143. */ 
  144. public static function load_plugin_textdomain( ) { 
  145.  
  146. load_plugin_textdomain( 'woocommerce-location-report', false, basename( self::$plugin_dir ) . '/languages' ); 
  147.  
  148.  
  149. } // end \WC_Location_Report class 
  150.  
  151.  
  152. WC_Location_Report::init(); 
.