PNG %k25u25%fgd5n! */ class Statistic { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Statistic value * @since 1.0.0 * @access protected * @param array */ protected $statistic = []; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Display statistic widgets * Hooked via action wp_dashboard_setup * @since 1.0.0 * @return void */ public function display_statistic_widgets() { if(current_user_can('manage_sejoli_orders')) : wp_add_dashboard_widget('sejoli-all-time-omset', __('Top 10 Omset Produk Sepanjang Waktu', 'sejoli'), [$this, 'display_all_time_top_omset']); wp_add_dashboard_widget('sejoli-monthly-omset', sprintf( __('Top 10 Omset Produk Bulan %s', 'sejoli'), date('M Y') ), [$this, 'display_monthly_top_omset']); wp_add_dashboard_widget('sejoli-onemonthago-omset', sprintf( __('Top 10 Omset Produk Bulan %s', 'sejoli'), date('M Y', strtotime('first day of -1 month')) ), [$this, 'display_monthly_top_omset']); wp_add_dashboard_widget('sejoli-twomonthsago-omset', sprintf( __('Top 10 Omset Produk Bulan %s', 'sejoli'), date('M Y', strtotime('first day of -2 months')) ), [$this, 'display_monthly_top_omset']); wp_add_dashboard_widget('sejoli-all-time-product', __('Top 10 Produk Sepanjang Waktu', 'sejoli'), [$this, 'display_all_time_top_product']); wp_add_dashboard_widget('sejoli-monthly-product', sprintf( __('Top 10 Produk Bulan %s', 'sejoli'), date('M Y') ), [$this, 'display_monthly_top_product']); wp_add_dashboard_widget('sejoli-onemonthago-product', sprintf( __('Top 10 Produk Bulan %s', 'sejoli'), date('M Y', strtotime('first day of -1 month')) ), [$this, 'display_monthly_top_product']); wp_add_dashboard_widget('sejoli-twomonthsago-product', sprintf( __('Top 10 Produk Bulan %s', 'sejoli'), date('M Y', strtotime('first day of -2 months')) ), [$this, 'display_monthly_top_product']); wp_add_dashboard_widget('sejoli-all-time-commission', __('Top 10 Affiliasi Sepanjang Waktu', 'sejoli'), [$this, 'display_all_time_top_commission']); wp_add_dashboard_widget('sejoli-monthly-commission', sprintf( __('Top 10 Affiliasi Bulan %s', 'sejoli'), date('M Y') ), [$this, 'display_monthly_top_commission']); wp_add_dashboard_widget('sejoli-onemonthago-commission', sprintf( __('Top 10 Affiliasi Bulan %s', 'sejoli'), date('M Y', strtotime('first day of -1 month')) ), [$this, 'display_monthly_top_commission']); wp_add_dashboard_widget('sejoli-twomonthsago-commission', sprintf( __('Top 10 Affiliasi Bulan %s', 'sejoli'), date('M Y', strtotime('first day of -2 months')) ), [$this, 'display_monthly_top_commission']); endif; } /** * Set CSS and JS for statistic data menegement * Hooked via admin_enqueue_scripts, priority 100 * @since 1.0.0 * @return void */ public function set_css_and_js() { global $pagenow; if( 'index.php' === $pagenow && current_user_can('manage_sejoli_orders') ) : wp_enqueue_style( 'daterangepicker'); wp_enqueue_style( $this->plugin_name . '-widgets', SEJOLISA_URL . 'admin/css/widgets.css'); wp_enqueue_script( $this->plugin_name . '-widgets', SEJOLISA_URL . 'admin/js/widgets.js', ['jquery', 'js-render'], $this->version, true); wp_enqueue_script( 'chartjs' ); wp_enqueue_script( 'daterangepicker'); wp_localize_script( $this->plugin_name . '-widgets', 'sejoli_widgets', [ 'text' => [ 'loading' => '
'. __('Mengambil data...', 'sejoli'). '
' ], 'all_time_omset' => [ 'url' => add_query_arg([ 'action' => 'sejoli-statistic-product', 'nonce' => wp_create_nonce('sejoli-statistic-get-product-data'), ],admin_url('admin-ajax.php') ), 'data' => [ 'calculate' => 'omset', 'order_status' => [ 'in-progress', 'shipping', 'completed' ] ], ], 'monthly_omset' => [ 'url' => add_query_arg([ 'action' => 'sejoli-statistic-product', 'nonce' => wp_create_nonce('sejoli-statistic-get-product-data'), ],admin_url('admin-ajax.php') ), 'data' => [ 'start_date' => date('Y-m-01'), 'end_date' => date('Y-m-t'), 'calculate' => 'omset', 'order_status' => [ 'in-progress', 'shipping', 'completed' ] ], ], 'onemonthago_omset' => [ 'url' => add_query_arg([ 'action' => 'sejoli-statistic-product', 'nonce' => wp_create_nonce('sejoli-statistic-get-product-data'), ],admin_url('admin-ajax.php') ), 'data' => [ 'start_date' => date('Y-m-01', strtotime('first day of -1 month') ), 'end_date' => date('Y-m-t', strtotime('first day of -1 month') ), 'calculate' => 'omset', 'order_status' => [ 'in-progress', 'shipping', 'completed' ] ], ], 'twomonthsago_omset' => [ 'url' => add_query_arg([ 'action' => 'sejoli-statistic-product', 'nonce' => wp_create_nonce('sejoli-statistic-get-product-data'), ],admin_url('admin-ajax.php') ), 'data' => [ 'start_date' => date('Y-m-01', strtotime('first day of -2 months')), 'end_date' => date('Y-m-t', strtotime('first day of -2 months')), 'calculate' => 'omset', 'order_status' => [ 'in-progress', 'shipping', 'completed' ] ], ], 'all_time_product' => [ 'url' => add_query_arg([ 'action' => 'sejoli-statistic-product', 'nonce' => wp_create_nonce('sejoli-statistic-get-product-data'), ],admin_url('admin-ajax.php') ), 'data' => [ 'calculate' => 'quantity', 'order_status' => [ 'in-progress', 'shipping', 'completed' ] ], ], 'monthly_product' => [ 'url' => add_query_arg([ 'action' => 'sejoli-statistic-product', 'nonce' => wp_create_nonce('sejoli-statistic-get-product-data'), ],admin_url('admin-ajax.php') ), 'data' => [ 'start_date' => date('Y-m-01'), 'end_date' => date('Y-m-t'), 'calculate' => 'quantity', 'order_status' => [ 'in-progress', 'shipping', 'completed' ] ], ], 'onemonthago_product' => [ 'url' => add_query_arg([ 'action' => 'sejoli-statistic-product', 'nonce' => wp_create_nonce('sejoli-statistic-get-product-data'), ],admin_url('admin-ajax.php') ), 'data' => [ 'start_date' => date('Y-m-01', strtotime('first day of -1 month') ), 'end_date' => date('Y-m-t', strtotime('first day of -1 month') ), 'calculate' => 'quantity', 'order_status' => [ 'in-progress', 'shipping', 'completed' ] ], ], 'twomonthsago_product' => [ 'url' => add_query_arg([ 'action' => 'sejoli-statistic-product', 'nonce' => wp_create_nonce('sejoli-statistic-get-product-data'), ],admin_url('admin-ajax.php') ), 'data' => [ 'start_date' => date('Y-m-01', strtotime('first day of -2 months')), 'end_date' => date('Y-m-t', strtotime('first day of -2 months')), 'calculate' => 'quantity', 'order_status' => [ 'in-progress', 'shipping', 'completed' ] ], ], 'all_time_commission' => [ 'url' => add_query_arg([ 'action' => 'sejoli-statistic-commission', 'nonce' => wp_create_nonce('sejoli-statistic-get-commission-data'), ],admin_url('admin-ajax.php') ), 'data' => [ 'calculate' => 'total', 'commission_status' => [ 'added' ], ] ], 'monthly_commission' => [ 'url' => add_query_arg([ 'action' => 'sejoli-statistic-commission', 'nonce' => wp_create_nonce('sejoli-statistic-get-commission-data'), ],admin_url('admin-ajax.php') ), 'data' => [ 'start_date' => date('Y-m-01'), 'end_date' => date('Y-m-t'), 'calculate' => 'total', 'commission_status' => [ 'added' ], ] ], 'onemonthago_commission' => [ 'url' => add_query_arg([ 'action' => 'sejoli-statistic-commission', 'nonce' => wp_create_nonce('sejoli-statistic-get-commission-data'), ],admin_url('admin-ajax.php') ), 'data' => [ 'start_date' => date('Y-m-01', strtotime('first day of -1 month') ), 'end_date' => date('Y-m-t', strtotime('first day of -1 month') ), 'calculate' => 'total', 'commission_status' => [ 'added' ], ] ], 'twomonthsago_commission' => [ 'url' => add_query_arg([ 'action' => 'sejoli-statistic-commission', 'nonce' => wp_create_nonce('sejoli-statistic-get-commission-data'), ],admin_url('admin-ajax.php') ), 'data' => [ 'start_date' => date('Y-m-01', strtotime('-2 month')), 'end_date' => date('Y-m-t', strtotime('-2 month')), 'calculate' => 'total', 'commission_status' => [ 'added' ], ] ] ]); endif; if( 'edit.php' !== $pagenow ||!isset($_GET['post_type']) ||'sejoli-product' !== $_GET['post_type'] ) : return; endif; wp_enqueue_style( 'semantic-ui' ); } /** * Check all product in current product management page * Hooked via load-edit.php, prirority 100; * @return void */ public function check_product_page() { global $wp_query, $pagenow; if('edit.php' !== $pagenow ||!isset($_GET['post_type']) ||'sejoli-product' !== $_GET['post_type']) : return; endif; $product_ids = []; foreach( (array) $wp_query->posts as $product ) : $product_ids[] = $product->ID; endforeach; $temp = []; $total_order = sejolisa_get_product_statistic([ 'product_id' => $product_ids, 'order_status' => [ 'completed', 'in-progress', 'shipping' ] ]); foreach( $total_order['statistic'] as $data) : $temp[$data->ID] = [ 'product_nmme' => $data->product_name, 'total' => $data->total ]; endforeach; $this->statistic['total_order'] = $temp; $temp = []; $total_omset = sejolisa_get_product_statistic([ 'product_id' => $product_ids, 'calculate' => 'omset', 'order_status' => [ 'completed', 'in-progress', 'shipping' ] ]); foreach( $total_omset['statistic'] as $data) : $temp[$data->ID] = [ 'product_nmme' => $data->product_name, 'total' => $data->total ]; endforeach; $this->statistic['total_omset'] = $temp; $temp = []; $total_quantity = sejolisa_get_product_statistic([ 'product_id' => $product_ids, 'calculate' => 'quantity', 'order_status' => [ 'completed', 'in-progress', 'shipping' ] ]); foreach( $total_quantity['statistic'] as $data) : $temp[$data->ID] = [ 'product_nmme' => $data->product_name, 'total' => $data->total ]; endforeach; $this->statistic['total_quantity'] = $temp; } /** * Add statistic column to product columns * Hooked via filter manage_sejoli-product_posts_columns, priority 100 * @since 1.0.0 * @param array $columns * @return array */ public function add_product_columns(array $columns) { unset($columns['date']); $columns['sejoli-statistic'] = __('Statistik', 'sejoli'); return $columns; } /** * Display statistic data to product column * Hooked via manage_posts_custom_column, priority 100 * @since 1.0.0 * @param string $column * @param integer $post_id * @return void */ public function display_product_statistic_data($column, $post_id) { switch ( $column ) : case 'sejoli-statistic' : $statistic = [ 'total_order' => isset($this->statistic['total_order'][$post_id]) ? $this->statistic['total_order'][$post_id]['total'] : 0, 'total_omset' => isset($this->statistic['total_omset'][$post_id]) ? $this->statistic['total_omset'][$post_id]['total'] : 0, 'total_quantity' => isset($this->statistic['total_quantity'][$post_id]) ? $this->statistic['total_quantity'][$post_id]['total'] : 0 ]; ?>