Skip to content

Commit

Permalink
Restore behavior: ensure the module is initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
pyronaur committed Jan 13, 2022
1 parent a05036e commit 6b3f987
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ protected function on_initialize() {
// for setting up the storage.
$recommendations = new Recommendations();
$recommendations->attach_hooks();

add_action( 'rest_api_init', array( $this->rest_api, 'register_rest_routes' ) );
$this->rest_api->enable_rest_routes();

add_action( 'wp', array( $this, 'display_critical_css' ) );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ class Boost_API {

public function __construct() {
foreach ( $this->available_routes as $route_class ) {
$this->routes[] = new Route( $route_class );
$route = new Route( $route_class );
$this->routes[] = $route;
}
}

public function register_rest_routes() {

/**
* @TODO: Something seems off here.
* I don't know why, but Boost expects modules to have routes always turned on.
* Enable REST Routes when the Critical CSS module is enabled.
*
* @return void
*/
public function enable_rest_routes() {
foreach ( $this->routes as $route ) {
$route->register_rest_route();
$route->enable();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ class Route {

protected $permissions;

protected $enabled = false;



public function __construct( $endpoint ) {
$this->endpoint = new $endpoint();
$this->permissions = $this->endpoint->permissions();

// @TODO: Move this out of the constructor.
// Actions shouldn't be registered on the constructor.
// This will be probably auto-fixed when Boost_API is moved out of Critical_CSS Realm
add_action( 'rest_api_init', array( $this, 'register_rest_route' ) );
}

public function register_rest_route() {
Expand All @@ -24,7 +31,7 @@ public function register_rest_route() {
JETPACK_BOOST_REST_PREFIX . '/' . $this->endpoint->name(),
array(
'methods' => $this->endpoint->request_methods(),
'callback' => array( $this->endpoint, 'response' ),
'callback' => array( $this, 'response' ),
'permission_callback' => array( $this, 'verify_permissions' ),
)
);
Expand All @@ -49,5 +56,28 @@ public function verify_permissions( $request ) {
return true;
}

public function response( $request ) {
if ( $this->enabled === true ) {
return $this->endpoint->response( $request );
}

return $this->response_endpoint_disabled();

}

public function response_endpoint_disabled() {
return rest_ensure_response(
new \WP_HTTP_Response(
array(
'status' => 'module-unavailable',
),
200
) );
}

public function enable() {
$this->enabled = true;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ public function request_methods() {
* @todo: Figure out what to do in the JavaScript when responding with the error status.
*/
public function response( $request ) {

// @TODO:
// $this->ensure_module_initialized();
/**
* This used to be a thing here:
* if ( true !== $this->is_initialized ) {
* wp_send_json( array( 'status' => 'module-unavailable' ) );
* }
*/

$cache_key = $request['cacheKey'];
$params = $request->get_params();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ public function request_methods() {
*/
public function response( $request ) {

// @TODO:
// $this->ensure_module_initialized();
/**
* This used to be a thing here:
* if ( true !== $this->is_initialized ) {
* wp_send_json( array( 'status' => 'module-unavailable' ) );
* }
*/

$cache_key = $request['cacheKey'];

if ( ! $cache_key ) {
Expand Down

0 comments on commit 6b3f987

Please sign in to comment.