* add toggle and check before registering routes

* move taxes out from analytics check

* fix merge error

* ensure features are loaded before calling Feature methods

* remove unused use
This commit is contained in:
Paul Sealock 2021-06-24 09:02:05 +12:00 committed by GitHub
parent 011b098645
commit d6f59cce62
5 changed files with 169 additions and 58 deletions

View File

@ -84,6 +84,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt
- Fix: RemoteFreeExtension hide bundle when all of its plugins are not visible #7182
- Fix: Fixing button state logic for remote payment gateways #7200
- Fix: Skip schedule customer data deletion on site deletion #7214
- Fix: Load Analytics API only when feature is turned on #7193
- Tweak: Revert Card component removal #7167
- Fix: Currency display on Orders activity card on homescreen #7181
- Fix: Report export filtering bug. #7165

View File

@ -0,0 +1,78 @@
<?php
/**
* REST API Features Controller
*
* Handles requests to /features
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Features\Features as FeaturesClass;
/**
* Features Controller.
*
* @extends WC_REST_Data_Controller
*/
class Features extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'features';
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_features' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check whether a given request has permission to read onboarding profile data.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce-admin' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Return available payment methods.
*
* @param \WP_REST_Request $request Request data.
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_features( $request ) {
return FeaturesClass::get_available_features();
}
}

View File

@ -5,6 +5,8 @@
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Admin\Features\Features;
defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\Loader;
@ -50,14 +52,13 @@ class Init {
*/
public function rest_api_init() {
$controllers = array(
'Automattic\WooCommerce\Admin\API\Features',
'Automattic\WooCommerce\Admin\API\Notes',
'Automattic\WooCommerce\Admin\API\NoteActions',
'Automattic\WooCommerce\Admin\API\Coupons',
'Automattic\WooCommerce\Admin\API\Customers',
'Automattic\WooCommerce\Admin\API\Data',
'Automattic\WooCommerce\Admin\API\DataCountries',
'Automattic\WooCommerce\Admin\API\DataDownloadIPs',
'Automattic\WooCommerce\Admin\API\Leaderboards',
'Automattic\WooCommerce\Admin\API\Marketing',
'Automattic\WooCommerce\Admin\API\MarketingOverview',
'Automattic\WooCommerce\Admin\API\Options',
@ -69,29 +70,7 @@ class Init {
'Automattic\WooCommerce\Admin\API\ProductVariations',
'Automattic\WooCommerce\Admin\API\ProductReviews',
'Automattic\WooCommerce\Admin\API\ProductVariations',
'Automattic\WooCommerce\Admin\API\Reports\Controller',
'Automattic\WooCommerce\Admin\API\SettingOptions',
'Automattic\WooCommerce\Admin\API\Reports\Import\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Export\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Products\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Variations\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Products\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Variations\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Revenue\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Orders\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Categories\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Taxes\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Coupons\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Stock\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Downloads\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Customers\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Taxes',
'Automattic\WooCommerce\Admin\API\Themes',
'Automattic\WooCommerce\Admin\API\Plugins',
'Automattic\WooCommerce\Admin\API\OnboardingFreeExtensions',
@ -100,10 +79,41 @@ class Init {
'Automattic\WooCommerce\Admin\API\OnboardingTasks',
'Automattic\WooCommerce\Admin\API\OnboardingThemes',
'Automattic\WooCommerce\Admin\API\NavigationFavorites',
'Automattic\WooCommerce\Admin\API\Taxes',
);
// The performance indicators controller must be registered last, after other /stats endpoints have been registered.
$controllers[] = 'Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators\Controller';
if ( Features::is_enabled( 'analytics' ) ) {
$analytics_controllers = array(
'Automattic\WooCommerce\Admin\API\Customers',
'Automattic\WooCommerce\Admin\API\Leaderboards',
'Automattic\WooCommerce\Admin\API\Reports\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Import\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Export\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Products\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Variations\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Products\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Variations\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Revenue\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Orders\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Categories\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Taxes\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Coupons\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Stock\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Downloads\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Customers\Controller',
'Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\Controller',
);
// The performance indicators controller must be registered last, after other /stats endpoints have been registered.
$analytics_controllers[] = 'Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators\Controller';
$controllers = array_merge( $controllers, $analytics_controllers );
}
$controllers = apply_filters( 'woocommerce_admin_rest_controllers', $controllers );

View File

@ -23,6 +23,7 @@ use \Automattic\WooCommerce\Admin\Notes\MerchantEmailNotifications\MerchantEmail
use \Automattic\WooCommerce\Admin\Notes\WelcomeToWooCommerceForStoreUsers;
use \Automattic\WooCommerce\Admin\Notes\ManageStoreActivityFromHomeScreen;
use \Automattic\WooCommerce\Admin\Notes\NavigationNudge;
use Automattic\WooCommerce\Admin\Features\Features;
/**
* Feature plugin main class.
@ -141,8 +142,8 @@ class FeaturePlugin {
return;
}
$this->includes();
$this->hooks();
$this->includes();
}
/**
@ -170,19 +171,25 @@ class FeaturePlugin {
* Include WC Admin classes.
*/
public function includes() {
// Initialize the WC API extensions.
ReportsSync::init();
// Initialize Database updates, option migrations, and Notes.
Install::init();
Events::instance()->init();
API\Init::instance();
ReportExporter::init();
PluginsInstaller::init();
// CRUD classes.
Notes::init();
// Initialize category lookup.
CategoryLookup::instance()->init();
// Initialize Plugins Installer.
PluginsInstaller::init();
// Initialize API.
API\Init::instance();
if ( Features::is_enabled( 'analytics' ) ) {
// Initialize Reports syncing.
ReportsSync::init();
CategoryLookup::instance()->init();
// Initialize Reports exporter.
ReportExporter::init();
}
// Admin note providers.
// @todo These should be bundled in the features/ folder, but loading them from there currently has a load order issue.

View File

@ -54,7 +54,7 @@ class Features {
}
/**
* Gets a build configured array of enabled WooCommerce Admin features/sections.
* Gets a build configured array of enabled WooCommerce Admin features/sections, but does not respect optionally disabled features.
*
* @return array Enabled Woocommerce Admin features/sections.
*/
@ -128,32 +128,47 @@ class Features {
}
/**
* Check if a feature is enabled. Defaults to true for all features unless they are optional.
* Gets a build configured array of enabled WooCommerce Admin respecting optionally disabled features.
*
* @return array Enabled Woocommerce Admin features/sections.
*/
public static function get_available_features() {
$features = self::get_features();
$optional_features_unavailable = [];
foreach ( array_keys( self::$optional_features ) as $optional_feature_key ) {
$feature_class = self::get_feature_class( $optional_feature_key );
if ( $feature_class ) {
$default = isset( self::$optional_features[ $optional_feature_key ]['default'] ) ?
self::$optional_features[ $optional_feature_key ]['default'] :
'no';
// Check if the feature is currently being enabled, if it is continue.
/* phpcs:disable WordPress.Security.NonceVerification */
$feature_option = $feature_class::TOGGLE_OPTION_NAME;
if ( isset( $_POST[ $feature_option ] ) && '1' === $_POST[ $feature_option ] ) {
continue;
}
if ( 'yes' !== get_option( $feature_class::TOGGLE_OPTION_NAME, $default ) ) {
$optional_features_unavailable[] = $optional_feature_key;
}
}
}
return array_values( array_diff( $features, $optional_features_unavailable ) );
}
/**
* Check if a feature is enabled.
*
* @param string $feature Feature slug.
* @return bool
*/
public static function is_enabled( $feature ) {
if ( ! self::exists( $feature ) ) {
return false;
}
$features = self::get_optional_feature_options();
if ( isset( $features[ $feature ] ) ) {
$feature_option = $features[ $feature ];
// Check if the feature is currently being enabled.
/* phpcs:disable WordPress.Security.NonceVerification */
if ( isset( $_POST[ $feature_option ] ) && '1' === $_POST[ $feature_option ] ) {
return true;
}
$default = isset( self::$optional_features[ $feature ]['default'] ) ? self::$optional_features[ $feature ]['default'] : 'no';
return 'yes' === get_option( $feature_option, $default );
}
return true;
$available_features = self::get_available_features();
return in_array( $feature, $available_features, true );
}
/**