From 96cac0e34cff870c67cdcb8871523f0e0a73233a Mon Sep 17 00:00:00 2001 From: Herb Miller Date: Sat, 14 Nov 2020 15:33:48 +0000 Subject: [PATCH] Issue #25 - continue refactoring overrides --- includes/block-override-functions.php | 23 ++++++++++++++ includes/block-overrides.php | 44 +++++---------------------- includes/query-pagination.php | 36 ++++++++++++++++++++++ 3 files changed, 66 insertions(+), 37 deletions(-) create mode 100644 includes/query-pagination.php diff --git a/includes/block-override-functions.php b/includes/block-override-functions.php index 413c633..a2a6b0a 100644 --- a/includes/block-override-functions.php +++ b/includes/block-override-functions.php @@ -4,6 +4,29 @@ * */ +/** + * Overrides a core block's render_callback method, if required. + * + * For the given blockname, if the overriding function is available + * and the current callback is the gutenberg function + * replace the render_callback with our own function. + * + * @param array $args Block attributes. + * @param string $blockname The block name to test for. + * @param string $render_callback The common suffix for the block's callback function. + * @return array Block attributes. + */ +function fizzie_maybe_override_block( $args, $blockname, $render_callback ) { + $fizzie_render_callback = 'fizzie_' . $render_callback; + if ( $blockname == $args['name'] && function_exists( $fizzie_render_callback ) ) { + if ( 'gutenberg_' . $render_callback == $args['render_callback'] ) { + $args['render_callback'] = $fizzie_render_callback; + } + } + return $args; +} + + /** * Determines whether or not to process this content. * diff --git a/includes/block-overrides.php b/includes/block-overrides.php index bb53bb5..9fd7db8 100644 --- a/includes/block-overrides.php +++ b/includes/block-overrides.php @@ -10,11 +10,8 @@ * Either comment out the ones that aren't needed any more * or find another way of detecting whether or not to include the file */ -//require_once __DIR__ . '/query-pagination.php'; +require_once __DIR__ . '/query-pagination.php'; -/* - * fizzie_maybe_override_block( 'core/query-pagination', 'gutenberg_render_block_core_query_pagination' ); - */ /** @@ -23,11 +20,15 @@ add_filter( 'register_block_type_args', 'fizzie_register_block_type_args', 9 ); function fizzie_register_block_type_args( $args ) { + $args = fizzie_maybe_override_block( $args,'core/query-pagination', 'render_block_core_query_pagination'); + $args = fizzie_maybe_override_block( $args,'core/query-loop', 'render_block_core_query_loop' ); + /* if ( 'core/query-pagination' == $args['name']) { if ( 'gutenberg_render_block_core_query_pagination' == $args['render_callback'] ) { $args['render_callback'] = 'fizzie_render_block_core_query_pagination'; } } + */ if ( 'core/query-loop' == $args['name'] ) { if ( 'gutenberg_render_block_core_query_loop' == $args['render_callback'] ) { $args['render_callback'] = 'fizzie_render_block_core_query_loop'; @@ -79,24 +80,7 @@ function fizzie_register_block_type_args( $args ) { return $args; } -/** - * Overrides core/query-pagination to implement main query pagination. - * - * Hack until a solution is delivered in Gutenberg. - * - * @param $attributes - * @param $content - * @param $block - * @return string - */ -function fizzie_render_block_core_query_pagination( $attributes, $content, $block ) { - if ( isset( $block->context['queryId'] ) ) { - $html = gutenberg_render_block_core_query_pagination( $attributes, $content, $block ); - } else { - $html = fizzie_render_block_core_query_pagination_main_query( $attributes, $content, $block ); - } - return $html; -} + /** * Overrides core/query-loop to implement main query processing. @@ -117,21 +101,7 @@ function fizzie_render_block_core_query_loop( $attributes, $content, $block ) { return $html; } -/** - * Renders the `core/query-pagination` block on the server for the main query. - * - * @param array $attributes Block attributes. - * @param string $content Block default content. - * @param WP_Block $block Block instance. - * - * @return string Returns the pagination for the query. - */ -function fizzie_render_block_core_query_pagination_main_query( $attributes, $content, $block ) { - $html = '
'; - $html .= paginate_links( [ 'type' => 'list'] ); - $html .= "
"; - return $html; -} + /** * Renders the `core/query-loop` block for the main query on the server. diff --git a/includes/query-pagination.php b/includes/query-pagination.php new file mode 100644 index 0000000..6b9cdf1 --- /dev/null +++ b/includes/query-pagination.php @@ -0,0 +1,36 @@ +context['queryId'] ) ) { + $html = gutenberg_render_block_core_query_pagination( $attributes, $content, $block ); + } else { + $html = fizzie_render_block_core_query_pagination_main_query( $attributes, $content, $block ); + } + return $html; +} + +/** + * Renders the `core/query-pagination` block on the server for the main query. + * + * @param array $attributes Block attributes. + * @param string $content Block default content. + * @param WP_Block $block Block instance. + * + * @return string Returns the pagination for the query. + */ +function fizzie_render_block_core_query_pagination_main_query( $attributes, $content, $block ) { + $html = '
'; + $html .= paginate_links( [ 'type' => 'list'] ); + $html .= "
"; + return $html; +}