PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.13.6
Independent Analytics – WordPress Analytics Plugin v2.13.6
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / WooCommerceOrderMetaBox / WooCommerceOrderMetaBox.php
independent-analytics / IAWP / WooCommerceOrderMetaBox Last commit date
WooCommerceOrderMetaBox.php 10 months ago
WooCommerceOrderMetaBox.php
124 lines
1 <?php
2
3 namespace IAWP\WooCommerceOrderMetaBox;
4
5 use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
6 use IAWPSCOPED\Carbon\CarbonImmutable;
7 use IAWP\Illuminate_Builder;
8 use IAWP\Tables;
9 use IAWP\Utils\Timezone;
10 use IAWPSCOPED\Illuminate\Database\Query\JoinClause;
11 /** @internal */
12 class WooCommerceOrderMetaBox
13 {
14 private int $order_id;
15 private ?object $record;
16 public function __construct(int $order_id)
17 {
18 $this->order_id = $order_id;
19 $this->record = $this->record();
20 }
21 public function add_meta_box() : void
22 {
23 if ($this->record === null) {
24 return;
25 }
26 \add_meta_box('iawp-wc-referrer-source', \esc_html__('Customer Journey', 'independent-analytics'), function () {
27 $this->render();
28 }, self::woocommerce_screen_id(), 'side');
29 }
30 private function render() : void
31 {
32 if ($this->record === null) {
33 return;
34 }
35 echo \IAWPSCOPED\iawp_render('woocommerce-order-meta-box', ['record' => $this->record]);
36 }
37 private function record() : ?object
38 {
39 $query = Illuminate_Builder::new()->select('initial_resources.cached_title AS initial_page_title', 'initial_resources.cached_url AS initial_page_url', 'sessions.total_views', 'sessions.created_at AS arrived_at', 'orders.created_at AS ordered_at', 'landing_pages.title AS landing_page_title', 'utm_sources.utm_source', 'utm_mediums.utm_medium', 'utm_campaigns.utm_campaign', 'utm_term', 'utm_content', 'referrers.referrer', 'referrers.domain AS referrer_domain', 'referrer_types.referrer_type', 'countries.country', 'countries.country_code', 'cities.city', 'device_types.device_type', 'device_oss.device_os', 'device_browsers.device_browser')->from(Tables::orders(), 'orders')->join(Tables::views() . ' AS views', function (JoinClause $join) {
40 $join->on('views.id', '=', 'orders.view_id');
41 })->join(Tables::sessions() . ' AS sessions', function (JoinClause $join) {
42 $join->on('sessions.session_id', '=', 'views.session_id');
43 })->leftJoin(Tables::campaigns() . ' AS campaigns', function (JoinClause $join) {
44 $join->on('sessions.campaign_id', '=', 'campaigns.campaign_id');
45 })->leftJoin(Tables::landing_pages() . ' AS landing_pages', function (JoinClause $join) {
46 $join->on('campaigns.landing_page_id', '=', 'landing_pages.id');
47 })->leftJoin(Tables::utm_sources() . ' AS utm_sources', function (JoinClause $join) {
48 $join->on('campaigns.utm_source_id', '=', 'utm_sources.id');
49 })->leftJoin(Tables::utm_mediums() . ' AS utm_mediums', function (JoinClause $join) {
50 $join->on('campaigns.utm_medium_id', '=', 'utm_mediums.id');
51 })->leftJoin(Tables::utm_campaigns() . ' AS utm_campaigns', function (JoinClause $join) {
52 $join->on('campaigns.utm_campaign_id', '=', 'utm_campaigns.id');
53 })->leftJoin(Tables::referrers() . ' AS referrers', function (JoinClause $join) {
54 $join->on('sessions.referrer_id', '=', 'referrers.id');
55 })->leftJoin(Tables::referrer_types() . ' AS referrer_types', function (JoinClause $join) {
56 $join->on('referrers.referrer_type_id', '=', 'referrer_types.id');
57 })->leftJoin(Tables::views() . ' AS initial_views', function (JoinClause $join) {
58 $join->on('sessions.initial_view_id', '=', 'initial_views.id');
59 })->leftJoin(Tables::resources() . ' AS initial_resources', function (JoinClause $join) {
60 $join->on('initial_views.resource_id', '=', 'initial_resources.id');
61 })->leftJoin(Tables::countries() . ' AS countries', function (JoinClause $join) {
62 $join->on('sessions.country_id', '=', 'countries.country_id');
63 })->leftJoin(Tables::cities() . ' AS cities', function (JoinClause $join) {
64 $join->on('sessions.city_id', '=', 'cities.city_id');
65 })->leftJoin(Tables::device_types() . ' AS device_types', function (JoinClause $join) {
66 $join->on('sessions.device_type_id', '=', 'device_types.device_type_id');
67 })->leftJoin(Tables::device_oss() . ' AS device_oss', function (JoinClause $join) {
68 $join->on('sessions.device_os_id', '=', 'device_oss.device_os_id');
69 })->leftJoin(Tables::device_browsers() . ' AS device_browsers', function (JoinClause $join) {
70 $join->on('sessions.device_browser_id', '=', 'device_browsers.device_browser_id');
71 })->where('orders.woocommerce_order_id', '=', $this->order_id);
72 $record = $query->get()->first();
73 if (!$record) {
74 return null;
75 }
76 if ($record->referrer_domain === "") {
77 $record->referrer_domain = null;
78 }
79 $record->arrived_at = $this->format_date($record->arrived_at);
80 $record->ordered_at = $this->format_date($record->ordered_at);
81 return $record;
82 }
83 private function format_date(string $value) : string
84 {
85 try {
86 $date = CarbonImmutable::parse($value)->setTimezone(Timezone::site_timezone());
87 $format = \IAWPSCOPED\iawp()->get_option('time_format', 'g:i a');
88 return $date->format($format);
89 } catch (\Throwable $e) {
90 return $value;
91 }
92 }
93 public static function register()
94 {
95 \add_action('add_meta_boxes', function ($post_type, $post) {
96 if (!\IAWPSCOPED\iawp()->is_woocommerce_support_enabled()) {
97 return;
98 }
99 $current_screen_id = \get_current_screen()->id ?? null;
100 if ($current_screen_id !== self::woocommerce_screen_id()) {
101 return;
102 }
103 $order_id = self::get_order_id($post);
104 $box = new self($order_id);
105 $box->add_meta_box();
106 }, 10, 2);
107 }
108 private static function get_order_id($post) : int
109 {
110 $order = $post;
111 if ($post instanceof \WP_Post) {
112 $order = wc_get_order($post->ID);
113 }
114 return $order->get_id();
115 }
116 private static function woocommerce_screen_id() : string
117 {
118 if (\class_exists('\\Automattic\\WooCommerce\\Internal\\DataStores\\Orders\\CustomOrdersTableController') && wc_get_container()->get(CustomOrdersTableController::class)->custom_orders_table_usage_is_enabled()) {
119 return wc_get_page_screen_id('shop-order');
120 }
121 return 'shop_order';
122 }
123 }
124