From 412e977f4773d8d116e37fa41150ef4fa7479066 Mon Sep 17 00:00:00 2001 From: Miina Sikk Date: Thu, 15 Mar 2018 21:08:30 +0200 Subject: [PATCH 1/5] Add handler for Gutenberg Categories block. --- includes/amp-helper-functions.php | 33 +++---- includes/class-amp-autoloader.php | 1 + ...class-amp-gutenberg-categories-handler.php | 95 +++++++++++++++++++ 3 files changed, 113 insertions(+), 16 deletions(-) create mode 100644 includes/embeds/class-amp-gutenberg-categories-handler.php diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 175bf72ff3a..07eb7fbc37c 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -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(), + '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 ); diff --git a/includes/class-amp-autoloader.php b/includes/class-amp-autoloader.php index 3ec923b7bde..f7c68c9f673 100644 --- a/includes/class-amp-autoloader.php +++ b/includes/class-amp-autoloader.php @@ -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', diff --git a/includes/embeds/class-amp-gutenberg-categories-handler.php b/includes/embeds/class-amp-gutenberg-categories-handler.php new file mode 100644 index 00000000000..a49dcd21d87 --- /dev/null +++ b/includes/embeds/class-amp-gutenberg-categories-handler.php @@ -0,0 +1,95 @@ +get_registered( 'core/categories' ); + $block->render_callback = array( $this, 'render' ); + } + } + + /** + * Render. + * + * @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', 'gutenberg' ); + $wrapper_markup = '
%2$s
'; + $items_markup = wp_dropdown_categories( $args ); + $type = 'dropdown'; + + $items_markup = preg_replace( + '/(?<=%s', esc_url( home_url() ), esc_attr( $form_id ), $block_content ); + } + return $block_content; + } +} From ba87e2014c13c1f144981dce605662c3e9d4089d Mon Sep 17 00:00:00 2001 From: Miina Sikk Date: Thu, 15 Mar 2018 21:20:23 +0200 Subject: [PATCH 2/5] Add better method comment. --- includes/embeds/class-amp-gutenberg-categories-handler.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/includes/embeds/class-amp-gutenberg-categories-handler.php b/includes/embeds/class-amp-gutenberg-categories-handler.php index a49dcd21d87..7e9b67ce761 100644 --- a/includes/embeds/class-amp-gutenberg-categories-handler.php +++ b/includes/embeds/class-amp-gutenberg-categories-handler.php @@ -36,7 +36,8 @@ public function override_category_block_render_callback() { } /** - * Render. + * Render Gutenberg block. This is essentially the same method as the original. + * Difference is excluding the disallowed JS script, adding
tags, and using on:change for