get_id(); $number = self::identifier($order->get_order_number()); $status = (string) $order->get_status(); $currency = (string) $order->get_currency(); $total = self::money($order->get_total()); $shipping_total = self::money($order->get_shipping_total()); $billing = self::address($order, 'billing'); $shipping = self::address($order, 'shipping'); $items = self::items($order->get_items('line_item')); $note = self::text($order->get_customer_note(), 2000); $payment = self::text($order->get_payment_method(), 100); $date = $order->get_date_created(); } catch (Throwable $error) { unset($error); return false; } $statuses = array('pending', 'processing', 'on-hold', 'completed', 'cancelled', 'refunded', 'failed', 'akkedli-confirmed', 'shipped'); if (!self::native_id($id) || false === $number || !in_array($status, $statuses, true) || 1 !== preg_match('/^[A-Z]{3}$/D', $currency) || false === $total || false === $shipping_total || false === $billing || false === $shipping || false === $items || false === $note || false === $payment || (null !== $date && !is_a($date, 'DateTimeInterface'))) { return false; } $data = array( 'id' => $id, 'number' => $number, 'status' => $status, 'currency' => $currency, 'total' => $total, 'shipping_total' => $shipping_total, 'customer_note' => $note, 'payment_method' => $payment, 'billing' => $billing, 'shipping' => $shipping, 'line_items' => $items, ); if (null !== $date) { $data['date_created'] = $date->format('c'); } return array('event' => 'order.created', 'order' => $data); } private function status_payload($order, $old_status, $new_status) { if (!is_object($order) || !is_a($order, 'WC_Order')) { return false; } try { $number = self::identifier($order->get_order_number()); } catch (Throwable $error) { unset($error); return false; } $previous = array('pending', 'processing', 'on-hold', 'completed', 'cancelled', 'refunded', 'failed', 'akkedli-confirmed', 'shipped'); $target = array('processing', 'on-hold', 'completed', 'cancelled', 'refunded', 'failed', 'akkedli-confirmed', 'shipped'); if (false === $number || !is_string($old_status) || !is_string($new_status) || !in_array($old_status, $previous, true) || !in_array($new_status, $target, true)) { return false; } return array('order_number' => $number, 'old_status' => $old_status, 'new_status' => $new_status); } private static function native_id($value) { return 1 === preg_match('/^[1-9]\d{0,15}$/D', $value) && (strlen($value) < 16 || strcmp($value, '9007199254740991') <= 0); } private static function identifier($value) { if (!is_string($value) && !is_int($value)) { return false; } $value = (string) $value; $trimmed = self::trim($value); return $value !== '' && $trimmed === $value && self::length($value) <= 100 ? $value : false; } private static function money($value) { if (!is_string($value) && !is_int($value)) { return false; } $value = (string) $value; if (1 !== preg_match('/^\d{1,10}(?:\.\d{1,6})?$/D', $value)) { return false; } list($whole, $fraction) = array_pad(explode('.', $value, 2), 2, ''); $whole = ltrim($whole, '0'); $whole = '' === $whole ? '0' : $whole; return strlen($whole) < 10 || (strlen($whole) === 10 && ($whole < '1000000000' || ('1000000000' === $whole && '' === trim($fraction, '0')))) ? $value : false; } private static function text($value, $limit, $trim = false) { if (!is_string($value)) { return false; } $value = $trim ? self::trim($value) : $value; $length = self::length($value); return false !== $length && $length <= $limit ? $value : false; } private static function length($value) { if (!is_string($value) || 1 !== preg_match('//u', $value)) { return false; } return preg_match_all('/./us', $value, $characters) + preg_match_all('/[\x{10000}-\x{10FFFF}]/u', $value, $astral); } private static function trim($value) { return preg_replace('/^[\s\x{FEFF}]+|[\s\x{FEFF}]+$/u', '', $value); } private static function address($order, $prefix) { $limits = array('first_name' => 100, 'last_name' => 100, 'company' => 200, 'address_1' => 500, 'address_2' => 500, 'city' => 100, 'state' => 100, 'postcode' => 30, 'country' => 10, 'phone' => 30); $address = array(); foreach ($limits as $field => $limit) { $getter = 'get_' . $prefix . '_' . $field; if (!is_callable(array($order, $getter))) { continue; } $value = self::text($order->$getter(), $limit); if (false === $value) { return false; } $address[$field] = $value; } if ('billing' === $prefix) { $email = self::text($order->get_billing_email(), 200); if (false === $email || ('' !== $email && false === filter_var($email, FILTER_VALIDATE_EMAIL))) { return false; } $address['email'] = $email; } return $address; } private static function items($source) { if (!is_array($source) && !is_a($source, 'Traversable')) { return false; } $items = array(); foreach ($source as $item) { if (!is_object($item) || count($items) >= 100) { return false; } try { $name = self::text($item->get_name(), 300, true); $quantity = $item->get_quantity(); $subtotal = self::money($item->get_subtotal()); $total = self::money($item->get_total()); $product_id = self::identifier($item->get_product_id()); } catch (Throwable $error) { unset($error); return false; } if (false === $name || '' === $name || !is_int($quantity) || $quantity < 1 || $quantity > 9999 || false === $subtotal || false === $total || false === $product_id) { return false; } $items[] = array('name' => $name, 'quantity' => $quantity, 'subtotal' => $subtotal, 'total' => $total, 'product_id' => $product_id); } return count($items) > 0 ? $items : false; } }