Skip to content

Commit

Permalink
Simple Payments Block: Fix availability (#10833)
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham authored and dereksmart committed Dec 11, 2018
1 parent 268d5d5 commit 0625cf5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
20 changes: 19 additions & 1 deletion class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ public static function load_blocks() {
return;
}

if ( Jetpack_Constants::is_true( 'REST_API_REQUEST' ) ) {
// We defer the loading of the blocks until we have a better scope in reset requests.
add_filter( 'rest_request_before_callbacks', array( __CLASS__, 'defered_register_blocks' ) );
return;
}

self::register_blocks();
}

static function defered_register_blocks( $request ) {
self::register_blocks();
return $request;
}

static function register_blocks() {
/**
* Filter the list of blocks that are available through jetpack.
*
Expand All @@ -70,13 +85,16 @@ public static function load_blocks() {
* @param array
*/
self::$blocks_index = apply_filters( 'jetpack_set_available_blocks', array() );

foreach ( self::$jetpack_blocks as $type => $args ) {
if ( 'publicize' === $type ) {
// publicize is not actually a block, it's a gutenberg plugin.
// We will handle it's registration on the client-side.
continue;
}
if ( isset( $args['availability']['callback'] ) ) {
$args['availability'] = call_user_func( $args['availability']['callback'] );
self::$jetpack_blocks[ $type ] = $args; // update this so that we don't have to call it again
}
if ( isset( $args['availability']['available'] ) && $args['availability']['available'] && in_array( $type, self::$blocks_index ) ) {
register_block_type( 'jetpack/' . $type, $args['args'] );
}
Expand Down
12 changes: 7 additions & 5 deletions modules/simple-payments/simple-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ public function init_hook_action() {

add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 );

if ( $this->is_enabled_jetpack_simple_payments() ) {
jetpack_register_block( 'simple-payments' );
} else {
jetpack_register_block( 'simple-payments', array(), array( 'available' => false, 'unavailable_reason' => 'missing_plan' ) );
}
jetpack_register_block( 'simple-payments', array(), array( 'callback' => array( $this, 'get_block_availability' ) ) );
}

function get_block_availability() {
return $this->is_enabled_jetpack_simple_payments()
? array( 'available' => true )
: array( 'available' => false, 'unavailable_reason' => 'missing_plan' );
}

function remove_auto_paragraph_from_product_description( $content ) {
Expand Down

0 comments on commit 0625cf5

Please sign in to comment.