AJAX
2 years ago
Admin_Page
2 years ago
Date_Range
2 years ago
Interval
2 years ago
Menu_Bar_Stats
2 years ago
Migrations
2 years ago
Models
2 years ago
Public_API
2 years ago
Rows
2 years ago
Statistics
2 years ago
Tables
2 years ago
Utils
2 years ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
2 years ago
Chart.php
2 years ago
Chart_Geo.php
2 years ago
Cron_Manager.php
2 years ago
Current_Traffic_Finder.php
2 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
2 years ago
Database_Manager.php
2 years ago
Email_Chart.php
2 years ago
Email_Reports.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
2 years ago
Filters.php
2 years ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.php
2 years ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
2 years ago
Independent_Analytics.php
2 years ago
Interrupt.php
2 years ago
Known_Referrers.php
2 years ago
Plugin_Conflict_Detector.php
2 years ago
Query.php
2 years ago
Quick_Stats.php
2 years ago
REST_API.php
2 years ago
Real_Time.php
2 years ago
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
2 years ago
Resource_Identifier.php
2 years ago
Settings.php
2 years ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
2 years ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
WooCommerce_Order.php
2 years ago
WooCommerce_Referrer_Meta_Box.php
2 years ago
WooCommerce_Referrer_Meta_Box.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP; |
| 4 | |
| 5 | use IAWP_SCOPED\IAWP\Models\Campaign; |
| 6 | use IAWP_SCOPED\IAWP\Models\Referrer; |
| 7 | use IAWP_SCOPED\IAWP\Utils\Security; |
| 8 | use IAWP_SCOPED\Illuminate\Database\Query\JoinClause; |
| 9 | /** @internal */ |
| 10 | class WooCommerce_Referrer_Meta_Box |
| 11 | { |
| 12 | public function __construct() |
| 13 | { |
| 14 | \add_action('add_meta_boxes_shop_order', [$this, 'maybe_add_meta_box'], 10, 1); |
| 15 | } |
| 16 | public function maybe_add_meta_box(\WP_Post $post) : void |
| 17 | { |
| 18 | if (\is_null($this->get_referrer_for($post))) { |
| 19 | return; |
| 20 | } |
| 21 | \add_meta_box('iawp-wc-referrer-source', \esc_html__('Order Referrer', 'independent-analytics'), [$this, 'render_meta_box_content'], 'shop_order', 'side'); |
| 22 | } |
| 23 | public function render_meta_box_content(\WP_Post $post) : void |
| 24 | { |
| 25 | $referrer_row = $this->get_referrer_for($post); |
| 26 | $campaign_row = $this->get_campaign_for($post); |
| 27 | $referrer_row->referrer_ids = !\is_null($referrer_row->referrer_id) ? [$referrer_row->referrer_id] : null; |
| 28 | $referrer = new Referrer($referrer_row); |
| 29 | $campaign = new Campaign($campaign_row); |
| 30 | if ($referrer->has_link()) { |
| 31 | $content = '<a target="_blank" href="' . \esc_url($referrer->referrer_url()) . '">' . Security::string($referrer->referrer()) . '</a>'; |
| 32 | } else { |
| 33 | $content = Security::string($referrer->referrer()); |
| 34 | } |
| 35 | echo '<p><strong>' . \esc_html__('Referrer:', 'independent-analytics') . '</strong> <span data-testid="referrer">' . $content . '</span> <a class="info-link" |
| 36 | href="https://independentwp.com/knowledgebase/woocommerce/order-referrers-box/" |
| 37 | target="_blank" |
| 38 | style="text-decoration:none;float:right;"> |
| 39 | <span class="dashicons dashicons-editor-help"></span> |
| 40 | </a> |
| 41 | </p>'; |
| 42 | if (\is_string($campaign->utm_source())) { |
| 43 | echo '<p><strong>' . \__('Campaign Source', 'independent-analytics') . ':</strong> <span data-testid="source">' . Security::string($campaign->utm_source()) . '</span> </p>'; |
| 44 | } |
| 45 | if (\is_string($campaign->utm_medium())) { |
| 46 | echo '<p><strong>' . \__('Campaign Medium', 'independent-analytics') . ':</strong> <span data-testid="medium">' . Security::string($campaign->utm_medium()) . '</span> </p>'; |
| 47 | } |
| 48 | if (\is_string($campaign->utm_campaign())) { |
| 49 | echo '<p><strong>' . \__('Campaign Name', 'independent-analytics') . ':</strong> <span data-testid="campaign">' . Security::string($campaign->utm_campaign()) . '</span> </p>'; |
| 50 | } |
| 51 | if (\is_string($campaign->utm_term())) { |
| 52 | echo '<p><strong>' . \__('Campaign Term', 'independent-analytics') . ':</strong> <span data-testid="term">' . Security::string($campaign->utm_term()) . '</span> </p>'; |
| 53 | } |
| 54 | if (\is_string($campaign->utm_content())) { |
| 55 | echo '<p><strong>' . \__('Campaign Content', 'independent-analytics') . ':</strong> <span data-testid="content">' . Security::string($campaign->utm_content()) . '</span> </p>'; |
| 56 | } |
| 57 | } |
| 58 | private function get_campaign_for(\WP_Post $post) : ?object |
| 59 | { |
| 60 | $wc_orders_table = Query::get_table_name(Query::WC_ORDERS); |
| 61 | $campaigns_table = Query::get_table_name(Query::CAMPAIGNS); |
| 62 | $views_table = Query::get_table_name(Query::VIEWS); |
| 63 | $sessions_table = Query::get_table_name(Query::SESSIONS); |
| 64 | $referrer_query = Illuminate_Builder::get_builder(); |
| 65 | $referrer_query->select("landing_page_title AS title", "utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content")->from($wc_orders_table, 'orders')->leftJoin("{$views_table} AS views", function (JoinClause $join) { |
| 66 | $join->on('views.id', '=', 'orders.view_id'); |
| 67 | })->leftJoin("{$sessions_table} AS sessions", function (JoinClause $join) { |
| 68 | $join->on('sessions.session_id', '=', 'views.session_id'); |
| 69 | })->leftJoin("{$campaigns_table} AS campaigns", function (JoinClause $join) { |
| 70 | $join->on('sessions.campaign_id', '=', 'campaigns.campaign_id'); |
| 71 | })->where('orders.order_id', '=', $post->ID); |
| 72 | $record = $referrer_query->get()->first(); |
| 73 | return $record; |
| 74 | } |
| 75 | private function get_referrer_for(\WP_Post $post) : ?object |
| 76 | { |
| 77 | $wc_orders_table = Query::get_table_name(Query::WC_ORDERS); |
| 78 | $views_table = Query::get_table_name(Query::VIEWS); |
| 79 | $sessions_table = Query::get_table_name(Query::SESSIONS); |
| 80 | $referrer_table = Query::get_table_name(Query::REFERRERS); |
| 81 | $referrer_query = Illuminate_Builder::get_builder(); |
| 82 | $referrer_query->select('sessions.referrer_id', 'referrer', 'type AS referrer_type', 'domain')->from($wc_orders_table, 'orders')->leftJoin("{$views_table} AS views", function (JoinClause $join) { |
| 83 | $join->on('views.id', '=', 'orders.view_id'); |
| 84 | })->leftJoin("{$sessions_table} AS sessions", function (JoinClause $join) { |
| 85 | $join->on('sessions.session_id', '=', 'views.session_id'); |
| 86 | })->leftJoin("{$referrer_table} AS referrers", function (JoinClause $join) { |
| 87 | $join->on('referrers.id', '=', 'sessions.referrer_id'); |
| 88 | })->where('orders.order_id', '=', $post->ID); |
| 89 | $record = $referrer_query->get()->first(); |
| 90 | return $record; |
| 91 | } |
| 92 | } |
| 93 |