| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Admin\Components; |
| 4 |
|
| 5 |
use Sellkit\Admin\Components\Analytics\Discount\summary; |
| 6 |
|
| 7 |
defined( 'ABSPATH' ) || die(); |
| 8 |
|
| 9 |
/** |
| 10 |
* Components class. |
| 11 |
* |
| 12 |
* @since 1.1.0 |
| 13 |
*/ |
| 14 |
class Analytics { |
| 15 |
|
| 16 |
/** |
| 17 |
* Class instance. |
| 18 |
* |
| 19 |
* @since 1.1.0 |
| 20 |
* @var Analytics |
| 21 |
*/ |
| 22 |
private static $instance = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Class instance. |
| 26 |
* |
| 27 |
* @since 1.1.0 |
| 28 |
* @var $date_range |
| 29 |
*/ |
| 30 |
public static $date_range; |
| 31 |
|
| 32 |
/** |
| 33 |
* Get a class instance. |
| 34 |
* |
| 35 |
* @since 1.1.0 |
| 36 |
* |
| 37 |
* @return Analytics Class instance. |
| 38 |
*/ |
| 39 |
public static function get_instance() { |
| 40 |
if ( null === self::$instance ) { |
| 41 |
self::$instance = new self(); |
| 42 |
} |
| 43 |
|
| 44 |
return self::$instance; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Class constructor. |
| 49 |
* |
| 50 |
* @since 1.1.0 |
| 51 |
*/ |
| 52 |
public function __construct() { |
| 53 |
self::$date_range = 7; |
| 54 |
|
| 55 |
add_action( 'wp_ajax_sellkit_get_chart_data', [ $this, 'get_chart_data' ] ); |
| 56 |
add_action( 'wp_ajax_sellkit_get_summary_chart_data', [ $this, 'get_summary' ] ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Gets analytics data. |
| 61 |
* |
| 62 |
* @since 1.1.0 |
| 63 |
*/ |
| 64 |
public function get_chart_data() { |
| 65 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 66 |
|
| 67 |
$feature = sellkit_htmlspecialchars( INPUT_GET, 'feature' ); |
| 68 |
$class = sellkit_htmlspecialchars( INPUT_GET, 'type' ); |
| 69 |
$date_range = sellkit_htmlspecialchars( INPUT_GET, 'date_range' ); |
| 70 |
$target_id = sellkit_htmlspecialchars( INPUT_GET, 'target_id' ); |
| 71 |
|
| 72 |
if ( 'thirty-days' === $date_range ) { |
| 73 |
self::$date_range = 30; |
| 74 |
} |
| 75 |
|
| 76 |
$file_name = str_replace( '_', '-', $class ); |
| 77 |
|
| 78 |
sellkit()->load_files( [ |
| 79 |
'admin/components/analytics/class', |
| 80 |
"admin/components/analytics/{$feature}/base", |
| 81 |
"admin/components/analytics/{$feature}/{$file_name}", |
| 82 |
] ); |
| 83 |
|
| 84 |
$feature = ucfirst( $feature ); |
| 85 |
$class = __NAMESPACE__ . "\Analytics\\$feature\\" . $class; |
| 86 |
|
| 87 |
$analytics = new $class( $target_id ); |
| 88 |
|
| 89 |
wp_send_json_success( $analytics->get_data() ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Gets summary data. |
| 94 |
* |
| 95 |
* @since 1.1.0 |
| 96 |
*/ |
| 97 |
public function get_summary() { |
| 98 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 99 |
|
| 100 |
$feature = sellkit_htmlspecialchars( INPUT_GET, 'feature' ); |
| 101 |
$date_range = sellkit_htmlspecialchars( INPUT_GET, 'date_range' ); |
| 102 |
|
| 103 |
if ( 'thirty-days' === $date_range ) { |
| 104 |
self::$date_range = 30; |
| 105 |
} |
| 106 |
|
| 107 |
sellkit()->load_files( [ "admin/components/analytics/{$feature}/summary" ] ); |
| 108 |
|
| 109 |
$class = __NAMESPACE__ . "\Analytics\\$feature\\summary"; |
| 110 |
|
| 111 |
wp_send_json_success( $class::get_instance()->data ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* The condition which is related to the target id. |
| 116 |
* |
| 117 |
* @since 1.1.0 |
| 118 |
* @return string |
| 119 |
* @param int $target_id Target id. |
| 120 |
*/ |
| 121 |
public static function target_id_condition( $target_id ) { |
| 122 |
$target_id_condition = '='; |
| 123 |
|
| 124 |
if ( empty( $target_id ) ) { |
| 125 |
$target_id_condition = '!='; |
| 126 |
} |
| 127 |
|
| 128 |
return $target_id_condition; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
Analytics::get_instance(); |
| 133 |
|