PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.14
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.14
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / includes / classes / apis / trait-api-permission.php
commercebird / includes / classes / apis Last commit date
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