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

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