From 9fcf30892d0bfe0a194e45d6e9f30f362902bad0 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Tue, 11 Sep 2018 17:32:23 +0300 Subject: [PATCH 01/23] Add server rendering for Related Posts block --- .../related-posts/jetpack-related-posts.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/modules/related-posts/jetpack-related-posts.php b/modules/related-posts/jetpack-related-posts.php index fcbe23e771f0b..ab57af5fb0ec1 100644 --- a/modules/related-posts/jetpack-related-posts.php +++ b/modules/related-posts/jetpack-related-posts.php @@ -89,6 +89,44 @@ public function __construct( $blog_id_local, $blog_id_wpcom ) { * ================= */ + /** + * Register the Related Posts Gutenberg block on server. + * + * @action init + * @uses add_settings_field, __, register_setting, add_action + * @return null + */ + public function register_block() { + register_block_type( + 'jetpack/related-posts', + array( + 'render_callback' => array( $this, 'render_block' ), + ) + ); + } + + public function render_block( $attributes ) { + $shortcode_atts = array( + 'enabled' => true, + 'show_headline' => isset( $attributes['headline'] ) && strlen( $attributes['headline'] ) > 0, + 'show_thumbnails' => ! empty( $attributes['displayThumbnails'] ), + 'show_date' => ! empty( $attributes['displayDate'] ), + 'show_context' => ! empty( $attributes['displayContext'] ), + 'layout' => ! empty( $attributes['postLayout'] ) ? $attributes['postLayout'] : 'grid', + 'headline' => isset( $attributes['headline'] ) ? $attributes['headline'] : '', + 'size' => ! empty( $attributes['postsToShow'] ) ? $attributes['postsToShow'] : 3, + ); + + $parsed_atts = array(); + foreach ( $shortcode_atts as $attribute => $value ) { + $parsed_atts[] = $attribute . '="' . esc_attr( $value ) . '"'; + } + + $string_atts = implode( ' ', $parsed_atts ); + + return '[' . self::SHORTCODE . ' ' . $string_atts . ']'; + } + /** * Add a checkbox field to Settings > Reading for enabling related posts. * From d88aaf7c62c9b51974c2fd4bb5bd7d60c5929b1f Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Wed, 12 Sep 2018 12:28:23 +0300 Subject: [PATCH 02/23] Setting block attributes properly --- .../related-posts/jetpack-related-posts.php | 105 +++++++++++------- 1 file changed, 67 insertions(+), 38 deletions(-) diff --git a/modules/related-posts/jetpack-related-posts.php b/modules/related-posts/jetpack-related-posts.php index ab57af5fb0ec1..c878b52de7dd2 100644 --- a/modules/related-posts/jetpack-related-posts.php +++ b/modules/related-posts/jetpack-related-posts.php @@ -53,6 +53,7 @@ public static function init_raw() { protected $_convert_charset; protected $_previous_post_id; protected $_found_shortcode = false; + protected $block_attributes = array(); /** * Constructor for Jetpack_RelatedPosts. @@ -89,44 +90,6 @@ public function __construct( $blog_id_local, $blog_id_wpcom ) { * ================= */ - /** - * Register the Related Posts Gutenberg block on server. - * - * @action init - * @uses add_settings_field, __, register_setting, add_action - * @return null - */ - public function register_block() { - register_block_type( - 'jetpack/related-posts', - array( - 'render_callback' => array( $this, 'render_block' ), - ) - ); - } - - public function render_block( $attributes ) { - $shortcode_atts = array( - 'enabled' => true, - 'show_headline' => isset( $attributes['headline'] ) && strlen( $attributes['headline'] ) > 0, - 'show_thumbnails' => ! empty( $attributes['displayThumbnails'] ), - 'show_date' => ! empty( $attributes['displayDate'] ), - 'show_context' => ! empty( $attributes['displayContext'] ), - 'layout' => ! empty( $attributes['postLayout'] ) ? $attributes['postLayout'] : 'grid', - 'headline' => isset( $attributes['headline'] ) ? $attributes['headline'] : '', - 'size' => ! empty( $attributes['postsToShow'] ) ? $attributes['postsToShow'] : 3, - ); - - $parsed_atts = array(); - foreach ( $shortcode_atts as $attribute => $value ) { - $parsed_atts[] = $attribute . '="' . esc_attr( $value ) . '"'; - } - - $string_atts = implode( ' ', $parsed_atts ); - - return '[' . self::SHORTCODE . ' ' . $string_atts . ']'; - } - /** * Add a checkbox field to Settings > Reading for enabling related posts. * @@ -286,6 +249,72 @@ public function get_target_html_unsupported() { return "\n\n\n\n"; } + /** + * =============== + * GUTENBERG BLOCK + * =============== + */ + + /** + * Register the Related Posts Gutenberg block on server. + * + * @action init + * @uses register_block_type + * @return null + */ + public function register_block() { + register_block_type( + 'a8c/related-posts', + array( + 'render_callback' => array( $this, 'render_block' ), + ) + ); + } + + /** + * Overwrite the current related posts options and render the related posts shortcode. + * + * @return string + */ + public function render_block( $attributes ) { + $headline = isset( $attributes['headline'] ) ? $attributes['headline'] : esc_html__( 'Related', 'jetpack' ); + $block_attributes = array( + 'enabled' => true, + 'show_headline' => strlen( $headline ) > 0, + 'show_thumbnails' => isset( $attributes['displayThumbnails'] ) ? (bool) $attributes['displayThumbnails'] : false, + 'show_date' => isset( $attributes['displayDate'] ) ? (bool) $attributes['displayDate'] : true, + 'show_context' => isset( $attributes['displayContext'] ) ? (bool) $attributes['displayContext'] : true, + 'layout' => ! empty( $attributes['postLayout'] ) ? $attributes['postLayout'] : 'grid', + 'headline' => $headline, + 'size' => ! empty( $attributes['postsToShow'] ) ? absint( $attributes['postsToShow'] ) : 3, + ); + + $this->set_block_attributes( $block_attributes ); + + add_filter( 'jetpack_relatedposts_filter_options', array( $this, 'get_block_attributes' ) ); + + return '[' . self::SHORTCODE . ']'; + } + + /** + * Returns the current Gutenberg block attributes + * + * @return array + */ + public function get_block_attributes() { + return $this->block_attributes; + } + + /** + * Updates the current Gutenberg block attributes + * + * @param $attributes string + * @return void + */ + public function set_block_attributes( $attributes = array() ) { + $this->block_attributes = $attributes; + } + /** * ======================== * PUBLIC UTILITY FUNCTIONS From 2222f298e859b31c6228158757a47e88d3b98d67 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Wed, 12 Sep 2018 13:07:33 +0300 Subject: [PATCH 03/23] Use a custom block rendering markup --- .../related-posts/jetpack-related-posts.php | 109 +++++++++++++----- 1 file changed, 81 insertions(+), 28 deletions(-) diff --git a/modules/related-posts/jetpack-related-posts.php b/modules/related-posts/jetpack-related-posts.php index c878b52de7dd2..d3b5a21cda4c9 100644 --- a/modules/related-posts/jetpack-related-posts.php +++ b/modules/related-posts/jetpack-related-posts.php @@ -53,7 +53,6 @@ public static function init_raw() { protected $_convert_charset; protected $_previous_post_id; protected $_found_shortcode = false; - protected $block_attributes = array(); /** * Constructor for Jetpack_RelatedPosts. @@ -272,47 +271,101 @@ public function register_block() { } /** - * Overwrite the current related posts options and render the related posts shortcode. + * Render the related posts markup. * * @return string */ public function render_block( $attributes ) { - $headline = isset( $attributes['headline'] ) ? $attributes['headline'] : esc_html__( 'Related', 'jetpack' ); + $headline = isset( $attributes['headline'] ) ? $attributes['headline'] : __( 'Related', 'jetpack' ); $block_attributes = array( - 'enabled' => true, - 'show_headline' => strlen( $headline ) > 0, 'show_thumbnails' => isset( $attributes['displayThumbnails'] ) ? (bool) $attributes['displayThumbnails'] : false, 'show_date' => isset( $attributes['displayDate'] ) ? (bool) $attributes['displayDate'] : true, 'show_context' => isset( $attributes['displayContext'] ) ? (bool) $attributes['displayContext'] : true, - 'layout' => ! empty( $attributes['postLayout'] ) ? $attributes['postLayout'] : 'grid', + 'layout' => isset( $attributes['postLayout'] ) && $attributes['postLayout'] === 'list' ? $attributes['postLayout'] : 'grid', 'headline' => $headline, 'size' => ! empty( $attributes['postsToShow'] ) ? absint( $attributes['postsToShow'] ) : 3, ); - $this->set_block_attributes( $block_attributes ); - - add_filter( 'jetpack_relatedposts_filter_options', array( $this, 'get_block_attributes' ) ); - - return '[' . self::SHORTCODE . ']'; - } + $related_posts = $this->get_for_post_id( get_the_ID(), array( + 'size' => $block_attributes['size'], + ) ); - /** - * Returns the current Gutenberg block attributes - * - * @return array - */ - public function get_block_attributes() { - return $this->block_attributes; - } + if ( ! $related_posts ) { + return ''; + } - /** - * Updates the current Gutenberg block attributes - * - * @param $attributes string - * @return void - */ - public function set_block_attributes( $attributes = array() ) { - $this->block_attributes = $attributes; + ob_start(); + ?> + + Date: Wed, 12 Sep 2018 13:09:00 +0300 Subject: [PATCH 04/23] Add some semicolons --- modules/related-posts/jetpack-related-posts.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/related-posts/jetpack-related-posts.php b/modules/related-posts/jetpack-related-posts.php index d3b5a21cda4c9..c718a099ebef8 100644 --- a/modules/related-posts/jetpack-related-posts.php +++ b/modules/related-posts/jetpack-related-posts.php @@ -318,16 +318,16 @@ class="" > @@ -335,8 +335,8 @@ class=""