Skip to content

Commit

Permalink
Dynamically render the title for navigation-link blocks with classNam…
Browse files Browse the repository at this point in the history
…e _title #28
  • Loading branch information
bobbingwide committed Oct 13, 2024
1 parent 01760b8 commit 240eb43
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
17 changes: 7 additions & 10 deletions libs/class-wp-top12-block-data-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function get_block_field_methods() {
* @return null
*/
function get_render_method( $parsed_block ) {
bw_trace2();
//bw_trace2();
$className = bw_array_get( $parsed_block['attrs'], 'className', '' );
$render_method = null;
$block_field_methods = $this->get_block_field_methods();
Expand All @@ -92,15 +92,15 @@ function get_render_method( $parsed_block ) {
return $render_method;
}


/**
* Updates the parsed block with the required values.
*
* Note: For the wp-countup-js block we need to hook into the post rendering filter as well.
* @TODO Find out why this is necessary.
* This is because the updates to innerHTML and innerContent are not reflected
* in WP_Block's properties inner_html and inner_content.
* See also wp_top12_render_block_wpcountupjs()
*
* @param $parsed_block
*
* @return mixed
*/
function wp_top12_fiddle_parsed_block( $parsed_block, $new_data_end, $new_data_start ) {
Expand Down Expand Up @@ -133,13 +133,13 @@ function countup_total_downloads( $parsed_block ) {

function chart_plugins( $parsed_block ) {
$parsed_block['attrs']['content'] = $this->plugins_chart_content;
bw_trace2();
//bw_trace2();
return $parsed_block;
}

function chart_total_downloads( $parsed_block ) {
$parsed_block['attrs']['content'] = $this->total_downloads_chart_content;
bw_trace2();
//bw_trace2();
return $parsed_block;
}

Expand Down Expand Up @@ -172,9 +172,6 @@ function build_dynamic_chart_content() {
$total_downloads_array[] = 'Date,Total downloads (M)';
arsort( $total_downloads_array );
$this->total_downloads_chart_content = implode( "\n", $total_downloads_array );
//print_r( $plugins_array );
//arsort( $total_downloads_array );

}
}

}
25 changes: 22 additions & 3 deletions wp-top12.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ function wp_top12_plugin_loaded() {
add_action( 'oik_fields_loaded', 'wp_top12_oik_fields_loaded');
}


/**
* Registers all block assets so that they can be enqueued through the block editor
* in the corresponding context.
Expand All @@ -110,7 +109,7 @@ function wp_top12_block_init() {
$script_asset = require( $script_asset_path );
//bw_trace2( $script_asset );
//wp_top12_register_scripts();
//$script_asset['dependencies'][] = 'chartjs-script';
//$script_asset['dependencies'][] = 'dashicons';
wp_register_script(
'wp-top12-block-editor',
plugins_url( $index_js, __FILE__ ),
Expand Down Expand Up @@ -185,10 +184,11 @@ function wp_top12_oik_fields_loaded() {
bw_register_field_for_object_type( '_total_downloads', 'post' );
add_filter( 'render_block_data', 'wp_top12_render_block_data', 10, 3 );
add_filter( 'render_block_roelmagdaleno/wp-countup-js', 'wp_top12_render_block_wpcountupjs', 10, 3 );
add_filter( 'render_block_core/navigation-link', 'wp_top12_render_block_core_navigation_link', 10, 3 );
}

function wp_top12_render_block_data( $parsed_block, $source_block, $parent_block ) {

//bw_trace2( $parsed_block, "parsed_block", false );
switch ( $parsed_block['blockName'] ) {
case 'roelmagdaleno/wp-countup-js':
case 'oik-sb/chart':
Expand Down Expand Up @@ -220,6 +220,25 @@ function wp_top12_render_block_wpcountupjs( $block_content, $parsed_block, $bloc
return $block_content;
}

/**
* Renders any dynamic label.
*
* We use the className attr to indicate that the label should be replaced by the post's post_title.
*
* @param $block_content
* @param $parsed_block
* @param $block
* @return mixed
*/
function wp_top12_render_block_core_navigation_link( $block_content, $parsed_block, $block ) {
bw_trace2();
$className = bw_array_get( $parsed_block['attrs'], 'className', '' );
if ( '_title' === $className) {
$post = get_post( $parsed_block['attrs']['id'] );
$block_content = str_replace( $parsed_block['attrs']['label'], $post->post_title, $block_content );
}
return $block_content;
}

function wp_top12_block_data_renderer() {
static $wp_top12_block_data_renderer = null;
Expand Down

0 comments on commit 240eb43

Please sign in to comment.