Codexta Assistant), all on by default: * * - send orders at all (off: nothing about an order leaves the shop and Codexta credits no order); * - only orders with a line the assistant added (the line item meta _codexta, see Codexta_Attribution); * - only what crediting an order needs: its id, total, currency, date, customer id and, per line, the amounts and the mark. No name, address, phone or email. * * Only webhooks that deliver to Codexta are touched (this connection's and any left by an earlier one); any other webhook of the shop is left alone. */ class Codexta_Orders { const OPT_SEND = 'codexta_orders_send'; const OPT_ASSISTED_ONLY = 'codexta_orders_assisted_only'; const OPT_MINIMAL = 'codexta_orders_minimal'; /** * Register the two filters. */ public static function init() { add_filter( 'woocommerce_webhook_should_deliver', array( __CLASS__, 'should_deliver' ), 10, 3 ); add_filter( 'woocommerce_webhook_payload', array( __CLASS__, 'payload' ), 10, 4 ); } /** * A setting; on unless the store owner switched it off. * * @param string $option Option name. * @return bool */ public static function enabled( $option ) { return 'no' !== get_option( $option, 'yes' ); } /** * Whether a webhook delivers to Codexta. Any of them, whatever site id its address carries: a webhook left by an earlier connection has an older * id and would otherwise slip past the settings and send every order in full. * * @param mixed $webhook WC_Webhook. * @return bool */ private static function is_ours( $webhook ) { return $webhook instanceof WC_Webhook && false !== strpos( (string) $webhook->get_delivery_url(), '/integrations/woocommerce/webhooks?u=' ); } /** * Skip an order the assistant had no part in. The hook fires while the order is being saved, before its lines are stored, so the cart of the * request being checked out decides first (its lines carry the mark), then the stored order. * * @param bool $deliver Whether WooCommerce delivers. * @param object $webhook The webhook. * @param mixed $arg First hook argument: the order id for order.created. * @return bool */ public static function should_deliver( $deliver, $webhook, $arg ) { if ( ! $deliver || ! self::is_ours( $webhook ) ) { return $deliver; } if ( '' === Codexta_Connect::clean_origin( $webhook->get_delivery_url() ) ) { return false; } if ( 0 !== strpos( (string) $webhook->get_topic(), 'order.' ) ) { return $deliver; } if ( ! self::enabled( self::OPT_SEND ) ) { return false; } if ( ! self::enabled( self::OPT_ASSISTED_ONLY ) ) { return $deliver; } if ( function_exists( 'WC' ) && WC()->cart ) { foreach ( WC()->cart->get_cart() as $line ) { if ( ! empty( $line['codexta_chat'] ) ) { return true; } } } $order = wc_get_order( absint( $arg ) ); if ( $order ) { foreach ( $order->get_items() as $item ) { if ( '' !== (string) $item->get_meta( Codexta_Attribution::META ) ) { return true; } } } return false; } /** * Cut an order's payload down to what attribution reads. * * @param array $payload The payload. * @param string $resource The resource (order, product ...). * @param int $resource_id Its id. * @param int $webhook_id The webhook. * @return array */ public static function payload( $payload, $resource, $resource_id, $webhook_id ) { if ( 'order' !== $resource || ! is_array( $payload ) || ! self::enabled( self::OPT_MINIMAL ) ) { return $payload; } $webhook = wc_get_webhook( $webhook_id ); if ( ! self::is_ours( $webhook ) ) { return $payload; } $lines = array(); foreach ( isset( $payload['line_items'] ) && is_array( $payload['line_items'] ) ? $payload['line_items'] : array() as $line ) { $marks = array(); foreach ( isset( $line['meta_data'] ) && is_array( $line['meta_data'] ) ? $line['meta_data'] : array() as $meta ) { if ( isset( $meta['key'] ) && Codexta_Attribution::META === $meta['key'] ) { $marks[] = array( 'key' => $meta['key'], 'value' => isset( $meta['value'] ) ? $meta['value'] : '', ); } } $lines[] = array( 'total' => isset( $line['total'] ) ? $line['total'] : '0', 'total_tax' => isset( $line['total_tax'] ) ? $line['total_tax'] : '0', 'meta_data' => $marks, ); } $keep = array( 'id', 'total', 'currency', 'date_created_gmt', 'customer_id' ); return array_merge( array_intersect_key( $payload, array_flip( $keep ) ), array( 'line_items' => $lines ) ); } }