Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/hide.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ function fastwc_should_hide_cart_button_for_product( $should_hide ) {
$fastwc_hidden_products = fastwc_get_products_to_hide_buttons();
$product_id = 0;

if ( ! empty( WC()->cart ) ) {
if ( ! empty( $fastwc_hidden_products ) && ! empty( WC()->cart ) ) {
$cart = WC()->cart->get_cart();

foreach ( $cart as $cart_item ) {
Expand Down
152 changes: 31 additions & 121 deletions includes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,176 +8,86 @@
// Define the API route base path.
define( 'FASTWC_ROUTES_BASE', 'wc/fast/v1' );

// Load route base class.
require_once FASTWC_PATH . 'includes/routes/class-route.php';
// Provides an API for polling shipping options.
require_once FASTWC_PATH . 'includes/routes/shipping.php';
require_once FASTWC_PATH . 'includes/routes/class-shipping.php';
// Provides an API that exposes shipping zones.
require_once FASTWC_PATH . 'includes/routes/shipping-zones.php';
require_once FASTWC_PATH . 'includes/routes/class-shipping-zones.php';
// Provides an API that exposes plugin info.
require_once FASTWC_PATH . 'includes/routes/plugin-info.php';
require_once FASTWC_PATH . 'includes/routes/class-plugin-info.php';
// Provides an API that exposes product attributes.
require_once FASTWC_PATH . 'includes/routes/product-attributes.php';
require_once FASTWC_PATH . 'includes/routes/class-product-attributes.php';
// Provides an API that exposes orders with refunds.
require_once FASTWC_PATH . 'includes/routes/refunds.php';
require_once FASTWC_PATH . 'includes/routes/class-refunds.php';
// Provides an API that exposes a list of disabled Fast webhooks.
require_once FASTWC_PATH . 'includes/routes/webhooks.php';
require_once FASTWC_PATH . 'includes/routes/class-webhooks.php';
// Provides an API that exposes a test authorization header.
require_once FASTWC_PATH . 'includes/routes/class-auth-test.php';

/**
* Register Fast Woocommerce routes for the REST API.
*/
function fastwc_rest_api_init() {
// Register a utility route to get information on installed plugins.
register_rest_route(
FASTWC_ROUTES_BASE . '/store',
'plugins',
array(
'methods' => 'GET',
'callback' => 'fastwc_get_plugin_info',
'permission_callback' => 'fastwc_api_permission_callback',
)
);

fastwc_log_info( 'Registered route: ' . FASTWC_ROUTES_BASE . '/store/plugins' );
new \FastWC\Routes\Plugin_Info();

// Register a route to collect all possible shipping locations.
register_rest_route(
FASTWC_ROUTES_BASE,
'shipping_zones',
array(
'methods' => 'GET',
'callback' => 'fastwc_get_zones',
'permission_callback' => 'fastwc_api_permission_callback',
)
);

fastwc_log_info( 'Registered route: ' . FASTWC_ROUTES_BASE . '/shipping_zones' );
new \FastWC\Routes\Shipping_Zones();

// Register a route to calculate available shipping rates.
// FE -> OMS -> Blender -> (pID, variantID, Shipping info, CustomerID)Plugin.
register_rest_route(
FASTWC_ROUTES_BASE,
'shipping',
array(
'methods' => 'POST',
'callback' => 'fastwc_calculate_shipping',
'permission_callback' => 'fastwc_api_permission_callback',
)
);

fastwc_log_info( 'Registered route: ' . FASTWC_ROUTES_BASE . '/shipping' );
new \FastWC\Routes\Shipping();

// Register a route to load product attributes.
register_rest_route(
FASTWC_ROUTES_BASE,
'product/attributes',
array(
'methods' => 'GET',
'callback' => 'fastwc_get_product_attributes',
'permission_callback' => 'fastwc_api_managewc_permission_callback',
)
);

fastwc_log_info( 'Registered route: ' . FASTWC_ROUTES_BASE . '/product/attributes' );
new \FastWC\Routes\Product_Attributes();

// Register a route to get all orders with refunds.
register_rest_route(
FASTWC_ROUTES_BASE,
'refunds',
array(
'methods' => 'GET',
'callback' => 'fastwc_get_orders_with_refunds',
'permission_callback' => 'fastwc_api_permission_callback',
)
);

fastwc_log_info( 'Registered route: ' . FASTWC_ROUTES_BASE . '/refunds' );
new \FastWC\Routes\Refunds();

// Register a route to get all disabled Fast webhooks.
register_rest_route(
FASTWC_ROUTES_BASE,
'webhooks',
array(
'methods' => 'GET',
'callback' => 'fastwc_route_get_disabled_webhooks',
'permission_callback' => 'fastwc_api_permission_callback',
)
);

fastwc_log_info( 'Registered route: ' . FASTWC_ROUTES_BASE . '/webhooks' );
new \FastWC\Routes\Webhooks();

// Register a route to test the Authorization header.
register_rest_route(
FASTWC_ROUTES_BASE,
'authecho',
array(
'methods' => 'GET',
'callback' => 'fastwc_test_authorization_header',
'permission_callback' => '__return_true',
)
);

fastwc_log_info( 'Registered route: ' . FASTWC_ROUTES_BASE . '/authecho' );
new \FastWC\Routes\Auth_Test();
}
add_action( 'rest_api_init', 'fastwc_rest_api_init' );

/**
* REST API permissions callback.
* Abstract REST API permissions callback.
*
* @param string $capability Capability name to check.
* @param string $log_string Initial string for the permission check log.
*
* @return bool
*/
function fastwc_api_permission_callback() {
function fastwc_api_general_permission_callback( $capability, $log_string ) {
// Make sure an instance of WooCommerce is loaded.
// This will load the `WC_REST_Authentication` class, which
// handles the API consumer key and secret.
WC();

$has_permission = current_user_can( 'manage_options' );
$has_permission = current_user_can( $capability );

fastwc_log_info( 'API Permission Callback: ' . ( $has_permission ? 'granted' : 'denied' ) );
fastwc_log_info( $log_string . ': ' . ( $has_permission ? 'granted' : 'denied' ) );

return $has_permission;
}

/**
* REST API permissions callback for product attributes
* REST API permissions callback.
*
* @return bool
*/
function fastwc_api_managewc_permission_callback() {
// Make sure an instance of WooCommerce is loaded.
// This will load the `WC_REST_Authentication` class, which
// handles the API consumer key and secret.
WC();

$has_permission = current_user_can( 'manage_woocommerce' );

fastwc_log_info( 'API Product Attributes Permission Callback: ' . ( $has_permission ? 'granted' : 'denied' ) );

return $has_permission;
function fastwc_api_permission_callback() {
return fastwc_api_general_permission_callback( 'manage_options', 'API Manage Options Permission Callback' );
}

/**
* Test the Authorization header.
*
* @param WP_REST_Request $request JSON request for shipping endpoint.
* REST API permissions callback for product attributes.
*
* @return array|WP_Error|WP_REST_Response
* @return bool
*/
function fastwc_test_authorization_header( $request ) {
$auth_header = 'No Authorization Header';

$headers = $request->get_headers();

if ( ! empty( $headers['authorization'] ) ) {
$header_count = count( $headers['authorization'] );

if ( is_array( $headers['authorization'] ) && $header_count > 0 ) {
$auth_header = $headers['authorization'][0];
} elseif ( is_string( $headers['authorization'] ) ) {
$auth_header = $headers['authorization'];
}
}

fastwc_log_info( 'Authorization header endpoint called: ' . $auth_header );

return new WP_REST_Response( $auth_header, 200 );
function fastwc_api_managewc_permission_callback() {
return fastwc_api_general_permission_callback( 'manage_woocommerce', 'API Manage WooCommerce Permission Callback' );
}
59 changes: 59 additions & 0 deletions includes/routes/class-auth-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Authorization Header API
*
* Provides an API to test the Authorization header.
*
* @package Fast
*/

namespace FastWC\Routes;

/**
* Fast plugin info route object.
*/
class Auth_Test extends Route {

/**
* Route name.
*
* @var string
*/
protected $route = 'authecho';

/**
* Permission callback.
*
* @var callable
*/
protected $permission_callback = '__return_true';

/**
* Test the Authorization header.
*
* @param WP_REST_Request $request JSON request for shipping endpoint.
*
* @return array|WP_Error|WP_REST_Response
*/
public function callback( $request ) {
$this->request = $request;

$auth_header = 'No Authorization Header';

$headers = $this->request->get_headers();

if ( ! empty( $headers['authorization'] ) ) {
$header_count = count( $headers['authorization'] );

if ( is_array( $headers['authorization'] ) && $header_count > 0 ) {
$auth_header = $headers['authorization'][0];
} elseif ( is_string( $headers['authorization'] ) ) {
$auth_header = $headers['authorization'];
}
}

\fastwc_log_info( 'Authorization header endpoint called: ' . $auth_header );

return new \WP_REST_Response( $auth_header, 200 );
}
}
79 changes: 79 additions & 0 deletions includes/routes/class-plugin-info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Plugin API
*
* Provides an API that exposes plugin info.
*
* @package Fast
*/

namespace FastWC\Routes;

/**
* Fast plugin info route object.
*/
class Plugin_Info extends Route {

/**
* Route namespace.
*
* @var string
*/
protected $namespace = FASTWC_ROUTES_BASE . '/store';

/**
* Route name.
*
* @var string
*/
protected $route = 'plugins';

/**
* Utility to get information on installed plugins.
*
* Returns an array of all installed plugins and indicates which are
* plugin are active and which are not. Array is keyed by the plugin's
* folder/slug.php (which is how WP looks at them) and includes the
* name, version, and true/false whether it is active or not.
*
* @see: https://codex.wordpress.org/Function_Reference/get_plugins
* @see: https://developer.wordpress.org/reference/functions/get_plugins/
* @see: https://developer.wordpress.org/reference/functions/get_option/
*
* @param WP_REST_Request $request JSON request for shipping endpoint.
*
* @return $plugins array {
* An array of all installed plugins.
*
* @type array $plugin {
* The plugin information (note: array key is folder/slug.php)
*
* @type string $name
* @type string $version
* @type boolean $active
* }
* }
*/
public function callback( $request ) {
// Get all plugins.
include_once 'wp-admin/includes/plugin.php';
$all_plugins = \get_plugins();

// Get active plugins.
$active_plugins = \get_option( 'active_plugins' );

$plugins = array();

// Assemble array of name, version, and whether plugin is active (boolean).
foreach ( $all_plugins as $key => $value ) {
$is_active = ( in_array( $key, $active_plugins, true ) ) ? true : false;
$plugins[] = array(
'name' => $value['Name'],
'version' => $value['Version'],
'active' => $is_active,
);
}

return new \WP_REST_Response( $plugins, 200 );
}
}
Loading