Skip to content

Commit

Permalink
Merge pull request #2555 from ampproject/add/gutenberg-version-depend…
Browse files Browse the repository at this point in the history
…ency

Add requirement that Gutenberg 5.8 be active for Stories
  • Loading branch information
westonruter authored Jun 10, 2019
2 parents ca17450 + 893f70f commit 82894fb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
11 changes: 4 additions & 7 deletions .dev-lib
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ if [[ ! -z $TRAVIS ]]; then
fi

function after_wp_install {
echo -n "Installing Gutenberg..."
if [[ "$WP_VERSION" = "4.9" ]]; then
# The last version that supported WordPress 4.9:
gutenberg_plugin_svn_url=https://plugins.svn.wordpress.org/gutenberg/tags/4.9.0/
else
if [[ "$WP_VERSION" != "4.9" ]]; then
echo -n "Installing Gutenberg..."
gutenberg_plugin_svn_url=https://plugins.svn.wordpress.org/gutenberg/trunk/
svn export -q "$gutenberg_plugin_svn_url" "$WP_CORE_DIR/src/wp-content/plugins/gutenberg"
echo "done"
fi
svn export -q "$gutenberg_plugin_svn_url" "$WP_CORE_DIR/src/wp-content/plugins/gutenberg"
echo "done"

if [[ ! -z $INSTALL_PWA_PLUGIN ]]; then
echo -n "Installing PWA 0.2-alpha2..."
Expand Down
21 changes: 17 additions & 4 deletions includes/class-amp-story-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class AMP_Story_Post_Type {
*/
const POST_TYPE_SLUG = 'amp_story';

/**
* Minimum required version of Gutenberg required.
*
* @var string
*/
const REQUIRED_GUTENBERG_VERSION = '5.8';

/**
* The image size for the AMP story card, used in an embed and the Latest Stories block.
*
Expand Down Expand Up @@ -90,15 +97,21 @@ class AMP_Story_Post_Type {
/**
* Check if the required version of block capabilities available.
*
* Note that Gutenberg requires WordPress 5.0, so this check also accounts for that.
*
* @todo Eventually the Gutenberg requirement should be removed.
*
* @return bool Whether capabilities are available.
*/
public static function has_required_block_capabilities() {
if ( ! function_exists( 'register_block_type' ) ) {
if ( ! function_exists( 'register_block_type' ) || version_compare( get_bloginfo( 'version' ), '5.0', '<' ) ) {
return false;
}

// TODO: Require only the latest WordPress version itself, not the plugin.
return function_exists( 'gutenberg_pre_init' );
return (
( defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE )
||
( defined( 'GUTENBERG_VERSION' ) && version_compare( GUTENBERG_VERSION, self::REQUIRED_GUTENBERG_VERSION, '>=' ) )
);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions tests/test-class-amp-story-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ class AMP_Story_Post_Type_Test extends WP_UnitTestCase {
public function setUp() {
parent::setUp();

foreach ( WP_Block_Type_Registry::get_instance()->get_all_registered() as $block ) {
if ( 'amp/' === substr( $block->name, 0, 4 ) ) {
WP_Block_Type_Registry::get_instance()->unregister( $block->name );
if ( class_exists( 'WP_Block_Type_Registry' ) ) {
foreach ( WP_Block_Type_Registry::get_instance()->get_all_registered() as $block ) {
if ( 'amp/' === substr( $block->name, 0, 4 ) ) {
WP_Block_Type_Registry::get_instance()->unregister( $block->name );
}
}
}

Expand Down
12 changes: 4 additions & 8 deletions tests/test-class-amp-story-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class AMP_Story_Templates_Test extends WP_UnitTestCase {
public function setUp() {
parent::setUp();

if ( ! AMP_Story_Post_Type::has_required_block_capabilities() ) {
$this->markTestSkipped( 'The minimum requirements for running stories are not present, so skipping test.' );
}

foreach ( WP_Block_Type_Registry::get_instance()->get_all_registered() as $block ) {
if ( 'amp/' === substr( $block->name, 0, 4 ) ) {
WP_Block_Type_Registry::get_instance()->unregister( $block->name );
Expand All @@ -33,10 +37,6 @@ public function setUp() {
* @covers AMP_Story_Templates::init()
*/
public function test_init() {
if ( ! function_exists( 'register_block_type' ) ) {
$this->markTestSkipped( 'The function register_block_type() is not present, so the AMP Story post type was not registered.' );
}

$amp_story_templates = new AMP_Story_Templates();
$amp_story_templates->init();

Expand All @@ -53,10 +53,6 @@ public function test_init() {
* @covers AMP_Story_Templates::filter_user_has_cap()
*/
public function test_filter_user_has_cap() {
if ( ! function_exists( 'register_block_type' ) ) {
$this->markTestSkipped( 'The function register_block_type() is not present, so the AMP Story post type was not registered.' );
}

$story_id = $this->factory()->post->create( array( 'post_type' => AMP_Story_Post_Type::POST_TYPE_SLUG ) );
wp_set_object_terms( $story_id, AMP_Story_Templates::TEMPLATES_TERM, AMP_Story_Templates::TEMPLATES_TAXONOMY );

Expand Down
3 changes: 3 additions & 0 deletions tests/validation/test-class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,9 @@ public function test_source_comments() {
* @return array
*/
public function get_block_data() {
if ( ! class_exists( 'WP_Block_Type_Registry' ) || 1 ) {
$this->markTestSkipped( 'Gutenberg not active.' );
}
$latest_posts_block = WP_Block_Type_Registry::get_instance()->get_registered( 'core/latest-posts' );

$reflection_function = new ReflectionFunction( $latest_posts_block->render_callback );
Expand Down

0 comments on commit 82894fb

Please sign in to comment.