diff --git a/.idea/laravel-woocommerce.iml b/.idea/laravel-woocommerce.iml new file mode 100644 index 0000000..5b9a05d --- /dev/null +++ b/.idea/laravel-woocommerce.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d85c6fc --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..5333dec --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + diff --git a/.idea/phpunit.xml b/.idea/phpunit.xml new file mode 100644 index 0000000..776785b --- /dev/null +++ b/.idea/phpunit.xml @@ -0,0 +1,10 @@ + + + + + + diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..dcb6b8c --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..8f576d6 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + $PROJECT_DIR$/composer.json + + + + + + + + + + + + + + + + + + + + + + + 1704112530708 + + + + + + + + + diff --git a/src/Models/Attribute.php b/src/Models/Attribute.php index 807328e..ee0cea0 100644 --- a/src/Models/Attribute.php +++ b/src/Models/Attribute.php @@ -21,7 +21,7 @@ class Attribute extends BaseModel */ protected function getTerms($attribute_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->all($options); } @@ -37,7 +37,7 @@ protected function getTerms($attribute_id, $options = []) */ protected function getTerm($attribute_id, $term_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->find($term_id, $options); } @@ -52,7 +52,7 @@ protected function getTerm($attribute_id, $term_id, $options = []) */ protected function addTerm($attribute_id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->create($data); } @@ -68,7 +68,7 @@ protected function addTerm($attribute_id, $data) */ protected function updateTerm($attribute_id, $term_id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->update($term_id, $data); } @@ -84,7 +84,7 @@ protected function updateTerm($attribute_id, $term_id, $data) */ protected function deleteTerm($attribute_id, $term_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->delete($term_id, $options); } @@ -99,7 +99,7 @@ protected function deleteTerm($attribute_id, $term_id, $options = []) */ protected function batchTerm($attribute_id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->batch($data); } diff --git a/src/Models/Customer.php b/src/Models/Customer.php index ea5be61..2b323cb 100644 --- a/src/Models/Customer.php +++ b/src/Models/Customer.php @@ -20,7 +20,7 @@ class Customer extends BaseModel */ protected function downloads($id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("customers/{$id}/downloads") ->all($options); } diff --git a/src/Models/Note.php b/src/Models/Note.php index cccb4b7..7220690 100644 --- a/src/Models/Note.php +++ b/src/Models/Note.php @@ -21,7 +21,7 @@ class Note extends BaseModel */ protected function all($order_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/notes") ->all($options); } @@ -37,7 +37,7 @@ protected function all($order_id, $options = []) */ protected function find($order_id, $note_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/notes") ->find($note_id, $options); } @@ -52,7 +52,7 @@ protected function find($order_id, $note_id, $options = []) */ protected function create($order_id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/notes") ->create($data); } @@ -68,7 +68,7 @@ protected function create($order_id, $data) */ protected function delete($order_id, $note_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/notes") ->delete($note_id, $options); } @@ -90,7 +90,7 @@ protected function paginate( $current_page = 1, $options = [] ) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/notes") ->paginate($per_page, $current_page, $options); } @@ -104,7 +104,7 @@ protected function paginate( */ protected function count($order_id) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/notes") ->count(); } @@ -118,7 +118,7 @@ protected function count($order_id) */ public function save($order_id) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/notes") ->save(); } diff --git a/src/Models/Order.php b/src/Models/Order.php index e2b109e..ca4896d 100644 --- a/src/Models/Order.php +++ b/src/Models/Order.php @@ -21,7 +21,7 @@ class Order extends BaseModel */ protected function notes($order_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/notes") ->all($options); } @@ -37,7 +37,7 @@ protected function notes($order_id, $options = []) */ protected function note($order_id, $note_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/notes") ->find($note_id, $options); } @@ -52,7 +52,7 @@ protected function note($order_id, $note_id, $options = []) */ protected function createNote($order_id, $data = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/notes") ->create($data); } @@ -68,7 +68,7 @@ protected function createNote($order_id, $data = []) */ protected function deleteNote($order_id, $note_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/notes") ->delete($note_id, $options); } @@ -83,7 +83,7 @@ protected function deleteNote($order_id, $note_id, $options = []) */ protected function refunds($order_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/refunds") ->all($options); } @@ -99,7 +99,7 @@ protected function refunds($order_id, $options = []) */ protected function refund($order_id, $refund_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/refunds") ->find($refund_id, $options); } @@ -114,7 +114,7 @@ protected function refund($order_id, $refund_id, $options = []) */ protected function createRefund($order_id, $data = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/refunds") ->create($data); } @@ -130,7 +130,7 @@ protected function createRefund($order_id, $data = []) */ protected function deleteRefund($order_id, $refund_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/refunds") ->delete($refund_id, $options); } diff --git a/src/Models/Refund.php b/src/Models/Refund.php index c6a89c4..f558102 100644 --- a/src/Models/Refund.php +++ b/src/Models/Refund.php @@ -21,7 +21,7 @@ class Refund extends BaseModel */ protected function all($order_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/refunds") ->all($options); } @@ -37,7 +37,7 @@ protected function all($order_id, $options = []) */ protected function find($order_id, $refund_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/refunds") ->find($refund_id, $options); } @@ -52,7 +52,7 @@ protected function find($order_id, $refund_id, $options = []) */ protected function create($order_id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/refunds") ->create($data); } @@ -68,7 +68,7 @@ protected function create($order_id, $data) */ protected function delete($order_id, $refund_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/refunds") ->delete($refund_id, $options); } @@ -89,7 +89,7 @@ protected function paginate( $current_page = 1, $options = [] ) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/refunds") ->paginate($per_page, $current_page, $options); } @@ -103,7 +103,7 @@ protected function paginate( */ protected function count($order_id) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/refunds") ->count(); } @@ -117,7 +117,7 @@ protected function count($order_id) */ public function save($order_id) { - return Query::init() + return Query::init($this->config) ->setEndpoint("orders/{$order_id}/refunds") ->save(); } diff --git a/src/Models/Report.php b/src/Models/Report.php index 943bcb5..c0af879 100644 --- a/src/Models/Report.php +++ b/src/Models/Report.php @@ -20,7 +20,7 @@ class Report extends BaseModel */ protected function sales($options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint('reports/sales') ->all($options); } @@ -34,7 +34,7 @@ protected function sales($options = []) */ protected function topSellers($options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint('reports/top_sellers') ->all($options); } @@ -48,7 +48,7 @@ protected function topSellers($options = []) */ protected function coupons($options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint('reports/coupons/totals') ->all($options); } @@ -62,7 +62,7 @@ protected function coupons($options = []) */ protected function customers($options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint('reports/customers/totals') ->all($options); } @@ -76,7 +76,7 @@ protected function customers($options = []) */ protected function orders($options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint('reports/orders/totals') ->all($options); } @@ -90,7 +90,7 @@ protected function orders($options = []) */ protected function products($options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint('reports/products/totals') ->all($options); } @@ -104,7 +104,7 @@ protected function products($options = []) */ protected function reviews($options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint('reports/reviews/totals') ->all($options); } diff --git a/src/Models/Setting.php b/src/Models/Setting.php index 7dab28e..b59c5a2 100644 --- a/src/Models/Setting.php +++ b/src/Models/Setting.php @@ -22,7 +22,7 @@ class Setting extends BaseModel */ protected function option($group_id, $id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("settings/{$group_id}") ->find($id, $options); } @@ -37,7 +37,7 @@ protected function option($group_id, $id, $options = []) */ protected function options($id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint('settings') ->find($id, $options); } @@ -53,7 +53,7 @@ protected function options($id, $options = []) */ protected function update($group_id, $id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("settings/{$group_id}") ->update($id, $data); } @@ -67,7 +67,7 @@ protected function update($group_id, $id, $data) */ protected function batch($id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("settings/{$id}") ->batch($data); } diff --git a/src/Models/ShippingZone.php b/src/Models/ShippingZone.php index be6ae97..0c64b39 100644 --- a/src/Models/ShippingZone.php +++ b/src/Models/ShippingZone.php @@ -22,7 +22,7 @@ class ShippingZone extends BaseModel */ protected function getLocations($id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("shipping/zones/{$id}/locations") ->all($options); } @@ -50,7 +50,7 @@ protected function updateLocations($id, $data = []) */ protected function addShippingZoneMethod($id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("shipping/zones/{$id}/methods") ->create($data); } @@ -66,7 +66,7 @@ protected function addShippingZoneMethod($id, $data) */ protected function getShippingZoneMethod($zone_id, $id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("shipping/zones/{$zone_id}/methods") ->find($id, $options); } @@ -81,7 +81,7 @@ protected function getShippingZoneMethod($zone_id, $id, $options = []) */ protected function getShippingZoneMethods($id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("shipping/zones/{$id}/methods") ->all($options); } @@ -97,7 +97,7 @@ protected function getShippingZoneMethods($id, $options = []) */ protected function updateShippingZoneMethod($zone_id, $id, $data = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("shipping/zones/{$zone_id}/methods") ->update($id, $data); } @@ -113,7 +113,7 @@ protected function updateShippingZoneMethod($zone_id, $id, $data = []) */ protected function deleteShippingZoneMethod($zone_id, $id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("shipping/zones/{$zone_id}/methods") ->delete($id, $options); } diff --git a/src/Models/Subscription.php b/src/Models/Subscription.php index 4912f92..1d13b10 100644 --- a/src/Models/Subscription.php +++ b/src/Models/Subscription.php @@ -21,7 +21,7 @@ class Subscription extends BaseModel */ protected function notes($subscription_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("subscriptions/{$subscription_id}/notes") ->all($options); } @@ -37,7 +37,7 @@ protected function notes($subscription_id, $options = []) */ protected function note($subscription_id, $note_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("subscriptions/{$subscription_id}/notes") ->find($note_id, $options); } @@ -52,7 +52,7 @@ protected function note($subscription_id, $note_id, $options = []) */ protected function createNote($subscription_id, $data = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("subscriptions/{$subscription_id}/notes") ->create($data); } @@ -68,7 +68,7 @@ protected function createNote($subscription_id, $data = []) */ protected function deleteNote($subscription_id, $note_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("subscriptions/{$subscription_id}/notes") ->delete($note_id, $options); } @@ -83,7 +83,7 @@ protected function deleteNote($subscription_id, $note_id, $options = []) */ protected function orders($subscription_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("subscriptions/{$subscription_id}/orders") ->all($options); } diff --git a/src/Models/System.php b/src/Models/System.php index 5bd16e2..34d7d70 100644 --- a/src/Models/System.php +++ b/src/Models/System.php @@ -20,7 +20,7 @@ class System extends BaseModel */ protected function status($options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint('system_status') ->all($options); } @@ -35,7 +35,7 @@ protected function status($options = []) */ protected function tool($id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint('system_status/tools') ->find($id, $options); } @@ -49,7 +49,7 @@ protected function tool($id, $options = []) */ protected function tools($options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint('system_status/tools') ->all($options); } @@ -64,7 +64,7 @@ protected function tools($options = []) */ protected function run($id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint('system_status/tools') ->update($id, $data); } diff --git a/src/Models/Term.php b/src/Models/Term.php index 6d6566e..3eeeb38 100644 --- a/src/Models/Term.php +++ b/src/Models/Term.php @@ -21,7 +21,7 @@ class Term extends BaseModel */ protected function all($attribute_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->all($options); } @@ -37,7 +37,7 @@ protected function all($attribute_id, $options = []) */ protected function find($attribute_id, $id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->find($id, $options); } @@ -52,7 +52,7 @@ protected function find($attribute_id, $id, $options = []) */ protected function create($attribute_id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->create($data); } @@ -68,7 +68,7 @@ protected function create($attribute_id, $data) */ protected function update($attribute_id, $id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->update($id, $data); } @@ -84,7 +84,7 @@ protected function update($attribute_id, $id, $data) */ protected function delete($attribute_id, $id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->delete($id, $options); } @@ -99,7 +99,7 @@ protected function delete($attribute_id, $id, $options = []) */ protected function batch($attribute_id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->batch($data); } @@ -120,7 +120,7 @@ protected function paginate( $current_page = 1, $options = [] ) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->paginate($per_page, $current_page, $options); } @@ -134,7 +134,7 @@ protected function paginate( */ protected function count($attribute_id) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->count(); } @@ -148,7 +148,7 @@ protected function count($attribute_id) */ public function save($attribute_id) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/attributes/{$attribute_id}/terms") ->save(); } diff --git a/src/Models/Variation.php b/src/Models/Variation.php index a56ac28..af850b1 100644 --- a/src/Models/Variation.php +++ b/src/Models/Variation.php @@ -21,7 +21,7 @@ class Variation extends BaseModel */ protected function all($product_id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/{$product_id}/variations") ->all($options); } @@ -37,7 +37,7 @@ protected function all($product_id, $options = []) */ protected function find($product_id, $id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/{$product_id}/variations") ->find($id, $options); } @@ -52,7 +52,7 @@ protected function find($product_id, $id, $options = []) */ protected function create($product_id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/{$product_id}/variations") ->create($data); } @@ -68,7 +68,7 @@ protected function create($product_id, $data) */ protected function update($product_id, $id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/{$product_id}/variations") ->update($id, $data); } @@ -84,7 +84,7 @@ protected function update($product_id, $id, $data) */ protected function delete($product_id, $id, $options = []) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/{$product_id}/variations") ->delete($id, $options); } @@ -99,7 +99,7 @@ protected function delete($product_id, $id, $options = []) */ protected function batch($product_id, $data) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/{$product_id}/variations") ->batch($data); } @@ -120,7 +120,7 @@ protected function paginate( $current_page = 1, $options = [] ) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/{$product_id}/variations") ->paginate($per_page, $current_page, $options); } @@ -134,7 +134,7 @@ protected function paginate( */ protected function count($product_id) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/{$product_id}/variations") ->count(); } @@ -148,7 +148,7 @@ protected function count($product_id) */ public function save($product_id) { - return Query::init() + return Query::init($this->config) ->setEndpoint("products/{$product_id}/variations") ->save(); } diff --git a/src/Query.php b/src/Query.php index 0a8f9f9..6e3678a 100644 --- a/src/Query.php +++ b/src/Query.php @@ -24,10 +24,10 @@ public function setEndpoint($endpoint) return $this; } - public function init() + public function init($config) { if (!static::$instance) { - static::$instance = new static(); + static::$instance = (new static())->withConfig($config ?? config('multisite.default')); } return static::$instance; diff --git a/src/Traits/QueryBuilderTrait.php b/src/Traits/QueryBuilderTrait.php index 5e5d491..fff5e3d 100644 --- a/src/Traits/QueryBuilderTrait.php +++ b/src/Traits/QueryBuilderTrait.php @@ -3,6 +3,7 @@ namespace Codexshaper\WooCommerce\Traits; use Codexshaper\WooCommerce\Facades\WooCommerce; +use Codexshaper\WooCommerce\WooCommerceApi; use Illuminate\Support\LazyCollection; trait QueryBuilderTrait @@ -37,6 +38,31 @@ trait QueryBuilderTrait */ protected $isOriginal = false; + protected ?string $config = null; + + private $wooInstance = null; + + protected function withConfig(string $config) + { + $this->config = $config; + + $this->wooCommerceInstance(); + + return $this; + } + + protected function wooCommerceInstance() + { + if ($this->wooInstance) { + return $this->wooInstance; + } + + $this->wooInstance = new WooCommerceApi($this->config ?? config('multisite.default')); + + return $this->wooInstance; + } + + /** * Retrieve all Items. * @@ -47,14 +73,14 @@ trait QueryBuilderTrait protected function all($options = []) { if ($this->isLazyCollection) { - return LazyCollection::make(WooCommerce::all($this->endpoint, $options)); + return LazyCollection::make( $this->wooCommerceInstance()->all($this->endpoint, $options)); } if ($this->isCollection) { - return collect(WooCommerce::all($this->endpoint, $options)); + return collect( $this->wooCommerceInstance()->all($this->endpoint, $options)); } - return WooCommerce::all($this->endpoint, $options); + return $this->wooCommerceInstance()->all($this->endpoint, $options); } /** @@ -68,14 +94,14 @@ protected function all($options = []) protected function find($id, $options = []) { if ($this->isLazyCollection) { - return LazyCollection::make(WooCommerce::find("{$this->endpoint}/{$id}", $options)); + return LazyCollection::make( $this->wooCommerceInstance()->find("{$this->endpoint}/{$id}", $options)); } if ($this->isCollection) { - return collect(WooCommerce::find("{$this->endpoint}/{$id}", $options)); + return collect( $this->wooCommerceInstance()->find("{$this->endpoint}/{$id}", $options)); } - return WooCommerce::find("{$this->endpoint}/{$id}", $options); + return $this->wooCommerceInstance()->find("{$this->endpoint}/{$id}", $options); } /** @@ -88,14 +114,14 @@ protected function find($id, $options = []) protected function create($data) { if ($this->isLazyCollection) { - return LazyCollection::make(WooCommerce::create($this->endpoint, $data)); + return LazyCollection::make( $this->wooCommerceInstance()->create($this->endpoint, $data)); } if ($this->isCollection) { - return collect(WooCommerce::create($this->endpoint, $data)); + return collect( $this->wooCommerceInstance()->create($this->endpoint, $data)); } - return WooCommerce::create($this->endpoint, $data); + return $this->wooCommerceInstance()->create($this->endpoint, $data); } /** @@ -109,14 +135,14 @@ protected function create($data) protected function update($id, $data) { if ($this->isLazyCollection) { - return LazyCollection::make(WooCommerce::update("{$this->endpoint}/{$id}", $data)); + return LazyCollection::make( $this->wooCommerceInstance()->update("{$this->endpoint}/{$id}", $data)); } if ($this->isCollection) { - return collect(WooCommerce::update("{$this->endpoint}/{$id}", $data)); + return collect( $this->wooCommerceInstance()->update("{$this->endpoint}/{$id}", $data)); } - return WooCommerce::update("{$this->endpoint}/{$id}", $data); + return $this->wooCommerceInstance()->update("{$this->endpoint}/{$id}", $data); } /** @@ -130,14 +156,14 @@ protected function update($id, $data) protected function delete($id, $options = []) { if ($this->isLazyCollection) { - return LazyCollection::make(WooCommerce::delete("{$this->endpoint}/{$id}", $options)); + return LazyCollection::make( $this->wooCommerceInstance()->delete("{$this->endpoint}/{$id}", $options)); } if ($this->isCollection) { - return collect(WooCommerce::delete("{$this->endpoint}/{$id}", $options)); + return collect( $this->wooCommerceInstance()->delete("{$this->endpoint}/{$id}", $options)); } - return WooCommerce::delete("{$this->endpoint}/{$id}", $options); + return $this->wooCommerceInstance()->delete("{$this->endpoint}/{$id}", $options); } /** @@ -150,32 +176,32 @@ protected function delete($id, $options = []) protected function batch($data) { if ($this->isLazyCollection) { - return LazyCollection::make(WooCommerce::create("{$this->endpoint}/batch", $data)); + return LazyCollection::make( $this->wooCommerceInstance()->create("{$this->endpoint}/batch", $data)); } if ($this->isCollection) { - return collect(WooCommerce::create("{$this->endpoint}/batch", $data)); + return collect( $this->wooCommerceInstance()->create("{$this->endpoint}/batch", $data)); } - return WooCommerce::create("{$this->endpoint}/batch", $data); + return $this->wooCommerceInstance()->create("{$this->endpoint}/batch", $data); } /** * Retrieve data. * - * @return array + * @return \Illuminate\Support\Collection|array */ protected function get() { if ($this->isLazyCollection) { - return LazyCollection::make(WooCommerce::all($this->endpoint, $this->options)); + return LazyCollection::make( $this->wooCommerceInstance()->all($this->endpoint, $this->options)); } if ($this->isCollection) { - return collect(WooCommerce::all($this->endpoint, $this->options)); + return collect( $this->wooCommerceInstance()->all($this->endpoint, $this->options)); } - return WooCommerce::all($this->endpoint, $this->options); + return $this->wooCommerceInstance()->all($this->endpoint, $this->options); } /** @@ -328,11 +354,11 @@ protected function paginate($per_page = 10, $current_page = 1, $options = []) } $data = $this->get(); - $totalResults = WooCommerce::countResults(); - $totalPages = WooCommerce::countPages(); - $currentPage = WooCommerce::current(); - $previousPage = WooCommerce::previous(); - $nextPage = WooCommerce::next(); + $totalResults = $this->wooCommerceInstance()->countResults(); + $totalPages = $this->wooCommerceInstance()->countPages(); + $currentPage = $this->wooCommerceInstance()->current(); + $previousPage = $this->wooCommerceInstance()->previous(); + $nextPage = $this->wooCommerceInstance()->next(); $pagination = [ 'total_results' => $totalResults, @@ -371,8 +397,8 @@ protected function paginate($per_page = 10, $current_page = 1, $options = []) protected function count() { try { - $results = WooCommerce::all($this->endpoint, $this->options); - $totalResults = WooCommerce::countResults(); + $results = $this->wooCommerceInstance()->all($this->endpoint, $this->options); + $totalResults = $this->wooCommerceInstance()->countResults(); return $totalResults; } catch (\Exception $ex) { @@ -387,7 +413,7 @@ protected function count() */ public function save() { - $this->results = WooCommerce::create($this->endpoint, $this->properties); + $this->results = $this->wooCommerceInstance()->create($this->endpoint, $this->properties); if ($this->isLazyCollection) { return LazyCollection::make($this->results); diff --git a/src/Traits/WooCommerceTrait.php b/src/Traits/WooCommerceTrait.php index 928a788..7eb2590 100644 --- a/src/Traits/WooCommerceTrait.php +++ b/src/Traits/WooCommerceTrait.php @@ -16,8 +16,6 @@ trait WooCommerceTrait public function all($endpoint = '', $options = []) { try { - self::__construct(); - return $this->client->get($endpoint, $options); } catch (\Exception $e) { throw new \Exception($e->getMessage(), 1); @@ -36,8 +34,6 @@ public function all($endpoint = '', $options = []) public function find($endpoint = '', $options = []) { try { - self::__construct(); - return $this->client->get($endpoint, $options); } catch (\Exception $e) { throw new \Exception($e->getMessage(), 1); @@ -56,8 +52,6 @@ public function find($endpoint = '', $options = []) public function create($endpoint, $data) { try { - self::__construct(); - return $this->client->post($endpoint, $data); } catch (\Exception $e) { throw new \Exception($e->getMessage(), 1); @@ -76,8 +70,6 @@ public function create($endpoint, $data) public function update($endpoint, $data) { try { - self::__construct(); - return $this->client->put($endpoint, $data); } catch (\Exception $e) { throw new \Exception($e->getMessage(), 1); @@ -96,8 +88,6 @@ public function update($endpoint, $data) public function delete($endpoint, $options = []) { try { - self::__construct(); - return $this->client->delete($endpoint, $options); } catch (\Exception $e) { throw new \Exception($e->getMessage(), 1); diff --git a/src/WooCommerceAnalyticsApi.php b/src/WooCommerceAnalyticsApi.php index f2a8b69..3a4fc54 100644 --- a/src/WooCommerceAnalyticsApi.php +++ b/src/WooCommerceAnalyticsApi.php @@ -26,26 +26,35 @@ class WooCommerceAnalyticsApi */ public function __construct() { - try { - $this->headers = [ - 'header_total' => config('woocommerce.header_total') ?? 'X-WP-Total', - 'header_total_pages' => config('woocommerce.header_total_pages') ?? 'X-WP-TotalPages', - ]; - - $this->client = new Client( - config('woocommerce.store_url'), - config('woocommerce.consumer_key'), - config('woocommerce.consumer_secret'), - [ - 'version' => 'wc-analytics', - 'wp_api' => config('woocommerce.wp_api_integration'), - 'verify_ssl' => config('woocommerce.verify_ssl'), - 'query_string_auth' => config('woocommerce.query_string_auth'), - 'timeout' => config('woocommerce.timeout'), - ] - ); - } catch (\Exception $e) { - throw new \Exception($e->getMessage(), 1); - } +// try { +// $this->setConfig( $this->config ?? config('multisite.default') ); +// } catch (\Exception $e) { +// throw new \Exception($e->getMessage(), 1); +// } + } + + public function setConfig($configSet) + { + $config = config('multisite.' . $configSet); + + $this->headers = [ + 'header_total' => $config['header_total'] ?? 'X-WP-Total', + 'header_total_pages' => $config['header_total_pages'] ?? 'X-WP-TotalPages', + ]; + + $this->client = new Client( + $config['store_url'], + $config['consumer_key'], + $config['consumer_secret'], + [ + 'version' => 'wc/' . $config['api_version'], + 'wp_api' => $config['wp_api'], + 'verify_ssl' => $config['verify_ssl'], + 'query_string_auth' => $config['query_string_auth'], + 'timeout' => $config['timeout'], + ] + ); + + return $this; } } diff --git a/src/WooCommerceApi.php b/src/WooCommerceApi.php index c294a6e..350443f 100644 --- a/src/WooCommerceApi.php +++ b/src/WooCommerceApi.php @@ -14,6 +14,8 @@ class WooCommerceApi */ protected $client; + protected $config; + /** *@var array */ @@ -24,28 +26,26 @@ class WooCommerceApi * * @return void */ - public function __construct() + public function __construct(string $config) { - try { - $this->headers = [ - 'header_total' => config('woocommerce.header_total') ?? 'X-WP-Total', - 'header_total_pages' => config('woocommerce.header_total_pages') ?? 'X-WP-TotalPages', - ]; - - $this->client = new Client( - config('woocommerce.store_url'), - config('woocommerce.consumer_key'), - config('woocommerce.consumer_secret'), - [ - 'version' => 'wc/'.config('woocommerce.api_version'), - 'wp_api' => config('woocommerce.wp_api_integration'), - 'verify_ssl' => config('woocommerce.verify_ssl'), - 'query_string_auth' => config('woocommerce.query_string_auth'), - 'timeout' => config('woocommerce.timeout'), - ] - ); - } catch (\Exception $e) { - throw new \Exception($e->getMessage(), 1); - } + $this->config = config('multisite.' . $config); + + $this->headers = [ + 'header_total' => $this->config['header_total'] ?? 'X-WP-Total', + 'header_total_pages' => $this->config['header_total_pages'] ?? 'X-WP-TotalPages', + ]; + + $this->client = new Client( + $this->config['store_url'], + $this->config['consumer_key'], + $this->config['consumer_secret'], + [ + 'version' => 'wc/' . $this->config['api_version'], + 'wp_api' => $this->config['wp_api'], + 'verify_ssl' => $this->config['verify_ssl'], + 'query_string_auth' => $this->config['query_string_auth'], + 'timeout' => $this->config['timeout'], + ] + ); } } diff --git a/src/WooCommerceServiceProvider.php b/src/WooCommerceServiceProvider.php index 6e17494..db5f283 100644 --- a/src/WooCommerceServiceProvider.php +++ b/src/WooCommerceServiceProvider.php @@ -15,6 +15,7 @@ public function boot() { $this->publishes([ __DIR__.'/config/woocommerce.php' => config_path('woocommerce.php'), + __DIR__.'/config/multisite.php' => config_path('multisite.php'), ], 'woocommerce'); } @@ -30,9 +31,9 @@ public function register() 'woocommerce' ); - $this->app->singleton('WooCommerceApi', function () { - return new WooCommerceApi(); - }); - $this->app->alias('Codexshaper\Woocommerce\WooCommerceApi', 'WooCommerceApi'); +// $this->app->singleton('WooCommerceApi', function () { +// return new WooCommerceApi(); +// }); +// $this->app->alias('Codexshaper\Woocommerce\WooCommerceApi', 'WooCommerceApi'); } } diff --git a/src/config/multisite.php b/src/config/multisite.php new file mode 100644 index 0000000..6a3f01c --- /dev/null +++ b/src/config/multisite.php @@ -0,0 +1,28 @@ + 'development', + + "development" => [ + "store_url" => "YOUR_STORE_URL", + "consumer_key" => "YOUR_CONSUMER_KEY", + "consumer_secret" => "YOUR_CONSUMER_SECRET", + ], + "staging" => [ + "store_url" => "YOUR_STORE_URL", + "consumer_key" => "YOUR_CONSUMER_KEY", + "consumer_secret" => "YOUR_CONSUMER_SECRET", + ], + "production" => [ + "store_url" => "YOUR_STORE_URL", + "consumer_key" => "YOUR_CONSUMER_KEY", + "consumer_secret" => "YOUR_CONSUMER_SECRET", + "verify_ssl" => false, + "api_version" => "v3", + "wp_api" => true, + "query_string_auth" => false, + "timeout" => 100, + "header_total" => "X-WP-Total", + "header_total_pages" => "X-WP-TotalPages" + ] +]; diff --git a/src/config/woocommerce.php b/src/config/woocommerce.php index 8c9555b..164940b 100644 --- a/src/config/woocommerce.php +++ b/src/config/woocommerce.php @@ -1,59 +1,65 @@ env('WOOCOMMERCE_STORE_URL', 'YOUR_STORE_URL'), + 'store_url' => config( 'multisite.' . env('WOOCOMMERCE_DEFAULT_STORE', 'development') . '.store_url'), /** *================================================================================ * Consumer Key *================================================================================. */ - 'consumer_key' => env('WOOCOMMERCE_CONSUMER_KEY', 'YOUR_CONSUMER_KEY'), + 'consumer_key' => config( 'multisite.' . env('WOOCOMMERCE_DEFAULT_STORE', 'development') . '.consumer_key'), /** * Consumer Secret. */ - 'consumer_secret' => env('WOOCOMMERCE_CONSUMER_SECRET', 'YOUR_CONSUMER_SECRET'), + 'consumer_secret' => config( 'multisite.' . env('WOOCOMMERCE_DEFAULT_STORE', 'development') . '.consumer_secret'), /** *================================================================================ - * SSL support + * SSL support, default is false. *================================================================================. */ - 'verify_ssl' => env('WOOCOMMERCE_VERIFY_SSL', false), + 'verify_ssl' => config( 'multisite.' . env('WOOCOMMERCE_DEFAULT_STORE', 'development') . '.verify_ssl', false), /** *================================================================================ - * Woocommerce API version + * Woocommerce API version, default is v3 *================================================================================. */ - 'api_version' => env('WOOCOMMERCE_API_VERSION', 'v3'), + 'api_version' => config( 'multisite.' . env('WOOCOMMERCE_DEFAULT_STORE', 'development') . '.api_version', 'v3'), /** *================================================================================ * Enable WP API Integration *================================================================================. */ - 'wp_api' => env('WP_API_INTEGRATION', true), + 'wp_api' => config( 'multisite.' . env('WOOCOMMERCE_DEFAULT_STORE', 'development') . '.wp_api', true), /** *================================================================================ * Force Basic Authentication as query string *================================================================================. */ - 'query_string_auth' => env('WOOCOMMERCE_WP_QUERY_STRING_AUTH', false), + 'query_string_auth' => config( 'multisite.' . env('WOOCOMMERCE_DEFAULT_STORE', 'development') . '.query_string_auth', false), /** *================================================================================ * Default WP timeout *================================================================================. */ - 'timeout' => env('WOOCOMMERCE_WP_TIMEOUT', 15), + 'timeout' => config( 'multisite.' . env('WOOCOMMERCE_DEFAULT_STORE', 'development') . '.timeout', 100), /** *================================================================================ @@ -61,7 +67,7 @@ * Default value X-WP-Total *================================================================================. */ - 'header_total' => env('WOOCOMMERCE_WP_HEADER_TOTAL', 'X-WP-Total'), + 'header_total' => config( 'multisite.' . env('WOOCOMMERCE_DEFAULT_STORE', 'development') . '.header_total', 'X-WP-Total'), /** *================================================================================ @@ -69,5 +75,5 @@ * Default value X-WP-TotalPages *================================================================================. */ - 'header_total_pages' => env('WOOCOMMERCE_WP_HEADER_TOTAL_PAGES', 'X-WP-TotalPages'), + 'header_total_pages' => config( 'multisite.' . env('WOOCOMMERCE_DEFAULT_STORE', 'development') . '.header_total_pages', 'X-WP-TotalPages'), ];