PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.4.0
EmailKit – Email Customizer for WooCommerce & WP v1.4.0
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / includes / Admin / Api / ShortCodeData.php

ShortCodeData.php in EmailKit – Email Customizer for WooCommerce & WP 1.4.0, at includes/Admin/Api/ShortCodeData.php

185 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace EmailKit\Admin\Api;
4
5 defined('ABSPATH') || exit;
6
7 class ShortCodeData {
8 /**
9 * @var string
10 */
11 public $prefix = '';
12 /**
13 * @var string
14 */
15 public $param = '';
16 /**
17 * @var mixed
18 */
19 public $request = null;
20
21 public function __construct() {
22 add_action('rest_api_init', function () {
23 register_rest_route('emailkit/v1', 'order-data', [
24 'methods' => \WP_REST_Server::ALLMETHODS,
25 'callback' => [$this, 'get_order_data'],
26 'permission_callback' => '__return_true'
27 ]);
28 });
29 }
30
31 /**
32 * @param $request
33 */
34 public function get_order_data($request) {
35 if(!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
36 return [
37 'status' => 'fail',
38 'message' => ['Nonce mismatch.']
39 ];
40 }
41
42 if(!is_user_logged_in() || !current_user_can('publish_posts')) {
43 return [
44 'status' => 'fail',
45 'message' => ['Access denied.']
46 ];
47 }
48
49 // Include necessary WordPress files
50 require_once ABSPATH.'wp-admin/includes/plugin.php';
51 require_once ABSPATH.'wp-includes/class-wp.php'; // Adjust the path if necessary
52
53
54
55 $site_url = get_bloginfo('url');
56 $current_user = wp_get_current_user();
57 $user_name = $current_user->user_login;
58 if(is_plugin_active('woocommerce/woocommerce.php')) {
59
60
61 $orders = wc_get_orders([
62 'limit' => 1,
63 'orderby' => 'date',
64 'order' => 'DESC',
65 'status' => array(),
66 ]);
67
68 if(!empty($orders)) {
69
70 $order = $orders[0];
71
72 $order_data = [];
73
74 foreach($order->get_items() as $item_id => $item) {
75 $product = $item->get_product();
76 $order_data[] = ['key' => 'product_name', 'value' => $product->get_name()];
77 $order_data[] = ['key' => 'product_price', 'value' => wc_price($product->get_price())];
78 }
79
80 $order_data[] = ['key' => 'order_id', 'value' => method_exists($order, 'get_id') ? $order->get_id() : '1'];
81 $order_data[] = ['key' => 'order_status', 'value' => method_exists($order, 'get_status') ? $order->get_status() : 'pending'];
82 $order_data[] = ['key' => 'billing_name', 'value' => method_exists($order, 'get_formatted_billing_full_name') ? $order->get_formatted_billing_full_name() : 'Jon Doe'];
83 $order_data[] = ['key' => 'order_number', 'value' => method_exists($order, 'get_order_number') ? $order->get_order_number() : '1'];
84 $order_data[] = ['key' => 'quantity', 'value' => method_exists($order, 'get_item_count') ? $order->get_item_count() : '2'];
85 $order_data[] = ['key' => 'order_currency', 'value' => method_exists($order, 'get_currency') ? $order->get_currency() : '$'];
86 $order_data[] = ['key' => 'billing_phone', 'value' => method_exists($order, 'get_billing_phone') ? $order->get_billing_phone() : '3456789'];
87 $order_data[] = ['key' => 'shipping_total', 'value' => method_exists($order, 'get_shipping_total') ? wc_price($order->get_shipping_total()) : '$0.00'];
88 $order_data[] = ['key' => 'order_subtotal', 'value' => method_exists($order, 'get_subtotal') ? wc_price($order->get_subtotal()) : '$0.00'];
89 $order_data[] = ['key' => 'shipping_tax_total', 'value' => method_exists($order, 'get_shipping_tax') ? wc_format_decimal($order->get_shipping_tax(), 2) : '$0.00'];
90 $order_data[] = ['key' => 'order_date', 'value' => method_exists($order, 'get_date_created') ? gmdate('Y-m-d H:i:s', strtotime($order->get_date_created()->format('Y-m-d H:i:s'))) : '2020-01-01 00:00:00'];
91 $order_data[] = ['key' => 'shipping_method', 'value' => method_exists($order, 'get_shipping_method') ? $order->get_shipping_method() : 'Cash'];
92 $order_data[] = ['key' => 'payment_method', 'value' => method_exists($order, 'get_payment_method_title') ? $order->get_payment_method_title() : 'Cash On Delivery'];
93 $order_data[] = ['key' => 'total', 'value' => method_exists($order, 'get_total') ? wc_price($order->get_total(), 2) : '$0.00'];
94 $order_data[] = ['key' => 'billing_first_name', 'value' => method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : 'Jon'];
95 $order_data[] = ['key' => 'billing_last_name', 'value' => method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : 'Doe'];
96 $order_data[] = ['key' => 'billing_company', 'value' => method_exists($order, 'get_billing_company') ? $order->get_billing_company() : 'xyz.com'];
97 $order_data[] = ['key' => 'billing_address_1', 'value' => method_exists($order, 'get_billing_address_1') ? $order->get_billing_address_1() : 'USA'];
98 $order_data[] = ['key' => 'billing_address_2', 'value' => method_exists($order, 'get_billing_address_2') ? $order->get_billing_address_2() : 'USA'];
99 $order_data[] = ['key' => 'billing_city', 'value' => method_exists($order, 'get_billing_city') ? $order->get_billing_city() : 'USA'];
100 $order_data[] = ['key' => 'billing_state', 'value' => method_exists($order, 'get_billing_state') ? $order->get_billing_state() : 'NORTH CAROLINA'];
101 $order_data[] = ['key' => 'billing_postcode', 'value' => method_exists($order, 'get_billing_postcode') ? $order->get_billing_postcode() : '123456'];
102 $order_data[] = ['key' => 'billing_country', 'value' => method_exists($order, 'get_billing_country') ? $order->get_billing_country() : 'America'];
103 $order_data[] = ['key' => 'billing_email', 'value' => method_exists($order, 'get_billing_email') ? $order->get_billing_email() : 'jondoe@example.com'];
104 $order_data[] = ['key' => 'shipping_first_name', 'value' => method_exists($order, 'get_shipping_first_name') ? $order->get_shipping_first_name() : 'Jon'];
105 $order_data[] = ['key' => 'shipping_last_name', 'value' => method_exists($order, 'get_shipping_last_name') ? $order->get_shipping_last_name() : 'Doe'];
106 $order_data[] = ['key' => 'shipping_company', 'value' => method_exists($order, 'get_shipping_company') ? $order->get_shipping_company() : 'xyz.com'];
107 $order_data[] = ['key' => 'shipping_address_1', 'value' => method_exists($order, 'get_shipping_address_1') ? $order->get_shipping_address_1() : 'USA'];
108 $order_data[] = ['key' => 'shipping_address_2', 'value' => method_exists($order, 'get_shipping_address_2') ? $order->get_shipping_address_2() : 'USA'];
109 $order_data[] = ['key' => 'shipping_city', 'value' => method_exists($order, 'get_shipping_city') ? $order->get_shipping_city() : 'America'];
110 $order_data[] = ['key' => 'shipping_state', 'value' => method_exists($order, 'get_shipping_state') ? $order->get_shipping_state() : 'North Carolina'];
111 $order_data[] = ['key' => 'shipping_postcode', 'value' => method_exists($order, 'get_shipping_postcode') ? $order->get_shipping_postcode() : '123456'];
112 $order_data[] = ['key' => 'shipping_country', 'value' => method_exists($order, 'get_shipping_country') ? $order->get_shipping_country() : 'America'];
113 $order_data[] = ['key' => 'customer_note', 'value' => method_exists($order, 'get_customer_note') ? $order->get_customer_note() : 'Happy To order'];
114 $order_data[] = ['key' => 'site_url', 'value' => $site_url];
115 $order_data[] = ['key' => 'user_name', 'value' => $user_name];
116
117 return [
118 'status' => 'success',
119 'data' => $order_data,
120 'message' => 'WooCommerce order data retrieved successfully.'
121 ];
122 }
123 else {
124
125 return $this->get_demo_data();
126 }
127
128 }
129 return $this->get_demo_data();
130 }
131
132 public function get_demo_data() {
133
134 $site_url = get_bloginfo('url');
135 $current_user = wp_get_current_user();
136 $user_name = $current_user->user_login;
137
138 $demo_data = [
139 ['key' => 'order_id', 'value' => '1'],
140 ['key' => 'order_number', 'value' => '1'],
141 ['key' => 'order_status', 'value' => 'pending'],
142 ['key' => 'billing_name', 'value' => 'Jon Doe'],
143 ['key' => 'quantity', 'value' => '2'],
144 ['key' => 'order_currency', 'value' => '$'],
145 ['key' => 'billing_phone', 'value' => '3456789'],
146 ['key' => 'shipping_total', 'value' => '$0.00'],
147 ['key' => 'order_subtotal', 'value' => '$0.00'],
148 ['key' => 'shipping_tax_total', 'value' => '$0.00'],
149 ['key' => 'order_date', 'value' => '2020-01-01 00:00:00'],
150 ['key' => 'shipping_method', 'value' => 'Cash'],
151 ['key' => 'payment_method', 'value' => 'Cash On Delivery'],
152 ['key' => 'total', 'value' => '$0.00'],
153 ['key' => 'billing_first_name', 'value' => 'Jon'],
154 ['key' => 'billing_last_name', 'value' => 'Doe'],
155 ['key' => 'billing_company', 'value' => 'xyz.com'],
156 ['key' => 'billing_address_1', 'value' => 'USA'],
157 ['key' => 'billing_address_2', 'value' => 'USA'],
158 ['key' => 'billing_city', 'value' => 'USA'],
159 ['key' => 'billing_state', 'value' => 'NORTH CAROLINA'],
160 ['key' => 'billing_postcode', 'value' => '123456'],
161 ['key' => 'billing_country', 'value' => 'America'],
162 ['key' => 'billing_email', 'value' => 'jondoe@example.com'],
163 ['key' => 'shipping_first_name', 'value' => 'Jon'],
164 ['key' => 'shipping_last_name', 'value' => 'Doe'],
165 ['key' => 'shipping_company', 'value' => 'xyz.com'],
166 ['key' => 'shipping_address_1', 'value' => 'USA'],
167 ['key' => 'shipping_address_2', 'value' => 'USA'],
168 ['key' => 'shipping_city', 'value' => 'America'],
169 ['key' => 'shipping_state', 'value' => 'North Carolina'],
170 ['key' => 'shipping_postcode', 'value' => '123456'],
171 ['key' => 'shipping_country', 'value' => 'America'],
172 ['key' => 'customer_note', 'value' => 'Happy To order'],
173 ['key' => 'site_url', 'value' => $site_url],
174 ['key' => 'user_name', 'value' => $user_name],
175
176 ];
177
178 return [
179 'status' => 'success',
180 'data' => $demo_data,
181 'message' => 'No WooCommerce orders found. Returning demo data.'
182 ];
183 }
184 }
185