EDD_Order.php
1 year ago
PMPro_Order.php
1 year ago
SureCart_Event_Sync_Job.php
1 year ago
SureCart_Order.php
1 year ago
SureCart_Store.php
1 year ago
WooCommerce_Order.php
1 year ago
WooCommerce_Referrer_Meta_Box.php
1 year ago
WooCommerce_Status_Manager.php
1 year ago
WooCommerce_Referrer_Meta_Box.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Ecommerce; |
| 4 | |
| 5 | use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; |
| 6 | use IAWP\Illuminate_Builder; |
| 7 | use IAWP\Models\Campaign; |
| 8 | use IAWP\Models\Referrer; |
| 9 | use IAWP\Query; |
| 10 | use IAWP\Utils\Security; |
| 11 | use IAWPSCOPED\Illuminate\Database\Query\JoinClause; |
| 12 | /** @internal */ |
| 13 | class WooCommerce_Referrer_Meta_Box |
| 14 | { |
| 15 | private $referrer; |
| 16 | private $campaign; |
| 17 | public function __construct() |
| 18 | { |
| 19 | \add_action('add_meta_boxes', [$this, 'maybe_add_meta_box'], 10, 2); |
| 20 | } |
| 21 | public function maybe_add_meta_box($post_type, $post_or_order_object) : void |
| 22 | { |
| 23 | if (!\IAWPSCOPED\iawp()->is_woocommerce_support_enabled()) { |
| 24 | return; |
| 25 | } |
| 26 | $order = $post_or_order_object instanceof \WP_Post ? wc_get_order($post_or_order_object->ID) : $post_or_order_object; |
| 27 | if ($order === \false) { |
| 28 | return; |
| 29 | } |
| 30 | $woocommerce_screen = \class_exists('\\Automattic\\WooCommerce\\Internal\\DataStores\\Orders\\CustomOrdersTableController') && wc_get_container()->get(CustomOrdersTableController::class)->custom_orders_table_usage_is_enabled() ? wc_get_page_screen_id('shop-order') : 'shop_order'; |
| 31 | $current_screen = \get_current_screen(); |
| 32 | if (\is_null($current_screen) || $current_screen->id !== $woocommerce_screen) { |
| 33 | return; |
| 34 | } |
| 35 | $referrer_row = $this->get_referrer_for($order->get_id()); |
| 36 | $this->referrer = \is_object($referrer_row) ? new Referrer($referrer_row) : null; |
| 37 | $campaign_row = $this->get_campaign_for($order->get_id()); |
| 38 | $this->campaign = \is_object($campaign_row) ? new Campaign($campaign_row) : null; |
| 39 | if (\is_null($this->referrer) && \is_null($this->campaign)) { |
| 40 | return; |
| 41 | } |
| 42 | \add_meta_box('iawp-wc-referrer-source', \esc_html__('Order Referrer', 'independent-analytics'), [$this, 'render_meta_box_content'], $woocommerce_screen, 'side'); |
| 43 | } |
| 44 | public function render_meta_box_content($post_or_order_object) : void |
| 45 | { |
| 46 | $order = $post_or_order_object instanceof \WP_Post ? wc_get_order($post_or_order_object->ID) : $post_or_order_object; |
| 47 | if (!\is_null($this->referrer)) { |
| 48 | if ($this->referrer->has_link()) { |
| 49 | $content = '<a target="_blank" href="' . \esc_url($this->referrer->referrer_url()) . '">' . Security::string($this->referrer->referrer()) . '</a>'; |
| 50 | } else { |
| 51 | $content = Security::string($this->referrer->referrer()); |
| 52 | } |
| 53 | echo '<p><strong>' . \esc_html__('Referrer:', 'independent-analytics') . '</strong> <span data-testid="referrer">' . $content . '</span> <a class="info-link" |
| 54 | href="https://independentwp.com/knowledgebase/woocommerce/order-referrers-box/" |
| 55 | target="_blank" |
| 56 | style="text-decoration:none;float:right;"> |
| 57 | <span class="dashicons dashicons-editor-help"></span> |
| 58 | </a> |
| 59 | </p>'; |
| 60 | } |
| 61 | if (!\is_null($this->campaign)) { |
| 62 | if (\is_string($this->campaign->utm_source())) { |
| 63 | echo '<p><strong>' . \__('Campaign Source', 'independent-analytics') . ':</strong> <span data-testid="source">' . Security::string($this->campaign->utm_source()) . '</span> </p>'; |
| 64 | } |
| 65 | if (\is_string($this->campaign->utm_medium())) { |
| 66 | echo '<p><strong>' . \__('Campaign Medium', 'independent-analytics') . ':</strong> <span data-testid="medium">' . Security::string($this->campaign->utm_medium()) . '</span> </p>'; |
| 67 | } |
| 68 | if (\is_string($this->campaign->utm_campaign())) { |
| 69 | echo '<p><strong>' . \__('Campaign Name', 'independent-analytics') . ':</strong> <span data-testid="campaign">' . Security::string($this->campaign->utm_campaign()) . '</span> </p>'; |
| 70 | } |
| 71 | if (\is_string($this->campaign->utm_term())) { |
| 72 | echo '<p><strong>' . \__('Campaign Term', 'independent-analytics') . ':</strong> <span data-testid="term">' . Security::string($this->campaign->utm_term()) . '</span> </p>'; |
| 73 | } |
| 74 | if (\is_string($this->campaign->utm_content())) { |
| 75 | echo '<p><strong>' . \__('Campaign Content', 'independent-analytics') . ':</strong> <span data-testid="content">' . Security::string($this->campaign->utm_content()) . '</span> </p>'; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | private function get_campaign_for($order_id) : ?object |
| 80 | { |
| 81 | $orders_table = Query::get_table_name(Query::ORDERS); |
| 82 | $campaigns_table = Query::get_table_name(Query::CAMPAIGNS); |
| 83 | $views_table = Query::get_table_name(Query::VIEWS); |
| 84 | $sessions_table = Query::get_table_name(Query::SESSIONS); |
| 85 | $campaign_query = Illuminate_Builder::new()->select("landing_page_title AS title", "utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content")->from($orders_table, 'orders')->join("{$views_table} AS views", function (JoinClause $join) { |
| 86 | $join->on('views.id', '=', 'orders.view_id'); |
| 87 | })->join("{$sessions_table} AS sessions", function (JoinClause $join) { |
| 88 | $join->on('sessions.session_id', '=', 'views.session_id'); |
| 89 | })->join("{$campaigns_table} AS campaigns", function (JoinClause $join) { |
| 90 | $join->on('sessions.campaign_id', '=', 'campaigns.campaign_id'); |
| 91 | })->where('orders.woocommerce_order_id', '=', $order_id); |
| 92 | $record = $campaign_query->get()->first(); |
| 93 | return $record; |
| 94 | } |
| 95 | private function get_referrer_for($order_id) : ?object |
| 96 | { |
| 97 | $orders_table = Query::get_table_name(Query::ORDERS); |
| 98 | $views_table = Query::get_table_name(Query::VIEWS); |
| 99 | $sessions_table = Query::get_table_name(Query::SESSIONS); |
| 100 | $referrer_table = Query::get_table_name(Query::REFERRERS); |
| 101 | $referrer_query = Illuminate_Builder::new()->select('sessions.referrer_id', 'referrer', 'type AS referrer_type', 'domain')->from($orders_table, 'orders')->join("{$views_table} AS views", function (JoinClause $join) { |
| 102 | $join->on('views.id', '=', 'orders.view_id'); |
| 103 | })->join("{$sessions_table} AS sessions", function (JoinClause $join) { |
| 104 | $join->on('sessions.session_id', '=', 'views.session_id'); |
| 105 | })->join("{$referrer_table} AS referrers", function (JoinClause $join) { |
| 106 | $join->on('referrers.id', '=', 'sessions.referrer_id'); |
| 107 | })->where('orders.woocommerce_order_id', '=', $order_id); |
| 108 | $record = $referrer_query->get()->first(); |
| 109 | return $record; |
| 110 | } |
| 111 | } |
| 112 |