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
11 months ago
trait-api-permission.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | namespace CommerceBird\API; |
| 4 | |
| 5 | use Exception; |
| 6 | // use CommerceBird\Admin\Actions\Ajax\ZohoInventoryAjax; |
| 7 | use CommerceBird\Admin\Actions\Ajax\SettingsAjax; |
| 8 | use WP_Error; |
| 9 | use WP_REST_Request; |
| 10 | use WP_REST_Response; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | trait Api { |
| 17 | |
| 18 | private static string $namespace = 'v2'; |
| 19 | |
| 20 | private string $empty_response = 'No data found from webhook. check on ' . __METHOD__ . ' of ' . __CLASS__ . ' on line ' . __LINE__; |
| 21 | |
| 22 | public static function endpoint(): string { |
| 23 | return get_rest_url() . self::$namespace . '/' . self::$endpoint; |
| 24 | } |
| 25 | |
| 26 | public function permission_check() { |
| 27 | // Get all headers |
| 28 | $headers = getallheaders(); |
| 29 | // Check if the Authorization header is present |
| 30 | if ( isset( $headers['Authorization'] ) ) { |
| 31 | $authorization = $headers['Authorization']; |
| 32 | if ( ! password_verify( 'commercebird-zi-webhook-token', $authorization ) ) { |
| 33 | return new WP_Error( |
| 34 | 'rest_forbidden', |
| 35 | 'You are not allowed to access this endpoint', |
| 36 | array( |
| 37 | 'status' => 400, |
| 38 | 'header' => $authorization, |
| 39 | ) |
| 40 | ); |
| 41 | } |
| 42 | } |
| 43 | // $subscription = ZohoInventoryAjax::instance()->get_subscription_data(); |
| 44 | $subscription = SettingsAjax::instance()->get_subscription_data(); |
| 45 | |
| 46 | if ( isset( $subscription['plan'] ) ) { |
| 47 | $subscription_plan = implode( ' ', $subscription['plan'] ); |
| 48 | if ( stripos( $subscription_plan, 'Premium' ) === false ) { |
| 49 | return new WP_Error( |
| 50 | 'rest_forbidden', |
| 51 | $subscription_plan, |
| 52 | array( |
| 53 | 'status' => 403, |
| 54 | 'data' => strpos( 'Premium', $subscription_plan ), |
| 55 | ) |
| 56 | ); |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | public function handle( WP_REST_Request $request ) { |
| 62 | $response = new WP_REST_Response(); |
| 63 | $response->set_data( $this->empty_response ); |
| 64 | $response->set_status( 404 ); |
| 65 | $data = $request->get_json_params(); |
| 66 | if ( empty( $data ) ) { |
| 67 | return rest_ensure_response( $response ); |
| 68 | } |
| 69 | if ( array_key_exists( 'JSONString', $data ) ) { |
| 70 | $data = str_replace( '\\', '', $data['JSONString'] ); |
| 71 | } |
| 72 | // Create a unique key based on the payload |
| 73 | $payload_hash = wp_hash( wp_json_encode( $data ) ); |
| 74 | $lock_key = 'cmbird_processing_payload_' . $payload_hash; |
| 75 | // Check and set lock using update_option |
| 76 | $lock_acquired = add_option( $lock_key, time() ); |
| 77 | if ( ! $lock_acquired ) { |
| 78 | $response->set_data( 'Duplicate payload ignored. Already being processed.' ); |
| 79 | $response->set_status( 200 ); |
| 80 | return rest_ensure_response( $response ); |
| 81 | } |
| 82 | // Process the payload |
| 83 | if ( ! empty( $data ) ) { |
| 84 | try { |
| 85 | $response = $this->process( $data ); |
| 86 | } catch (Exception $exception) { |
| 87 | $response->set_data( $exception->getMessage() ); |
| 88 | $response->set_status( 500 ); |
| 89 | } |
| 90 | } |
| 91 | // Clean up the lock after processing |
| 92 | delete_option( $lock_key ); |
| 93 | return rest_ensure_response( $response ); |
| 94 | } |
| 95 | } |
| 96 |