-
Notifications
You must be signed in to change notification settings - Fork 385
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
/** | ||
* Register embed. | ||
*/ | ||
public function register_embed() { | ||
if ( function_exists( 'gutenberg_init' ) ) { | ||
add_action( 'the_post', array( $this, 'override_category_block_render_callback' ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this needs to wait for |
||
} | ||
} | ||
|
||
/** | ||
* Unregister embed. | ||
*/ | ||
public function unregister_embed() {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should undo what is done in |
||
|
||
/** | ||
* Override the output of Gutenberg core/category block. | ||
*/ | ||
public function override_category_block_render_callback() { | ||
if ( is_amp_endpoint() ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that |
||
$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; | ||
} | ||
} |
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' ) ) ); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
.