From 99f218670489656263f929984a2cf29f951a4ed8 Mon Sep 17 00:00:00 2001 From: Robert <40750444+mcpuishor@users.noreply.github.com> Date: Sat, 24 Jul 2021 17:58:36 +0100 Subject: [PATCH 01/13] Create woocommerce-multiconfig.php --- src/config/woocommerce-multiconfig.php | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/config/woocommerce-multiconfig.php diff --git a/src/config/woocommerce-multiconfig.php b/src/config/woocommerce-multiconfig.php new file mode 100644 index 0000000..15e82c7 --- /dev/null +++ b/src/config/woocommerce-multiconfig.php @@ -0,0 +1,7 @@ + [], + "staging" => [], + "production" => [] +]; From 235dc9a359e887d960e027302ec4e878ab5d68b1 Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 24 Jul 2021 18:20:32 +0100 Subject: [PATCH 02/13] staging for multi-site configuration --- src/WooCommerceServiceProvider.php | 1 + src/config/multisite.php | 40 ++++++++++++++++++++++++++ src/config/woocommerce-multiconfig.php | 7 ----- src/config/woocommerce.php | 2 +- 4 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 src/config/multisite.php delete mode 100644 src/config/woocommerce-multiconfig.php diff --git a/src/WooCommerceServiceProvider.php b/src/WooCommerceServiceProvider.php index 6e17494..b828782 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'); } diff --git a/src/config/multisite.php b/src/config/multisite.php new file mode 100644 index 0000000..c36d6c3 --- /dev/null +++ b/src/config/multisite.php @@ -0,0 +1,40 @@ + [ + "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" + ], + "staging" => [ + "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" + ], + "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-multiconfig.php b/src/config/woocommerce-multiconfig.php deleted file mode 100644 index 15e82c7..0000000 --- a/src/config/woocommerce-multiconfig.php +++ /dev/null @@ -1,7 +0,0 @@ - [], - "staging" => [], - "production" => [] -]; diff --git a/src/config/woocommerce.php b/src/config/woocommerce.php index 8c9555b..b4fc520 100644 --- a/src/config/woocommerce.php +++ b/src/config/woocommerce.php @@ -6,7 +6,7 @@ * Store URL eg: http://example.com *================================================================================. */ - 'store_url' => env('WOOCOMMERCE_STORE_URL', 'YOUR_STORE_URL'), + 'store_url' => config( "multisite." . env('WOOCOMMERCE_DEFAULT_STORE', 'development') ".store_url"), /** *================================================================================ From 6a769a9f03cb92d2b40c114a5ca52edb67920d8b Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 24 Jul 2021 18:22:07 +0100 Subject: [PATCH 03/13] fixed typo --- src/config/woocommerce.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/woocommerce.php b/src/config/woocommerce.php index b4fc520..54a32f0 100644 --- a/src/config/woocommerce.php +++ b/src/config/woocommerce.php @@ -6,7 +6,7 @@ * Store URL eg: http://example.com *================================================================================. */ - 'store_url' => config( "multisite." . env('WOOCOMMERCE_DEFAULT_STORE', 'development') ".store_url"), + 'store_url' => config( 'multisite.' . env('WOOCOMMERCE_DEFAULT_STORE', 'development') . '.store_url'), /** *================================================================================ From 215d6c3af03357c2704848485dcea22d343075a4 Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 24 Jul 2021 18:36:14 +0100 Subject: [PATCH 04/13] configuration based multi-site --- src/config/multisite.php | 14 -------------- src/config/woocommerce.php | 28 +++++++++++++++++----------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/config/multisite.php b/src/config/multisite.php index c36d6c3..e6a1250 100644 --- a/src/config/multisite.php +++ b/src/config/multisite.php @@ -5,25 +5,11 @@ "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" ], "staging" => [ "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" ], "production" => [ "store_url" => "YOUR_STORE_URL", diff --git a/src/config/woocommerce.php b/src/config/woocommerce.php index 54a32f0..164940b 100644 --- a/src/config/woocommerce.php +++ b/src/config/woocommerce.php @@ -1,5 +1,11 @@ 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'), ]; From 797c8cf171e6a6ec5f6ab556dd3b50362edfb037 Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 24 Jul 2021 18:49:09 +0100 Subject: [PATCH 05/13] added runtime ability to change the store --- src/WooCommerceApi.php | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/WooCommerceApi.php b/src/WooCommerceApi.php index c294a6e..562bb61 100644 --- a/src/WooCommerceApi.php +++ b/src/WooCommerceApi.php @@ -27,25 +27,33 @@ class WooCommerceApi 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->forStore(); + } catch (\Exception $e) { + throw new \Exception($e->getMessage(), 1); + } + } + + public function forStore(string $store="") + { + if ( $store !=="" ) { + $store = "multisite." . $store; + } + $this->headers = [ + 'header_total' => config($store.'.header_total') ?? 'X-WP-Total', + 'header_total_pages' => config($store.'.header_total_pages') ?? 'X-WP-TotalPages', ]; $this->client = new Client( - config('woocommerce.store_url'), - config('woocommerce.consumer_key'), - config('woocommerce.consumer_secret'), + config($store.'.store_url'), + config($store.'.consumer_key'), + config($store.'.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'), + 'version' => 'wc/'.config($store.'.api_version'), + 'wp_api' => config($store.'.wp_api_integration'), + 'verify_ssl' => config($store.'.verify_ssl'), + 'query_string_auth' => config($store.'.query_string_auth'), + 'timeout' => config($store.'.timeout'), ] ); - } catch (\Exception $e) { - throw new \Exception($e->getMessage(), 1); - } } } From e255626b14ef4b8f6c91da324696f25d1e27aa8f Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 24 Jul 2021 19:05:22 +0100 Subject: [PATCH 06/13] Revert "added runtime ability to change the store" This reverts commit 797c8cf171e6a6ec5f6ab556dd3b50362edfb037. --- src/WooCommerceApi.php | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/WooCommerceApi.php b/src/WooCommerceApi.php index 562bb61..c294a6e 100644 --- a/src/WooCommerceApi.php +++ b/src/WooCommerceApi.php @@ -27,33 +27,25 @@ class WooCommerceApi public function __construct() { try { - $this->forStore(); - } catch (\Exception $e) { - throw new \Exception($e->getMessage(), 1); - } - } - - public function forStore(string $store="") - { - if ( $store !=="" ) { - $store = "multisite." . $store; - } - $this->headers = [ - 'header_total' => config($store.'.header_total') ?? 'X-WP-Total', - 'header_total_pages' => config($store.'.header_total_pages') ?? 'X-WP-TotalPages', + $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($store.'.store_url'), - config($store.'.consumer_key'), - config($store.'.consumer_secret'), + config('woocommerce.store_url'), + config('woocommerce.consumer_key'), + config('woocommerce.consumer_secret'), [ - 'version' => 'wc/'.config($store.'.api_version'), - 'wp_api' => config($store.'.wp_api_integration'), - 'verify_ssl' => config($store.'.verify_ssl'), - 'query_string_auth' => config($store.'.query_string_auth'), - 'timeout' => config($store.'.timeout'), + '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); + } } } From 267633452b28ec9c4807566edd2d21a3ea191f0e Mon Sep 17 00:00:00 2001 From: "Robert Tudor (gram)" Date: Mon, 1 Jan 2024 21:54:08 +0000 Subject: [PATCH 07/13] WIP multi-site --- .idea/laravel-woocommerce.iml | 11 ++++ .idea/modules.xml | 8 +++ .idea/php.xml | 22 ++++++++ .idea/phpunit.xml | 10 ++++ .idea/vcs.xml | 6 +++ .idea/workspace.xml | 87 ++++++++++++++++++++++++++++++++ src/Traits/QueryBuilderTrait.php | 78 +++++++++++++++++----------- src/WooCommerceApi.php | 39 +++++++------- src/config/multisite.php | 4 +- 9 files changed, 217 insertions(+), 48 deletions(-) create mode 100644 .idea/laravel-woocommerce.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/php.xml create mode 100644 .idea/phpunit.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml 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/Traits/QueryBuilderTrait.php b/src/Traits/QueryBuilderTrait.php index 5e5d491..27e8f0c 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,16 @@ trait QueryBuilderTrait */ protected $isOriginal = false; + protected $config = []; + + protected function wooCommerCeInstance() + { + return sizeof($this->config) == 0 + ? new WooCommerceApi() + : (new WooCommerceApi())->setConfig($this->config); + } + + /** * Retrieve all Items. * @@ -47,14 +58,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 +79,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 +99,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 +120,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 +141,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 +161,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); } /** @@ -238,6 +249,13 @@ protected function withLazyCollection() return $this; } + public function withConfig($configSet) + { + $this->config = $configSet; + + return $this; + } + /** * Set options for woocommerce request. * @@ -328,11 +346,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 +389,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 +405,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/WooCommerceApi.php b/src/WooCommerceApi.php index c294a6e..69163cd 100644 --- a/src/WooCommerceApi.php +++ b/src/WooCommerceApi.php @@ -27,25 +27,30 @@ class WooCommerceApi 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/'.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'), - ] - ); + $this->setConfig( config('multisite.' . config('multisite.default') )); } catch (\Exception $e) { throw new \Exception($e->getMessage(), 1); } } + + public function setConfig($configSet) + { + $this->headers = [ + 'header_total' => $configSet['header_total'] ?? 'X-WP-Total', + 'header_total_pages' => $configSet['header_total_pages'] ?? 'X-WP-TotalPages', + ]; + + $this->client = new Client( + $configSet['tore_url'], + $configSet['consumer_key'], + $configSet['consumer_secret'], + [ + 'version' => 'wc/' . $configSet['api_version'], + 'wp_api' => $configSet['wp_api_integration'], + 'verify_ssl' => $configSet['verify_ssl'], + 'query_string_auth' => $configSet['query_string_auth'], + 'timeout' => $configSet['timeout'], + ] + ); + } } diff --git a/src/config/multisite.php b/src/config/multisite.php index e6a1250..6a3f01c 100644 --- a/src/config/multisite.php +++ b/src/config/multisite.php @@ -1,6 +1,8 @@ - 'development', + "development" => [ "store_url" => "YOUR_STORE_URL", "consumer_key" => "YOUR_CONSUMER_KEY", From 74a11cce165fc4f59d70e7855bf5848443210c28 Mon Sep 17 00:00:00 2001 From: "Robert Tudor (gram)" Date: Tue, 2 Jan 2024 10:19:13 +0000 Subject: [PATCH 08/13] . WIP --- src/Traits/QueryBuilderTrait.php | 67 ++++++++++++++++---------------- src/WooCommerceApi.php | 6 ++- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/Traits/QueryBuilderTrait.php b/src/Traits/QueryBuilderTrait.php index 27e8f0c..18475b6 100644 --- a/src/Traits/QueryBuilderTrait.php +++ b/src/Traits/QueryBuilderTrait.php @@ -40,9 +40,10 @@ trait QueryBuilderTrait protected $config = []; - protected function wooCommerCeInstance() + protected function wooCommerceInstance() { - return sizeof($this->config) == 0 + + return sizeof($this->config) == 0 || !$this->config ? new WooCommerceApi() : (new WooCommerceApi())->setConfig($this->config); } @@ -58,14 +59,14 @@ protected function wooCommerCeInstance() protected function all($options = []) { if ($this->isLazyCollection) { - return LazyCollection::make( $this->wooCommerCeInstance()->all($this->endpoint, $options)); + return LazyCollection::make( $this->wooCommerceInstance()->all($this->endpoint, $options)); } if ($this->isCollection) { - return collect( $this->wooCommerCeInstance()->all($this->endpoint, $options)); + return collect( $this->wooCommerceInstance()->all($this->endpoint, $options)); } - return $this->wooCommerCeInstance()->all($this->endpoint, $options); + return $this->wooCommerceInstance()->all($this->endpoint, $options); } /** @@ -79,14 +80,14 @@ protected function all($options = []) protected function find($id, $options = []) { if ($this->isLazyCollection) { - return LazyCollection::make( $this->wooCommerCeInstance()->find("{$this->endpoint}/{$id}", $options)); + return LazyCollection::make( $this->wooCommerceInstance()->find("{$this->endpoint}/{$id}", $options)); } if ($this->isCollection) { - return collect( $this->wooCommerCeInstance()->find("{$this->endpoint}/{$id}", $options)); + return collect( $this->wooCommerceInstance()->find("{$this->endpoint}/{$id}", $options)); } - return $this->wooCommerCeInstance()->find("{$this->endpoint}/{$id}", $options); + return $this->wooCommerceInstance()->find("{$this->endpoint}/{$id}", $options); } /** @@ -99,14 +100,14 @@ protected function find($id, $options = []) protected function create($data) { if ($this->isLazyCollection) { - return LazyCollection::make( $this->wooCommerCeInstance()->create($this->endpoint, $data)); + return LazyCollection::make( $this->wooCommerceInstance()->create($this->endpoint, $data)); } if ($this->isCollection) { - return collect( $this->wooCommerCeInstance()->create($this->endpoint, $data)); + return collect( $this->wooCommerceInstance()->create($this->endpoint, $data)); } - return $this->wooCommerCeInstance()->create($this->endpoint, $data); + return $this->wooCommerceInstance()->create($this->endpoint, $data); } /** @@ -120,14 +121,14 @@ protected function create($data) protected function update($id, $data) { if ($this->isLazyCollection) { - return LazyCollection::make( $this->wooCommerCeInstance()->update("{$this->endpoint}/{$id}", $data)); + return LazyCollection::make( $this->wooCommerceInstance()->update("{$this->endpoint}/{$id}", $data)); } if ($this->isCollection) { - return collect( $this->wooCommerCeInstance()->update("{$this->endpoint}/{$id}", $data)); + return collect( $this->wooCommerceInstance()->update("{$this->endpoint}/{$id}", $data)); } - return $this->wooCommerCeInstance()->update("{$this->endpoint}/{$id}", $data); + return $this->wooCommerceInstance()->update("{$this->endpoint}/{$id}", $data); } /** @@ -141,14 +142,14 @@ protected function update($id, $data) protected function delete($id, $options = []) { if ($this->isLazyCollection) { - return LazyCollection::make( $this->wooCommerCeInstance()->delete("{$this->endpoint}/{$id}", $options)); + return LazyCollection::make( $this->wooCommerceInstance()->delete("{$this->endpoint}/{$id}", $options)); } if ($this->isCollection) { - return collect( $this->wooCommerCeInstance()->delete("{$this->endpoint}/{$id}", $options)); + return collect( $this->wooCommerceInstance()->delete("{$this->endpoint}/{$id}", $options)); } - return $this->wooCommerCeInstance()->delete("{$this->endpoint}/{$id}", $options); + return $this->wooCommerceInstance()->delete("{$this->endpoint}/{$id}", $options); } /** @@ -161,14 +162,14 @@ protected function delete($id, $options = []) protected function batch($data) { if ($this->isLazyCollection) { - return LazyCollection::make( $this->wooCommerCeInstance()->create("{$this->endpoint}/batch", $data)); + return LazyCollection::make( $this->wooCommerceInstance()->create("{$this->endpoint}/batch", $data)); } if ($this->isCollection) { - return collect( $this->wooCommerCeInstance()->create("{$this->endpoint}/batch", $data)); + return collect( $this->wooCommerceInstance()->create("{$this->endpoint}/batch", $data)); } - return $this->wooCommerCeInstance()->create("{$this->endpoint}/batch", $data); + return $this->wooCommerceInstance()->create("{$this->endpoint}/batch", $data); } /** @@ -179,14 +180,14 @@ protected function batch($data) protected function get() { if ($this->isLazyCollection) { - return LazyCollection::make( $this->wooCommerCeInstance()->all($this->endpoint, $this->options)); + return LazyCollection::make( $this->wooCommerceInstance()->all($this->endpoint, $this->options)); } if ($this->isCollection) { - return collect( $this->wooCommerCeInstance()->all($this->endpoint, $this->options)); + return collect( $this->wooCommerceInstance()->all($this->endpoint, $this->options)); } - return $this->wooCommerCeInstance()->all($this->endpoint, $this->options); + return $this->wooCommerceInstance()->all($this->endpoint, $this->options); } /** @@ -249,9 +250,9 @@ protected function withLazyCollection() return $this; } - public function withConfig($configSet) + protected function withConfig(string $configSet): static { - $this->config = $configSet; + $this->config = config('multisite.' . $configSet); return $this; } @@ -346,11 +347,11 @@ protected function paginate($per_page = 10, $current_page = 1, $options = []) } $data = $this->get(); - $totalResults = $this->wooCommerCeInstance()->countResults(); - $totalPages = $this->wooCommerCeInstance()->countPages(); - $currentPage = $this->wooCommerCeInstance()->current(); - $previousPage = $this->wooCommerCeInstance()->previous(); - $nextPage = $this->wooCommerCeInstance()->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, @@ -389,8 +390,8 @@ protected function paginate($per_page = 10, $current_page = 1, $options = []) protected function count() { try { - $results = $this->wooCommerCeInstance()->all($this->endpoint, $this->options); - $totalResults = $this->wooCommerCeInstance()->countResults(); + $results = $this->wooCommerceInstance()->all($this->endpoint, $this->options); + $totalResults = $this->wooCommerceInstance()->countResults(); return $totalResults; } catch (\Exception $ex) { @@ -405,7 +406,7 @@ protected function count() */ public function save() { - $this->results = $this->wooCommerCeInstance()->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/WooCommerceApi.php b/src/WooCommerceApi.php index 69163cd..5991529 100644 --- a/src/WooCommerceApi.php +++ b/src/WooCommerceApi.php @@ -41,16 +41,18 @@ public function setConfig($configSet) ]; $this->client = new Client( - $configSet['tore_url'], + $configSet['store_url'], $configSet['consumer_key'], $configSet['consumer_secret'], [ 'version' => 'wc/' . $configSet['api_version'], - 'wp_api' => $configSet['wp_api_integration'], + 'wp_api' => $configSet['wp_api'], 'verify_ssl' => $configSet['verify_ssl'], 'query_string_auth' => $configSet['query_string_auth'], 'timeout' => $configSet['timeout'], ] ); + + return $this; } } From 3670ba20042bf493735796a60f86ca0b47aa83eb Mon Sep 17 00:00:00 2001 From: "Robert Tudor (gram)" Date: Tue, 2 Jan 2024 11:54:29 +0000 Subject: [PATCH 09/13] create singleton for Woo Query trait --- src/Traits/QueryBuilderTrait.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Traits/QueryBuilderTrait.php b/src/Traits/QueryBuilderTrait.php index 18475b6..65e84ef 100644 --- a/src/Traits/QueryBuilderTrait.php +++ b/src/Traits/QueryBuilderTrait.php @@ -40,12 +40,19 @@ trait QueryBuilderTrait protected $config = []; + private $wooInstance = null; + protected function wooCommerceInstance() { + if ($this->wooInstance) { + return $this->wooInstance; + } + + $this->wooInstance = sizeof($this->config) == 0 || !$this->config + ? new WooCommerceApi() + : (new WooCommerceApi())->setConfig($this->config); - return sizeof($this->config) == 0 || !$this->config - ? new WooCommerceApi() - : (new WooCommerceApi())->setConfig($this->config); + return $this->wooInstance; } From fcd0125de966acc2f40e957ce31360c8b86e4431 Mon Sep 17 00:00:00 2001 From: "Robert Tudor (gram)" Date: Tue, 2 Jan 2024 13:24:46 +0000 Subject: [PATCH 10/13] fixed calls to custom configuration --- src/Traits/QueryBuilderTrait.php | 21 +++++++------ src/WooCommerceAnalyticsApi.php | 51 +++++++++++++++++++------------- src/WooCommerceApi.php | 34 +++++++++++---------- 3 files changed, 58 insertions(+), 48 deletions(-) diff --git a/src/Traits/QueryBuilderTrait.php b/src/Traits/QueryBuilderTrait.php index 65e84ef..9f61b71 100644 --- a/src/Traits/QueryBuilderTrait.php +++ b/src/Traits/QueryBuilderTrait.php @@ -38,19 +38,25 @@ trait QueryBuilderTrait */ protected $isOriginal = false; - protected $config = []; + protected string $config; private $wooInstance = null; + protected function withConfig(string $config) + { + $this->config = $config; + + return $this; + } + protected function wooCommerceInstance() { if ($this->wooInstance) { return $this->wooInstance; } - $this->wooInstance = sizeof($this->config) == 0 || !$this->config - ? new WooCommerceApi() - : (new WooCommerceApi())->setConfig($this->config); + $this->wooInstance = (new WooCommerceApi()) + ->setConfig($this->config ?? config('multisite.default') ); return $this->wooInstance; } @@ -257,13 +263,6 @@ protected function withLazyCollection() return $this; } - protected function withConfig(string $configSet): static - { - $this->config = config('multisite.' . $configSet); - - return $this; - } - /** * Set options for woocommerce request. * 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 5991529..5bf8c08 100644 --- a/src/WooCommerceApi.php +++ b/src/WooCommerceApi.php @@ -26,30 +26,32 @@ class WooCommerceApi */ public function __construct() { - try { - $this->setConfig( config('multisite.' . config('multisite.default') )); - } catch (\Exception $e) { - throw new \Exception($e->getMessage(), 1); - } +// try { +// $this->setConfig( config('multisite.default') ); +// } catch (\Exception $e) { +// throw new \Exception($e->getMessage(), 1); +// } } - public function setConfig($configSet) + public function setConfig($config) { + $config = config('multisite.' . $config); + $this->headers = [ - 'header_total' => $configSet['header_total'] ?? 'X-WP-Total', - 'header_total_pages' => $configSet['header_total_pages'] ?? 'X-WP-TotalPages', + 'header_total' => $config['header_total'] ?? 'X-WP-Total', + 'header_total_pages' => $config['header_total_pages'] ?? 'X-WP-TotalPages', ]; $this->client = new Client( - $configSet['store_url'], - $configSet['consumer_key'], - $configSet['consumer_secret'], + $config['store_url'], + $config['consumer_key'], + $config['consumer_secret'], [ - 'version' => 'wc/' . $configSet['api_version'], - 'wp_api' => $configSet['wp_api'], - 'verify_ssl' => $configSet['verify_ssl'], - 'query_string_auth' => $configSet['query_string_auth'], - 'timeout' => $configSet['timeout'], + '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'], ] ); From 311236dffeedb0ce59d0f0146b5a2aebc8b5bc8c Mon Sep 17 00:00:00 2001 From: "Robert Tudor (gram)" Date: Sat, 13 Jan 2024 20:47:46 +0000 Subject: [PATCH 11/13] WIP --- src/Models/Attribute.php | 12 +++++----- src/Models/Customer.php | 2 +- src/Models/Note.php | 14 +++++------ src/Models/Order.php | 16 ++++++------- src/Models/Refund.php | 14 +++++------ src/Models/Report.php | 14 +++++------ src/Models/Setting.php | 8 +++---- src/Models/ShippingZone.php | 12 +++++----- src/Models/Subscription.php | 10 ++++---- src/Models/System.php | 8 +++---- src/Models/Term.php | 18 +++++++-------- src/Models/Variation.php | 18 +++++++-------- src/Query.php | 4 ++-- src/Traits/QueryBuilderTrait.php | 5 ++-- src/Traits/WooCommerceTrait.php | 10 -------- src/WooCommerceApi.php | 37 +++++++++++------------------- src/WooCommerceServiceProvider.php | 8 +++---- 17 files changed, 96 insertions(+), 114 deletions(-) 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..cc97348 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); } return static::$instance; diff --git a/src/Traits/QueryBuilderTrait.php b/src/Traits/QueryBuilderTrait.php index 9f61b71..571bfca 100644 --- a/src/Traits/QueryBuilderTrait.php +++ b/src/Traits/QueryBuilderTrait.php @@ -46,6 +46,8 @@ protected function withConfig(string $config) { $this->config = $config; + $this->wooCommerceInstance(); + return $this; } @@ -55,8 +57,7 @@ protected function wooCommerceInstance() return $this->wooInstance; } - $this->wooInstance = (new WooCommerceApi()) - ->setConfig($this->config ?? config('multisite.default') ); + $this->wooInstance = new WooCommerceApi($this->config ?? config('multisite.default')); return $this->wooInstance; } 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/WooCommerceApi.php b/src/WooCommerceApi.php index 5bf8c08..350443f 100644 --- a/src/WooCommerceApi.php +++ b/src/WooCommerceApi.php @@ -14,6 +14,8 @@ class WooCommerceApi */ protected $client; + protected $config; + /** *@var array */ @@ -24,37 +26,26 @@ class WooCommerceApi * * @return void */ - public function __construct() + public function __construct(string $config) { -// try { -// $this->setConfig( config('multisite.default') ); -// } catch (\Exception $e) { -// throw new \Exception($e->getMessage(), 1); -// } - } - - public function setConfig($config) - { - $config = config('multisite.' . $config); + $this->config = config('multisite.' . $config); $this->headers = [ - 'header_total' => $config['header_total'] ?? 'X-WP-Total', - 'header_total_pages' => $config['header_total_pages'] ?? 'X-WP-TotalPages', + 'header_total' => $this->config['header_total'] ?? 'X-WP-Total', + 'header_total_pages' => $this->config['header_total_pages'] ?? 'X-WP-TotalPages', ]; $this->client = new Client( - $config['store_url'], - $config['consumer_key'], - $config['consumer_secret'], + $this->config['store_url'], + $this->config['consumer_key'], + $this->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'], + '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'], ] ); - - return $this; } } diff --git a/src/WooCommerceServiceProvider.php b/src/WooCommerceServiceProvider.php index b828782..db5f283 100644 --- a/src/WooCommerceServiceProvider.php +++ b/src/WooCommerceServiceProvider.php @@ -31,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'); } } From 57f3f747a901d654b34dedb28c11e4f44bdd0053 Mon Sep 17 00:00:00 2001 From: "Robert Tudor (gram)" Date: Sat, 20 Jan 2024 19:44:05 +0000 Subject: [PATCH 12/13] allowed nullable configuration in QueryBuilderTrait to default to use the default store configuration --- src/Traits/QueryBuilderTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/QueryBuilderTrait.php b/src/Traits/QueryBuilderTrait.php index 571bfca..fff5e3d 100644 --- a/src/Traits/QueryBuilderTrait.php +++ b/src/Traits/QueryBuilderTrait.php @@ -38,7 +38,7 @@ trait QueryBuilderTrait */ protected $isOriginal = false; - protected string $config; + protected ?string $config = null; private $wooInstance = null; From c48b0738b412a6699ddf0356bebb399545fbe1ca Mon Sep 17 00:00:00 2001 From: "Robert Tudor (gram)" Date: Sat, 20 Jan 2024 20:05:22 +0000 Subject: [PATCH 13/13] default --- src/Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Query.php b/src/Query.php index cc97348..6e3678a 100644 --- a/src/Query.php +++ b/src/Query.php @@ -27,7 +27,7 @@ public function setEndpoint($endpoint) public function init($config) { if (!static::$instance) { - static::$instance = (new static())->withConfig($config); + static::$instance = (new static())->withConfig($config ?? config('multisite.default')); } return static::$instance;