# emailkit/1.4.0/includes/Admin/Api/ShortCodeData.php

EmailKit – Email Customizer for WooCommerce &amp; WP, version 1.4.0. 185 lines.

- Page: https://pluginprobe.com/plugins/emailkit/1.4.0/code/includes/Admin/Api/ShortCodeData.php
- Raw: https://pluginprobe.com/plugins/emailkit/1.4.0/raw/includes/Admin/Api/ShortCodeData.php
- Modified: 2023-12-31T12:21:24+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/emailkit/1.4.0/code/includes/Admin/Api/ShortCodeData.php#L10-L20`.

```php
<?php

namespace EmailKit\Admin\Api;

defined('ABSPATH') || exit;

class ShortCodeData {
    /**
     * @var string
     */
    public $prefix = '';
    /**
     * @var string
     */
    public $param = '';
    /**
     * @var mixed
     */
    public $request = null;

    public function __construct() {
        add_action('rest_api_init', function () {
            register_rest_route('emailkit/v1', 'order-data', [
                'methods' => \WP_REST_Server::ALLMETHODS,
                'callback' => [$this, 'get_order_data'],
                'permission_callback' => '__return_true'
            ]);
        });
    }

    /**
     * @param $request
     */
    public function get_order_data($request) {
        if(!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
            return [
                'status' => 'fail',
                'message' => ['Nonce mismatch.']
            ];
        }

        if(!is_user_logged_in() || !current_user_can('publish_posts')) {
            return [
                'status' => 'fail',
                'message' => ['Access denied.']
            ];
        }

        // Include necessary WordPress files
        require_once ABSPATH.'wp-admin/includes/plugin.php';
        require_once ABSPATH.'wp-includes/class-wp.php';  // Adjust the path if necessary



        $site_url = get_bloginfo('url');
        $current_user = wp_get_current_user();
        $user_name = $current_user->user_login;
       if(is_plugin_active('woocommerce/woocommerce.php')) {


            $orders = wc_get_orders([
                'limit' => 1,
                'orderby' => 'date',
                'order' => 'DESC',
                'status' => array(),
            ]);

            if(!empty($orders)) {

                $order = $orders[0];

                $order_data = [];

                foreach($order->get_items() as $item_id => $item) {
                    $product = $item->get_product();
                    $order_data[] = ['key' => 'product_name', 'value' => $product->get_name()];
                    $order_data[] = ['key' => 'product_price', 'value' => wc_price($product->get_price())];
                }

                $order_data[] = ['key' => 'order_id', 'value' => method_exists($order, 'get_id') ? $order->get_id() : '1'];
                $order_data[] = ['key' => 'order_status', 'value' => method_exists($order, 'get_status') ? $order->get_status() : 'pending'];
                $order_data[] = ['key' => 'billing_name', 'value' => method_exists($order, 'get_formatted_billing_full_name') ? $order->get_formatted_billing_full_name() : 'Jon Doe'];
                $order_data[] = ['key' => 'order_number', 'value' => method_exists($order, 'get_order_number') ? $order->get_order_number() : '1'];
                $order_data[] = ['key' => 'quantity', 'value' => method_exists($order, 'get_item_count') ? $order->get_item_count() : '2'];
                $order_data[] = ['key' => 'order_currency', 'value' => method_exists($order, 'get_currency') ? $order->get_currency() : '$'];
                $order_data[] = ['key' => 'billing_phone', 'value' => method_exists($order, 'get_billing_phone') ? $order->get_billing_phone() : '3456789'];
                $order_data[] = ['key' => 'shipping_total', 'value' => method_exists($order, 'get_shipping_total') ? wc_price($order->get_shipping_total()) : '$0.00'];
                $order_data[] = ['key' => 'order_subtotal', 'value' => method_exists($order, 'get_subtotal') ? wc_price($order->get_subtotal()) : '$0.00'];
                $order_data[] = ['key' => 'shipping_tax_total', 'value' => method_exists($order, 'get_shipping_tax') ? wc_format_decimal($order->get_shipping_tax(), 2) : '$0.00'];
                $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'];
                $order_data[] = ['key' => 'shipping_method', 'value' => method_exists($order, 'get_shipping_method') ? $order->get_shipping_method() : 'Cash'];
                $order_data[] = ['key' => 'payment_method', 'value' => method_exists($order, 'get_payment_method_title') ? $order->get_payment_method_title() : 'Cash On Delivery'];
                $order_data[] = ['key' => 'total', 'value' => method_exists($order, 'get_total') ? wc_price($order->get_total(), 2) : '$0.00'];
                $order_data[] = ['key' => 'billing_first_name', 'value' => method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : 'Jon'];
                $order_data[] = ['key' => 'billing_last_name', 'value' => method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : 'Doe'];
                $order_data[] = ['key' => 'billing_company', 'value' => method_exists($order, 'get_billing_company') ? $order->get_billing_company() : 'xyz.com'];
                $order_data[] = ['key' => 'billing_address_1', 'value' => method_exists($order, 'get_billing_address_1') ? $order->get_billing_address_1() : 'USA'];
                $order_data[] = ['key' => 'billing_address_2', 'value' => method_exists($order, 'get_billing_address_2') ? $order->get_billing_address_2() : 'USA'];
                $order_data[] = ['key' => 'billing_city', 'value' => method_exists($order, 'get_billing_city') ? $order->get_billing_city() : 'USA'];
                $order_data[] = ['key' => 'billing_state', 'value' => method_exists($order, 'get_billing_state') ? $order->get_billing_state() : 'NORTH CAROLINA'];
                $order_data[] = ['key' => 'billing_postcode', 'value' => method_exists($order, 'get_billing_postcode') ? $order->get_billing_postcode() : '123456'];
                $order_data[] = ['key' => 'billing_country', 'value' => method_exists($order, 'get_billing_country') ? $order->get_billing_country() : 'America'];
                $order_data[] = ['key' => 'billing_email', 'value' => method_exists($order, 'get_billing_email') ? $order->get_billing_email() : 'jondoe@example.com'];
                $order_data[] = ['key' => 'shipping_first_name', 'value' => method_exists($order, 'get_shipping_first_name') ? $order->get_shipping_first_name() : 'Jon'];
                $order_data[] = ['key' => 'shipping_last_name', 'value' => method_exists($order, 'get_shipping_last_name') ? $order->get_shipping_last_name() : 'Doe'];
                $order_data[] = ['key' => 'shipping_company', 'value' => method_exists($order, 'get_shipping_company') ? $order->get_shipping_company() : 'xyz.com'];
                $order_data[] = ['key' => 'shipping_address_1', 'value' => method_exists($order, 'get_shipping_address_1') ? $order->get_shipping_address_1() : 'USA'];
                $order_data[] = ['key' => 'shipping_address_2', 'value' => method_exists($order, 'get_shipping_address_2') ? $order->get_shipping_address_2() : 'USA'];
                $order_data[] = ['key' => 'shipping_city', 'value' => method_exists($order, 'get_shipping_city') ? $order->get_shipping_city() : 'America'];
                $order_data[] = ['key' => 'shipping_state', 'value' => method_exists($order, 'get_shipping_state') ? $order->get_shipping_state() : 'North Carolina'];
                $order_data[] = ['key' => 'shipping_postcode', 'value' => method_exists($order, 'get_shipping_postcode') ? $order->get_shipping_postcode() : '123456'];
                $order_data[] = ['key' => 'shipping_country', 'value' => method_exists($order, 'get_shipping_country') ? $order->get_shipping_country() : 'America'];
                $order_data[] = ['key' => 'customer_note', 'value' => method_exists($order, 'get_customer_note') ? $order->get_customer_note() : 'Happy To order'];
                $order_data[] = ['key' => 'site_url', 'value' => $site_url];
                $order_data[] = ['key' => 'user_name', 'value' => $user_name];

                return [
                    'status' => 'success',
                    'data' => $order_data,
                    'message' => 'WooCommerce order data retrieved successfully.'
                ];
            }
        else {

            return $this->get_demo_data();
        }
           
        }
            return $this->get_demo_data();
    }

    public function get_demo_data() {

        $site_url = get_bloginfo('url');
        $current_user = wp_get_current_user();
        $user_name = $current_user->user_login;

        $demo_data = [
            ['key' => 'order_id', 'value' => '1'],
            ['key' => 'order_number', 'value' => '1'],
            ['key' => 'order_status', 'value' => 'pending'],
            ['key' => 'billing_name', 'value' => 'Jon Doe'],
            ['key' => 'quantity', 'value' => '2'],
            ['key' => 'order_currency', 'value' => '$'],
            ['key' => 'billing_phone', 'value' => '3456789'],
            ['key' => 'shipping_total', 'value' => '$0.00'],
            ['key' => 'order_subtotal', 'value' => '$0.00'],
            ['key' => 'shipping_tax_total', 'value' => '$0.00'],
            ['key' => 'order_date', 'value' => '2020-01-01 00:00:00'],
            ['key' => 'shipping_method', 'value' => 'Cash'],
            ['key' => 'payment_method', 'value' => 'Cash On Delivery'],
            ['key' => 'total', 'value' => '$0.00'],
            ['key' => 'billing_first_name', 'value' => 'Jon'],
            ['key' => 'billing_last_name', 'value' => 'Doe'],
            ['key' => 'billing_company', 'value' => 'xyz.com'],
            ['key' => 'billing_address_1', 'value' => 'USA'],
            ['key' => 'billing_address_2', 'value' => 'USA'],
            ['key' => 'billing_city', 'value' => 'USA'],
            ['key' => 'billing_state', 'value' => 'NORTH CAROLINA'],
            ['key' => 'billing_postcode', 'value' => '123456'],
            ['key' => 'billing_country', 'value' => 'America'],
            ['key' => 'billing_email', 'value' => 'jondoe@example.com'],
            ['key' => 'shipping_first_name', 'value' => 'Jon'],
            ['key' => 'shipping_last_name', 'value' => 'Doe'],
            ['key' => 'shipping_company', 'value' => 'xyz.com'],
            ['key' => 'shipping_address_1', 'value' => 'USA'],
            ['key' => 'shipping_address_2', 'value' => 'USA'],
            ['key' => 'shipping_city', 'value' => 'America'],
            ['key' => 'shipping_state', 'value' => 'North Carolina'],
            ['key' => 'shipping_postcode', 'value' => '123456'],
            ['key' => 'shipping_country', 'value' => 'America'],
            ['key' => 'customer_note', 'value' => 'Happy To order'],
            ['key' => 'site_url', 'value' => $site_url],
            ['key' => 'user_name', 'value' => $user_name],

        ];

        return [
            'status' => 'success',
            'data' => $demo_data,
            'message' => 'No WooCommerce orders found. Returning demo data.'
        ];
    }
}

```
