Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1010: [Gutenberg] Fix issues with rendering native blocks #1022

Merged
merged 5 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 17 additions & 16 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,22 +424,23 @@ function amp_get_content_embed_handlers( $post = null ) {
*/
return apply_filters( 'amp_content_embed_handlers',
array(
'AMP_Twitter_Embed_Handler' => array(),
'AMP_YouTube_Embed_Handler' => array(),
'AMP_DailyMotion_Embed_Handler' => array(),
'AMP_Vimeo_Embed_Handler' => array(),
'AMP_SoundCloud_Embed_Handler' => array(),
'AMP_Instagram_Embed_Handler' => array(),
'AMP_Issuu_Embed_Handler' => array(),
'AMP_Meetup_Embed_Handler' => array(),
'AMP_Vine_Embed_Handler' => array(),
'AMP_Facebook_Embed_Handler' => array(),
'AMP_Pinterest_Embed_Handler' => array(),
'AMP_Playlist_Embed_Handler' => array(),
'AMP_Reddit_Embed_Handler' => array(),
'AMP_Tumblr_Embed_Handler' => array(),
'AMP_Gallery_Embed_Handler' => array(),
'WPCOM_AMP_Polldaddy_Embed' => array(),
'AMP_Gutenberg_Categories_Handler' => array(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since “Gutenberg“ project name will not be carried over when merged into core, we should just call it AMP_Categories_Block_Handler.

'AMP_Twitter_Embed_Handler' => array(),
'AMP_YouTube_Embed_Handler' => array(),
'AMP_DailyMotion_Embed_Handler' => array(),
'AMP_Vimeo_Embed_Handler' => array(),
'AMP_SoundCloud_Embed_Handler' => array(),
'AMP_Instagram_Embed_Handler' => array(),
'AMP_Issuu_Embed_Handler' => array(),
'AMP_Meetup_Embed_Handler' => array(),
'AMP_Vine_Embed_Handler' => array(),
'AMP_Facebook_Embed_Handler' => array(),
'AMP_Pinterest_Embed_Handler' => array(),
'AMP_Playlist_Embed_Handler' => array(),
'AMP_Reddit_Embed_Handler' => array(),
'AMP_Tumblr_Embed_Handler' => array(),
'AMP_Gallery_Embed_Handler' => array(),
'WPCOM_AMP_Polldaddy_Embed' => array(),
),
$post
);
Expand Down
1 change: 1 addition & 0 deletions includes/class-amp-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class AMP_Autoloader {
'AMP_DailyMotion_Embed_Handler' => 'includes/embeds/class-amp-dailymotion-embed',
'AMP_Facebook_Embed_Handler' => 'includes/embeds/class-amp-facebook-embed',
'AMP_Gallery_Embed_Handler' => 'includes/embeds/class-amp-gallery-embed',
'AMP_Gutenberg_Categories_Handler' => 'includes/embeds/class-amp-gutenberg-categories-handler',
'AMP_Instagram_Embed_Handler' => 'includes/embeds/class-amp-instagram-embed',
'AMP_Issuu_Embed_Handler' => 'includes/embeds/class-amp-issuu-embed-handler',
'AMP_Meetup_Embed_Handler' => 'includes/embeds/class-amp-meetup-embed-handler',
Expand Down
98 changes: 98 additions & 0 deletions includes/embeds/class-amp-gutenberg-categories-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Class AMP_Gutenberg_Categories_Handler
*
* @package AMP
*/

/**
* Class AMP_Gutenberg_Categories_Handler
*
* @since 1.0
*/
class AMP_Gutenberg_Categories_Handler extends AMP_Base_Embed_Handler {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted above, the “Gutenberg” name is going to be dropped with the core merge so we can change it to just AMP_Categories_Block_Handler. But given that there is only one block that needs to be overridden, maybe it would be better to just call it AMP_Core_Block_Handler. This then can include the logic for the categories block and we can add support for any other core blocks that need special handling instead of creating a separate class for each, which I think is somewhat overkill.


/**
* Register embed.
*/
public function register_embed() {
if ( function_exists( 'gutenberg_init' ) ) {
add_action( 'the_post', array( $this, 'override_category_block_render_callback' ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs to wait for the_post action. I think it can just do $this-> override_category_block_render_callback()

}
}

/**
* Unregister embed.
*/
public function unregister_embed() {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should undo what is done in register_embed. The original render_callback for the core/categories block should be saved and then restored here.


/**
* Override the output of Gutenberg core/category block.
*/
public function override_category_block_render_callback() {
if ( is_amp_endpoint() ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that register_embed will have only been called if it is an AMP endpoint to begin with. So this override_category_block_render_callback method can actually be eliminated in favor of moving the logic to register_embed directly, minus the is_amp_endpoint check.

$registry = WP_Block_Type_Registry::get_instance();
$block = $registry->get_registered( 'core/categories' );
$block->render_callback = array( $this, 'render' );
}
}

/**
* Render Gutenberg block. This is essentially the same method as the original.
* Difference is excluding the disallowed JS script, adding <form> tags, and using on:change for <select>.
*
* @param array $attributes Attributes.
* @return string Rendered.
*/
public function render( $attributes ) {
static $block_id = 0;
$block_id++;

$align = 'center';
if ( isset( $attributes['align'] ) && in_array( $attributes['align'], array( 'left', 'right', 'full' ), true ) ) {
$align = $attributes['align'];
}

$args = array(
'echo' => false,
'hierarchical' => ! empty( $attributes['showHierarchy'] ),
'orderby' => 'name',
'show_count' => ! empty( $attributes['showPostCounts'] ),
'title_li' => '',
);

if ( ! empty( $attributes['displayAsDropdown'] ) ) {
$id = 'wp-block-categories-dropdown-' . $block_id;
$form_id = $id . '-form';
$args['id'] = $id;
$args['show_option_none'] = __( 'Select Category', 'amp' );
$wrapper_markup = '<div class="%1$s">%2$s</div>';
$items_markup = wp_dropdown_categories( $args );
$type = 'dropdown';

$items_markup = preg_replace(
'/(?<=<select\b)/',
sprintf( ' on="change:%s.submit"', esc_attr( $form_id ) ),
$items_markup,
1
);
} else {
$wrapper_markup = '<div class="%1$s"><ul>%2$s</ul></div>';
$items_markup = wp_list_categories( $args );
$type = 'list';
}

$class = "wp-block-categories wp-block-categories-{$type} align{$align}";

$block_content = sprintf(
$wrapper_markup,
esc_attr( $class ),
$items_markup
);

if ( ! empty( $attributes['displayAsDropdown'] ) ) {
$block_content = sprintf( '<form action="%s" method="get" target="_top" id="%s">%s</form>', esc_url( home_url() ), esc_attr( $form_id ), $block_content );
}
return $block_content;
}
}
48 changes: 48 additions & 0 deletions tests/test-class-amp-gutenberg-categories-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Tests for AMP_Gutenberg_Categories_Handler.
*
* @package AMP
* @since 1.0
*/

/**
* Tests for AMP_Gutenberg_Categories_Handler.
*
* @package AMP
* @covers AMP_Gutenberg_Categories_Handler
*/
class Test_AMP_Gutenberg_Categories_Handler extends WP_UnitTestCase {

/**
* Instance of the tested class.
*
* @var AMP_Gutenberg_Categories_Handler.
*/
public $instance;

/**
* Set up test.
*/
public function setUp() {
parent::setUp();
$this->instance = new AMP_Gutenberg_Categories_Handler();

// Require gutenberg file to be able to run the tests.
if ( file_exists( AMP__DIR__ . '/../gutenberg/gutenberg.php' ) ) {
require_once AMP__DIR__ . '/../gutenberg/gutenberg.php';
}
}

/**
* Test register_embed.
*
* @covers AMP_Gutenberg_Categories_Handler::register_embed()
*/
public function test_register_embed() {
if ( function_exists( 'gutenberg_init' ) ) {
$this->instance->register_embed();
$this->assertEquals( 10, has_action( 'the_post', array( $this->instance, 'override_category_block_render_callback' ) ) );
}
}
}