PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.4.5
EmailKit – Email Customizer for WooCommerce & WP v1.4.5
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.5, at includes/Admin/Api/ShortCodeData.php

221 lines 12.5 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' => [ esc_html__( 'Nonce mismatch.', 'emailkit' )]
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';
52
53 $short_codes = [];
54 $current_user = wp_get_current_user();
55 $user_name = $current_user->user_login;
56 $display_name = $current_user->display_name;
57 $user_email = $current_user->user_email;
58 $app_name = get_bloginfo('name');
59
60 // Site shortcodes
61 $short_codes[] = ['key' => 'site_name', 'value' => get_bloginfo( 'name' )];
62 $short_codes[] = ['key' => 'site_url', 'value' => get_bloginfo( 'url')];
63 $short_codes[] = ['key' => 'user_name', 'value' => $user_name];
64 $short_codes[] = ['key' => 'display_name', 'value' => $display_name];
65 $short_codes[] = ['key' => 'wp_user_email', 'value' => $user_email];
66 $short_codes[] = ['key' => 'app_name', 'value' => $app_name];
67
68
69 if(is_plugin_active('woocommerce/woocommerce.php')) {
70
71
72 $orders = wc_get_orders([
73 'limit' => 1,
74 'orderby' => 'date',
75 'order' => 'DESC',
76 'status' => array(),
77 ]);
78
79 if(!empty($orders)) {
80
81 $order = $orders[0];
82
83
84 foreach($order->get_items() as $item_id => $item) {
85 $product = $item->get_product();
86 $short_codes[] = ['key' => 'product_name', 'value' => $product->get_name()];
87 $short_codes[] = ['key' => 'product_price', 'value' => wc_price($product->get_price())];
88 }
89
90 $customer = $order->get_user();
91
92 // Include customer name and email in the order data
93 if ($customer) {
94 $short_codes[] = ['key' => 'user_login', 'value' => $customer->display_name];
95 $short_codes[] = ['key' => 'user_email', 'value' => $customer->user_email];
96 }
97 // Order shortcodes
98 $short_codes[] = ['key' => 'product_name', 'value' => 'T-Shirt with logo'];
99 $short_codes[] = ['key' => 'quantity', 'value' => method_exists($order, 'get_item_count') ? $order->get_item_count() : '2'];
100 $short_codes[] = ['key' => 'order_id', 'value' => method_exists($order, 'get_id') ? $order->get_id() : '1'];
101 $short_codes[] = ['key' => 'order_status', 'value' => method_exists($order, 'get_status') ? $order->get_status() : 'pending'];
102 $short_codes[] = ['key' => 'order_number', 'value' => method_exists($order, 'get_order_number') ? $order->get_order_number() : '1'];
103 $short_codes[] = ['key' => 'order_currency', 'value' => method_exists($order, 'get_currency') ? $order->get_currency() : '$'];
104 $short_codes[] = ['key' => 'order_subtotal', 'value' => method_exists($order, 'get_subtotal') ? wc_price($order->get_subtotal()) : '$0.00'];
105 $short_codes[] = ['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'];
106 $short_codes[] = ['key' => 'payment_method', 'value' => method_exists($order, 'get_payment_method_title') ? $order->get_payment_method_title() : 'Cash On Delivery'];
107 $short_codes[] = ['key' => 'total', 'value' => method_exists($order, 'get_total') ? wc_price($order->get_total(), 2) : '$0.00'];
108 $short_codes[] = ['key' => 'customer_note', 'value' => method_exists($order, 'get_customer_note') ? $order->get_customer_note() : esc_html__('Happy To order', 'emailkti')];
109
110 // Billing shortcodes
111 $short_codes[] = ['key' => 'billing_name', 'value' => method_exists($order, 'get_formatted_billing_full_name') ? $order->get_formatted_billing_full_name() : 'Jon Doe'];
112 $short_codes[] = ['key' => 'billing_first_name', 'value' => method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : 'Jon'];
113 $short_codes[] = ['key' => 'billing_last_name', 'value' => method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : 'Doe'];
114 $short_codes[] = ['key' => 'billing_email', 'value' => method_exists($order, 'get_billing_email') ? $order->get_billing_email() : 'jondoe@example.com'];
115 $short_codes[] = ['key' => 'billing_phone', 'value' => method_exists($order, 'get_billing_phone') ? $order->get_billing_phone() : '3456789'];
116 $short_codes[] = ['key' => 'billing_company', 'value' => method_exists($order, 'get_billing_company') ? $order->get_billing_company() : 'xyz.com'];
117 $short_codes[] = ['key' => 'billing_address_1', 'value' => method_exists($order, 'get_billing_address_1') ? $order->get_billing_address_1() : 'USA'];
118 $short_codes[] = ['key' => 'billing_address_2', 'value' => method_exists($order, 'get_billing_address_2') ? $order->get_billing_address_2() : 'USA'];
119 $short_codes[] = ['key' => 'billing_city', 'value' => method_exists($order, 'get_billing_city') ? $order->get_billing_city() : 'USA'];
120 $short_codes[] = ['key' => 'billing_country', 'value' => method_exists($order, 'get_billing_country') ? $order->get_billing_country() : 'America'];
121 $short_codes[] = ['key' => 'billing_postcode', 'value' => method_exists($order, 'get_billing_postcode') ? $order->get_billing_postcode() : '123456'];
122 $short_codes[] = ['key' => 'billing_state', 'value' => method_exists($order, 'get_billing_state') ? $order->get_billing_state() : 'NORTH CAROLINA'];
123
124 // Shipping shortcodes
125 $short_codes[] = ['key' => 'shipping_first_name', 'value' => method_exists($order, 'get_shipping_first_name') ? $order->get_shipping_first_name() : 'Jon'];
126 $short_codes[] = ['key' => 'shipping_last_name', 'value' => method_exists($order, 'get_shipping_last_name') ? $order->get_shipping_last_name() : 'Doe'];
127 $short_codes[] = ['key' => 'shipping_total', 'value' => method_exists($order, 'get_shipping_total') ? wc_price($order->get_shipping_total()) : '$0.00'];
128 $short_codes[] = ['key' => 'shipping_tax_total', 'value' => method_exists($order, 'get_shipping_tax') ? wc_format_decimal($order->get_shipping_tax(), 2) : '$0.00'];
129 $short_codes[] = ['key' => 'shipping_company', 'value' => method_exists($order, 'get_shipping_company') ? $order->get_shipping_company() : 'xyz.com'];
130 $short_codes[] = ['key' => 'shipping_address_1', 'value' => method_exists($order, 'get_shipping_address_1') ? $order->get_shipping_address_1() : 'USA'];
131 $short_codes[] = ['key' => 'shipping_address_2', 'value' => method_exists($order, 'get_shipping_address_2') ? $order->get_shipping_address_2() : 'USA'];
132 $short_codes[] = ['key' => 'shipping_method', 'value' => method_exists($order, 'get_shipping_method') ? $order->get_shipping_method() : 'Cash'];
133 $short_codes[] = ['key' => 'shipping_city', 'value' => method_exists($order, 'get_shipping_city') ? $order->get_shipping_city() : 'America'];
134 $short_codes[] = ['key' => 'shipping_state', 'value' => method_exists($order, 'get_shipping_state') ? $order->get_shipping_state() : 'North Carolina'];
135 $short_codes[] = ['key' => 'shipping_postcode', 'value' => method_exists($order, 'get_shipping_postcode') ? $order->get_shipping_postcode() : '123456'];
136 $short_codes[] = ['key' => 'shipping_country', 'value' => method_exists($order, 'get_shipping_country') ? $order->get_shipping_country() : 'America'];
137
138
139
140 return [
141 'status' => 'success',
142 'data' => $short_codes,
143 'message'=> esc_html__( 'WooCommerce order data retrieved successfully.', 'emailkit')
144 ];
145 }
146 else {
147
148 return $this->get_demo_data();
149 }
150
151 }
152 return $this->get_demo_data();
153 }
154
155 public function get_demo_data() {
156
157 $site_url = get_bloginfo('url');
158 $current_user = wp_get_current_user();
159 $user_name = $current_user->user_login;
160 $display_name = $current_user->display_name;
161 $user_email = $current_user->user_email;
162 $app_name = get_bloginfo('name');
163
164 $demo_data = [
165 ['key' => 'order_id', 'value' => '1'],
166 ['key' => 'order_number', 'value' => '1'],
167 ['key' => 'order_status', 'value' => 'pending'],
168 ['key' => 'billing_name', 'value' => 'Jon Doe'],
169 ['key' => 'quantity', 'value' => '2'],
170 ['key' => 'order_currency', 'value' => '$'],
171 ['key' => 'billing_phone', 'value' => '3456789'],
172 ['key' => 'shipping_total', 'value' => '$0.00'],
173 ['key' => 'order_subtotal', 'value' => '$0.00'],
174 ['key' => 'shipping_tax_total', 'value' => '$0.00'],
175 ['key' => 'order_date', 'value' => '2020-01-01 00:00:00'],
176 ['key' => 'shipping_method', 'value' => 'Cash'],
177 ['key' => 'payment_method', 'value' => 'Cash On Delivery'],
178 ['key' => 'total', 'value' => '$0.00'],
179 ['key' => 'billing_first_name', 'value' => 'Jon'],
180 ['key' => 'billing_last_name', 'value' => 'Doe'],
181 ['key' => 'billing_company', 'value' => 'xyz.com'],
182 ['key' => 'billing_address_1', 'value' => 'USA'],
183 ['key' => 'billing_address_2', 'value' => 'USA'],
184 ['key' => 'billing_city', 'value' => 'USA'],
185 ['key' => 'billing_state', 'value' => 'NORTH CAROLINA'],
186 ['key' => 'billing_postcode', 'value' => '123456'],
187 ['key' => 'billing_country', 'value' => 'America'],
188 ['key' => 'billing_email', 'value' => 'jondoe@example.com'],
189 ['key' => 'shipping_first_name', 'value' => 'Jon'],
190 ['key' => 'shipping_last_name', 'value' => 'Doe'],
191 ['key' => 'shipping_company', 'value' => 'xyz.com'],
192 ['key' => 'shipping_address_1', 'value' => 'USA'],
193 ['key' => 'shipping_address_2', 'value' => 'USA'],
194 ['key' => 'shipping_city', 'value' => 'America'],
195 ['key' => 'shipping_state', 'value' => 'North Carolina'],
196 ['key' => 'shipping_postcode', 'value' => '123456'],
197 ['key' => 'shipping_country', 'value' => 'America'],
198 ['key' => 'customer_note', 'value' => 'Happy To order'],
199 ['key' => 'site_name', 'value' => get_bloginfo( 'name' )],
200 ['key' => 'site_url', 'value' => $site_url],
201 ['key' => 'user_name', 'value' => $user_name],
202 ['key' => 'product_name', 'value' => 'T-Shirt with logo'],
203 ['key' => 'admin_email', 'value' => 'admin@site.com'],
204 ['key' => 'user_login', 'value' => 'admin'],
205 ['key' => 'user_email', 'value' => 'jondoe@example.com'],
206 ['key' => 'site_url', 'value' => $site_url],
207 ['key' => 'display_name', 'value' => $display_name],
208 ['key' => 'wp_user_email', 'value' => $user_email],
209 ['key' => 'app_name', 'value' => $app_name],
210
211
212 ];
213
214 return [
215 'status' => 'success',
216 'data' => $demo_data,
217 'message' => esc_html__( 'No WooCommerce orders found. Returning demo data.', 'emailkit')
218 ];
219 }
220 }
221