PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.4
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.4
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / modules / webhook / classes / class-cartflows-webhook-payload.php

class-cartflows-webhook-payload.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.4, at modules/webhook/classes/class-cartflows-webhook-payload.php

176 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CartFlows Webhook Payload Builder.
4 *
5 * Builds JSON payloads per event type.
6 *
7 * @package CartFlows
8 */
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 if ( ! class_exists( 'Cartflows_Webhook_Payload' ) ) {
16
17 /**
18 * Class Cartflows_Webhook_Payload.
19 */
20 class Cartflows_Webhook_Payload {
21
22 /**
23 * Member Variable
24 *
25 * @var instance
26 */
27 private static $instance = null;
28
29 /**
30 * Initiator
31 *
32 * @return object initialized object of class.
33 */
34 public static function get_instance() {
35 if ( is_null( self::$instance ) ) {
36 self::$instance = new self();
37 }
38 return self::$instance;
39 }
40
41 /**
42 * Build payload for an event.
43 *
44 * @param string $event Event name.
45 * @param array $context Event context data.
46 * @param string $webhook_id Webhook ID.
47 * @return array
48 */
49 public function build( $event, $context, $webhook_id = '' ) {
50 $payload = array(
51 'event' => $event,
52 'timestamp' => gmdate( 'c' ),
53 'webhook_id' => $webhook_id,
54 'data' => $this->build_event_data( $event, $context ),
55 );
56
57 return apply_filters( 'cartflows_webhook_payload', $payload, $event, $context );
58 }
59
60 /**
61 * Build a test payload.
62 *
63 * @param string $webhook_id Webhook ID.
64 * @return array
65 */
66 public function build_test( $webhook_id ) {
67 return array(
68 'event' => 'test',
69 'timestamp' => gmdate( 'c' ),
70 'webhook_id' => $webhook_id,
71 'data' => array(
72 'message' => 'This is a test webhook from CartFlows.',
73 'funnel_id' => 0,
74 'step_id' => 0,
75 ),
76 );
77 }
78
79 /**
80 * Build event-specific data.
81 *
82 * @param string $event Event name.
83 * @param array $context Event context.
84 * @return array
85 */
86 private function build_event_data( $event, $context ) {
87 $data = array(
88 'funnel_id' => isset( $context['funnel_id'] ) ? absint( $context['funnel_id'] ) : 0,
89 'step_id' => isset( $context['step_id'] ) ? absint( $context['step_id'] ) : 0,
90 );
91
92 switch ( $event ) {
93 case 'cartflows_step_visited':
94 $data['step_type'] = isset( $context['step_type'] ) ? sanitize_text_field( $context['step_type'] ) : '';
95 $data['step_url'] = isset( $context['step_url'] ) ? esc_url_raw( $context['step_url'] ) : '';
96 break;
97
98 case 'checkout_initiated':
99 $data = array_merge( $data, $this->get_order_data( $context ) );
100 break;
101
102 case 'order_created':
103 case 'order_completed':
104 case 'wcf-main-order':
105 case 'order_failed':
106 $data = array_merge( $data, $this->get_order_data( $context ) );
107 break;
108
109 case 'order_bump_accepted':
110 case 'order_bump_skipped':
111 $data = array_merge( $data, $this->get_order_data( $context ) );
112 $data['bump_product'] = isset( $context['bump_product'] ) ? $context['bump_product'] : array();
113 break;
114
115 case 'upsell_accepted':
116 case 'upsell_skipped':
117 case 'downsell_accepted':
118 case 'downsell_skipped':
119 $data = array_merge( $data, $this->get_order_data( $context ) );
120 $data['offer_product'] = isset( $context['offer_product'] ) ? $context['offer_product'] : array();
121 break;
122 }
123
124 return $data;
125 }
126
127 /**
128 * Get order-related data from context.
129 *
130 * @param array $context Event context.
131 * @return array
132 */
133 private function get_order_data( $context ) {
134 $data = array();
135
136 if ( isset( $context['order_id'] ) ) {
137 $data['order_id'] = absint( $context['order_id'] );
138 }
139
140 $order = isset( $context['order'] ) ? $context['order'] : null;
141
142 if ( ! $order && isset( $context['order_id'] ) ) {
143 $order = wc_get_order( $context['order_id'] );
144 }
145
146 if ( $order && is_a( $order, 'WC_Order' ) ) {
147 $data['order_id'] = $order->get_id();
148 $data['customer'] = array(
149 'id' => $order->get_customer_id(),
150 'email' => $order->get_billing_email(),
151 'first_name' => $order->get_billing_first_name(),
152 'last_name' => $order->get_billing_last_name(),
153 );
154
155 $data['products'] = array();
156 foreach ( $order->get_items() as $item ) {
157 $product = $item->get_product();
158 $data['products'][] = array(
159 'id' => $product ? $product->get_id() : 0,
160 'name' => $item->get_name(),
161 'quantity' => $item->get_quantity(),
162 'total' => $item->get_total(),
163 );
164 }
165
166 $data['totals'] = array(
167 'total' => $order->get_total(),
168 'currency' => $order->get_currency(),
169 );
170 }
171
172 return $data;
173 }
174 }
175 }
176