__( 'Easy Digital Downloads: new purchase', 'pingpush' ), 'group' => 'ecommerce', 'source' => 'Easy Digital Downloads', 'title' => __( 'New sale #{order_number}: {order_total}', 'pingpush' ), 'message' => __( "{customer_name}\n{items}", 'pingpush' ), 'sound' => 'cashregister', 'enabled' => true, 'placeholders' => array( 'order_number', 'order_total', 'items_count', 'items', 'customer_name', 'customer_email' ), ) ); } /** * Hooks. */ public function hooks() { add_action( 'edd_complete_purchase', array( $this, 'complete' ), 20, 1 ); } /** * Purchase complete. * * @param int $id Order (EDD 3) or payment (EDD 2) id. */ public function complete( $id ) { $data = function_exists( 'edd_get_order' ) ? $this->from_order( (int) $id ) : $this->from_payment( (int) $id ); if ( ! $data ) { return; } PingPush_Events::trigger( 'edd_purchase', $data, array( 'url' => admin_url( 'edit.php?post_type=download&page=edd-payment-history&view=view-order-details&id=' . (int) $id ), 'url_title' => __( 'View order', 'pingpush' ), 'dedupe' => (string) $id, 'dedupe_ttl' => DAY_IN_SECONDS, ) ); } /** * EDD 3 order data. * * @param int $id Order id. * @return array|null */ private function from_order( $id ) { $order = edd_get_order( $id ); if ( ! $order ) { return null; } $items = array(); $count = 0; foreach ( (array) $order->get_items() as $item ) { $count += (int) $item->quantity; $items[] = (int) $item->quantity . ' × ' . $item->product_name; } $name = ''; if ( function_exists( 'edd_get_customer' ) && $order->customer_id ) { $customer = edd_get_customer( $order->customer_id ); $name = $customer ? (string) $customer->name : ''; } return array( 'order_number' => method_exists( $order, 'get_number' ) ? (string) $order->get_number() : (string) $id, 'order_total' => $this->money( $order->total, $order->currency ), 'items_count' => (string) $count, 'items' => implode( "\n", $items ), 'customer_name' => '' !== $name ? $name : (string) $order->email, 'customer_email' => (string) $order->email, ); } /** * EDD 2 payment data. * * @param int $id Payment id. * @return array|null */ private function from_payment( $id ) { if ( ! function_exists( 'edd_get_payment' ) ) { return null; } $payment = edd_get_payment( $id ); if ( ! $payment ) { return null; } $items = array(); $count = 0; foreach ( (array) $payment->cart_details as $item ) { $qty = (int) ( $item['quantity'] ?? 1 ); $count += $qty; $items[] = $qty . ' × ' . ( $item['name'] ?? '' ); } $name = trim( $payment->first_name . ' ' . $payment->last_name ); return array( 'order_number' => (string) $payment->number, 'order_total' => $this->money( $payment->total, $payment->currency ), 'items_count' => (string) $count, 'items' => implode( "\n", $items ), 'customer_name' => '' !== $name ? $name : (string) $payment->email, 'customer_email' => (string) $payment->email, ); } /** * Plain-text price. * * @param float $amount Amount. * @param string $currency Currency. * @return string */ private function money( $amount, $currency ) { if ( function_exists( 'edd_currency_filter' ) && function_exists( 'edd_format_amount' ) ) { return trim( html_entity_decode( wp_strip_all_tags( edd_currency_filter( edd_format_amount( $amount ), $currency ) ), ENT_QUOTES, 'UTF-8' ) ); } return $currency . ' ' . number_format_i18n( (float) $amount, 2 ); } }