class_enrich_order.php
188 lines
| 1 | <?php |
| 2 | namespace PixelYourSite; |
| 3 | |
| 4 | class EnrichOrder { |
| 5 | private static $_instance; |
| 6 | |
| 7 | public static function instance() { |
| 8 | |
| 9 | if ( is_null( self::$_instance ) ) { |
| 10 | self::$_instance = new self(); |
| 11 | } |
| 12 | return self::$_instance; |
| 13 | } |
| 14 | |
| 15 | public function init() { |
| 16 | //woo |
| 17 | if(PYS()->getOption("woo_enabled_save_data_to_orders")) { |
| 18 | add_action( 'woocommerce_new_order',array($this,'woo_save_checkout_fields'),10, 1); |
| 19 | add_action( 'add_meta_boxes', array($this,'woo_add_order_meta_boxes') ); |
| 20 | if(PYS()->getOption("woo_add_enrich_to_admin_email")) { |
| 21 | add_action( 'woocommerce_email_customer_details', array($this,'woo_add_enrich_to_admin_email'),80,4 ); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // edd |
| 26 | if(PYS()->getOption("edd_enabled_save_data_to_orders")) { |
| 27 | add_filter('edd_payment_meta', array($this, 'edd_save_checkout_fields'),10,2); |
| 28 | add_action('edd_view_order_details_main_after', array($this, 'add_edd_order_details')); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | function add_edd_order_details($payment_id) { |
| 33 | echo '<div id="edd-payment-notes" class="postbox"> |
| 34 | <h3 class="hndle"><span>PixelYourSite</span></h3>'; |
| 35 | echo "<div style='margin:20px'><p>With the paid plugin, you can see more data on the Easy Digital Downloads Reports page. |
| 36 | <a target='_blank' href='https://www.pixelyoursite.com/easy-digital-downloads-first-party-reports/?utm_source=free-plugin-edd-order&utm_medium=free-plugin-edd-order&utm_campaign=free-plugin-edd-order&utm_content=free-plugin-edd-order&utm_term=free-plugin-edd-order'>Click here for details.</a> |
| 37 | </p>You can stop storing this data from the plugin's <a href='".admin_url("admin.php?page=pixelyoursite&tab=edd")."' target='_blank'>Easy Digital Downloads page</a></div>"; |
| 38 | include 'views/html-edd-order-box.php'; |
| 39 | echo '</div>'; |
| 40 | } |
| 41 | |
| 42 | function woo_add_order_meta_boxes () { |
| 43 | $screen = isWooUseHPStorage() |
| 44 | ? wc_get_page_screen_id( 'shop-order' ) |
| 45 | : 'shop_order'; |
| 46 | add_meta_box( 'pys_enrich_fields_woo', __('PixelYourSite','pixelyoursite'), |
| 47 | array($this,"woo_render_order_fields"), $screen); |
| 48 | } |
| 49 | |
| 50 | function woo_render_order_fields($post) { |
| 51 | if ($post instanceof \WP_Post) { |
| 52 | $orderId = $post->ID; |
| 53 | } elseif (method_exists($post, 'get_id')) { |
| 54 | $orderId = $post->get_id(); |
| 55 | } else { |
| 56 | // Обработка ситуации, когда $post не является ни объектом \WP_Post, ни объектом с методом get_id(). |
| 57 | $orderId = null; // Или другое значение по умолчанию. |
| 58 | } |
| 59 | echo "<div style='margin:20px 10px'><p>With the paid plugin, you can see more data on the WooCommerce Reports page. |
| 60 | <a href='https://www.pixelyoursite.com/woocommerce-first-party-reports?utm_source=free-plugin&utm_medium=order-page&utm_campaign=reports-order-page&utm_content=woocommerce-reports-client-page&utm_term=order-page-reports' target='_blank'>Click here for details</a> |
| 61 | </p>You can stop storing this data from the plugin's <a href='".admin_url("admin.php?page=pixelyoursite&tab=woo")."' target='_blank'>WooCommerce page</a>.</div>"; |
| 62 | include 'views/html-order-meta-box.php'; |
| 63 | } |
| 64 | |
| 65 | function woo_save_checkout_fields($order_id) { |
| 66 | $pysData = []; |
| 67 | $pysData = $this->getPysData(); |
| 68 | |
| 69 | $order = wc_get_order($order_id); |
| 70 | if ( isWooCommerceVersionGte('3.0.0') ) { |
| 71 | // WooCommerce >= 3.0 |
| 72 | if($order) { |
| 73 | $order->update_meta_data("pys_enrich_data",$pysData); |
| 74 | $order->save(); |
| 75 | } |
| 76 | |
| 77 | } else { |
| 78 | // WooCommerce < 3.0 |
| 79 | update_post_meta( $order_id, 'pys_enrich_data', $pysData ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param \WC_Order$order |
| 85 | * @param $sent_to_admin |
| 86 | * @param $plain_text |
| 87 | * @param $email |
| 88 | */ |
| 89 | |
| 90 | function woo_add_enrich_to_admin_email($order, $sent_to_admin) { |
| 91 | if($sent_to_admin) { |
| 92 | $orderId = $order->get_id(); |
| 93 | echo "<h2 style='text-align: center'>". __('PixelYourSite','pixelyoursite')."</h2>"; |
| 94 | echo "Your clients don't see this information! We send it to you in this \"New Order\" email. If you want to remove this data from the \"New Order\" email, open <a href='".admin_url("admin.php?page=pixelyoursite&tab=woo")."' target='_blank'>PixelYourSite's WooCommerce page</a>, disable \"Send reports data to the New Order email\" and save. |
| 95 | <br/>With PixelYourSite Professional, you can view and download this data from the plugin's own reports page. Find out how WooCommerce Reports work and how to visualize and download your data: <a href='https://www.pixelyoursite.com/woocommerce-first-party-reports?utm_source=free-plugin&utm_medium=order-email&utm_campaign=order-email-link&utm_content=woocommerce-reports&utm_term=woocommerce-reports-email-link' target='_blank'>Click here for details</a>.<br/>"; |
| 96 | include 'views/html-order-meta-box.php'; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | |
| 101 | function edd_save_checkout_fields( $payment_meta ,$init_payment_data) { |
| 102 | |
| 103 | if ( 0 !== did_action('edd_pre_process_purchase') ) { |
| 104 | $pysData = []; |
| 105 | |
| 106 | $pys_landing = ''; |
| 107 | $pys_source = ''; |
| 108 | |
| 109 | $utms = getUtms(true); |
| 110 | $utms_id = getUtmsId(true); |
| 111 | |
| 112 | $pys_utm = implode("|", array_map(function ($key, $value) { |
| 113 | return "$key:$value"; |
| 114 | }, array_keys($utms), $utms)); |
| 115 | $pys_utm_id = implode("|", array_map(function ($key, $value) { |
| 116 | return "$key:$value"; |
| 117 | }, array_keys($utms_id), $utms_id)); |
| 118 | $pys_browser_time = getBrowserTime(); |
| 119 | if (isset($_COOKIE['pys_landing_page']) || isset($_SESSION['LandingPage'])) { |
| 120 | $pys_landing = $_COOKIE['pys_landing_page'] ?? $_SESSION['LandingPage']; |
| 121 | } |
| 122 | if (isset($_COOKIE['pysTrafficSource']) || isset($_SESSION['TrafficSource'])) { |
| 123 | $pys_source = $_COOKIE['pysTrafficSource'] ?? $_SESSION['TrafficSource']; |
| 124 | } |
| 125 | $pysData['pys_landing'] = isset($_REQUEST['pys_landing']) ? sanitize_text_field($_REQUEST['pys_landing']) : $pys_landing; |
| 126 | $pysData['pys_source'] = isset($_REQUEST['pys_source']) ? sanitize_text_field($_REQUEST['pys_source']) : $pys_source; |
| 127 | $pysData['pys_utm'] = isset($_REQUEST['pys_utm']) ? sanitize_text_field($_REQUEST['pys_utm']) : $pys_utm; |
| 128 | $pysData['pys_browser_time'] = isset($_REQUEST['pys_browser_time']) ? sanitize_text_field($_REQUEST['pys_browser_time']) : $pys_browser_time; |
| 129 | |
| 130 | |
| 131 | $pysData['last_pys_landing'] = isset($_REQUEST['last_pys_landing']) ? sanitize_text_field($_REQUEST['last_pys_landing']) : $pys_landing; |
| 132 | $pysData['last_pys_source'] = isset($_REQUEST['last_pys_source']) ? sanitize_text_field($_REQUEST['last_pys_source']) : $pys_source; |
| 133 | $pysData['last_pys_utm'] = isset($_REQUEST['last_pys_utm']) ? sanitize_text_field($_REQUEST['last_pys_utm']) : $pys_utm; |
| 134 | |
| 135 | $pysData['pys_utm_id'] = isset($_REQUEST['pys_utm_id']) ? sanitize_text_field($_REQUEST['pys_utm_id']) : $pys_utm_id; |
| 136 | $pysData['last_pys_utm_id'] = isset($_REQUEST['last_pys_utm_id']) ? sanitize_text_field($_REQUEST['last_pys_utm_id']) : $pys_utm_id; |
| 137 | |
| 138 | |
| 139 | $payment_meta['pys_enrich_data'] = $pysData; |
| 140 | } |
| 141 | return $payment_meta; |
| 142 | } |
| 143 | |
| 144 | function getPysData(){ |
| 145 | $pysData = array(); |
| 146 | $pys_landing = ''; |
| 147 | $pys_source = ''; |
| 148 | $utms = getUtms(true); |
| 149 | $utms_id = getUtmsId(true); |
| 150 | |
| 151 | $pys_utm = implode("|", array_map(function ($key, $value) { |
| 152 | return "$key:$value"; |
| 153 | }, array_keys($utms), $utms)); |
| 154 | $pys_utm_id = implode("|", array_map(function ($key, $value) { |
| 155 | return "$key:$value"; |
| 156 | }, array_keys($utms_id), $utms_id)); |
| 157 | $pys_browser_time = getBrowserTime(); |
| 158 | if (isset($_COOKIE['pys_landing_page']) || isset($_SESSION['LandingPage'])) { |
| 159 | $pys_landing = $_COOKIE['pys_landing_page'] ?? $_SESSION['LandingPage']; |
| 160 | } |
| 161 | if (isset($_COOKIE['pysTrafficSource']) || isset($_SESSION['TrafficSource'])) { |
| 162 | $pys_source = $_COOKIE['pysTrafficSource'] ?? $_SESSION['TrafficSource']; |
| 163 | } |
| 164 | PYS()->getLog()->debug("Check", $pys_source); |
| 165 | $pysData['pys_landing'] = isset($_REQUEST['pys_landing']) ? sanitize_text_field($_REQUEST['pys_landing']) : $pys_landing; |
| 166 | $pysData['pys_source'] = isset($_REQUEST['pys_source']) ? sanitize_text_field($_REQUEST['pys_source']) : $pys_source; |
| 167 | $pysData['pys_utm'] = isset($_REQUEST['pys_utm']) ? sanitize_text_field($_REQUEST['pys_utm']) : $pys_utm; |
| 168 | $pysData['pys_browser_time'] = isset($_REQUEST['pys_browser_time']) ? sanitize_text_field($_REQUEST['pys_browser_time']) : $pys_browser_time; |
| 169 | |
| 170 | $pysData['last_pys_landing'] = isset($_REQUEST['last_pys_landing']) ? sanitize_text_field($_REQUEST['last_pys_landing']) : $pys_landing; |
| 171 | $pysData['last_pys_source'] = isset($_REQUEST['last_pys_source']) ? sanitize_text_field($_REQUEST['last_pys_source']) : $pys_source; |
| 172 | $pysData['last_pys_utm'] = isset($_REQUEST['last_pys_utm']) ? sanitize_text_field($_REQUEST['last_pys_utm']) : $pys_utm; |
| 173 | |
| 174 | $pysData['pys_utm_id'] = isset($_REQUEST['pys_utm_id']) ? sanitize_text_field($_REQUEST['pys_utm_id']) : $pys_utm_id; |
| 175 | $pysData['last_pys_utm_id'] = isset($_REQUEST['last_pys_utm_id']) ? sanitize_text_field($_REQUEST['last_pys_utm_id']) : $pys_utm_id; |
| 176 | |
| 177 | return $pysData; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @return EnrichOrder |
| 183 | */ |
| 184 | function EnrichOrder() { |
| 185 | return EnrichOrder::instance(); |
| 186 | } |
| 187 | |
| 188 | EnrichOrder(); |