Skip to content

Commit

Permalink
Issue #25 - continue refactoring overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Nov 14, 2020
1 parent cc236c4 commit 96cac0e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 37 deletions.
23 changes: 23 additions & 0 deletions includes/block-override-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
44 changes: 7 additions & 37 deletions includes/block-overrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
*/


/**
Expand All @@ -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';
Expand Down Expand Up @@ -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.
Expand All @@ -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 = '<div class="wp-block-query-pagination">';
$html .= paginate_links( [ 'type' => 'list'] );
$html .= "</div>";
return $html;
}


/**
* Renders the `core/query-loop` block for the main query on the server.
Expand Down
36 changes: 36 additions & 0 deletions includes/query-pagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* 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;
}

/**
* 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 = '<div class="wp-block-query-pagination">';
$html .= paginate_links( [ 'type' => 'list'] );
$html .= "</div>";
return $html;
}

0 comments on commit 96cac0e

Please sign in to comment.