class-api-for-cmbird.php
1 year ago
class-api-for-exact-webhooks.php
1 year ago
class-api-for-product-webhook.php
1 year ago
class-api-for-shipping-status.php
1 year ago
class-api-for-woo-order.php
1 year ago
class-api-for-zoho-inventory.php
1 year ago
class-commercebird-list-items-api-controller.php
1 year ago
class-commercebird-media-api-controller.php
1 year ago
class-commercebird-metadata-controller.php
1 year ago
index.php
1 year ago
trait-api-permission.php
1 year ago
class-api-for-cmbird.php
176 lines
| 1 | <?php |
| 2 | |
| 3 | namespace CommerceBird\API; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; |
| 7 | } |
| 8 | |
| 9 | use WP_REST_Response; |
| 10 | use WP_REST_Server; |
| 11 | use WP_REST_Controller; |
| 12 | |
| 13 | class CMBird_APIs extends WP_REST_Controller { |
| 14 | |
| 15 | protected $prefix = 'wc/v3'; |
| 16 | protected $rest_base = 'cmbird'; |
| 17 | |
| 18 | public function __construct() { |
| 19 | register_rest_route( |
| 20 | $this->prefix, |
| 21 | '/' . $this->rest_base . '/products-skus/', |
| 22 | array( |
| 23 | 'methods' => WP_REST_Server::READABLE, |
| 24 | 'callback' => array( $this, 'cmbird_get_all_product_with_skus' ), |
| 25 | 'permission_callback' => array( $this, 'permission_check' ), |
| 26 | ) |
| 27 | ); |
| 28 | // register route for setting purchase order automation settings |
| 29 | register_rest_route( |
| 30 | $this->prefix, |
| 31 | '/' . $this->rest_base . '/purchase-order-automation/', |
| 32 | array( |
| 33 | 'methods' => WP_REST_Server::CREATABLE, |
| 34 | 'callback' => array( $this, 'cmbird_set_purchase_order_automation_settings' ), |
| 35 | 'permission_callback' => array( $this, 'permission_check' ), |
| 36 | ) |
| 37 | ); |
| 38 | // register route for getting purchase order automation settings |
| 39 | register_rest_route( |
| 40 | $this->prefix, |
| 41 | '/' . $this->rest_base . '/purchase-order-automation/', |
| 42 | array( |
| 43 | 'methods' => WP_REST_Server::READABLE, |
| 44 | 'callback' => array( $this, 'cmbird_get_purchase_order_automation_settings' ), |
| 45 | 'permission_callback' => array( $this, 'permission_check' ), |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Check if a given request has access to get items. |
| 52 | */ |
| 53 | public function permission_check() { |
| 54 | return current_user_can( 'manage_woocommerce' ); |
| 55 | } |
| 56 | |
| 57 | public function cmbird_get_all_product_with_skus(): WP_REST_RESPONSE { |
| 58 | $rest_response = new WP_REST_Response(); |
| 59 | $rest_response->set_data( $this->empty_response ); |
| 60 | $rest_response->set_status( 200 ); |
| 61 | |
| 62 | global $wpdb; |
| 63 | |
| 64 | // Query to fetch product IDs and SKUs directly from the database |
| 65 | $results = $wpdb->get_results( |
| 66 | " |
| 67 | SELECT p.ID as product_id, pm.meta_value as sku, p.post_type as product_type |
| 68 | FROM {$wpdb->posts} p |
| 69 | LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = '_sku' |
| 70 | WHERE p.post_type IN ('product', 'product_variation') AND p.post_status = 'publish' |
| 71 | " |
| 72 | ); |
| 73 | |
| 74 | $rest_response->set_data( $results ); |
| 75 | |
| 76 | return rest_ensure_response( $rest_response ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Set purchase order automation settings. |
| 81 | * |
| 82 | * This endpoint expects a JSON payload in the following format: |
| 83 | * |
| 84 | * { |
| 85 | * "settings": { |
| 86 | * "enable_automation": bool, // Whether to enable automation for creating purchase orders. |
| 87 | * "cron_interval": string, // WordPress cron interval (e.g., 'hourly', 'twicedaily', 'daily'). |
| 88 | * "product_stock_status": array, // Array of stock statuses to monitor (e.g., 'low_stock', 'out_of_stock'). |
| 89 | * "send_to_vendor": bool // Whether to send purchase orders to vendors automatically. |
| 90 | * } |
| 91 | * } |
| 92 | * |
| 93 | * @param WP_REST_Request $request The REST API request object. |
| 94 | * @return WP_REST_Response A REST response indicating success or failure. |
| 95 | */ |
| 96 | public function cmbird_set_purchase_order_automation_settings( $request ) { |
| 97 | $rest_response = new WP_REST_Response(); |
| 98 | $rest_response->set_data( $this->empty_response ); |
| 99 | $rest_response->set_status( 200 ); |
| 100 | |
| 101 | if ( ! isset( $request['settings'] ) ) { |
| 102 | return rest_ensure_response( |
| 103 | array( |
| 104 | 'status' => false, |
| 105 | 'message' => 'Settings not found', |
| 106 | ) |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | $settings = $request->get_param('settings'); |
| 111 | |
| 112 | if ( ! empty( $settings ) ) { |
| 113 | update_option( 'cmbird_po_automation_settings', $settings, false ); |
| 114 | |
| 115 | // Handle cron job scheduling or clearing |
| 116 | if ( isset( $settings['enable_automation'] ) && true === $settings['enable_automation'] ) { |
| 117 | $interval = $settings['cron_interval'] ?? 'daily'; |
| 118 | |
| 119 | // Clear existing to prevent duplicates |
| 120 | wp_clear_scheduled_hook( 'cmbird_purchase_order_auto' ); |
| 121 | |
| 122 | if ( ! wp_next_scheduled( 'cmbird_purchase_order_auto' ) ) { |
| 123 | wp_schedule_event( time(), $interval, 'cmbird_purchase_order_auto' ); |
| 124 | } |
| 125 | } else { |
| 126 | // Disable automation: clear cron |
| 127 | wp_clear_scheduled_hook( 'cmbird_purchase_order_auto' ); |
| 128 | } |
| 129 | |
| 130 | return rest_ensure_response( |
| 131 | array( |
| 132 | 'status' => true, |
| 133 | 'message' => 'Settings updated successfully', |
| 134 | ) |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | return rest_ensure_response( |
| 139 | array( |
| 140 | 'status' => false, |
| 141 | 'message' => 'Settings not found', |
| 142 | ) |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get purchase order automation settings |
| 148 | * |
| 149 | * @param WP_REST_Request $request The REST API request object. |
| 150 | * @return WP_REST_Response A REST response containing the settings or an error message. |
| 151 | */ |
| 152 | public function cmbird_get_purchase_order_automation_settings( $request ) { |
| 153 | $rest_response = new WP_REST_Response(); |
| 154 | $rest_response->set_data( $this->empty_response ); |
| 155 | $rest_response->set_status( 200 ); |
| 156 | |
| 157 | $settings = get_option( 'cmbird_po_automation_settings' ); |
| 158 | |
| 159 | if ( ! empty( $settings ) ) { |
| 160 | return rest_ensure_response( |
| 161 | array( |
| 162 | 'status' => true, |
| 163 | 'data' => $settings, |
| 164 | ) |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | return rest_ensure_response( |
| 169 | array( |
| 170 | 'status' => false, |
| 171 | 'message' => 'Settings not found', |
| 172 | ) |
| 173 | ); |
| 174 | } |
| 175 | } |
| 176 |