From e3dc79481de184d619f7f9a256783c984c3952ee Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 21 Jun 2019 15:03:28 +0100 Subject: [PATCH 1/5] Init the blocks library using autoloading --- composer.json | 2 +- composer.lock | 21 ++++++++++++++------- includes/class-woocommerce.php | 5 +++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index b25a53db056..8799d4f29fa 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "require": { "composer/installers": "1.6.0", "woocommerce/woocommerce-rest-api": "dev-test/jetpack-autoloader", - "woocommerce/woocommerce-blocks": "dev-add/build-config", + "woocommerce/woocommerce-blocks": "dev-refactor/php-classes", "automattic/jetpack-autoloader": "^1" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 90201ec5eb5..3ab58aabc6b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b26e3ab11a27accf12e540dc6cb540a4", + "content-hash": "f269789179494befeb7caf0cb7bb29eb", "packages": [ { "name": "automattic/jetpack-autoloader", @@ -164,16 +164,16 @@ }, { "name": "woocommerce/woocommerce-blocks", - "version": "dev-add/build-config", + "version": "dev-refactor/php-classes", "source": { "type": "git", "url": "https://github.com/woocommerce/woocommerce-gutenberg-products-block.git", - "reference": "be2a243d0e999af6f4318f7b242cdac2565d36d8" + "reference": "52e2cd8ba1462c40febd5408994996872e2610d5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/woocommerce/woocommerce-gutenberg-products-block/zipball/be2a243d0e999af6f4318f7b242cdac2565d36d8", - "reference": "be2a243d0e999af6f4318f7b242cdac2565d36d8", + "url": "https://api.github.com/repos/woocommerce/woocommerce-gutenberg-products-block/zipball/52e2cd8ba1462c40febd5408994996872e2610d5", + "reference": "52e2cd8ba1462c40febd5408994996872e2610d5", "shasum": "" }, "require": { @@ -184,9 +184,16 @@ "woocommerce/woocommerce-sniffs": "0.0.6" }, "type": "wordpress-plugin", + "extra": { + "scripts-description": { + "phpcs": "Analyze code against the WordPress coding standards with PHP_CodeSniffer", + "phpcbf": "Fix coding standards warnings/errors automatically with PHP Code Beautifier" + } + }, "autoload": { "psr-4": { - "WooCommerce\\Blocks\\": "src/" + "WooCommerce\\Blocks\\": "src/", + "WooCommerce\\Blocks\\Tests\\": "tests/php/" } }, "notification-url": "https://packagist.org/downloads/", @@ -195,7 +202,7 @@ ], "description": "Feature plugin for the WooCommerce Gutenberg Products block", "homepage": "https://woocommerce.com/", - "time": "2019-06-20T14:53:51+00:00" + "time": "2019-06-20T09:39:04+00:00" }, { "name": "woocommerce/woocommerce-rest-api", diff --git a/includes/class-woocommerce.php b/includes/class-woocommerce.php index 181fc6ab96f..f45b3636ae8 100644 --- a/includes/class-woocommerce.php +++ b/includes/class-woocommerce.php @@ -540,6 +540,11 @@ final class WooCommerce { $this->deprecated_hook_handlers['actions'] = new WC_Deprecated_Action_Hooks(); $this->deprecated_hook_handlers['filters'] = new WC_Deprecated_Filter_Hooks(); + // Init any packages. + if ( version_compare( PHP_VERSION, '5.6.0', '>=' ) ) { + \Automattic\WooCommerce\Blocks\Library::instance()->init(); + } + // Classes/actions loaded for the frontend and for ajax requests. if ( $this->is_request( 'frontend' ) ) { wc_load_cart(); From d31f6f70a2659948d8750be4963427e580fcda7d Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 21 Jun 2019 17:28:46 +0100 Subject: [PATCH 2/5] Package init method --- includes/class-wc-api.php | 19 +++---------------- includes/class-woocommerce.php | 21 ++++++++++++++++----- includes/legacy/class-wc-legacy-api.php | 7 +++++++ 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/includes/class-wc-api.php b/includes/class-wc-api.php index 1bd0c4a6129..eb33cbf35f9 100644 --- a/includes/class-wc-api.php +++ b/includes/class-wc-api.php @@ -1,8 +1,8 @@ is_rest_api_loaded() || version_compare( PHP_VERSION, '5.6.0', '<' ) ) { - return; - } - \Automattic\WooCommerce\RestApi\Server::instance()->init(); - } - /** * Get the version of the REST API package being ran. * @@ -82,7 +69,7 @@ class WC_API extends WC_Legacy_API { */ public function get_endpoint_data( $endpoint, $params = array() ) { if ( ! $this->is_rest_api_loaded() ) { - $this->rest_api_init(); + return new WP_Error( 'rest_api_unavailable', __( 'The Rest API is unavailable.', 'woocommerce' ) ); } $request = new \WP_REST_Request( 'GET', $endpoint ); if ( $params ) { diff --git a/includes/class-woocommerce.php b/includes/class-woocommerce.php index f45b3636ae8..09f2df8c1c1 100644 --- a/includes/class-woocommerce.php +++ b/includes/class-woocommerce.php @@ -180,6 +180,7 @@ final class WooCommerce { register_shutdown_function( array( $this, 'log_errors' ) ); add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), -1 ); + add_action( 'plugins_loaded', array( $this, 'init_packages' ) ); add_action( 'after_setup_theme', array( $this, 'setup_environment' ) ); add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 ); add_action( 'init', array( $this, 'init' ), 0 ); @@ -191,6 +192,21 @@ final class WooCommerce { add_action( 'deactivated_plugin', array( $this, 'deactivated_plugin' ) ); } + /** + * Load any WooCommerce packages. + * + * Most packages will have their own init routines, so this is used early once plugins are loaded. + * + * @since 3.7.0 + */ + public function init_packages() { + if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) { + return; + } + \Automattic\WooCommerce\Blocks\Library::instance()->init(); + \Automattic\WooCommerce\RestApi\Server::instance()->init(); + } + /** * Ensures fatal errors are logged so they can be picked up in the status report. * @@ -540,11 +556,6 @@ final class WooCommerce { $this->deprecated_hook_handlers['actions'] = new WC_Deprecated_Action_Hooks(); $this->deprecated_hook_handlers['filters'] = new WC_Deprecated_Filter_Hooks(); - // Init any packages. - if ( version_compare( PHP_VERSION, '5.6.0', '>=' ) ) { - \Automattic\WooCommerce\Blocks\Library::instance()->init(); - } - // Classes/actions loaded for the frontend and for ajax requests. if ( $this->is_request( 'frontend' ) ) { wc_load_cart(); diff --git a/includes/legacy/class-wc-legacy-api.php b/includes/legacy/class-wc-legacy-api.php index c10cccb3bf2..f99eae731d5 100644 --- a/includes/legacy/class-wc-legacy-api.php +++ b/includes/legacy/class-wc-legacy-api.php @@ -271,6 +271,13 @@ class WC_Legacy_API { $this->server->serve_request(); } + /** + * Rest API Init. + * + * @deprecated since 3.7.0 - REST API clases autoload. + */ + public function rest_api_init() {} + /** * Include REST API classes. * From a6a37617786443c9842693c13ceeb3e80e3d2736 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 21 Jun 2019 19:23:43 +0100 Subject: [PATCH 3/5] Update blocks to use dev build --- composer.json | 2 +- composer.lock | 24 ++++++++++++------------ package.json | 1 - 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 8799d4f29fa..36c325ac113 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "require": { "composer/installers": "1.6.0", "woocommerce/woocommerce-rest-api": "dev-test/jetpack-autoloader", - "woocommerce/woocommerce-blocks": "dev-refactor/php-classes", + "woocommerce/woocommerce-blocks": "dev-build/2.2.0-dev", "automattic/jetpack-autoloader": "^1" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 3ab58aabc6b..f3296258ae3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f269789179494befeb7caf0cb7bb29eb", + "content-hash": "bd3583eea7bbf9c0401e52371ae1baf9", "packages": [ { "name": "automattic/jetpack-autoloader", @@ -164,19 +164,20 @@ }, { "name": "woocommerce/woocommerce-blocks", - "version": "dev-refactor/php-classes", + "version": "dev-build/2.2.0-dev", "source": { "type": "git", "url": "https://github.com/woocommerce/woocommerce-gutenberg-products-block.git", - "reference": "52e2cd8ba1462c40febd5408994996872e2610d5" + "reference": "64fdbe613ee66d5444696428567f3b683b1a2b06" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/woocommerce/woocommerce-gutenberg-products-block/zipball/52e2cd8ba1462c40febd5408994996872e2610d5", - "reference": "52e2cd8ba1462c40febd5408994996872e2610d5", + "url": "https://api.github.com/repos/woocommerce/woocommerce-gutenberg-products-block/zipball/64fdbe613ee66d5444696428567f3b683b1a2b06", + "reference": "64fdbe613ee66d5444696428567f3b683b1a2b06", "shasum": "" }, "require": { + "automattic/jetpack-autoloader": "^1", "composer/installers": "1.6.0" }, "require-dev": { @@ -192,8 +193,7 @@ }, "autoload": { "psr-4": { - "WooCommerce\\Blocks\\": "src/", - "WooCommerce\\Blocks\\Tests\\": "tests/php/" + "Automattic\\WooCommerce\\Blocks\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", @@ -202,7 +202,7 @@ ], "description": "Feature plugin for the WooCommerce Gutenberg Products block", "homepage": "https://woocommerce.com/", - "time": "2019-06-20T09:39:04+00:00" + "time": "2019-06-21T18:08:00+00:00" }, { "name": "woocommerce/woocommerce-rest-api", @@ -210,12 +210,12 @@ "source": { "type": "git", "url": "https://github.com/woocommerce/woocommerce-rest-api.git", - "reference": "46453550e8d7468f6a1f46b47fefd2fdc482c801" + "reference": "2943708bb12f70b845f32e5f26893436a926a1ee" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/woocommerce/woocommerce-rest-api/zipball/46453550e8d7468f6a1f46b47fefd2fdc482c801", - "reference": "46453550e8d7468f6a1f46b47fefd2fdc482c801", + "url": "https://api.github.com/repos/woocommerce/woocommerce-rest-api/zipball/2943708bb12f70b845f32e5f26893436a926a1ee", + "reference": "2943708bb12f70b845f32e5f26893436a926a1ee", "shasum": "" }, "require": { @@ -243,7 +243,7 @@ ], "description": "The WooCommerce core REST API.", "homepage": "https://github.com/woocommerce/woocommerce-rest-api", - "time": "2019-06-21T09:40:39+00:00" + "time": "2019-06-21T13:57:26+00:00" } ], "packages-dev": [ diff --git a/package.json b/package.json index 57499f2c4ce..b89e80de473 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "license": "GPL-3.0+", "main": "Gruntfile.js", "scripts": { - "prebuild": "npm install && composer install --no-dev", "build": "grunt", "build-watch": "grunt watch", "lint:js": "eslint assets/js --ext=js", From 6bfe8d63c5af561bb6637dfbdb59ef8f5b08501f Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 21 Jun 2019 19:58:38 +0100 Subject: [PATCH 4/5] Load Packge files --- includes/class-woocommerce.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-woocommerce.php b/includes/class-woocommerce.php index 09f2df8c1c1..d237265de37 100644 --- a/includes/class-woocommerce.php +++ b/includes/class-woocommerce.php @@ -203,8 +203,8 @@ final class WooCommerce { if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) { return; } - \Automattic\WooCommerce\Blocks\Library::instance()->init(); - \Automattic\WooCommerce\RestApi\Server::instance()->init(); + \Automattic\WooCommerce\Blocks\Package::init(); + \Automattic\WooCommerce\RestApi\Package::init(); } /** From 558a330291f050cabb9b3a42b749311ff37090a7 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 21 Jun 2019 20:00:03 +0100 Subject: [PATCH 5/5] Update version/path getters --- includes/class-wc-api.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/includes/class-wc-api.php b/includes/class-wc-api.php index eb33cbf35f9..37356a34330 100644 --- a/includes/class-wc-api.php +++ b/includes/class-wc-api.php @@ -33,10 +33,13 @@ class WC_API extends WC_Legacy_API { * Get the version of the REST API package being ran. * * @since 3.7.0 - * @return int|null + * @return string|null */ public function get_rest_api_package_version() { - return null; + if ( ! $this->is_rest_api_loaded() ) { + return null; + } + return \Automattic\WooCommerce\RestApi\Package::get_version(); } /** @@ -46,7 +49,10 @@ class WC_API extends WC_Legacy_API { * @return string */ public function get_rest_api_package_path() { - return ''; + if ( ! $this->is_rest_api_loaded() ) { + return null; + } + return \Automattic\WooCommerce\RestApi\Package::get_path(); } /** @@ -56,7 +62,7 @@ class WC_API extends WC_Legacy_API { * @return boolean */ protected function is_rest_api_loaded() { - return class_exists( '\Automattic\WooCommerce\RestApi\Server', false ); + return class_exists( '\Automattic\WooCommerce\RestApi\Package', false ); } /**