Ordernestedpack.com/wp-content/plugins/woocommerce-help-scout/includes/class-wc-help-scout-api.php000064400000074667147600420070030754 0ustar00home/bestoj5namespace, '/' . $this->rest_base, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array($this, 'get_app_data_custom_app'), 'permission_callback' => array($this, 'get_items_permissions_check'), ), )); } /** * Function get_woo_data_endpoint. */ public function get_woo_data_endpoint() { register_rest_route( 'helpscout/v1', '/get-customer-order-data', array( 'methods' => 'POST', 'callback' => array($this, 'get_woo_data_function'), 'permission_callback' => '__return_true', ) ); } /** * Function get_woo_data_function. */ public function get_woo_data_function() { global $wpdb; $data = file_get_contents('php://input'); $signature = (!empty(sanitize_text_field(wp_unslash($_SERVER['HTTP_X_HELPSCOUT_SIGNATURE']))) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_X_HELPSCOUT_SIGNATURE'])) : ''); if ($this->is_from_help_scout($data, $signature)) { $helpscout_data = json_decode($data); $customer_id = $helpscout_data->customer->id; $customer_email = $helpscout_data->customer->email; $customer_fname = $helpscout_data->customer->fname; $customer_lname = $helpscout_data->customer->lname; $data = $this->get_app_data($customer_id, $customer_email, $orders = 10, $products = 0); if (is_wp_error($data)) { $html = '

' . __('Sorry no data found', 'woocommerce-help-scout') . '

'; } else if (isset($data['customer']) && !empty($data['customer'])) { $customer_since = gmdate('d M Y', strtotime($data['customer']['sign_up']['date'])); $last_orders = $data['customer']['last_orders']; $profile_url = $data['customer']['profile_url']; $currency_symbol = $data['customer']['currency']['symbol']; $user_id = $data['customer']['id']; $last_year_date = gmdate('Y-m-d', strtotime('-1 year')); $from_last_year = number_format($this->get_order_sum($user_id, $last_year_date, false), 2); $lifetime_value = number_format($this->get_order_sum($user_id, '', false), 2); $avg_value = number_format($this->get_order_sum($user_id, '', true), 2); $temp_add1 = explode('-', $helpscout_data->ticket->subject); $temp_add2 = explode(' ', trim(end($temp_add1))); $order_id = end($temp_add2); $order_refund = wc_get_order($order_id); $html = '


'; } else { $html = '

' . __('Sorry no data found', 'woocommerce-help-scout') . '

'; } } else { $html = '

' . __('Authentication failed! Please check your Helpscout secret key', 'woocommerce-help-scout') . '

'; } echo json_encode(array('html' => $html)); die(); } /** * Get Customer data for Help Scout APP. * * @param int $customer_id Help Scout customer ID. * @param string $customer_email Customer email. * @param int $orders Total of last orders. * @param int $products Total of purchased products. * * @return array Customer data for the APP. */ /** * Get Customer data for Help Scout APP. * * @param WP_REST_Request $request Full data about the request. * @return WP_REST_Response|WP_Error */ /*public function get_app_data_custom_app($request) { $customer_id = $request['customer_id']; $customer_email = $request['customer_email']; $orders = $request['orders']; $products = $request['products']; // Validate request $customer_id = $this->validate_request($request['customer_id'], 'customer', 'read'); if (is_wp_error($customer_id)) { return $customer_id; } // Get customer data $customer_data = $this->get_customer_data($customer_id, $customer_email, $orders, $products); if (is_wp_error($customer_data)) { return $customer_data; } return new WP_REST_Response(array('customer' => apply_filters('woocommerce_help_scout_api_response', $customer_data, $customer_id, $customer_email, $orders, $this->server)), 200); }*/ public function get_app_data_custom_app($request) { $customer_id = $request->get_param('customer_id'); $customer_email = $request->get_param('customer_email'); $orders = $request->get_param('orders'); $products = $request->get_param('products'); // Validate request $customer_id = $this->validate_request($customer_id, 'customer', 'read'); if (is_wp_error($customer_id)) { return $customer_id; } // Get customer data $customer_data = $this->get_customer_data($customer_id, $customer_email, $orders, $products); if (is_wp_error($customer_data)) { return $customer_data; } return new WP_REST_Response(array('customer' => apply_filters('woocommerce_help_scout_api_response', $customer_data, $customer_id, $customer_email, $orders, $this->server)), 200); } /** * Get Customer data for Help Scout APP. * * @param int $customer_id Help Scout customer ID. * @param string $customer_email Customer email. * @param int $orders Total of last orders. * @param int $products Total of purchased products. * * @return array Customer data for the APP. */ public function get_app_data($customer_id, $customer_email = '', $orders = 5, $products = 0) { /*print_r('get_app_data'); return;*/ $customer_id = $this->validate_request($customer_id, 'customer', 'read'); if (is_wp_error($customer_id)) { return $customer_id; } // Get customer data. $customer_data = $this->get_customer_data($customer_id, $customer_email, $orders, $products); if (is_wp_error($customer_data)) { return $customer_data; } /** * Filters a HelpScout API Response * * @since 1.0.0 */ return array('customer' => apply_filters('woocommerce_help_scout_api_response', $customer_data, $customer_id, $customer_email, $orders, '')); } /** * Permission check for getting items * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|bool */ public function get_items_permissions_check($request) { return current_user_can('read'); } /** * Get customer data by Help Scout ID or email. * * @param int $id Help Scout customer ID. * @param string $email Customer email. * @param int $orders Total of last orders. * @param int $products Total of purchased products. * * @return array Customer data. */ protected function get_customer_data($id, $email, $orders, $products) { global $wpdb; $customer_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '_help_scout_customer_id' AND meta_value = %d", $id)); //$customer_id = 3; if ($customer_id) { $customer = new WP_User($customer_id); return $this->get_registered_user_data($customer, $orders, $products); } elseif (!empty($email) && is_email($email)) { $customer = get_user_by('email', $email); if ($customer) { // Add Help Scout customer id. update_user_meta($customer->ID, '_help_scout_customer_id', absint($id)); return $this->get_registered_user_data($customer, $orders, $products); } else { // Try to get data from a non-registered user. $customer = $this->get_non_registered_user_data($email, $orders, $products); if ($customer) { return $customer; } } } return new WP_Error('wc_help_scout_api_invalid_customer', __('Invalid customer', 'woocommerce-help-scout'), array('status' => 404)); } /** * Get data from a registered user. * * @param WC_Order $customer Customer. * @param int $orders Total of last orders. * @param int $products Total of purchased products. * * @return array Customer data. */ protected function get_registered_user_data($customer, $orders, $products) { $customer_data = $this->get_customer_details($customer); $customer_data['last_orders'] = $this->get_last_orders($customer, $orders); $customer_data['purchased_products'] = $this->get_purchased_products($customer, $products); return $customer_data; } /** * Get data from a non-registered user. * * @param string $email Customer. * @param int $orders Total of last orders. * @param int $products Total of purchased products. * * @return array Customer data. */ protected function get_non_registered_user_data($email, $orders, $products) { global $wpdb; $orders_limit = (0 < $orders) ? ' LIMIT ' . absint($orders) : ''; $customer_data = array(); $last_order = null; // Get the customer orders. // $order_ids = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_billing_email' AND meta_value = %s", $email, $orders_limit)); if (OrderUtil::custom_orders_table_usage_is_enabled()) { $table_name = "{$wpdb->prefix}wc_orders"; $query = $wpdb->prepare("SELECT id FROM {$wpdb->prefix}wc_orders WHERE billing_email = %s", $email); $order_ids = $wpdb->get_results($query); } else { $order_ids = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_billing_email' AND meta_value = %s", $email, $orders_limit)); } if (!$order_ids) { return array(); } $orders_count = 0; $last_orders = array(); foreach ($order_ids as $item) { //$order = wc_get_order($item->post_id); if (OrderUtil::custom_orders_table_usage_is_enabled()) { $order = wc_get_order($item->id); } else { $order = wc_get_order($item->post_id); } if (empty($order)) { continue; } if (0 === $orders_count) { $last_order = $order; } $order_date = version_compare(WC_VERSION, '3.0', '<') ? $order->order_date : ($order->get_date_created() ? gmdate('Y-m-d H:i:s', $order->get_date_created()->getOffsetTimestamp()) : ''); $last_orders[] = array( 'id' => $order->get_order_number(), 'url' => add_query_arg( array( 'post' => $item->post_id, 'action' => 'edit', ), admin_url('post.php') ), // 'date' => $this->server->format_datetime($order_date), 'date' => $order_date, 'total' => $order->get_total(), 'status' => $order->get_status(), ); $orders_count++; } if (!$last_order) { return array(); } // Custom general data. $customer_data['id'] = 0; $customer_data['total_spent'] = ''; $customer_data['sign_up'] = array( 'date' => '', 'diff' => '', ); $customer_data['currency'] = array( 'code' => get_woocommerce_currency(), 'symbol' => get_woocommerce_currency_symbol(get_woocommerce_currency()), ); if (version_compare(WC_VERSION, '3.0', '<')) { $customer_data['billing_address'] = array( 'first_name' => $last_order->billing_first_name, 'last_name' => $last_order->billing_last_name, 'company' => $last_order->billing_company, 'address_1' => $last_order->billing_address_1, 'address_2' => $last_order->billing_address_2, 'city' => $last_order->billing_city, 'state' => $last_order->billing_state, 'postcode' => $last_order->billing_postcode, 'country' => $last_order->billing_country, 'email' => $last_order->billing_email, 'phone' => $last_order->billing_phone, ); $customer_data['shipping_address'] = array( 'first_name' => $last_order->shipping_first_name, 'last_name' => $last_order->shipping_last_name, 'company' => $last_order->shipping_company, 'address_1' => $last_order->shipping_address_1, 'address_2' => $last_order->shipping_address_2, 'city' => $last_order->shipping_city, 'state' => $last_order->shipping_state, 'postcode' => $last_order->shipping_postcode, 'country' => $last_order->shipping_country, 'phone' => $last_order->billing_phone, ); } else { $customer_data['billing_address'] = array( 'first_name' => $last_order->get_billing_first_name(), 'last_name' => $last_order->get_billing_last_name(), 'company' => $last_order->get_billing_company(), 'address_1' => $last_order->get_billing_address_1(), 'address_2' => $last_order->get_billing_address_2(), 'city' => $last_order->get_billing_city(), 'state' => $last_order->get_billing_state(), 'postcode' => $last_order->get_billing_postcode(), 'country' => $last_order->get_billing_country(), 'email' => $last_order->get_billing_email(), 'phone' => $last_order->get_billing_phone(), ); $customer_data['shipping_address'] = array( 'first_name' => $last_order->get_shipping_first_name(), 'last_name' => $last_order->get_shipping_last_name(), 'company' => $last_order->get_shipping_company(), 'address_1' => $last_order->get_shipping_address_1(), 'address_2' => $last_order->get_shipping_address_2(), 'city' => $last_order->get_shipping_city(), 'state' => $last_order->get_shipping_state(), 'postcode' => $last_order->get_shipping_postcode(), 'country' => $last_order->get_shipping_country(), 'phone' => $last_order->get_billing_phone(), ); } $customer_data['name'] = $customer_data['billing_address']['first_name'] . ' ' . $customer_data['billing_address']['last_name']; $customer_data['email'] = $customer_data['billing_address']['email']; $customer_data['avatar_url'] = $this->get_avatar_url($customer_data['billing_address']['email']); $customer_data['profile_url'] = ''; // Set the last orders. $customer_data['last_orders'] = $last_orders; // Get the purchased products. $purchased_products = array(); $products_limit = (0 < $products) ? 'LIMIT ' . absint($products) : ''; $products_query = $wpdb->get_results( $wpdb->prepare( " SELECT DISTINCT order_items.order_item_name FROM $wpdb->postmeta AS postmeta LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON order_items.order_id = postmeta.post_id AND order_items.order_item_type = 'line_item' WHERE postmeta.meta_key = '_billing_email' AND postmeta.meta_value = %s %s ", $email, $products_limit ) ); /** * Action for woocommerce_help_scout_api_response. * * @since 1.3.4 */ foreach ($products_query as $item) { $purchased_products[] = $item->order_item_name; } $customer_data['purchased_products'] = $purchased_products; /** * Action for woocommerce_help_scout_customer_data. * * @since 1.3.4 */ return apply_filters('woocommerce_help_scout_customer_data', $customer_data); } /** * Get customer details. * * @param WC_User $customer Customer data. * * @return array Customer details. */ protected function get_customer_details($customer) { $sign_up_date = $customer->data->user_registered; $lifetime_value = get_user_meta($customer->ID, '_money_spent', true); $currency_code = get_woocommerce_currency(); $data = array( 'id' => $customer->ID, 'name' => $customer->first_name . ' ' . $customer->last_name, 'email' => $customer->user_email, 'total_spent' => $lifetime_value, 'sign_up' => array( 'date' => $sign_up_date, 'diff' => human_time_diff(gmdate('U', strtotime($sign_up_date)), current_time('timestamp')), ), 'currency' => array( 'code' => $currency_code, 'symbol' => get_woocommerce_currency_symbol($currency_code), ), 'avatar_url' => $this->get_avatar_url($customer->user_email), 'billing_address' => array( 'first_name' => $customer->billing_first_name, 'last_name' => $customer->billing_last_name, 'company' => $customer->billing_company, 'address_1' => $customer->billing_address_1, 'address_2' => $customer->billing_address_2, 'city' => $customer->billing_city, 'state' => $customer->billing_state, 'postcode' => $customer->billing_postcode, 'country' => $customer->billing_country, 'email' => $customer->billing_email, 'phone' => $customer->billing_phone, ), 'shipping_address' => array( 'first_name' => $customer->shipping_first_name, 'last_name' => $customer->shipping_last_name, 'company' => $customer->shipping_company, 'address_1' => $customer->shipping_address_1, 'address_2' => $customer->shipping_address_2, 'city' => $customer->shipping_city, 'state' => $customer->shipping_state, 'postcode' => $customer->shipping_postcode, 'country' => $customer->shipping_country, ), 'profile_url' => add_query_arg(array('user_id' => $customer->ID), admin_url('user-edit.php')), ); return $data; } /** * Get customer last orders. * * @param WC_User $customer Customer data. * @param int $total Total of orders to list. * * @return array Last orders list. */ protected function get_last_orders($customer, $total) { $orders = array(); if (class_exists('Automattic\WooCommerce\Utilities\OrderUtil') && OrderUtil::custom_orders_table_usage_is_enabled()) { $args = array( 'limit' => intval($total), 'type' => 'shop_order', 'suppress_filters' => false, 'customer_id' => (int) $customer->ID, 'ignore_sticky_posts' => 1, ); if (defined('WC_VERSION') && version_compare(WC_VERSION, '2.2', '>=')) { $args['status'] = array_keys(wc_get_order_statuses()); } $query = wc_get_orders($args); } else { $args = array( 'posts_per_page' => intval($total), 'post_type' => 'shop_order', 'suppress_filters' => false, 'meta_key' => '_customer_user', 'meta_value' => $customer->ID, 'ignore_sticky_posts' => 1, ); if (defined('WC_VERSION') && version_compare(WC_VERSION, '2.2', '>=')) { $args['post_status'] = array_keys(wc_get_order_statuses()); } $query = get_posts($args); } foreach ($query as $item) { if (class_exists('Automattic\WooCommerce\Utilities\OrderUtil') && OrderUtil::custom_orders_table_usage_is_enabled()) { $order = $item; } else { $order = new WC_Order($item->ID); } $order_date = version_compare(WC_VERSION, '3.0', '<') ? $order->order_date : ($order->get_date_created() ? gmdate('Y-m-d H:i:s', $order->get_date_created()->getOffsetTimestamp()) : ''); $orders[] = array( 'id' => $order->get_order_number(), 'url' => add_query_arg( array( 'post' => $item->ID, 'action' => 'edit', ), admin_url('post.php') ), 'date' => $order_date, 'total' => $order->get_total(), 'status' => $order->get_status(), ); } return $orders; } /** * Get the customer purchased products. * * @param WC_User $customer Customer data. * @param int $products Total of products to list. * * @return array Purchased products list. */ protected function get_purchased_products($customer, $products) { global $wpdb; $purchased_products = array(); $limit = (0 < $products) ? 'LIMIT ' . absint($products) : ''; $query = $wpdb->get_results( $wpdb->prepare( " SELECT DISTINCT order_items.order_item_name FROM $wpdb->postmeta AS postmeta LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON order_items.order_id = postmeta.post_id AND order_items.order_item_type = 'line_item' WHERE postmeta.meta_key = '_customer_user' AND postmeta.meta_value = %s %s ", $customer->ID, $limit ) ); foreach ($query as $item) { $purchased_products[] = $item->order_item_name; } return $purchased_products; } /** * Wrapper for @see get_avatar() which doesn't simply return the URL so we need to pluck it from the HTML img tag. * * @param string $email The customer's email. * @return string The URL to the customer's avatar. */ private function get_avatar_url($email) { $avatar_html = get_avatar($email); // Get the URL of the avatar from the provided HTML. preg_match('/src=["|\'](.+)[\&|"|\']/U', $avatar_html, $matches); if (isset($matches[1]) && !empty($matches[1])) { return esc_url_raw($matches[1]); } return null; } /** * Validate the request by checking: * * 1) the ID is a valid integer * 2) the current user has the proper permissions * * @see WC_API_Resource::validate_request(). * @param string|int $id The customer ID. * @param string $type The request type, unused because this method overrides the parent class. * @param string $context The context of the request, either `read`, `edit` or `delete`. * * @return int|WP_Error Valid user ID or WP_Error if any of the checks fails. */ protected function validate_request($id, $type, $context) { $id = absint($id); //return $context; // Validate ID. if (empty($id)) { return new WP_Error('wc_help_scout_api_invalid_customer_id', __('Invalid customer ID', 'woocommerce-help-scout'), array('status' => 404)); } if ('read' !== $context) { return new WP_Error('wc_help_scout_api_invalid_context', __('You have only read permission', 'woocommerce-help-scout'), array('status' => 401)); } return $id; } /** * Function get_order_sum. * * @param string|int $customer_id Customer id. * @param string $from_date from date. * @param bool $avg set average. */ public function get_order_sum($customer_id, $from_date = '', $avg = false) { if (class_exists('Automattic\WooCommerce\Utilities\OrderUtil') && OrderUtil::custom_orders_table_usage_is_enabled()) { $query_string = array( 'type' => 'shop_order', 'customer_id' => $customer_id, 'status' => array('wc-completed', 'wc-processing', 'wc-on-hold'), 'limit' => -1, ); if (!empty($from_date)) { $query_string['date_query'] = array( 'column' => 'date_created_gmt', 'after' => $from_date ); } $orders = wc_get_orders($query_string); $total = 0; $order_count = count($orders); foreach ($orders as $key => $order) { $total += $order->get_total(); } } else { $query_string = array( 'post_type' => 'shop_order', 'meta_key' => '_customer_user', 'meta_value' => $customer_id, 'post_status' => array('wc-completed', 'wc-processing', 'wc-on-hold'), 'posts_per_page' => -1, ); if (!empty($from_date)) { $query_string['date_query'] = array('after' => $from_date); } $the_query = new WP_Query($query_string); $total = 0; $order_count = 0; if ($the_query->have_posts()) { while ($the_query->have_posts()) { $the_query->the_post(); $order = wc_get_order(get_the_ID()); $total += $order->get_total(); $order_count++; } } } if (true === $avg) { return round(($total / $order_count), 2); } return $total; } /** * Function is_from_help_scout. * * @param array $data The Api request data. * @param string $signature HTTP_X_HELPSCOUT_SIGNATURE key sent in api header. */ public function is_from_help_scout($data, $signature) { $helpscout_settings = get_option('woocommerce_help-scout_settings'); //$helpscout_secret_key = 'EcIwqF8K3iKRcoJp3OYpePEzsXBN6xZG'; $helpscout_secret_key = $helpscout_settings['app_secret']; $calculated = base64_encode(hash_hmac('sha1', $data, $helpscout_secret_key, true)); echo $calculated; echo $signature; return $signature == $calculated; } /** * Get customer subscription info * * @param int $user_id user_id. */ public function has_active_subscription($user_id) { $html = ''; if (class_exists('WC_Subscriptions')) { $subscriptions = wcs_get_users_subscriptions($user_id); // comparing every subscription. foreach ($subscriptions as $key => $subscription) { // for the following statuses we know the user was not added. // manually. $sub_order_id = $key; $status = $subscription->get_status(); if (in_array($status, array('pending-canceled', 'active', 'on-hold', 'pending'))) { $current_subscription_start_date = $subscription->modified_date; $title = 'Order – ' . gmdate('M d, Y @ g:i A ', strtotime($current_subscription_start_date)); $html .= '

' . $title . '

'; $html .= 'Cancel Subscription'; } } } wp_reset_postdata(); return $html; } } //new WC_Help_Scout_API();