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